ndp: fixup route replication
[project/odhcpd.git] / src / ndp.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.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 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <errno.h>
19
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <arpa/inet.h>
23 #include <sys/socket.h>
24 #include <net/ethernet.h>
25 #include <netinet/ip6.h>
26 #include <netinet/icmp6.h>
27 #include <netpacket/packet.h>
28
29 #include <linux/rtnetlink.h>
30 #include <linux/filter.h>
31 #include "router.h"
32 #include "ndp.h"
33
34
35
36 static void handle_solicit(void *addr, void *data, size_t len,
37 struct interface *iface, void *dest);
38 static void handle_rtnetlink(void *addr, void *data, size_t len,
39 struct interface *iface, void *dest);
40 static ssize_t ping6(struct in6_addr *addr,
41 const struct interface *iface);
42
43 static uint32_t rtnl_seqid = 0;
44 static int ping_socket = -1;
45 static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink};
46
47
48 // Filter ICMPv6 messages of type neighbor soliciation
49 static struct sock_filter bpf[] = {
50 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
51 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
52 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
53 offsetof(struct icmp6_hdr, icmp6_type)),
54 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
55 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
56 BPF_STMT(BPF_RET | BPF_K, 0),
57 };
58 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
59
60
61 // Initialize NDP-proxy
62 int init_ndp(void)
63 {
64 // Setup netlink socket
65 if ((rtnl_event.uloop.fd = odhcpd_open_rtnl()) < 0)
66 return -1;
67
68 // Receive netlink neighbor and ip-address events
69 uint32_t group = RTNLGRP_IPV6_IFADDR;
70 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
71 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
72 group = RTNLGRP_IPV6_ROUTE;
73 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
74 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
75
76 odhcpd_register(&rtnl_event);
77
78 // Open ICMPv6 socket
79 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
80 if (ping_socket < 0) {
81 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
82 return -1;
83 }
84
85 int val = 2;
86 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
87
88 // This is required by RFC 4861
89 val = 255;
90 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
91 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
92
93 // Filter all packages, we only want to send
94 struct icmp6_filter filt;
95 ICMP6_FILTER_SETBLOCKALL(&filt);
96 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
97
98
99 // Netlink socket, continued...
100 group = RTNLGRP_NEIGH;
101 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
102
103 return 0;
104 }
105
106
107 static void dump_neigh_table(bool proxy)
108 {
109 struct {
110 struct nlmsghdr nh;
111 struct ndmsg ndm;
112 } req = {
113 {sizeof(req), RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP,
114 ++rtnl_seqid, 0},
115 {.ndm_family = AF_INET6, .ndm_flags = (proxy) ? NTF_PROXY : 0}
116 };
117 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
118 odhcpd_process(&rtnl_event);
119 }
120
121
122 int setup_ndp_interface(struct interface *iface, bool enable)
123 {
124 char procbuf[64];
125 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
126 int procfd = open(procbuf, O_WRONLY);
127 bool dump_neigh = false;
128
129 if (iface->ndp_event.uloop.fd > 0) {
130 uloop_fd_delete(&iface->ndp_event.uloop);
131 close(iface->ndp_event.uloop.fd);
132 iface->ndp_event.uloop.fd = -1;
133
134 if (!enable || iface->ndp != RELAYD_RELAY)
135 if (write(procfd, "0\n", 2) < 0) {}
136
137 dump_neigh = true;
138 }
139
140 if (enable && (iface->ra == RELAYD_SERVER ||
141 iface->dhcpv6 == RELAYD_SERVER || iface->ndp == RELAYD_RELAY)) {
142 // Synthesize initial address events
143 struct {
144 struct nlmsghdr nh;
145 struct ifaddrmsg ifa;
146 } req2 = {
147 {sizeof(req2), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
148 ++rtnl_seqid, 0},
149 {.ifa_family = AF_INET6, .ifa_index = iface->ifindex}
150 };
151 send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT);
152 }
153
154 if (enable && iface->ndp == RELAYD_RELAY) {
155 if (write(procfd, "1\n", 2) < 0) {}
156 close(procfd);
157
158 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
159 if (sock < 0) {
160 syslog(LOG_ERR, "Unable to open packet socket: %s",
161 strerror(errno));
162 return -1;
163 }
164
165 #ifdef PACKET_RECV_TYPE
166 int pktt = 1 << PACKET_MULTICAST;
167 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
168 #endif
169
170 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
171 &bpf_prog, sizeof(bpf_prog))) {
172 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
173 return -1;
174 }
175
176 struct sockaddr_ll ll = {
177 .sll_family = AF_PACKET,
178 .sll_ifindex = iface->ifindex,
179 .sll_protocol = htons(ETH_P_IPV6),
180 .sll_hatype = 0,
181 .sll_pkttype = 0,
182 .sll_halen = 0,
183 .sll_addr = {0},
184 };
185 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
186
187 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
188 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
189
190 iface->ndp_event.uloop.fd = sock;
191 iface->ndp_event.handle_dgram = handle_solicit;
192 odhcpd_register(&iface->ndp_event);
193
194 // If we already were enabled dump is unnecessary, if not do dump
195 if (!dump_neigh)
196 dump_neigh_table(false);
197 else
198 dump_neigh = false;
199 } else {
200 close(procfd);
201 }
202
203 if (dump_neigh)
204 dump_neigh_table(true);
205
206 return 0;
207 }
208
209
210 // Send an ICMP-ECHO. This is less for actually pinging but for the
211 // neighbor cache to be kept up-to-date.
212 static ssize_t ping6(struct in6_addr *addr,
213 const struct interface *iface)
214 {
215 struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, iface->ifindex};
216 struct icmp6_hdr echo = {.icmp6_type = ICMP6_ECHO_REQUEST};
217 struct iovec iov = {&echo, sizeof(echo)};
218
219 // Linux seems to not honor IPV6_PKTINFO on raw-sockets, so work around
220 setsockopt(ping_socket, SOL_SOCKET, SO_BINDTODEVICE,
221 iface->ifname, sizeof(iface->ifname));
222 return odhcpd_send(ping_socket, &dest, &iov, 1, iface);
223 }
224
225
226 // Handle solicitations
227 static void handle_solicit(void *addr, void *data, size_t len,
228 struct interface *iface, _unused void *dest)
229 {
230 struct ip6_hdr *ip6 = data;
231 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
232 struct sockaddr_ll *ll = addr;
233
234 // Solicitation is for duplicate address detection
235 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
236
237 // Don't forward any non-DAD solicitation for external ifaces
238 // TODO: check if we should even forward DADs for them
239 if (iface->external && !ns_is_dad)
240 return;
241
242 if (len < sizeof(*ip6) + sizeof(*req))
243 return; // Invalid reqicitation
244
245 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
246 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
247 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
248 return; // Invalid target
249
250 char ipbuf[INET6_ADDRSTRLEN];
251 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
252 syslog(LOG_DEBUG, "Got a NS for %s", ipbuf);
253
254 uint8_t mac[6];
255 odhcpd_get_mac(iface, mac);
256 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
257 return; // Looped back
258
259 struct interface *c;
260 list_for_each_entry(c, &interfaces, head)
261 if (iface->ndp == RELAYD_RELAY && iface != c &&
262 (ns_is_dad || !c->external))
263 ping6(&req->nd_ns_target, c);
264 }
265
266
267 void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
268 const struct interface *iface, const struct in6_addr *gw, bool add)
269 {
270 struct req {
271 struct nlmsghdr nh;
272 struct rtmsg rtm;
273 struct rtattr rta_dst;
274 struct in6_addr dst_addr;
275 struct rtattr rta_oif;
276 uint32_t ifindex;
277 struct rtattr rta_table;
278 uint32_t table;
279 struct rtattr rta_gw;
280 struct in6_addr gw;
281 } req = {
282 {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seqid, 0},
283 {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
284 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
285 *addr,
286 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
287 iface->ifindex,
288 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
289 RT_TABLE_MAIN,
290 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
291 IN6ADDR_ANY_INIT,
292 };
293
294 if (gw)
295 req.gw = *gw;
296
297 if (add) {
298 req.nh.nlmsg_type = RTM_NEWROUTE;
299 req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
300 req.rtm.rtm_protocol = RTPROT_STATIC;
301 req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
302 req.rtm.rtm_type = RTN_UNICAST;
303 } else {
304 req.nh.nlmsg_type = RTM_DELROUTE;
305 req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
306 }
307
308 req.nh.nlmsg_len = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
309 send(rtnl_event.uloop.fd, &req, req.nh.nlmsg_len, MSG_DONTWAIT);
310 }
311
312 // Use rtnetlink to modify kernel routes
313 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
314 {
315 char namebuf[INET6_ADDRSTRLEN];
316 inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
317 syslog(LOG_NOTICE, "%s about %s on %s",
318 (add) ? "Learned" : "Forgot", namebuf, iface->ifname);
319
320 if (iface->learn_routes)
321 odhcpd_setup_route(addr, 128, iface, NULL, add);
322 }
323
324
325 // Handler for neighbor cache entries from the kernel. This is our source
326 // to learn and unlearn hosts on interfaces.
327 static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
328 _unused struct interface *iface, _unused void *dest)
329 {
330 bool dump_neigh = false;
331 struct in6_addr last_solicited = IN6ADDR_ANY_INIT;
332
333 for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len);
334 nh = NLMSG_NEXT(nh, len)) {
335 struct ndmsg *ndm = NLMSG_DATA(nh);
336 struct rtmsg *rtm = NLMSG_DATA(nh);
337
338 bool is_addr = (nh->nlmsg_type == RTM_NEWADDR
339 || nh->nlmsg_type == RTM_DELADDR);
340 bool is_route = (nh->nlmsg_type == RTM_NEWROUTE
341 || nh->nlmsg_type == RTM_DELROUTE);
342 bool is_neigh = (nh->nlmsg_type == RTM_NEWNEIGH
343 || nh->nlmsg_type == RTM_DELNEIGH);
344
345 // Family and ifindex are on the same offset for NEIGH and ADDR
346 if ((!is_addr && !is_route && !is_neigh)
347 || NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
348 || ndm->ndm_family != AF_INET6)
349 continue;
350
351 // Inform about a change in default route
352 if (is_route && rtm->rtm_dst_len == 0)
353 raise(SIGUSR1);
354 else if (is_route && rtm->rtm_dst_len == 128)
355 continue;
356
357 // Data to retrieve
358 size_t rta_offset = (is_route) ? sizeof(*rtm) : (is_addr) ?
359 sizeof(struct ifaddrmsg) : sizeof(*ndm);
360 uint16_t atype = (is_route) ? RTA_DST : (is_addr) ? IFA_ADDRESS : NDA_DST;
361 ssize_t alen = NLMSG_PAYLOAD(nh, rta_offset);
362 struct in6_addr *addr = NULL;
363 int *ifindex = (!is_route) ? &ndm->ndm_ifindex : NULL;
364 int *metric = NULL;
365
366 for (struct rtattr *rta = (void*)(((uint8_t*)ndm) + rta_offset);
367 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) {
368 if (rta->rta_type == atype &&
369 RTA_PAYLOAD(rta) >= sizeof(*addr)) {
370 addr = RTA_DATA(rta);
371 } else if (is_route && rta->rta_type == RTA_OIF &&
372 RTA_PAYLOAD(rta) == sizeof(int)) {
373 ifindex = (int*)RTA_DATA(rta);
374 } else if (is_route && rta->rta_type == RTA_GATEWAY) {
375 ifindex = NULL;
376 break;
377 } else if (is_route && rta->rta_type == RTA_PRIORITY) {
378 metric = (int*)RTA_DATA(rta);
379 }
380 }
381
382 // Lookup interface
383 struct interface *iface = ifindex ? odhcpd_get_interface_by_index(*ifindex) : NULL;
384 if (!iface)
385 continue;
386
387 // Keep-alive neighbor entries for RA sending
388 if (nh->nlmsg_type == RTM_DELNEIGH && !(ndm->ndm_state & NUD_FAILED) &&
389 addr && IN6_IS_ADDR_LINKLOCAL(addr) && iface->ra == RELAYD_SERVER)
390 ping6(addr, iface);
391
392 // Address not specified or unrelated
393 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
394 IN6_IS_ADDR_MULTICAST(addr))
395 continue;
396
397 // Check for states
398 bool add;
399 if (is_addr)
400 add = (nh->nlmsg_type == RTM_NEWADDR);
401 else
402 add = (nh->nlmsg_type == RTM_NEWNEIGH && (ndm->ndm_state &
403 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE
404 | NUD_PERMANENT | NUD_NOARP)));
405
406 if (iface->ndp == RELAYD_RELAY && !is_route) {
407 // Replay change to all neighbor cache
408 struct {
409 struct nlmsghdr nh;
410 struct ndmsg ndm;
411 struct nlattr nla_dst;
412 struct in6_addr dst;
413 } req = {
414 {sizeof(req), RTM_DELNEIGH, NLM_F_REQUEST,
415 ++rtnl_seqid, 0},
416 {.ndm_family = AF_INET6, .ndm_flags = NTF_PROXY},
417 {sizeof(struct nlattr) + sizeof(struct in6_addr), NDA_DST},
418 *addr
419 };
420
421 if (ndm->ndm_flags & NTF_PROXY) {
422 // Dump & flush proxy entries
423 if (nh->nlmsg_type == RTM_NEWNEIGH) {
424 req.ndm.ndm_ifindex = iface->ifindex;
425 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
426 setup_route(addr, iface, false);
427 dump_neigh = true;
428 }
429 } else if (add) {
430 struct interface *c;
431 list_for_each_entry(c, &interfaces, head) {
432 if (iface == c)
433 continue;
434
435 if (c->ndp == RELAYD_RELAY) {
436 req.nh.nlmsg_type = RTM_NEWNEIGH;
437 req.nh.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
438
439 req.ndm.ndm_ifindex = c->ifindex;
440 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
441 } else { // Delete NDP cache from interfaces without relay
442 req.nh.nlmsg_type = RTM_DELNEIGH;
443 req.nh.nlmsg_flags &= ~(NLM_F_CREATE | NLM_F_REPLACE);
444
445 req.ndm.ndm_ifindex = c->ifindex;
446 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
447 }
448 }
449
450 setup_route(addr, iface, true);
451 } else {
452 if (nh->nlmsg_type == RTM_NEWNEIGH) {
453 // might be locally originating
454 if (!IN6_ARE_ADDR_EQUAL(&last_solicited, addr)) {
455 last_solicited = *addr;
456
457 struct interface *c;
458 list_for_each_entry(c, &interfaces, head)
459 if (iface->ndp == RELAYD_RELAY && iface != c &&
460 !c->external == false)
461 ping6(addr, c);
462 }
463 } else {
464 struct interface *c;
465 list_for_each_entry(c, &interfaces, head) {
466 if (c->ndp == RELAYD_RELAY && iface != c) {
467 req.ndm.ndm_ifindex = c->ifindex;
468 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
469 }
470 }
471 setup_route(addr, iface, false);
472
473 // also: dump to add proxies back in case it moved elsewhere
474 dump_neigh = true;
475 }
476 }
477 }
478
479 if (is_addr) {
480 if (iface->ra == RELAYD_SERVER)
481 raise(SIGUSR1); // Inform about a change in addresses
482
483 if (iface->dhcpv6 == RELAYD_SERVER)
484 iface->ia_reconf = true;
485 } else if (is_route) {
486 if (iface->ndp == RELAYD_RELAY && iface->master) {
487 // Replay on-link route changes on all slave interfaces
488 nh->nlmsg_flags = NLM_F_REQUEST;
489
490 if (nh->nlmsg_type == RTM_NEWROUTE)
491 nh->nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
492
493 struct interface *c;
494 list_for_each_entry(c, &interfaces, head) {
495 if (c->ndp == RELAYD_RELAY && !c->master) {
496 *ifindex = c->ifindex;
497 *metric = (*metric & 0xffff) | (c->ifindex << 16);
498 send(rtnl_event.uloop.fd, nh, nh->nlmsg_len, MSG_DONTWAIT);
499 }
500 }
501 }
502 }
503 }
504
505 if (dump_neigh)
506 dump_neigh_table(false);
507 }