ipconfig.c 36 KB

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