ipconfig.c 38 KB

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