• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/relayd/route.c

  1 /*
  2  *   Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
  3  *
  4  *   This program is free software; you can redistribute it and/or modify
  5  *   it under the terms of the GNU General Public License v2 as published by
  6  *   the Free Software Foundation.
  7  *
  8  *   This program is distributed in the hope that it will be useful,
  9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  *   GNU General Public License for more details.
 12  *
 13  *   You should have received a copy of the GNU General Public License
 14  *   along with this program; if not, write to the Free Software
 15  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 16  */
 17 
 18 #include <sys/socket.h>
 19 #include <stdio.h>
 20 #include <string.h>
 21 #include <errno.h>
 22 #include <unistd.h>
 23 #include <fcntl.h>
 24 #include <time.h>
 25 
 26 #include <linux/fib_rules.h>
 27 
 28 #include "relayd.h"
 29 
 30 #define NLMSG_ALIGNTO        4U
 31 #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
 32 
 33 static struct uloop_fd rtnl_sock;
 34 static unsigned int rtnl_seq, rtnl_dump_seq;
 35 int route_table = 16800;
 36 
 37 static void rtnl_flush(void)
 38 {
 39         ssize_t ret;
 40         int fd;
 41 
 42         fd = open("/proc/sys/net/ipv4/route/flush", O_WRONLY);
 43         if (fd < 0)
 44                 return;
 45 
 46         ret = write(fd, "-1", 2);
 47         if (ret != 2)
 48                 perror("write");
 49         close(fd);
 50 }
 51 
 52 enum {
 53         RULE_F_ADD = (1 << 0),
 54         RULE_F_DEFGW_WORKAROUND = (1 << 1),
 55 };
 56 
 57 static int get_route_table(struct relayd_interface *rif)
 58 {
 59         if (rif)
 60                 return rif->rt_table;
 61         else
 62                 return local_route_table;
 63 }
 64 
 65 static void
 66 rtnl_rule_request(struct relayd_interface *rif, int flags)
 67 {
 68         struct {
 69                 struct nlmsghdr nl;
 70                 struct rtmsg rt;
 71                 struct {
 72                         struct rtattr rta;
 73                         int table;
 74                 } __packed table;
 75                 struct {
 76                         struct rtattr rta;
 77                         int prio;
 78                 } __packed prio;
 79                 struct {
 80                         struct rtattr rta;
 81                         char ifname[IFNAMSIZ + 1];
 82                 } __packed dev;
 83         } __packed req = {
 84                 .rt = {
 85                         .rtm_family = AF_INET,
 86                         .rtm_table = RT_TABLE_UNSPEC,
 87                         .rtm_scope = RT_SCOPE_UNIVERSE,
 88                         .rtm_protocol = RTPROT_BOOT,
 89                 },
 90                 .prio = {
 91                         .rta.rta_type = FRA_PRIORITY,
 92                         .rta.rta_len = sizeof(req.prio),
 93                         .prio = 2,
 94                 },
 95                 .table.rta = {
 96                         .rta_type = FRA_TABLE,
 97                         .rta_len = sizeof(req.table),
 98                 },
 99         };
100         const char *ifname = "lo";
101         int padding = sizeof(req.dev.ifname);
102 
103         if (rif)
104                 ifname = rif->ifname;
105 
106         if (!(flags & RULE_F_DEFGW_WORKAROUND)) {
107                 int len = strlen(ifname) + 1;
108                 req.dev.rta.rta_type = FRA_IFNAME;
109                 padding -= NLMSG_ALIGN(len);
110                 strcpy(req.dev.ifname, ifname);
111                 req.dev.rta.rta_len = sizeof(req.dev.rta) + len;
112         } else {
113                 padding = sizeof(req.dev);
114                 req.prio.prio--;
115         }
116         req.table.table = get_route_table(rif);
117         req.nl.nlmsg_len = sizeof(req) - padding;
118 
119         req.nl.nlmsg_flags = NLM_F_REQUEST;
120         if (flags & RULE_F_ADD) {
121                 req.nl.nlmsg_type = RTM_NEWRULE;
122                 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
123 
124                 req.rt.rtm_type = RTN_UNICAST;
125         } else {
126                 req.nl.nlmsg_type = RTM_DELRULE;
127                 req.rt.rtm_type = RTN_UNSPEC;
128         }
129 
130         send(rtnl_sock.fd, &req, req.nl.nlmsg_len, 0);
131         rtnl_flush();
132 }
133 
134 struct rtnl_addr {
135         struct rtattr rta;
136         uint8_t ipaddr[4];
137 } __packed;
138 
139 static struct rtnl_addr *
140 rtnl_add_addr(struct rtnl_addr *addr, int *len, int type, const uint8_t *ipaddr)
141 {
142         addr->rta.rta_type = type;
143         memcpy(addr->ipaddr, ipaddr, 4);
144         *len += sizeof(*addr);
145         return addr + 1;
146 }
147 
148 static void
149 rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
150                    struct relayd_route *route, bool add)
151 {
152         static struct {
153                 struct nlmsghdr nl;
154                 struct rtmsg rt;
155                 struct {
156                         struct rtattr rta;
157                         int table;
158                 } __packed table;
159                 struct {
160                         struct rtattr rta;
161                         int ifindex;
162                 } __packed dev;
163                 struct rtnl_addr addr[3];
164         } __packed req = {
165                 .rt = {
166                         .rtm_family = AF_INET,
167                         .rtm_dst_len = 32,
168                         .rtm_table = RT_TABLE_MAIN,
169                 },
170                 .table.rta = {
171                         .rta_type = RTA_TABLE,
172                         .rta_len = sizeof(req.table),
173                 },
174                 .dev.rta = {
175                         .rta_type = RTA_OIF,
176                         .rta_len = sizeof(req.dev),
177                 },
178                 .addr[0].rta.rta_len = sizeof(struct rtnl_addr),
179                 .addr[1].rta.rta_len = sizeof(struct rtnl_addr),
180                 .addr[2].rta.rta_len = sizeof(struct rtnl_addr),
181         };
182         int pktlen = sizeof(req) - sizeof(req.addr);
183         struct rtnl_addr *addr = &req.addr[0];
184         const char *ifname = "loopback";
185 
186         req.dev.ifindex = host->rif->sll.sll_ifindex;
187         req.table.table = get_route_table(rif);
188 
189         req.nl.nlmsg_flags = NLM_F_REQUEST;
190         if (add) {
191                 req.nl.nlmsg_type = RTM_NEWROUTE;
192                 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
193 
194                 req.rt.rtm_protocol = RTPROT_BOOT;
195                 if (route) {
196                         req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
197                 } else {
198                         req.rt.rtm_scope = RT_SCOPE_LINK;
199                 }
200                 req.rt.rtm_type = RTN_UNICAST;
201         } else {
202                 req.nl.nlmsg_type = RTM_DELROUTE;
203                 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
204         }
205 
206         if (rif)
207                 ifname = rif->ifname;
208 
209         if (route) {
210                 DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT" (%s)\n", ifname,
211                         IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr),
212                         host->rif->ifname);
213 
214                 req.rt.rtm_dst_len = route->mask;
215                 if (route->mask)
216                         addr = rtnl_add_addr(addr, &pktlen, RTA_DST, route->dest);
217                 addr = rtnl_add_addr(addr, &pktlen, RTA_GATEWAY, host->ipaddr);
218         } else {
219                 DPRINTF(2, "%s: add host route to "IP_FMT" (%s)\n", ifname,
220                         IP_BUF(host->ipaddr), host->rif->ifname);
221                 addr = rtnl_add_addr(addr, &pktlen, RTA_DST, host->ipaddr);
222                 req.rt.rtm_dst_len = 32;
223         }
224 
225         /* local route */
226         if (!rif)
227                 addr = rtnl_add_addr(addr, &pktlen, RTA_PREFSRC, local_addr);
228 
229         req.nl.nlmsg_len = pktlen;
230         if (route)
231                 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND | RULE_F_ADD);
232         send(rtnl_sock.fd, &req, pktlen, 0);
233         if (route)
234                 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND);
235         rtnl_flush();
236 }
237 
238 void
239 rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
240 {
241         struct relayd_interface *rif;
242 
243         list_for_each_entry(rif, &interfaces, list) {
244                 if (rif == host->rif)
245                         continue;
246 
247                 rtnl_route_request(rif, host, route, add);
248         }
249         if (local_route_table)
250                 rtnl_route_request(NULL, host, route, add);
251 }
252 
253 void relayd_add_interface_routes(struct relayd_interface *rif)
254 {
255         rif->rt_table = route_table++;
256         rtnl_rule_request(rif, RULE_F_ADD);
257 }
258 
259 void relayd_del_interface_routes(struct relayd_interface *rif)
260 {
261         rtnl_rule_request(rif, 0);
262 }
263 
264 #ifndef NDA_RTA
265 #define NDA_RTA(r) \
266     ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
267 #endif
268 
269 static void rtnl_parse_newneigh(struct nlmsghdr *h)
270 {
271         struct relayd_interface *rif = NULL;
272         struct ndmsg *r = NLMSG_DATA(h);
273         const uint8_t *lladdr = NULL;
274         const uint8_t *ipaddr = NULL;
275         struct rtattr *rta;
276         int len;
277 
278         if (r->ndm_family != AF_INET)
279                 return;
280 
281         list_for_each_entry(rif, &interfaces, list) {
282                 if (rif->sll.sll_ifindex == r->ndm_ifindex)
283                         goto found_interface;
284         }
285         return;
286 
287 found_interface:
288         len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
289         for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
290                 switch(rta->rta_type) {
291                 case NDA_LLADDR:
292                         lladdr = RTA_DATA(rta);
293                         break;
294                 case NDA_DST:
295                         ipaddr = RTA_DATA(rta);
296                         break;
297                 default:
298                         break;
299                 }
300         }
301 
302         if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
303                 return;
304 
305         if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
306                 return;
307 
308         DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
309                 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
310         relayd_refresh_host(rif, lladdr, ipaddr);
311 }
312 
313 static void rtnl_parse_packet(void *data, int len)
314 {
315         struct nlmsghdr *h;
316 
317         for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
318                 if (h->nlmsg_type == NLMSG_DONE ||
319                     h->nlmsg_type == NLMSG_ERROR)
320                         return;
321 
322                 if (h->nlmsg_seq != rtnl_dump_seq)
323                         continue;
324 
325                 if (h->nlmsg_type == RTM_NEWNEIGH)
326                         rtnl_parse_newneigh(h);
327         }
328 }
329 
330 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
331 {
332         struct sockaddr_nl nladdr;
333         static uint8_t buf[16384];
334         struct iovec iov = {
335                 .iov_base = buf,
336                 .iov_len = sizeof(buf),
337         };
338         struct msghdr msg = {
339                 .msg_name = &nladdr,
340                 .msg_namelen = sizeof(nladdr),
341                 .msg_iov = &iov,
342                 .msg_iovlen = 1,
343         };
344 
345         do {
346                 int len;
347 
348                 len = recvmsg(rtnl_sock.fd, &msg, 0);
349                 if (len < 0) {
350                         if (errno == EINTR)
351                                 continue;
352 
353                         return;
354                 }
355 
356                 if (!len)
357                         break;
358 
359                 if (nladdr.nl_pid != 0)
360                         continue;
361 
362                 rtnl_parse_packet(buf, len);
363         } while (1);
364 }
365 
366 static void rtnl_dump_request(int nlmsg_type)
367 {
368         static struct {
369                 struct nlmsghdr nlh;
370                 struct rtgenmsg g;
371         } req = {
372                 .nlh = {
373                         .nlmsg_len = sizeof(req),
374                         .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
375                         .nlmsg_pid = 0,
376                 },
377                 .g.rtgen_family = AF_INET,
378         };
379         req.nlh.nlmsg_type = nlmsg_type;
380         req.nlh.nlmsg_seq = rtnl_seq;
381         send(rtnl_sock.fd, &req, sizeof(req), 0);
382         rtnl_seq++;
383 }
384 
385 int relayd_rtnl_init(void)
386 {
387         struct sockaddr_nl snl_local = {};
388 
389         rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
390         if (rtnl_sock.fd < 0) {
391                 perror("socket(AF_NETLINK)");
392                 return -1;
393         }
394 
395         snl_local.nl_family = AF_NETLINK;
396 
397         if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
398                 perror("bind");
399                 close(rtnl_sock.fd);
400                 return -1;
401         }
402 
403         rtnl_sock.cb = rtnl_cb;
404         uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
405 
406         rtnl_seq = time(NULL);
407         rtnl_dump_seq = rtnl_seq;
408         rtnl_dump_request(RTM_GETNEIGH);
409         rtnl_rule_request(NULL, RULE_F_ADD);
410 
411         return 0;
412 }
413 
414 void relayd_rtnl_done(void)
415 {
416         rtnl_rule_request(NULL, 0);
417         uloop_fd_delete(&rtnl_sock);
418         close(rtnl_sock.fd);
419 }
420 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt