3 Network input dispatcher... */
6 * Copyright (c) 1995, 1996, 1997, 1998, 1999
7 * The Internet Software Consortium. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
44 static char copyright[] =
45 "$Id: dispatch.c,v 1.47.2.14 1999/03/29 22:16:36 mellon Exp $ Copyright (c) 1995, 1996, 1997, 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
49 #include <sys/ioctl.h>
51 struct interface_info *interfaces, *dummy_interfaces, *fallback_interface;
52 struct protocol *protocols;
53 struct timeout *timeouts;
54 static struct timeout *free_timeouts;
55 static int interfaces_invalidated;
56 void (*bootp_packet_handler) PROTO ((struct interface_info *,
57 struct dhcp_packet *, int, unsigned int,
58 struct iaddr, struct hardware *));
60 int quiet_interface_discovery;
62 /* Use the SIOCGIFCONF ioctl to get a list of all the attached interfaces.
63 For each interface that's of type INET and not the loopback interface,
64 register that interface with the network I/O software, figure out what
65 subnet it's on, and add it to the list of interfaces. */
67 void discover_interfaces (state)
70 struct interface_info *tmp;
71 struct interface_info *last, *next;
77 struct subnet *subnet;
78 struct shared_network *share;
79 struct sockaddr_in foo;
82 #ifdef ALIAS_NAMES_PERMUTED
86 /* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
87 if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
88 error ("Can't create addrlist socket");
90 /* Get the interface configuration information... */
91 ic.ifc_len = sizeof buf;
92 ic.ifc_ifcu.ifcu_buf = (caddr_t)buf;
93 i = ioctl(sock, SIOCGIFCONF, &ic);
96 error ("ioctl: SIOCGIFCONF: %m");
98 /* If we already have a list of interfaces, and we're running as
99 a DHCP server, the interfaces were requested. */
100 if (interfaces && (state == DISCOVER_SERVER ||
101 state == DISCOVER_RELAY ||
102 state == DISCOVER_REQUESTED))
104 else if (state == DISCOVER_UNCONFIGURED)
105 ir = INTERFACE_REQUESTED | INTERFACE_AUTOMATIC;
107 ir = INTERFACE_REQUESTED;
109 /* Cycle through the list of interfaces looking for IP addresses. */
110 for (i = 0; i < ic.ifc_len;) {
111 struct ifreq *ifp = (struct ifreq *)((caddr_t)ic.ifc_req + i);
113 if (ifp -> ifr_addr.sa_len > sizeof (struct sockaddr))
114 i += (sizeof ifp -> ifr_name) + ifp -> ifr_addr.sa_len;
119 #ifdef ALIAS_NAMES_PERMUTED
120 if ((s = strrchr (ifp -> ifr_name, ':'))) {
125 #ifdef SKIP_DUMMY_INTERFACES
126 if (!strncmp (ifp -> ifr_name, "dummy", 5))
131 /* See if this is the sort of interface we want to
133 strcpy (ifr.ifr_name, ifp -> ifr_name);
134 if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0)
135 error ("Can't get interface flags for %s: %m",
138 /* Skip loopback, point-to-point and down interfaces,
139 except don't skip down interfaces if we're trying to
140 get a list of configurable interfaces. */
141 if ((ifr.ifr_flags & IFF_LOOPBACK) ||
142 #ifdef HAVE_IFF_POINTOPOINT
143 (ifr.ifr_flags & IFF_POINTOPOINT) ||
145 (!(ifr.ifr_flags & IFF_UP) &&
146 state != DISCOVER_UNCONFIGURED))
149 /* See if we've seen an interface that matches this one. */
150 for (tmp = interfaces; tmp; tmp = tmp -> next)
151 if (!strcmp (tmp -> name, ifp -> ifr_name))
154 /* If there isn't already an interface by this name,
157 tmp = ((struct interface_info *)
158 dmalloc (sizeof *tmp, "discover_interfaces"));
160 error ("Insufficient memory to %s %s",
161 "record interface", ifp -> ifr_name);
162 strcpy (tmp -> name, ifp -> ifr_name);
163 tmp -> next = interfaces;
168 /* If we have the capability, extract link information
169 and record it in a linked list. */
171 if (ifp -> ifr_addr.sa_family == AF_LINK) {
172 struct sockaddr_dl *foo = ((struct sockaddr_dl *)
174 tmp -> hw_address.hlen = foo -> sdl_alen;
175 tmp -> hw_address.htype = HTYPE_ETHER; /* XXX */
176 memcpy (tmp -> hw_address.haddr,
177 LLADDR (foo), foo -> sdl_alen);
181 if (ifp -> ifr_addr.sa_family == AF_INET) {
184 /* Get a pointer to the address... */
185 memcpy (&foo, &ifp -> ifr_addr,
186 sizeof ifp -> ifr_addr);
188 /* We don't want the loopback interface. */
189 if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK))
193 /* If this is the first real IP address we've
194 found, keep a pointer to ifreq structure in
195 which we found it. */
198 int len = ((sizeof ifp -> ifr_name) +
199 ifp -> ifr_addr.sa_len);
201 int len = sizeof *ifp;
203 tif = (struct ifreq *)malloc (len);
205 error ("no space to remember ifp.");
206 memcpy (tif, ifp, len);
208 tmp -> primary_address = foo.sin_addr;
211 /* Grab the address... */
213 memcpy (addr.iabuf, &foo.sin_addr.s_addr,
216 /* If there's a registered subnet for this address,
217 connect it together... */
218 if ((subnet = find_subnet (addr))) {
219 /* If this interface has multiple aliases
220 on the same subnet, ignore all but the
221 first we encounter. */
222 if (!subnet -> interface) {
223 subnet -> interface = tmp;
224 subnet -> interface_address = addr;
225 } else if (subnet -> interface != tmp) {
226 warn ("Multiple %s %s: %s %s",
227 "interfaces match the",
229 subnet -> interface -> name,
232 share = subnet -> shared_network;
233 if (tmp -> shared_network &&
234 tmp -> shared_network != share) {
235 warn ("Interface %s matches %s",
237 "multiple shared networks");
239 tmp -> shared_network = share;
242 if (!share -> interface) {
243 share -> interface = tmp;
244 } else if (share -> interface != tmp) {
245 warn ("Multiple %s %s: %s %s",
246 "interfaces match the",
247 "same shared network",
248 share -> interface -> name,
255 #if defined (LINUX_SLASHPROC_DISCOVERY)
256 /* On Linux, interfaces that don't have IP addresses don't show up
257 in the SIOCGIFCONF syscall. We got away with this prior to
258 Linux 2.1 because we would give each interface an IP address of
259 0.0.0.0 before trying to boot, but that doesn't work after 2.1
260 because we're using LPF, because we can't configure interfaces
261 with IP addresses of 0.0.0.0 anymore (grumble). This only
262 matters for the DHCP client, of course - the relay agent and
263 server should only care about interfaces that are configured
264 with IP addresses anyway.
266 The PROCDEV_DEVICE (/proc/net/dev) is a kernel-supplied file
267 that, when read, prints a human readable network status. We
268 extract the names of the network devices by skipping the first
269 two lines (which are header) and then parsing off everything
270 up to the colon in each subsequent line - these lines start
271 with the interface name, then a colon, then a bunch of
272 statistics. Yes, Virgina, this is a kludge, but you work
273 with what you have. */
275 if (state == DISCOVER_UNCONFIGURED) {
280 proc_dev = fopen (PROCDEV_DEVICE, "r");
282 error ("%s: %m", PROCDEV_DEVICE);
284 while (fgets (buffer, sizeof buffer, proc_dev)) {
288 /* Skip the first two blocks, which are header
295 sep = strrchr (buffer, ':');
301 /* See if we've seen an interface that matches
303 for (tmp = interfaces; tmp; tmp = tmp -> next)
304 if (!strcmp (tmp -> name, name))
307 /* If we found one, nothing more to do.. */
311 /* Otherwise, allocate one. */
312 tmp = ((struct interface_info *)
313 dmalloc (sizeof *tmp, "discover_interfaces"));
315 error ("Insufficient memory to %s %s",
316 "record interface", name);
317 memset (tmp, 0, sizeof *tmp);
318 strcpy (tmp -> name, name);
321 tmp -> next = interfaces;
328 /* Now cycle through all the interfaces we found, looking for
329 hardware addresses. */
330 #if defined (HAVE_SIOCGIFHWADDR) && !defined (HAVE_AF_LINK)
331 for (tmp = interfaces; tmp; tmp = tmp -> next) {
337 /* Make up an ifreq structure. */
338 tif = (struct ifreq *)malloc (sizeof (struct ifreq));
340 error ("no space to remember ifp.");
341 memset (tif, 0, sizeof (struct ifreq));
342 strcpy (tif -> ifr_name, tmp -> name);
346 /* Read the hardware address from this interface. */
348 if (ioctl (sock, SIOCGIFHWADDR, &ifr) < 0)
351 sa = *(struct sockaddr *)&ifr.ifr_hwaddr;
353 switch (sa.sa_family) {
354 #ifdef HAVE_ARPHRD_TUNNEL
356 /* ignore tunnel interfaces. */
358 #ifdef HAVE_ARPHRD_ROSE
361 #ifdef HAVE_ARPHRD_LOOPBACK
362 case ARPHRD_LOOPBACK:
363 /* ignore loopback interface */
368 tmp -> hw_address.hlen = 6;
369 tmp -> hw_address.htype = ARPHRD_ETHER;
370 memcpy (tmp -> hw_address.haddr, sa.sa_data, 6);
373 #ifndef HAVE_ARPHRD_IEEE802
374 # define ARPHRD_IEEE802 HTYPE_IEEE802
377 tmp -> hw_address.hlen = 6;
378 tmp -> hw_address.htype = ARPHRD_IEEE802;
379 memcpy (tmp -> hw_address.haddr, sa.sa_data, 6);
382 #ifndef HAVE_ARPHRD_FDDI
383 # define ARPHRD_FDDI HTYPE_FDDI
386 tmp -> hw_address.hlen = 16;
387 tmp -> hw_address.htype = HTYPE_FDDI; /* XXX */
388 memcpy (tmp -> hw_address.haddr, sa.sa_data, 16);
391 #ifdef HAVE_ARPHRD_METRICOM
392 case ARPHRD_METRICOM:
393 tmp -> hw_address.hlen = 6;
394 tmp -> hw_address.htype = ARPHRD_METRICOM;
395 memcpy (tmp -> hw_address.haddr, sa.sa_data, 6);
399 #ifdef HAVE_ARPHRD_AX25
401 tmp -> hw_address.hlen = 6;
402 tmp -> hw_address.htype = ARPHRD_AX25;
403 memcpy (tmp -> hw_address.haddr, sa.sa_data, 6);
407 #ifdef HAVE_ARPHRD_NETROM
409 tmp -> hw_address.hlen = 6;
410 tmp -> hw_address.htype = ARPHRD_NETROM;
411 memcpy (tmp -> hw_address.haddr, sa.sa_data, 6);
416 warn ("%s: unknown hardware address type %d",
417 ifr.ifr_name, sa.sa_family);
421 #endif /* defined (HAVE_SIOCGIFHWADDR) && !defined (HAVE_AF_LINK) */
423 /* If we're just trying to get a list of interfaces that we might
424 be able to configure, we can quit now. */
425 if (state == DISCOVER_UNCONFIGURED)
428 /* Weed out the interfaces that did not have IP addresses. */
429 last = (struct interface_info *)0;
430 for (tmp = interfaces; tmp; tmp = next) {
432 if ((tmp -> flags & INTERFACE_AUTOMATIC) &&
433 state == DISCOVER_REQUESTED)
434 tmp -> flags &= ~(INTERFACE_AUTOMATIC |
435 INTERFACE_REQUESTED);
436 if (!tmp -> ifp || !(tmp -> flags & INTERFACE_REQUESTED)) {
437 if ((tmp -> flags & INTERFACE_REQUESTED) != ir)
438 error ("%s: not found", tmp -> name);
440 interfaces = interfaces -> next;
442 last -> next = tmp -> next;
444 /* Remember the interface in case we need to know
446 tmp -> next = dummy_interfaces;
447 dummy_interfaces = tmp;
452 memcpy (&foo, &tmp -> ifp -> ifr_addr,
453 sizeof tmp -> ifp -> ifr_addr);
455 /* We must have a subnet declaration for each interface. */
456 if (!tmp -> shared_network && (state == DISCOVER_SERVER)) {
457 warn ("No subnet declaration for %s (%s).",
458 tmp -> name, inet_ntoa (foo.sin_addr));
459 warn ("Please write a subnet declaration for the %s",
460 "network segment to");
461 error ("which interface %s is attached.", tmp -> name);
464 /* Find subnets that don't have valid interface
466 for (subnet = (tmp -> shared_network
467 ? tmp -> shared_network -> subnets
468 : (struct subnet *)0);
469 subnet; subnet = subnet -> next_sibling) {
470 if (!subnet -> interface_address.len) {
471 /* Set the interface address for this subnet
472 to the first address we found. */
473 subnet -> interface_address.len = 4;
474 memcpy (subnet -> interface_address.iabuf,
475 &foo.sin_addr.s_addr, 4);
479 /* Register the interface... */
480 if_register_receive (tmp);
481 if_register_send (tmp);
484 /* Now register all the remaining interfaces as protocols. */
485 for (tmp = interfaces; tmp; tmp = tmp -> next)
486 add_protocol (tmp -> name, tmp -> rfdesc, got_one, tmp);
490 maybe_setup_fallback ();
493 struct interface_info *setup_fallback ()
496 ((struct interface_info *)
497 dmalloc (sizeof *fallback_interface, "discover_interfaces"));
498 if (!fallback_interface)
499 error ("Insufficient memory to record fallback interface.");
500 memset (fallback_interface, 0, sizeof *fallback_interface);
501 strcpy (fallback_interface -> name, "fallback");
502 fallback_interface -> shared_network =
503 new_shared_network ("parse_statement");
504 if (!fallback_interface -> shared_network)
505 error ("No memory for shared subnet");
506 memset (fallback_interface -> shared_network, 0,
507 sizeof (struct shared_network));
508 fallback_interface -> shared_network -> name = "fallback-net";
509 return fallback_interface;
512 void reinitialize_interfaces ()
514 struct interface_info *ip;
516 for (ip = interfaces; ip; ip = ip -> next) {
517 if_reinitialize_receive (ip);
518 if_reinitialize_send (ip);
521 if (fallback_interface)
522 if_reinitialize_send (fallback_interface);
524 interfaces_invalidated = 1;
528 /* Wait for packets to come in using poll(). When a packet comes in,
529 call receive_packet to receive the packet and possibly strip hardware
530 addressing information from it, and then call through the
531 bootp_packet_handler hook to try to do something with it. */
543 for (l = protocols; l; l = l -> next) {
546 fds = (struct pollfd *)malloc ((nfds) * sizeof (struct pollfd));
548 error ("Can't allocate poll structures.");
551 /* Call any expired timeouts, and then if there's
552 still a timeout registered, time out the select
557 if (timeouts -> when <= cur_time) {
559 timeouts = timeouts -> next;
560 (*(t -> func)) (t -> what);
561 t -> next = free_timeouts;
565 /* Figure timeout in milliseconds, and check for
566 potential overflow. We assume that integers
567 are 32 bits, which is harmless if they're 64
568 bits - we'll just get extra timeouts in that
569 case. Lease times would have to be quite
570 long in order for a 32-bit integer to overflow,
572 to_msec = timeouts -> when - cur_time;
573 if (to_msec > 2147483)
579 /* Set up the descriptors to be polled. */
581 for (l = protocols; l; l = l -> next) {
582 fds [i].fd = l -> fd;
583 fds [i].events = POLLIN;
588 /* Wait for a packet or a timeout... XXX */
589 count = poll (fds, nfds, to_msec);
591 /* Get the current time... */
592 GET_TIME (&cur_time);
594 /* Not likely to be transitory... */
596 if (errno == EAGAIN || errno == EINTR)
603 for (l = protocols; l; l = l -> next) {
604 if ((fds [i].revents & POLLIN)) {
607 (*(l -> handler)) (l);
608 if (interfaces_invalidated)
613 interfaces_invalidated = 0;
617 /* Wait for packets to come in using select(). When one does, call
618 receive_packet to receive the packet and possibly strip hardware
619 addressing information from it, and then call through the
620 bootp_packet_handler hook to try to do something with it. */
628 struct timeval tv, *tvp;
634 /* Call any expired timeouts, and then if there's
635 still a timeout registered, time out the select
640 if (timeouts -> when <= cur_time) {
642 timeouts = timeouts -> next;
643 (*(t -> func)) (t -> what);
644 t -> next = free_timeouts;
648 tv.tv_sec = timeouts -> when - cur_time;
652 tvp = (struct timeval *)0;
654 /* Set up the read mask. */
657 for (l = protocols; l; l = l -> next) {
658 FD_SET (l -> fd, &r);
663 /* Wait for a packet or a timeout... XXX */
664 count = select (max + 1, &r, &w, &x, tvp);
666 /* Get the current time... */
667 GET_TIME (&cur_time);
669 /* Not likely to be transitory... */
671 error ("select: %m");
673 for (l = protocols; l; l = l -> next) {
674 if (!FD_ISSET (l -> fd, &r))
677 (*(l -> handler)) (l);
678 if (interfaces_invalidated)
681 interfaces_invalidated = 0;
684 #endif /* USE_POLL */
689 struct sockaddr_in from;
690 struct hardware hfrom;
694 unsigned char packbuf [4095]; /* Packet input buffer.
695 Must be as large as largest
697 struct dhcp_packet packet;
699 struct interface_info *ip = l -> local;
702 receive_packet (ip, u.packbuf, sizeof u, &from, &hfrom)) < 0) {
703 warn ("receive_packet failed on %s: %m", ip -> name);
709 if (bootp_packet_handler) {
711 memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);
713 (*bootp_packet_handler) (ip, &u.packet, result,
714 from.sin_port, ifrom, &hfrom);
718 int locate_network (packet)
719 struct packet *packet;
723 /* If this came through a gateway, find the corresponding subnet... */
724 if (packet -> raw -> giaddr.s_addr) {
725 struct subnet *subnet;
727 memcpy (ia.iabuf, &packet -> raw -> giaddr, 4);
728 subnet = find_subnet (ia);
730 packet -> shared_network = subnet -> shared_network;
732 packet -> shared_network = (struct shared_network *)0;
734 packet -> shared_network =
735 packet -> interface -> shared_network;
737 if (packet -> shared_network)
742 void add_timeout (when, where, what)
744 void (*where) PROTO ((void *));
747 struct timeout *t, *q;
749 /* See if this timeout supersedes an existing timeout. */
750 t = (struct timeout *)0;
751 for (q = timeouts; q; q = q -> next) {
752 if (q -> func == where && q -> what == what) {
754 t -> next = q -> next;
756 timeouts = q -> next;
762 /* If we didn't supersede a timeout, allocate a timeout
767 free_timeouts = q -> next;
771 q = (struct timeout *)malloc (sizeof (struct timeout));
773 error ("Can't allocate timeout structure!");
781 /* Now sort this timeout into the timeout list. */
783 /* Beginning of list? */
784 if (!timeouts || timeouts -> when > q -> when) {
785 q -> next = timeouts;
790 /* Middle of list? */
791 for (t = timeouts; t -> next; t = t -> next) {
792 if (t -> next -> when > q -> when) {
793 q -> next = t -> next;
801 q -> next = (struct timeout *)0;
804 void cancel_timeout (where, what)
805 void (*where) PROTO ((void *));
808 struct timeout *t, *q;
810 /* Look for this timeout on the list, and unlink it if we find it. */
811 t = (struct timeout *)0;
812 for (q = timeouts; q; q = q -> next) {
813 if (q -> func == where && q -> what == what) {
815 t -> next = q -> next;
817 timeouts = q -> next;
823 /* If we found the timeout, put it on the free list. */
825 q -> next = free_timeouts;
830 /* Add a protocol to the list of protocols... */
831 void add_protocol (name, fd, handler, local)
834 void (*handler) PROTO ((struct protocol *));
839 p = (struct protocol *)malloc (sizeof *p);
841 error ("can't allocate protocol struct for %s", name);
844 p -> handler = handler;
847 p -> next = protocols;
851 void remove_protocol (proto)
852 struct protocol *proto;
854 struct protocol *p, *next, *prev;
856 prev = (struct protocol *)0;
857 for (p = protocols; p; p = next) {
861 prev -> next = p -> next;
863 protocols = p -> next;