ipconfig.c 39 KB

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