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 <net/net_namespace.h>
59 #include <net/ipconfig.h>
60 #include <net/route.h>
62 #include <asm/uaccess.h>
63 #include <net/checksum.h>
64 #include <asm/processor.h>
66 /* Define this to allow debugging output */
70 #define DBG(x) printk x
72 #define DBG(x) do { } while(0)
75 #if defined(CONFIG_IP_PNP_DHCP)
78 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
79 #define IPCONFIG_BOOTP
81 #if defined(CONFIG_IP_PNP_RARP)
84 #if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
85 #define IPCONFIG_DYNAMIC
88 /* Define the friendly delay before and after opening net devices */
89 #define CONF_PRE_OPEN 500 /* Before opening: 1/2 second */
90 #define CONF_POST_OPEN 1 /* After opening: 1 second */
92 /* Define the timeout for waiting for a DHCP/BOOTP/RARP reply */
93 #define CONF_OPEN_RETRIES 2 /* (Re)open devices twice */
94 #define CONF_SEND_RETRIES 6 /* Send six requests per open */
95 #define CONF_INTER_TIMEOUT (HZ/2) /* Inter-device timeout: 1/2 second */
96 #define CONF_BASE_TIMEOUT (HZ*2) /* Initial timeout: 2 seconds */
97 #define CONF_TIMEOUT_RANDOM (HZ) /* Maximum amount of randomization */
98 #define CONF_TIMEOUT_MULT *7/4 /* Rate of timeout growth */
99 #define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
100 #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
101 - '3' from resolv.h */
103 #define NONE cpu_to_be32(INADDR_NONE)
104 #define ANY cpu_to_be32(INADDR_ANY)
107 * Public IP configuration
110 /* This is used by platforms which might be able to set the ipconfig
111 * variables using firmware environment vars. If this is set, it will
112 * ignore such firmware variables.
114 int ic_set_manually __initdata
= 0; /* IPconfig parameters set manually */
116 static int ic_enable __initdata
= 0; /* IP config enabled? */
118 /* Protocol choice */
119 int ic_proto_enabled __initdata
= 0
120 #ifdef IPCONFIG_BOOTP
123 #ifdef CONFIG_IP_PNP_DHCP
131 static int ic_host_name_set __initdata
= 0; /* Host name set by us? */
133 __be32 ic_myaddr
= NONE
; /* My IP address */
134 static __be32 ic_netmask
= NONE
; /* Netmask for local subnet */
135 __be32 ic_gateway
= NONE
; /* Gateway IP address */
137 __be32 ic_servaddr
= NONE
; /* Boot server IP address */
139 __be32 root_server_addr
= NONE
; /* Address of NFS server */
140 u8 root_server_path
[256] = { 0, }; /* Path to mount as root */
142 u32 ic_dev_xid
; /* Device under configuration */
144 /* vendor class identifier */
145 static char vendor_class_identifier
[253] __initdata
;
147 /* Persistent data: */
149 static int ic_proto_used
; /* Protocol used, if any */
150 static __be32 ic_nameservers
[CONF_NAMESERVERS_MAX
]; /* DNS Server IP addresses */
151 static u8 ic_domain
[64]; /* DNS (not NIS) domain name */
157 /* Name of user-selected boot device */
158 static char user_dev_name
[IFNAMSIZ
] __initdata
= { 0, };
160 /* Protocols supported by available interfaces */
161 static int ic_proto_have_if __initdata
= 0;
163 /* MTU for boot device */
164 static int ic_dev_mtu __initdata
= 0;
166 #ifdef IPCONFIG_DYNAMIC
167 static DEFINE_SPINLOCK(ic_recv_lock
);
168 static volatile int ic_got_reply __initdata
= 0; /* Proto(s) that replied */
171 static int ic_dhcp_msgtype __initdata
= 0; /* DHCP msg type received */
180 struct ic_device
*next
;
181 struct net_device
*dev
;
182 unsigned short flags
;
187 static struct ic_device
*ic_first_dev __initdata
= NULL
;/* List of open device */
188 static struct net_device
*ic_dev __initdata
= NULL
; /* Selected device */
190 static bool __init
ic_device_match(struct net_device
*dev
)
192 if (user_dev_name
[0] ? !strcmp(dev
->name
, user_dev_name
) :
193 (!(dev
->flags
& IFF_LOOPBACK
) &&
194 (dev
->flags
& (IFF_POINTOPOINT
|IFF_BROADCAST
)) &&
195 strncmp(dev
->name
, "dummy", 5)))
200 static int __init
ic_open_devs(void)
202 struct ic_device
*d
, **last
;
203 struct net_device
*dev
;
204 unsigned short oflags
;
206 last
= &ic_first_dev
;
209 /* bring loopback device up first */
210 for_each_netdev(&init_net
, dev
) {
211 if (!(dev
->flags
& IFF_LOOPBACK
))
213 if (dev_change_flags(dev
, dev
->flags
| IFF_UP
) < 0)
214 printk(KERN_ERR
"IP-Config: Failed to open %s\n", dev
->name
);
217 for_each_netdev(&init_net
, dev
) {
218 if (dev
->flags
& IFF_LOOPBACK
)
220 if (ic_device_match(dev
)) {
225 printk(KERN_WARNING
"DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev
->name
, dev
->mtu
);
226 if (!(dev
->flags
& IFF_NOARP
))
228 able
&= ic_proto_enabled
;
229 if (ic_proto_enabled
&& !able
)
232 if (dev_change_flags(dev
, oflags
| IFF_UP
) < 0) {
233 printk(KERN_ERR
"IP-Config: Failed to open %s\n", dev
->name
);
236 if (!(d
= kmalloc(sizeof(struct ic_device
), GFP_KERNEL
))) {
246 get_random_bytes(&d
->xid
, sizeof(__be32
));
249 ic_proto_have_if
|= able
;
250 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
251 dev
->name
, able
, d
->xid
));
259 if (user_dev_name
[0])
260 printk(KERN_ERR
"IP-Config: Device `%s' not found.\n", user_dev_name
);
262 printk(KERN_ERR
"IP-Config: No network devices available.\n");
268 static void __init
ic_close_devs(void)
270 struct ic_device
*d
, *next
;
271 struct net_device
*dev
;
279 DBG(("IP-Config: Downing %s\n", dev
->name
));
280 dev_change_flags(dev
, d
->flags
);
288 * Interface to various network functions.
292 set_sockaddr(struct sockaddr_in
*sin
, __be32 addr
, __be16 port
)
294 sin
->sin_family
= AF_INET
;
295 sin
->sin_addr
.s_addr
= addr
;
296 sin
->sin_port
= port
;
299 static int __init
ic_devinet_ioctl(unsigned int cmd
, struct ifreq
*arg
)
303 mm_segment_t oldfs
= get_fs();
305 res
= devinet_ioctl(&init_net
, cmd
, (struct ifreq __user
*) arg
);
310 static int __init
ic_dev_ioctl(unsigned int cmd
, struct ifreq
*arg
)
314 mm_segment_t oldfs
= get_fs();
316 res
= dev_ioctl(&init_net
, cmd
, (struct ifreq __user
*) arg
);
321 static int __init
ic_route_ioctl(unsigned int cmd
, struct rtentry
*arg
)
325 mm_segment_t oldfs
= get_fs();
327 res
= ip_rt_ioctl(&init_net
, cmd
, (void __user
*) arg
);
333 * Set up interface addresses and routes.
336 static int __init
ic_setup_if(void)
339 struct sockaddr_in
*sin
= (void *) &ir
.ifr_ifru
.ifru_addr
;
342 memset(&ir
, 0, sizeof(ir
));
343 strcpy(ir
.ifr_ifrn
.ifrn_name
, ic_dev
->name
);
344 set_sockaddr(sin
, ic_myaddr
, 0);
345 if ((err
= ic_devinet_ioctl(SIOCSIFADDR
, &ir
)) < 0) {
346 printk(KERN_ERR
"IP-Config: Unable to set interface address (%d).\n", err
);
349 set_sockaddr(sin
, ic_netmask
, 0);
350 if ((err
= ic_devinet_ioctl(SIOCSIFNETMASK
, &ir
)) < 0) {
351 printk(KERN_ERR
"IP-Config: Unable to set interface netmask (%d).\n", err
);
354 set_sockaddr(sin
, ic_myaddr
| ~ic_netmask
, 0);
355 if ((err
= ic_devinet_ioctl(SIOCSIFBRDADDR
, &ir
)) < 0) {
356 printk(KERN_ERR
"IP-Config: Unable to set interface broadcast address (%d).\n", err
);
359 /* Handle the case where we need non-standard MTU on the boot link (a network
360 * using jumbo frames, for instance). If we can't set the mtu, don't error
361 * out, we'll try to muddle along.
363 if (ic_dev_mtu
!= 0) {
364 strcpy(ir
.ifr_name
, ic_dev
->name
);
365 ir
.ifr_mtu
= ic_dev_mtu
;
366 if ((err
= ic_dev_ioctl(SIOCSIFMTU
, &ir
)) < 0)
367 printk(KERN_ERR
"IP-Config: Unable to set interface mtu to %d (%d).\n",
373 static int __init
ic_setup_routes(void)
375 /* No need to setup device routes, only the default route... */
377 if (ic_gateway
!= NONE
) {
381 memset(&rm
, 0, sizeof(rm
));
382 if ((ic_gateway
^ ic_myaddr
) & ic_netmask
) {
383 printk(KERN_ERR
"IP-Config: Gateway not on directly connected network.\n");
386 set_sockaddr((struct sockaddr_in
*) &rm
.rt_dst
, 0, 0);
387 set_sockaddr((struct sockaddr_in
*) &rm
.rt_genmask
, 0, 0);
388 set_sockaddr((struct sockaddr_in
*) &rm
.rt_gateway
, ic_gateway
, 0);
389 rm
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
390 if ((err
= ic_route_ioctl(SIOCADDRT
, &rm
)) < 0) {
391 printk(KERN_ERR
"IP-Config: Cannot add default route (%d).\n", err
);
400 * Fill in default values for all missing parameters.
403 static int __init
ic_defaults(void)
406 * At this point we have no userspace running so need not
407 * claim locks on system_utsname
410 if (!ic_host_name_set
)
411 sprintf(init_utsname()->nodename
, "%pI4", &ic_myaddr
);
413 if (root_server_addr
== NONE
)
414 root_server_addr
= ic_servaddr
;
416 if (ic_netmask
== NONE
) {
417 if (IN_CLASSA(ntohl(ic_myaddr
)))
418 ic_netmask
= htonl(IN_CLASSA_NET
);
419 else if (IN_CLASSB(ntohl(ic_myaddr
)))
420 ic_netmask
= htonl(IN_CLASSB_NET
);
421 else if (IN_CLASSC(ntohl(ic_myaddr
)))
422 ic_netmask
= htonl(IN_CLASSC_NET
);
424 printk(KERN_ERR
"IP-Config: Unable to guess netmask for address %pI4\n",
428 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask
);
440 static int ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
442 static struct packet_type rarp_packet_type __initdata
= {
443 .type
= cpu_to_be16(ETH_P_RARP
),
444 .func
= ic_rarp_recv
,
447 static inline void __init
ic_rarp_init(void)
449 dev_add_pack(&rarp_packet_type
);
452 static inline void __init
ic_rarp_cleanup(void)
454 dev_remove_pack(&rarp_packet_type
);
458 * Process received RARP packet.
461 ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
464 unsigned char *rarp_ptr
;
466 unsigned char *sha
, *tha
; /* s for "source", t for "target" */
469 if (!net_eq(dev_net(dev
), &init_net
))
472 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
475 if (!pskb_may_pull(skb
, sizeof(struct arphdr
)))
478 /* Basic sanity checks can be done without the lock. */
479 rarp
= (struct arphdr
*)skb_transport_header(skb
);
481 /* If this test doesn't pass, it's not IP, or we should
484 if (rarp
->ar_hln
!= dev
->addr_len
|| dev
->type
!= ntohs(rarp
->ar_hrd
))
487 /* If it's not a RARP reply, delete it. */
488 if (rarp
->ar_op
!= htons(ARPOP_RREPLY
))
491 /* If it's not Ethernet, delete it. */
492 if (rarp
->ar_pro
!= htons(ETH_P_IP
))
495 if (!pskb_may_pull(skb
, arp_hdr_len(dev
)))
498 /* OK, it is all there and looks valid, process... */
499 rarp
= (struct arphdr
*)skb_transport_header(skb
);
500 rarp_ptr
= (unsigned char *) (rarp
+ 1);
502 /* One reply at a time, please. */
503 spin_lock(&ic_recv_lock
);
505 /* If we already have a reply, just drop the packet */
509 /* Find the ic_device that the packet arrived on */
511 while (d
&& d
->dev
!= dev
)
514 goto drop_unlock
; /* should never happen */
516 /* Extract variable-width fields */
518 rarp_ptr
+= dev
->addr_len
;
519 memcpy(&sip
, rarp_ptr
, 4);
522 rarp_ptr
+= dev
->addr_len
;
523 memcpy(&tip
, rarp_ptr
, 4);
525 /* Discard packets which are not meant for us. */
526 if (memcmp(tha
, dev
->dev_addr
, dev
->addr_len
))
529 /* Discard packets which are not from specified server. */
530 if (ic_servaddr
!= NONE
&& ic_servaddr
!= sip
)
533 /* We have a winner! */
535 if (ic_myaddr
== NONE
)
538 ic_got_reply
= IC_RARP
;
541 /* Show's over. Nothing to see here. */
542 spin_unlock(&ic_recv_lock
);
545 /* Throw the packet out. */
552 * Send RARP request packet over a single interface.
554 static void __init
ic_rarp_send_if(struct ic_device
*d
)
556 struct net_device
*dev
= d
->dev
;
557 arp_send(ARPOP_RREQUEST
, ETH_P_RARP
, 0, dev
, 0, NULL
,
558 dev
->dev_addr
, dev
->dev_addr
);
563 * DHCP/BOOTP support.
566 #ifdef IPCONFIG_BOOTP
568 struct bootp_pkt
{ /* BOOTP packet format */
569 struct iphdr iph
; /* IP header */
570 struct udphdr udph
; /* UDP header */
571 u8 op
; /* 1=request, 2=reply */
572 u8 htype
; /* HW address type */
573 u8 hlen
; /* HW address length */
574 u8 hops
; /* Used only by gateways */
575 __be32 xid
; /* Transaction ID */
576 __be16 secs
; /* Seconds since we started */
577 __be16 flags
; /* Just what it says */
578 __be32 client_ip
; /* Client's IP address if known */
579 __be32 your_ip
; /* Assigned IP address */
580 __be32 server_ip
; /* (Next, e.g. NFS) Server's IP address */
581 __be32 relay_ip
; /* IP address of BOOTP relay */
582 u8 hw_addr
[16]; /* Client's HW address */
583 u8 serv_name
[64]; /* Server host name */
584 u8 boot_file
[128]; /* Name of boot file */
585 u8 exten
[312]; /* DHCP options / BOOTP vendor extensions */
589 #define BOOTP_REQUEST 1
590 #define BOOTP_REPLY 2
592 /* DHCP message types */
593 #define DHCPDISCOVER 1
595 #define DHCPREQUEST 3
596 #define DHCPDECLINE 4
599 #define DHCPRELEASE 7
602 static int ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
604 static struct packet_type bootp_packet_type __initdata
= {
605 .type
= cpu_to_be16(ETH_P_IP
),
606 .func
= ic_bootp_recv
,
611 * Initialize DHCP/BOOTP extension fields in the request.
614 static const u8 ic_bootp_cookie
[4] = { 99, 130, 83, 99 };
619 ic_dhcp_init_options(u8
*options
)
621 u8 mt
= ((ic_servaddr
== NONE
)
622 ? DHCPDISCOVER
: DHCPREQUEST
);
626 #ifdef IPCONFIG_DEBUG
627 printk("DHCP: Sending message type %d\n", mt
);
630 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
633 *e
++ = 53; /* DHCP message type */
637 if (mt
== DHCPREQUEST
) {
638 *e
++ = 54; /* Server ID (IP address) */
640 memcpy(e
, &ic_servaddr
, 4);
643 *e
++ = 50; /* Requested IP address */
645 memcpy(e
, &ic_myaddr
, 4);
651 static const u8 ic_req_params
[] = {
653 3, /* Default gateway */
656 15, /* Domain name */
659 40, /* NIS domain name */
662 *e
++ = 55; /* Parameter request list */
663 *e
++ = sizeof(ic_req_params
);
664 memcpy(e
, ic_req_params
, sizeof(ic_req_params
));
665 e
+= sizeof(ic_req_params
);
667 if (*vendor_class_identifier
) {
668 printk(KERN_INFO
"DHCP: sending class identifier \"%s\"\n",
669 vendor_class_identifier
);
670 *e
++ = 60; /* Class-identifier */
671 len
= strlen(vendor_class_identifier
);
673 memcpy(e
, vendor_class_identifier
, len
);
678 *e
++ = 255; /* End of the list */
681 #endif /* IPCONFIG_DHCP */
683 static void __init
ic_bootp_init_ext(u8
*e
)
685 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
687 *e
++ = 1; /* Subnet mask request */
690 *e
++ = 3; /* Default gateway request */
693 *e
++ = 5; /* Name server request */
696 *e
++ = 12; /* Host name request */
699 *e
++ = 40; /* NIS Domain name request */
702 *e
++ = 17; /* Boot path */
706 *e
++ = 57; /* set extension buffer size for reply */
708 *e
++ = 1; /* 128+236+8+20+14, see dhcpd sources */
711 *e
++ = 255; /* End of the list */
716 * Initialize the DHCP/BOOTP mechanism.
718 static inline void __init
ic_bootp_init(void)
722 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++)
723 ic_nameservers
[i
] = NONE
;
725 dev_add_pack(&bootp_packet_type
);
730 * DHCP/BOOTP cleanup.
732 static inline void __init
ic_bootp_cleanup(void)
734 dev_remove_pack(&bootp_packet_type
);
739 * Send DHCP/BOOTP request to single interface.
741 static void __init
ic_bootp_send_if(struct ic_device
*d
, unsigned long jiffies_diff
)
743 struct net_device
*dev
= d
->dev
;
748 /* Allocate packet */
749 skb
= alloc_skb(sizeof(struct bootp_pkt
) + LL_ALLOCATED_SPACE(dev
) + 15,
753 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
754 b
= (struct bootp_pkt
*) skb_put(skb
, sizeof(struct bootp_pkt
));
755 memset(b
, 0, sizeof(struct bootp_pkt
));
757 /* Construct IP header */
758 skb_reset_network_header(skb
);
762 h
->tot_len
= htons(sizeof(struct bootp_pkt
));
763 h
->frag_off
= htons(IP_DF
);
765 h
->protocol
= IPPROTO_UDP
;
766 h
->daddr
= htonl(INADDR_BROADCAST
);
767 h
->check
= ip_fast_csum((unsigned char *) h
, h
->ihl
);
769 /* Construct UDP header */
770 b
->udph
.source
= htons(68);
771 b
->udph
.dest
= htons(67);
772 b
->udph
.len
= htons(sizeof(struct bootp_pkt
) - sizeof(struct iphdr
));
773 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
775 /* Construct DHCP/BOOTP header */
776 b
->op
= BOOTP_REQUEST
;
777 if (dev
->type
< 256) /* check for false types */
778 b
->htype
= dev
->type
;
779 else if (dev
->type
== ARPHRD_IEEE802_TR
) /* fix for token ring */
780 b
->htype
= ARPHRD_IEEE802
;
781 else if (dev
->type
== ARPHRD_FDDI
)
782 b
->htype
= ARPHRD_ETHER
;
784 printk("Unknown ARP type 0x%04x for device %s\n", dev
->type
, dev
->name
);
785 b
->htype
= dev
->type
; /* can cause undefined behavior */
788 /* server_ip and your_ip address are both already zero per RFC2131 */
789 b
->hlen
= dev
->addr_len
;
790 memcpy(b
->hw_addr
, dev
->dev_addr
, dev
->addr_len
);
791 b
->secs
= htons(jiffies_diff
/ HZ
);
794 /* add DHCP options or BOOTP extensions */
796 if (ic_proto_enabled
& IC_USE_DHCP
)
797 ic_dhcp_init_options(b
->exten
);
800 ic_bootp_init_ext(b
->exten
);
802 /* Chain packet down the line... */
804 skb
->protocol
= htons(ETH_P_IP
);
805 if (dev_hard_header(skb
, dev
, ntohs(skb
->protocol
),
806 dev
->broadcast
, dev
->dev_addr
, skb
->len
) < 0 ||
807 dev_queue_xmit(skb
) < 0)
813 * Copy BOOTP-supplied string if not already set.
815 static int __init
ic_bootp_string(char *dest
, char *src
, int len
, int max
)
821 memcpy(dest
, src
, len
);
828 * Process BOOTP extensions.
830 static void __init
ic_do_bootp_ext(u8
*ext
)
836 #ifdef IPCONFIG_DEBUG
839 printk("DHCP/BOOTP: Got extension %d:",*ext
);
840 for (c
=ext
+2; c
<ext
+2+ext
[1]; c
++)
846 case 1: /* Subnet mask */
847 if (ic_netmask
== NONE
)
848 memcpy(&ic_netmask
, ext
+1, 4);
850 case 3: /* Default gateway */
851 if (ic_gateway
== NONE
)
852 memcpy(&ic_gateway
, ext
+1, 4);
854 case 6: /* DNS server */
856 if (servers
> CONF_NAMESERVERS_MAX
)
857 servers
= CONF_NAMESERVERS_MAX
;
858 for (i
= 0; i
< servers
; i
++) {
859 if (ic_nameservers
[i
] == NONE
)
860 memcpy(&ic_nameservers
[i
], ext
+1+4*i
, 4);
863 case 12: /* Host name */
864 ic_bootp_string(utsname()->nodename
, ext
+1, *ext
, __NEW_UTS_LEN
);
865 ic_host_name_set
= 1;
867 case 15: /* Domain name (DNS) */
868 ic_bootp_string(ic_domain
, ext
+1, *ext
, sizeof(ic_domain
));
870 case 17: /* Root path */
871 if (!root_server_path
[0])
872 ic_bootp_string(root_server_path
, ext
+1, *ext
, sizeof(root_server_path
));
874 case 26: /* Interface MTU */
875 memcpy(&mtu
, ext
+1, sizeof(mtu
));
876 ic_dev_mtu
= ntohs(mtu
);
878 case 40: /* NIS Domain name (_not_ DNS) */
879 ic_bootp_string(utsname()->domainname
, ext
+1, *ext
, __NEW_UTS_LEN
);
886 * Receive BOOTP reply.
888 static int __init
ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
895 if (!net_eq(dev_net(dev
), &init_net
))
898 /* Perform verifications before taking the lock. */
899 if (skb
->pkt_type
== PACKET_OTHERHOST
)
902 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
905 if (!pskb_may_pull(skb
,
906 sizeof(struct iphdr
) +
907 sizeof(struct udphdr
)))
910 b
= (struct bootp_pkt
*)skb_network_header(skb
);
913 if (h
->ihl
!= 5 || h
->version
!= 4 || h
->protocol
!= IPPROTO_UDP
)
916 /* Fragments are not supported */
917 if (h
->frag_off
& htons(IP_OFFSET
| IP_MF
)) {
919 printk(KERN_ERR
"DHCP/BOOTP: Ignoring fragmented "
924 if (skb
->len
< ntohs(h
->tot_len
))
927 if (ip_fast_csum((char *) h
, h
->ihl
))
930 if (b
->udph
.source
!= htons(67) || b
->udph
.dest
!= htons(68))
933 if (ntohs(h
->tot_len
) < ntohs(b
->udph
.len
) + sizeof(struct iphdr
))
936 len
= ntohs(b
->udph
.len
) - sizeof(struct udphdr
);
937 ext_len
= len
- (sizeof(*b
) -
938 sizeof(struct iphdr
) -
939 sizeof(struct udphdr
) -
944 /* Ok the front looks good, make sure we can get at the rest. */
945 if (!pskb_may_pull(skb
, skb
->len
))
948 b
= (struct bootp_pkt
*)skb_network_header(skb
);
951 /* One reply at a time, please. */
952 spin_lock(&ic_recv_lock
);
954 /* If we already have a reply, just drop the packet */
958 /* Find the ic_device that the packet arrived on */
960 while (d
&& d
->dev
!= dev
)
963 goto drop_unlock
; /* should never happen */
965 /* Is it a reply to our BOOTP request? */
966 if (b
->op
!= BOOTP_REPLY
||
969 printk(KERN_ERR
"DHCP/BOOTP: Reply not for us, "
975 /* Is it a reply for the device we are configuring? */
976 if (b
->xid
!= ic_dev_xid
) {
978 printk(KERN_ERR
"DHCP/BOOTP: Ignoring delayed packet\n");
982 /* Parse extensions */
984 !memcmp(b
->exten
, ic_bootp_cookie
, 4)) { /* Check magic cookie */
985 u8
*end
= (u8
*) b
+ ntohs(b
->iph
.tot_len
);
989 if (ic_proto_enabled
& IC_USE_DHCP
) {
990 __be32 server_id
= NONE
;
994 while (ext
< end
&& *ext
!= 0xff) {
996 if (*opt
== 0) /* Padding */
1002 case 53: /* Message type */
1006 case 54: /* Server ID (IP address) */
1008 memcpy(&server_id
, opt
+ 2, 4);
1013 #ifdef IPCONFIG_DEBUG
1014 printk("DHCP: Got message type %d\n", mt
);
1019 /* While in the process of accepting one offer,
1020 * ignore all others.
1022 if (ic_myaddr
!= NONE
)
1025 /* Let's accept that offer. */
1026 ic_myaddr
= b
->your_ip
;
1027 ic_servaddr
= server_id
;
1028 #ifdef IPCONFIG_DEBUG
1029 printk("DHCP: Offered address %pI4 by server %pI4\n",
1030 &ic_myaddr
, &ic_servaddr
);
1032 /* The DHCP indicated server address takes
1033 * precedence over the bootp header one if
1034 * they are different.
1036 if ((server_id
!= NONE
) &&
1037 (b
->server_ip
!= server_id
))
1038 b
->server_ip
= ic_servaddr
;
1042 if (memcmp(dev
->dev_addr
, b
->hw_addr
, dev
->addr_len
) != 0)
1049 /* Urque. Forget it*/
1055 ic_dhcp_msgtype
= mt
;
1058 #endif /* IPCONFIG_DHCP */
1061 while (ext
< end
&& *ext
!= 0xff) {
1063 if (*opt
== 0) /* Padding */
1067 ic_do_bootp_ext(opt
);
1071 /* We have a winner! */
1073 ic_myaddr
= b
->your_ip
;
1074 ic_servaddr
= b
->server_ip
;
1075 if (ic_gateway
== NONE
&& b
->relay_ip
)
1076 ic_gateway
= b
->relay_ip
;
1077 if (ic_nameservers
[0] == NONE
)
1078 ic_nameservers
[0] = ic_servaddr
;
1079 ic_got_reply
= IC_BOOTP
;
1082 /* Show's over. Nothing to see here. */
1083 spin_unlock(&ic_recv_lock
);
1086 /* Throw the packet out. */
1097 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1100 #ifdef IPCONFIG_DYNAMIC
1102 static int __init
ic_dynamic(void)
1105 struct ic_device
*d
;
1106 unsigned long start_jiffies
, timeout
, jiff
;
1107 int do_bootp
= ic_proto_have_if
& IC_BOOTP
;
1108 int do_rarp
= ic_proto_have_if
& IC_RARP
;
1111 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1112 * This routine gets only called when some pieces of information
1113 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1115 if (!ic_proto_enabled
) {
1116 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1120 #ifdef IPCONFIG_BOOTP
1121 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_BOOTP
)
1122 printk(KERN_ERR
"DHCP/BOOTP: No suitable device found.\n");
1124 #ifdef IPCONFIG_RARP
1125 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_RARP
)
1126 printk(KERN_ERR
"RARP: No suitable device found.\n");
1129 if (!ic_proto_have_if
)
1130 /* Error message already printed */
1136 #ifdef IPCONFIG_BOOTP
1140 #ifdef IPCONFIG_RARP
1146 * Send requests and wait, until we get an answer. This loop
1147 * seems to be a terrible waste of CPU time, but actually there is
1148 * only one process running at all, so we don't need to use any
1149 * scheduler functions.
1150 * [Actually we could now, but the nothing else running note still
1153 printk(KERN_NOTICE
"Sending %s%s%s requests .",
1155 ? ((ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP") : "",
1156 (do_bootp
&& do_rarp
) ? " and " : "",
1157 do_rarp
? "RARP" : "");
1159 start_jiffies
= jiffies
;
1161 retries
= CONF_SEND_RETRIES
;
1162 get_random_bytes(&timeout
, sizeof(timeout
));
1163 timeout
= CONF_BASE_TIMEOUT
+ (timeout
% (unsigned) CONF_TIMEOUT_RANDOM
);
1165 /* Track the device we are configuring */
1166 ic_dev_xid
= d
->xid
;
1168 #ifdef IPCONFIG_BOOTP
1169 if (do_bootp
&& (d
->able
& IC_BOOTP
))
1170 ic_bootp_send_if(d
, jiffies
- start_jiffies
);
1172 #ifdef IPCONFIG_RARP
1173 if (do_rarp
&& (d
->able
& IC_RARP
))
1177 jiff
= jiffies
+ (d
->next
? CONF_INTER_TIMEOUT
: timeout
);
1178 while (time_before(jiffies
, jiff
) && !ic_got_reply
)
1179 schedule_timeout_uninterruptible(1);
1180 #ifdef IPCONFIG_DHCP
1181 /* DHCP isn't done until we get a DHCPACK. */
1182 if ((ic_got_reply
& IC_BOOTP
) &&
1183 (ic_proto_enabled
& IC_USE_DHCP
) &&
1184 ic_dhcp_msgtype
!= DHCPACK
) {
1189 #endif /* IPCONFIG_DHCP */
1200 printk(" timed out!\n");
1206 timeout
= timeout CONF_TIMEOUT_MULT
;
1207 if (timeout
> CONF_TIMEOUT_MAX
)
1208 timeout
= CONF_TIMEOUT_MAX
;
1213 #ifdef IPCONFIG_BOOTP
1217 #ifdef IPCONFIG_RARP
1222 if (!ic_got_reply
) {
1227 printk("IP-Config: Got %s answer from %pI4, ",
1228 ((ic_got_reply
& IC_RARP
) ? "RARP"
1229 : (ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP"),
1231 printk("my address is %pI4\n", &ic_myaddr
);
1236 #endif /* IPCONFIG_DYNAMIC */
1238 #ifdef CONFIG_PROC_FS
1240 static int pnp_seq_show(struct seq_file
*seq
, void *v
)
1244 if (ic_proto_used
& IC_PROTO
)
1245 seq_printf(seq
, "#PROTO: %s\n",
1246 (ic_proto_used
& IC_RARP
) ? "RARP"
1247 : (ic_proto_used
& IC_USE_DHCP
) ? "DHCP" : "BOOTP");
1249 seq_puts(seq
, "#MANUAL\n");
1253 "domain %s\n", ic_domain
);
1254 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++) {
1255 if (ic_nameservers
[i
] != NONE
)
1256 seq_printf(seq
, "nameserver %pI4\n",
1257 &ic_nameservers
[i
]);
1259 if (ic_servaddr
!= NONE
)
1260 seq_printf(seq
, "bootserver %pI4\n",
1265 static int pnp_seq_open(struct inode
*indoe
, struct file
*file
)
1267 return single_open(file
, pnp_seq_show
, NULL
);
1270 static const struct file_operations pnp_seq_fops
= {
1271 .owner
= THIS_MODULE
,
1272 .open
= pnp_seq_open
,
1274 .llseek
= seq_lseek
,
1275 .release
= single_release
,
1277 #endif /* CONFIG_PROC_FS */
1280 * Extract IP address from the parameter string if needed. Note that we
1281 * need to have root_server_addr set _before_ IPConfig gets called as it
1284 __be32 __init
root_nfs_parse_addr(char *name
)
1291 while (octets
< 4) {
1292 while (*cp
>= '0' && *cp
<= '9')
1294 if (cp
== cq
|| cp
- cq
> 3)
1296 if (*cp
== '.' || octets
== 3)
1302 if (octets
== 4 && (*cp
== ':' || *cp
== '\0')) {
1305 addr
= in_aton(name
);
1306 memmove(name
, cp
, strlen(cp
) + 1);
1313 #define DEVICE_WAIT_MAX 12 /* 12 seconds */
1315 static int __init
wait_for_devices(void)
1319 msleep(CONF_PRE_OPEN
);
1320 for (i
= 0; i
< DEVICE_WAIT_MAX
; i
++) {
1321 struct net_device
*dev
;
1325 for_each_netdev(&init_net
, dev
) {
1326 if (ic_device_match(dev
)) {
1340 * IP Autoconfig dispatcher.
1343 static int __init
ip_auto_config(void)
1346 #ifdef IPCONFIG_DYNAMIC
1347 int retries
= CONF_OPEN_RETRIES
;
1351 #ifdef CONFIG_PROC_FS
1352 proc_net_fops_create(&init_net
, "pnp", S_IRUGO
, &pnp_seq_fops
);
1353 #endif /* CONFIG_PROC_FS */
1358 DBG(("IP-Config: Entered.\n"));
1359 #ifdef IPCONFIG_DYNAMIC
1362 /* Wait for devices to appear */
1363 err
= wait_for_devices();
1367 /* Setup all network devices */
1368 err
= ic_open_devs();
1372 /* Give drivers a chance to settle */
1373 ssleep(CONF_POST_OPEN
);
1376 * If the config information is insufficient (e.g., our IP address or
1377 * IP address of the boot server is missing or we have multiple network
1378 * interfaces and no default was set), use BOOTP or RARP to get the
1381 if (ic_myaddr
== NONE
||
1382 #ifdef CONFIG_ROOT_NFS
1383 (root_server_addr
== NONE
&&
1384 ic_servaddr
== NONE
&&
1385 ROOT_DEV
== Root_NFS
) ||
1387 ic_first_dev
->next
) {
1388 #ifdef IPCONFIG_DYNAMIC
1389 if (ic_dynamic() < 0) {
1393 * I don't know why, but sometimes the
1394 * eepro100 driver (at least) gets upset and
1395 * doesn't work the first time it's opened.
1396 * But then if you close it and reopen it, it
1397 * works just fine. So we need to try that at
1398 * least once before giving up.
1400 * Also, if the root will be NFS-mounted, we
1401 * have nowhere to go if DHCP fails. So we
1402 * just have to keep trying forever.
1406 #ifdef CONFIG_ROOT_NFS
1407 if (ROOT_DEV
== Root_NFS
) {
1409 "IP-Config: Retrying forever (NFS root)...\n");
1416 "IP-Config: Reopening network devices...\n");
1420 /* Oh, well. At least we tried. */
1421 printk(KERN_ERR
"IP-Config: Auto-configuration of network failed.\n");
1424 #else /* !DYNAMIC */
1425 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1428 #endif /* IPCONFIG_DYNAMIC */
1430 /* Device selected manually or only one device -> use it */
1431 ic_dev
= ic_first_dev
->dev
;
1434 addr
= root_nfs_parse_addr(root_server_path
);
1435 if (root_server_addr
== NONE
)
1436 root_server_addr
= addr
;
1439 * Use defaults whereever applicable.
1441 if (ic_defaults() < 0)
1445 * Close all network devices except the device we've
1446 * autoconfigured and set up routes.
1449 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1453 * Record which protocol was actually used.
1455 #ifdef IPCONFIG_DYNAMIC
1456 ic_proto_used
= ic_got_reply
| (ic_proto_enabled
& IC_USE_DHCP
);
1459 #ifndef IPCONFIG_SILENT
1461 * Clue in the operator.
1463 printk("IP-Config: Complete:");
1464 printk("\n device=%s", ic_dev
->name
);
1465 printk(", addr=%pI4", &ic_myaddr
);
1466 printk(", mask=%pI4", &ic_netmask
);
1467 printk(", gw=%pI4", &ic_gateway
);
1468 printk(",\n host=%s, domain=%s, nis-domain=%s",
1469 utsname()->nodename
, ic_domain
, utsname()->domainname
);
1470 printk(",\n bootserver=%pI4", &ic_servaddr
);
1471 printk(", rootserver=%pI4", &root_server_addr
);
1472 printk(", rootpath=%s", root_server_path
);
1474 printk(", mtu=%d", ic_dev_mtu
);
1476 #endif /* !SILENT */
1481 late_initcall(ip_auto_config
);
1485 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1486 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
1488 static int __init
ic_proto_name(char *name
)
1490 if (!strcmp(name
, "on") || !strcmp(name
, "any")) {
1493 if (!strcmp(name
, "off") || !strcmp(name
, "none")) {
1496 #ifdef CONFIG_IP_PNP_DHCP
1497 else if (!strcmp(name
, "dhcp")) {
1498 ic_proto_enabled
&= ~IC_RARP
;
1502 #ifdef CONFIG_IP_PNP_BOOTP
1503 else if (!strcmp(name
, "bootp")) {
1504 ic_proto_enabled
&= ~(IC_RARP
| IC_USE_DHCP
);
1508 #ifdef CONFIG_IP_PNP_RARP
1509 else if (!strcmp(name
, "rarp")) {
1510 ic_proto_enabled
&= ~(IC_BOOTP
| IC_USE_DHCP
);
1514 #ifdef IPCONFIG_DYNAMIC
1515 else if (!strcmp(name
, "both")) {
1516 ic_proto_enabled
&= ~IC_USE_DHCP
; /* backward compat :-( */
1523 static int __init
ip_auto_config_setup(char *addrs
)
1528 ic_set_manually
= 1;
1532 * If any dhcp, bootp etc options are set, leave autoconfig on
1533 * and skip the below static IP processing.
1535 if (ic_proto_name(addrs
))
1538 /* If no static IP is given, turn off autoconfig and bail. */
1540 strcmp(addrs
, "off") == 0 ||
1541 strcmp(addrs
, "none") == 0) {
1546 /* Parse string for static IP assignment. */
1549 if ((cp
= strchr(ip
, ':')))
1551 if (strlen(ip
) > 0) {
1552 DBG(("IP-Config: Parameter #%d: `%s'\n", num
, ip
));
1555 if ((ic_myaddr
= in_aton(ip
)) == ANY
)
1559 if ((ic_servaddr
= in_aton(ip
)) == ANY
)
1563 if ((ic_gateway
= in_aton(ip
)) == ANY
)
1567 if ((ic_netmask
= in_aton(ip
)) == ANY
)
1571 if ((dp
= strchr(ip
, '.'))) {
1573 strlcpy(utsname()->domainname
, dp
,
1574 sizeof(utsname()->domainname
));
1576 strlcpy(utsname()->nodename
, ip
,
1577 sizeof(utsname()->nodename
));
1578 ic_host_name_set
= 1;
1581 strlcpy(user_dev_name
, ip
, sizeof(user_dev_name
));
1584 if (ic_proto_name(ip
) == 0 &&
1585 ic_myaddr
== NONE
) {
1598 static int __init
nfsaddrs_config_setup(char *addrs
)
1600 return ip_auto_config_setup(addrs
);
1603 static int __init
vendor_class_identifier_setup(char *addrs
)
1605 if (strlcpy(vendor_class_identifier
, addrs
,
1606 sizeof(vendor_class_identifier
))
1607 >= sizeof(vendor_class_identifier
))
1608 printk(KERN_WARNING
"DHCP: vendorclass too long, truncated to \"%s\"",
1609 vendor_class_identifier
);
1613 __setup("ip=", ip_auto_config_setup
);
1614 __setup("nfsaddrs=", nfsaddrs_config_setup
);
1615 __setup("dhcpclass=", vendor_class_identifier_setup
);