ipconfig.c 40 KB

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