ndisc.c 42 KB

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