ipconfig.c 36 KB

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