ndisc.h 13 KB

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