xref: /openbmc/linux/net/ipv6/ila/ila_lwt.c (revision 01a6e126)
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 = (struct 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 	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 	}
92 
93 	skb_dst_set(skb, dst);
94 	return dst_output(net, sk, skb);
95 
96 drop:
97 	kfree_skb(skb);
98 	return err;
99 }
100 
101 static int ila_input(struct sk_buff *skb)
102 {
103 	struct dst_entry *dst = skb_dst(skb);
104 	struct ila_lwt *ilwt = ila_lwt_lwtunnel(dst->lwtstate);
105 
106 	if (skb->protocol != htons(ETH_P_IPV6))
107 		goto drop;
108 
109 	if (!ilwt->lwt_output)
110 		ila_update_ipv6_locator(skb,
111 					ila_params_lwtunnel(dst->lwtstate),
112 					false);
113 
114 	return dst->lwtstate->orig_input(skb);
115 
116 drop:
117 	kfree_skb(skb);
118 	return -EINVAL;
119 }
120 
121 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
122 	[ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
123 	[ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
124 	[ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
125 	[ILA_ATTR_HOOK_TYPE] = { .type = NLA_U8, },
126 };
127 
128 static int ila_build_state(struct nlattr *nla,
129 			   unsigned int family, const void *cfg,
130 			   struct lwtunnel_state **ts,
131 			   struct netlink_ext_ack *extack)
132 {
133 	struct ila_lwt *ilwt;
134 	struct ila_params *p;
135 	struct nlattr *tb[ILA_ATTR_MAX + 1];
136 	struct lwtunnel_state *newts;
137 	const struct fib6_config *cfg6 = cfg;
138 	struct ila_addr *iaddr;
139 	u8 ident_type = ILA_ATYPE_USE_FORMAT;
140 	u8 hook_type = ILA_HOOK_ROUTE_OUTPUT;
141 	u8 csum_mode = ILA_CSUM_NO_ACTION;
142 	bool lwt_output = true;
143 	u8 eff_ident_type;
144 	int ret;
145 
146 	if (family != AF_INET6)
147 		return -EINVAL;
148 
149 	ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla, ila_nl_policy, extack);
150 	if (ret < 0)
151 		return ret;
152 
153 	if (!tb[ILA_ATTR_LOCATOR])
154 		return -EINVAL;
155 
156 	iaddr = (struct ila_addr *)&cfg6->fc_dst;
157 
158 	if (tb[ILA_ATTR_IDENT_TYPE])
159 		ident_type = nla_get_u8(tb[ILA_ATTR_IDENT_TYPE]);
160 
161 	if (ident_type == ILA_ATYPE_USE_FORMAT) {
162 		/* Infer identifier type from type field in formatted
163 		 * identifier.
164 		 */
165 
166 		if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
167 			/* Need to have full locator and at least type field
168 			 * included in destination
169 			 */
170 			return -EINVAL;
171 		}
172 
173 		eff_ident_type = iaddr->ident.type;
174 	} else {
175 		eff_ident_type = ident_type;
176 	}
177 
178 	switch (eff_ident_type) {
179 	case ILA_ATYPE_IID:
180 		/* Don't allow ILA for IID type */
181 		return -EINVAL;
182 	case ILA_ATYPE_LUID:
183 		break;
184 	case ILA_ATYPE_VIRT_V4:
185 	case ILA_ATYPE_VIRT_UNI_V6:
186 	case ILA_ATYPE_VIRT_MULTI_V6:
187 	case ILA_ATYPE_NONLOCAL_ADDR:
188 		/* These ILA formats are not supported yet. */
189 	default:
190 		return -EINVAL;
191 	}
192 
193 	if (tb[ILA_ATTR_HOOK_TYPE])
194 		hook_type = nla_get_u8(tb[ILA_ATTR_HOOK_TYPE]);
195 
196 	switch (hook_type) {
197 	case ILA_HOOK_ROUTE_OUTPUT:
198 		lwt_output = true;
199 		break;
200 	case ILA_HOOK_ROUTE_INPUT:
201 		lwt_output = false;
202 		break;
203 	default:
204 		return -EINVAL;
205 	}
206 
207 	if (tb[ILA_ATTR_CSUM_MODE])
208 		csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
209 
210 	if (csum_mode == ILA_CSUM_NEUTRAL_MAP &&
211 	    ila_csum_neutral_set(iaddr->ident)) {
212 		/* Don't allow translation if checksum neutral bit is
213 		 * configured and it's set in the SIR address.
214 		 */
215 		return -EINVAL;
216 	}
217 
218 	newts = lwtunnel_state_alloc(sizeof(*ilwt));
219 	if (!newts)
220 		return -ENOMEM;
221 
222 	ilwt = ila_lwt_lwtunnel(newts);
223 	ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
224 	if (ret) {
225 		kfree(newts);
226 		return ret;
227 	}
228 
229 	ilwt->lwt_output = !!lwt_output;
230 
231 	p = ila_params_lwtunnel(newts);
232 
233 	p->csum_mode = csum_mode;
234 	p->ident_type = ident_type;
235 	p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
236 
237 	/* Precompute checksum difference for translation since we
238 	 * know both the old locator and the new one.
239 	 */
240 	p->locator_match = iaddr->loc;
241 
242 	ila_init_saved_csum(p);
243 
244 	newts->type = LWTUNNEL_ENCAP_ILA;
245 	newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
246 			LWTUNNEL_STATE_INPUT_REDIRECT;
247 
248 	if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
249 		ilwt->connected = 1;
250 
251 	*ts = newts;
252 
253 	return 0;
254 }
255 
256 static void ila_destroy_state(struct lwtunnel_state *lwt)
257 {
258 	dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
259 }
260 
261 static int ila_fill_encap_info(struct sk_buff *skb,
262 			       struct lwtunnel_state *lwtstate)
263 {
264 	struct ila_params *p = ila_params_lwtunnel(lwtstate);
265 	struct ila_lwt *ilwt = ila_lwt_lwtunnel(lwtstate);
266 
267 	if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
268 			      ILA_ATTR_PAD))
269 		goto nla_put_failure;
270 
271 	if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
272 		goto nla_put_failure;
273 
274 	if (nla_put_u8(skb, ILA_ATTR_IDENT_TYPE, (__force u8)p->ident_type))
275 		goto nla_put_failure;
276 
277 	if (nla_put_u8(skb, ILA_ATTR_HOOK_TYPE,
278 		       ilwt->lwt_output ? ILA_HOOK_ROUTE_OUTPUT :
279 					  ILA_HOOK_ROUTE_INPUT))
280 		goto nla_put_failure;
281 
282 	return 0;
283 
284 nla_put_failure:
285 	return -EMSGSIZE;
286 }
287 
288 static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
289 {
290 	return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
291 	       nla_total_size(sizeof(u8)) +        /* ILA_ATTR_CSUM_MODE */
292 	       nla_total_size(sizeof(u8)) +        /* ILA_ATTR_IDENT_TYPE */
293 	       nla_total_size(sizeof(u8)) +        /* ILA_ATTR_HOOK_TYPE */
294 	       0;
295 }
296 
297 static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
298 {
299 	struct ila_params *a_p = ila_params_lwtunnel(a);
300 	struct ila_params *b_p = ila_params_lwtunnel(b);
301 
302 	return (a_p->locator.v64 != b_p->locator.v64);
303 }
304 
305 static const struct lwtunnel_encap_ops ila_encap_ops = {
306 	.build_state = ila_build_state,
307 	.destroy_state = ila_destroy_state,
308 	.output = ila_output,
309 	.input = ila_input,
310 	.fill_encap = ila_fill_encap_info,
311 	.get_encap_size = ila_encap_nlsize,
312 	.cmp_encap = ila_encap_cmp,
313 	.owner = THIS_MODULE,
314 };
315 
316 int ila_lwt_init(void)
317 {
318 	return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
319 }
320 
321 void ila_lwt_fini(void)
322 {
323 	lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
324 }
325