Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
15 | * | |
16 | * Fixed ip_auto_config_setup calling at startup in the new "Linker Magic" | |
17 | * initialization scheme. | |
18 | * - Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 08/11/1999 | |
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 | ||
34 | #include <linux/config.h> | |
35 | #include <linux/types.h> | |
36 | #include <linux/string.h> | |
37 | #include <linux/kernel.h> | |
38 | #include <linux/jiffies.h> | |
39 | #include <linux/random.h> | |
40 | #include <linux/init.h> | |
41 | #include <linux/utsname.h> | |
42 | #include <linux/in.h> | |
43 | #include <linux/if.h> | |
44 | #include <linux/inet.h> | |
45 | #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> | |
43d60661 | 57 | #include <linux/nfs_fs.h> |
1da177e4 LT |
58 | #include <net/arp.h> |
59 | #include <net/ip.h> | |
60 | #include <net/ipconfig.h> | |
61 | ||
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 */ | |
100 | #define CONF_NAMESERVERS_MAX 3 /* Maximum number of nameservers | |
101 | - '3' from resolv.h */ | |
102 | ||
103 | ||
104 | /* | |
105 | * Public IP configuration | |
106 | */ | |
107 | ||
108 | /* This is used by platforms which might be able to set the ipconfig | |
109 | * variables using firmware environment vars. If this is set, it will | |
110 | * ignore such firmware variables. | |
111 | */ | |
112 | int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */ | |
113 | ||
114 | static int ic_enable __initdata = 0; /* IP config enabled? */ | |
115 | ||
116 | /* Protocol choice */ | |
117 | int ic_proto_enabled __initdata = 0 | |
118 | #ifdef IPCONFIG_BOOTP | |
119 | | IC_BOOTP | |
120 | #endif | |
121 | #ifdef CONFIG_IP_PNP_DHCP | |
122 | | IC_USE_DHCP | |
123 | #endif | |
124 | #ifdef IPCONFIG_RARP | |
125 | | IC_RARP | |
126 | #endif | |
127 | ; | |
128 | ||
129 | static int ic_host_name_set __initdata = 0; /* Host name set by us? */ | |
130 | ||
131 | u32 ic_myaddr = INADDR_NONE; /* My IP address */ | |
132 | static u32 ic_netmask = INADDR_NONE; /* Netmask for local subnet */ | |
133 | u32 ic_gateway = INADDR_NONE; /* Gateway IP address */ | |
134 | ||
135 | u32 ic_servaddr = INADDR_NONE; /* Boot server IP address */ | |
136 | ||
137 | u32 root_server_addr = INADDR_NONE; /* Address of NFS server */ | |
138 | u8 root_server_path[256] = { 0, }; /* Path to mount as root */ | |
139 | ||
140 | /* Persistent data: */ | |
141 | ||
142 | static int ic_proto_used; /* Protocol used, if any */ | |
143 | static u32 ic_nameservers[CONF_NAMESERVERS_MAX]; /* DNS Server IP addresses */ | |
144 | static u8 ic_domain[64]; /* DNS (not NIS) domain name */ | |
145 | ||
146 | /* | |
147 | * Private state. | |
148 | */ | |
149 | ||
150 | /* Name of user-selected boot device */ | |
151 | static char user_dev_name[IFNAMSIZ] __initdata = { 0, }; | |
152 | ||
153 | /* Protocols supported by available interfaces */ | |
154 | static int ic_proto_have_if __initdata = 0; | |
155 | ||
156 | #ifdef IPCONFIG_DYNAMIC | |
157 | static DEFINE_SPINLOCK(ic_recv_lock); | |
158 | static volatile int ic_got_reply __initdata = 0; /* Proto(s) that replied */ | |
159 | #endif | |
160 | #ifdef IPCONFIG_DHCP | |
161 | static int ic_dhcp_msgtype __initdata = 0; /* DHCP msg type received */ | |
162 | #endif | |
163 | ||
164 | ||
165 | /* | |
166 | * Network devices | |
167 | */ | |
168 | ||
169 | struct ic_device { | |
170 | struct ic_device *next; | |
171 | struct net_device *dev; | |
172 | unsigned short flags; | |
173 | short able; | |
174 | u32 xid; | |
175 | }; | |
176 | ||
177 | static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */ | |
178 | static struct net_device *ic_dev __initdata = NULL; /* Selected device */ | |
179 | ||
180 | static int __init ic_open_devs(void) | |
181 | { | |
182 | struct ic_device *d, **last; | |
183 | struct net_device *dev; | |
184 | unsigned short oflags; | |
185 | ||
186 | last = &ic_first_dev; | |
187 | rtnl_shlock(); | |
188 | ||
189 | /* bring loopback device up first */ | |
190 | if (dev_change_flags(&loopback_dev, loopback_dev.flags | IFF_UP) < 0) | |
191 | printk(KERN_ERR "IP-Config: Failed to open %s\n", loopback_dev.name); | |
192 | ||
193 | for (dev = dev_base; dev; dev = dev->next) { | |
194 | if (dev == &loopback_dev) | |
195 | continue; | |
196 | if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) : | |
197 | (!(dev->flags & IFF_LOOPBACK) && | |
198 | (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) && | |
199 | strncmp(dev->name, "dummy", 5))) { | |
200 | int able = 0; | |
201 | if (dev->mtu >= 364) | |
202 | able |= IC_BOOTP; | |
203 | else | |
204 | printk(KERN_WARNING "DHCP/BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu); | |
205 | if (!(dev->flags & IFF_NOARP)) | |
206 | able |= IC_RARP; | |
207 | able &= ic_proto_enabled; | |
208 | if (ic_proto_enabled && !able) | |
209 | continue; | |
210 | oflags = dev->flags; | |
211 | if (dev_change_flags(dev, oflags | IFF_UP) < 0) { | |
212 | printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name); | |
213 | continue; | |
214 | } | |
215 | if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL))) { | |
216 | rtnl_shunlock(); | |
217 | return -1; | |
218 | } | |
219 | d->dev = dev; | |
220 | *last = d; | |
221 | last = &d->next; | |
222 | d->flags = oflags; | |
223 | d->able = able; | |
224 | if (able & IC_BOOTP) | |
225 | get_random_bytes(&d->xid, sizeof(u32)); | |
226 | else | |
227 | d->xid = 0; | |
228 | ic_proto_have_if |= able; | |
229 | DBG(("IP-Config: %s UP (able=%d, xid=%08x)\n", | |
230 | dev->name, able, d->xid)); | |
231 | } | |
232 | } | |
233 | rtnl_shunlock(); | |
234 | ||
235 | *last = NULL; | |
236 | ||
237 | if (!ic_first_dev) { | |
238 | if (user_dev_name[0]) | |
239 | printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name); | |
240 | else | |
241 | printk(KERN_ERR "IP-Config: No network devices available.\n"); | |
242 | return -1; | |
243 | } | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static void __init ic_close_devs(void) | |
248 | { | |
249 | struct ic_device *d, *next; | |
250 | struct net_device *dev; | |
251 | ||
252 | rtnl_shlock(); | |
253 | next = ic_first_dev; | |
254 | while ((d = next)) { | |
255 | next = d->next; | |
256 | dev = d->dev; | |
257 | if (dev != ic_dev) { | |
258 | DBG(("IP-Config: Downing %s\n", dev->name)); | |
259 | dev_change_flags(dev, d->flags); | |
260 | } | |
261 | kfree(d); | |
262 | } | |
263 | rtnl_shunlock(); | |
264 | } | |
265 | ||
266 | /* | |
267 | * Interface to various network functions. | |
268 | */ | |
269 | ||
270 | static inline void | |
271 | set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port) | |
272 | { | |
273 | sin->sin_family = AF_INET; | |
274 | sin->sin_addr.s_addr = addr; | |
275 | sin->sin_port = port; | |
276 | } | |
277 | ||
278 | static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg) | |
279 | { | |
280 | int res; | |
281 | ||
282 | mm_segment_t oldfs = get_fs(); | |
283 | set_fs(get_ds()); | |
284 | res = devinet_ioctl(cmd, (struct ifreq __user *) arg); | |
285 | set_fs(oldfs); | |
286 | return res; | |
287 | } | |
288 | ||
289 | static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg) | |
290 | { | |
291 | int res; | |
292 | ||
293 | mm_segment_t oldfs = get_fs(); | |
294 | set_fs(get_ds()); | |
295 | res = ip_rt_ioctl(cmd, (void __user *) arg); | |
296 | set_fs(oldfs); | |
297 | return res; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Set up interface addresses and routes. | |
302 | */ | |
303 | ||
304 | static int __init ic_setup_if(void) | |
305 | { | |
306 | struct ifreq ir; | |
307 | struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr; | |
308 | int err; | |
309 | ||
310 | memset(&ir, 0, sizeof(ir)); | |
311 | strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name); | |
312 | set_sockaddr(sin, ic_myaddr, 0); | |
313 | if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) { | |
314 | printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err); | |
315 | return -1; | |
316 | } | |
317 | set_sockaddr(sin, ic_netmask, 0); | |
318 | if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) { | |
319 | printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err); | |
320 | return -1; | |
321 | } | |
322 | set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0); | |
323 | if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) { | |
324 | printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err); | |
325 | return -1; | |
326 | } | |
327 | return 0; | |
328 | } | |
329 | ||
330 | static int __init ic_setup_routes(void) | |
331 | { | |
332 | /* No need to setup device routes, only the default route... */ | |
333 | ||
334 | if (ic_gateway != INADDR_NONE) { | |
335 | struct rtentry rm; | |
336 | int err; | |
337 | ||
338 | memset(&rm, 0, sizeof(rm)); | |
339 | if ((ic_gateway ^ ic_myaddr) & ic_netmask) { | |
340 | printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n"); | |
341 | return -1; | |
342 | } | |
343 | set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0); | |
344 | set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0); | |
345 | set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0); | |
346 | rm.rt_flags = RTF_UP | RTF_GATEWAY; | |
347 | if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) { | |
348 | printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err); | |
349 | return -1; | |
350 | } | |
351 | } | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | /* | |
357 | * Fill in default values for all missing parameters. | |
358 | */ | |
359 | ||
360 | static int __init ic_defaults(void) | |
361 | { | |
362 | /* | |
363 | * At this point we have no userspace running so need not | |
364 | * claim locks on system_utsname | |
365 | */ | |
366 | ||
367 | if (!ic_host_name_set) | |
368 | sprintf(system_utsname.nodename, "%u.%u.%u.%u", NIPQUAD(ic_myaddr)); | |
369 | ||
370 | if (root_server_addr == INADDR_NONE) | |
371 | root_server_addr = ic_servaddr; | |
372 | ||
373 | if (ic_netmask == INADDR_NONE) { | |
374 | if (IN_CLASSA(ntohl(ic_myaddr))) | |
375 | ic_netmask = htonl(IN_CLASSA_NET); | |
376 | else if (IN_CLASSB(ntohl(ic_myaddr))) | |
377 | ic_netmask = htonl(IN_CLASSB_NET); | |
378 | else if (IN_CLASSC(ntohl(ic_myaddr))) | |
379 | ic_netmask = htonl(IN_CLASSC_NET); | |
380 | else { | |
381 | printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n", | |
382 | NIPQUAD(ic_myaddr)); | |
383 | return -1; | |
384 | } | |
385 | printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask)); | |
386 | } | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | /* | |
392 | * RARP support. | |
393 | */ | |
394 | ||
395 | #ifdef IPCONFIG_RARP | |
396 | ||
f2ccd8fa | 397 | static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); |
1da177e4 LT |
398 | |
399 | static struct packet_type rarp_packet_type __initdata = { | |
400 | .type = __constant_htons(ETH_P_RARP), | |
401 | .func = ic_rarp_recv, | |
402 | }; | |
403 | ||
404 | static inline void ic_rarp_init(void) | |
405 | { | |
406 | dev_add_pack(&rarp_packet_type); | |
407 | } | |
408 | ||
409 | static inline void ic_rarp_cleanup(void) | |
410 | { | |
411 | dev_remove_pack(&rarp_packet_type); | |
412 | } | |
413 | ||
414 | /* | |
415 | * Process received RARP packet. | |
416 | */ | |
417 | static int __init | |
f2ccd8fa | 418 | ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
1da177e4 LT |
419 | { |
420 | struct arphdr *rarp; | |
421 | unsigned char *rarp_ptr; | |
422 | unsigned long sip, tip; | |
423 | unsigned char *sha, *tha; /* s for "source", t for "target" */ | |
424 | struct ic_device *d; | |
425 | ||
426 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) | |
427 | return NET_RX_DROP; | |
428 | ||
429 | if (!pskb_may_pull(skb, sizeof(struct arphdr))) | |
430 | goto drop; | |
431 | ||
432 | /* Basic sanity checks can be done without the lock. */ | |
433 | rarp = (struct arphdr *)skb->h.raw; | |
434 | ||
435 | /* If this test doesn't pass, it's not IP, or we should | |
436 | * ignore it anyway. | |
437 | */ | |
438 | if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) | |
439 | goto drop; | |
440 | ||
441 | /* If it's not a RARP reply, delete it. */ | |
442 | if (rarp->ar_op != htons(ARPOP_RREPLY)) | |
443 | goto drop; | |
444 | ||
445 | /* If it's not Ethernet, delete it. */ | |
446 | if (rarp->ar_pro != htons(ETH_P_IP)) | |
447 | goto drop; | |
448 | ||
449 | if (!pskb_may_pull(skb, | |
450 | sizeof(struct arphdr) + | |
451 | (2 * dev->addr_len) + | |
452 | (2 * 4))) | |
453 | goto drop; | |
454 | ||
455 | /* OK, it is all there and looks valid, process... */ | |
456 | rarp = (struct arphdr *)skb->h.raw; | |
457 | rarp_ptr = (unsigned char *) (rarp + 1); | |
458 | ||
459 | /* One reply at a time, please. */ | |
460 | spin_lock(&ic_recv_lock); | |
461 | ||
462 | /* If we already have a reply, just drop the packet */ | |
463 | if (ic_got_reply) | |
464 | goto drop_unlock; | |
465 | ||
466 | /* Find the ic_device that the packet arrived on */ | |
467 | d = ic_first_dev; | |
468 | while (d && d->dev != dev) | |
469 | d = d->next; | |
470 | if (!d) | |
471 | goto drop_unlock; /* should never happen */ | |
472 | ||
473 | /* Extract variable-width fields */ | |
474 | sha = rarp_ptr; | |
475 | rarp_ptr += dev->addr_len; | |
476 | memcpy(&sip, rarp_ptr, 4); | |
477 | rarp_ptr += 4; | |
478 | tha = rarp_ptr; | |
479 | rarp_ptr += dev->addr_len; | |
480 | memcpy(&tip, rarp_ptr, 4); | |
481 | ||
482 | /* Discard packets which are not meant for us. */ | |
483 | if (memcmp(tha, dev->dev_addr, dev->addr_len)) | |
484 | goto drop_unlock; | |
485 | ||
486 | /* Discard packets which are not from specified server. */ | |
487 | if (ic_servaddr != INADDR_NONE && ic_servaddr != sip) | |
488 | goto drop_unlock; | |
489 | ||
490 | /* We have a winner! */ | |
491 | ic_dev = dev; | |
492 | if (ic_myaddr == INADDR_NONE) | |
493 | ic_myaddr = tip; | |
494 | ic_servaddr = sip; | |
495 | ic_got_reply = IC_RARP; | |
496 | ||
497 | drop_unlock: | |
498 | /* Show's over. Nothing to see here. */ | |
499 | spin_unlock(&ic_recv_lock); | |
500 | ||
501 | drop: | |
502 | /* Throw the packet out. */ | |
503 | kfree_skb(skb); | |
504 | return 0; | |
505 | } | |
506 | ||
507 | ||
508 | /* | |
509 | * Send RARP request packet over a single interface. | |
510 | */ | |
511 | static void __init ic_rarp_send_if(struct ic_device *d) | |
512 | { | |
513 | struct net_device *dev = d->dev; | |
514 | arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL, | |
515 | dev->dev_addr, dev->dev_addr); | |
516 | } | |
517 | #endif | |
518 | ||
519 | /* | |
520 | * DHCP/BOOTP support. | |
521 | */ | |
522 | ||
523 | #ifdef IPCONFIG_BOOTP | |
524 | ||
525 | struct bootp_pkt { /* BOOTP packet format */ | |
526 | struct iphdr iph; /* IP header */ | |
527 | struct udphdr udph; /* UDP header */ | |
528 | u8 op; /* 1=request, 2=reply */ | |
529 | u8 htype; /* HW address type */ | |
530 | u8 hlen; /* HW address length */ | |
531 | u8 hops; /* Used only by gateways */ | |
532 | u32 xid; /* Transaction ID */ | |
533 | u16 secs; /* Seconds since we started */ | |
534 | u16 flags; /* Just what it says */ | |
535 | u32 client_ip; /* Client's IP address if known */ | |
536 | u32 your_ip; /* Assigned IP address */ | |
537 | u32 server_ip; /* (Next, e.g. NFS) Server's IP address */ | |
538 | u32 relay_ip; /* IP address of BOOTP relay */ | |
539 | u8 hw_addr[16]; /* Client's HW address */ | |
540 | u8 serv_name[64]; /* Server host name */ | |
541 | u8 boot_file[128]; /* Name of boot file */ | |
542 | u8 exten[312]; /* DHCP options / BOOTP vendor extensions */ | |
543 | }; | |
544 | ||
545 | /* packet ops */ | |
546 | #define BOOTP_REQUEST 1 | |
547 | #define BOOTP_REPLY 2 | |
548 | ||
549 | /* DHCP message types */ | |
550 | #define DHCPDISCOVER 1 | |
551 | #define DHCPOFFER 2 | |
552 | #define DHCPREQUEST 3 | |
553 | #define DHCPDECLINE 4 | |
554 | #define DHCPACK 5 | |
555 | #define DHCPNAK 6 | |
556 | #define DHCPRELEASE 7 | |
557 | #define DHCPINFORM 8 | |
558 | ||
f2ccd8fa | 559 | static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev); |
1da177e4 LT |
560 | |
561 | static struct packet_type bootp_packet_type __initdata = { | |
562 | .type = __constant_htons(ETH_P_IP), | |
563 | .func = ic_bootp_recv, | |
564 | }; | |
565 | ||
566 | ||
567 | /* | |
568 | * Initialize DHCP/BOOTP extension fields in the request. | |
569 | */ | |
570 | ||
571 | static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 }; | |
572 | ||
573 | #ifdef IPCONFIG_DHCP | |
574 | ||
575 | static void __init | |
576 | ic_dhcp_init_options(u8 *options) | |
577 | { | |
578 | u8 mt = ((ic_servaddr == INADDR_NONE) | |
579 | ? DHCPDISCOVER : DHCPREQUEST); | |
580 | u8 *e = options; | |
581 | ||
582 | #ifdef IPCONFIG_DEBUG | |
583 | printk("DHCP: Sending message type %d\n", mt); | |
584 | #endif | |
585 | ||
586 | memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ | |
587 | e += 4; | |
588 | ||
589 | *e++ = 53; /* DHCP message type */ | |
590 | *e++ = 1; | |
591 | *e++ = mt; | |
592 | ||
593 | if (mt == DHCPREQUEST) { | |
594 | *e++ = 54; /* Server ID (IP address) */ | |
595 | *e++ = 4; | |
596 | memcpy(e, &ic_servaddr, 4); | |
597 | e += 4; | |
598 | ||
599 | *e++ = 50; /* Requested IP address */ | |
600 | *e++ = 4; | |
601 | memcpy(e, &ic_myaddr, 4); | |
602 | e += 4; | |
603 | } | |
604 | ||
605 | /* always? */ | |
606 | { | |
607 | static const u8 ic_req_params[] = { | |
608 | 1, /* Subnet mask */ | |
609 | 3, /* Default gateway */ | |
610 | 6, /* DNS server */ | |
611 | 12, /* Host name */ | |
612 | 15, /* Domain name */ | |
613 | 17, /* Boot path */ | |
614 | 40, /* NIS domain name */ | |
615 | }; | |
616 | ||
617 | *e++ = 55; /* Parameter request list */ | |
618 | *e++ = sizeof(ic_req_params); | |
619 | memcpy(e, ic_req_params, sizeof(ic_req_params)); | |
620 | e += sizeof(ic_req_params); | |
621 | } | |
622 | ||
623 | *e++ = 255; /* End of the list */ | |
624 | } | |
625 | ||
626 | #endif /* IPCONFIG_DHCP */ | |
627 | ||
628 | static void __init ic_bootp_init_ext(u8 *e) | |
629 | { | |
630 | memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */ | |
631 | e += 4; | |
632 | *e++ = 1; /* Subnet mask request */ | |
633 | *e++ = 4; | |
634 | e += 4; | |
635 | *e++ = 3; /* Default gateway request */ | |
636 | *e++ = 4; | |
637 | e += 4; | |
638 | *e++ = 5; /* Name server request */ | |
639 | *e++ = 8; | |
640 | e += 8; | |
641 | *e++ = 12; /* Host name request */ | |
642 | *e++ = 32; | |
643 | e += 32; | |
644 | *e++ = 40; /* NIS Domain name request */ | |
645 | *e++ = 32; | |
646 | e += 32; | |
647 | *e++ = 17; /* Boot path */ | |
648 | *e++ = 40; | |
649 | e += 40; | |
650 | ||
651 | *e++ = 57; /* set extension buffer size for reply */ | |
652 | *e++ = 2; | |
653 | *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */ | |
654 | *e++ = 150; | |
655 | ||
656 | *e++ = 255; /* End of the list */ | |
657 | } | |
658 | ||
659 | ||
660 | /* | |
661 | * Initialize the DHCP/BOOTP mechanism. | |
662 | */ | |
663 | static inline void ic_bootp_init(void) | |
664 | { | |
665 | int i; | |
666 | ||
667 | for (i = 0; i < CONF_NAMESERVERS_MAX; i++) | |
668 | ic_nameservers[i] = INADDR_NONE; | |
669 | ||
670 | dev_add_pack(&bootp_packet_type); | |
671 | } | |
672 | ||
673 | ||
674 | /* | |
675 | * DHCP/BOOTP cleanup. | |
676 | */ | |
677 | static inline void ic_bootp_cleanup(void) | |
678 | { | |
679 | dev_remove_pack(&bootp_packet_type); | |
680 | } | |
681 | ||
682 | ||
683 | /* | |
684 | * Send DHCP/BOOTP request to single interface. | |
685 | */ | |
686 | static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff) | |
687 | { | |
688 | struct net_device *dev = d->dev; | |
689 | struct sk_buff *skb; | |
690 | struct bootp_pkt *b; | |
691 | int hh_len = LL_RESERVED_SPACE(dev); | |
692 | struct iphdr *h; | |
693 | ||
694 | /* Allocate packet */ | |
695 | skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL); | |
696 | if (!skb) | |
697 | return; | |
698 | skb_reserve(skb, hh_len); | |
699 | b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt)); | |
700 | memset(b, 0, sizeof(struct bootp_pkt)); | |
701 | ||
702 | /* Construct IP header */ | |
703 | skb->nh.iph = h = &b->iph; | |
704 | h->version = 4; | |
705 | h->ihl = 5; | |
706 | h->tot_len = htons(sizeof(struct bootp_pkt)); | |
707 | h->frag_off = htons(IP_DF); | |
708 | h->ttl = 64; | |
709 | h->protocol = IPPROTO_UDP; | |
710 | h->daddr = INADDR_BROADCAST; | |
711 | h->check = ip_fast_csum((unsigned char *) h, h->ihl); | |
712 | ||
713 | /* Construct UDP header */ | |
714 | b->udph.source = htons(68); | |
715 | b->udph.dest = htons(67); | |
716 | b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr)); | |
717 | /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */ | |
718 | ||
719 | /* Construct DHCP/BOOTP header */ | |
720 | b->op = BOOTP_REQUEST; | |
721 | if (dev->type < 256) /* check for false types */ | |
722 | b->htype = dev->type; | |
723 | else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */ | |
724 | b->htype = ARPHRD_IEEE802; | |
725 | else if (dev->type == ARPHRD_FDDI) | |
726 | b->htype = ARPHRD_ETHER; | |
727 | else { | |
728 | printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name); | |
729 | b->htype = dev->type; /* can cause undefined behavior */ | |
730 | } | |
731 | b->hlen = dev->addr_len; | |
732 | b->your_ip = INADDR_NONE; | |
733 | b->server_ip = INADDR_NONE; | |
734 | memcpy(b->hw_addr, dev->dev_addr, dev->addr_len); | |
735 | b->secs = htons(jiffies_diff / HZ); | |
736 | b->xid = d->xid; | |
737 | ||
738 | /* add DHCP options or BOOTP extensions */ | |
739 | #ifdef IPCONFIG_DHCP | |
740 | if (ic_proto_enabled & IC_USE_DHCP) | |
741 | ic_dhcp_init_options(b->exten); | |
742 | else | |
743 | #endif | |
744 | ic_bootp_init_ext(b->exten); | |
745 | ||
746 | /* Chain packet down the line... */ | |
747 | skb->dev = dev; | |
748 | skb->protocol = htons(ETH_P_IP); | |
749 | if ((dev->hard_header && | |
750 | dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) || | |
751 | dev_queue_xmit(skb) < 0) | |
752 | printk("E"); | |
753 | } | |
754 | ||
755 | ||
756 | /* | |
757 | * Copy BOOTP-supplied string if not already set. | |
758 | */ | |
759 | static int __init ic_bootp_string(char *dest, char *src, int len, int max) | |
760 | { | |
761 | if (!len) | |
762 | return 0; | |
763 | if (len > max-1) | |
764 | len = max-1; | |
765 | memcpy(dest, src, len); | |
766 | dest[len] = '\0'; | |
767 | return 1; | |
768 | } | |
769 | ||
770 | ||
771 | /* | |
772 | * Process BOOTP extensions. | |
773 | */ | |
774 | static void __init ic_do_bootp_ext(u8 *ext) | |
775 | { | |
776 | u8 servers; | |
777 | int i; | |
778 | ||
779 | #ifdef IPCONFIG_DEBUG | |
780 | u8 *c; | |
781 | ||
782 | printk("DHCP/BOOTP: Got extension %d:",*ext); | |
783 | for(c=ext+2; c<ext+2+ext[1]; c++) | |
784 | printk(" %02x", *c); | |
785 | printk("\n"); | |
786 | #endif | |
787 | ||
788 | switch (*ext++) { | |
789 | case 1: /* Subnet mask */ | |
790 | if (ic_netmask == INADDR_NONE) | |
791 | memcpy(&ic_netmask, ext+1, 4); | |
792 | break; | |
793 | case 3: /* Default gateway */ | |
794 | if (ic_gateway == INADDR_NONE) | |
795 | memcpy(&ic_gateway, ext+1, 4); | |
796 | break; | |
797 | case 6: /* DNS server */ | |
798 | servers= *ext/4; | |
799 | if (servers > CONF_NAMESERVERS_MAX) | |
800 | servers = CONF_NAMESERVERS_MAX; | |
801 | for (i = 0; i < servers; i++) { | |
802 | if (ic_nameservers[i] == INADDR_NONE) | |
803 | memcpy(&ic_nameservers[i], ext+1+4*i, 4); | |
804 | } | |
805 | break; | |
806 | case 12: /* Host name */ | |
807 | ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN); | |
808 | ic_host_name_set = 1; | |
809 | break; | |
810 | case 15: /* Domain name (DNS) */ | |
811 | ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain)); | |
812 | break; | |
813 | case 17: /* Root path */ | |
814 | if (!root_server_path[0]) | |
815 | ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path)); | |
816 | break; | |
817 | case 40: /* NIS Domain name (_not_ DNS) */ | |
818 | ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN); | |
819 | break; | |
820 | } | |
821 | } | |
822 | ||
823 | ||
824 | /* | |
825 | * Receive BOOTP reply. | |
826 | */ | |
f2ccd8fa | 827 | static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
1da177e4 LT |
828 | { |
829 | struct bootp_pkt *b; | |
830 | struct iphdr *h; | |
831 | struct ic_device *d; | |
832 | int len, ext_len; | |
833 | ||
834 | /* Perform verifications before taking the lock. */ | |
835 | if (skb->pkt_type == PACKET_OTHERHOST) | |
836 | goto drop; | |
837 | ||
838 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) | |
839 | return NET_RX_DROP; | |
840 | ||
841 | if (!pskb_may_pull(skb, | |
842 | sizeof(struct iphdr) + | |
843 | sizeof(struct udphdr))) | |
844 | goto drop; | |
845 | ||
846 | b = (struct bootp_pkt *) skb->nh.iph; | |
847 | h = &b->iph; | |
848 | ||
849 | if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP) | |
850 | goto drop; | |
851 | ||
852 | /* Fragments are not supported */ | |
853 | if (h->frag_off & htons(IP_OFFSET | IP_MF)) { | |
854 | if (net_ratelimit()) | |
855 | printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented " | |
856 | "reply.\n"); | |
857 | goto drop; | |
858 | } | |
859 | ||
860 | if (skb->len < ntohs(h->tot_len)) | |
861 | goto drop; | |
862 | ||
863 | if (ip_fast_csum((char *) h, h->ihl)) | |
864 | goto drop; | |
865 | ||
866 | if (b->udph.source != htons(67) || b->udph.dest != htons(68)) | |
867 | goto drop; | |
868 | ||
869 | if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr)) | |
870 | goto drop; | |
871 | ||
872 | len = ntohs(b->udph.len) - sizeof(struct udphdr); | |
873 | ext_len = len - (sizeof(*b) - | |
874 | sizeof(struct iphdr) - | |
875 | sizeof(struct udphdr) - | |
876 | sizeof(b->exten)); | |
877 | if (ext_len < 0) | |
878 | goto drop; | |
879 | ||
880 | /* Ok the front looks good, make sure we can get at the rest. */ | |
881 | if (!pskb_may_pull(skb, skb->len)) | |
882 | goto drop; | |
883 | ||
884 | b = (struct bootp_pkt *) skb->nh.iph; | |
885 | h = &b->iph; | |
886 | ||
887 | /* One reply at a time, please. */ | |
888 | spin_lock(&ic_recv_lock); | |
889 | ||
890 | /* If we already have a reply, just drop the packet */ | |
891 | if (ic_got_reply) | |
892 | goto drop_unlock; | |
893 | ||
894 | /* Find the ic_device that the packet arrived on */ | |
895 | d = ic_first_dev; | |
896 | while (d && d->dev != dev) | |
897 | d = d->next; | |
898 | if (!d) | |
899 | goto drop_unlock; /* should never happen */ | |
900 | ||
901 | /* Is it a reply to our BOOTP request? */ | |
902 | if (b->op != BOOTP_REPLY || | |
903 | b->xid != d->xid) { | |
904 | if (net_ratelimit()) | |
905 | printk(KERN_ERR "DHCP/BOOTP: Reply not for us, " | |
906 | "op[%x] xid[%x]\n", | |
907 | b->op, b->xid); | |
908 | goto drop_unlock; | |
909 | } | |
910 | ||
911 | /* Parse extensions */ | |
912 | if (ext_len >= 4 && | |
913 | !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */ | |
914 | u8 *end = (u8 *) b + ntohs(b->iph.tot_len); | |
915 | u8 *ext; | |
916 | ||
917 | #ifdef IPCONFIG_DHCP | |
918 | if (ic_proto_enabled & IC_USE_DHCP) { | |
919 | u32 server_id = INADDR_NONE; | |
920 | int mt = 0; | |
921 | ||
922 | ext = &b->exten[4]; | |
923 | while (ext < end && *ext != 0xff) { | |
924 | u8 *opt = ext++; | |
925 | if (*opt == 0) /* Padding */ | |
926 | continue; | |
927 | ext += *ext + 1; | |
928 | if (ext >= end) | |
929 | break; | |
930 | switch (*opt) { | |
931 | case 53: /* Message type */ | |
932 | if (opt[1]) | |
933 | mt = opt[2]; | |
934 | break; | |
935 | case 54: /* Server ID (IP address) */ | |
936 | if (opt[1] >= 4) | |
937 | memcpy(&server_id, opt + 2, 4); | |
938 | break; | |
939 | }; | |
940 | } | |
941 | ||
942 | #ifdef IPCONFIG_DEBUG | |
943 | printk("DHCP: Got message type %d\n", mt); | |
944 | #endif | |
945 | ||
946 | switch (mt) { | |
947 | case DHCPOFFER: | |
948 | /* While in the process of accepting one offer, | |
949 | * ignore all others. | |
950 | */ | |
951 | if (ic_myaddr != INADDR_NONE) | |
952 | goto drop_unlock; | |
953 | ||
954 | /* Let's accept that offer. */ | |
955 | ic_myaddr = b->your_ip; | |
956 | ic_servaddr = server_id; | |
957 | #ifdef IPCONFIG_DEBUG | |
958 | printk("DHCP: Offered address %u.%u.%u.%u", | |
959 | NIPQUAD(ic_myaddr)); | |
960 | printk(" by server %u.%u.%u.%u\n", | |
961 | NIPQUAD(ic_servaddr)); | |
962 | #endif | |
963 | /* The DHCP indicated server address takes | |
964 | * precedence over the bootp header one if | |
965 | * they are different. | |
966 | */ | |
967 | if ((server_id != INADDR_NONE) && | |
968 | (b->server_ip != server_id)) | |
969 | b->server_ip = ic_servaddr; | |
970 | break; | |
971 | ||
972 | case DHCPACK: | |
973 | if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0) | |
974 | goto drop_unlock; | |
975 | ||
976 | /* Yeah! */ | |
977 | break; | |
978 | ||
979 | default: | |
980 | /* Urque. Forget it*/ | |
981 | ic_myaddr = INADDR_NONE; | |
982 | ic_servaddr = INADDR_NONE; | |
983 | goto drop_unlock; | |
984 | }; | |
985 | ||
986 | ic_dhcp_msgtype = mt; | |
987 | ||
988 | } | |
989 | #endif /* IPCONFIG_DHCP */ | |
990 | ||
991 | ext = &b->exten[4]; | |
992 | while (ext < end && *ext != 0xff) { | |
993 | u8 *opt = ext++; | |
994 | if (*opt == 0) /* Padding */ | |
995 | continue; | |
996 | ext += *ext + 1; | |
997 | if (ext < end) | |
998 | ic_do_bootp_ext(opt); | |
999 | } | |
1000 | } | |
1001 | ||
1002 | /* We have a winner! */ | |
1003 | ic_dev = dev; | |
1004 | ic_myaddr = b->your_ip; | |
1005 | ic_servaddr = b->server_ip; | |
1006 | if (ic_gateway == INADDR_NONE && b->relay_ip) | |
1007 | ic_gateway = b->relay_ip; | |
1008 | if (ic_nameservers[0] == INADDR_NONE) | |
1009 | ic_nameservers[0] = ic_servaddr; | |
1010 | ic_got_reply = IC_BOOTP; | |
1011 | ||
1012 | drop_unlock: | |
1013 | /* Show's over. Nothing to see here. */ | |
1014 | spin_unlock(&ic_recv_lock); | |
1015 | ||
1016 | drop: | |
1017 | /* Throw the packet out. */ | |
1018 | kfree_skb(skb); | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | ||
1024 | #endif | |
1025 | ||
1026 | ||
1027 | /* | |
1028 | * Dynamic IP configuration -- DHCP, BOOTP, RARP. | |
1029 | */ | |
1030 | ||
1031 | #ifdef IPCONFIG_DYNAMIC | |
1032 | ||
1033 | static int __init ic_dynamic(void) | |
1034 | { | |
1035 | int retries; | |
1036 | struct ic_device *d; | |
1037 | unsigned long start_jiffies, timeout, jiff; | |
1038 | int do_bootp = ic_proto_have_if & IC_BOOTP; | |
1039 | int do_rarp = ic_proto_have_if & IC_RARP; | |
1040 | ||
1041 | /* | |
1042 | * If none of DHCP/BOOTP/RARP was selected, return with an error. | |
1043 | * This routine gets only called when some pieces of information | |
1044 | * are missing, and without DHCP/BOOTP/RARP we are unable to get it. | |
1045 | */ | |
1046 | if (!ic_proto_enabled) { | |
1047 | printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n"); | |
1048 | return -1; | |
1049 | } | |
1050 | ||
1051 | #ifdef IPCONFIG_BOOTP | |
1052 | if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP) | |
1053 | printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n"); | |
1054 | #endif | |
1055 | #ifdef IPCONFIG_RARP | |
1056 | if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP) | |
1057 | printk(KERN_ERR "RARP: No suitable device found.\n"); | |
1058 | #endif | |
1059 | ||
1060 | if (!ic_proto_have_if) | |
1061 | /* Error message already printed */ | |
1062 | return -1; | |
1063 | ||
1064 | /* | |
1065 | * Setup protocols | |
1066 | */ | |
1067 | #ifdef IPCONFIG_BOOTP | |
1068 | if (do_bootp) | |
1069 | ic_bootp_init(); | |
1070 | #endif | |
1071 | #ifdef IPCONFIG_RARP | |
1072 | if (do_rarp) | |
1073 | ic_rarp_init(); | |
1074 | #endif | |
1075 | ||
1076 | /* | |
1077 | * Send requests and wait, until we get an answer. This loop | |
1078 | * seems to be a terrible waste of CPU time, but actually there is | |
1079 | * only one process running at all, so we don't need to use any | |
1080 | * scheduler functions. | |
1081 | * [Actually we could now, but the nothing else running note still | |
1082 | * applies.. - AC] | |
1083 | */ | |
1084 | printk(KERN_NOTICE "Sending %s%s%s requests .", | |
1085 | do_bootp | |
1086 | ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "", | |
1087 | (do_bootp && do_rarp) ? " and " : "", | |
1088 | do_rarp ? "RARP" : ""); | |
1089 | ||
1090 | start_jiffies = jiffies; | |
1091 | d = ic_first_dev; | |
1092 | retries = CONF_SEND_RETRIES; | |
1093 | get_random_bytes(&timeout, sizeof(timeout)); | |
1094 | timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM); | |
1095 | for(;;) { | |
1096 | #ifdef IPCONFIG_BOOTP | |
1097 | if (do_bootp && (d->able & IC_BOOTP)) | |
1098 | ic_bootp_send_if(d, jiffies - start_jiffies); | |
1099 | #endif | |
1100 | #ifdef IPCONFIG_RARP | |
1101 | if (do_rarp && (d->able & IC_RARP)) | |
1102 | ic_rarp_send_if(d); | |
1103 | #endif | |
1104 | ||
1105 | jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout); | |
1106 | while (time_before(jiffies, jiff) && !ic_got_reply) { | |
1107 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1108 | schedule_timeout(1); | |
1109 | } | |
1110 | #ifdef IPCONFIG_DHCP | |
1111 | /* DHCP isn't done until we get a DHCPACK. */ | |
1112 | if ((ic_got_reply & IC_BOOTP) | |
1113 | && (ic_proto_enabled & IC_USE_DHCP) | |
1114 | && ic_dhcp_msgtype != DHCPACK) | |
1115 | { | |
1116 | ic_got_reply = 0; | |
1117 | printk(","); | |
1118 | continue; | |
1119 | } | |
1120 | #endif /* IPCONFIG_DHCP */ | |
1121 | ||
1122 | if (ic_got_reply) { | |
1123 | printk(" OK\n"); | |
1124 | break; | |
1125 | } | |
1126 | ||
1127 | if ((d = d->next)) | |
1128 | continue; | |
1129 | ||
1130 | if (! --retries) { | |
1131 | printk(" timed out!\n"); | |
1132 | break; | |
1133 | } | |
1134 | ||
1135 | d = ic_first_dev; | |
1136 | ||
1137 | timeout = timeout CONF_TIMEOUT_MULT; | |
1138 | if (timeout > CONF_TIMEOUT_MAX) | |
1139 | timeout = CONF_TIMEOUT_MAX; | |
1140 | ||
1141 | printk("."); | |
1142 | } | |
1143 | ||
1144 | #ifdef IPCONFIG_BOOTP | |
1145 | if (do_bootp) | |
1146 | ic_bootp_cleanup(); | |
1147 | #endif | |
1148 | #ifdef IPCONFIG_RARP | |
1149 | if (do_rarp) | |
1150 | ic_rarp_cleanup(); | |
1151 | #endif | |
1152 | ||
7a1af5d7 MB |
1153 | if (!ic_got_reply) { |
1154 | ic_myaddr = INADDR_NONE; | |
1da177e4 | 1155 | return -1; |
7a1af5d7 | 1156 | } |
1da177e4 LT |
1157 | |
1158 | printk("IP-Config: Got %s answer from %u.%u.%u.%u, ", | |
1159 | ((ic_got_reply & IC_RARP) ? "RARP" | |
1160 | : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"), | |
1161 | NIPQUAD(ic_servaddr)); | |
1162 | printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr)); | |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | #endif /* IPCONFIG_DYNAMIC */ | |
1168 | ||
1169 | #ifdef CONFIG_PROC_FS | |
1170 | ||
1171 | static int pnp_seq_show(struct seq_file *seq, void *v) | |
1172 | { | |
1173 | int i; | |
1174 | ||
1175 | if (ic_proto_used & IC_PROTO) | |
1176 | seq_printf(seq, "#PROTO: %s\n", | |
1177 | (ic_proto_used & IC_RARP) ? "RARP" | |
1178 | : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP"); | |
1179 | else | |
1180 | seq_puts(seq, "#MANUAL\n"); | |
1181 | ||
1182 | if (ic_domain[0]) | |
1183 | seq_printf(seq, | |
1184 | "domain %s\n", ic_domain); | |
1185 | for (i = 0; i < CONF_NAMESERVERS_MAX; i++) { | |
1186 | if (ic_nameservers[i] != INADDR_NONE) | |
1187 | seq_printf(seq, | |
1188 | "nameserver %u.%u.%u.%u\n", | |
1189 | NIPQUAD(ic_nameservers[i])); | |
1190 | } | |
1191 | if (ic_servaddr != INADDR_NONE) | |
1192 | seq_printf(seq, | |
1193 | "bootserver %u.%u.%u.%u\n", | |
1194 | NIPQUAD(ic_servaddr)); | |
1195 | return 0; | |
1196 | } | |
1197 | ||
1198 | static int pnp_seq_open(struct inode *indoe, struct file *file) | |
1199 | { | |
1200 | return single_open(file, pnp_seq_show, NULL); | |
1201 | } | |
1202 | ||
1203 | static struct file_operations pnp_seq_fops = { | |
1204 | .owner = THIS_MODULE, | |
1205 | .open = pnp_seq_open, | |
1206 | .read = seq_read, | |
1207 | .llseek = seq_lseek, | |
1208 | .release = single_release, | |
1209 | }; | |
1210 | #endif /* CONFIG_PROC_FS */ | |
1211 | ||
1212 | /* | |
1213 | * Extract IP address from the parameter string if needed. Note that we | |
1214 | * need to have root_server_addr set _before_ IPConfig gets called as it | |
1215 | * can override it. | |
1216 | */ | |
1217 | u32 __init root_nfs_parse_addr(char *name) | |
1218 | { | |
1219 | u32 addr; | |
1220 | int octets = 0; | |
1221 | char *cp, *cq; | |
1222 | ||
1223 | cp = cq = name; | |
1224 | while (octets < 4) { | |
1225 | while (*cp >= '0' && *cp <= '9') | |
1226 | cp++; | |
1227 | if (cp == cq || cp - cq > 3) | |
1228 | break; | |
1229 | if (*cp == '.' || octets == 3) | |
1230 | octets++; | |
1231 | if (octets < 4) | |
1232 | cp++; | |
1233 | cq = cp; | |
1234 | } | |
1235 | if (octets == 4 && (*cp == ':' || *cp == '\0')) { | |
1236 | if (*cp == ':') | |
1237 | *cp++ = '\0'; | |
1238 | addr = in_aton(name); | |
1239 | memmove(name, cp, strlen(cp) + 1); | |
1240 | } else | |
1241 | addr = INADDR_NONE; | |
1242 | ||
1243 | return addr; | |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * IP Autoconfig dispatcher. | |
1248 | */ | |
1249 | ||
1250 | static int __init ip_auto_config(void) | |
1251 | { | |
1252 | u32 addr; | |
1253 | ||
1254 | #ifdef CONFIG_PROC_FS | |
1255 | proc_net_fops_create("pnp", S_IRUGO, &pnp_seq_fops); | |
1256 | #endif /* CONFIG_PROC_FS */ | |
1257 | ||
1258 | if (!ic_enable) | |
1259 | return 0; | |
1260 | ||
1261 | DBG(("IP-Config: Entered.\n")); | |
1262 | #ifdef IPCONFIG_DYNAMIC | |
1263 | try_try_again: | |
1264 | #endif | |
1265 | /* Give hardware a chance to settle */ | |
1266 | msleep(CONF_PRE_OPEN); | |
1267 | ||
1268 | /* Setup all network devices */ | |
1269 | if (ic_open_devs() < 0) | |
1270 | return -1; | |
1271 | ||
1272 | /* Give drivers a chance to settle */ | |
1273 | ssleep(CONF_POST_OPEN); | |
1274 | ||
1275 | /* | |
1276 | * If the config information is insufficient (e.g., our IP address or | |
1277 | * IP address of the boot server is missing or we have multiple network | |
1278 | * interfaces and no default was set), use BOOTP or RARP to get the | |
1279 | * missing values. | |
1280 | */ | |
1281 | if (ic_myaddr == INADDR_NONE || | |
1282 | #ifdef CONFIG_ROOT_NFS | |
1283 | (MAJOR(ROOT_DEV) == UNNAMED_MAJOR | |
1284 | && root_server_addr == INADDR_NONE | |
1285 | && ic_servaddr == INADDR_NONE) || | |
1286 | #endif | |
1287 | ic_first_dev->next) { | |
1288 | #ifdef IPCONFIG_DYNAMIC | |
1289 | ||
1290 | int retries = CONF_OPEN_RETRIES; | |
1291 | ||
1292 | if (ic_dynamic() < 0) { | |
1293 | ic_close_devs(); | |
1294 | ||
1295 | /* | |
1296 | * I don't know why, but sometimes the | |
1297 | * eepro100 driver (at least) gets upset and | |
1298 | * doesn't work the first time it's opened. | |
1299 | * But then if you close it and reopen it, it | |
1300 | * works just fine. So we need to try that at | |
1301 | * least once before giving up. | |
1302 | * | |
1303 | * Also, if the root will be NFS-mounted, we | |
1304 | * have nowhere to go if DHCP fails. So we | |
1305 | * just have to keep trying forever. | |
1306 | * | |
1307 | * -- Chip | |
1308 | */ | |
1309 | #ifdef CONFIG_ROOT_NFS | |
1310 | if (ROOT_DEV == Root_NFS) { | |
1311 | printk(KERN_ERR | |
1312 | "IP-Config: Retrying forever (NFS root)...\n"); | |
1313 | goto try_try_again; | |
1314 | } | |
1315 | #endif | |
1316 | ||
1317 | if (--retries) { | |
1318 | printk(KERN_ERR | |
1319 | "IP-Config: Reopening network devices...\n"); | |
1320 | goto try_try_again; | |
1321 | } | |
1322 | ||
1323 | /* Oh, well. At least we tried. */ | |
1324 | printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n"); | |
1325 | return -1; | |
1326 | } | |
1327 | #else /* !DYNAMIC */ | |
1328 | printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n"); | |
1329 | ic_close_devs(); | |
1330 | return -1; | |
1331 | #endif /* IPCONFIG_DYNAMIC */ | |
1332 | } else { | |
1333 | /* Device selected manually or only one device -> use it */ | |
1334 | ic_dev = ic_first_dev->dev; | |
1335 | } | |
1336 | ||
1337 | addr = root_nfs_parse_addr(root_server_path); | |
1338 | if (root_server_addr == INADDR_NONE) | |
1339 | root_server_addr = addr; | |
1340 | ||
1341 | /* | |
1342 | * Use defaults whereever applicable. | |
1343 | */ | |
1344 | if (ic_defaults() < 0) | |
1345 | return -1; | |
1346 | ||
1347 | /* | |
1348 | * Close all network devices except the device we've | |
1349 | * autoconfigured and set up routes. | |
1350 | */ | |
1351 | ic_close_devs(); | |
1352 | if (ic_setup_if() < 0 || ic_setup_routes() < 0) | |
1353 | return -1; | |
1354 | ||
1355 | /* | |
1356 | * Record which protocol was actually used. | |
1357 | */ | |
1358 | #ifdef IPCONFIG_DYNAMIC | |
1359 | ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP); | |
1360 | #endif | |
1361 | ||
1362 | #ifndef IPCONFIG_SILENT | |
1363 | /* | |
1364 | * Clue in the operator. | |
1365 | */ | |
1366 | printk("IP-Config: Complete:"); | |
1367 | printk("\n device=%s", ic_dev->name); | |
1368 | printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr)); | |
1369 | printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask)); | |
1370 | printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway)); | |
1371 | printk(",\n host=%s, domain=%s, nis-domain=%s", | |
1372 | system_utsname.nodename, ic_domain, system_utsname.domainname); | |
1373 | printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr)); | |
1374 | printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr)); | |
1375 | printk(", rootpath=%s", root_server_path); | |
1376 | printk("\n"); | |
1377 | #endif /* !SILENT */ | |
1378 | ||
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | late_initcall(ip_auto_config); | |
1383 | ||
1384 | ||
1385 | /* | |
1386 | * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel | |
1387 | * command line parameter. It consists of option fields separated by colons in | |
1388 | * the following order: | |
1389 | * | |
1390 | * <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<PROTO> | |
1391 | * | |
1392 | * Any of the fields can be empty which means to use a default value: | |
1393 | * <client-ip> - address given by BOOTP or RARP | |
1394 | * <server-ip> - address of host returning BOOTP or RARP packet | |
1395 | * <gw-ip> - none, or the address returned by BOOTP | |
1396 | * <netmask> - automatically determined from <client-ip>, or the | |
1397 | * one returned by BOOTP | |
1398 | * <host name> - <client-ip> in ASCII notation, or the name returned | |
1399 | * by BOOTP | |
1400 | * <device> - use all available devices | |
1401 | * <PROTO>: | |
1402 | * off|none - don't do autoconfig at all (DEFAULT) | |
1403 | * on|any - use any configured protocol | |
1404 | * dhcp|bootp|rarp - use only the specified protocol | |
1405 | * both - use both BOOTP and RARP (not DHCP) | |
1406 | */ | |
1407 | static int __init ic_proto_name(char *name) | |
1408 | { | |
1409 | if (!strcmp(name, "on") || !strcmp(name, "any")) { | |
1410 | return 1; | |
1411 | } | |
1412 | #ifdef CONFIG_IP_PNP_DHCP | |
1413 | else if (!strcmp(name, "dhcp")) { | |
1414 | ic_proto_enabled &= ~IC_RARP; | |
1415 | return 1; | |
1416 | } | |
1417 | #endif | |
1418 | #ifdef CONFIG_IP_PNP_BOOTP | |
1419 | else if (!strcmp(name, "bootp")) { | |
1420 | ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP); | |
1421 | return 1; | |
1422 | } | |
1423 | #endif | |
1424 | #ifdef CONFIG_IP_PNP_RARP | |
1425 | else if (!strcmp(name, "rarp")) { | |
1426 | ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP); | |
1427 | return 1; | |
1428 | } | |
1429 | #endif | |
1430 | #ifdef IPCONFIG_DYNAMIC | |
1431 | else if (!strcmp(name, "both")) { | |
1432 | ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */ | |
1433 | return 1; | |
1434 | } | |
1435 | #endif | |
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | static int __init ip_auto_config_setup(char *addrs) | |
1440 | { | |
1441 | char *cp, *ip, *dp; | |
1442 | int num = 0; | |
1443 | ||
1444 | ic_set_manually = 1; | |
1445 | ||
1446 | ic_enable = (*addrs && | |
1447 | (strcmp(addrs, "off") != 0) && | |
1448 | (strcmp(addrs, "none") != 0)); | |
1449 | if (!ic_enable) | |
1450 | return 1; | |
1451 | ||
1452 | if (ic_proto_name(addrs)) | |
1453 | return 1; | |
1454 | ||
1455 | /* Parse the whole string */ | |
1456 | ip = addrs; | |
1457 | while (ip && *ip) { | |
1458 | if ((cp = strchr(ip, ':'))) | |
1459 | *cp++ = '\0'; | |
1460 | if (strlen(ip) > 0) { | |
1461 | DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip)); | |
1462 | switch (num) { | |
1463 | case 0: | |
1464 | if ((ic_myaddr = in_aton(ip)) == INADDR_ANY) | |
1465 | ic_myaddr = INADDR_NONE; | |
1466 | break; | |
1467 | case 1: | |
1468 | if ((ic_servaddr = in_aton(ip)) == INADDR_ANY) | |
1469 | ic_servaddr = INADDR_NONE; | |
1470 | break; | |
1471 | case 2: | |
1472 | if ((ic_gateway = in_aton(ip)) == INADDR_ANY) | |
1473 | ic_gateway = INADDR_NONE; | |
1474 | break; | |
1475 | case 3: | |
1476 | if ((ic_netmask = in_aton(ip)) == INADDR_ANY) | |
1477 | ic_netmask = INADDR_NONE; | |
1478 | break; | |
1479 | case 4: | |
1480 | if ((dp = strchr(ip, '.'))) { | |
1481 | *dp++ = '\0'; | |
1482 | strlcpy(system_utsname.domainname, dp, | |
1483 | sizeof(system_utsname.domainname)); | |
1484 | } | |
1485 | strlcpy(system_utsname.nodename, ip, | |
1486 | sizeof(system_utsname.nodename)); | |
1487 | ic_host_name_set = 1; | |
1488 | break; | |
1489 | case 5: | |
1490 | strlcpy(user_dev_name, ip, sizeof(user_dev_name)); | |
1491 | break; | |
1492 | case 6: | |
1493 | ic_proto_name(ip); | |
1494 | break; | |
1495 | } | |
1496 | } | |
1497 | ip = cp; | |
1498 | num++; | |
1499 | } | |
1500 | ||
1501 | return 1; | |
1502 | } | |
1503 | ||
1504 | static int __init nfsaddrs_config_setup(char *addrs) | |
1505 | { | |
1506 | return ip_auto_config_setup(addrs); | |
1507 | } | |
1508 | ||
1509 | __setup("ip=", ip_auto_config_setup); | |
1510 | __setup("nfsaddrs=", nfsaddrs_config_setup); |