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/config.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/jiffies.h>
39 #include <linux/random.h>
40 #include <linux/init.h>
41 #include <linux/utsname.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
49 #include <linux/socket.h>
50 #include <linux/route.h>
51 #include <linux/udp.h>
52 #include <linux/proc_fs.h>
53 #include <linux/seq_file.h>
54 #include <linux/major.h>
55 #include <linux/root_dev.h>
56 #include <linux/delay.h>
57 #include <linux/nfs_fs.h>
60 #include <net/ipconfig.h>
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 */
105 * Public IP configuration
108 /* This is used by platforms which might be able to set the ipconfig
109 * variables using firmware environment vars. If this is set, it will
110 * ignore such firmware variables.
112 int ic_set_manually __initdata
= 0; /* IPconfig parameters set manually */
114 static int ic_enable __initdata
= 0; /* IP config enabled? */
116 /* Protocol choice */
117 int ic_proto_enabled __initdata
= 0
118 #ifdef IPCONFIG_BOOTP
121 #ifdef CONFIG_IP_PNP_DHCP
129 static int ic_host_name_set __initdata
= 0; /* Host name set by us? */
131 u32 ic_myaddr
= INADDR_NONE
; /* My IP address */
132 static u32 ic_netmask
= INADDR_NONE
; /* Netmask for local subnet */
133 u32 ic_gateway
= INADDR_NONE
; /* Gateway IP address */
135 u32 ic_servaddr
= INADDR_NONE
; /* Boot server IP address */
137 u32 root_server_addr
= INADDR_NONE
; /* Address of NFS server */
138 u8 root_server_path
[256] = { 0, }; /* Path to mount as root */
140 /* Persistent data: */
142 static int ic_proto_used
; /* Protocol used, if any */
143 static u32 ic_nameservers
[CONF_NAMESERVERS_MAX
]; /* DNS Server IP addresses */
144 static u8 ic_domain
[64]; /* DNS (not NIS) domain name */
150 /* Name of user-selected boot device */
151 static char user_dev_name
[IFNAMSIZ
] __initdata
= { 0, };
153 /* Protocols supported by available interfaces */
154 static int ic_proto_have_if __initdata
= 0;
156 #ifdef IPCONFIG_DYNAMIC
157 static DEFINE_SPINLOCK(ic_recv_lock
);
158 static volatile int ic_got_reply __initdata
= 0; /* Proto(s) that replied */
161 static int ic_dhcp_msgtype __initdata
= 0; /* DHCP msg type received */
170 struct ic_device
*next
;
171 struct net_device
*dev
;
172 unsigned short flags
;
177 static struct ic_device
*ic_first_dev __initdata
= NULL
;/* List of open device */
178 static struct net_device
*ic_dev __initdata
= NULL
; /* Selected device */
180 static int __init
ic_open_devs(void)
182 struct ic_device
*d
, **last
;
183 struct net_device
*dev
;
184 unsigned short oflags
;
186 last
= &ic_first_dev
;
189 /* bring loopback device up first */
190 if (dev_change_flags(&loopback_dev
, loopback_dev
.flags
| IFF_UP
) < 0)
191 printk(KERN_ERR
"IP-Config: Failed to open %s\n", loopback_dev
.name
);
193 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
194 if (dev
== &loopback_dev
)
196 if (user_dev_name
[0] ? !strcmp(dev
->name
, user_dev_name
) :
197 (!(dev
->flags
& IFF_LOOPBACK
) &&
198 (dev
->flags
& (IFF_POINTOPOINT
|IFF_BROADCAST
)) &&
199 strncmp(dev
->name
, "dummy", 5))) {
204 printk(KERN_WARNING
"DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev
->name
, dev
->mtu
);
205 if (!(dev
->flags
& IFF_NOARP
))
207 able
&= ic_proto_enabled
;
208 if (ic_proto_enabled
&& !able
)
211 if (dev_change_flags(dev
, oflags
| IFF_UP
) < 0) {
212 printk(KERN_ERR
"IP-Config: Failed to open %s\n", dev
->name
);
215 if (!(d
= kmalloc(sizeof(struct ic_device
), GFP_KERNEL
))) {
225 get_random_bytes(&d
->xid
, sizeof(u32
));
228 ic_proto_have_if
|= able
;
229 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
230 dev
->name
, able
, d
->xid
));
238 if (user_dev_name
[0])
239 printk(KERN_ERR
"IP-Config: Device `%s' not found.\n", user_dev_name
);
241 printk(KERN_ERR
"IP-Config: No network devices available.\n");
247 static void __init
ic_close_devs(void)
249 struct ic_device
*d
, *next
;
250 struct net_device
*dev
;
258 DBG(("IP-Config: Downing %s\n", dev
->name
));
259 dev_change_flags(dev
, d
->flags
);
267 * Interface to various network functions.
271 set_sockaddr(struct sockaddr_in
*sin
, u32 addr
, u16 port
)
273 sin
->sin_family
= AF_INET
;
274 sin
->sin_addr
.s_addr
= addr
;
275 sin
->sin_port
= port
;
278 static int __init
ic_dev_ioctl(unsigned int cmd
, struct ifreq
*arg
)
282 mm_segment_t oldfs
= get_fs();
284 res
= devinet_ioctl(cmd
, (struct ifreq __user
*) arg
);
289 static int __init
ic_route_ioctl(unsigned int cmd
, struct rtentry
*arg
)
293 mm_segment_t oldfs
= get_fs();
295 res
= ip_rt_ioctl(cmd
, (void __user
*) arg
);
301 * Set up interface addresses and routes.
304 static int __init
ic_setup_if(void)
307 struct sockaddr_in
*sin
= (void *) &ir
.ifr_ifru
.ifru_addr
;
310 memset(&ir
, 0, sizeof(ir
));
311 strcpy(ir
.ifr_ifrn
.ifrn_name
, ic_dev
->name
);
312 set_sockaddr(sin
, ic_myaddr
, 0);
313 if ((err
= ic_dev_ioctl(SIOCSIFADDR
, &ir
)) < 0) {
314 printk(KERN_ERR
"IP-Config: Unable to set interface address (%d).\n", err
);
317 set_sockaddr(sin
, ic_netmask
, 0);
318 if ((err
= ic_dev_ioctl(SIOCSIFNETMASK
, &ir
)) < 0) {
319 printk(KERN_ERR
"IP-Config: Unable to set interface netmask (%d).\n", err
);
322 set_sockaddr(sin
, ic_myaddr
| ~ic_netmask
, 0);
323 if ((err
= ic_dev_ioctl(SIOCSIFBRDADDR
, &ir
)) < 0) {
324 printk(KERN_ERR
"IP-Config: Unable to set interface broadcast address (%d).\n", err
);
330 static int __init
ic_setup_routes(void)
332 /* No need to setup device routes, only the default route... */
334 if (ic_gateway
!= INADDR_NONE
) {
338 memset(&rm
, 0, sizeof(rm
));
339 if ((ic_gateway
^ ic_myaddr
) & ic_netmask
) {
340 printk(KERN_ERR
"IP-Config: Gateway not on directly connected network.\n");
343 set_sockaddr((struct sockaddr_in
*) &rm
.rt_dst
, 0, 0);
344 set_sockaddr((struct sockaddr_in
*) &rm
.rt_genmask
, 0, 0);
345 set_sockaddr((struct sockaddr_in
*) &rm
.rt_gateway
, ic_gateway
, 0);
346 rm
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
347 if ((err
= ic_route_ioctl(SIOCADDRT
, &rm
)) < 0) {
348 printk(KERN_ERR
"IP-Config: Cannot add default route (%d).\n", err
);
357 * Fill in default values for all missing parameters.
360 static int __init
ic_defaults(void)
363 * At this point we have no userspace running so need not
364 * claim locks on system_utsname
367 if (!ic_host_name_set
)
368 sprintf(system_utsname
.nodename
, "%u.%u.%u.%u", NIPQUAD(ic_myaddr
));
370 if (root_server_addr
== INADDR_NONE
)
371 root_server_addr
= ic_servaddr
;
373 if (ic_netmask
== INADDR_NONE
) {
374 if (IN_CLASSA(ntohl(ic_myaddr
)))
375 ic_netmask
= htonl(IN_CLASSA_NET
);
376 else if (IN_CLASSB(ntohl(ic_myaddr
)))
377 ic_netmask
= htonl(IN_CLASSB_NET
);
378 else if (IN_CLASSC(ntohl(ic_myaddr
)))
379 ic_netmask
= htonl(IN_CLASSC_NET
);
381 printk(KERN_ERR
"IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
385 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask
));
397 static int ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
399 static struct packet_type rarp_packet_type __initdata
= {
400 .type
= __constant_htons(ETH_P_RARP
),
401 .func
= ic_rarp_recv
,
404 static inline void ic_rarp_init(void)
406 dev_add_pack(&rarp_packet_type
);
409 static inline void ic_rarp_cleanup(void)
411 dev_remove_pack(&rarp_packet_type
);
415 * Process received RARP packet.
418 ic_rarp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
421 unsigned char *rarp_ptr
;
422 unsigned long sip
, tip
;
423 unsigned char *sha
, *tha
; /* s for "source", t for "target" */
426 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
429 if (!pskb_may_pull(skb
, sizeof(struct arphdr
)))
432 /* Basic sanity checks can be done without the lock. */
433 rarp
= (struct arphdr
*)skb
->h
.raw
;
435 /* If this test doesn't pass, it's not IP, or we should
438 if (rarp
->ar_hln
!= dev
->addr_len
|| dev
->type
!= ntohs(rarp
->ar_hrd
))
441 /* If it's not a RARP reply, delete it. */
442 if (rarp
->ar_op
!= htons(ARPOP_RREPLY
))
445 /* If it's not Ethernet, delete it. */
446 if (rarp
->ar_pro
!= htons(ETH_P_IP
))
449 if (!pskb_may_pull(skb
,
450 sizeof(struct arphdr
) +
451 (2 * dev
->addr_len
) +
455 /* OK, it is all there and looks valid, process... */
456 rarp
= (struct arphdr
*)skb
->h
.raw
;
457 rarp_ptr
= (unsigned char *) (rarp
+ 1);
459 /* One reply at a time, please. */
460 spin_lock(&ic_recv_lock
);
462 /* If we already have a reply, just drop the packet */
466 /* Find the ic_device that the packet arrived on */
468 while (d
&& d
->dev
!= dev
)
471 goto drop_unlock
; /* should never happen */
473 /* Extract variable-width fields */
475 rarp_ptr
+= dev
->addr_len
;
476 memcpy(&sip
, rarp_ptr
, 4);
479 rarp_ptr
+= dev
->addr_len
;
480 memcpy(&tip
, rarp_ptr
, 4);
482 /* Discard packets which are not meant for us. */
483 if (memcmp(tha
, dev
->dev_addr
, dev
->addr_len
))
486 /* Discard packets which are not from specified server. */
487 if (ic_servaddr
!= INADDR_NONE
&& ic_servaddr
!= sip
)
490 /* We have a winner! */
492 if (ic_myaddr
== INADDR_NONE
)
495 ic_got_reply
= IC_RARP
;
498 /* Show's over. Nothing to see here. */
499 spin_unlock(&ic_recv_lock
);
502 /* Throw the packet out. */
509 * Send RARP request packet over a single interface.
511 static void __init
ic_rarp_send_if(struct ic_device
*d
)
513 struct net_device
*dev
= d
->dev
;
514 arp_send(ARPOP_RREQUEST
, ETH_P_RARP
, 0, dev
, 0, NULL
,
515 dev
->dev_addr
, dev
->dev_addr
);
520 * DHCP/BOOTP support.
523 #ifdef IPCONFIG_BOOTP
525 struct bootp_pkt
{ /* BOOTP packet format */
526 struct iphdr iph
; /* IP header */
527 struct udphdr udph
; /* UDP header */
528 u8 op
; /* 1=request, 2=reply */
529 u8 htype
; /* HW address type */
530 u8 hlen
; /* HW address length */
531 u8 hops
; /* Used only by gateways */
532 u32 xid
; /* Transaction ID */
533 u16 secs
; /* Seconds since we started */
534 u16 flags
; /* Just what it says */
535 u32 client_ip
; /* Client's IP address if known */
536 u32 your_ip
; /* Assigned IP address */
537 u32 server_ip
; /* (Next, e.g. NFS) Server's IP address */
538 u32 relay_ip
; /* IP address of BOOTP relay */
539 u8 hw_addr
[16]; /* Client's HW address */
540 u8 serv_name
[64]; /* Server host name */
541 u8 boot_file
[128]; /* Name of boot file */
542 u8 exten
[312]; /* DHCP options / BOOTP vendor extensions */
546 #define BOOTP_REQUEST 1
547 #define BOOTP_REPLY 2
549 /* DHCP message types */
550 #define DHCPDISCOVER 1
552 #define DHCPREQUEST 3
553 #define DHCPDECLINE 4
556 #define DHCPRELEASE 7
559 static int ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
);
561 static struct packet_type bootp_packet_type __initdata
= {
562 .type
= __constant_htons(ETH_P_IP
),
563 .func
= ic_bootp_recv
,
568 * Initialize DHCP/BOOTP extension fields in the request.
571 static const u8 ic_bootp_cookie
[4] = { 99, 130, 83, 99 };
576 ic_dhcp_init_options(u8
*options
)
578 u8 mt
= ((ic_servaddr
== INADDR_NONE
)
579 ? DHCPDISCOVER
: DHCPREQUEST
);
582 #ifdef IPCONFIG_DEBUG
583 printk("DHCP: Sending message type %d\n", mt
);
586 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
589 *e
++ = 53; /* DHCP message type */
593 if (mt
== DHCPREQUEST
) {
594 *e
++ = 54; /* Server ID (IP address) */
596 memcpy(e
, &ic_servaddr
, 4);
599 *e
++ = 50; /* Requested IP address */
601 memcpy(e
, &ic_myaddr
, 4);
607 static const u8 ic_req_params
[] = {
609 3, /* Default gateway */
612 15, /* Domain name */
614 40, /* NIS domain name */
617 *e
++ = 55; /* Parameter request list */
618 *e
++ = sizeof(ic_req_params
);
619 memcpy(e
, ic_req_params
, sizeof(ic_req_params
));
620 e
+= sizeof(ic_req_params
);
623 *e
++ = 255; /* End of the list */
626 #endif /* IPCONFIG_DHCP */
628 static void __init
ic_bootp_init_ext(u8
*e
)
630 memcpy(e
, ic_bootp_cookie
, 4); /* RFC1048 Magic Cookie */
632 *e
++ = 1; /* Subnet mask request */
635 *e
++ = 3; /* Default gateway request */
638 *e
++ = 5; /* Name server request */
641 *e
++ = 12; /* Host name request */
644 *e
++ = 40; /* NIS Domain name request */
647 *e
++ = 17; /* Boot path */
651 *e
++ = 57; /* set extension buffer size for reply */
653 *e
++ = 1; /* 128+236+8+20+14, see dhcpd sources */
656 *e
++ = 255; /* End of the list */
661 * Initialize the DHCP/BOOTP mechanism.
663 static inline void ic_bootp_init(void)
667 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++)
668 ic_nameservers
[i
] = INADDR_NONE
;
670 dev_add_pack(&bootp_packet_type
);
675 * DHCP/BOOTP cleanup.
677 static inline void ic_bootp_cleanup(void)
679 dev_remove_pack(&bootp_packet_type
);
684 * Send DHCP/BOOTP request to single interface.
686 static void __init
ic_bootp_send_if(struct ic_device
*d
, unsigned long jiffies_diff
)
688 struct net_device
*dev
= d
->dev
;
691 int hh_len
= LL_RESERVED_SPACE(dev
);
694 /* Allocate packet */
695 skb
= alloc_skb(sizeof(struct bootp_pkt
) + hh_len
+ 15, GFP_KERNEL
);
698 skb_reserve(skb
, hh_len
);
699 b
= (struct bootp_pkt
*) skb_put(skb
, sizeof(struct bootp_pkt
));
700 memset(b
, 0, sizeof(struct bootp_pkt
));
702 /* Construct IP header */
703 skb
->nh
.iph
= h
= &b
->iph
;
706 h
->tot_len
= htons(sizeof(struct bootp_pkt
));
707 h
->frag_off
= htons(IP_DF
);
709 h
->protocol
= IPPROTO_UDP
;
710 h
->daddr
= INADDR_BROADCAST
;
711 h
->check
= ip_fast_csum((unsigned char *) h
, h
->ihl
);
713 /* Construct UDP header */
714 b
->udph
.source
= htons(68);
715 b
->udph
.dest
= htons(67);
716 b
->udph
.len
= htons(sizeof(struct bootp_pkt
) - sizeof(struct iphdr
));
717 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
719 /* Construct DHCP/BOOTP header */
720 b
->op
= BOOTP_REQUEST
;
721 if (dev
->type
< 256) /* check for false types */
722 b
->htype
= dev
->type
;
723 else if (dev
->type
== ARPHRD_IEEE802_TR
) /* fix for token ring */
724 b
->htype
= ARPHRD_IEEE802
;
725 else if (dev
->type
== ARPHRD_FDDI
)
726 b
->htype
= ARPHRD_ETHER
;
728 printk("Unknown ARP type 0x%04x for device %s\n", dev
->type
, dev
->name
);
729 b
->htype
= dev
->type
; /* can cause undefined behavior */
731 b
->hlen
= dev
->addr_len
;
732 b
->your_ip
= INADDR_NONE
;
733 b
->server_ip
= INADDR_NONE
;
734 memcpy(b
->hw_addr
, dev
->dev_addr
, dev
->addr_len
);
735 b
->secs
= htons(jiffies_diff
/ HZ
);
738 /* add DHCP options or BOOTP extensions */
740 if (ic_proto_enabled
& IC_USE_DHCP
)
741 ic_dhcp_init_options(b
->exten
);
744 ic_bootp_init_ext(b
->exten
);
746 /* Chain packet down the line... */
748 skb
->protocol
= htons(ETH_P_IP
);
749 if ((dev
->hard_header
&&
750 dev
->hard_header(skb
, dev
, ntohs(skb
->protocol
), dev
->broadcast
, dev
->dev_addr
, skb
->len
) < 0) ||
751 dev_queue_xmit(skb
) < 0)
757 * Copy BOOTP-supplied string if not already set.
759 static int __init
ic_bootp_string(char *dest
, char *src
, int len
, int max
)
765 memcpy(dest
, src
, len
);
772 * Process BOOTP extensions.
774 static void __init
ic_do_bootp_ext(u8
*ext
)
779 #ifdef IPCONFIG_DEBUG
782 printk("DHCP/BOOTP: Got extension %d:",*ext
);
783 for(c
=ext
+2; c
<ext
+2+ext
[1]; c
++)
789 case 1: /* Subnet mask */
790 if (ic_netmask
== INADDR_NONE
)
791 memcpy(&ic_netmask
, ext
+1, 4);
793 case 3: /* Default gateway */
794 if (ic_gateway
== INADDR_NONE
)
795 memcpy(&ic_gateway
, ext
+1, 4);
797 case 6: /* DNS server */
799 if (servers
> CONF_NAMESERVERS_MAX
)
800 servers
= CONF_NAMESERVERS_MAX
;
801 for (i
= 0; i
< servers
; i
++) {
802 if (ic_nameservers
[i
] == INADDR_NONE
)
803 memcpy(&ic_nameservers
[i
], ext
+1+4*i
, 4);
806 case 12: /* Host name */
807 ic_bootp_string(system_utsname
.nodename
, ext
+1, *ext
, __NEW_UTS_LEN
);
808 ic_host_name_set
= 1;
810 case 15: /* Domain name (DNS) */
811 ic_bootp_string(ic_domain
, ext
+1, *ext
, sizeof(ic_domain
));
813 case 17: /* Root path */
814 if (!root_server_path
[0])
815 ic_bootp_string(root_server_path
, ext
+1, *ext
, sizeof(root_server_path
));
817 case 40: /* NIS Domain name (_not_ DNS) */
818 ic_bootp_string(system_utsname
.domainname
, ext
+1, *ext
, __NEW_UTS_LEN
);
825 * Receive BOOTP reply.
827 static int __init
ic_bootp_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
834 /* Perform verifications before taking the lock. */
835 if (skb
->pkt_type
== PACKET_OTHERHOST
)
838 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
841 if (!pskb_may_pull(skb
,
842 sizeof(struct iphdr
) +
843 sizeof(struct udphdr
)))
846 b
= (struct bootp_pkt
*) skb
->nh
.iph
;
849 if (h
->ihl
!= 5 || h
->version
!= 4 || h
->protocol
!= IPPROTO_UDP
)
852 /* Fragments are not supported */
853 if (h
->frag_off
& htons(IP_OFFSET
| IP_MF
)) {
855 printk(KERN_ERR
"DHCP/BOOTP: Ignoring fragmented "
860 if (skb
->len
< ntohs(h
->tot_len
))
863 if (ip_fast_csum((char *) h
, h
->ihl
))
866 if (b
->udph
.source
!= htons(67) || b
->udph
.dest
!= htons(68))
869 if (ntohs(h
->tot_len
) < ntohs(b
->udph
.len
) + sizeof(struct iphdr
))
872 len
= ntohs(b
->udph
.len
) - sizeof(struct udphdr
);
873 ext_len
= len
- (sizeof(*b
) -
874 sizeof(struct iphdr
) -
875 sizeof(struct udphdr
) -
880 /* Ok the front looks good, make sure we can get at the rest. */
881 if (!pskb_may_pull(skb
, skb
->len
))
884 b
= (struct bootp_pkt
*) skb
->nh
.iph
;
887 /* One reply at a time, please. */
888 spin_lock(&ic_recv_lock
);
890 /* If we already have a reply, just drop the packet */
894 /* Find the ic_device that the packet arrived on */
896 while (d
&& d
->dev
!= dev
)
899 goto drop_unlock
; /* should never happen */
901 /* Is it a reply to our BOOTP request? */
902 if (b
->op
!= BOOTP_REPLY
||
905 printk(KERN_ERR
"DHCP/BOOTP: Reply not for us, "
911 /* Parse extensions */
913 !memcmp(b
->exten
, ic_bootp_cookie
, 4)) { /* Check magic cookie */
914 u8
*end
= (u8
*) b
+ ntohs(b
->iph
.tot_len
);
918 if (ic_proto_enabled
& IC_USE_DHCP
) {
919 u32 server_id
= INADDR_NONE
;
923 while (ext
< end
&& *ext
!= 0xff) {
925 if (*opt
== 0) /* Padding */
931 case 53: /* Message type */
935 case 54: /* Server ID (IP address) */
937 memcpy(&server_id
, opt
+ 2, 4);
942 #ifdef IPCONFIG_DEBUG
943 printk("DHCP: Got message type %d\n", mt
);
948 /* While in the process of accepting one offer,
951 if (ic_myaddr
!= INADDR_NONE
)
954 /* Let's accept that offer. */
955 ic_myaddr
= b
->your_ip
;
956 ic_servaddr
= server_id
;
957 #ifdef IPCONFIG_DEBUG
958 printk("DHCP: Offered address %u.%u.%u.%u",
960 printk(" by server %u.%u.%u.%u\n",
961 NIPQUAD(ic_servaddr
));
963 /* The DHCP indicated server address takes
964 * precedence over the bootp header one if
965 * they are different.
967 if ((server_id
!= INADDR_NONE
) &&
968 (b
->server_ip
!= server_id
))
969 b
->server_ip
= ic_servaddr
;
973 if (memcmp(dev
->dev_addr
, b
->hw_addr
, dev
->addr_len
) != 0)
980 /* Urque. Forget it*/
981 ic_myaddr
= INADDR_NONE
;
982 ic_servaddr
= INADDR_NONE
;
986 ic_dhcp_msgtype
= mt
;
989 #endif /* IPCONFIG_DHCP */
992 while (ext
< end
&& *ext
!= 0xff) {
994 if (*opt
== 0) /* Padding */
998 ic_do_bootp_ext(opt
);
1002 /* We have a winner! */
1004 ic_myaddr
= b
->your_ip
;
1005 ic_servaddr
= b
->server_ip
;
1006 if (ic_gateway
== INADDR_NONE
&& b
->relay_ip
)
1007 ic_gateway
= b
->relay_ip
;
1008 if (ic_nameservers
[0] == INADDR_NONE
)
1009 ic_nameservers
[0] = ic_servaddr
;
1010 ic_got_reply
= IC_BOOTP
;
1013 /* Show's over. Nothing to see here. */
1014 spin_unlock(&ic_recv_lock
);
1017 /* Throw the packet out. */
1028 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1031 #ifdef IPCONFIG_DYNAMIC
1033 static int __init
ic_dynamic(void)
1036 struct ic_device
*d
;
1037 unsigned long start_jiffies
, timeout
, jiff
;
1038 int do_bootp
= ic_proto_have_if
& IC_BOOTP
;
1039 int do_rarp
= ic_proto_have_if
& IC_RARP
;
1042 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1043 * This routine gets only called when some pieces of information
1044 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1046 if (!ic_proto_enabled
) {
1047 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1051 #ifdef IPCONFIG_BOOTP
1052 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_BOOTP
)
1053 printk(KERN_ERR
"DHCP/BOOTP: No suitable device found.\n");
1055 #ifdef IPCONFIG_RARP
1056 if ((ic_proto_enabled
^ ic_proto_have_if
) & IC_RARP
)
1057 printk(KERN_ERR
"RARP: No suitable device found.\n");
1060 if (!ic_proto_have_if
)
1061 /* Error message already printed */
1067 #ifdef IPCONFIG_BOOTP
1071 #ifdef IPCONFIG_RARP
1077 * Send requests and wait, until we get an answer. This loop
1078 * seems to be a terrible waste of CPU time, but actually there is
1079 * only one process running at all, so we don't need to use any
1080 * scheduler functions.
1081 * [Actually we could now, but the nothing else running note still
1084 printk(KERN_NOTICE
"Sending %s%s%s requests .",
1086 ? ((ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP") : "",
1087 (do_bootp
&& do_rarp
) ? " and " : "",
1088 do_rarp
? "RARP" : "");
1090 start_jiffies
= jiffies
;
1092 retries
= CONF_SEND_RETRIES
;
1093 get_random_bytes(&timeout
, sizeof(timeout
));
1094 timeout
= CONF_BASE_TIMEOUT
+ (timeout
% (unsigned) CONF_TIMEOUT_RANDOM
);
1096 #ifdef IPCONFIG_BOOTP
1097 if (do_bootp
&& (d
->able
& IC_BOOTP
))
1098 ic_bootp_send_if(d
, jiffies
- start_jiffies
);
1100 #ifdef IPCONFIG_RARP
1101 if (do_rarp
&& (d
->able
& IC_RARP
))
1105 jiff
= jiffies
+ (d
->next
? CONF_INTER_TIMEOUT
: timeout
);
1106 while (time_before(jiffies
, jiff
) && !ic_got_reply
)
1107 schedule_timeout_uninterruptible(1);
1108 #ifdef IPCONFIG_DHCP
1109 /* DHCP isn't done until we get a DHCPACK. */
1110 if ((ic_got_reply
& IC_BOOTP
)
1111 && (ic_proto_enabled
& IC_USE_DHCP
)
1112 && ic_dhcp_msgtype
!= DHCPACK
)
1118 #endif /* IPCONFIG_DHCP */
1129 printk(" timed out!\n");
1135 timeout
= timeout CONF_TIMEOUT_MULT
;
1136 if (timeout
> CONF_TIMEOUT_MAX
)
1137 timeout
= CONF_TIMEOUT_MAX
;
1142 #ifdef IPCONFIG_BOOTP
1146 #ifdef IPCONFIG_RARP
1151 if (!ic_got_reply
) {
1152 ic_myaddr
= INADDR_NONE
;
1156 printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
1157 ((ic_got_reply
& IC_RARP
) ? "RARP"
1158 : (ic_proto_enabled
& IC_USE_DHCP
) ? "DHCP" : "BOOTP"),
1159 NIPQUAD(ic_servaddr
));
1160 printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr
));
1165 #endif /* IPCONFIG_DYNAMIC */
1167 #ifdef CONFIG_PROC_FS
1169 static int pnp_seq_show(struct seq_file
*seq
, void *v
)
1173 if (ic_proto_used
& IC_PROTO
)
1174 seq_printf(seq
, "#PROTO: %s\n",
1175 (ic_proto_used
& IC_RARP
) ? "RARP"
1176 : (ic_proto_used
& IC_USE_DHCP
) ? "DHCP" : "BOOTP");
1178 seq_puts(seq
, "#MANUAL\n");
1182 "domain %s\n", ic_domain
);
1183 for (i
= 0; i
< CONF_NAMESERVERS_MAX
; i
++) {
1184 if (ic_nameservers
[i
] != INADDR_NONE
)
1186 "nameserver %u.%u.%u.%u\n",
1187 NIPQUAD(ic_nameservers
[i
]));
1189 if (ic_servaddr
!= INADDR_NONE
)
1191 "bootserver %u.%u.%u.%u\n",
1192 NIPQUAD(ic_servaddr
));
1196 static int pnp_seq_open(struct inode
*indoe
, struct file
*file
)
1198 return single_open(file
, pnp_seq_show
, NULL
);
1201 static struct file_operations pnp_seq_fops
= {
1202 .owner
= THIS_MODULE
,
1203 .open
= pnp_seq_open
,
1205 .llseek
= seq_lseek
,
1206 .release
= single_release
,
1208 #endif /* CONFIG_PROC_FS */
1211 * Extract IP address from the parameter string if needed. Note that we
1212 * need to have root_server_addr set _before_ IPConfig gets called as it
1215 u32 __init
root_nfs_parse_addr(char *name
)
1222 while (octets
< 4) {
1223 while (*cp
>= '0' && *cp
<= '9')
1225 if (cp
== cq
|| cp
- cq
> 3)
1227 if (*cp
== '.' || octets
== 3)
1233 if (octets
== 4 && (*cp
== ':' || *cp
== '\0')) {
1236 addr
= in_aton(name
);
1237 memmove(name
, cp
, strlen(cp
) + 1);
1245 * IP Autoconfig dispatcher.
1248 static int __init
ip_auto_config(void)
1252 #ifdef CONFIG_PROC_FS
1253 proc_net_fops_create("pnp", S_IRUGO
, &pnp_seq_fops
);
1254 #endif /* CONFIG_PROC_FS */
1259 DBG(("IP-Config: Entered.\n"));
1260 #ifdef IPCONFIG_DYNAMIC
1263 /* Give hardware a chance to settle */
1264 msleep(CONF_PRE_OPEN
);
1266 /* Setup all network devices */
1267 if (ic_open_devs() < 0)
1270 /* Give drivers a chance to settle */
1271 ssleep(CONF_POST_OPEN
);
1274 * If the config information is insufficient (e.g., our IP address or
1275 * IP address of the boot server is missing or we have multiple network
1276 * interfaces and no default was set), use BOOTP or RARP to get the
1279 if (ic_myaddr
== INADDR_NONE
||
1280 #ifdef CONFIG_ROOT_NFS
1281 (MAJOR(ROOT_DEV
) == UNNAMED_MAJOR
1282 && root_server_addr
== INADDR_NONE
1283 && ic_servaddr
== INADDR_NONE
) ||
1285 ic_first_dev
->next
) {
1286 #ifdef IPCONFIG_DYNAMIC
1288 int retries
= CONF_OPEN_RETRIES
;
1290 if (ic_dynamic() < 0) {
1294 * I don't know why, but sometimes the
1295 * eepro100 driver (at least) gets upset and
1296 * doesn't work the first time it's opened.
1297 * But then if you close it and reopen it, it
1298 * works just fine. So we need to try that at
1299 * least once before giving up.
1301 * Also, if the root will be NFS-mounted, we
1302 * have nowhere to go if DHCP fails. So we
1303 * just have to keep trying forever.
1307 #ifdef CONFIG_ROOT_NFS
1308 if (ROOT_DEV
== Root_NFS
) {
1310 "IP-Config: Retrying forever (NFS root)...\n");
1317 "IP-Config: Reopening network devices...\n");
1321 /* Oh, well. At least we tried. */
1322 printk(KERN_ERR
"IP-Config: Auto-configuration of network failed.\n");
1325 #else /* !DYNAMIC */
1326 printk(KERN_ERR
"IP-Config: Incomplete network configuration information.\n");
1329 #endif /* IPCONFIG_DYNAMIC */
1331 /* Device selected manually or only one device -> use it */
1332 ic_dev
= ic_first_dev
->dev
;
1335 addr
= root_nfs_parse_addr(root_server_path
);
1336 if (root_server_addr
== INADDR_NONE
)
1337 root_server_addr
= addr
;
1340 * Use defaults whereever applicable.
1342 if (ic_defaults() < 0)
1346 * Close all network devices except the device we've
1347 * autoconfigured and set up routes.
1350 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1354 * Record which protocol was actually used.
1356 #ifdef IPCONFIG_DYNAMIC
1357 ic_proto_used
= ic_got_reply
| (ic_proto_enabled
& IC_USE_DHCP
);
1360 #ifndef IPCONFIG_SILENT
1362 * Clue in the operator.
1364 printk("IP-Config: Complete:");
1365 printk("\n device=%s", ic_dev
->name
);
1366 printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr
));
1367 printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask
));
1368 printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway
));
1369 printk(",\n host=%s, domain=%s, nis-domain=%s",
1370 system_utsname
.nodename
, ic_domain
, system_utsname
.domainname
);
1371 printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr
));
1372 printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr
));
1373 printk(", rootpath=%s", root_server_path
);
1375 #endif /* !SILENT */
1380 late_initcall(ip_auto_config
);
1384 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
1385 * command line parameter. It consists of option fields separated by colons in
1386 * the following order:
1388 * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO>
1390 * Any of the fields can be empty which means to use a default value:
1391 * <client-ip> - address given by BOOTP or RARP
1392 * <server-ip> - address of host returning BOOTP or RARP packet
1393 * <gw-ip> - none, or the address returned by BOOTP
1394 * <netmask> - automatically determined from <client-ip>, or the
1395 * one returned by BOOTP
1396 * <host name> - <client-ip> in ASCII notation, or the name returned
1398 * <device> - use all available devices
1400 * off|none - don't do autoconfig at all (DEFAULT)
1401 * on|any - use any configured protocol
1402 * dhcp|bootp|rarp - use only the specified protocol
1403 * both - use both BOOTP and RARP (not DHCP)
1405 static int __init
ic_proto_name(char *name
)
1407 if (!strcmp(name
, "on") || !strcmp(name
, "any")) {
1410 #ifdef CONFIG_IP_PNP_DHCP
1411 else if (!strcmp(name
, "dhcp")) {
1412 ic_proto_enabled
&= ~IC_RARP
;
1416 #ifdef CONFIG_IP_PNP_BOOTP
1417 else if (!strcmp(name
, "bootp")) {
1418 ic_proto_enabled
&= ~(IC_RARP
| IC_USE_DHCP
);
1422 #ifdef CONFIG_IP_PNP_RARP
1423 else if (!strcmp(name
, "rarp")) {
1424 ic_proto_enabled
&= ~(IC_BOOTP
| IC_USE_DHCP
);
1428 #ifdef IPCONFIG_DYNAMIC
1429 else if (!strcmp(name
, "both")) {
1430 ic_proto_enabled
&= ~IC_USE_DHCP
; /* backward compat :-( */
1437 static int __init
ip_auto_config_setup(char *addrs
)
1442 ic_set_manually
= 1;
1444 ic_enable
= (*addrs
&&
1445 (strcmp(addrs
, "off") != 0) &&
1446 (strcmp(addrs
, "none") != 0));
1450 if (ic_proto_name(addrs
))
1453 /* Parse the whole string */
1456 if ((cp
= strchr(ip
, ':')))
1458 if (strlen(ip
) > 0) {
1459 DBG(("IP-Config: Parameter #%d: `%s'\n", num
, ip
));
1462 if ((ic_myaddr
= in_aton(ip
)) == INADDR_ANY
)
1463 ic_myaddr
= INADDR_NONE
;
1466 if ((ic_servaddr
= in_aton(ip
)) == INADDR_ANY
)
1467 ic_servaddr
= INADDR_NONE
;
1470 if ((ic_gateway
= in_aton(ip
)) == INADDR_ANY
)
1471 ic_gateway
= INADDR_NONE
;
1474 if ((ic_netmask
= in_aton(ip
)) == INADDR_ANY
)
1475 ic_netmask
= INADDR_NONE
;
1478 if ((dp
= strchr(ip
, '.'))) {
1480 strlcpy(system_utsname
.domainname
, dp
,
1481 sizeof(system_utsname
.domainname
));
1483 strlcpy(system_utsname
.nodename
, ip
,
1484 sizeof(system_utsname
.nodename
));
1485 ic_host_name_set
= 1;
1488 strlcpy(user_dev_name
, ip
, sizeof(user_dev_name
));
1502 static int __init
nfsaddrs_config_setup(char *addrs
)
1504 return ip_auto_config_setup(addrs
);
1507 __setup("ip=", ip_auto_config_setup
);
1508 __setup("nfsaddrs=", nfsaddrs_config_setup
);