1/*
2 * Copyright (C)2003-2006 Helsinki University of Technology
3 * Copyright (C)2003-2006 USAGI/WIDE Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19/*
20 * Authors:
21 *	Noriaki TAKAMIYA @USAGI
22 *	Masahide NAKAMURA @USAGI
23 */
24
25#include <linux/module.h>
26#include <linux/skbuff.h>
27#include <linux/time.h>
28#include <linux/ipv6.h>
29#include <linux/icmpv6.h>
30#include <net/sock.h>
31#include <net/ipv6.h>
32#include <net/ip6_checksum.h>
33#include <net/xfrm.h>
34#include <net/mip6.h>
35
36static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
37{
38	return x->coaddr;
39}
40
41static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
42{
43	return (n - len + 16) & 0x7;
44}
45
46static inline void *mip6_padn(__u8 *data, __u8 padlen)
47{
48	if (!data)
49		return NULL;
50	if (padlen == 1) {
51		data[0] = MIP6_OPT_PAD_1;
52	} else if (padlen > 1) {
53		data[0] = MIP6_OPT_PAD_N;
54		data[1] = padlen - 2;
55		if (padlen > 2)
56			memset(data+2, 0, data[1]);
57	}
58	return data + padlen;
59}
60
61static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
62{
63	icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
64}
65
66static int mip6_mh_len(int type)
67{
68	int len = 0;
69
70	switch (type) {
71	case IP6_MH_TYPE_BRR:
72		len = 0;
73		break;
74	case IP6_MH_TYPE_HOTI:
75	case IP6_MH_TYPE_COTI:
76	case IP6_MH_TYPE_BU:
77	case IP6_MH_TYPE_BACK:
78		len = 1;
79		break;
80	case IP6_MH_TYPE_HOT:
81	case IP6_MH_TYPE_COT:
82	case IP6_MH_TYPE_BERROR:
83		len = 2;
84		break;
85	}
86	return len;
87}
88
89int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
90{
91	struct ip6_mh *mh;
92
93	if (!pskb_may_pull(skb, (skb_transport_offset(skb)) + 8) ||
94	    !pskb_may_pull(skb, (skb_transport_offset(skb) +
95				 ((skb_transport_header(skb)[1] + 1) << 3))))
96		return -1;
97
98	mh = (struct ip6_mh *)skb_transport_header(skb);
99
100	if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
101		LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
102			       mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
103		mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
104					 skb_network_header(skb)));
105		return -1;
106	}
107
108	if (mh->ip6mh_proto != IPPROTO_NONE) {
109		LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
110			       mh->ip6mh_proto);
111		mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
112					 skb_network_header(skb)));
113		return -1;
114	}
115
116	return 0;
117}
118
119struct mip6_report_rate_limiter {
120	spinlock_t lock;
121	struct timeval stamp;
122	int iif;
123	struct in6_addr src;
124	struct in6_addr dst;
125};
126
127static struct mip6_report_rate_limiter mip6_report_rl = {
128	.lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock)
129};
130
131static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
132{
133	struct ipv6hdr *iph = ipv6_hdr(skb);
134	struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
135
136	if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
137	    !ipv6_addr_any((struct in6_addr *)x->coaddr))
138		return -ENOENT;
139
140	return destopt->nexthdr;
141}
142
143/* Destination Option Header is inserted.
144 * IP Header's src address is replaced with Home Address Option in
145 * Destination Option Header.
146 */
147static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
148{
149	struct ipv6hdr *iph;
150	struct ipv6_destopt_hdr *dstopt;
151	struct ipv6_destopt_hao *hao;
152	u8 nexthdr;
153	int len;
154
155	iph = (struct ipv6hdr *)skb->data;
156	iph->payload_len = htons(skb->len - sizeof(*iph));
157
158	nexthdr = *skb_network_header(skb);
159	*skb_network_header(skb) = IPPROTO_DSTOPTS;
160
161	dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
162	dstopt->nexthdr = nexthdr;
163
164	hao = mip6_padn((char *)(dstopt + 1),
165			calc_padlen(sizeof(*dstopt), 6));
166
167	hao->type = IPV6_TLV_HAO;
168	hao->length = sizeof(*hao) - 2;
169	BUG_TRAP(hao->length == 16);
170
171	len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
172
173	memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
174	memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
175
176	BUG_TRAP(len == x->props.header_len);
177	dstopt->hdrlen = (x->props.header_len >> 3) - 1;
178
179	return 0;
180}
181
182static inline int mip6_report_rl_allow(struct timeval *stamp,
183				       struct in6_addr *dst,
184				       struct in6_addr *src, int iif)
185{
186	int allow = 0;
187
188	spin_lock_bh(&mip6_report_rl.lock);
189	if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
190	    mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
191	    mip6_report_rl.iif != iif ||
192	    !ipv6_addr_equal(&mip6_report_rl.src, src) ||
193	    !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
194		mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
195		mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
196		mip6_report_rl.iif = iif;
197		ipv6_addr_copy(&mip6_report_rl.src, src);
198		ipv6_addr_copy(&mip6_report_rl.dst, dst);
199		allow = 1;
200	}
201	spin_unlock_bh(&mip6_report_rl.lock);
202	return allow;
203}
204
205static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
206{
207	struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
208	struct ipv6_destopt_hao *hao = NULL;
209	struct xfrm_selector sel;
210	int offset;
211	struct timeval stamp;
212	int err = 0;
213
214	if (unlikely(fl->proto == IPPROTO_MH &&
215		     fl->fl_mh_type <= IP6_MH_TYPE_MAX))
216		goto out;
217
218	if (likely(opt->dsthao)) {
219		offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
220		if (likely(offset >= 0))
221			hao = (struct ipv6_destopt_hao *)
222					(skb_network_header(skb) + offset);
223	}
224
225	skb_get_timestamp(skb, &stamp);
226
227	if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
228				  hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
229				  opt->iif))
230		goto out;
231
232	memset(&sel, 0, sizeof(sel));
233	memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
234	       sizeof(sel.daddr));
235	sel.prefixlen_d = 128;
236	memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
237	       sizeof(sel.saddr));
238	sel.prefixlen_s = 128;
239	sel.family = AF_INET6;
240	sel.proto = fl->proto;
241	sel.dport = xfrm_flowi_dport(fl);
242	if (sel.dport)
243		sel.dport_mask = htons(~0);
244	sel.sport = xfrm_flowi_sport(fl);
245	if (sel.sport)
246		sel.sport_mask = htons(~0);
247	sel.ifindex = fl->oif;
248
249	err = km_report(IPPROTO_DSTOPTS, &sel,
250			(hao ? (xfrm_address_t *)&hao->addr : NULL));
251
252 out:
253	return err;
254}
255
256static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
257			       u8 **nexthdr)
258{
259	u16 offset = sizeof(struct ipv6hdr);
260	struct ipv6_opt_hdr *exthdr =
261				   (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
262	const unsigned char *nh = skb_network_header(skb);
263	unsigned int packet_len = skb->tail - skb->network_header;
264	int found_rhdr = 0;
265
266	*nexthdr = &ipv6_hdr(skb)->nexthdr;
267
268	while (offset + 1 <= packet_len) {
269
270		switch (**nexthdr) {
271		case NEXTHDR_HOP:
272			break;
273		case NEXTHDR_ROUTING:
274			found_rhdr = 1;
275			break;
276		case NEXTHDR_DEST:
277			if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
278				LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
279				return offset;
280			}
281
282			if (found_rhdr)
283				return offset;
284
285			break;
286		default:
287			return offset;
288		}
289
290		offset += ipv6_optlen(exthdr);
291		*nexthdr = &exthdr->nexthdr;
292		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
293	}
294
295	return offset;
296}
297
298static int mip6_destopt_init_state(struct xfrm_state *x)
299{
300	if (x->id.spi) {
301		printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
302		       x->id.spi);
303		return -EINVAL;
304	}
305	if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
306		printk(KERN_INFO "%s: state's mode is not %u: %u\n",
307		       __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
308		return -EINVAL;
309	}
310
311	x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
312		calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
313		sizeof(struct ipv6_destopt_hao);
314	BUG_TRAP(x->props.header_len == 24);
315
316	return 0;
317}
318
319/*
320 * Do nothing about destroying since it has no specific operation for
321 * destination options header unlike IPsec protocols.
322 */
323static void mip6_destopt_destroy(struct xfrm_state *x)
324{
325}
326
327static struct xfrm_type mip6_destopt_type =
328{
329	.description	= "MIP6DESTOPT",
330	.owner		= THIS_MODULE,
331	.proto	     	= IPPROTO_DSTOPTS,
332	.flags		= XFRM_TYPE_NON_FRAGMENT,
333	.init_state	= mip6_destopt_init_state,
334	.destructor	= mip6_destopt_destroy,
335	.input		= mip6_destopt_input,
336	.output		= mip6_destopt_output,
337	.reject		= mip6_destopt_reject,
338	.hdr_offset	= mip6_destopt_offset,
339	.local_addr	= mip6_xfrm_addr,
340};
341
342static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
343{
344	struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
345
346	if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
347	    !ipv6_addr_any((struct in6_addr *)x->coaddr))
348		return -ENOENT;
349
350	return rt2->rt_hdr.nexthdr;
351}
352
353/* Routing Header type 2 is inserted.
354 * IP Header's dst address is replaced with Routing Header's Home Address.
355 */
356static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
357{
358	struct ipv6hdr *iph;
359	struct rt2_hdr *rt2;
360	u8 nexthdr;
361
362	iph = (struct ipv6hdr *)skb->data;
363	iph->payload_len = htons(skb->len - sizeof(*iph));
364
365	nexthdr = *skb_network_header(skb);
366	*skb_network_header(skb) = IPPROTO_ROUTING;
367
368	rt2 = (struct rt2_hdr *)skb_transport_header(skb);
369	rt2->rt_hdr.nexthdr = nexthdr;
370	rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
371	rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
372	rt2->rt_hdr.segments_left = 1;
373	memset(&rt2->reserved, 0, sizeof(rt2->reserved));
374
375	BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
376
377	memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
378	memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
379
380	return 0;
381}
382
383static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
384			     u8 **nexthdr)
385{
386	u16 offset = sizeof(struct ipv6hdr);
387	struct ipv6_opt_hdr *exthdr =
388				   (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
389	const unsigned char *nh = skb_network_header(skb);
390	unsigned int packet_len = skb->tail - skb->network_header;
391	int found_rhdr = 0;
392
393	*nexthdr = &ipv6_hdr(skb)->nexthdr;
394
395	while (offset + 1 <= packet_len) {
396
397		switch (**nexthdr) {
398		case NEXTHDR_HOP:
399			break;
400		case NEXTHDR_ROUTING:
401			if (offset + 3 <= packet_len) {
402				struct ipv6_rt_hdr *rt;
403				rt = (struct ipv6_rt_hdr *)(nh + offset);
404				if (rt->type != 0)
405					return offset;
406			}
407			found_rhdr = 1;
408			break;
409		case NEXTHDR_DEST:
410			if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
411				return offset;
412
413			if (found_rhdr)
414				return offset;
415
416			break;
417		default:
418			return offset;
419		}
420
421		offset += ipv6_optlen(exthdr);
422		*nexthdr = &exthdr->nexthdr;
423		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
424	}
425
426	return offset;
427}
428
429static int mip6_rthdr_init_state(struct xfrm_state *x)
430{
431	if (x->id.spi) {
432		printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
433		       x->id.spi);
434		return -EINVAL;
435	}
436	if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
437		printk(KERN_INFO "%s: state's mode is not %u: %u\n",
438		       __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
439		return -EINVAL;
440	}
441
442	x->props.header_len = sizeof(struct rt2_hdr);
443
444	return 0;
445}
446
447/*
448 * Do nothing about destroying since it has no specific operation for routing
449 * header type 2 unlike IPsec protocols.
450 */
451static void mip6_rthdr_destroy(struct xfrm_state *x)
452{
453}
454
455static struct xfrm_type mip6_rthdr_type =
456{
457	.description	= "MIP6RT",
458	.owner		= THIS_MODULE,
459	.proto	     	= IPPROTO_ROUTING,
460	.flags		= XFRM_TYPE_NON_FRAGMENT,
461	.init_state	= mip6_rthdr_init_state,
462	.destructor	= mip6_rthdr_destroy,
463	.input		= mip6_rthdr_input,
464	.output		= mip6_rthdr_output,
465	.hdr_offset	= mip6_rthdr_offset,
466	.remote_addr	= mip6_xfrm_addr,
467};
468
469int __init mip6_init(void)
470{
471	printk(KERN_INFO "Mobile IPv6\n");
472
473	if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
474		printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
475		goto mip6_destopt_xfrm_fail;
476	}
477	if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
478		printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
479		goto mip6_rthdr_xfrm_fail;
480	}
481	return 0;
482
483 mip6_rthdr_xfrm_fail:
484	xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
485 mip6_destopt_xfrm_fail:
486	return -EAGAIN;
487}
488
489void __exit mip6_fini(void)
490{
491	if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
492		printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
493	if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
494		printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
495}
496