~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/ila/ila_lwt.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/errno.h>
  3 #include <linux/ip.h>
  4 #include <linux/kernel.h>
  5 #include <linux/module.h>
  6 #include <linux/skbuff.h>
  7 #include <linux/socket.h>
  8 #include <linux/types.h>
  9 #include <net/checksum.h>
 10 #include <net/dst_cache.h>
 11 #include <net/ip.h>
 12 #include <net/ip6_fib.h>
 13 #include <net/ip6_route.h>
 14 #include <net/lwtunnel.h>
 15 #include <net/protocol.h>
 16 #include <uapi/linux/ila.h>
 17 #include "ila.h"
 18 
 19 struct ila_lwt {
 20         struct ila_params p;
 21         struct dst_cache dst_cache;
 22         u32 connected : 1;
 23         u32 lwt_output : 1;
 24 };
 25 
 26 static inline struct ila_lwt *ila_lwt_lwtunnel(
 27         struct lwtunnel_state *lwt)
 28 {
 29         return (struct ila_lwt *)lwt->data;
 30 }
 31 
 32 static inline struct ila_params *ila_params_lwtunnel(
 33         struct lwtunnel_state *lwt)
 34 {
 35         return &ila_lwt_lwtunnel(lwt)->p;
 36 }
 37 
 38 static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 39 {
 40         struct dst_entry *orig_dst = skb_dst(skb);
 41         struct rt6_info *rt = dst_rt6_info(orig_dst);
 42         struct ila_lwt *ilwt = ila_lwt_lwtunnel(orig_dst->lwtstate);
 43         struct dst_entry *dst;
 44         int err = -EINVAL;
 45 
 46         if (skb->protocol != htons(ETH_P_IPV6))
 47                 goto drop;
 48 
 49         if (ilwt->lwt_output)
 50                 ila_update_ipv6_locator(skb,
 51                                         ila_params_lwtunnel(orig_dst->lwtstate),
 52                                         true);
 53 
 54         if (rt->rt6i_flags & (RTF_GATEWAY | RTF_CACHE)) {
 55                 /* Already have a next hop address in route, no need for
 56                  * dest cache route.
 57                  */
 58                 return orig_dst->lwtstate->orig_output(net, sk, skb);
 59         }
 60 
 61         local_bh_disable();
 62         dst = dst_cache_get(&ilwt->dst_cache);
 63         local_bh_enable();
 64         if (unlikely(!dst)) {
 65                 struct ipv6hdr *ip6h = ipv6_hdr(skb);
 66                 struct flowi6 fl6;
 67 
 68                 /* Lookup a route for the new destination. Take into
 69                  * account that the base route may already have a gateway.
 70                  */
 71 
 72                 memset(&fl6, 0, sizeof(fl6));
 73                 fl6.flowi6_oif = orig_dst->dev->ifindex;
 74                 fl6.flowi6_iif = LOOPBACK_IFINDEX;
 75                 fl6.daddr = *rt6_nexthop(dst_rt6_info(orig_dst),
 76                                          &ip6h->daddr);
 77 
 78                 dst = ip6_route_output(net, NULL, &fl6);
 79                 if (dst->error) {
 80                         err = -EHOSTUNREACH;
 81                         dst_release(dst);
 82                         goto drop;
 83                 }
 84 
 85                 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
 86                 if (IS_ERR(dst)) {
 87                         err = PTR_ERR(dst);
 88                         goto drop;
 89                 }
 90 
 91                 if (ilwt->connected) {
 92                         local_bh_disable();
 93                         dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
 94                         local_bh_enable();
 95                 }
 96         }
 97 
 98         skb_dst_set(skb, dst);
 99         return dst_output(net, sk, skb);
100 
101 drop:
102         kfree_skb(skb);
103         return err;
104 }
105 
106 static int ila_input(struct sk_buff *skb)
107 {
108         struct dst_entry *dst = skb_dst(skb);
109         struct ila_lwt *ilwt = ila_lwt_lwtunnel(dst->lwtstate);
110 
111         if (skb->protocol != htons(ETH_P_IPV6))
112                 goto drop;
113 
114         if (!ilwt->lwt_output)
115                 ila_update_ipv6_locator(skb,
116                                         ila_params_lwtunnel(dst->lwtstate),
117                                         false);
118 
119         return dst->lwtstate->orig_input(skb);
120 
121 drop:
122         kfree_skb(skb);
123         return -EINVAL;
124 }
125 
126 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
127         [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
128         [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
129         [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
130         [ILA_ATTR_HOOK_TYPE] = { .type = NLA_U8, },
131 };
132 
133 static int ila_build_state(struct net *net, struct nlattr *nla,
134                            unsigned int family, const void *cfg,
135                            struct lwtunnel_state **ts,
136                            struct netlink_ext_ack *extack)
137 {
138         struct ila_lwt *ilwt;
139         struct ila_params *p;
140         struct nlattr *tb[ILA_ATTR_MAX + 1];
141         struct lwtunnel_state *newts;
142         const struct fib6_config *cfg6 = cfg;
143         struct ila_addr *iaddr;
144         u8 ident_type = ILA_ATYPE_USE_FORMAT;
145         u8 hook_type = ILA_HOOK_ROUTE_OUTPUT;
146         u8 csum_mode = ILA_CSUM_NO_ACTION;
147         bool lwt_output = true;
148         u8 eff_ident_type;
149         int ret;
150 
151         if (family != AF_INET6)
152                 return -EINVAL;
153 
154         ret = nla_parse_nested_deprecated(tb, ILA_ATTR_MAX, nla,
155                                           ila_nl_policy, extack);
156         if (ret < 0)
157                 return ret;
158 
159         if (!tb[ILA_ATTR_LOCATOR])
160                 return -EINVAL;
161 
162         iaddr = (struct ila_addr *)&cfg6->fc_dst;
163 
164         if (tb[ILA_ATTR_IDENT_TYPE])
165                 ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
166 
167         if (ident_type == ILA_ATYPE_USE_FORMAT) {
168                 /* Infer identifier type from type field in formatted
169                  * identifier.
170                  */
171 
172                 if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
173                         /* Need to have full locator and at least type field
174                          * included in destination
175                          */
176                         return -EINVAL;
177                 }
178 
179                 eff_ident_type = iaddr->ident.type;
180         } else {
181                 eff_ident_type = ident_type;
182         }
183 
184         switch (eff_ident_type) {
185         case ILA_ATYPE_IID:
186                 /* Don't allow ILA for IID type */
187                 return -EINVAL;
188         case ILA_ATYPE_LUID:
189                 break;
190         case ILA_ATYPE_VIRT_V4:
191         case ILA_ATYPE_VIRT_UNI_V6:
192         case ILA_ATYPE_VIRT_MULTI_V6:
193         case ILA_ATYPE_NONLOCAL_ADDR:
194                 /* These ILA formats are not supported yet. */
195         default:
196                 return -EINVAL;
197         }
198 
199         if (tb[ILA_ATTR_HOOK_TYPE])
200                 hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
201 
202         switch (hook_type) {
203         case ILA_HOOK_ROUTE_OUTPUT:
204                 lwt_output = true;
205                 break;
206         case ILA_HOOK_ROUTE_INPUT:
207                 lwt_output = false;
208                 break;
209         default:
210                 return -EINVAL;
211         }
212 
213         if (tb[ILA_ATTR_CSUM_MODE])
214                 csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
215 
216         if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
217             ila_csum_neutral_set(iaddr->ident)) {
218                 /* Don't allow translation if checksum neutral bit is
219                  * configured and it's set in the SIR address.
220                  */
221                 return -EINVAL;
222         }
223 
224         newts = lwtunnel_state_alloc(sizeof(*ilwt));
225         if (!newts)
226                 return -ENOMEM;
227 
228         ilwt = ila_lwt_lwtunnel(newts);
229         ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
230         if (ret) {
231                 kfree(newts);
232                 return ret;
233         }
234 
235         ilwt->lwt_output = !!lwt_output;
236 
237         p = ila_params_lwtunnel(newts);
238 
239         p->csum_mode = csum_mode;
240         p->ident_type = ident_type;
241         p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
242 
243         /* Precompute checksum difference for translation since we
244          * know both the old locator and the new one.
245          */
246         p->locator_match = iaddr->loc;
247 
248         ila_init_saved_csum(p);
249 
250         newts->type = LWTUNNEL_ENCAP_ILA;
251         newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
252                         LWTUNNEL_STATE_INPUT_REDIRECT;
253 
254         if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
255                 ilwt->connected = 1;
256 
257         *ts = newts;
258 
259         return 0;
260 }
261 
262 static void ila_destroy_state(struct lwtunnel_state *lwt)
263 {
264         dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
265 }
266 
267 static int ila_fill_encap_info(struct sk_buff *skb,
268                                struct lwtunnel_state *lwtstate)
269 {
270         struct ila_params *p = ila_params_lwtunnel(lwtstate);
271         struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
272 
273         if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
274                               ILA_ATTR_PAD))
275                 goto nla_put_failure;
276 
277         if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
278                 goto nla_put_failure;
279 
280         if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
281                 goto nla_put_failure;
282 
283         if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
284                        ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
285                                           ILA_HOOK_ROUTE_INPUT))
286                 goto nla_put_failure;
287 
288         return 0;
289 
290 nla_put_failure:
291         return -EMSGSIZE;
292 }
293 
294 static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
295 {
296         return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
297                nla_total_size(sizeof(u8)) +        /* ILA_ATTR_CSUM_MODE */
298                nla_total_size(sizeof(u8)) +        /* ILA_ATTR_IDENT_TYPE */
299                nla_total_size(sizeof(u8)) +        /* ILA_ATTR_HOOK_TYPE */
300                0;
301 }
302 
303 static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
304 {
305         struct ila_params *a_p = ila_params_lwtunnel(a);
306         struct ila_params *b_p = ila_params_lwtunnel(b);
307 
308         return (a_p->locator.v64 != b_p->locator.v64);
309 }
310 
311 static const struct lwtunnel_encap_ops ila_encap_ops = {
312         .build_state = ila_build_state,
313         .destroy_state = ila_destroy_state,
314         .output = ila_output,
315         .input = ila_input,
316         .fill_encap = ila_fill_encap_info,
317         .get_encap_size = ila_encap_nlsize,
318         .cmp_encap = ila_encap_cmp,
319         .owner = THIS_MODULE,
320 };
321 
322 int ila_lwt_init(void)
323 {
324         return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
325 }
326 
327 void ila_lwt_fini(void)
328 {
329         lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
330 }
331 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php