ndisc.c 46 KB

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