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.
23 #include <arpa/inet.h>
24 #include <net/route.h>
30 static void forward_router_solicitation(const struct interface
*iface
);
31 static void forward_router_advertisement(uint8_t *data
, size_t len
);
33 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
34 struct interface
*iface
, void *dest
);
35 static void trigger_router_advert(struct uloop_timeout
*event
);
36 static void sigusr1_refresh(int signal
);
38 static struct odhcpd_event router_event
= {{.fd
= -1}, handle_icmpv6
, NULL
};
40 static FILE *fp_route
= NULL
;
43 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
48 int sock
= socket(AF_INET6
, SOCK_RAW
| SOCK_CLOEXEC
, IPPROTO_ICMPV6
);
49 if (sock
< 0 && errno
!= EAFNOSUPPORT
) {
50 syslog(LOG_ERR
, "Failed to open RAW-socket: %s", strerror(errno
));
54 // Let the kernel compute our checksums
56 setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
58 // This is required by RFC 4861
60 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
61 setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
, sizeof(val
));
63 // We need to know the source interface
65 setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
));
66 setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
, sizeof(val
));
70 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
, sizeof(val
));
72 // Filter ICMPv6 package types
73 struct icmp6_filter filt
;
74 ICMP6_FILTER_SETBLOCKALL(&filt
);
75 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
76 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
77 setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
, sizeof(filt
));
80 router_event
.uloop
.fd
= sock
;
81 odhcpd_register(&router_event
);
83 if (!(fp_route
= fopen("/proc/net/ipv6_route", "r")))
84 syslog(LOG_ERR
, "Failed to open routing table: %s",
87 signal(SIGUSR1
, sigusr1_refresh
);
92 int setup_router_interface(struct interface
*iface
, bool enable
)
94 if (!fp_route
|| router_event
.uloop
.fd
< 0)
97 struct ipv6_mreq all_nodes
= {ALL_IPV6_NODES
, iface
->ifindex
};
98 struct ipv6_mreq all_routers
= {ALL_IPV6_ROUTERS
, iface
->ifindex
};
100 uloop_timeout_cancel(&iface
->timer_rs
);
101 iface
->timer_rs
.cb
= NULL
;
103 if (iface
->ifindex
<= 0)
106 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
107 &all_nodes
, sizeof(all_nodes
));
108 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
109 &all_routers
, sizeof(all_routers
));
113 trigger_router_advert(&iface
->timer_rs
);
115 void *mreq
= &all_routers
;
117 if (iface
->ra
== RELAYD_RELAY
&& iface
->master
) {
119 forward_router_solicitation(iface
);
120 } else if (iface
->ra
== RELAYD_SERVER
&& !iface
->master
) {
121 iface
->timer_rs
.cb
= trigger_router_advert
;
122 uloop_timeout_set(&iface
->timer_rs
, 1000);
125 if (iface
->ra
== RELAYD_RELAY
|| (iface
->ra
== RELAYD_SERVER
&& !iface
->master
))
126 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
,
127 IPV6_ADD_MEMBERSHIP
, mreq
, sizeof(all_nodes
));
133 // Signal handler to resend all RDs
134 static void sigusr1_refresh(_unused
int signal
)
136 struct interface
*iface
;
137 list_for_each_entry(iface
, &interfaces
, head
)
138 if (iface
->ra
== RELAYD_SERVER
&& !iface
->master
)
139 uloop_timeout_set(&iface
->timer_rs
, 1000);
142 static bool router_icmpv6_valid(struct sockaddr_in6
*source
, uint8_t *data
, size_t len
)
144 struct icmp6_hdr
*hdr
= (struct icmp6_hdr
*)data
;
145 struct icmpv6_opt
*opt
, *end
= (struct icmpv6_opt
*)&data
[len
];
147 /* Hoplimit is already checked in odhcpd_receive_packets */
148 if (len
< sizeof(*hdr
) || hdr
->icmp6_code
)
151 switch (hdr
->icmp6_type
) {
152 case ND_ROUTER_ADVERT
:
153 if (!IN6_IS_ADDR_LINKLOCAL(&source
->sin6_addr
))
156 opt
= (struct icmpv6_opt
*)((struct nd_router_advert
*)data
+ 1);
159 case ND_ROUTER_SOLICIT
:
160 opt
= (struct icmpv6_opt
*)((struct nd_router_solicit
*)data
+ 1);
167 icmpv6_for_each_option(opt
, opt
, end
)
168 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
&&
169 IN6_IS_ADDR_UNSPECIFIED(&source
->sin6_addr
) &&
170 hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
173 // Check all options parsed successfully
178 // Detect whether a default route exists, also find the source prefixes
179 static bool parse_routes(struct odhcpd_ipaddr
*n
, ssize_t len
)
183 char line
[512], ifname
[16];
184 bool found_default
= false;
185 struct odhcpd_ipaddr p
= {IN6ADDR_ANY_INIT
, 0, 0, 0, 0};
186 while (fgets(line
, sizeof(line
), fp_route
)) {
188 if (sscanf(line
, "00000000000000000000000000000000 00 "
189 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname
) &&
190 strcmp(ifname
, "lo")) {
191 found_default
= true;
192 } else if (sscanf(line
, "%8" SCNx32
"%8" SCNx32
"%*8" SCNx32
"%*8" SCNx32
" %hhx %*s "
193 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32
" lo",
194 &p
.addr
.s6_addr32
[0], &p
.addr
.s6_addr32
[1], &p
.prefix
, &rflags
) &&
195 p
.prefix
> 0 && (rflags
& RTF_NONEXTHOP
) && (rflags
& RTF_REJECT
)) {
196 // Find source prefixes by scanning through unreachable-routes
197 p
.addr
.s6_addr32
[0] = htonl(p
.addr
.s6_addr32
[0]);
198 p
.addr
.s6_addr32
[1] = htonl(p
.addr
.s6_addr32
[1]);
200 for (ssize_t i
= 0; i
< len
; ++i
) {
201 if (n
[i
].prefix
<= 64 && n
[i
].prefix
>= p
.prefix
&&
202 !odhcpd_bmemcmp(&p
.addr
, &n
[i
].addr
, p
.prefix
)) {
203 n
[i
].dprefix
= p
.prefix
;
211 return found_default
;
214 // Router Advert server mode
215 static uint64_t send_router_advert(struct interface
*iface
, const struct in6_addr
*from
)
217 time_t now
= odhcpd_time();
218 int mtu
= odhcpd_get_interface_config(iface
->ifname
, "mtu");
219 int hlim
= odhcpd_get_interface_config(iface
->ifname
, "hop_limit");
225 struct nd_router_advert h
;
226 struct icmpv6_opt lladdr
;
227 struct nd_opt_mtu mtu
;
228 struct nd_opt_prefix_info prefix
[sizeof(iface
->ia_addr
) / sizeof(*iface
->ia_addr
)];
230 .h
= {{.icmp6_type
= ND_ROUTER_ADVERT
, .icmp6_code
= 0}, 0, 0},
231 .lladdr
= {ND_OPT_SOURCE_LINKADDR
, 1, {0}},
232 .mtu
= {ND_OPT_MTU
, 1, 0, htonl(mtu
)},
236 adv
.h
.nd_ra_curhoplimit
= hlim
;
239 adv
.h
.nd_ra_flags_reserved
= ND_RA_FLAG_OTHER
;
241 if (iface
->managed
>= RELAYD_MANAGED_MFLAG
)
242 adv
.h
.nd_ra_flags_reserved
|= ND_RA_FLAG_MANAGED
;
244 if (iface
->route_preference
< 0)
245 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_LOW
;
246 else if (iface
->route_preference
> 0)
247 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_HIGH
;
248 odhcpd_get_mac(iface
, adv
.lladdr
.data
);
250 // If not currently shutting down
251 struct odhcpd_ipaddr addrs
[RELAYD_MAX_ADDRS
];
253 int64_t minvalid
= INT64_MAX
;
256 if (iface
->timer_rs
.cb
) {
257 ipcnt
= iface
->ia_addr_len
;
258 memcpy(addrs
, iface
->ia_addr
, ipcnt
* sizeof(*addrs
));
260 // Check default route
261 if (iface
->default_router
> 1)
262 adv
.h
.nd_ra_router_lifetime
= htons(iface
->default_router
);
263 else if (parse_routes(addrs
, ipcnt
))
264 adv
.h
.nd_ra_router_lifetime
= htons(1);
266 syslog(LOG_INFO
, "Initial router lifetime %d, %d address(es) available",
267 ntohs(adv
.h
.nd_ra_router_lifetime
), (int)ipcnt
);
270 // Construct Prefix Information options
273 struct in6_addr dns_pref
, *dns_addr
= &dns_pref
;
276 odhcpd_get_linklocal_interface_address(iface
->ifindex
, &dns_pref
);
278 for (ssize_t i
= 0; i
< ipcnt
; ++i
) {
279 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
281 if (addr
->prefix
> 96 || addr
->valid
<= (uint32_t)now
) {
282 char namebuf
[INET6_ADDRSTRLEN
];
284 inet_ntop(AF_INET6
, addr
, namebuf
, sizeof(namebuf
));
285 syslog(LOG_INFO
, "Address %s (prefix %d, valid %u) not suitable",
286 namebuf
, addr
->prefix
, addr
->valid
);
290 struct nd_opt_prefix_info
*p
= NULL
;
291 for (size_t i
= 0; i
< cnt
; ++i
) {
292 if (addr
->prefix
== adv
.prefix
[i
].nd_opt_pi_prefix_len
&&
293 !odhcpd_bmemcmp(&adv
.prefix
[i
].nd_opt_pi_prefix
,
294 &addr
->addr
, addr
->prefix
))
299 if (cnt
>= ARRAY_SIZE(adv
.prefix
))
302 p
= &adv
.prefix
[cnt
++];
305 if (addr
->preferred
> (uint32_t)now
&&
306 minvalid
> 1000LL * TIME_LEFT(addr
->valid
, now
))
307 minvalid
= 1000LL * TIME_LEFT(addr
->valid
, now
);
309 uint32_t this_lifetime
= TIME_LEFT(addr
->valid
, now
);
310 if (this_lifetime
> UINT16_MAX
)
311 this_lifetime
= UINT16_MAX
;
312 if (((addr
->addr
.s6_addr
[0] & 0xfe) != 0xfc || iface
->default_router
)
313 && adv
.h
.nd_ra_router_lifetime
314 && ntohs(adv
.h
.nd_ra_router_lifetime
) < this_lifetime
) {
315 adv
.h
.nd_ra_router_lifetime
= htons(this_lifetime
);
317 syslog(LOG_DEBUG
, "Updating router lifetime to %d", this_lifetime
);
320 odhcpd_bmemcpy(&p
->nd_opt_pi_prefix
, &addr
->addr
,
321 (iface
->ra_advrouter
) ? 128 : addr
->prefix
);
322 p
->nd_opt_pi_type
= ND_OPT_PREFIX_INFORMATION
;
323 p
->nd_opt_pi_len
= 4;
324 p
->nd_opt_pi_prefix_len
= (addr
->prefix
< 64) ? 64 : addr
->prefix
;
325 p
->nd_opt_pi_flags_reserved
= 0;
326 if (!iface
->ra_not_onlink
)
327 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_ONLINK
;
328 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& addr
->prefix
<= 64)
329 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_AUTO
;
330 if (iface
->ra_advrouter
)
331 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_RADDR
;
332 p
->nd_opt_pi_valid_time
= htonl(TIME_LEFT(addr
->valid
, now
));
333 if (addr
->preferred
> (uint32_t)now
)
334 p
->nd_opt_pi_preferred_time
= htonl(TIME_LEFT(addr
->preferred
, now
));
335 else if (addr
->valid
- now
< 7200)
336 p
->nd_opt_pi_valid_time
= 0;
339 if (!iface
->default_router
&& adv
.h
.nd_ra_router_lifetime
== htons(1)) {
340 syslog(LOG_WARNING
, "A default route is present but there is no public prefix "
341 "on %s thus we don't announce a default route!", iface
->ifname
);
342 adv
.h
.nd_ra_router_lifetime
= 0;
346 if (iface
->dns_cnt
> 0) {
347 dns_addr
= iface
->dns
;
348 dns_cnt
= iface
->dns_cnt
;
351 if (!dns_addr
|| IN6_IS_ADDR_UNSPECIFIED(dns_addr
))
360 } dns
= {ND_OPT_RECURSIVE_DNS
, (1 + (2 * dns_cnt
)), 0, 0, 0};
364 // DNS Search options
365 uint8_t search_buf
[256], *search_domain
= iface
->search
;
366 size_t search_len
= iface
->search_len
, search_padded
= 0;
368 if (!search_domain
&& !res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
369 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
370 sizeof(search_buf
), NULL
, NULL
);
372 search_domain
= search_buf
;
378 search_padded
= ((search_len
+ 7) & (~7)) + 8;
387 } *search
= alloca(sizeof(*search
) + search_padded
);
389 search
->type
= ND_OPT_DNS_SEARCH
;
390 search
->len
= search_len
? ((sizeof(*search
) + search_padded
) / 8) : 0;
393 memcpy(search
->name
, search_domain
, search_len
);
394 memset(&search
->name
[search_len
], 0, search_padded
- search_len
);
397 size_t routes_cnt
= 0;
405 } routes
[RELAYD_MAX_PREFIXES
];
407 for (ssize_t i
= 0; i
< ipcnt
; ++i
) {
408 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
409 if (addr
->dprefix
> 64 || addr
->dprefix
== 0 || addr
->valid
<= (uint32_t)now
||
410 (addr
->dprefix
== 64 && addr
->prefix
== 64)) {
411 continue; // Address not suitable
412 } else if (addr
->dprefix
> 32) {
413 addr
->addr
.s6_addr32
[1] &= htonl(~((1U << (64 - addr
->dprefix
)) - 1));
414 } else if (addr
->dprefix
<= 32) {
415 addr
->addr
.s6_addr32
[0] &= htonl(~((1U << (32 - addr
->dprefix
)) - 1));
416 addr
->addr
.s6_addr32
[1] = 0;
419 routes
[routes_cnt
].type
= ND_OPT_ROUTE_INFO
;
420 routes
[routes_cnt
].len
= sizeof(*routes
) / 8;
421 routes
[routes_cnt
].prefix
= addr
->dprefix
;
422 routes
[routes_cnt
].flags
= 0;
423 if (iface
->route_preference
< 0)
424 routes
[routes_cnt
].flags
|= ND_RA_PREF_LOW
;
425 else if (iface
->route_preference
> 0)
426 routes
[routes_cnt
].flags
|= ND_RA_PREF_HIGH
;
427 routes
[routes_cnt
].lifetime
= htonl(TIME_LEFT(addr
->valid
, now
));
428 routes
[routes_cnt
].addr
[0] = addr
->addr
.s6_addr32
[0];
429 routes
[routes_cnt
].addr
[1] = addr
->addr
.s6_addr32
[1];
430 routes
[routes_cnt
].addr
[2] = 0;
431 routes
[routes_cnt
].addr
[3] = 0;
436 // Calculate periodic transmit
438 uint32_t maxival
= iface
->ra_maxinterval
* 1000;
441 if (maxival
< 4000 || maxival
> MaxRtrAdvInterval
* 1000)
442 maxival
= MaxRtrAdvInterval
* 1000;
444 if (maxival
> minvalid
/ 3) {
445 maxival
= minvalid
/ 3;
451 minival
= (maxival
* 3) / 4;
453 search
->lifetime
= htonl(maxival
/ 100);
454 dns
.lifetime
= search
->lifetime
;
456 odhcpd_urandom(&msecs
, sizeof(msecs
));
457 msecs
= (labs(msecs
) % (maxival
- minival
)) + minival
;
459 struct icmpv6_opt adv_interval
= {
460 .type
= ND_OPT_RTR_ADV_INTERVAL
,
462 .data
= {0, 0, maxival
>> 24, maxival
>> 16, maxival
>> 8, maxival
}
465 struct iovec iov
[RA_IOV_LEN
] = {
466 {&adv
, (uint8_t*)&adv
.prefix
[cnt
] - (uint8_t*)&adv
},
467 {&routes
, routes_cnt
* sizeof(*routes
)},
468 {&dns
, (dns_cnt
) ? sizeof(dns
) : 0},
469 {dns_addr
, dns_cnt
* sizeof(*dns_addr
)},
470 {search
, search
->len
* 8},
471 {&adv_interval
, adv_interval
.len
* 8}};
472 struct sockaddr_in6 dest
= {AF_INET6
, 0, 0, ALL_IPV6_NODES
, 0};
474 if (from
&& !IN6_IS_ADDR_UNSPECIFIED(from
))
475 dest
.sin6_addr
= *from
;
477 odhcpd_send(router_event
.uloop
.fd
,
478 &dest
, iov
, ARRAY_SIZE(iov
), iface
);
484 static void trigger_router_advert(struct uloop_timeout
*event
)
486 struct interface
*iface
= container_of(event
, struct interface
, timer_rs
);
487 int msecs
= send_router_advert(iface
, NULL
);
489 // Rearm timer if not shut down
491 uloop_timeout_set(event
, msecs
);
495 // Event handler for incoming ICMPv6 packets
496 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
497 struct interface
*iface
, _unused
void *dest
)
499 struct icmp6_hdr
*hdr
= data
;
500 struct sockaddr_in6
*from
= addr
;
502 if (!router_icmpv6_valid(addr
, data
, len
))
505 if ((iface
->ra
== RELAYD_SERVER
&& !iface
->master
)) { // Server mode
506 if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
507 send_router_advert(iface
, &from
->sin6_addr
);
508 } else if (iface
->ra
== RELAYD_RELAY
) { // Relay mode
509 if (hdr
->icmp6_type
== ND_ROUTER_ADVERT
&& iface
->master
)
510 forward_router_advertisement(data
, len
);
511 else if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
&& !iface
->master
)
512 forward_router_solicitation(odhcpd_get_master_interface());
517 // Forward router solicitation
518 static void forward_router_solicitation(const struct interface
*iface
)
523 struct icmp6_hdr rs
= {ND_ROUTER_SOLICIT
, 0, 0, {{0}}};
524 struct iovec iov
= {&rs
, sizeof(rs
)};
525 struct sockaddr_in6 all_routers
=
526 {AF_INET6
, 0, 0, ALL_IPV6_ROUTERS
, iface
->ifindex
};
528 syslog(LOG_NOTICE
, "Sending RS to %s", iface
->ifname
);
529 odhcpd_send(router_event
.uloop
.fd
, &all_routers
, &iov
, 1, iface
);
533 // Handler for incoming router solicitations on slave interfaces
534 static void forward_router_advertisement(uint8_t *data
, size_t len
)
536 struct nd_router_advert
*adv
= (struct nd_router_advert
*)data
;
539 uint8_t *end
= data
+ len
;
540 uint8_t *mac_ptr
= NULL
;
541 struct in6_addr
*dns_ptr
= NULL
;
542 size_t dns_count
= 0;
544 struct icmpv6_opt
*opt
;
545 icmpv6_for_each_option(opt
, &adv
[1], end
) {
546 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
) {
547 // Store address of source MAC-address
549 } else if (opt
->type
== ND_OPT_RECURSIVE_DNS
&& opt
->len
> 1) {
550 // Check if we have to rewrite DNS
551 dns_ptr
= (struct in6_addr
*)&opt
->data
[6];
552 dns_count
= (opt
->len
- 1) / 2;
556 syslog(LOG_NOTICE
, "Got a RA");
558 // Indicate a proxy, however we don't follow the rest of RFC 4389 yet
559 adv
->nd_ra_flags_reserved
|= ND_RA_FLAG_PROXY
;
561 // Forward advertisement to all slave interfaces
562 struct sockaddr_in6 all_nodes
= {AF_INET6
, 0, 0, ALL_IPV6_NODES
, 0};
563 struct iovec iov
= {data
, len
};
565 struct interface
*iface
;
566 list_for_each_entry(iface
, &interfaces
, head
) {
567 if (iface
->ra
!= RELAYD_RELAY
|| iface
->master
)
570 // Fixup source hardware address option
572 odhcpd_get_mac(iface
, mac_ptr
);
574 // If we have to rewrite DNS entries
575 if (iface
->always_rewrite_dns
&& dns_ptr
&& dns_count
> 0) {
576 const struct in6_addr
*rewrite
= iface
->dns
;
577 struct in6_addr addr
;
578 size_t rewrite_cnt
= iface
->dns_cnt
;
580 if (rewrite_cnt
== 0) {
581 if (odhcpd_get_linklocal_interface_address(iface
->ifindex
, &addr
))
582 continue; // Unable to comply
588 // Copy over any other addresses
589 for (size_t i
= 0; i
< dns_count
; ++i
) {
590 size_t j
= (i
< rewrite_cnt
) ? i
: rewrite_cnt
- 1;
591 dns_ptr
[i
] = rewrite
[j
];
595 odhcpd_send(router_event
.uloop
.fd
, &all_nodes
, &iov
, 1, iface
);