ndp: fix syslog tracing for netlink neigbor and address events
[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
32 #include <netlink/msg.h>
33 #include <netlink/socket.h>
34 #include <netlink/attr.h>
35
36 #include "dhcpv6.h"
37 #include "odhcpd.h"
38
39 struct event_socket {
40 struct odhcpd_event ev;
41 struct nl_sock *sock;
42 int sock_bufsize;
43 };
44
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_rtnl_err(struct odhcpd_event *e, int error);
50
51 static int ping_socket = -1;
52 static struct event_socket rtnl_event = {
53 .ev = {
54 .uloop = {.fd = - 1, },
55 .handle_dgram = NULL,
56 .handle_error = catch_rtnl_err,
57 .recv_msgs = handle_rtnl_event,
58 },
59 .sock = NULL,
60 .sock_bufsize = 133120,
61 };
62
63 // Filter ICMPv6 messages of type neighbor soliciation
64 static struct sock_filter bpf[] = {
65 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
66 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
67 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
68 offsetof(struct icmp6_hdr, icmp6_type)),
69 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
70 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
71 BPF_STMT(BPF_RET | BPF_K, 0),
72 };
73 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
74
75
76 // Initialize NDP-proxy
77 int init_ndp(void)
78 {
79 int val = 2;
80
81 rtnl_event.sock = odhcpd_create_nl_socket(NETLINK_ROUTE);
82 if (!rtnl_event.sock)
83 goto err;
84
85 rtnl_event.ev.uloop.fd = nl_socket_get_fd(rtnl_event.sock);
86
87 if (nl_socket_set_buffer_size(rtnl_event.sock, rtnl_event.sock_bufsize, 0))
88 goto err;
89
90 nl_socket_disable_seq_check(rtnl_event.sock);
91
92 nl_socket_modify_cb(rtnl_event.sock, NL_CB_VALID, NL_CB_CUSTOM,
93 cb_rtnl_valid, NULL);
94
95 // Receive IPv6 address, IPv6 routes and neighbor events
96 if (nl_socket_add_memberships(rtnl_event.sock, RTNLGRP_IPV6_IFADDR,
97 RTNLGRP_IPV6_ROUTE, RTNLGRP_NEIGH, 0))
98 goto err;
99
100 odhcpd_register(&rtnl_event.ev);
101
102 // Open ICMPv6 socket
103 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
104 if (ping_socket < 0) {
105 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
106 return -1;
107 }
108
109 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
110
111 // This is required by RFC 4861
112 val = 255;
113 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
114 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
115
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));
120
121 return 0;
122
123 err:
124 if (rtnl_event.sock) {
125 nl_socket_free(rtnl_event.sock);
126 rtnl_event.sock = NULL;
127 rtnl_event.ev.uloop.fd = -1;
128 }
129
130 return -1;
131 }
132
133 static void dump_neigh_table(const bool proxy)
134 {
135 struct nl_msg *msg;
136 struct ndmsg ndm = {
137 .ndm_family = AF_INET6,
138 .ndm_flags = proxy ? NTF_PROXY : 0,
139 };
140
141 msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP);
142 if (!msg)
143 return;
144
145 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
146
147 nl_send_auto_complete(rtnl_event.sock, msg);
148
149 nlmsg_free(msg);
150 }
151
152 static void dump_addr_table(bool v6)
153 {
154 struct nl_msg *msg;
155 struct ifaddrmsg ifa = {
156 .ifa_family = v6 ? AF_INET6 : AF_INET,
157 };
158
159 msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
160 if (!msg)
161 return;
162
163 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
164
165 nl_send_auto_complete(rtnl_event.sock, msg);
166
167 nlmsg_free(msg);
168 }
169
170 int setup_ndp_interface(struct interface *iface, bool enable)
171 {
172 int ret = 0, procfd;
173 bool dump_neigh = false;
174 char procbuf[64];
175
176 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
177 procfd = open(procbuf, O_WRONLY);
178
179 if (procfd < 0) {
180 ret = -1;
181 goto out;
182 }
183
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;
188
189 if (!enable || iface->ndp != RELAYD_RELAY)
190 if (write(procfd, "0\n", 2) < 0) {}
191
192 dump_neigh = true;
193 }
194
195 if (enable && iface->ndp == RELAYD_RELAY) {
196 if (write(procfd, "1\n", 2) < 0) {}
197
198 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
199 if (sock < 0) {
200 syslog(LOG_ERR, "Unable to open packet socket: %s",
201 strerror(errno));
202 ret = -1;
203 goto out;
204 }
205
206 #ifdef PACKET_RECV_TYPE
207 int pktt = 1 << PACKET_MULTICAST;
208 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
209 #endif
210
211 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
212 &bpf_prog, sizeof(bpf_prog))) {
213 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
214 ret = -1;
215 goto out;
216 }
217
218 struct sockaddr_ll ll = {
219 .sll_family = AF_PACKET,
220 .sll_ifindex = iface->ifindex,
221 .sll_protocol = htons(ETH_P_IPV6),
222 .sll_hatype = 0,
223 .sll_pkttype = 0,
224 .sll_halen = 0,
225 .sll_addr = {0},
226 };
227 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
228
229 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
230 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
231
232 iface->ndp_event.uloop.fd = sock;
233 iface->ndp_event.handle_dgram = handle_solicit;
234 odhcpd_register(&iface->ndp_event);
235
236 // If we already were enabled dump is unnecessary, if not do dump
237 if (!dump_neigh)
238 dump_neigh_table(false);
239 else
240 dump_neigh = false;
241 }
242
243 if (dump_neigh)
244 dump_neigh_table(true);
245
246 out:
247 if (procfd >= 0)
248 close(procfd);
249
250 return ret;
251 }
252
253
254 // Send an ICMP-ECHO. This is less for actually pinging but for the
255 // neighbor cache to be kept up-to-date.
256 static void ping6(struct in6_addr *addr,
257 const struct interface *iface)
258 {
259 struct sockaddr_in6 dest = { .sin6_family = AF_INET6, .sin6_addr = *addr, .sin6_scope_id = iface->ifindex, };
260 struct icmp6_hdr echo = { .icmp6_type = ICMP6_ECHO_REQUEST };
261 struct iovec iov = { .iov_base = &echo, .iov_len = sizeof(echo) };
262 char ipbuf[INET6_ADDRSTRLEN];
263
264 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
265 syslog(LOG_NOTICE, "Pinging for %s%%%s", ipbuf, iface->ifname);
266
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);
270 }
271
272 // Handle solicitations
273 static void handle_solicit(void *addr, void *data, size_t len,
274 struct interface *iface, _unused void *dest)
275 {
276 struct ip6_hdr *ip6 = data;
277 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
278 struct sockaddr_ll *ll = addr;
279 char ipbuf[INET6_ADDRSTRLEN];
280 uint8_t mac[6];
281
282 // Solicitation is for duplicate address detection
283 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
284
285 // Don't process solicit messages on non relay interfaces
286 // Don't forward any non-DAD solicitation for external ifaces
287 // TODO: check if we should even forward DADs for them
288 if (iface->ndp != RELAYD_RELAY || (iface->external && !ns_is_dad))
289 return;
290
291 if (len < sizeof(*ip6) + sizeof(*req))
292 return; // Invalid reqicitation
293
294 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
295 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
296 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
297 return; // Invalid target
298
299 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
300 syslog(LOG_DEBUG, "Got a NS for %s%%%s", ipbuf, iface->ifname);
301
302 odhcpd_get_mac(iface, mac);
303 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
304 return; // Looped back
305
306 struct interface *c;
307 list_for_each_entry(c, &interfaces, head)
308 if (iface != c && c->ndp == RELAYD_RELAY &&
309 (ns_is_dad || !c->external))
310 ping6(&req->nd_ns_target, c);
311 }
312
313 // Use rtnetlink to modify kernel routes
314 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
315 {
316 char ipbuf[INET6_ADDRSTRLEN];
317
318 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
319 syslog(LOG_NOTICE, "%s about %s%%%s",
320 (add) ? "Learned" : "Forgot", ipbuf, iface->ifname);
321
322 if (iface->learn_routes)
323 odhcpd_setup_route(addr, 128, iface, NULL, 1024, add);
324 }
325
326 // Check address update
327 static void check_addr6_updates(struct interface *iface)
328 {
329 struct odhcpd_ipaddr *addr = NULL;
330 ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, true, &addr);
331
332 if (len < 0)
333 return;
334
335 bool change = len != (ssize_t)iface->ia_addr_len;
336 for (ssize_t i = 0; !change && i < len; ++i)
337 if (!IN6_ARE_ADDR_EQUAL(&addr[i].addr.in6, &iface->ia_addr[i].addr.in6) ||
338 (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
339 addr[i].valid < iface->ia_addr[i].valid ||
340 addr[i].preferred < iface->ia_addr[i].preferred)
341 change = true;
342
343 if (change)
344 dhcpv6_ia_preupdate(iface);
345
346 free(iface->ia_addr);
347 iface->ia_addr = addr;
348 iface->ia_addr_len = len;
349
350 if (change)
351 dhcpv6_ia_postupdate(iface);
352
353 if (change) {
354 syslog(LOG_INFO, "Raising SIGUSR1 due to address change on %s", iface->ifname);
355 raise(SIGUSR1);
356 }
357 }
358
359 static void setup_addr_for_relaying(struct in6_addr *addr, struct interface *iface, bool add)
360 {
361 struct interface *c;
362 char ipbuf[INET6_ADDRSTRLEN];
363
364 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
365
366 list_for_each_entry(c, &interfaces, head) {
367 if (iface == c || (c->ndp != RELAYD_RELAY && !add))
368 continue;
369
370 bool neigh_add = (c->ndp == RELAYD_RELAY ? add : false);
371
372 if (odhcpd_setup_proxy_neigh(addr, c, neigh_add))
373 syslog(LOG_DEBUG, "Failed to %s proxy neighbour entry %s%%%s",
374 neigh_add ? "add" : "delete", ipbuf, c->ifname);
375 else
376 syslog(LOG_DEBUG, "%s proxy neighbour entry %s%%%s",
377 neigh_add ? "Added" : "Deleted", ipbuf, c->ifname);
378 }
379 }
380
381 static void handle_rtnl_event(struct odhcpd_event *e)
382 {
383 struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
384
385 nl_recvmsgs_default(ev_sock->sock);
386 }
387
388
389 // Handler for neighbor cache entries from the kernel. This is our source
390 // to learn and unlearn hosts on interfaces.
391 static int cb_rtnl_valid(struct nl_msg *msg, _unused void *arg)
392 {
393 struct nlmsghdr *hdr = nlmsg_hdr(msg);
394 struct in6_addr *addr = NULL;
395 struct interface *iface = NULL;
396 bool add = false;
397 char ipbuf[INET6_ADDRSTRLEN];
398
399 switch (hdr->nlmsg_type) {
400 case RTM_NEWROUTE:
401 case RTM_DELROUTE: {
402 struct rtmsg *rtm = nlmsg_data(hdr);
403
404 if (!nlmsg_valid_hdr(hdr, sizeof(*rtm)) ||
405 rtm->rtm_family != AF_INET6)
406 return NL_SKIP;
407
408 if (rtm->rtm_dst_len == 0) {
409 syslog(LOG_INFO, "Raising SIGUSR1 due to default route change");
410 raise(SIGUSR1);
411 }
412 return NL_OK;
413 }
414
415 case RTM_NEWADDR:
416 add = true;
417 /* fall through */
418 case RTM_DELADDR: {
419 struct ifaddrmsg *ifa = nlmsg_data(hdr);
420 struct nlattr *nla[__IFA_MAX];
421
422 if (!nlmsg_valid_hdr(hdr, sizeof(*ifa)) ||
423 ifa->ifa_family != AF_INET6)
424 return NL_SKIP;
425
426 iface = odhcpd_get_interface_by_index(ifa->ifa_index);
427 if (!iface)
428 return NL_SKIP;
429
430 nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
431 if (!nla[IFA_ADDRESS])
432 return NL_SKIP;
433
434 addr = nla_data(nla[IFA_ADDRESS]);
435 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
436 IN6_IS_ADDR_MULTICAST(addr))
437 return NL_SKIP;
438
439 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
440 syslog(LOG_DEBUG, "Netlink %s %s%%%s", add ? "newaddr" : "deladdr",
441 ipbuf, iface->ifname);
442
443 check_addr6_updates(iface);
444
445 if (iface->ndp != RELAYD_RELAY)
446 break;
447
448 /* handle the relay logic below */
449 setup_addr_for_relaying(addr, iface, add);
450
451 if (!add)
452 dump_neigh_table(false);
453 break;
454 }
455
456 case RTM_NEWNEIGH:
457 add = true;
458 /* fall through */
459 case RTM_DELNEIGH: {
460 struct ndmsg *ndm = nlmsg_data(hdr);
461 struct nlattr *nla[__NDA_MAX];
462
463 if (!nlmsg_valid_hdr(hdr, sizeof(*ndm)) ||
464 ndm->ndm_family != AF_INET6)
465 return NL_SKIP;
466
467 iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex);
468 if (!iface || iface->ndp != RELAYD_RELAY)
469 return (iface ? NL_OK : NL_SKIP);
470
471 nlmsg_parse(hdr, sizeof(*ndm), nla, __NDA_MAX - 1, NULL);
472 if (!nla[NDA_DST])
473 return NL_SKIP;
474
475 addr = nla_data(nla[NDA_DST]);
476 if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
477 IN6_IS_ADDR_MULTICAST(addr))
478 return NL_SKIP;
479
480 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
481 syslog(LOG_DEBUG, "Netlink %s %s%%%s", add ? "newneigh" : "delneigh",
482 ipbuf, iface->ifname);
483
484 if (ndm->ndm_flags & NTF_PROXY) {
485 /* Dump and flush proxy entries */
486 if (hdr->nlmsg_type == RTM_NEWNEIGH) {
487 odhcpd_setup_proxy_neigh(addr, iface, false);
488 setup_route(addr, iface, false);
489 dump_neigh_table(false);
490 }
491
492 return NL_OK;
493 }
494
495 if (add && !(ndm->ndm_state &
496 (NUD_REACHABLE | NUD_STALE | NUD_DELAY | NUD_PROBE |
497 NUD_PERMANENT | NUD_NOARP)))
498 return NL_OK;
499
500 setup_addr_for_relaying(addr, iface, add);
501 setup_route(addr, iface, add);
502
503 if (!add)
504 dump_neigh_table(false);
505 break;
506 }
507
508 default:
509 return NL_SKIP;
510 }
511
512 return NL_OK;
513 }
514
515 static void catch_rtnl_err(struct odhcpd_event *e, int error)
516 {
517 struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
518
519 if (error != ENOBUFS)
520 goto err;
521
522 /* Double netlink event buffer size */
523 ev_sock->sock_bufsize *= 2;
524
525 if (nl_socket_set_buffer_size(ev_sock->sock, ev_sock->sock_bufsize, 0))
526 goto err;
527
528 dump_addr_table(true);
529 return;
530
531 err:
532 odhcpd_deregister(e);
533 }