summaryrefslogtreecommitdiff
path: root/net/ipv6/rpl_iptunnel.c
blob: b1c028df686ed2e888c002d8fd16fef21d04b0fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Authors:
 * (C) 2020 Alexander Aring <alex.aring@gmail.com>
 */

#include <linux/rpl_iptunnel.h>

#include <net/dst_cache.h>
#include <net/ip6_route.h>
#include <net/lwtunnel.h>
#include <net/ipv6.h>
#include <net/rpl.h>

struct rpl_iptunnel_encap {
	DECLARE_FLEX_ARRAY(struct ipv6_rpl_sr_hdr, srh);
};

struct rpl_lwt {
	struct dst_cache cache;
	struct rpl_iptunnel_encap tuninfo;
};

static inline struct rpl_lwt *rpl_lwt_lwtunnel(struct lwtunnel_state *lwt)
{
	return (struct rpl_lwt *)lwt->data;
}

static inline struct rpl_iptunnel_encap *
rpl_encap_lwtunnel(struct lwtunnel_state *lwt)
{
	return &rpl_lwt_lwtunnel(lwt)->tuninfo;
}

static const struct nla_policy rpl_iptunnel_policy[RPL_IPTUNNEL_MAX + 1] = {
	[RPL_IPTUNNEL_SRH]	= { .type = NLA_BINARY },
};

static bool rpl_validate_srh(struct net *net, struct ipv6_rpl_sr_hdr *srh,
			     size_t seglen)
{
	int err;

	if ((srh->hdrlen << 3) != seglen)
		return false;

	/* check at least one segment and seglen fit with segments_left */
	if (!srh->segments_left ||
	    (srh->segments_left * sizeof(struct in6_addr)) != seglen)
		return false;

	if (srh->cmpri || srh->cmpre)
		return false;

	err = ipv6_chk_rpl_srh_loop(net, srh->rpl_segaddr,
				    srh->segments_left);
	if (err)
		return false;

	if (ipv6_addr_type(&srh->rpl_segaddr[srh->segments_left - 1]) &
	    IPV6_ADDR_MULTICAST)
		return false;

	return true;
}

static int rpl_build_state(struct net *net, struct nlattr *nla,
			   unsigned int family, const void *cfg,
			   struct lwtunnel_state **ts,
			   struct netlink_ext_ack *extack)
{
	struct nlattr *tb[RPL_IPTUNNEL_MAX + 1];
	struct lwtunnel_state *newts;
	struct ipv6_rpl_sr_hdr *srh;
	struct rpl_lwt *rlwt;
	int err, srh_len;

	if (family != AF_INET6)
		return -EINVAL;

	err = nla_parse_nested(tb, RPL_IPTUNNEL_MAX, nla,
			       rpl_iptunnel_policy, extack);
	if (err < 0)
		return err;

	if (!tb[RPL_IPTUNNEL_SRH])
		return -EINVAL;

	srh = nla_data(tb[RPL_IPTUNNEL_SRH]);
	srh_len = nla_len(tb[RPL_IPTUNNEL_SRH]);

	if (srh_len < sizeof(*srh))
		return -EINVAL;

	/* verify that SRH is consistent */
	if (!rpl_validate_srh(net, srh, srh_len - sizeof(*srh)))
		return -EINVAL;

	newts = lwtunnel_state_alloc(srh_len + sizeof(*rlwt));
	if (!newts)
		return -ENOMEM;

	rlwt = rpl_lwt_lwtunnel(newts);

	err = dst_cache_init(&rlwt->cache, GFP_ATOMIC);
	if (err) {
		kfree(newts);
		return err;
	}

	memcpy(&rlwt->tuninfo.srh, srh, srh_len);

	newts->type = LWTUNNEL_ENCAP_RPL;
	newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
	newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;

	*ts = newts;

	return 0;
}

