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