ipconfig.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:34k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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