xref: /openbmc/linux/net/ipv4/ipconfig.c (revision a09d2831)
1 /*
2  *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
3  *  user-supplied information to configure own IP address and routes.
4  *
5  *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6  *
7  *  Derived from network configuration code in fs/nfs/nfsroot.c,
8  *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
9  *
10  *  BOOTP rewritten to construct and analyse packets itself instead
11  *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
12  *					     -- MJ, December 1998
13  *
14  *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
15  *  initialization scheme.
16  *	- Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
17  *
18  *  DHCP support added.  To users this looks like a whole separate
19  *  protocol, but we know it's just a bag on the side of BOOTP.
20  *		-- Chip Salzenberg <chip@valinux.com>, May 2000
21  *
22  *  Ported DHCP support from 2.2.16 to 2.4.0-test4
23  *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
24  *
25  *  Merged changes from 2.2.19 into 2.4.3
26  *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
27  *
28  *  Multiple Nameservers in /proc/net/pnp
29  *              --  Josef Siemes <jsiemes@web.de>, Aug 2002
30  */
31 
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/jiffies.h>
36 #include <linux/random.h>
37 #include <linux/init.h>
38 #include <linux/utsname.h>
39 #include <linux/in.h>
40 #include <linux/if.h>
41 #include <linux/inet.h>
42 #include <linux/inetdevice.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
46 #include <linux/ip.h>
47 #include <linux/socket.h>
48 #include <linux/route.h>
49 #include <linux/udp.h>
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
52 #include <linux/major.h>
53 #include <linux/root_dev.h>
54 #include <linux/delay.h>
55 #include <linux/nfs_fs.h>
56 #include <net/net_namespace.h>
57 #include <net/arp.h>
58 #include <net/ip.h>
59 #include <net/ipconfig.h>
60 #include <net/route.h>
61 
62 #include <asm/uaccess.h>
63 #include <net/checksum.h>
64 #include <asm/processor.h>
65 
66 /* Define this to allow debugging output */
67 #undef IPCONFIG_DEBUG
68 
69 #ifdef IPCONFIG_DEBUG
70 #define DBG(x) printk x
71 #else
72 #define DBG(x) do { } while(0)
73 #endif
74 
75 #if defined(CONFIG_IP_PNP_DHCP)
76 #define IPCONFIG_DHCP
77 #endif
78 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
79 #define IPCONFIG_BOOTP
80 #endif
81 #if defined(CONFIG_IP_PNP_RARP)
82 #define IPCONFIG_RARP
83 #endif
84 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
85 #define IPCONFIG_DYNAMIC
86 #endif
87 
88 /* Define the friendly delay before and after opening net devices */
89 #define CONF_PRE_OPEN		500	/* Before opening: 1/2 second */
90 #define CONF_POST_OPEN		1	/* After opening: 1 second */
91 
92 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
93 #define CONF_OPEN_RETRIES 	2	/* (Re)open devices twice */
94 #define CONF_SEND_RETRIES 	6	/* Send six requests per open */
95 #define CONF_INTER_TIMEOUT	(HZ/2)	/* Inter-device timeout: 1/2 second */
96 #define CONF_BASE_TIMEOUT	(HZ*2)	/* Initial timeout: 2 seconds */
97 #define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */
98 #define CONF_TIMEOUT_MULT	*7/4	/* Rate of timeout growth */
99 #define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */
100 #define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers
101 					   - '3' from resolv.h */
102 
103 #define NONE cpu_to_be32(INADDR_NONE)
104 #define ANY cpu_to_be32(INADDR_ANY)
105 
106 /*
107  * Public IP configuration
108  */
109 
110 /* This is used by platforms which might be able to set the ipconfig
111  * variables using firmware environment vars.  If this is set, it will
112  * ignore such firmware variables.
113  */
114 int ic_set_manually __initdata = 0;		/* IPconfig parameters set manually */
115 
116 static int ic_enable __initdata = 0;		/* IP config enabled? */
117 
118 /* Protocol choice */
119 int ic_proto_enabled __initdata = 0
120 #ifdef IPCONFIG_BOOTP
121 			| IC_BOOTP
122 #endif
123 #ifdef CONFIG_IP_PNP_DHCP
124 			| IC_USE_DHCP
125 #endif
126 #ifdef IPCONFIG_RARP
127 			| IC_RARP
128 #endif
129 			;
130 
131 static int ic_host_name_set __initdata = 0;	/* Host name set by us? */
132 
133 __be32 ic_myaddr = NONE;		/* My IP address */
134 static __be32 ic_netmask = NONE;	/* Netmask for local subnet */
135 __be32 ic_gateway = NONE;	/* Gateway IP address */
136 
137 __be32 ic_servaddr = NONE;	/* Boot server IP address */
138 
139 __be32 root_server_addr = NONE;	/* Address of NFS server */
140 u8 root_server_path[256] = { 0, };	/* Path to mount as root */
141 
142 u32 ic_dev_xid;		/* Device under configuration */
143 
144 /* vendor class identifier */
145 static char vendor_class_identifier[253] __initdata;
146 
147 /* Persistent data: */
148 
149 static int ic_proto_used;			/* Protocol used, if any */
150 static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
151 static u8 ic_domain[64];		/* DNS (not NIS) domain name */
152 
153 /*
154  * Private state.
155  */
156 
157 /* Name of user-selected boot device */
158 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
159 
160 /* Protocols supported by available interfaces */
161 static int ic_proto_have_if __initdata = 0;
162 
163 /* MTU for boot device */
164 static int ic_dev_mtu __initdata = 0;
165 
166 #ifdef IPCONFIG_DYNAMIC
167 static DEFINE_SPINLOCK(ic_recv_lock);
168 static volatile int ic_got_reply __initdata = 0;    /* Proto(s) that replied */
169 #endif
170 #ifdef IPCONFIG_DHCP
171 static int ic_dhcp_msgtype __initdata = 0;	/* DHCP msg type received */
172 #endif
173 
174 
175 /*
176  *	Network devices
177  */
178 
179 struct ic_device {
180 	struct ic_device *next;
181 	struct net_device *dev;
182 	unsigned short flags;
183 	short able;
184 	__be32 xid;
185 };
186 
187 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
188 static struct net_device *ic_dev __initdata = NULL;	/* Selected device */
189 
190 static int __init ic_open_devs(void)
191 {
192 	struct ic_device *d, **last;
193 	struct net_device *dev;
194 	unsigned short oflags;
195 
196 	last = &ic_first_dev;
197 	rtnl_lock();
198 
199 	/* bring loopback device up first */
200 	for_each_netdev(&init_net, dev) {
201 		if (!(dev->flags & IFF_LOOPBACK))
202 			continue;
203 		if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
204 			printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
205 	}
206 
207 	for_each_netdev(&init_net, dev) {
208 		if (dev->flags & IFF_LOOPBACK)
209 			continue;
210 		if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
211 		    (!(dev->flags & IFF_LOOPBACK) &&
212 		     (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
213 		     strncmp(dev->name, "dummy", 5))) {
214 			int able = 0;
215 			if (dev->mtu >= 364)
216 				able |= IC_BOOTP;
217 			else
218 				printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
219 			if (!(dev->flags & IFF_NOARP))
220 				able |= IC_RARP;
221 			able &= ic_proto_enabled;
222 			if (ic_proto_enabled && !able)
223 				continue;
224 			oflags = dev->flags;
225 			if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
226 				printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
227 				continue;
228 			}
229 			if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
230 				rtnl_unlock();
231 				return -1;
232 			}
233 			d->dev = dev;
234 			*last = d;
235 			last = &d->next;
236 			d->flags = oflags;
237 			d->able = able;
238 			if (able & IC_BOOTP)
239 				get_random_bytes(&d->xid, sizeof(__be32));
240 			else
241 				d->xid = 0;
242 			ic_proto_have_if |= able;
243 			DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
244 				dev->name, able, d->xid));
245 		}
246 	}
247 	rtnl_unlock();
248 
249 	*last = NULL;
250 
251 	if (!ic_first_dev) {
252 		if (user_dev_name[0])
253 			printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
254 		else
255 			printk(KERN_ERR "IP-Config: No network devices available.\n");
256 		return -1;
257 	}
258 	return 0;
259 }
260 
261 static void __init ic_close_devs(void)
262 {
263 	struct ic_device *d, *next;
264 	struct net_device *dev;
265 
266 	rtnl_lock();
267 	next = ic_first_dev;
268 	while ((d = next)) {
269 		next = d->next;
270 		dev = d->dev;
271 		if (dev != ic_dev) {
272 			DBG(("IP-Config: Downing %s\n", dev->name));
273 			dev_change_flags(dev, d->flags);
274 		}
275 		kfree(d);
276 	}
277 	rtnl_unlock();
278 }
279 
280 /*
281  *	Interface to various network functions.
282  */
283 
284 static inline void
285 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
286 {
287 	sin->sin_family = AF_INET;
288 	sin->sin_addr.s_addr = addr;
289 	sin->sin_port = port;
290 }
291 
292 static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
293 {
294 	int res;
295 
296 	mm_segment_t oldfs = get_fs();
297 	set_fs(get_ds());
298 	res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
299 	set_fs(oldfs);
300 	return res;
301 }
302 
303 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
304 {
305 	int res;
306 
307 	mm_segment_t oldfs = get_fs();
308 	set_fs(get_ds());
309 	res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
310 	set_fs(oldfs);
311 	return res;
312 }
313 
314 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
315 {
316 	int res;
317 
318 	mm_segment_t oldfs = get_fs();
319 	set_fs(get_ds());
320 	res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
321 	set_fs(oldfs);
322 	return res;
323 }
324 
325 /*
326  *	Set up interface addresses and routes.
327  */
328 
329 static int __init ic_setup_if(void)
330 {
331 	struct ifreq ir;
332 	struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
333 	int err;
334 
335 	memset(&ir, 0, sizeof(ir));
336 	strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
337 	set_sockaddr(sin, ic_myaddr, 0);
338 	if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
339 		printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
340 		return -1;
341 	}
342 	set_sockaddr(sin, ic_netmask, 0);
343 	if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
344 		printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
345 		return -1;
346 	}
347 	set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
348 	if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
349 		printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
350 		return -1;
351 	}
352 	/* Handle the case where we need non-standard MTU on the boot link (a network
353 	 * using jumbo frames, for instance).  If we can't set the mtu, don't error
354 	 * out, we'll try to muddle along.
355 	 */
356 	if (ic_dev_mtu != 0) {
357 		strcpy(ir.ifr_name, ic_dev->name);
358 		ir.ifr_mtu = ic_dev_mtu;
359 		if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
360 			printk(KERN_ERR "IP-Config: Unable to set interface mtu to %d (%d).\n",
361 			                 ic_dev_mtu, err);
362 	}
363 	return 0;
364 }
365 
366 static int __init ic_setup_routes(void)
367 {
368 	/* No need to setup device routes, only the default route... */
369 
370 	if (ic_gateway != NONE) {
371 		struct rtentry rm;
372 		int err;
373 
374 		memset(&rm, 0, sizeof(rm));
375 		if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
376 			printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
377 			return -1;
378 		}
379 		set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
380 		set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
381 		set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
382 		rm.rt_flags = RTF_UP | RTF_GATEWAY;
383 		if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
384 			printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
385 			return -1;
386 		}
387 	}
388 
389 	return 0;
390 }
391 
392 /*
393  *	Fill in default values for all missing parameters.
394  */
395 
396 static int __init ic_defaults(void)
397 {
398 	/*
399 	 *	At this point we have no userspace running so need not
400 	 *	claim locks on system_utsname
401 	 */
402 
403 	if (!ic_host_name_set)
404 		sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
405 
406 	if (root_server_addr == NONE)
407 		root_server_addr = ic_servaddr;
408 
409 	if (ic_netmask == NONE) {
410 		if (IN_CLASSA(ntohl(ic_myaddr)))
411 			ic_netmask = htonl(IN_CLASSA_NET);
412 		else if (IN_CLASSB(ntohl(ic_myaddr)))
413 			ic_netmask = htonl(IN_CLASSB_NET);
414 		else if (IN_CLASSC(ntohl(ic_myaddr)))
415 			ic_netmask = htonl(IN_CLASSC_NET);
416 		else {
417 			printk(KERN_ERR "IP-Config: Unable to guess netmask for address %pI4\n",
418 				&ic_myaddr);
419 			return -1;
420 		}
421 		printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
422 	}
423 
424 	return 0;
425 }
426 
427 /*
428  *	RARP support.
429  */
430 
431 #ifdef IPCONFIG_RARP
432 
433 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
434 
435 static struct packet_type rarp_packet_type __initdata = {
436 	.type =	cpu_to_be16(ETH_P_RARP),
437 	.func =	ic_rarp_recv,
438 };
439 
440 static inline void __init ic_rarp_init(void)
441 {
442 	dev_add_pack(&rarp_packet_type);
443 }
444 
445 static inline void __init ic_rarp_cleanup(void)
446 {
447 	dev_remove_pack(&rarp_packet_type);
448 }
449 
450 /*
451  *  Process received RARP packet.
452  */
453 static int __init
454 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
455 {
456 	struct arphdr *rarp;
457 	unsigned char *rarp_ptr;
458 	__be32 sip, tip;
459 	unsigned char *sha, *tha;		/* s for "source", t for "target" */
460 	struct ic_device *d;
461 
462 	if (!net_eq(dev_net(dev), &init_net))
463 		goto drop;
464 
465 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
466 		return NET_RX_DROP;
467 
468 	if (!pskb_may_pull(skb, sizeof(struct arphdr)))
469 		goto drop;
470 
471 	/* Basic sanity checks can be done without the lock.  */
472 	rarp = (struct arphdr *)skb_transport_header(skb);
473 
474 	/* If this test doesn't pass, it's not IP, or we should
475 	 * ignore it anyway.
476 	 */
477 	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
478 		goto drop;
479 
480 	/* If it's not a RARP reply, delete it. */
481 	if (rarp->ar_op != htons(ARPOP_RREPLY))
482 		goto drop;
483 
484 	/* If it's not Ethernet, delete it. */
485 	if (rarp->ar_pro != htons(ETH_P_IP))
486 		goto drop;
487 
488 	if (!pskb_may_pull(skb, arp_hdr_len(dev)))
489 		goto drop;
490 
491 	/* OK, it is all there and looks valid, process... */
492 	rarp = (struct arphdr *)skb_transport_header(skb);
493 	rarp_ptr = (unsigned char *) (rarp + 1);
494 
495 	/* One reply at a time, please. */
496 	spin_lock(&ic_recv_lock);
497 
498 	/* If we already have a reply, just drop the packet */
499 	if (ic_got_reply)
500 		goto drop_unlock;
501 
502 	/* Find the ic_device that the packet arrived on */
503 	d = ic_first_dev;
504 	while (d && d->dev != dev)
505 		d = d->next;
506 	if (!d)
507 		goto drop_unlock;	/* should never happen */
508 
509 	/* Extract variable-width fields */
510 	sha = rarp_ptr;
511 	rarp_ptr += dev->addr_len;
512 	memcpy(&sip, rarp_ptr, 4);
513 	rarp_ptr += 4;
514 	tha = rarp_ptr;
515 	rarp_ptr += dev->addr_len;
516 	memcpy(&tip, rarp_ptr, 4);
517 
518 	/* Discard packets which are not meant for us. */
519 	if (memcmp(tha, dev->dev_addr, dev->addr_len))
520 		goto drop_unlock;
521 
522 	/* Discard packets which are not from specified server. */
523 	if (ic_servaddr != NONE && ic_servaddr != sip)
524 		goto drop_unlock;
525 
526 	/* We have a winner! */
527 	ic_dev = dev;
528 	if (ic_myaddr == NONE)
529 		ic_myaddr = tip;
530 	ic_servaddr = sip;
531 	ic_got_reply = IC_RARP;
532 
533 drop_unlock:
534 	/* Show's over.  Nothing to see here.  */
535 	spin_unlock(&ic_recv_lock);
536 
537 drop:
538 	/* Throw the packet out. */
539 	kfree_skb(skb);
540 	return 0;
541 }
542 
543 
544 /*
545  *  Send RARP request packet over a single interface.
546  */
547 static void __init ic_rarp_send_if(struct ic_device *d)
548 {
549 	struct net_device *dev = d->dev;
550 	arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
551 		 dev->dev_addr, dev->dev_addr);
552 }
553 #endif
554 
555 /*
556  *	DHCP/BOOTP support.
557  */
558 
559 #ifdef IPCONFIG_BOOTP
560 
561 struct bootp_pkt {		/* BOOTP packet format */
562 	struct iphdr iph;	/* IP header */
563 	struct udphdr udph;	/* UDP header */
564 	u8 op;			/* 1=request, 2=reply */
565 	u8 htype;		/* HW address type */
566 	u8 hlen;		/* HW address length */
567 	u8 hops;		/* Used only by gateways */
568 	__be32 xid;		/* Transaction ID */
569 	__be16 secs;		/* Seconds since we started */
570 	__be16 flags;		/* Just what it says */
571 	__be32 client_ip;		/* Client's IP address if known */
572 	__be32 your_ip;		/* Assigned IP address */
573 	__be32 server_ip;		/* (Next, e.g. NFS) Server's IP address */
574 	__be32 relay_ip;		/* IP address of BOOTP relay */
575 	u8 hw_addr[16];		/* Client's HW address */
576 	u8 serv_name[64];	/* Server host name */
577 	u8 boot_file[128];	/* Name of boot file */
578 	u8 exten[312];		/* DHCP options / BOOTP vendor extensions */
579 };
580 
581 /* packet ops */
582 #define BOOTP_REQUEST	1
583 #define BOOTP_REPLY	2
584 
585 /* DHCP message types */
586 #define DHCPDISCOVER	1
587 #define DHCPOFFER	2
588 #define DHCPREQUEST	3
589 #define DHCPDECLINE	4
590 #define DHCPACK		5
591 #define DHCPNAK		6
592 #define DHCPRELEASE	7
593 #define DHCPINFORM	8
594 
595 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
596 
597 static struct packet_type bootp_packet_type __initdata = {
598 	.type =	cpu_to_be16(ETH_P_IP),
599 	.func =	ic_bootp_recv,
600 };
601 
602 
603 /*
604  *  Initialize DHCP/BOOTP extension fields in the request.
605  */
606 
607 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
608 
609 #ifdef IPCONFIG_DHCP
610 
611 static void __init
612 ic_dhcp_init_options(u8 *options)
613 {
614 	u8 mt = ((ic_servaddr == NONE)
615 		 ? DHCPDISCOVER : DHCPREQUEST);
616 	u8 *e = options;
617 	int len;
618 
619 #ifdef IPCONFIG_DEBUG
620 	printk("DHCP: Sending message type %d\n", mt);
621 #endif
622 
623 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
624 	e += 4;
625 
626 	*e++ = 53;		/* DHCP message type */
627 	*e++ = 1;
628 	*e++ = mt;
629 
630 	if (mt == DHCPREQUEST) {
631 		*e++ = 54;	/* Server ID (IP address) */
632 		*e++ = 4;
633 		memcpy(e, &ic_servaddr, 4);
634 		e += 4;
635 
636 		*e++ = 50;	/* Requested IP address */
637 		*e++ = 4;
638 		memcpy(e, &ic_myaddr, 4);
639 		e += 4;
640 	}
641 
642 	/* always? */
643 	{
644 		static const u8 ic_req_params[] = {
645 			1,	/* Subnet mask */
646 			3,	/* Default gateway */
647 			6,	/* DNS server */
648 			12,	/* Host name */
649 			15,	/* Domain name */
650 			17,	/* Boot path */
651 			26,	/* MTU */
652 			40,	/* NIS domain name */
653 		};
654 
655 		*e++ = 55;	/* Parameter request list */
656 		*e++ = sizeof(ic_req_params);
657 		memcpy(e, ic_req_params, sizeof(ic_req_params));
658 		e += sizeof(ic_req_params);
659 
660 		if (*vendor_class_identifier) {
661 			printk(KERN_INFO "DHCP: sending class identifier \"%s\"\n",
662 			       vendor_class_identifier);
663 			*e++ = 60;	/* Class-identifier */
664 			len = strlen(vendor_class_identifier);
665 			*e++ = len;
666 			memcpy(e, vendor_class_identifier, len);
667 			e += len;
668 		}
669 	}
670 
671 	*e++ = 255;	/* End of the list */
672 }
673 
674 #endif /* IPCONFIG_DHCP */
675 
676 static void __init ic_bootp_init_ext(u8 *e)
677 {
678 	memcpy(e, ic_bootp_cookie, 4);	/* RFC1048 Magic Cookie */
679 	e += 4;
680 	*e++ = 1;		/* Subnet mask request */
681 	*e++ = 4;
682 	e += 4;
683 	*e++ = 3;		/* Default gateway request */
684 	*e++ = 4;
685 	e += 4;
686 	*e++ = 5;		/* Name server request */
687 	*e++ = 8;
688 	e += 8;
689 	*e++ = 12;		/* Host name request */
690 	*e++ = 32;
691 	e += 32;
692 	*e++ = 40;		/* NIS Domain name request */
693 	*e++ = 32;
694 	e += 32;
695 	*e++ = 17;		/* Boot path */
696 	*e++ = 40;
697 	e += 40;
698 
699 	*e++ = 57;		/* set extension buffer size for reply */
700 	*e++ = 2;
701 	*e++ = 1;		/* 128+236+8+20+14, see dhcpd sources */
702 	*e++ = 150;
703 
704 	*e++ = 255;		/* End of the list */
705 }
706 
707 
708 /*
709  *  Initialize the DHCP/BOOTP mechanism.
710  */
711 static inline void __init ic_bootp_init(void)
712 {
713 	int i;
714 
715 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
716 		ic_nameservers[i] = NONE;
717 
718 	dev_add_pack(&bootp_packet_type);
719 }
720 
721 
722 /*
723  *  DHCP/BOOTP cleanup.
724  */
725 static inline void __init ic_bootp_cleanup(void)
726 {
727 	dev_remove_pack(&bootp_packet_type);
728 }
729 
730 
731 /*
732  *  Send DHCP/BOOTP request to single interface.
733  */
734 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
735 {
736 	struct net_device *dev = d->dev;
737 	struct sk_buff *skb;
738 	struct bootp_pkt *b;
739 	struct iphdr *h;
740 
741 	/* Allocate packet */
742 	skb = alloc_skb(sizeof(struct bootp_pkt) + LL_ALLOCATED_SPACE(dev) + 15,
743 			GFP_KERNEL);
744 	if (!skb)
745 		return;
746 	skb_reserve(skb, LL_RESERVED_SPACE(dev));
747 	b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
748 	memset(b, 0, sizeof(struct bootp_pkt));
749 
750 	/* Construct IP header */
751 	skb_reset_network_header(skb);
752 	h = ip_hdr(skb);
753 	h->version = 4;
754 	h->ihl = 5;
755 	h->tot_len = htons(sizeof(struct bootp_pkt));
756 	h->frag_off = htons(IP_DF);
757 	h->ttl = 64;
758 	h->protocol = IPPROTO_UDP;
759 	h->daddr = htonl(INADDR_BROADCAST);
760 	h->check = ip_fast_csum((unsigned char *) h, h->ihl);
761 
762 	/* Construct UDP header */
763 	b->udph.source = htons(68);
764 	b->udph.dest = htons(67);
765 	b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
766 	/* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
767 
768 	/* Construct DHCP/BOOTP header */
769 	b->op = BOOTP_REQUEST;
770 	if (dev->type < 256) /* check for false types */
771 		b->htype = dev->type;
772 	else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
773 		b->htype = ARPHRD_IEEE802;
774 	else if (dev->type == ARPHRD_FDDI)
775 		b->htype = ARPHRD_ETHER;
776 	else {
777 		printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
778 		b->htype = dev->type; /* can cause undefined behavior */
779 	}
780 
781 	/* server_ip and your_ip address are both already zero per RFC2131 */
782 	b->hlen = dev->addr_len;
783 	memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
784 	b->secs = htons(jiffies_diff / HZ);
785 	b->xid = d->xid;
786 
787 	/* add DHCP options or BOOTP extensions */
788 #ifdef IPCONFIG_DHCP
789 	if (ic_proto_enabled & IC_USE_DHCP)
790 		ic_dhcp_init_options(b->exten);
791 	else
792 #endif
793 		ic_bootp_init_ext(b->exten);
794 
795 	/* Chain packet down the line... */
796 	skb->dev = dev;
797 	skb->protocol = htons(ETH_P_IP);
798 	if (dev_hard_header(skb, dev, ntohs(skb->protocol),
799 			    dev->broadcast, dev->dev_addr, skb->len) < 0 ||
800 	    dev_queue_xmit(skb) < 0)
801 		printk("E");
802 }
803 
804 
805 /*
806  *  Copy BOOTP-supplied string if not already set.
807  */
808 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
809 {
810 	if (!len)
811 		return 0;
812 	if (len > max-1)
813 		len = max-1;
814 	memcpy(dest, src, len);
815 	dest[len] = '\0';
816 	return 1;
817 }
818 
819 
820 /*
821  *  Process BOOTP extensions.
822  */
823 static void __init ic_do_bootp_ext(u8 *ext)
824 {
825        u8 servers;
826        int i;
827 	u16 mtu;
828 
829 #ifdef IPCONFIG_DEBUG
830 	u8 *c;
831 
832 	printk("DHCP/BOOTP: Got extension %d:",*ext);
833 	for (c=ext+2; c<ext+2+ext[1]; c++)
834 		printk(" %02x", *c);
835 	printk("\n");
836 #endif
837 
838 	switch (*ext++) {
839 		case 1:		/* Subnet mask */
840 			if (ic_netmask == NONE)
841 				memcpy(&ic_netmask, ext+1, 4);
842 			break;
843 		case 3:		/* Default gateway */
844 			if (ic_gateway == NONE)
845 				memcpy(&ic_gateway, ext+1, 4);
846 			break;
847 		case 6:		/* DNS server */
848 			servers= *ext/4;
849 			if (servers > CONF_NAMESERVERS_MAX)
850 				servers = CONF_NAMESERVERS_MAX;
851 			for (i = 0; i < servers; i++) {
852 				if (ic_nameservers[i] == NONE)
853 					memcpy(&ic_nameservers[i], ext+1+4*i, 4);
854 			}
855 			break;
856 		case 12:	/* Host name */
857 			ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
858 			ic_host_name_set = 1;
859 			break;
860 		case 15:	/* Domain name (DNS) */
861 			ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
862 			break;
863 		case 17:	/* Root path */
864 			if (!root_server_path[0])
865 				ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
866 			break;
867 		case 26:	/* Interface MTU */
868 			memcpy(&mtu, ext+1, sizeof(mtu));
869 			ic_dev_mtu = ntohs(mtu);
870 			break;
871 		case 40:	/* NIS Domain name (_not_ DNS) */
872 			ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
873 			break;
874 	}
875 }
876 
877 
878 /*
879  *  Receive BOOTP reply.
880  */
881 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
882 {
883 	struct bootp_pkt *b;
884 	struct iphdr *h;
885 	struct ic_device *d;
886 	int len, ext_len;
887 
888 	if (!net_eq(dev_net(dev), &init_net))
889 		goto drop;
890 
891 	/* Perform verifications before taking the lock.  */
892 	if (skb->pkt_type == PACKET_OTHERHOST)
893 		goto drop;
894 
895 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
896 		return NET_RX_DROP;
897 
898 	if (!pskb_may_pull(skb,
899 			   sizeof(struct iphdr) +
900 			   sizeof(struct udphdr)))
901 		goto drop;
902 
903 	b = (struct bootp_pkt *)skb_network_header(skb);
904 	h = &b->iph;
905 
906 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
907 		goto drop;
908 
909 	/* Fragments are not supported */
910 	if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
911 		if (net_ratelimit())
912 			printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
913 			       "reply.\n");
914 		goto drop;
915 	}
916 
917 	if (skb->len < ntohs(h->tot_len))
918 		goto drop;
919 
920 	if (ip_fast_csum((char *) h, h->ihl))
921 		goto drop;
922 
923 	if (b->udph.source != htons(67) || b->udph.dest != htons(68))
924 		goto drop;
925 
926 	if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
927 		goto drop;
928 
929 	len = ntohs(b->udph.len) - sizeof(struct udphdr);
930 	ext_len = len - (sizeof(*b) -
931 			 sizeof(struct iphdr) -
932 			 sizeof(struct udphdr) -
933 			 sizeof(b->exten));
934 	if (ext_len < 0)
935 		goto drop;
936 
937 	/* Ok the front looks good, make sure we can get at the rest.  */
938 	if (!pskb_may_pull(skb, skb->len))
939 		goto drop;
940 
941 	b = (struct bootp_pkt *)skb_network_header(skb);
942 	h = &b->iph;
943 
944 	/* One reply at a time, please. */
945 	spin_lock(&ic_recv_lock);
946 
947 	/* If we already have a reply, just drop the packet */
948 	if (ic_got_reply)
949 		goto drop_unlock;
950 
951 	/* Find the ic_device that the packet arrived on */
952 	d = ic_first_dev;
953 	while (d && d->dev != dev)
954 		d = d->next;
955 	if (!d)
956 		goto drop_unlock;  /* should never happen */
957 
958 	/* Is it a reply to our BOOTP request? */
959 	if (b->op != BOOTP_REPLY ||
960 	    b->xid != d->xid) {
961 		if (net_ratelimit())
962 			printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
963 			       "op[%x] xid[%x]\n",
964 			       b->op, b->xid);
965 		goto drop_unlock;
966 	}
967 
968 	/* Is it a reply for the device we are configuring? */
969 	if (b->xid != ic_dev_xid) {
970 		if (net_ratelimit())
971 			printk(KERN_ERR "DHCP/BOOTP: Ignoring delayed packet \n");
972 		goto drop_unlock;
973 	}
974 
975 	/* Parse extensions */
976 	if (ext_len >= 4 &&
977 	    !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
978 		u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
979 		u8 *ext;
980 
981 #ifdef IPCONFIG_DHCP
982 		if (ic_proto_enabled & IC_USE_DHCP) {
983 			__be32 server_id = NONE;
984 			int mt = 0;
985 
986 			ext = &b->exten[4];
987 			while (ext < end && *ext != 0xff) {
988 				u8 *opt = ext++;
989 				if (*opt == 0)	/* Padding */
990 					continue;
991 				ext += *ext + 1;
992 				if (ext >= end)
993 					break;
994 				switch (*opt) {
995 				case 53:	/* Message type */
996 					if (opt[1])
997 						mt = opt[2];
998 					break;
999 				case 54:	/* Server ID (IP address) */
1000 					if (opt[1] >= 4)
1001 						memcpy(&server_id, opt + 2, 4);
1002 					break;
1003 				}
1004 			}
1005 
1006 #ifdef IPCONFIG_DEBUG
1007 			printk("DHCP: Got message type %d\n", mt);
1008 #endif
1009 
1010 			switch (mt) {
1011 			case DHCPOFFER:
1012 				/* While in the process of accepting one offer,
1013 				 * ignore all others.
1014 				 */
1015 				if (ic_myaddr != NONE)
1016 					goto drop_unlock;
1017 
1018 				/* Let's accept that offer. */
1019 				ic_myaddr = b->your_ip;
1020 				ic_servaddr = server_id;
1021 #ifdef IPCONFIG_DEBUG
1022 				printk("DHCP: Offered address %pI4 by server %pI4\n",
1023 				       &ic_myaddr, &ic_servaddr);
1024 #endif
1025 				/* The DHCP indicated server address takes
1026 				 * precedence over the bootp header one if
1027 				 * they are different.
1028 				 */
1029 				if ((server_id != NONE) &&
1030 				    (b->server_ip != server_id))
1031 					b->server_ip = ic_servaddr;
1032 				break;
1033 
1034 			case DHCPACK:
1035 				if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1036 					goto drop_unlock;
1037 
1038 				/* Yeah! */
1039 				break;
1040 
1041 			default:
1042 				/* Urque.  Forget it*/
1043 				ic_myaddr = NONE;
1044 				ic_servaddr = NONE;
1045 				goto drop_unlock;
1046 			}
1047 
1048 			ic_dhcp_msgtype = mt;
1049 
1050 		}
1051 #endif /* IPCONFIG_DHCP */
1052 
1053 		ext = &b->exten[4];
1054 		while (ext < end && *ext != 0xff) {
1055 			u8 *opt = ext++;
1056 			if (*opt == 0)	/* Padding */
1057 				continue;
1058 			ext += *ext + 1;
1059 			if (ext < end)
1060 				ic_do_bootp_ext(opt);
1061 		}
1062 	}
1063 
1064 	/* We have a winner! */
1065 	ic_dev = dev;
1066 	ic_myaddr = b->your_ip;
1067 	ic_servaddr = b->server_ip;
1068 	if (ic_gateway == NONE && b->relay_ip)
1069 		ic_gateway = b->relay_ip;
1070 	if (ic_nameservers[0] == NONE)
1071 		ic_nameservers[0] = ic_servaddr;
1072 	ic_got_reply = IC_BOOTP;
1073 
1074 drop_unlock:
1075 	/* Show's over.  Nothing to see here.  */
1076 	spin_unlock(&ic_recv_lock);
1077 
1078 drop:
1079 	/* Throw the packet out. */
1080 	kfree_skb(skb);
1081 
1082 	return 0;
1083 }
1084 
1085 
1086 #endif
1087 
1088 
1089 /*
1090  *	Dynamic IP configuration -- DHCP, BOOTP, RARP.
1091  */
1092 
1093 #ifdef IPCONFIG_DYNAMIC
1094 
1095 static int __init ic_dynamic(void)
1096 {
1097 	int retries;
1098 	struct ic_device *d;
1099 	unsigned long start_jiffies, timeout, jiff;
1100 	int do_bootp = ic_proto_have_if & IC_BOOTP;
1101 	int do_rarp = ic_proto_have_if & IC_RARP;
1102 
1103 	/*
1104 	 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1105 	 * This routine gets only called when some pieces of information
1106 	 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1107 	 */
1108 	if (!ic_proto_enabled) {
1109 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1110 		return -1;
1111 	}
1112 
1113 #ifdef IPCONFIG_BOOTP
1114 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1115 		printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
1116 #endif
1117 #ifdef IPCONFIG_RARP
1118 	if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1119 		printk(KERN_ERR "RARP: No suitable device found.\n");
1120 #endif
1121 
1122 	if (!ic_proto_have_if)
1123 		/* Error message already printed */
1124 		return -1;
1125 
1126 	/*
1127 	 * Setup protocols
1128 	 */
1129 #ifdef IPCONFIG_BOOTP
1130 	if (do_bootp)
1131 		ic_bootp_init();
1132 #endif
1133 #ifdef IPCONFIG_RARP
1134 	if (do_rarp)
1135 		ic_rarp_init();
1136 #endif
1137 
1138 	/*
1139 	 * Send requests and wait, until we get an answer. This loop
1140 	 * seems to be a terrible waste of CPU time, but actually there is
1141 	 * only one process running at all, so we don't need to use any
1142 	 * scheduler functions.
1143 	 * [Actually we could now, but the nothing else running note still
1144 	 *  applies.. - AC]
1145 	 */
1146 	printk(KERN_NOTICE "Sending %s%s%s requests .",
1147 	       do_bootp
1148 		? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1149 	       (do_bootp && do_rarp) ? " and " : "",
1150 	       do_rarp ? "RARP" : "");
1151 
1152 	start_jiffies = jiffies;
1153 	d = ic_first_dev;
1154 	retries = CONF_SEND_RETRIES;
1155 	get_random_bytes(&timeout, sizeof(timeout));
1156 	timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
1157 	for (;;) {
1158 		/* Track the device we are configuring */
1159 		ic_dev_xid = d->xid;
1160 
1161 #ifdef IPCONFIG_BOOTP
1162 		if (do_bootp && (d->able & IC_BOOTP))
1163 			ic_bootp_send_if(d, jiffies - start_jiffies);
1164 #endif
1165 #ifdef IPCONFIG_RARP
1166 		if (do_rarp && (d->able & IC_RARP))
1167 			ic_rarp_send_if(d);
1168 #endif
1169 
1170 		jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1171 		while (time_before(jiffies, jiff) && !ic_got_reply)
1172 			schedule_timeout_uninterruptible(1);
1173 #ifdef IPCONFIG_DHCP
1174 		/* DHCP isn't done until we get a DHCPACK. */
1175 		if ((ic_got_reply & IC_BOOTP) &&
1176 		    (ic_proto_enabled & IC_USE_DHCP) &&
1177 		    ic_dhcp_msgtype != DHCPACK) {
1178 			ic_got_reply = 0;
1179 			printk(",");
1180 			continue;
1181 		}
1182 #endif /* IPCONFIG_DHCP */
1183 
1184 		if (ic_got_reply) {
1185 			printk(" OK\n");
1186 			break;
1187 		}
1188 
1189 		if ((d = d->next))
1190 			continue;
1191 
1192 		if (! --retries) {
1193 			printk(" timed out!\n");
1194 			break;
1195 		}
1196 
1197 		d = ic_first_dev;
1198 
1199 		timeout = timeout CONF_TIMEOUT_MULT;
1200 		if (timeout > CONF_TIMEOUT_MAX)
1201 			timeout = CONF_TIMEOUT_MAX;
1202 
1203 		printk(".");
1204 	}
1205 
1206 #ifdef IPCONFIG_BOOTP
1207 	if (do_bootp)
1208 		ic_bootp_cleanup();
1209 #endif
1210 #ifdef IPCONFIG_RARP
1211 	if (do_rarp)
1212 		ic_rarp_cleanup();
1213 #endif
1214 
1215 	if (!ic_got_reply) {
1216 		ic_myaddr = NONE;
1217 		return -1;
1218 	}
1219 
1220 	printk("IP-Config: Got %s answer from %pI4, ",
1221 		((ic_got_reply & IC_RARP) ? "RARP"
1222 		 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1223 		&ic_servaddr);
1224 	printk("my address is %pI4\n", &ic_myaddr);
1225 
1226 	return 0;
1227 }
1228 
1229 #endif /* IPCONFIG_DYNAMIC */
1230 
1231 #ifdef CONFIG_PROC_FS
1232 
1233 static int pnp_seq_show(struct seq_file *seq, void *v)
1234 {
1235 	int i;
1236 
1237 	if (ic_proto_used & IC_PROTO)
1238 		seq_printf(seq, "#PROTO: %s\n",
1239 			   (ic_proto_used & IC_RARP) ? "RARP"
1240 			   : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1241 	else
1242 		seq_puts(seq, "#MANUAL\n");
1243 
1244 	if (ic_domain[0])
1245 		seq_printf(seq,
1246 			   "domain %s\n", ic_domain);
1247 	for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1248 		if (ic_nameservers[i] != NONE)
1249 			seq_printf(seq, "nameserver %pI4\n",
1250 				   &ic_nameservers[i]);
1251 	}
1252 	if (ic_servaddr != NONE)
1253 		seq_printf(seq, "bootserver %pI4\n",
1254 			   &ic_servaddr);
1255 	return 0;
1256 }
1257 
1258 static int pnp_seq_open(struct inode *indoe, struct file *file)
1259 {
1260 	return single_open(file, pnp_seq_show, NULL);
1261 }
1262 
1263 static const struct file_operations pnp_seq_fops = {
1264 	.owner		= THIS_MODULE,
1265 	.open		= pnp_seq_open,
1266 	.read		= seq_read,
1267 	.llseek		= seq_lseek,
1268 	.release	= single_release,
1269 };
1270 #endif /* CONFIG_PROC_FS */
1271 
1272 /*
1273  *  Extract IP address from the parameter string if needed. Note that we
1274  *  need to have root_server_addr set _before_ IPConfig gets called as it
1275  *  can override it.
1276  */
1277 __be32 __init root_nfs_parse_addr(char *name)
1278 {
1279 	__be32 addr;
1280 	int octets = 0;
1281 	char *cp, *cq;
1282 
1283 	cp = cq = name;
1284 	while (octets < 4) {
1285 		while (*cp >= '0' && *cp <= '9')
1286 			cp++;
1287 		if (cp == cq || cp - cq > 3)
1288 			break;
1289 		if (*cp == '.' || octets == 3)
1290 			octets++;
1291 		if (octets < 4)
1292 			cp++;
1293 		cq = cp;
1294 	}
1295 	if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1296 		if (*cp == ':')
1297 			*cp++ = '\0';
1298 		addr = in_aton(name);
1299 		memmove(name, cp, strlen(cp) + 1);
1300 	} else
1301 		addr = NONE;
1302 
1303 	return addr;
1304 }
1305 
1306 /*
1307  *	IP Autoconfig dispatcher.
1308  */
1309 
1310 static int __init ip_auto_config(void)
1311 {
1312 	__be32 addr;
1313 #ifdef IPCONFIG_DYNAMIC
1314 	int retries = CONF_OPEN_RETRIES;
1315 #endif
1316 
1317 #ifdef CONFIG_PROC_FS
1318 	proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
1319 #endif /* CONFIG_PROC_FS */
1320 
1321 	if (!ic_enable)
1322 		return 0;
1323 
1324 	DBG(("IP-Config: Entered.\n"));
1325 #ifdef IPCONFIG_DYNAMIC
1326  try_try_again:
1327 #endif
1328 	/* Give hardware a chance to settle */
1329 	msleep(CONF_PRE_OPEN);
1330 
1331 	/* Setup all network devices */
1332 	if (ic_open_devs() < 0)
1333 		return -1;
1334 
1335 	/* Give drivers a chance to settle */
1336 	ssleep(CONF_POST_OPEN);
1337 
1338 	/*
1339 	 * If the config information is insufficient (e.g., our IP address or
1340 	 * IP address of the boot server is missing or we have multiple network
1341 	 * interfaces and no default was set), use BOOTP or RARP to get the
1342 	 * missing values.
1343 	 */
1344 	if (ic_myaddr == NONE ||
1345 #ifdef CONFIG_ROOT_NFS
1346 	    (root_server_addr == NONE &&
1347 	     ic_servaddr == NONE &&
1348 	     ROOT_DEV == Root_NFS) ||
1349 #endif
1350 	    ic_first_dev->next) {
1351 #ifdef IPCONFIG_DYNAMIC
1352 		if (ic_dynamic() < 0) {
1353 			ic_close_devs();
1354 
1355 			/*
1356 			 * I don't know why, but sometimes the
1357 			 * eepro100 driver (at least) gets upset and
1358 			 * doesn't work the first time it's opened.
1359 			 * But then if you close it and reopen it, it
1360 			 * works just fine.  So we need to try that at
1361 			 * least once before giving up.
1362 			 *
1363 			 * Also, if the root will be NFS-mounted, we
1364 			 * have nowhere to go if DHCP fails.  So we
1365 			 * just have to keep trying forever.
1366 			 *
1367 			 * 				-- Chip
1368 			 */
1369 #ifdef CONFIG_ROOT_NFS
1370 			if (ROOT_DEV ==  Root_NFS) {
1371 				printk(KERN_ERR
1372 					"IP-Config: Retrying forever (NFS root)...\n");
1373 				goto try_try_again;
1374 			}
1375 #endif
1376 
1377 			if (--retries) {
1378 				printk(KERN_ERR
1379 				       "IP-Config: Reopening network devices...\n");
1380 				goto try_try_again;
1381 			}
1382 
1383 			/* Oh, well.  At least we tried. */
1384 			printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1385 			return -1;
1386 		}
1387 #else /* !DYNAMIC */
1388 		printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1389 		ic_close_devs();
1390 		return -1;
1391 #endif /* IPCONFIG_DYNAMIC */
1392 	} else {
1393 		/* Device selected manually or only one device -> use it */
1394 		ic_dev = ic_first_dev->dev;
1395 	}
1396 
1397 	addr = root_nfs_parse_addr(root_server_path);
1398 	if (root_server_addr == NONE)
1399 		root_server_addr = addr;
1400 
1401 	/*
1402 	 * Use defaults whereever applicable.
1403 	 */
1404 	if (ic_defaults() < 0)
1405 		return -1;
1406 
1407 	/*
1408 	 * Close all network devices except the device we've
1409 	 * autoconfigured and set up routes.
1410 	 */
1411 	ic_close_devs();
1412 	if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1413 		return -1;
1414 
1415 	/*
1416 	 * Record which protocol was actually used.
1417 	 */
1418 #ifdef IPCONFIG_DYNAMIC
1419 	ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1420 #endif
1421 
1422 #ifndef IPCONFIG_SILENT
1423 	/*
1424 	 * Clue in the operator.
1425 	 */
1426 	printk("IP-Config: Complete:");
1427 	printk("\n     device=%s", ic_dev->name);
1428 	printk(", addr=%pI4", &ic_myaddr);
1429 	printk(", mask=%pI4", &ic_netmask);
1430 	printk(", gw=%pI4", &ic_gateway);
1431 	printk(",\n     host=%s, domain=%s, nis-domain=%s",
1432 	       utsname()->nodename, ic_domain, utsname()->domainname);
1433 	printk(",\n     bootserver=%pI4", &ic_servaddr);
1434 	printk(", rootserver=%pI4", &root_server_addr);
1435 	printk(", rootpath=%s", root_server_path);
1436 	if (ic_dev_mtu)
1437 		printk(", mtu=%d", ic_dev_mtu);
1438 	printk("\n");
1439 #endif /* !SILENT */
1440 
1441 	return 0;
1442 }
1443 
1444 late_initcall(ip_auto_config);
1445 
1446 
1447 /*
1448  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1449  *  command line parameter.  See Documentation/filesystems/nfs/nfsroot.txt.
1450  */
1451 static int __init ic_proto_name(char *name)
1452 {
1453 	if (!strcmp(name, "on") || !strcmp(name, "any")) {
1454 		return 1;
1455 	}
1456 	if (!strcmp(name, "off") || !strcmp(name, "none")) {
1457 		return 0;
1458 	}
1459 #ifdef CONFIG_IP_PNP_DHCP
1460 	else if (!strcmp(name, "dhcp")) {
1461 		ic_proto_enabled &= ~IC_RARP;
1462 		return 1;
1463 	}
1464 #endif
1465 #ifdef CONFIG_IP_PNP_BOOTP
1466 	else if (!strcmp(name, "bootp")) {
1467 		ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1468 		return 1;
1469 	}
1470 #endif
1471 #ifdef CONFIG_IP_PNP_RARP
1472 	else if (!strcmp(name, "rarp")) {
1473 		ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1474 		return 1;
1475 	}
1476 #endif
1477 #ifdef IPCONFIG_DYNAMIC
1478 	else if (!strcmp(name, "both")) {
1479 		ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1480 		return 1;
1481 	}
1482 #endif
1483 	return 0;
1484 }
1485 
1486 static int __init ip_auto_config_setup(char *addrs)
1487 {
1488 	char *cp, *ip, *dp;
1489 	int num = 0;
1490 
1491 	ic_set_manually = 1;
1492 	ic_enable = 1;
1493 
1494 	/*
1495 	 * If any dhcp, bootp etc options are set, leave autoconfig on
1496 	 * and skip the below static IP processing.
1497 	 */
1498 	if (ic_proto_name(addrs))
1499 		return 1;
1500 
1501 	/* If no static IP is given, turn off autoconfig and bail.  */
1502 	if (*addrs == 0 ||
1503 	    strcmp(addrs, "off") == 0 ||
1504 	    strcmp(addrs, "none") == 0) {
1505 		ic_enable = 0;
1506 		return 1;
1507 	}
1508 
1509 	/* Parse string for static IP assignment.  */
1510 	ip = addrs;
1511 	while (ip && *ip) {
1512 		if ((cp = strchr(ip, ':')))
1513 			*cp++ = '\0';
1514 		if (strlen(ip) > 0) {
1515 			DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1516 			switch (num) {
1517 			case 0:
1518 				if ((ic_myaddr = in_aton(ip)) == ANY)
1519 					ic_myaddr = NONE;
1520 				break;
1521 			case 1:
1522 				if ((ic_servaddr = in_aton(ip)) == ANY)
1523 					ic_servaddr = NONE;
1524 				break;
1525 			case 2:
1526 				if ((ic_gateway = in_aton(ip)) == ANY)
1527 					ic_gateway = NONE;
1528 				break;
1529 			case 3:
1530 				if ((ic_netmask = in_aton(ip)) == ANY)
1531 					ic_netmask = NONE;
1532 				break;
1533 			case 4:
1534 				if ((dp = strchr(ip, '.'))) {
1535 					*dp++ = '\0';
1536 					strlcpy(utsname()->domainname, dp,
1537 						sizeof(utsname()->domainname));
1538 				}
1539 				strlcpy(utsname()->nodename, ip,
1540 					sizeof(utsname()->nodename));
1541 				ic_host_name_set = 1;
1542 				break;
1543 			case 5:
1544 				strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1545 				break;
1546 			case 6:
1547 				if (ic_proto_name(ip) == 0 &&
1548 				    ic_myaddr == NONE) {
1549 					ic_enable = 0;
1550 				}
1551 				break;
1552 			}
1553 		}
1554 		ip = cp;
1555 		num++;
1556 	}
1557 
1558 	return 1;
1559 }
1560 
1561 static int __init nfsaddrs_config_setup(char *addrs)
1562 {
1563 	return ip_auto_config_setup(addrs);
1564 }
1565 
1566 static int __init vendor_class_identifier_setup(char *addrs)
1567 {
1568 	if (strlcpy(vendor_class_identifier, addrs,
1569 		    sizeof(vendor_class_identifier))
1570 	    >= sizeof(vendor_class_identifier))
1571 		printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
1572 		       vendor_class_identifier);
1573 	return 1;
1574 }
1575 
1576 __setup("ip=", ip_auto_config_setup);
1577 __setup("nfsaddrs=", nfsaddrs_config_setup);
1578 __setup("dhcpclass=", vendor_class_identifier_setup);
1579