blob: b8f7763b2261474e4e5d35961193bcdcbebf26cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: ipconfig.c,v 1.46 2002/02/01 22:01:04 davem Exp $
3 *
4 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
5 * user-supplied information to configure own IP address and routes.
6 *
7 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 *
9 * Derived from network configuration code in fs/nfs/nfsroot.c,
10 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
11 *
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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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
19 *
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
23 *
24 * Ported DHCP support from 2.2.16 to 2.4.0-test4
25 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
26 *
27 * Merged changes from 2.2.19 into 2.4.3
28 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
29 *
30 * Multiple Nameservers in /proc/net/pnp
31 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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>
41#include <linux/in.h>
42#include <linux/if.h>
43#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020044#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/netdevice.h>
46#include <linux/if_arp.h>
47#include <linux/skbuff.h>
48#include <linux/ip.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>
Adrian Bunk43d60662005-09-05 18:05:52 -070057#include <linux/nfs_fs.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020058#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/arp.h>
60#include <net/ip.h>
61#include <net/ipconfig.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020062#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <asm/uaccess.h>
65#include <net/checksum.h>
66#include <asm/processor.h>
67
68/* Define this to allow debugging output */
69#undef IPCONFIG_DEBUG
70
71#ifdef IPCONFIG_DEBUG
72#define DBG(x) printk x
73#else
74#define DBG(x) do { } while(0)
75#endif
76
77#if defined(CONFIG_IP_PNP_DHCP)
78#define IPCONFIG_DHCP
79#endif
80#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
81#define IPCONFIG_BOOTP
82#endif
83#if defined(CONFIG_IP_PNP_RARP)
84#define IPCONFIG_RARP
85#endif
86#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
87#define IPCONFIG_DYNAMIC
88#endif
89
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 */
93
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 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900102#define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
103 - '3' from resolv.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Al Viro5a874db2006-11-08 00:19:38 -0800105#define NONE __constant_htonl(INADDR_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * Public IP configuration
109 */
110
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.
114 */
115int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
116
117static int ic_enable __initdata = 0; /* IP config enabled? */
118
119/* Protocol choice */
120int ic_proto_enabled __initdata = 0
121#ifdef IPCONFIG_BOOTP
122 | IC_BOOTP
123#endif
124#ifdef CONFIG_IP_PNP_DHCP
125 | IC_USE_DHCP
126#endif
127#ifdef IPCONFIG_RARP
128 | IC_RARP
129#endif
130 ;
131
132static int ic_host_name_set __initdata = 0; /* Host name set by us? */
133
Al Viro5a874db2006-11-08 00:19:38 -0800134__be32 ic_myaddr = NONE; /* My IP address */
135static __be32 ic_netmask = NONE; /* Netmask for local subnet */
136__be32 ic_gateway = NONE; /* Gateway IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Al Viro5a874db2006-11-08 00:19:38 -0800138__be32 ic_servaddr = NONE; /* Boot server IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Al Viro5a874db2006-11-08 00:19:38 -0800140__be32 root_server_addr = NONE; /* Address of NFS server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141u8 root_server_path[256] = { 0, }; /* Path to mount as root */
142
143/* Persistent data: */
144
145static int ic_proto_used; /* Protocol used, if any */
Al Viro5a874db2006-11-08 00:19:38 -0800146static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static u8 ic_domain[64]; /* DNS (not NIS) domain name */
148
149/*
150 * Private state.
151 */
152
153/* Name of user-selected boot device */
154static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
155
156/* Protocols supported by available interfaces */
157static int ic_proto_have_if __initdata = 0;
158
159#ifdef IPCONFIG_DYNAMIC
160static DEFINE_SPINLOCK(ic_recv_lock);
161static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */
162#endif
163#ifdef IPCONFIG_DHCP
164static int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */
165#endif
166
167
168/*
169 * Network devices
170 */
171
172struct ic_device {
173 struct ic_device *next;
174 struct net_device *dev;
175 unsigned short flags;
176 short able;
Al Viro5a874db2006-11-08 00:19:38 -0800177 __be32 xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
181static struct net_device *ic_dev __initdata = NULL; /* Selected device */
182
183static int __init ic_open_devs(void)
184{
185 struct ic_device *d, **last;
186 struct net_device *dev;
187 unsigned short oflags;
188
189 last = &ic_first_dev;
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800190 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* bring loopback device up first */
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700193 for_each_netdev(&init_net, dev) {
194 if (!(dev->flags & IFF_LOOPBACK))
195 continue;
196 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
197 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric W. Biederman881d9662007-09-17 11:56:21 -0700200 for_each_netdev(&init_net, dev) {
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700201 if (dev->flags & IFF_LOOPBACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 continue;
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))) {
207 int able = 0;
208 if (dev->mtu >= 364)
209 able |= IC_BOOTP;
210 else
211 printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
212 if (!(dev->flags & IFF_NOARP))
213 able |= IC_RARP;
214 able &= ic_proto_enabled;
215 if (ic_proto_enabled && !able)
216 continue;
217 oflags = dev->flags;
218 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
219 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
220 continue;
221 }
222 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800223 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -1;
225 }
226 d->dev = dev;
227 *last = d;
228 last = &d->next;
229 d->flags = oflags;
230 d->able = able;
231 if (able & IC_BOOTP)
Al Viro5a874db2006-11-08 00:19:38 -0800232 get_random_bytes(&d->xid, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 else
234 d->xid = 0;
235 ic_proto_have_if |= able;
236 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
237 dev->name, able, d->xid));
238 }
239 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800240 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 *last = NULL;
243
244 if (!ic_first_dev) {
245 if (user_dev_name[0])
246 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
247 else
248 printk(KERN_ERR "IP-Config: No network devices available.\n");
249 return -1;
250 }
251 return 0;
252}
253
254static void __init ic_close_devs(void)
255{
256 struct ic_device *d, *next;
257 struct net_device *dev;
258
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800259 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 next = ic_first_dev;
261 while ((d = next)) {
262 next = d->next;
263 dev = d->dev;
264 if (dev != ic_dev) {
265 DBG(("IP-Config: Downing %s\n", dev->name));
266 dev_change_flags(dev, d->flags);
267 }
268 kfree(d);
269 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800270 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * Interface to various network functions.
275 */
276
277static inline void
Al Viro5a874db2006-11-08 00:19:38 -0800278set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 sin->sin_family = AF_INET;
281 sin->sin_addr.s_addr = addr;
282 sin->sin_port = port;
283}
284
285static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
286{
287 int res;
288
289 mm_segment_t oldfs = get_fs();
290 set_fs(get_ds());
291 res = devinet_ioctl(cmd, (struct ifreq __user *) arg);
292 set_fs(oldfs);
293 return res;
294}
295
296static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
297{
298 int res;
299
300 mm_segment_t oldfs = get_fs();
301 set_fs(get_ds());
302 res = ip_rt_ioctl(cmd, (void __user *) arg);
303 set_fs(oldfs);
304 return res;
305}
306
307/*
308 * Set up interface addresses and routes.
309 */
310
311static int __init ic_setup_if(void)
312{
313 struct ifreq ir;
314 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
315 int err;
316
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);
322 return -1;
323 }
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);
327 return -1;
328 }
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);
332 return -1;
333 }
334 return 0;
335}
336
337static int __init ic_setup_routes(void)
338{
339 /* No need to setup device routes, only the default route... */
340
Al Viro5a874db2006-11-08 00:19:38 -0800341 if (ic_gateway != NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct rtentry rm;
343 int err;
344
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");
348 return -1;
349 }
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);
356 return -1;
357 }
358 }
359
360 return 0;
361}
362
363/*
364 * Fill in default values for all missing parameters.
365 */
366
367static int __init ic_defaults(void)
368{
369 /*
370 * At this point we have no userspace running so need not
371 * claim locks on system_utsname
372 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (!ic_host_name_set)
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700375 sprintf(init_utsname()->nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Al Viro5a874db2006-11-08 00:19:38 -0800377 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 root_server_addr = ic_servaddr;
379
Al Viro5a874db2006-11-08 00:19:38 -0800380 if (ic_netmask == NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 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);
387 else {
388 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
389 NIPQUAD(ic_myaddr));
390 return -1;
391 }
392 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));
393 }
394
395 return 0;
396}
397
398/*
399 * RARP support.
400 */
401
402#ifdef IPCONFIG_RARP
403
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700404static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406static struct packet_type rarp_packet_type __initdata = {
407 .type = __constant_htons(ETH_P_RARP),
408 .func = ic_rarp_recv,
409};
410
411static inline void ic_rarp_init(void)
412{
413 dev_add_pack(&rarp_packet_type);
414}
415
416static inline void ic_rarp_cleanup(void)
417{
418 dev_remove_pack(&rarp_packet_type);
419}
420
421/*
422 * Process received RARP packet.
423 */
424static int __init
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700425ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct arphdr *rarp;
428 unsigned char *rarp_ptr;
Al Viro5a874db2006-11-08 00:19:38 -0800429 __be32 sip, tip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned char *sha, *tha; /* s for "source", t for "target" */
431 struct ic_device *d;
432
Eric W. Biedermane730c152007-09-17 11:53:39 -0700433 if (dev->nd_net != &init_net)
434 goto drop;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
437 return NET_RX_DROP;
438
439 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
440 goto drop;
441
442 /* Basic sanity checks can be done without the lock. */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700443 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* If this test doesn't pass, it's not IP, or we should
446 * ignore it anyway.
447 */
448 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
449 goto drop;
450
451 /* If it's not a RARP reply, delete it. */
452 if (rarp->ar_op != htons(ARPOP_RREPLY))
453 goto drop;
454
455 /* If it's not Ethernet, delete it. */
456 if (rarp->ar_pro != htons(ETH_P_IP))
457 goto drop;
458
459 if (!pskb_may_pull(skb,
460 sizeof(struct arphdr) +
461 (2 * dev->addr_len) +
462 (2 * 4)))
463 goto drop;
464
465 /* OK, it is all there and looks valid, process... */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700466 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 rarp_ptr = (unsigned char *) (rarp + 1);
468
469 /* One reply at a time, please. */
470 spin_lock(&ic_recv_lock);
471
472 /* If we already have a reply, just drop the packet */
473 if (ic_got_reply)
474 goto drop_unlock;
475
476 /* Find the ic_device that the packet arrived on */
477 d = ic_first_dev;
478 while (d && d->dev != dev)
479 d = d->next;
480 if (!d)
481 goto drop_unlock; /* should never happen */
482
483 /* Extract variable-width fields */
484 sha = rarp_ptr;
485 rarp_ptr += dev->addr_len;
486 memcpy(&sip, rarp_ptr, 4);
487 rarp_ptr += 4;
488 tha = rarp_ptr;
489 rarp_ptr += dev->addr_len;
490 memcpy(&tip, rarp_ptr, 4);
491
492 /* Discard packets which are not meant for us. */
493 if (memcmp(tha, dev->dev_addr, dev->addr_len))
494 goto drop_unlock;
495
496 /* Discard packets which are not from specified server. */
Al Viro5a874db2006-11-08 00:19:38 -0800497 if (ic_servaddr != NONE && ic_servaddr != sip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto drop_unlock;
499
500 /* We have a winner! */
501 ic_dev = dev;
Al Viro5a874db2006-11-08 00:19:38 -0800502 if (ic_myaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ic_myaddr = tip;
504 ic_servaddr = sip;
505 ic_got_reply = IC_RARP;
506
507drop_unlock:
508 /* Show's over. Nothing to see here. */
509 spin_unlock(&ic_recv_lock);
510
511drop:
512 /* Throw the packet out. */
513 kfree_skb(skb);
514 return 0;
515}
516
517
518/*
519 * Send RARP request packet over a single interface.
520 */
521static void __init ic_rarp_send_if(struct ic_device *d)
522{
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);
526}
527#endif
528
529/*
530 * DHCP/BOOTP support.
531 */
532
533#ifdef IPCONFIG_BOOTP
534
535struct 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 */
Al Viro5a874db2006-11-08 00:19:38 -0800542 __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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 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 */
553};
554
555/* packet ops */
556#define BOOTP_REQUEST 1
557#define BOOTP_REPLY 2
558
559/* DHCP message types */
560#define DHCPDISCOVER 1
561#define DHCPOFFER 2
562#define DHCPREQUEST 3
563#define DHCPDECLINE 4
564#define DHCPACK 5
565#define DHCPNAK 6
566#define DHCPRELEASE 7
567#define DHCPINFORM 8
568
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700569static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571static struct packet_type bootp_packet_type __initdata = {
572 .type = __constant_htons(ETH_P_IP),
573 .func = ic_bootp_recv,
574};
575
576
577/*
578 * Initialize DHCP/BOOTP extension fields in the request.
579 */
580
581static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
582
583#ifdef IPCONFIG_DHCP
584
585static void __init
586ic_dhcp_init_options(u8 *options)
587{
Al Viro5a874db2006-11-08 00:19:38 -0800588 u8 mt = ((ic_servaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 ? DHCPDISCOVER : DHCPREQUEST);
590 u8 *e = options;
591
592#ifdef IPCONFIG_DEBUG
593 printk("DHCP: Sending message type %d\n", mt);
594#endif
595
596 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
597 e += 4;
598
599 *e++ = 53; /* DHCP message type */
600 *e++ = 1;
601 *e++ = mt;
602
603 if (mt == DHCPREQUEST) {
604 *e++ = 54; /* Server ID (IP address) */
605 *e++ = 4;
606 memcpy(e, &ic_servaddr, 4);
607 e += 4;
608
609 *e++ = 50; /* Requested IP address */
610 *e++ = 4;
611 memcpy(e, &ic_myaddr, 4);
612 e += 4;
613 }
614
615 /* always? */
616 {
617 static const u8 ic_req_params[] = {
618 1, /* Subnet mask */
619 3, /* Default gateway */
620 6, /* DNS server */
621 12, /* Host name */
622 15, /* Domain name */
623 17, /* Boot path */
624 40, /* NIS domain name */
625 };
626
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);
631 }
632
633 *e++ = 255; /* End of the list */
634}
635
636#endif /* IPCONFIG_DHCP */
637
638static void __init ic_bootp_init_ext(u8 *e)
639{
640 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
641 e += 4;
642 *e++ = 1; /* Subnet mask request */
643 *e++ = 4;
644 e += 4;
645 *e++ = 3; /* Default gateway request */
646 *e++ = 4;
647 e += 4;
648 *e++ = 5; /* Name server request */
649 *e++ = 8;
650 e += 8;
651 *e++ = 12; /* Host name request */
652 *e++ = 32;
653 e += 32;
654 *e++ = 40; /* NIS Domain name request */
655 *e++ = 32;
656 e += 32;
657 *e++ = 17; /* Boot path */
658 *e++ = 40;
659 e += 40;
660
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900661 *e++ = 57; /* set extension buffer size for reply */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *e++ = 2;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900663 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 *e++ = 150;
665
666 *e++ = 255; /* End of the list */
667}
668
669
670/*
671 * Initialize the DHCP/BOOTP mechanism.
672 */
673static inline void ic_bootp_init(void)
674{
675 int i;
676
677 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
Al Viro5a874db2006-11-08 00:19:38 -0800678 ic_nameservers[i] = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 dev_add_pack(&bootp_packet_type);
681}
682
683
684/*
685 * DHCP/BOOTP cleanup.
686 */
687static inline void ic_bootp_cleanup(void)
688{
689 dev_remove_pack(&bootp_packet_type);
690}
691
692
693/*
694 * Send DHCP/BOOTP request to single interface.
695 */
696static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
697{
698 struct net_device *dev = d->dev;
699 struct sk_buff *skb;
700 struct bootp_pkt *b;
701 int hh_len = LL_RESERVED_SPACE(dev);
702 struct iphdr *h;
703
704 /* Allocate packet */
705 skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
706 if (!skb)
707 return;
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));
711
712 /* Construct IP header */
Arnaldo Carvalho de Melo04b964d2007-03-10 19:27:27 -0300713 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700714 h = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 h->version = 4;
716 h->ihl = 5;
717 h->tot_len = htons(sizeof(struct bootp_pkt));
718 h->frag_off = htons(IP_DF);
719 h->ttl = 64;
720 h->protocol = IPPROTO_UDP;
Al Viro5a874db2006-11-08 00:19:38 -0800721 h->daddr = htonl(INADDR_BROADCAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
723
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 */
729
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;
738 else {
739 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
740 b->htype = dev->type; /* can cause undefined behavior */
741 }
742 b->hlen = dev->addr_len;
Al Viro5a874db2006-11-08 00:19:38 -0800743 b->your_ip = NONE;
744 b->server_ip = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
746 b->secs = htons(jiffies_diff / HZ);
747 b->xid = d->xid;
748
749 /* add DHCP options or BOOTP extensions */
750#ifdef IPCONFIG_DHCP
751 if (ic_proto_enabled & IC_USE_DHCP)
752 ic_dhcp_init_options(b->exten);
753 else
754#endif
755 ic_bootp_init_ext(b->exten);
756
757 /* Chain packet down the line... */
758 skb->dev = dev;
759 skb->protocol = htons(ETH_P_IP);
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700760 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
761 dev->broadcast, dev->dev_addr, skb->len) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 dev_queue_xmit(skb) < 0)
763 printk("E");
764}
765
766
767/*
768 * Copy BOOTP-supplied string if not already set.
769 */
770static int __init ic_bootp_string(char *dest, char *src, int len, int max)
771{
772 if (!len)
773 return 0;
774 if (len > max-1)
775 len = max-1;
776 memcpy(dest, src, len);
777 dest[len] = '\0';
778 return 1;
779}
780
781
782/*
783 * Process BOOTP extensions.
784 */
785static void __init ic_do_bootp_ext(u8 *ext)
786{
787 u8 servers;
788 int i;
789
790#ifdef IPCONFIG_DEBUG
791 u8 *c;
792
793 printk("DHCP/BOOTP: Got extension %d:",*ext);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800794 for (c=ext+2; c<ext+2+ext[1]; c++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 printk(" %02x", *c);
796 printk("\n");
797#endif
798
799 switch (*ext++) {
800 case 1: /* Subnet mask */
Al Viro5a874db2006-11-08 00:19:38 -0800801 if (ic_netmask == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 memcpy(&ic_netmask, ext+1, 4);
803 break;
804 case 3: /* Default gateway */
Al Viro5a874db2006-11-08 00:19:38 -0800805 if (ic_gateway == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 memcpy(&ic_gateway, ext+1, 4);
807 break;
808 case 6: /* DNS server */
809 servers= *ext/4;
810 if (servers > CONF_NAMESERVERS_MAX)
811 servers = CONF_NAMESERVERS_MAX;
812 for (i = 0; i < servers; i++) {
Al Viro5a874db2006-11-08 00:19:38 -0800813 if (ic_nameservers[i] == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
815 }
816 break;
817 case 12: /* Host name */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700818 ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ic_host_name_set = 1;
820 break;
821 case 15: /* Domain name (DNS) */
822 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
823 break;
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));
827 break;
828 case 40: /* NIS Domain name (_not_ DNS) */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700829 ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 }
832}
833
834
835/*
836 * Receive BOOTP reply.
837 */
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700838static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct bootp_pkt *b;
841 struct iphdr *h;
842 struct ic_device *d;
843 int len, ext_len;
844
Eric W. Biedermane730c152007-09-17 11:53:39 -0700845 if (dev->nd_net != &init_net)
846 goto drop;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Perform verifications before taking the lock. */
849 if (skb->pkt_type == PACKET_OTHERHOST)
850 goto drop;
851
852 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
853 return NET_RX_DROP;
854
855 if (!pskb_may_pull(skb,
856 sizeof(struct iphdr) +
857 sizeof(struct udphdr)))
858 goto drop;
859
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700860 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 h = &b->iph;
862
863 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
864 goto drop;
865
866 /* Fragments are not supported */
867 if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
868 if (net_ratelimit())
869 printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
870 "reply.\n");
871 goto drop;
872 }
873
874 if (skb->len < ntohs(h->tot_len))
875 goto drop;
876
877 if (ip_fast_csum((char *) h, h->ihl))
878 goto drop;
879
880 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
881 goto drop;
882
883 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
884 goto drop;
885
886 len = ntohs(b->udph.len) - sizeof(struct udphdr);
887 ext_len = len - (sizeof(*b) -
888 sizeof(struct iphdr) -
889 sizeof(struct udphdr) -
890 sizeof(b->exten));
891 if (ext_len < 0)
892 goto drop;
893
894 /* Ok the front looks good, make sure we can get at the rest. */
895 if (!pskb_may_pull(skb, skb->len))
896 goto drop;
897
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700898 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 h = &b->iph;
900
901 /* One reply at a time, please. */
902 spin_lock(&ic_recv_lock);
903
904 /* If we already have a reply, just drop the packet */
905 if (ic_got_reply)
906 goto drop_unlock;
907
908 /* Find the ic_device that the packet arrived on */
909 d = ic_first_dev;
910 while (d && d->dev != dev)
911 d = d->next;
912 if (!d)
913 goto drop_unlock; /* should never happen */
914
915 /* Is it a reply to our BOOTP request? */
916 if (b->op != BOOTP_REPLY ||
917 b->xid != d->xid) {
918 if (net_ratelimit())
919 printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
920 "op[%x] xid[%x]\n",
921 b->op, b->xid);
922 goto drop_unlock;
923 }
924
925 /* Parse extensions */
926 if (ext_len >= 4 &&
927 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900928 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 u8 *ext;
930
931#ifdef IPCONFIG_DHCP
932 if (ic_proto_enabled & IC_USE_DHCP) {
Al Viro5a874db2006-11-08 00:19:38 -0800933 __be32 server_id = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int mt = 0;
935
936 ext = &b->exten[4];
937 while (ext < end && *ext != 0xff) {
938 u8 *opt = ext++;
939 if (*opt == 0) /* Padding */
940 continue;
941 ext += *ext + 1;
942 if (ext >= end)
943 break;
944 switch (*opt) {
945 case 53: /* Message type */
946 if (opt[1])
947 mt = opt[2];
948 break;
949 case 54: /* Server ID (IP address) */
950 if (opt[1] >= 4)
951 memcpy(&server_id, opt + 2, 4);
952 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956#ifdef IPCONFIG_DEBUG
957 printk("DHCP: Got message type %d\n", mt);
958#endif
959
960 switch (mt) {
961 case DHCPOFFER:
962 /* While in the process of accepting one offer,
963 * ignore all others.
964 */
Al Viro5a874db2006-11-08 00:19:38 -0800965 if (ic_myaddr != NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 goto drop_unlock;
967
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",
973 NIPQUAD(ic_myaddr));
974 printk(" by server %u.%u.%u.%u\n",
975 NIPQUAD(ic_servaddr));
976#endif
977 /* The DHCP indicated server address takes
978 * precedence over the bootp header one if
979 * they are different.
980 */
Al Viro5a874db2006-11-08 00:19:38 -0800981 if ((server_id != NONE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 (b->server_ip != server_id))
983 b->server_ip = ic_servaddr;
984 break;
985
986 case DHCPACK:
987 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
988 goto drop_unlock;
989
990 /* Yeah! */
991 break;
992
993 default:
994 /* Urque. Forget it*/
Al Viro5a874db2006-11-08 00:19:38 -0800995 ic_myaddr = NONE;
996 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto drop_unlock;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 ic_dhcp_msgtype = mt;
1001
1002 }
1003#endif /* IPCONFIG_DHCP */
1004
1005 ext = &b->exten[4];
1006 while (ext < end && *ext != 0xff) {
1007 u8 *opt = ext++;
1008 if (*opt == 0) /* Padding */
1009 continue;
1010 ext += *ext + 1;
1011 if (ext < end)
1012 ic_do_bootp_ext(opt);
1013 }
1014 }
1015
1016 /* We have a winner! */
1017 ic_dev = dev;
1018 ic_myaddr = b->your_ip;
1019 ic_servaddr = b->server_ip;
Al Viro5a874db2006-11-08 00:19:38 -08001020 if (ic_gateway == NONE && b->relay_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ic_gateway = b->relay_ip;
Al Viro5a874db2006-11-08 00:19:38 -08001022 if (ic_nameservers[0] == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ic_nameservers[0] = ic_servaddr;
1024 ic_got_reply = IC_BOOTP;
1025
1026drop_unlock:
1027 /* Show's over. Nothing to see here. */
1028 spin_unlock(&ic_recv_lock);
1029
1030drop:
1031 /* Throw the packet out. */
1032 kfree_skb(skb);
1033
1034 return 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037
1038#endif
1039
1040
1041/*
1042 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1043 */
1044
1045#ifdef IPCONFIG_DYNAMIC
1046
1047static int __init ic_dynamic(void)
1048{
1049 int retries;
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;
1054
1055 /*
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.
1059 */
1060 if (!ic_proto_enabled) {
1061 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1062 return -1;
1063 }
1064
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");
1068#endif
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");
1072#endif
1073
1074 if (!ic_proto_have_if)
1075 /* Error message already printed */
1076 return -1;
1077
1078 /*
1079 * Setup protocols
1080 */
1081#ifdef IPCONFIG_BOOTP
1082 if (do_bootp)
1083 ic_bootp_init();
1084#endif
1085#ifdef IPCONFIG_RARP
1086 if (do_rarp)
1087 ic_rarp_init();
1088#endif
1089
1090 /*
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.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001095 * [Actually we could now, but the nothing else running note still
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * applies.. - AC]
1097 */
1098 printk(KERN_NOTICE "Sending %s%s%s requests .",
1099 do_bootp
1100 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1101 (do_bootp && do_rarp) ? " and " : "",
1102 do_rarp ? "RARP" : "");
1103
1104 start_jiffies = jiffies;
1105 d = ic_first_dev;
1106 retries = CONF_SEND_RETRIES;
1107 get_random_bytes(&timeout, sizeof(timeout));
1108 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001109 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#ifdef IPCONFIG_BOOTP
1111 if (do_bootp && (d->able & IC_BOOTP))
1112 ic_bootp_send_if(d, jiffies - start_jiffies);
1113#endif
1114#ifdef IPCONFIG_RARP
1115 if (do_rarp && (d->able & IC_RARP))
1116 ic_rarp_send_if(d);
1117#endif
1118
1119 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001120 while (time_before(jiffies, jiff) && !ic_got_reply)
1121 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#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)
1127 {
1128 ic_got_reply = 0;
1129 printk(",");
1130 continue;
1131 }
1132#endif /* IPCONFIG_DHCP */
1133
1134 if (ic_got_reply) {
1135 printk(" OK\n");
1136 break;
1137 }
1138
1139 if ((d = d->next))
1140 continue;
1141
1142 if (! --retries) {
1143 printk(" timed out!\n");
1144 break;
1145 }
1146
1147 d = ic_first_dev;
1148
1149 timeout = timeout CONF_TIMEOUT_MULT;
1150 if (timeout > CONF_TIMEOUT_MAX)
1151 timeout = CONF_TIMEOUT_MAX;
1152
1153 printk(".");
1154 }
1155
1156#ifdef IPCONFIG_BOOTP
1157 if (do_bootp)
1158 ic_bootp_cleanup();
1159#endif
1160#ifdef IPCONFIG_RARP
1161 if (do_rarp)
1162 ic_rarp_cleanup();
1163#endif
1164
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001165 if (!ic_got_reply) {
Al Viro5a874db2006-11-08 00:19:38 -08001166 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -1;
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001171 ((ic_got_reply & IC_RARP) ? "RARP"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 : (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));
1175
1176 return 0;
1177}
1178
1179#endif /* IPCONFIG_DYNAMIC */
1180
1181#ifdef CONFIG_PROC_FS
1182
1183static int pnp_seq_show(struct seq_file *seq, void *v)
1184{
1185 int i;
1186
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");
1191 else
1192 seq_puts(seq, "#MANUAL\n");
1193
1194 if (ic_domain[0])
1195 seq_printf(seq,
1196 "domain %s\n", ic_domain);
1197 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
Al Viro5a874db2006-11-08 00:19:38 -08001198 if (ic_nameservers[i] != NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 seq_printf(seq,
1200 "nameserver %u.%u.%u.%u\n",
1201 NIPQUAD(ic_nameservers[i]));
1202 }
Al Viro5a874db2006-11-08 00:19:38 -08001203 if (ic_servaddr != NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 seq_printf(seq,
1205 "bootserver %u.%u.%u.%u\n",
1206 NIPQUAD(ic_servaddr));
1207 return 0;
1208}
1209
1210static int pnp_seq_open(struct inode *indoe, struct file *file)
1211{
1212 return single_open(file, pnp_seq_show, NULL);
1213}
1214
Arjan van de Ven9a321442007-02-12 00:55:35 -08001215static const struct file_operations pnp_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .owner = THIS_MODULE,
1217 .open = pnp_seq_open,
1218 .read = seq_read,
1219 .llseek = seq_lseek,
1220 .release = single_release,
1221};
1222#endif /* CONFIG_PROC_FS */
1223
1224/*
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
1227 * can override it.
1228 */
Al Viro5a874db2006-11-08 00:19:38 -08001229__be32 __init root_nfs_parse_addr(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Al Viro5a874db2006-11-08 00:19:38 -08001231 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int octets = 0;
1233 char *cp, *cq;
1234
1235 cp = cq = name;
1236 while (octets < 4) {
1237 while (*cp >= '0' && *cp <= '9')
1238 cp++;
1239 if (cp == cq || cp - cq > 3)
1240 break;
1241 if (*cp == '.' || octets == 3)
1242 octets++;
1243 if (octets < 4)
1244 cp++;
1245 cq = cp;
1246 }
1247 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1248 if (*cp == ':')
1249 *cp++ = '\0';
1250 addr = in_aton(name);
1251 memmove(name, cp, strlen(cp) + 1);
1252 } else
Al Viro5a874db2006-11-08 00:19:38 -08001253 addr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 return addr;
1256}
1257
1258/*
1259 * IP Autoconfig dispatcher.
1260 */
1261
1262static int __init ip_auto_config(void)
1263{
Al Viro5a874db2006-11-08 00:19:38 -08001264 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001267 proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif /* CONFIG_PROC_FS */
1269
1270 if (!ic_enable)
1271 return 0;
1272
1273 DBG(("IP-Config: Entered.\n"));
1274#ifdef IPCONFIG_DYNAMIC
1275 try_try_again:
1276#endif
1277 /* Give hardware a chance to settle */
1278 msleep(CONF_PRE_OPEN);
1279
1280 /* Setup all network devices */
1281 if (ic_open_devs() < 0)
1282 return -1;
1283
1284 /* Give drivers a chance to settle */
1285 ssleep(CONF_POST_OPEN);
1286
1287 /*
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
1291 * missing values.
1292 */
Al Viro5a874db2006-11-08 00:19:38 -08001293 if (ic_myaddr == NONE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294#ifdef CONFIG_ROOT_NFS
Joakim Tjernlunddcbdc932007-08-10 15:14:54 -07001295 (root_server_addr == NONE
1296 && ic_servaddr == NONE
1297 && ROOT_DEV == Root_NFS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#endif
1299 ic_first_dev->next) {
1300#ifdef IPCONFIG_DYNAMIC
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int retries = CONF_OPEN_RETRIES;
1303
1304 if (ic_dynamic() < 0) {
1305 ic_close_devs();
1306
1307 /*
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.
1314 *
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.
1318 *
1319 * -- Chip
1320 */
1321#ifdef CONFIG_ROOT_NFS
1322 if (ROOT_DEV == Root_NFS) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001323 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 "IP-Config: Retrying forever (NFS root)...\n");
1325 goto try_try_again;
1326 }
1327#endif
1328
1329 if (--retries) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001330 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 "IP-Config: Reopening network devices...\n");
1332 goto try_try_again;
1333 }
1334
1335 /* Oh, well. At least we tried. */
1336 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1337 return -1;
1338 }
1339#else /* !DYNAMIC */
1340 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1341 ic_close_devs();
1342 return -1;
1343#endif /* IPCONFIG_DYNAMIC */
1344 } else {
1345 /* Device selected manually or only one device -> use it */
1346 ic_dev = ic_first_dev->dev;
1347 }
1348
1349 addr = root_nfs_parse_addr(root_server_path);
Al Viro5a874db2006-11-08 00:19:38 -08001350 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 root_server_addr = addr;
1352
1353 /*
1354 * Use defaults whereever applicable.
1355 */
1356 if (ic_defaults() < 0)
1357 return -1;
1358
1359 /*
1360 * Close all network devices except the device we've
1361 * autoconfigured and set up routes.
1362 */
1363 ic_close_devs();
1364 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1365 return -1;
1366
1367 /*
1368 * Record which protocol was actually used.
1369 */
1370#ifdef IPCONFIG_DYNAMIC
1371 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1372#endif
1373
1374#ifndef IPCONFIG_SILENT
1375 /*
1376 * Clue in the operator.
1377 */
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",
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001384 utsname()->nodename, ic_domain, utsname()->domainname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 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);
1388 printk("\n");
1389#endif /* !SILENT */
1390
1391 return 0;
1392}
1393
1394late_initcall(ip_auto_config);
1395
1396
1397/*
1398 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
Amos Waterlandf33e1d92007-12-14 11:30:22 -08001399 * command line parameter. See Documentation/nfsroot.txt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401static int __init ic_proto_name(char *name)
1402{
1403 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1404 return 1;
1405 }
Simon Hormana6c05c32007-12-25 20:54:42 -08001406 if (!strcmp(name, "off") || !strcmp(name, "none")) {
Amos Waterland92ffb852008-01-05 23:23:06 -08001407 return 0;
Simon Hormana6c05c32007-12-25 20:54:42 -08001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409#ifdef CONFIG_IP_PNP_DHCP
1410 else if (!strcmp(name, "dhcp")) {
1411 ic_proto_enabled &= ~IC_RARP;
1412 return 1;
1413 }
1414#endif
1415#ifdef CONFIG_IP_PNP_BOOTP
1416 else if (!strcmp(name, "bootp")) {
1417 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1418 return 1;
1419 }
1420#endif
1421#ifdef CONFIG_IP_PNP_RARP
1422 else if (!strcmp(name, "rarp")) {
1423 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1424 return 1;
1425 }
1426#endif
1427#ifdef IPCONFIG_DYNAMIC
1428 else if (!strcmp(name, "both")) {
1429 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1430 return 1;
1431 }
1432#endif
1433 return 0;
1434}
1435
1436static int __init ip_auto_config_setup(char *addrs)
1437{
1438 char *cp, *ip, *dp;
1439 int num = 0;
1440
1441 ic_set_manually = 1;
Simon Horman9cecd072007-12-27 21:19:10 -08001442 ic_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Amos Waterland92ffb852008-01-05 23:23:06 -08001444 /*
1445 * If any dhcp, bootp etc options are set, leave autoconfig on
1446 * and skip the below static IP processing.
1447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (ic_proto_name(addrs))
1449 return 1;
1450
Amos Waterland92ffb852008-01-05 23:23:06 -08001451 /* If no static IP is given, turn off autoconfig and bail. */
1452 if (*addrs == 0 ||
1453 strcmp(addrs, "off") == 0 ||
1454 strcmp(addrs, "none") == 0) {
1455 ic_enable = 0;
1456 return 1;
1457 }
1458
1459 /* Parse string for static IP assignment. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 ip = addrs;
1461 while (ip && *ip) {
1462 if ((cp = strchr(ip, ':')))
1463 *cp++ = '\0';
1464 if (strlen(ip) > 0) {
1465 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1466 switch (num) {
1467 case 0:
1468 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001469 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 case 1:
1472 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001473 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 case 2:
1476 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001477 ic_gateway = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479 case 3:
1480 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001481 ic_netmask = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case 4:
1484 if ((dp = strchr(ip, '.'))) {
1485 *dp++ = '\0';
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001486 strlcpy(utsname()->domainname, dp,
1487 sizeof(utsname()->domainname));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001489 strlcpy(utsname()->nodename, ip,
1490 sizeof(utsname()->nodename));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 ic_host_name_set = 1;
1492 break;
1493 case 5:
1494 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1495 break;
1496 case 6:
Amos Waterland92ffb852008-01-05 23:23:06 -08001497 if (ic_proto_name(ip) == 0 &&
1498 ic_myaddr == NONE) {
1499 ic_enable = 0;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
1502 }
1503 }
1504 ip = cp;
1505 num++;
1506 }
1507
1508 return 1;
1509}
1510
1511static int __init nfsaddrs_config_setup(char *addrs)
1512{
1513 return ip_auto_config_setup(addrs);
1514}
1515
1516__setup("ip=", ip_auto_config_setup);
1517__setup("nfsaddrs=", nfsaddrs_config_setup);