blob: a615a4f60c59188e6fb7016227e5af313361593f [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2012 Daniel Drown
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * getroute.c - get an ip route
17 */
18#include <string.h>
19#include <errno.h>
20
21#include <sys/socket.h>
22#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24#include <arpa/inet.h>
25
26#include <netlink/handlers.h>
27#include <netlink/msg.h>
28
29#include "getroute.h"
30#include "netlink_callbacks.h"
31#include "netlink_msg.h"
32
33/* function: get_default_route_cb
34 * finds the default route with the request family and out interface and saves the gateway
35 * msg - netlink message
36 * data - (struct default_route_data) requested filters and response storage
37 */
38static int get_default_route_cb(struct nl_msg *msg, void *data) {
39 struct rtmsg *rt_p;
40 struct rtattr *rta_p;
41 int rta_len;
42 struct default_route_data *default_route = data;
43 union anyip *this_gateway = NULL;
44 ssize_t this_gateway_size;
45 int this_interface_id = -1;
46
47 if(default_route->reply_found_route) { // we already found our route
48 return NL_OK;
49 }
50
51 rt_p = (struct rtmsg *)nlmsg_data(nlmsg_hdr(msg));
52 if(rt_p->rtm_dst_len != 0) { // not a default route
53 return NL_OK;
54 }
55 if((rt_p->rtm_family != default_route->request_family) || (rt_p->rtm_table != RT_TABLE_MAIN)) { // not a route we care about
56 return NL_OK;
57 }
58
59 rta_p = (struct rtattr *)RTM_RTA(rt_p);
60 rta_len = RTM_PAYLOAD(nlmsg_hdr(msg));
61 for(; RTA_OK(rta_p, rta_len); rta_p = RTA_NEXT(rta_p, rta_len)) {
62 switch(rta_p->rta_type) {
63 case RTA_GATEWAY:
64 this_gateway = RTA_DATA(rta_p);
65 this_gateway_size = RTA_PAYLOAD(rta_p);
66 break;
67 case RTA_OIF:
68 this_interface_id = *(int *)RTA_DATA(rta_p);
69 break;
70 default:
71 break;
72 }
73 }
74
75 if(this_interface_id == default_route->request_interface_id) {
76 default_route->reply_found_route = 1;
77 if(this_gateway != NULL) {
78 memcpy(&default_route->reply_gateway, this_gateway, this_gateway_size);
79 default_route->reply_has_gateway = 1;
80 } else {
81 default_route->reply_has_gateway = 0;
82 }
83 }
84 return NL_OK;
85}
86
87/* function: error_handler
88 * error callback for get_default_route
89 * nla - where the message came from
90 * err - netlink message
91 * arg - (int *) storage for the error number
92 */
Lorenzo Colitti56ec1612014-03-10 16:33:22 +090093static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla,
94 struct nlmsgerr *err, void *arg) {
Daniel Drowna45056e2012-03-23 10:42:54 -050095 int *retval = arg;
96 if(err->error < 0) { // error_handler called even on no error (NLMSG_ERROR reply type used)
97 *retval = err->error;
98 }
99 return NL_OK;
100}
101
102/* function: get_default_route
103 * finds the first default route with the given family and interface, returns the gateway (if it exists) in the struct
104 * default_route - requested family and interface, and response storage
105 */
106int get_default_route(struct default_route_data *default_route) {
107 struct rtmsg msg;
108 struct nl_cb *callbacks = NULL;
109 struct nl_msg *nlmsg = NULL;
110 int retval = 0;
111
112 default_route->reply_has_gateway = 0;
113 default_route->reply_found_route = 0;
114
115 memset(&msg,'\0',sizeof(msg));
116 msg.rtm_family = default_route->request_family;
117 msg.rtm_table = RT_TABLE_MAIN;
118 msg.rtm_protocol = RTPROT_KERNEL;
119 msg.rtm_scope = RT_SCOPE_UNIVERSE;
120
121 callbacks = nl_cb_alloc(NL_CB_DEFAULT);
122 if(!callbacks) {
123 retval = -ENOMEM;
124 goto cleanup;
125 }
126 // get_default_route_cb sets the response fields in default_route
127 nl_cb_set(callbacks, NL_CB_VALID, NL_CB_CUSTOM, get_default_route_cb, default_route);
128 nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, &retval);
129
130 nlmsg = nlmsg_alloc_rtmsg(RTM_GETROUTE, NLM_F_REQUEST | NLM_F_ROOT, &msg);
131 if(!nlmsg) {
132 retval = -ENOMEM;
133 goto cleanup;
134 }
135 send_netlink_msg(nlmsg, callbacks);
136
137cleanup:
138 if(callbacks)
139 nl_cb_put(callbacks);
140 if(nlmsg)
141 nlmsg_free(nlmsg);
142
143 return retval;
144}