ipconfig.c 40 KB

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