mip6.c 10.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C)2003-2006 Helsinki University of Technology
 * Copyright (C)2003-2006 USAGI/WIDE Project
 */
/*
 * Authors:
 *	Noriaki TAKAMIYA @USAGI
 *	Masahide NAKAMURA @USAGI
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15
#include <linux/module.h>
#include <linux/skbuff.h>
16
#include <linux/time.h>
17
#include <linux/ipv6.h>
18 19
#include <linux/icmpv6.h>
#include <net/sock.h>
20
#include <net/ipv6.h>
21
#include <net/ip6_checksum.h>
22
#include <net/rawv6.h>
23 24 25
#include <net/xfrm.h>
#include <net/mip6.h>

26 27 28 29 30 31 32 33 34 35
static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
{
	return (n - len + 16) & 0x7;
}

static inline void *mip6_padn(__u8 *data, __u8 padlen)
{
	if (!data)
		return NULL;
	if (padlen == 1) {
36
		data[0] = IPV6_TLV_PAD1;
37
	} else if (padlen > 1) {
38
		data[0] = IPV6_TLV_PADN;
39 40 41 42 43 44 45
		data[1] = padlen - 2;
		if (padlen > 2)
			memset(data+2, 0, data[1]);
	}
	return data + padlen;
}

46
static inline void mip6_param_prob(struct sk_buff *skb, u8 code, int pos)
47
{
48
	icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos);
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
}

static int mip6_mh_len(int type)
{
	int len = 0;

	switch (type) {
	case IP6_MH_TYPE_BRR:
		len = 0;
		break;
	case IP6_MH_TYPE_HOTI:
	case IP6_MH_TYPE_COTI:
	case IP6_MH_TYPE_BU:
	case IP6_MH_TYPE_BACK:
		len = 1;
		break;
	case IP6_MH_TYPE_HOT:
	case IP6_MH_TYPE_COT:
	case IP6_MH_TYPE_BERROR:
		len = 2;
		break;
	}
	return len;
}

74
static int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
75
{
E
Eric Dumazet 已提交
76 77
	struct ip6_mh _hdr;
	const struct ip6_mh *mh;
78

E
Eric Dumazet 已提交
79 80 81
	mh = skb_header_pointer(skb, skb_transport_offset(skb),
				sizeof(_hdr), &_hdr);
	if (!mh)
82 83
		return -1;

E
Eric Dumazet 已提交
84 85
	if (((mh->ip6mh_hdrlen + 1) << 3) > skb->len)
		return -1;
86 87

	if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
88 89 90
		net_dbg_ratelimited("mip6: MH message too short: %d vs >=%d\n",
				    mh->ip6mh_hdrlen,
				    mip6_mh_len(mh->ip6mh_type));
E
Eric Dumazet 已提交
91 92
		mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_hdrlen) +
				skb_network_header_len(skb));
93 94 95 96
		return -1;
	}

	if (mh->ip6mh_proto != IPPROTO_NONE) {
97 98
		net_dbg_ratelimited("mip6: MH invalid payload proto = %d\n",
				    mh->ip6mh_proto);
E
Eric Dumazet 已提交
99 100
		mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_proto) +
				skb_network_header_len(skb));
101 102 103 104 105 106
		return -1;
	}

	return 0;
}

107 108
struct mip6_report_rate_limiter {
	spinlock_t lock;
109
	ktime_t stamp;
110 111 112 113 114 115
	int iif;
	struct in6_addr src;
	struct in6_addr dst;
};

static struct mip6_report_rate_limiter mip6_report_rl = {
116
	.lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock)
117 118
};

119 120
static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
{
121
	const struct ipv6hdr *iph = ipv6_hdr(skb);
122
	struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
123
	int err = destopt->nexthdr;
124

125
	spin_lock(&x->lock);
126 127
	if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
	    !ipv6_addr_any((struct in6_addr *)x->coaddr))
128 129
		err = -ENOENT;
	spin_unlock(&x->lock);
130

131
	return err;
132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

/* Destination Option Header is inserted.
 * IP Header's src address is replaced with Home Address Option in
 * Destination Option Header.
 */
static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
{
	struct ipv6hdr *iph;
	struct ipv6_destopt_hdr *dstopt;
	struct ipv6_destopt_hao *hao;
	u8 nexthdr;
	int len;

146
	skb_push(skb, -skb_network_offset(skb));
147
	iph = ipv6_hdr(skb);
148

149 150
	nexthdr = *skb_mac_header(skb);
	*skb_mac_header(skb) = IPPROTO_DSTOPTS;
151

152
	dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
153 154 155 156 157 158
	dstopt->nexthdr = nexthdr;

	hao = mip6_padn((char *)(dstopt + 1),
			calc_padlen(sizeof(*dstopt), 6));

	hao->type = IPV6_TLV_HAO;
159
	BUILD_BUG_ON(sizeof(*hao) != 18);
160 161 162 163 164
	hao->length = sizeof(*hao) - 2;

	len = ((char *)hao - (char *)dstopt) + sizeof(*hao);

	memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
165
	spin_lock_bh(&x->lock);
166
	memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
167
	spin_unlock_bh(&x->lock);
168

169
	WARN_ON(len != x->props.header_len);
170 171 172 173 174
	dstopt->hdrlen = (x->props.header_len >> 3) - 1;

	return 0;
}

175
static inline int mip6_report_rl_allow(ktime_t stamp,
176 177
				       const struct in6_addr *dst,
				       const struct in6_addr *src, int iif)
178 179 180 181
{
	int allow = 0;

	spin_lock_bh(&mip6_report_rl.lock);
T
Thomas Gleixner 已提交
182
	if (mip6_report_rl.stamp != stamp ||
183 184 185
	    mip6_report_rl.iif != iif ||
	    !ipv6_addr_equal(&mip6_report_rl.src, src) ||
	    !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
186
		mip6_report_rl.stamp = stamp;
187
		mip6_report_rl.iif = iif;
A
Alexey Dobriyan 已提交
188 189
		mip6_report_rl.src = *src;
		mip6_report_rl.dst = *dst;
190 191 192 193 194 195
		allow = 1;
	}
	spin_unlock_bh(&mip6_report_rl.lock);
	return allow;
}

196 197
static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb,
			       const struct flowi *fl)
198
{
199
	struct net *net = xs_net(x);
200
	struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
201
	const struct flowi6 *fl6 = &fl->u.ip6;
202 203 204
	struct ipv6_destopt_hao *hao = NULL;
	struct xfrm_selector sel;
	int offset;
205
	ktime_t stamp;
206 207
	int err = 0;

208
	if (unlikely(fl6->flowi6_proto == IPPROTO_MH &&
209
		     fl6->fl6_mh_type <= IP6_MH_TYPE_MAX))
210 211
		goto out;

212 213 214
	if (likely(opt->dsthao)) {
		offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
		if (likely(offset >= 0))
215 216
			hao = (struct ipv6_destopt_hao *)
					(skb_network_header(skb) + offset);
217 218
	}

219
	stamp = skb_get_ktime(skb);
220

221
	if (!mip6_report_rl_allow(stamp, &ipv6_hdr(skb)->daddr,
222
				  hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
223 224 225 226
				  opt->iif))
		goto out;

	memset(&sel, 0, sizeof(sel));
227
	memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
228 229
	       sizeof(sel.daddr));
	sel.prefixlen_d = 128;
230
	memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
231 232 233
	       sizeof(sel.saddr));
	sel.prefixlen_s = 128;
	sel.family = AF_INET6;
234 235
	sel.proto = fl6->flowi6_proto;
	sel.dport = xfrm_flowi_dport(fl, &fl6->uli);
236
	if (sel.dport)
A
Al Viro 已提交
237
		sel.dport_mask = htons(~0);
238
	sel.sport = xfrm_flowi_sport(fl, &fl6->uli);
239
	if (sel.sport)
A
Al Viro 已提交
240
		sel.sport_mask = htons(~0);
241
	sel.ifindex = fl6->flowi6_oif;
242

243
	err = km_report(net, IPPROTO_DSTOPTS, &sel,
244 245 246 247 248 249
			(hao ? (xfrm_address_t *)&hao->addr : NULL));

 out:
	return err;
}

250
static int mip6_destopt_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
251 252
{
	if (x->id.spi) {
253
		NL_SET_ERR_MSG(extack, "SPI must be 0");
254 255 256
		return -EINVAL;
	}
	if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
257
		NL_SET_ERR_MSG(extack, "XFRM mode must be XFRM_MODE_ROUTEOPTIMIZATION");
258 259 260 261 262 263
		return -EINVAL;
	}

	x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
		calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
		sizeof(struct ipv6_destopt_hao);
264
	WARN_ON(x->props.header_len != 24);
265 266 267 268 269 270 271 272 273 274 275 276

	return 0;
}

/*
 * Do nothing about destroying since it has no specific operation for
 * destination options header unlike IPsec protocols.
 */
static void mip6_destopt_destroy(struct xfrm_state *x)
{
}

