ipconfig.c 40 KB

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