ndisc.c 45 KB

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