2 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
3 * user-supplied information to configure own IP address and routes.
5 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 * Derived from network configuration code in fs/nfs/nfsroot.c,
8 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
10 * BOOTP rewritten to construct and analyse packets itself instead
11 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
12 * -- MJ, December 1998
14 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
15 * initialization scheme.
16 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
18 * DHCP support added. To users this looks like a whole separate
19 * protocol, but we know it's just a bag on the side of BOOTP.
20 * -- Chip Salzenberg <chip@valinux.com>, May 2000
22 * Ported DHCP support from 2.2.16 to 2.4.0-test4
23 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
25 * Merged changes from 2.2.19 into 2.4.3
26 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
28 * Multiple Nameservers in /proc/net/pnp
29 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/jiffies.h>
36 #include <linux/random.h>
37 #include <linux/init.h>
38 #include <linux/utsname.h>
41 #include <linux/inet.h>
42 #include <linux/inetdevice.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
47 #include <linux/socket.h>
48 #include <linux/route.h>
49 #include <linux/udp.h>
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
52 #include <linux/major.h>
53 #include <linux/root_dev.h>
54 #include <linux/delay.h>
55 #include <linux/nfs_fs.h>
56 #include <linux/slab.h>
57 #include <linux/export.h>
58 #include <net/net_namespace.h>
61 #include <net/ipconfig.h>
62 #include <net/route.h>
64 #include <asm/uaccess.h>
65 #include <net/checksum.h>
66 #include <asm/processor.h>
68 /* Define this to allow debugging output */
72 #define DBG(x) printk x
74 #define DBG(x) do { } while(0)
77 #if defined(CONFIG_IP_PNP_DHCP)
80 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
81 #define IPCONFIG_BOOTP
83 #if defined(CONFIG_IP_PNP_RARP)
86 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
87 #define IPCONFIG_DYNAMIC
90 /* Define the friendly delay before and after opening net devices */
91 #define CONF_POST_OPEN 10 /* After opening: 10 msecs */
92 #define CONF_CARRIER_TIMEOUT 120000 /* Wait for carrier timeout */
94 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
95 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
96 #define CONF_SEND_RETRIES 6 /* Send six requests per open */
97 #define CONF_INTER_TIMEOUT (HZ) /* Inter-device timeout: 1 second */
98 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
99 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
100 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
101 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
102 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
103 - '3' from resolv.h */
105 #define NONE cpu_to_be32(INADDR_NONE)
106 #define ANY cpu_to_be32(INADDR_ANY)
109 * Public IP configuration
112 /* This is used by platforms which might be able to set the ipconfig
113 * variables using firmware environment vars. If this is set, it will
114 * ignore such firmware variables.
116 int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
118 static int ic_enable __initdata; /* IP config enabled? */
120 /* Protocol choice */
121 int ic_proto_enabled __initdata = 0
122 #ifdef IPCONFIG_BOOTP
125 #ifdef CONFIG_IP_PNP_DHCP
133 static int ic_host_name_set __initdata; /* Host name set by us? */
135 __be32 ic_myaddr = NONE; /* My IP address */
136 static __be32 ic_netmask = NONE; /* Netmask for local subnet */
137 __be32 ic_gateway = NONE; /* Gateway IP address */
139 __be32 ic_addrservaddr = NONE; /* IP Address of the IP addresses'server */
141 __be32 ic_servaddr = NONE; /* Boot server IP address */
143 __be32 root_server_addr = NONE; /* Address of NFS server */
144 u8 root_server_path[256] = { 0, }; /* Path to mount as root */
146 /* vendor class identifier */
147 static char vendor_class_identifier[253] __initdata;
149 #if defined(CONFIG_IP_PNP_DHCP)
150 static char dhcp_client_identifier[253] __initdata;
153 /* Persistent data: */
155 static int ic_proto_used; /* Protocol used, if any */
156 static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
157 static u8 ic_domain[64]; /* DNS (not NIS) domain name */
163 /* Name of user-selected boot device */
164 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
166 /* Protocols supported by available interfaces */
167 static int ic_proto_have_if __initdata;
169 /* MTU for boot device */
170 static int ic_dev_mtu __initdata;
172 #ifdef IPCONFIG_DYNAMIC
173 static DEFINE_SPINLOCK(ic_recv_lock);
174 static volatile int ic_got_reply __initdata; /* Proto(s) that replied */
177 static int ic_dhcp_msgtype __initdata; /* DHCP msg type received */
186 struct ic_device *next;
187 struct net_device *dev;
188 unsigned short flags;
193 static struct ic_device *ic_first_dev __initdata; /* List of open device */
194 static struct net_device *ic_dev __initdata; /* Selected device */
196 static bool __init ic_is_init_dev(struct net_device *dev)
198 if (dev->flags & IFF_LOOPBACK)
200 return user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
201 (!(dev->flags & IFF_LOOPBACK) &&
202 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
203 strncmp(dev->name, "dummy", 5));
206 static int __init ic_open_devs(void)
208 struct ic_device *d, **last;
209 struct net_device *dev;
210 unsigned short oflags;
211 unsigned long start, next_msg;
213 last = &ic_first_dev;
216 /* bring loopback and DSA master network devices up first */
217 for_each_netdev(&init_net, dev) {
218 if (!(dev->flags & IFF_LOOPBACK) && !netdev_uses_dsa(dev))
220 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
221 pr_err("IP-Config: Failed to open %s\n", dev->name);
224 for_each_netdev(&init_net, dev) {
225 if (ic_is_init_dev(dev)) {
230 pr_warn("DHCP/BOOTP: Ignoring device %s, MTU %d too small",
231 dev->name, dev->mtu);
232 if (!(dev->flags & IFF_NOARP))
234 able &= ic_proto_enabled;
235 if (ic_proto_enabled && !able)
238 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
239 pr_err("IP-Config: Failed to open %s\n",
243 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
253 get_random_bytes(&d->xid, sizeof(__be32));
256 ic_proto_have_if |= able;
257 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
258 dev->name, able, d->xid));
262 /* no point in waiting if we could not bring up at least one device */
266 /* wait for a carrier on at least one device */
268 next_msg = start + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
269 while (time_before(jiffies, start +
270 msecs_to_jiffies(CONF_CARRIER_TIMEOUT))) {
273 for_each_netdev(&init_net, dev)
274 if (ic_is_init_dev(dev) && netif_carrier_ok(dev))
279 if (time_before(jiffies, next_msg))
282 elapsed = jiffies_to_msecs(jiffies - start);
283 wait = (CONF_CARRIER_TIMEOUT - elapsed + 500)/1000;
284 pr_info("Waiting up to %d more seconds for network.\n", wait);
285 next_msg = jiffies + msecs_to_jiffies(CONF_CARRIER_TIMEOUT/12);
293 if (user_dev_name[0])
294 pr_err("IP-Config: Device `%s' not found\n",
297 pr_err("IP-Config: No network devices available\n");
303 static void __init ic_close_devs(void)
305 struct ic_device *d, *next;
306 struct net_device *dev;
313 if (dev != ic_dev && !netdev_uses_dsa(dev)) {
314 DBG(("IP-Config: Downing %s\n", dev->name));
315 dev_change_flags(dev, d->flags);
323 * Interface to various network functions.
327 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
329 sin->sin_family = AF_INET;
330 sin->sin_addr.s_addr = addr;
331 sin->sin_port = port;
334 static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
338 mm_segment_t oldfs = get_fs();
340 res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
345 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
349 mm_segment_t oldfs = get_fs();
351 res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
356 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
360 mm_segment_t oldfs = get_fs();
362 res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
368 * Set up interface addresses and routes.
371 static int __init ic_setup_if(void)
374 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
377 memset(&ir, 0, sizeof(ir));
378 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
379 set_sockaddr(sin, ic_myaddr, 0);
380 if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
381 pr_err("IP-Config: Unable to set interface address (%d)\n",
385 set_sockaddr(sin, ic_netmask, 0);
386 if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
387 pr_err("IP-Config: Unable to set interface netmask (%d)\n",
391 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
392 if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
393 pr_err("IP-Config: Unable to set interface broadcast address (%d)\n",
397 /* Handle the case where we need non-standard MTU on the boot link (a network
398 * using jumbo frames, for instance). If we can't set the mtu, don't error
399 * out, we'll try to muddle along.
401 if (ic_dev_mtu != 0) {
402 strcpy(ir.ifr_name, ic_dev->name);
403 ir.ifr_mtu = ic_dev_mtu;
404 if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
405 pr_err("IP-Config: Unable to set interface mtu to %d (%d)\n",
411 static int __init ic_setup_routes(void)
413 /* No need to setup device routes, only the default route... */
415 if (ic_gateway != NONE) {
419 memset(&rm, 0, sizeof(rm));
420 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
421 pr_err("IP-Config: Gateway not on directly connected network\n");
424 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
425 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
426 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
427 rm.rt_flags = RTF_UP | RTF_GATEWAY;
428 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
429 pr_err("IP-Config: Cannot add default route (%d)\n",
439 * Fill in default values for all missing parameters.
442 static int __init ic_defaults(void)
445 * At this point we have no userspace running so need not
446 * claim locks on system_utsname
449 if (!ic_host_name_set)
450 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
452 if (root_server_addr == NONE)
453 root_server_addr = ic_servaddr;
455 if (ic_netmask == NONE) {
456 if (IN_CLASSA(ntohl(ic_myaddr)))
457 ic_netmask = htonl(IN_CLASSA_NET);
458 else if (IN_CLASSB(ntohl(ic_myaddr)))
459 ic_netmask = htonl(IN_CLASSB_NET);
460 else if (IN_CLASSC(ntohl(ic_myaddr)))
461 ic_netmask = htonl(IN_CLASSC_NET);
463 pr_err("IP-Config: Unable to guess netmask for address %pI4\n",
467 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
479 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
481 static struct packet_type rarp_packet_type __initdata = {
482 .type = cpu_to_be16(ETH_P_RARP),
483 .func = ic_rarp_recv,
486 static inline void __init ic_rarp_init(void)
488 dev_add_pack(&rarp_packet_type);
491 static inline void __init ic_rarp_cleanup(void)
493 dev_remove_pack(&rarp_packet_type);
497 * Process received RARP packet.
500 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
503 unsigned char *rarp_ptr;
505 unsigned char *tha; /* t for "target" */
508 if (!net_eq(dev_net(dev), &init_net))
511 skb = skb_share_check(skb, GFP_ATOMIC);
515 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
518 /* Basic sanity checks can be done without the lock. */
519 rarp = (struct arphdr *)skb_transport_header(skb);
521 /* If this test doesn't pass, it's not IP, or we should
524 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
527 /* If it's not a RARP reply, delete it. */
528 if (rarp->ar_op != htons(ARPOP_RREPLY))
531 /* If it's not Ethernet, delete it. */
532 if (rarp->ar_pro != htons(ETH_P_IP))
535 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
538 /* OK, it is all there and looks valid, process... */
539 rarp = (struct arphdr *)skb_transport_header(skb);
540 rarp_ptr = (unsigned char *) (rarp + 1);
542 /* One reply at a time, please. */
543 spin_lock(&ic_recv_lock);
545 /* If we already have a reply, just drop the packet */
549 /* Find the ic_device that the packet arrived on */
551 while (d && d->dev != dev)
554 goto drop_unlock; /* should never happen */
556 /* Extract variable-width fields */
557 rarp_ptr += dev->addr_len;
558 memcpy(&sip, rarp_ptr, 4);
561 rarp_ptr += dev->addr_len;
562 memcpy(&tip, rarp_ptr, 4);
564 /* Discard packets which are not meant for us. */
565 if (memcmp(tha, dev->dev_addr, dev->addr_len))
568 /* Discard packets which are not from specified server. */
569 if (ic_servaddr != NONE && ic_servaddr != sip)
572 /* We have a winner! */
574 if (ic_myaddr == NONE)
577 ic_addrservaddr = sip;
578 ic_got_reply = IC_RARP;
581 /* Show's over. Nothing to see here. */
582 spin_unlock(&ic_recv_lock);
585 /* Throw the packet out. */
592 * Send RARP request packet over a single interface.
594 static void __init ic_rarp_send_if(struct ic_device *d)
596 struct net_device *dev = d->dev;
597 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
598 dev->dev_addr, dev->dev_addr);
603 * Predefine Nameservers
605 static inline void __init ic_nameservers_predef(void)
609 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
610 ic_nameservers[i] = NONE;
614 * DHCP/BOOTP support.
617 #ifdef IPCONFIG_BOOTP
619 struct bootp_pkt { /* BOOTP packet format */
620 struct iphdr iph; /* IP header */
621 struct udphdr udph; /* UDP header */
622 u8 op; /* 1=request, 2=reply */
623 u8 htype; /* HW address type */
624 u8 hlen; /* HW address length */
625 u8 hops; /* Used only by gateways */
626 __be32 xid; /* Transaction ID */
627 __be16 secs; /* Seconds since we started */
628 __be16 flags; /* Just what it says */
629 __be32 client_ip; /* Client's IP address if known */
630 __be32 your_ip; /* Assigned IP address */
631 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
632 __be32 relay_ip; /* IP address of BOOTP relay */
633 u8 hw_addr[16]; /* Client's HW address */
634 u8 serv_name[64]; /* Server host name */
635 u8 boot_file[128]; /* Name of boot file */
636 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
640 #define BOOTP_REQUEST 1
641 #define BOOTP_REPLY 2
643 /* DHCP message types */
644 #define DHCPDISCOVER 1
646 #define DHCPREQUEST 3
647 #define DHCPDECLINE 4
650 #define DHCPRELEASE 7
653 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
655 static struct packet_type bootp_packet_type __initdata = {
656 .type = cpu_to_be16(ETH_P_IP),
657 .func = ic_bootp_recv,
660 static __be32 ic_dev_xid; /* Device under configuration */
663 * Initialize DHCP/BOOTP extension fields in the request.
666 static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
671 ic_dhcp_init_options(u8 *options)
673 u8 mt = ((ic_servaddr == NONE)
674 ? DHCPDISCOVER : DHCPREQUEST);
678 #ifdef IPCONFIG_DEBUG
679 printk("DHCP: Sending message type %d\n", mt);
682 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
685 *e++ = 53; /* DHCP message type */
689 if (mt == DHCPREQUEST) {
690 *e++ = 54; /* Server ID (IP address) */
692 memcpy(e, &ic_servaddr, 4);
695 *e++ = 50; /* Requested IP address */
697 memcpy(e, &ic_myaddr, 4);
703 static const u8 ic_req_params[] = {
705 3, /* Default gateway */
708 15, /* Domain name */
711 40, /* NIS domain name */
714 *e++ = 55; /* Parameter request list */
715 *e++ = sizeof(ic_req_params);
716 memcpy(e, ic_req_params, sizeof(ic_req_params));
717 e += sizeof(ic_req_params);
719 if (ic_host_name_set) {
720 *e++ = 12; /* host-name */
721 len = strlen(utsname()->nodename);
723 memcpy(e, utsname()->nodename, len);
726 if (*vendor_class_identifier) {
727 pr_info("DHCP: sending class identifier \"%s\"\n",
728 vendor_class_identifier);
729 *e++ = 60; /* Class-identifier */
730 len = strlen(vendor_class_identifier);
732 memcpy(e, vendor_class_identifier, len);
735 len = strlen(dhcp_client_identifier + 1);
736 /* the minimum length of identifier is 2, include 1 byte type,
737 * and can not be larger than the length of options
739 if (len >= 1 && len < 312 - (e - options) - 1) {
742 memcpy(e, dhcp_client_identifier, len + 1);
747 *e++ = 255; /* End of the list */
750 #endif /* IPCONFIG_DHCP */
752 static void __init ic_bootp_init_ext(u8 *e)
754 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
756 *e++ = 1; /* Subnet mask request */
759 *e++ = 3; /* Default gateway request */
762 *e++ = 5; /* Name server request */
765 *e++ = 12; /* Host name request */
768 *e++ = 40; /* NIS Domain name request */
771 *e++ = 17; /* Boot path */
775 *e++ = 57; /* set extension buffer size for reply */
777 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
780 *e++ = 255; /* End of the list */
785 * Initialize the DHCP/BOOTP mechanism.
787 static inline void __init ic_bootp_init(void)
789 ic_nameservers_predef();
791 dev_add_pack(&bootp_packet_type);
796 * DHCP/BOOTP cleanup.
798 static inline void __init ic_bootp_cleanup(void)
800 dev_remove_pack(&bootp_packet_type);
805 * Send DHCP/BOOTP request to single interface.
807 static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
809 struct net_device *dev = d->dev;
813 int hlen = LL_RESERVED_SPACE(dev);
814 int tlen = dev->needed_tailroom;
816 /* Allocate packet */
817 skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
821 skb_reserve(skb, hlen);
822 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
823 memset(b, 0, sizeof(struct bootp_pkt));
825 /* Construct IP header */
826 skb_reset_network_header(skb);
830 h->tot_len = htons(sizeof(struct bootp_pkt));
831 h->frag_off = htons(IP_DF);
833 h->protocol = IPPROTO_UDP;
834 h->daddr = htonl(INADDR_BROADCAST);
835 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
837 /* Construct UDP header */
838 b->udph.source = htons(68);
839 b->udph.dest = htons(67);
840 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
841 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
843 /* Construct DHCP/BOOTP header */
844 b->op = BOOTP_REQUEST;
845 if (dev->type < 256) /* check for false types */
846 b->htype = dev->type;
847 else if (dev->type == ARPHRD_FDDI)
848 b->htype = ARPHRD_ETHER;
850 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
851 b->htype = dev->type; /* can cause undefined behavior */
854 /* server_ip and your_ip address are both already zero per RFC2131 */
855 b->hlen = dev->addr_len;
856 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
857 b->secs = htons(jiffies_diff / HZ);
860 /* add DHCP options or BOOTP extensions */
862 if (ic_proto_enabled & IC_USE_DHCP)
863 ic_dhcp_init_options(b->exten);
866 ic_bootp_init_ext(b->exten);
868 /* Chain packet down the line... */
870 skb->protocol = htons(ETH_P_IP);
871 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
872 dev->broadcast, dev->dev_addr, skb->len) < 0) {
878 if (dev_queue_xmit(skb) < 0)
884 * Copy BOOTP-supplied string if not already set.
886 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
892 memcpy(dest, src, len);
899 * Process BOOTP extensions.
901 static void __init ic_do_bootp_ext(u8 *ext)
907 #ifdef IPCONFIG_DEBUG
910 printk("DHCP/BOOTP: Got extension %d:",*ext);
911 for (c=ext+2; c<ext+2+ext[1]; c++)
917 case 1: /* Subnet mask */
918 if (ic_netmask == NONE)
919 memcpy(&ic_netmask, ext+1, 4);
921 case 3: /* Default gateway */
922 if (ic_gateway == NONE)
923 memcpy(&ic_gateway, ext+1, 4);
925 case 6: /* DNS server */
927 if (servers > CONF_NAMESERVERS_MAX)
928 servers = CONF_NAMESERVERS_MAX;
929 for (i = 0; i < servers; i++) {
930 if (ic_nameservers[i] == NONE)
931 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
934 case 12: /* Host name */
935 ic_bootp_string(utsname()->nodename, ext+1, *ext,
937 ic_host_name_set = 1;
939 case 15: /* Domain name (DNS) */
940 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
942 case 17: /* Root path */
943 if (!root_server_path[0])
944 ic_bootp_string(root_server_path, ext+1, *ext,
945 sizeof(root_server_path));
947 case 26: /* Interface MTU */
948 memcpy(&mtu, ext+1, sizeof(mtu));
949 ic_dev_mtu = ntohs(mtu);
951 case 40: /* NIS Domain name (_not_ DNS) */
952 ic_bootp_string(utsname()->domainname, ext+1, *ext,
960 * Receive BOOTP reply.
962 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
969 if (!net_eq(dev_net(dev), &init_net))
972 /* Perform verifications before taking the lock. */
973 if (skb->pkt_type == PACKET_OTHERHOST)
976 skb = skb_share_check(skb, GFP_ATOMIC);
980 if (!pskb_may_pull(skb,
981 sizeof(struct iphdr) +
982 sizeof(struct udphdr)))
985 b = (struct bootp_pkt *)skb_network_header(skb);
988 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
991 /* Fragments are not supported */
992 if (ip_is_fragment(h)) {
993 net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
997 if (skb->len < ntohs(h->tot_len))
1000 if (ip_fast_csum((char *) h, h->ihl))
1003 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
1006 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
1009 len = ntohs(b->udph.len) - sizeof(struct udphdr);
1010 ext_len = len - (sizeof(*b) -
1011 sizeof(struct iphdr) -
1012 sizeof(struct udphdr) -
1017 /* Ok the front looks good, make sure we can get at the rest. */
1018 if (!pskb_may_pull(skb, skb->len))
1021 b = (struct bootp_pkt *)skb_network_header(skb);
1024 /* One reply at a time, please. */
1025 spin_lock(&ic_recv_lock);
1027 /* If we already have a reply, just drop the packet */
1031 /* Find the ic_device that the packet arrived on */
1033 while (d && d->dev != dev)
1036 goto drop_unlock; /* should never happen */
1038 /* Is it a reply to our BOOTP request? */
1039 if (b->op != BOOTP_REPLY ||
1041 net_err_ratelimited("DHCP/BOOTP: Reply not for us, op[%x] xid[%x]\n",
1046 /* Is it a reply for the device we are configuring? */
1047 if (b->xid != ic_dev_xid) {
1048 net_err_ratelimited("DHCP/BOOTP: Ignoring delayed packet\n");
1052 /* Parse extensions */
1054 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
1055 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
1058 #ifdef IPCONFIG_DHCP
1059 if (ic_proto_enabled & IC_USE_DHCP) {
1060 __be32 server_id = NONE;
1064 while (ext < end && *ext != 0xff) {
1066 if (*opt == 0) /* Padding */
1072 case 53: /* Message type */
1076 case 54: /* Server ID (IP address) */
1078 memcpy(&server_id, opt + 2, 4);
1083 #ifdef IPCONFIG_DEBUG
1084 printk("DHCP: Got message type %d\n", mt);
1089 /* While in the process of accepting one offer,
1090 * ignore all others.
1092 if (ic_myaddr != NONE)
1095 /* Let's accept that offer. */
1096 ic_myaddr = b->your_ip;
1097 ic_servaddr = server_id;
1098 #ifdef IPCONFIG_DEBUG
1099 printk("DHCP: Offered address %pI4 by server %pI4\n",
1100 &ic_myaddr, &b->iph.saddr);
1102 /* The DHCP indicated server address takes
1103 * precedence over the bootp header one if
1104 * they are different.
1106 if ((server_id != NONE) &&
1107 (b->server_ip != server_id))
1108 b->server_ip = ic_servaddr;
1112 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1119 /* Urque. Forget it*/
1125 ic_dhcp_msgtype = mt;
1128 #endif /* IPCONFIG_DHCP */
1131 while (ext < end && *ext != 0xff) {
1133 if (*opt == 0) /* Padding */
1137 ic_do_bootp_ext(opt);
1141 /* We have a winner! */
1143 ic_myaddr = b->your_ip;
1144 ic_servaddr = b->server_ip;
1145 ic_addrservaddr = b->iph.saddr;
1146 if (ic_gateway == NONE && b->relay_ip)
1147 ic_gateway = b->relay_ip;
1148 if (ic_nameservers[0] == NONE)
1149 ic_nameservers[0] = ic_servaddr;
1150 ic_got_reply = IC_BOOTP;
1153 /* Show's over. Nothing to see here. */
1154 spin_unlock(&ic_recv_lock);
1157 /* Throw the packet out. */
1168 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1171 #ifdef IPCONFIG_DYNAMIC
1173 static int __init ic_dynamic(void)
1176 struct ic_device *d;
1177 unsigned long start_jiffies, timeout, jiff;
1178 int do_bootp = ic_proto_have_if & IC_BOOTP;
1179 int do_rarp = ic_proto_have_if & IC_RARP;
1182 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1183 * This routine gets only called when some pieces of information
1184 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1186 if (!ic_proto_enabled) {
1187 pr_err("IP-Config: Incomplete network configuration information\n");
1191 #ifdef IPCONFIG_BOOTP
1192 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1193 pr_err("DHCP/BOOTP: No suitable device found\n");
1195 #ifdef IPCONFIG_RARP
1196 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1197 pr_err("RARP: No suitable device found\n");
1200 if (!ic_proto_have_if)
1201 /* Error message already printed */
1207 #ifdef IPCONFIG_BOOTP
1211 #ifdef IPCONFIG_RARP
1217 * Send requests and wait, until we get an answer. This loop
1218 * seems to be a terrible waste of CPU time, but actually there is
1219 * only one process running at all, so we don't need to use any
1220 * scheduler functions.
1221 * [Actually we could now, but the nothing else running note still
1224 pr_notice("Sending %s%s%s requests .",
1226 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1227 (do_bootp && do_rarp) ? " and " : "",
1228 do_rarp ? "RARP" : "");
1230 start_jiffies = jiffies;
1232 retries = CONF_SEND_RETRIES;
1233 get_random_bytes(&timeout, sizeof(timeout));
1234 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
1236 #ifdef IPCONFIG_BOOTP
1237 /* Track the device we are configuring */
1238 ic_dev_xid = d->xid;
1240 if (do_bootp && (d->able & IC_BOOTP))
1241 ic_bootp_send_if(d, jiffies - start_jiffies);
1243 #ifdef IPCONFIG_RARP
1244 if (do_rarp && (d->able & IC_RARP))
1248 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
1249 while (time_before(jiffies, jiff) && !ic_got_reply)
1250 schedule_timeout_uninterruptible(1);
1251 #ifdef IPCONFIG_DHCP
1252 /* DHCP isn't done until we get a DHCPACK. */
1253 if ((ic_got_reply & IC_BOOTP) &&
1254 (ic_proto_enabled & IC_USE_DHCP) &&
1255 ic_dhcp_msgtype != DHCPACK) {
1260 #endif /* IPCONFIG_DHCP */
1271 pr_cont(" timed out!\n");
1277 timeout = timeout CONF_TIMEOUT_MULT;
1278 if (timeout > CONF_TIMEOUT_MAX)
1279 timeout = CONF_TIMEOUT_MAX;
1284 #ifdef IPCONFIG_BOOTP
1288 #ifdef IPCONFIG_RARP
1293 if (!ic_got_reply) {
1298 printk("IP-Config: Got %s answer from %pI4, ",
1299 ((ic_got_reply & IC_RARP) ? "RARP"
1300 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1302 pr_cont("my address is %pI4\n", &ic_myaddr);
1307 #endif /* IPCONFIG_DYNAMIC */
1309 #ifdef CONFIG_PROC_FS
1311 static int pnp_seq_show(struct seq_file *seq, void *v)
1315 if (ic_proto_used & IC_PROTO)
1316 seq_printf(seq, "#PROTO: %s\n",
1317 (ic_proto_used & IC_RARP) ? "RARP"
1318 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1320 seq_puts(seq, "#MANUAL\n");
1324 "domain %s\n", ic_domain);
1325 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1326 if (ic_nameservers[i] != NONE)
1327 seq_printf(seq, "nameserver %pI4\n",
1328 &ic_nameservers[i]);
1330 if (ic_servaddr != NONE)
1331 seq_printf(seq, "bootserver %pI4\n",
1336 static int pnp_seq_open(struct inode *indoe, struct file *file)
1338 return single_open(file, pnp_seq_show, NULL);
1341 static const struct file_operations pnp_seq_fops = {
1342 .owner = THIS_MODULE,
1343 .open = pnp_seq_open,
1345 .llseek = seq_lseek,
1346 .release = single_release,
1348 #endif /* CONFIG_PROC_FS */
1351 * Extract IP address from the parameter string if needed. Note that we
1352 * need to have root_server_addr set _before_ IPConfig gets called as it
1355 __be32 __init root_nfs_parse_addr(char *name)
1362 while (octets < 4) {
1363 while (*cp >= '0' && *cp <= '9')
1365 if (cp == cq || cp - cq > 3)
1367 if (*cp == '.' || octets == 3)
1373 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1376 addr = in_aton(name);
1377 memmove(name, cp, strlen(cp) + 1);
1384 #define DEVICE_WAIT_MAX 12 /* 12 seconds */
1386 static int __init wait_for_devices(void)
1390 for (i = 0; i < DEVICE_WAIT_MAX; i++) {
1391 struct net_device *dev;
1395 for_each_netdev(&init_net, dev) {
1396 if (ic_is_init_dev(dev)) {
1410 * IP Autoconfig dispatcher.
1413 static int __init ip_auto_config(void)
1416 #ifdef IPCONFIG_DYNAMIC
1417 int retries = CONF_OPEN_RETRIES;
1422 #ifdef CONFIG_PROC_FS
1423 proc_create("pnp", S_IRUGO, init_net.proc_net, &pnp_seq_fops);
1424 #endif /* CONFIG_PROC_FS */
1429 DBG(("IP-Config: Entered.\n"));
1430 #ifdef IPCONFIG_DYNAMIC
1433 /* Wait for devices to appear */
1434 err = wait_for_devices();
1438 /* Setup all network devices */
1439 err = ic_open_devs();
1443 /* Give drivers a chance to settle */
1444 msleep(CONF_POST_OPEN);
1447 * If the config information is insufficient (e.g., our IP address or
1448 * IP address of the boot server is missing or we have multiple network
1449 * interfaces and no default was set), use BOOTP or RARP to get the
1452 if (ic_myaddr == NONE ||
1453 #ifdef CONFIG_ROOT_NFS
1454 (root_server_addr == NONE &&
1455 ic_servaddr == NONE &&
1456 ROOT_DEV == Root_NFS) ||
1458 ic_first_dev->next) {
1459 #ifdef IPCONFIG_DYNAMIC
1460 if (ic_dynamic() < 0) {
1464 * I don't know why, but sometimes the
1465 * eepro100 driver (at least) gets upset and
1466 * doesn't work the first time it's opened.
1467 * But then if you close it and reopen it, it
1468 * works just fine. So we need to try that at
1469 * least once before giving up.
1471 * Also, if the root will be NFS-mounted, we
1472 * have nowhere to go if DHCP fails. So we
1473 * just have to keep trying forever.
1477 #ifdef CONFIG_ROOT_NFS
1478 if (ROOT_DEV == Root_NFS) {
1479 pr_err("IP-Config: Retrying forever (NFS root)...\n");
1485 pr_err("IP-Config: Reopening network devices...\n");
1489 /* Oh, well. At least we tried. */
1490 pr_err("IP-Config: Auto-configuration of network failed\n");
1493 #else /* !DYNAMIC */
1494 pr_err("IP-Config: Incomplete network configuration information\n");
1497 #endif /* IPCONFIG_DYNAMIC */
1499 /* Device selected manually or only one device -> use it */
1500 ic_dev = ic_first_dev->dev;
1503 addr = root_nfs_parse_addr(root_server_path);
1504 if (root_server_addr == NONE)
1505 root_server_addr = addr;
1508 * Use defaults wherever applicable.
1510 if (ic_defaults() < 0)
1514 * Close all network devices except the device we've
1515 * autoconfigured and set up routes.
1518 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1522 * Record which protocol was actually used.
1524 #ifdef IPCONFIG_DYNAMIC
1525 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1528 #ifndef IPCONFIG_SILENT
1530 * Clue in the operator.
1532 pr_info("IP-Config: Complete:\n");
1534 pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
1535 ic_dev->name, ic_dev->addr_len, ic_dev->dev_addr,
1536 &ic_myaddr, &ic_netmask, &ic_gateway);
1537 pr_info(" host=%s, domain=%s, nis-domain=%s\n",
1538 utsname()->nodename, ic_domain, utsname()->domainname);
1539 pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
1540 &ic_servaddr, &root_server_addr, root_server_path);
1542 pr_cont(", mtu=%d", ic_dev_mtu);
1543 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
1544 if (ic_nameservers[i] != NONE) {
1545 pr_info(" nameserver%u=%pI4",
1546 i, &ic_nameservers[i]);
1549 for (i++; i < CONF_NAMESERVERS_MAX; i++)
1550 if (ic_nameservers[i] != NONE)
1551 pr_cont(", nameserver%u=%pI4", i, &ic_nameservers[i]);
1553 #endif /* !SILENT */
1558 late_initcall(ip_auto_config);
1562 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1563 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
1565 static int __init ic_proto_name(char *name)
1567 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1570 if (!strcmp(name, "off") || !strcmp(name, "none")) {
1573 #ifdef CONFIG_IP_PNP_DHCP
1574 else if (!strncmp(name, "dhcp", 4)) {
1577 ic_proto_enabled &= ~IC_RARP;
1578 client_id = strstr(name, "dhcp,");
1582 client_id = client_id + 5;
1583 v = strchr(client_id, ',');
1587 if (kstrtou8(client_id, 0, dhcp_client_identifier))
1588 DBG("DHCP: Invalid client identifier type\n");
1589 strncpy(dhcp_client_identifier + 1, v + 1, 251);
1595 #ifdef CONFIG_IP_PNP_BOOTP
1596 else if (!strcmp(name, "bootp")) {
1597 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1601 #ifdef CONFIG_IP_PNP_RARP
1602 else if (!strcmp(name, "rarp")) {
1603 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1607 #ifdef IPCONFIG_DYNAMIC
1608 else if (!strcmp(name, "both")) {
1609 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1616 static int __init ip_auto_config_setup(char *addrs)
1621 ic_set_manually = 1;
1625 * If any dhcp, bootp etc options are set, leave autoconfig on
1626 * and skip the below static IP processing.
1628 if (ic_proto_name(addrs))
1631 /* If no static IP is given, turn off autoconfig and bail. */
1633 strcmp(addrs, "off") == 0 ||
1634 strcmp(addrs, "none") == 0) {
1639 ic_nameservers_predef();
1641 /* Parse string for static IP assignment. */
1644 if ((cp = strchr(ip, ':')))
1646 if (strlen(ip) > 0) {
1647 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1650 if ((ic_myaddr = in_aton(ip)) == ANY)
1654 if ((ic_servaddr = in_aton(ip)) == ANY)
1658 if ((ic_gateway = in_aton(ip)) == ANY)
1662 if ((ic_netmask = in_aton(ip)) == ANY)
1666 if ((dp = strchr(ip, '.'))) {
1668 strlcpy(utsname()->domainname, dp,
1669 sizeof(utsname()->domainname));
1671 strlcpy(utsname()->nodename, ip,
1672 sizeof(utsname()->nodename));
1673 ic_host_name_set = 1;
1676 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1679 if (ic_proto_name(ip) == 0 &&
1680 ic_myaddr == NONE) {
1685 if (CONF_NAMESERVERS_MAX >= 1) {
1686 ic_nameservers[0] = in_aton(ip);
1687 if (ic_nameservers[0] == ANY)
1688 ic_nameservers[0] = NONE;
1692 if (CONF_NAMESERVERS_MAX >= 2) {
1693 ic_nameservers[1] = in_aton(ip);
1694 if (ic_nameservers[1] == ANY)
1695 ic_nameservers[1] = NONE;
1706 __setup("ip=", ip_auto_config_setup);
1708 static int __init nfsaddrs_config_setup(char *addrs)
1710 return ip_auto_config_setup(addrs);
1712 __setup("nfsaddrs=", nfsaddrs_config_setup);
1714 static int __init vendor_class_identifier_setup(char *addrs)
1716 if (strlcpy(vendor_class_identifier, addrs,
1717 sizeof(vendor_class_identifier))
1718 >= sizeof(vendor_class_identifier))
1719 pr_warn("DHCP: vendorclass too long, truncated to \"%s\"",
1720 vendor_class_identifier);
1723 __setup("dhcpclass=", vendor_class_identifier_setup);