ndisc.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * Neighbour Discovery for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Mike Shaver <shaver@ingenia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. /*
  15. * Changes:
  16. *
  17. * Pierre Ynard : export userland ND options
  18. * through netlink (RDNSS support)
  19. * Lars Fenneberg : fixed MTU setting on receipt
  20. * of an RA.
  21. * Janos Farkas : kmalloc failure checks
  22. * Alexey Kuznetsov : state machine reworked
  23. * and moved to net/core.
  24. * Pekka Savola : RFC2461 validation
  25. * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
  26. */
  27. /* Set to 3 to get tracing... */
  28. #define ND_DEBUG 1
  29. #define ND_PRINTK(fmt, args...) do { if (net_ratelimit()) { printk(fmt, ## args); } } while(0)
  30. #define ND_NOPRINTK(x...) do { ; } while(0)
  31. #define ND_PRINTK0 ND_PRINTK
  32. #define ND_PRINTK1 ND_NOPRINTK
  33. #define ND_PRINTK2 ND_NOPRINTK
  34. #define ND_PRINTK3 ND_NOPRINTK
  35. #if ND_DEBUG >= 1
  36. #undef ND_PRINTK1
  37. #define ND_PRINTK1 ND_PRINTK
  38. #endif
  39. #if ND_DEBUG >= 2
  40. #undef ND_PRINTK2
  41. #define ND_PRINTK2 ND_PRINTK
  42. #endif
  43. #if ND_DEBUG >= 3
  44. #undef ND_PRINTK3
  45. #define ND_PRINTK3 ND_PRINTK
  46. #endif
  47. #include <linux/module.h>
  48. #include <linux/errno.h>
  49. #include <linux/types.h>
  50. #include <linux/socket.h>
  51. #include <linux/sockios.h>
  52. #include <linux/sched.h>
  53. #include <linux/net.h>
  54. #include <linux/in6.h>
  55. #include <linux/route.h>
  56. #include <linux/init.h>
  57. #include <linux/rcupdate.h>
  58. #ifdef CONFIG_SYSCTL
  59. #include <linux/sysctl.h>
  60. #endif
  61. #include <linux/if_addr.h>
  62. #include <linux/if_arp.h>
  63. #include <linux/ipv6.h>
  64. #include <linux/icmpv6.h>
  65. #include <linux/jhash.h>
  66. #include <net/sock.h>
  67. #include <net/snmp.h>
  68. #include <net/ipv6.h>
  69. #include <net/protocol.h>
  70. #include <net/ndisc.h>
  71. #include <net/ip6_route.h>
  72. #include <net/addrconf.h>
  73. #include <net/icmp.h>
  74. #include <net/netlink.h>
  75. #include <linux/rtnetlink.h>
  76. #include <net/flow.h>
  77. #include <net/ip6_checksum.h>
  78. #include <net/inet_common.h>
  79. #include <linux/proc_fs.h>
  80. #include <linux/netfilter.h>
  81. #include <linux/netfilter_ipv6.h>
  82. static u32 ndisc_hash(const void *pkey, const struct net_device *dev);
  83. static int ndisc_constructor(struct neighbour *neigh);
  84. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
  85. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
  86. static int pndisc_constructor(struct pneigh_entry *n);
  87. static void pndisc_destructor(struct pneigh_entry *n);
  88. static void pndisc_redo(struct sk_buff *skb);
  89. static struct neigh_ops ndisc_generic_ops = {
  90. .family = AF_INET6,
  91. .solicit = ndisc_solicit,
  92. .error_report = ndisc_error_report,
  93. .output = neigh_resolve_output,
  94. .connected_output = neigh_connected_output,
  95. .hh_output = dev_queue_xmit,
  96. .queue_xmit = dev_queue_xmit,
  97. };
  98. static struct neigh_ops ndisc_hh_ops = {
  99. .family = AF_INET6,
  100. .solicit = ndisc_solicit,
  101. .error_report = ndisc_error_report,
  102. .output = neigh_resolve_output,
  103. .connected_output = neigh_resolve_output,
  104. .hh_output = dev_queue_xmit,
  105. .queue_xmit = dev_queue_xmit,
  106. };
  107. static struct neigh_ops ndisc_direct_ops = {
  108. .family = AF_INET6,
  109. .output = dev_queue_xmit,
  110. .connected_output = dev_queue_xmit,
  111. .hh_output = dev_queue_xmit,
  112. .queue_xmit = dev_queue_xmit,
  113. };
  114. struct neigh_table nd_tbl = {
  115. .family = AF_INET6,
  116. .entry_size = sizeof(struct neighbour) + sizeof(struct in6_addr),
  117. .key_len = sizeof(struct in6_addr),
  118. .hash = ndisc_hash,
  119. .constructor = ndisc_constructor,
  120. .pconstructor = pndisc_constructor,
  121. .pdestructor = pndisc_destructor,
  122. .proxy_redo = pndisc_redo,
  123. .id = "ndisc_cache",
  124. .parms = {
  125. .tbl = &nd_tbl,
  126. .base_reachable_time = 30 * HZ,
  127. .retrans_time = 1 * HZ,
  128. .gc_staletime = 60 * HZ,
  129. .reachable_time = 30 * HZ,
  130. .delay_probe_time = 5 * HZ,
  131. .queue_len = 3,
  132. .ucast_probes = 3,
  133. .mcast_probes = 3,
  134. .anycast_delay = 1 * HZ,
  135. .proxy_delay = (8 * HZ) / 10,
  136. .proxy_qlen = 64,
  137. },
  138. .gc_interval = 30 * HZ,
  139. .gc_thresh1 = 128,
  140. .gc_thresh2 = 512,
  141. .gc_thresh3 = 1024,
  142. };
  143. /* ND options */
  144. struct ndisc_options {
  145. struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
  146. #ifdef CONFIG_IPV6_ROUTE_INFO
  147. struct nd_opt_hdr *nd_opts_ri;
  148. struct nd_opt_hdr *nd_opts_ri_end;
  149. #endif
  150. struct nd_opt_hdr *nd_useropts;
  151. struct nd_opt_hdr *nd_useropts_end;
  152. };
  153. #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
  154. #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
  155. #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
  156. #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
  157. #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
  158. #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
  159. #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
  160. /*
  161. * Return the padding between the option length and the start of the
  162. * link addr. Currently only IP-over-InfiniBand needs this, although
  163. * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
  164. * also need a pad of 2.
  165. */
  166. static int ndisc_addr_option_pad(unsigned short type)
  167. {
  168. switch (type) {
  169. case ARPHRD_INFINIBAND: return 2;
  170. default: return 0;
  171. }
  172. }
  173. static inline int ndisc_opt_addr_space(struct net_device *dev)
  174. {
  175. return NDISC_OPT_SPACE(dev->addr_len + ndisc_addr_option_pad(dev->type));
  176. }
  177. static u8 *ndisc_fill_addr_option(u8 *opt, int type, void *data, int data_len,
  178. unsigned short addr_type)
  179. {
  180. int space = NDISC_OPT_SPACE(data_len);
  181. int pad = ndisc_addr_option_pad(addr_type);
  182. opt[0] = type;
  183. opt[1] = space>>3;
  184. memset(opt + 2, 0, pad);
  185. opt += pad;
  186. space -= pad;
  187. memcpy(opt+2, data, data_len);
  188. data_len += 2;
  189. opt += data_len;
  190. if ((space -= data_len) > 0)
  191. memset(opt, 0, space);
  192. return opt + space;
  193. }
  194. static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
  195. struct nd_opt_hdr *end)
  196. {
  197. int type;
  198. if (!cur || !end || cur >= end)
  199. return NULL;
  200. type = cur->nd_opt_type;
  201. do {
  202. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  203. } while(cur < end && cur->nd_opt_type != type);
  204. return (cur <= end && cur->nd_opt_type == type ? cur : NULL);
  205. }
  206. static inline int ndisc_is_useropt(struct nd_opt_hdr *opt)
  207. {
  208. return (opt->nd_opt_type == ND_OPT_RDNSS);
  209. }
  210. static struct nd_opt_hdr *ndisc_next_useropt(struct nd_opt_hdr *cur,
  211. struct nd_opt_hdr *end)
  212. {
  213. if (!cur || !end || cur >= end)
  214. return NULL;
  215. do {
  216. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  217. } while(cur < end && !ndisc_is_useropt(cur));
  218. return (cur <= end && ndisc_is_useropt(cur) ? cur : NULL);
  219. }
  220. static struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len,
  221. struct ndisc_options *ndopts)
  222. {
  223. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
  224. if (!nd_opt || opt_len < 0 || !ndopts)
  225. return NULL;
  226. memset(ndopts, 0, sizeof(*ndopts));
  227. while (opt_len) {
  228. int l;
  229. if (opt_len < sizeof(struct nd_opt_hdr))
  230. return NULL;
  231. l = nd_opt->nd_opt_len << 3;
  232. if (opt_len < l || l == 0)
  233. return NULL;
  234. switch (nd_opt->nd_opt_type) {
  235. case ND_OPT_SOURCE_LL_ADDR:
  236. case ND_OPT_TARGET_LL_ADDR:
  237. case ND_OPT_MTU:
  238. case ND_OPT_REDIRECT_HDR:
  239. if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
  240. ND_PRINTK2(KERN_WARNING
  241. "%s(): duplicated ND6 option found: type=%d\n",
  242. __func__,
  243. nd_opt->nd_opt_type);
  244. } else {
  245. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  246. }
  247. break;
  248. case ND_OPT_PREFIX_INFO:
  249. ndopts->nd_opts_pi_end = nd_opt;
  250. if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
  251. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  252. break;
  253. #ifdef CONFIG_IPV6_ROUTE_INFO
  254. case ND_OPT_ROUTE_INFO:
  255. ndopts->nd_opts_ri_end = nd_opt;
  256. if (!ndopts->nd_opts_ri)
  257. ndopts->nd_opts_ri = nd_opt;
  258. break;
  259. #endif
  260. default:
  261. if (ndisc_is_useropt(nd_opt)) {
  262. ndopts->nd_useropts_end = nd_opt;
  263. if (!ndopts->nd_useropts)
  264. ndopts->nd_useropts = nd_opt;
  265. } else {
  266. /*
  267. * Unknown options must be silently ignored,
  268. * to accommodate future extension to the
  269. * protocol.
  270. */
  271. ND_PRINTK2(KERN_NOTICE
  272. "%s(): ignored unsupported option; type=%d, len=%d\n",
  273. __func__,
  274. nd_opt->nd_opt_type, nd_opt->nd_opt_len);
  275. }
  276. }
  277. opt_len -= l;
  278. nd_opt = ((void *)nd_opt) + l;
  279. }
  280. return ndopts;
  281. }
  282. static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
  283. struct net_device *dev)
  284. {
  285. u8 *lladdr = (u8 *)(p + 1);
  286. int lladdrlen = p->nd_opt_len << 3;
  287. int prepad = ndisc_addr_option_pad(dev->type);
  288. if (lladdrlen != NDISC_OPT_SPACE(dev->addr_len + prepad))
  289. return NULL;
  290. return (lladdr + prepad);
  291. }
  292. int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
  293. {
  294. switch (dev->type) {
  295. case ARPHRD_ETHER:
  296. case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
  297. case ARPHRD_FDDI:
  298. ipv6_eth_mc_map(addr, buf);
  299. return 0;
  300. case ARPHRD_IEEE802_TR:
  301. ipv6_tr_mc_map(addr,buf);
  302. return 0;
  303. case ARPHRD_ARCNET:
  304. ipv6_arcnet_mc_map(addr, buf);
  305. return 0;
  306. case ARPHRD_INFINIBAND:
  307. ipv6_ib_mc_map(addr, dev->broadcast, buf);
  308. return 0;
  309. default:
  310. if (dir) {
  311. memcpy(buf, dev->broadcast, dev->addr_len);
  312. return 0;
  313. }
  314. }
  315. return -EINVAL;
  316. }
  317. EXPORT_SYMBOL(ndisc_mc_map);
  318. static u32 ndisc_hash(const void *pkey, const struct net_device *dev)
  319. {
  320. const u32 *p32 = pkey;
  321. u32 addr_hash, i;
  322. addr_hash = 0;
  323. for (i = 0; i < (sizeof(struct in6_addr) / sizeof(u32)); i++)
  324. addr_hash ^= *p32++;
  325. return jhash_2words(addr_hash, dev->ifindex, nd_tbl.hash_rnd);
  326. }
  327. static int ndisc_constructor(struct neighbour *neigh)
  328. {
  329. struct in6_addr *addr = (struct in6_addr*)&neigh->primary_key;
  330. struct net_device *dev = neigh->dev;
  331. struct inet6_dev *in6_dev;
  332. struct neigh_parms *parms;
  333. int is_multicast = ipv6_addr_is_multicast(addr);
  334. rcu_read_lock();
  335. in6_dev = in6_dev_get(dev);
  336. if (in6_dev == NULL) {
  337. rcu_read_unlock();
  338. return -EINVAL;
  339. }
  340. parms = in6_dev->nd_parms;
  341. __neigh_parms_put(neigh->parms);
  342. neigh->parms = neigh_parms_clone(parms);
  343. rcu_read_unlock();
  344. neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
  345. if (!dev->header_ops) {
  346. neigh->nud_state = NUD_NOARP;
  347. neigh->ops = &ndisc_direct_ops;
  348. neigh->output = neigh->ops->queue_xmit;
  349. } else {
  350. if (is_multicast) {
  351. neigh->nud_state = NUD_NOARP;
  352. ndisc_mc_map(addr, neigh->ha, dev, 1);
  353. } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
  354. neigh->nud_state = NUD_NOARP;
  355. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  356. if (dev->flags&IFF_LOOPBACK)
  357. neigh->type = RTN_LOCAL;
  358. } else if (dev->flags&IFF_POINTOPOINT) {
  359. neigh->nud_state = NUD_NOARP;
  360. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  361. }
  362. if (dev->header_ops->cache)
  363. neigh->ops = &ndisc_hh_ops;
  364. else
  365. neigh->ops = &ndisc_generic_ops;
  366. if (neigh->nud_state&NUD_VALID)
  367. neigh->output = neigh->ops->connected_output;
  368. else
  369. neigh->output = neigh->ops->output;
  370. }
  371. in6_dev_put(in6_dev);
  372. return 0;
  373. }
  374. static int pndisc_constructor(struct pneigh_entry *n)
  375. {
  376. struct in6_addr *addr = (struct in6_addr*)&n->key;
  377. struct in6_addr maddr;
  378. struct net_device *dev = n->dev;
  379. if (dev == NULL || __in6_dev_get(dev) == NULL)
  380. return -EINVAL;
  381. addrconf_addr_solict_mult(addr, &maddr);
  382. ipv6_dev_mc_inc(dev, &maddr);
  383. return 0;
  384. }
  385. static void pndisc_destructor(struct pneigh_entry *n)
  386. {
  387. struct in6_addr *addr = (struct in6_addr*)&n->key;
  388. struct in6_addr maddr;
  389. struct net_device *dev = n->dev;
  390. if (dev == NULL || __in6_dev_get(dev) == NULL)
  391. return;
  392. addrconf_addr_solict_mult(addr, &maddr);
  393. ipv6_dev_mc_dec(dev, &maddr);
  394. }
  395. struct sk_buff *ndisc_build_skb(struct net_device *dev,
  396. const struct in6_addr *daddr,
  397. const struct in6_addr *saddr,
  398. struct icmp6hdr *icmp6h,
  399. const struct in6_addr *target,
  400. int llinfo)
  401. {
  402. struct net *net = dev_net(dev);
  403. struct sock *sk = net->ipv6.ndisc_sk;
  404. struct sk_buff *skb;
  405. struct icmp6hdr *hdr;
  406. int len;
  407. int err;
  408. u8 *opt;
  409. if (!dev->addr_len)
  410. llinfo = 0;
  411. len = sizeof(struct icmp6hdr) + (target ? sizeof(*target) : 0);
  412. if (llinfo)
  413. len += ndisc_opt_addr_space(dev);
  414. skb = sock_alloc_send_skb(sk,
  415. (MAX_HEADER + sizeof(struct ipv6hdr) +
  416. len + LL_ALLOCATED_SPACE(dev)),
  417. 1, &err);
  418. if (!skb) {
  419. ND_PRINTK0(KERN_ERR
  420. "ICMPv6 ND: %s() failed to allocate an skb, err=%d.\n",
  421. __func__, err);
  422. return NULL;
  423. }
  424. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  425. ip6_nd_hdr(sk, skb, dev, saddr, daddr, IPPROTO_ICMPV6, len);
  426. skb->transport_header = skb->tail;
  427. skb_put(skb, len);
  428. hdr = (struct icmp6hdr *)skb_transport_header(skb);
  429. memcpy(hdr, icmp6h, sizeof(*hdr));
  430. opt = skb_transport_header(skb) + sizeof(struct icmp6hdr);
  431. if (target) {
  432. ipv6_addr_copy((struct in6_addr *)opt, target);
  433. opt += sizeof(*target);
  434. }
  435. if (llinfo)
  436. ndisc_fill_addr_option(opt, llinfo, dev->dev_addr,
  437. dev->addr_len, dev->type);
  438. hdr->icmp6_cksum = csum_ipv6_magic(saddr, daddr, len,
  439. IPPROTO_ICMPV6,
  440. csum_partial(hdr,
  441. len, 0));
  442. return skb;
  443. }
  444. EXPORT_SYMBOL(ndisc_build_skb);
  445. void ndisc_send_skb(struct sk_buff *skb,
  446. struct net_device *dev,
  447. struct neighbour *neigh,
  448. const struct in6_addr *daddr,
  449. const struct in6_addr *saddr,
  450. struct icmp6hdr *icmp6h)
  451. {
  452. struct flowi fl;
  453. struct dst_entry *dst;
  454. struct net *net = dev_net(dev);
  455. struct sock *sk = net->ipv6.ndisc_sk;
  456. struct inet6_dev *idev;
  457. int err;
  458. u8 type;
  459. type = icmp6h->icmp6_type;
  460. icmpv6_flow_init(sk, &fl, type, saddr, daddr, dev->ifindex);
  461. dst = icmp6_dst_alloc(dev, neigh, daddr);
  462. if (!dst) {
  463. kfree_skb(skb);
  464. return;
  465. }
  466. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  467. if (err < 0) {
  468. kfree_skb(skb);
  469. return;
  470. }
  471. skb_dst_set(skb, dst);
  472. idev = in6_dev_get(dst->dev);
  473. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  474. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, dst->dev,
  475. dst_output);
  476. if (!err) {
  477. ICMP6MSGOUT_INC_STATS(net, idev, type);
  478. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  479. }
  480. if (likely(idev != NULL))
  481. in6_dev_put(idev);
  482. }
  483. EXPORT_SYMBOL(ndisc_send_skb);
  484. /*
  485. * Send a Neighbour Discover packet
  486. */
  487. static void __ndisc_send(struct net_device *dev,
  488. struct neighbour *neigh,
  489. const struct in6_addr *daddr,
  490. const struct in6_addr *saddr,
  491. struct icmp6hdr *icmp6h, const struct in6_addr *target,
  492. int llinfo)
  493. {
  494. struct sk_buff *skb;
  495. skb = ndisc_build_skb(dev, daddr, saddr, icmp6h, target, llinfo);
  496. if (!skb)
  497. return;
  498. ndisc_send_skb(skb, dev, neigh, daddr, saddr, icmp6h);
  499. }
  500. static void ndisc_send_na(struct net_device *dev, struct neighbour *neigh,
  501. const struct in6_addr *daddr,
  502. const struct in6_addr *solicited_addr,
  503. int router, int solicited, int override, int inc_opt)
  504. {
  505. struct in6_addr tmpaddr;
  506. struct inet6_ifaddr *ifp;
  507. const struct in6_addr *src_addr;
  508. struct icmp6hdr icmp6h = {
  509. .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
  510. };
  511. /* for anycast or proxy, solicited_addr != src_addr */
  512. ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
  513. if (ifp) {
  514. src_addr = solicited_addr;
  515. if (ifp->flags & IFA_F_OPTIMISTIC)
  516. override = 0;
  517. in6_ifa_put(ifp);
  518. } else {
  519. if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
  520. inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
  521. &tmpaddr))
  522. return;
  523. src_addr = &tmpaddr;
  524. }
  525. icmp6h.icmp6_router = router;
  526. icmp6h.icmp6_solicited = solicited;
  527. icmp6h.icmp6_override = override;
  528. __ndisc_send(dev, neigh, daddr, src_addr,
  529. &icmp6h, solicited_addr,
  530. inc_opt ? ND_OPT_TARGET_LL_ADDR : 0);
  531. }
  532. void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh,
  533. const struct in6_addr *solicit,
  534. const struct in6_addr *daddr, const struct in6_addr *saddr)
  535. {
  536. struct in6_addr addr_buf;
  537. struct icmp6hdr icmp6h = {
  538. .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
  539. };
  540. if (saddr == NULL) {
  541. if (ipv6_get_lladdr(dev, &addr_buf,
  542. (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
  543. return;
  544. saddr = &addr_buf;
  545. }
  546. __ndisc_send(dev, neigh, daddr, saddr,
  547. &icmp6h, solicit,
  548. !ipv6_addr_any(saddr) ? ND_OPT_SOURCE_LL_ADDR : 0);
  549. }
  550. void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
  551. const struct in6_addr *daddr)
  552. {
  553. struct icmp6hdr icmp6h = {
  554. .icmp6_type = NDISC_ROUTER_SOLICITATION,
  555. };
  556. int send_sllao = dev->addr_len;
  557. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  558. /*
  559. * According to section 2.2 of RFC 4429, we must not
  560. * send router solicitations with a sllao from
  561. * optimistic addresses, but we may send the solicitation
  562. * if we don't include the sllao. So here we check
  563. * if our address is optimistic, and if so, we
  564. * suppress the inclusion of the sllao.
  565. */
  566. if (send_sllao) {
  567. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
  568. dev, 1);
  569. if (ifp) {
  570. if (ifp->flags & IFA_F_OPTIMISTIC) {
  571. send_sllao = 0;
  572. }
  573. in6_ifa_put(ifp);
  574. } else {
  575. send_sllao = 0;
  576. }
  577. }
  578. #endif
  579. __ndisc_send(dev, NULL, daddr, saddr,
  580. &icmp6h, NULL,
  581. send_sllao ? ND_OPT_SOURCE_LL_ADDR : 0);
  582. }
  583. EXPORT_SYMBOL(ndisc_send_rs);
  584. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
  585. {
  586. /*
  587. * "The sender MUST return an ICMP
  588. * destination unreachable"
  589. */
  590. dst_link_failure(skb);
  591. kfree_skb(skb);
  592. }
  593. /* Called with locked neigh: either read or both */
  594. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
  595. {
  596. struct in6_addr *saddr = NULL;
  597. struct in6_addr mcaddr;
  598. struct net_device *dev = neigh->dev;
  599. struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
  600. int probes = atomic_read(&neigh->probes);
  601. if (skb && ipv6_chk_addr(dev_net(dev), &ipv6_hdr(skb)->saddr, dev, 1))
  602. saddr = &ipv6_hdr(skb)->saddr;
  603. if ((probes -= neigh->parms->ucast_probes) < 0) {
  604. if (!(neigh->nud_state & NUD_VALID)) {
  605. ND_PRINTK1(KERN_DEBUG "%s(): trying to ucast probe in NUD_INVALID: %pI6\n",
  606. __func__, target);
  607. }
  608. ndisc_send_ns(dev, neigh, target, target, saddr);
  609. } else if ((probes -= neigh->parms->app_probes) < 0) {
  610. #ifdef CONFIG_ARPD
  611. neigh_app_ns(neigh);
  612. #endif
  613. } else {
  614. addrconf_addr_solict_mult(target, &mcaddr);
  615. ndisc_send_ns(dev, NULL, target, &mcaddr, saddr);
  616. }
  617. }
  618. static int pndisc_is_router(const void *pkey,
  619. struct net_device *dev)
  620. {
  621. struct pneigh_entry *n;
  622. int ret = -1;
  623. read_lock_bh(&nd_tbl.lock);
  624. n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
  625. if (n)
  626. ret = !!(n->flags & NTF_ROUTER);
  627. read_unlock_bh(&nd_tbl.lock);
  628. return ret;
  629. }
  630. static void ndisc_recv_ns(struct sk_buff *skb)
  631. {
  632. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  633. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  634. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  635. u8 *lladdr = NULL;
  636. u32 ndoptlen = skb->tail - (skb->transport_header +
  637. offsetof(struct nd_msg, opt));
  638. struct ndisc_options ndopts;
  639. struct net_device *dev = skb->dev;
  640. struct inet6_ifaddr *ifp;
  641. struct inet6_dev *idev = NULL;
  642. struct neighbour *neigh;
  643. int dad = ipv6_addr_any(saddr);
  644. int inc;
  645. int is_router = -1;
  646. if (ipv6_addr_is_multicast(&msg->target)) {
  647. ND_PRINTK2(KERN_WARNING
  648. "ICMPv6 NS: multicast target address");
  649. return;
  650. }
  651. /*
  652. * RFC2461 7.1.1:
  653. * DAD has to be destined for solicited node multicast address.
  654. */
  655. if (dad &&
  656. !(daddr->s6_addr32[0] == htonl(0xff020000) &&
  657. daddr->s6_addr32[1] == htonl(0x00000000) &&
  658. daddr->s6_addr32[2] == htonl(0x00000001) &&
  659. daddr->s6_addr [12] == 0xff )) {
  660. ND_PRINTK2(KERN_WARNING
  661. "ICMPv6 NS: bad DAD packet (wrong destination)\n");
  662. return;
  663. }
  664. if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
  665. ND_PRINTK2(KERN_WARNING
  666. "ICMPv6 NS: invalid ND options\n");
  667. return;
  668. }
  669. if (ndopts.nd_opts_src_lladdr) {
  670. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
  671. if (!lladdr) {
  672. ND_PRINTK2(KERN_WARNING
  673. "ICMPv6 NS: invalid link-layer address length\n");
  674. return;
  675. }
  676. /* RFC2461 7.1.1:
  677. * If the IP source address is the unspecified address,
  678. * there MUST NOT be source link-layer address option
  679. * in the message.
  680. */
  681. if (dad) {
  682. ND_PRINTK2(KERN_WARNING
  683. "ICMPv6 NS: bad DAD packet (link-layer address option)\n");
  684. return;
  685. }
  686. }
  687. inc = ipv6_addr_is_multicast(daddr);
  688. ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
  689. if (ifp) {
  690. if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
  691. if (dad) {
  692. if (dev->type == ARPHRD_IEEE802_TR) {
  693. const unsigned char *sadr;
  694. sadr = skb_mac_header(skb);
  695. if (((sadr[8] ^ dev->dev_addr[0]) & 0x7f) == 0 &&
  696. sadr[9] == dev->dev_addr[1] &&
  697. sadr[10] == dev->dev_addr[2] &&
  698. sadr[11] == dev->dev_addr[3] &&
  699. sadr[12] == dev->dev_addr[4] &&
  700. sadr[13] == dev->dev_addr[5]) {
  701. /* looped-back to us */
  702. goto out;
  703. }
  704. }
  705. /*
  706. * We are colliding with another node
  707. * who is doing DAD
  708. * so fail our DAD process
  709. */
  710. addrconf_dad_failure(ifp);
  711. return;
  712. } else {
  713. /*
  714. * This is not a dad solicitation.
  715. * If we are an optimistic node,
  716. * we should respond.
  717. * Otherwise, we should ignore it.
  718. */
  719. if (!(ifp->flags & IFA_F_OPTIMISTIC))
  720. goto out;
  721. }
  722. }
  723. idev = ifp->idev;
  724. } else {
  725. struct net *net = dev_net(dev);
  726. idev = in6_dev_get(dev);
  727. if (!idev) {
  728. /* XXX: count this drop? */
  729. return;
  730. }
  731. if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
  732. (idev->cnf.forwarding &&
  733. (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
  734. (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
  735. if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
  736. skb->pkt_type != PACKET_HOST &&
  737. inc != 0 &&
  738. idev->nd_parms->proxy_delay != 0) {
  739. /*
  740. * for anycast or proxy,
  741. * sender should delay its response
  742. * by a random time between 0 and
  743. * MAX_ANYCAST_DELAY_TIME seconds.
  744. * (RFC2461) -- yoshfuji
  745. */
  746. struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
  747. if (n)
  748. pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
  749. goto out;
  750. }
  751. } else
  752. goto out;
  753. }
  754. if (is_router < 0)
  755. is_router = !!idev->cnf.forwarding;
  756. if (dad) {
  757. ndisc_send_na(dev, NULL, &in6addr_linklocal_allnodes, &msg->target,
  758. is_router, 0, (ifp != NULL), 1);
  759. goto out;
  760. }
  761. if (inc)
  762. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
  763. else
  764. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
  765. /*
  766. * update / create cache entry
  767. * for the source address
  768. */
  769. neigh = __neigh_lookup(&nd_tbl, saddr, dev,
  770. !inc || lladdr || !dev->addr_len);
  771. if (neigh)
  772. neigh_update(neigh, lladdr, NUD_STALE,
  773. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  774. NEIGH_UPDATE_F_OVERRIDE);
  775. if (neigh || !dev->header_ops) {
  776. ndisc_send_na(dev, neigh, saddr, &msg->target,
  777. is_router,
  778. 1, (ifp != NULL && inc), inc);
  779. if (neigh)
  780. neigh_release(neigh);
  781. }
  782. out:
  783. if (ifp)
  784. in6_ifa_put(ifp);
  785. else
  786. in6_dev_put(idev);
  787. return;
  788. }
  789. static void ndisc_recv_na(struct sk_buff *skb)
  790. {
  791. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  792. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  793. struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  794. u8 *lladdr = NULL;
  795. u32 ndoptlen = skb->tail - (skb->transport_header +
  796. offsetof(struct nd_msg, opt));
  797. struct ndisc_options ndopts;
  798. struct net_device *dev = skb->dev;
  799. struct inet6_ifaddr *ifp;
  800. struct neighbour *neigh;
  801. if (skb->len < sizeof(struct nd_msg)) {
  802. ND_PRINTK2(KERN_WARNING
  803. "ICMPv6 NA: packet too short\n");
  804. return;
  805. }
  806. if (ipv6_addr_is_multicast(&msg->target)) {
  807. ND_PRINTK2(KERN_WARNING
  808. "ICMPv6 NA: target address is multicast.\n");
  809. return;
  810. }
  811. if (ipv6_addr_is_multicast(daddr) &&
  812. msg->icmph.icmp6_solicited) {
  813. ND_PRINTK2(KERN_WARNING
  814. "ICMPv6 NA: solicited NA is multicasted.\n");
  815. return;
  816. }
  817. if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
  818. ND_PRINTK2(KERN_WARNING
  819. "ICMPv6 NS: invalid ND option\n");
  820. return;
  821. }
  822. if (ndopts.nd_opts_tgt_lladdr) {
  823. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
  824. if (!lladdr) {
  825. ND_PRINTK2(KERN_WARNING
  826. "ICMPv6 NA: invalid link-layer address length\n");
  827. return;
  828. }
  829. }
  830. ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
  831. if (ifp) {
  832. if (ifp->flags & IFA_F_TENTATIVE) {
  833. addrconf_dad_failure(ifp);
  834. return;
  835. }
  836. /* What should we make now? The advertisement
  837. is invalid, but ndisc specs say nothing
  838. about it. It could be misconfiguration, or
  839. an smart proxy agent tries to help us :-)
  840. We should not print the error if NA has been
  841. received from loopback - it is just our own
  842. unsolicited advertisement.
  843. */
  844. if (skb->pkt_type != PACKET_LOOPBACK)
  845. ND_PRINTK1(KERN_WARNING
  846. "ICMPv6 NA: someone advertises our address on %s!\n",
  847. ifp->idev->dev->name);
  848. in6_ifa_put(ifp);
  849. return;
  850. }
  851. neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
  852. if (neigh) {
  853. u8 old_flags = neigh->flags;
  854. struct net *net = dev_net(dev);
  855. if (neigh->nud_state & NUD_FAILED)
  856. goto out;
  857. /*
  858. * Don't update the neighbor cache entry on a proxy NA from
  859. * ourselves because either the proxied node is off link or it
  860. * has already sent a NA to us.
  861. */
  862. if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
  863. net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
  864. pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
  865. /* XXX: idev->cnf.prixy_ndp */
  866. goto out;
  867. }
  868. neigh_update(neigh, lladdr,
  869. msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
  870. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  871. (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
  872. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  873. (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0));
  874. if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
  875. /*
  876. * Change: router to host
  877. */
  878. struct rt6_info *rt;
  879. rt = rt6_get_dflt_router(saddr, dev);
  880. if (rt)
  881. ip6_del_rt(rt);
  882. }
  883. out:
  884. neigh_release(neigh);
  885. }
  886. }
  887. static void ndisc_recv_rs(struct sk_buff *skb)
  888. {
  889. struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
  890. unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
  891. struct neighbour *neigh;
  892. struct inet6_dev *idev;
  893. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  894. struct ndisc_options ndopts;
  895. u8 *lladdr = NULL;
  896. if (skb->len < sizeof(*rs_msg))
  897. return;
  898. idev = in6_dev_get(skb->dev);
  899. if (!idev) {
  900. if (net_ratelimit())
  901. ND_PRINTK1("ICMP6 RS: can't find in6 device\n");
  902. return;
  903. }
  904. /* Don't accept RS if we're not in router mode */
  905. if (!idev->cnf.forwarding)
  906. goto out;
  907. /*
  908. * Don't update NCE if src = ::;
  909. * this implies that the source node has no ip address assigned yet.
  910. */
  911. if (ipv6_addr_any(saddr))
  912. goto out;
  913. /* Parse ND options */
  914. if (!ndisc_parse_options(rs_msg->opt, ndoptlen, &ndopts)) {
  915. if (net_ratelimit())
  916. ND_PRINTK2("ICMP6 NS: invalid ND option, ignored\n");
  917. goto out;
  918. }
  919. if (ndopts.nd_opts_src_lladdr) {
  920. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  921. skb->dev);
  922. if (!lladdr)
  923. goto out;
  924. }
  925. neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
  926. if (neigh) {
  927. neigh_update(neigh, lladdr, NUD_STALE,
  928. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  929. NEIGH_UPDATE_F_OVERRIDE|
  930. NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
  931. neigh_release(neigh);
  932. }
  933. out:
  934. in6_dev_put(idev);
  935. }
  936. static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
  937. {
  938. struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
  939. struct sk_buff *skb;
  940. struct nlmsghdr *nlh;
  941. struct nduseroptmsg *ndmsg;
  942. struct net *net = dev_net(ra->dev);
  943. int err;
  944. int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
  945. + (opt->nd_opt_len << 3));
  946. size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
  947. skb = nlmsg_new(msg_size, GFP_ATOMIC);
  948. if (skb == NULL) {
  949. err = -ENOBUFS;
  950. goto errout;
  951. }
  952. nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
  953. if (nlh == NULL) {
  954. goto nla_put_failure;
  955. }
  956. ndmsg = nlmsg_data(nlh);
  957. ndmsg->nduseropt_family = AF_INET6;
  958. ndmsg->nduseropt_ifindex = ra->dev->ifindex;
  959. ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
  960. ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
  961. ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
  962. memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
  963. NLA_PUT(skb, NDUSEROPT_SRCADDR, sizeof(struct in6_addr),
  964. &ipv6_hdr(ra)->saddr);
  965. nlmsg_end(skb, nlh);
  966. rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
  967. return;
  968. nla_put_failure:
  969. nlmsg_free(skb);
  970. err = -EMSGSIZE;
  971. errout:
  972. rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
  973. }
  974. static void ndisc_router_discovery(struct sk_buff *skb)
  975. {
  976. struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
  977. struct neighbour *neigh = NULL;
  978. struct inet6_dev *in6_dev;
  979. struct rt6_info *rt = NULL;
  980. int lifetime;
  981. struct ndisc_options ndopts;
  982. int optlen;
  983. unsigned int pref = 0;
  984. __u8 * opt = (__u8 *)(ra_msg + 1);
  985. optlen = (skb->tail - skb->transport_header) - sizeof(struct ra_msg);
  986. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  987. ND_PRINTK2(KERN_WARNING
  988. "ICMPv6 RA: source address is not link-local.\n");
  989. return;
  990. }
  991. if (optlen < 0) {
  992. ND_PRINTK2(KERN_WARNING
  993. "ICMPv6 RA: packet too short\n");
  994. return;
  995. }
  996. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  997. if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
  998. ND_PRINTK2(KERN_WARNING
  999. "ICMPv6 RA: from host or unauthorized router\n");
  1000. return;
  1001. }
  1002. #endif
  1003. /*
  1004. * set the RA_RECV flag in the interface
  1005. */
  1006. in6_dev = in6_dev_get(skb->dev);
  1007. if (in6_dev == NULL) {
  1008. ND_PRINTK0(KERN_ERR
  1009. "ICMPv6 RA: can't find inet6 device for %s.\n",
  1010. skb->dev->name);
  1011. return;
  1012. }
  1013. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_ra) {
  1014. in6_dev_put(in6_dev);
  1015. return;
  1016. }
  1017. if (!ndisc_parse_options(opt, optlen, &ndopts)) {
  1018. in6_dev_put(in6_dev);
  1019. ND_PRINTK2(KERN_WARNING
  1020. "ICMP6 RA: invalid ND options\n");
  1021. return;
  1022. }
  1023. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1024. /* skip link-specific parameters from interior routers */
  1025. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT)
  1026. goto skip_linkparms;
  1027. #endif
  1028. if (in6_dev->if_flags & IF_RS_SENT) {
  1029. /*
  1030. * flag that an RA was received after an RS was sent
  1031. * out on this interface.
  1032. */
  1033. in6_dev->if_flags |= IF_RA_RCVD;
  1034. }
  1035. /*
  1036. * Remember the managed/otherconf flags from most recently
  1037. * received RA message (RFC 2462) -- yoshfuji
  1038. */
  1039. in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
  1040. IF_RA_OTHERCONF)) |
  1041. (ra_msg->icmph.icmp6_addrconf_managed ?
  1042. IF_RA_MANAGED : 0) |
  1043. (ra_msg->icmph.icmp6_addrconf_other ?
  1044. IF_RA_OTHERCONF : 0);
  1045. if (!in6_dev->cnf.accept_ra_defrtr)
  1046. goto skip_defrtr;
  1047. lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
  1048. #ifdef CONFIG_IPV6_ROUTER_PREF
  1049. pref = ra_msg->icmph.icmp6_router_pref;
  1050. /* 10b is handled as if it were 00b (medium) */
  1051. if (pref == ICMPV6_ROUTER_PREF_INVALID ||
  1052. !in6_dev->cnf.accept_ra_rtr_pref)
  1053. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  1054. #endif
  1055. rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
  1056. if (rt)
  1057. neigh = rt->rt6i_nexthop;
  1058. if (rt && lifetime == 0) {
  1059. neigh_clone(neigh);
  1060. ip6_del_rt(rt);
  1061. rt = NULL;
  1062. }
  1063. if (rt == NULL && lifetime) {
  1064. ND_PRINTK3(KERN_DEBUG
  1065. "ICMPv6 RA: adding default router.\n");
  1066. rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
  1067. if (rt == NULL) {
  1068. ND_PRINTK0(KERN_ERR
  1069. "ICMPv6 RA: %s() failed to add default route.\n",
  1070. __func__);
  1071. in6_dev_put(in6_dev);
  1072. return;
  1073. }
  1074. neigh = rt->rt6i_nexthop;
  1075. if (neigh == NULL) {
  1076. ND_PRINTK0(KERN_ERR
  1077. "ICMPv6 RA: %s() got default router without neighbour.\n",
  1078. __func__);
  1079. dst_release(&rt->u.dst);
  1080. in6_dev_put(in6_dev);
  1081. return;
  1082. }
  1083. neigh->flags |= NTF_ROUTER;
  1084. } else if (rt) {
  1085. rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  1086. }
  1087. if (rt)
  1088. rt->rt6i_expires = jiffies + (HZ * lifetime);
  1089. if (ra_msg->icmph.icmp6_hop_limit) {
  1090. in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
  1091. if (rt)
  1092. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = ra_msg->icmph.icmp6_hop_limit;
  1093. }
  1094. skip_defrtr:
  1095. /*
  1096. * Update Reachable Time and Retrans Timer
  1097. */
  1098. if (in6_dev->nd_parms) {
  1099. unsigned long rtime = ntohl(ra_msg->retrans_timer);
  1100. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
  1101. rtime = (rtime*HZ)/1000;
  1102. if (rtime < HZ/10)
  1103. rtime = HZ/10;
  1104. in6_dev->nd_parms->retrans_time = rtime;
  1105. in6_dev->tstamp = jiffies;
  1106. inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
  1107. }
  1108. rtime = ntohl(ra_msg->reachable_time);
  1109. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
  1110. rtime = (rtime*HZ)/1000;
  1111. if (rtime < HZ/10)
  1112. rtime = HZ/10;
  1113. if (rtime != in6_dev->nd_parms->base_reachable_time) {
  1114. in6_dev->nd_parms->base_reachable_time = rtime;
  1115. in6_dev->nd_parms->gc_staletime = 3 * rtime;
  1116. in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
  1117. in6_dev->tstamp = jiffies;
  1118. inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
  1119. }
  1120. }
  1121. }
  1122. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1123. skip_linkparms:
  1124. #endif
  1125. /*
  1126. * Process options.
  1127. */
  1128. if (!neigh)
  1129. neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
  1130. skb->dev, 1);
  1131. if (neigh) {
  1132. u8 *lladdr = NULL;
  1133. if (ndopts.nd_opts_src_lladdr) {
  1134. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  1135. skb->dev);
  1136. if (!lladdr) {
  1137. ND_PRINTK2(KERN_WARNING
  1138. "ICMPv6 RA: invalid link-layer address length\n");
  1139. goto out;
  1140. }
  1141. }
  1142. neigh_update(neigh, lladdr, NUD_STALE,
  1143. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1144. NEIGH_UPDATE_F_OVERRIDE|
  1145. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1146. NEIGH_UPDATE_F_ISROUTER);
  1147. }
  1148. #ifdef CONFIG_IPV6_ROUTE_INFO
  1149. if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
  1150. struct nd_opt_hdr *p;
  1151. for (p = ndopts.nd_opts_ri;
  1152. p;
  1153. p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
  1154. struct route_info *ri = (struct route_info *)p;
  1155. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1156. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
  1157. ri->prefix_len == 0)
  1158. continue;
  1159. #endif
  1160. if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
  1161. continue;
  1162. rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3,
  1163. &ipv6_hdr(skb)->saddr);
  1164. }
  1165. }
  1166. #endif
  1167. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1168. /* skip link-specific ndopts from interior routers */
  1169. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT)
  1170. goto out;
  1171. #endif
  1172. if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
  1173. struct nd_opt_hdr *p;
  1174. for (p = ndopts.nd_opts_pi;
  1175. p;
  1176. p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
  1177. addrconf_prefix_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3);
  1178. }
  1179. }
  1180. if (ndopts.nd_opts_mtu) {
  1181. __be32 n;
  1182. u32 mtu;
  1183. memcpy(&n, ((u8*)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
  1184. mtu = ntohl(n);
  1185. if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
  1186. ND_PRINTK2(KERN_WARNING
  1187. "ICMPv6 RA: invalid mtu: %d\n",
  1188. mtu);
  1189. } else if (in6_dev->cnf.mtu6 != mtu) {
  1190. in6_dev->cnf.mtu6 = mtu;
  1191. if (rt)
  1192. rt->u.dst.metrics[RTAX_MTU-1] = mtu;
  1193. rt6_mtu_change(skb->dev, mtu);
  1194. }
  1195. }
  1196. if (ndopts.nd_useropts) {
  1197. struct nd_opt_hdr *p;
  1198. for (p = ndopts.nd_useropts;
  1199. p;
  1200. p = ndisc_next_useropt(p, ndopts.nd_useropts_end)) {
  1201. ndisc_ra_useropt(skb, p);
  1202. }
  1203. }
  1204. if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
  1205. ND_PRINTK2(KERN_WARNING
  1206. "ICMPv6 RA: invalid RA options");
  1207. }
  1208. out:
  1209. if (rt)
  1210. dst_release(&rt->u.dst);
  1211. else if (neigh)
  1212. neigh_release(neigh);
  1213. in6_dev_put(in6_dev);
  1214. }
  1215. static void ndisc_redirect_rcv(struct sk_buff *skb)
  1216. {
  1217. struct inet6_dev *in6_dev;
  1218. struct icmp6hdr *icmph;
  1219. struct in6_addr *dest;
  1220. struct in6_addr *target; /* new first hop to destination */
  1221. struct neighbour *neigh;
  1222. int on_link = 0;
  1223. struct ndisc_options ndopts;
  1224. int optlen;
  1225. u8 *lladdr = NULL;
  1226. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1227. switch (skb->ndisc_nodetype) {
  1228. case NDISC_NODETYPE_HOST:
  1229. case NDISC_NODETYPE_NODEFAULT:
  1230. ND_PRINTK2(KERN_WARNING
  1231. "ICMPv6 Redirect: from host or unauthorized router\n");
  1232. return;
  1233. }
  1234. #endif
  1235. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  1236. ND_PRINTK2(KERN_WARNING
  1237. "ICMPv6 Redirect: source address is not link-local.\n");
  1238. return;
  1239. }
  1240. optlen = skb->tail - skb->transport_header;
  1241. optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
  1242. if (optlen < 0) {
  1243. ND_PRINTK2(KERN_WARNING
  1244. "ICMPv6 Redirect: packet too short\n");
  1245. return;
  1246. }
  1247. icmph = icmp6_hdr(skb);
  1248. target = (struct in6_addr *) (icmph + 1);
  1249. dest = target + 1;
  1250. if (ipv6_addr_is_multicast(dest)) {
  1251. ND_PRINTK2(KERN_WARNING
  1252. "ICMPv6 Redirect: destination address is multicast.\n");
  1253. return;
  1254. }
  1255. if (ipv6_addr_equal(dest, target)) {
  1256. on_link = 1;
  1257. } else if (ipv6_addr_type(target) !=
  1258. (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1259. ND_PRINTK2(KERN_WARNING
  1260. "ICMPv6 Redirect: target address is not link-local unicast.\n");
  1261. return;
  1262. }
  1263. in6_dev = in6_dev_get(skb->dev);
  1264. if (!in6_dev)
  1265. return;
  1266. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) {
  1267. in6_dev_put(in6_dev);
  1268. return;
  1269. }
  1270. /* RFC2461 8.1:
  1271. * The IP source address of the Redirect MUST be the same as the current
  1272. * first-hop router for the specified ICMP Destination Address.
  1273. */
  1274. if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
  1275. ND_PRINTK2(KERN_WARNING
  1276. "ICMPv6 Redirect: invalid ND options\n");
  1277. in6_dev_put(in6_dev);
  1278. return;
  1279. }
  1280. if (ndopts.nd_opts_tgt_lladdr) {
  1281. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
  1282. skb->dev);
  1283. if (!lladdr) {
  1284. ND_PRINTK2(KERN_WARNING
  1285. "ICMPv6 Redirect: invalid link-layer address length\n");
  1286. in6_dev_put(in6_dev);
  1287. return;
  1288. }
  1289. }
  1290. neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
  1291. if (neigh) {
  1292. rt6_redirect(dest, &ipv6_hdr(skb)->daddr,
  1293. &ipv6_hdr(skb)->saddr, neigh, lladdr,
  1294. on_link);
  1295. neigh_release(neigh);
  1296. }
  1297. in6_dev_put(in6_dev);
  1298. }
  1299. void ndisc_send_redirect(struct sk_buff *skb, struct neighbour *neigh,
  1300. const struct in6_addr *target)
  1301. {
  1302. struct net_device *dev = skb->dev;
  1303. struct net *net = dev_net(dev);
  1304. struct sock *sk = net->ipv6.ndisc_sk;
  1305. int len = sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
  1306. struct sk_buff *buff;
  1307. struct icmp6hdr *icmph;
  1308. struct in6_addr saddr_buf;
  1309. struct in6_addr *addrp;
  1310. struct rt6_info *rt;
  1311. struct dst_entry *dst;
  1312. struct inet6_dev *idev;
  1313. struct flowi fl;
  1314. u8 *opt;
  1315. int rd_len;
  1316. int err;
  1317. u8 ha_buf[MAX_ADDR_LEN], *ha = NULL;
  1318. if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
  1319. ND_PRINTK2(KERN_WARNING
  1320. "ICMPv6 Redirect: no link-local address on %s\n",
  1321. dev->name);
  1322. return;
  1323. }
  1324. if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
  1325. ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1326. ND_PRINTK2(KERN_WARNING
  1327. "ICMPv6 Redirect: target address is not link-local unicast.\n");
  1328. return;
  1329. }
  1330. icmpv6_flow_init(sk, &fl, NDISC_REDIRECT,
  1331. &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
  1332. dst = ip6_route_output(net, NULL, &fl);
  1333. if (dst == NULL)
  1334. return;
  1335. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  1336. if (err)
  1337. return;
  1338. rt = (struct rt6_info *) dst;
  1339. if (rt->rt6i_flags & RTF_GATEWAY) {
  1340. ND_PRINTK2(KERN_WARNING
  1341. "ICMPv6 Redirect: destination is not a neighbour.\n");
  1342. goto release;
  1343. }
  1344. if (!xrlim_allow(dst, 1*HZ))
  1345. goto release;
  1346. if (dev->addr_len) {
  1347. read_lock_bh(&neigh->lock);
  1348. if (neigh->nud_state & NUD_VALID) {
  1349. memcpy(ha_buf, neigh->ha, dev->addr_len);
  1350. read_unlock_bh(&neigh->lock);
  1351. ha = ha_buf;
  1352. len += ndisc_opt_addr_space(dev);
  1353. } else
  1354. read_unlock_bh(&neigh->lock);
  1355. }
  1356. rd_len = min_t(unsigned int,
  1357. IPV6_MIN_MTU-sizeof(struct ipv6hdr)-len, skb->len + 8);
  1358. rd_len &= ~0x7;
  1359. len += rd_len;
  1360. buff = sock_alloc_send_skb(sk,
  1361. (MAX_HEADER + sizeof(struct ipv6hdr) +
  1362. len + LL_ALLOCATED_SPACE(dev)),
  1363. 1, &err);
  1364. if (buff == NULL) {
  1365. ND_PRINTK0(KERN_ERR
  1366. "ICMPv6 Redirect: %s() failed to allocate an skb, err=%d.\n",
  1367. __func__, err);
  1368. goto release;
  1369. }
  1370. skb_reserve(buff, LL_RESERVED_SPACE(dev));
  1371. ip6_nd_hdr(sk, buff, dev, &saddr_buf, &ipv6_hdr(skb)->saddr,
  1372. IPPROTO_ICMPV6, len);
  1373. skb_set_transport_header(buff, skb_tail_pointer(buff) - buff->data);
  1374. skb_put(buff, len);
  1375. icmph = icmp6_hdr(buff);
  1376. memset(icmph, 0, sizeof(struct icmp6hdr));
  1377. icmph->icmp6_type = NDISC_REDIRECT;
  1378. /*
  1379. * copy target and destination addresses
  1380. */
  1381. addrp = (struct in6_addr *)(icmph + 1);
  1382. ipv6_addr_copy(addrp, target);
  1383. addrp++;
  1384. ipv6_addr_copy(addrp, &ipv6_hdr(skb)->daddr);
  1385. opt = (u8*) (addrp + 1);
  1386. /*
  1387. * include target_address option
  1388. */
  1389. if (ha)
  1390. opt = ndisc_fill_addr_option(opt, ND_OPT_TARGET_LL_ADDR, ha,
  1391. dev->addr_len, dev->type);
  1392. /*
  1393. * build redirect option and copy skb over to the new packet.
  1394. */
  1395. memset(opt, 0, 8);
  1396. *(opt++) = ND_OPT_REDIRECT_HDR;
  1397. *(opt++) = (rd_len >> 3);
  1398. opt += 6;
  1399. memcpy(opt, ipv6_hdr(skb), rd_len - 8);
  1400. icmph->icmp6_cksum = csum_ipv6_magic(&saddr_buf, &ipv6_hdr(skb)->saddr,
  1401. len, IPPROTO_ICMPV6,
  1402. csum_partial(icmph, len, 0));
  1403. skb_dst_set(buff, dst);
  1404. idev = in6_dev_get(dst->dev);
  1405. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  1406. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, buff, NULL, dst->dev,
  1407. dst_output);
  1408. if (!err) {
  1409. ICMP6MSGOUT_INC_STATS(net, idev, NDISC_REDIRECT);
  1410. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1411. }
  1412. if (likely(idev != NULL))
  1413. in6_dev_put(idev);
  1414. return;
  1415. release:
  1416. dst_release(dst);
  1417. }
  1418. static void pndisc_redo(struct sk_buff *skb)
  1419. {
  1420. ndisc_recv_ns(skb);
  1421. kfree_skb(skb);
  1422. }
  1423. int ndisc_rcv(struct sk_buff *skb)
  1424. {
  1425. struct nd_msg *msg;
  1426. if (!pskb_may_pull(skb, skb->len))
  1427. return 0;
  1428. msg = (struct nd_msg *)skb_transport_header(skb);
  1429. __skb_push(skb, skb->data - skb_transport_header(skb));
  1430. if (ipv6_hdr(skb)->hop_limit != 255) {
  1431. ND_PRINTK2(KERN_WARNING
  1432. "ICMPv6 NDISC: invalid hop-limit: %d\n",
  1433. ipv6_hdr(skb)->hop_limit);
  1434. return 0;
  1435. }
  1436. if (msg->icmph.icmp6_code != 0) {
  1437. ND_PRINTK2(KERN_WARNING
  1438. "ICMPv6 NDISC: invalid ICMPv6 code: %d\n",
  1439. msg->icmph.icmp6_code);
  1440. return 0;
  1441. }
  1442. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  1443. switch (msg->icmph.icmp6_type) {
  1444. case NDISC_NEIGHBOUR_SOLICITATION:
  1445. ndisc_recv_ns(skb);
  1446. break;
  1447. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  1448. ndisc_recv_na(skb);
  1449. break;
  1450. case NDISC_ROUTER_SOLICITATION:
  1451. ndisc_recv_rs(skb);
  1452. break;
  1453. case NDISC_ROUTER_ADVERTISEMENT:
  1454. ndisc_router_discovery(skb);
  1455. break;
  1456. case NDISC_REDIRECT:
  1457. ndisc_redirect_rcv(skb);
  1458. break;
  1459. }
  1460. return 0;
  1461. }
  1462. static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1463. {
  1464. struct net_device *dev = ptr;
  1465. struct net *net = dev_net(dev);
  1466. switch (event) {
  1467. case NETDEV_CHANGEADDR:
  1468. neigh_changeaddr(&nd_tbl, dev);
  1469. fib6_run_gc(~0UL, net);
  1470. break;
  1471. case NETDEV_DOWN:
  1472. neigh_ifdown(&nd_tbl, dev);
  1473. fib6_run_gc(~0UL, net);
  1474. break;
  1475. default:
  1476. break;
  1477. }
  1478. return NOTIFY_DONE;
  1479. }
  1480. static struct notifier_block ndisc_netdev_notifier = {
  1481. .notifier_call = ndisc_netdev_event,
  1482. };
  1483. #ifdef CONFIG_SYSCTL
  1484. static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
  1485. const char *func, const char *dev_name)
  1486. {
  1487. static char warncomm[TASK_COMM_LEN];
  1488. static int warned;
  1489. if (strcmp(warncomm, current->comm) && warned < 5) {
  1490. strcpy(warncomm, current->comm);
  1491. printk(KERN_WARNING
  1492. "process `%s' is using deprecated sysctl (%s) "
  1493. "net.ipv6.neigh.%s.%s; "
  1494. "Use net.ipv6.neigh.%s.%s_ms "
  1495. "instead.\n",
  1496. warncomm, func,
  1497. dev_name, ctl->procname,
  1498. dev_name, ctl->procname);
  1499. warned++;
  1500. }
  1501. }
  1502. int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, struct file * filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  1503. {
  1504. struct net_device *dev = ctl->extra1;
  1505. struct inet6_dev *idev;
  1506. int ret;
  1507. if ((strcmp(ctl->procname, "retrans_time") == 0) ||
  1508. (strcmp(ctl->procname, "base_reachable_time") == 0))
  1509. ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
  1510. if (strcmp(ctl->procname, "retrans_time") == 0)
  1511. ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  1512. else if (strcmp(ctl->procname, "base_reachable_time") == 0)
  1513. ret = proc_dointvec_jiffies(ctl, write,
  1514. filp, buffer, lenp, ppos);
  1515. else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
  1516. (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
  1517. ret = proc_dointvec_ms_jiffies(ctl, write,
  1518. filp, buffer, lenp, ppos);
  1519. else
  1520. ret = -1;
  1521. if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
  1522. if (ctl->data == &idev->nd_parms->base_reachable_time)
  1523. idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time);
  1524. idev->tstamp = jiffies;
  1525. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1526. in6_dev_put(idev);
  1527. }
  1528. return ret;
  1529. }
  1530. int ndisc_ifinfo_sysctl_strategy(ctl_table *ctl,
  1531. void __user *oldval, size_t __user *oldlenp,
  1532. void __user *newval, size_t newlen)
  1533. {
  1534. struct net_device *dev = ctl->extra1;
  1535. struct inet6_dev *idev;
  1536. int ret;
  1537. if (ctl->ctl_name == NET_NEIGH_RETRANS_TIME ||
  1538. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME)
  1539. ndisc_warn_deprecated_sysctl(ctl, "procfs", dev ? dev->name : "default");
  1540. switch (ctl->ctl_name) {
  1541. case NET_NEIGH_REACHABLE_TIME:
  1542. ret = sysctl_jiffies(ctl, oldval, oldlenp, newval, newlen);
  1543. break;
  1544. case NET_NEIGH_RETRANS_TIME_MS:
  1545. case NET_NEIGH_REACHABLE_TIME_MS:
  1546. ret = sysctl_ms_jiffies(ctl, oldval, oldlenp, newval, newlen);
  1547. break;
  1548. default:
  1549. ret = 0;
  1550. }
  1551. if (newval && newlen && ret > 0 &&
  1552. dev && (idev = in6_dev_get(dev)) != NULL) {
  1553. if (ctl->ctl_name == NET_NEIGH_REACHABLE_TIME ||
  1554. ctl->ctl_name == NET_NEIGH_REACHABLE_TIME_MS)
  1555. idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time);
  1556. idev->tstamp = jiffies;
  1557. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1558. in6_dev_put(idev);
  1559. }
  1560. return ret;
  1561. }
  1562. #endif
  1563. static int ndisc_net_init(struct net *net)
  1564. {
  1565. struct ipv6_pinfo *np;
  1566. struct sock *sk;
  1567. int err;
  1568. err = inet_ctl_sock_create(&sk, PF_INET6,
  1569. SOCK_RAW, IPPROTO_ICMPV6, net);
  1570. if (err < 0) {
  1571. ND_PRINTK0(KERN_ERR
  1572. "ICMPv6 NDISC: Failed to initialize the control socket (err %d).\n",
  1573. err);
  1574. return err;
  1575. }
  1576. net->ipv6.ndisc_sk = sk;
  1577. np = inet6_sk(sk);
  1578. np->hop_limit = 255;
  1579. /* Do not loopback ndisc messages */
  1580. np->mc_loop = 0;
  1581. return 0;
  1582. }
  1583. static void ndisc_net_exit(struct net *net)
  1584. {
  1585. inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
  1586. }
  1587. static struct pernet_operations ndisc_net_ops = {
  1588. .init = ndisc_net_init,
  1589. .exit = ndisc_net_exit,
  1590. };
  1591. int __init ndisc_init(void)
  1592. {
  1593. int err;
  1594. err = register_pernet_subsys(&ndisc_net_ops);
  1595. if (err)
  1596. return err;
  1597. /*
  1598. * Initialize the neighbour table
  1599. */
  1600. neigh_table_init(&nd_tbl);
  1601. #ifdef CONFIG_SYSCTL
  1602. err = neigh_sysctl_register(NULL, &nd_tbl.parms, NET_IPV6,
  1603. NET_IPV6_NEIGH, "ipv6",
  1604. &ndisc_ifinfo_sysctl_change,
  1605. &ndisc_ifinfo_sysctl_strategy);
  1606. if (err)
  1607. goto out_unregister_pernet;
  1608. #endif
  1609. err = register_netdevice_notifier(&ndisc_netdev_notifier);
  1610. if (err)
  1611. goto out_unregister_sysctl;
  1612. out:
  1613. return err;
  1614. out_unregister_sysctl:
  1615. #ifdef CONFIG_SYSCTL
  1616. neigh_sysctl_unregister(&nd_tbl.parms);
  1617. out_unregister_pernet:
  1618. #endif
  1619. unregister_pernet_subsys(&ndisc_net_ops);
  1620. goto out;
  1621. }
  1622. void ndisc_cleanup(void)
  1623. {
  1624. unregister_netdevice_notifier(&ndisc_netdev_notifier);
  1625. #ifdef CONFIG_SYSCTL
  1626. neigh_sysctl_unregister(&nd_tbl.parms);
  1627. #endif
  1628. neigh_table_clear(&nd_tbl);
  1629. unregister_pernet_subsys(&ndisc_net_ops);
  1630. }