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