Print this page
3245 in.ndp daemon should not be session leader
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c
+++ new/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 *
21 21 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
22 22 */
23 23
24 24 #include "defs.h"
25 25 #include "tables.h"
26 26 #include <fcntl.h>
27 27 #include <sys/un.h>
28 28
29 29 static void initlog(void);
30 30 static void run_timeouts(void);
31 31
32 32 static void advertise(struct sockaddr_in6 *sin6, struct phyint *pi,
33 33 boolean_t no_prefixes);
34 34 static void solicit(struct sockaddr_in6 *sin6, struct phyint *pi);
35 35 static void initifs(boolean_t first);
36 36 static void check_if_removed(struct phyint *pi);
37 37 static void loopback_ra_enqueue(struct phyint *pi,
38 38 struct nd_router_advert *ra, int len);
39 39 static void loopback_ra_dequeue(void);
40 40 static void check_daemonize(void);
41 41
42 42 struct in6_addr all_nodes_mcast = { { 0xff, 0x2, 0x0, 0x0,
43 43 0x0, 0x0, 0x0, 0x0,
44 44 0x0, 0x0, 0x0, 0x0,
45 45 0x0, 0x0, 0x0, 0x1 } };
46 46
47 47 struct in6_addr all_routers_mcast = { { 0xff, 0x2, 0x0, 0x0,
48 48 0x0, 0x0, 0x0, 0x0,
49 49 0x0, 0x0, 0x0, 0x0,
50 50 0x0, 0x0, 0x0, 0x2 } };
51 51
52 52 static struct sockaddr_in6 v6allnodes = { AF_INET6, 0, 0,
53 53 { 0xff, 0x2, 0x0, 0x0,
54 54 0x0, 0x0, 0x0, 0x0,
55 55 0x0, 0x0, 0x0, 0x0,
56 56 0x0, 0x0, 0x0, 0x1 } };
57 57
58 58 static struct sockaddr_in6 v6allrouters = { AF_INET6, 0, 0,
59 59 { 0xff, 0x2, 0x0, 0x0,
60 60 0x0, 0x0, 0x0, 0x0,
61 61 0x0, 0x0, 0x0, 0x0,
62 62 0x0, 0x0, 0x0, 0x2 } };
63 63
64 64 static char **argv0; /* Saved for re-exec on SIGHUP */
65 65
66 66 static uint64_t packet[(IP_MAXPACKET + 1)/8];
67 67
68 68 static int show_ifs = 0;
69 69 static boolean_t already_daemonized = _B_FALSE;
70 70 int debug = 0;
71 71 int no_loopback = 0; /* Do not send RA packets to ourselves */
72 72
73 73 /*
74 74 * Size of routing socket message used by in.ndpd which includes the header,
75 75 * space for the RTA_DST, RTA_GATEWAY and RTA_NETMASK (each a sockaddr_in6)
76 76 * plus space for the RTA_IFP (a sockaddr_dl).
77 77 */
78 78 #define NDP_RTM_MSGLEN sizeof (struct rt_msghdr) + \
79 79 sizeof (struct sockaddr_in6) + \
80 80 sizeof (struct sockaddr_in6) + \
81 81 sizeof (struct sockaddr_in6) + \
82 82 sizeof (struct sockaddr_dl)
83 83
84 84 /*
85 85 * These are referenced externally in tables.c in order to fill in the
86 86 * dynamic portions of the routing socket message and then to send the message
87 87 * itself.
88 88 */
89 89 int rtsock = -1; /* Routing socket */
90 90 struct rt_msghdr *rt_msg; /* Routing socket message */
91 91 struct sockaddr_in6 *rta_gateway; /* RTA_GATEWAY sockaddr */
92 92 struct sockaddr_dl *rta_ifp; /* RTA_IFP sockaddr */
93 93
94 94 /*
95 95 * These sockets are used internally in this file.
96 96 */
97 97 static int mibsock = -1; /* mib request socket */
98 98 static int cmdsock = -1; /* command socket */
99 99
100 100 static int ndpd_setup_cmd_listener(void);
101 101 static void ndpd_cmd_handler(int);
102 102 static int ndpd_process_cmd(int, ipadm_ndpd_msg_t *);
103 103 static int ndpd_send_error(int, int);
104 104 static int ndpd_set_autoconf(const char *, boolean_t);
105 105 static int ndpd_create_addrs(const char *, struct sockaddr_in6, int,
106 106 boolean_t, boolean_t, char *);
107 107 static int ndpd_delete_addrs(const char *);
108 108 static int phyint_check_ipadm_intfid(struct phyint *);
109 109
110 110 /*
111 111 * Return the current time in milliseconds truncated to
112 112 * fit in an integer.
113 113 */
114 114 uint_t
115 115 getcurrenttime(void)
116 116 {
117 117 struct timeval tp;
118 118
119 119 if (gettimeofday(&tp, NULL) < 0) {
120 120 logperror("getcurrenttime: gettimeofday failed");
121 121 exit(1);
122 122 }
123 123 return (tp.tv_sec * 1000 + tp.tv_usec / 1000);
124 124 }
125 125
126 126 /*
127 127 * Output a preformated packet from the packet[] buffer.
128 128 */
129 129 static void
130 130 sendpacket(struct sockaddr_in6 *sin6, int sock, int size, int flags)
131 131 {
132 132 int cc;
133 133 char abuf[INET6_ADDRSTRLEN];
134 134
135 135 cc = sendto(sock, (char *)packet, size, flags,
136 136 (struct sockaddr *)sin6, sizeof (*sin6));
137 137 if (cc < 0 || cc != size) {
138 138 if (cc < 0) {
139 139 logperror("sendpacket: sendto");
140 140 }
141 141 logmsg(LOG_ERR, "sendpacket: wrote %s %d chars, ret=%d\n",
142 142 inet_ntop(sin6->sin6_family,
143 143 (void *)&sin6->sin6_addr,
144 144 abuf, sizeof (abuf)),
145 145 size, cc);
146 146 }
147 147 }
148 148
149 149 /*
150 150 * If possible, place an ND_OPT_SOURCE_LINKADDR option at `optp'.
151 151 * Return the number of bytes placed in the option.
152 152 */
153 153 static uint_t
154 154 add_opt_lla(struct phyint *pi, struct nd_opt_lla *optp)
155 155 {
156 156 uint_t optlen;
157 157 uint_t hwaddrlen;
158 158 struct lifreq lifr;
159 159
160 160 /* If this phyint doesn't have a link-layer address, bail */
161 161 if (phyint_get_lla(pi, &lifr) == -1)
162 162 return (0);
163 163
164 164 hwaddrlen = lifr.lifr_nd.lnr_hdw_len;
165 165 /* roundup to multiple of 8 and make padding zero */
166 166 optlen = ((sizeof (struct nd_opt_hdr) + hwaddrlen + 7) / 8) * 8;
167 167 bzero(optp, optlen);
168 168 optp->nd_opt_lla_type = ND_OPT_SOURCE_LINKADDR;
169 169 optp->nd_opt_lla_len = optlen / 8;
170 170 bcopy(lifr.lifr_nd.lnr_hdw_addr, optp->nd_opt_lla_hdw_addr, hwaddrlen);
171 171
172 172 return (optlen);
173 173 }
174 174
175 175 /* Send a Router Solicitation */
176 176 static void
177 177 solicit(struct sockaddr_in6 *sin6, struct phyint *pi)
178 178 {
179 179 int packetlen = 0;
180 180 struct nd_router_solicit *rs = (struct nd_router_solicit *)packet;
181 181 char *pptr = (char *)packet;
182 182
183 183 rs->nd_rs_type = ND_ROUTER_SOLICIT;
184 184 rs->nd_rs_code = 0;
185 185 rs->nd_rs_cksum = htons(0);
186 186 rs->nd_rs_reserved = htonl(0);
187 187
188 188 packetlen += sizeof (*rs);
189 189 pptr += sizeof (*rs);
190 190
191 191 /* add options */
192 192 packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
193 193
194 194 if (debug & D_PKTOUT) {
195 195 print_route_sol("Sending solicitation to ", pi, rs, packetlen,
196 196 sin6);
197 197 }
198 198 sendpacket(sin6, pi->pi_sock, packetlen, 0);
199 199 }
200 200
201 201 /*
202 202 * Send a (set of) Router Advertisements and feed them back to ourselves
203 203 * for processing. Unless no_prefixes is set all prefixes are included.
204 204 * If there are too many prefix options to fit in one packet multiple
205 205 * packets will be sent - each containing a subset of the prefix options.
206 206 */
207 207 static void
208 208 advertise(struct sockaddr_in6 *sin6, struct phyint *pi, boolean_t no_prefixes)
209 209 {
210 210 struct nd_opt_prefix_info *po;
211 211 char *pptr = (char *)packet;
212 212 struct nd_router_advert *ra;
213 213 struct adv_prefix *adv_pr;
214 214 int packetlen = 0;
215 215
216 216 ra = (struct nd_router_advert *)pptr;
217 217 ra->nd_ra_type = ND_ROUTER_ADVERT;
218 218 ra->nd_ra_code = 0;
219 219 ra->nd_ra_cksum = htons(0);
220 220 ra->nd_ra_curhoplimit = pi->pi_AdvCurHopLimit;
221 221 ra->nd_ra_flags_reserved = 0;
222 222 if (pi->pi_AdvManagedFlag)
223 223 ra->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
224 224 if (pi->pi_AdvOtherConfigFlag)
225 225 ra->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
226 226
227 227 if (pi->pi_adv_state == FINAL_ADV)
228 228 ra->nd_ra_router_lifetime = htons(0);
229 229 else
230 230 ra->nd_ra_router_lifetime = htons(pi->pi_AdvDefaultLifetime);
231 231 ra->nd_ra_reachable = htonl(pi->pi_AdvReachableTime);
232 232 ra->nd_ra_retransmit = htonl(pi->pi_AdvRetransTimer);
233 233
234 234 packetlen = sizeof (*ra);
235 235 pptr += sizeof (*ra);
236 236
237 237 if (pi->pi_adv_state == FINAL_ADV) {
238 238 if (debug & D_PKTOUT) {
239 239 print_route_adv("Sending advert (FINAL) to ", pi,
240 240 ra, packetlen, sin6);
241 241 }
242 242 sendpacket(sin6, pi->pi_sock, packetlen, 0);
243 243 /* Feed packet back in for router operation */
244 244 loopback_ra_enqueue(pi, ra, packetlen);
245 245 return;
246 246 }
247 247
248 248 /* add options */
249 249 packetlen += add_opt_lla(pi, (struct nd_opt_lla *)pptr);
250 250 pptr = (char *)packet + packetlen;
251 251
252 252 if (pi->pi_AdvLinkMTU != 0) {
253 253 struct nd_opt_mtu *mo = (struct nd_opt_mtu *)pptr;
254 254
255 255 mo->nd_opt_mtu_type = ND_OPT_MTU;
256 256 mo->nd_opt_mtu_len = sizeof (struct nd_opt_mtu) / 8;
257 257 mo->nd_opt_mtu_reserved = 0;
258 258 mo->nd_opt_mtu_mtu = htonl(pi->pi_AdvLinkMTU);
259 259
260 260 packetlen += sizeof (struct nd_opt_mtu);
261 261 pptr += sizeof (struct nd_opt_mtu);
262 262 }
263 263
264 264 if (no_prefixes) {
265 265 if (debug & D_PKTOUT) {
266 266 print_route_adv("Sending advert to ", pi,
267 267 ra, packetlen, sin6);
268 268 }
269 269 sendpacket(sin6, pi->pi_sock, packetlen, 0);
270 270 /* Feed packet back in for router operation */
271 271 loopback_ra_enqueue(pi, ra, packetlen);
272 272 return;
273 273 }
274 274
275 275 po = (struct nd_opt_prefix_info *)pptr;
276 276 for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
277 277 adv_pr = adv_pr->adv_pr_next) {
278 278 if (!adv_pr->adv_pr_AdvOnLinkFlag &&
279 279 !adv_pr->adv_pr_AdvAutonomousFlag) {
280 280 continue;
281 281 }
282 282
283 283 /*
284 284 * If the prefix doesn't fit in packet send
285 285 * what we have so far and start with new packet.
286 286 */
287 287 if (packetlen + sizeof (*po) >
288 288 pi->pi_LinkMTU - sizeof (struct ip6_hdr)) {
289 289 if (debug & D_PKTOUT) {
290 290 print_route_adv("Sending advert "
291 291 "(FRAG) to ",
292 292 pi, ra, packetlen, sin6);
293 293 }
294 294 sendpacket(sin6, pi->pi_sock, packetlen, 0);
295 295 /* Feed packet back in for router operation */
296 296 loopback_ra_enqueue(pi, ra, packetlen);
297 297 packetlen = sizeof (*ra);
298 298 pptr = (char *)packet + sizeof (*ra);
299 299 po = (struct nd_opt_prefix_info *)pptr;
300 300 }
301 301 po->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
302 302 po->nd_opt_pi_len = sizeof (*po)/8;
303 303 po->nd_opt_pi_flags_reserved = 0;
304 304 if (adv_pr->adv_pr_AdvOnLinkFlag) {
305 305 po->nd_opt_pi_flags_reserved |=
306 306 ND_OPT_PI_FLAG_ONLINK;
307 307 }
308 308 if (adv_pr->adv_pr_AdvAutonomousFlag) {
309 309 po->nd_opt_pi_flags_reserved |=
310 310 ND_OPT_PI_FLAG_AUTO;
311 311 }
312 312 po->nd_opt_pi_prefix_len = adv_pr->adv_pr_prefix_len;
313 313 /*
314 314 * If both Adv*Expiration and Adv*Lifetime are
315 315 * set we prefer the former and make the lifetime
316 316 * decrement in real time.
317 317 */
318 318 if (adv_pr->adv_pr_AdvValidRealTime) {
319 319 po->nd_opt_pi_valid_time =
320 320 htonl(adv_pr->adv_pr_AdvValidExpiration);
321 321 } else {
322 322 po->nd_opt_pi_valid_time =
323 323 htonl(adv_pr->adv_pr_AdvValidLifetime);
324 324 }
325 325 if (adv_pr->adv_pr_AdvPreferredRealTime) {
326 326 po->nd_opt_pi_preferred_time =
327 327 htonl(adv_pr->adv_pr_AdvPreferredExpiration);
328 328 } else {
329 329 po->nd_opt_pi_preferred_time =
330 330 htonl(adv_pr->adv_pr_AdvPreferredLifetime);
331 331 }
332 332 po->nd_opt_pi_reserved2 = htonl(0);
333 333 po->nd_opt_pi_prefix = adv_pr->adv_pr_prefix;
334 334
335 335 po++;
336 336 packetlen += sizeof (*po);
337 337 }
338 338 if (debug & D_PKTOUT) {
339 339 print_route_adv("Sending advert to ", pi,
340 340 ra, packetlen, sin6);
341 341 }
342 342 sendpacket(sin6, pi->pi_sock, packetlen, 0);
343 343 /* Feed packet back in for router operation */
344 344 loopback_ra_enqueue(pi, ra, packetlen);
345 345 }
346 346
347 347 /* Poll support */
348 348 static int pollfd_num = 0; /* Allocated and initialized */
349 349 static struct pollfd *pollfds = NULL;
350 350
351 351 /*
352 352 * Add fd to the set being polled. Returns 0 if ok; -1 if failed.
353 353 */
354 354 int
355 355 poll_add(int fd)
356 356 {
357 357 int i;
358 358 int new_num;
359 359 struct pollfd *newfds;
360 360
361 361 /* Check if already present */
362 362 for (i = 0; i < pollfd_num; i++) {
363 363 if (pollfds[i].fd == fd)
364 364 return (0);
365 365 }
366 366 /* Check for empty spot already present */
367 367 for (i = 0; i < pollfd_num; i++) {
368 368 if (pollfds[i].fd == -1) {
369 369 pollfds[i].fd = fd;
370 370 return (0);
371 371 }
372 372 }
373 373
374 374 /* Allocate space for 32 more fds and initialize to -1 */
375 375 new_num = pollfd_num + 32;
376 376 newfds = realloc(pollfds, new_num * sizeof (struct pollfd));
377 377 if (newfds == NULL) {
378 378 logperror("realloc");
379 379 return (-1);
380 380 }
381 381
382 382 newfds[pollfd_num].fd = fd;
383 383 newfds[pollfd_num++].events = POLLIN;
384 384
385 385 for (i = pollfd_num; i < new_num; i++) {
386 386 newfds[i].fd = -1;
387 387 newfds[i].events = POLLIN;
388 388 }
389 389 pollfd_num = new_num;
390 390 pollfds = newfds;
391 391 return (0);
392 392 }
393 393
394 394 /*
395 395 * Remove fd from the set being polled. Returns 0 if ok; -1 if failed.
396 396 */
397 397 int
398 398 poll_remove(int fd)
399 399 {
400 400 int i;
401 401
402 402 /* Check if already present */
403 403 for (i = 0; i < pollfd_num; i++) {
404 404 if (pollfds[i].fd == fd) {
405 405 pollfds[i].fd = -1;
406 406 return (0);
407 407 }
408 408 }
409 409 return (-1);
410 410 }
411 411
412 412 /*
413 413 * Extract information about the ifname (either a physical interface and
414 414 * the ":0" logical interface or just a logical interface).
415 415 * If the interface (still) exists in kernel set pr_in_use
416 416 * for caller to be able to detect interfaces that are removed.
417 417 * Starts sending advertisements/solicitations when new physical interfaces
418 418 * are detected.
419 419 */
420 420 static void
421 421 if_process(int s, char *ifname, boolean_t first)
422 422 {
423 423 struct lifreq lifr;
424 424 struct phyint *pi;
425 425 struct prefix *pr;
426 426 char *cp;
427 427 char phyintname[LIFNAMSIZ + 1];
428 428
429 429 if (debug & D_IFSCAN)
430 430 logmsg(LOG_DEBUG, "if_process(%s)\n", ifname);
431 431
432 432 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
433 433 lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
434 434 if (ioctl(s, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
435 435 if (errno == ENXIO) {
436 436 /*
437 437 * Interface has disappeared
438 438 */
439 439 return;
440 440 }
441 441 logperror("if_process: ioctl (get interface flags)");
442 442 return;
443 443 }
444 444
445 445 /*
446 446 * Ignore loopback, point-to-multipoint and VRRP interfaces.
447 447 * The IP addresses over VRRP interfaces cannot be auto-configured.
448 448 * Point-to-point interfaces always have IFF_MULTICAST set.
449 449 */
450 450 if (!(lifr.lifr_flags & IFF_MULTICAST) ||
451 451 (lifr.lifr_flags & (IFF_LOOPBACK|IFF_VRRP))) {
452 452 return;
453 453 }
454 454
455 455 if (!(lifr.lifr_flags & IFF_IPV6))
456 456 return;
457 457
458 458 (void) strncpy(phyintname, ifname, sizeof (phyintname));
459 459 phyintname[sizeof (phyintname) - 1] = '\0';
460 460 if ((cp = strchr(phyintname, IF_SEPARATOR)) != NULL) {
461 461 *cp = '\0';
462 462 }
463 463
464 464 pi = phyint_lookup(phyintname);
465 465 if (pi == NULL) {
466 466 pi = phyint_create(phyintname);
467 467 if (pi == NULL) {
468 468 logmsg(LOG_ERR, "if_process: out of memory\n");
469 469 return;
470 470 }
471 471 /*
472 472 * if in.ndpd is restarted, check with ipmgmtd if there is any
473 473 * interface id to be configured for this interface.
474 474 */
475 475 if (first) {
476 476 if (phyint_check_ipadm_intfid(pi) == -1)
477 477 logmsg(LOG_ERR, "Could not get ipadm info\n");
478 478 }
479 479 } else {
480 480 /*
481 481 * if the phyint already exists, synchronize it with
482 482 * the kernel state. For a newly created phyint, phyint_create
483 483 * calls phyint_init_from_k().
484 484 */
485 485 (void) phyint_init_from_k(pi);
486 486 }
487 487 if (pi->pi_sock == -1 && !(pi->pi_kernel_state & PI_PRESENT)) {
488 488 /* Interface is not yet present */
489 489 if (debug & D_PHYINT) {
490 490 logmsg(LOG_DEBUG, "if_process: interface not yet "
491 491 "present %s\n", pi->pi_name);
492 492 }
493 493 return;
494 494 }
495 495
496 496 if (pi->pi_sock != -1) {
497 497 if (poll_add(pi->pi_sock) == -1) {
498 498 /*
499 499 * reset state.
500 500 */
501 501 phyint_cleanup(pi);
502 502 }
503 503 }
504 504
505 505 /*
506 506 * Check if IFF_ROUTER has been turned off in kernel in which
507 507 * case we have to turn off AdvSendAdvertisements.
508 508 * The kernel will automatically turn off IFF_ROUTER if
509 509 * ip6_forwarding is turned off.
510 510 * Note that we do not switch back should IFF_ROUTER be turned on.
511 511 */
512 512 if (!first &&
513 513 pi->pi_AdvSendAdvertisements && !(pi->pi_flags & IFF_ROUTER)) {
514 514 logmsg(LOG_INFO, "No longer a router on %s\n", pi->pi_name);
515 515 check_to_advertise(pi, START_FINAL_ADV);
516 516
517 517 pi->pi_AdvSendAdvertisements = 0;
518 518 pi->pi_sol_state = NO_SOLICIT;
519 519 }
520 520
521 521 /*
522 522 * Send advertisments and solicitation only if the interface is
523 523 * present in the kernel.
524 524 */
525 525 if (pi->pi_kernel_state & PI_PRESENT) {
526 526
527 527 if (pi->pi_AdvSendAdvertisements) {
528 528 if (pi->pi_adv_state == NO_ADV)
529 529 check_to_advertise(pi, START_INIT_ADV);
530 530 } else {
531 531 if (pi->pi_sol_state == NO_SOLICIT)
532 532 check_to_solicit(pi, START_INIT_SOLICIT);
533 533 }
534 534 }
535 535
536 536 /*
537 537 * Track static kernel prefixes to prevent in.ndpd from clobbering
538 538 * them by creating a struct prefix for each prefix detected in the
539 539 * kernel.
540 540 */
541 541 pr = prefix_lookup_name(pi, ifname);
542 542 if (pr == NULL) {
543 543 pr = prefix_create_name(pi, ifname);
544 544 if (pr == NULL) {
545 545 logmsg(LOG_ERR, "if_process: out of memory\n");
546 546 return;
547 547 }
548 548 if (prefix_init_from_k(pr) == -1) {
549 549 prefix_delete(pr);
550 550 return;
551 551 }
552 552 }
553 553 /* Detect prefixes which are removed */
554 554 if (pr->pr_kernel_state != 0)
555 555 pr->pr_in_use = _B_TRUE;
556 556
557 557 if ((lifr.lifr_flags & IFF_DUPLICATE) &&
558 558 !(lifr.lifr_flags & IFF_DHCPRUNNING) &&
559 559 (pr->pr_flags & IFF_TEMPORARY)) {
560 560 in6_addr_t *token;
561 561 int i;
562 562 char abuf[INET6_ADDRSTRLEN];
563 563
564 564 if (++pr->pr_attempts >= MAX_DAD_FAILURES) {
565 565 logmsg(LOG_ERR, "%s: token %s is duplicate after %d "
566 566 "attempts; disabling temporary addresses on %s",
567 567 pr->pr_name, inet_ntop(AF_INET6,
568 568 (void *)&pi->pi_tmp_token, abuf, sizeof (abuf)),
569 569 pr->pr_attempts, pi->pi_name);
570 570 pi->pi_TmpAddrsEnabled = 0;
571 571 tmptoken_delete(pi);
572 572 prefix_delete(pr);
573 573 return;
574 574 }
575 575 logmsg(LOG_WARNING, "%s: token %s is duplicate; trying again",
576 576 pr->pr_name, inet_ntop(AF_INET6, (void *)&pi->pi_tmp_token,
577 577 abuf, sizeof (abuf)));
578 578 if (!tmptoken_create(pi)) {
579 579 prefix_delete(pr);
580 580 return;
581 581 }
582 582 token = &pi->pi_tmp_token;
583 583 for (i = 0; i < 16; i++) {
584 584 /*
585 585 * prefix_create ensures that pr_prefix has all-zero
586 586 * bits after prefixlen.
587 587 */
588 588 pr->pr_address.s6_addr[i] = pr->pr_prefix.s6_addr[i] |
589 589 token->s6_addr[i];
590 590 }
591 591 if (prefix_lookup_addr_match(pr) != NULL) {
592 592 prefix_delete(pr);
593 593 return;
594 594 }
595 595 pr->pr_CreateTime = getcurrenttime() / MILLISEC;
596 596 /*
597 597 * We've got a new token. Clearing PR_AUTO causes
598 598 * prefix_update_k to bring the interface up and set the
599 599 * address.
600 600 */
601 601 pr->pr_kernel_state &= ~PR_AUTO;
602 602 prefix_update_k(pr);
603 603 }
604 604 }
605 605
606 606 static int ifsock = -1;
607 607
608 608 /*
609 609 * Scan all interfaces to detect changes as well as new and deleted intefaces
610 610 * 'first' is set for the initial call only. Do not effect anything.
611 611 */
612 612 static void
613 613 initifs(boolean_t first)
614 614 {
615 615 char *buf;
616 616 int bufsize;
617 617 int numifs;
618 618 int n;
619 619 struct lifnum lifn;
620 620 struct lifconf lifc;
621 621 struct lifreq *lifr;
622 622 struct phyint *pi;
623 623 struct phyint *next_pi;
624 624 struct prefix *pr;
625 625
626 626 if (debug & D_IFSCAN)
627 627 logmsg(LOG_DEBUG, "Reading interface configuration\n");
628 628 if (ifsock < 0) {
629 629 ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
630 630 if (ifsock < 0) {
631 631 logperror("initifs: socket");
632 632 return;
633 633 }
634 634 }
635 635 lifn.lifn_family = AF_INET6;
636 636 lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
637 637 if (ioctl(ifsock, SIOCGLIFNUM, (char *)&lifn) < 0) {
638 638 logperror("initifs: ioctl (get interface numbers)");
639 639 return;
640 640 }
641 641 numifs = lifn.lifn_count;
642 642 bufsize = numifs * sizeof (struct lifreq);
643 643
644 644 buf = (char *)malloc(bufsize);
645 645 if (buf == NULL) {
646 646 logmsg(LOG_ERR, "initifs: out of memory\n");
647 647 return;
648 648 }
649 649
650 650 /*
651 651 * Mark the interfaces so that we can find phyints and prefixes
652 652 * which have disappeared from the kernel.
653 653 * if_process will set pr_in_use when it finds the interface
654 654 * in the kernel.
655 655 */
656 656 for (pi = phyints; pi != NULL; pi = pi->pi_next) {
657 657 /*
658 658 * Before re-examining the state of the interfaces,
659 659 * PI_PRESENT should be cleared from pi_kernel_state.
660 660 */
661 661 pi->pi_kernel_state &= ~PI_PRESENT;
662 662 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
663 663 pr->pr_in_use = _B_FALSE;
664 664 }
665 665 }
666 666
667 667 lifc.lifc_family = AF_INET6;
668 668 lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY;
669 669 lifc.lifc_len = bufsize;
670 670 lifc.lifc_buf = buf;
671 671
672 672 if (ioctl(ifsock, SIOCGLIFCONF, (char *)&lifc) < 0) {
673 673 logperror("initifs: ioctl (get interface configuration)");
674 674 free(buf);
675 675 return;
676 676 }
677 677
678 678 lifr = (struct lifreq *)lifc.lifc_req;
679 679 for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifr++)
680 680 if_process(ifsock, lifr->lifr_name, first);
681 681 free(buf);
682 682
683 683 /*
684 684 * Detect phyints that have been removed from the kernel.
685 685 * Since we can't recreate it here (would require ifconfig plumb
686 686 * logic) we just terminate use of that phyint.
687 687 */
688 688 for (pi = phyints; pi != NULL; pi = next_pi) {
689 689 next_pi = pi->pi_next;
690 690 /*
691 691 * If interface (still) exists in kernel, set
692 692 * pi_state to indicate that.
693 693 */
694 694 if (pi->pi_kernel_state & PI_PRESENT) {
695 695 pi->pi_state |= PI_PRESENT;
696 696 }
697 697
698 698 check_if_removed(pi);
699 699 }
700 700 if (show_ifs)
701 701 phyint_print_all();
702 702 }
703 703
704 704
705 705 /*
706 706 * Router advertisement state machine. Used for everything but timer
707 707 * events which use advertise_event directly.
708 708 */
709 709 void
710 710 check_to_advertise(struct phyint *pi, enum adv_events event)
711 711 {
712 712 uint_t delay;
713 713 enum adv_states old_state = pi->pi_adv_state;
714 714
715 715 if (debug & D_STATE) {
716 716 logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d\n",
717 717 pi->pi_name, (int)event, (int)old_state);
718 718 }
719 719 delay = advertise_event(pi, event, 0);
720 720 if (delay != TIMER_INFINITY) {
721 721 /* Make sure the global next event is updated */
722 722 timer_schedule(delay);
723 723 }
724 724
725 725 if (debug & D_STATE) {
726 726 logmsg(LOG_DEBUG, "check_to_advertise(%s, %d) state %d -> %d\n",
727 727 pi->pi_name, (int)event, (int)old_state,
728 728 (int)pi->pi_adv_state);
729 729 }
730 730 }
731 731
732 732 /*
733 733 * Router advertisement state machine.
734 734 * Return the number of milliseconds until next timeout (TIMER_INFINITY
735 735 * if never).
736 736 * For the ADV_TIMER event the caller passes in the number of milliseconds
737 737 * since the last timer event in the 'elapsed' parameter.
738 738 */
739 739 uint_t
740 740 advertise_event(struct phyint *pi, enum adv_events event, uint_t elapsed)
741 741 {
742 742 uint_t delay;
743 743
744 744 if (debug & D_STATE) {
745 745 logmsg(LOG_DEBUG, "advertise_event(%s, %d, %d) state %d\n",
746 746 pi->pi_name, (int)event, elapsed, (int)pi->pi_adv_state);
747 747 }
748 748 check_daemonize();
749 749 if (!pi->pi_AdvSendAdvertisements)
750 750 return (TIMER_INFINITY);
751 751 if (pi->pi_flags & IFF_NORTEXCH) {
752 752 if (debug & D_PKTOUT) {
753 753 logmsg(LOG_DEBUG, "Suppress sending RA packet on %s "
754 754 "(no route exchange on interface)\n",
755 755 pi->pi_name);
756 756 }
757 757 return (TIMER_INFINITY);
758 758 }
759 759
760 760 switch (event) {
761 761 case ADV_OFF:
762 762 pi->pi_adv_state = NO_ADV;
763 763 return (TIMER_INFINITY);
764 764
765 765 case START_INIT_ADV:
766 766 if (pi->pi_adv_state == INIT_ADV)
767 767 return (pi->pi_adv_time_left);
768 768 pi->pi_adv_count = ND_MAX_INITIAL_RTR_ADVERTISEMENTS;
769 769 pi->pi_adv_time_left = 0;
770 770 pi->pi_adv_state = INIT_ADV;
771 771 break; /* send advertisement */
772 772
773 773 case START_FINAL_ADV:
774 774 if (pi->pi_adv_state == NO_ADV)
775 775 return (TIMER_INFINITY);
776 776 if (pi->pi_adv_state == FINAL_ADV)
777 777 return (pi->pi_adv_time_left);
778 778 pi->pi_adv_count = ND_MAX_FINAL_RTR_ADVERTISEMENTS;
779 779 pi->pi_adv_time_left = 0;
780 780 pi->pi_adv_state = FINAL_ADV;
781 781 break; /* send advertisement */
782 782
783 783 case RECEIVED_SOLICIT:
784 784 if (pi->pi_adv_state == NO_ADV)
785 785 return (TIMER_INFINITY);
786 786 if (pi->pi_adv_state == SOLICIT_ADV) {
787 787 if (pi->pi_adv_time_left != 0)
788 788 return (pi->pi_adv_time_left);
789 789 break;
790 790 }
791 791 delay = GET_RANDOM(0, ND_MAX_RA_DELAY_TIME);
792 792 if (delay < pi->pi_adv_time_left)
793 793 pi->pi_adv_time_left = delay;
794 794 if (pi->pi_adv_time_since_sent < ND_MIN_DELAY_BETWEEN_RAS) {
795 795 /*
796 796 * Send an advertisement (ND_MIN_DELAY_BETWEEN_RAS
797 797 * plus random delay) after the previous
798 798 * advertisement was sent.
799 799 */
800 800 pi->pi_adv_time_left = delay +
801 801 ND_MIN_DELAY_BETWEEN_RAS -
802 802 pi->pi_adv_time_since_sent;
803 803 }
804 804 pi->pi_adv_state = SOLICIT_ADV;
805 805 break;
806 806
807 807 case ADV_TIMER:
808 808 if (pi->pi_adv_state == NO_ADV)
809 809 return (TIMER_INFINITY);
810 810 /* Decrease time left */
811 811 if (pi->pi_adv_time_left >= elapsed)
812 812 pi->pi_adv_time_left -= elapsed;
813 813 else
814 814 pi->pi_adv_time_left = 0;
815 815
816 816 /* Increase time since last advertisement was sent */
817 817 pi->pi_adv_time_since_sent += elapsed;
818 818 break;
819 819 default:
820 820 logmsg(LOG_ERR, "advertise_event: Unknown event %d\n",
821 821 (int)event);
822 822 return (TIMER_INFINITY);
823 823 }
824 824
825 825 if (pi->pi_adv_time_left != 0)
826 826 return (pi->pi_adv_time_left);
827 827
828 828 /* Send advertisement and calculate next time to send */
829 829 if (pi->pi_adv_state == FINAL_ADV) {
830 830 /* Omit the prefixes */
831 831 advertise(&v6allnodes, pi, _B_TRUE);
832 832 } else {
833 833 advertise(&v6allnodes, pi, _B_FALSE);
834 834 }
835 835 pi->pi_adv_time_since_sent = 0;
836 836
837 837 switch (pi->pi_adv_state) {
838 838 case SOLICIT_ADV:
839 839 /*
840 840 * The solicited advertisement has been sent.
841 841 * Revert to periodic advertisements.
842 842 */
843 843 pi->pi_adv_state = REG_ADV;
844 844 /* FALLTHRU */
845 845 case REG_ADV:
846 846 pi->pi_adv_time_left =
847 847 GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
848 848 1000 * pi->pi_MaxRtrAdvInterval);
849 849 break;
850 850
851 851 case INIT_ADV:
852 852 if (--pi->pi_adv_count > 0) {
853 853 delay = GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
854 854 1000 * pi->pi_MaxRtrAdvInterval);
855 855 if (delay > ND_MAX_INITIAL_RTR_ADVERT_INTERVAL)
856 856 delay = ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
857 857 pi->pi_adv_time_left = delay;
858 858 } else {
859 859 pi->pi_adv_time_left =
860 860 GET_RANDOM(1000 * pi->pi_MinRtrAdvInterval,
861 861 1000 * pi->pi_MaxRtrAdvInterval);
862 862 pi->pi_adv_state = REG_ADV;
863 863 }
864 864 break;
865 865
866 866 case FINAL_ADV:
867 867 if (--pi->pi_adv_count > 0) {
868 868 pi->pi_adv_time_left =
869 869 ND_MAX_INITIAL_RTR_ADVERT_INTERVAL;
870 870 } else {
871 871 pi->pi_adv_state = NO_ADV;
872 872 }
873 873 break;
874 874 }
875 875 if (pi->pi_adv_state != NO_ADV)
876 876 return (pi->pi_adv_time_left);
877 877 else
878 878 return (TIMER_INFINITY);
879 879 }
880 880
881 881 /*
882 882 * Router solicitation state machine. Used for everything but timer
883 883 * events which use solicit_event directly.
884 884 */
885 885 void
886 886 check_to_solicit(struct phyint *pi, enum solicit_events event)
887 887 {
888 888 uint_t delay;
889 889 enum solicit_states old_state = pi->pi_sol_state;
890 890
891 891 if (debug & D_STATE) {
892 892 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
893 893 pi->pi_name, (int)event, (int)old_state);
894 894 }
895 895 delay = solicit_event(pi, event, 0);
896 896 if (delay != TIMER_INFINITY) {
897 897 /* Make sure the global next event is updated */
898 898 timer_schedule(delay);
899 899 }
900 900
↓ open down ↓ |
900 lines elided |
↑ open up ↑ |
901 901 if (debug & D_STATE) {
902 902 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
903 903 pi->pi_name, (int)event, (int)old_state,
904 904 (int)pi->pi_sol_state);
905 905 }
906 906 }
907 907
908 908 static void
909 909 daemonize_ndpd(void)
910 910 {
911 - FILE *pidfp;
912 - mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
913 911 struct itimerval it;
914 912 boolean_t timerval = _B_TRUE;
915 913
916 914 /*
917 915 * Need to get current timer settings so they can be restored
918 916 * after the fork(), as the it_value and it_interval values for
919 917 * the ITIMER_REAL timer are reset to 0 in the child process.
920 918 */
921 919 if (getitimer(ITIMER_REAL, &it) < 0) {
922 920 if (debug & D_TIMER)
923 921 logmsg(LOG_DEBUG,
924 922 "daemonize_ndpd: failed to get itimerval\n");
925 923 timerval = _B_FALSE;
926 924 }
927 925
928 926 /* Daemonize. */
929 - switch (fork()) {
930 - case 0:
931 - /* Child */
932 - break;
933 - case -1:
927 + if (daemon(0, 0) == -1) {
934 928 logperror("fork");
935 929 exit(1);
936 - default:
937 - /* Parent */
938 - _exit(0);
939 930 }
940 931
941 - /* Store our process id, blow away any existing file if it exists. */
942 - if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
943 - (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
944 - argv0[0], strerror(errno));
945 - } else {
946 - (void) fprintf(pidfp, "%ld\n", getpid());
947 - (void) fclose(pidfp);
948 - (void) chmod(PATH_PID, pidmode);
949 - }
950 -
951 - (void) close(0);
952 - (void) close(1);
953 - (void) close(2);
954 -
955 - (void) chdir("/");
956 - (void) open("/dev/null", O_RDWR);
957 - (void) dup2(0, 1);
958 - (void) dup2(0, 2);
959 - (void) setsid();
960 -
961 932 already_daemonized = _B_TRUE;
962 933
963 934 /*
964 935 * Restore timer values, if we were able to save them; if not,
965 936 * check and set the right value by calling run_timeouts().
966 937 */
967 938 if (timerval) {
968 939 if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
969 940 logperror("daemonize_ndpd: setitimer");
970 941 exit(2);
971 942 }
972 943 } else {
973 944 run_timeouts();
974 945 }
975 946 }
976 947
977 948 /*
978 949 * Check to see if the time is right to daemonize. The right time is when:
979 950 *
980 951 * 1. We haven't already daemonized.
981 952 * 2. We are not in debug mode.
982 953 * 3. All interfaces are marked IFF_NOXMIT.
983 954 * 4. All non-router interfaces have their prefixes set up and we're
984 955 * done sending router solicitations on those interfaces without
985 956 * prefixes.
986 957 */
987 958 static void
988 959 check_daemonize(void)
989 960 {
990 961 struct phyint *pi;
991 962
992 963 if (already_daemonized || debug != 0)
993 964 return;
994 965
995 966 for (pi = phyints; pi != NULL; pi = pi->pi_next) {
996 967 if (!(pi->pi_flags & IFF_NOXMIT))
997 968 break;
998 969 }
999 970
1000 971 /*
1001 972 * If we can't transmit on any of the interfaces there is no reason
1002 973 * to hold up progress.
1003 974 */
1004 975 if (pi == NULL) {
1005 976 daemonize_ndpd();
1006 977 return;
1007 978 }
1008 979
1009 980 /* Check all interfaces. If any are still soliciting, just return. */
1010 981 for (pi = phyints; pi != NULL; pi = pi->pi_next) {
1011 982 if (pi->pi_AdvSendAdvertisements ||
1012 983 !(pi->pi_kernel_state & PI_PRESENT))
1013 984 continue;
1014 985
1015 986 if (pi->pi_sol_state == INIT_SOLICIT)
1016 987 return;
1017 988 }
1018 989
1019 990 daemonize_ndpd();
1020 991 }
1021 992
1022 993 /*
1023 994 * Router solicitation state machine.
1024 995 * Return the number of milliseconds until next timeout (TIMER_INFINITY
1025 996 * if never).
1026 997 * For the SOL_TIMER event the caller passes in the number of milliseconds
1027 998 * since the last timer event in the 'elapsed' parameter.
1028 999 */
1029 1000 uint_t
1030 1001 solicit_event(struct phyint *pi, enum solicit_events event, uint_t elapsed)
1031 1002 {
1032 1003 if (debug & D_STATE) {
1033 1004 logmsg(LOG_DEBUG, "solicit_event(%s, %d, %d) state %d\n",
1034 1005 pi->pi_name, (int)event, elapsed, (int)pi->pi_sol_state);
1035 1006 }
1036 1007
1037 1008 if (pi->pi_AdvSendAdvertisements)
1038 1009 return (TIMER_INFINITY);
1039 1010 if (pi->pi_flags & IFF_NORTEXCH) {
1040 1011 if (debug & D_PKTOUT) {
1041 1012 logmsg(LOG_DEBUG, "Suppress sending RS packet on %s "
1042 1013 "(no route exchange on interface)\n",
1043 1014 pi->pi_name);
1044 1015 }
1045 1016 return (TIMER_INFINITY);
1046 1017 }
1047 1018
1048 1019 switch (event) {
1049 1020 case SOLICIT_OFF:
1050 1021 pi->pi_sol_state = NO_SOLICIT;
1051 1022 check_daemonize();
1052 1023 return (TIMER_INFINITY);
1053 1024
1054 1025 case SOLICIT_DONE:
1055 1026 pi->pi_sol_state = DONE_SOLICIT;
1056 1027 check_daemonize();
1057 1028 return (TIMER_INFINITY);
1058 1029
1059 1030 case RESTART_INIT_SOLICIT:
1060 1031 /*
1061 1032 * This event allows us to start solicitation over again
1062 1033 * without losing the RA flags. We start solicitation over
1063 1034 * when we are missing an interface prefix for a newly-
1064 1035 * encountered DHCP interface.
1065 1036 */
1066 1037 if (pi->pi_sol_state == INIT_SOLICIT)
1067 1038 return (pi->pi_sol_time_left);
1068 1039 pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
1069 1040 pi->pi_sol_time_left =
1070 1041 GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
1071 1042 pi->pi_sol_state = INIT_SOLICIT;
1072 1043 break;
1073 1044
1074 1045 case START_INIT_SOLICIT:
1075 1046 if (pi->pi_sol_state == INIT_SOLICIT)
1076 1047 return (pi->pi_sol_time_left);
1077 1048 pi->pi_ra_flags = 0;
1078 1049 pi->pi_sol_count = ND_MAX_RTR_SOLICITATIONS;
1079 1050 pi->pi_sol_time_left =
1080 1051 GET_RANDOM(0, ND_MAX_RTR_SOLICITATION_DELAY);
1081 1052 pi->pi_sol_state = INIT_SOLICIT;
1082 1053 break;
1083 1054
1084 1055 case SOL_TIMER:
1085 1056 if (pi->pi_sol_state == NO_SOLICIT)
1086 1057 return (TIMER_INFINITY);
1087 1058 /* Decrease time left */
1088 1059 if (pi->pi_sol_time_left >= elapsed)
1089 1060 pi->pi_sol_time_left -= elapsed;
1090 1061 else
1091 1062 pi->pi_sol_time_left = 0;
1092 1063 break;
1093 1064 default:
1094 1065 logmsg(LOG_ERR, "solicit_event: Unknown event %d\n",
1095 1066 (int)event);
1096 1067 return (TIMER_INFINITY);
1097 1068 }
1098 1069
1099 1070 if (pi->pi_sol_time_left != 0)
1100 1071 return (pi->pi_sol_time_left);
1101 1072
1102 1073 /* Send solicitation and calculate next time */
1103 1074 switch (pi->pi_sol_state) {
1104 1075 case INIT_SOLICIT:
1105 1076 solicit(&v6allrouters, pi);
1106 1077 if (--pi->pi_sol_count == 0) {
1107 1078 if (debug & D_STATE) {
1108 1079 logmsg(LOG_DEBUG, "solicit_event: no routers "
1109 1080 "found on %s; assuming default flags\n",
1110 1081 pi->pi_name);
1111 1082 }
1112 1083 if (pi->pi_autoconf && pi->pi_StatefulAddrConf) {
1113 1084 pi->pi_ra_flags |= ND_RA_FLAG_MANAGED |
1114 1085 ND_RA_FLAG_OTHER;
1115 1086 start_dhcp(pi);
1116 1087 }
1117 1088 pi->pi_sol_state = DONE_SOLICIT;
1118 1089 check_daemonize();
1119 1090 return (TIMER_INFINITY);
1120 1091 }
1121 1092 pi->pi_sol_time_left = ND_RTR_SOLICITATION_INTERVAL;
1122 1093 return (pi->pi_sol_time_left);
1123 1094 case NO_SOLICIT:
1124 1095 case DONE_SOLICIT:
1125 1096 return (TIMER_INFINITY);
1126 1097 default:
1127 1098 return (pi->pi_sol_time_left);
1128 1099 }
1129 1100 }
1130 1101
1131 1102 /*
1132 1103 * Timer mechanism using relative time (in milliseconds) from the
1133 1104 * previous timer event. Timers exceeding TIMER_INFINITY milliseconds
1134 1105 * will fire after TIMER_INFINITY milliseconds.
1135 1106 */
1136 1107 static uint_t timer_previous; /* When last SIGALRM occurred */
1137 1108 static uint_t timer_next; /* Currently scheduled timeout */
1138 1109
1139 1110 static void
1140 1111 timer_init(void)
1141 1112 {
1142 1113 timer_previous = getcurrenttime();
1143 1114 timer_next = TIMER_INFINITY;
1144 1115 run_timeouts();
1145 1116 }
1146 1117
1147 1118 /*
1148 1119 * Make sure the next SIGALRM occurs delay milliseconds from the current
1149 1120 * time if not earlier.
1150 1121 * Handles getcurrenttime (32 bit integer holding milliseconds) wraparound
1151 1122 * by treating differences greater than 0x80000000 as negative.
1152 1123 */
1153 1124 void
1154 1125 timer_schedule(uint_t delay)
1155 1126 {
1156 1127 uint_t now;
1157 1128 struct itimerval itimerval;
1158 1129
1159 1130 now = getcurrenttime();
1160 1131 if (debug & D_TIMER) {
1161 1132 logmsg(LOG_DEBUG, "timer_schedule(%u): now %u next %u\n",
1162 1133 delay, now, timer_next);
1163 1134 }
1164 1135 /* Will this timer occur before the currently scheduled SIGALRM? */
1165 1136 if (delay >= timer_next - now) {
1166 1137 if (debug & D_TIMER) {
1167 1138 logmsg(LOG_DEBUG, "timer_schedule(%u): no action - "
1168 1139 "next in %u ms\n",
1169 1140 delay, timer_next - now);
1170 1141 }
1171 1142 return;
1172 1143 }
1173 1144 if (delay == 0) {
1174 1145 /* Minimum allowed delay */
1175 1146 delay = 1;
1176 1147 }
1177 1148 timer_next = now + delay;
1178 1149
1179 1150 itimerval.it_value.tv_sec = delay / 1000;
1180 1151 itimerval.it_value.tv_usec = (delay % 1000) * 1000;
1181 1152 itimerval.it_interval.tv_sec = 0;
1182 1153 itimerval.it_interval.tv_usec = 0;
1183 1154 if (debug & D_TIMER) {
1184 1155 logmsg(LOG_DEBUG, "timer_schedule(%u): sec %lu usec %lu\n",
1185 1156 delay,
1186 1157 itimerval.it_value.tv_sec, itimerval.it_value.tv_usec);
1187 1158 }
1188 1159 if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) {
1189 1160 logperror("timer_schedule: setitimer");
1190 1161 exit(2);
1191 1162 }
1192 1163 }
1193 1164
1194 1165 /*
1195 1166 * Conditional running of timer. If more than 'minimal_time' millseconds
1196 1167 * since the timer routines were last run we run them.
1197 1168 * Used when packets arrive.
1198 1169 */
1199 1170 static void
1200 1171 conditional_run_timeouts(uint_t minimal_time)
1201 1172 {
1202 1173 uint_t now;
1203 1174 uint_t elapsed;
1204 1175
1205 1176 now = getcurrenttime();
1206 1177 elapsed = now - timer_previous;
1207 1178 if (elapsed > minimal_time) {
1208 1179 if (debug & D_TIMER) {
1209 1180 logmsg(LOG_DEBUG, "conditional_run_timeouts: "
1210 1181 "elapsed %d\n", elapsed);
1211 1182 }
1212 1183 run_timeouts();
1213 1184 }
1214 1185 }
1215 1186
1216 1187 /*
1217 1188 * Timer has fired.
1218 1189 * Determine when the next timer event will occur by asking all
1219 1190 * the timer routines.
1220 1191 * Should not be called from a timer routine but in some cases this is
1221 1192 * done because the code doesn't know that e.g. it was called from
1222 1193 * ifconfig_timer(). In this case the nested run_timeouts will just return but
1223 1194 * the running run_timeouts will ensure to call all the timer functions by
1224 1195 * looping once more.
1225 1196 */
1226 1197 static void
1227 1198 run_timeouts(void)
1228 1199 {
1229 1200 uint_t now;
1230 1201 uint_t elapsed;
1231 1202 uint_t next;
1232 1203 uint_t nexti;
1233 1204 struct phyint *pi;
1234 1205 struct phyint *next_pi;
1235 1206 struct prefix *pr;
1236 1207 struct prefix *next_pr;
1237 1208 struct adv_prefix *adv_pr;
1238 1209 struct adv_prefix *next_adv_pr;
1239 1210 struct router *dr;
1240 1211 struct router *next_dr;
1241 1212 static boolean_t timeout_running;
1242 1213 static boolean_t do_retry;
1243 1214
1244 1215 if (timeout_running) {
1245 1216 if (debug & D_TIMER)
1246 1217 logmsg(LOG_DEBUG, "run_timeouts: nested call\n");
1247 1218 do_retry = _B_TRUE;
1248 1219 return;
1249 1220 }
1250 1221 timeout_running = _B_TRUE;
1251 1222 retry:
1252 1223 /* How much time since the last time we were called? */
1253 1224 now = getcurrenttime();
1254 1225 elapsed = now - timer_previous;
1255 1226 timer_previous = now;
1256 1227
1257 1228 if (debug & D_TIMER)
1258 1229 logmsg(LOG_DEBUG, "run_timeouts: elapsed %d\n", elapsed);
1259 1230
1260 1231 next = TIMER_INFINITY;
1261 1232 for (pi = phyints; pi != NULL; pi = next_pi) {
1262 1233 next_pi = pi->pi_next;
1263 1234 nexti = phyint_timer(pi, elapsed);
1264 1235 if (nexti != TIMER_INFINITY && nexti < next)
1265 1236 next = nexti;
1266 1237 if (debug & D_TIMER) {
1267 1238 logmsg(LOG_DEBUG, "run_timeouts (pi %s): %d -> %u ms\n",
1268 1239 pi->pi_name, nexti, next);
1269 1240 }
1270 1241 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1271 1242 next_pr = pr->pr_next;
1272 1243 nexti = prefix_timer(pr, elapsed);
1273 1244 if (nexti != TIMER_INFINITY && nexti < next)
1274 1245 next = nexti;
1275 1246 if (debug & D_TIMER) {
1276 1247 logmsg(LOG_DEBUG, "run_timeouts (pr %s): "
1277 1248 "%d -> %u ms\n", pr->pr_name, nexti, next);
1278 1249 }
1279 1250 }
1280 1251 for (adv_pr = pi->pi_adv_prefix_list; adv_pr != NULL;
1281 1252 adv_pr = next_adv_pr) {
1282 1253 next_adv_pr = adv_pr->adv_pr_next;
1283 1254 nexti = adv_prefix_timer(adv_pr, elapsed);
1284 1255 if (nexti != TIMER_INFINITY && nexti < next)
1285 1256 next = nexti;
1286 1257 if (debug & D_TIMER) {
1287 1258 logmsg(LOG_DEBUG, "run_timeouts "
1288 1259 "(adv pr on %s): %d -> %u ms\n",
1289 1260 adv_pr->adv_pr_physical->pi_name,
1290 1261 nexti, next);
1291 1262 }
1292 1263 }
1293 1264 for (dr = pi->pi_router_list; dr != NULL; dr = next_dr) {
1294 1265 next_dr = dr->dr_next;
1295 1266 nexti = router_timer(dr, elapsed);
1296 1267 if (nexti != TIMER_INFINITY && nexti < next)
1297 1268 next = nexti;
1298 1269 if (debug & D_TIMER) {
1299 1270 logmsg(LOG_DEBUG, "run_timeouts (dr): "
1300 1271 "%d -> %u ms\n", nexti, next);
1301 1272 }
1302 1273 }
1303 1274 if (pi->pi_TmpAddrsEnabled) {
1304 1275 nexti = tmptoken_timer(pi, elapsed);
1305 1276 if (nexti != TIMER_INFINITY && nexti < next)
1306 1277 next = nexti;
1307 1278 if (debug & D_TIMER) {
1308 1279 logmsg(LOG_DEBUG, "run_timeouts (tmp on %s): "
1309 1280 "%d -> %u ms\n", pi->pi_name, nexti, next);
1310 1281 }
1311 1282 }
1312 1283 }
1313 1284 /*
1314 1285 * Make sure the timer functions are run at least once
1315 1286 * an hour.
1316 1287 */
1317 1288 if (next == TIMER_INFINITY)
1318 1289 next = 3600 * 1000; /* 1 hour */
1319 1290
1320 1291 if (debug & D_TIMER)
1321 1292 logmsg(LOG_DEBUG, "run_timeouts: %u ms\n", next);
1322 1293 timer_schedule(next);
1323 1294 if (do_retry) {
1324 1295 if (debug & D_TIMER)
1325 1296 logmsg(LOG_DEBUG, "run_timeouts: retry\n");
1326 1297 do_retry = _B_FALSE;
1327 1298 goto retry;
1328 1299 }
1329 1300 timeout_running = _B_FALSE;
1330 1301 }
1331 1302
1332 1303 static int eventpipe_read = -1; /* Used for synchronous signal delivery */
1333 1304 static int eventpipe_write = -1;
1334 1305
1335 1306 /*
1336 1307 * Ensure that signals are processed synchronously with the rest of
1337 1308 * the code by just writing a one character signal number on the pipe.
1338 1309 * The poll loop will pick this up and process the signal event.
1339 1310 */
1340 1311 static void
1341 1312 sig_handler(int signo)
1342 1313 {
1343 1314 uchar_t buf = (uchar_t)signo;
1344 1315
1345 1316 if (eventpipe_write == -1) {
1346 1317 logmsg(LOG_ERR, "sig_handler: no pipe\n");
1347 1318 return;
1348 1319 }
1349 1320 if (write(eventpipe_write, &buf, sizeof (buf)) < 0)
1350 1321 logperror("sig_handler: write");
1351 1322 }
1352 1323
1353 1324 /*
1354 1325 * Pick up a signal "byte" from the pipe and process it.
1355 1326 */
1356 1327 static void
1357 1328 in_signal(int fd)
1358 1329 {
1359 1330 uchar_t buf;
1360 1331 struct phyint *pi;
1361 1332 struct phyint *next_pi;
1362 1333
1363 1334 switch (read(fd, &buf, sizeof (buf))) {
1364 1335 case -1:
1365 1336 logperror("in_signal: read");
1366 1337 exit(1);
1367 1338 /* NOTREACHED */
1368 1339 case 1:
1369 1340 break;
1370 1341 case 0:
1371 1342 logmsg(LOG_ERR, "in_signal: read eof\n");
1372 1343 exit(1);
1373 1344 /* NOTREACHED */
1374 1345 default:
1375 1346 logmsg(LOG_ERR, "in_signal: read > 1\n");
1376 1347 exit(1);
1377 1348 }
1378 1349
1379 1350 if (debug & D_TIMER)
1380 1351 logmsg(LOG_DEBUG, "in_signal() got %d\n", buf);
1381 1352
1382 1353 switch (buf) {
1383 1354 case SIGALRM:
1384 1355 if (debug & D_TIMER) {
1385 1356 uint_t now = getcurrenttime();
1386 1357
1387 1358 logmsg(LOG_DEBUG, "in_signal(SIGALRM) delta %u\n",
1388 1359 now - timer_next);
1389 1360 }
1390 1361 timer_next = TIMER_INFINITY;
1391 1362 run_timeouts();
1392 1363 break;
1393 1364 case SIGHUP:
1394 1365 /* Re-read config file by exec'ing ourselves */
1395 1366 for (pi = phyints; pi != NULL; pi = next_pi) {
1396 1367 next_pi = pi->pi_next;
1397 1368 if (pi->pi_AdvSendAdvertisements)
1398 1369 check_to_advertise(pi, START_FINAL_ADV);
1399 1370
1400 1371 /*
1401 1372 * Remove all the configured addresses.
1402 1373 * Remove the addrobj names created with ipmgmtd.
1403 1374 * Release the dhcpv6 addresses if any.
1404 1375 * Cleanup the phyints.
1405 1376 */
1406 1377 phyint_delete(pi);
1407 1378 }
↓ open down ↓ |
437 lines elided |
↑ open up ↑ |
1408 1379
1409 1380 /*
1410 1381 * Prevent fd leaks. Everything gets re-opened at start-up
1411 1382 * time. 0, 1, and 2 are closed and re-opened as
1412 1383 * /dev/null, so we'll leave those open.
1413 1384 */
1414 1385 closefrom(3);
1415 1386
1416 1387 logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
1417 1388 (void) execv(argv0[0], argv0);
1418 - (void) unlink(PATH_PID);
1419 1389 _exit(0177);
1420 1390 /* NOTREACHED */
1421 1391 case SIGUSR1:
1422 1392 logmsg(LOG_DEBUG, "Printing configuration:\n");
1423 1393 phyint_print_all();
1424 1394 break;
1425 1395 case SIGINT:
1426 1396 case SIGTERM:
1427 1397 case SIGQUIT:
1428 1398 for (pi = phyints; pi != NULL; pi = next_pi) {
1429 1399 next_pi = pi->pi_next;
1430 1400 if (pi->pi_AdvSendAdvertisements)
1431 1401 check_to_advertise(pi, START_FINAL_ADV);
1432 1402
1433 1403 phyint_delete(pi);
1434 1404 }
1435 1405 (void) unlink(NDPD_SNMP_SOCKET);
1436 - (void) unlink(PATH_PID);
1437 1406 exit(0);
1438 1407 /* NOTREACHED */
1439 1408 case 255:
1440 1409 /*
1441 1410 * Special "signal" from loopback_ra_enqueue.
1442 1411 * Handle any queued loopback router advertisements.
1443 1412 */
1444 1413 loopback_ra_dequeue();
1445 1414 break;
1446 1415 default:
1447 1416 logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
1448 1417 }
1449 1418 }
1450 1419
1451 1420 /*
1452 1421 * Create pipe for signal delivery and set up signal handlers.
1453 1422 */
1454 1423 static void
1455 1424 setup_eventpipe(void)
1456 1425 {
1457 1426 int fds[2];
1458 1427 struct sigaction act;
1459 1428
1460 1429 if ((pipe(fds)) < 0) {
1461 1430 logperror("setup_eventpipe: pipe");
1462 1431 exit(1);
1463 1432 }
1464 1433 eventpipe_read = fds[0];
1465 1434 eventpipe_write = fds[1];
1466 1435 if (poll_add(eventpipe_read) == -1) {
1467 1436 exit(1);
1468 1437 }
1469 1438 act.sa_handler = sig_handler;
1470 1439 act.sa_flags = SA_RESTART;
1471 1440 (void) sigaction(SIGALRM, &act, NULL);
1472 1441
1473 1442 (void) sigset(SIGHUP, sig_handler);
1474 1443 (void) sigset(SIGUSR1, sig_handler);
1475 1444 (void) sigset(SIGTERM, sig_handler);
1476 1445 (void) sigset(SIGINT, sig_handler);
1477 1446 (void) sigset(SIGQUIT, sig_handler);
1478 1447 }
1479 1448
1480 1449 /*
1481 1450 * Create a routing socket for receiving RTM_IFINFO messages and initialize
1482 1451 * the routing socket message header and as much of the sockaddrs as possible.
1483 1452 */
1484 1453 static int
1485 1454 setup_rtsock(void)
1486 1455 {
1487 1456 int s;
1488 1457 int ret;
1489 1458 char *cp;
1490 1459 struct sockaddr_in6 *sin6;
1491 1460
1492 1461 s = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
1493 1462 if (s == -1) {
1494 1463 logperror("socket(PF_ROUTE)");
1495 1464 exit(1);
1496 1465 }
1497 1466 ret = fcntl(s, F_SETFL, O_NDELAY|O_NONBLOCK);
1498 1467 if (ret < 0) {
1499 1468 logperror("fcntl(O_NDELAY)");
1500 1469 exit(1);
1501 1470 }
1502 1471 if (poll_add(s) == -1) {
1503 1472 exit(1);
1504 1473 }
1505 1474
1506 1475 /*
1507 1476 * Allocate storage for the routing socket message.
1508 1477 */
1509 1478 rt_msg = (struct rt_msghdr *)malloc(NDP_RTM_MSGLEN);
1510 1479 if (rt_msg == NULL) {
1511 1480 logperror("malloc");
1512 1481 exit(1);
1513 1482 }
1514 1483
1515 1484 /*
1516 1485 * Initialize the routing socket message by zero-filling it and then
1517 1486 * setting the fields where are constant through the lifetime of the
1518 1487 * process.
1519 1488 */
1520 1489 bzero(rt_msg, NDP_RTM_MSGLEN);
1521 1490 rt_msg->rtm_msglen = NDP_RTM_MSGLEN;
1522 1491 rt_msg->rtm_version = RTM_VERSION;
1523 1492 rt_msg->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_IFP;
1524 1493 rt_msg->rtm_pid = getpid();
1525 1494 if (rt_msg->rtm_pid < 0) {
1526 1495 logperror("getpid");
1527 1496 exit(1);
1528 1497 }
1529 1498
1530 1499 /*
1531 1500 * The RTA_DST sockaddr does not change during the lifetime of the
1532 1501 * process so it can be completely initialized at this time.
1533 1502 */
1534 1503 cp = (char *)rt_msg + sizeof (struct rt_msghdr);
1535 1504 sin6 = (struct sockaddr_in6 *)cp;
1536 1505 sin6->sin6_family = AF_INET6;
1537 1506 sin6->sin6_addr = in6addr_any;
1538 1507
1539 1508 /*
1540 1509 * Initialize the constant portion of the RTA_GATEWAY sockaddr.
1541 1510 */
1542 1511 cp += sizeof (struct sockaddr_in6);
1543 1512 rta_gateway = (struct sockaddr_in6 *)cp;
1544 1513 rta_gateway->sin6_family = AF_INET6;
1545 1514
1546 1515 /*
1547 1516 * The RTA_NETMASK sockaddr does not change during the lifetime of the
1548 1517 * process so it can be completely initialized at this time.
1549 1518 */
1550 1519 cp += sizeof (struct sockaddr_in6);
1551 1520 sin6 = (struct sockaddr_in6 *)cp;
1552 1521 sin6->sin6_family = AF_INET6;
1553 1522 sin6->sin6_addr = in6addr_any;
1554 1523
1555 1524 /*
1556 1525 * Initialize the constant portion of the RTA_IFP sockaddr.
1557 1526 */
1558 1527 cp += sizeof (struct sockaddr_in6);
1559 1528 rta_ifp = (struct sockaddr_dl *)cp;
1560 1529 rta_ifp->sdl_family = AF_LINK;
1561 1530
1562 1531 return (s);
1563 1532 }
1564 1533
1565 1534 static int
1566 1535 setup_mibsock(void)
1567 1536 {
1568 1537 int sock;
1569 1538 int ret;
1570 1539 int len;
1571 1540 struct sockaddr_un laddr;
1572 1541
1573 1542 sock = socket(AF_UNIX, SOCK_DGRAM, 0);
1574 1543 if (sock == -1) {
1575 1544 logperror("setup_mibsock: socket(AF_UNIX)");
1576 1545 exit(1);
1577 1546 }
1578 1547
1579 1548 bzero(&laddr, sizeof (laddr));
1580 1549 laddr.sun_family = AF_UNIX;
1581 1550
1582 1551 (void) strncpy(laddr.sun_path, NDPD_SNMP_SOCKET,
1583 1552 sizeof (laddr.sun_path));
1584 1553 len = sizeof (struct sockaddr_un);
1585 1554
1586 1555 (void) unlink(NDPD_SNMP_SOCKET);
1587 1556 ret = bind(sock, (struct sockaddr *)&laddr, len);
1588 1557 if (ret < 0) {
1589 1558 logperror("setup_mibsock: bind\n");
1590 1559 exit(1);
1591 1560 }
1592 1561
1593 1562 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1594 1563 if (ret < 0) {
1595 1564 logperror("fcntl(O_NONBLOCK)");
1596 1565 exit(1);
1597 1566 }
1598 1567 if (poll_add(sock) == -1) {
1599 1568 exit(1);
1600 1569 }
1601 1570 return (sock);
1602 1571 }
1603 1572
1604 1573 /*
1605 1574 * Retrieve one routing socket message. If RTM_IFINFO indicates
1606 1575 * new phyint do a full scan of the interfaces. If RTM_IFINFO
1607 1576 * indicates an existing phyint, only scan that phyint and associated
1608 1577 * prefixes.
1609 1578 */
1610 1579 static void
1611 1580 process_rtsock(int rtsock)
1612 1581 {
1613 1582 int n;
1614 1583 #define MSG_SIZE 2048/8
1615 1584 int64_t msg[MSG_SIZE];
1616 1585 struct rt_msghdr *rtm;
1617 1586 struct if_msghdr *ifm;
1618 1587 struct phyint *pi;
1619 1588 struct prefix *pr;
1620 1589 boolean_t need_initifs = _B_FALSE;
1621 1590 boolean_t need_ifscan = _B_FALSE;
1622 1591 int64_t ifscan_msg[10][MSG_SIZE];
1623 1592 int ifscan_index = 0;
1624 1593 int i;
1625 1594
1626 1595 /* Empty the rtsock and coealesce all the work that we have */
1627 1596 while (ifscan_index < 10) {
1628 1597 n = read(rtsock, msg, sizeof (msg));
1629 1598 if (n <= 0) {
1630 1599 /* No more messages */
1631 1600 break;
1632 1601 }
1633 1602 rtm = (struct rt_msghdr *)msg;
1634 1603 if (rtm->rtm_version != RTM_VERSION) {
1635 1604 logmsg(LOG_ERR,
1636 1605 "process_rtsock: version %d not understood\n",
1637 1606 rtm->rtm_version);
1638 1607 return;
1639 1608 }
1640 1609 switch (rtm->rtm_type) {
1641 1610 case RTM_NEWADDR:
1642 1611 case RTM_DELADDR:
1643 1612 /*
1644 1613 * Some logical interface has changed - have to scan
1645 1614 * everything to determine what actually changed.
1646 1615 */
1647 1616 if (debug & D_IFSCAN) {
1648 1617 logmsg(LOG_DEBUG, "process_rtsock: "
1649 1618 "message %d\n", rtm->rtm_type);
1650 1619 }
1651 1620 need_initifs = _B_TRUE;
1652 1621 break;
1653 1622 case RTM_IFINFO:
1654 1623 need_ifscan = _B_TRUE;
1655 1624 (void) memcpy(ifscan_msg[ifscan_index], rtm,
1656 1625 sizeof (msg));
1657 1626 ifscan_index++;
1658 1627 /* Handled below */
1659 1628 break;
1660 1629 default:
1661 1630 /* Not interesting */
1662 1631 break;
1663 1632 }
1664 1633 }
1665 1634 /*
1666 1635 * If we do full scan i.e initifs, we don't need to
1667 1636 * scan a particular interface as we should have
1668 1637 * done that as part of initifs.
1669 1638 */
1670 1639 if (need_initifs) {
1671 1640 initifs(_B_FALSE);
1672 1641 return;
1673 1642 }
1674 1643
1675 1644 if (!need_ifscan)
1676 1645 return;
1677 1646
1678 1647 for (i = 0; i < ifscan_index; i++) {
1679 1648 ifm = (struct if_msghdr *)ifscan_msg[i];
1680 1649 if (debug & D_IFSCAN)
1681 1650 logmsg(LOG_DEBUG, "process_rtsock: index %d\n",
1682 1651 ifm->ifm_index);
1683 1652
1684 1653 pi = phyint_lookup_on_index(ifm->ifm_index);
1685 1654 if (pi == NULL) {
1686 1655 /*
1687 1656 * A new physical interface. Do a full scan of the
1688 1657 * to catch any new logical interfaces.
1689 1658 */
1690 1659 initifs(_B_FALSE);
1691 1660 return;
1692 1661 }
1693 1662
1694 1663 if (ifm->ifm_flags != (uint_t)pi->pi_flags) {
1695 1664 if (debug & D_IFSCAN) {
1696 1665 logmsg(LOG_DEBUG, "process_rtsock: clr for "
1697 1666 "%s old flags 0x%llx new flags 0x%x\n",
1698 1667 pi->pi_name, pi->pi_flags, ifm->ifm_flags);
1699 1668 }
1700 1669 }
1701 1670
1702 1671
1703 1672 /*
1704 1673 * Mark the interfaces so that we can find phyints and prefixes
1705 1674 * which have disappeared from the kernel.
1706 1675 * if_process will set pr_in_use when it finds the
1707 1676 * interface in the kernel.
1708 1677 * Before re-examining the state of the interfaces,
1709 1678 * PI_PRESENT should be cleared from pi_kernel_state.
1710 1679 */
1711 1680 pi->pi_kernel_state &= ~PI_PRESENT;
1712 1681 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
1713 1682 pr->pr_in_use = _B_FALSE;
1714 1683 }
1715 1684
1716 1685 if (ifsock < 0) {
1717 1686 ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
1718 1687 if (ifsock < 0) {
1719 1688 logperror("process_rtsock: socket");
1720 1689 return;
1721 1690 }
1722 1691 }
1723 1692 if_process(ifsock, pi->pi_name, _B_FALSE);
1724 1693 for (pr = pi->pi_prefix_list; pr != NULL; pr = pr->pr_next) {
1725 1694 if_process(ifsock, pr->pr_name, _B_FALSE);
1726 1695 }
1727 1696 /*
1728 1697 * If interface (still) exists in kernel, set
1729 1698 * pi_state to indicate that.
1730 1699 */
1731 1700 if (pi->pi_kernel_state & PI_PRESENT) {
1732 1701 pi->pi_state |= PI_PRESENT;
1733 1702 }
1734 1703 check_if_removed(pi);
1735 1704 if (show_ifs)
1736 1705 phyint_print_all();
1737 1706 }
1738 1707 }
1739 1708
1740 1709 static void
1741 1710 process_mibsock(int mibsock)
1742 1711 {
1743 1712 struct phyint *pi;
1744 1713 socklen_t fromlen;
1745 1714 struct sockaddr_un from;
1746 1715 ndpd_info_t ndpd_info;
1747 1716 ssize_t len;
1748 1717 int command;
1749 1718
1750 1719 fromlen = (socklen_t)sizeof (from);
1751 1720 len = recvfrom(mibsock, &command, sizeof (int), 0,
1752 1721 (struct sockaddr *)&from, &fromlen);
1753 1722
1754 1723 if (len < sizeof (int) || command != NDPD_SNMP_INFO_REQ) {
1755 1724 logperror("process_mibsock: bad command \n");
1756 1725 return;
1757 1726 }
1758 1727
1759 1728 ndpd_info.info_type = NDPD_SNMP_INFO_RESPONSE;
1760 1729 ndpd_info.info_version = NDPD_SNMP_INFO_VER;
1761 1730 ndpd_info.info_num_of_phyints = num_of_phyints;
1762 1731
1763 1732 (void) sendto(mibsock, &ndpd_info, sizeof (ndpd_info_t), 0,
1764 1733 (struct sockaddr *)&from, fromlen);
1765 1734
1766 1735 for (pi = phyints; pi != NULL; pi = pi->pi_next) {
1767 1736 int prefixes;
1768 1737 int routers;
1769 1738 struct prefix *prefix_list;
1770 1739 struct router *router_list;
1771 1740 ndpd_phyint_info_t phyint;
1772 1741 ndpd_prefix_info_t prefix;
1773 1742 ndpd_router_info_t router;
1774 1743 /*
1775 1744 * get number of prefixes
1776 1745 */
1777 1746 routers = 0;
1778 1747 prefixes = 0;
1779 1748 prefix_list = pi->pi_prefix_list;
1780 1749 while (prefix_list != NULL) {
1781 1750 prefixes++;
1782 1751 prefix_list = prefix_list->pr_next;
1783 1752 }
1784 1753
1785 1754 /*
1786 1755 * get number of routers
1787 1756 */
1788 1757 router_list = pi->pi_router_list;
1789 1758 while (router_list != NULL) {
1790 1759 routers++;
1791 1760 router_list = router_list->dr_next;
1792 1761 }
1793 1762
1794 1763 phyint.phyint_info_type = NDPD_PHYINT_INFO;
1795 1764 phyint.phyint_info_version = NDPD_PHYINT_INFO_VER;
1796 1765 phyint.phyint_index = pi->pi_index;
1797 1766 bcopy(pi->pi_config,
1798 1767 phyint.phyint_config, I_IFSIZE);
1799 1768 phyint.phyint_num_of_prefixes = prefixes;
1800 1769 phyint.phyint_num_of_routers = routers;
1801 1770 (void) sendto(mibsock, &phyint, sizeof (phyint), 0,
1802 1771 (struct sockaddr *)&from, fromlen);
1803 1772
1804 1773 /*
1805 1774 * Copy prefix information
1806 1775 */
1807 1776
1808 1777 prefix_list = pi->pi_prefix_list;
1809 1778 while (prefix_list != NULL) {
1810 1779 prefix.prefix_info_type = NDPD_PREFIX_INFO;
1811 1780 prefix.prefix_info_version = NDPD_PREFIX_INFO_VER;
1812 1781 prefix.prefix_prefix = prefix_list->pr_prefix;
1813 1782 prefix.prefix_len = prefix_list->pr_prefix_len;
1814 1783 prefix.prefix_flags = prefix_list->pr_flags;
1815 1784 prefix.prefix_phyint_index = pi->pi_index;
1816 1785 prefix.prefix_ValidLifetime =
1817 1786 prefix_list->pr_ValidLifetime;
1818 1787 prefix.prefix_PreferredLifetime =
1819 1788 prefix_list->pr_PreferredLifetime;
1820 1789 prefix.prefix_OnLinkLifetime =
1821 1790 prefix_list->pr_OnLinkLifetime;
1822 1791 prefix.prefix_OnLinkFlag =
1823 1792 prefix_list->pr_OnLinkFlag;
1824 1793 prefix.prefix_AutonomousFlag =
1825 1794 prefix_list->pr_AutonomousFlag;
1826 1795 (void) sendto(mibsock, &prefix, sizeof (prefix), 0,
1827 1796 (struct sockaddr *)&from, fromlen);
1828 1797 prefix_list = prefix_list->pr_next;
1829 1798 }
1830 1799 /*
1831 1800 * Copy router information
1832 1801 */
1833 1802 router_list = pi->pi_router_list;
1834 1803 while (router_list != NULL) {
1835 1804 router.router_info_type = NDPD_ROUTER_INFO;
1836 1805 router.router_info_version = NDPD_ROUTER_INFO_VER;
1837 1806 router.router_address = router_list->dr_address;
1838 1807 router.router_lifetime = router_list->dr_lifetime;
1839 1808 router.router_phyint_index = pi->pi_index;
1840 1809 (void) sendto(mibsock, &router, sizeof (router), 0,
1841 1810 (struct sockaddr *)&from, fromlen);
1842 1811 router_list = router_list->dr_next;
1843 1812 }
1844 1813 }
1845 1814 }
1846 1815
1847 1816 /*
1848 1817 * Look if the phyint or one of its prefixes have been removed from
1849 1818 * the kernel and take appropriate action.
1850 1819 * Uses pr_in_use and pi{,_kernel}_state.
1851 1820 */
1852 1821 static void
1853 1822 check_if_removed(struct phyint *pi)
1854 1823 {
1855 1824 struct prefix *pr, *next_pr;
1856 1825
1857 1826 /*
1858 1827 * Detect prefixes which are removed.
1859 1828 * Static prefixes are just removed from our tables.
1860 1829 * Non-static prefixes are recreated i.e. in.ndpd takes precedence
1861 1830 * over manually removing prefixes via ifconfig.
1862 1831 */
1863 1832 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1864 1833 next_pr = pr->pr_next;
1865 1834 if (!pr->pr_in_use) {
1866 1835 /* Clear everything except PR_STATIC */
1867 1836 pr->pr_kernel_state &= PR_STATIC;
1868 1837 if (pr->pr_state & PR_STATIC)
1869 1838 prefix_update_ipadm_addrobj(pr, _B_FALSE);
1870 1839 pr->pr_name[0] = '\0';
1871 1840 if (pr->pr_state & PR_STATIC) {
1872 1841 prefix_delete(pr);
1873 1842 } else if (!(pi->pi_kernel_state & PI_PRESENT)) {
1874 1843 /*
1875 1844 * Ensure that there are no future attempts to
1876 1845 * run prefix_update_k since the phyint is gone.
1877 1846 */
1878 1847 pr->pr_state = pr->pr_kernel_state;
1879 1848 } else if (pr->pr_state != pr->pr_kernel_state) {
1880 1849 logmsg(LOG_INFO, "Prefix manually removed "
1881 1850 "on %s; recreating\n", pi->pi_name);
1882 1851 prefix_update_k(pr);
1883 1852 }
1884 1853 }
1885 1854 }
1886 1855
1887 1856 /*
1888 1857 * Detect phyints that have been removed from the kernel, and tear
1889 1858 * down any prefixes we created that are associated with that phyint.
1890 1859 * (NOTE: IPMP depends on in.ndpd tearing down these prefixes so an
1891 1860 * administrator can easily place an IP interface with ADDRCONF'd
1892 1861 * addresses into an IPMP group.)
1893 1862 */
1894 1863 if (!(pi->pi_kernel_state & PI_PRESENT) &&
1895 1864 (pi->pi_state & PI_PRESENT)) {
1896 1865 logmsg(LOG_ERR, "Interface %s has been removed from kernel. "
1897 1866 "in.ndpd will no longer use it\n", pi->pi_name);
1898 1867
1899 1868 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
1900 1869 next_pr = pr->pr_next;
1901 1870 if (pr->pr_state & PR_AUTO)
1902 1871 prefix_update_ipadm_addrobj(pr, _B_FALSE);
1903 1872 prefix_delete(pr);
1904 1873 }
1905 1874
1906 1875 /*
1907 1876 * Clear state so that should the phyint reappear we will
1908 1877 * start with initial advertisements or solicitations.
1909 1878 */
1910 1879 phyint_cleanup(pi);
1911 1880 }
1912 1881 }
1913 1882
1914 1883
1915 1884 /*
1916 1885 * Queuing mechanism for router advertisements that are sent by in.ndpd
1917 1886 * and that also need to be processed by in.ndpd.
1918 1887 * Uses "signal number" 255 to indicate to the main poll loop
1919 1888 * that there is something to dequeue and send to incomining_ra().
1920 1889 */
1921 1890 struct raq {
1922 1891 struct raq *raq_next;
1923 1892 struct phyint *raq_pi;
1924 1893 int raq_packetlen;
1925 1894 uchar_t *raq_packet;
1926 1895 };
1927 1896 static struct raq *raq_head = NULL;
1928 1897
1929 1898 /*
1930 1899 * Allocate a struct raq and memory for the packet.
1931 1900 * Send signal 255 to have poll dequeue.
1932 1901 */
1933 1902 static void
1934 1903 loopback_ra_enqueue(struct phyint *pi, struct nd_router_advert *ra, int len)
1935 1904 {
1936 1905 struct raq *raq;
1937 1906 struct raq **raqp;
1938 1907
1939 1908 if (no_loopback)
1940 1909 return;
1941 1910
1942 1911 if (debug & D_PKTOUT)
1943 1912 logmsg(LOG_DEBUG, "loopback_ra_enqueue for %s\n", pi->pi_name);
1944 1913
1945 1914 raq = calloc(sizeof (struct raq), 1);
1946 1915 if (raq == NULL) {
1947 1916 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
1948 1917 return;
1949 1918 }
1950 1919 raq->raq_packet = malloc(len);
1951 1920 if (raq->raq_packet == NULL) {
1952 1921 free(raq);
1953 1922 logmsg(LOG_ERR, "loopback_ra_enqueue: out of memory\n");
1954 1923 return;
1955 1924 }
1956 1925 bcopy(ra, raq->raq_packet, len);
1957 1926 raq->raq_packetlen = len;
1958 1927 raq->raq_pi = pi;
1959 1928
1960 1929 /* Tail insert */
1961 1930 raqp = &raq_head;
1962 1931 while (*raqp != NULL)
1963 1932 raqp = &((*raqp)->raq_next);
1964 1933 *raqp = raq;
1965 1934
1966 1935 /* Signal for poll loop */
1967 1936 sig_handler(255);
1968 1937 }
1969 1938
1970 1939 /*
1971 1940 * Dequeue and process all queued advertisements.
1972 1941 */
1973 1942 static void
1974 1943 loopback_ra_dequeue(void)
1975 1944 {
1976 1945 struct sockaddr_in6 from = IN6ADDR_LOOPBACK_INIT;
1977 1946 struct raq *raq;
1978 1947
1979 1948 if (debug & D_PKTIN)
1980 1949 logmsg(LOG_DEBUG, "loopback_ra_dequeue()\n");
1981 1950
1982 1951 while ((raq = raq_head) != NULL) {
1983 1952 raq_head = raq->raq_next;
1984 1953 raq->raq_next = NULL;
1985 1954
1986 1955 if (debug & D_PKTIN) {
1987 1956 logmsg(LOG_DEBUG, "loopback_ra_dequeue for %s\n",
1988 1957 raq->raq_pi->pi_name);
1989 1958 }
1990 1959
1991 1960 incoming_ra(raq->raq_pi,
1992 1961 (struct nd_router_advert *)raq->raq_packet,
1993 1962 raq->raq_packetlen, &from, _B_TRUE);
1994 1963 free(raq->raq_packet);
1995 1964 free(raq);
1996 1965 }
1997 1966 }
1998 1967
1999 1968
2000 1969 static void
2001 1970 usage(char *cmd)
2002 1971 {
2003 1972 (void) fprintf(stderr,
2004 1973 "usage: %s [ -adt ] [-f <config file>]\n", cmd);
2005 1974 }
2006 1975
2007 1976 int
2008 1977 main(int argc, char *argv[])
2009 1978 {
2010 1979 int i;
2011 1980 struct phyint *pi;
2012 1981 int c;
2013 1982 char *config_file = PATH_NDPD_CONF;
2014 1983 boolean_t file_required = _B_FALSE;
2015 1984
2016 1985 argv0 = argv;
2017 1986 srandom(gethostid());
2018 1987 (void) umask(0022);
2019 1988
2020 1989 while ((c = getopt(argc, argv, "adD:ntIf:")) != EOF) {
2021 1990 switch (c) {
2022 1991 case 'a':
2023 1992 /*
2024 1993 * The StatelessAddrConf variable in ndpd.conf, if
2025 1994 * present, will override this setting.
2026 1995 */
2027 1996 ifdefaults[I_StatelessAddrConf].cf_value = 0;
2028 1997 break;
2029 1998 case 'd':
2030 1999 debug = D_ALL;
2031 2000 break;
2032 2001 case 'D':
2033 2002 i = strtol((char *)optarg, NULL, 0);
2034 2003 if (i == 0) {
2035 2004 (void) fprintf(stderr, "Bad debug flags: %s\n",
2036 2005 (char *)optarg);
2037 2006 exit(1);
2038 2007 }
2039 2008 debug |= i;
2040 2009 break;
2041 2010 case 'n':
2042 2011 no_loopback = 1;
2043 2012 break;
2044 2013 case 'I':
2045 2014 show_ifs = 1;
2046 2015 break;
2047 2016 case 't':
2048 2017 debug |= D_PKTIN | D_PKTOUT | D_PKTBAD;
2049 2018 break;
2050 2019 case 'f':
2051 2020 config_file = (char *)optarg;
2052 2021 file_required = _B_TRUE;
2053 2022 break;
2054 2023 case '?':
2055 2024 usage(argv[0]);
2056 2025 exit(1);
2057 2026 }
2058 2027 }
2059 2028
2060 2029 if (parse_config(config_file, file_required) == -1)
2061 2030 exit(2);
2062 2031
2063 2032 if (show_ifs)
2064 2033 phyint_print_all();
2065 2034
2066 2035 if (debug == 0)
2067 2036 initlog();
2068 2037
2069 2038 cmdsock = ndpd_setup_cmd_listener();
2070 2039 setup_eventpipe();
2071 2040 rtsock = setup_rtsock();
2072 2041 mibsock = setup_mibsock();
2073 2042 timer_init();
2074 2043 initifs(_B_TRUE);
2075 2044
2076 2045 check_daemonize();
2077 2046
2078 2047 for (;;) {
2079 2048 if (poll(pollfds, pollfd_num, -1) < 0) {
2080 2049 if (errno == EINTR)
2081 2050 continue;
2082 2051 logperror("main: poll");
2083 2052 exit(1);
2084 2053 }
2085 2054 for (i = 0; i < pollfd_num; i++) {
2086 2055 if (!(pollfds[i].revents & POLLIN))
2087 2056 continue;
2088 2057 if (pollfds[i].fd == eventpipe_read) {
2089 2058 in_signal(eventpipe_read);
2090 2059 break;
2091 2060 }
2092 2061 if (pollfds[i].fd == rtsock) {
2093 2062 process_rtsock(rtsock);
2094 2063 break;
2095 2064 }
2096 2065 if (pollfds[i].fd == mibsock) {
2097 2066 process_mibsock(mibsock);
2098 2067 break;
2099 2068 }
2100 2069 if (pollfds[i].fd == cmdsock) {
2101 2070 ndpd_cmd_handler(cmdsock);
2102 2071 break;
2103 2072 }
2104 2073 /*
2105 2074 * Run timer routine to advance clock if more than
2106 2075 * half a second since the clock was advanced.
2107 2076 * This limits CPU usage under severe packet
2108 2077 * arrival rates but it creates a slight inaccuracy
2109 2078 * in the timer mechanism.
2110 2079 */
2111 2080 conditional_run_timeouts(500U);
2112 2081 for (pi = phyints; pi != NULL; pi = pi->pi_next) {
2113 2082 if (pollfds[i].fd == pi->pi_sock) {
2114 2083 in_data(pi);
2115 2084 break;
2116 2085 }
2117 2086 }
2118 2087 }
2119 2088 }
2120 2089 /* NOTREACHED */
2121 2090 return (0);
2122 2091 }
2123 2092
2124 2093 /*
2125 2094 * LOGGER
2126 2095 */
2127 2096
2128 2097 static boolean_t logging = _B_FALSE;
2129 2098
2130 2099 static void
2131 2100 initlog(void)
2132 2101 {
2133 2102 logging = _B_TRUE;
2134 2103 openlog("in.ndpd", LOG_PID | LOG_CONS, LOG_DAEMON);
2135 2104 }
2136 2105
2137 2106 /* Print the date/time without a trailing carridge return */
2138 2107 static void
2139 2108 fprintdate(FILE *file)
2140 2109 {
2141 2110 char buf[BUFSIZ];
2142 2111 struct tm tms;
2143 2112 time_t now;
2144 2113
2145 2114 now = time(NULL);
2146 2115 (void) localtime_r(&now, &tms);
2147 2116 (void) strftime(buf, sizeof (buf), "%h %d %X", &tms);
2148 2117 (void) fprintf(file, "%s ", buf);
2149 2118 }
2150 2119
2151 2120 /* PRINTFLIKE2 */
2152 2121 void
2153 2122 logmsg(int level, const char *fmt, ...)
2154 2123 {
2155 2124 va_list ap;
2156 2125 va_start(ap, fmt);
2157 2126
2158 2127 if (logging) {
2159 2128 vsyslog(level, fmt, ap);
2160 2129 } else {
2161 2130 fprintdate(stderr);
2162 2131 (void) vfprintf(stderr, fmt, ap);
2163 2132 }
2164 2133 va_end(ap);
2165 2134 }
2166 2135
2167 2136 void
2168 2137 logperror(const char *str)
2169 2138 {
2170 2139 if (logging) {
2171 2140 syslog(LOG_ERR, "%s: %m\n", str);
2172 2141 } else {
2173 2142 fprintdate(stderr);
2174 2143 (void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
2175 2144 }
2176 2145 }
2177 2146
2178 2147 void
2179 2148 logperror_pi(const struct phyint *pi, const char *str)
2180 2149 {
2181 2150 if (logging) {
2182 2151 syslog(LOG_ERR, "%s (interface %s): %m\n",
2183 2152 str, pi->pi_name);
2184 2153 } else {
2185 2154 fprintdate(stderr);
2186 2155 (void) fprintf(stderr, "%s (interface %s): %s\n",
2187 2156 str, pi->pi_name, strerror(errno));
2188 2157 }
2189 2158 }
2190 2159
2191 2160 void
2192 2161 logperror_pr(const struct prefix *pr, const char *str)
2193 2162 {
2194 2163 if (logging) {
2195 2164 syslog(LOG_ERR, "%s (prefix %s if %s): %m\n",
2196 2165 str, pr->pr_name, pr->pr_physical->pi_name);
2197 2166 } else {
2198 2167 fprintdate(stderr);
2199 2168 (void) fprintf(stderr, "%s (prefix %s if %s): %s\n",
2200 2169 str, pr->pr_name, pr->pr_physical->pi_name,
2201 2170 strerror(errno));
2202 2171 }
2203 2172 }
2204 2173
2205 2174 static int
2206 2175 ndpd_setup_cmd_listener(void)
2207 2176 {
2208 2177 int sock;
2209 2178 int ret;
2210 2179 struct sockaddr_un servaddr;
2211 2180
2212 2181 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2213 2182 if (sock < 0) {
2214 2183 logperror("socket");
2215 2184 exit(1);
2216 2185 }
2217 2186
2218 2187 bzero(&servaddr, sizeof (servaddr));
2219 2188 servaddr.sun_family = AF_UNIX;
2220 2189 (void) strlcpy(servaddr.sun_path, IPADM_UDS_PATH,
2221 2190 sizeof (servaddr.sun_path));
2222 2191 (void) unlink(servaddr.sun_path);
2223 2192 ret = bind(sock, (struct sockaddr *)&servaddr, sizeof (servaddr));
2224 2193 if (ret < 0) {
2225 2194 logperror("bind");
2226 2195 exit(1);
2227 2196 }
2228 2197 if (listen(sock, 30) < 0) {
2229 2198 logperror("listen");
2230 2199 exit(1);
2231 2200 }
2232 2201 if (poll_add(sock) == -1) {
2233 2202 logmsg(LOG_ERR, "command socket could not be added to the "
2234 2203 "polling set\n");
2235 2204 exit(1);
2236 2205 }
2237 2206
2238 2207 return (sock);
2239 2208 }
2240 2209
2241 2210 /*
2242 2211 * Commands received over the command socket come here
2243 2212 */
2244 2213 static void
2245 2214 ndpd_cmd_handler(int sock)
2246 2215 {
2247 2216 int newfd;
2248 2217 struct sockaddr_storage peer;
2249 2218 socklen_t peerlen;
2250 2219 ipadm_ndpd_msg_t ndpd_msg;
2251 2220 int retval;
2252 2221
2253 2222 peerlen = sizeof (peer);
2254 2223 newfd = accept(sock, (struct sockaddr *)&peer, &peerlen);
2255 2224 if (newfd < 0) {
2256 2225 logperror("accept");
2257 2226 return;
2258 2227 }
2259 2228
2260 2229 retval = ipadm_ndpd_read(newfd, &ndpd_msg, sizeof (ndpd_msg));
2261 2230 if (retval != 0)
2262 2231 logperror("Could not read ndpd command");
2263 2232
2264 2233 retval = ndpd_process_cmd(newfd, &ndpd_msg);
2265 2234 if (retval != 0) {
2266 2235 logmsg(LOG_ERR, "ndpd command on interface %s failed with "
2267 2236 "error %s\n", ndpd_msg.inm_ifname, strerror(retval));
2268 2237 }
2269 2238 (void) close(newfd);
2270 2239 }
2271 2240
2272 2241 /*
2273 2242 * Process the commands received from the cmd listener socket.
2274 2243 */
2275 2244 static int
2276 2245 ndpd_process_cmd(int newfd, ipadm_ndpd_msg_t *msg)
2277 2246 {
2278 2247 int err;
2279 2248
2280 2249 if (!ipadm_check_auth()) {
2281 2250 logmsg(LOG_ERR, "User not authorized to send the command\n");
2282 2251 (void) ndpd_send_error(newfd, EPERM);
2283 2252 return (EPERM);
2284 2253 }
2285 2254 switch (msg->inm_cmd) {
2286 2255 case IPADM_DISABLE_AUTOCONF:
2287 2256 err = ndpd_set_autoconf(msg->inm_ifname, _B_FALSE);
2288 2257 break;
2289 2258
2290 2259 case IPADM_ENABLE_AUTOCONF:
2291 2260 err = ndpd_set_autoconf(msg->inm_ifname, _B_TRUE);
2292 2261 break;
2293 2262
2294 2263 case IPADM_CREATE_ADDRS:
2295 2264 err = ndpd_create_addrs(msg->inm_ifname, msg->inm_intfid,
2296 2265 msg->inm_intfidlen, msg->inm_stateless,
2297 2266 msg->inm_stateful, msg->inm_aobjname);
2298 2267 break;
2299 2268
2300 2269 case IPADM_DELETE_ADDRS:
2301 2270 err = ndpd_delete_addrs(msg->inm_ifname);
2302 2271 break;
2303 2272
2304 2273 default:
2305 2274 err = EINVAL;
2306 2275 break;
2307 2276 }
2308 2277
2309 2278 (void) ndpd_send_error(newfd, err);
2310 2279
2311 2280 return (err);
2312 2281 }
2313 2282
2314 2283 static int
2315 2284 ndpd_send_error(int fd, int error)
2316 2285 {
2317 2286 return (ipadm_ndpd_write(fd, &error, sizeof (error)));
2318 2287 }
2319 2288
2320 2289 /*
2321 2290 * Disables/Enables autoconfiguration of addresses on the
2322 2291 * given physical interface.
2323 2292 * This is provided to support the legacy method of configuring IPv6
2324 2293 * addresses. i.e. `ifconfig bge0 inet6 plumb` will plumb the interface
2325 2294 * and start stateless and stateful autoconfiguration. If this function is
2326 2295 * not called with enable=_B_FALSE, no autoconfiguration will be done until
2327 2296 * ndpd_create_addrs() is called with an Interface ID.
2328 2297 */
2329 2298 static int
2330 2299 ndpd_set_autoconf(const char *ifname, boolean_t enable)
2331 2300 {
2332 2301 struct phyint *pi;
2333 2302
2334 2303 pi = phyint_lookup((char *)ifname);
2335 2304 if (pi == NULL) {
2336 2305 /*
2337 2306 * If the physical interface was plumbed but no
2338 2307 * addresses were configured yet, phyint will not exist.
2339 2308 */
2340 2309 pi = phyint_create((char *)ifname);
2341 2310 if (pi == NULL) {
2342 2311 logmsg(LOG_ERR, "could not create phyint for "
2343 2312 "interface %s", ifname);
2344 2313 return (ENOMEM);
2345 2314 }
2346 2315 }
2347 2316 pi->pi_autoconf = enable;
2348 2317
2349 2318 if (debug & D_PHYINT) {
2350 2319 logmsg(LOG_DEBUG, "ndpd_set_autoconf: %s autoconf for "
2351 2320 "interface %s\n", (enable ? "enabled" : "disabled"),
2352 2321 pi->pi_name);
2353 2322 }
2354 2323 return (0);
2355 2324 }
2356 2325
2357 2326 /*
2358 2327 * Create auto-configured addresses on the given interface using
2359 2328 * the given token as the interface id during the next Router Advertisement.
2360 2329 * Currently, only one token per interface is supported.
2361 2330 */
2362 2331 static int
2363 2332 ndpd_create_addrs(const char *ifname, struct sockaddr_in6 intfid, int intfidlen,
2364 2333 boolean_t stateless, boolean_t stateful, char *addrobj)
2365 2334 {
2366 2335 struct phyint *pi;
2367 2336 struct lifreq lifr;
2368 2337 struct sockaddr_in6 *sin6;
2369 2338 int err;
2370 2339
2371 2340 pi = phyint_lookup((char *)ifname);
2372 2341 if (pi == NULL) {
2373 2342 /*
2374 2343 * If the physical interface was plumbed but no
2375 2344 * addresses were configured yet, phyint will not exist.
2376 2345 */
2377 2346 pi = phyint_create((char *)ifname);
2378 2347 if (pi == NULL) {
2379 2348 if (debug & D_PHYINT)
2380 2349 logmsg(LOG_ERR, "could not create phyint "
2381 2350 "for interface %s", ifname);
2382 2351 return (ENOMEM);
2383 2352 }
2384 2353 } else if (pi->pi_autoconf) {
2385 2354 logmsg(LOG_ERR, "autoconfiguration already in progress\n");
2386 2355 return (EEXIST);
2387 2356 }
2388 2357 check_autoconf_var_consistency(pi, stateless, stateful);
2389 2358
2390 2359 if (intfidlen == 0) {
2391 2360 pi->pi_default_token = _B_TRUE;
2392 2361 if (ifsock < 0) {
2393 2362 ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
2394 2363 if (ifsock < 0) {
2395 2364 err = errno;
2396 2365 logperror("ndpd_create_addrs: socket");
2397 2366 return (err);
2398 2367 }
2399 2368 }
2400 2369 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2401 2370 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
2402 2371 if (ioctl(ifsock, SIOCGLIFTOKEN, (char *)&lifr) < 0) {
2403 2372 err = errno;
2404 2373 logperror("SIOCGLIFTOKEN");
2405 2374 return (err);
2406 2375 }
2407 2376 pi->pi_token = sin6->sin6_addr;
2408 2377 pi->pi_token_length = lifr.lifr_addrlen;
2409 2378 } else {
2410 2379 pi->pi_default_token = _B_FALSE;
2411 2380 pi->pi_token = intfid.sin6_addr;
2412 2381 pi->pi_token_length = intfidlen;
2413 2382 }
2414 2383 pi->pi_stateless = stateless;
2415 2384 pi->pi_stateful = stateful;
2416 2385 (void) strlcpy(pi->pi_ipadm_aobjname, addrobj,
2417 2386 sizeof (pi->pi_ipadm_aobjname));
2418 2387
2419 2388 /* We can allow autoconfiguration now. */
2420 2389 pi->pi_autoconf = _B_TRUE;
2421 2390
2422 2391 /* Restart the solicitations. */
2423 2392 if (pi->pi_sol_state == DONE_SOLICIT)
2424 2393 pi->pi_sol_state = NO_SOLICIT;
2425 2394 if (pi->pi_sol_state == NO_SOLICIT)
2426 2395 check_to_solicit(pi, START_INIT_SOLICIT);
2427 2396 if (debug & D_PHYINT)
2428 2397 logmsg(LOG_DEBUG, "ndpd_create_addrs: "
2429 2398 "added token to interface %s\n", pi->pi_name);
2430 2399 return (0);
2431 2400 }
2432 2401
2433 2402 /*
2434 2403 * This function deletes all addresses on the given interface
2435 2404 * with the given Interface ID.
2436 2405 */
2437 2406 static int
2438 2407 ndpd_delete_addrs(const char *ifname)
2439 2408 {
2440 2409 struct phyint *pi;
2441 2410 struct prefix *pr, *next_pr;
2442 2411 struct lifreq lifr;
2443 2412 int err;
2444 2413
2445 2414 pi = phyint_lookup((char *)ifname);
2446 2415 if (pi == NULL) {
2447 2416 logmsg(LOG_ERR, "no phyint found for %s", ifname);
2448 2417 return (ENXIO);
2449 2418 }
2450 2419 if (IN6_IS_ADDR_UNSPECIFIED(&pi->pi_token)) {
2451 2420 logmsg(LOG_ERR, "token does not exist for %s", ifname);
2452 2421 return (EINVAL);
2453 2422 }
2454 2423
2455 2424 if (ifsock < 0) {
2456 2425 ifsock = socket(AF_INET6, SOCK_DGRAM, 0);
2457 2426 if (ifsock < 0) {
2458 2427 err = errno;
2459 2428 logperror("ndpd_create_addrs: socket");
2460 2429 return (err);
2461 2430 }
2462 2431 }
2463 2432 /* Remove the prefixes for this phyint if they exist */
2464 2433 for (pr = pi->pi_prefix_list; pr != NULL; pr = next_pr) {
2465 2434 next_pr = pr->pr_next;
2466 2435 if (pr->pr_name[0] == '\0') {
2467 2436 prefix_delete(pr);
2468 2437 continue;
2469 2438 }
2470 2439 /*
2471 2440 * Delete all the prefixes for the auto-configured
2472 2441 * addresses as well as the DHCPv6 addresses.
2473 2442 */
2474 2443 (void) strncpy(lifr.lifr_name, pr->pr_name,
2475 2444 sizeof (lifr.lifr_name));
2476 2445 if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
2477 2446 err = errno;
2478 2447 logperror("SIOCGLIFFLAGS");
2479 2448 return (err);
2480 2449 }
2481 2450 if ((lifr.lifr_flags & IFF_ADDRCONF) ||
2482 2451 (lifr.lifr_flags & IFF_DHCPRUNNING)) {
2483 2452 prefix_update_ipadm_addrobj(pr, _B_FALSE);
2484 2453 }
2485 2454 prefix_delete(pr);
2486 2455 }
2487 2456
2488 2457 /*
2489 2458 * If we had started dhcpagent, we need to release the leases
2490 2459 * if any are required.
2491 2460 */
2492 2461 if (pi->pi_stateful) {
2493 2462 (void) strncpy(lifr.lifr_name, pi->pi_name,
2494 2463 sizeof (lifr.lifr_name));
2495 2464 if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
2496 2465 err = errno;
2497 2466 logperror("SIOCGLIFFLAGS");
2498 2467 return (err);
2499 2468 }
2500 2469 if (lifr.lifr_flags & IFF_DHCPRUNNING)
2501 2470 release_dhcp(pi);
2502 2471 }
2503 2472
2504 2473 /*
2505 2474 * Reset the Interface ID on this phyint and stop autoconfigurations
2506 2475 * until a new interface ID is provided.
2507 2476 */
2508 2477 pi->pi_token = in6addr_any;
2509 2478 pi->pi_token_length = 0;
2510 2479 pi->pi_autoconf = _B_FALSE;
2511 2480 pi->pi_ipadm_aobjname[0] = '\0';
2512 2481
2513 2482 /* Reset the stateless and stateful settings to default. */
2514 2483 pi->pi_stateless = pi->pi_StatelessAddrConf;
2515 2484 pi->pi_stateful = pi->pi_StatefulAddrConf;
2516 2485
2517 2486 if (debug & D_PHYINT) {
2518 2487 logmsg(LOG_DEBUG, "ndpd_delete_addrs: "
2519 2488 "removed token from interface %s\n", pi->pi_name);
2520 2489 }
2521 2490 return (0);
2522 2491 }
2523 2492
2524 2493 void
2525 2494 check_autoconf_var_consistency(struct phyint *pi, boolean_t stateless,
2526 2495 boolean_t stateful)
2527 2496 {
2528 2497 /*
2529 2498 * If StatelessAddrConf and StatelessAddrConf are set in
2530 2499 * /etc/inet/ndpd.conf, check if the new values override those
2531 2500 * settings. If so, log a warning.
2532 2501 */
2533 2502 if ((pi->pi_StatelessAddrConf !=
2534 2503 ifdefaults[I_StatelessAddrConf].cf_value &&
2535 2504 stateless != pi->pi_StatelessAddrConf) ||
2536 2505 (pi->pi_StatefulAddrConf !=
2537 2506 ifdefaults[I_StatefulAddrConf].cf_value &&
2538 2507 stateful != pi->pi_StatefulAddrConf)) {
2539 2508 logmsg(LOG_ERR, "check_autoconf_var_consistency: "
2540 2509 "Overriding the StatelessAddrConf or StatefulAddrConf "
2541 2510 "settings in ndpd.conf with the new values for "
2542 2511 "interface %s\n", pi->pi_name);
2543 2512 }
2544 2513 }
2545 2514
2546 2515 /*
2547 2516 * If ipadm was used to start autoconfiguration and in.ndpd was restarted
2548 2517 * for some reason, in.ndpd has to resume autoconfiguration when it comes up.
2549 2518 * In this function, it scans the ipadm_addr_info() output to find a link-local
2550 2519 * on this interface with address type "addrconf" and extracts the interface id.
2551 2520 * It also stores the addrobj name to be used later when new addresses are
2552 2521 * created for the prefixes advertised by the router.
2553 2522 * If autoconfiguration was never started on this interface before in.ndpd
2554 2523 * was killed, then in.ndpd should refrain from configuring prefixes, even if
2555 2524 * there is a valid link-local on this interface, created by ipadm (identified
2556 2525 * if there is a valid addrobj name).
2557 2526 */
2558 2527 static int
2559 2528 phyint_check_ipadm_intfid(struct phyint *pi)
2560 2529 {
2561 2530 ipadm_status_t status;
2562 2531 ipadm_addr_info_t *addrinfo;
2563 2532 struct ifaddrs *ifap;
2564 2533 ipadm_addr_info_t *ainfop;
2565 2534 struct sockaddr_in6 *sin6;
2566 2535 ipadm_handle_t iph;
2567 2536
2568 2537 if (ipadm_open(&iph, 0) != IPADM_SUCCESS) {
2569 2538 logmsg(LOG_ERR, "could not open handle to libipadm\n");
2570 2539 return (-1);
2571 2540 }
2572 2541
2573 2542 status = ipadm_addr_info(iph, pi->pi_name, &addrinfo,
2574 2543 IPADM_OPT_ZEROADDR, LIFC_NOXMIT|LIFC_TEMPORARY);
2575 2544 if (status != IPADM_SUCCESS) {
2576 2545 ipadm_close(iph);
2577 2546 return (-1);
2578 2547 }
2579 2548 pi->pi_autoconf = _B_TRUE;
2580 2549 for (ainfop = addrinfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
2581 2550 ifap = &ainfop->ia_ifa;
2582 2551 if (ifap->ifa_addr->sa_family != AF_INET6 ||
2583 2552 ainfop->ia_state == IFA_DISABLED)
2584 2553 continue;
2585 2554 sin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
2586 2555 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2587 2556 if (ainfop->ia_atype == IPADM_ADDR_IPV6_ADDRCONF) {
2588 2557 pi->pi_token = sin6->sin6_addr;
2589 2558 pi->pi_token._S6_un._S6_u32[0] = 0;
2590 2559 pi->pi_token._S6_un._S6_u32[1] = 0;
2591 2560 pi->pi_autoconf = _B_TRUE;
2592 2561 (void) strlcpy(pi->pi_ipadm_aobjname,
2593 2562 ainfop->ia_aobjname,
2594 2563 sizeof (pi->pi_ipadm_aobjname));
2595 2564 break;
2596 2565 }
2597 2566 /*
2598 2567 * If IFF_NOLINKLOCAL is set, then the link-local
2599 2568 * was created using ipadm. Do not autoconfigure until
2600 2569 * ipadm is explicitly used for autoconfiguration.
2601 2570 */
2602 2571 if (ifap->ifa_flags & IFF_NOLINKLOCAL)
2603 2572 pi->pi_autoconf = _B_FALSE;
2604 2573 } else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
2605 2574 strrchr(ifap->ifa_name, ':') == NULL) {
2606 2575 /* The interface was created using ipadm. */
2607 2576 pi->pi_autoconf = _B_FALSE;
2608 2577 }
2609 2578 }
2610 2579 ipadm_free_addr_info(addrinfo);
2611 2580 if (!pi->pi_autoconf) {
2612 2581 pi->pi_token = in6addr_any;
2613 2582 pi->pi_token_length = 0;
2614 2583 }
2615 2584 ipadm_close(iph);
2616 2585 return (0);
2617 2586 }
↓ open down ↓ |
1171 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX