ipconfig.c 38 KB

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