2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
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.
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.
20 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #include <net/ethernet.h>
23 #include <netinet/ip6.h>
24 #include <netinet/icmp6.h>
25 #include <netpacket/packet.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/filter.h>
34 static void handle_solicit(void *addr, void *data, size_t len,
35 struct interface *iface);
36 static void handle_rtnetlink(void *addr, void *data, size_t len,
37 struct interface *iface);
38 static struct ndp_neighbor* find_neighbor(struct in6_addr *addr, bool strict);
39 static void modify_neighbor(struct in6_addr *addr, struct interface *iface,
41 static ssize_t ping6(struct in6_addr *addr,
42 const struct interface *iface);
44 static struct list_head neighbors = LIST_HEAD_INIT(neighbors);
45 static size_t neighbor_count = 0;
46 static uint32_t rtnl_seqid = 0;
48 static int ping_socket = -1;
49 static struct odhcpd_event ndp_event = {{.fd = -1}, handle_solicit};
50 static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink};
53 // Filter ICMPv6 messages of type neighbor soliciation
54 static struct sock_filter bpf[] = {
55 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
56 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
57 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
58 offsetof(struct icmp6_hdr, icmp6_type)),
59 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
60 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
61 BPF_STMT(BPF_RET | BPF_K, 0),
63 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
66 // Initialize NDP-proxy
69 // Setup netlink socket
70 if ((rtnl_event.uloop.fd = odhcpd_open_rtnl()) < 0)
73 // Receive netlink neighbor and ip-address events
74 uint32_t group = RTNLGRP_IPV6_IFADDR;
75 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
76 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
77 group = RTNLGRP_IPV6_ROUTE;
78 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
79 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
81 // Synthesize initial address events
86 {sizeof(req2), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
88 {.ifa_family = AF_INET6}
90 send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT);
91 odhcpd_register(&rtnl_event);
94 // Create socket for intercepting NDP
95 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
96 htons(ETH_P_ALL)); // ETH_P_ALL for ingress + egress
98 syslog(LOG_ERR, "Unable to open packet socket: %s",
103 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
104 &bpf_prog, sizeof(bpf_prog))) {
105 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
109 ndp_event.uloop.fd = sock;
110 odhcpd_register(&ndp_event);
112 // Open ICMPv6 socket
113 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
114 if (ping_socket < 0) {
115 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
120 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
122 // This is required by RFC 4861
124 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
125 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
127 // Filter all packages, we only want to send
128 struct icmp6_filter filt;
129 ICMP6_FILTER_SETBLOCKALL(&filt);
130 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
133 // Netlink socket, continued...
134 group = RTNLGRP_NEIGH;
135 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
137 // Synthesize initial neighbor events
142 {sizeof(req), RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP,
144 {.ndm_family = AF_INET6}
146 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
152 int setup_ndp_interface(struct interface *iface, bool enable)
154 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
155 setsockopt(ndp_event.uloop.fd, SOL_PACKET, PACKET_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
157 struct ndp_neighbor *c, *n;
158 list_for_each_entry_safe(c, n, &neighbors, head)
159 if (c->iface == iface && (c->timeout == 0 || iface->ndp != RELAYD_RELAY || !enable))
160 modify_neighbor(&c->addr, c->iface, false);
162 if (enable && iface->ndp == RELAYD_RELAY) {
163 setsockopt(ndp_event.uloop.fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
165 if (iface->static_ndp_len) {
166 char *entry = alloca(iface->static_ndp_len), *saveptr;
168 syslog(LOG_ERR, "Alloca failed for static NDP list");
171 memcpy(entry, iface->static_ndp, iface->static_ndp_len);
173 for (entry = strtok_r(entry, " ", &saveptr); entry; entry = strtok_r(NULL, " ", &saveptr)) {
175 struct ndp_neighbor *n = malloc(sizeof(*n));
177 syslog(LOG_ERR, "Malloc failed for static NDP-prefix %s", entry);
184 sep = strchr(entry, '/');
187 syslog(LOG_ERR, "Invalid static NDP-prefix %s", entry);
192 n->len = atoi(sep + 1);
193 if (inet_pton(AF_INET6, entry, &n->addr) != 1 || n->len > 128) {
195 syslog(LOG_ERR, "Invalid static NDP-prefix %s/%s", entry, sep + 1);
199 list_add(&n->head, &neighbors);
208 // Send an ICMP-ECHO. This is less for actually pinging but for the
209 // neighbor cache to be kept up-to-date.
210 static ssize_t ping6(struct in6_addr *addr,
211 const struct interface *iface)
213 struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, 0};
214 struct icmp6_hdr echo = {.icmp6_type = ICMP6_ECHO_REQUEST};
215 struct iovec iov = {&echo, sizeof(echo)};
217 // Linux seems to not honor IPV6_PKTINFO on raw-sockets, so work around
218 setsockopt(ping_socket, SOL_SOCKET, SO_BINDTODEVICE,
219 iface->ifname, sizeof(iface->ifname));
220 return odhcpd_send(ping_socket, &dest, &iov, 1, iface);
224 // Handle solicitations
225 static void handle_solicit(void *addr, void *data, size_t len,
226 struct interface *iface)
228 struct ip6_hdr *ip6 = data;
229 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
230 struct sockaddr_ll *ll = addr;
232 // Solicitation is for duplicate address detection
233 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
235 // Don't forward any non-DAD solicitation for external ifaces
236 // TODO: check if we should even forward DADs for them
237 if (iface->external && !ns_is_dad)
240 if (len < sizeof(*ip6) + sizeof(*req))
241 return; // Invalid reqicitation
243 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
244 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
245 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
246 return; // Invalid target
248 char ipbuf[INET6_ADDRSTRLEN];
249 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
250 syslog(LOG_DEBUG, "Got a NS for %s", ipbuf);
253 odhcpd_get_mac(iface, mac);
254 if (!memcmp(ll->sll_addr, mac, sizeof(mac)) &&
255 ll->sll_pkttype != PACKET_OUTGOING)
256 return; // Looped back
258 time_t now = time(NULL);
260 struct ndp_neighbor *n = find_neighbor(&req->nd_ns_target, false);
261 if (n && (n->iface || abs(n->timeout - now) < 5)) {
262 syslog(LOG_DEBUG, "%s is on %s", ipbuf,
263 (n->iface) ? n->iface->ifname : "<pending>");
264 if (!n->iface || n->iface == iface)
267 // Found on other interface, answer with advertisement
269 struct nd_neighbor_advert body;
270 struct nd_opt_hdr opt_ll_hdr;
274 .nd_na_hdr = {ND_NEIGHBOR_ADVERT,
276 .nd_na_target = req->nd_ns_target,
278 .opt_ll_hdr = {ND_OPT_TARGET_LINKADDR, 1},
281 memcpy(advert.mac, mac, sizeof(advert.mac));
282 advert.body.nd_na_flags_reserved = ND_NA_FLAG_ROUTER |
283 ND_NA_FLAG_SOLICITED;
285 struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
286 if (!ns_is_dad) // If not DAD, then unicast to source
287 dest.sin6_addr = ip6->ip6_src;
289 // Linux seems to not honor IPV6_PKTINFO on raw-sockets, so work around
290 setsockopt(ping_socket, SOL_SOCKET, SO_BINDTODEVICE,
291 iface->ifname, sizeof(iface->ifname));
292 struct iovec iov = {&advert, sizeof(advert)};
293 odhcpd_send(ping_socket, &dest, &iov, 1, iface);
295 // Send echo to all other interfaces to see where target is on
296 // This will trigger neighbor discovery which is what we want.
297 // We will observe the neighbor cache to see results.
301 list_for_each_entry(c, &interfaces, head)
302 if (iface->ndp == RELAYD_RELAY && iface != c &&
303 (!ns_is_dad || !c->external == false))
304 sent += ping6(&req->nd_ns_target, c);
306 if (sent > 0) // Sent a ping, add pending neighbor entry
307 modify_neighbor(&req->nd_ns_target, NULL, true);
312 void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
313 const struct interface *iface, const struct in6_addr *gw, bool add)
318 struct rtattr rta_dst;
319 struct in6_addr dst_addr;
320 struct rtattr rta_oif;
322 struct rtattr rta_table;
324 struct rtattr rta_gw;
327 {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seqid, 0},
328 {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
329 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
331 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
333 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
335 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
343 req.nh.nlmsg_type = RTM_NEWROUTE;
344 req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
345 req.rtm.rtm_protocol = RTPROT_BOOT;
346 req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
347 req.rtm.rtm_type = RTN_UNICAST;
349 req.nh.nlmsg_type = RTM_DELROUTE;
350 req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
353 size_t reqlen = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
354 send(rtnl_event.uloop.fd, &req, reqlen, MSG_DONTWAIT);
357 // Use rtnetlink to modify kernel routes
358 static void setup_route(struct in6_addr *addr, struct interface *iface,
361 char namebuf[INET6_ADDRSTRLEN];
362 inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
363 syslog(LOG_NOTICE, "%s about %s on %s", (add) ? "Learned" : "Forgot",
364 namebuf, (iface) ? iface->ifname : "<pending>");
366 if (!iface || !iface->learn_routes)
369 odhcpd_setup_route(addr, 128, iface, NULL, add);
372 static void free_neighbor(struct ndp_neighbor *n)
374 setup_route(&n->addr, n->iface, false);
380 static struct ndp_neighbor* find_neighbor(struct in6_addr *addr, bool strict)
382 time_t now = time(NULL);
383 struct ndp_neighbor *n, *e;
384 list_for_each_entry_safe(n, e, &neighbors, head) {
385 if ((!strict && !odhcpd_bmemcmp(&n->addr, addr, n->len)) ||
386 (n->len == 128 && IN6_ARE_ADDR_EQUAL(&n->addr, addr)))
389 if (!n->iface && abs(n->timeout - now) >= 5)
396 // Modified our own neighbor-entries
397 static void modify_neighbor(struct in6_addr *addr,
398 struct interface *iface, bool add)
400 if (!addr || (void*)addr == (void*)iface)
403 struct ndp_neighbor *n = find_neighbor(addr, true);
404 if (!add) { // Delete action
405 if (n && (!n->iface || n->iface == iface))
407 } else if (!n) { // No entry yet, add one if possible
408 if (neighbor_count >= NDP_MAX_NEIGHBORS ||
409 !(n = malloc(sizeof(*n))))
417 list_add(&n->head, &neighbors);
419 setup_route(addr, n->iface, add);
420 } else if (n->iface == iface) {
423 } else if (iface && (!n->iface ||
424 (!iface->external && n->iface->external))) {
425 setup_route(addr, n->iface, false);
427 setup_route(addr, n->iface, add);
429 // TODO: In case a host switches interfaces we might want
430 // to set its old neighbor entry to NUD_STALE and ping it
431 // on the old interface to confirm if the MACs match.
435 // Handler for neighbor cache entries from the kernel. This is our source
436 // to learn and unlearn hosts on interfaces.
437 static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
438 _unused struct interface *iface)
440 for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len);
441 nh = NLMSG_NEXT(nh, len)) {
442 struct rtmsg *rtm = NLMSG_DATA(nh);
443 if ((nh->nlmsg_type == RTM_NEWROUTE ||
444 nh->nlmsg_type == RTM_DELROUTE) &&
445 rtm->rtm_dst_len == 0)
446 raise(SIGUSR1); // Inform about a change in default route
448 struct ndmsg *ndm = NLMSG_DATA(nh);
449 struct ifaddrmsg *ifa = NLMSG_DATA(nh);
450 if (nh->nlmsg_type != RTM_NEWNEIGH
451 && nh->nlmsg_type != RTM_DELNEIGH
452 && nh->nlmsg_type != RTM_NEWADDR
453 && nh->nlmsg_type != RTM_DELADDR)
454 continue; // Unrelated message type
455 bool is_addr = (nh->nlmsg_type == RTM_NEWADDR
456 || nh->nlmsg_type == RTM_DELADDR);
458 // Family and ifindex are on the same offset for NEIGH and ADDR
459 if (NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
460 || ndm->ndm_family != AF_INET6)
464 struct interface *iface;
465 if (!(iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex)))
469 size_t rta_offset = (is_addr) ? sizeof(*ifa) : sizeof(*ndm);
470 uint16_t atype = (is_addr) ? IFA_ADDRESS : NDA_DST;
471 ssize_t alen = NLMSG_PAYLOAD(nh, rta_offset);
472 struct in6_addr *addr = NULL;
474 for (struct rtattr *rta = (void*)(((uint8_t*)ndm) + rta_offset);
475 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen))
476 if (rta->rta_type == atype &&
477 RTA_PAYLOAD(rta) >= sizeof(*addr))
478 addr = RTA_DATA(rta);
480 // Address not specified or unrelated
481 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
482 IN6_IS_ADDR_MULTICAST(addr))
488 add = (nh->nlmsg_type == RTM_NEWADDR);
490 add = (nh->nlmsg_type == RTM_NEWNEIGH && (ndm->ndm_state &
491 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE
492 | NUD_PERMANENT | NUD_NOARP)));
494 if (iface->ndp == RELAYD_RELAY)
495 modify_neighbor(addr, iface, add);
497 if (is_addr && iface->ra == RELAYD_SERVER)
498 raise(SIGUSR1); // Inform about a change in addresses
500 if (is_addr && iface->dhcpv6 == RELAYD_SERVER)
501 iface->ia_reconf = true;
503 if (iface->ndp == RELAYD_RELAY && is_addr && iface->master) {
504 // Replay address changes on all slave interfaces
505 nh->nlmsg_flags = NLM_F_REQUEST;
507 if (nh->nlmsg_type == RTM_NEWADDR)
508 nh->nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
511 list_for_each_entry(c, &interfaces, head) {
512 if (c->ndp == RELAYD_RELAY && !c->master) {
513 ifa->ifa_index = c->ifindex;
514 send(rtnl_event.uloop.fd, nh, nh->nlmsg_len, MSG_DONTWAIT);
519 /* TODO: See if this is required for optimal operation
520 // Keep neighbor entries alive so we don't loose routes
522 if (add && (ndm->ndm_state & NUD_STALE))