ipconfig.c 36 KB

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