ndisc.c 45 KB

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