ipconfig.c 36 KB

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