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