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. b->hlen = dev->addr_len;
  639. b->your_ip = NONE;
  640. b->server_ip = NONE;
  641. memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
  642. b->secs = htons(jiffies_diff / HZ);
  643. b->xid = d->xid;
  644. /* add DHCP options or BOOTP extensions */
  645. #ifdef IPCONFIG_DHCP
  646. if (ic_proto_enabled & IC_USE_DHCP)
  647. ic_dhcp_init_options(b->exten);
  648. else
  649. #endif
  650. ic_bootp_init_ext(b->exten);
  651. /* Chain packet down the line... */
  652. skb->dev = dev;
  653. skb->protocol = htons(ETH_P_IP);
  654. if (dev_hard_header(skb, dev, ntohs(skb->protocol),
  655. dev->broadcast, dev->dev_addr, skb->len) < 0 ||
  656. dev_queue_xmit(skb) < 0)
  657. printk("E");
  658. }
  659. /*
  660. * Copy BOOTP-supplied string if not already set.
  661. */
  662. static int __init ic_bootp_string(char *dest, char *src, int len, int max)
  663. {
  664. if (!len)
  665. return 0;
  666. if (len > max-1)
  667. len = max-1;
  668. memcpy(dest, src, len);
  669. dest[len] = '\0';
  670. return 1;
  671. }
  672. /*
  673. * Process BOOTP extensions.
  674. */
  675. static void __init ic_do_bootp_ext(u8 *ext)
  676. {
  677. u8 servers;
  678. int i;
  679. #ifdef IPCONFIG_DEBUG
  680. u8 *c;
  681. printk("DHCP/BOOTP: Got extension %d:",*ext);
  682. for (c=ext+2; c<ext+2+ext[1]; c++)
  683. printk(" %02x", *c);
  684. printk("\n");
  685. #endif
  686. switch (*ext++) {
  687. case 1: /* Subnet mask */
  688. if (ic_netmask == NONE)
  689. memcpy(&ic_netmask, ext+1, 4);
  690. break;
  691. case 3: /* Default gateway */
  692. if (ic_gateway == NONE)
  693. memcpy(&ic_gateway, ext+1, 4);
  694. break;
  695. case 6: /* DNS server */
  696. servers= *ext/4;
  697. if (servers > CONF_NAMESERVERS_MAX)
  698. servers = CONF_NAMESERVERS_MAX;
  699. for (i = 0; i < servers; i++) {
  700. if (ic_nameservers[i] == NONE)
  701. memcpy(&ic_nameservers[i], ext+1+4*i, 4);
  702. }
  703. break;
  704. case 12: /* Host name */
  705. ic_bootp_string(utsname()->nodename, ext+1, *ext, __NEW_UTS_LEN);
  706. ic_host_name_set = 1;
  707. break;
  708. case 15: /* Domain name (DNS) */
  709. ic_bootp_string(ic_domain, ext+1, *ext, sizeof(ic_domain));
  710. break;
  711. case 17: /* Root path */
  712. if (!root_server_path[0])
  713. ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
  714. break;
  715. case 40: /* NIS Domain name (_not_ DNS) */
  716. ic_bootp_string(utsname()->domainname, ext+1, *ext, __NEW_UTS_LEN);
  717. break;
  718. }
  719. }
  720. /*
  721. * Receive BOOTP reply.
  722. */
  723. static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  724. {
  725. struct bootp_pkt *b;
  726. struct iphdr *h;
  727. struct ic_device *d;
  728. int len, ext_len;
  729. if (dev->nd_net != &init_net)
  730. goto drop;
  731. /* Perform verifications before taking the lock. */
  732. if (skb->pkt_type == PACKET_OTHERHOST)
  733. goto drop;
  734. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  735. return NET_RX_DROP;
  736. if (!pskb_may_pull(skb,
  737. sizeof(struct iphdr) +
  738. sizeof(struct udphdr)))
  739. goto drop;
  740. b = (struct bootp_pkt *)skb_network_header(skb);
  741. h = &b->iph;
  742. if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP)
  743. goto drop;
  744. /* Fragments are not supported */
  745. if (h->frag_off & htons(IP_OFFSET | IP_MF)) {
  746. if (net_ratelimit())
  747. printk(KERN_ERR "DHCP/BOOTP: Ignoring fragmented "
  748. "reply.\n");
  749. goto drop;
  750. }
  751. if (skb->len < ntohs(h->tot_len))
  752. goto drop;
  753. if (ip_fast_csum((char *) h, h->ihl))
  754. goto drop;
  755. if (b->udph.source != htons(67) || b->udph.dest != htons(68))
  756. goto drop;
  757. if (ntohs(h->tot_len) < ntohs(b->udph.len) + sizeof(struct iphdr))
  758. goto drop;
  759. len = ntohs(b->udph.len) - sizeof(struct udphdr);
  760. ext_len = len - (sizeof(*b) -
  761. sizeof(struct iphdr) -
  762. sizeof(struct udphdr) -
  763. sizeof(b->exten));
  764. if (ext_len < 0)
  765. goto drop;
  766. /* Ok the front looks good, make sure we can get at the rest. */
  767. if (!pskb_may_pull(skb, skb->len))
  768. goto drop;
  769. b = (struct bootp_pkt *)skb_network_header(skb);
  770. h = &b->iph;
  771. /* One reply at a time, please. */
  772. spin_lock(&ic_recv_lock);
  773. /* If we already have a reply, just drop the packet */
  774. if (ic_got_reply)
  775. goto drop_unlock;
  776. /* Find the ic_device that the packet arrived on */
  777. d = ic_first_dev;
  778. while (d && d->dev != dev)
  779. d = d->next;
  780. if (!d)
  781. goto drop_unlock; /* should never happen */
  782. /* Is it a reply to our BOOTP request? */
  783. if (b->op != BOOTP_REPLY ||
  784. b->xid != d->xid) {
  785. if (net_ratelimit())
  786. printk(KERN_ERR "DHCP/BOOTP: Reply not for us, "
  787. "op[%x] xid[%x]\n",
  788. b->op, b->xid);
  789. goto drop_unlock;
  790. }
  791. /* Parse extensions */
  792. if (ext_len >= 4 &&
  793. !memcmp(b->exten, ic_bootp_cookie, 4)) { /* Check magic cookie */
  794. u8 *end = (u8 *) b + ntohs(b->iph.tot_len);
  795. u8 *ext;
  796. #ifdef IPCONFIG_DHCP
  797. if (ic_proto_enabled & IC_USE_DHCP) {
  798. __be32 server_id = NONE;
  799. int mt = 0;
  800. ext = &b->exten[4];
  801. while (ext < end && *ext != 0xff) {
  802. u8 *opt = ext++;
  803. if (*opt == 0) /* Padding */
  804. continue;
  805. ext += *ext + 1;
  806. if (ext >= end)
  807. break;
  808. switch (*opt) {
  809. case 53: /* Message type */
  810. if (opt[1])
  811. mt = opt[2];
  812. break;
  813. case 54: /* Server ID (IP address) */
  814. if (opt[1] >= 4)
  815. memcpy(&server_id, opt + 2, 4);
  816. break;
  817. }
  818. }
  819. #ifdef IPCONFIG_DEBUG
  820. printk("DHCP: Got message type %d\n", mt);
  821. #endif
  822. switch (mt) {
  823. case DHCPOFFER:
  824. /* While in the process of accepting one offer,
  825. * ignore all others.
  826. */
  827. if (ic_myaddr != NONE)
  828. goto drop_unlock;
  829. /* Let's accept that offer. */
  830. ic_myaddr = b->your_ip;
  831. ic_servaddr = server_id;
  832. #ifdef IPCONFIG_DEBUG
  833. printk("DHCP: Offered address %u.%u.%u.%u",
  834. NIPQUAD(ic_myaddr));
  835. printk(" by server %u.%u.%u.%u\n",
  836. NIPQUAD(ic_servaddr));
  837. #endif
  838. /* The DHCP indicated server address takes
  839. * precedence over the bootp header one if
  840. * they are different.
  841. */
  842. if ((server_id != NONE) &&
  843. (b->server_ip != server_id))
  844. b->server_ip = ic_servaddr;
  845. break;
  846. case DHCPACK:
  847. if (memcmp(dev->dev_addr, b->hw_addr, dev->addr_len) != 0)
  848. goto drop_unlock;
  849. /* Yeah! */
  850. break;
  851. default:
  852. /* Urque. Forget it*/
  853. ic_myaddr = NONE;
  854. ic_servaddr = NONE;
  855. goto drop_unlock;
  856. }
  857. ic_dhcp_msgtype = mt;
  858. }
  859. #endif /* IPCONFIG_DHCP */
  860. ext = &b->exten[4];
  861. while (ext < end && *ext != 0xff) {
  862. u8 *opt = ext++;
  863. if (*opt == 0) /* Padding */
  864. continue;
  865. ext += *ext + 1;
  866. if (ext < end)
  867. ic_do_bootp_ext(opt);
  868. }
  869. }
  870. /* We have a winner! */
  871. ic_dev = dev;
  872. ic_myaddr = b->your_ip;
  873. ic_servaddr = b->server_ip;
  874. if (ic_gateway == NONE && b->relay_ip)
  875. ic_gateway = b->relay_ip;
  876. if (ic_nameservers[0] == NONE)
  877. ic_nameservers[0] = ic_servaddr;
  878. ic_got_reply = IC_BOOTP;
  879. drop_unlock:
  880. /* Show's over. Nothing to see here. */
  881. spin_unlock(&ic_recv_lock);
  882. drop:
  883. /* Throw the packet out. */
  884. kfree_skb(skb);
  885. return 0;
  886. }
  887. #endif
  888. /*
  889. * Dynamic IP configuration -- DHCP, BOOTP, RARP.
  890. */
  891. #ifdef IPCONFIG_DYNAMIC
  892. static int __init ic_dynamic(void)
  893. {
  894. int retries;
  895. struct ic_device *d;
  896. unsigned long start_jiffies, timeout, jiff;
  897. int do_bootp = ic_proto_have_if & IC_BOOTP;
  898. int do_rarp = ic_proto_have_if & IC_RARP;
  899. /*
  900. * If none of DHCP/BOOTP/RARP was selected, return with an error.
  901. * This routine gets only called when some pieces of information
  902. * are missing, and without DHCP/BOOTP/RARP we are unable to get it.
  903. */
  904. if (!ic_proto_enabled) {
  905. printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
  906. return -1;
  907. }
  908. #ifdef IPCONFIG_BOOTP
  909. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_BOOTP)
  910. printk(KERN_ERR "DHCP/BOOTP: No suitable device found.\n");
  911. #endif
  912. #ifdef IPCONFIG_RARP
  913. if ((ic_proto_enabled ^ ic_proto_have_if) & IC_RARP)
  914. printk(KERN_ERR "RARP: No suitable device found.\n");
  915. #endif
  916. if (!ic_proto_have_if)
  917. /* Error message already printed */
  918. return -1;
  919. /*
  920. * Setup protocols
  921. */
  922. #ifdef IPCONFIG_BOOTP
  923. if (do_bootp)
  924. ic_bootp_init();
  925. #endif
  926. #ifdef IPCONFIG_RARP
  927. if (do_rarp)
  928. ic_rarp_init();
  929. #endif
  930. /*
  931. * Send requests and wait, until we get an answer. This loop
  932. * seems to be a terrible waste of CPU time, but actually there is
  933. * only one process running at all, so we don't need to use any
  934. * scheduler functions.
  935. * [Actually we could now, but the nothing else running note still
  936. * applies.. - AC]
  937. */
  938. printk(KERN_NOTICE "Sending %s%s%s requests .",
  939. do_bootp
  940. ? ((ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP") : "",
  941. (do_bootp && do_rarp) ? " and " : "",
  942. do_rarp ? "RARP" : "");
  943. start_jiffies = jiffies;
  944. d = ic_first_dev;
  945. retries = CONF_SEND_RETRIES;
  946. get_random_bytes(&timeout, sizeof(timeout));
  947. timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
  948. for (;;) {
  949. #ifdef IPCONFIG_BOOTP
  950. if (do_bootp && (d->able & IC_BOOTP))
  951. ic_bootp_send_if(d, jiffies - start_jiffies);
  952. #endif
  953. #ifdef IPCONFIG_RARP
  954. if (do_rarp && (d->able & IC_RARP))
  955. ic_rarp_send_if(d);
  956. #endif
  957. jiff = jiffies + (d->next ? CONF_INTER_TIMEOUT : timeout);
  958. while (time_before(jiffies, jiff) && !ic_got_reply)
  959. schedule_timeout_uninterruptible(1);
  960. #ifdef IPCONFIG_DHCP
  961. /* DHCP isn't done until we get a DHCPACK. */
  962. if ((ic_got_reply & IC_BOOTP)
  963. && (ic_proto_enabled & IC_USE_DHCP)
  964. && ic_dhcp_msgtype != DHCPACK)
  965. {
  966. ic_got_reply = 0;
  967. printk(",");
  968. continue;
  969. }
  970. #endif /* IPCONFIG_DHCP */
  971. if (ic_got_reply) {
  972. printk(" OK\n");
  973. break;
  974. }
  975. if ((d = d->next))
  976. continue;
  977. if (! --retries) {
  978. printk(" timed out!\n");
  979. break;
  980. }
  981. d = ic_first_dev;
  982. timeout = timeout CONF_TIMEOUT_MULT;
  983. if (timeout > CONF_TIMEOUT_MAX)
  984. timeout = CONF_TIMEOUT_MAX;
  985. printk(".");
  986. }
  987. #ifdef IPCONFIG_BOOTP
  988. if (do_bootp)
  989. ic_bootp_cleanup();
  990. #endif
  991. #ifdef IPCONFIG_RARP
  992. if (do_rarp)
  993. ic_rarp_cleanup();
  994. #endif
  995. if (!ic_got_reply) {
  996. ic_myaddr = NONE;
  997. return -1;
  998. }
  999. printk("IP-Config: Got %s answer from %u.%u.%u.%u, ",
  1000. ((ic_got_reply & IC_RARP) ? "RARP"
  1001. : (ic_proto_enabled & IC_USE_DHCP) ? "DHCP" : "BOOTP"),
  1002. NIPQUAD(ic_servaddr));
  1003. printk("my address is %u.%u.%u.%u\n", NIPQUAD(ic_myaddr));
  1004. return 0;
  1005. }
  1006. #endif /* IPCONFIG_DYNAMIC */
  1007. #ifdef CONFIG_PROC_FS
  1008. static int pnp_seq_show(struct seq_file *seq, void *v)
  1009. {
  1010. int i;
  1011. if (ic_proto_used & IC_PROTO)
  1012. seq_printf(seq, "#PROTO: %s\n",
  1013. (ic_proto_used & IC_RARP) ? "RARP"
  1014. : (ic_proto_used & IC_USE_DHCP) ? "DHCP" : "BOOTP");
  1015. else
  1016. seq_puts(seq, "#MANUAL\n");
  1017. if (ic_domain[0])
  1018. seq_printf(seq,
  1019. "domain %s\n", ic_domain);
  1020. for (i = 0; i < CONF_NAMESERVERS_MAX; i++) {
  1021. if (ic_nameservers[i] != NONE)
  1022. seq_printf(seq,
  1023. "nameserver %u.%u.%u.%u\n",
  1024. NIPQUAD(ic_nameservers[i]));
  1025. }
  1026. if (ic_servaddr != NONE)
  1027. seq_printf(seq,
  1028. "bootserver %u.%u.%u.%u\n",
  1029. NIPQUAD(ic_servaddr));
  1030. return 0;
  1031. }
  1032. static int pnp_seq_open(struct inode *indoe, struct file *file)
  1033. {
  1034. return single_open(file, pnp_seq_show, NULL);
  1035. }
  1036. static const struct file_operations pnp_seq_fops = {
  1037. .owner = THIS_MODULE,
  1038. .open = pnp_seq_open,
  1039. .read = seq_read,
  1040. .llseek = seq_lseek,
  1041. .release = single_release,
  1042. };
  1043. #endif /* CONFIG_PROC_FS */
  1044. /*
  1045. * Extract IP address from the parameter string if needed. Note that we
  1046. * need to have root_server_addr set _before_ IPConfig gets called as it
  1047. * can override it.
  1048. */
  1049. __be32 __init root_nfs_parse_addr(char *name)
  1050. {
  1051. __be32 addr;
  1052. int octets = 0;
  1053. char *cp, *cq;
  1054. cp = cq = name;
  1055. while (octets < 4) {
  1056. while (*cp >= '0' && *cp <= '9')
  1057. cp++;
  1058. if (cp == cq || cp - cq > 3)
  1059. break;
  1060. if (*cp == '.' || octets == 3)
  1061. octets++;
  1062. if (octets < 4)
  1063. cp++;
  1064. cq = cp;
  1065. }
  1066. if (octets == 4 && (*cp == ':' || *cp == '\0')) {
  1067. if (*cp == ':')
  1068. *cp++ = '\0';
  1069. addr = in_aton(name);
  1070. memmove(name, cp, strlen(cp) + 1);
  1071. } else
  1072. addr = NONE;
  1073. return addr;
  1074. }
  1075. /*
  1076. * IP Autoconfig dispatcher.
  1077. */
  1078. static int __init ip_auto_config(void)
  1079. {
  1080. __be32 addr;
  1081. #ifdef CONFIG_PROC_FS
  1082. proc_net_fops_create(&init_net, "pnp", S_IRUGO, &pnp_seq_fops);
  1083. #endif /* CONFIG_PROC_FS */
  1084. if (!ic_enable)
  1085. return 0;
  1086. DBG(("IP-Config: Entered.\n"));
  1087. #ifdef IPCONFIG_DYNAMIC
  1088. try_try_again:
  1089. #endif
  1090. /* Give hardware a chance to settle */
  1091. msleep(CONF_PRE_OPEN);
  1092. /* Setup all network devices */
  1093. if (ic_open_devs() < 0)
  1094. return -1;
  1095. /* Give drivers a chance to settle */
  1096. ssleep(CONF_POST_OPEN);
  1097. /*
  1098. * If the config information is insufficient (e.g., our IP address or
  1099. * IP address of the boot server is missing or we have multiple network
  1100. * interfaces and no default was set), use BOOTP or RARP to get the
  1101. * missing values.
  1102. */
  1103. if (ic_myaddr == NONE ||
  1104. #ifdef CONFIG_ROOT_NFS
  1105. (root_server_addr == NONE
  1106. && ic_servaddr == NONE
  1107. && ROOT_DEV == Root_NFS) ||
  1108. #endif
  1109. ic_first_dev->next) {
  1110. #ifdef IPCONFIG_DYNAMIC
  1111. int retries = CONF_OPEN_RETRIES;
  1112. if (ic_dynamic() < 0) {
  1113. ic_close_devs();
  1114. /*
  1115. * I don't know why, but sometimes the
  1116. * eepro100 driver (at least) gets upset and
  1117. * doesn't work the first time it's opened.
  1118. * But then if you close it and reopen it, it
  1119. * works just fine. So we need to try that at
  1120. * least once before giving up.
  1121. *
  1122. * Also, if the root will be NFS-mounted, we
  1123. * have nowhere to go if DHCP fails. So we
  1124. * just have to keep trying forever.
  1125. *
  1126. * -- Chip
  1127. */
  1128. #ifdef CONFIG_ROOT_NFS
  1129. if (ROOT_DEV == Root_NFS) {
  1130. printk(KERN_ERR
  1131. "IP-Config: Retrying forever (NFS root)...\n");
  1132. goto try_try_again;
  1133. }
  1134. #endif
  1135. if (--retries) {
  1136. printk(KERN_ERR
  1137. "IP-Config: Reopening network devices...\n");
  1138. goto try_try_again;
  1139. }
  1140. /* Oh, well. At least we tried. */
  1141. printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
  1142. return -1;
  1143. }
  1144. #else /* !DYNAMIC */
  1145. printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
  1146. ic_close_devs();
  1147. return -1;
  1148. #endif /* IPCONFIG_DYNAMIC */
  1149. } else {
  1150. /* Device selected manually or only one device -> use it */
  1151. ic_dev = ic_first_dev->dev;
  1152. }
  1153. addr = root_nfs_parse_addr(root_server_path);
  1154. if (root_server_addr == NONE)
  1155. root_server_addr = addr;
  1156. /*
  1157. * Use defaults whereever applicable.
  1158. */
  1159. if (ic_defaults() < 0)
  1160. return -1;
  1161. /*
  1162. * Close all network devices except the device we've
  1163. * autoconfigured and set up routes.
  1164. */
  1165. ic_close_devs();
  1166. if (ic_setup_if() < 0 || ic_setup_routes() < 0)
  1167. return -1;
  1168. /*
  1169. * Record which protocol was actually used.
  1170. */
  1171. #ifdef IPCONFIG_DYNAMIC
  1172. ic_proto_used = ic_got_reply | (ic_proto_enabled & IC_USE_DHCP);
  1173. #endif
  1174. #ifndef IPCONFIG_SILENT
  1175. /*
  1176. * Clue in the operator.
  1177. */
  1178. printk("IP-Config: Complete:");
  1179. printk("\n device=%s", ic_dev->name);
  1180. printk(", addr=%u.%u.%u.%u", NIPQUAD(ic_myaddr));
  1181. printk(", mask=%u.%u.%u.%u", NIPQUAD(ic_netmask));
  1182. printk(", gw=%u.%u.%u.%u", NIPQUAD(ic_gateway));
  1183. printk(",\n host=%s, domain=%s, nis-domain=%s",
  1184. utsname()->nodename, ic_domain, utsname()->domainname);
  1185. printk(",\n bootserver=%u.%u.%u.%u", NIPQUAD(ic_servaddr));
  1186. printk(", rootserver=%u.%u.%u.%u", NIPQUAD(root_server_addr));
  1187. printk(", rootpath=%s", root_server_path);
  1188. printk("\n");
  1189. #endif /* !SILENT */
  1190. return 0;
  1191. }
  1192. late_initcall(ip_auto_config);
  1193. /*
  1194. * Decode any IP configuration options in the "ip=" or "nfsaddrs=" kernel
  1195. * command line parameter. See Documentation/nfsroot.txt.
  1196. */
  1197. static int __init ic_proto_name(char *name)
  1198. {
  1199. if (!strcmp(name, "on") || !strcmp(name, "any")) {
  1200. return 1;
  1201. }
  1202. if (!strcmp(name, "off") || !strcmp(name, "none")) {
  1203. return 0;
  1204. }
  1205. #ifdef CONFIG_IP_PNP_DHCP
  1206. else if (!strcmp(name, "dhcp")) {
  1207. ic_proto_enabled &= ~IC_RARP;
  1208. return 1;
  1209. }
  1210. #endif
  1211. #ifdef CONFIG_IP_PNP_BOOTP
  1212. else if (!strcmp(name, "bootp")) {
  1213. ic_proto_enabled &= ~(IC_RARP | IC_USE_DHCP);
  1214. return 1;
  1215. }
  1216. #endif
  1217. #ifdef CONFIG_IP_PNP_RARP
  1218. else if (!strcmp(name, "rarp")) {
  1219. ic_proto_enabled &= ~(IC_BOOTP | IC_USE_DHCP);
  1220. return 1;
  1221. }
  1222. #endif
  1223. #ifdef IPCONFIG_DYNAMIC
  1224. else if (!strcmp(name, "both")) {
  1225. ic_proto_enabled &= ~IC_USE_DHCP; /* backward compat :-( */
  1226. return 1;
  1227. }
  1228. #endif
  1229. return 0;
  1230. }
  1231. static int __init ip_auto_config_setup(char *addrs)
  1232. {
  1233. char *cp, *ip, *dp;
  1234. int num = 0;
  1235. ic_set_manually = 1;
  1236. ic_enable = 1;
  1237. /*
  1238. * If any dhcp, bootp etc options are set, leave autoconfig on
  1239. * and skip the below static IP processing.
  1240. */
  1241. if (ic_proto_name(addrs))
  1242. return 1;
  1243. /* If no static IP is given, turn off autoconfig and bail. */
  1244. if (*addrs == 0 ||
  1245. strcmp(addrs, "off") == 0 ||
  1246. strcmp(addrs, "none") == 0) {
  1247. ic_enable = 0;
  1248. return 1;
  1249. }
  1250. /* Parse string for static IP assignment. */
  1251. ip = addrs;
  1252. while (ip && *ip) {
  1253. if ((cp = strchr(ip, ':')))
  1254. *cp++ = '\0';
  1255. if (strlen(ip) > 0) {
  1256. DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
  1257. switch (num) {
  1258. case 0:
  1259. if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
  1260. ic_myaddr = NONE;
  1261. break;
  1262. case 1:
  1263. if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
  1264. ic_servaddr = NONE;
  1265. break;
  1266. case 2:
  1267. if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
  1268. ic_gateway = NONE;
  1269. break;
  1270. case 3:
  1271. if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
  1272. ic_netmask = NONE;
  1273. break;
  1274. case 4:
  1275. if ((dp = strchr(ip, '.'))) {
  1276. *dp++ = '\0';
  1277. strlcpy(utsname()->domainname, dp,
  1278. sizeof(utsname()->domainname));
  1279. }
  1280. strlcpy(utsname()->nodename, ip,
  1281. sizeof(utsname()->nodename));
  1282. ic_host_name_set = 1;
  1283. break;
  1284. case 5:
  1285. strlcpy(user_dev_name, ip, sizeof(user_dev_name));
  1286. break;
  1287. case 6:
  1288. if (ic_proto_name(ip) == 0 &&
  1289. ic_myaddr == NONE) {
  1290. ic_enable = 0;
  1291. }
  1292. break;
  1293. }
  1294. }
  1295. ip = cp;
  1296. num++;
  1297. }
  1298. return 1;
  1299. }
  1300. static int __init nfsaddrs_config_setup(char *addrs)
  1301. {
  1302. return ip_auto_config_setup(addrs);
  1303. }
  1304. static int __init vendor_class_identifier_setup(char *addrs)
  1305. {
  1306. if (strlcpy(vendor_class_identifier, addrs,
  1307. sizeof(vendor_class_identifier))
  1308. >= sizeof(vendor_class_identifier))
  1309. printk(KERN_WARNING "DHCP: vendorclass too long, truncated to \"%s\"",
  1310. vendor_class_identifier);
  1311. return 1;
  1312. }
  1313. __setup("ip=", ip_auto_config_setup);
  1314. __setup("nfsaddrs=", nfsaddrs_config_setup);
  1315. __setup("dhcpclass=", vendor_class_identifier_setup);