• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcp6c/src/ra.c

  1 /**
  2  * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
  3  * Copyright (C) 2017-2018 Hans Dedecker <dedeckeh@gmail.com>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License v2 as published by
  7  * the Free Software Foundation.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  */
 15 
 16 #include <errno.h>
 17 #include <fcntl.h>
 18 #include <stdio.h>
 19 #include <signal.h>
 20 #include <string.h>
 21 #include <stddef.h>
 22 #include <stdbool.h>
 23 #include <syslog.h>
 24 #include <unistd.h>
 25 #include <resolv.h>
 26 #include <alloca.h>
 27 
 28 #include <net/if.h>
 29 #include <arpa/inet.h>
 30 #include <sys/socket.h>
 31 #include <sys/types.h>
 32 #include <netinet/in.h>
 33 #include <netinet/icmp6.h>
 34 
 35 #include <linux/rtnetlink.h>
 36 
 37 #ifndef SOL_NETLINK
 38 #define SOL_NETLINK 270
 39 #endif
 40 
 41 #ifndef NETLINK_ADD_MEMBERSHIP
 42 #define NETLINK_ADD_MEMBERSHIP 1
 43 #endif
 44 
 45 #ifndef IFF_LOWER_UP
 46 #define IFF_LOWER_UP 0x10000
 47 #endif
 48 
 49 #include "odhcp6c.h"
 50 #include "ra.h"
 51 
 52 static bool nocarrier = false;
 53 
 54 static int sock = -1, rtnl = -1;
 55 static int if_index = 0;
 56 static char if_name[IF_NAMESIZE] = {0};
 57 static volatile int rs_attempt = 0;
 58 static struct in6_addr lladdr = IN6ADDR_ANY_INIT;
 59 static unsigned int ra_options = 0;
 60 static unsigned int ra_holdoff_interval = 0;
 61 static int ra_hoplimit = 0;
 62 static int ra_mtu = 0;
 63 static int ra_reachable = 0;
 64 static int ra_retransmit = 0;
 65 
 66 struct {
 67         struct icmp6_hdr hdr;
 68         struct icmpv6_opt lladdr;
 69 } rs = {
 70         .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}},
 71         .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
 72 };
 73 
 74 static void ra_send_rs(int signal __attribute__((unused)));
 75 
 76 int ra_init(const char *ifname, const struct in6_addr *ifid,
 77                 unsigned int options, unsigned int holdoff_interval)
 78 {
 79         ra_options = options;
 80         ra_holdoff_interval = holdoff_interval;
 81 
 82         const pid_t ourpid = getpid();
 83         sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
 84         if (sock < 0)
 85                 goto failure;
 86 
 87         if_index = if_nametoindex(ifname);
 88         if (!if_index)
 89                 goto failure;
 90 
 91         strncpy(if_name, ifname, sizeof(if_name) - 1);
 92         lladdr = *ifid;
 93 
 94         rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
 95         if (rtnl < 0)
 96                 goto failure;
 97 
 98         struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
 99         if (connect(rtnl, (const struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)) < 0)
100                 goto failure;
101 
102         int val = RTNLGRP_LINK;
103         if (setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val)) < 0)
104                 goto failure;
105 
106         if (fcntl(rtnl, F_SETOWN, ourpid) < 0)
107                 goto failure;
108 
109         if (fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC) < 0)
110                 goto failure;
111 
112         struct {
113                 struct nlmsghdr hdr;
114                 struct ifinfomsg ifi;
115         } req = {
116                 .hdr = {sizeof(req), RTM_GETLINK, NLM_F_REQUEST, 1, 0},
117                 .ifi = {.ifi_index = if_index}
118         };
119         if (send(rtnl, &req, sizeof(req), 0) < 0)
120                 goto failure;
121 
122         ra_link_up();
123 
124         // Filter ICMPv6 package types
125         struct icmp6_filter filt;
126         ICMP6_FILTER_SETBLOCKALL(&filt);
127         ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
128         if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt)) < 0)
129                 goto failure;
130 
131         // Bind to all-nodes
132         struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
133         if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an)) < 0)
134                 goto failure;
135 
136         // Let the kernel compute our checksums
137         val = 2;
138         if (setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val)) < 0)
139                 goto failure;
140 
141         // This is required by RFC 4861
142         val = 255;
143         if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) < 0)
144                 goto failure;
145 
146         // Receive multicast hops
147         val = 1;
148         if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val)) < 0)
149                 goto failure;
150 
151         // Bind to one device
152         if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0)
153                 goto failure;
154 
155         // Add async-mode
156         if (fcntl(sock, F_SETOWN, ourpid) < 0)
157                 goto failure;
158 
159         val = fcntl(sock, F_GETFL);
160         if (val < 0)
161                 goto failure;
162 
163         if (fcntl(sock, F_SETFL, val | O_ASYNC) < 0)
164                 goto failure;
165 
166         // Send RS
167         signal(SIGALRM, ra_send_rs);
168         ra_send_rs(SIGALRM);
169 
170         return 0;
171 
172 failure:
173         if (sock >= 0)
174                 close(sock);
175 
176         if (rtnl >= 0)
177                 close(rtnl);
178 
179         return -1;
180 }
181 
182 static void ra_send_rs(int signal __attribute__((unused)))
183 {
184         const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
185         const struct icmpv6_opt llnull = {ND_OPT_SOURCE_LINKADDR, 1, {0}};
186         size_t len;
187 
188         if ((rs_attempt % 2 == 0) && memcmp(&rs.lladdr, &llnull, sizeof(llnull)))
189                 len = sizeof(rs);
190         else
191                 len = sizeof(struct icmp6_hdr);
192 
193         if (sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
194                 syslog(LOG_ERR, "Failed to send RS (%s)",  strerror(errno));
195 
196         if (++rs_attempt <= 3)
197                 alarm(4);
198 }
199 
200 static int16_t pref_to_priority(uint8_t flags)
201 {
202         flags = (flags >> 3) & 0x03;
203 
204         return (flags == 0x0) ? 512 : (flags == 0x1) ? 384 :
205                         (flags == 0x3) ? 640 : -1;
206 }
207 
208 bool ra_link_up(void)
209 {
210         static bool firstcall = true;
211         struct {
212                 struct nlmsghdr hdr;
213                 struct ifinfomsg msg;
214                 uint8_t pad[4000];
215         } resp;
216         bool ret = false;
217         ssize_t read;
218 
219         do {
220                 read = recv(rtnl, &resp, sizeof(resp), MSG_DONTWAIT);
221 
222                 if (read < 0 || !NLMSG_OK(&resp.hdr, (size_t)read) ||
223                                 resp.hdr.nlmsg_type != RTM_NEWLINK ||
224                                 resp.msg.ifi_index != if_index)
225                         continue;
226 
227                 ssize_t alen = NLMSG_PAYLOAD(&resp.hdr, sizeof(resp.msg));
228                 for (struct rtattr *rta = (struct rtattr*)(resp.pad);
229                                 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) {
230                         if (rta->rta_type == IFLA_ADDRESS &&
231                                         RTA_PAYLOAD(rta) >= sizeof(rs.lladdr.data))
232                                 memcpy(rs.lladdr.data, RTA_DATA(rta), sizeof(rs.lladdr.data));
233                 }
234 
235                 bool hascarrier = resp.msg.ifi_flags & IFF_LOWER_UP;
236                 if (!firstcall && nocarrier != !hascarrier)
237                         ret = true;
238 
239                 nocarrier = !hascarrier;
240                 firstcall = false;
241         } while (read > 0);
242 
243         if (ret) {
244                 syslog(LOG_NOTICE, "carrier => %i event on %s", (int)!nocarrier, if_name);
245 
246                 rs_attempt = 0;
247                 ra_send_rs(SIGALRM);
248         }
249 
250         return ret;
251 }
252 
253 static bool ra_icmpv6_valid(struct sockaddr_in6 *source, int hlim, uint8_t *data, size_t len)
254 {
255         struct icmp6_hdr *hdr = (struct icmp6_hdr*)data;
256         struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
257 
258         if (hlim != 255 || len < sizeof(*hdr) || hdr->icmp6_code)
259                 return false;
260 
261         switch (hdr->icmp6_type) {
262         case ND_ROUTER_ADVERT:
263                 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
264                         return false;
265 
266                 opt = (struct icmpv6_opt*)((struct nd_router_advert*)data + 1);
267                 break;
268 
269         default:
270                 return false;
271         }
272 
273         icmpv6_for_each_option(opt, opt, end)
274                 ;
275 
276         return opt == end;
277 }
278 
279 static bool ra_set_hoplimit(int val)
280 {
281         if (val > 0 && val != ra_hoplimit) {
282                 ra_hoplimit = val;
283                 return true;
284         }
285 
286         return false;
287 }
288 
289 static bool ra_set_mtu(int val)
290 {
291         if (val >= 1280 && val <= 65535 && ra_mtu != val) {
292                 ra_mtu = val;
293                 return true;
294         }
295 
296         return false;
297 }
298 
299 static bool ra_set_reachable(int val)
300 {
301         if (val > 0 && val <= 3600000 && ra_reachable != val) {
302                 ra_reachable = val;
303                 return true;
304         }
305 
306         return false;
307 }
308 
309 static bool ra_set_retransmit(int val)
310 {
311         if (val > 0 && val <= 60000 && ra_retransmit != val) {
312                 ra_retransmit = val;
313                 return true;
314         }
315 
316         return false;
317 }
318 
319 int ra_get_hoplimit(void)
320 {
321         return ra_hoplimit;
322 }
323 
324 int ra_get_mtu(void)
325 {
326         return ra_mtu;
327 }
328 
329 int ra_get_reachable(void)
330 {
331         return ra_reachable;
332 }
333 
334 int ra_get_retransmit(void)
335 {
336         return ra_retransmit;
337 }
338 
339 bool ra_process(void)
340 {
341         bool found = false;
342         bool changed = false;
343         uint8_t buf[1500] _aligned(4);
344         union {
345                 struct cmsghdr hdr;
346                 uint8_t buf[CMSG_SPACE(sizeof(int))];
347         } cmsg_buf;
348         struct nd_router_advert *adv = (struct nd_router_advert*)buf;
349         struct odhcp6c_entry *entry = alloca(sizeof(*entry) + 256);
350         const struct in6_addr any = IN6ADDR_ANY_INIT;
351 
352         memset(entry, 0, sizeof(*entry));
353 
354         if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) {
355                 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
356                 socklen_t alen = sizeof(addr);
357                 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
358 
359                 if (sock >= 0) {
360                         if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
361                                         !getsockname(sock, (struct sockaddr*)&addr, &alen))
362                                 lladdr = addr.sin6_addr;
363 
364                         close(sock);
365                 }
366         }
367 
368         while (true) {
369                 struct sockaddr_in6 from;
370                 struct iovec iov = {buf, sizeof(buf)};
371                 struct msghdr msg = {
372                         .msg_name = (void *) &from,
373                         .msg_namelen = sizeof(from),
374                         .msg_iov = &iov,
375                         .msg_iovlen = 1,
376                         .msg_control = cmsg_buf.buf,
377                         .msg_controllen = sizeof(cmsg_buf),
378                         .msg_flags = 0
379                 };
380                 struct icmpv6_opt *opt;
381                 uint32_t router_valid;
382                 int hlim = 0;
383 
384                 ssize_t len = recvmsg(sock, &msg, MSG_DONTWAIT);
385                 if (len <= 0)
386                         break;
387 
388                 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr))
389                         continue;
390 
391                 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL;
392                                 ch = CMSG_NXTHDR(&msg, ch))
393                         if (ch->cmsg_level == IPPROTO_IPV6 &&
394                                         ch->cmsg_type == IPV6_HOPLIMIT)
395                                 memcpy(&hlim, CMSG_DATA(ch), sizeof(hlim));
396 
397                 if (!ra_icmpv6_valid(&from, hlim, buf, len))
398                         continue;
399 
400                 if (!found) {
401                         odhcp6c_expire();
402                         found = true;
403                 }
404 
405                 router_valid = ntohs(adv->nd_ra_router_lifetime);
406 
407                 /* RFC4861 ยง6.3.7
408                  * Once the host sends a Router Solicitation, and receives a valid
409                  * Router Advertisement with a non-zero Router Lifetime, the host MUST
410                  * desist from sending additional solicitations on that interface
411                  * Moreover, a host SHOULD send at least one solicitation in the case
412                  * where an advertisement is received prior to having sent a solicitation.
413                  */
414                 if (rs_attempt > 0 && router_valid > 0) {
415                         alarm(0);
416                         rs_attempt = 0;
417                 }
418 
419                 // Parse default route
420                 entry->target = any;
421                 entry->length = 0;
422                 entry->router = from.sin6_addr;
423                 entry->priority = pref_to_priority(adv->nd_ra_flags_reserved);
424                 if (entry->priority < 0)
425                         entry->priority = pref_to_priority(0);
426 
427                 entry->valid = router_valid;
428                 entry->preferred = entry->valid;
429                 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry,
430                                                 0, ra_holdoff_interval);
431 
432                 // Parse hoplimit
433                 changed |= ra_set_hoplimit(adv->nd_ra_curhoplimit);
434 
435                 // Parse ND parameters
436                 changed |= ra_set_reachable(ntohl(adv->nd_ra_reachable));
437                 changed |= ra_set_retransmit(ntohl(adv->nd_ra_retransmit));
438 
439                 // Evaluate options
440                 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
441                         if (opt->type == ND_OPT_MTU) {
442                                 uint32_t *mtu = (uint32_t*)&opt->data[2];
443                                 changed |= ra_set_mtu(ntohl(*mtu));
444                         } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
445                                 struct icmpv6_opt_route_info *ri = (struct icmpv6_opt_route_info *)opt;
446 
447                                 if (ri->prefix_len > 128) {
448                                         continue;
449                                 } else if (ri->prefix_len > 64) {
450                                         if (ri->len < 2)
451                                                 continue;
452                                 } else if (ri->prefix_len > 0) {
453                                         if (ri->len < 1)
454                                                 continue;
455                                 }
456 
457                                 entry->router = from.sin6_addr;
458                                 entry->target = any;
459                                 entry->priority = pref_to_priority(ri->flags);
460                                 entry->length = ri->prefix_len;
461                                 entry->valid = ntohl(ri->lifetime);
462                                 memcpy(&entry->target, ri->prefix, (ri->len - 1) * 8);
463 
464                                 if (IN6_IS_ADDR_LINKLOCAL(&entry->target)
465                                                 || IN6_IS_ADDR_LOOPBACK(&entry->target)
466                                                 || IN6_IS_ADDR_MULTICAST(&entry->target))
467                                         continue;
468 
469                                 if (entry->priority > 0)
470                                         changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry,
471                                                                         0, ra_holdoff_interval);
472                         } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
473                                 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
474                                 entry->router = any;
475                                 entry->target = pinfo->nd_opt_pi_prefix;
476                                 entry->priority = 256;
477                                 entry->length = pinfo->nd_opt_pi_prefix_len;
478                                 entry->valid = ntohl(pinfo->nd_opt_pi_valid_time);
479                                 entry->preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
480 
481                                 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
482                                                 || IN6_IS_ADDR_LOOPBACK(&entry->target)
483                                                 || IN6_IS_ADDR_MULTICAST(&entry->target)
484                                                 || entry->valid < entry->preferred)
485                                         continue;
486 
487                                 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
488                                         changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry,
489                                                                         7200, ra_holdoff_interval);
490 
491                                 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
492                                                 pinfo->nd_opt_pi_prefix_len != 64)
493                                         continue;
494 
495                                 entry->target.s6_addr32[2] = lladdr.s6_addr32[2];
496                                 entry->target.s6_addr32[3] = lladdr.s6_addr32[3];
497 
498                                 changed |= odhcp6c_update_entry(STATE_RA_PREFIX, entry,
499                                                                 7200, ra_holdoff_interval);
500                         } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
501                                 entry->router = from.sin6_addr;
502                                 entry->priority = 0;
503                                 entry->length = 128;
504                                 uint32_t *valid = (uint32_t*)&opt->data[2];
505                                 entry->valid = ntohl(*valid);
506                                 entry->preferred = 0;
507 
508                                 for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
509                                         memcpy(&entry->target, &opt->data[6 + i * sizeof(entry->target)],
510                                                         sizeof(entry->target));
511                                         changed |= odhcp6c_update_entry(STATE_RA_DNS, entry,
512                                                                         0, ra_holdoff_interval);
513                                 }
514                         } else if (opt->type == ND_OPT_DNSSL && opt->len > 1) {
515                                 uint32_t *valid = (uint32_t*)&opt->data[2];
516                                 uint8_t *buf = &opt->data[6];
517                                 uint8_t *end = &buf[(opt->len - 1) * 8];
518 
519                                 entry->router = from.sin6_addr;
520                                 entry->valid = ntohl(*valid);
521 
522                                 while (buf < end) {
523                                         int len = dn_expand(buf, end, buf, (char*)entry->auxtarget, 256);
524                                         if (len < 1)
525                                                 break;
526 
527                                         buf = &buf[len];
528                                         entry->auxlen = strlen((char*)entry->auxtarget);
529 
530                                         if (entry->auxlen == 0)
531                                                 continue;
532 
533                                         changed |= odhcp6c_update_entry(STATE_RA_SEARCH, entry,
534                                                                         0, ra_holdoff_interval);
535                                         entry->auxlen = 0;
536                                 }
537                         }
538                 }
539 
540                 if (ra_options & RA_RDNSS_DEFAULT_LIFETIME) {
541                         int states[2] = {STATE_RA_DNS, STATE_RA_SEARCH};
542 
543                         for (size_t i = 0; i < 2; ++i) {
544                                 size_t ra_dns_len;
545                                 uint8_t *start = odhcp6c_get_state(states[i], &ra_dns_len);
546 
547                                 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
548                                                         (uint8_t*)c < &start[ra_dns_len] &&
549                                                         (uint8_t*)odhcp6c_next_entry(c) <= &start[ra_dns_len];
550                                                         c = odhcp6c_next_entry(c)) {
551                                         if (IN6_ARE_ADDR_EQUAL(&c->router, &from.sin6_addr) &&
552                                                         c->valid > router_valid)
553                                                 c->valid = router_valid;
554                                 }
555                         }
556                 }
557         }
558 
559         if (found)
560                 odhcp6c_expire();
561 
562         return found && changed;
563 }
564 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt