1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#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>
42#include <linux/inetdevice.h>
43#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>
55#include <linux/nfs_fs.h>
56#include <net/net_namespace.h>
57#include <net/arp.h>
58#include <net/ip.h>
59#include <net/ipconfig.h>
60#include <net/route.h>
61
62#include <asm/uaccess.h>
63#include <net/checksum.h>
64#include <asm/processor.h>
65
66
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
89#define CONF_PRE_OPEN 500
90#define CONF_POST_OPEN 1
91
92
93#define CONF_OPEN_RETRIES 2
94#define CONF_SEND_RETRIES 6
95#define CONF_INTER_TIMEOUT (HZ/2)
96#define CONF_BASE_TIMEOUT (HZ*2)
97#define CONF_TIMEOUT_RANDOM (HZ)
98#define CONF_TIMEOUT_MULT *7/4
99#define CONF_TIMEOUT_MAX (HZ*30)
100#define CONF_NAMESERVERS_MAX 3
101
102
103#define NONE cpu_to_be32(INADDR_NONE)
104#define ANY cpu_to_be32(INADDR_ANY)
105
106
107
108
109
110
111
112
113
114int ic_set_manually __initdata = 0;
115
116static int ic_enable __initdata = 0;
117
118
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;
132
133__be32 ic_myaddr = NONE;
134static __be32 ic_netmask = NONE;
135__be32 ic_gateway = NONE;
136
137__be32 ic_servaddr = NONE;
138
139__be32 root_server_addr = NONE;
140u8 root_server_path[256] = { 0, };
141
142u32 ic_dev_xid;
143
144
145static char vendor_class_identifier[253] __initdata;
146
147
148
149static int ic_proto_used;
150static __be32 ic_nameservers[CONF_NAMESERVERS_MAX];
151static u8 ic_domain[64];
152
153
154
155
156
157
158static char user_dev_name[IFNAMSIZ] __initdata = { 0, };
159
160
161static int ic_proto_have_if __initdata = 0;
162
163
164static int ic_dev_mtu __initdata = 0;
165
166#ifdef IPCONFIG_DYNAMIC
167static DEFINE_SPINLOCK(ic_recv_lock);
168static volatile int ic_got_reply __initdata = 0;
169#endif
170#ifdef IPCONFIG_DHCP
171static int ic_dhcp_msgtype __initdata = 0;
172#endif
173
174
175
176
177
178
179struct ic_device {
180 struct ic_device *next;
181 struct net_device *dev;
182 unsigned short flags;
183 short able;
184 __be32 xid;
185};
186
187static struct ic_device *ic_first_dev __initdata = NULL;
188static struct net_device *ic_dev __initdata = NULL;
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;
197 rtnl_lock();
198
199
200 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 }
206
207 for_each_netdev(&init_net, dev) {
208 if (dev->flags & IFF_LOOPBACK)
209 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))) {
230 rtnl_unlock();
231 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)
239 get_random_bytes(&d->xid, sizeof(__be32));
240 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 }
247 rtnl_unlock();
248
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
266 rtnl_lock();
267 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 }
277 rtnl_unlock();
278}
279
280
281
282
283
284static inline void
285set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
286{
287 sin->sin_family = AF_INET;
288 sin->sin_addr.s_addr = addr;
289 sin->sin_port = port;
290}
291
292static int __init ic_devinet_ioctl(unsigned int cmd, struct ifreq *arg)
293{
294 int res;
295
296 mm_segment_t oldfs = get_fs();
297 set_fs(get_ds());
298 res = devinet_ioctl(&init_net, cmd, (struct ifreq __user *) arg);
299 set_fs(oldfs);
300 return res;
301}
302
303static 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
314static 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());
320 res = ip_rt_ioctl(&init_net, cmd, (void __user *) arg);
321 set_fs(oldfs);
322 return res;
323}
324
325
326
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);
338 if ((err = ic_devinet_ioctl(SIOCSIFADDR, &ir)) < 0) {
339 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
340 return -1;
341 }
342 set_sockaddr(sin, ic_netmask, 0);
343 if ((err = ic_devinet_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
344 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);
348 if ((err = ic_devinet_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
349 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
350 return -1;
351 }
352
353
354
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 }
363 return 0;
364}
365
366static int __init ic_setup_routes(void)
367{
368
369
370 if (ic_gateway != NONE) {
371 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
394
395
396static int __init ic_defaults(void)
397{
398
399
400
401
402
403 if (!ic_host_name_set)
404 sprintf(init_utsname()->nodename, "%pI4", &ic_myaddr);
405
406 if (root_server_addr == NONE)
407 root_server_addr = ic_servaddr;
408
409 if (ic_netmask == NONE) {
410 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 {
417 printk(KERN_ERR "IP-Config: Unable to guess netmask for address %pI4\n",
418 &ic_myaddr);
419 return -1;
420 }
421 printk("IP-Config: Guessing netmask %pI4\n", &ic_netmask);
422 }
423
424 return 0;
425}
426
427
428
429
430
431#ifdef IPCONFIG_RARP
432
433static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
434
435static struct packet_type rarp_packet_type __initdata = {
436 .type = cpu_to_be16(ETH_P_RARP),
437 .func = ic_rarp_recv,
438};
439
440static inline void __init ic_rarp_init(void)
441{
442 dev_add_pack(&rarp_packet_type);
443}
444
445static inline void __init ic_rarp_cleanup(void)
446{
447 dev_remove_pack(&rarp_packet_type);
448}
449
450
451
452
453static int __init
454ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
455{
456 struct arphdr *rarp;
457 unsigned char *rarp_ptr;
458 __be32 sip, tip;
459 unsigned char *sha, *tha;
460 struct ic_device *d;
461
462 if (!net_eq(dev_net(dev), &init_net))
463 goto drop;
464
465 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
472 rarp = (struct arphdr *)skb_transport_header(skb);
473
474
475
476
477 if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
478 goto drop;
479
480
481 if (rarp->ar_op != htons(ARPOP_RREPLY))
482 goto drop;
483
484
485 if (rarp->ar_pro != htons(ETH_P_IP))
486 goto drop;
487
488 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
489 goto drop;
490
491
492 rarp = (struct arphdr *)skb_transport_header(skb);
493 rarp_ptr = (unsigned char *) (rarp + 1);
494
495
496 spin_lock(&ic_recv_lock);
497
498
499 if (ic_got_reply)
500 goto drop_unlock;
501
502
503 d = ic_first_dev;
504 while (d && d->dev != dev)
505 d = d->next;
506 if (!d)
507 goto drop_unlock;
508
509
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
519 if (memcmp(tha, dev->dev_addr, dev->addr_len))
520 goto drop_unlock;
521
522
523 if (ic_servaddr != NONE && ic_servaddr != sip)
524 goto drop_unlock;
525
526
527 ic_dev = dev;
528 if (ic_myaddr == NONE)
529 ic_myaddr = tip;
530 ic_servaddr = sip;
531 ic_got_reply = IC_RARP;
532
533drop_unlock:
534
535 spin_unlock(&ic_recv_lock);
536
537drop:
538
539 kfree_skb(skb);
540 return 0;
541}
542
543
544
545
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
557
558
559#ifdef IPCONFIG_BOOTP
560
561struct bootp_pkt {
562 struct iphdr iph;
563 struct udphdr udph;
564 u8 op;
565 u8 htype;
566 u8 hlen;
567 u8 hops;
568 __be32 xid;
569 __be16 secs;
570 __be16 flags;
571 __be32 client_ip;
572 __be32 your_ip;
573 __be32 server_ip;
574 __be32 relay_ip;
575 u8 hw_addr[16];
576 u8 serv_name[64];
577 u8 boot_file[128];
578 u8 exten[312];
579};
580
581
582#define BOOTP_REQUEST 1
583#define BOOTP_REPLY 2
584
585
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
595static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
596
597static struct packet_type bootp_packet_type __initdata = {
598 .type = cpu_to_be16(ETH_P_IP),
599 .func = ic_bootp_recv,
600};
601
602
603
604
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{
614 u8 mt = ((ic_servaddr == NONE)
615 ? DHCPDISCOVER : DHCPREQUEST);
616 u8 *e = options;
617 int len;
618
619#ifdef IPCONFIG_DEBUG
620 printk("DHCP: Sending message type %d\n", mt);
621#endif
622
623 memcpy(e, ic_bootp_cookie, 4);
624 e += 4;
625
626 *e++ = 53;
627 *e++ = 1;
628 *e++ = mt;
629
630 if (mt == DHCPREQUEST) {
631 *e++ = 54;
632 *e++ = 4;
633 memcpy(e, &ic_servaddr, 4);
634 e += 4;
635
636 *e++ = 50;
637 *e++ = 4;
638 memcpy(e, &ic_myaddr, 4);
639 e += 4;
640 }
641
642
643 {
644 static const u8 ic_req_params[] = {
645 1,
646 3,
647 6,
648 12,
649 15,
650 17,
651 26,
652 40,
653 };
654
655 *e++ = 55;
656 *e++ = sizeof(ic_req_params);
657 memcpy(e, ic_req_params, sizeof(ic_req_params));
658 e += sizeof(ic_req_params);
659
660 if (*vendor_class_identifier) {
661 printk(KERN_INFO "DHCP: sending class identifier \"%s\"\n",
662 vendor_class_identifier);
663 *e++ = 60;
664 len = strlen(vendor_class_identifier);
665 *e++ = len;
666 memcpy(e, vendor_class_identifier, len);
667 e += len;
668 }
669 }
670
671 *e++ = 255;
672}
673
674#endif
675
676static void __init ic_bootp_init_ext(u8 *e)
677{
678 memcpy(e, ic_bootp_cookie, 4);
679 e += 4;
680 *e++ = 1;
681 *e++ = 4;
682 e += 4;
683 *e++ = 3;
684 *e++ = 4;
685 e += 4;
686 *e++ = 5;
687 *e++ = 8;
688 e += 8;
689 *e++ = 12;
690 *e++ = 32;
691 e += 32;
692 *e++ = 40;
693 *e++ = 32;
694 e += 32;
695 *e++ = 17;
696 *e++ = 40;
697 e += 40;
698
699 *e++ = 57;
700 *e++ = 2;
701 *e++ = 1;
702 *e++ = 150;
703
704 *e++ = 255;
705}
706
707
708
709
710
711static inline void __init ic_bootp_init(void)
712{
713 int i;
714
715 for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
716 ic_nameservers[i] = NONE;
717
718 dev_add_pack(&bootp_packet_type);
719}
720
721
722
723
724
725static inline void __init ic_bootp_cleanup(void)
726{
727 dev_remove_pack(&bootp_packet_type);
728}
729
730
731
732
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;
739 struct iphdr *h;
740
741
742 skb = alloc_skb(sizeof(struct bootp_pkt) + LL_ALLOCATED_SPACE(dev) + 15,
743 GFP_KERNEL);
744 if (!skb)
745 return;
746 skb_reserve(skb, LL_RESERVED_SPACE(dev));
747 b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
748 memset(b, 0, sizeof(struct bootp_pkt));
749
750
751 skb_reset_network_header(skb);
752 h = ip_hdr(skb);
753 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;
759 h->daddr = htonl(INADDR_BROADCAST);
760 h->check = ip_fast_csum((unsigned char *) h, h->ihl);
761
762
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
767
768
769 b->op = BOOTP_REQUEST;
770 if (dev->type < 256)
771 b->htype = dev->type;
772 else if (dev->type == ARPHRD_IEEE802_TR)
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;
779 }
780
781
782 b->hlen = dev->addr_len;
783 memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
784 b->secs = htons(jiffies_diff / HZ);
785 b->xid = d->xid;
786
787
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
796 skb->dev = dev;
797 skb->protocol = htons(ETH_P_IP);
798 if (dev_hard_header(skb, dev, ntohs(skb->protocol),
799 dev->broadcast, dev->dev_addr, skb->len) < 0 ||
800 dev_queue_xmit(skb) < 0)
801 printk("E");
802}
803
804
805
806
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
822
823static void __init ic_do_bootp_ext(u8 *ext)
824{
825 u8 servers;
826 int i;
827 u16 mtu;
828
829#ifdef IPCONFIG_DEBUG
830 u8 *c;
831
832 printk("DHCP/BOOTP: Got extension %d:",*ext);
833 for (c=ext+2; c<ext+2+ext[1]; c++)
834 printk(" %02x", *c);
835 printk("\n");
836#endif
837
838 switch (*ext++) {
839 case 1:
840 if (ic_netmask == NONE)
841 memcpy(&ic_netmask, ext+1, 4);
842 break;
843 case 3:
844 if (ic_gateway == NONE)
845 memcpy(&ic_gateway, ext+1, 4);
846 break;
847 case 6:
848 servers= *ext/4;
849 if (servers > CONF_NAMESERVERS_MAX)
850 servers = CONF_NAMESERVERS_MAX;
851 for (i = 0; i < servers; i++) {
852 if (ic_nameservers[i] == NONE)
853 memcpy(&ic_nameservers[i], ext+1+4*i, 4);
854 }
855 break;
856 case 12:
857 ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
858 ic_host_name_set = 1;
859 break;
860 case 15:
861 ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
862 break;
863 case 17:
864 if (!root_server_path[0])
865 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
866 break;
867 case 26:
868 memcpy(&mtu, ext+1, sizeof(mtu));
869 ic_dev_mtu = ntohs(mtu);
870 break;
871 case 40:
872 ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
873 break;
874 }
875}
876
877
878
879
880
881static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
882{
883 struct bootp_pkt *b;
884 struct iphdr *h;
885 struct ic_device *d;
886 int len, ext_len;
887
888 if (!net_eq(dev_net(dev), &init_net))
889 goto drop;
890
891
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
903 b = (struct bootp_pkt *)skb_network_header(skb);
904 h = &b->iph;
905
906 if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
907 goto drop;
908
909
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
938 if (!pskb_may_pull(skb, skb->len))
939 goto drop;
940
941 b = (struct bootp_pkt *)skb_network_header(skb);
942 h = &b->iph;
943
944
945 spin_lock(&ic_recv_lock);
946
947
948 if (ic_got_reply)
949 goto drop_unlock;
950
951
952 d = ic_first_dev;
953 while (d && d->dev != dev)
954 d = d->next;
955 if (!d)
956 goto drop_unlock;
957
958
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
968
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
975
976 if (ext_len >= 4 &&
977 !memcmp(b->exten, ic_bootp_cookie, 4)) {
978 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
979 u8 *ext;
980
981#ifdef IPCONFIG_DHCP
982 if (ic_proto_enabled & IC_USE_DHCP) {
983 __be32 server_id = NONE;
984 int mt = 0;
985
986 ext = &b->exten[4];
987 while (ext < end && *ext != 0xff) {
988 u8 *opt = ext++;
989 if (*opt == 0)
990 continue;
991 ext += *ext + 1;
992 if (ext >= end)
993 break;
994 switch (*opt) {
995 case 53:
996 if (opt[1])
997 mt = opt[2];
998 break;
999 case 54:
1000 if (opt[1] >= 4)
1001 memcpy(&server_id, opt + 2, 4);
1002 break;
1003 }
1004 }
1005
1006#ifdef IPCONFIG_DEBUG
1007 printk("DHCP: Got message type %d\n", mt);
1008#endif
1009
1010 switch (mt) {
1011 case DHCPOFFER:
1012
1013
1014
1015 if (ic_myaddr != NONE)
1016 goto drop_unlock;
1017
1018
1019 ic_myaddr = b->your_ip;
1020 ic_servaddr = server_id;
1021#ifdef IPCONFIG_DEBUG
1022 printk("DHCP: Offered address %pI4 by server %pI4\n",
1023 &ic_myaddr, &ic_servaddr);
1024#endif
1025
1026
1027
1028
1029 if ((server_id != NONE) &&
1030 (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
1039 break;
1040
1041 default:
1042
1043 ic_myaddr = NONE;
1044 ic_servaddr = NONE;
1045 goto drop_unlock;
1046 }
1047
1048 ic_dhcp_msgtype = mt;
1049
1050 }
1051#endif
1052
1053 ext = &b->exten[4];
1054 while (ext < end && *ext != 0xff) {
1055 u8 *opt = ext++;
1056 if (*opt == 0)
1057 continue;
1058 ext += *ext + 1;
1059 if (ext < end)
1060 ic_do_bootp_ext(opt);
1061 }
1062 }
1063
1064
1065 ic_dev = dev;
1066 ic_myaddr = b->your_ip;
1067 ic_servaddr = b->server_ip;
1068 if (ic_gateway == NONE && b->relay_ip)
1069 ic_gateway = b->relay_ip;
1070 if (ic_nameservers[0] == NONE)
1071 ic_nameservers[0] = ic_servaddr;
1072 ic_got_reply = IC_BOOTP;
1073
1074drop_unlock:
1075
1076 spin_unlock(&ic_recv_lock);
1077
1078drop:
1079
1080 kfree_skb(skb);
1081
1082 return 0;
1083}
1084
1085
1086#endif
1087
1088
1089
1090
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
1105
1106
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
1124 return -1;
1125
1126
1127
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
1140
1141
1142
1143
1144
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);
1157 for (;;) {
1158
1159 ic_dev_xid = d->xid;
1160
1161#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);
1171 while (time_before(jiffies, jiff) && !ic_got_reply)
1172 schedule_timeout_uninterruptible(1);
1173#ifdef IPCONFIG_DHCP
1174
1175 if ((ic_got_reply & IC_BOOTP)
1176 && (ic_proto_enabled & IC_USE_DHCP)
1177 && ic_dhcp_msgtype != DHCPACK)
1178 {
1179 ic_got_reply = 0;
1180 printk(",");
1181 continue;
1182 }
1183#endif
1184
1185 if (ic_got_reply) {
1186 printk(" OK\n");
1187 break;
1188 }
1189
1190 if ((d = d->next))
1191 continue;
1192
1193 if (! --retries) {
1194 printk(" timed out!\n");
1195 break;
1196 }
1197
1198 d = ic_first_dev;
1199
1200 timeout = timeout CONF_TIMEOUT_MULT;
1201 if (timeout > CONF_TIMEOUT_MAX)
1202 timeout = CONF_TIMEOUT_MAX;
1203
1204 printk(".");
1205 }
1206
1207#ifdef IPCONFIG_BOOTP
1208 if (do_bootp)
1209 ic_bootp_cleanup();
1210#endif
1211#ifdef IPCONFIG_RARP
1212 if (do_rarp)
1213 ic_rarp_cleanup();
1214#endif
1215
1216 if (!ic_got_reply) {
1217 ic_myaddr = NONE;
1218 return -1;
1219 }
1220
1221 printk("IP-Config: Got %s answer from %pI4, ",
1222 ((ic_got_reply & IC_RARP) ? "RARP"
1223 : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
1224 &ic_servaddr);
1225 printk("my address is %pI4\n", &ic_myaddr);
1226
1227 return 0;
1228}
1229
1230#endif
1231
1232#ifdef CONFIG_PROC_FS
1233
1234static int pnp_seq_show(struct seq_file *seq, void *v)
1235{
1236 int i;
1237
1238 if (ic_proto_used & IC_PROTO)
1239 seq_printf(seq, "#PROTO: %s\n",
1240 (ic_proto_used & IC_RARP) ? "RARP"
1241 : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
1242 else
1243 seq_puts(seq, "#MANUAL\n");
1244
1245 if (ic_domain[0])
1246 seq_printf(seq,
1247 "domain %s\n", ic_domain);
1248 for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
1249 if (ic_nameservers[i] != NONE)
1250 seq_printf(seq, "nameserver %pI4\n",
1251 &ic_nameservers[i]);
1252 }
1253 if (ic_servaddr != NONE)
1254 seq_printf(seq, "bootserver %pI4\n",
1255 &ic_servaddr);
1256 return 0;
1257}
1258
1259static int pnp_seq_open(struct inode *indoe, struct file *file)
1260{
1261 return single_open(file, pnp_seq_show, NULL);
1262}
1263
1264static const struct file_operations pnp_seq_fops = {
1265 .owner = THIS_MODULE,
1266 .open = pnp_seq_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1270};
1271#endif
1272
1273
1274
1275
1276
1277
1278__be32 __init root_nfs_parse_addr(char *name)
1279{
1280 __be32 addr;
1281 int octets = 0;
1282 char *cp, *cq;
1283
1284 cp = cq = name;
1285 while (octets < 4) {
1286 while (*cp >= '0' && *cp <= '9')
1287 cp++;
1288 if (cp == cq || cp - cq > 3)
1289 break;
1290 if (*cp == '.' || octets == 3)
1291 octets++;
1292 if (octets < 4)
1293 cp++;
1294 cq = cp;
1295 }
1296 if (octets == 4 && (*cp == ':' || *cp == '\0')) {
1297 if (*cp == ':')
1298 *cp++ = '\0';
1299 addr = in_aton(name);
1300 memmove(name, cp, strlen(cp) + 1);
1301 } else
1302 addr = NONE;
1303
1304 return addr;
1305}
1306
1307
1308
1309
1310
1311static int __init ip_auto_config(void)
1312{
1313 __be32 addr;
1314#ifdef IPCONFIG_DYNAMIC
1315 int retries = CONF_OPEN_RETRIES;
1316#endif
1317
1318#ifdef CONFIG_PROC_FS
1319 proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
1320#endif
1321
1322 if (!ic_enable)
1323 return 0;
1324
1325 DBG(("IP-Config: Entered.\n"));
1326#ifdef IPCONFIG_DYNAMIC
1327 try_try_again:
1328#endif
1329
1330 msleep(CONF_PRE_OPEN);
1331
1332
1333 if (ic_open_devs() < 0)
1334 return -1;
1335
1336
1337 ssleep(CONF_POST_OPEN);
1338
1339
1340
1341
1342
1343
1344
1345 if (ic_myaddr == NONE ||
1346#ifdef CONFIG_ROOT_NFS
1347 (root_server_addr == NONE
1348 && ic_servaddr == NONE
1349 && ROOT_DEV == Root_NFS) ||
1350#endif
1351 ic_first_dev->next) {
1352#ifdef IPCONFIG_DYNAMIC
1353 if (ic_dynamic() < 0) {
1354 ic_close_devs();
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370#ifdef CONFIG_ROOT_NFS
1371 if (ROOT_DEV == Root_NFS) {
1372 printk(KERN_ERR
1373 "IP-Config: Retrying forever (NFS root)...\n");
1374 goto try_try_again;
1375 }
1376#endif
1377
1378 if (--retries) {
1379 printk(KERN_ERR
1380 "IP-Config: Reopening network devices...\n");
1381 goto try_try_again;
1382 }
1383
1384
1385 printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
1386 return -1;
1387 }
1388#else
1389 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
1390 ic_close_devs();
1391 return -1;
1392#endif
1393 } else {
1394
1395 ic_dev = ic_first_dev->dev;
1396 }
1397
1398 addr = root_nfs_parse_addr(root_server_path);
1399 if (root_server_addr == NONE)
1400 root_server_addr = addr;
1401
1402
1403
1404
1405 if (ic_defaults() < 0)
1406 return -1;
1407
1408
1409
1410
1411
1412 ic_close_devs();
1413 if (ic_setup_if() < 0 || ic_setup_routes() < 0)
1414 return -1;
1415
1416
1417
1418
1419#ifdef IPCONFIG_DYNAMIC
1420 ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
1421#endif
1422
1423#ifndef IPCONFIG_SILENT
1424
1425
1426
1427 printk("IP-Config: Complete:");
1428 printk("\n device=%s", ic_dev->name);
1429 printk(", addr=%pI4", &ic_myaddr);
1430 printk(", mask=%pI4", &ic_netmask);
1431 printk(", gw=%pI4", &ic_gateway);
1432 printk(",\n host=%s, domain=%s, nis-domain=%s",
1433 utsname()->nodename, ic_domain, utsname()->domainname);
1434 printk(",\n bootserver=%pI4", &ic_servaddr);
1435 printk(", rootserver=%pI4", &root_server_addr);
1436 printk(", rootpath=%s", root_server_path);
1437 if (ic_dev_mtu)
1438 printk(", mtu=%d", ic_dev_mtu);
1439 printk("\n");
1440#endif
1441
1442 return 0;
1443}
1444
1445late_initcall(ip_auto_config);
1446
1447
1448
1449
1450
1451
1452static int __init ic_proto_name(char *name)
1453{
1454 if (!strcmp(name, "on") || !strcmp(name, "any")) {
1455 return 1;
1456 }
1457 if (!strcmp(name, "off") || !strcmp(name, "none")) {
1458 return 0;
1459 }
1460#ifdef CONFIG_IP_PNP_DHCP
1461 else if (!strcmp(name, "dhcp")) {
1462 ic_proto_enabled &= ~IC_RARP;
1463 return 1;
1464 }
1465#endif
1466#ifdef CONFIG_IP_PNP_BOOTP
1467 else if (!strcmp(name, "bootp")) {
1468 ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
1469 return 1;
1470 }
1471#endif
1472#ifdef CONFIG_IP_PNP_RARP
1473 else if (!strcmp(name, "rarp")) {
1474 ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
1475 return 1;
1476 }
1477#endif
1478#ifdef IPCONFIG_DYNAMIC
1479 else if (!strcmp(name, "both")) {
1480 ic_proto_enabled &= ~IC_USE_DHCP;
1481 return 1;
1482 }
1483#endif
1484 return 0;
1485}
1486
1487static int __init ip_auto_config_setup(char *addrs)
1488{
1489 char *cp, *ip, *dp;
1490 int num = 0;
1491
1492 ic_set_manually = 1;
1493 ic_enable = 1;
1494
1495
1496
1497
1498
1499 if (ic_proto_name(addrs))
1500 return 1;
1501
1502
1503 if (*addrs == 0 ||
1504 strcmp(addrs, "off") == 0 ||
1505 strcmp(addrs, "none") == 0) {
1506 ic_enable = 0;
1507 return 1;
1508 }
1509
1510
1511 ip = addrs;
1512 while (ip && *ip) {
1513 if ((cp = strchr(ip, ':')))
1514 *cp++ = '\0';
1515 if (strlen(ip) > 0) {
1516 DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
1517 switch (num) {
1518 case 0:
1519 if ((ic_myaddr = in_aton(ip)) == ANY)
1520 ic_myaddr = NONE;
1521 break;
1522 case 1:
1523 if ((ic_servaddr = in_aton(ip)) == ANY)
1524 ic_servaddr = NONE;
1525 break;
1526 case 2:
1527 if ((ic_gateway = in_aton(ip)) == ANY)
1528 ic_gateway = NONE;
1529 break;
1530 case 3:
1531 if ((ic_netmask = in_aton(ip)) == ANY)
1532 ic_netmask = NONE;
1533 break;
1534 case 4:
1535 if ((dp = strchr(ip, '.'))) {
1536 *dp++ = '\0';
1537 strlcpy(utsname()->domainname, dp,
1538 sizeof(utsname()->domainname));
1539 }
1540 strlcpy(utsname()->nodename, ip,
1541 sizeof(utsname()->nodename));
1542 ic_host_name_set = 1;
1543 break;
1544 case 5:
1545 strlcpy(user_dev_name, ip, sizeof(user_dev_name));
1546 break;
1547 case 6:
1548 if (ic_proto_name(ip) == 0 &&
1549 ic_myaddr == NONE) {
1550 ic_enable = 0;
1551 }
1552 break;
1553 }
1554 }
1555 ip = cp;
1556 num++;
1557 }
1558
1559 return 1;
1560}
1561
1562static int __init nfsaddrs_config_setup(char *addrs)
1563{
1564 return ip_auto_config_setup(addrs);
1565}
1566
1567static int __init vendor_class_identifier_setup(char *addrs)
1568{
1569 if (strlcpy(vendor_class_identifier, addrs,
1570 sizeof(vendor_class_identifier))
1571 >= sizeof(vendor_class_identifier))
1572 printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
1573 vendor_class_identifier);
1574 return 1;
1575}
1576
1577__setup("ip=", ip_auto_config_setup);
1578__setup("nfsaddrs=", nfsaddrs_config_setup);
1579__setup("dhcpclass=", vendor_class_identifier_setup);
1580