ipconfig.c 38 KB

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