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