2 * $Id: ipconfig.c,v 1.43.2.1 2001/12/13 10:39:53 davem Exp $
4 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
5 * user-supplied information to configure own IP address and routes.
7 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
9 * Derived from network configuration code in fs/nfs/nfsroot.c,
10 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
12 * BOOTP rewritten to construct and analyse packets itself instead
13 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
14 * -- MJ, December 1998
16 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
17 * initialization scheme.
18 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
20 * DHCP support added. To users this looks like a whole separate
21 * protocol, but we know it's just a bag on the side of BOOTP.
22 * -- Chip Salzenberg <chip@valinux.com>, May 2000
24 * Ported DHCP support from 2.2.16 to 2.4.0-test4
25 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
27 * Merged changes from 2.2.19 into 2.4.3
28 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
30 * Multipe Nameservers in /proc/net/pnp
31 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
34 #include <linux/config.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/random.h>
40 #include <linux/init.h>
41 #include <linux/utsname.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
49 #include <linux/socket.h>
50 #include <linux/route.h>
51 #include <linux/udp.h>
52 #include <linux/proc_fs.h>
53 #include <linux/major.h>
56 #include <net/ipconfig.h>
58 #include <asm/uaccess.h>
59 #include <asm/checksum.h>
60 #include <asm/processor.h>
62 /* Define this to allow debugging output */
66 #define DBG(x) printk x
68 #define DBG(x) do { } while(0)
71 #if defined(CONFIG_IP_PNP_DHCP)
74 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
75 #define IPCONFIG_BOOTP
77 #if defined(CONFIG_IP_PNP_RARP)
80 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
81 #define IPCONFIG_DYNAMIC
84 /* Define the friendly delay before and after opening net devices */
85 #define CONF_PRE_OPEN (HZ/2) /* Before opening: 1/2 second */
86 #define CONF_POST_OPEN (1*HZ) /* After opening: 1 second */
88 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
89 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
90 #define CONF_SEND_RETRIES 6 /* Send six requests per open */
91 #define CONF_INTER_TIMEOUT (HZ/2) /* Inter-device timeout: 1/2 second */
92 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
93 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
94 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
95 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
96 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
97 - '3' from resolv.h */
101 * Public IP configuration
104 /* This is used by platforms which might be able to set the ipconfig
105 * variables using firmware environment vars. If this is set, it will
106 * ignore such firmware variables.
108 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
110 int ic_enable __initdata = 0; /* IP config enabled? */
112 /* Protocol choice */
113 int ic_proto_enabled __initdata = 0
114 #ifdef IPCONFIG_BOOTP
117 #ifdef CONFIG_IP_PNP_DHCP
125 int ic_host_name_set __initdata = 0; /* Host name set by us? */
127 u32 ic_myaddr = INADDR_NONE; /* My IP address */
128 u32 ic_netmask = INADDR_NONE; /* Netmask for local subnet */
129 u32 ic_gateway = INADDR_NONE; /* Gateway IP address */
131 u32 ic_servaddr = INADDR_NONE; /* Boot server IP address */
133 u32 root_server_addr = INADDR_NONE; /* Address of NFS server */
134 u8 root_server_path[256] = { 0, }; /* Path to mount as root */
136 /* Persistent data: */
138 int ic_proto_used; /* Protocol used, if any */
139 u32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
140 u8 ic_domain[64]; /* DNS (not NIS) domain name */
146 /* Name of user-selected boot device */
147 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
149 /* Protocols supported by available interfaces */
150 static int ic_proto_have_if __initdata = 0;
152 #ifdef IPCONFIG_DYNAMIC
153 static spinlock_t ic_recv_lock = SPIN_LOCK_UNLOCKED;
154 static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */
157 static int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */
166 struct ic_device *next;
167 struct net_device *dev;
168 unsigned short flags;
173 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
174 static struct net_device *ic_dev __initdata = NULL; /* Selected device */
176 static int __init ic_open_devs(void)
178 struct ic_device *d, **last;
179 struct net_device *dev;
180 unsigned short oflags;
182 last = &ic_first_dev;
184 for (dev = dev_base; dev; dev = dev->next) {
185 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
186 (!(dev->flags & IFF_LOOPBACK) &&
187 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
188 strncmp(dev->name, "dummy", 5))) {
193 printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
194 if (!(dev->flags & IFF_NOARP))
196 able &= ic_proto_enabled;
197 if (ic_proto_enabled && !able)
200 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
201 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
204 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
214 get_random_bytes(&d->xid, sizeof(u32));
217 ic_proto_have_if |= able;
218 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
219 dev->name, able, d->xid));
227 if (user_dev_name[0])
228 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
230 printk(KERN_ERR "IP-Config: No network devices available.\n");
236 static void __init ic_close_devs(void)
238 struct ic_device *d, *next;
239 struct net_device *dev;
247 DBG(("IP-Config: Downing %s\n", dev->name));
248 dev_change_flags(dev, d->flags);
256 * Interface to various network functions.
260 set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
262 sin->sin_family = AF_INET;
263 sin->sin_addr.s_addr = addr;
264 sin->sin_port = port;
267 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
271 mm_segment_t oldfs = get_fs();
273 res = devinet_ioctl(cmd, arg);
278 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
282 mm_segment_t oldfs = get_fs();
284 res = ip_rt_ioctl(cmd, arg);
290 * Set up interface addresses and routes.
293 static int __init ic_setup_if(void)
296 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
299 memset(&ir, 0, sizeof(ir));
300 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
301 set_sockaddr(sin, ic_myaddr, 0);
302 if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
303 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
306 set_sockaddr(sin, ic_netmask, 0);
307 if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
308 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
311 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
312 if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
313 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
319 static int __init ic_setup_routes(void)
321 /* No need to setup device routes, only the default route... */
323 if (ic_gateway != INADDR_NONE) {
327 memset(&rm, 0, sizeof(rm));
328 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
329 printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
332 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
333 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
334 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
335 rm.rt_flags = RTF_UP | RTF_GATEWAY;
336 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
337 printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
346 * Fill in default values for all missing parameters.
349 static int __init ic_defaults(void)
352 * At this point we have no userspace running so need not
353 * claim locks on system_utsname
356 if (!ic_host_name_set)
357 sprintf(system_utsname.nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr));
359 if (root_server_addr == INADDR_NONE)
360 root_server_addr = ic_servaddr;
362 if (ic_netmask == INADDR_NONE) {
363 if (IN_CLASSA(ntohl(ic_myaddr)))
364 ic_netmask = htonl(IN_CLASSA_NET);
365 else if (IN_CLASSB(ntohl(ic_myaddr)))
366 ic_netmask = htonl(IN_CLASSB_NET);
367 else if (IN_CLASSC(ntohl(ic_myaddr)))
368 ic_netmask = htonl(IN_CLASSC_NET);
370 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
374 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));
386 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
388 static struct packet_type rarp_packet_type __initdata = {
389 type: __constant_htons(ETH_P_RARP),
393 static inline void ic_rarp_init(void)
395 dev_add_pack(&rarp_packet_type);
398 static inline void ic_rarp_cleanup(void)
400 dev_remove_pack(&rarp_packet_type);
404 * Process received RARP packet.
407 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
409 struct arphdr *rarp = (struct arphdr *)skb->h.raw;
410 unsigned char *rarp_ptr = (unsigned char *) (rarp + 1);
411 unsigned long sip, tip;
412 unsigned char *sha, *tha; /* s for "source", t for "target" */
415 /* One reply at a time, please. */
416 spin_lock(&ic_recv_lock);
418 /* If we already have a reply, just drop the packet */
422 /* Find the ic_device that the packet arrived on */
424 while (d && d->dev != dev)
427 goto drop; /* should never happen */
429 /* If this test doesn't pass, it's not IP, or we should ignore it anyway */
430 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
433 /* If it's not a RARP reply, delete it. */
434 if (rarp->ar_op != htons(ARPOP_RREPLY))
437 /* If it's not Ethernet, delete it. */
438 if (rarp->ar_pro != htons(ETH_P_IP))
441 /* Extract variable-width fields */
443 rarp_ptr += dev->addr_len;
444 memcpy(&sip, rarp_ptr, 4);
447 rarp_ptr += dev->addr_len;
448 memcpy(&tip, rarp_ptr, 4);
450 /* Discard packets which are not meant for us. */
451 if (memcmp(tha, dev->dev_addr, dev->addr_len))
454 /* Discard packets which are not from specified server. */
455 if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
458 /* We have a winner! */
460 if (ic_myaddr == INADDR_NONE)
463 ic_got_reply = IC_RARP;
466 /* Show's over. Nothing to see here. */
467 spin_unlock(&ic_recv_lock);
469 /* Throw the packet out. */
476 * Send RARP request packet over a single interface.
478 static void __init ic_rarp_send_if(struct ic_device *d)
480 struct net_device *dev = d->dev;
481 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
482 dev->dev_addr, dev->dev_addr);
487 * DHCP/BOOTP support.
490 #ifdef IPCONFIG_BOOTP
492 struct bootp_pkt { /* BOOTP packet format */
493 struct iphdr iph; /* IP header */
494 struct udphdr udph; /* UDP header */
495 u8 op; /* 1=request, 2=reply */
496 u8 htype; /* HW address type */
497 u8 hlen; /* HW address length */
498 u8 hops; /* Used only by gateways */
499 u32 xid; /* Transaction ID */
500 u16 secs; /* Seconds since we started */
501 u16 flags; /* Just what it says */
502 u32 client_ip; /* Client's IP address if known */
503 u32 your_ip; /* Assigned IP address */
504 u32 server_ip; /* (Next, e.g. NFS) Server's IP address */
505 u32 relay_ip; /* IP address of BOOTP relay */
506 u8 hw_addr[16]; /* Client's HW address */
507 u8 serv_name[64]; /* Server host name */
508 u8 boot_file[128]; /* Name of boot file */
509 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
513 #define BOOTP_REQUEST 1
514 #define BOOTP_REPLY 2
516 /* DHCP message types */
517 #define DHCPDISCOVER 1
519 #define DHCPREQUEST 3
520 #define DHCPDECLINE 4
523 #define DHCPRELEASE 7
526 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
528 static struct packet_type bootp_packet_type __initdata = {
529 type: __constant_htons(ETH_P_IP),
535 * Initialize DHCP/BOOTP extension fields in the request.
538 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
543 ic_dhcp_init_options(u8 *options)
545 u8 mt = ((ic_servaddr == INADDR_NONE)
546 ? DHCPDISCOVER : DHCPREQUEST);
549 #ifdef IPCONFIG_DEBUG
550 printk("DHCP: Sending message type %d\n", mt);
553 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
556 *e++ = 53; /* DHCP message type */
560 if (mt == DHCPREQUEST) {
561 *e++ = 54; /* Server ID (IP address) */
563 memcpy(e, &ic_servaddr, 4);
566 *e++ = 50; /* Requested IP address */
568 memcpy(e, &ic_myaddr, 4);
574 static const u8 ic_req_params[] = {
576 3, /* Default gateway */
579 15, /* Domain name */
581 40, /* NIS domain name */
584 *e++ = 55; /* Parameter request list */
585 *e++ = sizeof(ic_req_params);
586 memcpy(e, ic_req_params, sizeof(ic_req_params));
587 e += sizeof(ic_req_params);
590 *e++ = 255; /* End of the list */
593 #endif /* IPCONFIG_DHCP */
595 static void __init ic_bootp_init_ext(u8 *e)
597 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
599 *e++ = 1; /* Subnet mask request */
602 *e++ = 3; /* Default gateway request */
605 *e++ = 5; /* Name server reqeust */
608 *e++ = 12; /* Host name request */
611 *e++ = 40; /* NIS Domain name request */
614 *e++ = 17; /* Boot path */
618 *e++ = 57; /* set extension buffer size for reply */
620 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
623 *e++ = 255; /* End of the list */
628 * Initialize the DHCP/BOOTP mechanism.
630 static inline void ic_bootp_init(void)
634 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
635 ic_nameservers[i] = INADDR_NONE;
637 dev_add_pack(&bootp_packet_type);
642 * DHCP/BOOTP cleanup.
644 static inline void ic_bootp_cleanup(void)
646 dev_remove_pack(&bootp_packet_type);
651 * Send DHCP/BOOTP request to single interface.
653 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
655 struct net_device *dev = d->dev;
658 int hh_len = (dev->hard_header_len + 15) & ~15;
661 /* Allocate packet */
662 skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
665 skb_reserve(skb, hh_len);
666 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
667 memset(b, 0, sizeof(struct bootp_pkt));
669 /* Construct IP header */
670 skb->nh.iph = h = &b->iph;
673 h->tot_len = htons(sizeof(struct bootp_pkt));
674 h->frag_off = htons(IP_DF);
676 h->protocol = IPPROTO_UDP;
677 h->daddr = INADDR_BROADCAST;
678 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
680 /* Construct UDP header */
681 b->udph.source = htons(68);
682 b->udph.dest = htons(67);
683 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
684 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
686 /* Construct DHCP/BOOTP header */
687 b->op = BOOTP_REQUEST;
688 if (dev->type < 256) /* check for false types */
689 b->htype = dev->type;
690 else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
691 b->htype = ARPHRD_IEEE802;
692 else if (dev->type == ARPHRD_FDDI)
693 b->htype = ARPHRD_ETHER;
695 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
696 b->htype = dev->type; /* can cause undefined behavior */
698 b->hlen = dev->addr_len;
699 b->your_ip = INADDR_NONE;
700 b->server_ip = INADDR_NONE;
701 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
702 b->secs = htons(jiffies_diff / HZ);
705 /* add DHCP options or BOOTP extensions */
707 if (ic_proto_enabled & IC_USE_DHCP)
708 ic_dhcp_init_options(b->exten);
711 ic_bootp_init_ext(b->exten);
713 /* Chain packet down the line... */
715 skb->protocol = htons(ETH_P_IP);
716 if ((dev->hard_header &&
717 dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
718 dev_queue_xmit(skb) < 0)
724 * Copy BOOTP-supplied string if not already set.
726 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
732 memcpy(dest, src, len);
739 * Process BOOTP extensions.
741 static void __init ic_do_bootp_ext(u8 *ext)
746 #ifdef IPCONFIG_DEBUG
749 printk("DHCP/BOOTP: Got extension %d:",*ext);
750 for(c=ext+2; c<ext+2+ext[1]; c++)
756 case 1: /* Subnet mask */
757 if (ic_netmask == INADDR_NONE)
758 memcpy(&ic_netmask, ext+1, 4);
760 case 3: /* Default gateway */
761 if (ic_gateway == INADDR_NONE)
762 memcpy(&ic_gateway, ext+1, 4);
764 case 6: /* DNS server */
766 if (servers > CONF_NAMESERVERS_MAX)
767 servers = CONF_NAMESERVERS_MAX;
768 for (i = 0; i < servers; i++) {
769 if (ic_nameservers[i] == INADDR_NONE)
770 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
773 case 12: /* Host name */
774 ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
775 ic_host_name_set = 1;
777 case 15: /* Domain name (DNS) */
778 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
780 case 17: /* Root path */
781 if (!root_server_path[0])
782 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
784 case 40: /* NIS Domain name (_not_ DNS) */
785 ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
792 * Receive BOOTP reply.
794 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
796 struct bootp_pkt *b = (struct bootp_pkt *) skb->nh.iph;
797 struct iphdr *h = &b->iph;
801 /* One reply at a time, please. */
802 spin_lock(&ic_recv_lock);
804 /* If we already have a reply, just drop the packet */
808 /* Find the ic_device that the packet arrived on */
810 while (d && d->dev != dev)
813 goto drop; /* should never happen */
815 /* Check whether it's a BOOTP packet */
816 if (skb->pkt_type == PACKET_OTHERHOST ||
817 skb->len < sizeof(struct udphdr) + sizeof(struct iphdr) ||
820 ip_fast_csum((char *) h, h->ihl) != 0 ||
821 skb->len < ntohs(h->tot_len) ||
822 h->protocol != IPPROTO_UDP ||
823 b->udph.source != htons(67) ||
824 b->udph.dest != htons(68) ||
825 ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
828 /* Fragments are not supported */
829 if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
830 printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented reply.\n");
834 /* Is it a reply to our BOOTP request? */
835 len = ntohs(b->udph.len) - sizeof(struct udphdr);
836 if (len < 300 || /* See RFC 951:2.1 */
837 b->op != BOOTP_REPLY ||
843 /* Parse extensions */
844 if (!memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
845 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
849 if (ic_proto_enabled & IC_USE_DHCP) {
850 u32 server_id = INADDR_NONE;
854 while (ext < end && *ext != 0xff) {
856 if (*opt == 0) /* Padding */
862 case 53: /* Message type */
866 case 54: /* Server ID (IP address) */
868 memcpy(&server_id, opt + 2, 4);
873 #ifdef IPCONFIG_DEBUG
874 printk("DHCP: Got message type %d\n", mt);
879 /* While in the process of accepting one offer,
882 if (ic_myaddr != INADDR_NONE)
885 /* Let's accept that offer. */
886 ic_myaddr = b->your_ip;
887 ic_servaddr = server_id;
888 #ifdef IPCONFIG_DEBUG
889 printk("DHCP: Offered address %u.%u.%u.%u",
891 printk(" by server %u.%u.%u.%u\n",
892 NIPQUAD(ic_servaddr));
894 /* The DHCP indicated server address takes
895 * precedence over the bootp header one if
896 * they are different.
898 if ((server_id != INADDR_NONE) &&
899 (b->server_ip != server_id))
900 b->server_ip = ic_servaddr;
904 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
911 /* Urque. Forget it*/
912 ic_myaddr = INADDR_NONE;
913 ic_servaddr = INADDR_NONE;
917 ic_dhcp_msgtype = mt;
920 #endif /* IPCONFIG_DHCP */
923 while (ext < end && *ext != 0xff) {
925 if (*opt == 0) /* Padding */
929 ic_do_bootp_ext(opt);
933 /* We have a winner! */
935 ic_myaddr = b->your_ip;
936 ic_servaddr = b->server_ip;
937 if (ic_gateway == INADDR_NONE && b->relay_ip)
938 ic_gateway = b->relay_ip;
939 if (ic_nameservers[0] == INADDR_NONE)
940 ic_nameservers[0] = ic_servaddr;
941 ic_got_reply = IC_BOOTP;
944 /* Show's over. Nothing to see here. */
945 spin_unlock(&ic_recv_lock);
947 /* Throw the packet out. */
958 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
961 #ifdef IPCONFIG_DYNAMIC
963 static int __init ic_dynamic(void)
967 unsigned long start_jiffies, timeout, jiff;
968 int do_bootp = ic_proto_have_if & IC_BOOTP;
969 int do_rarp = ic_proto_have_if & IC_RARP;
972 * If none of DHCP/BOOTP/RARP was selected, return with an error.
973 * This routine gets only called when some pieces of information
974 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
976 if (!ic_proto_enabled) {
977 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
981 #ifdef IPCONFIG_BOOTP
982 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
983 printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
986 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
987 printk(KERN_ERR "RARP: No suitable device found.\n");
990 if (!ic_proto_have_if)
991 /* Error message already printed */
997 #ifdef IPCONFIG_BOOTP
1001 #ifdef IPCONFIG_RARP
1007 * Send requests and wait, until we get an answer. This loop
1008 * seems to be a terrible waste of CPU time, but actually there is
1009 * only one process running at all, so we don't need to use any
1010 * scheduler functions.
1011 * [Actually we could now, but the nothing else running note still
1014 printk(KERN_NOTICE "Sending %s%s%s requests .",
1016 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1017 (do_bootp && do_rarp) ? " and " : "",
1018 do_rarp ? "RARP" : "");
1020 start_jiffies = jiffies;
1022 retries = CONF_SEND_RETRIES;
1023 get_random_bytes(&timeout, sizeof(timeout));
1024 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
1026 #ifdef IPCONFIG_BOOTP
1027 if (do_bootp && (d->able & IC_BOOTP))
1028 ic_bootp_send_if(d, jiffies - start_jiffies);
1030 #ifdef IPCONFIG_RARP
1031 if (do_rarp && (d->able & IC_RARP))
1035 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1036 while (time_before(jiffies, jiff) && !ic_got_reply) {
1040 #ifdef IPCONFIG_DHCP
1041 /* DHCP isn't done until we get a DHCPACK. */
1042 if ((ic_got_reply & IC_BOOTP)
1043 && (ic_proto_enabled & IC_USE_DHCP)
1044 && ic_dhcp_msgtype != DHCPACK)
1050 #endif /* IPCONFIG_DHCP */
1061 printk(" timed out!\n");
1067 timeout = timeout CONF_TIMEOUT_MULT;
1068 if (timeout > CONF_TIMEOUT_MAX)
1069 timeout = CONF_TIMEOUT_MAX;
1074 #ifdef IPCONFIG_BOOTP
1078 #ifdef IPCONFIG_RARP
1086 printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
1087 ((ic_got_reply & IC_RARP) ? "RARP"
1088 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1089 NIPQUAD(ic_servaddr));
1090 printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
1095 #endif /* IPCONFIG_DYNAMIC */
1097 #ifdef CONFIG_PROC_FS
1099 static int pnp_get_info(char *buffer, char **start,
1100 off_t offset, int length)
1105 if (ic_proto_used & IC_PROTO)
1106 sprintf(buffer, "#PROTO: %s\n",
1107 (ic_proto_used & IC_RARP) ? "RARP"
1108 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1110 strcpy(buffer, "#MANUAL\n");
1111 len = strlen(buffer);
1114 len += sprintf(buffer + len,
1115 "domain %s\n", ic_domain);
1116 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1117 if (ic_nameservers[i] != INADDR_NONE)
1118 len += sprintf(buffer + len,
1119 "nameserver %u.%u.%u.%u\n",
1120 NIPQUAD(ic_nameservers[i]));
1122 if (ic_servaddr != INADDR_NONE)
1123 len += sprintf(buffer + len,
1124 "bootserver %u.%u.%u.%u\n",
1125 NIPQUAD(ic_servaddr));
1129 *start = buffer + offset;
1131 if (offset + length > len)
1132 length = len - offset;
1136 #endif /* CONFIG_PROC_FS */
1139 * Extract IP address from the parameter string if needed. Note that we
1140 * need to have root_server_addr set _before_ IPConfig gets called as it
1143 u32 __init root_nfs_parse_addr(char *name)
1150 while (octets < 4) {
1151 while (*cp >= '0' && *cp <= '9')
1153 if (cp == cq || cp - cq > 3)
1155 if (*cp == '.' || octets == 3)
1161 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1164 addr = in_aton(name);
1165 memmove(name, cp, strlen(cp) + 1);
1173 * IP Autoconfig dispatcher.
1176 static int __init ip_auto_config(void)
1181 #ifdef CONFIG_PROC_FS
1182 proc_net_create("pnp", 0, pnp_get_info);
1183 #endif /* CONFIG_PROC_FS */
1188 DBG(("IP-Config: Entered.\n"));
1190 #ifdef IPCONFIG_DYNAMIC
1193 /* Give hardware a chance to settle */
1194 jiff = jiffies + CONF_PRE_OPEN;
1195 while (time_before(jiffies, jiff))
1198 /* Setup all network devices */
1199 if (ic_open_devs() < 0)
1202 /* Give drivers a chance to settle */
1203 jiff = jiffies + CONF_POST_OPEN;
1204 while (time_before(jiffies, jiff))
1208 * If the config information is insufficient (e.g., our IP address or
1209 * IP address of the boot server is missing or we have multiple network
1210 * interfaces and no default was set), use BOOTP or RARP to get the
1213 if (ic_myaddr == INADDR_NONE ||
1214 #ifdef CONFIG_ROOT_NFS
1215 (MAJOR(ROOT_DEV) == UNNAMED_MAJOR
1216 && root_server_addr == INADDR_NONE
1217 && ic_servaddr == INADDR_NONE) ||
1219 ic_first_dev->next) {
1220 #ifdef IPCONFIG_DYNAMIC
1222 int retries = CONF_OPEN_RETRIES;
1224 if (ic_dynamic() < 0) {
1228 * I don't know why, but sometimes the
1229 * eepro100 driver (at least) gets upset and
1230 * doesn't work the first time it's opened.
1231 * But then if you close it and reopen it, it
1232 * works just fine. So we need to try that at
1233 * least once before giving up.
1235 * Also, if the root will be NFS-mounted, we
1236 * have nowhere to go if DHCP fails. So we
1237 * just have to keep trying forever.
1241 #ifdef CONFIG_ROOT_NFS
1242 if (ROOT_DEV == MKDEV(NFS_MAJOR, NFS_MINOR)) {
1244 "IP-Config: Retrying forever (NFS root)...\n");
1251 "IP-Config: Reopening network devices...\n");
1255 /* Oh, well. At least we tried. */
1256 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1259 #else /* !DYNAMIC */
1260 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1263 #endif /* IPCONFIG_DYNAMIC */
1265 /* Device selected manually or only one device -> use it */
1266 ic_dev = ic_first_dev->dev;
1269 addr = root_nfs_parse_addr(root_server_path);
1270 if (root_server_addr == INADDR_NONE)
1271 root_server_addr = addr;
1274 * Use defaults whereever applicable.
1276 if (ic_defaults() < 0)
1280 * Close all network devices except the device we've
1281 * autoconfigured and set up routes.
1284 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1288 * Record which protocol was actually used.
1290 #ifdef IPCONFIG_DYNAMIC
1291 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1294 #ifndef IPCONFIG_SILENT
1296 * Clue in the operator.
1298 printk("IP-Config: Complete:");
1299 printk("\n device=%s", ic_dev->name);
1300 printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr));
1301 printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask));
1302 printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway));
1303 printk(",\n host=%s, domain=%s, nis-domain=%s",
1304 system_utsname.nodename, ic_domain, system_utsname.domainname);
1305 printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr));
1306 printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr));
1307 printk(", rootpath=%s", root_server_path);
1309 #endif /* !SILENT */
1314 module_init(ip_auto_config);
1318 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1319 * command line parameter. It consists of option fields separated by colons in
1320 * the following order:
1322 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO>
1324 * Any of the fields can be empty which means to use a default value:
1325 * <client-ip> - address given by BOOTP or RARP
1326 * <server-ip> - address of host returning BOOTP or RARP packet
1327 * <gw-ip> - none, or the address returned by BOOTP
1328 * <netmask> - automatically determined from <client-ip>, or the
1329 * one returned by BOOTP
1330 * <host name> - <client-ip> in ASCII notation, or the name returned
1332 * <device> - use all available devices
1334 * off|none - don't do autoconfig at all (DEFAULT)
1335 * on|any - use any configured protocol
1336 * dhcp|bootp|rarp - use only the specified protocol
1337 * both - use both BOOTP and RARP (not DHCP)
1339 static int __init ic_proto_name(char *name)
1341 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1344 #ifdef CONFIG_IP_PNP_DHCP
1345 else if (!strcmp(name, "dhcp")) {
1346 ic_proto_enabled &= ~IC_RARP;
1350 #ifdef CONFIG_IP_PNP_BOOTP
1351 else if (!strcmp(name, "bootp")) {
1352 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1356 #ifdef CONFIG_IP_PNP_RARP
1357 else if (!strcmp(name, "rarp")) {
1358 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1362 #ifdef IPCONFIG_DYNAMIC
1363 else if (!strcmp(name, "both")) {
1364 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1371 static int __init ip_auto_config_setup(char *addrs)
1376 ic_set_manually = 1;
1378 ic_enable = (*addrs &&
1379 (strcmp(addrs, "off") != 0) &&
1380 (strcmp(addrs, "none") != 0));
1384 if (ic_proto_name(addrs))
1387 /* Parse the whole string */
1390 if ((cp = strchr(ip, ':')))
1392 if (strlen(ip) > 0) {
1393 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1396 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
1397 ic_myaddr = INADDR_NONE;
1400 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
1401 ic_servaddr = INADDR_NONE;
1404 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
1405 ic_gateway = INADDR_NONE;
1408 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
1409 ic_netmask = INADDR_NONE;
1412 if ((dp = strchr(ip, '.'))) {
1414 strncpy(system_utsname.domainname, dp, __NEW_UTS_LEN);
1415 system_utsname.domainname[__NEW_UTS_LEN] = '\0';
1417 strncpy(system_utsname.nodename, ip, __NEW_UTS_LEN);
1418 system_utsname.nodename[__NEW_UTS_LEN] = '\0';
1419 ic_host_name_set = 1;
1422 strncpy(user_dev_name, ip, IFNAMSIZ);
1423 user_dev_name[IFNAMSIZ-1] = '\0';
1437 static int __init nfsaddrs_config_setup(char *addrs)
1439 return ip_auto_config_setup(addrs);
1442 __setup("ip=", ip_auto_config_setup);
1443 __setup("nfsaddrs=", nfsaddrs_config_setup);