ipconfig.c 38 KB

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