ipconfig.c 36 KB

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