linux/include/net/dst_metadata.h
<<
>>
Prefs
   1#ifndef __NET_DST_METADATA_H
   2#define __NET_DST_METADATA_H 1
   3
   4#include <linux/skbuff.h>
   5#ifndef __GENKSYMS__
   6#include <net/ip_tunnels.h>
   7#endif
   8#include <net/dst.h>
   9
  10enum metadata_type {
  11        METADATA_IP_TUNNEL,
  12        METADATA_HW_PORT_MUX,
  13};
  14
  15struct hw_port_info {
  16        struct net_device *lower_dev;
  17        u32 port_id;
  18};
  19
  20struct metadata_dst {
  21        struct dst_entry                dst;
  22        size_t                          opts_len;
  23        enum metadata_type              type;
  24        union {
  25                struct ip_tunnel_info   tun_info;
  26                struct hw_port_info     port_info;
  27        } u;
  28};
  29
  30static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
  31{
  32        struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
  33
  34        if (md_dst && md_dst->dst.flags & DST_METADATA)
  35                return md_dst;
  36
  37        return NULL;
  38}
  39
  40static inline struct ip_tunnel_info *
  41skb_tunnel_info(const struct sk_buff *skb)
  42{
  43        struct metadata_dst *md_dst = skb_metadata_dst(skb);
  44        struct dst_entry *dst;
  45
  46        if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
  47                return &md_dst->u.tun_info;
  48
  49        dst = skb_dst(skb);
  50        if (dst && dst->lwtstate)
  51                return lwt_tun_info(dst->lwtstate);
  52
  53        return NULL;
  54}
  55
  56static inline bool skb_valid_dst(const struct sk_buff *skb)
  57{
  58        struct dst_entry *dst = skb_dst(skb);
  59
  60        return dst && !(dst->flags & DST_METADATA);
  61}
  62
  63static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
  64                                       const struct sk_buff *skb_b)
  65{
  66        const struct metadata_dst *a, *b;
  67
  68        if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
  69                return 0;
  70
  71        a = (const struct metadata_dst *) skb_dst(skb_a);
  72        b = (const struct metadata_dst *) skb_dst(skb_b);
  73
  74        if (!a != !b || a->type != b->type)
  75                return 1;
  76
  77        switch (a->type) {
  78        case METADATA_HW_PORT_MUX:
  79                return memcmp(&a->u.port_info, &b->u.port_info,
  80                              sizeof(a->u.port_info));
  81        case METADATA_IP_TUNNEL:
  82                return memcmp(&a->u.tun_info, &b->u.tun_info,
  83                              sizeof(a->u.tun_info) +
  84                                         a->u.tun_info.options_len);
  85        default:
  86                return 1;
  87        }
  88}
  89
  90void metadata_dst_free(struct metadata_dst *);
  91struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
  92                                        gfp_t flags);
  93
  94static inline struct metadata_dst *tun_rx_dst(int md_size)
  95{
  96        struct metadata_dst *tun_dst;
  97
  98        tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
  99        if (!tun_dst)
 100                return NULL;
 101
 102        tun_dst->u.tun_info.options_len = 0;
 103        tun_dst->u.tun_info.mode = 0;
 104        return tun_dst;
 105}
 106
 107static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
 108{
 109        struct metadata_dst *md_dst = skb_metadata_dst(skb);
 110        int md_size;
 111        struct metadata_dst *new_md;
 112
 113        if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
 114                return ERR_PTR(-EINVAL);
 115
 116        md_size = md_dst->u.tun_info.options_len;
 117        new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
 118        if (!new_md)
 119                return ERR_PTR(-ENOMEM);
 120
 121        memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
 122               sizeof(struct ip_tunnel_info) + md_size);
 123        skb_dst_drop(skb);
 124        dst_hold(&new_md->dst);
 125        skb_dst_set(skb, &new_md->dst);
 126        return new_md;
 127}
 128
 129static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
 130{
 131        struct metadata_dst *dst;
 132
 133        dst = tun_dst_unclone(skb);
 134        if (IS_ERR(dst))
 135                return NULL;
 136
 137        return &dst->u.tun_info;
 138}
 139
 140static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
 141                                                    __be32 daddr,
 142                                                    __u8 tos, __u8 ttl,
 143                                                    __be16 tp_dst,
 144                                                    __be16 flags,
 145                                                    __be64 tunnel_id,
 146                                                    int md_size)
 147{
 148        struct metadata_dst *tun_dst;
 149
 150        tun_dst = tun_rx_dst(md_size);
 151        if (!tun_dst)
 152                return NULL;
 153
 154        ip_tunnel_key_init(&tun_dst->u.tun_info.key,
 155                           saddr, daddr, tos, ttl,
 156                           0, 0, tp_dst, tunnel_id, flags);
 157        return tun_dst;
 158}
 159
 160static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
 161                                                 __be16 flags,
 162                                                 __be64 tunnel_id,
 163                                                 int md_size)
 164{
 165        const struct iphdr *iph = ip_hdr(skb);
 166
 167        return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
 168                                0, flags, tunnel_id, md_size);
 169}
 170
 171static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
 172                                                      const struct in6_addr *daddr,
 173                                                      __u8 tos, __u8 ttl,
 174                                                      __be16 tp_dst,
 175                                                      __be32 label,
 176                                                      __be16 flags,
 177                                                      __be64 tunnel_id,
 178                                                      int md_size)
 179{
 180        struct metadata_dst *tun_dst;
 181        struct ip_tunnel_info *info;
 182
 183        tun_dst = tun_rx_dst(md_size);
 184        if (!tun_dst)
 185                return NULL;
 186
 187        info = &tun_dst->u.tun_info;
 188        info->mode = IP_TUNNEL_INFO_IPV6;
 189        info->key.tun_flags = flags;
 190        info->key.tun_id = tunnel_id;
 191        info->key.tp_src = 0;
 192        info->key.tp_dst = tp_dst;
 193
 194        info->key.u.ipv6.src = *saddr;
 195        info->key.u.ipv6.dst = *daddr;
 196
 197        info->key.tos = tos;
 198        info->key.ttl = ttl;
 199        info->key.label = label;
 200
 201        return tun_dst;
 202}
 203
 204static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
 205                                                   __be16 flags,
 206                                                   __be64 tunnel_id,
 207                                                   int md_size)
 208{
 209        const struct ipv6hdr *ip6h = ipv6_hdr(skb);
 210
 211        return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
 212                                  ipv6_get_dsfield(ip6h), ip6h->hop_limit,
 213                                  0, ip6_flowlabel(ip6h), flags, tunnel_id,
 214                                  md_size);
 215}
 216#endif /* __NET_DST_METADATA_H */
 217