ipconfig.c 36 KB

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