| 1 |
|
| 2 | #ifndef __NET_DST_METADATA_H
|
| 3 | #define __NET_DST_METADATA_H 1
|
| 4 |
|
| 5 | #include <linux/skbuff.h>
|
| 6 | #include <net/ip.h>
|
| 7 | #include <net/ip_tunnels.h>
|
| 8 | #include <net/macsec.h>
|
| 9 | #include <net/dst.h>
|
| 10 |
|
| 11 | enum metadata_type {
|
| 12 | METADATA_IP_TUNNEL,
|
| 13 | METADATA_HW_PORT_MUX,
|
| 14 | METADATA_MACSEC,
|
| 15 | METADATA_XFRM,
|
| 16 | };
|
| 17 |
|
| 18 | struct hw_port_info {
|
| 19 | struct net_device *lower_dev;
|
| 20 | u32 port_id;
|
| 21 | };
|
| 22 |
|
| 23 | struct macsec_info {
|
| 24 | sci_t sci;
|
| 25 | };
|
| 26 |
|
| 27 | struct xfrm_md_info {
|
| 28 | u32 if_id;
|
| 29 | int link;
|
| 30 | struct dst_entry *dst_orig;
|
| 31 | };
|
| 32 |
|
| 33 | struct metadata_dst {
|
| 34 | struct dst_entry dst;
|
| 35 | enum metadata_type type;
|
| 36 | union {
|
| 37 | struct ip_tunnel_info tun_info;
|
| 38 | struct hw_port_info port_info;
|
| 39 | struct macsec_info macsec_info;
|
| 40 | struct xfrm_md_info xfrm_info;
|
| 41 | } u;
|
| 42 | };
|
| 43 |
|
| 44 | static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
|
| 45 | {
|
| 46 | struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
|
| 47 |
|
| 48 | if (md_dst && md_dst->dst.flags & DST_METADATA)
|
| 49 | return md_dst;
|
| 50 |
|
| 51 | return NULL;
|
| 52 | }
|
| 53 |
|
| 54 | static inline struct ip_tunnel_info *
|
| 55 | skb_tunnel_info(const struct sk_buff *skb)
|
| 56 | {
|
| 57 | struct metadata_dst *md_dst = skb_metadata_dst(skb);
|
| 58 | struct dst_entry *dst;
|
| 59 |
|
| 60 | if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
|
| 61 | return &md_dst->u.tun_info;
|
| 62 |
|
| 63 | dst = skb_dst(skb);
|
| 64 | if (dst && dst->lwtstate &&
|
| 65 | (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
|
| 66 | dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
|
| 67 | return lwt_tun_info(dst->lwtstate);
|
| 68 |
|
| 69 | return NULL;
|
| 70 | }
|
| 71 |
|
| 72 | static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
|
| 73 | {
|
| 74 | return (struct xfrm_md_info *)lwt->data;
|
| 75 | }
|
| 76 |
|
| 77 | static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
|
| 78 | {
|
| 79 | struct metadata_dst *md_dst = skb_metadata_dst(skb);
|
| 80 | struct dst_entry *dst;
|
| 81 |
|
| 82 | if (md_dst && md_dst->type == METADATA_XFRM)
|
| 83 | return &md_dst->u.xfrm_info;
|
| 84 |
|
| 85 | dst = skb_dst(skb);
|
| 86 | if (dst && dst->lwtstate &&
|
| 87 | dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
|
| 88 | return lwt_xfrm_info(dst->lwtstate);
|
| 89 |
|
| 90 | return NULL;
|
| 91 | }
|
| 92 |
|
| 93 | static inline bool skb_valid_dst(const struct sk_buff *skb)
|
| 94 | {
|
| 95 | struct dst_entry *dst = skb_dst(skb);
|
| 96 |
|
| 97 | return dst && !(dst->flags & DST_METADATA);
|
| 98 | }
|
| 99 |
|
| 100 | static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
|
| 101 | const struct sk_buff *skb_b)
|
| 102 | {
|
| 103 | const struct metadata_dst *a, *b;
|
| 104 |
|
| 105 | if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
|
| 106 | return 0;
|
| 107 |
|
| 108 | a = (const struct metadata_dst *) skb_dst(skb_a);
|
| 109 | b = (const struct metadata_dst *) skb_dst(skb_b);
|
| 110 |
|
| 111 | if (!a != !b || a->type != b->type)
|
| 112 | return 1;
|
| 113 |
|
| 114 | switch (a->type) {
|
| 115 | case METADATA_HW_PORT_MUX:
|
| 116 | return memcmp(&a->u.port_info, &b->u.port_info,
|
| 117 | sizeof(a->u.port_info));
|
| 118 | case METADATA_IP_TUNNEL:
|
| 119 | return memcmp(&a->u.tun_info, &b->u.tun_info,
|
| 120 | sizeof(a->u.tun_info) +
|
| 121 | a->u.tun_info.options_len);
|
| 122 | case METADATA_MACSEC:
|
| 123 | return memcmp(&a->u.macsec_info, &b->u.macsec_info,
|
| 124 | sizeof(a->u.macsec_info));
|
| 125 | case METADATA_XFRM:
|
| 126 | return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
|
| 127 | sizeof(a->u.xfrm_info));
|
| 128 | default:
|
| 129 | return 1;
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | void metadata_dst_free(struct metadata_dst *);
|
| 134 | struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
|
| 135 | gfp_t flags);
|
| 136 | void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
|
| 137 | struct metadata_dst __percpu *
|
| 138 | metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
|
| 139 |
|
| 140 | static inline struct metadata_dst *tun_rx_dst(int md_size)
|
| 141 | {
|
| 142 | struct metadata_dst *tun_dst;
|
| 143 |
|
| 144 | tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
|
| 145 | if (!tun_dst)
|
| 146 | return NULL;
|
| 147 |
|
| 148 | tun_dst->u.tun_info.options_len = 0;
|
| 149 | tun_dst->u.tun_info.mode = 0;
|
| 150 | return tun_dst;
|
| 151 | }
|
| 152 |
|
| 153 | static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
|
| 154 | {
|
| 155 | struct metadata_dst *md_dst = skb_metadata_dst(skb);
|
| 156 | int md_size;
|
| 157 | struct metadata_dst *new_md;
|
| 158 |
|
| 159 | if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
|
| 160 | return ERR_PTR(-EINVAL);
|
| 161 |
|
| 162 | md_size = md_dst->u.tun_info.options_len;
|
| 163 | new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
|
| 164 | if (!new_md)
|
| 165 | return ERR_PTR(-ENOMEM);
|
| 166 |
|
| 167 |
|
| 168 | new_md->u.tun_info = md_dst->u.tun_info;
|
| 169 | memcpy(ip_tunnel_info_opts(&new_md->u.tun_info),
|
| 170 | ip_tunnel_info_opts(&md_dst->u.tun_info), md_size);
|
| 171 |
|
| 172 | #ifdef CONFIG_DST_CACHE
|
| 173 |
|
| 174 | if (new_md->u.tun_info.dst_cache.cache) {
|
| 175 | int ret;
|
| 176 |
|
| 177 | ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
|
| 178 | if (ret) {
|
| 179 | metadata_dst_free(new_md);
|
| 180 | return ERR_PTR(ret);
|
| 181 | }
|
| 182 | }
|
| 183 | #endif
|
| 184 |
|
| 185 | skb_dst_drop(skb);
|
| 186 | skb_dst_set(skb, &new_md->dst);
|
| 187 | return new_md;
|
| 188 | }
|
| 189 |
|
| 190 | static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
|
| 191 | {
|
| 192 | struct metadata_dst *dst;
|
| 193 |
|
| 194 | dst = tun_dst_unclone(skb);
|
| 195 | if (IS_ERR(dst))
|
| 196 | return NULL;
|
| 197 |
|
| 198 | return &dst->u.tun_info;
|
| 199 | }
|
| 200 |
|
| 201 | static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
|
| 202 | __be32 daddr,
|
| 203 | __u8 tos, __u8 ttl,
|
| 204 | __be16 tp_dst,
|
| 205 | const unsigned long *flags,
|
| 206 | __be64 tunnel_id,
|
| 207 | int md_size)
|
| 208 | {
|
| 209 | struct metadata_dst *tun_dst;
|
| 210 |
|
| 211 | tun_dst = tun_rx_dst(md_size);
|
| 212 | if (!tun_dst)
|
| 213 | return NULL;
|
| 214 |
|
| 215 | ip_tunnel_key_init(&tun_dst->u.tun_info.key,
|
| 216 | saddr, daddr, tos, ttl,
|
| 217 | 0, 0, tp_dst, tunnel_id, flags);
|
| 218 | return tun_dst;
|
| 219 | }
|
| 220 |
|
| 221 | static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
|
| 222 | const unsigned long *flags,
|
| 223 | __be64 tunnel_id,
|
| 224 | int md_size)
|
| 225 | {
|
| 226 | const struct iphdr *iph = ip_hdr(skb);
|
| 227 | struct metadata_dst *tun_dst;
|
| 228 |
|
| 229 | tun_dst = __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
|
| 230 | 0, flags, tunnel_id, md_size);
|
| 231 |
|
| 232 | if (tun_dst && (iph->frag_off & htons(IP_DF)))
|
| 233 | __set_bit(IP_TUNNEL_DONT_FRAGMENT_BIT,
|
| 234 | tun_dst->u.tun_info.key.tun_flags);
|
| 235 | return tun_dst;
|
| 236 | }
|
| 237 |
|
| 238 | static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
|
| 239 | const struct in6_addr *daddr,
|
| 240 | __u8 tos, __u8 ttl,
|
| 241 | __be16 tp_dst,
|
| 242 | __be32 label,
|
| 243 | const unsigned long *flags,
|
| 244 | __be64 tunnel_id,
|
| 245 | int md_size)
|
| 246 | {
|
| 247 | struct metadata_dst *tun_dst;
|
| 248 | struct ip_tunnel_info *info;
|
| 249 |
|
| 250 | tun_dst = tun_rx_dst(md_size);
|
| 251 | if (!tun_dst)
|
| 252 | return NULL;
|
| 253 |
|
| 254 | info = &tun_dst->u.tun_info;
|
| 255 | info->mode = IP_TUNNEL_INFO_IPV6;
|
| 256 | ip_tunnel_flags_copy(info->key.tun_flags, flags);
|
| 257 | info->key.tun_id = tunnel_id;
|
| 258 | info->key.tp_src = 0;
|
| 259 | info->key.tp_dst = tp_dst;
|
| 260 |
|
| 261 | info->key.u.ipv6.src = *saddr;
|
| 262 | info->key.u.ipv6.dst = *daddr;
|
| 263 |
|
| 264 | info->key.tos = tos;
|
| 265 | info->key.ttl = ttl;
|
| 266 | info->key.label = label;
|
| 267 |
|
| 268 | return tun_dst;
|
| 269 | }
|
| 270 |
|
| 271 | static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
|
| 272 | const unsigned long *flags,
|
| 273 | __be64 tunnel_id,
|
| 274 | int md_size)
|
| 275 | {
|
| 276 | const struct ipv6hdr *ip6h = ipv6_hdr(skb);
|
| 277 |
|
| 278 | return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
|
| 279 | ipv6_get_dsfield(ip6h), ip6h->hop_limit,
|
| 280 | 0, ip6_flowlabel(ip6h), flags, tunnel_id,
|
| 281 | md_size);
|
| 282 | }
|
| 283 | #endif
|