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