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