ipconfig.c 42 KB

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