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