ipconfig.c 40 KB

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