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 <net/route.h>
29 static void forward_router_solicitation(const struct interface
*iface
);
30 static void forward_router_advertisement(uint8_t *data
, size_t len
);
32 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
33 struct interface
*iface
, void *dest
);
34 static void trigger_router_advert(struct uloop_timeout
*event
);
35 static void sigusr1_refresh(int signal
);
37 static struct odhcpd_event router_event
= {{.fd
= -1}, handle_icmpv6
};
39 static FILE *fp_route
= NULL
;
46 int sock
= socket(AF_INET6
, SOCK_RAW
| SOCK_CLOEXEC
, IPPROTO_ICMPV6
);
47 if (sock
< 0 && errno
!= EAFNOSUPPORT
) {
48 syslog(LOG_ERR
, "Failed to open RAW-socket: %s", strerror(errno
));
52 // Let the kernel compute our checksums
54 setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
56 // This is required by RFC 4861
58 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
59 setsockopt(sock
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &val
, sizeof(val
));
61 // We need to know the source interface
63 setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &val
, sizeof(val
));
64 setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &val
, sizeof(val
));
68 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &val
, sizeof(val
));
70 // Filter ICMPv6 package types
71 struct icmp6_filter filt
;
72 ICMP6_FILTER_SETBLOCKALL(&filt
);
73 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
74 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
75 setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
, sizeof(filt
));
78 router_event
.uloop
.fd
= sock
;
79 odhcpd_register(&router_event
);
81 if (!(fp_route
= fopen("/proc/net/ipv6_route", "r")))
82 syslog(LOG_ERR
, "Failed to open routing table: %s",
85 signal(SIGUSR1
, sigusr1_refresh
);
90 int setup_router_interface(struct interface
*iface
, bool enable
)
92 if (!fp_route
|| router_event
.uloop
.fd
< 0)
95 struct ipv6_mreq all_nodes
= {ALL_IPV6_NODES
, iface
->ifindex
};
96 struct ipv6_mreq all_routers
= {ALL_IPV6_ROUTERS
, iface
->ifindex
};
98 uloop_timeout_cancel(&iface
->timer_rs
);
99 iface
->timer_rs
.cb
= NULL
;
101 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
102 &all_nodes
, sizeof(all_nodes
));
103 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
104 &all_routers
, sizeof(all_routers
));
108 trigger_router_advert(&iface
->timer_rs
);
110 void *mreq
= &all_routers
;
112 if (iface
->ra
== RELAYD_RELAY
&& iface
->master
) {
114 forward_router_solicitation(iface
);
115 } else if (iface
->ra
== RELAYD_SERVER
&& !iface
->master
) {
116 iface
->timer_rs
.cb
= trigger_router_advert
;
117 trigger_router_advert(&iface
->timer_rs
);
120 if (iface
->ra
== RELAYD_RELAY
|| (iface
->ra
== RELAYD_SERVER
&& !iface
->master
))
121 setsockopt(router_event
.uloop
.fd
, IPPROTO_IPV6
,
122 IPV6_ADD_MEMBERSHIP
, mreq
, sizeof(all_nodes
));
128 // Signal handler to resend all RDs
129 static void sigusr1_refresh(_unused
int signal
)
131 struct interface
*iface
;
132 list_for_each_entry(iface
, &interfaces
, head
)
133 if (iface
->ra
== RELAYD_SERVER
&& !iface
->master
)
134 uloop_timeout_set(&iface
->timer_rs
, 1000);
137 static bool router_icmpv6_valid(struct sockaddr_in6
*source
, uint8_t *data
, size_t len
)
139 struct icmp6_hdr
*hdr
= (struct icmp6_hdr
*)data
;
140 struct icmpv6_opt
*opt
, *end
= (struct icmpv6_opt
*)&data
[len
];
142 /* Hoplimit is already checked in odhcpd_receive_packets */
143 if (len
< sizeof(*hdr
) || hdr
->icmp6_code
)
146 switch (hdr
->icmp6_type
) {
147 case ND_ROUTER_ADVERT
:
148 if (!IN6_IS_ADDR_LINKLOCAL(&source
->sin6_addr
))
151 opt
= (struct icmpv6_opt
*)((struct nd_router_advert
*)data
+ 1);
154 case ND_ROUTER_SOLICIT
:
155 opt
= (struct icmpv6_opt
*)((struct nd_router_solicit
*)data
+ 1);
162 icmpv6_for_each_option(opt
, opt
, end
)
163 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
&&
164 IN6_IS_ADDR_UNSPECIFIED(&source
->sin6_addr
) &&
165 hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
168 // Check all options parsed successfully
173 // Detect whether a default route exists, also find the source prefixes
174 static bool parse_routes(struct odhcpd_ipaddr
*n
, ssize_t len
)
178 char line
[512], ifname
[16];
179 bool found_default
= false;
180 struct odhcpd_ipaddr p
= {IN6ADDR_ANY_INIT
, 0, 0, 0, 0};
181 while (fgets(line
, sizeof(line
), fp_route
)) {
183 if (sscanf(line
, "00000000000000000000000000000000 00 "
184 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname
) &&
185 strcmp(ifname
, "lo")) {
186 found_default
= true;
187 } else if (sscanf(line
, "%8" SCNx32
"%8" SCNx32
"%*8" SCNx32
"%*8" SCNx32
" %hhx %*s "
188 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32
" lo",
189 &p
.addr
.s6_addr32
[0], &p
.addr
.s6_addr32
[1], &p
.prefix
, &rflags
) &&
190 p
.prefix
> 0 && (rflags
& RTF_NONEXTHOP
) && (rflags
& RTF_REJECT
)) {
191 // Find source prefixes by scanning through unreachable-routes
192 p
.addr
.s6_addr32
[0] = htonl(p
.addr
.s6_addr32
[0]);
193 p
.addr
.s6_addr32
[1] = htonl(p
.addr
.s6_addr32
[1]);
195 for (ssize_t i
= 0; i
< len
; ++i
) {
196 if (n
[i
].prefix
<= 64 && n
[i
].prefix
>= p
.prefix
&&
197 !odhcpd_bmemcmp(&p
.addr
, &n
[i
].addr
, p
.prefix
)) {
198 n
[i
].dprefix
= p
.prefix
;
206 return found_default
;
210 static void send_neigh_ra(const struct in6_addr
*addr
,
211 const struct interface
*iface
, void *data
)
213 struct sockaddr_in6 dest
= {
214 .sin6_family
= AF_INET6
,
216 .sin6_scope_id
= iface
->ifindex
,
218 if (IN6_IS_ADDR_LINKLOCAL(addr
))
219 odhcpd_send(router_event
.uloop
.fd
, &dest
, data
, RA_IOV_LEN
, iface
);
223 // Router Advert server mode
224 static uint64_t send_router_advert(struct interface
*iface
, const struct in6_addr
*from
)
226 int mtu
= odhcpd_get_interface_config(iface
->ifname
, "mtu");
227 int hlim
= odhcpd_get_interface_config(iface
->ifname
, "hop_limit");
233 struct nd_router_advert h
;
234 struct icmpv6_opt lladdr
;
235 struct nd_opt_mtu mtu
;
236 struct nd_opt_prefix_info prefix
[RELAYD_MAX_PREFIXES
];
238 .h
= {{.icmp6_type
= ND_ROUTER_ADVERT
, .icmp6_code
= 0}, 0, 0},
239 .lladdr
= {ND_OPT_SOURCE_LINKADDR
, 1, {0}},
240 .mtu
= {ND_OPT_MTU
, 1, 0, htonl(mtu
)},
244 adv
.h
.nd_ra_curhoplimit
= hlim
;
247 adv
.h
.nd_ra_flags_reserved
= ND_RA_FLAG_OTHER
;
249 if (iface
->managed
>= RELAYD_MANAGED_MFLAG
)
250 adv
.h
.nd_ra_flags_reserved
|= ND_RA_FLAG_MANAGED
;
252 if (iface
->route_preference
< 0)
253 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_LOW
;
254 else if (iface
->route_preference
> 0)
255 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_HIGH
;
256 odhcpd_get_mac(iface
, adv
.lladdr
.data
);
258 // If not currently shutting down
259 struct odhcpd_ipaddr addrs
[RELAYD_MAX_PREFIXES
];
261 uint64_t maxpreferred
= 0;
264 if (iface
->timer_rs
.cb
) {
265 ipcnt
= odhcpd_get_interface_addresses(iface
->ifindex
,
266 addrs
, ARRAY_SIZE(addrs
));
268 // Check default route
269 if (parse_routes(addrs
, ipcnt
) || iface
->default_router
> 1)
270 adv
.h
.nd_ra_router_lifetime
=
271 htons(3 * MaxRtrAdvInterval
);
274 // Construct Prefix Information options
275 bool have_public
= false;
278 struct in6_addr dns_pref
= IN6ADDR_ANY_INIT
, *dns_addr
= &dns_pref
;
279 uint32_t dns_time
= 0;
282 for (ssize_t i
= 0; i
< ipcnt
; ++i
) {
283 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
284 if (addr
->prefix
> 96)
285 continue; // Address not suitable
287 if (addr
->preferred
> MaxPreferredTime
)
288 addr
->preferred
= MaxPreferredTime
;
290 if (addr
->valid
> MaxValidTime
)
291 addr
->valid
= MaxValidTime
;
293 struct nd_opt_prefix_info
*p
= NULL
;
294 for (size_t i
= 0; i
< cnt
; ++i
) {
295 if (addr
->prefix
== adv
.prefix
[i
].nd_opt_pi_prefix_len
&&
296 !odhcpd_bmemcmp(&adv
.prefix
[i
].nd_opt_pi_prefix
,
297 &addr
->addr
, addr
->prefix
))
302 if (cnt
>= ARRAY_SIZE(adv
.prefix
))
305 p
= &adv
.prefix
[cnt
++];
308 if ((addr
->addr
.s6_addr
[0] & 0xfe) != 0xfc && addr
->preferred
> 0) {
311 if (maxpreferred
< 1000 * addr
->preferred
)
312 maxpreferred
= 1000 * addr
->preferred
;
315 odhcpd_bmemcpy(&p
->nd_opt_pi_prefix
, &addr
->addr
,
316 (iface
->ra_advrouter
) ? 128 : addr
->prefix
);
317 p
->nd_opt_pi_type
= ND_OPT_PREFIX_INFORMATION
;
318 p
->nd_opt_pi_len
= 4;
319 p
->nd_opt_pi_prefix_len
= (addr
->prefix
< 64) ? 64 : addr
->prefix
;
320 p
->nd_opt_pi_flags_reserved
= 0;
321 if (!iface
->ra_not_onlink
)
322 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_ONLINK
;
323 if (iface
->managed
< RELAYD_MANAGED_NO_AFLAG
&& addr
->prefix
<= 64)
324 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_AUTO
;
325 if (iface
->ra_advrouter
)
326 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_RADDR
;
327 p
->nd_opt_pi_valid_time
= htonl(addr
->valid
);
328 p
->nd_opt_pi_preferred_time
= htonl(addr
->preferred
);
330 if (addr
->preferred
> dns_time
) {
331 dns_time
= addr
->preferred
;
332 dns_pref
= addr
->addr
;
336 if (!have_public
&& !iface
->default_router
&& adv
.h
.nd_ra_router_lifetime
) {
337 syslog(LOG_WARNING
, "A default route is present but there is no public prefix "
338 "on %s thus we don't announce a default route!", iface
->ifname
);
339 adv
.h
.nd_ra_router_lifetime
= 0;
343 if (iface
->dns_cnt
> 0) {
344 dns_addr
= iface
->dns
;
345 dns_cnt
= iface
->dns_cnt
;
346 dns_time
= 2 * MaxRtrAdvInterval
;
349 if (!dns_addr
|| IN6_IS_ADDR_UNSPECIFIED(dns_addr
))
358 } dns
= {ND_OPT_RECURSIVE_DNS
, (1 + (2 * dns_cnt
)), 0, 0, htonl(dns_time
)};
362 // DNS Search options
363 uint8_t search_buf
[256], *search_domain
= iface
->search
;
364 size_t search_len
= iface
->search_len
, search_padded
= 0;
366 if (!search_domain
&& !res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
367 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
368 sizeof(search_buf
), NULL
, NULL
);
370 search_domain
= search_buf
;
376 search_padded
= ((search_len
+ 7) & (~7)) + 8;
385 } *search
= alloca(sizeof(*search
) + search_padded
);
387 search
->type
= ND_OPT_DNS_SEARCH
;
388 search
->len
= search_len
? ((sizeof(*search
) + search_padded
) / 8) : 0;
391 search
->lifetime
= htonl(2 * MaxRtrAdvInterval
);;
392 memcpy(search
->name
, search_domain
, search_len
);
393 memset(&search
->name
[search_len
], 0, search_padded
- search_len
);
396 size_t routes_cnt
= 0;
404 } routes
[RELAYD_MAX_PREFIXES
];
406 for (ssize_t i
= 0; i
< ipcnt
; ++i
) {
407 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
408 if (addr
->dprefix
> 64 || addr
->dprefix
== 0 ||
409 (addr
->dprefix
== 64 && addr
->prefix
== 64)) {
410 continue; // Address not suitable
411 } else if (addr
->dprefix
> 32) {
412 addr
->addr
.s6_addr32
[1] &= htonl(~((1U << (64 - addr
->dprefix
)) - 1));
413 } else if (addr
->dprefix
<= 32) {
414 addr
->addr
.s6_addr32
[0] &= htonl(~((1U << (32 - addr
->dprefix
)) - 1));
415 addr
->addr
.s6_addr32
[1] = 0;
418 routes
[routes_cnt
].type
= ND_OPT_ROUTE_INFO
;
419 routes
[routes_cnt
].len
= sizeof(*routes
) / 8;
420 routes
[routes_cnt
].prefix
= addr
->dprefix
;
421 routes
[routes_cnt
].flags
= 0;
422 if (iface
->route_preference
< 0)
423 routes
[routes_cnt
].flags
|= ND_RA_PREF_LOW
;
424 else if (iface
->route_preference
> 0)
425 routes
[routes_cnt
].flags
|= ND_RA_PREF_HIGH
;
426 routes
[routes_cnt
].lifetime
= htonl(addr
->valid
);
427 routes
[routes_cnt
].addr
[0] = addr
->addr
.s6_addr32
[0];
428 routes
[routes_cnt
].addr
[1] = addr
->addr
.s6_addr32
[1];
429 routes
[routes_cnt
].addr
[2] = 0;
430 routes
[routes_cnt
].addr
[3] = 0;
435 // Calculate periodic transmit
437 uint32_t maxival
= MaxRtrAdvInterval
* 1000;
438 uint32_t minival
= MinRtrAdvInterval
* 1000;
440 if (maxpreferred
> 0 && maxival
> maxpreferred
/ 2) {
441 maxival
= maxpreferred
/ 2;
446 minival
= maxival
/ 3;
448 minival
= (maxival
* 3) / 4;
451 odhcpd_urandom(&msecs
, sizeof(msecs
));
452 msecs
= (labs(msecs
) % (maxival
- minival
)) + minival
;
454 struct icmpv6_opt adv_interval
= {
455 .type
= ND_OPT_RTR_ADV_INTERVAL
,
457 .data
= {0, 0, maxival
>> 24, maxival
>> 16, maxival
>> 8, maxival
}
460 struct iovec iov
[RA_IOV_LEN
] = {
461 {&adv
, (uint8_t*)&adv
.prefix
[cnt
] - (uint8_t*)&adv
},
462 {&routes
, routes_cnt
* sizeof(*routes
)},
463 {&dns
, (dns_cnt
) ? sizeof(dns
) : 0},
464 {dns_addr
, dns_cnt
* sizeof(*dns_addr
)},
465 {search
, search
->len
* 8},
466 {&adv_interval
, adv_interval
.len
* 8}};
467 struct sockaddr_in6 dest
= {AF_INET6
, 0, 0, ALL_IPV6_NODES
, 0};
469 if (from
&& !IN6_IS_ADDR_UNSPECIFIED(from
))
470 dest
.sin6_addr
= *from
;
472 odhcpd_send(router_event
.uloop
.fd
,
473 &dest
, iov
, ARRAY_SIZE(iov
), iface
);
475 odhcpd_iterate_interface_neighbors(iface
, send_neigh_ra
, iov
);
480 static void trigger_router_advert(struct uloop_timeout
*event
)
482 struct interface
*iface
= container_of(event
, struct interface
, timer_rs
);
483 int msecs
= send_router_advert(iface
, NULL
);
485 // Rearm timer if not shut down
487 uloop_timeout_set(event
, msecs
);
491 // Event handler for incoming ICMPv6 packets
492 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
493 struct interface
*iface
, _unused
void *dest
)
495 struct icmp6_hdr
*hdr
= data
;
496 struct sockaddr_in6
*from
= addr
;
498 if (!router_icmpv6_valid(addr
, data
, len
))
501 if ((iface
->ra
== RELAYD_SERVER
&& !iface
->master
)) { // Server mode
502 if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
503 send_router_advert(iface
, &from
->sin6_addr
);
504 } else if (iface
->ra
== RELAYD_RELAY
) { // Relay mode
505 if (hdr
->icmp6_type
== ND_ROUTER_ADVERT
&& iface
->master
)
506 forward_router_advertisement(data
, len
);
507 else if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
&& !iface
->master
)
508 forward_router_solicitation(odhcpd_get_master_interface());
513 // Forward router solicitation
514 static void forward_router_solicitation(const struct interface
*iface
)
519 struct icmp6_hdr rs
= {ND_ROUTER_SOLICIT
, 0, 0, {{0}}};
520 struct iovec iov
= {&rs
, sizeof(rs
)};
521 struct sockaddr_in6 all_routers
=
522 {AF_INET6
, 0, 0, ALL_IPV6_ROUTERS
, iface
->ifindex
};
524 syslog(LOG_NOTICE
, "Sending RS to %s", iface
->ifname
);
525 odhcpd_send(router_event
.uloop
.fd
, &all_routers
, &iov
, 1, iface
);
529 // Handler for incoming router solicitations on slave interfaces
530 static void forward_router_advertisement(uint8_t *data
, size_t len
)
532 struct nd_router_advert
*adv
= (struct nd_router_advert
*)data
;
535 uint8_t *end
= data
+ len
;
536 uint8_t *mac_ptr
= NULL
;
537 struct in6_addr
*dns_ptr
= NULL
;
538 size_t dns_count
= 0;
540 struct icmpv6_opt
*opt
;
541 icmpv6_for_each_option(opt
, &adv
[1], end
) {
542 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
) {
543 // Store address of source MAC-address
545 } else if (opt
->type
== ND_OPT_RECURSIVE_DNS
&& opt
->len
> 1) {
546 // Check if we have to rewrite DNS
547 dns_ptr
= (struct in6_addr
*)&opt
->data
[6];
548 dns_count
= (opt
->len
- 1) / 2;
552 syslog(LOG_NOTICE
, "Got a RA");
554 // Indicate a proxy, however we don't follow the rest of RFC 4389 yet
555 adv
->nd_ra_flags_reserved
|= ND_RA_FLAG_PROXY
;
557 // Forward advertisement to all slave interfaces
558 struct sockaddr_in6 all_nodes
= {AF_INET6
, 0, 0, ALL_IPV6_NODES
, 0};
559 struct iovec iov
= {data
, len
};
561 struct interface
*iface
;
562 list_for_each_entry(iface
, &interfaces
, head
) {
563 if (iface
->ra
!= RELAYD_RELAY
|| iface
->master
)
566 // Fixup source hardware address option
568 odhcpd_get_mac(iface
, mac_ptr
);
570 // If we have to rewrite DNS entries
571 if (iface
->always_rewrite_dns
&& dns_ptr
&& dns_count
> 0) {
572 const struct in6_addr
*rewrite
= iface
->dns
;
573 struct in6_addr addr
;
574 size_t rewrite_cnt
= iface
->dns_cnt
;
576 if (rewrite_cnt
== 0) {
577 if (odhcpd_get_preferred_interface_address(iface
->ifindex
, &addr
) < 1)
578 continue; // Unable to comply
584 // Copy over any other addresses
585 for (size_t i
= 0; i
< dns_count
; ++i
) {
586 size_t j
= (i
< rewrite_cnt
) ? i
: rewrite_cnt
- 1;
587 dns_ptr
[i
] = rewrite
[j
];
591 odhcpd_send(router_event
.uloop
.fd
, &all_nodes
, &iov
, 1, iface
);