ipconfig.c 43 KB

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