ipconfig.c 36 KB

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