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