ipconfig.c 38 KB

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