blob: 10a6a604bf32bee0aa1f4a7d591557173919d296 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Automatic Configuration of IP -- use DHCP, BOOTP, RARP, or
3 * user-supplied information to configure own IP address and routes.
4 *
5 * Copyright (C) 1996-1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 *
7 * Derived from network configuration code in fs/nfs/nfsroot.c,
8 * originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
9 *
10 * BOOTP rewritten to construct and analyse packets itself instead
11 * of misusing the IP layer. num_bugs_causing_wrong_arp_replies--;
12 * -- MJ, December 1998
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic"
15 * initialization scheme.
16 * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999
17 *
18 * DHCP support added. To users this looks like a whole separate
19 * protocol, but we know it's just a bag on the side of BOOTP.
20 * -- Chip Salzenberg <chip@valinux.com>, May 2000
21 *
22 * Ported DHCP support from 2.2.16 to 2.4.0-test4
23 * -- Eric Biederman <ebiederman@lnxi.com>, 30 Aug 2000
24 *
25 * Merged changes from 2.2.19 into 2.4.3
26 * -- Eric Biederman <ebiederman@lnxi.com>, 22 April Aug 2001
27 *
28 * Multiple Nameservers in /proc/net/pnp
29 * -- Josef Siemes <jsiemes@web.de>, Aug 2002
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/string.h>
34#include <linux/kernel.h>
35#include <linux/jiffies.h>
36#include <linux/random.h>
37#include <linux/init.h>
38#include <linux/utsname.h>
39#include <linux/in.h>
40#include <linux/if.h>
41#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020042#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/netdevice.h>
44#include <linux/if_arp.h>
45#include <linux/skbuff.h>
46#include <linux/ip.h>
47#include <linux/socket.h>
48#include <linux/route.h>
49#include <linux/udp.h>
50#include <linux/proc_fs.h>
51#include <linux/seq_file.h>
52#include <linux/major.h>
53#include <linux/root_dev.h>
54#include <linux/delay.h>
Adrian Bunk43d60662005-09-05 18:05:52 -070055#include <linux/nfs_fs.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020056#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <net/arp.h>
58#include <net/ip.h>
59#include <net/ipconfig.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020060#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <asm/uaccess.h>
63#include <net/checksum.h>
64#include <asm/processor.h>
65
66/* Define this to allow debugging output */
67#undef IPCONFIG_DEBUG
68
69#ifdef IPCONFIG_DEBUG
70#define DBG(x) printk x
71#else
72#define DBG(x) do { } while(0)
73#endif
74
75#if defined(CONFIG_IP_PNP_DHCP)
76#define IPCONFIG_DHCP
77#endif
78#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_DHCP)
79#define IPCONFIG_BOOTP
80#endif
81#if defined(CONFIG_IP_PNP_RARP)
82#define IPCONFIG_RARP
83#endif
84#if defined(IPCONFIG_BOOTP) || defined(IPCONFIG_RARP)
85#define IPCONFIG_DYNAMIC
86#endif
87
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 */
91
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 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900100#define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers
101 - '3' from resolv.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Harvey Harrison09640e62009-02-01 00:45:17 -0800103#define NONE cpu_to_be32(INADDR_NONE)
104#define ANY cpu_to_be32(INADDR_ANY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * Public IP configuration
108 */
109
110/* This is used by platforms which might be able to set the ipconfig
111 * variables using firmware environment vars. If this is set, it will
112 * ignore such firmware variables.
113 */
114int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
115
116static int ic_enable __initdata = 0; /* IP config enabled? */
117
118/* Protocol choice */
119int ic_proto_enabled __initdata = 0
120#ifdef IPCONFIG_BOOTP
121 | IC_BOOTP
122#endif
123#ifdef CONFIG_IP_PNP_DHCP
124 | IC_USE_DHCP
125#endif
126#ifdef IPCONFIG_RARP
127 | IC_RARP
128#endif
129 ;
130
131static int ic_host_name_set __initdata = 0; /* Host name set by us? */
132
Al Viro5a874db2006-11-08 00:19:38 -0800133__be32 ic_myaddr = NONE; /* My IP address */
134static __be32 ic_netmask = NONE; /* Netmask for local subnet */
135__be32 ic_gateway = NONE; /* Gateway IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Al Viro5a874db2006-11-08 00:19:38 -0800137__be32 ic_servaddr = NONE; /* Boot server IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Al Viro5a874db2006-11-08 00:19:38 -0800139__be32 root_server_addr = NONE; /* Address of NFS server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140u8 root_server_path[256] = { 0, }; /* Path to mount as root */
141
Chris Friesen2513dfb2009-05-17 20:39:33 -0700142u32 ic_dev_xid; /* Device under configuration */
143
David S. Miller1b0b04f2007-11-19 21:56:16 -0800144/* vendor class identifier */
145static char vendor_class_identifier[253] __initdata;
Rainer Jochem62013db2007-11-14 02:18:39 -0800146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* Persistent data: */
148
149static int ic_proto_used; /* Protocol used, if any */
Al Viro5a874db2006-11-08 00:19:38 -0800150static __be32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static u8 ic_domain[64]; /* DNS (not NIS) domain name */
152
153/*
154 * Private state.
155 */
156
157/* Name of user-selected boot device */
158static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
159
160/* Protocols supported by available interfaces */
161static int ic_proto_have_if __initdata = 0;
162
Chris Friesen9643f452009-05-19 15:31:50 -0700163/* MTU for boot device */
164static int ic_dev_mtu __initdata = 0;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#ifdef IPCONFIG_DYNAMIC
167static DEFINE_SPINLOCK(ic_recv_lock);
168static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */
169#endif
170#ifdef IPCONFIG_DHCP
171static int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */
172#endif
173
174
175/*
176 * Network devices
177 */
178
179struct ic_device {
180 struct ic_device *next;
181 struct net_device *dev;
182 unsigned short flags;
183 short able;
Al Viro5a874db2006-11-08 00:19:38 -0800184 __be32 xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
188static struct net_device *ic_dev __initdata = NULL; /* Selected device */
189
190static int __init ic_open_devs(void)
191{
192 struct ic_device *d, **last;
193 struct net_device *dev;
194 unsigned short oflags;
195
196 last = &ic_first_dev;
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800197 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* bring loopback device up first */
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700200 for_each_netdev(&init_net, dev) {
201 if (!(dev->flags & IFF_LOOPBACK))
202 continue;
203 if (dev_change_flags(dev, dev->flags | IFF_UP) < 0)
204 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Eric W. Biederman881d9662007-09-17 11:56:21 -0700207 for_each_netdev(&init_net, dev) {
Eric W. Biederman0cc217e2007-09-26 22:10:06 -0700208 if (dev->flags & IFF_LOOPBACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 continue;
210 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
211 (!(dev->flags & IFF_LOOPBACK) &&
212 (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
213 strncmp(dev->name, "dummy", 5))) {
214 int able = 0;
215 if (dev->mtu >= 364)
216 able |= IC_BOOTP;
217 else
218 printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
219 if (!(dev->flags & IFF_NOARP))
220 able |= IC_RARP;
221 able &= ic_proto_enabled;
222 if (ic_proto_enabled && !able)
223 continue;
224 oflags = dev->flags;
225 if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
226 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
227 continue;
228 }
229 if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800230 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -1;
232 }
233 d->dev = dev;
234 *last = d;
235 last = &d->next;
236 d->flags = oflags;
237 d->able = able;
238 if (able & IC_BOOTP)
Al Viro5a874db2006-11-08 00:19:38 -0800239 get_random_bytes(&d->xid, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 else
241 d->xid = 0;
242 ic_proto_have_if |= able;
243 DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n",
244 dev->name, able, d->xid));
245 }
246 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800247 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 *last = NULL;
250
251 if (!ic_first_dev) {
252 if (user_dev_name[0])
253 printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
254 else
255 printk(KERN_ERR "IP-Config: No network devices available.\n");
256 return -1;
257 }
258 return 0;
259}
260
261static void __init ic_close_devs(void)
262{
263 struct ic_device *d, *next;
264 struct net_device *dev;
265
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800266 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 next = ic_first_dev;
268 while ((d = next)) {
269 next = d->next;
270 dev = d->dev;
271 if (dev != ic_dev) {
272 DBG(("IP-Config: Downing %s\n", dev->name));
273 dev_change_flags(dev, d->flags);
274 }
275 kfree(d);
276 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800277 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Interface to various network functions.
282 */
283
284static inline void
Al Viro5a874db2006-11-08 00:19:38 -0800285set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 sin->sin_family = AF_INET;
288 sin->sin_addr.s_addr = addr;
289 sin->sin_port = port;
290}
291
Chris Friesen9643f452009-05-19 15:31:50 -0700292static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int res;
295
296 mm_segment_t oldfs = get_fs();
297 set_fs(get_ds());
Denis V. Luneve5b13cb2008-02-28 20:51:43 -0800298 res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 set_fs(oldfs);
300 return res;
301}
302
Chris Friesen9643f452009-05-19 15:31:50 -0700303static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
304{
305 int res;
306
307 mm_segment_t oldfs = get_fs();
308 set_fs(get_ds());
309 res = dev_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
310 set_fs(oldfs);
311 return res;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
315{
316 int res;
317
318 mm_segment_t oldfs = get_fs();
319 set_fs(get_ds());
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800320 res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 set_fs(oldfs);
322 return res;
323}
324
325/*
326 * Set up interface addresses and routes.
327 */
328
329static int __init ic_setup_if(void)
330{
331 struct ifreq ir;
332 struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
333 int err;
334
335 memset(&ir, 0, sizeof(ir));
336 strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
337 set_sockaddr(sin, ic_myaddr, 0);
Chris Friesen9643f452009-05-19 15:31:50 -0700338 if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
340 return -1;
341 }
342 set_sockaddr(sin, ic_netmask, 0);
Chris Friesen9643f452009-05-19 15:31:50 -0700343 if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
345 return -1;
346 }
347 set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
Chris Friesen9643f452009-05-19 15:31:50 -0700348 if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
350 return -1;
351 }
Chris Friesen9643f452009-05-19 15:31:50 -0700352 /* Handle the case where we need non-standard MTU on the boot link (a network
353 * using jumbo frames, for instance). If we can't set the mtu, don't error
354 * out, we'll try to muddle along.
355 */
356 if (ic_dev_mtu != 0) {
357 strcpy(ir.ifr_name, ic_dev->name);
358 ir.ifr_mtu = ic_dev_mtu;
359 if ((err = ic_dev_ioctl(SIOCSIFMTU, &ir)) < 0)
360 printk(KERN_ERR "IP-Config: Unable to set interface mtu to %d (%d).\n",
361 ic_dev_mtu, err);
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364}
365
366static int __init ic_setup_routes(void)
367{
368 /* No need to setup device routes, only the default route... */
369
Al Viro5a874db2006-11-08 00:19:38 -0800370 if (ic_gateway != NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct rtentry rm;
372 int err;
373
374 memset(&rm, 0, sizeof(rm));
375 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
376 printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
377 return -1;
378 }
379 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
380 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
381 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
382 rm.rt_flags = RTF_UP | RTF_GATEWAY;
383 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
384 printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
385 return -1;
386 }
387 }
388
389 return 0;
390}
391
392/*
393 * Fill in default values for all missing parameters.
394 */
395
396static int __init ic_defaults(void)
397{
398 /*
399 * At this point we have no userspace running so need not
400 * claim locks on system_utsname
401 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!ic_host_name_set)
Harvey Harrison673d57e2008-10-31 00:53:57 -0700404 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Al Viro5a874db2006-11-08 00:19:38 -0800406 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 root_server_addr = ic_servaddr;
408
Al Viro5a874db2006-11-08 00:19:38 -0800409 if (ic_netmask == NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (IN_CLASSA(ntohl(ic_myaddr)))
411 ic_netmask = htonl(IN_CLASSA_NET);
412 else if (IN_CLASSB(ntohl(ic_myaddr)))
413 ic_netmask = htonl(IN_CLASSB_NET);
414 else if (IN_CLASSC(ntohl(ic_myaddr)))
415 ic_netmask = htonl(IN_CLASSC_NET);
416 else {
Harvey Harrison673d57e2008-10-31 00:53:57 -0700417 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %pI4\n",
418 &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -1;
420 }
Harvey Harrison673d57e2008-10-31 00:53:57 -0700421 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 return 0;
425}
426
427/*
428 * RARP support.
429 */
430
431#ifdef IPCONFIG_RARP
432
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700433static 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 -0700434
435static struct packet_type rarp_packet_type __initdata = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800436 .type = cpu_to_be16(ETH_P_RARP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .func = ic_rarp_recv,
438};
439
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700440static inline void __init ic_rarp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 dev_add_pack(&rarp_packet_type);
443}
444
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700445static inline void __init ic_rarp_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 dev_remove_pack(&rarp_packet_type);
448}
449
450/*
451 * Process received RARP packet.
452 */
453static int __init
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700454ic_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 -0700455{
456 struct arphdr *rarp;
457 unsigned char *rarp_ptr;
Al Viro5a874db2006-11-08 00:19:38 -0800458 __be32 sip, tip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 unsigned char *sha, *tha; /* s for "source", t for "target" */
460 struct ic_device *d;
461
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700462 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700463 goto drop;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
466 return NET_RX_DROP;
467
468 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
469 goto drop;
470
471 /* Basic sanity checks can be done without the lock. */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700472 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* If this test doesn't pass, it's not IP, or we should
475 * ignore it anyway.
476 */
477 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
478 goto drop;
479
480 /* If it's not a RARP reply, delete it. */
481 if (rarp->ar_op != htons(ARPOP_RREPLY))
482 goto drop;
483
484 /* If it's not Ethernet, delete it. */
485 if (rarp->ar_pro != htons(ETH_P_IP))
486 goto drop;
487
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800488 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto drop;
490
491 /* OK, it is all there and looks valid, process... */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700492 rarp = (struct arphdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rarp_ptr = (unsigned char *) (rarp + 1);
494
495 /* One reply at a time, please. */
496 spin_lock(&ic_recv_lock);
497
498 /* If we already have a reply, just drop the packet */
499 if (ic_got_reply)
500 goto drop_unlock;
501
502 /* Find the ic_device that the packet arrived on */
503 d = ic_first_dev;
504 while (d && d->dev != dev)
505 d = d->next;
506 if (!d)
507 goto drop_unlock; /* should never happen */
508
509 /* Extract variable-width fields */
510 sha = rarp_ptr;
511 rarp_ptr += dev->addr_len;
512 memcpy(&sip, rarp_ptr, 4);
513 rarp_ptr += 4;
514 tha = rarp_ptr;
515 rarp_ptr += dev->addr_len;
516 memcpy(&tip, rarp_ptr, 4);
517
518 /* Discard packets which are not meant for us. */
519 if (memcmp(tha, dev->dev_addr, dev->addr_len))
520 goto drop_unlock;
521
522 /* Discard packets which are not from specified server. */
Al Viro5a874db2006-11-08 00:19:38 -0800523 if (ic_servaddr != NONE && ic_servaddr != sip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto drop_unlock;
525
526 /* We have a winner! */
527 ic_dev = dev;
Al Viro5a874db2006-11-08 00:19:38 -0800528 if (ic_myaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ic_myaddr = tip;
530 ic_servaddr = sip;
531 ic_got_reply = IC_RARP;
532
533drop_unlock:
534 /* Show's over. Nothing to see here. */
535 spin_unlock(&ic_recv_lock);
536
537drop:
538 /* Throw the packet out. */
539 kfree_skb(skb);
540 return 0;
541}
542
543
544/*
545 * Send RARP request packet over a single interface.
546 */
547static void __init ic_rarp_send_if(struct ic_device *d)
548{
549 struct net_device *dev = d->dev;
550 arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
551 dev->dev_addr, dev->dev_addr);
552}
553#endif
554
555/*
556 * DHCP/BOOTP support.
557 */
558
559#ifdef IPCONFIG_BOOTP
560
561struct bootp_pkt { /* BOOTP packet format */
562 struct iphdr iph; /* IP header */
563 struct udphdr udph; /* UDP header */
564 u8 op; /* 1=request, 2=reply */
565 u8 htype; /* HW address type */
566 u8 hlen; /* HW address length */
567 u8 hops; /* Used only by gateways */
Al Viro5a874db2006-11-08 00:19:38 -0800568 __be32 xid; /* Transaction ID */
569 __be16 secs; /* Seconds since we started */
570 __be16 flags; /* Just what it says */
571 __be32 client_ip; /* Client's IP address if known */
572 __be32 your_ip; /* Assigned IP address */
573 __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
574 __be32 relay_ip; /* IP address of BOOTP relay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 u8 hw_addr[16]; /* Client's HW address */
576 u8 serv_name[64]; /* Server host name */
577 u8 boot_file[128]; /* Name of boot file */
578 u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
579};
580
581/* packet ops */
582#define BOOTP_REQUEST 1
583#define BOOTP_REPLY 2
584
585/* DHCP message types */
586#define DHCPDISCOVER 1
587#define DHCPOFFER 2
588#define DHCPREQUEST 3
589#define DHCPDECLINE 4
590#define DHCPACK 5
591#define DHCPNAK 6
592#define DHCPRELEASE 7
593#define DHCPINFORM 8
594
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700595static 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 -0700596
597static struct packet_type bootp_packet_type __initdata = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800598 .type = cpu_to_be16(ETH_P_IP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 .func = ic_bootp_recv,
600};
601
602
603/*
604 * Initialize DHCP/BOOTP extension fields in the request.
605 */
606
607static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
608
609#ifdef IPCONFIG_DHCP
610
611static void __init
612ic_dhcp_init_options(u8 *options)
613{
Al Viro5a874db2006-11-08 00:19:38 -0800614 u8 mt = ((ic_servaddr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ? DHCPDISCOVER : DHCPREQUEST);
616 u8 *e = options;
Rainer Jochem62013db2007-11-14 02:18:39 -0800617 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619#ifdef IPCONFIG_DEBUG
620 printk("DHCP: Sending message type %d\n", mt);
621#endif
622
623 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
624 e += 4;
625
626 *e++ = 53; /* DHCP message type */
627 *e++ = 1;
628 *e++ = mt;
629
630 if (mt == DHCPREQUEST) {
631 *e++ = 54; /* Server ID (IP address) */
632 *e++ = 4;
633 memcpy(e, &ic_servaddr, 4);
634 e += 4;
635
636 *e++ = 50; /* Requested IP address */
637 *e++ = 4;
638 memcpy(e, &ic_myaddr, 4);
639 e += 4;
640 }
641
642 /* always? */
643 {
644 static const u8 ic_req_params[] = {
645 1, /* Subnet mask */
646 3, /* Default gateway */
647 6, /* DNS server */
648 12, /* Host name */
649 15, /* Domain name */
650 17, /* Boot path */
Chris Friesen9643f452009-05-19 15:31:50 -0700651 26, /* MTU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 40, /* NIS domain name */
653 };
654
655 *e++ = 55; /* Parameter request list */
656 *e++ = sizeof(ic_req_params);
657 memcpy(e, ic_req_params, sizeof(ic_req_params));
658 e += sizeof(ic_req_params);
Rainer Jochem62013db2007-11-14 02:18:39 -0800659
660 if (*vendor_class_identifier) {
661 printk(KERN_INFO "DHCP: sending class identifier \"%s\"\n",
662 vendor_class_identifier);
663 *e++ = 60; /* Class-identifier */
664 len = strlen(vendor_class_identifier);
665 *e++ = len;
666 memcpy(e, vendor_class_identifier, len);
667 e += len;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 *e++ = 255; /* End of the list */
672}
673
674#endif /* IPCONFIG_DHCP */
675
676static void __init ic_bootp_init_ext(u8 *e)
677{
678 memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
679 e += 4;
680 *e++ = 1; /* Subnet mask request */
681 *e++ = 4;
682 e += 4;
683 *e++ = 3; /* Default gateway request */
684 *e++ = 4;
685 e += 4;
686 *e++ = 5; /* Name server request */
687 *e++ = 8;
688 e += 8;
689 *e++ = 12; /* Host name request */
690 *e++ = 32;
691 e += 32;
692 *e++ = 40; /* NIS Domain name request */
693 *e++ = 32;
694 e += 32;
695 *e++ = 17; /* Boot path */
696 *e++ = 40;
697 e += 40;
698
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900699 *e++ = 57; /* set extension buffer size for reply */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *e++ = 2;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900701 *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *e++ = 150;
703
704 *e++ = 255; /* End of the list */
705}
706
707
708/*
709 * Initialize the DHCP/BOOTP mechanism.
710 */
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700711static inline void __init ic_bootp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int i;
714
715 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
Al Viro5a874db2006-11-08 00:19:38 -0800716 ic_nameservers[i] = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 dev_add_pack(&bootp_packet_type);
719}
720
721
722/*
723 * DHCP/BOOTP cleanup.
724 */
Sam Ravnborg45e741b2008-04-29 20:58:15 -0700725static inline void __init ic_bootp_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 dev_remove_pack(&bootp_packet_type);
728}
729
730
731/*
732 * Send DHCP/BOOTP request to single interface.
733 */
734static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
735{
736 struct net_device *dev = d->dev;
737 struct sk_buff *skb;
738 struct bootp_pkt *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct iphdr *h;
740
741 /* Allocate packet */
Johannes Bergf5184d22008-05-12 20:48:31 -0700742 skb = alloc_skb(sizeof(struct bootp_pkt) + LL_ALLOCATED_SPACE(dev) + 15,
743 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!skb)
745 return;
Johannes Bergf5184d22008-05-12 20:48:31 -0700746 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
748 memset(b, 0, sizeof(struct bootp_pkt));
749
750 /* Construct IP header */
Arnaldo Carvalho de Melo04b964d2007-03-10 19:27:27 -0300751 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700752 h = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 h->version = 4;
754 h->ihl = 5;
755 h->tot_len = htons(sizeof(struct bootp_pkt));
756 h->frag_off = htons(IP_DF);
757 h->ttl = 64;
758 h->protocol = IPPROTO_UDP;
Al Viro5a874db2006-11-08 00:19:38 -0800759 h->daddr = htonl(INADDR_BROADCAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
761
762 /* Construct UDP header */
763 b->udph.source = htons(68);
764 b->udph.dest = htons(67);
765 b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
766 /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
767
768 /* Construct DHCP/BOOTP header */
769 b->op = BOOTP_REQUEST;
770 if (dev->type < 256) /* check for false types */
771 b->htype = dev->type;
772 else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
773 b->htype = ARPHRD_IEEE802;
774 else if (dev->type == ARPHRD_FDDI)
775 b->htype = ARPHRD_ETHER;
776 else {
777 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
778 b->htype = dev->type; /* can cause undefined behavior */
779 }
Stephen Hemmingerdea75bd2008-03-04 17:03:49 -0800780
781 /* server_ip and your_ip address are both already zero per RFC2131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 b->hlen = dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
784 b->secs = htons(jiffies_diff / HZ);
785 b->xid = d->xid;
786
787 /* add DHCP options or BOOTP extensions */
788#ifdef IPCONFIG_DHCP
789 if (ic_proto_enabled & IC_USE_DHCP)
790 ic_dhcp_init_options(b->exten);
791 else
792#endif
793 ic_bootp_init_ext(b->exten);
794
795 /* Chain packet down the line... */
796 skb->dev = dev;
797 skb->protocol = htons(ETH_P_IP);
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700798 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
799 dev->broadcast, dev->dev_addr, skb->len) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 dev_queue_xmit(skb) < 0)
801 printk("E");
802}
803
804
805/*
806 * Copy BOOTP-supplied string if not already set.
807 */
808static int __init ic_bootp_string(char *dest, char *src, int len, int max)
809{
810 if (!len)
811 return 0;
812 if (len > max-1)
813 len = max-1;
814 memcpy(dest, src, len);
815 dest[len] = '\0';
816 return 1;
817}
818
819
820/*
821 * Process BOOTP extensions.
822 */
823static void __init ic_do_bootp_ext(u8 *ext)
824{
825 u8 servers;
826 int i;
Chris Friesen9643f452009-05-19 15:31:50 -0700827 u16 mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829#ifdef IPCONFIG_DEBUG
830 u8 *c;
831
832 printk("DHCP/BOOTP: Got extension %d:",*ext);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800833 for (c=ext+2; c<ext+2+ext[1]; c++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 printk(" %02x", *c);
835 printk("\n");
836#endif
837
838 switch (*ext++) {
839 case 1: /* Subnet mask */
Al Viro5a874db2006-11-08 00:19:38 -0800840 if (ic_netmask == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 memcpy(&ic_netmask, ext+1, 4);
842 break;
843 case 3: /* Default gateway */
Al Viro5a874db2006-11-08 00:19:38 -0800844 if (ic_gateway == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 memcpy(&ic_gateway, ext+1, 4);
846 break;
847 case 6: /* DNS server */
848 servers= *ext/4;
849 if (servers > CONF_NAMESERVERS_MAX)
850 servers = CONF_NAMESERVERS_MAX;
851 for (i = 0; i < servers; i++) {
Al Viro5a874db2006-11-08 00:19:38 -0800852 if (ic_nameservers[i] == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
854 }
855 break;
856 case 12: /* Host name */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700857 ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ic_host_name_set = 1;
859 break;
860 case 15: /* Domain name (DNS) */
861 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
862 break;
863 case 17: /* Root path */
864 if (!root_server_path[0])
865 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
866 break;
Chris Friesen9643f452009-05-19 15:31:50 -0700867 case 26: /* Interface MTU */
868 memcpy(&mtu, ext+1, sizeof(mtu));
869 ic_dev_mtu = ntohs(mtu);
870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case 40: /* NIS Domain name (_not_ DNS) */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700872 ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874 }
875}
876
877
878/*
879 * Receive BOOTP reply.
880 */
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700881static 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 -0700882{
883 struct bootp_pkt *b;
884 struct iphdr *h;
885 struct ic_device *d;
886 int len, ext_len;
887
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700888 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700889 goto drop;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Perform verifications before taking the lock. */
892 if (skb->pkt_type == PACKET_OTHERHOST)
893 goto drop;
894
895 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
896 return NET_RX_DROP;
897
898 if (!pskb_may_pull(skb,
899 sizeof(struct iphdr) +
900 sizeof(struct udphdr)))
901 goto drop;
902
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700903 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 h = &b->iph;
905
906 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
907 goto drop;
908
909 /* Fragments are not supported */
910 if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
911 if (net_ratelimit())
912 printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
913 "reply.\n");
914 goto drop;
915 }
916
917 if (skb->len < ntohs(h->tot_len))
918 goto drop;
919
920 if (ip_fast_csum((char *) h, h->ihl))
921 goto drop;
922
923 if (b->udph.source != htons(67) || b->udph.dest != htons(68))
924 goto drop;
925
926 if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
927 goto drop;
928
929 len = ntohs(b->udph.len) - sizeof(struct udphdr);
930 ext_len = len - (sizeof(*b) -
931 sizeof(struct iphdr) -
932 sizeof(struct udphdr) -
933 sizeof(b->exten));
934 if (ext_len < 0)
935 goto drop;
936
937 /* Ok the front looks good, make sure we can get at the rest. */
938 if (!pskb_may_pull(skb, skb->len))
939 goto drop;
940
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700941 b = (struct bootp_pkt *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 h = &b->iph;
943
944 /* One reply at a time, please. */
945 spin_lock(&ic_recv_lock);
946
947 /* If we already have a reply, just drop the packet */
948 if (ic_got_reply)
949 goto drop_unlock;
950
951 /* Find the ic_device that the packet arrived on */
952 d = ic_first_dev;
953 while (d && d->dev != dev)
954 d = d->next;
955 if (!d)
956 goto drop_unlock; /* should never happen */
957
958 /* Is it a reply to our BOOTP request? */
959 if (b->op != BOOTP_REPLY ||
960 b->xid != d->xid) {
961 if (net_ratelimit())
962 printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
963 "op[%x] xid[%x]\n",
964 b->op, b->xid);
965 goto drop_unlock;
966 }
967
Chris Friesen2513dfb2009-05-17 20:39:33 -0700968 /* Is it a reply for the device we are configuring? */
969 if (b->xid != ic_dev_xid) {
970 if (net_ratelimit())
971 printk(KERN_ERR "DHCP/BOOTP: Ignoring delayed packet \n");
972 goto drop_unlock;
973 }
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* Parse extensions */
976 if (ext_len >= 4 &&
977 !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900978 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 u8 *ext;
980
981#ifdef IPCONFIG_DHCP
982 if (ic_proto_enabled & IC_USE_DHCP) {
Al Viro5a874db2006-11-08 00:19:38 -0800983 __be32 server_id = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int mt = 0;
985
986 ext = &b->exten[4];
987 while (ext < end && *ext != 0xff) {
988 u8 *opt = ext++;
989 if (*opt == 0) /* Padding */
990 continue;
991 ext += *ext + 1;
992 if (ext >= end)
993 break;
994 switch (*opt) {
995 case 53: /* Message type */
996 if (opt[1])
997 mt = opt[2];
998 break;
999 case 54: /* Server ID (IP address) */
1000 if (opt[1] >= 4)
1001 memcpy(&server_id, opt + 2, 4);
1002 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
1006#ifdef IPCONFIG_DEBUG
1007 printk("DHCP: Got message type %d\n", mt);
1008#endif
1009
1010 switch (mt) {
1011 case DHCPOFFER:
1012 /* While in the process of accepting one offer,
1013 * ignore all others.
1014 */
Al Viro5a874db2006-11-08 00:19:38 -08001015 if (ic_myaddr != NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto drop_unlock;
1017
1018 /* Let's accept that offer. */
1019 ic_myaddr = b->your_ip;
1020 ic_servaddr = server_id;
1021#ifdef IPCONFIG_DEBUG
Harvey Harrison673d57e2008-10-31 00:53:57 -07001022 printk("DHCP: Offered address %pI4 by server %pI4\n",
1023 &ic_myaddr, &ic_servaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#endif
1025 /* The DHCP indicated server address takes
1026 * precedence over the bootp header one if
1027 * they are different.
1028 */
Al Viro5a874db2006-11-08 00:19:38 -08001029 if ((server_id != NONE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 (b->server_ip != server_id))
1031 b->server_ip = ic_servaddr;
1032 break;
1033
1034 case DHCPACK:
1035 if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
1036 goto drop_unlock;
1037
1038 /* Yeah! */
1039 break;
1040
1041 default:
1042 /* Urque. Forget it*/
Al Viro5a874db2006-11-08 00:19:38 -08001043 ic_myaddr = NONE;
1044 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto drop_unlock;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 ic_dhcp_msgtype = mt;
1049
1050 }
1051#endif /* IPCONFIG_DHCP */
1052
1053 ext = &b->exten[4];
1054 while (ext < end && *ext != 0xff) {
1055 u8 *opt = ext++;
1056 if (*opt == 0) /* Padding */
1057 continue;
1058 ext += *ext + 1;
1059 if (ext < end)
1060 ic_do_bootp_ext(opt);
1061 }
1062 }
1063
1064 /* We have a winner! */
1065 ic_dev = dev;
1066 ic_myaddr = b->your_ip;
1067 ic_servaddr = b->server_ip;
Al Viro5a874db2006-11-08 00:19:38 -08001068 if (ic_gateway == NONE && b->relay_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ic_gateway = b->relay_ip;
Al Viro5a874db2006-11-08 00:19:38 -08001070 if (ic_nameservers[0] == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ic_nameservers[0] = ic_servaddr;
1072 ic_got_reply = IC_BOOTP;
1073
1074drop_unlock:
1075 /* Show's over. Nothing to see here. */
1076 spin_unlock(&ic_recv_lock);
1077
1078drop:
1079 /* Throw the packet out. */
1080 kfree_skb(skb);
1081
1082 return 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085
1086#endif
1087
1088
1089/*
1090 * Dynamic IP configuration -- DHCP, BOOTP, RARP.
1091 */
1092
1093#ifdef IPCONFIG_DYNAMIC
1094
1095static int __init ic_dynamic(void)
1096{
1097 int retries;
1098 struct ic_device *d;
1099 unsigned long start_jiffies, timeout, jiff;
1100 int do_bootp = ic_proto_have_if & IC_BOOTP;
1101 int do_rarp = ic_proto_have_if & IC_RARP;
1102
1103 /*
1104 * If none of DHCP/BOOTP/RARP was selected, return with an error.
1105 * This routine gets only called when some pieces of information
1106 * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
1107 */
1108 if (!ic_proto_enabled) {
1109 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1110 return -1;
1111 }
1112
1113#ifdef IPCONFIG_BOOTP
1114 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
1115 printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
1116#endif
1117#ifdef IPCONFIG_RARP
1118 if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
1119 printk(KERN_ERR "RARP: No suitable device found.\n");
1120#endif
1121
1122 if (!ic_proto_have_if)
1123 /* Error message already printed */
1124 return -1;
1125
1126 /*
1127 * Setup protocols
1128 */
1129#ifdef IPCONFIG_BOOTP
1130 if (do_bootp)
1131 ic_bootp_init();
1132#endif
1133#ifdef IPCONFIG_RARP
1134 if (do_rarp)
1135 ic_rarp_init();
1136#endif
1137
1138 /*
1139 * Send requests and wait, until we get an answer. This loop
1140 * seems to be a terrible waste of CPU time, but actually there is
1141 * only one process running at all, so we don't need to use any
1142 * scheduler functions.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001143 * [Actually we could now, but the nothing else running note still
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * applies.. - AC]
1145 */
1146 printk(KERN_NOTICE "Sending %s%s%s requests .",
1147 do_bootp
1148 ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
1149 (do_bootp && do_rarp) ? " and " : "",
1150 do_rarp ? "RARP" : "");
1151
1152 start_jiffies = jiffies;
1153 d = ic_first_dev;
1154 retries = CONF_SEND_RETRIES;
1155 get_random_bytes(&timeout, sizeof(timeout));
1156 timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001157 for (;;) {
Chris Friesen2513dfb2009-05-17 20:39:33 -07001158 /* Track the device we are configuring */
1159 ic_dev_xid = d->xid;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161#ifdef IPCONFIG_BOOTP
1162 if (do_bootp && (d->able & IC_BOOTP))
1163 ic_bootp_send_if(d, jiffies - start_jiffies);
1164#endif
1165#ifdef IPCONFIG_RARP
1166 if (do_rarp && (d->able & IC_RARP))
1167 ic_rarp_send_if(d);
1168#endif
1169
1170 jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001171 while (time_before(jiffies, jiff) && !ic_got_reply)
1172 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173#ifdef IPCONFIG_DHCP
1174 /* DHCP isn't done until we get a DHCPACK. */
Joe Perches9d4fb272009-11-23 10:41:23 -08001175 if ((ic_got_reply & IC_BOOTP) &&
1176 (ic_proto_enabled & IC_USE_DHCP) &&
1177 ic_dhcp_msgtype != DHCPACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ic_got_reply = 0;
1179 printk(",");
1180 continue;
1181 }
1182#endif /* IPCONFIG_DHCP */
1183
1184 if (ic_got_reply) {
1185 printk(" OK\n");
1186 break;
1187 }
1188
1189 if ((d = d->next))
1190 continue;
1191
1192 if (! --retries) {
1193 printk(" timed out!\n");
1194 break;
1195 }
1196
1197 d = ic_first_dev;
1198
1199 timeout = timeout CONF_TIMEOUT_MULT;
1200 if (timeout > CONF_TIMEOUT_MAX)
1201 timeout = CONF_TIMEOUT_MAX;
1202
1203 printk(".");
1204 }
1205
1206#ifdef IPCONFIG_BOOTP
1207 if (do_bootp)
1208 ic_bootp_cleanup();
1209#endif
1210#ifdef IPCONFIG_RARP
1211 if (do_rarp)
1212 ic_rarp_cleanup();
1213#endif
1214
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001215 if (!ic_got_reply) {
Al Viro5a874db2006-11-08 00:19:38 -08001216 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -1;
Maxime Bizon7a1af5d2005-06-28 13:21:12 -07001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Harvey Harrison673d57e2008-10-31 00:53:57 -07001220 printk("IP-Config: Got %s answer from %pI4, ",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001221 ((ic_got_reply & IC_RARP) ? "RARP"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
Harvey Harrison673d57e2008-10-31 00:53:57 -07001223 &ic_servaddr);
1224 printk("my address is %pI4\n", &ic_myaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return 0;
1227}
1228
1229#endif /* IPCONFIG_DYNAMIC */
1230
1231#ifdef CONFIG_PROC_FS
1232
1233static int pnp_seq_show(struct seq_file *seq, void *v)
1234{
1235 int i;
1236
1237 if (ic_proto_used & IC_PROTO)
1238 seq_printf(seq, "#PROTO: %s\n",
1239 (ic_proto_used & IC_RARP) ? "RARP"
1240 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1241 else
1242 seq_puts(seq, "#MANUAL\n");
1243
1244 if (ic_domain[0])
1245 seq_printf(seq,
1246 "domain %s\n", ic_domain);
1247 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
Al Viro5a874db2006-11-08 00:19:38 -08001248 if (ic_nameservers[i] != NONE)
Harvey Harrison673d57e2008-10-31 00:53:57 -07001249 seq_printf(seq, "nameserver %pI4\n",
1250 &ic_nameservers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Al Viro5a874db2006-11-08 00:19:38 -08001252 if (ic_servaddr != NONE)
Harvey Harrison673d57e2008-10-31 00:53:57 -07001253 seq_printf(seq, "bootserver %pI4\n",
1254 &ic_servaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256}
1257
1258static int pnp_seq_open(struct inode *indoe, struct file *file)
1259{
1260 return single_open(file, pnp_seq_show, NULL);
1261}
1262
Arjan van de Ven9a321442007-02-12 00:55:35 -08001263static const struct file_operations pnp_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 .owner = THIS_MODULE,
1265 .open = pnp_seq_open,
1266 .read = seq_read,
1267 .llseek = seq_lseek,
1268 .release = single_release,
1269};
1270#endif /* CONFIG_PROC_FS */
1271
1272/*
1273 * Extract IP address from the parameter string if needed. Note that we
1274 * need to have root_server_addr set _before_ IPConfig gets called as it
1275 * can override it.
1276 */
Al Viro5a874db2006-11-08 00:19:38 -08001277__be32 __init root_nfs_parse_addr(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Al Viro5a874db2006-11-08 00:19:38 -08001279 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int octets = 0;
1281 char *cp, *cq;
1282
1283 cp = cq = name;
1284 while (octets < 4) {
1285 while (*cp >= '0' && *cp <= '9')
1286 cp++;
1287 if (cp == cq || cp - cq > 3)
1288 break;
1289 if (*cp == '.' || octets == 3)
1290 octets++;
1291 if (octets < 4)
1292 cp++;
1293 cq = cp;
1294 }
1295 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1296 if (*cp == ':')
1297 *cp++ = '\0';
1298 addr = in_aton(name);
1299 memmove(name, cp, strlen(cp) + 1);
1300 } else
Al Viro5a874db2006-11-08 00:19:38 -08001301 addr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 return addr;
1304}
1305
1306/*
1307 * IP Autoconfig dispatcher.
1308 */
1309
1310static int __init ip_auto_config(void)
1311{
Al Viro5a874db2006-11-08 00:19:38 -08001312 __be32 addr;
Benjamin Zores9d8dba62009-01-29 16:19:13 -08001313#ifdef IPCONFIG_DYNAMIC
1314 int retries = CONF_OPEN_RETRIES;
1315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001318 proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#endif /* CONFIG_PROC_FS */
1320
1321 if (!ic_enable)
1322 return 0;
1323
1324 DBG(("IP-Config: Entered.\n"));
1325#ifdef IPCONFIG_DYNAMIC
1326 try_try_again:
1327#endif
1328 /* Give hardware a chance to settle */
1329 msleep(CONF_PRE_OPEN);
1330
1331 /* Setup all network devices */
1332 if (ic_open_devs() < 0)
1333 return -1;
1334
1335 /* Give drivers a chance to settle */
1336 ssleep(CONF_POST_OPEN);
1337
1338 /*
1339 * If the config information is insufficient (e.g., our IP address or
1340 * IP address of the boot server is missing or we have multiple network
1341 * interfaces and no default was set), use BOOTP or RARP to get the
1342 * missing values.
1343 */
Al Viro5a874db2006-11-08 00:19:38 -08001344 if (ic_myaddr == NONE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345#ifdef CONFIG_ROOT_NFS
Joe Perches9d4fb272009-11-23 10:41:23 -08001346 (root_server_addr == NONE &&
1347 ic_servaddr == NONE &&
1348 ROOT_DEV == Root_NFS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#endif
1350 ic_first_dev->next) {
1351#ifdef IPCONFIG_DYNAMIC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (ic_dynamic() < 0) {
1353 ic_close_devs();
1354
1355 /*
1356 * I don't know why, but sometimes the
1357 * eepro100 driver (at least) gets upset and
1358 * doesn't work the first time it's opened.
1359 * But then if you close it and reopen it, it
1360 * works just fine. So we need to try that at
1361 * least once before giving up.
1362 *
1363 * Also, if the root will be NFS-mounted, we
1364 * have nowhere to go if DHCP fails. So we
1365 * just have to keep trying forever.
1366 *
1367 * -- Chip
1368 */
1369#ifdef CONFIG_ROOT_NFS
1370 if (ROOT_DEV == Root_NFS) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001371 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 "IP-Config: Retrying forever (NFS root)...\n");
1373 goto try_try_again;
1374 }
1375#endif
1376
1377 if (--retries) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001378 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 "IP-Config: Reopening network devices...\n");
1380 goto try_try_again;
1381 }
1382
1383 /* Oh, well. At least we tried. */
1384 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1385 return -1;
1386 }
1387#else /* !DYNAMIC */
1388 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1389 ic_close_devs();
1390 return -1;
1391#endif /* IPCONFIG_DYNAMIC */
1392 } else {
1393 /* Device selected manually or only one device -> use it */
1394 ic_dev = ic_first_dev->dev;
1395 }
1396
1397 addr = root_nfs_parse_addr(root_server_path);
Al Viro5a874db2006-11-08 00:19:38 -08001398 if (root_server_addr == NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 root_server_addr = addr;
1400
1401 /*
1402 * Use defaults whereever applicable.
1403 */
1404 if (ic_defaults() < 0)
1405 return -1;
1406
1407 /*
1408 * Close all network devices except the device we've
1409 * autoconfigured and set up routes.
1410 */
1411 ic_close_devs();
1412 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1413 return -1;
1414
1415 /*
1416 * Record which protocol was actually used.
1417 */
1418#ifdef IPCONFIG_DYNAMIC
1419 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1420#endif
1421
1422#ifndef IPCONFIG_SILENT
1423 /*
1424 * Clue in the operator.
1425 */
1426 printk("IP-Config: Complete:");
Uwe Kleine-Koenig9c004092008-02-17 22:28:32 -08001427 printk("\n device=%s", ic_dev->name);
Harvey Harrison673d57e2008-10-31 00:53:57 -07001428 printk(", addr=%pI4", &ic_myaddr);
1429 printk(", mask=%pI4", &ic_netmask);
1430 printk(", gw=%pI4", &ic_gateway);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 printk(",\n host=%s, domain=%s, nis-domain=%s",
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001432 utsname()->nodename, ic_domain, utsname()->domainname);
Harvey Harrison673d57e2008-10-31 00:53:57 -07001433 printk(",\n bootserver=%pI4", &ic_servaddr);
1434 printk(", rootserver=%pI4", &root_server_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(", rootpath=%s", root_server_path);
Chris Friesen9643f452009-05-19 15:31:50 -07001436 if (ic_dev_mtu)
1437 printk(", mtu=%d", ic_dev_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 printk("\n");
1439#endif /* !SILENT */
1440
1441 return 0;
1442}
1443
1444late_initcall(ip_auto_config);
1445
1446
1447/*
1448 * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
J. Bruce Fieldsdc7a0812009-10-27 14:41:35 -04001449 * command line parameter. See Documentation/filesystems/nfs/nfsroot.txt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
1451static int __init ic_proto_name(char *name)
1452{
1453 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1454 return 1;
1455 }
Simon Hormana6c05c32007-12-25 20:54:42 -08001456 if (!strcmp(name, "off") || !strcmp(name, "none")) {
Amos Waterland92ffb852008-01-05 23:23:06 -08001457 return 0;
Simon Hormana6c05c32007-12-25 20:54:42 -08001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459#ifdef CONFIG_IP_PNP_DHCP
1460 else if (!strcmp(name, "dhcp")) {
1461 ic_proto_enabled &= ~IC_RARP;
1462 return 1;
1463 }
1464#endif
1465#ifdef CONFIG_IP_PNP_BOOTP
1466 else if (!strcmp(name, "bootp")) {
1467 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1468 return 1;
1469 }
1470#endif
1471#ifdef CONFIG_IP_PNP_RARP
1472 else if (!strcmp(name, "rarp")) {
1473 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1474 return 1;
1475 }
1476#endif
1477#ifdef IPCONFIG_DYNAMIC
1478 else if (!strcmp(name, "both")) {
1479 ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
1480 return 1;
1481 }
1482#endif
1483 return 0;
1484}
1485
1486static int __init ip_auto_config_setup(char *addrs)
1487{
1488 char *cp, *ip, *dp;
1489 int num = 0;
1490
1491 ic_set_manually = 1;
Simon Horman9cecd072007-12-27 21:19:10 -08001492 ic_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Amos Waterland92ffb852008-01-05 23:23:06 -08001494 /*
1495 * If any dhcp, bootp etc options are set, leave autoconfig on
1496 * and skip the below static IP processing.
1497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (ic_proto_name(addrs))
1499 return 1;
1500
Amos Waterland92ffb852008-01-05 23:23:06 -08001501 /* If no static IP is given, turn off autoconfig and bail. */
1502 if (*addrs == 0 ||
1503 strcmp(addrs, "off") == 0 ||
1504 strcmp(addrs, "none") == 0) {
1505 ic_enable = 0;
1506 return 1;
1507 }
1508
1509 /* Parse string for static IP assignment. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 ip = addrs;
1511 while (ip && *ip) {
1512 if ((cp = strchr(ip, ':')))
1513 *cp++ = '\0';
1514 if (strlen(ip) > 0) {
1515 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1516 switch (num) {
1517 case 0:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001518 if ((ic_myaddr = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001519 ic_myaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case 1:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001522 if ((ic_servaddr = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001523 ic_servaddr = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 case 2:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001526 if ((ic_gateway = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001527 ic_gateway = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529 case 3:
Al Viroe6f1ceb2008-03-17 22:44:53 -07001530 if ((ic_netmask = in_aton(ip)) == ANY)
Al Viro5a874db2006-11-08 00:19:38 -08001531 ic_netmask = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533 case 4:
1534 if ((dp = strchr(ip, '.'))) {
1535 *dp++ = '\0';
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001536 strlcpy(utsname()->domainname, dp,
1537 sizeof(utsname()->domainname));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001539 strlcpy(utsname()->nodename, ip,
1540 sizeof(utsname()->nodename));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ic_host_name_set = 1;
1542 break;
1543 case 5:
1544 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1545 break;
1546 case 6:
Amos Waterland92ffb852008-01-05 23:23:06 -08001547 if (ic_proto_name(ip) == 0 &&
1548 ic_myaddr == NONE) {
1549 ic_enable = 0;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 break;
1552 }
1553 }
1554 ip = cp;
1555 num++;
1556 }
1557
1558 return 1;
1559}
1560
1561static int __init nfsaddrs_config_setup(char *addrs)
1562{
1563 return ip_auto_config_setup(addrs);
1564}
1565
Rainer Jochem62013db2007-11-14 02:18:39 -08001566static int __init vendor_class_identifier_setup(char *addrs)
1567{
1568 if (strlcpy(vendor_class_identifier, addrs,
1569 sizeof(vendor_class_identifier))
1570 >= sizeof(vendor_class_identifier))
1571 printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
1572 vendor_class_identifier);
1573 return 1;
1574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576__setup("ip=", ip_auto_config_setup);
1577__setup("nfsaddrs=", nfsaddrs_config_setup);
Rainer Jochem62013db2007-11-14 02:18:39 -08001578__setup("dhcpclass=", vendor_class_identifier_setup);