Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/errno.h>
0003 #include <linux/ip.h>
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/skbuff.h>
0007 #include <linux/socket.h>
0008 #include <linux/types.h>
0009 #include <net/checksum.h>
0010 #include <net/dst_cache.h>
0011 #include <net/ip.h>
0012 #include <net/ip6_fib.h>
0013 #include <net/ip6_route.h>
0014 #include <net/lwtunnel.h>
0015 #include <net/protocol.h>
0016 #include <uapi/linux/ila.h>
0017 #include "ila.h"
0018 
0019 struct ila_lwt {
0020     struct ila_params p;
0021     struct dst_cache dst_cache;
0022     u32 connected : 1;
0023     u32 lwt_output : 1;
0024 };
0025 
0026 static inline struct ila_lwt *ila_lwt_lwtunnel(
0027     struct lwtunnel_state *lwt)
0028 {
0029     return (struct ila_lwt *)lwt->data;
0030 }
0031 
0032 static inline struct ila_params *ila_params_lwtunnel(
0033     struct lwtunnel_state *lwt)
0034 {
0035     return &ila_lwt_lwtunnel(lwt)->p;
0036 }
0037 
0038 static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
0039 {
0040     struct dst_entry *orig_dst = skb_dst(skb);
0041     struct rt6_info *rt = (struct rt6_info *)orig_dst;
0042     struct ila_lwt *ilwt = ila_lwt_lwtunnel(orig_dst->lwtstate);
0043     struct dst_entry *dst;
0044     int err = -EINVAL;
0045 
0046     if (skb->protocol != htons(ETH_P_IPV6))
0047         goto drop;
0048 
0049     if (ilwt->lwt_output)
0050         ila_update_ipv6_locator(skb,
0051                     ila_params_lwtunnel(orig_dst->lwtstate),
0052                     true);
0053 
0054     if (rt->rt6i_flags & (RTF_GATEWAY | RTF_CACHE)) {
0055         /* Already have a next hop address in route, no need for
0056          * dest cache route.
0057          */
0058         return orig_dst->lwtstate->orig_output(net, sk, skb);
0059     }
0060 
0061     dst = dst_cache_get(&ilwt->dst_cache);
0062     if (unlikely(!dst)) {
0063         struct ipv6hdr *ip6h = ipv6_hdr(skb);
0064         struct flowi6 fl6;
0065 
0066         /* Lookup a route for the new destination. Take into
0067          * account that the base route may already have a gateway.
0068          */
0069 
0070         memset(&fl6, 0, sizeof(fl6));
0071         fl6.flowi6_oif = orig_dst->dev->ifindex;
0072         fl6.flowi6_iif = LOOPBACK_IFINDEX;
0073         fl6.daddr = *rt6_nexthop((struct rt6_info *)orig_dst,
0074                      &ip6h->daddr);
0075 
0076         dst = ip6_route_output(net, NULL, &fl6);
0077         if (dst->error) {
0078             err = -EHOSTUNREACH;
0079             dst_release(dst);
0080             goto drop;
0081         }
0082 
0083         dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
0084         if (IS_ERR(dst)) {
0085             err = PTR_ERR(dst);
0086             goto drop;
0087         }
0088 
0089         if (ilwt->connected)
0090             dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
0091     }
0092 
0093     skb_dst_set(skb, dst);
0094     return dst_output(net, sk, skb);
0095 
0096 drop:
0097     kfree_skb(skb);
0098     return err;
0099 }
0100 
0101 static int ila_input(struct sk_buff *skb)
0102 {
0103     struct dst_entry *dst = skb_dst(skb);
0104     struct ila_lwt *ilwt = ila_lwt_lwtunnel(dst->lwtstate);
0105 
0106     if (skb->protocol != htons(ETH_P_IPV6))
0107         goto drop;
0108 
0109     if (!ilwt->lwt_output)
0110         ila_update_ipv6_locator(skb,
0111                     ila_params_lwtunnel(dst->lwtstate),
0112                     false);
0113 
0114     return dst->lwtstate->orig_input(skb);
0115 
0116 drop:
0117     kfree_skb(skb);
0118     return -EINVAL;
0119 }
0120 
0121 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
0122     [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
0123     [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
0124     [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
0125     [ILA_ATTR_HOOK_TYPE] = { .type = NLA_U8, },
0126 };
0127 
0128 static int ila_build_state(struct net *net, struct nlattr *nla,
0129                unsigned int family, const void *cfg,
0130                struct lwtunnel_state **ts,
0131                struct netlink_ext_ack *extack)
0132 {
0133     struct ila_lwt *ilwt;
0134     struct ila_params *p;
0135     struct nlattr *tb[ILA_ATTR_MAX + 1];
0136     struct lwtunnel_state *newts;
0137     const struct fib6_config *cfg6 = cfg;
0138     struct ila_addr *iaddr;
0139     u8 ident_type = ILA_ATYPE_USE_FORMAT;
0140     u8 hook_type = ILA_HOOK_ROUTE_OUTPUT;
0141     u8 csum_mode = ILA_CSUM_NO_ACTION;
0142     bool lwt_output = true;
0143     u8 eff_ident_type;
0144     int ret;
0145 
0146     if (family != AF_INET6)
0147         return -EINVAL;
0148 
0149     ret = nla_parse_nested_deprecated(tb, ILA_ATTR_MAX, nla,
0150                       ila_nl_policy, extack);
0151     if (ret < 0)
0152         return ret;
0153 
0154     if (!tb[ILA_ATTR_LOCATOR])
0155         return -EINVAL;
0156 
0157     iaddr = (struct ila_addr *)&cfg6->fc_dst;
0158 
0159     if (tb[ILA_ATTR_IDENT_TYPE])
0160         ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
0161 
0162     if (ident_type == ILA_ATYPE_USE_FORMAT) {
0163         /* Infer identifier type from type field in formatted
0164          * identifier.
0165          */
0166 
0167         if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
0168             /* Need to have full locator and at least type field
0169              * included in destination
0170              */
0171             return -EINVAL;
0172         }
0173 
0174         eff_ident_type = iaddr->ident.type;
0175     } else {
0176         eff_ident_type = ident_type;
0177     }
0178 
0179     switch (eff_ident_type) {
0180     case ILA_ATYPE_IID:
0181         /* Don't allow ILA for IID type */
0182         return -EINVAL;
0183     case ILA_ATYPE_LUID:
0184         break;
0185     case ILA_ATYPE_VIRT_V4:
0186     case ILA_ATYPE_VIRT_UNI_V6:
0187     case ILA_ATYPE_VIRT_MULTI_V6:
0188     case ILA_ATYPE_NONLOCAL_ADDR:
0189         /* These ILA formats are not supported yet. */
0190     default:
0191         return -EINVAL;
0192     }
0193 
0194     if (tb[ILA_ATTR_HOOK_TYPE])
0195         hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
0196 
0197     switch (hook_type) {
0198     case ILA_HOOK_ROUTE_OUTPUT:
0199         lwt_output = true;
0200         break;
0201     case ILA_HOOK_ROUTE_INPUT:
0202         lwt_output = false;
0203         break;
0204     default:
0205         return -EINVAL;
0206     }
0207 
0208     if (tb[ILA_ATTR_CSUM_MODE])
0209         csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
0210 
0211     if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
0212         ila_csum_neutral_set(iaddr->ident)) {
0213         /* Don't allow translation if checksum neutral bit is
0214          * configured and it's set in the SIR address.
0215          */
0216         return -EINVAL;
0217     }
0218 
0219     newts = lwtunnel_state_alloc(sizeof(*ilwt));
0220     if (!newts)
0221         return -ENOMEM;
0222 
0223     ilwt = ila_lwt_lwtunnel(newts);
0224     ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
0225     if (ret) {
0226         kfree(newts);
0227         return ret;
0228     }
0229 
0230     ilwt->lwt_output = !!lwt_output;
0231 
0232     p = ila_params_lwtunnel(newts);
0233 
0234     p->csum_mode = csum_mode;
0235     p->ident_type = ident_type;
0236     p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
0237 
0238     /* Precompute checksum difference for translation since we
0239      * know both the old locator and the new one.
0240      */
0241     p->locator_match = iaddr->loc;
0242 
0243     ila_init_saved_csum(p);
0244 
0245     newts->type = LWTUNNEL_ENCAP_ILA;
0246     newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
0247             LWTUNNEL_STATE_INPUT_REDIRECT;
0248 
0249     if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
0250         ilwt->connected = 1;
0251 
0252     *ts = newts;
0253 
0254     return 0;
0255 }
0256 
0257 static void ila_destroy_state(struct lwtunnel_state *lwt)
0258 {
0259     dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
0260 }
0261 
0262 static int ila_fill_encap_info(struct sk_buff *skb,
0263                    struct lwtunnel_state *lwtstate)
0264 {
0265     struct ila_params *p = ila_params_lwtunnel(lwtstate);
0266     struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
0267 
0268     if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
0269                   ILA_ATTR_PAD))
0270         goto nla_put_failure;
0271 
0272     if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
0273         goto nla_put_failure;
0274 
0275     if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
0276         goto nla_put_failure;
0277 
0278     if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
0279                ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
0280                       ILA_HOOK_ROUTE_INPUT))
0281         goto nla_put_failure;
0282 
0283     return 0;
0284 
0285 nla_put_failure:
0286     return -EMSGSIZE;
0287 }
0288 
0289 static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
0290 {
0291     return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
0292            nla_total_size(sizeof(u8)) +        /* ILA_ATTR_CSUM_MODE */
0293            nla_total_size(sizeof(u8)) +        /* ILA_ATTR_IDENT_TYPE */
0294            nla_total_size(sizeof(u8)) +        /* ILA_ATTR_HOOK_TYPE */
0295            0;
0296 }
0297 
0298 static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
0299 {
0300     struct ila_params *a_p = ila_params_lwtunnel(a);
0301     struct ila_params *b_p = ila_params_lwtunnel(b);
0302 
0303     return (a_p->locator.v64 != b_p->locator.v64);
0304 }
0305 
0306 static const struct lwtunnel_encap_ops ila_encap_ops = {
0307     .build_state = ila_build_state,
0308     .destroy_state = ila_destroy_state,
0309     .output = ila_output,
0310     .input = ila_input,
0311     .fill_encap = ila_fill_encap_info,
0312     .get_encap_size = ila_encap_nlsize,
0313     .cmp_encap = ila_encap_cmp,
0314     .owner = THIS_MODULE,
0315 };
0316 
0317 int ila_lwt_init(void)
0318 {
0319     return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
0320 }
0321 
0322 void ila_lwt_fini(void)
0323 {
0324     lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
0325 }