ndisc.c 42 KB

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