ipconfig.c 38 KB

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