GNU Linux-libre 4.4.288-gnu1
[releases.git] / net / ipv4 / ipconfig.c
1 /*
2  *  Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
3  *  user-supplied information to configure own IP address and routes.
4  *
5  *  Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6  *
7  *  Derived from network configuration code in fs/nfs/nfsroot.c,
8  *  originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
9  *
10  *  BOOTP rewritten to construct and analyse packets itself instead
11  *  of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
12  *                                           -- MJ, December 1998
13  *
14  *  Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
15  *  initialization scheme.
16  *      - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
17  *
18  *  DHCP support added.  To users this looks like a whole separate
19  *  protocol, but we know it's just a bag on the side of BOOTP.
20  *              -- Chip Salzenberg <chip@valinux.com>, May 2000
21  *
22  *  Ported DHCP support from 2.2.16 to 2.4.0-test4
23  *              -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
24  *
25  *  Merged changes from 2.2.19 into 2.4.3
26  *              -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
27  *
28  *  Multiple Nameservers in /proc/net/pnp
29  *              --  Josef Siemes <jsiemes@web.de>, Aug 2002
30  */
31
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/jiffies.h>
36 #include <linux/random.h>
37 #include <linux/init.h>
38 #include <linux/utsname.h>
39 #include <linux/in.h>
40 #include <linux/if.h>
41 #include <linux/inet.h>
42 #include <linux/inetdevice.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
46 #include <linux/ip.h>
47 #include <linux/socket.h>
48 #include <linux/route.h>
49 #include <linux/udp.h>
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
52 #include <linux/major.h>
53 #include <linux/root_dev.h>
54 #include <linux/delay.h>
55 #include <linux/nfs_fs.h>
56 #include <linux/slab.h>
57 #include <linux/export.h>
58 #include <net/net_namespace.h>
59 #include <net/arp.h>
60 #include <net/ip.h>
61 #include <net/ipconfig.h>
62 #include <net/route.h>
63
64 #include <asm/uaccess.h>
65 #include <net/checksum.h>
66 #include <asm/processor.h>
67
68 /* Define this to allow debugging output */
69 #undef IPCONFIG_DEBUG
70
71 #ifdef IPCONFIG_DEBUG
72 #define DBG(x) printk x
73 #else
74 #define DBG(x) do { } while(0)
75 #endif
76
77 #if defined(CONFIG_IP_PNP_DHCP)
78 #define IPCONFIG_DHCP
79 #endif
80 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
81 #define IPCONFIG_BOOTP
82 #endif
83 #if defined(CONFIG_IP_PNP_RARP)
84 #define IPCONFIG_RARP
85 #endif
86 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
87 #define IPCONFIG_DYNAMIC
88 #endif
89
90 /* Define the friendly delay before and after opening net devices */
91 #define CONF_POST_OPEN          10      /* After opening: 10 msecs */
92 #define CONF_CARRIER_TIMEOUT    120000  /* Wait for carrier timeout */
93
94 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
95 #define CONF_OPEN_RETRIES       2       /* (Re)open devices twice */
96 #define CONF_SEND_RETRIES       6       /* Send six requests per open */
97 #define CONF_INTER_TIMEOUT      (HZ)    /* Inter-device timeout: 1 second */
98 #define CONF_BASE_TIMEOUT       (HZ*2)  /* Initial timeout: 2 seconds */
99 #define CONF_TIMEOUT_RANDOM     (HZ)    /* Maximum amount of randomization */
100 #define CONF_TIMEOUT_MULT       *7/4    /* Rate of timeout growth */
101 #define CONF_TIMEOUT_MAX        (HZ*30) /* Maximum allowed timeout */
102 #define CONF_NAMESERVERS_MAX   3       /* Maximum number of nameservers
103                                            - '3' from resolv.h */
104
105 #define NONE cpu_to_be32(INADDR_NONE)
106 #define ANY cpu_to_be32(INADDR_ANY)
107
108 /*
109  * Public IP configuration
110  */
111
112 /* This is used by platforms which might be able to set the ipconfig
113  * variables using firmware environment vars.  If this is set, it will
114  * ignore such firmware variables.
115  */
116 int ic_set_manually __initdata = 0;             /* IPconfig parameters set manually */
117
118 static int ic_enable __initdata;                /* IP config enabled? */
119
120 /* Protocol choice */
121 int ic_proto_enabled __initdata = 0
122 #ifdef IPCONFIG_BOOTP
123                         | IC_BOOTP
124 #endif
125 #ifdef CONFIG_IP_PNP_DHCP
126                         | IC_USE_DHCP
127 #endif
128 #ifdef IPCONFIG_RARP
129                         | IC_RARP
130 #endif
131                         ;
132
133 static int ic_host_name_set __initdata; /* Host name set by us? */
134
135 __be32 ic_myaddr = NONE;                /* My IP address */
136 static __be32 ic_netmask = NONE;        /* Netmask for local subnet */
137 __be32 ic_gateway = NONE;       /* Gateway IP address */
138
139 __be32 ic_addrservaddr = NONE;  /* IP Address of the IP addresses'server */
140
141 __be32 ic_servaddr = NONE;      /* Boot server IP address */
142
143 __be32 root_server_addr = NONE; /* Address of NFS server */
144 u8 root_server_path[256] = { 0, };      /* Path to mount as root */
145
146 /* vendor class identifier */
147 static char vendor_class_identifier[253] __initdata;
148
149 #if defined(CONFIG_IP_PNP_DHCP)
150 static char dhcp_client_identifier[253] __initdata;
151 #endif
152
153 /* Persistent data: */
154
155 #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 */
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 net_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 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);
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",
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;
246                         }
247                         if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
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                         DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
262                                 dev->name, able, d->xid));
263                 }
264         }
265
266         /* no point in waiting if we could not bring up at least one device */
267         if (!ic_first_dev)
268                 goto have_carrier;
269
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;
276
277                 for_each_netdev(&init_net, dev)
278                         if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
279                                 goto have_carrier;
280
281                 msleep(1);
282
283                 if (time_before(jiffies, next_msg))
284                         continue;
285
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);
290         }
291 have_carrier:
292         rtnl_unlock();
293
294         *last = NULL;
295
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;
303         }
304         return 0;
305 }
306
307 static void __init ic_close_devs(void)
308 {
309         struct ic_device *d, *next;
310         struct net_device *dev;
311
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);
320                 }
321                 kfree(d);
322         }
323         rtnl_unlock();
324 }
325
326 /*
327  *      Interface to various network functions.
328  */
329
330 static inline void
331 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
332 {
333         sin->sin_family = AF_INET;
334         sin->sin_addr.s_addr = addr;
335         sin->sin_port = port;
336 }
337
338 static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
339 {
340         int res;
341
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;
347 }
348
349 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
350 {
351         int res;
352
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;
358 }
359
360 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
361 {
362         int res;
363
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;
369 }
370
371 /*
372  *      Set up interface addresses and routes.
373  */
374
375 static int __init ic_setup_if(void)
376 {
377         struct ifreq ir;
378         struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
379         int err;
380
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;
388         }
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;
394         }
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;
400         }
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.
404          */
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);
411         }
412         return 0;
413 }
414
415 static int __init ic_setup_routes(void)
416 {
417         /* No need to setup device routes, only the default route... */
418
419         if (ic_gateway != NONE) {
420                 struct rtentry rm;
421                 int err;
422
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;
427                 }
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;
436                 }
437         }
438
439         return 0;
440 }
441
442 /*
443  *      Fill in default values for all missing parameters.
444  */
445
446 static int __init ic_defaults(void)
447 {
448         /*
449          *      At this point we have no userspace running so need not
450          *      claim locks on system_utsname
451          */
452
453         if (!ic_host_name_set)
454                 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
455
456         if (root_server_addr == NONE)
457                 root_server_addr = ic_servaddr;
458
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;
470                 }
471                 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
472         }
473
474         return 0;
475 }
476
477 /*
478  *      RARP support.
479  */
480
481 #ifdef IPCONFIG_RARP
482
483 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
484
485 static struct packet_type rarp_packet_type __initdata = {
486         .type = cpu_to_be16(ETH_P_RARP),
487         .func = ic_rarp_recv,
488 };
489
490 static inline void __init ic_rarp_init(void)
491 {
492         dev_add_pack(&rarp_packet_type);
493 }
494
495 static inline void __init ic_rarp_cleanup(void)
496 {
497         dev_remove_pack(&rarp_packet_type);
498 }
499
500 /*
501  *  Process received RARP packet.
502  */
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)
505 {
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;
511
512         if (!net_eq(dev_net(dev), &init_net))
513                 goto drop;
514
515         skb = skb_share_check(skb, GFP_ATOMIC);
516         if (!skb)
517                 return NET_RX_DROP;
518
519         if (!pskb_may_pull(skb, sizeof(struct arphdr)))
520                 goto drop;
521
522         /* Basic sanity checks can be done without the lock.  */
523         rarp = (struct arphdr *)skb_transport_header(skb);
524
525         /* If this test doesn't pass, it's not IP, or we should
526          * ignore it anyway.
527          */
528         if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
529                 goto drop;
530
531         /* If it's not a RARP reply, delete it. */
532         if (rarp->ar_op != htons(ARPOP_RREPLY))
533                 goto drop;
534
535         /* If it's not Ethernet, delete it. */
536         if (rarp->ar_pro != htons(ETH_P_IP))
537                 goto drop;
538
539         if (!pskb_may_pull(skb, arp_hdr_len(dev)))
540                 goto drop;
541
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);
545
546         /* One reply at a time, please. */
547         spin_lock(&ic_recv_lock);
548
549         /* If we already have a reply, just drop the packet */
550         if (ic_got_reply)
551                 goto drop_unlock;
552
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 */
559
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);
567
568         /* Discard packets which are not meant for us. */
569         if (memcmp(tha, dev->dev_addr, dev->addr_len))
570                 goto drop_unlock;
571
572         /* Discard packets which are not from specified server. */
573         if (ic_servaddr != NONE && ic_servaddr != sip)
574                 goto drop_unlock;
575
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;
583
584 drop_unlock:
585         /* Show's over.  Nothing to see here.  */
586         spin_unlock(&ic_recv_lock);
587
588 drop:
589         /* Throw the packet out. */
590         kfree_skb(skb);
591         return 0;
592 }
593
594
595 /*
596  *  Send RARP request packet over a single interface.
597  */
598 static void __init ic_rarp_send_if(struct ic_device *d)
599 {
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);
603 }
604 #endif
605
606 /*
607  *  Predefine Nameservers
608  */
609 static inline void __init ic_nameservers_predef(void)
610 {
611         int i;
612
613         for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
614                 ic_nameservers[i] = NONE;
615 }
616
617 /*
618  *      DHCP/BOOTP support.
619  */
620
621 #ifdef IPCONFIG_BOOTP
622
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 */
641 };
642
643 /* packet ops */
644 #define BOOTP_REQUEST   1
645 #define BOOTP_REPLY     2
646
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
656
657 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
658
659 static struct packet_type bootp_packet_type __initdata = {
660         .type = cpu_to_be16(ETH_P_IP),
661         .func = ic_bootp_recv,
662 };
663
664 static __be32 ic_dev_xid;               /* Device under configuration */
665
666 /*
667  *  Initialize DHCP/BOOTP extension fields in the request.
668  */
669
670 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
671
672 #ifdef IPCONFIG_DHCP
673
674 static void __init
675 ic_dhcp_init_options(u8 *options)
676 {
677         u8 mt = ((ic_servaddr == NONE)
678                  ? DHCPDISCOVER : DHCPREQUEST);
679         u8 *e = options;
680         int len;
681
682 #ifdef IPCONFIG_DEBUG
683         printk("DHCP: Sending message type %d\n", mt);
684 #endif
685
686         memcpy(e, ic_bootp_cookie, 4);  /* RFC1048 Magic Cookie */
687         e += 4;
688
689         *e++ = 53;              /* DHCP message type */
690         *e++ = 1;
691         *e++ = mt;
692
693         if (mt == DHCPREQUEST) {
694                 *e++ = 54;      /* Server ID (IP address) */
695                 *e++ = 4;
696                 memcpy(e, &ic_servaddr, 4);
697                 e += 4;
698
699                 *e++ = 50;      /* Requested IP address */
700                 *e++ = 4;
701                 memcpy(e, &ic_myaddr, 4);
702                 e += 4;
703         }
704
705         /* always? */
706         {
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 */
716                 };
717
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);
722
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;
729                 }
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;
738                 }
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
742                  */
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;
748                 }
749         }
750
751         *e++ = 255;     /* End of the list */
752 }
753
754 #endif /* IPCONFIG_DHCP */
755
756 static void __init ic_bootp_init_ext(u8 *e)
757 {
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;
778
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;
783
784         *e++ = 255;             /* End of the list */
785 }
786
787
788 /*
789  *  Initialize the DHCP/BOOTP mechanism.
790  */
791 static inline void __init ic_bootp_init(void)
792 {
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
797          */
798         ic_nameservers_predef();
799
800         dev_add_pack(&bootp_packet_type);
801 }
802
803
804 /*
805  *  DHCP/BOOTP cleanup.
806  */
807 static inline void __init ic_bootp_cleanup(void)
808 {
809         dev_remove_pack(&bootp_packet_type);
810 }
811
812
813 /*
814  *  Send DHCP/BOOTP request to single interface.
815  */
816 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
817 {
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;
824
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));
833
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);
845
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 */
851
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 */
861         }
862
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;
868
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);
876
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;
885         }
886
887         if (dev_queue_xmit(skb) < 0)
888                 printk("E");
889 }
890
891
892 /*
893  *  Copy BOOTP-supplied string
894  */
895 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
896 {
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;
904 }
905
906
907 /*
908  *  Process BOOTP extensions.
909  */
910 static void __init ic_do_bootp_ext(u8 *ext)
911 {
912         u8 servers;
913         int i;
914         __be16 mtu;
915
916 #ifdef IPCONFIG_DEBUG
917         u8 *c;
918
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
924
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);
941                 }
942                 break;
943         case 12:        /* Host name */
944                 if (!ic_host_name_set) {
945                         ic_bootp_string(utsname()->nodename, ext+1, *ext,
946                                         __NEW_UTS_LEN);
947                         ic_host_name_set = 1;
948                 }
949                 break;
950         case 15:        /* Domain name (DNS) */
951                 if (!ic_domain[0])
952                         ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
953                 break;
954         case 17:        /* Root path */
955                 if (!root_server_path[0])
956                         ic_bootp_string(root_server_path, ext+1, *ext,
957                                         sizeof(root_server_path));
958                 break;
959         case 26:        /* Interface MTU */
960                 memcpy(&mtu, ext+1, sizeof(mtu));
961                 ic_dev_mtu = ntohs(mtu);
962                 break;
963         case 40:        /* NIS Domain name (_not_ DNS) */
964                 ic_bootp_string(utsname()->domainname, ext+1, *ext,
965                                 __NEW_UTS_LEN);
966                 break;
967         }
968 }
969
970
971 /*
972  *  Receive BOOTP reply.
973  */
974 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
975 {
976         struct bootp_pkt *b;
977         struct iphdr *h;
978         struct ic_device *d;
979         int len, ext_len;
980
981         if (!net_eq(dev_net(dev), &init_net))
982                 goto drop;
983
984         /* Perform verifications before taking the lock.  */
985         if (skb->pkt_type == PACKET_OTHERHOST)
986                 goto drop;
987
988         skb = skb_share_check(skb, GFP_ATOMIC);
989         if (!skb)
990                 return NET_RX_DROP;
991
992         if (!pskb_may_pull(skb,
993                            sizeof(struct iphdr) +
994                            sizeof(struct udphdr)))
995                 goto drop;
996
997         b = (struct bootp_pkt *)skb_network_header(skb);
998         h = &b->iph;
999
1000         if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
1001                 goto drop;
1002
1003         /* Fragments are not supported */
1004         if (ip_is_fragment(h)) {
1005                 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
1006                 goto drop;
1007         }
1008
1009         if (skb->len < ntohs(h->tot_len))
1010                 goto drop;
1011
1012         if (ip_fast_csum((char *) h, h->ihl))
1013                 goto drop;
1014
1015         if (b->udph.source != htons(67) || b->udph.dest != htons(68))
1016                 goto drop;
1017
1018         if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
1019                 goto drop;
1020
1021         len = ntohs(b->udph.len) - sizeof(struct udphdr);
1022         ext_len = len - (sizeof(*b) -
1023                          sizeof(struct iphdr) -
1024                          sizeof(struct udphdr) -
1025                          sizeof(b->exten));
1026         if (ext_len < 0)
1027                 goto drop;
1028
1029         /* Ok the front looks good, make sure we can get at the rest.  */
1030         if (!pskb_may_pull(skb, skb->len))
1031                 goto drop;
1032
1033         b = (struct bootp_pkt *)skb_network_header(skb);
1034         h = &b->iph;
1035
1036         /* One reply at a time, please. */
1037         spin_lock(&ic_recv_lock);
1038
1039         /* If we already have a reply, just drop the packet */
1040         if (ic_got_reply)
1041                 goto drop_unlock;
1042
1043         /* Find the ic_device that the packet arrived on */
1044         d = ic_first_dev;
1045         while (d && d->dev != dev)
1046                 d = d->next;
1047         if (!d)
1048                 goto drop_unlock;  /* should never happen */
1049
1050         /* Is it a reply to our BOOTP request? */
1051         if (b->op != BOOTP_REPLY ||
1052             b->xid != d->xid) {
1053                 net_err_ratelimited("DHCP/BOOTP: Reply not for us, op[%x] xid[%x]\n",
1054                                     b->op, b->xid);
1055                 goto drop_unlock;
1056         }
1057
1058         /* Is it a reply for the device we are configuring? */
1059         if (b->xid != ic_dev_xid) {
1060                 net_err_ratelimited("DHCP/BOOTP: Ignoring delayed packet\n");
1061                 goto drop_unlock;
1062         }
1063
1064         /* Parse extensions */
1065         if (ext_len >= 4 &&
1066             !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
1067                 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
1068                 u8 *ext;
1069
1070 #ifdef IPCONFIG_DHCP
1071                 if (ic_proto_enabled & IC_USE_DHCP) {
1072                         __be32 server_id = NONE;
1073                         int mt = 0;
1074
1075                         ext = &b->exten[4];
1076                         while (ext < end && *ext != 0xff) {
1077                                 u8 *opt = ext++;
1078                                 if (*opt == 0)  /* Padding */
1079                                         continue;
1080                                 ext += *ext + 1;
1081                                 if (ext >= end)
1082                                         break;
1083                                 switch (*opt) {
1084                                 case 53:        /* Message type */
1085                                         if (opt[1])
1086                                                 mt = opt[2];
1087                                         break;
1088                                 case 54:        /* Server ID (IP address) */
1089                                         if (opt[1] >= 4)
1090                                                 memcpy(&server_id, opt + 2, 4);
1091                                         break;
1092                                 }
1093                         }
1094
1095 #ifdef IPCONFIG_DEBUG
1096                         printk("DHCP: Got message type %d\n", mt);
1097 #endif
1098
1099                         switch (mt) {
1100                         case DHCPOFFER:
1101                                 /* While in the process of accepting one offer,
1102                                  * ignore all others.
1103                                  */
1104                                 if (ic_myaddr != NONE)
1105                                         goto drop_unlock;
1106
1107                                 /* Let's accept that offer. */
1108                                 ic_myaddr = b->your_ip;
1109                                 ic_servaddr = server_id;
1110 #ifdef IPCONFIG_DEBUG
1111                                 printk("DHCP: Offered address %pI4 by server %pI4\n",
1112                                        &ic_myaddr, &b->iph.saddr);
1113 #endif
1114                                 /* The DHCP indicated server address takes
1115                                  * precedence over the bootp header one if
1116                                  * they are different.
1117                                  */
1118                                 if ((server_id != NONE) &&
1119                                     (b->server_ip != server_id))
1120                                         b->server_ip = ic_servaddr;
1121                                 break;
1122
1123                         case DHCPACK:
1124                                 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1125                                         goto drop_unlock;
1126
1127                                 /* Yeah! */
1128                                 break;
1129
1130                         default:
1131                                 /* Urque.  Forget it*/
1132                                 ic_myaddr = NONE;
1133                                 ic_servaddr = NONE;
1134                                 goto drop_unlock;
1135                         }
1136
1137                         ic_dhcp_msgtype = mt;
1138
1139                 }
1140 #endif /* IPCONFIG_DHCP */
1141
1142                 ext = &b->exten[4];
1143                 while (ext < end && *ext != 0xff) {
1144                         u8 *opt = ext++;
1145                         if (*opt == 0)  /* Padding */
1146                                 continue;
1147                         ext += *ext + 1;
1148                         if (ext < end)
1149                                 ic_do_bootp_ext(opt);
1150                 }
1151         }
1152
1153         /* We have a winner! */
1154         ic_dev = dev;
1155         ic_myaddr = b->your_ip;
1156         ic_servaddr = b->server_ip;
1157         ic_addrservaddr = b->iph.saddr;
1158         if (ic_gateway == NONE && b->relay_ip)
1159                 ic_gateway = b->relay_ip;
1160         if (ic_nameservers[0] == NONE)
1161                 ic_nameservers[0] = ic_servaddr;
1162         ic_got_reply = IC_BOOTP;
1163
1164 drop_unlock:
1165         /* Show's over.  Nothing to see here.  */
1166         spin_unlock(&ic_recv_lock);
1167
1168 drop:
1169         /* Throw the packet out. */
1170         kfree_skb(skb);
1171
1172         return 0;
1173 }
1174
1175
1176 #endif
1177
1178
1179 /*
1180  *      Dynamic IP configuration -- DHCP, BOOTP, RARP.
1181  */
1182
1183 #ifdef IPCONFIG_DYNAMIC
1184
1185 static int __init ic_dynamic(void)
1186 {
1187         int retries;
1188         struct ic_device *d;
1189         unsigned long start_jiffies, timeout, jiff;
1190         int do_bootp = ic_proto_have_if & IC_BOOTP;
1191         int do_rarp = ic_proto_have_if & IC_RARP;
1192
1193         /*
1194          * If none of DHCP/BOOTP/RARP was selected, return with an error.
1195          * This routine gets only called when some pieces of information
1196          * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1197          */
1198         if (!ic_proto_enabled) {
1199                 pr_err("IP-Config: Incomplete network configuration information\n");
1200                 return -1;
1201         }
1202
1203 #ifdef IPCONFIG_BOOTP
1204         if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1205                 pr_err("DHCP/BOOTP: No suitable device found\n");
1206 #endif
1207 #ifdef IPCONFIG_RARP
1208         if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1209                 pr_err("RARP: No suitable device found\n");
1210 #endif
1211
1212         if (!ic_proto_have_if)
1213                 /* Error message already printed */
1214                 return -1;
1215
1216         /*
1217          * Setup protocols
1218          */
1219 #ifdef IPCONFIG_BOOTP
1220         if (do_bootp)
1221                 ic_bootp_init();
1222 #endif
1223 #ifdef IPCONFIG_RARP
1224         if (do_rarp)
1225                 ic_rarp_init();
1226 #endif
1227
1228         /*
1229          * Send requests and wait, until we get an answer. This loop
1230          * seems to be a terrible waste of CPU time, but actually there is
1231          * only one process running at all, so we don't need to use any
1232          * scheduler functions.
1233          * [Actually we could now, but the nothing else running note still
1234          *  applies.. - AC]
1235          */
1236         pr_notice("Sending %s%s%s requests .",
1237                   do_bootp
1238                   ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1239                   (do_bootp && do_rarp) ? " and " : "",
1240                   do_rarp ? "RARP" : "");
1241
1242         start_jiffies = jiffies;
1243         d = ic_first_dev;
1244         retries = CONF_SEND_RETRIES;
1245         get_random_bytes(&timeout, sizeof(timeout));
1246         timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
1247         for (;;) {
1248 #ifdef IPCONFIG_BOOTP
1249                 /* Track the device we are configuring */
1250                 ic_dev_xid = d->xid;
1251
1252                 if (do_bootp && (d->able & IC_BOOTP))
1253                         ic_bootp_send_if(d, jiffies - start_jiffies);
1254 #endif
1255 #ifdef IPCONFIG_RARP
1256                 if (do_rarp && (d->able & IC_RARP))
1257                         ic_rarp_send_if(d);
1258 #endif
1259
1260                 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1261                 while (time_before(jiffies, jiff) && !ic_got_reply)
1262                         schedule_timeout_uninterruptible(1);
1263 #ifdef IPCONFIG_DHCP
1264                 /* DHCP isn't done until we get a DHCPACK. */
1265                 if ((ic_got_reply & IC_BOOTP) &&
1266                     (ic_proto_enabled & IC_USE_DHCP) &&
1267                     ic_dhcp_msgtype != DHCPACK) {
1268                         ic_got_reply = 0;
1269                         pr_cont(",");
1270                         continue;
1271                 }
1272 #endif /* IPCONFIG_DHCP */
1273
1274                 if (ic_got_reply) {
1275                         pr_cont(" OK\n");
1276                         break;
1277                 }
1278
1279                 if ((d = d->next))
1280                         continue;
1281
1282                 if (! --retries) {
1283                         pr_cont(" timed out!\n");
1284                         break;
1285                 }
1286
1287                 d = ic_first_dev;
1288
1289                 timeout = timeout CONF_TIMEOUT_MULT;
1290                 if (timeout > CONF_TIMEOUT_MAX)
1291                         timeout = CONF_TIMEOUT_MAX;
1292
1293                 pr_cont(".");
1294         }
1295
1296 #ifdef IPCONFIG_BOOTP
1297         if (do_bootp)
1298                 ic_bootp_cleanup();
1299 #endif
1300 #ifdef IPCONFIG_RARP
1301         if (do_rarp)
1302                 ic_rarp_cleanup();
1303 #endif
1304
1305         if (!ic_got_reply) {
1306                 ic_myaddr = NONE;
1307                 return -1;
1308         }
1309
1310         printk("IP-Config: Got %s answer from %pI4, ",
1311                 ((ic_got_reply & IC_RARP) ? "RARP"
1312                  : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1313                &ic_addrservaddr);
1314         pr_cont("my address is %pI4\n", &ic_myaddr);
1315
1316         return 0;
1317 }
1318
1319 #endif /* IPCONFIG_DYNAMIC */
1320
1321 #ifdef CONFIG_PROC_FS
1322
1323 static int pnp_seq_show(struct seq_file *seq, void *v)
1324 {
1325         int i;
1326
1327         if (ic_proto_used & IC_PROTO)
1328                 seq_printf(seq, "#PROTO: %s\n",
1329                            (ic_proto_used & IC_RARP) ? "RARP"
1330                            : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1331         else
1332                 seq_puts(seq, "#MANUAL\n");
1333
1334         if (ic_domain[0])
1335                 seq_printf(seq,
1336                            "domain %s\n", ic_domain);
1337         for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1338                 if (ic_nameservers[i] != NONE)
1339                         seq_printf(seq, "nameserver %pI4\n",
1340                                    &ic_nameservers[i]);
1341         }
1342         if (ic_servaddr != NONE)
1343                 seq_printf(seq, "bootserver %pI4\n",
1344                            &ic_servaddr);
1345         return 0;
1346 }
1347
1348 static int pnp_seq_open(struct inode *indoe, struct file *file)
1349 {
1350         return single_open(file, pnp_seq_show, NULL);
1351 }
1352
1353 static const struct file_operations pnp_seq_fops = {
1354         .owner          = THIS_MODULE,
1355         .open           = pnp_seq_open,
1356         .read           = seq_read,
1357         .llseek         = seq_lseek,
1358         .release        = single_release,
1359 };
1360 #endif /* CONFIG_PROC_FS */
1361
1362 /*
1363  *  Extract IP address from the parameter string if needed. Note that we
1364  *  need to have root_server_addr set _before_ IPConfig gets called as it
1365  *  can override it.
1366  */
1367 __be32 __init root_nfs_parse_addr(char *name)
1368 {
1369         __be32 addr;
1370         int octets = 0;
1371         char *cp, *cq;
1372
1373         cp = cq = name;
1374         while (octets < 4) {
1375                 while (*cp >= '0' && *cp <= '9')
1376                         cp++;
1377                 if (cp == cq || cp - cq > 3)
1378                         break;
1379                 if (*cp == '.' || octets == 3)
1380                         octets++;
1381                 if (octets < 4)
1382                         cp++;
1383                 cq = cp;
1384         }
1385         if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1386                 if (*cp == ':')
1387                         *cp++ = '\0';
1388                 addr = in_aton(name);
1389                 memmove(name, cp, strlen(cp) + 1);
1390         } else
1391                 addr = NONE;
1392
1393         return addr;
1394 }
1395
1396 #define DEVICE_WAIT_MAX         12 /* 12 seconds */
1397
1398 static int __init wait_for_devices(void)
1399 {
1400         int i;
1401
1402         for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1403                 struct net_device *dev;
1404                 int found = 0;
1405
1406                 rtnl_lock();
1407                 for_each_netdev(&init_net, dev) {
1408                         if (ic_is_init_dev(dev)) {
1409                                 found = 1;
1410                                 break;
1411                         }
1412                 }
1413                 rtnl_unlock();
1414                 if (found)
1415                         return 0;
1416                 ssleep(1);
1417         }
1418         return -ENODEV;
1419 }
1420
1421 /*
1422  *      IP Autoconfig dispatcher.
1423  */
1424
1425 static int __init ip_auto_config(void)
1426 {
1427         __be32 addr;
1428 #ifdef IPCONFIG_DYNAMIC
1429         int retries = CONF_OPEN_RETRIES;
1430 #endif
1431         int err;
1432         unsigned int i;
1433
1434         /* Initialise all name servers to NONE (but only if the "ip=" or
1435          * "nfsaddrs=" kernel command line parameters weren't decoded, otherwise
1436          * we'll overwrite the IP addresses specified there)
1437          */
1438         if (ic_set_manually == 0)
1439                 ic_nameservers_predef();
1440
1441 #ifdef CONFIG_PROC_FS
1442         proc_create("pnp", S_IRUGO, init_net.proc_net, &pnp_seq_fops);
1443 #endif /* CONFIG_PROC_FS */
1444
1445         if (!ic_enable)
1446                 return 0;
1447
1448         DBG(("IP-Config: Entered.\n"));
1449 #ifdef IPCONFIG_DYNAMIC
1450  try_try_again:
1451 #endif
1452         /* Wait for devices to appear */
1453         err = wait_for_devices();
1454         if (err)
1455                 return err;
1456
1457         /* Setup all network devices */
1458         err = ic_open_devs();
1459         if (err)
1460                 return err;
1461
1462         /* Give drivers a chance to settle */
1463         msleep(CONF_POST_OPEN);
1464
1465         /*
1466          * If the config information is insufficient (e.g., our IP address or
1467          * IP address of the boot server is missing or we have multiple network
1468          * interfaces and no default was set), use BOOTP or RARP to get the
1469          * missing values.
1470          */
1471         if (ic_myaddr == NONE ||
1472 #ifdef CONFIG_ROOT_NFS
1473             (root_server_addr == NONE &&
1474              ic_servaddr == NONE &&
1475              ROOT_DEV == Root_NFS) ||
1476 #endif
1477             ic_first_dev->next) {
1478 #ifdef IPCONFIG_DYNAMIC
1479                 if (ic_dynamic() < 0) {
1480                         ic_close_devs();
1481
1482                         /*
1483                          * I don't know why, but sometimes the
1484                          * eepro100 driver (at least) gets upset and
1485                          * doesn't work the first time it's opened.
1486                          * But then if you close it and reopen it, it
1487                          * works just fine.  So we need to try that at
1488                          * least once before giving up.
1489                          *
1490                          * Also, if the root will be NFS-mounted, we
1491                          * have nowhere to go if DHCP fails.  So we
1492                          * just have to keep trying forever.
1493                          *
1494                          *                              -- Chip
1495                          */
1496 #ifdef CONFIG_ROOT_NFS
1497                         if (ROOT_DEV ==  Root_NFS) {
1498                                 pr_err("IP-Config: Retrying forever (NFS root)...\n");
1499                                 goto try_try_again;
1500                         }
1501 #endif
1502
1503                         if (--retries) {
1504                                 pr_err("IP-Config: Reopening network devices...\n");
1505                                 goto try_try_again;
1506                         }
1507
1508                         /* Oh, well.  At least we tried. */
1509                         pr_err("IP-Config: Auto-configuration of network failed\n");
1510                         return -1;
1511                 }
1512 #else /* !DYNAMIC */
1513                 pr_err("IP-Config: Incomplete network configuration information\n");
1514                 ic_close_devs();
1515                 return -1;
1516 #endif /* IPCONFIG_DYNAMIC */
1517         } else {
1518                 /* Device selected manually or only one device -> use it */
1519                 ic_dev = ic_first_dev->dev;
1520         }
1521
1522         addr = root_nfs_parse_addr(root_server_path);
1523         if (root_server_addr == NONE)
1524                 root_server_addr = addr;
1525
1526         /*
1527          * Use defaults wherever applicable.
1528          */
1529         if (ic_defaults() < 0)
1530                 return -1;
1531
1532         /*
1533          * Close all network devices except the device we've
1534          * autoconfigured and set up routes.
1535          */
1536         ic_close_devs();
1537         if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1538                 return -1;
1539
1540         /*
1541          * Record which protocol was actually used.
1542          */
1543 #ifdef IPCONFIG_DYNAMIC
1544         ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1545 #endif
1546
1547 #ifndef IPCONFIG_SILENT
1548         /*
1549          * Clue in the operator.
1550          */
1551         pr_info("IP-Config: Complete:\n");
1552
1553         pr_info("     device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
1554                 ic_dev->name, ic_dev->addr_len, ic_dev->dev_addr,
1555                 &ic_myaddr, &ic_netmask, &ic_gateway);
1556         pr_info("     host=%s, domain=%s, nis-domain=%s\n",
1557                 utsname()->nodename, ic_domain, utsname()->domainname);
1558         pr_info("     bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1559                 &ic_servaddr, &root_server_addr, root_server_path);
1560         if (ic_dev_mtu)
1561                 pr_cont(", mtu=%d", ic_dev_mtu);
1562         for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
1563                 if (ic_nameservers[i] != NONE) {
1564                         pr_info("     nameserver%u=%pI4",
1565                                 i, &ic_nameservers[i]);
1566                         break;
1567                 }
1568         for (i++; i < CONF_NAMESERVERS_MAX; i++)
1569                 if (ic_nameservers[i] != NONE)
1570                         pr_cont(", nameserver%u=%pI4", i, &ic_nameservers[i]);
1571         pr_cont("\n");
1572 #endif /* !SILENT */
1573
1574         return 0;
1575 }
1576
1577 late_initcall(ip_auto_config);
1578
1579
1580 /*
1581  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1582  *  command line parameter.  See Documentation/filesystems/nfs/nfsroot.txt.
1583  */
1584 static int __init ic_proto_name(char *name)
1585 {
1586         if (!strcmp(name, "on") || !strcmp(name, "any")) {
1587                 return 1;
1588         }
1589         if (!strcmp(name, "off") || !strcmp(name, "none")) {
1590                 return 0;
1591         }
1592 #ifdef CONFIG_IP_PNP_DHCP
1593         else if (!strncmp(name, "dhcp", 4)) {
1594                 char *client_id;
1595
1596                 ic_proto_enabled &= ~IC_RARP;
1597                 client_id = strstr(name, "dhcp,");
1598                 if (client_id) {
1599                         char *v;
1600
1601                         client_id = client_id + 5;
1602                         v = strchr(client_id, ',');
1603                         if (!v)
1604                                 return 1;
1605                         *v = 0;
1606                         if (kstrtou8(client_id, 0, dhcp_client_identifier))
1607                                 DBG("DHCP: Invalid client identifier type\n");
1608                         strncpy(dhcp_client_identifier + 1, v + 1, 251);
1609                         *v = ',';
1610                 }
1611                 return 1;
1612         }
1613 #endif
1614 #ifdef CONFIG_IP_PNP_BOOTP
1615         else if (!strcmp(name, "bootp")) {
1616                 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1617                 return 1;
1618         }
1619 #endif
1620 #ifdef CONFIG_IP_PNP_RARP
1621         else if (!strcmp(name, "rarp")) {
1622                 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1623                 return 1;
1624         }
1625 #endif
1626 #ifdef IPCONFIG_DYNAMIC
1627         else if (!strcmp(name, "both")) {
1628                 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1629                 return 1;
1630         }
1631 #endif
1632         return 0;
1633 }
1634
1635 static int __init ip_auto_config_setup(char *addrs)
1636 {
1637         char *cp, *ip, *dp;
1638         int num = 0;
1639
1640         ic_set_manually = 1;
1641         ic_enable = 1;
1642
1643         /*
1644          * If any dhcp, bootp etc options are set, leave autoconfig on
1645          * and skip the below static IP processing.
1646          */
1647         if (ic_proto_name(addrs))
1648                 return 1;
1649
1650         /* If no static IP is given, turn off autoconfig and bail.  */
1651         if (*addrs == 0 ||
1652             strcmp(addrs, "off") == 0 ||
1653             strcmp(addrs, "none") == 0) {
1654                 ic_enable = 0;
1655                 return 1;
1656         }
1657
1658         /* Initialise all name servers to NONE */
1659         ic_nameservers_predef();
1660
1661         /* Parse string for static IP assignment.  */
1662         ip = addrs;
1663         while (ip && *ip) {
1664                 if ((cp = strchr(ip, ':')))
1665                         *cp++ = '\0';
1666                 if (strlen(ip) > 0) {
1667                         DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1668                         switch (num) {
1669                         case 0:
1670                                 if ((ic_myaddr = in_aton(ip)) == ANY)
1671                                         ic_myaddr = NONE;
1672                                 break;
1673                         case 1:
1674                                 if ((ic_servaddr = in_aton(ip)) == ANY)
1675                                         ic_servaddr = NONE;
1676                                 break;
1677                         case 2:
1678                                 if ((ic_gateway = in_aton(ip)) == ANY)
1679                                         ic_gateway = NONE;
1680                                 break;
1681                         case 3:
1682                                 if ((ic_netmask = in_aton(ip)) == ANY)
1683                                         ic_netmask = NONE;
1684                                 break;
1685                         case 4:
1686                                 if ((dp = strchr(ip, '.'))) {
1687                                         *dp++ = '\0';
1688                                         strlcpy(utsname()->domainname, dp,
1689                                                 sizeof(utsname()->domainname));
1690                                 }
1691                                 strlcpy(utsname()->nodename, ip,
1692                                         sizeof(utsname()->nodename));
1693                                 ic_host_name_set = 1;
1694                                 break;
1695                         case 5:
1696                                 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1697                                 break;
1698                         case 6:
1699                                 if (ic_proto_name(ip) == 0 &&
1700                                     ic_myaddr == NONE) {
1701                                         ic_enable = 0;
1702                                 }
1703                                 break;
1704                         case 7:
1705                                 if (CONF_NAMESERVERS_MAX >= 1) {
1706                                         ic_nameservers[0] = in_aton(ip);
1707                                         if (ic_nameservers[0] == ANY)
1708                                                 ic_nameservers[0] = NONE;
1709                                 }
1710                                 break;
1711                         case 8:
1712                                 if (CONF_NAMESERVERS_MAX >= 2) {
1713                                         ic_nameservers[1] = in_aton(ip);
1714                                         if (ic_nameservers[1] == ANY)
1715                                                 ic_nameservers[1] = NONE;
1716                                 }
1717                                 break;
1718                         }
1719                 }
1720                 ip = cp;
1721                 num++;
1722         }
1723
1724         return 1;
1725 }
1726 __setup("ip=", ip_auto_config_setup);
1727
1728 static int __init nfsaddrs_config_setup(char *addrs)
1729 {
1730         return ip_auto_config_setup(addrs);
1731 }
1732 __setup("nfsaddrs=", nfsaddrs_config_setup);
1733
1734 static int __init vendor_class_identifier_setup(char *addrs)
1735 {
1736         if (strlcpy(vendor_class_identifier, addrs,
1737                     sizeof(vendor_class_identifier))
1738             >= sizeof(vendor_class_identifier))
1739                 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"",
1740                         vendor_class_identifier);
1741         return 1;
1742 }
1743 __setup("dhcpclass=", vendor_class_identifier_setup);