static void rpl_destroy_state(struct lwtunnel_state *lwt)
{
	dst_cache_destroy(&rpl_lwt_lwtunnel(lwt)->cache);
}

static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rlwt,
			     const struct ipv6_rpl_sr_hdr *srh)
{
	struct ipv6_rpl_sr_hdr *isrh, *csrh;
	const struct ipv6hdr *oldhdr;
	struct ipv6hdr *hdr;
	unsigned char *buf;
	size_t hdrlen;
	int err;

	oldhdr = ipv6_hdr(skb);

	buf = kcalloc(struct_size(srh, segments.addr, srh->segments_left), 2, GFP_ATOMIC);
	if (!buf)
		return -ENOMEM;

	isrh = (struct ipv6_rpl_sr_hdr *)buf;
	csrh = (struct ipv6_rpl_sr_hdr *)(buf + ((srh->hdrlen + 1) << 3));

	memcpy(isrh, srh, sizeof(*isrh));
	memcpy(isrh->rpl_segaddr, &srh->rpl_segaddr[1],
	       (srh->segments_left - 1) * 16);
	isrh->rpl_segaddr[srh->segments_left - 1] = oldhdr->daddr;

	ipv6_rpl_srh_compress(csrh, isrh, &srh->rpl_segaddr[0],
			      isrh->segments_left - 1);

	hdrlen = ((csrh->hdrlen + 1) << 3);

	err = skb_cow_head(skb, hdrlen + skb->mac_len);
	if (unlikely(err)) {
		kfree(buf);
		return err;
	}

	skb_pull(skb, sizeof(struct ipv6hdr));
	skb_postpull_rcsum(skb, skb_network_header(skb),
			   sizeof(struct ipv6hdr));

	skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
	skb_reset_network_header(skb);
	skb_mac_header_rebuild(skb);

	hdr = ipv6_hdr(skb);
	memmove(hdr, oldhdr, sizeof(*hdr));
	isrh = (void *)hdr + sizeof(*hdr);
	memcpy(isrh, csrh, hdrlen);

	isrh->nexthdr = hdr->nexthdr;
	hdr->nexthdr = NEXTHDR_ROUTING;
	hdr->daddr = srh->rpl_segaddr[0];

	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
	skb_set_transport_header(skb, sizeof(struct ipv6hdr));

	skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);

	kfree(buf);

	return 0;
}

static int rpl_do_srh(struct sk_buff *skb, const struct rpl_lwt *rlwt)
{
	struct dst_entry *dst = skb_dst(skb);
	struct rpl_iptunnel_encap *tinfo;

	if (skb->protocol != htons(ETH_P_IPV6))
		return -EINVAL;

	tinfo = rpl_encap_lwtunnel(dst->lwtstate);

	return rpl_do_srh_inline(skb, rlwt, tinfo->srh);
}

static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)
{
	struct dst_entry *orig_dst = skb_dst(skb);
	struct dst_entry *dst = NULL;
	struct rpl_lwt *rlwt;
	int err;

	rlwt = rpl_lwt_lwtunnel(orig_dst->lwtstate);

	err = rpl_do_srh(skb, rlwt);
	if (unlikely(err))
		goto drop;

	preempt_disable();
	dst = dst_cache_get(&rlwt->cache);
	preempt_enable();

	if (unlikely(!dst)) {
		struct ipv6hdr *hdr = ipv6_hdr(skb);
		struct flowi6 fl6;

		memset(&fl6, 0, sizeof(fl6));
		fl6.daddr = hdr->daddr;
		fl6.saddr = hdr->saddr;
		fl6.flowlabel = ip6_flowinfo(hdr);
		fl6.flowi6_mark = skb->mark;
		fl6.flowi6_proto = hdr->nexthdr;

		dst = ip6_route_output(net, NULL, &fl6);
		if (dst->error) {
			err = dst->error;
			dst_release(dst);
			goto drop;
		}

		preempt_disable();
		dst_cache_set_ip6(&rlwt->cache, dst, &fl6.saddr);
		preempt_enable();
	}

	skb_dst_drop(skb);
	skb_dst_set(skb, dst);

	err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
	if (unlikely(err))
		goto drop;

	return dst_output(net, sk, skb);

drop:
	kfree_skb(skb);
	return err;
}

