ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / ipv4 / ipconfig.c
blob60f564db25a3d557ff0cb2c2be2d3a5b1568999a
1 /*
2 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
3 * user-supplied information to configure own IP address and routes.
5 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 * Derived from network configuration code in fs/nfs/nfsroot.c,
8 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
10 * BOOTP rewritten to construct and analyse packets itself instead
11 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
12 * -- MJ, December 1998
14 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
15 * initialization scheme.
16 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
18 * DHCP support added. To users this looks like a whole separate
19 * protocol, but we know it's just a bag on the side of BOOTP.
20 * -- Chip Salzenberg <chip@valinux.com>, May 2000
22 * Ported DHCP support from 2.2.16 to 2.4.0-test4
23 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
25 * Merged changes from 2.2.19 into 2.4.3
26 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
28 * Multiple Nameservers in /proc/net/pnp
29 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/jiffies.h>
36 #include <linux/random.h>
37 #include <linux/init.h>
38 #include <linux/utsname.h>
39 #include <linux/in.h>
40 #include <linux/if.h>
41 #include <linux/inet.h>
42 #include <linux/inetdevice.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
46 #include <linux/ip.h>
47 #include <linux/socket.h>
48 #include <linux/route.h>
49 #include <linux/udp.h>
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
52 #include <linux/major.h>
53 #include <linux/root_dev.h>
54 #include <linux/delay.h>
55 #include <linux/nfs_fs.h>
56 #include <linux/slab.h>
57 #include <linux/export.h>
58 #include <net/net_namespace.h>
59 #include <net/arp.h>
60 #include <net/ip.h>
61 #include <net/ipconfig.h>
62 #include <net/route.h>
64 #include <asm/uaccess.h>
65 #include <net/checksum.h>
66 #include <asm/processor.h>
68 /* Define this to allow debugging output */
69 #undef IPCONFIG_DEBUG
71 #ifdef IPCONFIG_DEBUG
72 #define DBG(x) printk x
73 #else
74 #define DBG(x) do { } while(0)
75 #endif
77 #if defined(CONFIG_IP_PNP_DHCP)
78 #define IPCONFIG_DHCP
79 #endif
80 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
81 #define IPCONFIG_BOOTP
82 #endif
83 #if defined(CONFIG_IP_PNP_RARP)
84 #define IPCONFIG_RARP
85 #endif
86 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
87 #define IPCONFIG_DYNAMIC
88 #endif
90 /* Define the friendly delay before and after opening net devices */
91 #define CONF_POST_OPEN 10 /* After opening: 10 msecs */
92 #define CONF_CARRIER_TIMEOUT 120000 /* Wait for carrier timeout */
94 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
95 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
96 #define CONF_SEND_RETRIES 6 /* Send six requests per open */
97 #define CONF_INTER_TIMEOUT (HZ) /* Inter-device timeout: 1 second */
98 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
99 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
100 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
101 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
102 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
103 - '3' from resolv.h */
105 #define NONE cpu_to_be32(INADDR_NONE)
106 #define ANY cpu_to_be32(INADDR_ANY)
109 * Public IP configuration
112 /* This is used by platforms which might be able to set the ipconfig
113 * variables using firmware environment vars. If this is set, it will
114 * ignore such firmware variables.
116 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
118 static int ic_enable __initdata; /* IP config enabled? */
120 /* Protocol choice */
121 int ic_proto_enabled __initdata = 0
122 #ifdef IPCONFIG_BOOTP
123 | IC_BOOTP
124 #endif
125 #ifdef CONFIG_IP_PNP_DHCP
126 | IC_USE_DHCP
127 #endif
128 #ifdef IPCONFIG_RARP
129 | IC_RARP
130 #endif
133 static int ic_host_name_set __initdata; /* Host name set by us? */
135 __be32 ic_myaddr = NONE; /* My IP address */
136 static __be32 ic_netmask = NONE; /* Netmask for local subnet */
137 __be32 ic_gateway = NONE; /* Gateway IP address */
139 __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */
141 __be32 ic_servaddr = NONE; /* Boot server IP address */
143 __be32 root_server_addr = NONE; /* Address of NFS server */
144 u8 root_server_path[256] = { 0, }; /* Path to mount as root */
146 /* vendor class identifier */
147 static char vendor_class_identifier[253] __initdata;
149 #if defined(CONFIG_IP_PNP_DHCP)
150 static char dhcp_client_identifier[253] __initdata;
151 #endif
153 /* Persistent data: */
155 #ifdef IPCONFIG_DYNAMIC
156 static int ic_proto_used; /* Protocol used, if any */
157 #else
158 #define ic_proto_used 0
159 #endif
160 static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
161 static u8 ic_domain[64]; /* DNS (not NIS) domain name */
164 * Private state.
167 /* Name of user-selected boot device */
168 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
170 /* Protocols supported by available interfaces */
171 static int ic_proto_have_if __initdata;
173 /* MTU for boot device */
174 static int ic_dev_mtu __initdata;
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
186 * Network devices
189 struct ic_device {
190 struct ic_device *next;
191 struct net_device *dev;
192 unsigned short flags;
193 short able;
194 __be32 xid;
197 static struct ic_device *ic_first_dev __initdata; /* List of open device */
198 static struct net_device *ic_dev __initdata; /* Selected device */
200 static bool __init ic_is_init_dev(struct net_device *dev)
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));
210 static int __init ic_open_devs(void)
212 struct ic_device *d, **last;
213 struct net_device *dev;
214 unsigned short oflags;
215 unsigned long start, next_msg;
217 last = &ic_first_dev;
218 rtnl_lock();
220 /* bring loopback and DSA master network devices up first */
221 for_each_netdev(&init_net, dev) {
222 if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev))
223 continue;
224 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
225 pr_err("IP-Config: Failed to open %s\n", dev->name);
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",
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) < 0) {
243 pr_err("IP-Config: Failed to open %s\n",
244 dev->name);
245 continue;
247 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
248 rtnl_unlock();
249 return -ENOMEM;
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 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
262 dev->name, able, d->xid));
266 /* no point in waiting if we could not bring up at least one device */
267 if (!ic_first_dev)
268 goto have_carrier;
270 /* wait for a carrier on at least one device */
271 start = jiffies;
272 next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
273 while (time_before(jiffies, start +
274 msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) {
275 int wait, elapsed;
277 for_each_netdev(&init_net, dev)
278 if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
279 goto have_carrier;
281 msleep(1);
283 if (time_before(jiffies, next_msg))
284 continue;
286 elapsed = jiffies_to_msecs(jiffies - start);
287 wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;
288 pr_info("Waiting up to %d more seconds for network.\n", wait);
289 next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
291 have_carrier:
292 rtnl_unlock();
294 *last = NULL;
296 if (!ic_first_dev) {
297 if (user_dev_name[0])
298 pr_err("IP-Config: Device `%s' not found\n",
299 user_dev_name);
300 else
301 pr_err("IP-Config: No network devices available\n");
302 return -ENODEV;
304 return 0;
307 static void __init ic_close_devs(void)
309 struct ic_device *d, *next;
310 struct net_device *dev;
312 rtnl_lock();
313 next = ic_first_dev;
314 while ((d = next)) {
315 next = d->next;
316 dev = d->dev;
317 if (dev != ic_dev && !netdev_uses_dsa(dev)) {
318 DBG(("IP-Config: Downing %s\n", dev->name));
319 dev_change_flags(dev, d->flags);
321 kfree(d);
323 rtnl_unlock();
327 * Interface to various network functions.
330 static inline void
331 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
333 sin->sin_family = AF_INET;
334 sin->sin_addr.s_addr = addr;
335 sin->sin_port = port;
338 static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
340 int res;
342 mm_segment_t oldfs = get_fs();
343 set_fs(get_ds());
344 res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
345 set_fs(oldfs);
346 return res;
349 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
351 int res;
353 mm_segment_t oldfs = get_fs();
354 set_fs(get_ds());
355 res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
356 set_fs(oldfs);
357 return res;
360 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
362 int res;
364 mm_segment_t oldfs = get_fs();
365 set_fs(get_ds());
366 res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
367 set_fs(oldfs);
368 return res;
372 * Set up interface addresses and routes.
375 static int __init ic_setup_if(void)
377 struct ifreq ir;
378 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
379 int err;
381 memset(&ir, 0, sizeof(ir));
382 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
383 set_sockaddr(sin, ic_myaddr, 0);
384 if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
385 pr_err("IP-Config: Unable to set interface address (%d)\n",
386 err);
387 return -1;
389 set_sockaddr(sin, ic_netmask, 0);
390 if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
391 pr_err("IP-Config: Unable to set interface netmask (%d)\n",
392 err);
393 return -1;
395 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
396 if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
397 pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",
398 err);
399 return -1;
401 /* Handle the case where we need non-standard MTU on the boot link (a network
402 * using jumbo frames, for instance). If we can't set the mtu, don't error
403 * out, we'll try to muddle along.
405 if (ic_dev_mtu != 0) {
406 strcpy(ir.ifr_name, ic_dev->name);
407 ir.ifr_mtu = ic_dev_mtu;
408 if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
409 pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",
410 ic_dev_mtu, err);
412 return 0;
415 static int __init ic_setup_routes(void)
417 /* No need to setup device routes, only the default route... */
419 if (ic_gateway != NONE) {
420 struct rtentry rm;
421 int err;
423 memset(&rm, 0, sizeof(rm));
424 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
425 pr_err("IP-Config: Gateway not on directly connected network\n");
426 return -1;
428 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
429 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
430 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
431 rm.rt_flags = RTF_UP | RTF_GATEWAY;
432 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
433 pr_err("IP-Config: Cannot add default route (%d)\n",
434 err);
435 return -1;
439 return 0;
443 * Fill in default values for all missing parameters.
446 static int __init ic_defaults(void)
449 * At this point we have no userspace running so need not
450 * claim locks on system_utsname
453 if (!ic_host_name_set)
454 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
456 if (root_server_addr == NONE)
457 root_server_addr = ic_servaddr;
459 if (ic_netmask == NONE) {
460 if (IN_CLASSA(ntohl(ic_myaddr)))
461 ic_netmask = htonl(IN_CLASSA_NET);
462 else if (IN_CLASSB(ntohl(ic_myaddr)))
463 ic_netmask = htonl(IN_CLASSB_NET);
464 else if (IN_CLASSC(ntohl(ic_myaddr)))
465 ic_netmask = htonl(IN_CLASSC_NET);
466 else {
467 pr_err("IP-Config: Unable to guess netmask for address %pI4\n",
468 &ic_myaddr);
469 return -1;
471 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
474 return 0;
478 * RARP support.
481 #ifdef IPCONFIG_RARP
483 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
485 static struct packet_type rarp_packet_type __initdata = {
486 .type = cpu_to_be16(ETH_P_RARP),
487 .func = ic_rarp_recv,
490 static inline void __init ic_rarp_init(void)
492 dev_add_pack(&rarp_packet_type);
495 static inline void __init ic_rarp_cleanup(void)
497 dev_remove_pack(&rarp_packet_type);
501 * Process received RARP packet.
503 static int __init
504 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
506 struct arphdr *rarp;
507 unsigned char *rarp_ptr;
508 __be32 sip, tip;
509 unsigned char *tha; /* t for "target" */
510 struct ic_device *d;
512 if (!net_eq(dev_net(dev), &init_net))
513 goto drop;
515 skb = skb_share_check(skb, GFP_ATOMIC);
516 if (!skb)
517 return NET_RX_DROP;
519 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
520 goto drop;
522 /* Basic sanity checks can be done without the lock. */
523 rarp = (struct arphdr *)skb_transport_header(skb);
525 /* If this test doesn't pass, it's not IP, or we should
526 * ignore it anyway.
528 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
529 goto drop;
531 /* If it's not a RARP reply, delete it. */
532 if (rarp->ar_op != htons(ARPOP_RREPLY))
533 goto drop;
535 /* If it's not Ethernet, delete it. */
536 if (rarp->ar_pro != htons(ETH_P_IP))
537 goto drop;
539 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
540 goto drop;
542 /* OK, it is all there and looks valid, process... */
543 rarp = (struct arphdr *)skb_transport_header(skb);
544 rarp_ptr = (unsigned char *) (rarp + 1);
546 /* One reply at a time, please. */
547 spin_lock(&ic_recv_lock);
549 /* If we already have a reply, just drop the packet */
550 if (ic_got_reply)
551 goto drop_unlock;
553 /* Find the ic_device that the packet arrived on */
554 d = ic_first_dev;
555 while (d && d->dev != dev)
556 d = d->next;
557 if (!d)
558 goto drop_unlock; /* should never happen */
560 /* Extract variable-width fields */
561 rarp_ptr += dev->addr_len;
562 memcpy(&sip, rarp_ptr, 4);
563 rarp_ptr += 4;
564 tha = rarp_ptr;
565 rarp_ptr += dev->addr_len;
566 memcpy(&tip, rarp_ptr, 4);
568 /* Discard packets which are not meant for us. */
569 if (memcmp(tha, dev->dev_addr, dev->addr_len))
570 goto drop_unlock;
572 /* Discard packets which are not from specified server. */
573 if (ic_servaddr != NONE && ic_servaddr != sip)
574 goto drop_unlock;
576 /* We have a winner! */
577 ic_dev = dev;
578 if (ic_myaddr == NONE)
579 ic_myaddr = tip;
580 ic_servaddr = sip;
581 ic_addrservaddr = sip;
582 ic_got_reply = IC_RARP;
584 drop_unlock:
585 /* Show's over. Nothing to see here. */
586 spin_unlock(&ic_recv_lock);
588 drop:
589 /* Throw the packet out. */
590 kfree_skb(skb);
591 return 0;
596 * Send RARP request packet over a single interface.
598 static void __init ic_rarp_send_if(struct ic_device *d)
600 struct net_device *dev = d->dev;
601 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
602 dev->dev_addr, dev->dev_addr);
604 #endif
607 * Predefine Nameservers
609 static inline void __init ic_nameservers_predef(void)
611 int i;
613 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
614 ic_nameservers[i] = NONE;
618 * DHCP/BOOTP support.
621 #ifdef IPCONFIG_BOOTP
623 struct bootp_pkt { /* BOOTP packet format */
624 struct iphdr iph; /* IP header */
625 struct udphdr udph; /* UDP header */
626 u8 op; /* 1=request, 2=reply */
627 u8 htype; /* HW address type */
628 u8 hlen; /* HW address length */
629 u8 hops; /* Used only by gateways */
630 __be32 xid; /* Transaction ID */
631 __be16 secs; /* Seconds since we started */
632 __be16 flags; /* Just what it says */
633 __be32 client_ip; /* Client's IP address if known */
634 __be32 your_ip; /* Assigned IP address */
635 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
636 __be32 relay_ip; /* IP address of BOOTP relay */
637 u8 hw_addr[16]; /* Client's HW address */
638 u8 serv_name[64]; /* Server host name */
639 u8 boot_file[128]; /* Name of boot file */
640 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
643 /* packet ops */
644 #define BOOTP_REQUEST 1
645 #define BOOTP_REPLY 2
647 /* DHCP message types */
648 #define DHCPDISCOVER 1
649 #define DHCPOFFER 2
650 #define DHCPREQUEST 3
651 #define DHCPDECLINE 4
652 #define DHCPACK 5
653 #define DHCPNAK 6
654 #define DHCPRELEASE 7
655 #define DHCPINFORM 8
657 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
659 static struct packet_type bootp_packet_type __initdata = {
660 .type = cpu_to_be16(ETH_P_IP),
661 .func = ic_bootp_recv,
664 static __be32 ic_dev_xid; /* Device under configuration */
667 * Initialize DHCP/BOOTP extension fields in the request.
670 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
672 #ifdef IPCONFIG_DHCP
674 static void __init
675 ic_dhcp_init_options(u8 *options)
677 u8 mt = ((ic_servaddr == NONE)
678 ? DHCPDISCOVER : DHCPREQUEST);
679 u8 *e = options;
680 int len;
682 #ifdef IPCONFIG_DEBUG
683 printk("DHCP: Sending message type %d\n", mt);
684 #endif
686 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
687 e += 4;
689 *e++ = 53; /* DHCP message type */
690 *e++ = 1;
691 *e++ = mt;
693 if (mt == DHCPREQUEST) {
694 *e++ = 54; /* Server ID (IP address) */
695 *e++ = 4;
696 memcpy(e, &ic_servaddr, 4);
697 e += 4;
699 *e++ = 50; /* Requested IP address */
700 *e++ = 4;
701 memcpy(e, &ic_myaddr, 4);
702 e += 4;
705 /* always? */
707 static const u8 ic_req_params[] = {
708 1, /* Subnet mask */
709 3, /* Default gateway */
710 6, /* DNS server */
711 12, /* Host name */
712 15, /* Domain name */
713 17, /* Boot path */
714 26, /* MTU */
715 40, /* NIS domain name */
718 *e++ = 55; /* Parameter request list */
719 *e++ = sizeof(ic_req_params);
720 memcpy(e, ic_req_params, sizeof(ic_req_params));
721 e += sizeof(ic_req_params);
723 if (ic_host_name_set) {
724 *e++ = 12; /* host-name */
725 len = strlen(utsname()->nodename);
726 *e++ = len;
727 memcpy(e, utsname()->nodename, len);
728 e += len;
730 if (*vendor_class_identifier) {
731 pr_info("DHCP: sending class identifier \"%s\"\n",
732 vendor_class_identifier);
733 *e++ = 60; /* Class-identifier */
734 len = strlen(vendor_class_identifier);
735 *e++ = len;
736 memcpy(e, vendor_class_identifier, len);
737 e += len;
739 len = strlen(dhcp_client_identifier + 1);
740 /* the minimum length of identifier is 2, include 1 byte type,
741 * and can not be larger than the length of options
743 if (len >= 1 && len < 312 - (e - options) - 1) {
744 *e++ = 61;
745 *e++ = len + 1;
746 memcpy(e, dhcp_client_identifier, len + 1);
747 e += len + 1;
751 *e++ = 255; /* End of the list */
754 #endif /* IPCONFIG_DHCP */
756 static void __init ic_bootp_init_ext(u8 *e)
758 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
759 e += 4;
760 *e++ = 1; /* Subnet mask request */
761 *e++ = 4;
762 e += 4;
763 *e++ = 3; /* Default gateway request */
764 *e++ = 4;
765 e += 4;
766 *e++ = 5; /* Name server request */
767 *e++ = 8;
768 e += 8;
769 *e++ = 12; /* Host name request */
770 *e++ = 32;
771 e += 32;
772 *e++ = 40; /* NIS Domain name request */
773 *e++ = 32;
774 e += 32;
775 *e++ = 17; /* Boot path */
776 *e++ = 40;
777 e += 40;
779 *e++ = 57; /* set extension buffer size for reply */
780 *e++ = 2;
781 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
782 *e++ = 150;
784 *e++ = 255; /* End of the list */
789 * Initialize the DHCP/BOOTP mechanism.
791 static inline void __init ic_bootp_init(void)
793 /* Re-initialise all name servers to NONE, in case any were set via the
794 * "ip=" or "nfsaddrs=" kernel command line parameters: any IP addresses
795 * specified there will already have been decoded but are no longer
796 * needed
798 ic_nameservers_predef();
800 dev_add_pack(&bootp_packet_type);
805 * DHCP/BOOTP cleanup.
807 static inline void __init ic_bootp_cleanup(void)
809 dev_remove_pack(&bootp_packet_type);
814 * Send DHCP/BOOTP request to single interface.
816 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
818 struct net_device *dev = d->dev;
819 struct sk_buff *skb;
820 struct bootp_pkt *b;
821 struct iphdr *h;
822 int hlen = LL_RESERVED_SPACE(dev);
823 int tlen = dev->needed_tailroom;
825 /* Allocate packet */
826 skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
827 GFP_KERNEL);
828 if (!skb)
829 return;
830 skb_reserve(skb, hlen);
831 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
832 memset(b, 0, sizeof(struct bootp_pkt));
834 /* Construct IP header */
835 skb_reset_network_header(skb);
836 h = ip_hdr(skb);
837 h->version = 4;
838 h->ihl = 5;
839 h->tot_len = htons(sizeof(struct bootp_pkt));
840 h->frag_off = htons(IP_DF);
841 h->ttl = 64;
842 h->protocol = IPPROTO_UDP;
843 h->daddr = htonl(INADDR_BROADCAST);
844 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
846 /* Construct UDP header */
847 b->udph.source = htons(68);
848 b->udph.dest = htons(67);
849 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
850 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
852 /* Construct DHCP/BOOTP header */
853 b->op = BOOTP_REQUEST;
854 if (dev->type < 256) /* check for false types */
855 b->htype = dev->type;
856 else if (dev->type == ARPHRD_FDDI)
857 b->htype = ARPHRD_ETHER;
858 else {
859 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
860 b->htype = dev->type; /* can cause undefined behavior */
863 /* server_ip and your_ip address are both already zero per RFC2131 */
864 b->hlen = dev->addr_len;
865 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
866 b->secs = htons(jiffies_diff / HZ);
867 b->xid = d->xid;
869 /* add DHCP options or BOOTP extensions */
870 #ifdef IPCONFIG_DHCP
871 if (ic_proto_enabled & IC_USE_DHCP)
872 ic_dhcp_init_options(b->exten);
873 else
874 #endif
875 ic_bootp_init_ext(b->exten);
877 /* Chain packet down the line... */
878 skb->dev = dev;
879 skb->protocol = htons(ETH_P_IP);
880 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
881 dev->broadcast, dev->dev_addr, skb->len) < 0) {
882 kfree_skb(skb);
883 printk("E");
884 return;
887 if (dev_queue_xmit(skb) < 0)
888 printk("E");
893 * Copy BOOTP-supplied string if not already set.
895 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
897 if (!len)
898 return 0;
899 if (len > max-1)
900 len = max-1;
901 memcpy(dest, src, len);
902 dest[len] = '\0';
903 return 1;
908 * Process BOOTP extensions.
910 static void __init ic_do_bootp_ext(u8 *ext)
912 u8 servers;
913 int i;
914 __be16 mtu;
916 #ifdef IPCONFIG_DEBUG
917 u8 *c;
919 printk("DHCP/BOOTP: Got extension %d:",*ext);
920 for (c=ext+2; c<ext+2+ext[1]; c++)
921 printk(" %02x", *c);
922 printk("\n");
923 #endif
925 switch (*ext++) {
926 case 1: /* Subnet mask */
927 if (ic_netmask == NONE)
928 memcpy(&ic_netmask, ext+1, 4);
929 break;
930 case 3: /* Default gateway */
931 if (ic_gateway == NONE)
932 memcpy(&ic_gateway, ext+1, 4);
933 break;
934 case 6: /* DNS server */
935 servers= *ext/4;
936 if (servers > CONF_NAMESERVERS_MAX)
937 servers = CONF_NAMESERVERS_MAX;
938 for (i = 0; i < servers; i++) {
939 if (ic_nameservers[i] == NONE)
940 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
942 break;
943 case 12: /* Host name */
944 ic_bootp_string(utsname()->nodename, ext+1, *ext,
945 __NEW_UTS_LEN);
946 ic_host_name_set = 1;
947 break;
948 case 15: /* Domain name (DNS) */
949 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
950 break;
951 case 17: /* Root path */
952 if (!root_server_path[0])
953 ic_bootp_string(root_server_path, ext+1, *ext,
954 sizeof(root_server_path));
955 break;
956 case 26: /* Interface MTU */
957 memcpy(&mtu, ext+1, sizeof(mtu));
958 ic_dev_mtu = ntohs(mtu);
959 break;
960 case 40: /* NIS Domain name (_not_ DNS) */
961 ic_bootp_string(utsname()->domainname, ext+1, *ext,
962 __NEW_UTS_LEN);
963 break;
969 * Receive BOOTP reply.
971 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
973 struct bootp_pkt *b;
974 struct iphdr *h;
975 struct ic_device *d;
976 int len, ext_len;
978 if (!net_eq(dev_net(dev), &init_net))
979 goto drop;
981 /* Perform verifications before taking the lock. */
982 if (skb->pkt_type == PACKET_OTHERHOST)
983 goto drop;
985 skb = skb_share_check(skb, GFP_ATOMIC);
986 if (!skb)
987 return NET_RX_DROP;
989 if (!pskb_may_pull(skb,
990 sizeof(struct iphdr) +
991 sizeof(struct udphdr)))
992 goto drop;
994 b = (struct bootp_pkt *)skb_network_header(skb);
995 h = &b->iph;
997 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
998 goto drop;
1000 /* Fragments are not supported */
1001 if (ip_is_fragment(h)) {
1002 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
1003 goto drop;
1006 if (skb->len < ntohs(h->tot_len))
1007 goto drop;
1009 if (ip_fast_csum((char *) h, h->ihl))
1010 goto drop;
1012 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
1013 goto drop;
1015 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
1016 goto drop;
1018 len = ntohs(b->udph.len) - sizeof(struct udphdr);
1019 ext_len = len - (sizeof(*b) -
1020 sizeof(struct iphdr) -
1021 sizeof(struct udphdr) -
1022 sizeof(b->exten));
1023 if (ext_len < 0)
1024 goto drop;
1026 /* Ok the front looks good, make sure we can get at the rest. */
1027 if (!pskb_may_pull(skb, skb->len))
1028 goto drop;
1030 b = (struct bootp_pkt *)skb_network_header(skb);
1031 h = &b->iph;
1033 /* One reply at a time, please. */
1034 spin_lock(&ic_recv_lock);
1036 /* If we already have a reply, just drop the packet */
1037 if (ic_got_reply)
1038 goto drop_unlock;
1040 /* Find the ic_device that the packet arrived on */
1041 d = ic_first_dev;
1042 while (d && d->dev != dev)
1043 d = d->next;
1044 if (!d)
1045 goto drop_unlock; /* should never happen */
1047 /* Is it a reply to our BOOTP request? */
1048 if (b->op != BOOTP_REPLY ||
1049 b->xid != d->xid) {
1050 net_err_ratelimited("DHCP/BOOTP: Reply not for us, op[%x] xid[%x]\n",
1051 b->op, b->xid);
1052 goto drop_unlock;
1055 /* Is it a reply for the device we are configuring? */
1056 if (b->xid != ic_dev_xid) {
1057 net_err_ratelimited("DHCP/BOOTP: Ignoring delayed packet\n");
1058 goto drop_unlock;
1061 /* Parse extensions */
1062 if (ext_len >= 4 &&
1063 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
1064 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
1065 u8 *ext;
1067 #ifdef IPCONFIG_DHCP
1068 if (ic_proto_enabled & IC_USE_DHCP) {
1069 __be32 server_id = NONE;
1070 int mt = 0;
1072 ext = &b->exten[4];
1073 while (ext < end && *ext != 0xff) {
1074 u8 *opt = ext++;
1075 if (*opt == 0) /* Padding */
1076 continue;
1077 ext += *ext + 1;
1078 if (ext >= end)
1079 break;
1080 switch (*opt) {
1081 case 53: /* Message type */
1082 if (opt[1])
1083 mt = opt[2];
1084 break;
1085 case 54: /* Server ID (IP address) */
1086 if (opt[1] >= 4)
1087 memcpy(&server_id, opt + 2, 4);
1088 break;
1092 #ifdef IPCONFIG_DEBUG
1093 printk("DHCP: Got message type %d\n", mt);
1094 #endif
1096 switch (mt) {
1097 case DHCPOFFER:
1098 /* While in the process of accepting one offer,
1099 * ignore all others.
1101 if (ic_myaddr != NONE)
1102 goto drop_unlock;
1104 /* Let's accept that offer. */
1105 ic_myaddr = b->your_ip;
1106 ic_servaddr = server_id;
1107 #ifdef IPCONFIG_DEBUG
1108 printk("DHCP: Offered address %pI4 by server %pI4\n",
1109 &ic_myaddr, &b->iph.saddr);
1110 #endif
1111 /* The DHCP indicated server address takes
1112 * precedence over the bootp header one if
1113 * they are different.
1115 if ((server_id != NONE) &&
1116 (b->server_ip != server_id))
1117 b->server_ip = ic_servaddr;
1118 break;
1120 case DHCPACK:
1121 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1122 goto drop_unlock;
1124 /* Yeah! */
1125 break;
1127 default:
1128 /* Urque. Forget it*/
1129 ic_myaddr = NONE;
1130 ic_servaddr = NONE;
1131 goto drop_unlock;
1134 ic_dhcp_msgtype = mt;
1137 #endif /* IPCONFIG_DHCP */
1139 ext = &b->exten[4];
1140 while (ext < end && *ext != 0xff) {
1141 u8 *opt = ext++;
1142 if (*opt == 0) /* Padding */
1143 continue;
1144 ext += *ext + 1;
1145 if (ext < end)
1146 ic_do_bootp_ext(opt);
1150 /* We have a winner! */
1151 ic_dev = dev;
1152 ic_myaddr = b->your_ip;
1153 ic_servaddr = b->server_ip;
1154 ic_addrservaddr = b->iph.saddr;
1155 if (ic_gateway == NONE && b->relay_ip)
1156 ic_gateway = b->relay_ip;
1157 if (ic_nameservers[0] == NONE)
1158 ic_nameservers[0] = ic_servaddr;
1159 ic_got_reply = IC_BOOTP;
1161 drop_unlock:
1162 /* Show's over. Nothing to see here. */
1163 spin_unlock(&ic_recv_lock);
1165 drop:
1166 /* Throw the packet out. */
1167 kfree_skb(skb);
1169 return 0;
1173 #endif
1177 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1180 #ifdef IPCONFIG_DYNAMIC
1182 static int __init ic_dynamic(void)
1184 int retries;
1185 struct ic_device *d;
1186 unsigned long start_jiffies, timeout, jiff;
1187 int do_bootp = ic_proto_have_if & IC_BOOTP;
1188 int do_rarp = ic_proto_have_if & IC_RARP;
1191 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1192 * This routine gets only called when some pieces of information
1193 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1195 if (!ic_proto_enabled) {
1196 pr_err("IP-Config: Incomplete network configuration information\n");
1197 return -1;
1200 #ifdef IPCONFIG_BOOTP
1201 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1202 pr_err("DHCP/BOOTP: No suitable device found\n");
1203 #endif
1204 #ifdef IPCONFIG_RARP
1205 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1206 pr_err("RARP: No suitable device found\n");
1207 #endif
1209 if (!ic_proto_have_if)
1210 /* Error message already printed */
1211 return -1;
1214 * Setup protocols
1216 #ifdef IPCONFIG_BOOTP
1217 if (do_bootp)
1218 ic_bootp_init();
1219 #endif
1220 #ifdef IPCONFIG_RARP
1221 if (do_rarp)
1222 ic_rarp_init();
1223 #endif
1226 * Send requests and wait, until we get an answer. This loop
1227 * seems to be a terrible waste of CPU time, but actually there is
1228 * only one process running at all, so we don't need to use any
1229 * scheduler functions.
1230 * [Actually we could now, but the nothing else running note still
1231 * applies.. - AC]
1233 pr_notice("Sending %s%s%s requests .",
1234 do_bootp
1235 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1236 (do_bootp && do_rarp) ? " and " : "",
1237 do_rarp ? "RARP" : "");
1239 start_jiffies = jiffies;
1240 d = ic_first_dev;
1241 retries = CONF_SEND_RETRIES;
1242 get_random_bytes(&timeout, sizeof(timeout));
1243 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
1244 for (;;) {
1245 #ifdef IPCONFIG_BOOTP
1246 /* Track the device we are configuring */
1247 ic_dev_xid = d->xid;
1249 if (do_bootp && (d->able & IC_BOOTP))
1250 ic_bootp_send_if(d, jiffies - start_jiffies);
1251 #endif
1252 #ifdef IPCONFIG_RARP
1253 if (do_rarp && (d->able & IC_RARP))
1254 ic_rarp_send_if(d);
1255 #endif
1257 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1258 while (time_before(jiffies, jiff) && !ic_got_reply)
1259 schedule_timeout_uninterruptible(1);
1260 #ifdef IPCONFIG_DHCP
1261 /* DHCP isn't done until we get a DHCPACK. */
1262 if ((ic_got_reply & IC_BOOTP) &&
1263 (ic_proto_enabled & IC_USE_DHCP) &&
1264 ic_dhcp_msgtype != DHCPACK) {
1265 ic_got_reply = 0;
1266 pr_cont(",");
1267 continue;
1269 #endif /* IPCONFIG_DHCP */
1271 if (ic_got_reply) {
1272 pr_cont(" OK\n");
1273 break;
1276 if ((d = d->next))
1277 continue;
1279 if (! --retries) {
1280 pr_cont(" timed out!\n");
1281 break;
1284 d = ic_first_dev;
1286 timeout = timeout CONF_TIMEOUT_MULT;
1287 if (timeout > CONF_TIMEOUT_MAX)
1288 timeout = CONF_TIMEOUT_MAX;
1290 pr_cont(".");
1293 #ifdef IPCONFIG_BOOTP
1294 if (do_bootp)
1295 ic_bootp_cleanup();
1296 #endif
1297 #ifdef IPCONFIG_RARP
1298 if (do_rarp)
1299 ic_rarp_cleanup();
1300 #endif
1302 if (!ic_got_reply) {
1303 ic_myaddr = NONE;
1304 return -1;
1307 printk("IP-Config: Got %s answer from %pI4, ",
1308 ((ic_got_reply & IC_RARP) ? "RARP"
1309 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1310 &ic_addrservaddr);
1311 pr_cont("my address is %pI4\n", &ic_myaddr);
1313 return 0;
1316 #endif /* IPCONFIG_DYNAMIC */
1318 #ifdef CONFIG_PROC_FS
1320 static int pnp_seq_show(struct seq_file *seq, void *v)
1322 int i;
1324 if (ic_proto_used & IC_PROTO)
1325 seq_printf(seq, "#PROTO: %s\n",
1326 (ic_proto_used & IC_RARP) ? "RARP"
1327 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1328 else
1329 seq_puts(seq, "#MANUAL\n");
1331 if (ic_domain[0])
1332 seq_printf(seq,
1333 "domain %s\n", ic_domain);
1334 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1335 if (ic_nameservers[i] != NONE)
1336 seq_printf(seq, "nameserver %pI4\n",
1337 &ic_nameservers[i]);
1339 if (ic_servaddr != NONE)
1340 seq_printf(seq, "bootserver %pI4\n",
1341 &ic_servaddr);
1342 return 0;
1345 static int pnp_seq_open(struct inode *indoe, struct file *file)
1347 return single_open(file, pnp_seq_show, NULL);
1350 static const struct file_operations pnp_seq_fops = {
1351 .owner = THIS_MODULE,
1352 .open = pnp_seq_open,
1353 .read = seq_read,
1354 .llseek = seq_lseek,
1355 .release = single_release,
1357 #endif /* CONFIG_PROC_FS */
1360 * Extract IP address from the parameter string if needed. Note that we
1361 * need to have root_server_addr set _before_ IPConfig gets called as it
1362 * can override it.
1364 __be32 __init root_nfs_parse_addr(char *name)
1366 __be32 addr;
1367 int octets = 0;
1368 char *cp, *cq;
1370 cp = cq = name;
1371 while (octets < 4) {
1372 while (*cp >= '0' && *cp <= '9')
1373 cp++;
1374 if (cp == cq || cp - cq > 3)
1375 break;
1376 if (*cp == '.' || octets == 3)
1377 octets++;
1378 if (octets < 4)
1379 cp++;
1380 cq = cp;
1382 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1383 if (*cp == ':')
1384 *cp++ = '\0';
1385 addr = in_aton(name);
1386 memmove(name, cp, strlen(cp) + 1);
1387 } else
1388 addr = NONE;
1390 return addr;
1393 #define DEVICE_WAIT_MAX 12 /* 12 seconds */
1395 static int __init wait_for_devices(void)
1397 int i;
1399 for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1400 struct net_device *dev;
1401 int found = 0;
1403 rtnl_lock();
1404 for_each_netdev(&init_net, dev) {
1405 if (ic_is_init_dev(dev)) {
1406 found = 1;
1407 break;
1410 rtnl_unlock();
1411 if (found)
1412 return 0;
1413 ssleep(1);
1415 return -ENODEV;
1419 * IP Autoconfig dispatcher.
1422 static int __init ip_auto_config(void)
1424 __be32 addr;
1425 #ifdef IPCONFIG_DYNAMIC
1426 int retries = CONF_OPEN_RETRIES;
1427 #endif
1428 int err;
1429 unsigned int i;
1431 /* Initialise all name servers to NONE (but only if the "ip=" or
1432 * "nfsaddrs=" kernel command line parameters weren't decoded, otherwise
1433 * we'll overwrite the IP addresses specified there)
1435 if (ic_set_manually == 0)
1436 ic_nameservers_predef();
1438 #ifdef CONFIG_PROC_FS
1439 proc_create("pnp", S_IRUGO, init_net.proc_net, &pnp_seq_fops);
1440 #endif /* CONFIG_PROC_FS */
1442 if (!ic_enable)
1443 return 0;
1445 DBG(("IP-Config: Entered.\n"));
1446 #ifdef IPCONFIG_DYNAMIC
1447 try_try_again:
1448 #endif
1449 /* Wait for devices to appear */
1450 err = wait_for_devices();
1451 if (err)
1452 return err;
1454 /* Setup all network devices */
1455 err = ic_open_devs();
1456 if (err)
1457 return err;
1459 /* Give drivers a chance to settle */
1460 msleep(CONF_POST_OPEN);
1463 * If the config information is insufficient (e.g., our IP address or
1464 * IP address of the boot server is missing or we have multiple network
1465 * interfaces and no default was set), use BOOTP or RARP to get the
1466 * missing values.
1468 if (ic_myaddr == NONE ||
1469 #ifdef CONFIG_ROOT_NFS
1470 (root_server_addr == NONE &&
1471 ic_servaddr == NONE &&
1472 ROOT_DEV == Root_NFS) ||
1473 #endif
1474 ic_first_dev->next) {
1475 #ifdef IPCONFIG_DYNAMIC
1476 if (ic_dynamic() < 0) {
1477 ic_close_devs();
1480 * I don't know why, but sometimes the
1481 * eepro100 driver (at least) gets upset and
1482 * doesn't work the first time it's opened.
1483 * But then if you close it and reopen it, it
1484 * works just fine. So we need to try that at
1485 * least once before giving up.
1487 * Also, if the root will be NFS-mounted, we
1488 * have nowhere to go if DHCP fails. So we
1489 * just have to keep trying forever.
1491 * -- Chip
1493 #ifdef CONFIG_ROOT_NFS
1494 if (ROOT_DEV == Root_NFS) {
1495 pr_err("IP-Config: Retrying forever (NFS root)...\n");
1496 goto try_try_again;
1498 #endif
1500 if (--retries) {
1501 pr_err("IP-Config: Reopening network devices...\n");
1502 goto try_try_again;
1505 /* Oh, well. At least we tried. */
1506 pr_err("IP-Config: Auto-configuration of network failed\n");
1507 return -1;
1509 #else /* !DYNAMIC */
1510 pr_err("IP-Config: Incomplete network configuration information\n");
1511 ic_close_devs();
1512 return -1;
1513 #endif /* IPCONFIG_DYNAMIC */
1514 } else {
1515 /* Device selected manually or only one device -> use it */
1516 ic_dev = ic_first_dev->dev;
1519 addr = root_nfs_parse_addr(root_server_path);
1520 if (root_server_addr == NONE)
1521 root_server_addr = addr;
1524 * Use defaults wherever applicable.
1526 if (ic_defaults() < 0)
1527 return -1;
1530 * Close all network devices except the device we've
1531 * autoconfigured and set up routes.
1533 ic_close_devs();
1534 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1535 return -1;
1538 * Record which protocol was actually used.
1540 #ifdef IPCONFIG_DYNAMIC
1541 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1542 #endif
1544 #ifndef IPCONFIG_SILENT
1546 * Clue in the operator.
1548 pr_info("IP-Config: Complete:\n");
1550 pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
1551 ic_dev->name, ic_dev->addr_len, ic_dev->dev_addr,
1552 &ic_myaddr, &ic_netmask, &ic_gateway);
1553 pr_info(" host=%s, domain=%s, nis-domain=%s\n",
1554 utsname()->nodename, ic_domain, utsname()->domainname);
1555 pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1556 &ic_servaddr, &root_server_addr, root_server_path);
1557 if (ic_dev_mtu)
1558 pr_cont(", mtu=%d", ic_dev_mtu);
1559 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
1560 if (ic_nameservers[i] != NONE) {
1561 pr_info(" nameserver%u=%pI4",
1562 i, &ic_nameservers[i]);
1563 break;
1565 for (i++; i < CONF_NAMESERVERS_MAX; i++)
1566 if (ic_nameservers[i] != NONE)
1567 pr_cont(", nameserver%u=%pI4", i, &ic_nameservers[i]);
1568 pr_cont("\n");
1569 #endif /* !SILENT */
1571 return 0;
1574 late_initcall(ip_auto_config);
1578 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1579 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
1581 static int __init ic_proto_name(char *name)
1583 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1584 return 1;
1586 if (!strcmp(name, "off") || !strcmp(name, "none")) {
1587 return 0;
1589 #ifdef CONFIG_IP_PNP_DHCP
1590 else if (!strncmp(name, "dhcp", 4)) {
1591 char *client_id;
1593 ic_proto_enabled &= ~IC_RARP;
1594 client_id = strstr(name, "dhcp,");
1595 if (client_id) {
1596 char *v;
1598 client_id = client_id + 5;
1599 v = strchr(client_id, ',');
1600 if (!v)
1601 return 1;
1602 *v = 0;
1603 if (kstrtou8(client_id, 0, dhcp_client_identifier))
1604 DBG("DHCP: Invalid client identifier type\n");
1605 strncpy(dhcp_client_identifier + 1, v + 1, 251);
1606 *v = ',';
1608 return 1;
1610 #endif
1611 #ifdef CONFIG_IP_PNP_BOOTP
1612 else if (!strcmp(name, "bootp")) {
1613 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1614 return 1;
1616 #endif
1617 #ifdef CONFIG_IP_PNP_RARP
1618 else if (!strcmp(name, "rarp")) {
1619 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1620 return 1;
1622 #endif
1623 #ifdef IPCONFIG_DYNAMIC
1624 else if (!strcmp(name, "both")) {
1625 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1626 return 1;
1628 #endif
1629 return 0;
1632 static int __init ip_auto_config_setup(char *addrs)
1634 char *cp, *ip, *dp;
1635 int num = 0;
1637 ic_set_manually = 1;
1638 ic_enable = 1;
1641 * If any dhcp, bootp etc options are set, leave autoconfig on
1642 * and skip the below static IP processing.
1644 if (ic_proto_name(addrs))
1645 return 1;
1647 /* If no static IP is given, turn off autoconfig and bail. */
1648 if (*addrs == 0 ||
1649 strcmp(addrs, "off") == 0 ||
1650 strcmp(addrs, "none") == 0) {
1651 ic_enable = 0;
1652 return 1;
1655 /* Initialise all name servers to NONE */
1656 ic_nameservers_predef();
1658 /* Parse string for static IP assignment. */
1659 ip = addrs;
1660 while (ip && *ip) {
1661 if ((cp = strchr(ip, ':')))
1662 *cp++ = '\0';
1663 if (strlen(ip) > 0) {
1664 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1665 switch (num) {
1666 case 0:
1667 if ((ic_myaddr = in_aton(ip)) == ANY)
1668 ic_myaddr = NONE;
1669 break;
1670 case 1:
1671 if ((ic_servaddr = in_aton(ip)) == ANY)
1672 ic_servaddr = NONE;
1673 break;
1674 case 2:
1675 if ((ic_gateway = in_aton(ip)) == ANY)
1676 ic_gateway = NONE;
1677 break;
1678 case 3:
1679 if ((ic_netmask = in_aton(ip)) == ANY)
1680 ic_netmask = NONE;
1681 break;
1682 case 4:
1683 if ((dp = strchr(ip, '.'))) {
1684 *dp++ = '\0';
1685 strlcpy(utsname()->domainname, dp,
1686 sizeof(utsname()->domainname));
1688 strlcpy(utsname()->nodename, ip,
1689 sizeof(utsname()->nodename));
1690 ic_host_name_set = 1;
1691 break;
1692 case 5:
1693 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1694 break;
1695 case 6:
1696 if (ic_proto_name(ip) == 0 &&
1697 ic_myaddr == NONE) {
1698 ic_enable = 0;
1700 break;
1701 case 7:
1702 if (CONF_NAMESERVERS_MAX >= 1) {
1703 ic_nameservers[0] = in_aton(ip);
1704 if (ic_nameservers[0] == ANY)
1705 ic_nameservers[0] = NONE;
1707 break;
1708 case 8:
1709 if (CONF_NAMESERVERS_MAX >= 2) {
1710 ic_nameservers[1] = in_aton(ip);
1711 if (ic_nameservers[1] == ANY)
1712 ic_nameservers[1] = NONE;
1714 break;
1717 ip = cp;
1718 num++;
1721 return 1;
1723 __setup("ip=", ip_auto_config_setup);
1725 static int __init nfsaddrs_config_setup(char *addrs)
1727 return ip_auto_config_setup(addrs);
1729 __setup("nfsaddrs=", nfsaddrs_config_setup);
1731 static int __init vendor_class_identifier_setup(char *addrs)
1733 if (strlcpy(vendor_class_identifier, addrs,
1734 sizeof(vendor_class_identifier))
1735 >= sizeof(vendor_class_identifier))
1736 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"",
1737 vendor_class_identifier);
1738 return 1;
1740 __setup("dhcpclass=", vendor_class_identifier_setup);