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);
41 static uint32_t rtnl_seqid = 0;
42 static int ping_socket = -1;
43 static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink};
46 // Filter ICMPv6 messages of type neighbor soliciation
47 static struct sock_filter bpf[] = {
48 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
49 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
50 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
51 offsetof(struct icmp6_hdr, icmp6_type)),
52 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
53 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
54 BPF_STMT(BPF_RET | BPF_K, 0),
56 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
59 // Initialize NDP-proxy
62 // Setup netlink socket
63 if ((rtnl_event.uloop.fd = odhcpd_open_rtnl()) < 0)
66 // Receive netlink neighbor and ip-address events
67 uint32_t group = RTNLGRP_IPV6_IFADDR;
68 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
69 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
70 group = RTNLGRP_IPV6_ROUTE;
71 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
72 NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
74 odhcpd_register(&rtnl_event);
77 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
78 if (ping_socket < 0) {
79 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
84 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
86 // This is required by RFC 4861
88 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
89 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
91 // Filter all packages, we only want to send
92 struct icmp6_filter filt;
93 ICMP6_FILTER_SETBLOCKALL(&filt);
94 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
97 // Netlink socket, continued...
98 group = RTNLGRP_NEIGH;
99 setsockopt(rtnl_event.uloop.fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
105 static void dump_neigh_table(bool proxy)
111 {sizeof(req), RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP,
113 {.ndm_family = AF_INET6, .ndm_flags = (proxy) ? NTF_PROXY : 0}
115 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
116 odhcpd_process(&rtnl_event);
120 int setup_ndp_interface(struct interface *iface, bool enable)
123 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
124 int procfd = open(procbuf, O_WRONLY);
125 bool dump_neigh = false;
127 if (iface->ndp_event.uloop.fd > 0) {
128 uloop_fd_delete(&iface->ndp_event.uloop);
129 close(iface->ndp_event.uloop.fd);
130 iface->ndp_event.uloop.fd = -1;
132 if (!enable || iface->ndp != RELAYD_RELAY)
133 if (write(procfd, "0\n", 2) < 0) {}
138 if (enable && (iface->ra == RELAYD_SERVER ||
139 iface->dhcpv6 == RELAYD_SERVER || iface->ndp == RELAYD_RELAY)) {
140 // Synthesize initial address events
143 struct ifaddrmsg ifa;
145 {sizeof(req2), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
147 {.ifa_family = AF_INET6, .ifa_index = iface->ifindex}
149 send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT);
152 if (enable && iface->ndp == RELAYD_RELAY) {
153 if (write(procfd, "1\n", 2) < 0) {}
156 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
158 syslog(LOG_ERR, "Unable to open packet socket: %s",
163 #ifdef PACKET_RECV_TYPE
164 int pktt = 1 << PACKET_MULTICAST;
165 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
168 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
169 &bpf_prog, sizeof(bpf_prog))) {
170 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
174 struct sockaddr_ll ll = {
175 .sll_family = AF_PACKET,
176 .sll_ifindex = iface->ifindex,
177 .sll_protocol = htons(ETH_P_IPV6),
183 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
185 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
186 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
188 iface->ndp_event.uloop.fd = sock;
189 iface->ndp_event.handle_dgram = handle_solicit;
190 odhcpd_register(&iface->ndp_event);
192 // If we already were enabled dump is unnecessary, if not do dump
194 dump_neigh_table(false);
202 dump_neigh_table(true);
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 void ping6(struct in6_addr *addr,
211 const struct interface *iface)
213 struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, iface->ifindex};
214 struct icmp6_hdr echo = {.icmp6_type = ICMP6_ECHO_REQUEST};
215 struct iovec iov = {&echo, sizeof(echo)};
217 odhcpd_setup_route(addr, 128, iface, NULL, 128, true);
218 odhcpd_send(ping_socket, &dest, &iov, 1, iface);
219 odhcpd_setup_route(addr, 128, iface, NULL, 128, false);
223 // Handle solicitations
224 static void handle_solicit(void *addr, void *data, size_t len,
225 struct interface *iface, _unused void *dest)
227 struct ip6_hdr *ip6 = data;
228 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
229 struct sockaddr_ll *ll = addr;
231 // Solicitation is for duplicate address detection
232 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
234 // Don't forward any non-DAD solicitation for external ifaces
235 // TODO: check if we should even forward DADs for them
236 if (iface->external && !ns_is_dad)
239 if (len < sizeof(*ip6) + sizeof(*req))
240 return; // Invalid reqicitation
242 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
243 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
244 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
245 return; // Invalid target
247 char ipbuf[INET6_ADDRSTRLEN];
248 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
249 syslog(LOG_DEBUG, "Got a NS for %s", ipbuf);
252 odhcpd_get_mac(iface, mac);
253 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
254 return; // Looped back
257 list_for_each_entry(c, &interfaces, head)
258 if (iface->ndp == RELAYD_RELAY && iface != c &&
259 (ns_is_dad || !c->external))
260 ping6(&req->nd_ns_target, c);
263 // Use rtnetlink to modify kernel routes
264 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
266 char namebuf[INET6_ADDRSTRLEN];
267 inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
268 syslog(LOG_NOTICE, "%s about %s on %s",
269 (add) ? "Learned" : "Forgot", namebuf, iface->ifname);
271 if (iface->learn_routes)
272 odhcpd_setup_route(addr, 128, iface, NULL, 1024, add);
276 // Handler for neighbor cache entries from the kernel. This is our source
277 // to learn and unlearn hosts on interfaces.
278 static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
279 _unused struct interface *iface, _unused void *dest)
281 bool dump_neigh = false;
282 struct in6_addr last_solicited = IN6ADDR_ANY_INIT;
284 for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len);
285 nh = NLMSG_NEXT(nh, len)) {
286 struct ndmsg *ndm = NLMSG_DATA(nh);
287 struct rtmsg *rtm = NLMSG_DATA(nh);
289 bool is_addr = (nh->nlmsg_type == RTM_NEWADDR
290 || nh->nlmsg_type == RTM_DELADDR);
291 bool is_route = (nh->nlmsg_type == RTM_NEWROUTE
292 || nh->nlmsg_type == RTM_DELROUTE);
293 bool is_neigh = (nh->nlmsg_type == RTM_NEWNEIGH
294 || nh->nlmsg_type == RTM_DELNEIGH);
296 // Family and ifindex are on the same offset for NEIGH and ADDR
297 if ((!is_addr && !is_route && !is_neigh)
298 || NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
299 || ndm->ndm_family != AF_INET6)
302 // Inform about a change in default route
303 if (is_route && rtm->rtm_dst_len == 0)
309 size_t rta_offset = (is_addr) ? sizeof(struct ifaddrmsg) : sizeof(*ndm);
310 uint16_t atype = (is_addr) ? IFA_ADDRESS : NDA_DST;
311 ssize_t alen = NLMSG_PAYLOAD(nh, rta_offset);
312 struct in6_addr *addr = NULL;
314 for (struct rtattr *rta = (void*)(((uint8_t*)ndm) + rta_offset);
315 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) {
316 if (rta->rta_type == atype &&
317 RTA_PAYLOAD(rta) >= sizeof(*addr)) {
318 addr = RTA_DATA(rta);
323 struct interface *iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex);
327 // Address not specified or unrelated
328 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
329 IN6_IS_ADDR_MULTICAST(addr))
335 add = (nh->nlmsg_type == RTM_NEWADDR);
337 add = (nh->nlmsg_type == RTM_NEWNEIGH && (ndm->ndm_state &
338 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE
339 | NUD_PERMANENT | NUD_NOARP)));
341 if (iface->ndp == RELAYD_RELAY) {
342 // Replay change to all neighbor cache
346 struct nlattr nla_dst;
349 {sizeof(req), RTM_DELNEIGH, NLM_F_REQUEST,
351 {.ndm_family = AF_INET6, .ndm_flags = NTF_PROXY},
352 {sizeof(struct nlattr) + sizeof(struct in6_addr), NDA_DST},
356 if (ndm->ndm_flags & NTF_PROXY) {
357 // Dump & flush proxy entries
358 if (nh->nlmsg_type == RTM_NEWNEIGH) {
359 req.ndm.ndm_ifindex = iface->ifindex;
360 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
361 setup_route(addr, iface, false);
366 list_for_each_entry(c, &interfaces, head) {
370 if (c->ndp == RELAYD_RELAY) {
371 req.nh.nlmsg_type = RTM_NEWNEIGH;
372 req.nh.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
374 req.ndm.ndm_ifindex = c->ifindex;
375 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
376 } else { // Delete NDP cache from interfaces without relay
377 req.nh.nlmsg_type = RTM_DELNEIGH;
378 req.nh.nlmsg_flags &= ~(NLM_F_CREATE | NLM_F_REPLACE);
380 req.ndm.ndm_ifindex = c->ifindex;
381 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
385 setup_route(addr, iface, true);
387 if (nh->nlmsg_type == RTM_NEWNEIGH) {
388 // might be locally originating
389 if (!IN6_ARE_ADDR_EQUAL(&last_solicited, addr)) {
390 last_solicited = *addr;
393 list_for_each_entry(c, &interfaces, head)
394 if (iface->ndp == RELAYD_RELAY && iface != c &&
395 !c->external == false)
400 list_for_each_entry(c, &interfaces, head) {
401 if (c->ndp == RELAYD_RELAY && iface != c) {
402 req.ndm.ndm_ifindex = c->ifindex;
403 send(rtnl_event.uloop.fd, &req, sizeof(req), MSG_DONTWAIT);
406 setup_route(addr, iface, false);
408 // also: dump to add proxies back in case it moved elsewhere
415 if (iface->ra == RELAYD_SERVER)
416 raise(SIGUSR1); // Inform about a change in addresses
418 if (iface->dhcpv6 == RELAYD_SERVER)
419 iface->ia_reconf = true;
424 dump_neigh_table(false);