0001
0002 #ifndef __NET_DST_METADATA_H
0003 #define __NET_DST_METADATA_H 1
0004
0005 #include <linux/skbuff.h>
0006 #include <net/ip_tunnels.h>
0007 #include <net/dst.h>
0008
0009 enum metadata_type {
0010 METADATA_IP_TUNNEL,
0011 METADATA_HW_PORT_MUX,
0012 };
0013
0014 struct hw_port_info {
0015 struct net_device *lower_dev;
0016 u32 port_id;
0017 };
0018
0019 struct metadata_dst {
0020 struct dst_entry dst;
0021 enum metadata_type type;
0022 union {
0023 struct ip_tunnel_info tun_info;
0024 struct hw_port_info port_info;
0025 } u;
0026 };
0027
0028 static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
0029 {
0030 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
0031
0032 if (md_dst && md_dst->dst.flags & DST_METADATA)
0033 return md_dst;
0034
0035 return NULL;
0036 }
0037
0038 static inline struct ip_tunnel_info *
0039 skb_tunnel_info(const struct sk_buff *skb)
0040 {
0041 struct metadata_dst *md_dst = skb_metadata_dst(skb);
0042 struct dst_entry *dst;
0043
0044 if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
0045 return &md_dst->u.tun_info;
0046
0047 dst = skb_dst(skb);
0048 if (dst && dst->lwtstate &&
0049 (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
0050 dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
0051 return lwt_tun_info(dst->lwtstate);
0052
0053 return NULL;
0054 }
0055
0056 static inline bool skb_valid_dst(const struct sk_buff *skb)
0057 {
0058 struct dst_entry *dst = skb_dst(skb);
0059
0060 return dst && !(dst->flags & DST_METADATA);
0061 }
0062
0063 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
0064 const struct sk_buff *skb_b)
0065 {
0066 const struct metadata_dst *a, *b;
0067
0068 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
0069 return 0;
0070
0071 a = (const struct metadata_dst *) skb_dst(skb_a);
0072 b = (const struct metadata_dst *) skb_dst(skb_b);
0073
0074 if (!a != !b || a->type != b->type)
0075 return 1;
0076
0077 switch (a->type) {
0078 case METADATA_HW_PORT_MUX:
0079 return memcmp(&a->u.port_info, &b->u.port_info,
0080 sizeof(a->u.port_info));
0081 case METADATA_IP_TUNNEL:
0082 return memcmp(&a->u.tun_info, &b->u.tun_info,
0083 sizeof(a->u.tun_info) +
0084 a->u.tun_info.options_len);
0085 default:
0086 return 1;
0087 }
0088 }
0089
0090 void metadata_dst_free(struct metadata_dst *);
0091 struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
0092 gfp_t flags);
0093 void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
0094 struct metadata_dst __percpu *
0095 metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
0096
0097 static inline struct metadata_dst *tun_rx_dst(int md_size)
0098 {
0099 struct metadata_dst *tun_dst;
0100
0101 tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
0102 if (!tun_dst)
0103 return NULL;
0104
0105 tun_dst->u.tun_info.options_len = 0;
0106 tun_dst->u.tun_info.mode = 0;
0107 return tun_dst;
0108 }
0109
0110 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
0111 {
0112 struct metadata_dst *md_dst = skb_metadata_dst(skb);
0113 int md_size;
0114 struct metadata_dst *new_md;
0115
0116 if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
0117 return ERR_PTR(-EINVAL);
0118
0119 md_size = md_dst->u.tun_info.options_len;
0120 new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
0121 if (!new_md)
0122 return ERR_PTR(-ENOMEM);
0123
0124 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
0125 sizeof(struct ip_tunnel_info) + md_size);
0126 #ifdef CONFIG_DST_CACHE
0127
0128 if (new_md->u.tun_info.dst_cache.cache) {
0129 int ret;
0130
0131 ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
0132 if (ret) {
0133 metadata_dst_free(new_md);
0134 return ERR_PTR(ret);
0135 }
0136 }
0137 #endif
0138
0139 skb_dst_drop(skb);
0140 skb_dst_set(skb, &new_md->dst);
0141 return new_md;
0142 }
0143
0144 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
0145 {
0146 struct metadata_dst *dst;
0147
0148 dst = tun_dst_unclone(skb);
0149 if (IS_ERR(dst))
0150 return NULL;
0151
0152 return &dst->u.tun_info;
0153 }
0154
0155 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
0156 __be32 daddr,
0157 __u8 tos, __u8 ttl,
0158 __be16 tp_dst,
0159 __be16 flags,
0160 __be64 tunnel_id,
0161 int md_size)
0162 {
0163 struct metadata_dst *tun_dst;
0164
0165 tun_dst = tun_rx_dst(md_size);
0166 if (!tun_dst)
0167 return NULL;
0168
0169 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
0170 saddr, daddr, tos, ttl,
0171 0, 0, tp_dst, tunnel_id, flags);
0172 return tun_dst;
0173 }
0174
0175 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
0176 __be16 flags,
0177 __be64 tunnel_id,
0178 int md_size)
0179 {
0180 const struct iphdr *iph = ip_hdr(skb);
0181
0182 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
0183 0, flags, tunnel_id, md_size);
0184 }
0185
0186 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
0187 const struct in6_addr *daddr,
0188 __u8 tos, __u8 ttl,
0189 __be16 tp_dst,
0190 __be32 label,
0191 __be16 flags,
0192 __be64 tunnel_id,
0193 int md_size)
0194 {
0195 struct metadata_dst *tun_dst;
0196 struct ip_tunnel_info *info;
0197
0198 tun_dst = tun_rx_dst(md_size);
0199 if (!tun_dst)
0200 return NULL;
0201
0202 info = &tun_dst->u.tun_info;
0203 info->mode = IP_TUNNEL_INFO_IPV6;
0204 info->key.tun_flags = flags;
0205 info->key.tun_id = tunnel_id;
0206 info->key.tp_src = 0;
0207 info->key.tp_dst = tp_dst;
0208
0209 info->key.u.ipv6.src = *saddr;
0210 info->key.u.ipv6.dst = *daddr;
0211
0212 info->key.tos = tos;
0213 info->key.ttl = ttl;
0214 info->key.label = label;
0215
0216 return tun_dst;
0217 }
0218
0219 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
0220 __be16 flags,
0221 __be64 tunnel_id,
0222 int md_size)
0223 {
0224 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
0225
0226 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
0227 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
0228 0, ip6_flowlabel(ip6h), flags, tunnel_id,
0229 md_size);
0230 }
0231 #endif