ipconfig.c 38 KB

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