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