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