ipconfig.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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(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,
  394. sizeof(struct arphdr) +
  395. (2 * dev->addr_len) +
  396. (2 * 4)))
  397. goto drop;
  398. /* OK, it is all there and looks valid, process... */
  399. rarp = (struct arphdr *)skb_transport_header(skb);
  400. rarp_ptr = (unsigned char *) (rarp + 1);
  401. /* One reply at a time, please. */
  402. spin_lock(&ic_recv_lock);
  403. /* If we already have a reply, just drop the packet */
  404. if (ic_got_reply)
  405. goto drop_unlock;
  406. /* Find the ic_device that the packet arrived on */
  407. d = ic_first_dev;
  408. while (d && d->dev != dev)
  409. d = d->next;
  410. if (!d)
  411. goto drop_unlock; /* should never happen */
  412. /* Extract variable-width fields */
  413. sha = rarp_ptr;
  414. rarp_ptr += dev->addr_len;
  415. memcpy(&sip, rarp_ptr, 4);
  416. rarp_ptr += 4;
  417. tha = rarp_ptr;
  418. rarp_ptr += dev->addr_len;
  419. memcpy(&tip, rarp_ptr, 4);
  420. /* Discard packets which are not meant for us. */
  421. if (memcmp(tha, dev->dev_addr, dev->addr_len))
  422. goto drop_unlock;
  423. /* Discard packets which are not from specified server. */
  424. if (ic_servaddr != NONE && ic_servaddr != sip)
  425. goto drop_unlock;
  426. /* We have a winner! */
  427. ic_dev = dev;
  428. if (ic_myaddr == NONE)
  429. ic_myaddr = tip;
  430. ic_servaddr = sip;
  431. ic_got_reply = IC_RARP;
  432. drop_unlock:
  433. /* Show's over. Nothing to see here. */
  434. spin_unlock(&ic_recv_lock);
  435. drop:
  436. /* Throw the packet out. */
  437. kfree_skb(skb);
  438. return 0;
  439. }
  440. /*
  441. * Send RARP request packet over a single interface.
  442. */
  443. static void __init ic_rarp_send_if(struct ic_device *d)
  444. {
  445. struct net_device *dev = d->dev;
  446. arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
  447. dev->dev_addr, dev->dev_addr);
  448. }
  449. #endif
  450. /*
  451. * DHCP/BOOTP support.
  452. */
  453. #ifdef IPCONFIG_BOOTP
  454. struct bootp_pkt { /* BOOTP packet format */
  455. struct iphdr iph; /* IP header */
  456. struct udphdr udph; /* UDP header */
  457. u8 op; /* 1=request, 2=reply */
  458. u8 htype; /* HW address type */
  459. u8 hlen; /* HW address length */
  460. u8 hops; /* Used only by gateways */
  461. __be32 xid; /* Transaction ID */
  462. __be16 secs; /* Seconds since we started */
  463. __be16 flags; /* Just what it says */
  464. __be32 client_ip; /* Client's IP address if known */
  465. __be32 your_ip; /* Assigned IP address */
  466. __be32 server_ip; /* (Next, e.g. NFS) Server's IP address */
  467. __be32 relay_ip; /* IP address of BOOTP relay */
  468. u8 hw_addr[16]; /* Client's HW address */
  469. u8 serv_name[64]; /* Server host name */
  470. u8 boot_file[128]; /* Name of boot file */
  471. u8 exten[312]; /* DHCP options / BOOTP vendor extensions */
  472. };
  473. /* packet ops */
  474. #define BOOTP_REQUEST 1
  475. #define BOOTP_REPLY 2
  476. /* DHCP message types */
  477. #define DHCPDISCOVER 1
  478. #define DHCPOFFER 2
  479. #define DHCPREQUEST 3
  480. #define DHCPDECLINE 4
  481. #define DHCPACK 5
  482. #define DHCPNAK 6
  483. #define DHCPRELEASE 7
  484. #define DHCPINFORM 8
  485. static int ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev);
  486. static struct packet_type bootp_packet_type __initdata = {
  487. .type = __constant_htons(ETH_P_IP),
  488. .func = ic_bootp_recv,
  489. };
  490. /*
  491. * Initialize DHCP/BOOTP extension fields in the request.
  492. */
  493. static const u8 ic_bootp_cookie[4] = { 99, 130, 83, 99 };
  494. #ifdef IPCONFIG_DHCP
  495. static void __init
  496. ic_dhcp_init_options(u8 *options)
  497. {
  498. u8 mt = ((ic_servaddr == NONE)
  499. ? DHCPDISCOVER : DHCPREQUEST);
  500. u8 *e = options;
  501. int len;
  502. #ifdef IPCONFIG_DEBUG
  503. printk("DHCP: Sending message type %d\n", mt);
  504. #endif
  505. memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
  506. e += 4;
  507. *e++ = 53; /* DHCP message type */
  508. *e++ = 1;
  509. *e++ = mt;
  510. if (mt == DHCPREQUEST) {
  511. *e++ = 54; /* Server ID (IP address) */
  512. *e++ = 4;
  513. memcpy(e, &ic_servaddr, 4);
  514. e += 4;
  515. *e++ = 50; /* Requested IP address */
  516. *e++ = 4;
  517. memcpy(e, &ic_myaddr, 4);
  518. e += 4;
  519. }
  520. /* always? */
  521. {
  522. static const u8 ic_req_params[] = {
  523. 1, /* Subnet mask */
  524. 3, /* Default gateway */
  525. 6, /* DNS server */
  526. 12, /* Host name */
  527. 15, /* Domain name */
  528. 17, /* Boot path */
  529. 40, /* NIS domain name */
  530. };
  531. *e++ = 55; /* Parameter request list */
  532. *e++ = sizeof(ic_req_params);
  533. memcpy(e, ic_req_params, sizeof(ic_req_params));
  534. e += sizeof(ic_req_params);
  535. if (*vendor_class_identifier) {
  536. printk(KERN_INFO "DHCP: sending class identifier \"%s\"\n",
  537. vendor_class_identifier);
  538. *e++ = 60; /* Class-identifier */
  539. len = strlen(vendor_class_identifier);
  540. *e++ = len;
  541. memcpy(e, vendor_class_identifier, len);
  542. e += len;
  543. }
  544. }
  545. *e++ = 255; /* End of the list */
  546. }
  547. #endif /* IPCONFIG_DHCP */
  548. static void __init ic_bootp_init_ext(u8 *e)
  549. {
  550. memcpy(e, ic_bootp_cookie, 4); /* RFC1048 Magic Cookie */
  551. e += 4;
  552. *e++ = 1; /* Subnet mask request */
  553. *e++ = 4;
  554. e += 4;
  555. *e++ = 3; /* Default gateway request */
  556. *e++ = 4;
  557. e += 4;
  558. *e++ = 5; /* Name server request */
  559. *e++ = 8;
  560. e += 8;
  561. *e++ = 12; /* Host name request */
  562. *e++ = 32;
  563. e += 32;
  564. *e++ = 40; /* NIS Domain name request */
  565. *e++ = 32;
  566. e += 32;
  567. *e++ = 17; /* Boot path */
  568. *e++ = 40;
  569. e += 40;
  570. *e++ = 57; /* set extension buffer size for reply */
  571. *e++ = 2;
  572. *e++ = 1; /* 128+236+8+20+14, see dhcpd sources */
  573. *e++ = 150;
  574. *e++ = 255; /* End of the list */
  575. }
  576. /*
  577. * Initialize the DHCP/BOOTP mechanism.
  578. */
  579. static inline void ic_bootp_init(void)
  580. {
  581. int i;
  582. for (i = 0; i < CONF_NAMESERVERS_MAX; i++)
  583. ic_nameservers[i] = NONE;
  584. dev_add_pack(&bootp_packet_type);
  585. }
  586. /*
  587. * DHCP/BOOTP cleanup.
  588. */
  589. static inline void ic_bootp_cleanup(void)
  590. {
  591. dev_remove_pack(&bootp_packet_type);
  592. }
  593. /*
  594. * Send DHCP/BOOTP request to single interface.
  595. */
  596. static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
  597. {
  598. struct net_device *dev = d->dev;
  599. struct sk_buff *skb;
  600. struct bootp_pkt *b;
  601. int hh_len = LL_RESERVED_SPACE(dev);
  602. struct iphdr *h;
  603. /* Allocate packet */
  604. skb = alloc_skb(sizeof(struct bootp_pkt) + hh_len + 15, GFP_KERNEL);
  605. if (!skb)
  606. return;
  607. skb_reserve(skb, hh_len);
  608. b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
  609. memset(b, 0, sizeof(struct bootp_pkt));
  610. /* Construct IP header */
  611. skb_reset_network_header(skb);
  612. h = ip_hdr(skb);
  613. h->version = 4;
  614. h->ihl = 5;
  615. h->tot_len = htons(sizeof(struct bootp_pkt));
  616. h->frag_off = htons(IP_DF);
  617. h->ttl = 64;
  618. h->protocol = IPPROTO_UDP;
  619. h->daddr = htonl(INADDR_BROADCAST);
  620. h->check = ip_fast_csum((unsigned char *) h, h->ihl);
  621. /* Construct UDP header */
  622. b->udph.source = htons(68);
  623. b->udph.dest = htons(67);
  624. b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));
  625. /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */
  626. /* Construct DHCP/BOOTP header */
  627. b->op = BOOTP_REQUEST;
  628. if (dev->type < 256) /* check for false types */
  629. b->htype = dev->type;
  630. else if (dev->type == ARPHRD_IEEE802_TR) /* fix for token ring */
  631. b->htype = ARPHRD_IEEE802;
  632. else if (dev->type == ARPHRD_FDDI)
  633. b->htype = ARPHRD_ETHER;
  634. else {
  635. printk("Unknown ARP type 0x%04x for device %s\n", dev->type, dev->name);
  636. b->htype = dev->type; /* can cause undefined behavior */
  637. }
  638. /* server_ip and your_ip address are both already zero per RFC2131 */
  639. b->hlen = dev->addr_len;
  640. memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
  641. b->secs = htons(jiffies_diff / HZ);
  642. b->xid = d->xid;
  643. /* add DHCP options or BOOTP extensions */
  644. #ifdef IPCONFIG_DHCP
  645. if (ic_proto_enabled & IC_USE_DHCP)
  646. ic_dhcp_init_options(b->exten);
  647. else
  648. #endif
  649. ic_bootp_init_ext(b->exten);
  650. /* Chain packet down the line... */
  651. skb->dev = dev;
  652. skb->protocol = htons(ETH_P_IP);
  653. if (dev_hard_header(skb, dev, ntohs(skb->protocol),
  654. dev->broadcast, dev->dev_addr, skb->len) < 0 ||
  655. dev_queue_xmit(skb) < 0)
  656. printk("E");
  657. }
  658. /*
  659. * Copy BOOTP-supplied string if not already set.
  660. */
  661. static int __init ic_bootp_string(char *dest, char *src, int len, int max)
  662. {
  663. if (!len)
  664. return 0;
  665. if (len > max-1)
  666. len = max-1;
  667. memcpy(dest, src, len);
  668. dest[len] = '\0';
  669. return 1;
  670. }
  671. /*
  672. * Process BOOTP extensions.
  673. */
  674. static void __init ic_do_bootp_ext(u8 *ext)
  675. {
  676. u8 servers;
  677. int i;
  678. #ifdef IPCONFIG_DEBUG
  679. u8 *c;
  680. printk("DHCP/BOOTP: Got extension %d:",*ext);
  681. for (c=ext+2; c<ext+2+ext[1]; c++)
  682. printk(" %02x", *c);
  683. printk("\n");
  684. #endif
  685. switch (*ext++) {
  686. case 1: /* Subnet mask */
  687. if (ic_netmask == NONE)
  688. memcpy(&ic_netmask, ext+1, 4);
  689. break;
  690. case 3: /* Default gateway */
  691. if (ic_gateway == NONE)
  692. memcpy(&ic_gateway, ext+1, 4);
  693. break;
  694. case 6: /* DNS server */
  695. servers= *ext/4;
  696. if (servers > CONF_NAMESERVERS_MAX)
  697. servers = CONF_NAMESERVERS_MAX;
  698. for (i = 0; i < servers; i++) {
  699. if (ic_nameservers[i] == NONE)
  700. memcpy(&ic_nameservers[i], ext+1+4*i, 4);
  701. }
  702. break;
  703. case 12: /* Host name */
  704. ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
  705. ic_host_name_set = 1;
  706. break;
  707. case 15: /* Domain name (DNS) */
  708. ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
  709. break;
  710. case 17: /* Root path */
  711. if (!root_server_path[0])
  712. ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
  713. break;
  714. case 40: /* NIS Domain name (_not_ DNS) */
  715. ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
  716. break;
  717. }
  718. }
  719. /*
  720. * Receive BOOTP reply.
  721. */
  722. static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  723. {
  724. struct bootp_pkt *b;
  725. struct iphdr *h;
  726. struct ic_device *d;
  727. int len, ext_len;
  728. if (dev->nd_net != &init_net)
  729. goto drop;
  730. /* Perform verifications before taking the lock. */
  731. if (skb->pkt_type == PACKET_OTHERHOST)
  732. goto drop;
  733. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  734. return NET_RX_DROP;
  735. if (!pskb_may_pull(skb,
  736. sizeof(struct iphdr) +
  737. sizeof(struct udphdr)))
  738. goto drop;
  739. b = (struct bootp_pkt *)skb_network_header(skb);
  740. h = &b->iph;
  741. if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
  742. goto drop;
  743. /* Fragments are not supported */
  744. if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
  745. if (net_ratelimit())
  746. printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
  747. "reply.\n");
  748. goto drop;
  749. }
  750. if (skb->len < ntohs(h->tot_len))
  751. goto drop;
  752. if (ip_fast_csum((char *) h, h->ihl))
  753. goto drop;
  754. if (b->udph.source != htons(67) || b->udph.dest != htons(68))
  755. goto drop;
  756. if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
  757. goto drop;
  758. len = ntohs(b->udph.len) - sizeof(struct udphdr);
  759. ext_len = len - (sizeof(*b) -
  760. sizeof(struct iphdr) -
  761. sizeof(struct udphdr) -
  762. sizeof(b->exten));
  763. if (ext_len < 0)
  764. goto drop;
  765. /* Ok the front looks good, make sure we can get at the rest. */
  766. if (!pskb_may_pull(skb, skb->len))
  767. goto drop;
  768. b = (struct bootp_pkt *)skb_network_header(skb);
  769. h = &b->iph;
  770. /* One reply at a time, please. */
  771. spin_lock(&ic_recv_lock);
  772. /* If we already have a reply, just drop the packet */
  773. if (ic_got_reply)
  774. goto drop_unlock;
  775. /* Find the ic_device that the packet arrived on */
  776. d = ic_first_dev;
  777. while (d && d->dev != dev)
  778. d = d->next;
  779. if (!d)
  780. goto drop_unlock; /* should never happen */
  781. /* Is it a reply to our BOOTP request? */
  782. if (b->op != BOOTP_REPLY ||
  783. b->xid != d->xid) {
  784. if (net_ratelimit())
  785. printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
  786. "op[%x] xid[%x]\n",
  787. b->op, b->xid);
  788. goto drop_unlock;
  789. }
  790. /* Parse extensions */
  791. if (ext_len >= 4 &&
  792. !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
  793. u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
  794. u8 *ext;
  795. #ifdef IPCONFIG_DHCP
  796. if (ic_proto_enabled & IC_USE_DHCP) {
  797. __be32 server_id = NONE;
  798. int mt = 0;
  799. ext = &b->exten[4];
  800. while (ext < end && *ext != 0xff) {
  801. u8 *opt = ext++;
  802. if (*opt == 0) /* Padding */
  803. continue;
  804. ext += *ext + 1;
  805. if (ext >= end)
  806. break;
  807. switch (*opt) {
  808. case 53: /* Message type */
  809. if (opt[1])
  810. mt = opt[2];
  811. break;
  812. case 54: /* Server ID (IP address) */
  813. if (opt[1] >= 4)
  814. memcpy(&server_id, opt + 2, 4);
  815. break;
  816. }
  817. }
  818. #ifdef IPCONFIG_DEBUG
  819. printk("DHCP: Got message type %d\n", mt);
  820. #endif
  821. switch (mt) {
  822. case DHCPOFFER:
  823. /* While in the process of accepting one offer,
  824. * ignore all others.
  825. */
  826. if (ic_myaddr != NONE)
  827. goto drop_unlock;
  828. /* Let's accept that offer. */
  829. ic_myaddr = b->your_ip;
  830. ic_servaddr = server_id;
  831. #ifdef IPCONFIG_DEBUG
  832. printk("DHCP: Offered address %u.%u.%u.%u",
  833. NIPQUAD(ic_myaddr));
  834. printk(" by server %u.%u.%u.%u\n",
  835. NIPQUAD(ic_servaddr));
  836. #endif
  837. /* The DHCP indicated server address takes
  838. * precedence over the bootp header one if
  839. * they are different.
  840. */
  841. if ((server_id != NONE) &&
  842. (b->server_ip != server_id))
  843. b->server_ip = ic_servaddr;
  844. break;
  845. case DHCPACK:
  846. if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
  847. goto drop_unlock;
  848. /* Yeah! */
  849. break;
  850. default:
  851. /* Urque. Forget it*/
  852. ic_myaddr = NONE;
  853. ic_servaddr = NONE;
  854. goto drop_unlock;
  855. }
  856. ic_dhcp_msgtype = mt;
  857. }
  858. #endif /* IPCONFIG_DHCP */
  859. ext = &b->exten[4];
  860. while (ext < end && *ext != 0xff) {
  861. u8 *opt = ext++;
  862. if (*opt == 0) /* Padding */
  863. continue;
  864. ext += *ext + 1;
  865. if (ext < end)
  866. ic_do_bootp_ext(opt);
  867. }
  868. }
  869. /* We have a winner! */
  870. ic_dev = dev;
  871. ic_myaddr = b->your_ip;
  872. ic_servaddr = b->server_ip;
  873. if (ic_gateway == NONE && b->relay_ip)
  874. ic_gateway = b->relay_ip;
  875. if (ic_nameservers[0] == NONE)
  876. ic_nameservers[0] = ic_servaddr;
  877. ic_got_reply = IC_BOOTP;
  878. drop_unlock:
  879. /* Show's over. Nothing to see here. */
  880. spin_unlock(&ic_recv_lock);
  881. drop:
  882. /* Throw the packet out. */
  883. kfree_skb(skb);
  884. return 0;
  885. }
  886. #endif
  887. /*
  888. * Dynamic IP configuration -- DHCP, BOOTP, RARP.
  889. */
  890. #ifdef IPCONFIG_DYNAMIC
  891. static int __init ic_dynamic(void)
  892. {
  893. int retries;
  894. struct ic_device *d;
  895. unsigned long start_jiffies, timeout, jiff;
  896. int do_bootp = ic_proto_have_if & IC_BOOTP;
  897. int do_rarp = ic_proto_have_if & IC_RARP;
  898. /*
  899. * If none of DHCP/BOOTP/RARP was selected, return with an error.
  900. * This routine gets only called when some pieces of information
  901. * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
  902. */
  903. if (!ic_proto_enabled) {
  904. printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
  905. return -1;
  906. }
  907. #ifdef IPCONFIG_BOOTP
  908. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
  909. printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
  910. #endif
  911. #ifdef IPCONFIG_RARP
  912. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
  913. printk(KERN_ERR "RARP: No suitable device found.\n");
  914. #endif
  915. if (!ic_proto_have_if)
  916. /* Error message already printed */
  917. return -1;
  918. /*
  919. * Setup protocols
  920. */
  921. #ifdef IPCONFIG_BOOTP
  922. if (do_bootp)
  923. ic_bootp_init();
  924. #endif
  925. #ifdef IPCONFIG_RARP
  926. if (do_rarp)
  927. ic_rarp_init();
  928. #endif
  929. /*
  930. * Send requests and wait, until we get an answer. This loop
  931. * seems to be a terrible waste of CPU time, but actually there is
  932. * only one process running at all, so we don't need to use any
  933. * scheduler functions.
  934. * [Actually we could now, but the nothing else running note still
  935. * applies.. - AC]
  936. */
  937. printk(KERN_NOTICE "Sending %s%s%s requests .",
  938. do_bootp
  939. ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
  940. (do_bootp && do_rarp) ? " and " : "",
  941. do_rarp ? "RARP" : "");
  942. start_jiffies = jiffies;
  943. d = ic_first_dev;
  944. retries = CONF_SEND_RETRIES;
  945. get_random_bytes(&timeout, sizeof(timeout));
  946. timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
  947. for (;;) {
  948. #ifdef IPCONFIG_BOOTP
  949. if (do_bootp && (d->able & IC_BOOTP))
  950. ic_bootp_send_if(d, jiffies - start_jiffies);
  951. #endif
  952. #ifdef IPCONFIG_RARP
  953. if (do_rarp && (d->able & IC_RARP))
  954. ic_rarp_send_if(d);
  955. #endif
  956. jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
  957. while (time_before(jiffies, jiff) && !ic_got_reply)
  958. schedule_timeout_uninterruptible(1);
  959. #ifdef IPCONFIG_DHCP
  960. /* DHCP isn't done until we get a DHCPACK. */
  961. if ((ic_got_reply & IC_BOOTP)
  962. && (ic_proto_enabled & IC_USE_DHCP)
  963. && ic_dhcp_msgtype != DHCPACK)
  964. {
  965. ic_got_reply = 0;
  966. printk(",");
  967. continue;
  968. }
  969. #endif /* IPCONFIG_DHCP */
  970. if (ic_got_reply) {
  971. printk(" OK\n");
  972. break;
  973. }
  974. if ((d = d->next))
  975. continue;
  976. if (! --retries) {
  977. printk(" timed out!\n");
  978. break;
  979. }
  980. d = ic_first_dev;
  981. timeout = timeout CONF_TIMEOUT_MULT;
  982. if (timeout > CONF_TIMEOUT_MAX)
  983. timeout = CONF_TIMEOUT_MAX;
  984. printk(".");
  985. }
  986. #ifdef IPCONFIG_BOOTP
  987. if (do_bootp)
  988. ic_bootp_cleanup();
  989. #endif
  990. #ifdef IPCONFIG_RARP
  991. if (do_rarp)
  992. ic_rarp_cleanup();
  993. #endif
  994. if (!ic_got_reply) {
  995. ic_myaddr = NONE;
  996. return -1;
  997. }
  998. printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
  999. ((ic_got_reply & IC_RARP) ? "RARP"
  1000. : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
  1001. NIPQUAD(ic_servaddr));
  1002. printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
  1003. return 0;
  1004. }
  1005. #endif /* IPCONFIG_DYNAMIC */
  1006. #ifdef CONFIG_PROC_FS
  1007. static int pnp_seq_show(struct seq_file *seq, void *v)
  1008. {
  1009. int i;
  1010. if (ic_proto_used & IC_PROTO)
  1011. seq_printf(seq, "#PROTO: %s\n",
  1012. (ic_proto_used & IC_RARP) ? "RARP"
  1013. : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
  1014. else
  1015. seq_puts(seq, "#MANUAL\n");
  1016. if (ic_domain[0])
  1017. seq_printf(seq,
  1018. "domain %s\n", ic_domain);
  1019. for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
  1020. if (ic_nameservers[i] != NONE)
  1021. seq_printf(seq,
  1022. "nameserver %u.%u.%u.%u\n",
  1023. NIPQUAD(ic_nameservers[i]));
  1024. }
  1025. if (ic_servaddr != NONE)
  1026. seq_printf(seq,
  1027. "bootserver %u.%u.%u.%u\n",
  1028. NIPQUAD(ic_servaddr));
  1029. return 0;
  1030. }
  1031. static int pnp_seq_open(struct inode *indoe, struct file *file)
  1032. {
  1033. return single_open(file, pnp_seq_show, NULL);
  1034. }
  1035. static const struct file_operations pnp_seq_fops = {
  1036. .owner = THIS_MODULE,
  1037. .open = pnp_seq_open,
  1038. .read = seq_read,
  1039. .llseek = seq_lseek,
  1040. .release = single_release,
  1041. };
  1042. #endif /* CONFIG_PROC_FS */
  1043. /*
  1044. * Extract IP address from the parameter string if needed. Note that we
  1045. * need to have root_server_addr set _before_ IPConfig gets called as it
  1046. * can override it.
  1047. */
  1048. __be32 __init root_nfs_parse_addr(char *name)
  1049. {
  1050. __be32 addr;
  1051. int octets = 0;
  1052. char *cp, *cq;
  1053. cp = cq = name;
  1054. while (octets < 4) {
  1055. while (*cp >= '0' && *cp <= '9')
  1056. cp++;
  1057. if (cp == cq || cp - cq > 3)
  1058. break;
  1059. if (*cp == '.' || octets == 3)
  1060. octets++;
  1061. if (octets < 4)
  1062. cp++;
  1063. cq = cp;
  1064. }
  1065. if (octets == 4 && (*cp == ':' || *cp == '\0')) {
  1066. if (*cp == ':')
  1067. *cp++ = '\0';
  1068. addr = in_aton(name);
  1069. memmove(name, cp, strlen(cp) + 1);
  1070. } else
  1071. addr = NONE;
  1072. return addr;
  1073. }
  1074. /*
  1075. * IP Autoconfig dispatcher.
  1076. */
  1077. static int __init ip_auto_config(void)
  1078. {
  1079. __be32 addr;
  1080. #ifdef CONFIG_PROC_FS
  1081. proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
  1082. #endif /* CONFIG_PROC_FS */
  1083. if (!ic_enable)
  1084. return 0;
  1085. DBG(("IP-Config: Entered.\n"));
  1086. #ifdef IPCONFIG_DYNAMIC
  1087. try_try_again:
  1088. #endif
  1089. /* Give hardware a chance to settle */
  1090. msleep(CONF_PRE_OPEN);
  1091. /* Setup all network devices */
  1092. if (ic_open_devs() < 0)
  1093. return -1;
  1094. /* Give drivers a chance to settle */
  1095. ssleep(CONF_POST_OPEN);
  1096. /*
  1097. * If the config information is insufficient (e.g., our IP address or
  1098. * IP address of the boot server is missing or we have multiple network
  1099. * interfaces and no default was set), use BOOTP or RARP to get the
  1100. * missing values.
  1101. */
  1102. if (ic_myaddr == NONE ||
  1103. #ifdef CONFIG_ROOT_NFS
  1104. (root_server_addr == NONE
  1105. && ic_servaddr == NONE
  1106. && ROOT_DEV == Root_NFS) ||
  1107. #endif
  1108. ic_first_dev->next) {
  1109. #ifdef IPCONFIG_DYNAMIC
  1110. int retries = CONF_OPEN_RETRIES;
  1111. if (ic_dynamic() < 0) {
  1112. ic_close_devs();
  1113. /*
  1114. * I don't know why, but sometimes the
  1115. * eepro100 driver (at least) gets upset and
  1116. * doesn't work the first time it's opened.
  1117. * But then if you close it and reopen it, it
  1118. * works just fine. So we need to try that at
  1119. * least once before giving up.
  1120. *
  1121. * Also, if the root will be NFS-mounted, we
  1122. * have nowhere to go if DHCP fails. So we
  1123. * just have to keep trying forever.
  1124. *
  1125. * -- Chip
  1126. */
  1127. #ifdef CONFIG_ROOT_NFS
  1128. if (ROOT_DEV == Root_NFS) {
  1129. printk(KERN_ERR
  1130. "IP-Config: Retrying forever (NFS root)...\n");
  1131. goto try_try_again;
  1132. }
  1133. #endif
  1134. if (--retries) {
  1135. printk(KERN_ERR
  1136. "IP-Config: Reopening network devices...\n");
  1137. goto try_try_again;
  1138. }
  1139. /* Oh, well. At least we tried. */
  1140. printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
  1141. return -1;
  1142. }
  1143. #else /* !DYNAMIC */
  1144. printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
  1145. ic_close_devs();
  1146. return -1;
  1147. #endif /* IPCONFIG_DYNAMIC */
  1148. } else {
  1149. /* Device selected manually or only one device -> use it */
  1150. ic_dev = ic_first_dev->dev;
  1151. }
  1152. addr = root_nfs_parse_addr(root_server_path);
  1153. if (root_server_addr == NONE)
  1154. root_server_addr = addr;
  1155. /*
  1156. * Use defaults whereever applicable.
  1157. */
  1158. if (ic_defaults() < 0)
  1159. return -1;
  1160. /*
  1161. * Close all network devices except the device we've
  1162. * autoconfigured and set up routes.
  1163. */
  1164. ic_close_devs();
  1165. if (ic_setup_if() < 0 || ic_setup_routes() < 0)
  1166. return -1;
  1167. /*
  1168. * Record which protocol was actually used.
  1169. */
  1170. #ifdef IPCONFIG_DYNAMIC
  1171. ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
  1172. #endif
  1173. #ifndef IPCONFIG_SILENT
  1174. /*
  1175. * Clue in the operator.
  1176. */
  1177. printk("IP-Config: Complete:");
  1178. printk("\n device=%s", ic_dev->name);
  1179. printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr));
  1180. printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask));
  1181. printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway));
  1182. printk(",\n host=%s, domain=%s, nis-domain=%s",
  1183. utsname()->nodename, ic_domain, utsname()->domainname);
  1184. printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr));
  1185. printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr));
  1186. printk(", rootpath=%s", root_server_path);
  1187. printk("\n");
  1188. #endif /* !SILENT */
  1189. return 0;
  1190. }
  1191. late_initcall(ip_auto_config);
  1192. /*
  1193. * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
  1194. * command line parameter. See Documentation/nfsroot.txt.
  1195. */
  1196. static int __init ic_proto_name(char *name)
  1197. {
  1198. if (!strcmp(name, "on") || !strcmp(name, "any")) {
  1199. return 1;
  1200. }
  1201. if (!strcmp(name, "off") || !strcmp(name, "none")) {
  1202. return 0;
  1203. }
  1204. #ifdef CONFIG_IP_PNP_DHCP
  1205. else if (!strcmp(name, "dhcp")) {
  1206. ic_proto_enabled &= ~IC_RARP;
  1207. return 1;
  1208. }
  1209. #endif
  1210. #ifdef CONFIG_IP_PNP_BOOTP
  1211. else if (!strcmp(name, "bootp")) {
  1212. ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
  1213. return 1;
  1214. }
  1215. #endif
  1216. #ifdef CONFIG_IP_PNP_RARP
  1217. else if (!strcmp(name, "rarp")) {
  1218. ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
  1219. return 1;
  1220. }
  1221. #endif
  1222. #ifdef IPCONFIG_DYNAMIC
  1223. else if (!strcmp(name, "both")) {
  1224. ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
  1225. return 1;
  1226. }
  1227. #endif
  1228. return 0;
  1229. }
  1230. static int __init ip_auto_config_setup(char *addrs)
  1231. {
  1232. char *cp, *ip, *dp;
  1233. int num = 0;
  1234. ic_set_manually = 1;
  1235. ic_enable = 1;
  1236. /*
  1237. * If any dhcp, bootp etc options are set, leave autoconfig on
  1238. * and skip the below static IP processing.
  1239. */
  1240. if (ic_proto_name(addrs))
  1241. return 1;
  1242. /* If no static IP is given, turn off autoconfig and bail. */
  1243. if (*addrs == 0 ||
  1244. strcmp(addrs, "off") == 0 ||
  1245. strcmp(addrs, "none") == 0) {
  1246. ic_enable = 0;
  1247. return 1;
  1248. }
  1249. /* Parse string for static IP assignment. */
  1250. ip = addrs;
  1251. while (ip && *ip) {
  1252. if ((cp = strchr(ip, ':')))
  1253. *cp++ = '\0';
  1254. if (strlen(ip) > 0) {
  1255. DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
  1256. switch (num) {
  1257. case 0:
  1258. if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
  1259. ic_myaddr = NONE;
  1260. break;
  1261. case 1:
  1262. if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
  1263. ic_servaddr = NONE;
  1264. break;
  1265. case 2:
  1266. if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
  1267. ic_gateway = NONE;
  1268. break;
  1269. case 3:
  1270. if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
  1271. ic_netmask = NONE;
  1272. break;
  1273. case 4:
  1274. if ((dp = strchr(ip, '.'))) {
  1275. *dp++ = '\0';
  1276. strlcpy(utsname()->domainname, dp,
  1277. sizeof(utsname()->domainname));
  1278. }
  1279. strlcpy(utsname()->nodename, ip,
  1280. sizeof(utsname()->nodename));
  1281. ic_host_name_set = 1;
  1282. break;
  1283. case 5:
  1284. strlcpy(user_dev_name, ip, sizeof(user_dev_name));
  1285. break;
  1286. case 6:
  1287. if (ic_proto_name(ip) == 0 &&
  1288. ic_myaddr == NONE) {
  1289. ic_enable = 0;
  1290. }
  1291. break;
  1292. }
  1293. }
  1294. ip = cp;
  1295. num++;
  1296. }
  1297. return 1;
  1298. }
  1299. static int __init nfsaddrs_config_setup(char *addrs)
  1300. {
  1301. return ip_auto_config_setup(addrs);
  1302. }
  1303. static int __init vendor_class_identifier_setup(char *addrs)
  1304. {
  1305. if (strlcpy(vendor_class_identifier, addrs,
  1306. sizeof(vendor_class_identifier))
  1307. >= sizeof(vendor_class_identifier))
  1308. printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
  1309. vendor_class_identifier);
  1310. return 1;
  1311. }
  1312. __setup("ip=", ip_auto_config_setup);
  1313. __setup("nfsaddrs=", nfsaddrs_config_setup);
  1314. __setup("dhcpclass=", vendor_class_identifier_setup);