ndp: add switch/case fallthrough comments
[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/filter.h>
30 #include <linux/neighbour.h>
31
32 #include "dhcpv6.h"
33 #include "odhcpd.h"
34
35
36 static void ndp_netevent_cb(unsigned long event, struct netevent_handler_info *info);
37 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add);
38 static void setup_addr_for_relaying(struct in6_addr *addr, struct interface *iface, bool add);
39 static void handle_solicit(void *addr, void *data, size_t len,
40 struct interface *iface, void *dest);
41
42 static int ping_socket = -1;
43
44 // Filter ICMPv6 messages of type neighbor soliciation
45 static struct sock_filter bpf[] = {
46 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, offsetof(struct ip6_hdr, ip6_nxt)),
47 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
48 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, sizeof(struct ip6_hdr) +
49 offsetof(struct icmp6_hdr, icmp6_type)),
50 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_NEIGHBOR_SOLICIT, 0, 1),
51 BPF_STMT(BPF_RET | BPF_K, 0xffffffff),
52 BPF_STMT(BPF_RET | BPF_K, 0),
53 };
54 static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
55 static struct netevent_handler ndp_netevent_handler = { .cb = ndp_netevent_cb, };
56
57 // Initialize NDP-proxy
58 int ndp_init(void)
59 {
60 int val = 2;
61
62 // Open ICMPv6 socket
63 ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
64 if (ping_socket < 0) {
65 syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno));
66 return -1;
67 }
68
69 setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
70
71 // This is required by RFC 4861
72 val = 255;
73 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
74 setsockopt(ping_socket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val));
75
76 // Filter all packages, we only want to send
77 struct icmp6_filter filt;
78 ICMP6_FILTER_SETBLOCKALL(&filt);
79 setsockopt(ping_socket, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
80
81 netlink_add_netevent_handler(&ndp_netevent_handler);
82
83 return 0;
84 }
85
86 int ndp_setup_interface(struct interface *iface, bool enable)
87 {
88 int ret = 0, procfd;
89 bool dump_neigh = false;
90 char procbuf[64];
91
92 snprintf(procbuf, sizeof(procbuf), "/proc/sys/net/ipv6/conf/%s/proxy_ndp", iface->ifname);
93 procfd = open(procbuf, O_WRONLY);
94
95 if (procfd < 0) {
96 ret = -1;
97 goto out;
98 }
99
100 if (iface->ndp_event.uloop.fd > 0) {
101 uloop_fd_delete(&iface->ndp_event.uloop);
102 close(iface->ndp_event.uloop.fd);
103 iface->ndp_event.uloop.fd = -1;
104
105 if (!enable || iface->ndp != MODE_RELAY)
106 if (write(procfd, "0\n", 2) < 0) {}
107
108 dump_neigh = true;
109 }
110
111 if (enable && iface->ndp == MODE_RELAY) {
112 if (write(procfd, "1\n", 2) < 0) {}
113
114 int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IPV6));
115 if (sock < 0) {
116 syslog(LOG_ERR, "Unable to open packet socket: %s",
117 strerror(errno));
118 ret = -1;
119 goto out;
120 }
121
122 #ifdef PACKET_RECV_TYPE
123 int pktt = 1 << PACKET_MULTICAST;
124 setsockopt(sock, SOL_PACKET, PACKET_RECV_TYPE, &pktt, sizeof(pktt));
125 #endif
126
127 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,
128 &bpf_prog, sizeof(bpf_prog))) {
129 syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno));
130 ret = -1;
131 goto out;
132 }
133
134 struct sockaddr_ll ll = {
135 .sll_family = AF_PACKET,
136 .sll_ifindex = iface->ifindex,
137 .sll_protocol = htons(ETH_P_IPV6),
138 .sll_hatype = 0,
139 .sll_pkttype = 0,
140 .sll_halen = 0,
141 .sll_addr = {0},
142 };
143 bind(sock, (struct sockaddr*)&ll, sizeof(ll));
144
145 struct packet_mreq mreq = {iface->ifindex, PACKET_MR_ALLMULTI, ETH_ALEN, {0}};
146 setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
147
148 iface->ndp_event.uloop.fd = sock;
149 iface->ndp_event.handle_dgram = handle_solicit;
150 odhcpd_register(&iface->ndp_event);
151
152 // If we already were enabled dump is unnecessary, if not do dump
153 if (!dump_neigh)
154 netlink_dump_neigh_table(false);
155 else
156 dump_neigh = false;
157 }
158
159 if (dump_neigh)
160 netlink_dump_neigh_table(true);
161
162 out:
163 if (procfd >= 0)
164 close(procfd);
165
166 return ret;
167 }
168
169 static void ndp_netevent_cb(unsigned long event, struct netevent_handler_info *info)
170 {
171 struct interface *iface = info->iface;
172 bool add = true;
173
174 if (!iface || iface->ndp == MODE_DISABLED)
175 return;
176
177 switch (event) {
178 case NETEV_ADDR6_DEL:
179 add = false;
180 netlink_dump_neigh_table(false);
181 /* fall through */
182 case NETEV_ADDR6_ADD:
183 setup_addr_for_relaying(&info->addr.in6, iface, add);
184 break;
185 case NETEV_NEIGH6_DEL:
186 add = false;
187 /* fall through */
188 case NETEV_NEIGH6_ADD:
189 if (info->neigh.flags & NTF_PROXY) {
190 if (add) {
191 netlink_setup_proxy_neigh(&info->neigh.dst.in6, iface->ifindex, false);
192 setup_route(&info->neigh.dst.in6, iface, false);
193 netlink_dump_neigh_table(false);
194 }
195 break;
196 }
197
198 if (add &&
199 !(info->neigh.state &
200 (NUD_REACHABLE|NUD_STALE|NUD_DELAY|NUD_PROBE|NUD_PERMANENT|NUD_NOARP)))
201 break;
202
203 setup_addr_for_relaying(&info->neigh.dst.in6, iface, add);
204 setup_route(&info->neigh.dst.in6, iface, add);
205
206 if (!add)
207 netlink_dump_neigh_table(false);
208 break;
209 default:
210 break;
211 }
212 }
213
214 // Send an ICMP-ECHO. This is less for actually pinging but for the
215 // neighbor cache to be kept up-to-date.
216 static void ping6(struct in6_addr *addr,
217 const struct interface *iface)
218 {
219 struct sockaddr_in6 dest = { .sin6_family = AF_INET6, .sin6_addr = *addr, .sin6_scope_id = iface->ifindex, };
220 struct icmp6_hdr echo = { .icmp6_type = ICMP6_ECHO_REQUEST };
221 struct iovec iov = { .iov_base = &echo, .iov_len = sizeof(echo) };
222 char ipbuf[INET6_ADDRSTRLEN];
223
224 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
225 syslog(LOG_NOTICE, "Pinging for %s%%%s", ipbuf, iface->ifname);
226
227 netlink_setup_route(addr, 128, iface->ifindex, NULL, 128, true);
228 odhcpd_send(ping_socket, &dest, &iov, 1, iface);
229 netlink_setup_route(addr, 128, iface->ifindex, NULL, 128, false);
230 }
231
232 // Handle solicitations
233 static void handle_solicit(void *addr, void *data, size_t len,
234 struct interface *iface, _unused void *dest)
235 {
236 struct ip6_hdr *ip6 = data;
237 struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1];
238 struct sockaddr_ll *ll = addr;
239 char ipbuf[INET6_ADDRSTRLEN];
240 uint8_t mac[6];
241
242 // Solicitation is for duplicate address detection
243 bool ns_is_dad = IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src);
244
245 // Don't process solicit messages on non relay interfaces
246 // Don't forward any non-DAD solicitation for external ifaces
247 // TODO: check if we should even forward DADs for them
248 if (iface->ndp != MODE_RELAY || (iface->external && !ns_is_dad))
249 return;
250
251 if (len < sizeof(*ip6) + sizeof(*req))
252 return; // Invalid reqicitation
253
254 if (IN6_IS_ADDR_LINKLOCAL(&req->nd_ns_target) ||
255 IN6_IS_ADDR_LOOPBACK(&req->nd_ns_target) ||
256 IN6_IS_ADDR_MULTICAST(&req->nd_ns_target))
257 return; // Invalid target
258
259 inet_ntop(AF_INET6, &req->nd_ns_target, ipbuf, sizeof(ipbuf));
260 syslog(LOG_DEBUG, "Got a NS for %s%%%s", ipbuf, iface->ifname);
261
262 odhcpd_get_mac(iface, mac);
263 if (!memcmp(ll->sll_addr, mac, sizeof(mac)))
264 return; // Looped back
265
266 struct interface *c;
267 list_for_each_entry(c, &interfaces, head)
268 if (iface != c && c->ndp == MODE_RELAY &&
269 (ns_is_dad || !c->external))
270 ping6(&req->nd_ns_target, c);
271 }
272
273 // Use rtnetlink to modify kernel routes
274 static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
275 {
276 char ipbuf[INET6_ADDRSTRLEN];
277
278 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
279 syslog(LOG_NOTICE, "%s about %s%s%%%s",
280 (add) ? "Learning" : "Forgetting",
281 iface->learn_routes ? "proxy routing for " : "",
282 ipbuf, iface->ifname);
283
284 if (iface->learn_routes)
285 netlink_setup_route(addr, 128, iface->ifindex, NULL, 1024, add);
286 }
287
288 static void setup_addr_for_relaying(struct in6_addr *addr, struct interface *iface, bool add)
289 {
290 struct interface *c;
291 char ipbuf[INET6_ADDRSTRLEN];
292
293 inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf));
294
295 list_for_each_entry(c, &interfaces, head) {
296 if (iface == c || (c->ndp != MODE_RELAY && !add))
297 continue;
298
299 bool neigh_add = (c->ndp == MODE_RELAY ? add : false);
300
301 if (netlink_setup_proxy_neigh(addr, c->ifindex, neigh_add))
302 syslog(LOG_DEBUG, "Failed to %s proxy neighbour entry %s%%%s",
303 neigh_add ? "add" : "delete", ipbuf, c->ifname);
304 else
305 syslog(LOG_DEBUG, "%s proxy neighbour entry %s%%%s",
306 neigh_add ? "Added" : "Deleted", ipbuf, c->ifname);
307 }
308 }