static int rpl_input(struct sk_buff *skb)
{
	struct dst_entry *orig_dst = skb_dst(skb);
	struct dst_entry *dst = NULL;
	struct rpl_lwt *rlwt;
	int err;

	rlwt = rpl_lwt_lwtunnel(orig_dst->lwtstate);

	err = rpl_do_srh(skb, rlwt);
	if (unlikely(err)) {
		kfree_skb(skb);
		return err;
	}

	preempt_disable();
	dst = dst_cache_get(&rlwt->cache);
	preempt_enable();

	skb_dst_drop(skb);

	if (!dst) {
		ip6_route_input(skb);
		dst = skb_dst(skb);
		if (!dst->error) {
			preempt_disable();
			dst_cache_set_ip6(&rlwt->cache, dst,
					  &ipv6_hdr(skb)->saddr);
			preempt_enable();
		}
	} else {
		skb_dst_set(skb, dst);
	}

	err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
	if (unlikely(err))
		return err;

	return dst_input(skb);
}

static int nla_put_rpl_srh(struct sk_buff *skb, int attrtype,
			   struct rpl_iptunnel_encap *tuninfo)
{
	struct rpl_iptunnel_encap *data;
	struct nlattr *nla;
	int len;

	len = RPL_IPTUNNEL_SRH_SIZE(tuninfo->srh);

	nla = nla_reserve(skb, attrtype, len);
	if (!nla)
		return -EMSGSIZE;

	data = nla_data(nla);
	memcpy(data, tuninfo->srh, len);

	return 0;
}

static int rpl_fill_encap_info(struct sk_buff *skb,
			       struct lwtunnel_state *lwtstate)
{
	struct rpl_iptunnel_encap *tuninfo = rpl_encap_lwtunnel(lwtstate);

	if (nla_put_rpl_srh(skb, RPL_IPTUNNEL_SRH, tuninfo))
		return -EMSGSIZE;

	return 0;
}

static int rpl_encap_nlsize(struct lwtunnel_state *lwtstate)
{
	struct rpl_iptunnel_encap *tuninfo = rpl_encap_lwtunnel(lwtstate);

	return nla_total_size(RPL_IPTUNNEL_SRH_SIZE(tuninfo->srh));
}

static int rpl_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
{
	struct rpl_iptunnel_encap *a_hdr = rpl_encap_lwtunnel(a);
	struct rpl_iptunnel_encap *b_hdr = rpl_encap_lwtunnel(b);
	int len = RPL_IPTUNNEL_SRH_SIZE(a_hdr->srh);

	if (len != RPL_IPTUNNEL_SRH_SIZE(b_hdr->srh))
		return 1;

	return memcmp(a_hdr, b_hdr, len);
}

static const struct lwtunnel_encap_ops rpl_ops = {
	.build_state	= rpl_build_state,
	.destroy_state	= rpl_destroy_state,
	.output		= rpl_output,
	.input		= rpl_input,
	.fill_encap	= rpl_fill_encap_info,
	.get_encap_size	= rpl_encap_nlsize,
	.cmp_encap	= rpl_encap_cmp,
	.owner		= THIS_MODULE,
};

int __init rpl_init(void)
{
	int err;

	err = lwtunnel_encap_add_ops(&rpl_ops, LWTUNNEL_ENCAP_RPL);
	if (err)
		goto out;

	pr_info("RPL Segment Routing with IPv6\n");

	return 0;

out:
	return err;
}

void rpl_exit(void)
{
	lwtunnel_encap_del_ops(&rpl_ops, LWTUNNEL_ENCAP_RPL);
}