ndisc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _NDISC_H
  3. #define _NDISC_H
  4. #include <net/ipv6_stubs.h>
  5. /*
  6. * ICMP codes for neighbour discovery messages
  7. */
  8. #define NDISC_ROUTER_SOLICITATION 133
  9. #define NDISC_ROUTER_ADVERTISEMENT 134
  10. #define NDISC_NEIGHBOUR_SOLICITATION 135
  11. #define NDISC_NEIGHBOUR_ADVERTISEMENT 136
  12. #define NDISC_REDIRECT 137
  13. /*
  14. * Router type: cross-layer information from link-layer to
  15. * IPv6 layer reported by certain link types (e.g., RFC4214).
  16. */
  17. #define NDISC_NODETYPE_UNSPEC 0 /* unspecified (default) */
  18. #define NDISC_NODETYPE_HOST 1 /* host or unauthorized router */
  19. #define NDISC_NODETYPE_NODEFAULT 2 /* non-default router */
  20. #define NDISC_NODETYPE_DEFAULT 3 /* default router */
  21. /*
  22. * ndisc options
  23. */
  24. enum {
  25. __ND_OPT_PREFIX_INFO_END = 0,
  26. ND_OPT_SOURCE_LL_ADDR = 1, /* RFC2461 */
  27. ND_OPT_TARGET_LL_ADDR = 2, /* RFC2461 */
  28. ND_OPT_PREFIX_INFO = 3, /* RFC2461 */
  29. ND_OPT_REDIRECT_HDR = 4, /* RFC2461 */
  30. ND_OPT_MTU = 5, /* RFC2461 */
  31. ND_OPT_NONCE = 14, /* RFC7527 */
  32. __ND_OPT_ARRAY_MAX,
  33. ND_OPT_ROUTE_INFO = 24, /* RFC4191 */
  34. ND_OPT_RDNSS = 25, /* RFC5006 */
  35. ND_OPT_DNSSL = 31, /* RFC6106 */
  36. ND_OPT_6CO = 34, /* RFC6775 */
  37. ND_OPT_CAPTIVE_PORTAL = 37, /* RFC7710 */
  38. ND_OPT_PREF64 = 38, /* RFC8781 */
  39. __ND_OPT_MAX
  40. };
  41. #define MAX_RTR_SOLICITATION_DELAY HZ
  42. #define ND_REACHABLE_TIME (30*HZ)
  43. #define ND_RETRANS_TIMER HZ
  44. #include <linux/compiler.h>
  45. #include <linux/icmpv6.h>
  46. #include <linux/in6.h>
  47. #include <linux/types.h>
  48. #include <linux/if_arp.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/hash.h>
  51. #include <net/neighbour.h>
  52. /* Set to 3 to get tracing... */
  53. #define ND_DEBUG 1
  54. #define ND_PRINTK(val, level, fmt, ...) \
  55. do { \
  56. if (val <= ND_DEBUG) \
  57. net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
  58. } while (0)
  59. struct ctl_table;
  60. struct inet6_dev;
  61. struct net_device;
  62. struct net_proto_family;
  63. struct sk_buff;
  64. struct prefix_info;
  65. extern struct neigh_table nd_tbl;
  66. struct nd_msg {
  67. struct icmp6hdr icmph;
  68. struct in6_addr target;
  69. __u8 opt[];
  70. };
  71. struct rs_msg {
  72. struct icmp6hdr icmph;
  73. __u8 opt[];
  74. };
  75. struct ra_msg {
  76. struct icmp6hdr icmph;
  77. __be32 reachable_time;
  78. __be32 retrans_timer;
  79. };
  80. struct rd_msg {
  81. struct icmp6hdr icmph;
  82. struct in6_addr target;
  83. struct in6_addr dest;
  84. __u8 opt[];
  85. };
  86. struct nd_opt_hdr {
  87. __u8 nd_opt_type;
  88. __u8 nd_opt_len;
  89. } __packed;
  90. /* ND options */
  91. struct ndisc_options {
  92. struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
  93. #ifdef CONFIG_IPV6_ROUTE_INFO
  94. struct nd_opt_hdr *nd_opts_ri;
  95. struct nd_opt_hdr *nd_opts_ri_end;
  96. #endif
  97. struct nd_opt_hdr *nd_useropts;
  98. struct nd_opt_hdr *nd_useropts_end;
  99. #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
  100. struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
  101. #endif
  102. };
  103. #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
  104. #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
  105. #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
  106. #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
  107. #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
  108. #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
  109. #define nd_opts_nonce nd_opt_array[ND_OPT_NONCE]
  110. #define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
  111. #define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
  112. #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
  113. struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
  114. u8 *opt, int opt_len,
  115. struct ndisc_options *ndopts);
  116. void __ndisc_fill_addr_option(struct sk_buff *skb, int type, const void *data,
  117. int data_len, int pad);
  118. #define NDISC_OPS_REDIRECT_DATA_SPACE 2
  119. /*
  120. * This structure defines the hooks for IPv6 neighbour discovery.
  121. * The following hooks can be defined; unless noted otherwise, they are
  122. * optional and can be filled with a null pointer.
  123. *
  124. * int (*parse_options)(const struct net_device *dev,
  125. * struct nd_opt_hdr *nd_opt,
  126. * struct ndisc_options *ndopts):
  127. * This function is called while parsing ndisc ops and put each position
  128. * as pointer into ndopts. If this function return unequal 0, then this
  129. * function took care about the ndisc option, if 0 then the IPv6 ndisc
  130. * option parser will take care about that option.
  131. *
  132. * void (*update)(const struct net_device *dev, struct neighbour *n,
  133. * u32 flags, u8 icmp6_type,
  134. * const struct ndisc_options *ndopts):
  135. * This function is called when IPv6 ndisc updates the neighbour cache
  136. * entry. Additional options which can be updated may be previously
  137. * parsed by parse_opts callback and accessible over ndopts parameter.
  138. *
  139. * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
  140. * struct neighbour *neigh, u8 *ha_buf,
  141. * u8 **ha):
  142. * This function is called when the necessary option space will be
  143. * calculated before allocating a skb. The parameters neigh, ha_buf
  144. * abd ha are available on NDISC_REDIRECT messages only.
  145. *
  146. * void (*fill_addr_option)(const struct net_device *dev,
  147. * struct sk_buff *skb, u8 icmp6_type,
  148. * const u8 *ha):
  149. * This function is called when the skb will finally fill the option
  150. * fields inside skb. NOTE: this callback should fill the option
  151. * fields to the skb which are previously indicated by opt_space
  152. * parameter. That means the decision to add such option should
  153. * not lost between these two callbacks, e.g. protected by interface
  154. * up state.
  155. *
  156. * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
  157. * const struct prefix_info *pinfo,
  158. * struct inet6_dev *in6_dev,
  159. * struct in6_addr *addr,
  160. * int addr_type, u32 addr_flags,
  161. * bool sllao, bool tokenized,
  162. * __u32 valid_lft, u32 prefered_lft,
  163. * bool dev_addr_generated):
  164. * This function is called when a RA messages is received with valid
  165. * PIO option fields and an IPv6 address will be added to the interface
  166. * for autoconfiguration. The parameter dev_addr_generated reports about
  167. * if the address was based on dev->dev_addr or not. This can be used
  168. * to add a second address if link-layer operates with two link layer
  169. * addresses. E.g. 802.15.4 6LoWPAN.
  170. */
  171. struct ndisc_ops {
  172. int (*parse_options)(const struct net_device *dev,
  173. struct nd_opt_hdr *nd_opt,
  174. struct ndisc_options *ndopts);
  175. void (*update)(const struct net_device *dev, struct neighbour *n,
  176. u32 flags, u8 icmp6_type,
  177. const struct ndisc_options *ndopts);
  178. int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
  179. struct neighbour *neigh, u8 *ha_buf,
  180. u8 **ha);
  181. void (*fill_addr_option)(const struct net_device *dev,
  182. struct sk_buff *skb, u8 icmp6_type,
  183. const u8 *ha);
  184. void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
  185. const struct prefix_info *pinfo,
  186. struct inet6_dev *in6_dev,
  187. struct in6_addr *addr,
  188. int addr_type, u32 addr_flags,
  189. bool sllao, bool tokenized,
  190. __u32 valid_lft, u32 prefered_lft,
  191. bool dev_addr_generated);
  192. };
  193. #if IS_ENABLED(CONFIG_IPV6)
  194. static inline int ndisc_ops_parse_options(const struct net_device *dev,
  195. struct nd_opt_hdr *nd_opt,
  196. struct ndisc_options *ndopts)
  197. {
  198. if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
  199. return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
  200. else
  201. return 0;
  202. }
  203. static inline void ndisc_ops_update(const struct net_device *dev,
  204. struct neighbour *n, u32 flags,
  205. u8 icmp6_type,
  206. const struct ndisc_options *ndopts)
  207. {
  208. if (dev->ndisc_ops && dev->ndisc_ops->update)
  209. dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
  210. }
  211. static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
  212. u8 icmp6_type)
  213. {
  214. if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
  215. icmp6_type != NDISC_REDIRECT)
  216. return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
  217. NULL, NULL);
  218. else
  219. return 0;
  220. }
  221. static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
  222. struct neighbour *neigh,
  223. u8 *ha_buf, u8 **ha)
  224. {
  225. if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
  226. return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
  227. neigh, ha_buf, ha);
  228. else
  229. return 0;
  230. }
  231. static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
  232. struct sk_buff *skb,
  233. u8 icmp6_type)
  234. {
  235. if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
  236. icmp6_type != NDISC_REDIRECT)
  237. dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
  238. }
  239. static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
  240. struct sk_buff *skb,
  241. const u8 *ha)
  242. {
  243. if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
  244. dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
  245. }
  246. static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
  247. struct net_device *dev,
  248. const struct prefix_info *pinfo,
  249. struct inet6_dev *in6_dev,
  250. struct in6_addr *addr,
  251. int addr_type, u32 addr_flags,
  252. bool sllao, bool tokenized,
  253. __u32 valid_lft,
  254. u32 prefered_lft,
  255. bool dev_addr_generated)
  256. {
  257. if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
  258. dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
  259. addr, addr_type,
  260. addr_flags, sllao,
  261. tokenized, valid_lft,
  262. prefered_lft,
  263. dev_addr_generated);
  264. }
  265. #endif
  266. /*
  267. * Return the padding between the option length and the start of the
  268. * link addr. Currently only IP-over-InfiniBand needs this, although
  269. * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
  270. * also need a pad of 2.
  271. */
  272. static inline int ndisc_addr_option_pad(unsigned short type)
  273. {
  274. switch (type) {
  275. case ARPHRD_INFINIBAND: return 2;
  276. default: return 0;
  277. }
  278. }
  279. static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
  280. {
  281. return NDISC_OPT_SPACE(addr_len + pad);
  282. }
  283. #if IS_ENABLED(CONFIG_IPV6)
  284. static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
  285. {
  286. return __ndisc_opt_addr_space(dev->addr_len,
  287. ndisc_addr_option_pad(dev->type)) +
  288. ndisc_ops_opt_addr_space(dev, icmp6_type);
  289. }
  290. static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
  291. struct neighbour *neigh,
  292. u8 *ops_data_buf,
  293. u8 **ops_data)
  294. {
  295. return __ndisc_opt_addr_space(dev->addr_len,
  296. ndisc_addr_option_pad(dev->type)) +
  297. ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
  298. ops_data);
  299. }
  300. #endif
  301. static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
  302. unsigned char addr_len, int prepad)
  303. {
  304. u8 *lladdr = (u8 *)(p + 1);
  305. int lladdrlen = p->nd_opt_len << 3;
  306. if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
  307. return NULL;
  308. return lladdr + prepad;
  309. }
  310. static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
  311. struct net_device *dev)
  312. {
  313. return __ndisc_opt_addr_data(p, dev->addr_len,
  314. ndisc_addr_option_pad(dev->type));
  315. }
  316. static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
  317. {
  318. const u32 *p32 = pkey;
  319. return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
  320. (p32[1] * hash_rnd[1]) +
  321. (p32[2] * hash_rnd[2]) +
  322. (p32[3] * hash_rnd[3]));
  323. }
  324. static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
  325. {
  326. return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
  327. }
  328. static inline
  329. struct neighbour *__ipv6_neigh_lookup_noref_stub(struct net_device *dev,
  330. const void *pkey)
  331. {
  332. return ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
  333. ndisc_hashfn, pkey, dev);
  334. }
  335. static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
  336. {
  337. struct neighbour *n;
  338. rcu_read_lock();
  339. n = __ipv6_neigh_lookup_noref(dev, pkey);
  340. if (n && !refcount_inc_not_zero(&n->refcnt))
  341. n = NULL;
  342. rcu_read_unlock();
  343. return n;
  344. }
  345. static inline void __ipv6_confirm_neigh(struct net_device *dev,
  346. const void *pkey)
  347. {
  348. struct neighbour *n;
  349. rcu_read_lock();
  350. n = __ipv6_neigh_lookup_noref(dev, pkey);
  351. neigh_confirm(n);
  352. rcu_read_unlock();
  353. }
  354. static inline void __ipv6_confirm_neigh_stub(struct net_device *dev,
  355. const void *pkey)
  356. {
  357. struct neighbour *n;
  358. rcu_read_lock();
  359. n = __ipv6_neigh_lookup_noref_stub(dev, pkey);
  360. neigh_confirm(n);
  361. rcu_read_unlock();
  362. }
  363. /* uses ipv6_stub and is meant for use outside of IPv6 core */
  364. static inline struct neighbour *ip_neigh_gw6(struct net_device *dev,
  365. const void *addr)
  366. {
  367. struct neighbour *neigh;
  368. neigh = __ipv6_neigh_lookup_noref_stub(dev, addr);
  369. if (unlikely(!neigh))
  370. neigh = __neigh_create(ipv6_stub->nd_tbl, addr, dev, false);
  371. return neigh;
  372. }
  373. int ndisc_init(void);
  374. int ndisc_late_init(void);
  375. void ndisc_late_cleanup(void);
  376. void ndisc_cleanup(void);
  377. enum skb_drop_reason ndisc_rcv(struct sk_buff *skb);
  378. struct sk_buff *ndisc_ns_create(struct net_device *dev, const struct in6_addr *solicit,
  379. const struct in6_addr *saddr, u64 nonce);
  380. void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
  381. const struct in6_addr *daddr, const struct in6_addr *saddr,
  382. u64 nonce);
  383. void ndisc_send_skb(struct sk_buff *skb, const struct in6_addr *daddr,
  384. const struct in6_addr *saddr);
  385. void ndisc_send_rs(struct net_device *dev,
  386. const struct in6_addr *saddr, const struct in6_addr *daddr);
  387. void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
  388. const struct in6_addr *solicited_addr,
  389. bool router, bool solicited, bool override, bool inc_opt);
  390. void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
  391. int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
  392. int dir);
  393. void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
  394. const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
  395. struct ndisc_options *ndopts);
  396. /*
  397. * IGMP
  398. */
  399. int igmp6_init(void);
  400. int igmp6_late_init(void);
  401. void igmp6_cleanup(void);
  402. void igmp6_late_cleanup(void);
  403. void igmp6_event_query(struct sk_buff *skb);
  404. void igmp6_event_report(struct sk_buff *skb);
  405. #ifdef CONFIG_SYSCTL
  406. int ndisc_ifinfo_sysctl_change(const struct ctl_table *ctl, int write,
  407. void *buffer, size_t *lenp, loff_t *ppos);
  408. #endif
  409. void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
  410. #endif