Print this page
Remove include userdefs.h where not needed
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c
+++ new/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c
1 1 /*
2 2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
4 4 *
5 5 * Copyright (c) 1983, 1988, 1993
6 6 * The Regents of the University of California. All rights reserved.
7 7 *
8 8 * Redistribution and use in source and binary forms, with or without
9 9 * modification, are permitted provided that the following conditions
10 10 * are met:
11 11 * 1. Redistributions of source code must retain the above copyright
12 12 * notice, this list of conditions and the following disclaimer.
13 13 * 2. Redistributions in binary form must reproduce the above copyright
14 14 * notice, this list of conditions and the following disclaimer in the
15 15 * documentation and/or other materials provided with the distribution.
16 16 * 3. All advertising materials mentioning features or use of this software
17 17 * must display the following acknowledgment:
18 18 * This product includes software developed by the University of
19 19 * California, Berkeley and its contributors.
20 20 * 4. Neither the name of the University nor the names of its contributors
21 21 * may be used to endorse or promote products derived from this software
22 22 * without specific prior written permission.
23 23 *
24 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 34 * SUCH DAMAGE.
35 35 *
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
36 36 * $FreeBSD: src/sbin/routed/main.c,v 1.14 2000/08/11 08:24:38 sheldonh Exp $
37 37 * char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n"
38 38 * " The Regents of the University of California. All rights reserved.\n";
39 39 */
40 40
41 41 #include "defs.h"
42 42 #include "pathnames.h"
43 43 #include <signal.h>
44 44 #include <fcntl.h>
45 45 #include <sys/file.h>
46 -#include <userdefs.h>
47 46 #include <sys/stat.h>
48 47
49 48 #define IN_ROUTED_VERSION "2.22"
50 49
51 50 int stopint;
52 51 boolean_t supplier; /* supply or broadcast updates */
53 52 boolean_t supplier_set;
54 53 /* -S option. _B_TRUE=treat all RIP speakers as default routers. */
55 54 boolean_t save_space = _B_FALSE;
56 55
57 56 static boolean_t default_gateway; /* _B_TRUE=advertise default */
58 57 static boolean_t background = _B_TRUE;
59 58 boolean_t ridhosts; /* _B_TRUE=reduce host routes */
60 59 boolean_t mhome; /* _B_TRUE=want multi-homed host route */
61 60 boolean_t advertise_mhome; /* _B_TRUE=must continue advertising it */
62 61 boolean_t auth_ok = _B_TRUE; /* _B_TRUE=ignore auth if we don't care */
63 62 boolean_t no_install; /* _B_TRUE=don't install in kernel */
64 63
65 64 struct timeval epoch; /* when started */
66 65 struct timeval clk;
67 66 static struct timeval prev_clk;
68 67 static int usec_fudge;
69 68 struct timeval now; /* current idea of time */
70 69 /* If a route's rts_time is <= to now_stale, the route is stale. */
71 70 time_t now_stale;
72 71 /* If a route's rts_time is <= to now_expire, the route is expired */
73 72 time_t now_expire;
74 73 /* If a route's rts_time is <= to now_garbage, the route needs to be deleted */
75 74 time_t now_garbage;
76 75
77 76 static struct timeval next_bcast; /* next general broadcast */
78 77 struct timeval no_flash = { /* inhibit flash update */
79 78 EPOCH+SUPPLY_INTERVAL, 0
80 79 };
81 80
82 81 /* When now reaches this time, it's time to call sync_kern() */
83 82 static struct timeval sync_kern_timer;
84 83
85 84 static fd_set fdbits;
86 85 static int sock_max;
87 86 int rip_sock = -1; /* RIP socket */
88 87 boolean_t rip_enabled;
89 88 static boolean_t openlog_done;
90 89
91 90 /*
92 91 * The interface to which rip_sock is currently pointing for
93 92 * output.
94 93 */
95 94 struct interface *rip_sock_interface;
96 95
97 96 int rt_sock; /* routing socket */
98 97
99 98
100 99 static int open_rip_sock();
101 100 static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
102 101 static void sigalrm(int);
103 102 static void sigterm(int);
104 103
105 104 int
106 105 main(int argc, char *argv[])
107 106 {
108 107 int n, off;
109 108 char *p, *q;
110 109 const char *cp;
111 110 struct timeval select_timeout, result;
112 111 fd_set ibits;
113 112 in_addr_t p_net, p_mask;
114 113 struct parm parm;
115 114 char *tracename = NULL;
116 115 boolean_t vflag = _B_FALSE;
117 116 boolean_t version = _B_FALSE;
118 117 int sigerr = 0;
119 118 FILE *pidfp;
120 119 mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
121 120
122 121 (void) setlocale(LC_ALL, "");
123 122
124 123 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
125 124 #define TEXT_DOMAIN "SYS_TEXT"
126 125 #endif /* ! TEXT_DOMAIN */
127 126
128 127 (void) textdomain(TEXT_DOMAIN);
129 128
130 129 /*
131 130 * Some shells are badly broken and send SIGHUP to backgrounded
132 131 * processes.
133 132 */
134 133 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
135 134 sigerr = errno;
136 135
137 136 ftrace = stdout;
138 137
139 138 if (gettimeofday(&clk, 0) == -1) {
140 139 logbad(_B_FALSE, "gettimeofday: %s", rip_strerror(errno));
141 140 }
142 141 prev_clk = clk;
143 142 epoch = clk;
144 143 epoch.tv_sec -= EPOCH;
145 144 now.tv_sec = EPOCH;
146 145 now_stale = EPOCH - STALE_TIME;
147 146 now_expire = EPOCH - EXPIRE_TIME;
148 147 now_garbage = EPOCH - GARBAGE_TIME;
149 148 select_timeout.tv_sec = 0;
150 149
151 150 while ((n = getopt(argc, argv, "sSqdghmpAztVvnT:F:P:")) != -1) {
152 151 switch (n) {
153 152 case 'A':
154 153 /*
155 154 * Ignore authentication if we do not care.
156 155 * Crazy as it is, that is what RFC 2453 requires.
157 156 */
158 157 auth_ok = _B_FALSE;
159 158 break;
160 159
161 160 case 't':
162 161 if (new_tracelevel < 2)
163 162 new_tracelevel = 2;
164 163 background = _B_FALSE;
165 164 break;
166 165
167 166 case 'd': /* put in.routed in foreground */
168 167 background = _B_FALSE;
169 168 break;
170 169
171 170 case 'F': /* minimal routes for SLIP */
172 171 n = FAKE_METRIC;
173 172 p = strchr(optarg, ',');
174 173 if (p != NULL) {
175 174 n = (int)strtoul(p+1, &q, 0);
176 175 if (*q == '\0' && p+1 != q &&
177 176 n <= HOPCNT_INFINITY-1 && n >= 1)
178 177 *p = '\0';
179 178 }
180 179 if (!getnet(optarg, &p_net, &p_mask)) {
181 180 if (p != NULL)
182 181 *p = ',';
183 182 msglog(gettext("bad network; \"-F %s\""),
184 183 optarg);
185 184 break;
186 185 }
187 186 (void) memset(&parm, 0, sizeof (parm));
188 187 parm.parm_net = p_net;
189 188 parm.parm_mask = p_mask;
190 189 parm.parm_d_metric = n;
191 190 cp = insert_parm(&parm);
192 191 if (cp != NULL)
193 192 msglog(gettext("bad -F: %s"), cp);
194 193 break;
195 194
196 195 case 'g':
197 196 (void) memset(&parm, 0, sizeof (parm));
198 197 parm.parm_d_metric = 1;
199 198 cp = insert_parm(&parm);
200 199 if (cp != NULL)
201 200 msglog(gettext("bad -g: %s"), cp);
202 201 else
203 202 default_gateway = _B_TRUE;
204 203 break;
205 204
206 205 case 'h': /* suppress extra host routes */
207 206 ridhosts = _B_TRUE;
208 207 break;
209 208
210 209 case 'm': /* advertise host route */
211 210 mhome = _B_TRUE; /* on multi-homed hosts */
212 211 break;
213 212
214 213 case 'n': /* No-install mode */
215 214 no_install = _B_TRUE;
216 215 break;
217 216
218 217 case 'P':
219 218 /* handle arbitrary parameters. */
220 219 q = strdup(optarg);
221 220 if (q == NULL)
222 221 logbad(_B_FALSE, "strdup: %s",
223 222 rip_strerror(errno));
224 223 cp = parse_parms(q, _B_FALSE);
225 224 if (cp != NULL)
226 225 msglog(gettext("%1$s in \"-P %2$s\""), cp,
227 226 optarg);
228 227 free(q);
229 228 break;
230 229
231 230 case 'q':
232 231 supplier = _B_FALSE;
233 232 supplier_set = _B_TRUE;
234 233 break;
235 234
236 235 case 's':
237 236 supplier = _B_TRUE;
238 237 supplier_set = _B_TRUE;
239 238 break;
240 239
241 240 case 'S': /* save-space option */
242 241 save_space = _B_TRUE;
243 242 break;
244 243
245 244 case 'T':
246 245 tracename = optarg;
247 246 break;
248 247
249 248 case 'V':
250 249 /* display version */
251 250 version = _B_TRUE;
252 251 msglog(gettext("version " IN_ROUTED_VERSION));
253 252 break;
254 253
255 254 case 'v':
256 255 /* display route changes to supplied logfile */
257 256 new_tracelevel = 1;
258 257 vflag = _B_TRUE;
259 258 break;
260 259
261 260 case 'z': /* increase debug-level */
262 261 new_tracelevel++;
263 262 break;
264 263
265 264 default:
266 265 goto usage;
267 266 }
268 267 }
269 268 argc -= optind;
270 269 argv += optind;
271 270
272 271 if (tracename == NULL && argc >= 1) {
273 272 tracename = *argv++;
274 273 argc--;
275 274 }
276 275 if (tracename != NULL && tracename[0] == '\0')
277 276 goto usage;
278 277 if (vflag && tracename == NULL)
279 278 goto usage;
280 279 if (argc != 0) {
281 280 usage:
282 281 (void) fprintf(stderr, gettext(
283 282 "usage: in.routed [-AdghmnqsStVvz] "
284 283 "[-T <tracefile>]\n"));
285 284 (void) fprintf(stderr,
286 285 gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
287 286 logbad(_B_FALSE, gettext("excess arguments"));
288 287 }
289 288 if (geteuid() != 0) {
290 289 /*
291 290 * Regular users are allowed to run in.routed for the
292 291 * sole purpose of obtaining the version number. In
293 292 * that case, exit(EXIT_SUCCESS) without complaining.
294 293 */
295 294 if (version)
296 295 exit(EXIT_SUCCESS);
297 296 logbad(_B_FALSE, gettext("requires UID 0"));
298 297 }
299 298
300 299 if (default_gateway) {
301 300 if (supplier_set && !supplier) {
302 301 msglog(gettext("-g and -q are incompatible"));
303 302 } else {
304 303 supplier = _B_TRUE;
305 304 supplier_set = _B_TRUE;
306 305 }
307 306 }
308 307
309 308 if (signal(SIGALRM, sigalrm) == SIG_ERR)
310 309 sigerr = errno;
311 310 /* SIGHUP fatal during debugging */
312 311 if (!background)
313 312 if (signal(SIGHUP, sigterm) == SIG_ERR)
314 313 sigerr = errno;
315 314 if (signal(SIGTERM, sigterm) == SIG_ERR)
316 315 sigerr = errno;
317 316 if (signal(SIGINT, sigterm) == SIG_ERR)
318 317 sigerr = errno;
319 318 if (signal(SIGUSR1, sigtrace_more) == SIG_ERR)
320 319 sigerr = errno;
321 320 if (signal(SIGUSR2, sigtrace_less) == SIG_ERR)
322 321 sigerr = errno;
323 322 if (signal(SIGHUP, sigtrace_dump) == SIG_ERR)
324 323 sigerr = errno;
325 324
326 325 if (sigerr)
327 326 msglog("signal: %s", rip_strerror(sigerr));
328 327
329 328 /* get into the background */
330 329 if (background && daemon(0, 0) < 0)
331 330 BADERR(_B_FALSE, "daemon()");
332 331
333 332 /* Store our process id, blow away any existing file if it exists. */
334 333 if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
335 334 (void) fprintf(stderr,
336 335 gettext("in.routed: unable to open " PATH_PID ": %s\n"),
337 336 strerror(errno));
338 337 } else {
339 338 (void) fprintf(pidfp, "%ld\n", getpid());
340 339 (void) fclose(pidfp);
341 340 (void) chmod(PATH_PID, pidmode);
342 341 }
343 342
344 343 srandom((int)(clk.tv_sec ^ clk.tv_usec ^ getpid()));
345 344
346 345 /* allocate the interface tables */
347 346 iftbl_alloc();
348 347
349 348 /* prepare socket connected to the kernel. */
350 349 rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_INET);
351 350 if (rt_sock < 0)
352 351 BADERR(_B_TRUE, "rt_sock = socket()");
353 352 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
354 353 logbad(_B_TRUE, "fcntl(rt_sock) O_NONBLOCK: %s",
355 354 rip_strerror(errno));
356 355 off = 0;
357 356 if (setsockopt(rt_sock, SOL_SOCKET, SO_USELOOPBACK,
358 357 &off, sizeof (off)) < 0)
359 358 LOGERR("setsockopt(SO_USELOOPBACK,0)");
360 359
361 360 fix_select();
362 361
363 362
364 363 if (tracename != NULL) {
365 364 (void) strlcpy(inittracename, tracename,
366 365 sizeof (inittracename));
367 366 set_tracefile(inittracename, "%s", -1);
368 367 } else {
369 368 tracelevel_msg("%s", -1); /* turn on tracing to stdio */
370 369 }
371 370
372 371 bufinit();
373 372
374 373 /* initialize radix tree */
375 374 rtinit();
376 375
377 376 /*
378 377 * Pick a random part of the second for our output to minimize
379 378 * collisions.
380 379 *
381 380 * Start broadcasting after hearing from other routers, and
382 381 * at a random time so a bunch of systems do not get synchronized
383 382 * after a power failure.
384 383 *
385 384 * Since now is the number of seconds since epoch (this is initially
386 385 * EPOCH seconds), these times are really relative to now.
387 386 */
388 387 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
389 388 age_timer.tv_usec = next_bcast.tv_usec;
390 389 age_timer.tv_sec = EPOCH+MIN_WAITTIME;
391 390 rdisc_timer = next_bcast;
392 391 ifscan_timer.tv_usec = next_bcast.tv_usec;
393 392
394 393 /*
395 394 * Open the global rip socket. From now on, this socket can be
396 395 * assumed to be open. It will remain open until in.routed
397 396 * exits.
398 397 */
399 398 rip_sock = open_rip_sock();
400 399
401 400 /*
402 401 * Collect an initial view of the world by checking the interface
403 402 * configuration and the kludge file.
404 403 *
405 404 * gwkludge() could call addroutefordefault(), resulting in a call to
406 405 * iflookup, and thus ifscan() to find the physical interfaces.
407 406 * ifscan() will attempt to use the rip_sock in order to join
408 407 * mcast groups, so gwkludge *must* be called after opening
409 408 * the rip_sock.
410 409 */
411 410 gwkludge();
412 411
413 412 ifscan();
414 413
415 414 /* Ask for routes */
416 415 rip_query();
417 416 rdisc_sol();
418 417
419 418 /* Now turn off stdio if not tracing */
420 419 if (new_tracelevel == 0)
421 420 trace_close(background);
422 421
423 422 /* Loop until a fatal error occurs, listening and broadcasting. */
424 423 for (;;) {
425 424 prev_clk = clk;
426 425 if (gettimeofday(&clk, 0) == -1) {
427 426 logbad(_B_FALSE, "gettimeofday: %s",
428 427 rip_strerror(errno));
429 428 }
430 429 if (prev_clk.tv_sec == clk.tv_sec &&
431 430 prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
432 431 /*
433 432 * Much of `in.routed` depends on time always advancing.
434 433 * On systems that do not guarantee that gettimeofday()
435 434 * produces unique timestamps even if called within
436 435 * a single tick, use trickery like that in classic
437 436 * BSD kernels.
438 437 */
439 438 clk.tv_usec += ++usec_fudge;
440 439
441 440 } else {
442 441 time_t dt;
443 442
444 443 usec_fudge = 0;
445 444
446 445 timevalsub(&result, &clk, &prev_clk);
447 446 if (result.tv_sec < 0 || result.tv_sec >
448 447 select_timeout.tv_sec + 5) {
449 448 /*
450 449 * Deal with time changes before other
451 450 * housekeeping to keep everything straight.
452 451 */
453 452 dt = result.tv_sec;
454 453 if (dt > 0)
455 454 dt -= select_timeout.tv_sec;
456 455 trace_act("time changed by %d sec", (int)dt);
457 456 epoch.tv_sec += dt;
458 457 }
459 458 }
460 459 timevalsub(&now, &clk, &epoch);
461 460 now_stale = now.tv_sec - STALE_TIME;
462 461 now_expire = now.tv_sec - EXPIRE_TIME;
463 462 now_garbage = now.tv_sec - GARBAGE_TIME;
464 463
465 464 /* deal with signals that should affect tracing */
466 465 set_tracelevel();
467 466
468 467 if (stopint != 0) {
469 468 trace_off("exiting with signal %d", stopint);
470 469 break;
471 470 }
472 471
473 472 /* look for new or dead interfaces */
474 473 timevalsub(&select_timeout, &ifscan_timer, &now);
475 474 if (select_timeout.tv_sec <= 0) {
476 475 select_timeout.tv_sec = 0;
477 476 ifscan();
478 477 rip_query();
479 478 continue;
480 479 }
481 480
482 481 /*
483 482 * Check the kernel table occassionally for mysteriously
484 483 * evaporated routes
485 484 */
486 485 timevalsub(&result, &sync_kern_timer, &now);
487 486 if (result.tv_sec <= 0) {
488 487 sync_kern();
489 488 sync_kern_timer.tv_sec = (now.tv_sec
490 489 + CHECK_QUIET_INTERVAL);
491 490 continue;
492 491 }
493 492 if (timercmp(&result, &select_timeout, < /* */))
494 493 select_timeout = result;
495 494
496 495 /* If it is time, then broadcast our routes. */
497 496 if (should_supply(NULL) || advertise_mhome) {
498 497 timevalsub(&result, &next_bcast, &now);
499 498 if (result.tv_sec <= 0) {
500 499 /*
501 500 * Synchronize the aging and broadcast
502 501 * timers to minimize awakenings
503 502 */
504 503 age(0);
505 504 age_peer_info();
506 505
507 506 rip_bcast(0);
508 507
509 508 /*
510 509 * It is desirable to send routing updates
511 510 * regularly. So schedule the next update
512 511 * 30 seconds after the previous one was
513 512 * scheduled, instead of 30 seconds after
514 513 * the previous update was finished.
515 514 * Even if we just started after discovering
516 515 * a 2nd interface or were otherwise delayed,
517 516 * pick a 30-second aniversary of the
518 517 * original broadcast time.
519 518 */
520 519 n = 1 + (0-result.tv_sec)/SUPPLY_INTERVAL;
521 520 next_bcast.tv_sec += n*SUPPLY_INTERVAL;
522 521
523 522 continue;
524 523 }
525 524
526 525 if (timercmp(&result, &select_timeout, < /* */))
527 526 select_timeout = result;
528 527 }
529 528
530 529 /*
531 530 * If we need a flash update, either do it now or
532 531 * set the delay to end when it is time.
533 532 *
534 533 * If we are within MIN_WAITTIME seconds of a full update,
535 534 * do not bother.
536 535 */
537 536 if (need_flash && should_supply(NULL) &&
538 537 no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
539 538 /* accurate to the millisecond */
540 539 if (!timercmp(&no_flash, &now, > /* */))
541 540 rip_bcast(1);
542 541 timevalsub(&result, &no_flash, &now);
543 542 if (timercmp(&result, &select_timeout, < /* */))
544 543 select_timeout = result;
545 544 }
546 545
547 546 /* trigger the main aging timer. */
548 547 timevalsub(&result, &age_timer, &now);
549 548 if (result.tv_sec <= 0) {
550 549 age(0);
551 550 continue;
552 551 }
553 552 if (timercmp(&result, &select_timeout, < /* */))
554 553 select_timeout = result;
555 554
556 555 /* update the kernel routing table */
557 556 timevalsub(&result, &need_kern, &now);
558 557 if (result.tv_sec <= 0) {
559 558 age(0);
560 559 continue;
561 560 }
562 561 if (timercmp(&result, &select_timeout, < /* */))
563 562 select_timeout = result;
564 563
565 564 /*
566 565 * take care of router discovery. We compare timeval
567 566 * structures here to have millisecond granularity.
568 567 */
569 568 if (!timercmp(&rdisc_timer, &now, > /* */)) {
570 569 rdisc_age(0);
571 570 continue;
572 571 }
573 572 timevalsub(&result, &rdisc_timer, &now);
574 573 if (timercmp(&result, &select_timeout, < /* */))
575 574 select_timeout = result;
576 575
577 576 /*
578 577 * Well-known bit of select(3c) silliness inherited
579 578 * from BSD: anything over 100 million seconds is
580 579 * considered an "error." Reset that to zero.
581 580 */
582 581 if (select_timeout.tv_sec > 100000000)
583 582 select_timeout.tv_sec = 0;
584 583
585 584 /* wait for input or a timer to expire. */
586 585 trace_flush();
587 586 ibits = fdbits;
588 587 n = select(sock_max, &ibits, 0, 0, &select_timeout);
589 588 if (n <= 0) {
590 589 if (n < 0 && errno != EINTR && errno != EAGAIN)
591 590 BADERR(_B_TRUE, "select");
592 591 continue;
593 592 }
594 593
595 594 if (FD_ISSET(rt_sock, &ibits)) {
596 595 read_rt();
597 596 n--;
598 597 }
599 598 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
600 599 read_d();
601 600 n--;
602 601 }
603 602 if (rdisc_mib_sock >= 0 && FD_ISSET(rdisc_mib_sock, &ibits)) {
604 603 process_d_mib_sock();
605 604 n--;
606 605 }
607 606 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
608 607 if (read_rip() == -1) {
609 608 rip_enabled = _B_FALSE;
610 609 trace_off("main rip socket failed");
611 610 (void) close(rip_sock);
612 611 rip_sock = -1;
613 612 fix_select();
614 613 break;
615 614 }
616 615 n--;
617 616 }
618 617 }
619 618 rip_bcast(0);
620 619 rdisc_adv(_B_FALSE);
621 620 (void) unlink(PATH_PID);
622 621 return (stopint | 128);
623 622 }
624 623
625 624
626 625 static void
627 626 sigalrm(int sig)
628 627 {
629 628 /*
630 629 * Historically, SIGALRM would cause the daemon to check for
631 630 * new and broken interfaces.
632 631 */
633 632 ifscan_timer.tv_sec = now.tv_sec;
634 633 trace_act("SIGALRM");
635 634 if (signal(sig, sigalrm) == SIG_ERR)
636 635 msglog("signal: %s", rip_strerror(errno));
637 636 }
638 637
639 638
640 639 /* watch for fatal signals */
641 640 static void
642 641 sigterm(int sig)
643 642 {
644 643 stopint = sig;
645 644 if (signal(sig, SIG_DFL) == SIG_ERR) /* catch it only once */
646 645 msglog("signal: %s", rip_strerror(errno));
647 646 }
648 647
649 648
650 649 void
651 650 fix_select(void)
652 651 {
653 652 (void) FD_ZERO(&fdbits);
654 653 sock_max = 0;
655 654
656 655 FD_SET(rt_sock, &fdbits);
657 656 if (sock_max <= rt_sock)
658 657 sock_max = rt_sock+1;
659 658 if (rip_sock >= 0) {
660 659 FD_SET(rip_sock, &fdbits);
661 660 if (sock_max <= rip_sock)
662 661 sock_max = rip_sock+1;
663 662 }
664 663 if (rdisc_sock >= 0) {
665 664 FD_SET(rdisc_sock, &fdbits);
666 665 if (sock_max <= rdisc_sock)
667 666 sock_max = rdisc_sock+1;
668 667 FD_SET(rdisc_mib_sock, &fdbits);
669 668 if (sock_max <= rdisc_mib_sock)
670 669 sock_max = rdisc_mib_sock+1;
671 670 }
672 671 }
673 672
674 673
675 674 void
676 675 fix_sock(int sock,
677 676 const char *name)
678 677 {
679 678 int on;
680 679 #define MIN_SOCKBUF (4*1024)
681 680 static int rbuf;
682 681
683 682 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
684 683 logbad(_B_TRUE, "fcntl(%s) O_NONBLOCK: %s", name,
685 684 rip_strerror(errno));
686 685 on = 1;
687 686 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
688 687 msglog("setsockopt(%s,SO_BROADCAST): %s",
689 688 name, rip_strerror(errno));
690 689
691 690 if (rbuf >= MIN_SOCKBUF) {
692 691 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
693 692 &rbuf, sizeof (rbuf)) < 0)
694 693 msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
695 694 name, rbuf, rip_strerror(errno));
696 695 } else {
697 696 for (rbuf = 60*1024; ; rbuf -= 4096) {
698 697 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
699 698 &rbuf, sizeof (rbuf)) == 0) {
700 699 trace_act("RCVBUF=%d", rbuf);
701 700 break;
702 701 }
703 702 if (rbuf < MIN_SOCKBUF) {
704 703 msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
705 704 name, rbuf, rip_strerror(errno));
706 705 break;
707 706 }
708 707 }
709 708 }
710 709 }
711 710
712 711
713 712 /*
714 713 * Open and return the global rip socket. It is guaranteed to return
715 714 * a good file descriptor.
716 715 */
717 716 static int
718 717 open_rip_sock()
719 718 {
720 719 struct sockaddr_in sin;
721 720 unsigned char ttl;
722 721 int s;
723 722 int on = 1;
724 723
725 724
726 725 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
727 726 BADERR(_B_TRUE, "rip_sock = socket()");
728 727
729 728 (void) memset(&sin, 0, sizeof (sin));
730 729 sin.sin_family = AF_INET;
731 730 sin.sin_port = htons(RIP_PORT);
732 731 sin.sin_addr.s_addr = INADDR_ANY;
733 732 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
734 733 BADERR(_B_FALSE, "bind(rip_sock)");
735 734 }
736 735 fix_sock(s, "rip_sock");
737 736
738 737 ttl = 1;
739 738 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
740 739 &ttl, sizeof (ttl)) < 0)
741 740 DBGERR(_B_TRUE, "rip_sock setsockopt(IP_MULTICAST_TTL)");
742 741
743 742 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &on, sizeof (on)))
744 743 BADERR(_B_FALSE, "setsockopt(IP_RECVIF)");
745 744
746 745 return (s);
747 746 }
748 747
749 748
750 749 /*
751 750 * Disable RIP. Note that we don't close the global rip socket since
752 751 * it is used even when RIP is disabled to receive and answer certain
753 752 * queries.
754 753 */
755 754 void
756 755 rip_off(void)
757 756 {
758 757 struct ip_mreq m;
759 758 struct interface *ifp;
760 759 char addrstr[INET_ADDRSTRLEN];
761 760
762 761 if (rip_enabled && !mhome) {
763 762 trace_act("turn off RIP");
764 763
765 764 /*
766 765 * Unsubscribe from the 224.0.0.9 RIP multicast
767 766 * group address
768 767 */
769 768 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
770 769 if ((ifp->int_if_flags & IFF_MULTICAST) &&
771 770 !IS_IFF_QUIET(ifp->int_if_flags) &&
772 771 !IS_RIP_IN_OFF(ifp->int_state) &&
773 772 !(ifp->int_state & IS_DUP)) {
774 773 m.imr_multiaddr.s_addr =
775 774 htonl(INADDR_RIP_GROUP);
776 775 m.imr_interface.s_addr =
777 776 (ifp->int_if_flags & IFF_POINTOPOINT) ?
778 777 ifp->int_dstaddr : ifp->int_addr;
779 778 (void) strlcpy(addrstr,
780 779 inet_ntoa(m.imr_multiaddr),
781 780 sizeof (addrstr));
782 781 if (setsockopt(rip_sock, IPPROTO_IP,
783 782 IP_DROP_MEMBERSHIP, &m,
784 783 sizeof (m)) < 0 &&
785 784 errno != EADDRNOTAVAIL && errno != ENOENT)
786 785 writelog(LOG_WARNING,
787 786 "%s: setsockopt(IP_DROP_MEMBERSHIP "
788 787 "%s, %s): %s", ifp->int_name,
789 788 addrstr, inet_ntoa(m.imr_interface),
790 789 rip_strerror(errno));
791 790 }
792 791 }
793 792 rip_enabled = _B_FALSE;
794 793
795 794 age(0);
796 795 }
797 796 }
798 797
799 798
800 799 /* turn on RIP multicast input via an interface */
801 800 void
802 801 rip_mcast_on(struct interface *ifp)
803 802 {
804 803 struct ip_mreq m;
805 804
806 805 if (!IS_RIP_IN_OFF(ifp->int_state) &&
807 806 (ifp->int_if_flags & IFF_MULTICAST) &&
808 807 !IS_IFF_QUIET(ifp->int_if_flags) &&
809 808 !(ifp->int_state & IS_DUP)) {
810 809 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
811 810 m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
812 811 ifp->int_dstaddr : ifp->int_addr;
813 812 if ((setsockopt(rip_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
814 813 &m, sizeof (m)) < 0) && !(ifp->int_state & IS_BROKE))
815 814 writelog(LOG_WARNING,
816 815 "Could not join 224.0.0.9 on interface %s: %s",
817 816 ifp->int_name, rip_strerror(errno));
818 817 }
819 818 }
820 819
821 820 /* turn off RIP multicast input via an interface */
822 821 void
823 822 rip_mcast_off(struct interface *ifp)
824 823 {
825 824 struct ip_mreq m;
826 825
827 826 if ((ifp->int_if_flags & IFF_MULTICAST) &&
828 827 !IS_IFF_QUIET(ifp->int_if_flags) && rip_enabled) {
829 828 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
830 829 m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
831 830 ifp->int_dstaddr : ifp->int_addr;
832 831 if ((setsockopt(rip_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
833 832 &m, sizeof (m)) < 0) && errno != EADDRNOTAVAIL &&
834 833 errno != ENOENT)
835 834 writelog(LOG_WARNING,
836 835 "setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s",
837 836 ifp->int_name, rip_strerror(errno));
838 837 }
839 838 }
840 839
841 840 /* enable RIP */
842 841 void
843 842 rip_on(struct interface *ifp)
844 843 {
845 844 /*
846 845 * If RIP is already enabled, only start receiving
847 846 * multicasts for this interface.
848 847 */
849 848 if (rip_enabled) {
850 849 if (ifp != NULL)
851 850 rip_mcast_on(ifp);
852 851 return;
853 852 }
854 853
855 854 /*
856 855 * If RIP is disabled and it makes sense to enable it, then enable
857 856 * it on all of the interfaces. It makes sense if either router
858 857 * discovery is off, or if router discovery is on and at most one
859 858 * interface is doing RIP.
860 859 */
861 860 if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
862 861 trace_act("turn on RIP");
863 862
864 863 rip_enabled = _B_TRUE;
865 864 rip_sock_interface = NULL;
866 865
867 866 /* Do not advertise anything until we have heard something */
868 867 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
869 868 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
870 869
871 870 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
872 871 ifp->int_query_time = NEVER;
873 872 rip_mcast_on(ifp);
874 873 }
875 874 ifscan_timer.tv_sec = now.tv_sec;
876 875 }
877 876
878 877 fix_select();
879 878 }
880 879
881 880
882 881 /* die if malloc(3) fails */
883 882 void *
884 883 rtmalloc(size_t size,
885 884 const char *msg)
886 885 {
887 886 void *p = malloc(size);
888 887 if (p == NULL)
889 888 logbad(_B_TRUE, "malloc(%lu) failed in %s: %s", (ulong_t)size,
890 889 msg, rip_strerror(errno));
891 890 return (p);
892 891 }
893 892
894 893
895 894 /* get a random instant in an interval */
896 895 void
897 896 intvl_random(struct timeval *tp, /* put value here */
898 897 ulong_t lo, /* value is after this second */
899 898 ulong_t hi) /* and before this */
900 899 {
901 900 tp->tv_sec = (time_t)(hi == lo ? lo : (lo + random() % ((hi - lo))));
902 901 tp->tv_usec = random() % 1000000;
903 902 }
904 903
905 904
906 905 void
907 906 timevaladd(struct timeval *t1,
908 907 struct timeval *t2)
909 908 {
910 909
911 910 t1->tv_sec += t2->tv_sec;
912 911 if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
913 912 t1->tv_sec++;
914 913 t1->tv_usec -= 1000000;
915 914 }
916 915 }
917 916
918 917
919 918 /* t1 = t2 - t3 */
920 919 static void
921 920 timevalsub(struct timeval *t1,
922 921 struct timeval *t2,
923 922 struct timeval *t3)
924 923 {
925 924 t1->tv_sec = t2->tv_sec - t3->tv_sec;
926 925 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
927 926 t1->tv_sec--;
928 927 t1->tv_usec += 1000000;
929 928 }
930 929 }
931 930
932 931 static void
933 932 do_openlog(void)
934 933 {
935 934 openlog_done = _B_TRUE;
936 935 openlog("in.routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
937 936 }
938 937
939 938 /* put a LOG_ERR message into the system log */
940 939 void
941 940 msglog(const char *p, ...)
942 941 {
943 942 va_list args;
944 943
945 944 trace_flush();
946 945
947 946 if (!openlog_done)
948 947 do_openlog();
949 948 va_start(args, p);
950 949 vsyslog(LOG_ERR, p, args);
951 950
952 951 if (ftrace != 0) {
953 952 if (ftrace == stdout)
954 953 (void) fputs("in.routed: ", ftrace);
955 954 (void) vfprintf(ftrace, p, args);
956 955 (void) fputc('\n', ftrace);
957 956 }
958 957 }
959 958
960 959
961 960 /*
962 961 * Put a message about a bad system into the system log if
963 962 * we have not complained about it recently.
964 963 *
965 964 * It is desirable to complain about all bad systems, but not too often.
966 965 * In the worst case, it is not practical to keep track of all bad systems.
967 966 * For example, there can be many systems with the wrong password.
968 967 */
969 968 void
970 969 msglim(struct msg_limit *lim, in_addr_t addr, const char *p, ...)
971 970 {
972 971 va_list args;
973 972 int i;
974 973 struct msg_sub *ms1, *ms;
975 974 const char *p1;
976 975
977 976 va_start(args, p);
978 977
979 978 /*
980 979 * look for the oldest slot in the table
981 980 * or the slot for the bad router.
982 981 */
983 982 ms = ms1 = lim->subs;
984 983 for (i = MSG_SUBJECT_N; ; i--, ms1++) {
985 984 if (i == 0) {
986 985 /* Reuse a slot at most once every 10 minutes. */
987 986 if (lim->reuse > now.tv_sec) {
988 987 ms = NULL;
989 988 } else {
990 989 lim->reuse = now.tv_sec + 10*60;
991 990 }
992 991 break;
993 992 }
994 993 if (ms->addr == addr) {
995 994 /*
996 995 * Repeat a complaint about a given system at
997 996 * most once an hour.
998 997 */
999 998 if (ms->until > now.tv_sec)
1000 999 ms = NULL;
1001 1000 break;
1002 1001 }
1003 1002 if (ms->until < ms1->until)
1004 1003 ms = ms1;
1005 1004 }
1006 1005 if (ms != NULL) {
1007 1006 ms->addr = addr;
1008 1007 ms->until = now.tv_sec + 60*60; /* 60 minutes */
1009 1008
1010 1009 if (!openlog_done)
1011 1010 do_openlog();
1012 1011 trace_flush();
1013 1012 for (p1 = p; *p1 == ' '; p1++)
1014 1013 continue;
1015 1014 vsyslog(LOG_ERR, p1, args);
1016 1015 }
1017 1016
1018 1017 /* always display the message if tracing */
1019 1018 if (ftrace != 0) {
1020 1019 (void) vfprintf(ftrace, p, args);
1021 1020 (void) fputc('\n', ftrace);
1022 1021 }
1023 1022 }
1024 1023
1025 1024
1026 1025 void
1027 1026 logbad(boolean_t dump, const char *p, ...)
1028 1027 {
1029 1028 va_list args;
1030 1029
1031 1030 trace_flush();
1032 1031
1033 1032 if (!openlog_done)
1034 1033 do_openlog();
1035 1034 va_start(args, p);
1036 1035 vsyslog(LOG_ERR, p, args);
1037 1036
1038 1037 (void) fputs(gettext("in.routed: "), stderr);
1039 1038 (void) vfprintf(stderr, p, args);
1040 1039 (void) fputs(gettext("; giving up\n"), stderr);
1041 1040 (void) fflush(stderr);
1042 1041
1043 1042 if (dump)
1044 1043 abort();
1045 1044 exit(EXIT_FAILURE);
1046 1045 }
1047 1046
1048 1047 /* put a message into the system log */
1049 1048 void
1050 1049 writelog(int level, const char *p, ...)
1051 1050 {
1052 1051 va_list args;
1053 1052
1054 1053 trace_flush();
1055 1054
1056 1055 if (!openlog_done)
1057 1056 do_openlog();
1058 1057 va_start(args, p);
1059 1058 vsyslog(level, p, args);
1060 1059
1061 1060 if (ftrace != 0) {
1062 1061 if (ftrace == stdout)
1063 1062 (void) fputs("in.routed: ", ftrace);
1064 1063 (void) vfprintf(ftrace, p, args);
1065 1064 (void) fputc('\n', ftrace);
1066 1065 }
1067 1066 }
↓ open down ↓ |
1011 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX