ipconfig.c 36 KB

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