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