* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / ipv4 / ipconfig.c
blob37b41e93a65f827e9ce2f75d7e4bd0cd0e9876d4
1 /*
2 * $Id: ipconfig.c,v 1.24 1999/08/20 00:35:14 davem Exp $
4 * Automatic Configuration of IP -- use BOOTP or RARP or user-supplied
5 * information to configure own IP address and routes.
7 * Copyright (C) 1996--1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
9 * Derived from network configuration code in fs/nfs/nfsroot.c,
10 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
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
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
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/random.h>
27 #include <linux/init.h>
28 #include <linux/utsname.h>
29 #include <linux/in.h>
30 #include <linux/if.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/skbuff.h>
35 #include <linux/ip.h>
36 #include <linux/socket.h>
37 #include <linux/route.h>
38 #include <linux/udp.h>
39 #include <net/arp.h>
40 #include <net/ip.h>
41 #include <net/ipconfig.h>
43 #include <asm/segment.h>
44 #include <asm/uaccess.h>
45 #include <asm/checksum.h>
47 /* Define this to allow debugging output */
48 #undef IPCONFIG_DEBUG
50 #ifdef IPCONFIG_DEBUG
51 #define DBG(x) printk x
52 #else
53 #define DBG(x) do { } while(0)
54 #endif
56 /* Define the timeout for waiting for a RARP/BOOTP reply */
57 #define CONF_BASE_TIMEOUT (HZ*5) /* Initial timeout: 5 seconds */
58 #define CONF_RETRIES 10 /* 10 retries */
59 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
60 #define CONF_TIMEOUT_MULT *5/4 /* Rate of timeout growth */
61 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
63 /* IP configuration */
64 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */
65 u32 ic_myaddr __initdata = INADDR_NONE; /* My IP address */
66 u32 ic_servaddr __initdata = INADDR_NONE; /* Server IP address */
67 u32 ic_gateway __initdata = INADDR_NONE; /* Gateway IP address */
68 u32 ic_netmask __initdata = INADDR_NONE; /* Netmask for local subnet */
69 int ic_enable __initdata = 1; /* Automatic IP configuration enabled */
70 int ic_host_name_set __initdata = 0; /* Host name configured manually */
71 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
73 u32 root_server_addr __initdata = INADDR_NONE; /* Address of boot server */
74 u8 root_server_path[256] __initdata = { 0, }; /* Path to mount as root */
76 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
78 #define CONFIG_IP_PNP_DYNAMIC
80 static int ic_proto_enabled __initdata = 0 /* Protocols enabled */
81 #ifdef CONFIG_IP_PNP_BOOTP
82 | IC_BOOTP
83 #endif
84 #ifdef CONFIG_IP_PNP_RARP
85 | IC_RARP
86 #endif
88 static int ic_got_reply __initdata = 0; /* Protocol(s) we got reply from */
90 #else
92 static int ic_proto_enabled __initdata = 0;
94 #endif
96 static int ic_proto_have_if __initdata = 0;
99 * Network devices
102 struct ic_device {
103 struct ic_device *next;
104 struct net_device *dev;
105 unsigned short flags;
106 int able;
109 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
110 static struct net_device *ic_dev __initdata = NULL; /* Selected device */
112 static int __init ic_open_devs(void)
114 struct ic_device *d, **last;
115 struct net_device *dev;
116 unsigned short oflags;
118 last = &ic_first_dev;
119 read_lock(&dev_base_lock);
120 for (dev = dev_base; dev; dev = dev->next) {
121 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
122 (!(dev->flags & IFF_LOOPBACK) &&
123 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
124 strncmp(dev->name, "dummy", 5))) {
125 int able = 0;
126 if (dev->mtu >= 364)
127 able |= IC_BOOTP;
128 else
129 printk(KERN_WARNING "BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
130 if (!(dev->flags & IFF_NOARP))
131 able |= IC_RARP;
132 able &= ic_proto_enabled;
133 if (ic_proto_enabled && !able)
134 continue;
135 oflags = dev->flags;
136 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
137 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
138 continue;
140 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL)))
141 return -1;
142 d->dev = dev;
143 *last = d;
144 last = &d->next;
145 d->flags = oflags;
146 d->able = able;
147 ic_proto_have_if |= able;
148 DBG(("IP-Config: Opened %s (able=%d)\n", dev->name, able));
151 read_unlock(&dev_base_lock);
153 *last = NULL;
155 if (!ic_first_dev) {
156 if (user_dev_name[0])
157 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
158 else
159 printk(KERN_ERR "IP-Config: No network devices available.\n");
160 return -1;
162 return 0;
165 static void __init ic_close_devs(void)
167 struct ic_device *d, *next;
168 struct net_device *dev;
170 next = ic_first_dev;
171 while ((d = next)) {
172 next = d->next;
173 dev = d->dev;
174 if (dev != ic_dev) {
175 DBG(("IP-Config: Downing %s\n", dev->name));
176 dev_change_flags(dev, d->flags);
178 kfree_s(d, sizeof(struct ic_device));
183 * Interface to various network functions.
186 static inline void
187 set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
189 sin->sin_family = AF_INET;
190 sin->sin_addr.s_addr = addr;
191 sin->sin_port = port;
194 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
196 int res;
198 mm_segment_t oldfs = get_fs();
199 set_fs(get_ds());
200 res = devinet_ioctl(cmd, arg);
201 set_fs(oldfs);
202 return res;
205 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
207 int res;
209 mm_segment_t oldfs = get_fs();
210 set_fs(get_ds());
211 res = ip_rt_ioctl(cmd, arg);
212 set_fs(oldfs);
213 return res;
217 * Set up interface addresses and routes.
220 static int __init ic_setup_if(void)
222 struct ifreq ir;
223 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
224 int err;
226 memset(&ir, 0, sizeof(ir));
227 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
228 set_sockaddr(sin, ic_myaddr, 0);
229 if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
230 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
231 return -1;
233 set_sockaddr(sin, ic_netmask, 0);
234 if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
235 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
236 return -1;
238 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
239 if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
240 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
241 return -1;
243 return 0;
246 static int __init ic_setup_routes(void)
248 /* No need to setup device routes, only the default route... */
250 if (ic_gateway != INADDR_NONE) {
251 struct rtentry rm;
252 int err;
254 memset(&rm, 0, sizeof(rm));
255 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
256 printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
257 return -1;
259 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
260 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
261 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
262 rm.rt_flags = RTF_UP | RTF_GATEWAY;
263 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
264 printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
265 return -1;
269 return 0;
273 * Fill in default values for all missing parameters.
276 static int __init ic_defaults(void)
279 * At this point we have no userspace running so need not
280 * claim locks on system_utsname
283 if (!ic_host_name_set)
284 strcpy(system_utsname.nodename, in_ntoa(ic_myaddr));
286 if (root_server_addr == INADDR_NONE)
287 root_server_addr = ic_servaddr;
289 if (ic_netmask == INADDR_NONE) {
290 if (IN_CLASSA(ntohl(ic_myaddr)))
291 ic_netmask = htonl(IN_CLASSA_NET);
292 else if (IN_CLASSB(ntohl(ic_myaddr)))
293 ic_netmask = htonl(IN_CLASSB_NET);
294 else if (IN_CLASSC(ntohl(ic_myaddr)))
295 ic_netmask = htonl(IN_CLASSC_NET);
296 else {
297 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %08x\n", ic_myaddr);
298 return -1;
300 printk("IP-Config: Guessing netmask %s\n", in_ntoa(ic_netmask));
303 return 0;
307 * RARP support.
310 #ifdef CONFIG_IP_PNP_RARP
312 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
314 static struct packet_type rarp_packet_type __initdata = {
315 __constant_htons(ETH_P_RARP),
316 NULL, /* Listen to all devices */
317 ic_rarp_recv,
318 NULL,
319 NULL
322 static inline void ic_rarp_init(void)
324 dev_add_pack(&rarp_packet_type);
327 static inline void ic_rarp_cleanup(void)
329 dev_remove_pack(&rarp_packet_type);
333 * Process received RARP packet.
335 static int __init
336 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
338 struct arphdr *rarp = (struct arphdr *)skb->h.raw;
339 unsigned char *rarp_ptr = (unsigned char *) (rarp + 1);
340 unsigned long sip, tip;
341 unsigned char *sha, *tha; /* s for "source", t for "target" */
343 /* If we already have a reply, just drop the packet */
344 if (ic_got_reply)
345 goto drop;
347 /* If this test doesn't pass, it's not IP, or we should ignore it anyway */
348 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
349 goto drop;
351 /* If it's not a RARP reply, delete it. */
352 if (rarp->ar_op != htons(ARPOP_RREPLY))
353 goto drop;
355 /* If it's not Ethernet, delete it. */
356 if (rarp->ar_pro != htons(ETH_P_IP))
357 goto drop;
359 /* Extract variable-width fields */
360 sha = rarp_ptr;
361 rarp_ptr += dev->addr_len;
362 memcpy(&sip, rarp_ptr, 4);
363 rarp_ptr += 4;
364 tha = rarp_ptr;
365 rarp_ptr += dev->addr_len;
366 memcpy(&tip, rarp_ptr, 4);
368 /* Discard packets which are not meant for us. */
369 if (memcmp(tha, dev->dev_addr, dev->addr_len))
370 goto drop;
372 /* Discard packets which are not from specified server. */
373 if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
374 goto drop;
376 /* Victory! The packet is what we were looking for! */
377 if (!ic_got_reply) {
378 ic_got_reply = IC_RARP;
379 ic_dev = dev;
380 if (ic_myaddr == INADDR_NONE)
381 ic_myaddr = tip;
382 ic_servaddr = sip;
385 /* And throw the packet out... */
386 drop:
387 kfree_skb(skb);
388 return 0;
393 * Send RARP request packet over all devices which allow RARP.
395 static void __init ic_rarp_send(void)
397 struct ic_device *d;
399 for (d=ic_first_dev; d; d=d->next)
400 if (d->able & IC_RARP) {
401 struct net_device *dev = d->dev;
402 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
403 dev->dev_addr, dev->dev_addr);
407 #endif
410 * BOOTP support.
413 #ifdef CONFIG_IP_PNP_BOOTP
415 struct bootp_pkt { /* BOOTP packet format */
416 struct iphdr iph; /* IP header */
417 struct udphdr udph; /* UDP header */
418 u8 op; /* 1=request, 2=reply */
419 u8 htype; /* HW address type */
420 u8 hlen; /* HW address length */
421 u8 hops; /* Used only by gateways */
422 u32 xid; /* Transaction ID */
423 u16 secs; /* Seconds since we started */
424 u16 flags; /* Just what it says */
425 u32 client_ip; /* Client's IP address if known */
426 u32 your_ip; /* Assigned IP address */
427 u32 server_ip; /* Server's IP address */
428 u32 relay_ip; /* IP address of BOOTP relay */
429 u8 hw_addr[16]; /* Client's HW address */
430 u8 serv_name[64]; /* Server host name */
431 u8 boot_file[128]; /* Name of boot file */
432 u8 vendor_area[128]; /* Area for extensions */
435 #define BOOTP_REQUEST 1
436 #define BOOTP_REPLY 2
438 static u32 ic_bootp_xid;
440 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
442 static struct packet_type bootp_packet_type __initdata = {
443 __constant_htons(ETH_P_IP),
444 NULL, /* Listen to all devices */
445 ic_bootp_recv,
446 NULL,
447 NULL
452 * Initialize BOOTP extension fields in the request.
454 static void __init ic_bootp_init_ext(u8 *e)
456 *e++ = 99; /* RFC1048 Magic Cookie */
457 *e++ = 130;
458 *e++ = 83;
459 *e++ = 99;
460 *e++ = 1; /* Subnet mask request */
461 *e++ = 4;
462 e += 4;
463 *e++ = 3; /* Default gateway request */
464 *e++ = 4;
465 e += 4;
466 *e++ = 12; /* Host name request */
467 *e++ = 32;
468 e += 32;
469 *e++ = 40; /* NIS Domain name request */
470 *e++ = 32;
471 e += 32;
472 *e++ = 17; /* Boot path */
473 *e++ = 32;
474 e += 32;
475 *e = 255; /* End of the list */
480 * Initialize the BOOTP mechanism.
482 static inline void ic_bootp_init(void)
484 get_random_bytes(&ic_bootp_xid, sizeof(u32));
485 DBG(("BOOTP: XID=%08x\n", ic_bootp_xid));
486 dev_add_pack(&bootp_packet_type);
491 * BOOTP cleanup.
493 static inline void ic_bootp_cleanup(void)
495 dev_remove_pack(&bootp_packet_type);
500 * Send BOOTP request to single interface.
502 static void __init ic_bootp_send_if(struct ic_device *d, u32 jiffies)
504 struct net_device *dev = d->dev;
505 struct sk_buff *skb;
506 struct bootp_pkt *b;
507 int hh_len = (dev->hard_header_len + 15) & ~15;
508 struct iphdr *h;
510 /* Allocate packet */
511 skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
512 if (!skb)
513 return;
514 skb_reserve(skb, hh_len);
515 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
516 memset(b, 0, sizeof(struct bootp_pkt));
518 /* Construct IP header */
519 skb->nh.iph = h = &b->iph;
520 h->version = 4;
521 h->ihl = 5;
522 h->tot_len = htons(sizeof(struct bootp_pkt));
523 h->frag_off = htons(IP_DF);
524 h->ttl = 64;
525 h->protocol = IPPROTO_UDP;
526 h->daddr = INADDR_BROADCAST;
527 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
529 /* Construct UDP header */
530 b->udph.source = htons(68);
531 b->udph.dest = htons(67);
532 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
533 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
535 /* Construct BOOTP header */
536 b->op = BOOTP_REQUEST;
537 b->htype = dev->type;
538 b->hlen = dev->addr_len;
539 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
540 b->secs = htons(jiffies / HZ);
541 b->xid = ic_bootp_xid;
542 ic_bootp_init_ext(b->vendor_area);
544 /* Chain packet down the line... */
545 skb->dev = dev;
546 skb->protocol = __constant_htons(ETH_P_IP);
547 if ((dev->hard_header &&
548 dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
549 dev_queue_xmit(skb) < 0)
550 printk("E");
555 * Send BOOTP requests to all interfaces.
557 static void __init ic_bootp_send(u32 jiffies)
559 struct ic_device *d;
561 for(d=ic_first_dev; d; d=d->next)
562 if (d->able & IC_BOOTP)
563 ic_bootp_send_if(d, jiffies);
568 * Copy BOOTP-supplied string if not already set.
570 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
572 if (!len)
573 return 0;
574 if (len > max-1)
575 len = max-1;
576 strncpy(dest, src, len);
577 dest[len] = '\0';
578 return 1;
583 * Process BOOTP extension.
585 static void __init ic_do_bootp_ext(u8 *ext)
587 #ifdef IPCONFIG_DEBUG
588 u8 *c;
590 printk("BOOTP: Got extension %02x",*ext);
591 for(c=ext+2; c<ext+2+ext[1]; c++)
592 printk(" %02x", *c);
593 printk("\n");
594 #endif
596 switch (*ext++) {
597 case 1: /* Subnet mask */
598 if (ic_netmask == INADDR_NONE)
599 memcpy(&ic_netmask, ext+1, 4);
600 break;
601 case 3: /* Default gateway */
602 if (ic_gateway == INADDR_NONE)
603 memcpy(&ic_gateway, ext+1, 4);
604 break;
605 case 12: /* Host name */
606 ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
607 ic_host_name_set = 1;
608 break;
609 case 40: /* NIS Domain name */
610 ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
611 break;
612 case 17: /* Root path */
613 if (!root_server_path[0])
614 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
615 break;
621 * Receive BOOTP reply.
623 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
625 struct bootp_pkt *b = (struct bootp_pkt *) skb->nh.iph;
626 struct iphdr *h = &b->iph;
627 int len;
629 /* If we already have a reply, just drop the packet */
630 if (ic_got_reply)
631 goto drop;
633 /* Check whether it's a BOOTP packet */
634 if (skb->pkt_type == PACKET_OTHERHOST ||
635 skb->len < sizeof(struct udphdr) + sizeof(struct iphdr) ||
636 h->ihl != 5 ||
637 h->version != 4 ||
638 ip_fast_csum((char *) h, h->ihl) != 0 ||
639 skb->len < ntohs(h->tot_len) ||
640 h->protocol != IPPROTO_UDP ||
641 b->udph.source != htons(67) ||
642 b->udph.dest != htons(68) ||
643 ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
644 goto drop;
646 /* Fragments are not supported */
647 if (h->frag_off & htons(IP_OFFSET|IP_MF)) {
648 printk(KERN_ERR "BOOTP: Ignoring fragmented reply.\n");
649 goto drop;
652 /* Is it a reply to our BOOTP request? */
653 len = ntohs(b->udph.len) - sizeof(struct udphdr);
654 if (len < 300 || /* See RFC 951:2.1 */
655 b->op != BOOTP_REPLY ||
656 b->xid != ic_bootp_xid) {
657 printk("?");
658 goto drop;
661 /* Extract basic fields */
662 ic_myaddr = b->your_ip;
663 ic_servaddr = b->server_ip;
664 ic_got_reply = IC_BOOTP;
665 ic_dev = dev;
667 /* Parse extensions */
668 if (b->vendor_area[0] == 99 && /* Check magic cookie */
669 b->vendor_area[1] == 130 &&
670 b->vendor_area[2] == 83 &&
671 b->vendor_area[3] == 99) {
672 u8 *ext = &b->vendor_area[4];
673 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
674 while (ext < end && *ext != 0xff) {
675 if (*ext == 0) /* Padding */
676 ext++;
677 else {
678 u8 *opt = ext;
679 ext += ext[1] + 2;
680 if (ext <= end)
681 ic_do_bootp_ext(opt);
686 if (ic_gateway == INADDR_NONE && b->relay_ip)
687 ic_gateway = b->relay_ip;
689 drop:
690 kfree_skb(skb);
691 return 0;
695 #endif
699 * Dynamic IP configuration -- BOOTP and RARP.
702 #ifdef CONFIG_IP_PNP_DYNAMIC
704 static int __init ic_dynamic(void)
706 int retries;
707 unsigned long timeout, jiff;
708 unsigned long start_jiffies;
709 int do_rarp = ic_proto_have_if & IC_RARP;
710 int do_bootp = ic_proto_have_if & IC_BOOTP;
713 * If neither BOOTP nor RARP was selected, return with an error. This
714 * routine gets only called when some pieces of information are mis-
715 * sing, and without BOOTP and RARP we are not able to get that in-
716 * formation.
718 if (!ic_proto_enabled) {
719 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
720 return -1;
723 #ifdef CONFIG_IP_PNP_BOOTP
724 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
725 printk(KERN_ERR "BOOTP: No suitable device found.\n");
726 #endif
728 #ifdef CONFIG_IP_PNP_RARP
729 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
730 printk(KERN_ERR "RARP: No suitable device found.\n");
731 #endif
733 if (!ic_proto_have_if)
734 /* Error message already printed */
735 return -1;
738 * Setup RARP and BOOTP protocols
740 #ifdef CONFIG_IP_PNP_RARP
741 if (do_rarp)
742 ic_rarp_init();
743 #endif
744 #ifdef CONFIG_IP_PNP_BOOTP
745 if (do_bootp)
746 ic_bootp_init();
747 #endif
750 * Send requests and wait, until we get an answer. This loop
751 * seems to be a terrible waste of CPU time, but actually there is
752 * only one process running at all, so we don't need to use any
753 * scheduler functions.
754 * [Actually we could now, but the nothing else running note still
755 * applies.. - AC]
757 printk(KERN_NOTICE "Sending %s%s%s requests...",
758 do_bootp ? "BOOTP" : "",
759 do_bootp && do_rarp ? " and " : "",
760 do_rarp ? "RARP" : "");
761 start_jiffies = jiffies;
762 retries = CONF_RETRIES;
763 get_random_bytes(&timeout, sizeof(timeout));
764 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
765 for(;;) {
766 #ifdef CONFIG_IP_PNP_BOOTP
767 if (do_bootp)
768 ic_bootp_send(jiffies - start_jiffies);
769 #endif
770 #ifdef CONFIG_IP_PNP_RARP
771 if (do_rarp)
772 ic_rarp_send();
773 #endif
774 printk(".");
775 jiff = jiffies + timeout;
776 while (jiffies < jiff && !ic_got_reply)
778 if (ic_got_reply) {
779 printk(" OK\n");
780 break;
782 if (! --retries) {
783 printk(" timed out!\n");
784 break;
786 timeout = timeout CONF_TIMEOUT_MULT;
787 if (timeout > CONF_TIMEOUT_MAX)
788 timeout = CONF_TIMEOUT_MAX;
791 #ifdef CONFIG_IP_PNP_RARP
792 if (do_rarp)
793 ic_rarp_cleanup();
794 #endif
795 #ifdef CONFIG_IP_PNP_BOOTP
796 if (do_bootp)
797 ic_bootp_cleanup();
798 #endif
800 if (!ic_got_reply)
801 return -1;
803 printk("IP-Config: Got %s answer from %s, ",
804 (ic_got_reply & IC_BOOTP) ? "BOOTP" : "RARP",
805 in_ntoa(ic_servaddr));
806 printk("my address is %s\n", in_ntoa(ic_myaddr));
808 return 0;
811 #endif
814 * IP Autoconfig dispatcher.
817 int __init ip_auto_config(void)
819 if (!ic_enable)
820 return 0;
822 DBG(("IP-Config: Entered.\n"));
824 /* Setup all network devices */
825 if (ic_open_devs() < 0)
826 return -1;
829 * If the config information is insufficient (e.g., our IP address or
830 * IP address of the boot server is missing or we have multiple network
831 * interfaces and no default was set), use BOOTP or RARP to get the
832 * missing values.
834 if (ic_myaddr == INADDR_NONE ||
835 #ifdef CONFIG_ROOT_NFS
836 (root_server_addr == INADDR_NONE && ic_servaddr == INADDR_NONE) ||
837 #endif
838 ic_first_dev->next) {
839 #ifdef CONFIG_IP_PNP_DYNAMIC
840 if (ic_dynamic() < 0) {
841 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
842 ic_close_devs();
843 return -1;
845 #else
846 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
847 ic_close_devs();
848 return -1;
849 #endif
850 } else {
851 ic_dev = ic_first_dev->dev; /* Device selected manually or only one device -> use it */
855 * Use defaults whereever applicable.
857 if (ic_defaults() < 0)
858 return -1;
861 * Close all network devices except the device we've
862 * autoconfigured and set up routes.
864 ic_close_devs();
865 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
866 return -1;
868 DBG(("IP-Config: device=%s, local=%08x, server=%08x, boot=%08x, gw=%08x, mask=%08x\n",
869 ic_dev->name, ic_myaddr, ic_servaddr, root_server_addr, ic_gateway, ic_netmask));
870 DBG(("IP-Config: host=%s, domain=%s, path=`%s'\n", system_utsname.nodename,
871 system_utsname.domainname, root_server_path));
872 return 0;
876 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
877 * command line parameter. It consists of option fields separated by colons in
878 * the following order:
880 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<bootp|rarp>
882 * Any of the fields can be empty which means to use a default value:
883 * <client-ip> - address given by BOOTP or RARP
884 * <server-ip> - address of host returning BOOTP or RARP packet
885 * <gw-ip> - none, or the address returned by BOOTP
886 * <netmask> - automatically determined from <client-ip>, or the
887 * one returned by BOOTP
888 * <host name> - <client-ip> in ASCII notation, or the name returned
889 * by BOOTP
890 * <device> - use all available devices
891 * <bootp|rarp|both|off> - use both protocols to determine my own address
893 static int __init ic_proto_name(char *name)
895 if (!strcmp(name, "off")) {
896 ic_proto_enabled = 0;
897 return 1;
899 #ifdef CONFIG_IP_PNP_BOOTP
900 else if (!strcmp(name, "bootp")) {
901 ic_proto_enabled &= ~IC_RARP;
902 return 1;
904 #endif
905 #ifdef CONFIG_IP_PNP_RARP
906 else if (!strcmp(name, "rarp")) {
907 ic_proto_enabled &= ~IC_BOOTP;
908 return 1;
910 #endif
911 #ifdef CONFIG_IP_PNP_DYNAMIC
912 else if (!strcmp(name, "both")) {
913 return 1;
915 #endif
916 return 0;
919 static int __init ip_auto_config_setup(char *addrs)
921 char *cp, *ip, *dp;
922 int num = 0;
924 ic_set_manually = 1;
925 if (!strcmp(addrs, "off")) {
926 ic_enable = 0;
927 return 1;
929 if (ic_proto_name(addrs))
930 return 1;
932 /* Parse the whole string */
933 ip = addrs;
934 while (ip && *ip) {
935 if ((cp = strchr(ip, ':')))
936 *cp++ = '\0';
937 if (strlen(ip) > 0) {
938 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
939 switch (num) {
940 case 0:
941 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
942 ic_myaddr = INADDR_NONE;
943 break;
944 case 1:
945 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
946 ic_servaddr = INADDR_NONE;
947 break;
948 case 2:
949 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
950 ic_gateway = INADDR_NONE;
951 break;
952 case 3:
953 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
954 ic_netmask = INADDR_NONE;
955 break;
956 case 4:
957 if ((dp = strchr(ip, '.'))) {
958 *dp++ = '\0';
959 strncpy(system_utsname.domainname, dp, __NEW_UTS_LEN);
960 system_utsname.domainname[__NEW_UTS_LEN] = '\0';
962 strncpy(system_utsname.nodename, ip, __NEW_UTS_LEN);
963 system_utsname.nodename[__NEW_UTS_LEN] = '\0';
964 ic_host_name_set = 1;
965 break;
966 case 5:
967 strncpy(user_dev_name, ip, IFNAMSIZ);
968 user_dev_name[IFNAMSIZ-1] = '\0';
969 break;
970 case 6:
971 ic_proto_name(ip);
972 break;
975 ip = cp;
976 num++;
979 return 0;
982 static int __init nfsaddrs_config_setup(char *addrs)
984 return ip_auto_config_setup(addrs);
987 __setup("ip=", ip_auto_config_setup);
988 __setup("nfsaddrs=", nfsaddrs_config_setup);