Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _NDISC_H
0003 #define _NDISC_H
0004 
0005 #include <net/ipv6_stubs.h>
0006 
0007 /*
0008  *  ICMP codes for neighbour discovery messages
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  * Router type: cross-layer information from link-layer to
0019  * IPv6 layer reported by certain link types (e.g., RFC4214).
0020  */
0021 #define NDISC_NODETYPE_UNSPEC       0   /* unspecified (default) */
0022 #define NDISC_NODETYPE_HOST     1   /* host or unauthorized router */
0023 #define NDISC_NODETYPE_NODEFAULT    2   /* non-default router */
0024 #define NDISC_NODETYPE_DEFAULT      3   /* default router */
0025 
0026 /*
0027  *  ndisc options
0028  */
0029 
0030 enum {
0031     __ND_OPT_PREFIX_INFO_END = 0,
0032     ND_OPT_SOURCE_LL_ADDR = 1,  /* RFC2461 */
0033     ND_OPT_TARGET_LL_ADDR = 2,  /* RFC2461 */
0034     ND_OPT_PREFIX_INFO = 3,     /* RFC2461 */
0035     ND_OPT_REDIRECT_HDR = 4,    /* RFC2461 */
0036     ND_OPT_MTU = 5,         /* RFC2461 */
0037     ND_OPT_NONCE = 14,              /* RFC7527 */
0038     __ND_OPT_ARRAY_MAX,
0039     ND_OPT_ROUTE_INFO = 24,     /* RFC4191 */
0040     ND_OPT_RDNSS = 25,      /* RFC5006 */
0041     ND_OPT_DNSSL = 31,      /* RFC6106 */
0042     ND_OPT_6CO = 34,        /* RFC6775 */
0043     ND_OPT_CAPTIVE_PORTAL = 37, /* RFC7710 */
0044     ND_OPT_PREF64 = 38,     /* RFC8781 */
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 /* Set to 3 to get tracing... */
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 /* ND options */
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  * This structure defines the hooks for IPv6 neighbour discovery.
0147  * The following hooks can be defined; unless noted otherwise, they are
0148  * optional and can be filled with a null pointer.
0149  *
0150  * int (*is_useropt)(u8 nd_opt_type):
0151  *     This function is called when IPv6 decide RA userspace options. if
0152  *     this function returns 1 then the option given by nd_opt_type will
0153  *     be handled as userspace option additional to the IPv6 options.
0154  *
0155  * int (*parse_options)(const struct net_device *dev,
0156  *          struct nd_opt_hdr *nd_opt,
0157  *          struct ndisc_options *ndopts):
0158  *     This function is called while parsing ndisc ops and put each position
0159  *     as pointer into ndopts. If this function return unequal 0, then this
0160  *     function took care about the ndisc option, if 0 then the IPv6 ndisc
0161  *     option parser will take care about that option.
0162  *
0163  * void (*update)(const struct net_device *dev, struct neighbour *n,
0164  *        u32 flags, u8 icmp6_type,
0165  *        const struct ndisc_options *ndopts):
0166  *     This function is called when IPv6 ndisc updates the neighbour cache
0167  *     entry. Additional options which can be updated may be previously
0168  *     parsed by parse_opts callback and accessible over ndopts parameter.
0169  *
0170  * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
0171  *           struct neighbour *neigh, u8 *ha_buf,
0172  *           u8 **ha):
0173  *     This function is called when the necessary option space will be
0174  *     calculated before allocating a skb. The parameters neigh, ha_buf
0175  *     abd ha are available on NDISC_REDIRECT messages only.
0176  *
0177  * void (*fill_addr_option)(const struct net_device *dev,
0178  *              struct sk_buff *skb, u8 icmp6_type,
0179  *              const u8 *ha):
0180  *     This function is called when the skb will finally fill the option
0181  *     fields inside skb. NOTE: this callback should fill the option
0182  *     fields to the skb which are previously indicated by opt_space
0183  *     parameter. That means the decision to add such option should
0184  *     not lost between these two callbacks, e.g. protected by interface
0185  *     up state.
0186  *
0187  * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
0188  *                 const struct prefix_info *pinfo,
0189  *                 struct inet6_dev *in6_dev,
0190  *                 struct in6_addr *addr,
0191  *                 int addr_type, u32 addr_flags,
0192  *                 bool sllao, bool tokenized,
0193  *                 __u32 valid_lft, u32 prefered_lft,
0194  *                 bool dev_addr_generated):
0195  *     This function is called when a RA messages is received with valid
0196  *     PIO option fields and an IPv6 address will be added to the interface
0197  *     for autoconfiguration. The parameter dev_addr_generated reports about
0198  *     if the address was based on dev->dev_addr or not. This can be used
0199  *     to add a second address if link-layer operates with two link layer
0200  *     addresses. E.g. 802.15.4 6LoWPAN.
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  * Return the padding between the option length and the start of the
0317  * link addr.  Currently only IP-over-InfiniBand needs this, although
0318  * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
0319  * also need a pad of 2.
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 /* uses ipv6_stub and is meant for use outside of IPv6 core */
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  *  IGMP
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