Simon Kelley | c47e3ba | 2014-01-08 17:07:54 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2014 Simon Kelley |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 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 | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 31 | int ind, managed, other, found_context, first, adv_router; |
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 | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 34 | struct in6_addr link_local, link_global, ula; |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame^] | 35 | unsigned int glob_pref_time, link_pref_time, ula_pref_time, adv_interval, prio; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct search_param { |
| 39 | time_t now; int iface; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 40 | char name[IF_NAMESIZE+1]; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 43 | 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] | 44 | static int add_prefixes(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 45 | int scope, int if_index, int flags, |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 46 | unsigned int preferred, unsigned int valid, void *vparam); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 47 | static int iface_search(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 48 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 49 | int prefered, int valid, void *vparam); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 50 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 51 | static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now); |
| 52 | static unsigned int calc_lifetime(struct ra_interface *ra); |
| 53 | static unsigned int calc_interval(struct ra_interface *ra); |
| 54 | static unsigned int calc_prio(struct ra_interface *ra); |
| 55 | static struct ra_interface *find_iface_param(char *iface); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 56 | |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 57 | static int hop_limit; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 58 | |
| 59 | void ra_init(time_t now) |
| 60 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 61 | struct icmp6_filter filter; |
| 62 | int fd; |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 63 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 64 | int class = IPTOS_CLASS_CS6; |
| 65 | #endif |
| 66 | int val = 255; /* radvd uses this value */ |
Simon Kelley | 7b6dd88 | 2012-03-01 10:26:16 +0000 | [diff] [blame] | 67 | socklen_t len = sizeof(int); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 68 | struct dhcp_context *context; |
| 69 | |
Simon Kelley | 843c96b | 2012-02-27 17:42:38 +0000 | [diff] [blame] | 70 | /* ensure this is around even if we're not doing DHCPv6 */ |
| 71 | expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet)); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 72 | |
| 73 | /* 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] | 74 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 75 | if ((context->flags & CONTEXT_RA_NAME)) |
| 76 | break; |
| 77 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 78 | /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */ |
| 79 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 80 | ICMP6_FILTER_SETBLOCKALL(&filter); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 81 | if (daemon->doing_ra) |
| 82 | { |
| 83 | ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter); |
| 84 | if (context) |
| 85 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter); |
| 86 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 87 | |
| 88 | if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 || |
Simon Kelley | c5379c1 | 2012-02-24 20:05:52 +0000 | [diff] [blame] | 89 | getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) || |
Simon Kelley | 0e88d53 | 2012-03-28 22:22:05 +0100 | [diff] [blame] | 90 | #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 91 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 || |
| 92 | #endif |
| 93 | !fix_fd(fd) || |
| 94 | !set_ipv6pktinfo(fd) || |
| 95 | setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) || |
| 96 | setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) || |
| 97 | setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1) |
| 98 | die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET); |
| 99 | |
| 100 | daemon->icmp6fd = fd; |
| 101 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 102 | if (daemon->doing_ra) |
| 103 | ra_start_unsolicted(now, NULL); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 106 | void ra_start_unsolicted(time_t now, struct dhcp_context *context) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 107 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 108 | /* 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] | 109 | if it's not appropriate to advertise those contexts. |
| 110 | This gets re-called on a netlink route-change to re-do the advertisement |
| 111 | and pick up new interfaces */ |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 112 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 113 | if (context) |
Simon Kelley | 1b75c1e | 2012-12-18 19:55:25 +0000 | [diff] [blame] | 114 | context->ra_short_period_start = context->ra_time = now; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 115 | else |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 116 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 117 | if (!(context->flags & CONTEXT_TEMPLATE)) |
Simon Kelley | 1b75c1e | 2012-12-18 19:55:25 +0000 | [diff] [blame] | 118 | { |
| 119 | context->ra_time = now + (rand16()/13000); /* range 0 - 5 */ |
| 120 | /* re-do frequently for a minute or so, in case the first gets lost. */ |
| 121 | context->ra_short_period_start = now; |
| 122 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 125 | void icmp6_packet(time_t now) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 126 | { |
| 127 | char interface[IF_NAMESIZE+1]; |
| 128 | ssize_t sz; |
| 129 | int if_index = 0; |
| 130 | struct cmsghdr *cmptr; |
| 131 | struct msghdr msg; |
| 132 | union { |
| 133 | struct cmsghdr align; /* this ensures alignment */ |
| 134 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 135 | } control_u; |
| 136 | struct sockaddr_in6 from; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 137 | unsigned char *packet; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 138 | struct iname *tmp; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 139 | |
| 140 | /* Note: use outpacket for input buffer */ |
| 141 | msg.msg_control = control_u.control6; |
| 142 | msg.msg_controllen = sizeof(control_u); |
| 143 | msg.msg_flags = 0; |
| 144 | msg.msg_name = &from; |
| 145 | msg.msg_namelen = sizeof(from); |
| 146 | msg.msg_iov = &daemon->outpacket; |
| 147 | msg.msg_iovlen = 1; |
| 148 | |
| 149 | if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8) |
| 150 | return; |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 151 | |
| 152 | packet = (unsigned char *)daemon->outpacket.iov_base; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 153 | |
| 154 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 155 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 156 | { |
| 157 | union { |
| 158 | unsigned char *c; |
| 159 | struct in6_pktinfo *p; |
| 160 | } p; |
| 161 | p.c = CMSG_DATA(cmptr); |
| 162 | |
| 163 | if_index = p.p->ipi6_ifindex; |
| 164 | } |
| 165 | |
| 166 | if (!indextoname(daemon->icmp6fd, if_index, interface)) |
| 167 | return; |
| 168 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 169 | if (!iface_check(AF_LOCAL, NULL, interface, NULL)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 170 | return; |
| 171 | |
| 172 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 173 | if (tmp->name && wildcard_match(tmp->name, interface)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 174 | return; |
| 175 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 176 | if (packet[1] != 0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 177 | return; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 178 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 179 | if (packet[0] == ICMP6_ECHO_REPLY) |
| 180 | lease_ping_reply(&from.sin6_addr, packet, interface); |
| 181 | else if (packet[0] == ND_ROUTER_SOLICIT) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 182 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 183 | char *mac = ""; |
| 184 | |
| 185 | /* look for link-layer address option for logging */ |
| 186 | if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz) |
| 187 | { |
| 188 | print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2); |
| 189 | mac = daemon->namebuff; |
| 190 | } |
| 191 | |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 192 | if (!option_bool(OPT_QUIET_RA)) |
| 193 | my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac); |
Simon Kelley | f632e56 | 2012-05-12 15:05:34 +0100 | [diff] [blame] | 194 | /* source address may not be valid in solicit request. */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 195 | 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] | 196 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 199 | 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] | 200 | { |
| 201 | struct ra_packet *ra; |
| 202 | struct ra_param parm; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 203 | struct sockaddr_in6 addr; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 204 | struct dhcp_context *context, *tmp, **up; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 205 | struct dhcp_netid iface_id; |
| 206 | struct dhcp_opt *opt_cfg; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 207 | struct ra_interface *ra_param = find_iface_param(iface_name); |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 208 | int done_dns = 0, old_prefix = 0; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 209 | unsigned int min_pref_time; |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 210 | #ifdef HAVE_LINUX_NETWORK |
| 211 | FILE *f; |
| 212 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 213 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 214 | parm.ind = iface; |
| 215 | parm.managed = 0; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 216 | parm.other = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 217 | parm.found_context = 0; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 218 | parm.adv_router = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 219 | parm.if_name = iface_name; |
| 220 | parm.first = 1; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 221 | parm.now = now; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 222 | parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 223 | parm.adv_interval = calc_interval(ra_param); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame^] | 224 | parm.prio = calc_prio(ra_param); |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 225 | |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame^] | 226 | save_counter(0); |
| 227 | ra = expand(sizeof(struct ra_packet)); |
| 228 | |
| 229 | ra->type = ND_ROUTER_ADVERT; |
| 230 | ra->code = 0; |
| 231 | ra->hop_limit = hop_limit; |
| 232 | ra->flags = parm.prio; |
| 233 | ra->lifetime = htons(calc_lifetime(ra_param)); |
| 234 | ra->reachable_time = 0; |
| 235 | ra->retrans_time = 0; |
| 236 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 237 | /* set tag with name == interface */ |
| 238 | iface_id.net = iface_name; |
| 239 | iface_id.next = NULL; |
| 240 | parm.tags = &iface_id; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 241 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 242 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 243 | { |
| 244 | context->flags &= ~CONTEXT_RA_DONE; |
| 245 | context->netid.next = &context->netid; |
| 246 | } |
| 247 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 248 | if (!iface_enumerate(AF_INET6, &parm, add_prefixes)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 249 | return; |
| 250 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 251 | /* Find smallest preferred time within address classes, |
| 252 | to use as lifetime for options. This is a rather arbitrary choice. */ |
| 253 | min_pref_time = 0xffffffff; |
| 254 | if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time) |
| 255 | min_pref_time = parm.glob_pref_time; |
| 256 | |
| 257 | if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time) |
| 258 | min_pref_time = parm.ula_pref_time; |
| 259 | |
| 260 | if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time) |
| 261 | min_pref_time = parm.link_pref_time; |
| 262 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 263 | /* Look for constructed contexts associated with addresses which have gone, |
| 264 | and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */ |
| 265 | for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp) |
| 266 | { |
| 267 | tmp = context->next; |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 268 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 269 | if (context->if_index == iface && (context->flags & CONTEXT_OLD)) |
| 270 | { |
| 271 | unsigned int old = difftime(now, context->address_lost_time); |
| 272 | |
| 273 | if (old > context->saved_valid) |
| 274 | { |
| 275 | /* We've advertised this enough, time to go */ |
| 276 | *up = context->next; |
| 277 | free(context); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | struct prefix_opt *opt; |
| 282 | struct in6_addr local = context->start6; |
| 283 | int do_slaac = 0; |
| 284 | |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 285 | old_prefix = 1; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 286 | |
| 287 | /* zero net part of address */ |
| 288 | setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU)); |
| 289 | |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 290 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 291 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 6ea1f23 | 2013-12-19 15:45:12 +0000 | [diff] [blame] | 292 | { |
| 293 | do_slaac = 1; |
| 294 | if (context->flags & CONTEXT_DHCP) |
| 295 | { |
| 296 | parm.other = 1; |
| 297 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 298 | parm.managed = 1; |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | /* don't do RA for non-ra-only unless --enable-ra is set */ |
| 304 | if (option_bool(OPT_RA)) |
| 305 | { |
| 306 | parm.managed = 1; |
| 307 | parm.other = 1; |
| 308 | } |
| 309 | } |
| 310 | |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 311 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 312 | { |
| 313 | opt->type = ICMP6_OPT_PREFIX; |
| 314 | opt->len = 4; |
| 315 | opt->prefix_len = context->prefix; |
| 316 | /* autonomous only if we're not doing dhcp, always set "on-link" */ |
| 317 | opt->flags = do_slaac ? 0xC0 : 0x80; |
| 318 | opt->valid_lifetime = htonl(context->saved_valid - old); |
| 319 | opt->preferred_lifetime = htonl(0); |
| 320 | opt->reserved = 0; |
| 321 | opt->prefix = local; |
| 322 | |
| 323 | inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN); |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 324 | if (!option_bool(OPT_QUIET_RA)) |
| 325 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | up = &context->next; |
| 329 | } |
| 330 | } |
| 331 | else |
| 332 | up = &context->next; |
| 333 | } |
| 334 | |
Simon Kelley | 4452292 | 2013-11-15 14:45:04 +0000 | [diff] [blame] | 335 | /* If we're advertising only old prefixes, set router lifetime to zero. */ |
| 336 | if (old_prefix && !parm.found_context) |
| 337 | ra->lifetime = htons(0); |
| 338 | |
| 339 | /* No prefixes to advertise. */ |
| 340 | if (!old_prefix && !parm.found_context) |
| 341 | return; |
| 342 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 343 | /* If we're sending router address instead of prefix in at least on prefix, |
| 344 | include the advertisement interval option. */ |
| 345 | if (parm.adv_router) |
| 346 | { |
| 347 | put_opt6_char(ICMP6_OPT_ADV_INTERVAL); |
| 348 | put_opt6_char(1); |
| 349 | put_opt6_short(0); |
| 350 | /* interval value is in milliseconds */ |
| 351 | put_opt6_long(1000 * calc_interval(find_iface_param(iface_name))); |
| 352 | } |
| 353 | |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 354 | #ifdef HAVE_LINUX_NETWORK |
| 355 | /* Note that IPv6 MTU is not necessarilly the same as the IPv4 MTU |
| 356 | available from SIOCGIFMTU */ |
| 357 | sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", iface_name); |
| 358 | if ((f = fopen(daemon->namebuff, "r"))) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 359 | { |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 360 | if (fgets(daemon->namebuff, MAXDNAME, f)) |
| 361 | { |
| 362 | put_opt6_char(ICMP6_OPT_MTU); |
| 363 | put_opt6_char(1); |
| 364 | put_opt6_short(0); |
| 365 | put_opt6_long(atoi(daemon->namebuff)); |
| 366 | } |
| 367 | fclose(f); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 368 | } |
Simon Kelley | 3b43646 | 2012-12-28 11:55:45 +0000 | [diff] [blame] | 369 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 370 | |
| 371 | iface_enumerate(AF_LOCAL, &iface, add_lla); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 372 | |
| 373 | /* RDNSS, RFC 6106, use relevant DHCP6 options */ |
| 374 | (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 375 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 376 | for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next) |
| 377 | { |
| 378 | int i; |
| 379 | |
| 380 | /* netids match and not encapsulated? */ |
| 381 | if (!(opt_cfg->flags & DHOPT_TAGOK)) |
| 382 | continue; |
| 383 | |
| 384 | if (opt_cfg->opt == OPTION6_DNS_SERVER) |
| 385 | { |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 386 | struct in6_addr *a; |
| 387 | int len; |
| 388 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 389 | done_dns = 1; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 390 | |
| 391 | if (opt_cfg->len == 0) |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 392 | continue; |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 393 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 394 | /* reduce len for any addresses we can't substitute */ |
| 395 | for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0; |
| 396 | i < opt_cfg->len; i += IN6ADDRSZ, a++) |
| 397 | if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) || |
| 398 | (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) || |
| 399 | (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0)) |
| 400 | len -= IN6ADDRSZ; |
| 401 | |
| 402 | if (len != 0) |
| 403 | { |
| 404 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 405 | put_opt6_char((len/8) + 1); |
| 406 | put_opt6_short(0); |
| 407 | put_opt6_long(min_pref_time); |
| 408 | |
| 409 | for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++) |
| 410 | if (IN6_IS_ADDR_UNSPECIFIED(a)) |
| 411 | { |
| 412 | if (parm.glob_pref_time != 0) |
| 413 | put_opt6(&parm.link_global, IN6ADDRSZ); |
| 414 | } |
| 415 | else if (IN6_IS_ADDR_ULA_ZERO(a)) |
| 416 | { |
| 417 | if (parm.ula_pref_time != 0) |
| 418 | put_opt6(&parm.ula, IN6ADDRSZ); |
| 419 | } |
| 420 | else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a)) |
| 421 | { |
| 422 | if (parm.link_pref_time != 0) |
| 423 | put_opt6(&parm.link_local, IN6ADDRSZ); |
| 424 | } |
| 425 | else |
| 426 | put_opt6(a, IN6ADDRSZ); |
| 427 | } |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0) |
| 431 | { |
| 432 | int len = ((opt_cfg->len+7)/8); |
| 433 | |
| 434 | put_opt6_char(ICMP6_OPT_DNSSL); |
| 435 | put_opt6_char(len + 1); |
| 436 | put_opt6_short(0); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 437 | put_opt6_long(min_pref_time); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 438 | put_opt6(opt_cfg->val, opt_cfg->len); |
| 439 | |
| 440 | /* pad */ |
| 441 | for (i = opt_cfg->len; i < len * 8; i++) |
| 442 | put_opt6_char(0); |
| 443 | } |
| 444 | } |
| 445 | |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 446 | if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0) |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 447 | { |
Simon Kelley | ab915f8 | 2013-04-30 10:41:28 +0100 | [diff] [blame] | 448 | /* default == us, as long as we are supplying DNS service. */ |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 449 | put_opt6_char(ICMP6_OPT_RDNSS); |
| 450 | put_opt6_char(3); |
| 451 | put_opt6_short(0); |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 452 | put_opt6_long(min_pref_time); |
Simon Kelley | 806cf78 | 2013-10-14 14:08:44 +0100 | [diff] [blame] | 453 | put_opt6(&parm.link_local, IN6ADDRSZ); |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 454 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 455 | |
| 456 | /* set managed bits unless we're providing only RA on this link */ |
| 457 | if (parm.managed) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 458 | ra->flags |= 0x80; /* M flag, managed, */ |
| 459 | if (parm.other) |
| 460 | ra->flags |= 0x40; /* O flag, other */ |
| 461 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 462 | /* decide where we're sending */ |
| 463 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 464 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 465 | addr.sin6_len = sizeof(struct sockaddr_in6); |
| 466 | #endif |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 467 | addr.sin6_family = AF_INET6; |
| 468 | addr.sin6_port = htons(IPPROTO_ICMPV6); |
| 469 | if (dest) |
| 470 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 471 | addr.sin6_addr = *dest; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 472 | if (IN6_IS_ADDR_LINKLOCAL(dest) || |
| 473 | IN6_IS_ADDR_MC_LINKLOCAL(dest)) |
| 474 | addr.sin6_scope_id = iface; |
| 475 | } |
| 476 | else |
Simon Kelley | 8f3194f | 2013-09-30 15:04:58 +0100 | [diff] [blame] | 477 | { |
| 478 | inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr); |
| 479 | setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &iface, sizeof(iface)); |
| 480 | } |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 481 | |
Simon Kelley | 8f3194f | 2013-09-30 15:04:58 +0100 | [diff] [blame] | 482 | while (sendto(daemon->icmp6fd, daemon->outpacket.iov_base, save_counter(0), 0, |
| 483 | (struct sockaddr *)&addr, sizeof(addr)) == -1 && retry_send()); |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 484 | |
| 485 | } |
| 486 | |
| 487 | static int add_prefixes(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 488 | int scope, int if_index, int flags, |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 489 | unsigned int preferred, unsigned int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 490 | { |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 491 | struct ra_param *param = vparam; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 492 | |
| 493 | (void)scope; /* warning */ |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 494 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 495 | if (if_index == param->ind) |
| 496 | { |
| 497 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 498 | { |
| 499 | /* Can there be more than one LL address? |
| 500 | Select the one with the longest preferred time |
| 501 | if there is. */ |
| 502 | if (preferred > param->link_pref_time) |
| 503 | { |
| 504 | param->link_pref_time = preferred; |
| 505 | param->link_local = *local; |
| 506 | } |
| 507 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 508 | else if (!IN6_IS_ADDR_LOOPBACK(local) && |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 509 | !IN6_IS_ADDR_MULTICAST(local)) |
| 510 | { |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 511 | int real_prefix = 0; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 512 | int do_slaac = 0; |
| 513 | int deprecate = 0; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 514 | int constructed = 0; |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 515 | int adv_router = 0; |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 516 | unsigned int time = 0xffffffff; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 517 | struct dhcp_context *context; |
| 518 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 519 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 520 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 521 | prefix <= context->prefix && |
| 522 | is_same_net6(local, &context->start6, context->prefix) && |
| 523 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 524 | { |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 525 | context->saved_valid = valid; |
| 526 | |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 527 | if (context->flags & CONTEXT_RA) |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 528 | { |
| 529 | do_slaac = 1; |
Simon Kelley | 4723d49 | 2012-03-30 21:04:17 +0100 | [diff] [blame] | 530 | if (context->flags & CONTEXT_DHCP) |
Simon Kelley | 05e92e5 | 2012-03-30 22:24:15 +0100 | [diff] [blame] | 531 | { |
| 532 | param->other = 1; |
| 533 | if (!(context->flags & CONTEXT_RA_STATELESS)) |
| 534 | param->managed = 1; |
| 535 | } |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 536 | } |
Simon Kelley | 884a6df | 2012-03-20 16:20:22 +0000 | [diff] [blame] | 537 | else |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 538 | { |
| 539 | /* don't do RA for non-ra-only unless --enable-ra is set */ |
| 540 | if (!option_bool(OPT_RA)) |
| 541 | continue; |
| 542 | param->managed = 1; |
Simon Kelley | 30cd966 | 2012-03-25 20:44:38 +0100 | [diff] [blame] | 543 | param->other = 1; |
Simon Kelley | 0010b47 | 2012-02-29 12:18:30 +0000 | [diff] [blame] | 544 | } |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 545 | |
| 546 | /* Configured to advertise router address, not prefix. See RFC 3775 7.2 |
| 547 | In this case we do all addresses associated with a context, |
| 548 | hence the real_prefix setting here. */ |
| 549 | if (context->flags & CONTEXT_RA_ROUTER) |
| 550 | { |
| 551 | adv_router = 1; |
| 552 | param->adv_router = 1; |
| 553 | real_prefix = context->prefix; |
| 554 | } |
| 555 | |
Simon Kelley | 1ecbaaa | 2013-07-25 14:19:27 +0100 | [diff] [blame] | 556 | /* find floor time, don't reduce below 3 * RA interval. */ |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 557 | if (time > context->lease_time) |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 558 | { |
| 559 | time = context->lease_time; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 560 | if (time < ((unsigned int)(3 * param->adv_interval))) |
| 561 | time = 3 * param->adv_interval; |
Simon Kelley | 7f035f5 | 2012-12-22 21:27:08 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 564 | if (context->flags & CONTEXT_DEPRECATE) |
| 565 | deprecate = 1; |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 566 | |
| 567 | if (context->flags & CONTEXT_CONSTRUCTED) |
| 568 | constructed = 1; |
| 569 | |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 570 | |
Simon Kelley | 18f0fb0 | 2012-03-31 21:18:55 +0100 | [diff] [blame] | 571 | /* collect dhcp-range tags */ |
| 572 | if (context->netid.next == &context->netid && context->netid.net) |
| 573 | { |
| 574 | context->netid.next = param->tags; |
| 575 | param->tags = &context->netid; |
| 576 | } |
| 577 | |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 578 | /* subsequent prefixes on the same interface |
| 579 | and subsequent instances of this prefix don't need timers. |
| 580 | Be careful not to find the same prefix twice with different |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 581 | addresses unless we're advertising the actual addresses. */ |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 582 | if (!(context->flags & CONTEXT_RA_DONE)) |
| 583 | { |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 584 | if (!param->first) |
| 585 | context->ra_time = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 586 | context->flags |= CONTEXT_RA_DONE; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 587 | real_prefix = context->prefix; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 588 | } |
Simon Kelley | 5ae34bf | 2012-06-04 21:14:03 +0100 | [diff] [blame] | 589 | |
| 590 | param->first = 0; |
| 591 | param->found_context = 1; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 592 | } |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 593 | |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 594 | /* configured time is ceiling */ |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 595 | if (!constructed || valid > time) |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 596 | valid = time; |
| 597 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 598 | if (flags & IFACE_DEPRECATED) |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 599 | preferred = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 600 | |
Simon Kelley | 3bc0d93 | 2012-12-28 11:31:44 +0000 | [diff] [blame] | 601 | if (deprecate) |
| 602 | time = 0; |
| 603 | |
| 604 | /* configured time is ceiling */ |
| 605 | if (!constructed || preferred > time) |
| 606 | preferred = time; |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 607 | |
| 608 | if (IN6_IS_ADDR_ULA(local)) |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 609 | { |
Simon Kelley | c3a0408 | 2014-01-11 22:18:19 +0000 | [diff] [blame] | 610 | if (preferred > param->ula_pref_time) |
| 611 | { |
| 612 | param->ula_pref_time = preferred; |
| 613 | param->ula = *local; |
| 614 | } |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | if (preferred > param->glob_pref_time) |
| 619 | { |
| 620 | param->glob_pref_time = preferred; |
| 621 | param->link_global = *local; |
| 622 | } |
Simon Kelley | 55b42f6 | 2012-12-21 16:53:15 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 625 | if (real_prefix != 0) |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 626 | { |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 627 | struct prefix_opt *opt; |
| 628 | |
| 629 | if ((opt = expand(sizeof(struct prefix_opt)))) |
| 630 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 631 | /* zero net part of address */ |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 632 | if (!adv_router) |
| 633 | setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU)); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 634 | |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 635 | opt->type = ICMP6_OPT_PREFIX; |
| 636 | opt->len = 4; |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 637 | opt->prefix_len = real_prefix; |
Simon Kelley | fd05f12 | 2012-08-12 17:48:50 +0100 | [diff] [blame] | 638 | /* autonomous only if we're not doing dhcp, always set "on-link" */ |
Simon Kelley | 7ea3d3f | 2014-04-25 22:04:05 +0100 | [diff] [blame] | 639 | opt->flags = 0x80; |
| 640 | if (do_slaac) |
| 641 | opt->flags |= 0x40; |
| 642 | if (adv_router) |
| 643 | opt->flags |= 0x20; |
Simon Kelley | ed8b68a | 2012-12-21 16:23:26 +0000 | [diff] [blame] | 644 | opt->valid_lifetime = htonl(valid); |
| 645 | opt->preferred_lifetime = htonl(preferred); |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 646 | opt->reserved = 0; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 647 | opt->prefix = *local; |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 648 | |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 649 | inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN); |
Kevin Darbyshire-Bryant | 8c0b73d | 2013-10-11 11:56:33 +0100 | [diff] [blame] | 650 | if (!option_bool(OPT_QUIET_RA)) |
| 651 | my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff); |
Ilya Ponetaev | 5bf50af | 2014-09-09 12:46:21 +0100 | [diff] [blame^] | 652 | |
| 653 | /* Send Route Information option (RFC4191, 2.3) */ |
| 654 | put_opt6_char(ICMP6_OPT_RT_INFO); |
| 655 | /* Length in units of 8 octets will be 1 (header) + |
| 656 | * 0, 1 or 2 (0...128 bits / 64 bit per unit) */ |
| 657 | if (0 == prefix) |
| 658 | put_opt6_char(1); |
| 659 | else if (prefix < 65) |
| 660 | put_opt6_char(2); |
| 661 | else |
| 662 | put_opt6_char(3); |
| 663 | |
| 664 | put_opt6_char(prefix); |
| 665 | /* Same priority and advertised prefix */ |
| 666 | put_opt6_char(param->prio); |
| 667 | /* "valid lifetime" seems more reasonable than "preferred" */ |
| 668 | put_opt6_long(valid); |
| 669 | /* Actual prefix, only necessary part |
| 670 | * Don't append any data in case of prefix length == 0 */ |
| 671 | if (0 != prefix) |
| 672 | { |
| 673 | if (prefix < 65) |
| 674 | put_opt6((void *)local, 8); |
| 675 | else |
| 676 | put_opt6((void *)local, 16); |
| 677 | } |
Simon Kelley | 1e02a85 | 2012-03-29 11:07:25 +0100 | [diff] [blame] | 678 | } |
Simon Kelley | c825754 | 2012-03-28 21:15:41 +0100 | [diff] [blame] | 679 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
| 686 | { |
| 687 | (void)type; |
| 688 | |
| 689 | if (index == *((int *)parm)) |
| 690 | { |
| 691 | /* size is in units of 8 octets and includes type and length (2 bytes) |
| 692 | add 7 to round up */ |
| 693 | int len = (maclen + 9) >> 3; |
| 694 | unsigned char *p = expand(len << 3); |
| 695 | memset(p, 0, len << 3); |
| 696 | *p++ = ICMP6_OPT_SOURCE_MAC; |
| 697 | *p++ = len; |
| 698 | memcpy(p, mac, maclen); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | return 1; |
| 704 | } |
| 705 | |
| 706 | time_t periodic_ra(time_t now) |
| 707 | { |
| 708 | struct search_param param; |
| 709 | struct dhcp_context *context; |
| 710 | time_t next_event; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 711 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 712 | param.now = now; |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 713 | param.iface = 0; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 714 | |
| 715 | while (1) |
| 716 | { |
| 717 | /* find overdue events, and time of first future event */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 718 | for (next_event = 0, context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 719 | if (context->ra_time != 0) |
| 720 | { |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 721 | if (difftime(context->ra_time, now) <= 0.0) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 722 | break; /* overdue */ |
| 723 | |
Simon Kelley | 7dbe981 | 2012-03-25 14:49:54 +0100 | [diff] [blame] | 724 | if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0) |
| 725 | next_event = context->ra_time; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* none overdue */ |
| 729 | if (!context) |
| 730 | break; |
| 731 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 732 | if ((context->flags & CONTEXT_OLD) && |
| 733 | context->if_index != 0 && |
Simon Kelley | dd9d9ce | 2013-11-15 11:24:00 +0000 | [diff] [blame] | 734 | indextoname(daemon->icmp6fd, context->if_index, param.name)) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 735 | { |
| 736 | /* A context for an old address. We'll not find the interface by |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 737 | looking for addresses, but we know it anyway, since the context is |
| 738 | constructed */ |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 739 | param.iface = context->if_index; |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 740 | new_timeout(context, param.name, now); |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 741 | } |
| 742 | else if (iface_enumerate(AF_INET6, ¶m, iface_search)) |
| 743 | /* There's a context overdue, but we can't find an interface |
| 744 | associated with it, because it's for a subnet we dont |
| 745 | have an interface on. Probably we're doing DHCP on |
| 746 | a remote subnet via a relay. Zero the timer, since we won't |
| 747 | ever be able to send ra's and satistfy it. */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 748 | context->ra_time = 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 749 | |
| 750 | if (param.iface != 0 && |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 751 | iface_check(AF_LOCAL, NULL, param.name, NULL)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 752 | { |
| 753 | struct iname *tmp; |
| 754 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 755 | if (tmp->name && wildcard_match(tmp->name, param.name)) |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 756 | break; |
| 757 | if (!tmp) |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 758 | send_ra(now, param.iface, param.name, NULL); |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 761 | return next_event; |
| 762 | } |
Simon Kelley | 421594f | 2012-12-02 12:17:35 +0000 | [diff] [blame] | 763 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 764 | static int iface_search(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 765 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 766 | int preferred, int valid, void *vparam) |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 767 | { |
| 768 | struct search_param *param = vparam; |
Simon Kelley | 741c295 | 2012-02-25 13:09:18 +0000 | [diff] [blame] | 769 | struct dhcp_context *context; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 770 | |
| 771 | (void)scope; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 772 | (void)preferred; |
| 773 | (void)valid; |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 774 | |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 775 | for (context = daemon->dhcp6; context; context = context->next) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 776 | if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 777 | prefix <= context->prefix && |
| 778 | is_same_net6(local, &context->start6, context->prefix) && |
| 779 | is_same_net6(local, &context->end6, context->prefix) && |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 780 | context->ra_time != 0 && |
| 781 | difftime(context->ra_time, param->now) <= 0.0) |
| 782 | { |
| 783 | /* found an interface that's overdue for RA determine new |
| 784 | timeout value and arrange for RA to be sent unless interface is |
| 785 | still doing DAD.*/ |
| 786 | |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 787 | if (!(flags & IFACE_TENTATIVE)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 788 | param->iface = if_index; |
| 789 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 790 | /* should never fail */ |
| 791 | if (!indextoname(daemon->icmp6fd, if_index, param->name)) |
| 792 | { |
| 793 | param->iface = 0; |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | new_timeout(context, param->name, param->now); |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 798 | |
| 799 | /* zero timers for other contexts on the same subnet, so they don't timeout |
| 800 | independently */ |
| 801 | for (context = context->next; context; context = context->next) |
Vladislav Grishenko | 4c82efc | 2013-12-03 16:05:30 +0000 | [diff] [blame] | 802 | if (prefix <= context->prefix && |
| 803 | is_same_net6(local, &context->start6, context->prefix) && |
| 804 | is_same_net6(local, &context->end6, context->prefix)) |
Simon Kelley | 6e3dba3 | 2012-12-16 21:52:45 +0000 | [diff] [blame] | 805 | context->ra_time = 0; |
| 806 | |
| 807 | return 0; /* found, abort */ |
| 808 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 809 | |
| 810 | return 1; /* keep searching */ |
| 811 | } |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 812 | |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 813 | static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 814 | { |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 815 | if (difftime(now, context->ra_short_period_start) < 60.0) |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 816 | /* range 5 - 20 */ |
| 817 | context->ra_time = now + 5 + (rand16()/4400); |
| 818 | else |
Simon Kelley | c4cd95d | 2013-10-10 20:58:11 +0100 | [diff] [blame] | 819 | { |
| 820 | /* range 3/4 - 1 times MaxRtrAdvInterval */ |
| 821 | unsigned int adv_interval = calc_interval(find_iface_param(iface_name)); |
| 822 | context->ra_time = now + (3 * adv_interval)/4 + ((adv_interval * (unsigned int)rand16()) >> 18); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | static struct ra_interface *find_iface_param(char *iface) |
| 827 | { |
| 828 | struct ra_interface *ra; |
| 829 | |
| 830 | for (ra = daemon->ra_interfaces; ra; ra = ra->next) |
| 831 | if (wildcard_match(ra->name, iface)) |
| 832 | return ra; |
| 833 | |
| 834 | return NULL; |
| 835 | } |
| 836 | |
| 837 | static unsigned int calc_interval(struct ra_interface *ra) |
| 838 | { |
| 839 | int interval = 600; |
| 840 | |
| 841 | if (ra && ra->interval != 0) |
| 842 | { |
| 843 | interval = ra->interval; |
| 844 | if (interval > 1800) |
| 845 | interval = 1800; |
| 846 | else if (interval < 4) |
| 847 | interval = 4; |
| 848 | } |
| 849 | |
| 850 | return (unsigned int)interval; |
| 851 | } |
| 852 | |
| 853 | static unsigned int calc_lifetime(struct ra_interface *ra) |
| 854 | { |
| 855 | int lifetime, interval = (int)calc_interval(ra); |
| 856 | |
| 857 | if (!ra || ra->lifetime == -1) /* not specified */ |
| 858 | lifetime = 3 * interval; |
| 859 | else |
| 860 | { |
| 861 | lifetime = ra->lifetime; |
| 862 | if (lifetime < interval && lifetime != 0) |
| 863 | lifetime = interval; |
| 864 | else if (lifetime > 9000) |
| 865 | lifetime = 9000; |
| 866 | } |
| 867 | |
| 868 | return (unsigned int)lifetime; |
| 869 | } |
| 870 | |
| 871 | static unsigned int calc_prio(struct ra_interface *ra) |
| 872 | { |
| 873 | if (ra) |
| 874 | return ra->prio; |
| 875 | |
| 876 | return 0; |
Simon Kelley | ef1a94a | 2013-07-26 13:59:03 +0100 | [diff] [blame] | 877 | } |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 878 | |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 879 | #endif |