Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 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 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 18 | /* NB. This code may be called during a DHCPv4 or transaction which is in ping-wait |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 19 | It therefore cannot use any DHCP buffer resources except outpacket, which is |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 20 | not used by DHCPv4 code. This code may also be called when DHCP 4 or 6 isn't |
| 21 | active, so we ensure that outpacket is allocated here too */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 22 | |
| 23 | #include "dnsmasq.h" |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef HAVE_DHCP6 |
| 26 | |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 27 | #include <netinet/icmp6.h> |
| 28 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 29 | struct ra_param { |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 30 | int ind, managed, other, found_context, first; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 31 | char *if_name; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 32 | struct dhcp_netid *tags; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 33 | struct in6_addr link_local; |
| 34 | }; |
| 35 | |
| 36 | struct search_param { |
| 37 | time_t now; int iface; |
| 38 | }; |
| 39 | |
| 40 | static void send_ra(int iface, char *iface_name, struct in6_addr *dest); |
| 41 | static int add_prefixes(struct in6_addr *local, int prefix, |
| 42 | int scope, int if_index, int dad, void *vparam); |
| 43 | static int iface_search(struct in6_addr *local, int prefix, |
| 44 | int scope, int if_index, int dad, void *vparam); |
| 45 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
| 46 | |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 47 | static int hop_limit; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 48 | static time_t ra_short_period_start; |
| 49 | |
| 50 | void ra_init(time_t now) |
| 51 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 52 | struct icmp6_filter filter; |
| 53 | int fd; |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 54 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 55 | int class = IPTOS_CLASS_CS6; |
| 56 | #endif |
| 57 | int val = 255; /* radvd uses this value */ |
Simon Kelley | 7b6dd88 | 2012-03-01 10:26:16 +0000 | [diff] [blame] | 58 | socklen_t len = sizeof(int); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 59 | struct dhcp_context *context; |
| 60 | |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 61 | /* ensure this is around even if we're not doing DHCPv6 */ |
| 62 | expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet)); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 63 | |
| 64 | /* See if we're guessing SLAAC addresses, if so we need to recieve ping replies */ |
| 65 | for (context = daemon->ra_contexts; context; context = context->next) |
| 66 | if ((context->flags & CONTEXT_RA_NAME)) |
| 67 | break; |
| 68 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 69 | ICMP6_FILTER_SETBLOCKALL(&filter); |
| 70 | ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 71 | if (context) |
| 72 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 73 | |
| 74 | if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 || |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 75 | getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) || |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 76 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 77 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 || |
| 78 | #endif |
| 79 | !fix_fd(fd) || |
| 80 | !set_ipv6pktinfo(fd) || |
| 81 | setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) || |
| 82 | setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) || |
| 83 | setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1) |
| 84 | die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET); |
| 85 | |
| 86 | daemon->icmp6fd = fd; |
| 87 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 88 | ra_start_unsolicted(now, NULL); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 91 | void ra_start_unsolicted(time_t now, struct dhcp_context *context) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 92 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 93 | /* init timers so that we do ra's for some/all soon. some ra_times will end up zeroed |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 94 | if it's not appropriate to advertise those contexts. |
| 95 | This gets re-called on a netlink route-change to re-do the advertisement |
| 96 | and pick up new interfaces */ |
| 97 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 98 | if (context) |
| 99 | context->ra_time = now; |
| 100 | else |
| 101 | for (context = daemon->ra_contexts; context; context = context->next) |
| 102 | context->ra_time = now + (rand16()/13000); /* range 0 - 5 */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 103 | |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 104 | /* re-do frequently for a minute or so, in case the first gets lost. */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 105 | ra_short_period_start = now; |
| 106 | } |
| 107 | |
| 108 | void icmp6_packet(void) |
| 109 | { |
| 110 | char interface[IF_NAMESIZE+1]; |
| 111 | ssize_t sz; |
| 112 | int if_index = 0; |
| 113 | struct cmsghdr *cmptr; |
| 114 | struct msghdr msg; |
| 115 | union { |
| 116 | struct cmsghdr align; /* this ensures alignment */ |
| 117 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 118 | } control_u; |
| 119 | struct sockaddr_in6 from; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 120 | unsigned char *packet; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 121 | struct iname *tmp; |
| 122 | struct dhcp_context *context; |
| 123 | |
| 124 | /* Note: use outpacket for input buffer */ |
| 125 | msg.msg_control = control_u.control6; |
| 126 | msg.msg_controllen = sizeof(control_u); |
| 127 | msg.msg_flags = 0; |
| 128 | msg.msg_name = &from; |
| 129 | msg.msg_namelen = sizeof(from); |
| 130 | msg.msg_iov = &daemon->outpacket; |
| 131 | msg.msg_iovlen = 1; |
| 132 | |
| 133 | if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8) |
| 134 | return; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 135 | |
| 136 | packet = (unsigned char *)daemon->outpacket.iov_base; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 137 | |
| 138 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 139 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 140 | { |
| 141 | union { |
| 142 | unsigned char *c; |
| 143 | struct in6_pktinfo *p; |
| 144 | } p; |
| 145 | p.c = CMSG_DATA(cmptr); |
| 146 | |
| 147 | if_index = p.p->ipi6_ifindex; |
| 148 | } |
| 149 | |
| 150 | if (!indextoname(daemon->icmp6fd, if_index, interface)) |
| 151 | return; |
| 152 | |
| 153 | if (!iface_check(AF_LOCAL, NULL, interface)) |
| 154 | return; |
| 155 | |
| 156 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 157 | if (tmp->name && (strcmp(tmp->name, interface) == 0)) |
| 158 | return; |
| 159 | |
| 160 | /* weird libvirt-inspired access control */ |
Simon Kelley | 51931b8 | 2012-05-29 17:06:02 +0100 | [diff] [blame] | 161 | for (context = daemon->ra_contexts ? daemon->ra_contexts : daemon->dhcp6; |
| 162 | context; context = context->next) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 163 | if (!context->interface || strcmp(context->interface, interface) == 0) |
| 164 | break; |
| 165 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 166 | if (!context || packet[1] != 0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 167 | return; |
| 168 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 169 | if (packet[0] == ICMP6_ECHO_REPLY) |
| 170 | lease_ping_reply(&from.sin6_addr, packet, interface); |
| 171 | else if (packet[0] == ND_ROUTER_SOLICIT) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 172 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 173 | char *mac = ""; |
| 174 | |
| 175 | /* look for link-layer address option for logging */ |
| 176 | if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz) |
| 177 | { |
| 178 | print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2); |
| 179 | mac = daemon->namebuff; |
| 180 | } |
| 181 | |
| 182 | my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac); |
Simon Kelley | f632e56 | 2012-05-12 15:05:34 +0100 | [diff] [blame] | 183 | /* source address may not be valid in solicit request. */ |
| 184 | send_ra(if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 185 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void send_ra(int iface, char *iface_name, struct in6_addr *dest) |
| 189 | { |
| 190 | struct ra_packet *ra; |
| 191 | struct ra_param parm; |
| 192 | struct ifreq ifr; |
| 193 | struct sockaddr_in6 addr; |
| 194 | struct dhcp_context *context; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 195 | struct dhcp_netid iface_id; |
| 196 | struct dhcp_opt *opt_cfg; |
| 197 | int done_dns = 0; |
| 198 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 199 | save_counter(0); |
| 200 | ra = expand(sizeof(struct ra_packet)); |
| 201 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 202 | ra->type = ND_ROUTER_ADVERT; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 203 | ra->code = 0; |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 204 | ra->hop_limit = hop_limit; |
Simon Kelley | 884a6df | 2012-03-20 16:20:22 +0000 | [diff] [blame] | 205 | ra->flags = 0x00; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 206 | ra->lifetime = htons(1800); /* AdvDefaultLifetime*/ |
| 207 | ra->reachable_time = 0; |
| 208 | ra->retrans_time = 0; |
| 209 | |
| 210 | parm.ind = iface; |
| 211 | parm.managed = 0; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 212 | parm.other = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 213 | parm.found_context = 0; |
| 214 | parm.if_name = iface_name; |
| 215 | parm.first = 1; |
| 216 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 217 | /* set tag with name == interface */ |
| 218 | iface_id.net = iface_name; |
| 219 | iface_id.next = NULL; |
| 220 | parm.tags = &iface_id; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 221 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 222 | for (context = daemon->ra_contexts; context; context = context->next) |
| 223 | { |
| 224 | context->flags &= ~CONTEXT_RA_DONE; |
| 225 | context->netid.next = &context->netid; |
| 226 | } |
| 227 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 228 | if (!iface_enumerate(AF_INET6, &parm, add_prefixes) || |
| 229 | !parm.found_context) |
| 230 | return; |
| 231 | |
| 232 | strncpy(ifr.ifr_name, iface_name, IF_NAMESIZE); |
| 233 | |
| 234 | if (ioctl(daemon->icmp6fd, SIOCGIFMTU, &ifr) != -1) |
| 235 | { |
| 236 | put_opt6_char(ICMP6_OPT_MTU); |
| 237 | put_opt6_char(1); |
| 238 | put_opt6_short(0); |
| 239 | put_opt6_long(ifr.ifr_mtu); |
| 240 | } |
| 241 | |
| 242 | iface_enumerate(AF_LOCAL, &iface, add_lla); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 243 | |
| 244 | /* RDNSS, RFC 6106, use relevant DHCP6 options */ |
| 245 | (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 246 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 247 | for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | /* netids match and not encapsulated? */ |
| 252 | if (!(opt_cfg->flags & DHOPT_TAGOK)) |
| 253 | continue; |
| 254 | |
| 255 | if (opt_cfg->opt == OPTION6_DNS_SERVER) |
| 256 | { |
| 257 | struct in6_addr *a = (struct in6_addr *)opt_cfg->val; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 258 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 259 | done_dns = 1; |
| 260 | if (opt_cfg->len == 0) |
| 261 | continue; |
| 262 | |
| 263 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 264 | put_opt6_char((opt_cfg->len/8) + 1); |
| 265 | put_opt6_short(0); |
| 266 | put_opt6_long(1800); /* lifetime - twice RA retransmit */ |
| 267 | /* zero means "self" */ |
| 268 | for (i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++) |
| 269 | if (IN6_IS_ADDR_UNSPECIFIED(a)) |
| 270 | put_opt6(&parm.link_local, IN6ADDRSZ); |
| 271 | else |
| 272 | put_opt6(a, IN6ADDRSZ); |
| 273 | } |
| 274 | |
| 275 | if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0) |
| 276 | { |
| 277 | int len = ((opt_cfg->len+7)/8); |
| 278 | |
| 279 | put_opt6_char(ICMP6_OPT_DNSSL); |
| 280 | put_opt6_char(len + 1); |
| 281 | put_opt6_short(0); |
| 282 | put_opt6_long(1800); /* lifetime - twice RA retransmit */ |
| 283 | put_opt6(opt_cfg->val, opt_cfg->len); |
| 284 | |
| 285 | /* pad */ |
| 286 | for (i = opt_cfg->len; i < len * 8; i++) |
| 287 | put_opt6_char(0); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (!done_dns) |
| 292 | { |
| 293 | /* default == us. */ |
| 294 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 295 | put_opt6_char(3); |
| 296 | put_opt6_short(0); |
| 297 | put_opt6_long(1800); /* lifetime - twice RA retransmit */ |
| 298 | put_opt6(&parm.link_local, IN6ADDRSZ); |
| 299 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 300 | |
| 301 | /* set managed bits unless we're providing only RA on this link */ |
| 302 | if (parm.managed) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 303 | ra->flags |= 0x80; /* M flag, managed, */ |
| 304 | if (parm.other) |
| 305 | ra->flags |= 0x40; /* O flag, other */ |
| 306 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 307 | /* decide where we're sending */ |
| 308 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 309 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 310 | addr.sin6_len = sizeof(struct sockaddr_in6); |
| 311 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 312 | addr.sin6_family = AF_INET6; |
| 313 | addr.sin6_port = htons(IPPROTO_ICMPV6); |
| 314 | if (dest) |
| 315 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 316 | addr.sin6_addr = *dest; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 317 | if (IN6_IS_ADDR_LINKLOCAL(dest) || |
| 318 | IN6_IS_ADDR_MC_LINKLOCAL(dest)) |
| 319 | addr.sin6_scope_id = iface; |
| 320 | } |
| 321 | else |
Simon Kelley | f632e56 | 2012-05-12 15:05:34 +0100 | [diff] [blame] | 322 | inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr); |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 323 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 324 | send_from(daemon->icmp6fd, 0, daemon->outpacket.iov_base, save_counter(0), |
Simon Kelley | 50303b1 | 2012-04-04 22:13:17 +0100 | [diff] [blame] | 325 | (union mysockaddr *)&addr, (struct all_addr *)&parm.link_local, iface); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 326 | |
| 327 | } |
| 328 | |
| 329 | static int add_prefixes(struct in6_addr *local, int prefix, |
| 330 | int scope, int if_index, int dad, void *vparam) |
| 331 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 332 | struct ra_param *param = vparam; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 333 | |
| 334 | (void)scope; /* warning */ |
| 335 | (void)dad; |
| 336 | |
| 337 | if (if_index == param->ind) |
| 338 | { |
| 339 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
| 340 | param->link_local = *local; |
| 341 | else if (!IN6_IS_ADDR_LOOPBACK(local) && |
| 342 | !IN6_IS_ADDR_LINKLOCAL(local) && |
| 343 | !IN6_IS_ADDR_MULTICAST(local)) |
| 344 | { |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 345 | int do_prefix = 0; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 346 | int do_slaac = 0; |
| 347 | int deprecate = 0; |
| 348 | unsigned int time = 0xffffffff; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 349 | struct dhcp_context *context; |
| 350 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 351 | for (context = daemon->ra_contexts; context; context = context->next) |
| 352 | if (prefix == context->prefix && |
| 353 | is_same_net6(local, &context->start6, prefix) && |
| 354 | is_same_net6(local, &context->end6, prefix)) |
| 355 | { |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 356 | if ((context->flags & |
| 357 | (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS))) |
| 358 | { |
| 359 | do_slaac = 1; |
Simon Kelley | 4723d49 | 2012-03-30 21:04:17 +0100 | [diff] [blame] | 360 | if (context->flags & CONTEXT_DHCP) |
Simon Kelley | 05e92e5 | 2012-03-30 22:24:15 +0100 | [diff] [blame] | 361 | { |
| 362 | param->other = 1; |
| 363 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 364 | param->managed = 1; |
| 365 | } |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 366 | } |
Simon Kelley | 884a6df | 2012-03-20 16:20:22 +0000 | [diff] [blame] | 367 | else |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 368 | { |
| 369 | /* don't do RA for non-ra-only unless --enable-ra is set */ |
| 370 | if (!option_bool(OPT_RA)) |
| 371 | continue; |
| 372 | param->managed = 1; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 373 | param->other = 1; |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 376 | /* find floor time */ |
| 377 | if (time > context->lease_time) |
| 378 | time = context->lease_time; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 379 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 380 | if (context->flags & CONTEXT_DEPRECATE) |
| 381 | deprecate = 1; |
| 382 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 383 | /* collect dhcp-range tags */ |
| 384 | if (context->netid.next == &context->netid && context->netid.net) |
| 385 | { |
| 386 | context->netid.next = param->tags; |
| 387 | param->tags = &context->netid; |
| 388 | } |
| 389 | |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame^] | 390 | /* subsequent prefixes on the same interface |
| 391 | and subsequent instances of this prefix don't need timers. |
| 392 | Be careful not to find the same prefix twice with different |
| 393 | addresses. */ |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 394 | if (!(context->flags & CONTEXT_RA_DONE)) |
| 395 | { |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame^] | 396 | if (!param->first) |
| 397 | context->ra_time = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 398 | context->flags |= CONTEXT_RA_DONE; |
| 399 | do_prefix = 1; |
| 400 | } |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame^] | 401 | |
| 402 | param->first = 0; |
| 403 | param->found_context = 1; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 404 | } |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 405 | |
| 406 | if (do_prefix) |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 407 | { |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 408 | struct prefix_opt *opt; |
| 409 | |
| 410 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 411 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 412 | /* zero net part of address */ |
| 413 | setaddr6part(local, addr6part(local) & ~((prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU)); |
| 414 | |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 415 | /* lifetimes must be min 2 hrs, by RFC 2462 */ |
| 416 | if (time < 7200) |
| 417 | time = 7200; |
| 418 | |
| 419 | opt->type = ICMP6_OPT_PREFIX; |
| 420 | opt->len = 4; |
| 421 | opt->prefix_len = prefix; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 422 | /* autonomous only if we're not doing dhcp */ |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 423 | opt->flags = do_slaac ? 0x40 : 0x00; |
| 424 | opt->valid_lifetime = htonl(time); |
| 425 | opt->preferred_lifetime = htonl(deprecate ? 0 : time); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 426 | opt->reserved = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 427 | opt->prefix = *local; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 428 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 429 | inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 430 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff); |
| 431 | } |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 432 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
| 439 | { |
| 440 | (void)type; |
| 441 | |
| 442 | if (index == *((int *)parm)) |
| 443 | { |
| 444 | /* size is in units of 8 octets and includes type and length (2 bytes) |
| 445 | add 7 to round up */ |
| 446 | int len = (maclen + 9) >> 3; |
| 447 | unsigned char *p = expand(len << 3); |
| 448 | memset(p, 0, len << 3); |
| 449 | *p++ = ICMP6_OPT_SOURCE_MAC; |
| 450 | *p++ = len; |
| 451 | memcpy(p, mac, maclen); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | time_t periodic_ra(time_t now) |
| 460 | { |
| 461 | struct search_param param; |
| 462 | struct dhcp_context *context; |
| 463 | time_t next_event; |
| 464 | char interface[IF_NAMESIZE+1]; |
| 465 | |
| 466 | param.now = now; |
| 467 | |
| 468 | while (1) |
| 469 | { |
| 470 | /* find overdue events, and time of first future event */ |
| 471 | for (next_event = 0, context = daemon->ra_contexts; context; context = context->next) |
| 472 | if (context->ra_time != 0) |
| 473 | { |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 474 | if (difftime(context->ra_time, now) <= 0.0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 475 | break; /* overdue */ |
| 476 | |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 477 | if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0) |
| 478 | next_event = context->ra_time; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* none overdue */ |
| 482 | if (!context) |
| 483 | break; |
| 484 | |
| 485 | /* There's a context overdue, but we can't find an interface |
| 486 | associated with it, because it's for a subnet we dont |
| 487 | have an interface on. Probably we're doing DHCP on |
| 488 | a remote subnet via a relay. Zero the timer, since we won't |
| 489 | ever be able to send ra's and satistfy it. */ |
| 490 | if (iface_enumerate(AF_INET6, ¶m, iface_search)) |
| 491 | context->ra_time = 0; |
| 492 | else if (indextoname(daemon->icmp6fd, param.iface, interface)) |
| 493 | send_ra(param.iface, interface, NULL); |
| 494 | } |
| 495 | |
| 496 | return next_event; |
| 497 | } |
| 498 | |
| 499 | static int iface_search(struct in6_addr *local, int prefix, |
| 500 | int scope, int if_index, int dad, void *vparam) |
| 501 | { |
| 502 | struct search_param *param = vparam; |
Simon Kelley | 741c295 | 2012-02-25 13:09:18 +0000 | [diff] [blame] | 503 | struct dhcp_context *context; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 504 | |
| 505 | (void)scope; |
| 506 | (void)dad; |
| 507 | |
| 508 | for (context = daemon->ra_contexts; context; context = context->next) |
| 509 | if (prefix == context->prefix && |
| 510 | is_same_net6(local, &context->start6, prefix) && |
| 511 | is_same_net6(local, &context->end6, prefix)) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 512 | if (context->ra_time != 0 && difftime(context->ra_time, param->now) <= 0.0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 513 | { |
| 514 | /* found an interface that's overdue for RA determine new |
| 515 | timeout value and zap other contexts on the same interface |
| 516 | so they don't timeout independently .*/ |
| 517 | param->iface = if_index; |
| 518 | |
| 519 | if (difftime(param->now, ra_short_period_start) < 60.0) |
| 520 | /* range 5 - 20 */ |
| 521 | context->ra_time = param->now + 5 + (rand16()/4400); |
| 522 | else |
| 523 | /* range 450 - 600 */ |
| 524 | context->ra_time = param->now + 450 + (rand16()/440); |
| 525 | |
| 526 | return 0; /* found, abort */ |
| 527 | } |
| 528 | |
| 529 | return 1; /* keep searching */ |
| 530 | } |
| 531 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 532 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 533 | #endif |