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>
32 #include <netlink/msg.h>
33 #include <netlink/socket.h>
34 #include <netlink/attr.h>
41 struct odhcpd_event ev;
45 static void handle_solicit(void *addr, void *data, size_t len,
46 struct interface *iface, void *dest);
47 static void handle_rtnl_event(struct odhcpd_event *ev);
48 static int cb_rtnl_valid(struct nl_msg *msg, void *arg);
49 static void catch_rtnetlink(int error);
51 static int ping_socket = -1;
52 static struct event_socket rtnl_event = {
54 .uloop = {.fd = - 1, },
56 .handle_error = catch_rtnetlink,
57 .recv_msgs = handle_rtnl_event,
62 // Filter ICMPv6 messages of type neighbor soliciation
63 static struct sock_filter bpf[] = {
64 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
65 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
66 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
67 offsetof(struct icmp6_hdr, icmp6_type)),
68 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
69 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
70 BPF_STMT(BPF_RET | BPF_K, 0),
72 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
75 // Initialize NDP-proxy
80 rtnl_event.sock = odhcpd_create_nl_socket(NETLINK_ROUTE);
84 rtnl_event.ev.uloop.fd = nl_socket_get_fd(rtnl_event.sock);
86 if (nl_socket_set_buffer_size(rtnl_event.sock, val, 0))
89 nl_socket_disable_seq_check(rtnl_event.sock);
91 nl_socket_modify_cb(rtnl_event.sock, NL_CB_VALID, NL_CB_CUSTOM,
94 // Receive IPv6 address, IPv6 routes and neighbor events
95 if (nl_socket_add_memberships(rtnl_event.sock, RTNLGRP_IPV6_IFADDR,
96 RTNLGRP_IPV6_ROUTE, RTNLGRP_NEIGH, 0))
99 odhcpd_register(&rtnl_event.ev);
101 // Open ICMPv6 socket
102 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
103 if (ping_socket < 0) {
104 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
109 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
111 // This is required by RFC 4861
113 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
114 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
116 // Filter all packages, we only want to send
117 struct icmp6_filter filt;
118 ICMP6_FILTER_SETBLOCKALL(&filt);
119 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
124 if (rtnl_event.sock) {
125 nl_socket_free(rtnl_event.sock);
126 rtnl_event.sock = NULL;
127 rtnl_event.ev.uloop.fd = -1;
133 static void dump_neigh_table(const bool proxy)
137 .ndm_family = AF_INET6,
138 .ndm_flags = proxy ? NTF_PROXY : 0,
141 msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP);
145 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
147 nl_send_auto_complete(rtnl_event.sock, msg);
152 static void dump_addr_table(void)
155 struct ifaddrmsg ifa = {
156 .ifa_family = AF_INET6,
159 msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
163 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
165 nl_send_auto_complete(rtnl_event.sock, msg);
170 int setup_ndp_interface(struct interface *iface, bool enable)
173 bool dump_neigh = false;
176 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
177 procfd = open(procbuf, O_WRONLY);
184 if (iface->ndp_event.uloop.fd > 0) {
185 uloop_fd_delete(&iface->ndp_event.uloop);
186 close(iface->ndp_event.uloop.fd);
187 iface->ndp_event.uloop.fd = -1;
189 if (!enable || iface->ndp != RELAYD_RELAY)
190 if (write(procfd, "0\n", 2) < 0) {}
195 if (enable && (iface->ra == RELAYD_SERVER ||
196 iface->dhcpv6 == RELAYD_SERVER || iface->ndp == RELAYD_RELAY))
199 if (enable && iface->ndp == RELAYD_RELAY) {
200 if (write(procfd, "1\n", 2) < 0) {}
202 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
204 syslog(LOG_ERR, "Unable to open packet socket: %s",
210 #ifdef PACKET_RECV_TYPE
211 int pktt = 1 << PACKET_MULTICAST;
212 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
215 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
216 &bpf_prog, sizeof(bpf_prog))) {
217 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
222 struct sockaddr_ll ll = {
223 .sll_family = AF_PACKET,
224 .sll_ifindex = iface->ifindex,
225 .sll_protocol = htons(ETH_P_IPV6),
231 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
233 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
234 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
236 iface->ndp_event.uloop.fd = sock;
237 iface->ndp_event.handle_dgram = handle_solicit;
238 odhcpd_register(&iface->ndp_event);
240 // If we already were enabled dump is unnecessary, if not do dump
242 dump_neigh_table(false);
248 dump_neigh_table(true);
258 // Send an ICMP-ECHO. This is less for actually pinging but for the
259 // neighbor cache to be kept up-to-date.
260 static void ping6(struct in6_addr *addr,
261 const struct interface *iface)
263 struct sockaddr_in6 dest = { .sin6_family = AF_INET6, .sin6_addr = *addr, .sin6_scope_id = iface->ifindex, };
264 struct icmp6_hdr echo = { .icmp6_type = ICMP6_ECHO_REQUEST };
265 struct iovec iov = { .iov_base = &echo, .iov_len = sizeof(echo) };
267 odhcpd_setup_route(addr, 128, iface, NULL, 128, true);
268 odhcpd_send(ping_socket, &dest, &iov, 1, iface);
269 odhcpd_setup_route(addr, 128, iface, NULL, 128, false);
273 // Handle solicitations
274 static void handle_solicit(void *addr, void *data, size_t len,
275 struct interface *iface, _unused void *dest)
277 struct ip6_hdr *ip6 = data;
278 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
279 struct sockaddr_ll *ll = addr;
281 // Solicitation is for duplicate address detection
282 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
284 // Don't process solicit messages on non relay interfaces
285 // Don't forward any non-DAD solicitation for external ifaces
286 // TODO: check if we should even forward DADs for them
287 if (iface->ndp != RELAYD_RELAY || (iface->external && !ns_is_dad))
290 if (len < sizeof(*ip6) + sizeof(*req))
291 return; // Invalid reqicitation
293 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
294 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
295 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
296 return; // Invalid target
298 char ipbuf[INET6_ADDRSTRLEN];
299 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
300 syslog(LOG_DEBUG, "Got a NS for %s", ipbuf);
303 odhcpd_get_mac(iface, mac);
304 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
305 return; // Looped back
308 list_for_each_entry(c, &interfaces, head)
309 if (iface != c && c->ndp == RELAYD_RELAY &&
310 (ns_is_dad || !c->external))
311 ping6(&req->nd_ns_target, c);
314 // Use rtnetlink to modify kernel routes
315 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
317 char namebuf[INET6_ADDRSTRLEN];
318 inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
319 syslog(LOG_NOTICE, "%s about %s on %s",
320 (add) ? "Learned" : "Forgot", namebuf, iface->ifname);
322 if (iface->learn_routes)
323 odhcpd_setup_route(addr, 128, iface, NULL, 1024, add);
327 static int prefixcmp(const void *va, const void *vb)
329 const struct odhcpd_ipaddr *a = va, *b = vb;
330 uint32_t a_pref = ((a->addr.s6_addr[0] & 0xfe) != 0xfc) ? a->preferred : 1;
331 uint32_t b_pref = ((b->addr.s6_addr[0] & 0xfe) != 0xfc) ? b->preferred : 1;
332 return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
335 // Check address update
336 static void check_addr_updates(struct interface *iface)
338 struct odhcpd_ipaddr addr[RELAYD_MAX_ADDRS] = {{IN6ADDR_ANY_INIT, 0, 0, 0, 0}};
339 time_t now = odhcpd_time();
340 ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, addr, ARRAY_SIZE(addr));
345 qsort(addr, len, sizeof(*addr), prefixcmp);
347 for (int i = 0; i < len; ++i) {
348 addr[i].addr.s6_addr32[3] = 0;
350 if (addr[i].preferred < UINT32_MAX - now)
351 addr[i].preferred += now;
353 if (addr[i].valid < UINT32_MAX - now)
354 addr[i].valid += now;
357 bool change = len != (ssize_t)iface->ia_addr_len;
358 for (ssize_t i = 0; !change && i < len; ++i)
359 if (!IN6_ARE_ADDR_EQUAL(&addr[i].addr, &iface->ia_addr[i].addr) ||
360 (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
361 addr[i].valid < iface->ia_addr[i].valid ||
362 addr[i].preferred < iface->ia_addr[i].preferred)
366 dhcpv6_ia_preupdate(iface);
368 memcpy(iface->ia_addr, addr, len * sizeof(*addr));
369 iface->ia_addr_len = len;
372 dhcpv6_ia_postupdate(iface, now);
375 syslog(LOG_INFO, "Raising SIGUSR1 due to address change on %s", iface->ifname);
380 void setup_addr_for_relaying(struct in6_addr *addr, struct interface *iface, bool add)
384 list_for_each_entry(c, &interfaces, head) {
385 if (iface == c || (c->ndp != RELAYD_RELAY && !add))
388 odhcpd_setup_proxy_neigh(addr, c, c->ndp == RELAYD_RELAY ? add : false);
392 void setup_ping6(struct in6_addr *addr, struct interface *iface)
396 list_for_each_entry(c, &interfaces, head) {
397 if (iface == c || c->ndp != RELAYD_RELAY ||
405 static struct in6_addr last_solicited;
407 static void handle_rtnl_event(struct odhcpd_event *e)
409 struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
411 nl_recvmsgs_default(ev_sock->sock);
415 // Handler for neighbor cache entries from the kernel. This is our source
416 // to learn and unlearn hosts on interfaces.
417 static int cb_rtnl_valid(struct nl_msg *msg, _unused void *arg)
419 struct nlmsghdr *hdr = nlmsg_hdr(msg);
420 struct in6_addr *addr = NULL;
421 struct interface *iface = NULL;
424 switch (hdr->nlmsg_type) {
427 struct rtmsg *rtm = nlmsg_data(hdr);
429 if (!nlmsg_valid_hdr(hdr, sizeof(*rtm)) ||
430 rtm->rtm_family != AF_INET6)
433 if (rtm->rtm_dst_len == 0) {
434 syslog(LOG_INFO, "Raising SIGUSR1 due to default route change");
443 struct ifaddrmsg *ifa = nlmsg_data(hdr);
444 struct nlattr *nla[__IFA_MAX];
446 if (!nlmsg_valid_hdr(hdr, sizeof(*ifa)) ||
447 ifa->ifa_family != AF_INET6)
450 iface = odhcpd_get_interface_by_index(ifa->ifa_index);
454 nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
455 if (!nla[IFA_ADDRESS])
458 addr = nla_data(nla[IFA_ADDRESS]);
459 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
460 IN6_IS_ADDR_MULTICAST(addr))
463 check_addr_updates(iface);
465 if (iface->ndp != RELAYD_RELAY)
468 /* handle the relay logic below */
469 setup_addr_for_relaying(addr, iface, add);
472 dump_neigh_table(false);
479 struct ndmsg *ndm = nlmsg_data(hdr);
480 struct nlattr *nla[__NDA_MAX];
482 if (!nlmsg_valid_hdr(hdr, sizeof(*ndm)) ||
483 ndm->ndm_family != AF_INET6)
486 iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex);
487 if (!iface || iface->ndp != RELAYD_RELAY)
488 return (iface ? NL_OK : NL_SKIP);
490 nlmsg_parse(hdr, sizeof(*ndm), nla, __NDA_MAX - 1, NULL);
494 addr = nla_data(nla[NDA_DST]);
495 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
496 IN6_IS_ADDR_MULTICAST(addr))
499 if (ndm->ndm_flags & NTF_PROXY) {
500 /* Dump and flush proxy entries */
501 if (hdr->nlmsg_type == RTM_NEWNEIGH) {
502 odhcpd_setup_proxy_neigh(addr, iface, false);
503 setup_route(addr, iface, false);
504 dump_neigh_table(false);
510 if (add && !(ndm->ndm_state &
511 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE |
512 NUD_PERMANENT | NUD_NOARP))) {
513 if (!IN6_ARE_ADDR_EQUAL(&last_solicited, addr)) {
514 last_solicited = *addr;
515 setup_ping6(addr, iface);
521 setup_addr_for_relaying(addr, iface, add);
522 setup_route(addr, iface, add);
525 dump_neigh_table(false);
536 static void catch_rtnetlink(int error)
538 if (error == ENOBUFS)