ipconfig.c 40 KB

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