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