~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/net/ipv4/ipconfig.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  $Id: ipconfig.c,v 1.34 2000/07/26 01:04:18 davem Exp $
  3  *
  4  *  Automatic Configuration of IP -- use BOOTP or RARP or user-supplied
  5  *  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 
 21 #include <linux/config.h>
 22 #include <linux/types.h>
 23 #include <linux/string.h>
 24 #include <linux/kernel.h>
 25 #include <linux/sched.h>
 26 #include <linux/random.h>
 27 #include <linux/init.h>
 28 #include <linux/utsname.h>
 29 #include <linux/in.h>
 30 #include <linux/if.h>
 31 #include <linux/inet.h>
 32 #include <linux/netdevice.h>
 33 #include <linux/if_arp.h>
 34 #include <linux/skbuff.h>
 35 #include <linux/ip.h>
 36 #include <linux/socket.h>
 37 #include <linux/route.h>
 38 #include <linux/udp.h>
 39 #include <net/arp.h>
 40 #include <net/ip.h>
 41 #include <net/ipconfig.h>
 42 
 43 #include <asm/uaccess.h>
 44 #include <asm/checksum.h>
 45 
 46 /* Define this to allow debugging output */
 47 #undef IPCONFIG_DEBUG
 48 
 49 #ifdef IPCONFIG_DEBUG
 50 #define DBG(x) printk x
 51 #else
 52 #define DBG(x) do { } while(0)
 53 #endif
 54 
 55 /* Define the timeout for waiting for a RARP/BOOTP reply */
 56 #define CONF_BASE_TIMEOUT       (HZ*5)  /* Initial timeout: 5 seconds */
 57 #define CONF_RETRIES            10      /* 10 retries */
 58 #define CONF_TIMEOUT_RANDOM     (HZ)    /* Maximum amount of randomization */
 59 #define CONF_TIMEOUT_MULT       *5/4    /* Rate of timeout growth */
 60 #define CONF_TIMEOUT_MAX        (HZ*30) /* Maximum allowed timeout */
 61 
 62 /* IP configuration */
 63 static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */
 64 u32 ic_myaddr __initdata = INADDR_NONE;         /* My IP address */
 65 u32 ic_servaddr __initdata = INADDR_NONE;       /* Server IP address */
 66 u32 ic_gateway __initdata = INADDR_NONE;        /* Gateway IP address */
 67 u32 ic_netmask __initdata = INADDR_NONE;        /* Netmask for local subnet */
 68 int ic_enable __initdata = 1;                   /* Automatic IP configuration enabled */
 69 int ic_host_name_set __initdata = 0;            /* Host name configured manually */
 70 int ic_set_manually __initdata = 0;             /* IPconfig parameters set manually */
 71 
 72 u32 root_server_addr __initdata = INADDR_NONE;          /* Address of boot server */
 73 u8 root_server_path[256] __initdata = { 0, };           /* Path to mount as root */
 74 
 75 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
 76 
 77 #define CONFIG_IP_PNP_DYNAMIC
 78 
 79 int ic_proto_enabled __initdata = 0                     /* Protocols enabled */
 80 #ifdef CONFIG_IP_PNP_BOOTP
 81                         | IC_BOOTP
 82 #endif
 83 #ifdef CONFIG_IP_PNP_RARP
 84                         | IC_RARP
 85 #endif
 86                         ;
 87 static int ic_got_reply __initdata = 0;                         /* Protocol(s) we got reply from */
 88 
 89 #else
 90 
 91 static int ic_proto_enabled __initdata = 0;
 92 
 93 #endif
 94 
 95 static int ic_proto_have_if __initdata = 0;
 96 
 97 /*
 98  *      Network devices
 99  */
