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