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