ipconfig.c 36 KB

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