ACPI / bus: Leave modalias empty for devices which are not present
[linux/fpc-iii.git] / net / ipv4 / ipconfig.c
blob0bc7412d9e14a3395ab9c877ee6683f33748a3b1
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 static int ic_proto_used; /* Protocol used, if any */
156 static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
157 static u8 ic_domain[64]; /* DNS (not NIS) domain name */
160 * Private state.
163 /* Name of user-selected boot device */
164 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
166 /* Protocols supported by available interfaces */
167 static int ic_proto_have_if __initdata;
169 /* MTU for boot device */
170 static int ic_dev_mtu __initdata;
172 #ifdef IPCONFIG_DYNAMIC
173 static DEFINE_SPINLOCK(ic_recv_lock);
174 static volatile int ic_got_reply __initdata; /* Proto(s) that replied */
175 #endif
176 #ifdef IPCONFIG_DHCP
177 static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */
178 #endif
182 * Network devices
185 struct ic_device {
186 struct ic_device *next;
187 struct net_device *dev;
188 unsigned short flags;
189 short able;
190 __be32 xid;
193 static struct ic_device *ic_first_dev __initdata; /* List of open device */
194 static struct net_device *ic_dev __initdata; /* Selected device */
196 static bool __init ic_is_init_dev(struct net_device *dev)
198 if (dev->flags & IFF_LOOPBACK)
199 return false;
200 return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
201 (!(dev->flags & IFF_LOOPBACK) &&
202 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
203 strncmp(dev->name, "dummy", 5));
206 static int __init ic_open_devs(void)
208 struct ic_device *d, **last;
209 struct net_device *dev;
210 unsigned short oflags;
211 unsigned long start, next_msg;
213 last = &ic_first_dev;
214 rtnl_lock();
216 /* bring loopback and DSA master network devices up first */
217 for_each_netdev(&init_net, dev) {
218 if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev))
219 continue;
220 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
221 pr_err("IP-Config: Failed to open %s\n", dev->name);
224 for_each_netdev(&init_net, dev) {
225 if (ic_is_init_dev(dev)) {
226 int able = 0;
227 if (dev->mtu >= 364)
228 able |= IC_BOOTP;
229 else
230 pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small",
231 dev->name, dev->mtu);
232 if (!(dev->flags & IFF_NOARP))
233 able |= IC_RARP;
234 able &= ic_proto_enabled;
235 if (ic_proto_enabled && !able)
236 continue;
237 oflags = dev->flags;
238 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
239 pr_err("IP-Config: Failed to open %s\n",
240 dev->name);
241 continue;
243 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
244 rtnl_unlock();
245 return -ENOMEM;
247 d->dev = dev;
248 *last = d;
249 last = &d->next;
250 d->flags = oflags;
251 d->able = able;
252 if (able & IC_BOOTP)
253 get_random_bytes(&d->xid, sizeof(__be32));
254 else
255 d->xid = 0;
256 ic_proto_have_if |= able;
257 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
258 dev->name, able, d->xid));
262 /* no point in waiting if we could not bring up at least one device */
263 if (!ic_first_dev)
264 goto have_carrier;
266 /* wait for a carrier on at least one device */
267 start = jiffies;
268 next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
269 while (time_before(jiffies, start +
270 msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) {
271 int wait, elapsed;
273 for_each_netdev(&init_net, dev)
274 if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
275 goto have_carrier;
277 msleep(1);
279 if (time_before(jiffies, next_msg))
280 continue;
282 elapsed = jiffies_to_msecs(jiffies - start);
283 wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;
284 pr_info("Waiting up to %d more seconds for network.\n", wait);
285 next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
287 have_carrier:
288 rtnl_unlock();
290 *last = NULL;
292 if (!ic_first_dev) {
293 if (user_dev_name[0])
294 pr_err("IP-Config: Device `%s' not found\n",
295 user_dev_name);
296 else
297 pr_err("IP-Config: No network devices available\n");
298 return -ENODEV;
300 return 0;
303 static void __init ic_close_devs(void)
305 struct ic_device *d, *next;
306 struct net_device *dev;
308 rtnl_lock();
309 next = ic_first_dev;
310 while ((d = next)) {
311 next = d->next;
312 dev = d->dev;
313 if (dev != ic_dev && !netdev_uses_dsa(dev)) {
314 DBG(("IP-Config: Downing %s\n", dev->name));
315 dev_change_flags(dev, d->flags);
317 kfree(d);
319 rtnl_unlock();
323 * Interface to various network functions.
326 static inline void
327 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
329 sin->sin_family = AF_INET;
330 sin->sin_addr.s_addr = addr;
331 sin->sin_port = port;
334 static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
336 int res;
338 mm_segment_t oldfs = get_fs();
339 set_fs(get_ds());
340 res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
341 set_fs(oldfs);
342 return res;
345 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
347 int res;
349 mm_segment_t oldfs = get_fs();
350 set_fs(get_ds());
351 res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
352 set_fs(oldfs);
353 return res;
356 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
358 int res;
360 mm_segment_t oldfs = get_fs();
361 set_fs(get_ds());
362 res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
363 set_fs(oldfs);
364 return res;
368 * Set up interface addresses and routes.
371 static int __init ic_setup_if(void)
373 struct ifreq ir;
374 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
375 int err;
377 memset(&ir, 0, sizeof(ir));
378 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
379 set_sockaddr(sin, ic_myaddr, 0);
380 if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
381 pr_err("IP-Config: Unable to set interface address (%d)\n",
382 err);
383 return -1;
385 set_sockaddr(sin, ic_netmask, 0);
386 if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
387 pr_err("IP-Config: Unable to set interface netmask (%d)\n",
388 err);
389 return -1;
391 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
392 if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
393 pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",
394 err);
395 return -1;
397 /* Handle the case where we need non-standard MTU on the boot link (a network
398 * using jumbo frames, for instance). If we can't set the mtu, don't error
399 * out, we'll try to muddle along.
401 if (ic_dev_mtu != 0) {
402 strcpy(ir.ifr_name, ic_dev->name);
403 ir.ifr_mtu = ic_dev_mtu;
404 if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
405 pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",
406 ic_dev_mtu, err);
408 return 0;
411 static int __init ic_setup_routes(void)
413 /* No need to setup device routes, only the default route... */
415 if (ic_gateway != NONE) {
416 struct rtentry rm;
417 int err;
419 memset(&rm, 0, sizeof(rm));
420 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
421 pr_err("IP-Config: Gateway not on directly connected network\n");
422 return -1;
424 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
425 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
426 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
427 rm.rt_flags = RTF_UP | RTF_GATEWAY;
428 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
429 pr_err("IP-Config: Cannot add default route (%d)\n",
430 err);
431 return -1;
435 return 0;
439 * Fill in default values for all missing parameters.
442 static int __init ic_defaults(void)
445 * At this point we have no userspace running so need not
446 * claim locks on system_utsname
449 if (!ic_host_name_set)
450 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
452 if (root_server_addr == NONE)
453 root_server_addr = ic_servaddr;
455 if (ic_netmask == NONE) {
456 if (IN_CLASSA(ntohl(ic_myaddr)))
457 ic_netmask = htonl(IN_CLASSA_NET);
458 else if (IN_CLASSB(ntohl(ic_myaddr)))
459 ic_netmask = htonl(IN_CLASSB_NET);
460 else if (IN_CLASSC(ntohl(ic_myaddr)))
461 ic_netmask = htonl(IN_CLASSC_NET);
462 else {
463 pr_err("IP-Config: Unable to guess netmask for address %pI4\n",
464 &ic_myaddr);
465 return -1;
467 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
470 return 0;
474 * RARP support.
477 #ifdef IPCONFIG_RARP
479 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
481 static struct packet_type rarp_packet_type __initdata = {
482 .type = cpu_to_be16(ETH_P_RARP),
483 .func = ic_rarp_recv,
486 static inline void __init ic_rarp_init(void)
488 dev_add_pack(&rarp_packet_type);
491 static inline void __init ic_rarp_cleanup(void)
493 dev_remove_pack(&rarp_packet_type);
497 * Process received RARP packet.
499 static int __init
500 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
502 struct arphdr *rarp;
503 unsigned char *rarp_ptr;
504 __be32 sip, tip;
505 unsigned char *tha; /* t for "target" */
506 struct ic_device *d;
508 if (!net_eq(dev_net(dev), &init_net))
509 goto drop;
511 skb = skb_share_check(skb, GFP_ATOMIC);
512 if (!skb)
513 return NET_RX_DROP;
515 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
516 goto drop;
518 /* Basic sanity checks can be done without the lock. */
519 rarp = (struct arphdr *)skb_transport_header(skb);
521 /* If this test doesn't pass, it's not IP, or we should
522 * ignore it anyway.
524 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
525 goto drop;
527 /* If it's not a RARP reply, delete it. */
528 if (rarp->ar_op != htons(ARPOP_RREPLY))
529 goto drop;
531 /* If it's not Ethernet, delete it. */
532 if (rarp->ar_pro != htons(ETH_P_IP))
533 goto drop;
535 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
536 goto drop;
538 /* OK, it is all there and looks valid, process... */
539 rarp = (struct arphdr *)skb_transport_header(skb);
540 rarp_ptr = (unsigned char *) (rarp + 1);
542 /* One reply at a time, please. */
543 spin_lock(&ic_recv_lock);
545 /* If we already have a reply, just drop the packet */
546 if (ic_got_reply)
547 goto drop_unlock;
549 /* Find the ic_device that the packet arrived on */
550 d = ic_first_dev;
551 while (d && d->dev != dev)
552 d = d->next;
553 if (!d)
554 goto drop_unlock; /* should never happen */
556 /* Extract variable-width fields */
557 rarp_ptr += dev->addr_len;
558 memcpy(&sip, rarp_ptr, 4);
559 rarp_ptr += 4;
560 tha = rarp_ptr;
561 rarp_ptr += dev->addr_len;
562 memcpy(&tip, rarp_ptr, 4);
564 /* Discard packets which are not meant for us. */
565 if (memcmp(tha, dev->dev_addr, dev->addr_len))
566 goto drop_unlock;
568 /* Discard packets which are not from specified server. */
569 if (ic_servaddr != NONE && ic_servaddr != sip)
570 goto drop_unlock;
572 /* We have a winner! */
573 ic_dev = dev;
574 if (ic_myaddr == NONE)
575 ic_myaddr = tip;
576 ic_servaddr = sip;
577 ic_addrservaddr = sip;
578 ic_got_reply = IC_RARP;
580 drop_unlock:
581 /* Show's over. Nothing to see here. */
582 spin_unlock(&ic_recv_lock);
584 drop:
585 /* Throw the packet out. */
586 kfree_skb(skb);
587 return 0;
592 * Send RARP request packet over a single interface.
594 static void __init ic_rarp_send_if(struct ic_device *d)
596 struct net_device *dev = d->dev;
597 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
598 dev->dev_addr, dev->dev_addr);
600 #endif
603 * Predefine Nameservers
605 static inline void __init ic_nameservers_predef(void)
607 int i;
609 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
610 ic_nameservers[i] = NONE;
614 * DHCP/BOOTP support.
617 #ifdef IPCONFIG_BOOTP
619 struct bootp_pkt { /* BOOTP packet format */
620 struct iphdr iph; /* IP header */
621 struct udphdr udph; /* UDP header */
622 u8 op; /* 1=request, 2=reply */
623 u8 htype; /* HW address type */
624 u8 hlen; /* HW address length */
625 u8 hops; /* Used only by gateways */
626 __be32 xid; /* Transaction ID */
627 __be16 secs; /* Seconds since we started */
628 __be16 flags; /* Just what it says */
629 __be32 client_ip; /* Client's IP address if known */
630 __be32 your_ip; /* Assigned IP address */
631 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
632 __be32 relay_ip; /* IP address of BOOTP relay */
633 u8 hw_addr[16]; /* Client's HW address */
634 u8 serv_name[64]; /* Server host name */
635 u8 boot_file[128]; /* Name of boot file */
636 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
639 /* packet ops */
640 #define BOOTP_REQUEST 1
641 #define BOOTP_REPLY 2
643 /* DHCP message types */
644 #define DHCPDISCOVER 1
645 #define DHCPOFFER 2
646 #define DHCPREQUEST 3
647 #define DHCPDECLINE 4
648 #define DHCPACK 5
649 #define DHCPNAK 6
650 #define DHCPRELEASE 7
651 #define DHCPINFORM 8
653 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
655 static struct packet_type bootp_packet_type __initdata = {
656 .type = cpu_to_be16(ETH_P_IP),
657 .func = ic_bootp_recv,
660 static __be32 ic_dev_xid; /* Device under configuration */
663 * Initialize DHCP/BOOTP extension fields in the request.
666 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
668 #ifdef IPCONFIG_DHCP
670 static void __init
671 ic_dhcp_init_options(u8 *options)
673 u8 mt = ((ic_servaddr == NONE)
674 ? DHCPDISCOVER : DHCPREQUEST);
675 u8 *e = options;
676 int len;
678 #ifdef IPCONFIG_DEBUG
679 printk("DHCP: Sending message type %d\n", mt);
680 #endif
682 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
683 e += 4;
685 *e++ = 53; /* DHCP message type */
686 *e++ = 1;
687 *e++ = mt;
689 if (mt == DHCPREQUEST) {
690 *e++ = 54; /* Server ID (IP address) */
691 *e++ = 4;
692 memcpy(e, &ic_servaddr, 4);
693 e += 4;
695 *e++ = 50; /* Requested IP address */
696 *e++ = 4;
697 memcpy(e, &ic_myaddr, 4);
698 e += 4;
701 /* always? */
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 */
714 *e++ = 55; /* Parameter request list */
715 *e++ = sizeof(ic_req_params);
716 memcpy(e, ic_req_params, sizeof(ic_req_params));
717 e += sizeof(ic_req_params);
719 if (ic_host_name_set) {
720 *e++ = 12; /* host-name */
721 len = strlen(utsname()->nodename);
722 *e++ = len;
723 memcpy(e, utsname()->nodename, len);
724 e += len;
726 if (*vendor_class_identifier) {
727 pr_info("DHCP: sending class identifier \"%s\"\n",
728 vendor_class_identifier);
729 *e++ = 60; /* Class-identifier */
730 len = strlen(vendor_class_identifier);
731 *e++ = len;
732 memcpy(e, vendor_class_identifier, len);
733 e += len;
735 len = strlen(dhcp_client_identifier + 1);
736 /* the minimum length of identifier is 2, include 1 byte type,
737 * and can not be larger than the length of options
739 if (len >= 1 && len < 312 - (e - options) - 1) {
740 *e++ = 61;
741 *e++ = len + 1;
742 memcpy(e, dhcp_client_identifier, len + 1);
743 e += len + 1;
747 *e++ = 255; /* End of the list */
750 #endif /* IPCONFIG_DHCP */
752 static void __init ic_bootp_init_ext(u8 *e)
754 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
755 e += 4;
756 *e++ = 1; /* Subnet mask request */
757 *e++ = 4;
758 e += 4;
759 *e++ = 3; /* Default gateway request */
760 *e++ = 4;
761 e += 4;
762 *e++ = 5; /* Name server request */
763 *e++ = 8;
764 e += 8;
765 *e++ = 12; /* Host name request */
766 *e++ = 32;
767 e += 32;
768 *e++ = 40; /* NIS Domain name request */
769 *e++ = 32;
770 e += 32;
771 *e++ = 17; /* Boot path */
772 *e++ = 40;
773 e += 40;
775 *e++ = 57; /* set extension buffer size for reply */
776 *e++ = 2;
777 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
778 *e++ = 150;
780 *e++ = 255; /* End of the list */
785 * Initialize the DHCP/BOOTP mechanism.
787 static inline void __init ic_bootp_init(void)
789 ic_nameservers_predef();
791 dev_add_pack(&bootp_packet_type);
796 * DHCP/BOOTP cleanup.
798 static inline void __init ic_bootp_cleanup(void)
800 dev_remove_pack(&bootp_packet_type);
805 * Send DHCP/BOOTP request to single interface.
807 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
809 struct net_device *dev = d->dev;
810 struct sk_buff *skb;
811 struct bootp_pkt *b;
812 struct iphdr *h;
813 int hlen = LL_RESERVED_SPACE(dev);
814 int tlen = dev->needed_tailroom;
816 /* Allocate packet */
817 skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
818 GFP_KERNEL);
819 if (!skb)
820 return;
821 skb_reserve(skb, hlen);
822 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
823 memset(b, 0, sizeof(struct bootp_pkt));
825 /* Construct IP header */
826 skb_reset_network_header(skb);
827 h = ip_hdr(skb);
828 h->version = 4;
829 h->ihl = 5;
830 h->tot_len = htons(sizeof(struct bootp_pkt));
831 h->frag_off = htons(IP_DF);
832 h->ttl = 64;
833 h->protocol = IPPROTO_UDP;
834 h->daddr = htonl(INADDR_BROADCAST);
835 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
837 /* Construct UDP header */
838 b->udph.source = htons(68);
839 b->udph.dest = htons(67);
840 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
841 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
843 /* Construct DHCP/BOOTP header */
844 b->op = BOOTP_REQUEST;
845 if (dev->type < 256) /* check for false types */
846 b->htype = dev->type;
847 else if (dev->type == ARPHRD_FDDI)
848 b->htype = ARPHRD_ETHER;
849 else {
850 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
851 b->htype = dev->type; /* can cause undefined behavior */
854 /* server_ip and your_ip address are both already zero per RFC2131 */
855 b->hlen = dev->addr_len;
856 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
857 b->secs = htons(jiffies_diff / HZ);
858 b->xid = d->xid;
860 /* add DHCP options or BOOTP extensions */
861 #ifdef IPCONFIG_DHCP
862 if (ic_proto_enabled & IC_USE_DHCP)
863 ic_dhcp_init_options(b->exten);
864 else
865 #endif
866 ic_bootp_init_ext(b->exten);
868 /* Chain packet down the line... */
869 skb->dev = dev;
870 skb->protocol = htons(ETH_P_IP);
871 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
872 dev->broadcast, dev->dev_addr, skb->len) < 0) {
873 kfree_skb(skb);
874 printk("E");
875 return;
878 if (dev_queue_xmit(skb) < 0)
879 printk("E");
884 * Copy BOOTP-supplied string if not already set.
886 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
888 if (!len)
889 return 0;
890 if (len > max-1)
891 len = max-1;
892 memcpy(dest, src, len);
893 dest[len] = '\0';
894 return 1;
899 * Process BOOTP extensions.
901 static void __init ic_do_bootp_ext(u8 *ext)
903 u8 servers;
904 int i;
905 __be16 mtu;
907 #ifdef IPCONFIG_DEBUG
908 u8 *c;
910 printk("DHCP/BOOTP: Got extension %d:",*ext);
911 for (c=ext+2; c<ext+2+ext[1]; c++)
912 printk(" %02x", *c);
913 printk("\n");
914 #endif
916 switch (*ext++) {
917 case 1: /* Subnet mask */
918 if (ic_netmask == NONE)
919 memcpy(&ic_netmask, ext+1, 4);
920 break;
921 case 3: /* Default gateway */
922 if (ic_gateway == NONE)
923 memcpy(&ic_gateway, ext+1, 4);
924 break;
925 case 6: /* DNS server */
926 servers= *ext/4;
927 if (servers > CONF_NAMESERVERS_MAX)
928 servers = CONF_NAMESERVERS_MAX;
929 for (i = 0; i < servers; i++) {
930 if (ic_nameservers[i] == NONE)
931 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
933 break;
934 case 12: /* Host name */
935 ic_bootp_string(utsname()->nodename, ext+1, *ext,
936 __NEW_UTS_LEN);
937 ic_host_name_set = 1;
938 break;
939 case 15: /* Domain name (DNS) */
940 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
941 break;
942 case 17: /* Root path */
943 if (!root_server_path[0])
944 ic_bootp_string(root_server_path, ext+1, *ext,
945 sizeof(root_server_path));
946 break;
947 case 26: /* Interface MTU */
948 memcpy(&mtu, ext+1, sizeof(mtu));
949 ic_dev_mtu = ntohs(mtu);
950 break;
951 case 40: /* NIS Domain name (_not_ DNS) */
952 ic_bootp_string(utsname()->domainname, ext+1, *ext,
953 __NEW_UTS_LEN);
954 break;
960 * Receive BOOTP reply.
962 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
964 struct bootp_pkt *b;
965 struct iphdr *h;
966 struct ic_device *d;
967 int len, ext_len;
969 if (!net_eq(dev_net(dev), &init_net))
970 goto drop;
972 /* Perform verifications before taking the lock. */
973 if (skb->pkt_type == PACKET_OTHERHOST)
974 goto drop;
976 skb = skb_share_check(skb, GFP_ATOMIC);
977 if (!skb)
978 return NET_RX_DROP;
980 if (!pskb_may_pull(skb,
981 sizeof(struct iphdr) +
982 sizeof(struct udphdr)))
983 goto drop;
985 b = (struct bootp_pkt *)skb_network_header(skb);
986 h = &b->iph;
988 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
989 goto drop;
991 /* Fragments are not supported */
992 if (ip_is_fragment(h)) {
993 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
994 goto drop;
997 if (skb->len < ntohs(h->tot_len))
998 goto drop;
1000 if (ip_fast_csum((char *) h, h->ihl))
1001 goto drop;
1003 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
1004 goto drop;
1006 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
1007 goto drop;
1009 len = ntohs(b->udph.len) - sizeof(struct udphdr);
1010 ext_len = len - (sizeof(*b) -
1011 sizeof(struct iphdr) -
1012 sizeof(struct udphdr) -
1013 sizeof(b->exten));
1014 if (ext_len < 0)
1015 goto drop;
1017 /* Ok the front looks good, make sure we can get at the rest. */
1018 if (!pskb_may_pull(skb, skb->len))
1019 goto drop;
1021 b = (struct bootp_pkt *)skb_network_header(skb);
1022 h = &b->iph;
1024 /* One reply at a time, please. */
1025 spin_lock(&ic_recv_lock);
1027 /* If we already have a reply, just drop the packet */
1028 if (ic_got_reply)
1029 goto drop_unlock;
1031 /* Find the ic_device that the packet arrived on */
1032 d = ic_first_dev;
1033 while (d && d->dev != dev)
1034 d = d->next;
1035 if (!d)
1036 goto drop_unlock; /* should never happen */
1038 /* Is it a reply to our BOOTP request? */
1039 if (b->op != BOOTP_REPLY ||
1040 b->xid != d->xid) {
1041 net_err_ratelimited("DHCP/BOOTP: Reply not for us, op[%x] xid[%x]\n",
1042 b->op, b->xid);
1043 goto drop_unlock;
1046 /* Is it a reply for the device we are configuring? */
1047 if (b->xid != ic_dev_xid) {
1048 net_err_ratelimited("DHCP/BOOTP: Ignoring delayed packet\n");
1049 goto drop_unlock;
1052 /* Parse extensions */
1053 if (ext_len >= 4 &&
1054 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
1055 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
1056 u8 *ext;
1058 #ifdef IPCONFIG_DHCP
1059 if (ic_proto_enabled & IC_USE_DHCP) {
1060 __be32 server_id = NONE;
1061 int mt = 0;
1063 ext = &b->exten[4];
1064 while (ext < end && *ext != 0xff) {
1065 u8 *opt = ext++;
1066 if (*opt == 0) /* Padding */
1067 continue;
1068 ext += *ext + 1;
1069 if (ext >= end)
1070 break;
1071 switch (*opt) {
1072 case 53: /* Message type */
1073 if (opt[1])
1074 mt = opt[2];
1075 break;
1076 case 54: /* Server ID (IP address) */
1077 if (opt[1] >= 4)
1078 memcpy(&server_id, opt + 2, 4);
1079 break;
1083 #ifdef IPCONFIG_DEBUG
1084 printk("DHCP: Got message type %d\n", mt);
1085 #endif
1087 switch (mt) {
1088 case DHCPOFFER:
1089 /* While in the process of accepting one offer,
1090 * ignore all others.
1092 if (ic_myaddr != NONE)
1093 goto drop_unlock;
1095 /* Let's accept that offer. */
1096 ic_myaddr = b->your_ip;
1097 ic_servaddr = server_id;
1098 #ifdef IPCONFIG_DEBUG
1099 printk("DHCP: Offered address %pI4 by server %pI4\n",
1100 &ic_myaddr, &b->iph.saddr);
1101 #endif
1102 /* The DHCP indicated server address takes
1103 * precedence over the bootp header one if
1104 * they are different.
1106 if ((server_id != NONE) &&
1107 (b->server_ip != server_id))
1108 b->server_ip = ic_servaddr;
1109 break;
1111 case DHCPACK:
1112 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1113 goto drop_unlock;
1115 /* Yeah! */
1116 break;
1118 default:
1119 /* Urque. Forget it*/
1120 ic_myaddr = NONE;
1121 ic_servaddr = NONE;
1122 goto drop_unlock;
1125 ic_dhcp_msgtype = mt;
1128 #endif /* IPCONFIG_DHCP */
1130 ext = &b->exten[4];
1131 while (ext < end && *ext != 0xff) {
1132 u8 *opt = ext++;
1133 if (*opt == 0) /* Padding */
1134 continue;
1135 ext += *ext + 1;
1136 if (ext < end)
1137 ic_do_bootp_ext(opt);
1141 /* We have a winner! */
1142 ic_dev = dev;
1143 ic_myaddr = b->your_ip;
1144 ic_servaddr = b->server_ip;
1145 ic_addrservaddr = b->iph.saddr;
1146 if (ic_gateway == NONE && b->relay_ip)
1147 ic_gateway = b->relay_ip;
1148 if (ic_nameservers[0] == NONE)
1149 ic_nameservers[0] = ic_servaddr;
1150 ic_got_reply = IC_BOOTP;
1152 drop_unlock:
1153 /* Show's over. Nothing to see here. */
1154 spin_unlock(&ic_recv_lock);
1156 drop:
1157 /* Throw the packet out. */
1158 kfree_skb(skb);
1160 return 0;
1164 #endif
1168 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1171 #ifdef IPCONFIG_DYNAMIC
1173 static int __init ic_dynamic(void)
1175 int retries;
1176 struct ic_device *d;
1177 unsigned long start_jiffies, timeout, jiff;
1178 int do_bootp = ic_proto_have_if & IC_BOOTP;
1179 int do_rarp = ic_proto_have_if & IC_RARP;
1182 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1183 * This routine gets only called when some pieces of information
1184 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1186 if (!ic_proto_enabled) {
1187 pr_err("IP-Config: Incomplete network configuration information\n");
1188 return -1;
1191 #ifdef IPCONFIG_BOOTP
1192 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1193 pr_err("DHCP/BOOTP: No suitable device found\n");
1194 #endif
1195 #ifdef IPCONFIG_RARP
1196 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1197 pr_err("RARP: No suitable device found\n");
1198 #endif
1200 if (!ic_proto_have_if)
1201 /* Error message already printed */
1202 return -1;
1205 * Setup protocols
1207 #ifdef IPCONFIG_BOOTP
1208 if (do_bootp)
1209 ic_bootp_init();
1210 #endif
1211 #ifdef IPCONFIG_RARP
1212 if (do_rarp)
1213 ic_rarp_init();
1214 #endif
1217 * Send requests and wait, until we get an answer. This loop
1218 * seems to be a terrible waste of CPU time, but actually there is
1219 * only one process running at all, so we don't need to use any
1220 * scheduler functions.
1221 * [Actually we could now, but the nothing else running note still
1222 * applies.. - AC]
1224 pr_notice("Sending %s%s%s requests .",
1225 do_bootp
1226 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1227 (do_bootp && do_rarp) ? " and " : "",
1228 do_rarp ? "RARP" : "");
1230 start_jiffies = jiffies;
1231 d = ic_first_dev;
1232 retries = CONF_SEND_RETRIES;
1233 get_random_bytes(&timeout, sizeof(timeout));
1234 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
1235 for (;;) {
1236 #ifdef IPCONFIG_BOOTP
1237 /* Track the device we are configuring */
1238 ic_dev_xid = d->xid;
1240 if (do_bootp && (d->able & IC_BOOTP))
1241 ic_bootp_send_if(d, jiffies - start_jiffies);
1242 #endif
1243 #ifdef IPCONFIG_RARP
1244 if (do_rarp && (d->able & IC_RARP))
1245 ic_rarp_send_if(d);
1246 #endif
1248 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1249 while (time_before(jiffies, jiff) && !ic_got_reply)
1250 schedule_timeout_uninterruptible(1);
1251 #ifdef IPCONFIG_DHCP
1252 /* DHCP isn't done until we get a DHCPACK. */
1253 if ((ic_got_reply & IC_BOOTP) &&
1254 (ic_proto_enabled & IC_USE_DHCP) &&
1255 ic_dhcp_msgtype != DHCPACK) {
1256 ic_got_reply = 0;
1257 pr_cont(",");
1258 continue;
1260 #endif /* IPCONFIG_DHCP */
1262 if (ic_got_reply) {
1263 pr_cont(" OK\n");
1264 break;
1267 if ((d = d->next))
1268 continue;
1270 if (! --retries) {
1271 pr_cont(" timed out!\n");
1272 break;
1275 d = ic_first_dev;
1277 timeout = timeout CONF_TIMEOUT_MULT;
1278 if (timeout > CONF_TIMEOUT_MAX)
1279 timeout = CONF_TIMEOUT_MAX;
1281 pr_cont(".");
1284 #ifdef IPCONFIG_BOOTP
1285 if (do_bootp)
1286 ic_bootp_cleanup();
1287 #endif
1288 #ifdef IPCONFIG_RARP
1289 if (do_rarp)
1290 ic_rarp_cleanup();
1291 #endif
1293 if (!ic_got_reply) {
1294 ic_myaddr = NONE;
1295 return -1;
1298 printk("IP-Config: Got %s answer from %pI4, ",
1299 ((ic_got_reply & IC_RARP) ? "RARP"
1300 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1301 &ic_addrservaddr);
1302 pr_cont("my address is %pI4\n", &ic_myaddr);
1304 return 0;
1307 #endif /* IPCONFIG_DYNAMIC */
1309 #ifdef CONFIG_PROC_FS
1311 static int pnp_seq_show(struct seq_file *seq, void *v)
1313 int i;
1315 if (ic_proto_used & IC_PROTO)
1316 seq_printf(seq, "#PROTO: %s\n",
1317 (ic_proto_used & IC_RARP) ? "RARP"
1318 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1319 else
1320 seq_puts(seq, "#MANUAL\n");
1322 if (ic_domain[0])
1323 seq_printf(seq,
1324 "domain %s\n", ic_domain);
1325 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1326 if (ic_nameservers[i] != NONE)
1327 seq_printf(seq, "nameserver %pI4\n",
1328 &ic_nameservers[i]);
1330 if (ic_servaddr != NONE)
1331 seq_printf(seq, "bootserver %pI4\n",
1332 &ic_servaddr);
1333 return 0;
1336 static int pnp_seq_open(struct inode *indoe, struct file *file)
1338 return single_open(file, pnp_seq_show, NULL);
1341 static const struct file_operations pnp_seq_fops = {
1342 .owner = THIS_MODULE,
1343 .open = pnp_seq_open,
1344 .read = seq_read,
1345 .llseek = seq_lseek,
1346 .release = single_release,
1348 #endif /* CONFIG_PROC_FS */
1351 * Extract IP address from the parameter string if needed. Note that we
1352 * need to have root_server_addr set _before_ IPConfig gets called as it
1353 * can override it.
1355 __be32 __init root_nfs_parse_addr(char *name)
1357 __be32 addr;
1358 int octets = 0;
1359 char *cp, *cq;
1361 cp = cq = name;
1362 while (octets < 4) {
1363 while (*cp >= '0' && *cp <= '9')
1364 cp++;
1365 if (cp == cq || cp - cq > 3)
1366 break;
1367 if (*cp == '.' || octets == 3)
1368 octets++;
1369 if (octets < 4)
1370 cp++;
1371 cq = cp;
1373 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1374 if (*cp == ':')
1375 *cp++ = '\0';
1376 addr = in_aton(name);
1377 memmove(name, cp, strlen(cp) + 1);
1378 } else
1379 addr = NONE;
1381 return addr;
1384 #define DEVICE_WAIT_MAX 12 /* 12 seconds */
1386 static int __init wait_for_devices(void)
1388 int i;
1390 for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1391 struct net_device *dev;
1392 int found = 0;
1394 rtnl_lock();
1395 for_each_netdev(&init_net, dev) {
1396 if (ic_is_init_dev(dev)) {
1397 found = 1;
1398 break;
1401 rtnl_unlock();
1402 if (found)
1403 return 0;
1404 ssleep(1);
1406 return -ENODEV;
1410 * IP Autoconfig dispatcher.
1413 static int __init ip_auto_config(void)
1415 __be32 addr;
1416 #ifdef IPCONFIG_DYNAMIC
1417 int retries = CONF_OPEN_RETRIES;
1418 #endif
1419 int err;
1420 unsigned int i;
1422 #ifdef CONFIG_PROC_FS
1423 proc_create("pnp", S_IRUGO, init_net.proc_net, &pnp_seq_fops);
1424 #endif /* CONFIG_PROC_FS */
1426 if (!ic_enable)
1427 return 0;
1429 DBG(("IP-Config: Entered.\n"));
1430 #ifdef IPCONFIG_DYNAMIC
1431 try_try_again:
1432 #endif
1433 /* Wait for devices to appear */
1434 err = wait_for_devices();
1435 if (err)
1436 return err;
1438 /* Setup all network devices */
1439 err = ic_open_devs();
1440 if (err)
1441 return err;
1443 /* Give drivers a chance to settle */
1444 msleep(CONF_POST_OPEN);
1447 * If the config information is insufficient (e.g., our IP address or
1448 * IP address of the boot server is missing or we have multiple network
1449 * interfaces and no default was set), use BOOTP or RARP to get the
1450 * missing values.
1452 if (ic_myaddr == NONE ||
1453 #ifdef CONFIG_ROOT_NFS
1454 (root_server_addr == NONE &&
1455 ic_servaddr == NONE &&
1456 ROOT_DEV == Root_NFS) ||
1457 #endif
1458 ic_first_dev->next) {
1459 #ifdef IPCONFIG_DYNAMIC
1460 if (ic_dynamic() < 0) {
1461 ic_close_devs();
1464 * I don't know why, but sometimes the
1465 * eepro100 driver (at least) gets upset and
1466 * doesn't work the first time it's opened.
1467 * But then if you close it and reopen it, it
1468 * works just fine. So we need to try that at
1469 * least once before giving up.
1471 * Also, if the root will be NFS-mounted, we
1472 * have nowhere to go if DHCP fails. So we
1473 * just have to keep trying forever.
1475 * -- Chip
1477 #ifdef CONFIG_ROOT_NFS
1478 if (ROOT_DEV == Root_NFS) {
1479 pr_err("IP-Config: Retrying forever (NFS root)...\n");
1480 goto try_try_again;
1482 #endif
1484 if (--retries) {
1485 pr_err("IP-Config: Reopening network devices...\n");
1486 goto try_try_again;
1489 /* Oh, well. At least we tried. */
1490 pr_err("IP-Config: Auto-configuration of network failed\n");
1491 return -1;
1493 #else /* !DYNAMIC */
1494 pr_err("IP-Config: Incomplete network configuration information\n");
1495 ic_close_devs();
1496 return -1;
1497 #endif /* IPCONFIG_DYNAMIC */
1498 } else {
1499 /* Device selected manually or only one device -> use it */
1500 ic_dev = ic_first_dev->dev;
1503 addr = root_nfs_parse_addr(root_server_path);
1504 if (root_server_addr == NONE)
1505 root_server_addr = addr;
1508 * Use defaults wherever applicable.
1510 if (ic_defaults() < 0)
1511 return -1;
1514 * Close all network devices except the device we've
1515 * autoconfigured and set up routes.
1517 ic_close_devs();
1518 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1519 return -1;
1522 * Record which protocol was actually used.
1524 #ifdef IPCONFIG_DYNAMIC
1525 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1526 #endif
1528 #ifndef IPCONFIG_SILENT
1530 * Clue in the operator.
1532 pr_info("IP-Config: Complete:\n");
1534 pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
1535 ic_dev->name, ic_dev->addr_len, ic_dev->dev_addr,
1536 &ic_myaddr, &ic_netmask, &ic_gateway);
1537 pr_info(" host=%s, domain=%s, nis-domain=%s\n",
1538 utsname()->nodename, ic_domain, utsname()->domainname);
1539 pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1540 &ic_servaddr, &root_server_addr, root_server_path);
1541 if (ic_dev_mtu)
1542 pr_cont(", mtu=%d", ic_dev_mtu);
1543 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
1544 if (ic_nameservers[i] != NONE) {
1545 pr_info(" nameserver%u=%pI4",
1546 i, &ic_nameservers[i]);
1547 break;
1549 for (i++; i < CONF_NAMESERVERS_MAX; i++)
1550 if (ic_nameservers[i] != NONE)
1551 pr_cont(", nameserver%u=%pI4", i, &ic_nameservers[i]);
1552 pr_cont("\n");
1553 #endif /* !SILENT */
1555 return 0;
1558 late_initcall(ip_auto_config);
1562 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1563 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
1565 static int __init ic_proto_name(char *name)
1567 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1568 return 1;
1570 if (!strcmp(name, "off") || !strcmp(name, "none")) {
1571 return 0;
1573 #ifdef CONFIG_IP_PNP_DHCP
1574 else if (!strncmp(name, "dhcp", 4)) {
1575 char *client_id;
1577 ic_proto_enabled &= ~IC_RARP;
1578 client_id = strstr(name, "dhcp,");
1579 if (client_id) {
1580 char *v;
1582 client_id = client_id + 5;
1583 v = strchr(client_id, ',');
1584 if (!v)
1585 return 1;
1586 *v = 0;
1587 if (kstrtou8(client_id, 0, dhcp_client_identifier))
1588 DBG("DHCP: Invalid client identifier type\n");
1589 strncpy(dhcp_client_identifier + 1, v + 1, 251);
1590 *v = ',';
1592 return 1;
1594 #endif
1595 #ifdef CONFIG_IP_PNP_BOOTP
1596 else if (!strcmp(name, "bootp")) {
1597 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1598 return 1;
1600 #endif
1601 #ifdef CONFIG_IP_PNP_RARP
1602 else if (!strcmp(name, "rarp")) {
1603 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1604 return 1;
1606 #endif
1607 #ifdef IPCONFIG_DYNAMIC
1608 else if (!strcmp(name, "both")) {
1609 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1610 return 1;
1612 #endif
1613 return 0;
1616 static int __init ip_auto_config_setup(char *addrs)
1618 char *cp, *ip, *dp;
1619 int num = 0;
1621 ic_set_manually = 1;
1622 ic_enable = 1;
1625 * If any dhcp, bootp etc options are set, leave autoconfig on
1626 * and skip the below static IP processing.
1628 if (ic_proto_name(addrs))
1629 return 1;
1631 /* If no static IP is given, turn off autoconfig and bail. */
1632 if (*addrs == 0 ||
1633 strcmp(addrs, "off") == 0 ||
1634 strcmp(addrs, "none") == 0) {
1635 ic_enable = 0;
1636 return 1;
1639 ic_nameservers_predef();
1641 /* Parse string for static IP assignment. */
1642 ip = addrs;
1643 while (ip && *ip) {
1644 if ((cp = strchr(ip, ':')))
1645 *cp++ = '\0';
1646 if (strlen(ip) > 0) {
1647 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1648 switch (num) {
1649 case 0:
1650 if ((ic_myaddr = in_aton(ip)) == ANY)
1651 ic_myaddr = NONE;
1652 break;
1653 case 1:
1654 if ((ic_servaddr = in_aton(ip)) == ANY)
1655 ic_servaddr = NONE;
1656 break;
1657 case 2:
1658 if ((ic_gateway = in_aton(ip)) == ANY)
1659 ic_gateway = NONE;
1660 break;
1661 case 3:
1662 if ((ic_netmask = in_aton(ip)) == ANY)
1663 ic_netmask = NONE;
1664 break;
1665 case 4:
1666 if ((dp = strchr(ip, '.'))) {
1667 *dp++ = '\0';
1668 strlcpy(utsname()->domainname, dp,
1669 sizeof(utsname()->domainname));
1671 strlcpy(utsname()->nodename, ip,
1672 sizeof(utsname()->nodename));
1673 ic_host_name_set = 1;
1674 break;
1675 case 5:
1676 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1677 break;
1678 case 6:
1679 if (ic_proto_name(ip) == 0 &&
1680 ic_myaddr == NONE) {
1681 ic_enable = 0;
1683 break;
1684 case 7:
1685 if (CONF_NAMESERVERS_MAX >= 1) {
1686 ic_nameservers[0] = in_aton(ip);
1687 if (ic_nameservers[0] == ANY)
1688 ic_nameservers[0] = NONE;
1690 break;
1691 case 8:
1692 if (CONF_NAMESERVERS_MAX >= 2) {
1693 ic_nameservers[1] = in_aton(ip);
1694 if (ic_nameservers[1] == ANY)
1695 ic_nameservers[1] = NONE;
1697 break;
1700 ip = cp;
1701 num++;
1704 return 1;
1706 __setup("ip=", ip_auto_config_setup);
1708 static int __init nfsaddrs_config_setup(char *addrs)
1710 return ip_auto_config_setup(addrs);
1712 __setup("nfsaddrs=", nfsaddrs_config_setup);
1714 static int __init vendor_class_identifier_setup(char *addrs)
1716 if (strlcpy(vendor_class_identifier, addrs,
1717 sizeof(vendor_class_identifier))
1718 >= sizeof(vendor_class_identifier))
1719 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"",
1720 vendor_class_identifier);
1721 return 1;
1723 __setup("dhcpclass=", vendor_class_identifier_setup);