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.
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>
29 #include <linux/rtnetlink.h>
30 #include <linux/filter.h>
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);
43 static uint32_t rtnl_seqid = 0;
44 static int ping_socket = -1;
45 static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink};
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),
58 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
61 // Initialize NDP-proxy
64 // Setup netlink socket
65 if ((rtnl_event.uloop.fd = odhcpd_open_rtnl()) < 0)
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));
76 // Synthesize initial address events
81 {sizeof(req2), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
83 {.ifa_family = AF_INET6}
85 send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT);
86 odhcpd_register(&rtnl_event);
89 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
90 if (ping_socket < 0) {
91 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
96 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
98 // This is required by RFC 4861
100 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
101 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
103 // Filter all packages, we only want to send
104 struct icmp6_filter filt;
105 ICMP6_FILTER_SETBLOCKALL(&filt);
106 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
109 // Netlink socket, continued...
110 group = RTNLGRP_NEIGH;
111 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
117 static void dump_neigh_table(bool proxy)
123 {sizeof(req), RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP,
125 {.ndm_family = AF_INET6, .ndm_flags = (proxy) ? NTF_PROXY : 0}
127 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
131 int setup_ndp_interface(struct interface *iface, bool enable)
134 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
135 int procfd = open(procbuf, O_WRONLY);
136 bool dump_neigh = false;
138 if (iface->ndp_event.uloop.fd >= 0) {
139 uloop_fd_delete(&iface->ndp_event.uloop);
140 close(iface->ndp_event.uloop.fd);
141 iface->ndp_event.uloop.fd = -1;
143 write(procfd, "0\n", 2);
147 if (enable && iface->ndp == RELAYD_RELAY) {
148 write(procfd, "1\n", 2);
150 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
152 syslog(LOG_ERR, "Unable to open packet socket: %s",
157 #ifdef PACKET_RECV_TYPE
158 int pktt = 1 << PACKET_MULTICAST;
159 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
162 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
163 &bpf_prog, sizeof(bpf_prog))) {
164 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
168 struct sockaddr_ll ll = {
169 .sll_family = AF_PACKET,
170 .sll_ifindex = iface->ifindex,
171 .sll_protocol = htons(ETH_P_IPV6),
177 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
179 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
180 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
182 iface->ndp_event.uloop.fd = sock;
183 iface->ndp_event.handle_dgram = handle_solicit;
184 odhcpd_register(&iface->ndp_event);
186 // If we already were enabled dump is unnecessary, if not do dump
188 dump_neigh_table(false);
195 dump_neigh_table(true);
201 // Send an ICMP-ECHO. This is less for actually pinging but for the
202 // neighbor cache to be kept up-to-date.
203 static ssize_t ping6(struct in6_addr *addr,
204 const struct interface *iface)
206 struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, 0};
207 struct icmp6_hdr echo = {.icmp6_type = ICMP6_ECHO_REQUEST};
208 struct iovec iov = {&echo, sizeof(echo)};
210 // Linux seems to not honor IPV6_PKTINFO on raw-sockets, so work around
211 setsockopt(ping_socket, SOL_SOCKET, SO_BINDTODEVICE,
212 iface->ifname, sizeof(iface->ifname));
213 return odhcpd_send(ping_socket, &dest, &iov, 1, iface);
217 // Handle solicitations
218 static void handle_solicit(void *addr, void *data, size_t len,
219 struct interface *iface, _unused void *dest)
221 struct ip6_hdr *ip6 = data;
222 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
223 struct sockaddr_ll *ll = addr;
225 // Solicitation is for duplicate address detection
226 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
228 // Don't forward any non-DAD solicitation for external ifaces
229 // TODO: check if we should even forward DADs for them
230 if (iface->external && !ns_is_dad)
233 if (len < sizeof(*ip6) + sizeof(*req))
234 return; // Invalid reqicitation
236 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
237 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
238 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
239 return; // Invalid target
241 char ipbuf[INET6_ADDRSTRLEN];
242 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
243 syslog(LOG_DEBUG, "Got a NS for %s", ipbuf);
246 odhcpd_get_mac(iface, mac);
247 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
248 return; // Looped back
251 list_for_each_entry(c, &interfaces, head)
252 if (iface->ndp == RELAYD_RELAY && iface != c &&
253 (!ns_is_dad || !c->external == false))
254 ping6(&req->nd_ns_target, c);
258 void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
259 const struct interface *iface, const struct in6_addr *gw, bool add)
264 struct rtattr rta_dst;
265 struct in6_addr dst_addr;
266 struct rtattr rta_oif;
268 struct rtattr rta_table;
270 struct rtattr rta_gw;
273 {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seqid, 0},
274 {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
275 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
277 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
279 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
281 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
289 req.nh.nlmsg_type = RTM_NEWROUTE;
290 req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
291 req.rtm.rtm_protocol = RTPROT_BOOT;
292 req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
293 req.rtm.rtm_type = RTN_UNICAST;
295 req.nh.nlmsg_type = RTM_DELROUTE;
296 req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
299 size_t reqlen = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
300 send(rtnl_event.uloop.fd, &req, reqlen, MSG_DONTWAIT);
303 // Use rtnetlink to modify kernel routes
304 static void setup_route(struct in6_addr *addr, struct interface *iface,
307 char namebuf[INET6_ADDRSTRLEN];
308 inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
309 syslog(LOG_NOTICE, "%s about %s on %s", (add) ? "Learned" : "Forgot",
310 namebuf, (iface) ? iface->ifname : "<pending>");
312 if (!iface || !iface->learn_routes)
315 odhcpd_setup_route(addr, 128, iface, NULL, add);
319 // Handler for neighbor cache entries from the kernel. This is our source
320 // to learn and unlearn hosts on interfaces.
321 static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
322 _unused struct interface *iface, _unused void *dest)
324 bool dump_neigh = false;
325 struct in6_addr last_solicited = IN6ADDR_ANY_INIT;
327 for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len);
328 nh = NLMSG_NEXT(nh, len)) {
329 struct rtmsg *rtm = NLMSG_DATA(nh);
330 if ((nh->nlmsg_type == RTM_NEWROUTE ||
331 nh->nlmsg_type == RTM_DELROUTE) &&
332 rtm->rtm_dst_len == 0)
333 raise(SIGUSR1); // Inform about a change in default route
335 struct ndmsg *ndm = NLMSG_DATA(nh);
336 struct ifaddrmsg *ifa = NLMSG_DATA(nh);
337 if (nh->nlmsg_type != RTM_NEWNEIGH
338 && nh->nlmsg_type != RTM_DELNEIGH
339 && nh->nlmsg_type != RTM_NEWADDR
340 && nh->nlmsg_type != RTM_DELADDR)
341 continue; // Unrelated message type
342 bool is_addr = (nh->nlmsg_type == RTM_NEWADDR
343 || nh->nlmsg_type == RTM_DELADDR);
345 // Family and ifindex are on the same offset for NEIGH and ADDR
346 if (NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
347 || ndm->ndm_family != AF_INET6)
351 struct interface *iface;
352 if (!(iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex)))
356 size_t rta_offset = (is_addr) ? sizeof(*ifa) : sizeof(*ndm);
357 uint16_t atype = (is_addr) ? IFA_ADDRESS : NDA_DST;
358 ssize_t alen = NLMSG_PAYLOAD(nh, rta_offset);
359 struct in6_addr *addr = NULL;
361 for (struct rtattr *rta = (void*)(((uint8_t*)ndm) + rta_offset);
362 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen))
363 if (rta->rta_type == atype &&
364 RTA_PAYLOAD(rta) >= sizeof(*addr))
365 addr = RTA_DATA(rta);
367 // Address not specified or unrelated
368 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
369 IN6_IS_ADDR_MULTICAST(addr))
375 add = (nh->nlmsg_type == RTM_NEWADDR);
377 add = (nh->nlmsg_type == RTM_NEWNEIGH && (ndm->ndm_state &
378 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE
379 | NUD_PERMANENT | NUD_NOARP)));
381 if (iface->ndp == RELAYD_RELAY) {
382 // Replay change to all neighbor cache
386 struct nlattr nla_dst;
389 {sizeof(req), RTM_DELNEIGH, NLM_F_REQUEST,
391 {.ndm_family = AF_INET6, .ndm_flags = NTF_PROXY},
392 {sizeof(struct nlattr) + sizeof(struct in6_addr), NDA_DST},
396 if (ndm->ndm_flags & NTF_PROXY) {
397 // Dump & flush proxy entries
398 if (nh->nlmsg_type == RTM_NEWNEIGH) {
399 req.ndm.ndm_ifindex = iface->ifindex;
400 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
405 list_for_each_entry(c, &interfaces, head) {
409 if (c->ndp == RELAYD_RELAY) {
410 req.nh.nlmsg_type = RTM_NEWNEIGH;
411 req.nh.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
413 req.ndm.ndm_ifindex = c->ifindex;
414 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
415 } else { // Delete NDP cache from interfaces without relay
416 req.nh.nlmsg_type = RTM_DELNEIGH;
417 req.nh.nlmsg_flags &= ~(NLM_F_CREATE | NLM_F_REPLACE);
419 req.ndm.ndm_ifindex = c->ifindex;
420 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
424 setup_route(addr, iface, true);
426 if (nh->nlmsg_type == RTM_NEWNEIGH) {
427 // might be locally originating
428 if (!IN6_ARE_ADDR_EQUAL(&last_solicited, addr)) {
429 last_solicited = *addr;
432 list_for_each_entry(c, &interfaces, head)
433 if (iface->ndp == RELAYD_RELAY && iface != c &&
434 !c->external == false)
439 list_for_each_entry(c, &interfaces, head) {
440 if (c->ndp == RELAYD_RELAY && iface != c) {
441 req.ndm.ndm_ifindex = c->ifindex;
442 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
445 setup_route(addr, iface, false);
447 // also: dump to add proxies back in case it moved elsewhere
454 if (iface->ra == RELAYD_SERVER)
455 raise(SIGUSR1); // Inform about a change in addresses
457 if (iface->dhcpv6 == RELAYD_SERVER)
458 iface->ia_reconf = true;
460 if (iface->ndp == RELAYD_RELAY && iface->master) {
461 // Replay address changes on all slave interfaces
462 nh->nlmsg_flags = NLM_F_REQUEST;
464 if (nh->nlmsg_type == RTM_NEWADDR)
465 nh->nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
468 list_for_each_entry(c, &interfaces, head) {
469 if (c->ndp == RELAYD_RELAY && !c->master) {
470 ifa->ifa_index = c->ifindex;
471 send(rtnl_event.uloop.fd, nh, nh->nlmsg_len, MSG_DONTWAIT);
479 dump_neigh_table(false);