ipconfig.c 36 KB

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