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