ipconfig.c 36 KB

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