0001
0002 #ifndef _NDISC_H
0003 #define _NDISC_H
0004
0005 #include <net/ipv6_stubs.h>
0006
0007
0008
0009
0010
0011 #define NDISC_ROUTER_SOLICITATION 133
0012 #define NDISC_ROUTER_ADVERTISEMENT 134
0013 #define NDISC_NEIGHBOUR_SOLICITATION 135
0014 #define NDISC_NEIGHBOUR_ADVERTISEMENT 136
0015 #define NDISC_REDIRECT 137
0016
0017
0018
0019
0020
0021 #define NDISC_NODETYPE_UNSPEC 0
0022 #define NDISC_NODETYPE_HOST 1
0023 #define NDISC_NODETYPE_NODEFAULT 2
0024 #define NDISC_NODETYPE_DEFAULT 3
0025
0026
0027
0028
0029
0030 enum {
0031 __ND_OPT_PREFIX_INFO_END = 0,
0032 ND_OPT_SOURCE_LL_ADDR = 1,
0033 ND_OPT_TARGET_LL_ADDR = 2,
0034 ND_OPT_PREFIX_INFO = 3,
0035 ND_OPT_REDIRECT_HDR = 4,
0036 ND_OPT_MTU = 5,
0037 ND_OPT_NONCE = 14,
0038 __ND_OPT_ARRAY_MAX,
0039 ND_OPT_ROUTE_INFO = 24,
0040 ND_OPT_RDNSS = 25,
0041 ND_OPT_DNSSL = 31,
0042 ND_OPT_6CO = 34,
0043 ND_OPT_CAPTIVE_PORTAL = 37,
0044 ND_OPT_PREF64 = 38,
0045 __ND_OPT_MAX
0046 };
0047
0048 #define MAX_RTR_SOLICITATION_DELAY HZ
0049
0050 #define ND_REACHABLE_TIME (30*HZ)
0051 #define ND_RETRANS_TIMER HZ
0052
0053 #include <linux/compiler.h>
0054 #include <linux/icmpv6.h>
0055 #include <linux/in6.h>
0056 #include <linux/types.h>
0057 #include <linux/if_arp.h>
0058 #include <linux/netdevice.h>
0059 #include <linux/hash.h>
0060
0061 #include <net/neighbour.h>
0062
0063
0064 #define ND_DEBUG 1
0065
0066 #define ND_PRINTK(val, level, fmt, ...) \
0067 do { \
0068 if (val <= ND_DEBUG) \
0069 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
0070 } while (0)
0071
0072 struct ctl_table;
0073 struct inet6_dev;
0074 struct net_device;
0075 struct net_proto_family;
0076 struct sk_buff;
0077 struct prefix_info;
0078
0079 extern struct neigh_table nd_tbl;
0080
0081 struct nd_msg {
0082 struct icmp6hdr icmph;
0083 struct in6_addr target;
0084 __u8 opt[];
0085 };
0086
0087 struct rs_msg {
0088 struct icmp6hdr icmph;
0089 __u8 opt[];
0090 };
0091
0092 struct ra_msg {
0093 struct icmp6hdr icmph;
0094 __be32 reachable_time;
0095 __be32 retrans_timer;
0096 };
0097
0098 struct rd_msg {
0099 struct icmp6hdr icmph;
0100 struct in6_addr target;
0101 struct in6_addr dest;
0102 __u8 opt[];
0103 };
0104
0105 struct nd_opt_hdr {
0106 __u8 nd_opt_type;
0107 __u8 nd_opt_len;
0108 } __packed;
0109
0110
0111 struct ndisc_options {
0112 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
0113 #ifdef CONFIG_IPV6_ROUTE_INFO
0114 struct nd_opt_hdr *nd_opts_ri;
0115 struct nd_opt_hdr *nd_opts_ri_end;
0116 #endif
0117 struct nd_opt_hdr *nd_useropts;
0118 struct nd_opt_hdr *nd_useropts_end;
0119 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
0120 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
0121 #endif
0122 };
0123
0124 #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
0125 #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
0126 #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
0127 #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
0128 #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
0129 #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
0130 #define nd_opts_nonce nd_opt_array[ND_OPT_NONCE]
0131 #define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
0132 #define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
0133
0134 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
0135
0136 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
0137 u8 *opt, int opt_len,
0138 struct ndisc_options *ndopts);
0139
0140 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, const void *data,
0141 int data_len, int pad);
0142
0143 #define NDISC_OPS_REDIRECT_DATA_SPACE 2
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 struct ndisc_ops {
0203 int (*is_useropt)(u8 nd_opt_type);
0204 int (*parse_options)(const struct net_device *dev,
0205 struct nd_opt_hdr *nd_opt,
0206 struct ndisc_options *ndopts);
0207 void (*update)(const struct net_device *dev, struct neighbour *n,
0208 u32 flags, u8 icmp6_type,
0209 const struct ndisc_options *ndopts);
0210 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
0211 struct neighbour *neigh, u8 *ha_buf,
0212 u8 **ha);
0213 void (*fill_addr_option)(const struct net_device *dev,
0214 struct sk_buff *skb, u8 icmp6_type,
0215 const u8 *ha);
0216 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
0217 const struct prefix_info *pinfo,
0218 struct inet6_dev *in6_dev,
0219 struct in6_addr *addr,
0220 int addr_type, u32 addr_flags,
0221 bool sllao, bool tokenized,
0222 __u32 valid_lft, u32 prefered_lft,
0223 bool dev_addr_generated);
0224 };
0225
0226 #if IS_ENABLED(CONFIG_IPV6)
0227 static inline int ndisc_ops_is_useropt(const struct net_device *dev,
0228 u8 nd_opt_type)
0229 {
0230 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
0231 return dev->ndisc_ops->is_useropt(nd_opt_type);
0232 else
0233 return 0;
0234 }
0235
0236 static inline int ndisc_ops_parse_options(const struct net_device *dev,
0237 struct nd_opt_hdr *nd_opt,
0238 struct ndisc_options *ndopts)
0239 {
0240 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
0241 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
0242 else
0243 return 0;
0244 }
0245
0246 static inline void ndisc_ops_update(const struct net_device *dev,
0247 struct neighbour *n, u32 flags,
0248 u8 icmp6_type,
0249 const struct ndisc_options *ndopts)
0250 {
0251 if (dev->ndisc_ops && dev->ndisc_ops->update)
0252 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
0253 }
0254
0255 static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
0256 u8 icmp6_type)
0257 {
0258 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
0259 icmp6_type != NDISC_REDIRECT)
0260 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
0261 NULL, NULL);
0262 else
0263 return 0;
0264 }
0265
0266 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
0267 struct neighbour *neigh,
0268 u8 *ha_buf, u8 **ha)
0269 {
0270 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
0271 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
0272 neigh, ha_buf, ha);
0273 else
0274 return 0;
0275 }
0276
0277 static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
0278 struct sk_buff *skb,
0279 u8 icmp6_type)
0280 {
0281 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
0282 icmp6_type != NDISC_REDIRECT)
0283 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
0284 }
0285
0286 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
0287 struct sk_buff *skb,
0288 const u8 *ha)
0289 {
0290 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
0291 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
0292 }
0293
0294 static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
0295 struct net_device *dev,
0296 const struct prefix_info *pinfo,
0297 struct inet6_dev *in6_dev,
0298 struct in6_addr *addr,
0299 int addr_type, u32 addr_flags,
0300 bool sllao, bool tokenized,
0301 __u32 valid_lft,
0302 u32 prefered_lft,
0303 bool dev_addr_generated)
0304 {
0305 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
0306 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
0307 addr, addr_type,
0308 addr_flags, sllao,
0309 tokenized, valid_lft,
0310 prefered_lft,
0311 dev_addr_generated);
0312 }
0313 #endif
0314
0315
0316
0317
0318
0319
0320
0321 static inline int ndisc_addr_option_pad(unsigned short type)
0322 {
0323 switch (type) {
0324 case ARPHRD_INFINIBAND: return 2;
0325 default: return 0;
0326 }
0327 }
0328
0329 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
0330 {
0331 return NDISC_OPT_SPACE(addr_len + pad);
0332 }
0333
0334 #if IS_ENABLED(CONFIG_IPV6)
0335 static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
0336 {
0337 return __ndisc_opt_addr_space(dev->addr_len,
0338 ndisc_addr_option_pad(dev->type)) +
0339 ndisc_ops_opt_addr_space(dev, icmp6_type);
0340 }
0341
0342 static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
0343 struct neighbour *neigh,
0344 u8 *ops_data_buf,
0345 u8 **ops_data)
0346 {
0347 return __ndisc_opt_addr_space(dev->addr_len,
0348 ndisc_addr_option_pad(dev->type)) +
0349 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
0350 ops_data);
0351 }
0352 #endif
0353
0354 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
0355 unsigned char addr_len, int prepad)
0356 {
0357 u8 *lladdr = (u8 *)(p + 1);
0358 int lladdrlen = p->nd_opt_len << 3;
0359 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
0360 return NULL;
0361 return lladdr + prepad;
0362 }
0363
0364 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
0365 struct net_device *dev)
0366 {
0367 return __ndisc_opt_addr_data(p, dev->addr_len,
0368 ndisc_addr_option_pad(dev->type));
0369 }
0370
0371 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
0372 {
0373 const u32 *p32 = pkey;
0374
0375 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
0376 (p32[1] * hash_rnd[1]) +
0377 (p32[2] * hash_rnd[2]) +
0378 (p32[3] * hash_rnd[3]));
0379 }
0380
0381 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
0382 {
0383 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
0384 }
0385
0386 static inline
0387 struct neighbour *__ipv6_neigh_lookup_noref_stub(struct net_device *dev,
0388 const void *pkey)
0389 {
0390 return ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
0391 ndisc_hashfn, pkey, dev);
0392 }
0393
0394 static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
0395 {
0396 struct neighbour *n;
0397
0398 rcu_read_lock_bh();
0399 n = __ipv6_neigh_lookup_noref(dev, pkey);
0400 if (n && !refcount_inc_not_zero(&n->refcnt))
0401 n = NULL;
0402 rcu_read_unlock_bh();
0403
0404 return n;
0405 }
0406
0407 static inline void __ipv6_confirm_neigh(struct net_device *dev,
0408 const void *pkey)
0409 {
0410 struct neighbour *n;
0411
0412 rcu_read_lock_bh();
0413 n = __ipv6_neigh_lookup_noref(dev, pkey);
0414 neigh_confirm(n);
0415 rcu_read_unlock_bh();
0416 }
0417
0418 static inline void __ipv6_confirm_neigh_stub(struct net_device *dev,
0419 const void *pkey)
0420 {
0421 struct neighbour *n;
0422
0423 rcu_read_lock_bh();
0424 n = __ipv6_neigh_lookup_noref_stub(dev, pkey);
0425 neigh_confirm(n);
0426 rcu_read_unlock_bh();
0427 }
0428
0429
0430 static inline struct neighbour *ip_neigh_gw6(struct net_device *dev,
0431 const void *addr)
0432 {
0433 struct neighbour *neigh;
0434
0435 neigh = __ipv6_neigh_lookup_noref_stub(dev, addr);
0436 if (unlikely(!neigh))
0437 neigh = __neigh_create(ipv6_stub->nd_tbl, addr, dev, false);
0438
0439 return neigh;
0440 }
0441
0442 int ndisc_init(void);
0443 int ndisc_late_init(void);
0444
0445 void ndisc_late_cleanup(void);
0446 void ndisc_cleanup(void);
0447
0448 int ndisc_rcv(struct sk_buff *skb);
0449
0450 struct sk_buff *ndisc_ns_create(struct net_device *dev, const struct in6_addr *solicit,
0451 const struct in6_addr *saddr, u64 nonce);
0452 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
0453 const struct in6_addr *daddr, const struct in6_addr *saddr,
0454 u64 nonce);
0455
0456 void ndisc_send_skb(struct sk_buff *skb, const struct in6_addr *daddr,
0457 const struct in6_addr *saddr);
0458
0459 void ndisc_send_rs(struct net_device *dev,
0460 const struct in6_addr *saddr, const struct in6_addr *daddr);
0461 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
0462 const struct in6_addr *solicited_addr,
0463 bool router, bool solicited, bool override, bool inc_opt);
0464
0465 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
0466
0467 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
0468 int dir);
0469
0470 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
0471 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
0472 struct ndisc_options *ndopts);
0473
0474
0475
0476
0477 int igmp6_init(void);
0478 int igmp6_late_init(void);
0479
0480 void igmp6_cleanup(void);
0481 void igmp6_late_cleanup(void);
0482
0483 void igmp6_event_query(struct sk_buff *skb);
0484
0485 void igmp6_event_report(struct sk_buff *skb);
0486
0487
0488 #ifdef CONFIG_SYSCTL
0489 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
0490 void *buffer, size_t *lenp, loff_t *ppos);
0491 int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
0492 void __user *oldval, size_t __user *oldlenp,
0493 void __user *newval, size_t newlen);
0494 #endif
0495
0496 void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
0497
0498 #endif