ipconfig.c 43 KB

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