File Index
Files and directories
Sign in
FILE

ila_lwt.c

File preview with syntax highlighting.

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