10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netinet/icmp6.h>
14 #include <linux/rtnetlink.h>
21 static int sock
= -1, rtnl_sock
= -1;
22 static unsigned if_index
= 0;
23 static char if_name
[IF_NAMESIZE
] = {0};
24 static volatile int rs_attempt
= 1;
25 static struct in6_addr lladdr
= IN6ADDR_ANY_INIT
;
27 static void ra_send_rs(int signal
__attribute__((unused
)));
29 int ra_init(const char *ifname
)
31 sock
= socket(AF_INET6
, SOCK_RAW
| SOCK_CLOEXEC
, IPPROTO_ICMPV6
);
32 if_index
= if_nametoindex(ifname
);
33 strncpy(if_name
, ifname
, sizeof(if_name
) - 1);
35 // Filter ICMPv6 package types
36 struct icmp6_filter filt
;
37 ICMP6_FILTER_SETBLOCKALL(&filt
);
38 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
39 setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
, sizeof(filt
));
42 struct ipv6_mreq an
= {ALL_IPV6_NODES
, if_index
};
43 setsockopt(sock
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, &an
, sizeof(an
));
45 // Let the kernel compute our checksums
47 setsockopt(sock
, IPPROTO_RAW
, IPV6_CHECKSUM
, &val
, sizeof(val
));
49 // This is required by RFC 4861
51 setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &val
, sizeof(val
));
54 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, ifname
, strlen(ifname
));
57 const pid_t ourpid
= getpid();
58 fcntl(sock
, F_SETOWN
, ourpid
);
59 fcntl(sock
, F_SETFL
, fcntl(sock
, F_GETFL
) | O_ASYNC
);
62 FILE *fp
= fopen("/proc/net/if_inet6", "r");
64 char addrbuf
[33], ifbuf
[16];
65 while (fscanf(fp
, "%32s %*x %*x %*x %*x %15s", addrbuf
, ifbuf
) == 2) {
66 if (!strcmp(ifbuf
, if_name
)) {
67 script_unhexlify((uint8_t*)&lladdr
, sizeof(lladdr
), addrbuf
);
74 // Open rtnetlink socket
75 rtnl_sock
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, NETLINK_ROUTE
);
76 struct sockaddr_nl rtnl_kernel
= { .nl_family
= AF_NETLINK
};
77 if (connect(rtnl_sock
, (struct sockaddr
*)&rtnl_kernel
, sizeof(rtnl_kernel
)))
79 uint32_t group
= RTNLGRP_IPV6_IFADDR
;
80 setsockopt(rtnl_sock
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
, &group
, sizeof(group
));
83 fcntl(rtnl_sock
, F_SETOWN
, ourpid
);
84 fcntl(rtnl_sock
, F_SETFL
, fcntl(rtnl_sock
, F_GETFL
) | O_ASYNC
);
87 signal(SIGALRM
, ra_send_rs
);
94 static void ra_send_rs(int signal
__attribute__((unused
)))
96 const struct icmp6_hdr rs
= {ND_ROUTER_SOLICIT
, 0, 0, {{0}}};
97 const struct sockaddr_in6 dest
= {AF_INET6
, 0, 0, ALL_IPV6_ROUTERS
, if_index
};
98 sendto(sock
, &rs
, sizeof(rs
), MSG_DONTWAIT
, (struct sockaddr
*)&dest
, sizeof(dest
));
100 if (++rs_attempt
< 3)
105 static int16_t pref_to_priority(uint8_t flags
)
107 flags
= (flags
>> 3) & 0x03;
108 return (flags
== 0x00) ? 1024 : (flags
== 0x01) ? 512 :
109 (flags
== 0x11) ? 2048 : -1;
113 static void update_proc(const char *sect
, const char *opt
, uint32_t value
)
116 snprintf(buf
, sizeof(buf
), "/proc/sys/net/ipv6/%s/%s/%s", sect
, if_name
, opt
);
118 int fd
= open(buf
, O_WRONLY
);
119 write(fd
, buf
, snprintf(buf
, sizeof(buf
), "%u", value
));
124 static bool ra_deduplicate(const struct in6_addr
*any
, uint8_t length
)
126 struct odhcp6c_entry entry
= {IN6ADDR_ANY_INIT
, length
, 0, *any
, 0, 0};
127 struct odhcp6c_entry
*x
= odhcp6c_find_entry(STATE_RA_PREFIX
, &entry
);
129 odhcp6c_random(&x
->target
.s6_addr32
[2], 2 * sizeof(uint32_t));
130 } else if (odhcp6c_find_entry(STATE_IA_NA
, &entry
)) {
131 dhcpv6_request(DHCPV6_MSG_DECLINE
);
139 bool ra_rtnl_process(void)
144 ssize_t len
= recv(rtnl_sock
, buf
, sizeof(buf
), MSG_DONTWAIT
);
148 for (struct nlmsghdr
*nh
= (struct nlmsghdr
*)buf
; NLMSG_OK(nh
, len
);
149 nh
= NLMSG_NEXT(nh
, len
)) {
150 struct ifaddrmsg
*ifa
= NLMSG_DATA(nh
);
151 struct in6_addr
*addr
= NULL
;
152 if (nh
->nlmsg_type
!= RTM_NEWADDR
|| NLMSG_PAYLOAD(nh
, 0) < sizeof(*ifa
) ||
153 !(ifa
->ifa_flags
& IFA_F_DADFAILED
) ||
154 ifa
->ifa_index
!= if_index
)
157 ssize_t alen
= NLMSG_PAYLOAD(nh
, sizeof(*ifa
));
158 for (struct rtattr
*rta
= (struct rtattr
*)&ifa
[1]; RTA_OK(rta
, alen
);
159 rta
= RTA_NEXT(rta
, alen
))
160 if (rta
->rta_type
== IFA_LOCAL
&& RTA_PAYLOAD(rta
) >= sizeof(*addr
))
161 addr
= RTA_DATA(rta
);
164 found
|= ra_deduplicate(addr
, ifa
->ifa_prefixlen
);
171 bool ra_process(void)
175 struct nd_router_advert
*adv
= (struct nd_router_advert
*)buf
;
176 struct odhcp6c_entry entry
= {IN6ADDR_ANY_INIT
, 0, 0, IN6ADDR_ANY_INIT
, 0, 0};
177 const struct in6_addr any
= IN6ADDR_ANY_INIT
;
181 struct sockaddr_in6 from
;
182 socklen_t from_len
= sizeof(from
);
183 ssize_t len
= recvfrom(sock
, buf
, sizeof(buf
), MSG_DONTWAIT
, &from
, &from_len
);
186 else if (len
< (ssize_t
)sizeof(*adv
))
189 // Stop sending solicits
190 if (rs_attempt
> 0) {
197 // Parse default route
198 entry
.router
= from
.sin6_addr
;
199 entry
.priority
= pref_to_priority(adv
->nd_ra_flags_reserved
);
200 if (entry
.priority
< 0)
201 entry
.priority
= pref_to_priority(0);
202 entry
.valid
= ntohs(adv
->nd_ra_router_lifetime
);
203 entry
.preferred
= entry
.valid
;
204 odhcp6c_update_entry(STATE_RA_ROUTE
, &entry
);
206 // Parse ND parameters
207 if (adv
->nd_ra_reachable
)
208 update_proc("neigh", "base_reachable_time_ms", ntohl(adv
->nd_ra_reachable
));
210 if (adv
->nd_ra_retransmit
)
211 update_proc("neigh", "retrans_time_ms", ntohl(adv
->nd_ra_retransmit
));
214 struct icmpv6_opt
*opt
;
215 icmpv6_for_each_option(opt
, &adv
[1], &buf
[len
]) {
216 if (opt
->type
== ND_OPT_MTU
) {
217 update_proc("conf", "mtu", ntohl(*((uint32_t*)&opt
->data
[2])));
218 } else if (opt
->type
== ND_OPT_ROUTE_INFORMATION
&& opt
->len
<= 3) {
219 entry
.router
= from
.sin6_addr
;
221 entry
.priority
= pref_to_priority(opt
->data
[1]);
222 entry
.length
= opt
->data
[0];
223 entry
.valid
= ntohl(*((uint32_t*)&opt
->data
[2]));
224 memcpy(&entry
.target
, &opt
->data
[6], (opt
->len
- 1) * 8);
226 if (entry
.length
> 128 || IN6_IS_ADDR_LINKLOCAL(&entry
.target
)
227 || IN6_IS_ADDR_LOOPBACK(&entry
.target
)
228 || IN6_IS_ADDR_MULTICAST(&entry
.target
))
231 if (entry
.priority
> 0)
232 odhcp6c_update_entry(STATE_RA_ROUTE
, &entry
);
233 } else if (opt
->type
== ND_OPT_PREFIX_INFORMATION
&& opt
->len
== 4) {
234 struct nd_opt_prefix_info
*pinfo
= (struct nd_opt_prefix_info
*)opt
;
236 entry
.target
= pinfo
->nd_opt_pi_prefix
;
238 entry
.length
= pinfo
->nd_opt_pi_prefix_len
;
239 entry
.valid
= ntohl(pinfo
->nd_opt_pi_valid_time
);
240 entry
.preferred
= ntohl(pinfo
->nd_opt_pi_preferred_time
);
242 if (entry
.length
> 128 || IN6_IS_ADDR_LINKLOCAL(&entry
.target
)
243 || IN6_IS_ADDR_LOOPBACK(&entry
.target
)
244 || IN6_IS_ADDR_MULTICAST(&entry
.target
)
245 || entry
.valid
< entry
.preferred
)
248 if (pinfo
->nd_opt_pi_flags_reserved
& ND_OPT_PI_FLAG_ONLINK
)
249 odhcp6c_update_entry_safe(STATE_RA_ROUTE
, &entry
, 7201);
251 if (!(pinfo
->nd_opt_pi_flags_reserved
& ND_OPT_PI_FLAG_AUTO
) ||
252 pinfo
->nd_opt_pi_prefix_len
!= 64)
255 entry
.target
.s6_addr32
[2] = lladdr
.s6_addr32
[2];
256 entry
.target
.s6_addr32
[3] = lladdr
.s6_addr32
[3];
258 odhcp6c_update_entry_safe(STATE_RA_PREFIX
, &entry
, 7201);