2 * $Id: ipconfig.c,v 1.46 2002/02/01 22:01:04 davem Exp $
4 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
5 * user-supplied 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
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
24 * Ported DHCP support from 2.2.16 to 2.4.0-test4
25 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
27 * Merged changes from 2.2.19 into 2.4.3
28 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
30 * Multiple Nameservers in /proc/net/pnp
31 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/kernel.h>
37 #include <linux/jiffies.h>
38 #include <linux/random.h>
39 #include <linux/init.h>
40 #include <linux/utsname.h>
43 #include <linux/inet.h>
44 #include <linux/inetdevice.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
49 #include <linux/socket.h>
50 #include <linux/route.h>
51 #include <linux/udp.h>
52 #include <linux/proc_fs.h>
53 #include <linux/seq_file.h>
54 #include <linux/major.h>
55 #include <linux/root_dev.h>
56 #include <linux/delay.h>
57 #include <linux/nfs_fs.h>
60 #include <net/ipconfig.h>
61 #include <net/route.h>
63 #include <asm/uaccess.h>
64 #include <net/checksum.h>
65 #include <asm/processor.h>
67 /* Define this to allow debugging output */
71 #define DBG(x) printk x
73 #define DBG(x) do { } while(0)
76 #if defined(CONFIG_IP_PNP_DHCP)
79 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
80 #define IPCONFIG_BOOTP
82 #if defined(CONFIG_IP_PNP_RARP)
85 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
86 #define IPCONFIG_DYNAMIC
89 /* Define the friendly delay before and after opening net devices */
90 #define CONF_PRE_OPEN 500 /* Before opening: 1/2 second */
91 #define CONF_POST_OPEN 1 /* After opening: 1 second */
93 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
94 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
95 #define CONF_SEND_RETRIES 6 /* Send six requests per open */
96 #define CONF_INTER_TIMEOUT (HZ/2) /* Inter-device timeout: 1/2 second */
97 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
98 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
99 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
100 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
101 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
102 - '3' from resolv.h */
106 * Public IP configuration
109 /* This is used by platforms which might be able to set the ipconfig
110 * variables using firmware environment vars. If this is set, it will
111 * ignore such firmware variables.
113 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
115 static int ic_enable __initdata = 0; /* IP config enabled? */
117 /* Protocol choice */
118 int ic_proto_enabled __initdata = 0
119 #ifdef IPCONFIG_BOOTP
122 #ifdef CONFIG_IP_PNP_DHCP
130 static int ic_host_name_set __initdata = 0; /* Host name set by us? */
132 u32 ic_myaddr = INADDR_NONE; /* My IP address */
133 static u32 ic_netmask = INADDR_NONE; /* Netmask for local subnet */
134 u32 ic_gateway = INADDR_NONE; /* Gateway IP address */
136 u32 ic_servaddr = INADDR_NONE; /* Boot server IP address */
138 u32 root_server_addr = INADDR_NONE; /* Address of NFS server */
139 u8 root_server_path[256] = { 0, }; /* Path to mount as root */
141 /* Persistent data: */
143 static int ic_proto_used; /* Protocol used, if any */
144 static u32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
145 static u8 ic_domain[64]; /* DNS (not NIS) domain name */
151 /* Name of user-selected boot device */
152 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
154 /* Protocols supported by available interfaces */
155 static int ic_proto_have_if __initdata = 0;
157 #ifdef IPCONFIG_DYNAMIC
158 static DEFINE_SPINLOCK(ic_recv_lock);
159 static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */
162 static int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */
171 struct ic_device *next;
172 struct net_device *dev;
173 unsigned short flags;
178 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
179 static struct net_device *ic_dev __initdata = NULL; /* Selected device */
181 static int __init ic_open_devs(void)
183 struct ic_device *d, **last;
184 struct net_device *dev;
185 unsigned short oflags;
187 last = &ic_first_dev;
190 /* bring loopback device up first */
191 if (dev_change_flags(&loopback_dev, loopback_dev.flags | IFF_UP) < 0)
192 printk(KERN_ERR "IP-Config: Failed to open %s\n", loopback_dev.name);
194 for (dev = dev_base; dev; dev = dev->next) {
195 if (dev == &loopback_dev)
197 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
198 (!(dev->flags & IFF_LOOPBACK) &&
199 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
200 strncmp(dev->name, "dummy", 5))) {
205 printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
206 if (!(dev->flags & IFF_NOARP))
208 able &= ic_proto_enabled;
209 if (ic_proto_enabled && !able)
212 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
213 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
216 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
226 get_random_bytes(&d->xid, sizeof(u32));
229 ic_proto_have_if |= able;
230 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
231 dev->name, able, d->xid));
239 if (user_dev_name[0])
240 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
242 printk(KERN_ERR "IP-Config: No network devices available.\n");
248 static void __init ic_close_devs(void)
250 struct ic_device *d, *next;
251 struct net_device *dev;
259 DBG(("IP-Config: Downing %s\n", dev->name));
260 dev_change_flags(dev, d->flags);
268 * Interface to various network functions.
272 set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
274 sin->sin_family = AF_INET;
275 sin->sin_addr.s_addr = addr;
276 sin->sin_port = port;
279 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
283 mm_segment_t oldfs = get_fs();
285 res = devinet_ioctl(cmd, (struct ifreq __user *) arg);
290 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
294 mm_segment_t oldfs = get_fs();
296 res = ip_rt_ioctl(cmd, (void __user *) arg);
302 * Set up interface addresses and routes.
305 static int __init ic_setup_if(void)
308 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
311 memset(&ir, 0, sizeof(ir));
312 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
313 set_sockaddr(sin, ic_myaddr, 0);
314 if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
315 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
318 set_sockaddr(sin, ic_netmask, 0);
319 if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
320 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
323 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
324 if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
325 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
331 static int __init ic_setup_routes(void)
333 /* No need to setup device routes, only the default route... */
335 if (ic_gateway != INADDR_NONE) {
339 memset(&rm, 0, sizeof(rm));
340 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
341 printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
344 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
345 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
346 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
347 rm.rt_flags = RTF_UP | RTF_GATEWAY;
348 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
349 printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
358 * Fill in default values for all missing parameters.
361 static int __init ic_defaults(void)
364 * At this point we have no userspace running so need not
365 * claim locks on system_utsname
368 if (!ic_host_name_set)
369 sprintf(init_utsname()->nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr));
371 if (root_server_addr == INADDR_NONE)
372 root_server_addr = ic_servaddr;
374 if (ic_netmask == INADDR_NONE) {
375 if (IN_CLASSA(ntohl(ic_myaddr)))
376 ic_netmask = htonl(IN_CLASSA_NET);
377 else if (IN_CLASSB(ntohl(ic_myaddr)))
378 ic_netmask = htonl(IN_CLASSB_NET);
379 else if (IN_CLASSC(ntohl(ic_myaddr)))
380 ic_netmask = htonl(IN_CLASSC_NET);
382 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
386 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));
398 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
400 static struct packet_type rarp_packet_type __initdata = {
401 .type = __constant_htons(ETH_P_RARP),
402 .func = ic_rarp_recv,
405 static inline void ic_rarp_init(void)
407 dev_add_pack(&rarp_packet_type);
410 static inline void ic_rarp_cleanup(void)
412 dev_remove_pack(&rarp_packet_type);
416 * Process received RARP packet.
419 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
422 unsigned char *rarp_ptr;
423 unsigned long sip, tip;
424 unsigned char *sha, *tha; /* s for "source", t for "target" */
427 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
430 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
433 /* Basic sanity checks can be done without the lock. */
434 rarp = (struct arphdr *)skb->h.raw;
436 /* If this test doesn't pass, it's not IP, or we should
439 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
442 /* If it's not a RARP reply, delete it. */
443 if (rarp->ar_op != htons(ARPOP_RREPLY))
446 /* If it's not Ethernet, delete it. */
447 if (rarp->ar_pro != htons(ETH_P_IP))
450 if (!pskb_may_pull(skb,
451 sizeof(struct arphdr) +
452 (2 * dev->addr_len) +
456 /* OK, it is all there and looks valid, process... */
457 rarp = (struct arphdr *)skb->h.raw;
458 rarp_ptr = (unsigned char *) (rarp + 1);
460 /* One reply at a time, please. */
461 spin_lock(&ic_recv_lock);
463 /* If we already have a reply, just drop the packet */
467 /* Find the ic_device that the packet arrived on */
469 while (d && d->dev != dev)
472 goto drop_unlock; /* should never happen */
474 /* Extract variable-width fields */
476 rarp_ptr += dev->addr_len;
477 memcpy(&sip, rarp_ptr, 4);
480 rarp_ptr += dev->addr_len;
481 memcpy(&tip, rarp_ptr, 4);
483 /* Discard packets which are not meant for us. */
484 if (memcmp(tha, dev->dev_addr, dev->addr_len))
487 /* Discard packets which are not from specified server. */
488 if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
491 /* We have a winner! */
493 if (ic_myaddr == INADDR_NONE)
496 ic_got_reply = IC_RARP;
499 /* Show's over. Nothing to see here. */
500 spin_unlock(&ic_recv_lock);
503 /* Throw the packet out. */
510 * Send RARP request packet over a single interface.
512 static void __init ic_rarp_send_if(struct ic_device *d)
514 struct net_device *dev = d->dev;
515 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
516 dev->dev_addr, dev->dev_addr);
521 * DHCP/BOOTP support.
524 #ifdef IPCONFIG_BOOTP
526 struct bootp_pkt { /* BOOTP packet format */
527 struct iphdr iph; /* IP header */
528 struct udphdr udph; /* UDP header */
529 u8 op; /* 1=request, 2=reply */
530 u8 htype; /* HW address type */
531 u8 hlen; /* HW address length */
532 u8 hops; /* Used only by gateways */
533 u32 xid; /* Transaction ID */
534 u16 secs; /* Seconds since we started */
535 u16 flags; /* Just what it says */
536 u32 client_ip; /* Client's IP address if known */
537 u32 your_ip; /* Assigned IP address */
538 u32 server_ip; /* (Next, e.g. NFS) Server's IP address */
539 u32 relay_ip; /* IP address of BOOTP relay */
540 u8 hw_addr[16]; /* Client's HW address */
541 u8 serv_name[64]; /* Server host name */
542 u8 boot_file[128]; /* Name of boot file */
543 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
547 #define BOOTP_REQUEST 1
548 #define BOOTP_REPLY 2
550 /* DHCP message types */
551 #define DHCPDISCOVER 1
553 #define DHCPREQUEST 3
554 #define DHCPDECLINE 4
557 #define DHCPRELEASE 7
560 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
562 static struct packet_type bootp_packet_type __initdata = {
563 .type = __constant_htons(ETH_P_IP),
564 .func = ic_bootp_recv,
569 * Initialize DHCP/BOOTP extension fields in the request.
572 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
577 ic_dhcp_init_options(u8 *options)
579 u8 mt = ((ic_servaddr == INADDR_NONE)
580 ? DHCPDISCOVER : DHCPREQUEST);
583 #ifdef IPCONFIG_DEBUG
584 printk("DHCP: Sending message type %d\n", mt);
587 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
590 *e++ = 53; /* DHCP message type */
594 if (mt == DHCPREQUEST) {
595 *e++ = 54; /* Server ID (IP address) */
597 memcpy(e, &ic_servaddr, 4);
600 *e++ = 50; /* Requested IP address */
602 memcpy(e, &ic_myaddr, 4);
608 static const u8 ic_req_params[] = {
610 3, /* Default gateway */
613 15, /* Domain name */
615 40, /* NIS domain name */
618 *e++ = 55; /* Parameter request list */
619 *e++ = sizeof(ic_req_params);
620 memcpy(e, ic_req_params, sizeof(ic_req_params));
621 e += sizeof(ic_req_params);
624 *e++ = 255; /* End of the list */
627 #endif /* IPCONFIG_DHCP */
629 static void __init ic_bootp_init_ext(u8 *e)
631 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
633 *e++ = 1; /* Subnet mask request */
636 *e++ = 3; /* Default gateway request */
639 *e++ = 5; /* Name server request */
642 *e++ = 12; /* Host name request */
645 *e++ = 40; /* NIS Domain name request */
648 *e++ = 17; /* Boot path */
652 *e++ = 57; /* set extension buffer size for reply */
654 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
657 *e++ = 255; /* End of the list */
662 * Initialize the DHCP/BOOTP mechanism.
664 static inline void ic_bootp_init(void)
668 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
669 ic_nameservers[i] = INADDR_NONE;
671 dev_add_pack(&bootp_packet_type);
676 * DHCP/BOOTP cleanup.
678 static inline void ic_bootp_cleanup(void)
680 dev_remove_pack(&bootp_packet_type);
685 * Send DHCP/BOOTP request to single interface.
687 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
689 struct net_device *dev = d->dev;
692 int hh_len = LL_RESERVED_SPACE(dev);
695 /* Allocate packet */
696 skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
699 skb_reserve(skb, hh_len);
700 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
701 memset(b, 0, sizeof(struct bootp_pkt));
703 /* Construct IP header */
704 skb->nh.iph = h = &b->iph;
707 h->tot_len = htons(sizeof(struct bootp_pkt));
708 h->frag_off = htons(IP_DF);
710 h->protocol = IPPROTO_UDP;
711 h->daddr = INADDR_BROADCAST;
712 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
714 /* Construct UDP header */
715 b->udph.source = htons(68);
716 b->udph.dest = htons(67);
717 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
718 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
720 /* Construct DHCP/BOOTP header */
721 b->op = BOOTP_REQUEST;
722 if (dev->type < 256) /* check for false types */
723 b->htype = dev->type;
724 else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
725 b->htype = ARPHRD_IEEE802;
726 else if (dev->type == ARPHRD_FDDI)
727 b->htype = ARPHRD_ETHER;
729 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
730 b->htype = dev->type; /* can cause undefined behavior */
732 b->hlen = dev->addr_len;
733 b->your_ip = INADDR_NONE;
734 b->server_ip = INADDR_NONE;
735 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
736 b->secs = htons(jiffies_diff / HZ);
739 /* add DHCP options or BOOTP extensions */
741 if (ic_proto_enabled & IC_USE_DHCP)
742 ic_dhcp_init_options(b->exten);
745 ic_bootp_init_ext(b->exten);
747 /* Chain packet down the line... */
749 skb->protocol = htons(ETH_P_IP);
750 if ((dev->hard_header &&
751 dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
752 dev_queue_xmit(skb) < 0)
758 * Copy BOOTP-supplied string if not already set.
760 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
766 memcpy(dest, src, len);
773 * Process BOOTP extensions.
775 static void __init ic_do_bootp_ext(u8 *ext)
780 #ifdef IPCONFIG_DEBUG
783 printk("DHCP/BOOTP: Got extension %d:",*ext);
784 for(c=ext+2; c<ext+2+ext[1]; c++)
790 case 1: /* Subnet mask */
791 if (ic_netmask == INADDR_NONE)
792 memcpy(&ic_netmask, ext+1, 4);
794 case 3: /* Default gateway */
795 if (ic_gateway == INADDR_NONE)
796 memcpy(&ic_gateway, ext+1, 4);
798 case 6: /* DNS server */
800 if (servers > CONF_NAMESERVERS_MAX)
801 servers = CONF_NAMESERVERS_MAX;
802 for (i = 0; i < servers; i++) {
803 if (ic_nameservers[i] == INADDR_NONE)
804 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
807 case 12: /* Host name */
808 ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
809 ic_host_name_set = 1;
811 case 15: /* Domain name (DNS) */
812 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
814 case 17: /* Root path */
815 if (!root_server_path[0])
816 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
818 case 40: /* NIS Domain name (_not_ DNS) */
819 ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
826 * Receive BOOTP reply.
828 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
835 /* Perform verifications before taking the lock. */
836 if (skb->pkt_type == PACKET_OTHERHOST)
839 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
842 if (!pskb_may_pull(skb,
843 sizeof(struct iphdr) +
844 sizeof(struct udphdr)))
847 b = (struct bootp_pkt *) skb->nh.iph;
850 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
853 /* Fragments are not supported */
854 if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
856 printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
861 if (skb->len < ntohs(h->tot_len))
864 if (ip_fast_csum((char *) h, h->ihl))
867 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
870 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
873 len = ntohs(b->udph.len) - sizeof(struct udphdr);
874 ext_len = len - (sizeof(*b) -
875 sizeof(struct iphdr) -
876 sizeof(struct udphdr) -
881 /* Ok the front looks good, make sure we can get at the rest. */
882 if (!pskb_may_pull(skb, skb->len))
885 b = (struct bootp_pkt *) skb->nh.iph;
888 /* One reply at a time, please. */
889 spin_lock(&ic_recv_lock);
891 /* If we already have a reply, just drop the packet */
895 /* Find the ic_device that the packet arrived on */
897 while (d && d->dev != dev)
900 goto drop_unlock; /* should never happen */
902 /* Is it a reply to our BOOTP request? */
903 if (b->op != BOOTP_REPLY ||
906 printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
912 /* Parse extensions */
914 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
915 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
919 if (ic_proto_enabled & IC_USE_DHCP) {
920 u32 server_id = INADDR_NONE;
924 while (ext < end && *ext != 0xff) {
926 if (*opt == 0) /* Padding */
932 case 53: /* Message type */
936 case 54: /* Server ID (IP address) */
938 memcpy(&server_id, opt + 2, 4);
943 #ifdef IPCONFIG_DEBUG
944 printk("DHCP: Got message type %d\n", mt);
949 /* While in the process of accepting one offer,
952 if (ic_myaddr != INADDR_NONE)
955 /* Let's accept that offer. */
956 ic_myaddr = b->your_ip;
957 ic_servaddr = server_id;
958 #ifdef IPCONFIG_DEBUG
959 printk("DHCP: Offered address %u.%u.%u.%u",
961 printk(" by server %u.%u.%u.%u\n",
962 NIPQUAD(ic_servaddr));
964 /* The DHCP indicated server address takes
965 * precedence over the bootp header one if
966 * they are different.
968 if ((server_id != INADDR_NONE) &&
969 (b->server_ip != server_id))
970 b->server_ip = ic_servaddr;
974 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
981 /* Urque. Forget it*/
982 ic_myaddr = INADDR_NONE;
983 ic_servaddr = INADDR_NONE;
987 ic_dhcp_msgtype = mt;
990 #endif /* IPCONFIG_DHCP */
993 while (ext < end && *ext != 0xff) {
995 if (*opt == 0) /* Padding */
999 ic_do_bootp_ext(opt);
1003 /* We have a winner! */
1005 ic_myaddr = b->your_ip;
1006 ic_servaddr = b->server_ip;
1007 if (ic_gateway == INADDR_NONE && b->relay_ip)
1008 ic_gateway = b->relay_ip;
1009 if (ic_nameservers[0] == INADDR_NONE)
1010 ic_nameservers[0] = ic_servaddr;
1011 ic_got_reply = IC_BOOTP;
1014 /* Show's over. Nothing to see here. */
1015 spin_unlock(&ic_recv_lock);
1018 /* Throw the packet out. */
1029 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1032 #ifdef IPCONFIG_DYNAMIC
1034 static int __init ic_dynamic(void)
1037 struct ic_device *d;
1038 unsigned long start_jiffies, timeout, jiff;
1039 int do_bootp = ic_proto_have_if & IC_BOOTP;
1040 int do_rarp = ic_proto_have_if & IC_RARP;
1043 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1044 * This routine gets only called when some pieces of information
1045 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1047 if (!ic_proto_enabled) {
1048 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1052 #ifdef IPCONFIG_BOOTP
1053 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1054 printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
1056 #ifdef IPCONFIG_RARP
1057 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1058 printk(KERN_ERR "RARP: No suitable device found.\n");
1061 if (!ic_proto_have_if)
1062 /* Error message already printed */
1068 #ifdef IPCONFIG_BOOTP
1072 #ifdef IPCONFIG_RARP
1078 * Send requests and wait, until we get an answer. This loop
1079 * seems to be a terrible waste of CPU time, but actually there is
1080 * only one process running at all, so we don't need to use any
1081 * scheduler functions.
1082 * [Actually we could now, but the nothing else running note still
1085 printk(KERN_NOTICE "Sending %s%s%s requests .",
1087 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1088 (do_bootp && do_rarp) ? " and " : "",
1089 do_rarp ? "RARP" : "");
1091 start_jiffies = jiffies;
1093 retries = CONF_SEND_RETRIES;
1094 get_random_bytes(&timeout, sizeof(timeout));
1095 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
1097 #ifdef IPCONFIG_BOOTP
1098 if (do_bootp && (d->able & IC_BOOTP))
1099 ic_bootp_send_if(d, jiffies - start_jiffies);
1101 #ifdef IPCONFIG_RARP
1102 if (do_rarp && (d->able & IC_RARP))
1106 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1107 while (time_before(jiffies, jiff) && !ic_got_reply)
1108 schedule_timeout_uninterruptible(1);
1109 #ifdef IPCONFIG_DHCP
1110 /* DHCP isn't done until we get a DHCPACK. */
1111 if ((ic_got_reply & IC_BOOTP)
1112 && (ic_proto_enabled & IC_USE_DHCP)
1113 && ic_dhcp_msgtype != DHCPACK)
1119 #endif /* IPCONFIG_DHCP */
1130 printk(" timed out!\n");
1136 timeout = timeout CONF_TIMEOUT_MULT;
1137 if (timeout > CONF_TIMEOUT_MAX)
1138 timeout = CONF_TIMEOUT_MAX;
1143 #ifdef IPCONFIG_BOOTP
1147 #ifdef IPCONFIG_RARP
1152 if (!ic_got_reply) {
1153 ic_myaddr = INADDR_NONE;
1157 printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
1158 ((ic_got_reply & IC_RARP) ? "RARP"
1159 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1160 NIPQUAD(ic_servaddr));
1161 printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
1166 #endif /* IPCONFIG_DYNAMIC */
1168 #ifdef CONFIG_PROC_FS
1170 static int pnp_seq_show(struct seq_file *seq, void *v)
1174 if (ic_proto_used & IC_PROTO)
1175 seq_printf(seq, "#PROTO: %s\n",
1176 (ic_proto_used & IC_RARP) ? "RARP"
1177 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1179 seq_puts(seq, "#MANUAL\n");
1183 "domain %s\n", ic_domain);
1184 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1185 if (ic_nameservers[i] != INADDR_NONE)
1187 "nameserver %u.%u.%u.%u\n",
1188 NIPQUAD(ic_nameservers[i]));
1190 if (ic_servaddr != INADDR_NONE)
1192 "bootserver %u.%u.%u.%u\n",
1193 NIPQUAD(ic_servaddr));
1197 static int pnp_seq_open(struct inode *indoe, struct file *file)
1199 return single_open(file, pnp_seq_show, NULL);
1202 static struct file_operations pnp_seq_fops = {
1203 .owner = THIS_MODULE,
1204 .open = pnp_seq_open,
1206 .llseek = seq_lseek,
1207 .release = single_release,
1209 #endif /* CONFIG_PROC_FS */
1212 * Extract IP address from the parameter string if needed. Note that we
1213 * need to have root_server_addr set _before_ IPConfig gets called as it
1216 u32 __init root_nfs_parse_addr(char *name)
1223 while (octets < 4) {
1224 while (*cp >= '0' && *cp <= '9')
1226 if (cp == cq || cp - cq > 3)
1228 if (*cp == '.' || octets == 3)
1234 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1237 addr = in_aton(name);
1238 memmove(name, cp, strlen(cp) + 1);
1246 * IP Autoconfig dispatcher.
1249 static int __init ip_auto_config(void)
1253 #ifdef CONFIG_PROC_FS
1254 proc_net_fops_create("pnp", S_IRUGO, &pnp_seq_fops);
1255 #endif /* CONFIG_PROC_FS */
1260 DBG(("IP-Config: Entered.\n"));
1261 #ifdef IPCONFIG_DYNAMIC
1264 /* Give hardware a chance to settle */
1265 msleep(CONF_PRE_OPEN);
1267 /* Setup all network devices */
1268 if (ic_open_devs() < 0)
1271 /* Give drivers a chance to settle */
1272 ssleep(CONF_POST_OPEN);
1275 * If the config information is insufficient (e.g., our IP address or
1276 * IP address of the boot server is missing or we have multiple network
1277 * interfaces and no default was set), use BOOTP or RARP to get the
1280 if (ic_myaddr == INADDR_NONE ||
1281 #ifdef CONFIG_ROOT_NFS
1282 (MAJOR(ROOT_DEV) == UNNAMED_MAJOR
1283 && root_server_addr == INADDR_NONE
1284 && ic_servaddr == INADDR_NONE) ||
1286 ic_first_dev->next) {
1287 #ifdef IPCONFIG_DYNAMIC
1289 int retries = CONF_OPEN_RETRIES;
1291 if (ic_dynamic() < 0) {
1295 * I don't know why, but sometimes the
1296 * eepro100 driver (at least) gets upset and
1297 * doesn't work the first time it's opened.
1298 * But then if you close it and reopen it, it
1299 * works just fine. So we need to try that at
1300 * least once before giving up.
1302 * Also, if the root will be NFS-mounted, we
1303 * have nowhere to go if DHCP fails. So we
1304 * just have to keep trying forever.
1308 #ifdef CONFIG_ROOT_NFS
1309 if (ROOT_DEV == Root_NFS) {
1311 "IP-Config: Retrying forever (NFS root)...\n");
1318 "IP-Config: Reopening network devices...\n");
1322 /* Oh, well. At least we tried. */
1323 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1326 #else /* !DYNAMIC */
1327 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1330 #endif /* IPCONFIG_DYNAMIC */
1332 /* Device selected manually or only one device -> use it */
1333 ic_dev = ic_first_dev->dev;
1336 addr = root_nfs_parse_addr(root_server_path);
1337 if (root_server_addr == INADDR_NONE)
1338 root_server_addr = addr;
1341 * Use defaults whereever applicable.
1343 if (ic_defaults() < 0)
1347 * Close all network devices except the device we've
1348 * autoconfigured and set up routes.
1351 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1355 * Record which protocol was actually used.
1357 #ifdef IPCONFIG_DYNAMIC
1358 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1361 #ifndef IPCONFIG_SILENT
1363 * Clue in the operator.
1365 printk("IP-Config: Complete:");
1366 printk("\n device=%s", ic_dev->name);
1367 printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr));
1368 printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask));
1369 printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway));
1370 printk(",\n host=%s, domain=%s, nis-domain=%s",
1371 utsname()->nodename, ic_domain, utsname()->domainname);
1372 printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr));
1373 printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr));
1374 printk(", rootpath=%s", root_server_path);
1376 #endif /* !SILENT */
1381 late_initcall(ip_auto_config);
1385 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1386 * command line parameter. It consists of option fields separated by colons in
1387 * the following order:
1389 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO>
1391 * Any of the fields can be empty which means to use a default value:
1392 * <client-ip> - address given by BOOTP or RARP
1393 * <server-ip> - address of host returning BOOTP or RARP packet
1394 * <gw-ip> - none, or the address returned by BOOTP
1395 * <netmask> - automatically determined from <client-ip>, or the
1396 * one returned by BOOTP
1397 * <host name> - <client-ip> in ASCII notation, or the name returned
1399 * <device> - use all available devices
1401 * off|none - don't do autoconfig at all (DEFAULT)
1402 * on|any - use any configured protocol
1403 * dhcp|bootp|rarp - use only the specified protocol
1404 * both - use both BOOTP and RARP (not DHCP)
1406 static int __init ic_proto_name(char *name)
1408 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1411 #ifdef CONFIG_IP_PNP_DHCP
1412 else if (!strcmp(name, "dhcp")) {
1413 ic_proto_enabled &= ~IC_RARP;
1417 #ifdef CONFIG_IP_PNP_BOOTP
1418 else if (!strcmp(name, "bootp")) {
1419 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1423 #ifdef CONFIG_IP_PNP_RARP
1424 else if (!strcmp(name, "rarp")) {
1425 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1429 #ifdef IPCONFIG_DYNAMIC
1430 else if (!strcmp(name, "both")) {
1431 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1438 static int __init ip_auto_config_setup(char *addrs)
1443 ic_set_manually = 1;
1445 ic_enable = (*addrs &&
1446 (strcmp(addrs, "off") != 0) &&
1447 (strcmp(addrs, "none") != 0));
1451 if (ic_proto_name(addrs))
1454 /* Parse the whole string */
1457 if ((cp = strchr(ip, ':')))
1459 if (strlen(ip) > 0) {
1460 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1463 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
1464 ic_myaddr = INADDR_NONE;
1467 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
1468 ic_servaddr = INADDR_NONE;
1471 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
1472 ic_gateway = INADDR_NONE;
1475 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
1476 ic_netmask = INADDR_NONE;
1479 if ((dp = strchr(ip, '.'))) {
1481 strlcpy(utsname()->domainname, dp,
1482 sizeof(utsname()->domainname));
1484 strlcpy(utsname()->nodename, ip,
1485 sizeof(utsname()->nodename));
1486 ic_host_name_set = 1;
1489 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1503 static int __init nfsaddrs_config_setup(char *addrs)
1505 return ip_auto_config_setup(addrs);
1508 __setup("ip=", ip_auto_config_setup);
1509 __setup("nfsaddrs=", nfsaddrs_config_setup);