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