ipconfig.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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(sizeof(struct ic_device), GFP_KERNEL))) {
  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 + msecs_to_jiffies(20000);
  242. while (time_before(jiffies, start +
  243. msecs_to_jiffies(carrier_timeout * 1000))) {
  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 + msecs_to_jiffies(20000);
  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. u8 mt = ((ic_servaddr == NONE)
  579. ? DHCPDISCOVER : DHCPREQUEST);
  580. u8 *e = options;
  581. int len;
  582. pr_debug("DHCP: Sending message type %d (%s)\n", mt, d->dev->name);
  583. memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
  584. e += 4;
  585. *e++ = 53; /* DHCP message type */
  586. *e++ = 1;
  587. *e++ = mt;
  588. if (mt == DHCPREQUEST) {
  589. *e++ = 54; /* Server ID (IP address) */
  590. *e++ = 4;
  591. memcpy(e, &ic_servaddr, 4);
  592. e += 4;
  593. *e++ = 50; /* Requested IP address */
  594. *e++ = 4;
  595. memcpy(e, &ic_myaddr, 4);
  596. e += 4;
  597. }
  598. /* always? */
  599. {
  600. static const u8 ic_req_params[] = {
  601. 1, /* Subnet mask */
  602. 3, /* Default gateway */
  603. 6, /* DNS server */
  604. 12, /* Host name */
  605. 15, /* Domain name */
  606. 17, /* Boot path */
  607. 26, /* MTU */
  608. 40, /* NIS domain name */
  609. 42, /* NTP servers */
  610. };
  611. *e++ = 55; /* Parameter request list */
  612. *e++ = sizeof(ic_req_params);
  613. memcpy(e, ic_req_params, sizeof(ic_req_params));
  614. e += sizeof(ic_req_params);
  615. if (ic_host_name_set) {
  616. *e++ = 12; /* host-name */
  617. len = strlen(utsname()->nodename);
  618. *e++ = len;
  619. memcpy(e, utsname()->nodename, len);
  620. e += len;
  621. }
  622. if (*vendor_class_identifier) {
  623. pr_info("DHCP: sending class identifier \"%s\"\n",
  624. vendor_class_identifier);
  625. *e++ = 60; /* Class-identifier */
  626. len = strlen(vendor_class_identifier);
  627. *e++ = len;
  628. memcpy(e, vendor_class_identifier, len);
  629. e += len;
  630. }
  631. len = strlen(dhcp_client_identifier + 1);
  632. /* the minimum length of identifier is 2, include 1 byte type,
  633. * and can not be larger than the length of options
  634. */
  635. if (len >= 1 && len < 312 - (e - options) - 1) {
  636. *e++ = 61;
  637. *e++ = len + 1;
  638. memcpy(e, dhcp_client_identifier, len + 1);
  639. e += len + 1;
  640. }
  641. }
  642. *e++ = 255; /* End of the list */
  643. }
  644. #endif /* IPCONFIG_DHCP */
  645. static void __init ic_bootp_init_ext(u8 *e)
  646. {
  647. memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
  648. e += 4;
  649. *e++ = 1; /* Subnet mask request */
  650. *e++ = 4;
  651. e += 4;
  652. *e++ = 3; /* Default gateway request */
  653. *e++ = 4;
  654. e += 4;
  655. #if CONF_NAMESERVERS_MAX > 0
  656. *e++ = 6; /* (DNS) name server request */
  657. *e++ = 4 * CONF_NAMESERVERS_MAX;
  658. e += 4 * CONF_NAMESERVERS_MAX;
  659. #endif
  660. *e++ = 12; /* Host name request */
  661. *e++ = 32;
  662. e += 32;
  663. *e++ = 40; /* NIS Domain name request */
  664. *e++ = 32;
  665. e += 32;
  666. *e++ = 17; /* Boot path */
  667. *e++ = 40;
  668. e += 40;
  669. *e++ = 57; /* set extension buffer size for reply */
  670. *e++ = 2;
  671. *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
  672. *e++ = 150;
  673. *e++ = 255; /* End of the list */
  674. }
  675. /*
  676. * Initialize the DHCP/BOOTP mechanism.
  677. */
  678. static inline void __init ic_bootp_init(void)
  679. {
  680. /* Re-initialise all name servers and NTP servers to NONE, in case any
  681. * were set via the "ip=" or "nfsaddrs=" kernel command line parameters:
  682. * any IP addresses specified there will already have been decoded but
  683. * are no longer needed
  684. */
  685. ic_nameservers_predef();
  686. ic_ntp_servers_predef();
  687. dev_add_pack(&bootp_packet_type);
  688. }
  689. /*
  690. * DHCP/BOOTP cleanup.
  691. */
  692. static inline void __init ic_bootp_cleanup(void)
  693. {
  694. dev_remove_pack(&bootp_packet_type);
  695. }
  696. /*
  697. * Send DHCP/BOOTP request to single interface.
  698. */
  699. static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
  700. {
  701. struct net_device *dev = d->dev;
  702. struct sk_buff *skb;
  703. struct bootp_pkt *b;
  704. struct iphdr *h;
  705. int hlen = LL_RESERVED_SPACE(dev);
  706. int tlen = dev->needed_tailroom;
  707. /* Allocate packet */
  708. skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
  709. GFP_KERNEL);
  710. if (!skb)
  711. return;
  712. skb_reserve(skb, hlen);
  713. b = skb_put_zero(skb, sizeof(struct bootp_pkt));
  714. /* Construct IP header */
  715. skb_reset_network_header(skb);
  716. h = ip_hdr(skb);
  717. h->version = 4;
  718. h->ihl = 5;
  719. h->tot_len = htons(sizeof(struct bootp_pkt));
  720. h->frag_off = htons(IP_DF);
  721. h->ttl = 64;
  722. h->protocol = IPPROTO_UDP;
  723. h->daddr = htonl(INADDR_BROADCAST);
  724. h->check = ip_fast_csum((unsigned char *) h, h->ihl);
  725. /* Construct UDP header */
  726. b->udph.source = htons(68);
  727. b->udph.dest = htons(67);
  728. b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
  729. /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
  730. /* Construct DHCP/BOOTP header */
  731. b->op = BOOTP_REQUEST;
  732. if (dev->type < 256) /* check for false types */
  733. b->htype = dev->type;
  734. else if (dev->type == ARPHRD_FDDI)
  735. b->htype = ARPHRD_ETHER;
  736. else {
  737. pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type,
  738. dev->name);
  739. b->htype = dev->type; /* can cause undefined behavior */
  740. }
  741. /* server_ip and your_ip address are both already zero per RFC2131 */
  742. b->hlen = dev->addr_len;
  743. memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
  744. b->secs = htons(jiffies_diff / HZ);
  745. b->xid = d->xid;
  746. /* add DHCP options or BOOTP extensions */
  747. #ifdef IPCONFIG_DHCP
  748. if (ic_proto_enabled & IC_USE_DHCP)
  749. ic_dhcp_init_options(b->exten, d);
  750. else
  751. #endif
  752. ic_bootp_init_ext(b->exten);
  753. /* Chain packet down the line... */
  754. skb->dev = dev;
  755. skb->protocol = htons(ETH_P_IP);
  756. if (dev_hard_header(skb, dev, ntohs(skb->protocol),
  757. dev->broadcast, dev->dev_addr, skb->len) < 0) {
  758. kfree_skb(skb);
  759. printk("E");
  760. return;
  761. }
  762. if (dev_queue_xmit(skb) < 0)
  763. printk("E");
  764. }
  765. /*
  766. * Copy BOOTP-supplied string
  767. */
  768. static int __init ic_bootp_string(char *dest, char *src, int len, int max)
  769. {
  770. if (!len)
  771. return 0;
  772. if (len > max-1)
  773. len = max-1;
  774. memcpy(dest, src, len);
  775. dest[len] = '\0';
  776. return 1;
  777. }
  778. /*
  779. * Process BOOTP extensions.
  780. */
  781. static void __init ic_do_bootp_ext(u8 *ext)
  782. {
  783. u8 servers;
  784. int i;
  785. __be16 mtu;
  786. u8 *c;
  787. pr_debug("DHCP/BOOTP: Got extension %d:", *ext);
  788. for (c=ext+2; c<ext+2+ext[1]; c++)
  789. pr_debug(" %02x", *c);
  790. pr_debug("\n");
  791. switch (*ext++) {
  792. case 1: /* Subnet mask */
  793. if (ic_netmask == NONE)
  794. memcpy(&ic_netmask, ext+1, 4);
  795. break;
  796. case 3: /* Default gateway */
  797. if (ic_gateway == NONE)
  798. memcpy(&ic_gateway, ext+1, 4);
  799. break;
  800. case 6: /* DNS server */
  801. servers= *ext/4;
  802. if (servers > CONF_NAMESERVERS_MAX)
  803. servers = CONF_NAMESERVERS_MAX;
  804. for (i = 0; i < servers; i++) {
  805. if (ic_nameservers[i] == NONE ||
  806. ic_nameservers_fallback)
  807. memcpy(&ic_nameservers[i], ext+1+4*i, 4);
  808. }
  809. break;
  810. case 12: /* Host name */
  811. if (!ic_host_name_set) {
  812. ic_bootp_string(utsname()->nodename, ext+1, *ext,
  813. __NEW_UTS_LEN);
  814. ic_host_name_set = 1;
  815. }
  816. break;
  817. case 15: /* Domain name (DNS) */
  818. if (!ic_domain[0])
  819. ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
  820. break;
  821. case 17: /* Root path */
  822. if (!root_server_path[0])
  823. ic_bootp_string(root_server_path, ext+1, *ext,
  824. sizeof(root_server_path));
  825. break;
  826. case 26: /* Interface MTU */
  827. memcpy(&mtu, ext+1, sizeof(mtu));
  828. ic_dev_mtu = ntohs(mtu);
  829. break;
  830. case 40: /* NIS Domain name (_not_ DNS) */
  831. ic_bootp_string(utsname()->domainname, ext+1, *ext,
  832. __NEW_UTS_LEN);
  833. break;
  834. case 42: /* NTP servers */
  835. servers = *ext / 4;
  836. if (servers > CONF_NTP_SERVERS_MAX)
  837. servers = CONF_NTP_SERVERS_MAX;
  838. for (i = 0; i < servers; i++) {
  839. if (ic_ntp_servers[i] == NONE)
  840. memcpy(&ic_ntp_servers[i], ext+1+4*i, 4);
  841. }
  842. break;
  843. }
  844. }
  845. /*
  846. * Receive BOOTP reply.
  847. */
  848. static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  849. {
  850. struct bootp_pkt *b;
  851. struct iphdr *h;
  852. struct ic_device *d;
  853. int len, ext_len;
  854. if (!net_eq(dev_net(dev), &init_net))
  855. goto drop;
  856. /* Perform verifications before taking the lock. */
  857. if (skb->pkt_type == PACKET_OTHERHOST)
  858. goto drop;
  859. skb = skb_share_check(skb, GFP_ATOMIC);
  860. if (!skb)
  861. return NET_RX_DROP;
  862. if (!pskb_may_pull(skb,
  863. sizeof(struct iphdr) +
  864. sizeof(struct udphdr)))
  865. goto drop;
  866. b = (struct bootp_pkt *)skb_network_header(skb);
  867. h = &b->iph;
  868. if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
  869. goto drop;
  870. /* Fragments are not supported */
  871. if (ip_is_fragment(h)) {
  872. net_err_ratelimited("DHCP/BOOTP: Ignoring fragmented reply\n");
  873. goto drop;
  874. }
  875. if (skb->len < ntohs(h->tot_len))
  876. goto drop;
  877. if (ip_fast_csum((char *) h, h->ihl))
  878. goto drop;
  879. if (b->udph.source != htons(67) || b->udph.dest != htons(68))
  880. goto drop;
  881. if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
  882. goto drop;
  883. len = ntohs(b->udph.len) - sizeof(struct udphdr);
  884. ext_len = len - (sizeof(*b) -
  885. sizeof(struct iphdr) -
  886. sizeof(struct udphdr) -
  887. sizeof(b->exten));
  888. if (ext_len < 0)
  889. goto drop;
  890. /* Ok the front looks good, make sure we can get at the rest. */
  891. if (!pskb_may_pull(skb, skb->len))
  892. goto drop;
  893. b = (struct bootp_pkt *)skb_network_header(skb);
  894. h = &b->iph;
  895. /* One reply at a time, please. */
  896. spin_lock(&ic_recv_lock);
  897. /* If we already have a reply, just drop the packet */
  898. if (ic_got_reply)
  899. goto drop_unlock;
  900. /* Find the ic_device that the packet arrived on */
  901. d = ic_first_dev;
  902. while (d && d->dev != dev)
  903. d = d->next;
  904. if (!d)
  905. goto drop_unlock; /* should never happen */
  906. /* Is it a reply to our BOOTP request? */
  907. if (b->op != BOOTP_REPLY ||
  908. b->xid != d->xid) {
  909. net_err_ratelimited("DHCP/BOOTP: Reply not for us on %s, op[%x] xid[%x]\n",
  910. d->dev->name, b->op, b->xid);
  911. goto drop_unlock;
  912. }
  913. /* Parse extensions */
  914. if (ext_len >= 4 &&
  915. !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
  916. u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
  917. u8 *ext;
  918. #ifdef IPCONFIG_DHCP
  919. if (ic_proto_enabled & IC_USE_DHCP) {
  920. __be32 server_id = NONE;
  921. int mt = 0;
  922. ext = &b->exten[4];
  923. while (ext < end && *ext != 0xff) {
  924. u8 *opt = ext++;
  925. if (*opt == 0) /* Padding */
  926. continue;
  927. ext += *ext + 1;
  928. if (ext >= end)
  929. break;
  930. switch (*opt) {
  931. case 53: /* Message type */
  932. if (opt[1])
  933. mt = opt[2];
  934. break;
  935. case 54: /* Server ID (IP address) */
  936. if (opt[1] >= 4)
  937. memcpy(&server_id, opt + 2, 4);
  938. break;
  939. }
  940. }
  941. pr_debug("DHCP: Got message type %d (%s)\n", mt, d->dev->name);
  942. switch (mt) {
  943. case DHCPOFFER:
  944. /* While in the process of accepting one offer,
  945. * ignore all others.
  946. */
  947. if (ic_myaddr != NONE)
  948. goto drop_unlock;
  949. /* Let's accept that offer. */
  950. ic_myaddr = b->your_ip;
  951. ic_servaddr = server_id;
  952. pr_debug("DHCP: Offered address %pI4 by server %pI4\n",
  953. &ic_myaddr, &b->iph.saddr);
  954. /* The DHCP indicated server address takes
  955. * precedence over the bootp header one if
  956. * they are different.
  957. */
  958. if ((server_id != NONE) &&
  959. (b->server_ip != server_id))
  960. b->server_ip = ic_servaddr;
  961. break;
  962. case DHCPACK:
  963. if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
  964. goto drop_unlock;
  965. /* Yeah! */
  966. break;
  967. default:
  968. /* Urque. Forget it*/
  969. ic_myaddr = NONE;
  970. ic_servaddr = NONE;
  971. goto drop_unlock;
  972. }
  973. ic_dhcp_msgtype = mt;
  974. }
  975. #endif /* IPCONFIG_DHCP */
  976. ext = &b->exten[4];
  977. while (ext < end && *ext != 0xff) {
  978. u8 *opt = ext++;
  979. if (*opt == 0) /* Padding */
  980. continue;
  981. ext += *ext + 1;
  982. if (ext < end)
  983. ic_do_bootp_ext(opt);
  984. }
  985. }
  986. /* We have a winner! */
  987. ic_dev = d;
  988. ic_myaddr = b->your_ip;
  989. ic_servaddr = b->server_ip;
  990. ic_addrservaddr = b->iph.saddr;
  991. if (ic_gateway == NONE && b->relay_ip)
  992. ic_gateway = b->relay_ip;
  993. if (ic_nameservers[0] == NONE) {
  994. ic_nameservers[0] = ic_servaddr;
  995. ic_nameservers_fallback = 1;
  996. }
  997. ic_got_reply = IC_BOOTP;
  998. drop_unlock:
  999. /* Show's over. Nothing to see here. */
  1000. spin_unlock(&ic_recv_lock);
  1001. drop:
  1002. /* Throw the packet out. */
  1003. kfree_skb(skb);
  1004. return 0;
  1005. }
  1006. #endif
  1007. /*
  1008. * Dynamic IP configuration -- DHCP, BOOTP, RARP.
  1009. */
  1010. #ifdef IPCONFIG_DYNAMIC
  1011. static int __init ic_dynamic(void)
  1012. {
  1013. int retries;
  1014. struct ic_device *d;
  1015. unsigned long start_jiffies, timeout, jiff;
  1016. int do_bootp = ic_proto_have_if & IC_BOOTP;
  1017. int do_rarp = ic_proto_have_if & IC_RARP;
  1018. /*
  1019. * If none of DHCP/BOOTP/RARP was selected, return with an error.
  1020. * This routine gets only called when some pieces of information
  1021. * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
  1022. */
  1023. if (!ic_proto_enabled) {
  1024. pr_err("IP-Config: Incomplete network configuration information\n");
  1025. return -1;
  1026. }
  1027. #ifdef IPCONFIG_BOOTP
  1028. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
  1029. pr_err("DHCP/BOOTP: No suitable device found\n");
  1030. #endif
  1031. #ifdef IPCONFIG_RARP
  1032. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
  1033. pr_err("RARP: No suitable device found\n");
  1034. #endif
  1035. if (!ic_proto_have_if)
  1036. /* Error message already printed */
  1037. return -1;
  1038. /*
  1039. * Setup protocols
  1040. */
  1041. #ifdef IPCONFIG_BOOTP
  1042. if (do_bootp)
  1043. ic_bootp_init();
  1044. #endif
  1045. #ifdef IPCONFIG_RARP
  1046. if (do_rarp)
  1047. ic_rarp_init();
  1048. #endif
  1049. /*
  1050. * Send requests and wait, until we get an answer. This loop
  1051. * seems to be a terrible waste of CPU time, but actually there is
  1052. * only one process running at all, so we don't need to use any
  1053. * scheduler functions.
  1054. * [Actually we could now, but the nothing else running note still
  1055. * applies.. - AC]
  1056. */
  1057. pr_notice("Sending %s%s%s requests .",
  1058. do_bootp
  1059. ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
  1060. (do_bootp && do_rarp) ? " and " : "",
  1061. do_rarp ? "RARP" : "");
  1062. start_jiffies = jiffies;
  1063. d = ic_first_dev;
  1064. retries = CONF_SEND_RETRIES;
  1065. get_random_bytes(&timeout, sizeof(timeout));
  1066. timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned int) CONF_TIMEOUT_RANDOM);
  1067. for (;;) {
  1068. #ifdef IPCONFIG_BOOTP
  1069. if (do_bootp && (d->able & IC_BOOTP))
  1070. ic_bootp_send_if(d, jiffies - start_jiffies);
  1071. #endif
  1072. #ifdef IPCONFIG_RARP
  1073. if (do_rarp && (d->able & IC_RARP))
  1074. ic_rarp_send_if(d);
  1075. #endif
  1076. if (!d->next) {
  1077. jiff = jiffies + timeout;
  1078. while (time_before(jiffies, jiff) && !ic_got_reply)
  1079. schedule_timeout_uninterruptible(1);
  1080. }
  1081. #ifdef IPCONFIG_DHCP
  1082. /* DHCP isn't done until we get a DHCPACK. */
  1083. if ((ic_got_reply & IC_BOOTP) &&
  1084. (ic_proto_enabled & IC_USE_DHCP) &&
  1085. ic_dhcp_msgtype != DHCPACK) {
  1086. ic_got_reply = 0;
  1087. /* continue on device that got the reply */
  1088. d = ic_dev;
  1089. pr_cont(",");
  1090. continue;
  1091. }
  1092. #endif /* IPCONFIG_DHCP */
  1093. if (ic_got_reply) {
  1094. pr_cont(" OK\n");
  1095. break;
  1096. }
  1097. if ((d = d->next))
  1098. continue;
  1099. if (! --retries) {
  1100. pr_cont(" timed out!\n");
  1101. break;
  1102. }
  1103. d = ic_first_dev;
  1104. timeout = timeout CONF_TIMEOUT_MULT;
  1105. if (timeout > CONF_TIMEOUT_MAX)
  1106. timeout = CONF_TIMEOUT_MAX;
  1107. pr_cont(".");
  1108. }
  1109. #ifdef IPCONFIG_BOOTP
  1110. if (do_bootp)
  1111. ic_bootp_cleanup();
  1112. #endif
  1113. #ifdef IPCONFIG_RARP
  1114. if (do_rarp)
  1115. ic_rarp_cleanup();
  1116. #endif
  1117. if (!ic_got_reply) {
  1118. ic_myaddr = NONE;
  1119. return -1;
  1120. }
  1121. pr_info("IP-Config: Got %s answer from %pI4, my address is %pI4\n",
  1122. ((ic_got_reply & IC_RARP) ? "RARP"
  1123. : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
  1124. &ic_addrservaddr, &ic_myaddr);
  1125. return 0;
  1126. }
  1127. #endif /* IPCONFIG_DYNAMIC */
  1128. #ifdef CONFIG_PROC_FS
  1129. /* proc_dir_entry for /proc/net/ipconfig */
  1130. static struct proc_dir_entry *ipconfig_dir;
  1131. /* Name servers: */
  1132. static int pnp_seq_show(struct seq_file *seq, void *v)
  1133. {
  1134. int i;
  1135. if (ic_proto_used & IC_PROTO)
  1136. seq_printf(seq, "#PROTO: %s\n",
  1137. (ic_proto_used & IC_RARP) ? "RARP"
  1138. : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
  1139. else
  1140. seq_puts(seq, "#MANUAL\n");
  1141. if (ic_domain[0])
  1142. seq_printf(seq,
  1143. "domain %s\n", ic_domain);
  1144. for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
  1145. if (ic_nameservers[i] != NONE)
  1146. seq_printf(seq, "nameserver %pI4\n",
  1147. &ic_nameservers[i]);
  1148. }
  1149. if (ic_servaddr != NONE)
  1150. seq_printf(seq, "bootserver %pI4\n",
  1151. &ic_servaddr);
  1152. return 0;
  1153. }
  1154. /* Create the /proc/net/ipconfig directory */
  1155. static int __init ipconfig_proc_net_init(void)
  1156. {
  1157. ipconfig_dir = proc_net_mkdir(&init_net, "ipconfig", init_net.proc_net);
  1158. if (!ipconfig_dir)
  1159. return -ENOMEM;
  1160. return 0;
  1161. }
  1162. /* Create a new file under /proc/net/ipconfig */
  1163. static int ipconfig_proc_net_create(const char *name,
  1164. const struct proc_ops *proc_ops)
  1165. {
  1166. char *pname;
  1167. struct proc_dir_entry *p;
  1168. if (!ipconfig_dir)
  1169. return -ENOMEM;
  1170. pname = kasprintf(GFP_KERNEL, "%s%s", "ipconfig/", name);
  1171. if (!pname)
  1172. return -ENOMEM;
  1173. p = proc_create(pname, 0444, init_net.proc_net, proc_ops);
  1174. kfree(pname);
  1175. if (!p)
  1176. return -ENOMEM;
  1177. return 0;
  1178. }
  1179. /* Write NTP server IP addresses to /proc/net/ipconfig/ntp_servers */
  1180. static int ntp_servers_show(struct seq_file *seq, void *v)
  1181. {
  1182. int i;
  1183. for (i = 0; i < CONF_NTP_SERVERS_MAX; i++) {
  1184. if (ic_ntp_servers[i] != NONE)
  1185. seq_printf(seq, "%pI4\n", &ic_ntp_servers[i]);
  1186. }
  1187. return 0;
  1188. }
  1189. DEFINE_PROC_SHOW_ATTRIBUTE(ntp_servers);
  1190. #endif /* CONFIG_PROC_FS */
  1191. /*
  1192. * Extract IP address from the parameter string if needed. Note that we
  1193. * need to have root_server_addr set _before_ IPConfig gets called as it
  1194. * can override it.
  1195. */
  1196. __be32 __init root_nfs_parse_addr(char *name)
  1197. {
  1198. __be32 addr;
  1199. int octets = 0;
  1200. char *cp, *cq;
  1201. cp = cq = name;
  1202. while (octets < 4) {
  1203. while (*cp >= '0' && *cp <= '9')
  1204. cp++;
  1205. if (cp == cq || cp - cq > 3)
  1206. break;
  1207. if (*cp == '.' || octets == 3)
  1208. octets++;
  1209. if (octets < 4)
  1210. cp++;
  1211. cq = cp;
  1212. }
  1213. if (octets == 4 && (*cp == ':' || *cp == '\0')) {
  1214. if (*cp == ':')
  1215. *cp++ = '\0';
  1216. addr = in_aton(name);
  1217. memmove(name, cp, strlen(cp) + 1);
  1218. } else
  1219. addr = NONE;
  1220. return addr;
  1221. }
  1222. #define DEVICE_WAIT_MAX 12 /* 12 seconds */
  1223. static int __init wait_for_devices(void)
  1224. {
  1225. int i;
  1226. bool try_init_devs = true;
  1227. for (i = 0; i < DEVICE_WAIT_MAX; i++) {
  1228. struct net_device *dev;
  1229. int found = 0;
  1230. /* make sure deferred device probes are finished */
  1231. wait_for_device_probe();
  1232. rtnl_lock();
  1233. for_each_netdev(&init_net, dev) {
  1234. if (ic_is_init_dev(dev)) {
  1235. found = 1;
  1236. break;
  1237. }
  1238. }
  1239. rtnl_unlock();
  1240. if (found)
  1241. return 0;
  1242. if (try_init_devs &&
  1243. (ROOT_DEV == Root_NFS || ROOT_DEV == Root_CIFS)) {
  1244. try_init_devs = false;
  1245. wait_for_init_devices_probe();
  1246. }
  1247. ssleep(1);
  1248. }
  1249. return -ENODEV;
  1250. }
  1251. /*
  1252. * IP Autoconfig dispatcher.
  1253. */
  1254. static int __init ip_auto_config(void)
  1255. {
  1256. __be32 addr;
  1257. #ifdef IPCONFIG_DYNAMIC
  1258. int retries = CONF_OPEN_RETRIES;
  1259. #endif
  1260. int err;
  1261. unsigned int i, count;
  1262. /* Initialise all name servers and NTP servers to NONE (but only if the
  1263. * "ip=" or "nfsaddrs=" kernel command line parameters weren't decoded,
  1264. * otherwise we'll overwrite the IP addresses specified there)
  1265. */
  1266. if (ic_set_manually == 0) {
  1267. ic_nameservers_predef();
  1268. ic_ntp_servers_predef();
  1269. }
  1270. #ifdef CONFIG_PROC_FS
  1271. proc_create_single("pnp", 0444, init_net.proc_net, pnp_seq_show);
  1272. if (ipconfig_proc_net_init() == 0)
  1273. ipconfig_proc_net_create("ntp_servers", &ntp_servers_proc_ops);
  1274. #endif /* CONFIG_PROC_FS */
  1275. if (!ic_enable)
  1276. return 0;
  1277. pr_debug("IP-Config: Entered.\n");
  1278. #ifdef IPCONFIG_DYNAMIC
  1279. try_try_again:
  1280. #endif
  1281. /* Wait for devices to appear */
  1282. err = wait_for_devices();
  1283. if (err)
  1284. return err;
  1285. /* Setup all network devices */
  1286. err = ic_open_devs();
  1287. if (err)
  1288. return err;
  1289. /* Give drivers a chance to settle */
  1290. msleep(CONF_POST_OPEN);
  1291. /*
  1292. * If the config information is insufficient (e.g., our IP address or
  1293. * IP address of the boot server is missing or we have multiple network
  1294. * interfaces and no default was set), use BOOTP or RARP to get the
  1295. * missing values.
  1296. */
  1297. if (ic_myaddr == NONE ||
  1298. #if defined(CONFIG_ROOT_NFS) || defined(CONFIG_CIFS_ROOT)
  1299. (root_server_addr == NONE &&
  1300. ic_servaddr == NONE &&
  1301. (ROOT_DEV == Root_NFS || ROOT_DEV == Root_CIFS)) ||
  1302. #endif
  1303. ic_first_dev->next) {
  1304. #ifdef IPCONFIG_DYNAMIC
  1305. if (ic_dynamic() < 0) {
  1306. ic_close_devs();
  1307. /*
  1308. * I don't know why, but sometimes the
  1309. * eepro100 driver (at least) gets upset and
  1310. * doesn't work the first time it's opened.
  1311. * But then if you close it and reopen it, it
  1312. * works just fine. So we need to try that at
  1313. * least once before giving up.
  1314. *
  1315. * Also, if the root will be NFS-mounted, we
  1316. * have nowhere to go if DHCP fails. So we
  1317. * just have to keep trying forever.
  1318. *
  1319. * -- Chip
  1320. */
  1321. #ifdef CONFIG_ROOT_NFS
  1322. if (ROOT_DEV == Root_NFS) {
  1323. pr_err("IP-Config: Retrying forever (NFS root)...\n");
  1324. goto try_try_again;
  1325. }
  1326. #endif
  1327. #ifdef CONFIG_CIFS_ROOT
  1328. if (ROOT_DEV == Root_CIFS) {
  1329. pr_err("IP-Config: Retrying forever (CIFS root)...\n");
  1330. goto try_try_again;
  1331. }
  1332. #endif
  1333. if (--retries) {
  1334. pr_err("IP-Config: Reopening network devices...\n");
  1335. goto try_try_again;
  1336. }
  1337. /* Oh, well. At least we tried. */
  1338. pr_err("IP-Config: Auto-configuration of network failed\n");
  1339. return -1;
  1340. }
  1341. #else /* !DYNAMIC */
  1342. pr_err("IP-Config: Incomplete network configuration information\n");
  1343. ic_close_devs();
  1344. return -1;
  1345. #endif /* IPCONFIG_DYNAMIC */
  1346. } else {
  1347. /* Device selected manually or only one device -> use it */
  1348. ic_dev = ic_first_dev;
  1349. }
  1350. addr = root_nfs_parse_addr(root_server_path);
  1351. if (root_server_addr == NONE)
  1352. root_server_addr = addr;
  1353. /*
  1354. * Use defaults wherever applicable.
  1355. */
  1356. if (ic_defaults() < 0)
  1357. return -1;
  1358. /*
  1359. * Record which protocol was actually used.
  1360. */
  1361. #ifdef IPCONFIG_DYNAMIC
  1362. ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
  1363. #endif
  1364. #ifndef IPCONFIG_SILENT
  1365. /*
  1366. * Clue in the operator.
  1367. */
  1368. pr_info("IP-Config: Complete:\n");
  1369. pr_info(" device=%s, hwaddr=%*phC, ipaddr=%pI4, mask=%pI4, gw=%pI4\n",
  1370. ic_dev->dev->name, ic_dev->dev->addr_len, ic_dev->dev->dev_addr,
  1371. &ic_myaddr, &ic_netmask, &ic_gateway);
  1372. pr_info(" host=%s, domain=%s, nis-domain=%s\n",
  1373. utsname()->nodename, ic_domain, utsname()->domainname);
  1374. pr_info(" bootserver=%pI4, rootserver=%pI4, rootpath=%s",
  1375. &ic_servaddr, &root_server_addr, root_server_path);
  1376. if (ic_dev_mtu)
  1377. pr_cont(", mtu=%d", ic_dev_mtu);
  1378. /* Name servers (if any): */
  1379. for (i = 0, count = 0; i < CONF_NAMESERVERS_MAX; i++) {
  1380. if (ic_nameservers[i] != NONE) {
  1381. if (i == 0)
  1382. pr_info(" nameserver%u=%pI4",
  1383. i, &ic_nameservers[i]);
  1384. else
  1385. pr_cont(", nameserver%u=%pI4",
  1386. i, &ic_nameservers[i]);
  1387. count++;
  1388. }
  1389. if ((i + 1 == CONF_NAMESERVERS_MAX) && count > 0)
  1390. pr_cont("\n");
  1391. }
  1392. /* NTP servers (if any): */
  1393. for (i = 0, count = 0; i < CONF_NTP_SERVERS_MAX; i++) {
  1394. if (ic_ntp_servers[i] != NONE) {
  1395. if (i == 0)
  1396. pr_info(" ntpserver%u=%pI4",
  1397. i, &ic_ntp_servers[i]);
  1398. else
  1399. pr_cont(", ntpserver%u=%pI4",
  1400. i, &ic_ntp_servers[i]);
  1401. count++;
  1402. }
  1403. if ((i + 1 == CONF_NTP_SERVERS_MAX) && count > 0)
  1404. pr_cont("\n");
  1405. }
  1406. #endif /* !SILENT */
  1407. /*
  1408. * Close all network devices except the device we've
  1409. * autoconfigured and set up routes.
  1410. */
  1411. if (ic_setup_if() < 0 || ic_setup_routes() < 0)
  1412. err = -1;
  1413. else
  1414. err = 0;
  1415. ic_close_devs();
  1416. return err;
  1417. }
  1418. late_initcall(ip_auto_config);
  1419. /*
  1420. * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
  1421. * command line parameter. See Documentation/admin-guide/nfs/nfsroot.rst.
  1422. */
  1423. static int __init ic_proto_name(char *name)
  1424. {
  1425. if (!strcmp(name, "on") || !strcmp(name, "any")) {
  1426. return 1;
  1427. }
  1428. if (!strcmp(name, "off") || !strcmp(name, "none")) {
  1429. return 0;
  1430. }
  1431. #ifdef CONFIG_IP_PNP_DHCP
  1432. else if (!strncmp(name, "dhcp", 4)) {
  1433. char *client_id;
  1434. ic_proto_enabled &= ~IC_RARP;
  1435. client_id = strstr(name, "dhcp,");
  1436. if (client_id) {
  1437. char *v;
  1438. client_id = client_id + 5;
  1439. v = strchr(client_id, ',');
  1440. if (!v)
  1441. return 1;
  1442. *v = 0;
  1443. if (kstrtou8(client_id, 0, dhcp_client_identifier))
  1444. pr_debug("DHCP: Invalid client identifier type\n");
  1445. strncpy(dhcp_client_identifier + 1, v + 1, 251);
  1446. *v = ',';
  1447. }
  1448. return 1;
  1449. }
  1450. #endif
  1451. #ifdef CONFIG_IP_PNP_BOOTP
  1452. else if (!strcmp(name, "bootp")) {
  1453. ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
  1454. return 1;
  1455. }
  1456. #endif
  1457. #ifdef CONFIG_IP_PNP_RARP
  1458. else if (!strcmp(name, "rarp")) {
  1459. ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
  1460. return 1;
  1461. }
  1462. #endif
  1463. #ifdef IPCONFIG_DYNAMIC
  1464. else if (!strcmp(name, "both")) {
  1465. ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
  1466. return 1;
  1467. }
  1468. #endif
  1469. return 0;
  1470. }
  1471. static int __init ip_auto_config_setup(char *addrs)
  1472. {
  1473. char *cp, *ip, *dp;
  1474. int num = 0;
  1475. ic_set_manually = 1;
  1476. ic_enable = 1;
  1477. /*
  1478. * If any dhcp, bootp etc options are set, leave autoconfig on
  1479. * and skip the below static IP processing.
  1480. */
  1481. if (ic_proto_name(addrs))
  1482. return 1;
  1483. /* If no static IP is given, turn off autoconfig and bail. */
  1484. if (*addrs == 0 ||
  1485. strcmp(addrs, "off") == 0 ||
  1486. strcmp(addrs, "none") == 0) {
  1487. ic_enable = 0;
  1488. return 1;
  1489. }
  1490. /* Initialise all name servers and NTP servers to NONE */
  1491. ic_nameservers_predef();
  1492. ic_ntp_servers_predef();
  1493. /* Parse string for static IP assignment. */
  1494. ip = addrs;
  1495. while (ip && *ip) {
  1496. if ((cp = strchr(ip, ':')))
  1497. *cp++ = '\0';
  1498. if (strlen(ip) > 0) {
  1499. pr_debug("IP-Config: Parameter #%d: `%s'\n", num, ip);
  1500. switch (num) {
  1501. case 0:
  1502. if ((ic_myaddr = in_aton(ip)) == ANY)
  1503. ic_myaddr = NONE;
  1504. break;
  1505. case 1:
  1506. if ((ic_servaddr = in_aton(ip)) == ANY)
  1507. ic_servaddr = NONE;
  1508. break;
  1509. case 2:
  1510. if ((ic_gateway = in_aton(ip)) == ANY)
  1511. ic_gateway = NONE;
  1512. break;
  1513. case 3:
  1514. if ((ic_netmask = in_aton(ip)) == ANY)
  1515. ic_netmask = NONE;
  1516. break;
  1517. case 4:
  1518. if ((dp = strchr(ip, '.'))) {
  1519. *dp++ = '\0';
  1520. strscpy(utsname()->domainname, dp,
  1521. sizeof(utsname()->domainname));
  1522. }
  1523. strscpy(utsname()->nodename, ip,
  1524. sizeof(utsname()->nodename));
  1525. ic_host_name_set = 1;
  1526. break;
  1527. case 5:
  1528. strscpy(user_dev_name, ip, sizeof(user_dev_name));
  1529. break;
  1530. case 6:
  1531. if (ic_proto_name(ip) == 0 &&
  1532. ic_myaddr == NONE) {
  1533. ic_enable = 0;
  1534. }
  1535. break;
  1536. case 7:
  1537. if (CONF_NAMESERVERS_MAX >= 1) {
  1538. ic_nameservers[0] = in_aton(ip);
  1539. if (ic_nameservers[0] == ANY)
  1540. ic_nameservers[0] = NONE;
  1541. }
  1542. break;
  1543. case 8:
  1544. if (CONF_NAMESERVERS_MAX >= 2) {
  1545. ic_nameservers[1] = in_aton(ip);
  1546. if (ic_nameservers[1] == ANY)
  1547. ic_nameservers[1] = NONE;
  1548. }
  1549. break;
  1550. case 9:
  1551. if (CONF_NTP_SERVERS_MAX >= 1) {
  1552. ic_ntp_servers[0] = in_aton(ip);
  1553. if (ic_ntp_servers[0] == ANY)
  1554. ic_ntp_servers[0] = NONE;
  1555. }
  1556. break;
  1557. }
  1558. }
  1559. ip = cp;
  1560. num++;
  1561. }
  1562. return 1;
  1563. }
  1564. __setup("ip=", ip_auto_config_setup);
  1565. static int __init nfsaddrs_config_setup(char *addrs)
  1566. {
  1567. return ip_auto_config_setup(addrs);
  1568. }
  1569. __setup("nfsaddrs=", nfsaddrs_config_setup);
  1570. static int __init vendor_class_identifier_setup(char *addrs)
  1571. {
  1572. if (strscpy(vendor_class_identifier, addrs,
  1573. sizeof(vendor_class_identifier))
  1574. >= sizeof(vendor_class_identifier))
  1575. pr_warn("DHCP: vendorclass too long, truncated to \"%s\"\n",
  1576. vendor_class_identifier);
  1577. return 1;
  1578. }
  1579. __setup("dhcpclass=", vendor_class_identifier_setup);
  1580. static int __init set_carrier_timeout(char *str)
  1581. {
  1582. ssize_t ret;
  1583. if (!str)
  1584. return 0;
  1585. ret = kstrtouint(str, 0, &carrier_timeout);
  1586. if (ret)
  1587. return 0;
  1588. return 1;
  1589. }
  1590. __setup("carrier_timeout=", set_carrier_timeout);