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