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>
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_PRE_OPEN 500 /* Before opening: 1/2 second */
92 #define CONF_POST_OPEN 1 /* After opening: 1 second */
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/2) /* Inter-device timeout: 1/2 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 __constant_htonl(INADDR_NONE)
108 * Public IP configuration
111 /* This is used by platforms which might be able to set the ipconfig
112 * variables using firmware environment vars. If this is set, it will
113 * ignore such firmware variables.
115 int ic_set_manually __initdata
= 0; /* IPconfig parameters set manually */
117 static int ic_enable __initdata
= 0; /* IP config enabled? */
119 /* Protocol choice */
120 int ic_proto_enabled __initdata
= 0
121 #ifdef IPCONFIG_BOOTP
124 #ifdef CONFIG_IP_PNP_DHCP
132 static int ic_host_name_set __initdata
= 0; /* Host name set by us? */
134 __be32 ic_myaddr
= NONE
; /* My IP address */
135 static __be32 ic_netmask
= NONE
; /* Netmask for local subnet */
136 __be32 ic_gateway
= NONE
; /* Gateway IP address */
138 __be32 ic_servaddr
= NONE
; /* Boot server IP address */
140 __be32 root_server_addr
= NONE
; /* Address of NFS server */
141 u8 root_server_path
[256] = { 0, }; /* Path to mount as root */
143 /* Persistent data: */
145 static int ic_proto_used
; /* Protocol used, if any */
146 static __be32 ic_nameservers
[CONF_NAMESERVERS_MAX
]; /* DNS Server IP addresses */
147 static u8 ic_domain
[64]; /* DNS (not NIS) domain name */
153 /* Name of user-selected boot device */
154 static char user_dev_name
[IFNAMSIZ
] __initdata
= { 0, };
156 /* Protocols supported by available interfaces */
157 static int ic_proto_have_if __initdata
= 0;
159 #ifdef IPCONFIG_DYNAMIC
160 static DEFINE_SPINLOCK(ic_recv_lock
);
161 static volatile int ic_got_reply __initdata
= 0; /* Proto(s) that replied */
164 static int ic_dhcp_msgtype __initdata
= 0; /* DHCP msg type received */
173 struct ic_device
*next
;
174 struct net_device
*dev
;
175 unsigned short flags
;
180 static struct ic_device
*ic_first_dev __initdata
= NULL
;/* List of open device */
181 static struct net_device
*ic_dev __initdata
= NULL
; /* Selected device */
183 static int __init
ic_open_devs(void)
185 struct ic_device
*d
, **last
;
186 struct net_device
*dev
;
187 unsigned short oflags
;
189 last
= &ic_first_dev
;
192 /* bring loopback device up first */
193 for_each_netdev(&init_net
, dev
) {
194 if (!(dev
->flags
& IFF_LOOPBACK
))
196 if (dev_change_flags(dev
, dev
->flags
| IFF_UP
) < 0)
197 printk(KERN_ERR
"IP-Config: Failed to open %s\n", dev
->name
);
200 for_each_netdev(&init_net
, dev
) {
201 if (dev
->flags
& IFF_LOOPBACK
)
203 if (user_dev_name
[0] ? !strcmp(dev
->name
, user_dev_name
) :
204 (!(dev
->flags
& IFF_LOOPBACK
) &&
205 (dev
->flags
& (IFF_POINTOPOINT
|IFF_BROADCAST
)) &&
206 strncmp(dev
->name
, "dummy", 5))) {
211 printk(KERN_WARNING
"DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev
->name
, dev
->mtu
);
212 if (!(dev
->flags
& IFF_NOARP
))
214 able
&= ic_proto_enabled
;
215 if (ic_proto_enabled
&& !able
)
218 if (dev_change_flags(dev
, oflags
| IFF_UP
) < 0) {
219 printk(KERN_ERR
"IP-Config: Failed to open %s\n", dev
->name
);
222 if (!(d
= kmalloc(sizeof(struct ic_device
), GFP_KERNEL
))) {
232 get_random_bytes(&d
->xid
, sizeof(__be32
));
235 ic_proto_have_if
|= able
;
236 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
237 dev
->name
, able
, d
->xid
));
245 if (user_dev_name
[0])
246 printk(KERN_ERR
"IP-Config: Device `%s' not found.\n", user_dev_name
);
248 printk(KERN_ERR
"IP-Config: No network devices available.\n");
254 static void __init
ic_close_devs(void)
256 struct ic_device
*d
, *next
;
257 struct net_device
*dev
;
265 DBG(("IP-Config: Downing %s\n", dev
->name
));
266 dev_change_flags(dev
, d
->flags
);
274 * Interface to various network functions.
278 set_sockaddr(struct sockaddr_in
*sin
, __be32 addr
, __be16 port
)
280 sin
->sin_family
= AF_INET
;
281 sin
->sin_addr
.s_addr
= addr
;
282 sin
->sin_port
= port
;
285 static int __init
ic_dev_ioctl(unsigned int cmd
, struct ifreq
*arg
)
289 mm_segment_t oldfs
= get_fs();
291 res
= devinet_ioctl(cmd
, (struct ifreq __user
*) arg
);
296 static int __init
ic_route_ioctl(unsigned int cmd
, struct rtentry
*arg
)
300 mm_segment_t oldfs
= get_fs();
302 res
= ip_rt_ioctl(cmd
, (void __user
*) arg
);
308 * Set up interface addresses and routes.
311 static int __init
ic_setup_if(void)
314 struct sockaddr_in
*sin
= (void *) &ir
.ifr_ifru
.ifru_addr
;
317 memset(&ir
, 0, sizeof(ir
));
318 strcpy(ir
.ifr_ifrn
.ifrn_name
, ic_dev
->name
);
319 set_sockaddr(sin
, ic_myaddr
, 0);
320 if ((err
= ic_dev_ioctl(SIOCSIFADDR
, &ir
)) < 0) {
321 printk(KERN_ERR
"IP-Config: Unable to set interface address (%d).\n", err
);
324 set_sockaddr(sin
, ic_netmask
, 0);
325 if ((err
= ic_dev_ioctl(SIOCSIFNETMASK
, &ir
)) < 0) {
326 printk(KERN_ERR
"IP-Config: Unable to set interface netmask (%d).\n", err
);
329 set_sockaddr(sin
, ic_myaddr
| ~ic_netmask
, 0);
330 if ((err
= ic_dev_ioctl(SIOCSIFBRDADDR
, &ir
)) < 0) {
331 printk(KERN_ERR
"IP-Config: Unable to set interface broadcast address (%d).\n", err
);
337 static int __init
ic_setup_routes(void)
339 /* No need to setup device routes, only the default route... */
341 if (ic_gateway
!= NONE
) {
345 memset(&rm
, 0, sizeof(rm
));
346 if ((ic_gateway
^ ic_myaddr
) & ic_netmask
) {
347 printk(KERN_ERR
"IP-Config: Gateway not on directly connected network.\n");
350 set_sockaddr((struct sockaddr_in
*) &rm
.rt_dst
, 0, 0);
351 set_sockaddr((struct sockaddr_in
*) &rm
.rt_genmask
, 0, 0);
352 set_sockaddr((struct sockaddr_in
*) &rm
.rt_gateway
, ic_gateway
, 0);
353 rm
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
354 if ((err
= ic_route_ioctl(SIOCADDRT
, &rm
)) < 0) {
355 printk(KERN_ERR
"IP-Config: Cannot add default route (%d).\n", err
);
364 * Fill in default values for all missing parameters.
367 static int __init
ic_defaults(void)
370 * At this point we have no userspace running so need not
371 * claim locks on system_utsname
374 if (!ic_host_name_set
)
375 sprintf(init_utsname()->nodename
, "%u.%u.%u.%u", NIPQUAD(ic_myaddr
));
377 if (root_server_addr
== NONE
)
378 root_server_addr
= ic_servaddr
;
380 if (ic_netmask
== NONE
) {
381 if (IN_CLASSA(ntohl(ic_myaddr
)))
382 ic_netmask
= htonl(IN_CLASSA_NET
);
383 else if (IN_CLASSB(ntohl(ic_myaddr
)))
384 ic_netmask
= htonl(IN_CLASSB_NET
);
385 else if (IN_CLASSC(ntohl(ic_myaddr
)))
386 ic_netmask
= htonl(IN_CLASSC_NET
);
388 printk(KERN_ERR
"IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
392 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask
));
404 static int ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
406 static struct packet_type rarp_packet_type __initdata
= {
407 .type
= __constant_htons(ETH_P_RARP
),
408 .func
= ic_rarp_recv
,
411 static inline void ic_rarp_init(void)
413 dev_add_pack(&rarp_packet_type
);
416 static inline void ic_rarp_cleanup(void)
418 dev_remove_pack(&rarp_packet_type
);
422 * Process received RARP packet.
425 ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
428 unsigned char *rarp_ptr
;
430 unsigned char *sha
, *tha
; /* s for "source", t for "target" */
433 if (dev
->nd_net
!= &init_net
)
436 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
439 if (!pskb_may_pull(skb
, sizeof(struct arphdr
)))
442 /* Basic sanity checks can be done without the lock. */
443 rarp
= (struct arphdr
*)skb_transport_header(skb
);
445 /* If this test doesn't pass, it's not IP, or we should
448 if (rarp
->ar_hln
!= dev
->addr_len
|| dev
->type
!= ntohs(rarp
->ar_hrd
))
451 /* If it's not a RARP reply, delete it. */
452 if (rarp
->ar_op
!= htons(ARPOP_RREPLY
))
455 /* If it's not Ethernet, delete it. */
456 if (rarp
->ar_pro
!= htons(ETH_P_IP
))
459 if (!pskb_may_pull(skb
,
460 sizeof(struct arphdr
) +
461 (2 * dev
->addr_len
) +
465 /* OK, it is all there and looks valid, process... */
466 rarp
= (struct arphdr
*)skb_transport_header(skb
);
467 rarp_ptr
= (unsigned char *) (rarp
+ 1);
469 /* One reply at a time, please. */
470 spin_lock(&ic_recv_lock
);
472 /* If we already have a reply, just drop the packet */
476 /* Find the ic_device that the packet arrived on */
478 while (d
&& d
->dev
!= dev
)
481 goto drop_unlock
; /* should never happen */
483 /* Extract variable-width fields */
485 rarp_ptr
+= dev
->addr_len
;
486 memcpy(&sip
, rarp_ptr
, 4);
489 rarp_ptr
+= dev
->addr_len
;
490 memcpy(&tip
, rarp_ptr
, 4);
492 /* Discard packets which are not meant for us. */
493 if (memcmp(tha
, dev
->dev_addr
, dev
->addr_len
))
496 /* Discard packets which are not from specified server. */
497 if (ic_servaddr
!= NONE
&& ic_servaddr
!= sip
)
500 /* We have a winner! */
502 if (ic_myaddr
== NONE
)
505 ic_got_reply
= IC_RARP
;
508 /* Show's over. Nothing to see here. */
509 spin_unlock(&ic_recv_lock
);
512 /* Throw the packet out. */
519 * Send RARP request packet over a single interface.
521 static void __init
ic_rarp_send_if(struct ic_device
*d
)
523 struct net_device
*dev
= d
->dev
;
524 arp_send(ARPOP_RREQUEST
, ETH_P_RARP
, 0, dev
, 0, NULL
,
525 dev
->dev_addr
, dev
->dev_addr
);
530 * DHCP/BOOTP support.
533 #ifdef IPCONFIG_BOOTP
535 struct bootp_pkt
{ /* BOOTP packet format */
536 struct iphdr iph
; /* IP header */
537 struct udphdr udph
; /* UDP header */
538 u8 op
; /* 1=request, 2=reply */
539 u8 htype
; /* HW address type */
540 u8 hlen
; /* HW address length */
541 u8 hops
; /* Used only by gateways */
542 __be32 xid
; /* Transaction ID */
543 __be16 secs
; /* Seconds since we started */
544 __be16 flags
; /* Just what it says */
545 __be32 client_ip
; /* Client's IP address if known */
546 __be32 your_ip
; /* Assigned IP address */
547 __be32 server_ip
; /* (Next, e.g. NFS) Server's IP address */
548 __be32 relay_ip
; /* IP address of BOOTP relay */
549 u8 hw_addr
[16]; /* Client's HW address */
550 u8 serv_name
[64]; /* Server host name */
551 u8 boot_file
[128]; /* Name of boot file */
552 u8 exten
[312]; /* DHCP options / BOOTP vendor extensions */
556 #define BOOTP_REQUEST 1
557 #define BOOTP_REPLY 2
559 /* DHCP message types */
560 #define DHCPDISCOVER 1
562 #define DHCPREQUEST 3
563 #define DHCPDECLINE 4
566 #define DHCPRELEASE 7
569 static int ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
571 static struct packet_type bootp_packet_type __initdata
= {
572 .type
= __constant_htons(ETH_P_IP
),
573 .func
= ic_bootp_recv
,
578 * Initialize DHCP/BOOTP extension fields in the request.
581 static const u8 ic_bootp_cookie
[4] = { 99, 130, 83, 99 };
586 ic_dhcp_init_options(u8
*options
)
588 u8 mt
= ((ic_servaddr
== NONE
)
589 ? DHCPDISCOVER
: DHCPREQUEST
);
592 #ifdef IPCONFIG_DEBUG
593 printk("DHCP: Sending message type %d\n", mt
);
596 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
599 *e
++ = 53; /* DHCP message type */
603 if (mt
== DHCPREQUEST
) {
604 *e
++ = 54; /* Server ID (IP address) */
606 memcpy(e
, &ic_servaddr
, 4);
609 *e
++ = 50; /* Requested IP address */
611 memcpy(e
, &ic_myaddr
, 4);
617 static const u8 ic_req_params
[] = {
619 3, /* Default gateway */
622 15, /* Domain name */
624 40, /* NIS domain name */
627 *e
++ = 55; /* Parameter request list */
628 *e
++ = sizeof(ic_req_params
);
629 memcpy(e
, ic_req_params
, sizeof(ic_req_params
));
630 e
+= sizeof(ic_req_params
);
633 *e
++ = 255; /* End of the list */
636 #endif /* IPCONFIG_DHCP */
638 static void __init
ic_bootp_init_ext(u8
*e
)
640 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
642 *e
++ = 1; /* Subnet mask request */
645 *e
++ = 3; /* Default gateway request */
648 *e
++ = 5; /* Name server request */
651 *e
++ = 12; /* Host name request */
654 *e
++ = 40; /* NIS Domain name request */
657 *e
++ = 17; /* Boot path */
661 *e
++ = 57; /* set extension buffer size for reply */
663 *e
++ = 1; /* 128+236+8+20+14, see dhcpd sources */
666 *e
++ = 255; /* End of the list */
671 * Initialize the DHCP/BOOTP mechanism.
673 static inline void ic_bootp_init(void)
677 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++)
678 ic_nameservers
[i
] = NONE
;
680 dev_add_pack(&bootp_packet_type
);
685 * DHCP/BOOTP cleanup.
687 static inline void ic_bootp_cleanup(void)
689 dev_remove_pack(&bootp_packet_type
);
694 * Send DHCP/BOOTP request to single interface.
696 static void __init
ic_bootp_send_if(struct ic_device
*d
, unsigned long jiffies_diff
)
698 struct net_device
*dev
= d
->dev
;
701 int hh_len
= LL_RESERVED_SPACE(dev
);
704 /* Allocate packet */
705 skb
= alloc_skb(sizeof(struct bootp_pkt
) + hh_len
+ 15, GFP_KERNEL
);
708 skb_reserve(skb
, hh_len
);
709 b
= (struct bootp_pkt
*) skb_put(skb
, sizeof(struct bootp_pkt
));
710 memset(b
, 0, sizeof(struct bootp_pkt
));
712 /* Construct IP header */
713 skb_reset_network_header(skb
);
717 h
->tot_len
= htons(sizeof(struct bootp_pkt
));
718 h
->frag_off
= htons(IP_DF
);
720 h
->protocol
= IPPROTO_UDP
;
721 h
->daddr
= htonl(INADDR_BROADCAST
);
722 h
->check
= ip_fast_csum((unsigned char *) h
, h
->ihl
);
724 /* Construct UDP header */
725 b
->udph
.source
= htons(68);
726 b
->udph
.dest
= htons(67);
727 b
->udph
.len
= htons(sizeof(struct bootp_pkt
) - sizeof(struct iphdr
));
728 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
730 /* Construct DHCP/BOOTP header */
731 b
->op
= BOOTP_REQUEST
;
732 if (dev
->type
< 256) /* check for false types */
733 b
->htype
= dev
->type
;
734 else if (dev
->type
== ARPHRD_IEEE802_TR
) /* fix for token ring */
735 b
->htype
= ARPHRD_IEEE802
;
736 else if (dev
->type
== ARPHRD_FDDI
)
737 b
->htype
= ARPHRD_ETHER
;
739 printk("Unknown ARP type 0x%04x for device %s\n", dev
->type
, dev
->name
);
740 b
->htype
= dev
->type
; /* can cause undefined behavior */
742 b
->hlen
= dev
->addr_len
;
745 memcpy(b
->hw_addr
, dev
->dev_addr
, dev
->addr_len
);
746 b
->secs
= htons(jiffies_diff
/ HZ
);
749 /* add DHCP options or BOOTP extensions */
751 if (ic_proto_enabled
& IC_USE_DHCP
)
752 ic_dhcp_init_options(b
->exten
);
755 ic_bootp_init_ext(b
->exten
);
757 /* Chain packet down the line... */
759 skb
->protocol
= htons(ETH_P_IP
);
760 if (dev_hard_header(skb
, dev
, ntohs(skb
->protocol
),
761 dev
->broadcast
, dev
->dev_addr
, skb
->len
) < 0 ||
762 dev_queue_xmit(skb
) < 0)
768 * Copy BOOTP-supplied string if not already set.
770 static int __init
ic_bootp_string(char *dest
, char *src
, int len
, int max
)
776 memcpy(dest
, src
, len
);
783 * Process BOOTP extensions.
785 static void __init
ic_do_bootp_ext(u8
*ext
)
790 #ifdef IPCONFIG_DEBUG
793 printk("DHCP/BOOTP: Got extension %d:",*ext
);
794 for (c
=ext
+2; c
<ext
+2+ext
[1]; c
++)
800 case 1: /* Subnet mask */
801 if (ic_netmask
== NONE
)
802 memcpy(&ic_netmask
, ext
+1, 4);
804 case 3: /* Default gateway */
805 if (ic_gateway
== NONE
)
806 memcpy(&ic_gateway
, ext
+1, 4);
808 case 6: /* DNS server */
810 if (servers
> CONF_NAMESERVERS_MAX
)
811 servers
= CONF_NAMESERVERS_MAX
;
812 for (i
= 0; i
< servers
; i
++) {
813 if (ic_nameservers
[i
] == NONE
)
814 memcpy(&ic_nameservers
[i
], ext
+1+4*i
, 4);
817 case 12: /* Host name */
818 ic_bootp_string(utsname()->nodename
, ext
+1, *ext
, __NEW_UTS_LEN
);
819 ic_host_name_set
= 1;
821 case 15: /* Domain name (DNS) */
822 ic_bootp_string(ic_domain
, ext
+1, *ext
, sizeof(ic_domain
));
824 case 17: /* Root path */
825 if (!root_server_path
[0])
826 ic_bootp_string(root_server_path
, ext
+1, *ext
, sizeof(root_server_path
));
828 case 40: /* NIS Domain name (_not_ DNS) */
829 ic_bootp_string(utsname()->domainname
, ext
+1, *ext
, __NEW_UTS_LEN
);
836 * Receive BOOTP reply.
838 static int __init
ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
845 if (dev
->nd_net
!= &init_net
)
848 /* Perform verifications before taking the lock. */
849 if (skb
->pkt_type
== PACKET_OTHERHOST
)
852 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
855 if (!pskb_may_pull(skb
,
856 sizeof(struct iphdr
) +
857 sizeof(struct udphdr
)))
860 b
= (struct bootp_pkt
*)skb_network_header(skb
);
863 if (h
->ihl
!= 5 || h
->version
!= 4 || h
->protocol
!= IPPROTO_UDP
)
866 /* Fragments are not supported */
867 if (h
->frag_off
& htons(IP_OFFSET
| IP_MF
)) {
869 printk(KERN_ERR
"DHCP/BOOTP: Ignoring fragmented "
874 if (skb
->len
< ntohs(h
->tot_len
))
877 if (ip_fast_csum((char *) h
, h
->ihl
))
880 if (b
->udph
.source
!= htons(67) || b
->udph
.dest
!= htons(68))
883 if (ntohs(h
->tot_len
) < ntohs(b
->udph
.len
) + sizeof(struct iphdr
))
886 len
= ntohs(b
->udph
.len
) - sizeof(struct udphdr
);
887 ext_len
= len
- (sizeof(*b
) -
888 sizeof(struct iphdr
) -
889 sizeof(struct udphdr
) -
894 /* Ok the front looks good, make sure we can get at the rest. */
895 if (!pskb_may_pull(skb
, skb
->len
))
898 b
= (struct bootp_pkt
*)skb_network_header(skb
);
901 /* One reply at a time, please. */
902 spin_lock(&ic_recv_lock
);
904 /* If we already have a reply, just drop the packet */
908 /* Find the ic_device that the packet arrived on */
910 while (d
&& d
->dev
!= dev
)
913 goto drop_unlock
; /* should never happen */
915 /* Is it a reply to our BOOTP request? */
916 if (b
->op
!= BOOTP_REPLY
||
919 printk(KERN_ERR
"DHCP/BOOTP: Reply not for us, "
925 /* Parse extensions */
927 !memcmp(b
->exten
, ic_bootp_cookie
, 4)) { /* Check magic cookie */
928 u8
*end
= (u8
*) b
+ ntohs(b
->iph
.tot_len
);
932 if (ic_proto_enabled
& IC_USE_DHCP
) {
933 __be32 server_id
= NONE
;
937 while (ext
< end
&& *ext
!= 0xff) {
939 if (*opt
== 0) /* Padding */
945 case 53: /* Message type */
949 case 54: /* Server ID (IP address) */
951 memcpy(&server_id
, opt
+ 2, 4);
956 #ifdef IPCONFIG_DEBUG
957 printk("DHCP: Got message type %d\n", mt
);
962 /* While in the process of accepting one offer,
965 if (ic_myaddr
!= NONE
)
968 /* Let's accept that offer. */
969 ic_myaddr
= b
->your_ip
;
970 ic_servaddr
= server_id
;
971 #ifdef IPCONFIG_DEBUG
972 printk("DHCP: Offered address %u.%u.%u.%u",
974 printk(" by server %u.%u.%u.%u\n",
975 NIPQUAD(ic_servaddr
));
977 /* The DHCP indicated server address takes
978 * precedence over the bootp header one if
979 * they are different.
981 if ((server_id
!= NONE
) &&
982 (b
->server_ip
!= server_id
))
983 b
->server_ip
= ic_servaddr
;
987 if (memcmp(dev
->dev_addr
, b
->hw_addr
, dev
->addr_len
) != 0)
994 /* Urque. Forget it*/
1000 ic_dhcp_msgtype
= mt
;
1003 #endif /* IPCONFIG_DHCP */
1006 while (ext
< end
&& *ext
!= 0xff) {
1008 if (*opt
== 0) /* Padding */
1012 ic_do_bootp_ext(opt
);
1016 /* We have a winner! */
1018 ic_myaddr
= b
->your_ip
;
1019 ic_servaddr
= b
->server_ip
;
1020 if (ic_gateway
== NONE
&& b
->relay_ip
)
1021 ic_gateway
= b
->relay_ip
;
1022 if (ic_nameservers
[0] == NONE
)
1023 ic_nameservers
[0] = ic_servaddr
;
1024 ic_got_reply
= IC_BOOTP
;
1027 /* Show's over. Nothing to see here. */
1028 spin_unlock(&ic_recv_lock
);
1031 /* Throw the packet out. */
1042 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1045 #ifdef IPCONFIG_DYNAMIC
1047 static int __init
ic_dynamic(void)
1050 struct ic_device
*d
;
1051 unsigned long start_jiffies
, timeout
, jiff
;
1052 int do_bootp
= ic_proto_have_if
& IC_BOOTP
;
1053 int do_rarp
= ic_proto_have_if
& IC_RARP
;
1056 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1057 * This routine gets only called when some pieces of information
1058 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1060 if (!ic_proto_enabled
) {
1061 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1065 #ifdef IPCONFIG_BOOTP
1066 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_BOOTP
)
1067 printk(KERN_ERR
"DHCP/BOOTP: No suitable device found.\n");
1069 #ifdef IPCONFIG_RARP
1070 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_RARP
)
1071 printk(KERN_ERR
"RARP: No suitable device found.\n");
1074 if (!ic_proto_have_if
)
1075 /* Error message already printed */
1081 #ifdef IPCONFIG_BOOTP
1085 #ifdef IPCONFIG_RARP
1091 * Send requests and wait, until we get an answer. This loop
1092 * seems to be a terrible waste of CPU time, but actually there is
1093 * only one process running at all, so we don't need to use any
1094 * scheduler functions.
1095 * [Actually we could now, but the nothing else running note still
1098 printk(KERN_NOTICE
"Sending %s%s%s requests .",
1100 ? ((ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP") : "",
1101 (do_bootp
&& do_rarp
) ? " and " : "",
1102 do_rarp
? "RARP" : "");
1104 start_jiffies
= jiffies
;
1106 retries
= CONF_SEND_RETRIES
;
1107 get_random_bytes(&timeout
, sizeof(timeout
));
1108 timeout
= CONF_BASE_TIMEOUT
+ (timeout
% (unsigned) CONF_TIMEOUT_RANDOM
);
1110 #ifdef IPCONFIG_BOOTP
1111 if (do_bootp
&& (d
->able
& IC_BOOTP
))
1112 ic_bootp_send_if(d
, jiffies
- start_jiffies
);
1114 #ifdef IPCONFIG_RARP
1115 if (do_rarp
&& (d
->able
& IC_RARP
))
1119 jiff
= jiffies
+ (d
->next
? CONF_INTER_TIMEOUT
: timeout
);
1120 while (time_before(jiffies
, jiff
) && !ic_got_reply
)
1121 schedule_timeout_uninterruptible(1);
1122 #ifdef IPCONFIG_DHCP
1123 /* DHCP isn't done until we get a DHCPACK. */
1124 if ((ic_got_reply
& IC_BOOTP
)
1125 && (ic_proto_enabled
& IC_USE_DHCP
)
1126 && ic_dhcp_msgtype
!= DHCPACK
)
1132 #endif /* IPCONFIG_DHCP */
1143 printk(" timed out!\n");
1149 timeout
= timeout CONF_TIMEOUT_MULT
;
1150 if (timeout
> CONF_TIMEOUT_MAX
)
1151 timeout
= CONF_TIMEOUT_MAX
;
1156 #ifdef IPCONFIG_BOOTP
1160 #ifdef IPCONFIG_RARP
1165 if (!ic_got_reply
) {
1170 printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
1171 ((ic_got_reply
& IC_RARP
) ? "RARP"
1172 : (ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP"),
1173 NIPQUAD(ic_servaddr
));
1174 printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr
));
1179 #endif /* IPCONFIG_DYNAMIC */
1181 #ifdef CONFIG_PROC_FS
1183 static int pnp_seq_show(struct seq_file
*seq
, void *v
)
1187 if (ic_proto_used
& IC_PROTO
)
1188 seq_printf(seq
, "#PROTO: %s\n",
1189 (ic_proto_used
& IC_RARP
) ? "RARP"
1190 : (ic_proto_used
& IC_USE_DHCP
) ? "DHCP" : "BOOTP");
1192 seq_puts(seq
, "#MANUAL\n");
1196 "domain %s\n", ic_domain
);
1197 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++) {
1198 if (ic_nameservers
[i
] != NONE
)
1200 "nameserver %u.%u.%u.%u\n",
1201 NIPQUAD(ic_nameservers
[i
]));
1203 if (ic_servaddr
!= NONE
)
1205 "bootserver %u.%u.%u.%u\n",
1206 NIPQUAD(ic_servaddr
));
1210 static int pnp_seq_open(struct inode
*indoe
, struct file
*file
)
1212 return single_open(file
, pnp_seq_show
, NULL
);
1215 static const struct file_operations pnp_seq_fops
= {
1216 .owner
= THIS_MODULE
,
1217 .open
= pnp_seq_open
,
1219 .llseek
= seq_lseek
,
1220 .release
= single_release
,
1222 #endif /* CONFIG_PROC_FS */
1225 * Extract IP address from the parameter string if needed. Note that we
1226 * need to have root_server_addr set _before_ IPConfig gets called as it
1229 __be32 __init
root_nfs_parse_addr(char *name
)
1236 while (octets
< 4) {
1237 while (*cp
>= '0' && *cp
<= '9')
1239 if (cp
== cq
|| cp
- cq
> 3)
1241 if (*cp
== '.' || octets
== 3)
1247 if (octets
== 4 && (*cp
== ':' || *cp
== '\0')) {
1250 addr
= in_aton(name
);
1251 memmove(name
, cp
, strlen(cp
) + 1);
1259 * IP Autoconfig dispatcher.
1262 static int __init
ip_auto_config(void)
1266 #ifdef CONFIG_PROC_FS
1267 proc_net_fops_create(&init_net
, "pnp", S_IRUGO
, &pnp_seq_fops
);
1268 #endif /* CONFIG_PROC_FS */
1273 DBG(("IP-Config: Entered.\n"));
1274 #ifdef IPCONFIG_DYNAMIC
1277 /* Give hardware a chance to settle */
1278 msleep(CONF_PRE_OPEN
);
1280 /* Setup all network devices */
1281 if (ic_open_devs() < 0)
1284 /* Give drivers a chance to settle */
1285 ssleep(CONF_POST_OPEN
);
1288 * If the config information is insufficient (e.g., our IP address or
1289 * IP address of the boot server is missing or we have multiple network
1290 * interfaces and no default was set), use BOOTP or RARP to get the
1293 if (ic_myaddr
== NONE
||
1294 #ifdef CONFIG_ROOT_NFS
1295 (root_server_addr
== NONE
1296 && ic_servaddr
== NONE
1297 && ROOT_DEV
== Root_NFS
) ||
1299 ic_first_dev
->next
) {
1300 #ifdef IPCONFIG_DYNAMIC
1302 int retries
= CONF_OPEN_RETRIES
;
1304 if (ic_dynamic() < 0) {
1308 * I don't know why, but sometimes the
1309 * eepro100 driver (at least) gets upset and
1310 * doesn't work the first time it's opened.
1311 * But then if you close it and reopen it, it
1312 * works just fine. So we need to try that at
1313 * least once before giving up.
1315 * Also, if the root will be NFS-mounted, we
1316 * have nowhere to go if DHCP fails. So we
1317 * just have to keep trying forever.
1321 #ifdef CONFIG_ROOT_NFS
1322 if (ROOT_DEV
== Root_NFS
) {
1324 "IP-Config: Retrying forever (NFS root)...\n");
1331 "IP-Config: Reopening network devices...\n");
1335 /* Oh, well. At least we tried. */
1336 printk(KERN_ERR
"IP-Config: Auto-configuration of network failed.\n");
1339 #else /* !DYNAMIC */
1340 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1343 #endif /* IPCONFIG_DYNAMIC */
1345 /* Device selected manually or only one device -> use it */
1346 ic_dev
= ic_first_dev
->dev
;
1349 addr
= root_nfs_parse_addr(root_server_path
);
1350 if (root_server_addr
== NONE
)
1351 root_server_addr
= addr
;
1354 * Use defaults whereever applicable.
1356 if (ic_defaults() < 0)
1360 * Close all network devices except the device we've
1361 * autoconfigured and set up routes.
1364 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1368 * Record which protocol was actually used.
1370 #ifdef IPCONFIG_DYNAMIC
1371 ic_proto_used
= ic_got_reply
| (ic_proto_enabled
& IC_USE_DHCP
);
1374 #ifndef IPCONFIG_SILENT
1376 * Clue in the operator.
1378 printk("IP-Config: Complete:");
1379 printk("\n device=%s", ic_dev
->name
);
1380 printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr
));
1381 printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask
));
1382 printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway
));
1383 printk(",\n host=%s, domain=%s, nis-domain=%s",
1384 utsname()->nodename
, ic_domain
, utsname()->domainname
);
1385 printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr
));
1386 printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr
));
1387 printk(", rootpath=%s", root_server_path
);
1389 #endif /* !SILENT */
1394 late_initcall(ip_auto_config
);
1398 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1399 * command line parameter. It consists of option fields separated by colons in
1400 * the following order:
1402 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO>
1404 * Any of the fields can be empty which means to use a default value:
1405 * <client-ip> - address given by BOOTP or RARP
1406 * <server-ip> - address of host returning BOOTP or RARP packet
1407 * <gw-ip> - none, or the address returned by BOOTP
1408 * <netmask> - automatically determined from <client-ip>, or the
1409 * one returned by BOOTP
1410 * <host name> - <client-ip> in ASCII notation, or the name returned
1412 * <device> - use all available devices
1414 * off|none - don't do autoconfig at all (DEFAULT)
1415 * on|any - use any configured protocol
1416 * dhcp|bootp|rarp - use only the specified protocol
1417 * both - use both BOOTP and RARP (not DHCP)
1419 static int __init
ic_proto_name(char *name
)
1421 if (!strcmp(name
, "on") || !strcmp(name
, "any")) {
1424 #ifdef CONFIG_IP_PNP_DHCP
1425 else if (!strcmp(name
, "dhcp")) {
1426 ic_proto_enabled
&= ~IC_RARP
;
1430 #ifdef CONFIG_IP_PNP_BOOTP
1431 else if (!strcmp(name
, "bootp")) {
1432 ic_proto_enabled
&= ~(IC_RARP
| IC_USE_DHCP
);
1436 #ifdef CONFIG_IP_PNP_RARP
1437 else if (!strcmp(name
, "rarp")) {
1438 ic_proto_enabled
&= ~(IC_BOOTP
| IC_USE_DHCP
);
1442 #ifdef IPCONFIG_DYNAMIC
1443 else if (!strcmp(name
, "both")) {
1444 ic_proto_enabled
&= ~IC_USE_DHCP
; /* backward compat :-( */
1451 static int __init
ip_auto_config_setup(char *addrs
)
1456 ic_set_manually
= 1;
1458 ic_enable
= (*addrs
&&
1459 (strcmp(addrs
, "off") != 0) &&
1460 (strcmp(addrs
, "none") != 0));
1464 if (ic_proto_name(addrs
))
1467 /* Parse the whole string */
1470 if ((cp
= strchr(ip
, ':')))
1472 if (strlen(ip
) > 0) {
1473 DBG(("IP-Config: Parameter #%d: `%s'\n", num
, ip
));
1476 if ((ic_myaddr
= in_aton(ip
)) == INADDR_ANY
)
1480 if ((ic_servaddr
= in_aton(ip
)) == INADDR_ANY
)
1484 if ((ic_gateway
= in_aton(ip
)) == INADDR_ANY
)
1488 if ((ic_netmask
= in_aton(ip
)) == INADDR_ANY
)
1492 if ((dp
= strchr(ip
, '.'))) {
1494 strlcpy(utsname()->domainname
, dp
,
1495 sizeof(utsname()->domainname
));
1497 strlcpy(utsname()->nodename
, ip
,
1498 sizeof(utsname()->nodename
));
1499 ic_host_name_set
= 1;
1502 strlcpy(user_dev_name
, ip
, sizeof(user_dev_name
));
1516 static int __init
nfsaddrs_config_setup(char *addrs
)
1518 return ip_auto_config_setup(addrs
);
1521 __setup("ip=", ip_auto_config_setup
);
1522 __setup("nfsaddrs=", nfsaddrs_config_setup
);