277
static const struct xfrm_type mip6_destopt_type = {
278
	.owner		= THIS_MODULE,
279
	.proto		= IPPROTO_DSTOPTS,
280
	.flags		= XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_LOCAL_COADDR,
281 282 283 284
	.init_state	= mip6_destopt_init_state,
	.destructor	= mip6_destopt_destroy,
	.input		= mip6_destopt_input,
	.output		= mip6_destopt_output,
285
	.reject		= mip6_destopt_reject,
286 287
};

288 289
static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
{
290
	const struct ipv6hdr *iph = ipv6_hdr(skb);
291
	struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
292
	int err = rt2->rt_hdr.nexthdr;
293

294
	spin_lock(&x->lock);
295
	if (!ipv6_addr_equal(&iph->daddr, (struct in6_addr *)x->coaddr) &&
296
	    !ipv6_addr_any((struct in6_addr *)x->coaddr))
297 298
		err = -ENOENT;
	spin_unlock(&x->lock);
299

300
	return err;
301 302 303 304 305 306 307 308 309 310 311
}

/* Routing Header type 2 is inserted.
 * IP Header's dst address is replaced with Routing Header's Home Address.
 */
static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
{
	struct ipv6hdr *iph;
	struct rt2_hdr *rt2;
	u8 nexthdr;

312
	skb_push(skb, -skb_network_offset(skb));
313
	iph = ipv6_hdr(skb);
314

315 316
	nexthdr = *skb_mac_header(skb);
	*skb_mac_header(skb) = IPPROTO_ROUTING;
317

318
	rt2 = (struct rt2_hdr *)skb_transport_header(skb);
319 320 321 322 323 324
	rt2->rt_hdr.nexthdr = nexthdr;
	rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
	rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
	rt2->rt_hdr.segments_left = 1;
	memset(&rt2->reserved, 0, sizeof(rt2->reserved));

325
	WARN_ON(rt2->rt_hdr.hdrlen != 2);
326 327

	memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
328
	spin_lock_bh(&x->lock);
329
	memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
330
	spin_unlock_bh(&x->lock);
331 332 333 334

	return 0;
}

335
static int mip6_rthdr_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
336 337
{
	if (x->id.spi) {
338
		NL_SET_ERR_MSG(extack, "SPI must be 0");
339 340 341
		return -EINVAL;
	}
	if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
342
		NL_SET_ERR_MSG(extack, "XFRM mode must be XFRM_MODE_ROUTEOPTIMIZATION");
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		return -EINVAL;
	}

	x->props.header_len = sizeof(struct rt2_hdr);

	return 0;
}

/*
 * Do nothing about destroying since it has no specific operation for routing
 * header type 2 unlike IPsec protocols.
 */
static void mip6_rthdr_destroy(struct xfrm_state *x)
{
}

359
static const struct xfrm_type mip6_rthdr_type = {
360
	.owner		= THIS_MODULE,
361
	.proto		= IPPROTO_ROUTING,
362
	.flags		= XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_REMOTE_COADDR,
363 364 365 366 367 368
	.init_state	= mip6_rthdr_init_state,
	.destructor	= mip6_rthdr_destroy,
	.input		= mip6_rthdr_input,
	.output		= mip6_rthdr_output,
};

369
static int __init mip6_init(void)
370
{
371
	pr_info("Mobile IPv6\n");
372

373
	if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
374
		pr_info("%s: can't add xfrm type(destopt)\n", __func__);
375 376
		goto mip6_destopt_xfrm_fail;
	}
377
	if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
378
		pr_info("%s: can't add xfrm type(rthdr)\n", __func__);
379 380
		goto mip6_rthdr_xfrm_fail;
	}
381
	if (rawv6_mh_filter_register(mip6_mh_filter) < 0) {
382
		pr_info("%s: can't add rawv6 mh filter\n", __func__);
383 384 385 386
		goto mip6_rawv6_mh_fail;
	}


387 388
	return 0;

389 390
 mip6_rawv6_mh_fail:
	xfrm_unregister_type(&mip6_rthdr_type, AF_INET6);
391
 mip6_rthdr_xfrm_fail:
392 393
	xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
 mip6_destopt_xfrm_fail:
394 395 396
	return -EAGAIN;
}

397
static void __exit mip6_fini(void)
398
{
399
	if (rawv6_mh_filter_unregister(mip6_mh_filter) < 0)
400
		pr_info("%s: can't remove rawv6 mh filter\n", __func__);
401 402
	xfrm_unregister_type(&mip6_rthdr_type, AF_INET6);
	xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
403
}
404 405 406 407 408

module_init(mip6_init);
module_exit(mip6_fini);

MODULE_LICENSE("GPL");
409 410
MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_DSTOPTS);
MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ROUTING);