ipconfig.c 38 KB

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