Import 2.3.13
[davej-history.git] / net / ipv4 / ipconfig.c
blob52dde7a7c82f4d0dab8ff04a143ec9d740d4804d
1 /*
2 * $Id: ipconfig.c,v 1.23 1999/06/28 11:35:07 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
17 #include <linux/config.h>
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/random.h>
23 #include <linux/init.h>
24 #include <linux/utsname.h>
25 #include <linux/in.h>
26 #include <linux/if.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/skbuff.h>
31 #include <linux/ip.h>
32 #include <linux/socket.h>
33 #include <linux/route.h>
34 #include <linux/udp.h>
35 #include <net/arp.h>
36 #include <net/ip.h>
37 #include <net/ipconfig.h>
39 #include <asm/segment.h>
40 #include <asm/uaccess.h>
41 #include <asm/checksum.h>
43 /* Define this to allow debugging output */
44 #undef IPCONFIG_DEBUG
46 #ifdef IPCONFIG_DEBUG
47 #define DBG(x) printk x
48 #else
49 #define DBG(x) do { } while(0)
50 #endif
52 /* Define the timeout for waiting for a RARP/BOOTP reply */
53 #define CONF_BASE_TIMEOUT (HZ*5) /* Initial timeout: 5 seconds */
54 #define CONF_RETRIES 10 /* 10 retries */
55 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
56 #define CONF_TIMEOUT_MULT *5/4 /* Rate of timeout growth */
57 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
59 /* IP configuration */
60 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */
61 u32 ic_myaddr __initdata = INADDR_NONE; /* My IP address */
62 u32 ic_servaddr __initdata = INADDR_NONE; /* Server IP address */
63 u32 ic_gateway __initdata = INADDR_NONE; /* Gateway IP address */
64 u32 ic_netmask __initdata = INADDR_NONE; /* Netmask for local subnet */
65 int ic_enable __initdata = 1; /* Automatic IP configuration enabled */
66 int ic_host_name_set __initdata = 0; /* Host name configured manually */
67 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
69 u32 root_server_addr __initdata = INADDR_NONE; /* Address of boot server */
70 u8 root_server_path[256] __initdata = { 0, }; /* Path to mount as root */
72 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
74 #define CONFIG_IP_PNP_DYNAMIC
76 static int ic_proto_enabled __initdata = 0 /* Protocols enabled */
77 #ifdef CONFIG_IP_PNP_BOOTP
78 | IC_BOOTP
79 #endif
80 #ifdef CONFIG_IP_PNP_RARP
81 | IC_RARP
82 #endif
84 static int ic_got_reply __initdata = 0; /* Protocol(s) we got reply from */
86 #else
88 static int ic_proto_enabled __initdata = 0;
90 #endif
92 static int ic_proto_have_if __initdata = 0;
95 * Network devices
98 struct ic_device {
99 struct ic_device *next;
100 struct device *dev;
101 unsigned short flags;
102 int able;
105 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
106 static struct device *ic_dev __initdata = NULL; /* Selected device */
108 static int __init ic_open_devs(void)
110 struct ic_device *d, **last;
111 struct device *dev;
112 unsigned short oflags;
114 last = &ic_first_dev;
115 read_lock(&dev_base_lock);
116 for (dev = dev_base; dev; dev = dev->next) {
117 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
118 (!(dev->flags & IFF_LOOPBACK) &&
119 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
120 strncmp(dev->name, "dummy", 5))) {
121 int able = 0;
122 if (dev->mtu >= 364)
123 able |= IC_BOOTP;
124 else
125 printk(KERN_WARNING "BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
126 if (!(dev->flags & IFF_NOARP))
127 able |= IC_RARP;
128 able &= ic_proto_enabled;
129 if (ic_proto_enabled && !able)
130 continue;
131 oflags = dev->flags;
132 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
133 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
134 continue;
136 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL)))
137 return -1;
138 d->dev = dev;
139 *last = d;
140 last = &d->next;
141 d->flags = oflags;
142 d->able = able;
143 ic_proto_have_if |= able;
144 DBG(("IP-Config: Opened %s (able=%d)\n", dev->name, able));
147 read_unlock(&dev_base_lock);
149 *last = NULL;
151 if (!ic_first_dev) {
152 if (user_dev_name[0])
153 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
154 else
155 printk(KERN_ERR "IP-Config: No network devices available.\n");
156 return -1;
158 return 0;
161 static void __init ic_close_devs(void)
163 struct ic_device *d, *next;
164 struct device *dev;
166 next = ic_first_dev;
167 while ((d = next)) {
168 next = d->next;
169 dev = d->dev;
170 if (dev != ic_dev) {
171 DBG(("IP-Config: Downing %s\n", dev->name));
172 dev_change_flags(dev, d->flags);
174 kfree_s(d, sizeof(struct ic_device));
179 * Interface to various network functions.
182 static inline void
183 set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
185 sin->sin_family = AF_INET;
186 sin->sin_addr.s_addr = addr;
187 sin->sin_port = port;
190 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
192 int res;
194 mm_segment_t oldfs = get_fs();
195 set_fs(get_ds());
196 res = devinet_ioctl(cmd, arg);
197 set_fs(oldfs);
198 return res;
201 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
203 int res;
205 mm_segment_t oldfs = get_fs();
206 set_fs(get_ds());
207 res = ip_rt_ioctl(cmd, arg);
208 set_fs(oldfs);
209 return res;
213 * Set up interface addresses and routes.
216 static int __init ic_setup_if(void)
218 struct ifreq ir;
219 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
220 int err;
222 memset(&ir, 0, sizeof(ir));
223 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
224 set_sockaddr(sin, ic_myaddr, 0);
225 if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
226 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
227 return -1;
229 set_sockaddr(sin, ic_netmask, 0);
230 if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
231 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
232 return -1;
234 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
235 if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
236 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
237 return -1;
239 return 0;
242 static int __init ic_setup_routes(void)
244 /* No need to setup device routes, only the default route... */
246 if (ic_gateway != INADDR_NONE) {
247 struct rtentry rm;
248 int err;
250 memset(&rm, 0, sizeof(rm));
251 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
252 printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
253 return -1;
255 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
256 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
257 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
258 rm.rt_flags = RTF_UP | RTF_GATEWAY;
259 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
260 printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
261 return -1;
265 return 0;
269 * Fill in default values for all missing parameters.
272 static int __init ic_defaults(void)
275 * At this point we have no userspace running so need not
276 * claim locks on system_utsname
279 if (!ic_host_name_set)
280 strcpy(system_utsname.nodename, in_ntoa(ic_myaddr));
282 if (root_server_addr == INADDR_NONE)
283 root_server_addr = ic_servaddr;
285 if (ic_netmask == INADDR_NONE) {
286 if (IN_CLASSA(ntohl(ic_myaddr)))
287 ic_netmask = htonl(IN_CLASSA_NET);
288 else if (IN_CLASSB(ntohl(ic_myaddr)))
289 ic_netmask = htonl(IN_CLASSB_NET);
290 else if (IN_CLASSC(ntohl(ic_myaddr)))
291 ic_netmask = htonl(IN_CLASSC_NET);
292 else {
293 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %08x\n", ic_myaddr);
294 return -1;
296 printk("IP-Config: Guessing netmask %s\n", in_ntoa(ic_netmask));
299 return 0;
303 * RARP support.
306 #ifdef CONFIG_IP_PNP_RARP
308 static int ic_rarp_recv(struct sk_buff *skb, struct device *dev, struct packet_type *pt);
310 static struct packet_type rarp_packet_type __initdata = {
311 __constant_htons(ETH_P_RARP),
312 NULL, /* Listen to all devices */
313 ic_rarp_recv,
314 NULL,
315 NULL
318 static inline void ic_rarp_init(void)
320 dev_add_pack(&rarp_packet_type);
323 static inline void ic_rarp_cleanup(void)
325 dev_remove_pack(&rarp_packet_type);
329 * Process received RARP packet.
331 static int __init
332 ic_rarp_recv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
334 struct arphdr *rarp = (struct arphdr *)skb->h.raw;
335 unsigned char *rarp_ptr = (unsigned char *) (rarp + 1);
336 unsigned long sip, tip;
337 unsigned char *sha, *tha; /* s for "source", t for "target" */
339 /* If we already have a reply, just drop the packet */
340 if (ic_got_reply)
341 goto drop;
343 /* If this test doesn't pass, it's not IP, or we should ignore it anyway */
344 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
345 goto drop;
347 /* If it's not a RARP reply, delete it. */
348 if (rarp->ar_op != htons(ARPOP_RREPLY))
349 goto drop;
351 /* If it's not Ethernet, delete it. */
352 if (rarp->ar_pro != htons(ETH_P_IP))
353 goto drop;
355 /* Extract variable-width fields */
356 sha = rarp_ptr;
357 rarp_ptr += dev->addr_len;
358 memcpy(&sip, rarp_ptr, 4);
359 rarp_ptr += 4;
360 tha = rarp_ptr;
361 rarp_ptr += dev->addr_len;
362 memcpy(&tip, rarp_ptr, 4);
364 /* Discard packets which are not meant for us. */
365 if (memcmp(tha, dev->dev_addr, dev->addr_len))
366 goto drop;
368 /* Discard packets which are not from specified server. */
369 if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
370 goto drop;
372 /* Victory! The packet is what we were looking for! */
373 if (!ic_got_reply) {
374 ic_got_reply = IC_RARP;
375 ic_dev = dev;
376 if (ic_myaddr == INADDR_NONE)
377 ic_myaddr = tip;
378 ic_servaddr = sip;
381 /* And throw the packet out... */
382 drop:
383 kfree_skb(skb);
384 return 0;
389 * Send RARP request packet over all devices which allow RARP.
391 static void __init ic_rarp_send(void)
393 struct ic_device *d;
395 for (d=ic_first_dev; d; d=d->next)
396 if (d->able & IC_RARP) {
397 struct device *dev = d->dev;
398 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
399 dev->dev_addr, dev->dev_addr);
403 #endif
406 * BOOTP support.
409 #ifdef CONFIG_IP_PNP_BOOTP
411 struct bootp_pkt { /* BOOTP packet format */
412 struct iphdr iph; /* IP header */
413 struct udphdr udph; /* UDP header */
414 u8 op; /* 1=request, 2=reply */
415 u8 htype; /* HW address type */
416 u8 hlen; /* HW address length */
417 u8 hops; /* Used only by gateways */
418 u32 xid; /* Transaction ID */
419 u16 secs; /* Seconds since we started */
420 u16 flags; /* Just what it says */
421 u32 client_ip; /* Client's IP address if known */
422 u32 your_ip; /* Assigned IP address */
423 u32 server_ip; /* Server's IP address */
424 u32 relay_ip; /* IP address of BOOTP relay */
425 u8 hw_addr[16]; /* Client's HW address */
426 u8 serv_name[64]; /* Server host name */
427 u8 boot_file[128]; /* Name of boot file */
428 u8 vendor_area[128]; /* Area for extensions */
431 #define BOOTP_REQUEST 1
432 #define BOOTP_REPLY 2
434 static u32 ic_bootp_xid;
436 static int ic_bootp_recv(struct sk_buff *skb, struct device *dev, struct packet_type *pt);
438 static struct packet_type bootp_packet_type __initdata = {
439 __constant_htons(ETH_P_IP),
440 NULL, /* Listen to all devices */
441 ic_bootp_recv,
442 NULL,
443 NULL
448 * Initialize BOOTP extension fields in the request.
450 static void __init ic_bootp_init_ext(u8 *e)
452 *e++ = 99; /* RFC1048 Magic Cookie */
453 *e++ = 130;
454 *e++ = 83;
455 *e++ = 99;
456 *e++ = 1; /* Subnet mask request */
457 *e++ = 4;
458 e += 4;
459 *e++ = 3; /* Default gateway request */
460 *e++ = 4;
461 e += 4;
462 *e++ = 12; /* Host name request */
463 *e++ = 32;
464 e += 32;
465 *e++ = 40; /* NIS Domain name request */
466 *e++ = 32;
467 e += 32;
468 *e++ = 17; /* Boot path */
469 *e++ = 32;
470 e += 32;
471 *e = 255; /* End of the list */
476 * Initialize the BOOTP mechanism.
478 static inline void ic_bootp_init(void)
480 get_random_bytes(&ic_bootp_xid, sizeof(u32));
481 DBG(("BOOTP: XID=%08x\n", ic_bootp_xid));
482 dev_add_pack(&bootp_packet_type);
487 * BOOTP cleanup.
489 static inline void ic_bootp_cleanup(void)
491 dev_remove_pack(&bootp_packet_type);
496 * Send BOOTP request to single interface.
498 static void __init ic_bootp_send_if(struct ic_device *d, u32 jiffies)
500 struct device *dev = d->dev;
501 struct sk_buff *skb;
502 struct bootp_pkt *b;
503 int hh_len = (dev->hard_header_len + 15) & ~15;
504 struct iphdr *h;
506 /* Allocate packet */
507 skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
508 if (!skb)
509 return;
510 skb_reserve(skb, hh_len);
511 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
512 memset(b, 0, sizeof(struct bootp_pkt));
514 /* Construct IP header */
515 skb->nh.iph = h = &b->iph;
516 h->version = 4;
517 h->ihl = 5;
518 h->tot_len = htons(sizeof(struct bootp_pkt));
519 h->frag_off = htons(IP_DF);
520 h->ttl = 64;
521 h->protocol = IPPROTO_UDP;
522 h->daddr = INADDR_BROADCAST;
523 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
525 /* Construct UDP header */
526 b->udph.source = htons(68);
527 b->udph.dest = htons(67);
528 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
529 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
531 /* Construct BOOTP header */
532 b->op = BOOTP_REQUEST;
533 b->htype = dev->type;
534 b->hlen = dev->addr_len;
535 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
536 b->secs = htons(jiffies / HZ);
537 b->xid = ic_bootp_xid;
538 ic_bootp_init_ext(b->vendor_area);
540 /* Chain packet down the line... */
541 skb->dev = dev;
542 skb->protocol = __constant_htons(ETH_P_IP);
543 if ((dev->hard_header &&
544 dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
545 dev_queue_xmit(skb) < 0)
546 printk("E");
551 * Send BOOTP requests to all interfaces.
553 static void __init ic_bootp_send(u32 jiffies)
555 struct ic_device *d;
557 for(d=ic_first_dev; d; d=d->next)
558 if (d->able & IC_BOOTP)
559 ic_bootp_send_if(d, jiffies);
564 * Copy BOOTP-supplied string if not already set.
566 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
568 if (!len)
569 return 0;
570 if (len > max-1)
571 len = max-1;
572 strncpy(dest, src, len);
573 dest[len] = '\0';
574 return 1;
579 * Process BOOTP extension.
581 static void __init ic_do_bootp_ext(u8 *ext)
583 #ifdef IPCONFIG_DEBUG
584 u8 *c;
586 printk("BOOTP: Got extension %02x",*ext);
587 for(c=ext+2; c<ext+2+ext[1]; c++)
588 printk(" %02x", *c);
589 printk("\n");
590 #endif
592 switch (*ext++) {
593 case 1: /* Subnet mask */
594 if (ic_netmask == INADDR_NONE)
595 memcpy(&ic_netmask, ext+1, 4);
596 break;
597 case 3: /* Default gateway */
598 if (ic_gateway == INADDR_NONE)
599 memcpy(&ic_gateway, ext+1, 4);
600 break;
601 case 12: /* Host name */
602 ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
603 ic_host_name_set = 1;
604 break;
605 case 40: /* NIS Domain name */
606 ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
607 break;
608 case 17: /* Root path */
609 if (!root_server_path[0])
610 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
611 break;
617 * Receive BOOTP reply.
619 static int __init ic_bootp_recv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
621 struct bootp_pkt *b = (struct bootp_pkt *) skb->nh.iph;
622 struct iphdr *h = &b->iph;
623 int len;
625 /* If we already have a reply, just drop the packet */
626 if (ic_got_reply)
627 goto drop;
629 /* Check whether it's a BOOTP packet */
630 if (skb->pkt_type == PACKET_OTHERHOST ||
631 skb->len < sizeof(struct udphdr) + sizeof(struct iphdr) ||
632 h->ihl != 5 ||
633 h->version != 4 ||
634 ip_fast_csum((char *) h, h->ihl) != 0 ||
635 skb->len < ntohs(h->tot_len) ||
636 h->protocol != IPPROTO_UDP ||
637 b->udph.source != htons(67) ||
638 b->udph.dest != htons(68) ||
639 ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
640 goto drop;
642 /* Fragments are not supported */
643 if (h->frag_off & htons(IP_OFFSET|IP_MF)) {
644 printk(KERN_ERR "BOOTP: Ignoring fragmented reply.\n");
645 goto drop;
648 /* Is it a reply to our BOOTP request? */
649 len = ntohs(b->udph.len) - sizeof(struct udphdr);
650 if (len < 300 || /* See RFC 951:2.1 */
651 b->op != BOOTP_REPLY ||
652 b->xid != ic_bootp_xid) {
653 printk("?");
654 goto drop;
657 /* Extract basic fields */
658 ic_myaddr = b->your_ip;
659 ic_servaddr = b->server_ip;
660 ic_got_reply = IC_BOOTP;
661 ic_dev = dev;
663 /* Parse extensions */
664 if (b->vendor_area[0] == 99 && /* Check magic cookie */
665 b->vendor_area[1] == 130 &&
666 b->vendor_area[2] == 83 &&
667 b->vendor_area[3] == 99) {
668 u8 *ext = &b->vendor_area[4];
669 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
670 while (ext < end && *ext != 0xff) {
671 if (*ext == 0) /* Padding */
672 ext++;
673 else {
674 u8 *opt = ext;
675 ext += ext[1] + 2;
676 if (ext <= end)
677 ic_do_bootp_ext(opt);
682 if (ic_gateway == INADDR_NONE && b->relay_ip)
683 ic_gateway = b->relay_ip;
685 drop:
686 kfree_skb(skb);
687 return 0;
691 #endif
695 * Dynamic IP configuration -- BOOTP and RARP.
698 #ifdef CONFIG_IP_PNP_DYNAMIC
700 static int __init ic_dynamic(void)
702 int retries;
703 unsigned long timeout, jiff;
704 unsigned long start_jiffies;
705 int do_rarp = ic_proto_have_if & IC_RARP;
706 int do_bootp = ic_proto_have_if & IC_BOOTP;
709 * If neither BOOTP nor RARP was selected, return with an error. This
710 * routine gets only called when some pieces of information are mis-
711 * sing, and without BOOTP and RARP we are not able to get that in-
712 * formation.
714 if (!ic_proto_enabled) {
715 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
716 return -1;
719 #ifdef CONFIG_IP_PNP_BOOTP
720 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
721 printk(KERN_ERR "BOOTP: No suitable device found.\n");
722 #endif
724 #ifdef CONFIG_IP_PNP_RARP
725 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
726 printk(KERN_ERR "RARP: No suitable device found.\n");
727 #endif
729 if (!ic_proto_have_if)
730 /* Error message already printed */
731 return -1;
734 * Setup RARP and BOOTP protocols
736 #ifdef CONFIG_IP_PNP_RARP
737 if (do_rarp)
738 ic_rarp_init();
739 #endif
740 #ifdef CONFIG_IP_PNP_BOOTP
741 if (do_bootp)
742 ic_bootp_init();
743 #endif
746 * Send requests and wait, until we get an answer. This loop
747 * seems to be a terrible waste of CPU time, but actually there is
748 * only one process running at all, so we don't need to use any
749 * scheduler functions.
750 * [Actually we could now, but the nothing else running note still
751 * applies.. - AC]
753 printk(KERN_NOTICE "Sending %s%s%s requests...",
754 do_bootp ? "BOOTP" : "",
755 do_bootp && do_rarp ? " and " : "",
756 do_rarp ? "RARP" : "");
757 start_jiffies = jiffies;
758 retries = CONF_RETRIES;
759 get_random_bytes(&timeout, sizeof(timeout));
760 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
761 for(;;) {
762 #ifdef CONFIG_IP_PNP_BOOTP
763 if (do_bootp)
764 ic_bootp_send(jiffies - start_jiffies);
765 #endif
766 #ifdef CONFIG_IP_PNP_RARP
767 if (do_rarp)
768 ic_rarp_send();
769 #endif
770 printk(".");
771 jiff = jiffies + timeout;
772 while (jiffies < jiff && !ic_got_reply)
774 if (ic_got_reply) {
775 printk(" OK\n");
776 break;
778 if (! --retries) {
779 printk(" timed out!\n");
780 break;
782 timeout = timeout CONF_TIMEOUT_MULT;
783 if (timeout > CONF_TIMEOUT_MAX)
784 timeout = CONF_TIMEOUT_MAX;
787 #ifdef CONFIG_IP_PNP_RARP
788 if (do_rarp)
789 ic_rarp_cleanup();
790 #endif
791 #ifdef CONFIG_IP_PNP_BOOTP
792 if (do_bootp)
793 ic_bootp_cleanup();
794 #endif
796 if (!ic_got_reply)
797 return -1;
799 printk("IP-Config: Got %s answer from %s, ",
800 (ic_got_reply & IC_BOOTP) ? "BOOTP" : "RARP",
801 in_ntoa(ic_servaddr));
802 printk("my address is %s\n", in_ntoa(ic_myaddr));
804 return 0;
807 #endif
810 * IP Autoconfig dispatcher.
813 int __init ip_auto_config(void)
815 if (!ic_enable)
816 return 0;
818 DBG(("IP-Config: Entered.\n"));
820 /* Setup all network devices */
821 if (ic_open_devs() < 0)
822 return -1;
825 * If the config information is insufficient (e.g., our IP address or
826 * IP address of the boot server is missing or we have multiple network
827 * interfaces and no default was set), use BOOTP or RARP to get the
828 * missing values.
830 if (ic_myaddr == INADDR_NONE ||
831 #ifdef CONFIG_ROOT_NFS
832 (root_server_addr == INADDR_NONE && ic_servaddr == INADDR_NONE) ||
833 #endif
834 ic_first_dev->next) {
835 #ifdef CONFIG_IP_PNP_DYNAMIC
836 if (ic_dynamic() < 0) {
837 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
838 ic_close_devs();
839 return -1;
841 #else
842 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
843 ic_close_devs();
844 return -1;
845 #endif
846 } else {
847 ic_dev = ic_first_dev->dev; /* Device selected manually or only one device -> use it */
851 * Use defaults whereever applicable.
853 if (ic_defaults() < 0)
854 return -1;
857 * Close all network devices except the device we've
858 * autoconfigured and set up routes.
860 ic_close_devs();
861 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
862 return -1;
864 DBG(("IP-Config: device=%s, local=%08x, server=%08x, boot=%08x, gw=%08x, mask=%08x\n",
865 ic_dev->name, ic_myaddr, ic_servaddr, root_server_addr, ic_gateway, ic_netmask));
866 DBG(("IP-Config: host=%s, domain=%s, path=`%s'\n", system_utsname.nodename,
867 system_utsname.domainname, root_server_path));
868 return 0;
872 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
873 * command line parameter. It consists of option fields separated by colons in
874 * the following order:
876 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<bootp|rarp>
878 * Any of the fields can be empty which means to use a default value:
879 * <client-ip> - address given by BOOTP or RARP
880 * <server-ip> - address of host returning BOOTP or RARP packet
881 * <gw-ip> - none, or the address returned by BOOTP
882 * <netmask> - automatically determined from <client-ip>, or the
883 * one returned by BOOTP
884 * <host name> - <client-ip> in ASCII notation, or the name returned
885 * by BOOTP
886 * <device> - use all available devices
887 * <bootp|rarp|both|off> - use both protocols to determine my own address
889 static int __init ic_proto_name(char *name)
891 if (!strcmp(name, "off")) {
892 ic_proto_enabled = 0;
893 return 1;
895 #ifdef CONFIG_IP_PNP_BOOTP
896 else if (!strcmp(name, "bootp")) {
897 ic_proto_enabled &= ~IC_RARP;
898 return 1;
900 #endif
901 #ifdef CONFIG_IP_PNP_RARP
902 else if (!strcmp(name, "rarp")) {
903 ic_proto_enabled &= ~IC_BOOTP;
904 return 1;
906 #endif
907 #ifdef CONFIG_IP_PNP_DYNAMIC
908 else if (!strcmp(name, "both")) {
909 return 1;
911 #endif
912 return 0;
915 void __init ip_auto_config_setup(char *addrs, int *ints)
917 char *cp, *ip, *dp;
918 int num = 0;
920 ic_set_manually = 1;
921 if (!strcmp(addrs, "off")) {
922 ic_enable = 0;
923 return;
925 if (ic_proto_name(addrs))
926 return;
928 /* Parse the whole string */
929 ip = addrs;
930 while (ip && *ip) {
931 if ((cp = strchr(ip, ':')))
932 *cp++ = '\0';
933 if (strlen(ip) > 0) {
934 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
935 switch (num) {
936 case 0:
937 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
938 ic_myaddr = INADDR_NONE;
939 break;
940 case 1:
941 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
942 ic_servaddr = INADDR_NONE;
943 break;
944 case 2:
945 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
946 ic_gateway = INADDR_NONE;
947 break;
948 case 3:
949 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
950 ic_netmask = INADDR_NONE;
951 break;
952 case 4:
953 if ((dp = strchr(ip, '.'))) {
954 *dp++ = '\0';
955 strncpy(system_utsname.domainname, dp, __NEW_UTS_LEN);
956 system_utsname.domainname[__NEW_UTS_LEN] = '\0';
958 strncpy(system_utsname.nodename, ip, __NEW_UTS_LEN);
959 system_utsname.nodename[__NEW_UTS_LEN] = '\0';
960 ic_host_name_set = 1;
961 break;
962 case 5:
963 strncpy(user_dev_name, ip, IFNAMSIZ);
964 user_dev_name[IFNAMSIZ-1] = '\0';
965 break;
966 case 6:
967 ic_proto_name(ip);
968 break;
971 ip = cp;
972 num++;