100 
101 struct ic_device {
102         struct ic_device *next;
103         struct net_device *dev;
104         unsigned short flags;
105         int able;
106 };
107 
108 static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
109 static struct net_device *ic_dev __initdata = NULL;             /* Selected device */
110 
111 static int __init ic_open_devs(void)
112 {
113         struct ic_device *d, **last;
114         struct net_device *dev;
115         unsigned short oflags;
116 
117         last = &ic_first_dev;
118         rtnl_shlock();
119         for (dev = dev_base; dev; dev = dev->next) {
120                 if (user_dev_name[0] ? !strcmp(dev->name, user_dev_name) :
121                     (!(dev->flags & IFF_LOOPBACK) &&
122                      (dev->flags & (IFF_POINTOPOINT|IFF_BROADCAST)) &&
123                      strncmp(dev->name, "dummy", 5))) {
124                         int able = 0;
125                         if (dev->mtu >= 364)
126                                 able |= IC_BOOTP;
127                         else
128                                 printk(KERN_WARNING "BOOTP: Ignoring device %s, MTU %d too small", dev->name, dev->mtu);
129                         if (!(dev->flags & IFF_NOARP))
130                                 able |= IC_RARP;
131                         able &= ic_proto_enabled;
132                         if (ic_proto_enabled && !able)
133                                 continue;
134                         oflags = dev->flags;
135                         if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
136                                 printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
137                                 continue;
138                         }
139                         if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL)))
140                                 return -1;
141                         d->dev = dev;
142                         *last = d;
143                         last = &d->next;
144                         d->flags = oflags;
145                         d->able = able;
146                         ic_proto_have_if |= able;
147                         DBG(("IP-Config: Opened %s (able=%d)\n", dev->name, able));
148                 }
149         }
150         rtnl_shunlock();
151 
152         *last = NULL;
153 
154         if (!ic_first_dev) {
155                 if (user_dev_name[0])
156                         printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
157                 else
158                         printk(KERN_ERR "IP-Config: No network devices available.\n");
159                 return -1;
160         }
161         return 0;
162 }
163 
164 static void __init ic_close_devs(void)
165 {
166         struct ic_device *d, *next;
167         struct net_device *dev;
168 
169         rtnl_shlock();
170         next = ic_first_dev;
171         while ((d = next)) {
172                 next = d->next;
173                 dev = d->dev;
174                 if (dev != ic_dev) {
175                         DBG(("IP-Config: Downing %s\n", dev->name));
176                         dev_change_flags(dev, d->flags);
177                 }
178                 kfree(d);
179         }
180         rtnl_shunlock();
181 }
182 
183 /*
184  *      Interface to various network functions.
185  */
186 
187 static inline void
188 set_sockaddr(struct sockaddr_in *sin, u32 addr, u16 port)
189 {
190         sin->sin_family = AF_INET;
191         sin->sin_addr.s_addr = addr;
192         sin->sin_port = port;
193 }
194 
195 static int __init ic_dev_ioctl(unsigned int cmd, struct ifreq *arg)
196 {
197         int res;
198 
199         mm_segment_t oldfs = get_fs();
200         set_fs(get_ds());
201         res = devinet_ioctl(cmd, arg);
202         set_fs(oldfs);
203         return res;
204 }
205 
206 static int __init ic_route_ioctl(unsigned int cmd, struct rtentry *arg)
207 {
208         int res;
209 
210         mm_segment_t oldfs = get_fs();
211         set_fs(get_ds());
212         res = ip_rt_ioctl(cmd, arg);
213         set_fs(oldfs);
214         return res;
215 }
216 
217 /*
218  *      Set up interface addresses and routes.
219  */
220 
221 static int __init ic_setup_if(void)
222 {
223         struct ifreq ir;
224         struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
225         int err;
226 
227         memset(&ir, 0, sizeof(ir));
228         strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
229         set_sockaddr(sin, ic_myaddr, 0);
230         if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
231                 printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
232                 return -1;
233         }
234         set_sockaddr(sin, ic_netmask, 0);
235         if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
236                 printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
237                 return -1;
238         }
239         set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
240         if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
241                 printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
242                 return -1;
243         }
244         return 0;
245 }
246 
247 static int __init ic_setup_routes(void)
248 {
249         /* No need to setup device routes, only the default route... */
250 
251         if (ic_gateway != INADDR_NONE) {
252                 struct rtentry rm;
253                 int err;
254 
255                 memset(&rm, 0, sizeof(rm));
256                 if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
257                         printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
258                         return -1;
259                 }
260                 set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
261                 set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
262                 set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
263                 rm.rt_flags = RTF_UP | RTF_GATEWAY;
264                 if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
265                         printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
266                         return -1;
267                 }
268         }
269 
270         return 0;
271 }
272 
273 /*
274  *      Fill in default values for all missing parameters.
275  */
276 
277 static int __init ic_defaults(void)
278 {
279         /*
280          *      At this point we have no userspace running so need not
281          *      claim locks on system_utsname
282          */
283          
284         if (!ic_host_name_set)
285                 strcpy(system_utsname.nodename, in_ntoa(ic_myaddr));
286 
287         if (root_server_addr == INADDR_NONE)
288                 root_server_addr = ic_servaddr;
289 
290         if (ic_netmask == INADDR_NONE) {
291                 if (IN_CLASSA(ntohl(ic_myaddr)))
292                         ic_netmask = htonl(IN_CLASSA_NET);
293                 else if (IN_CLASSB(ntohl(ic_myaddr)))
294                         ic_netmask = htonl(IN_CLASSB_NET);
295                 else if (IN_CLASSC(ntohl(ic_myaddr)))
296                         ic_netmask = htonl(IN_CLASSC_NET);
297                 else {
298                         printk(KERN_ERR "IP-Config: Unable to guess netmask for address %u.%u.%u.%u\n",
299                                 NIPQUAD(ic_myaddr));
300                         return -1;
301                 }
302                 printk("IP-Config: Guessing netmask %u.%u.%u.%u\n", NIPQUAD(ic_netmask));
303         }
304 
305         return 0;
306 }
307 
308 /*
309  *      RARP support.
310  */
311 
312 #ifdef CONFIG_IP_PNP_RARP
313 
314 static int ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
315 
316 static struct packet_type rarp_packet_type __initdata = {
317         __constant_htons(ETH_P_RARP),
318         NULL,                   /* Listen to all devices */
319         ic_rarp_recv,
320         NULL,
321         NULL
322 };
323 
324 static inline void ic_rarp_init(void)
325 {
326         dev_add_pack(&rarp_packet_type);
327 }
328 
329 static inline void ic_rarp_cleanup(void)
330 {
331         dev_remove_pack(&rarp_packet_type);
332 }
333 
334 /*
335  *  Process received RARP packet.
336  */
337 static int __init
338 ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
339 {
340         struct arphdr *rarp = (struct arphdr *)skb->h.raw;
341         unsigned char *rarp_ptr = (unsigned char *) (rarp + 1);
342         unsigned long sip, tip;
343         unsigned char *sha, *tha;               /* s for "source", t for "target" */
344 
345         /* If we already have a reply, just drop the packet */
346         if (ic_got_reply)
347                 goto drop;
348 
349         /* If this test doesn't pass, it's not IP, or we should ignore it anyway */
350         if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd))
351                 goto drop;
352 
353         /* If it's not a RARP reply, delete it. */
354         if (rarp->ar_op != htons(ARPOP_RREPLY))
355                 goto drop;
356 
357         /* If it's not Ethernet, delete it. */
358         if (rarp->ar_pro != htons(ETH_P_IP))
359                 goto drop;
360 
361         /* Extract variable-width fields */
362         sha = rarp_ptr;
363         rarp_ptr += dev->addr_len;
364         memcpy(&sip, rarp_ptr, 4);
365         rarp_ptr += 4;
366         tha = rarp_ptr;
367         rarp_ptr += dev->addr_len;
368         memcpy(&tip, rarp_ptr, 4);
369 
370         /* Discard packets which are not meant for us. */
371         if (memcmp(tha, dev->dev_addr, dev->addr_len))
372                 goto drop;
373 
374         /* Discard packets which are not from specified server. */
375         if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
376                 goto drop;
377 
378         /* Victory! The packet is what we were looking for! */
379         if (!ic_got_reply) {
380                 ic_got_reply = IC_RARP;
381                 ic_dev = dev;
382                 if (ic_myaddr == INADDR_NONE)
383                         ic_myaddr = tip;
384                 ic_servaddr = sip;
385         }
386 
387         /* And throw the packet out... */
388 drop:
389         kfree_skb(skb);
390         return 0;
391 }
392 
393 
394 /*
395  *  Send RARP request packet over all devices which allow RARP.
396  */
397 static void __init ic_rarp_send(void)
398 {
399         struct ic_device *d;
400 
401         for (d=ic_first_dev; d; d=d->next)
402                 if (d->able & IC_RARP) {
403                         struct net_device *dev = d->dev;
404                         arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
405                                  dev->dev_addr, dev->dev_addr);
406                 }
407 }
408 
409 #endif
410 
411 /*
412  *      BOOTP support.
413  */
414 
415 #ifdef CONFIG_IP_PNP_BOOTP
416 
417 struct bootp_pkt {              /* BOOTP packet format */
418         struct iphdr iph;       /* IP header */
419         struct udphdr udph;     /* UDP header */
420         u8 op;                  /* 1=request, 2=reply */
421         u8 htype;               /* HW address type */
422         u8 hlen;                /* HW address length */
423         u8 hops;                /* Used only by gateways */
424         u32 xid;                /* Transaction ID */
425         u16 secs;               /* Seconds since we started */
426         u16 flags;              /* Just what it says */
427         u32 client_ip;          /* Client's IP address if known */
428         u32 your_ip;            /* Assigned IP address */
429         u32 server_ip;          /* Server's IP address */
430         u32 relay_ip;           /* IP address of BOOTP relay */
431         u8 hw_addr[16];         /* Client's HW address */
432         u8 serv_name[64];       /* Server host name */
433         u8 boot_file[128];      /* Name of boot file */
434         u8 vendor_area[128];    /* Area for extensions */
435 };
436 
437 #define BOOTP_REQUEST 1
438 #define BOOTP_REPLY 2
439 
440 static u32 ic_bootp_xid;
441 
442 static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt);
443 
444 static struct packet_type bootp_packet_type __initdata = {
445         __constant_htons(ETH_P_IP),
446         NULL,                   /* Listen to all devices */
447         ic_bootp_recv,
448         NULL,
449         NULL
450 };
451 
452 
453 /*
454  *  Initialize BOOTP extension fields in the request.
455  */
456 static void __init ic_bootp_init_ext(u8 *e)
457 {
458         *e++ = 99;              /* RFC1048 Magic Cookie */
459         *e++ = 130;
460         *e++ = 83;
461         *e++ = 99;
462         *e++ = 1;               /* Subnet mask request */
463         *e++ = 4;
464         e += 4;
465         *e++ = 3;               /* Default gateway request */
466         *e++ = 4;
467         e += 4;
468         *e++ = 12;              /* Host name request */
469         *e++ = 32;
470         e += 32;
471         *e++ = 40;              /* NIS Domain name request */
472         *e++ = 32;
473         e += 32;
474         *e++ = 17;              /* Boot path */
475         *e++ = 32;
476         e += 32;
477         *e = 255;               /* End of the list */
478 }
479 
480 
481 /*
482  *  Initialize the BOOTP mechanism.
483  */
484 static inline void ic_bootp_init(void)
485 {
486         get_random_bytes(&ic_bootp_xid, sizeof(u32));
487         DBG(("BOOTP: XID=%08x\n", ic_bootp_xid));
488         dev_add_pack(&bootp_packet_type);
489 }
490 
491 
492 /*
493  *  BOOTP cleanup.
494  */
495 static inline void ic_bootp_cleanup(void)
496 {
497         dev_remove_pack(&bootp_packet_type);
498 }
499 
500 
501 /*
502  *  Send BOOTP request to single interface.
503  */
504 static void __init ic_bootp_send_if(struct ic_device *d, u32 jiffies)
505 {
506         struct net_device *dev = d->dev;
507         struct sk_buff *skb;
508         struct bootp_pkt *b;
509         int hh_len = (dev->hard_header_len + 15) & ~15;
510         struct iphdr *h;
511 
512         /* Allocate packet */
513         skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
514         if (!skb)
515                 return;
516         skb_reserve(skb, hh_len);
517         b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
518         memset(b, 0, sizeof(struct bootp_pkt));
519 
520         /* Construct IP header */
521         skb->nh.iph = h = &b->iph;
522         h->version = 4;
523         h->ihl = 5;
524         h->tot_len = htons(sizeof(struct bootp_pkt));
525         h->frag_off = htons(IP_DF);
526         h->ttl = 64;
527         h->protocol = IPPROTO_UDP;
528         h->daddr = INADDR_BROADCAST;
529         h->check = ip_fast_csum((unsigned char *) h, h->ihl);
530 
531         /* Construct UDP header */
532         b->udph.source = htons(68);
533         b->udph.dest = htons(67);
534         b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
535         /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
536 
537         /* Construct BOOTP header */
538         b->op = BOOTP_REQUEST;
539         if (dev->type < 256) /* check for false types */
540                 b->htype = dev->type;
541         else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
542                 b->htype = ARPHRD_IEEE802;
543         else {
544                 printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
545                 b->htype = dev->type; /* can cause undefined behavior */
546         }
547         b->hlen = dev->addr_len;
548         memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
549         b->secs = htons(jiffies / HZ);
550         b->xid = ic_bootp_xid;
551         ic_bootp_init_ext(b->vendor_area);
552 
553         /* Chain packet down the line... */
554         skb->dev = dev;
555         skb->protocol = __constant_htons(ETH_P_IP);
556         if ((dev->hard_header &&
557              dev->hard_header(skb, dev, ntohs(skb->protocol), dev->broadcast, dev->dev_addr, skb->len) < 0) ||
558             dev_queue_xmit(skb) < 0)
559                 printk("E");
560 }
561 
562 
563 /*
564  *  Send BOOTP requests to all interfaces.
565  */
566 static void __init ic_bootp_send(u32 jiffies)
567 {
568         struct ic_device *d;
569 
570         for(d=ic_first_dev; d; d=d->next)
571                 if (d->able & IC_BOOTP)
572                         ic_bootp_send_if(d, jiffies);
573 }
574 
575 
576 /*
577  *  Copy BOOTP-supplied string if not already set.
578  */
579 static int __init ic_bootp_string(char *dest, char *src, int len, int max)
580 {
581         if (!len)
582                 return 0;
583         if (len > max-1)
584                 len = max-1;
585         strncpy(dest, src, len);
586         dest[len] = '\0';
587         return 1;
588 }
589 
590 
591 /*
592  *  Process BOOTP extension.
593  */
594 static void __init ic_do_bootp_ext(u8 *ext)
595 {
596 #ifdef IPCONFIG_DEBUG
597         u8 *c;
598 
599         printk("BOOTP: Got extension %02x",*ext);
600         for(c=ext+2; c<ext+2+ext[1]; c++)
601                 printk(" %02x", *c);
602         printk("\n");
603 #endif
604 
605         switch (*ext++) {
606                 case 1:         /* Subnet mask */
607                         if (ic_netmask == INADDR_NONE)
608                                 memcpy(&ic_netmask, ext+1, 4);
609                         break;
610                 case 3:         /* Default gateway */
611                         if (ic_gateway == INADDR_NONE)
612                                 memcpy(&ic_gateway, ext+1, 4);
613                         break;
614                 case 12:        /* Host name */
615                         ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
616                         ic_host_name_set = 1;
617                         break;
618                 case 40:        /* NIS Domain name */
619                         ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
620                         break;
621                 case 17:        /* Root path */
622                         if (!root_server_path[0])
623                                 ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
624                         break;
625         }
626 }
627 
628 
629 /*
630  *  Receive BOOTP reply.
631  */
632 static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
633 {
634         struct bootp_pkt *b = (struct bootp_pkt *) skb->nh.iph;
635         struct iphdr *h = &b->iph;
636         int len;
637 
638         /* If we already have a reply, just drop the packet */
639         if (ic_got_reply)
640                 goto drop;
641 
642         /* Check whether it's a BOOTP packet */
643         if (skb->pkt_type == PACKET_OTHERHOST ||
644             skb->len < sizeof(struct udphdr) + sizeof(struct iphdr) ||
645             h->ihl != 5 ||
646             h->version != 4 ||
647             ip_fast_csum((char *) h, h->ihl) != 0 ||
648             skb->len < ntohs(h->tot_len) ||
649             h->protocol != IPPROTO_UDP ||
650             b->udph.source != htons(67) ||
651             b->udph.dest != htons(68) ||
652             ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
653                 goto drop;
654 
655         /* Fragments are not supported */
656         if (h->frag_off & htons(IP_OFFSET|IP_MF)) {
657                 printk(KERN_ERR "BOOTP: Ignoring fragmented reply.\n");
658                 goto drop;
659         }
660 
661         /* Is it a reply to our BOOTP request? */
662         len = ntohs(b->udph.len) - sizeof(struct udphdr);
663         if (len < 300 ||                                    /* See RFC 951:2.1 */
664             b->op != BOOTP_REPLY ||
665             b->xid != ic_bootp_xid) {
666                 printk("?");
667                 goto drop;
668         }
669 
670         /* Extract basic fields */
671         ic_myaddr = b->your_ip;
672         ic_servaddr = b->server_ip;
673         ic_got_reply = IC_BOOTP;
674         ic_dev = dev;
675 
676         /* Parse extensions */
677         if (b->vendor_area[0] == 99 &&  /* Check magic cookie */
678             b->vendor_area[1] == 130 &&
679             b->vendor_area[2] == 83 &&
680             b->vendor_area[3] == 99) {
681                 u8 *ext = &b->vendor_area[4];
682                 u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
683                 while (ext < end && *ext != 0xff) {
684                         if (*ext == 0)          /* Padding */
685                                 ext++;
686                         else {
687                                 u8 *opt = ext;
688                                 ext += ext[1] + 2;
689                                 if (ext <= end)
690                                         ic_do_bootp_ext(opt);
691                         }
692                 }
693         }
694 
695         if (ic_gateway == INADDR_NONE && b->relay_ip)
696                 ic_gateway = b->relay_ip;
697 
698 drop:
699         kfree_skb(skb);
700         return 0;
701 }       
702 
703 
704 #endif
705 
706 
707 /*
708  *      Dynamic IP configuration -- BOOTP and RARP.
709  */
710 
711 #ifdef CONFIG_IP_PNP_DYNAMIC
712 
713 static int __init ic_dynamic(void)
714 {
715         int retries;
716         unsigned long timeout, jiff;
717         unsigned long start_jiffies;
718         int do_rarp = ic_proto_have_if & IC_RARP;
719         int do_bootp = ic_proto_have_if & IC_BOOTP;
720 
721         /*
722          * If neither BOOTP nor RARP was selected, return with an error. This
723          * routine gets only called when some pieces of information are mis-
724          * sing, and without BOOTP and RARP we are not able to get that in-
725          * formation.
726          */
727         if (!ic_proto_enabled) {
728                 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
729                 return -1;
730         }
731 
732 #ifdef CONFIG_IP_PNP_BOOTP
733         if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
734                 printk(KERN_ERR "BOOTP: No suitable device found.\n");
735 #endif
736 
737 #ifdef CONFIG_IP_PNP_RARP
738         if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
739                 printk(KERN_ERR "RARP: No suitable device found.\n");
740 #endif
741 
742         if (!ic_proto_have_if)
743                 /* Error message already printed */
744                 return -1;
745 
746         /*
747          * Setup RARP and BOOTP protocols
748          */
749 #ifdef CONFIG_IP_PNP_RARP
750         if (do_rarp)
751                 ic_rarp_init();
752 #endif
753 #ifdef CONFIG_IP_PNP_BOOTP
754         if (do_bootp)
755                 ic_bootp_init();
756 #endif
757 
758         /*
759          * Send requests and wait, until we get an answer. This loop
760          * seems to be a terrible waste of CPU time, but actually there is
761          * only one process running at all, so we don't need to use any
762          * scheduler functions.
763          * [Actually we could now, but the nothing else running note still 
764          *  applies.. - AC]
765          */
766         printk(KERN_NOTICE "Sending %s%s%s requests...",
767                 do_bootp ? "BOOTP" : "",
768                 do_bootp && do_rarp ? " and " : "",
769                 do_rarp ? "RARP" : "");
770         start_jiffies = jiffies;
771         retries = CONF_RETRIES;
772         get_random_bytes(&timeout, sizeof(timeout));
773         timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
774         for(;;) {
775 #ifdef CONFIG_IP_PNP_BOOTP
776                 if (do_bootp)
777                         ic_bootp_send(jiffies - start_jiffies);
778 #endif
779 #ifdef CONFIG_IP_PNP_RARP
780                 if (do_rarp)
781                         ic_rarp_send();
782 #endif
783                 printk(".");
784                 jiff = jiffies + timeout;
785                 while (jiffies < jiff && !ic_got_reply)
786                         barrier();
787                 if (ic_got_reply) {
788                         printk(" OK\n");
789                         break;
790                 }
791                 if (! --retries) {
792                         printk(" timed out!\n");
793                         break;
794                 }
795                 timeout = timeout CONF_TIMEOUT_MULT;
796                 if (timeout > CONF_TIMEOUT_MAX)
797                         timeout = CONF_TIMEOUT_MAX;
798         }
799 
800 #ifdef CONFIG_IP_PNP_RARP
801         if (do_rarp)
802                 ic_rarp_cleanup();
803 #endif
804 #ifdef CONFIG_IP_PNP_BOOTP
805         if (do_bootp)
806                 ic_bootp_cleanup();
807 #endif
808 
809         if (!ic_got_reply)
810                 return -1;
811 
812         printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
813                 (ic_got_reply & IC_BOOTP) ? "BOOTP" : "RARP",
814                 NIPQUAD(ic_servaddr));
815         printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
816 
817         return 0;
818 }
819 
820 #endif
821 
822 /*
823  *      IP Autoconfig dispatcher.
824  */
825 
826 static int __init ip_auto_config(void)
827 {
828         if (!ic_enable)
829                 return 0;
830 
831         DBG(("IP-Config: Entered.\n"));
832 
833         /* Setup all network devices */
834         if (ic_open_devs() < 0)
835                 return -1;
836 
837         /*
838          * If the config information is insufficient (e.g., our IP address or
839          * IP address of the boot server is missing or we have multiple network
840          * interfaces and no default was set), use BOOTP or RARP to get the
841          * missing values.
842          */
843         if (ic_myaddr == INADDR_NONE ||
844 #ifdef CONFIG_ROOT_NFS
845             (root_server_addr == INADDR_NONE && ic_servaddr == INADDR_NONE) ||
846 #endif
847             ic_first_dev->next) {
848 #ifdef CONFIG_IP_PNP_DYNAMIC
849                 if (ic_dynamic() < 0) {
850                         printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
851                         ic_close_devs();
852                         return -1;
853                 }
854 #else
855                 printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
856                 ic_close_devs();
857                 return -1;
858 #endif
859         } else {
860                 ic_dev = ic_first_dev->dev;     /* Device selected manually or only one device -> use it */
861         }
862 
863         /*
864          * Use defaults whereever applicable.
865          */
866         if (ic_defaults() < 0)
867                 return -1;
868 
869         /*
870          * Close all network devices except the device we've
871          * autoconfigured and set up routes.
872          */
873         ic_close_devs();
874         if (ic_setup_if() < 0 || ic_setup_routes() < 0)
875                 return -1;
876 
877         DBG(("IP-Config: device=%s, local=%08x, server=%08x, boot=%08x, gw=%08x, mask=%08x\n",
878             ic_dev->name, ic_myaddr, ic_servaddr, root_server_addr, ic_gateway, ic_netmask));
879         DBG(("IP-Config: host=%s, domain=%s, path=`%s'\n", system_utsname.nodename,
880             system_utsname.domainname, root_server_path));
881         return 0;
882 }
883 
884 module_init(ip_auto_config);
885 
886 
887 /*
888  *  Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
889  *  command line parameter. It consists of option fields separated by colons in
890  *  the following order:
891  *
892  *  <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<bootp|rarp>
893  *
894  *  Any of the fields can be empty which means to use a default value:
895  *      <client-ip>     - address given by BOOTP or RARP
896  *      <server-ip>     - address of host returning BOOTP or RARP packet
897  *      <gw-ip>         - none, or the address returned by BOOTP
898  *      <netmask>       - automatically determined from <client-ip>, or the
899  *                        one returned by BOOTP
900  *      <host name>     - <client-ip> in ASCII notation, or the name returned
901  *                        by BOOTP
902  *      <device>        - use all available devices
903  *      <bootp|rarp|both|off> - use both protocols to determine my own address
904  */
905 static int __init ic_proto_name(char *name)
906 {
907         if (!strcmp(name, "off")) {
908                 ic_proto_enabled = 0;
909                 return 1;
910         }
911 #ifdef CONFIG_IP_PNP_BOOTP
912         else if (!strcmp(name, "bootp")) {
913                 ic_proto_enabled &= ~IC_RARP;
914                 return 1;
915         }
916 #endif
917 #ifdef CONFIG_IP_PNP_RARP
918         else if (!strcmp(name, "rarp")) {
919                 ic_proto_enabled &= ~IC_BOOTP;
920                 return 1;
921         }
922 #endif
923 #ifdef CONFIG_IP_PNP_DYNAMIC
924         else if (!strcmp(name, "both")) {
925                 return 1;
926         }
927 #endif
928         return 0;
929 }
930 
931 static int __init ip_auto_config_setup(char *addrs)
932 {
933         char *cp, *ip, *dp;
934         int num = 0;
935 
936         ic_set_manually = 1;
937         if (!strcmp(addrs, "off")) {
938                 ic_enable = 0;
939                 return 1;
940         }
941         if (ic_proto_name(addrs))
942                 return 1;
943 
944         /* Parse the whole string */
945         ip = addrs;
946         while (ip && *ip) {
947                 if ((cp = strchr(ip, ':')))
948                         *cp++ = '\0';
949                 if (strlen(ip) > 0) {
950                         DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
951                         switch (num) {
952                         case 0:
953                                 if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
954                                         ic_myaddr = INADDR_NONE;
955                                 break;
956                         case 1:
957                                 if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
958                                         ic_servaddr = INADDR_NONE;
959                                 break;
960                         case 2:
961                                 if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
962                                         ic_gateway = INADDR_NONE;
963                                 break;
964                         case 3:
965                                 if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
966                                         ic_netmask = INADDR_NONE;
967                                 break;
968                         case 4:
969                                 if ((dp = strchr(ip, '.'))) {
970                                         *dp++ = '\0';
971                                         strncpy(system_utsname.domainname, dp, __NEW_UTS_LEN);
972                                         system_utsname.domainname[__NEW_UTS_LEN] = '\0';
973                                 }
974                                 strncpy(system_utsname.nodename, ip, __NEW_UTS_LEN);
975                                 system_utsname.nodename[__NEW_UTS_LEN] = '\0';
976                                 ic_host_name_set = 1;
977                                 break;
978                         case 5:
979                                 strncpy(user_dev_name, ip, IFNAMSIZ);
980                                 user_dev_name[IFNAMSIZ-1] = '\0';
981                                 break;
982                         case 6:
983                                 ic_proto_name(ip);
984                                 break;
985                         }
986                 }
987                 ip = cp;
988                 num++;
989         }
990 
991         return 1;
992 }
993 
994 static int __init nfsaddrs_config_setup(char *addrs)
995 {
996         return ip_auto_config_setup(addrs);
997 }
998 
999 __setup("ip=", ip_auto_config_setup);
1000 __setup("nfsaddrs=", nfsaddrs_config_setup);
1001 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.