ipconfig.c 36 KB

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