| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * IP Payload Compression Protocol (IPComp) - RFC3173. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2003 James Morris <jmorris@intercode.com.au> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify it | 
					
						
							|  |  |  |  * under the terms of the GNU General Public License as published by the Free | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:47 +09:00
										 |  |  |  * Software Foundation; either version 2 of the License, or (at your option) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |  * any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Todo: | 
					
						
							|  |  |  |  *   - Tunable compression parameters. | 
					
						
							|  |  |  |  *   - Compression stats. | 
					
						
							|  |  |  |  *   - Adaptive compression. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							| 
									
										
										
										
											2007-11-07 02:21:47 -08:00
										 |  |  | #include <linux/err.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #include <linux/rtnetlink.h>
 | 
					
						
							|  |  |  | #include <net/ip.h>
 | 
					
						
							|  |  |  | #include <net/xfrm.h>
 | 
					
						
							|  |  |  | #include <net/icmp.h>
 | 
					
						
							|  |  |  | #include <net/ipcomp.h>
 | 
					
						
							| 
									
										
										
										
											2005-12-27 02:43:12 -02:00
										 |  |  | #include <net/protocol.h>
 | 
					
						
							| 
									
										
										
										
											2008-07-25 02:54:40 -07:00
										 |  |  | #include <net/sock.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void ipcomp4_err(struct sk_buff *skb, u32 info) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-25 10:38:34 +00:00
										 |  |  | 	struct net *net = dev_net(skb->dev); | 
					
						
							| 
									
										
										
										
											2006-09-27 18:47:24 -07:00
										 |  |  | 	__be32 spi; | 
					
						
							| 
									
										
										
										
											2011-04-22 04:53:02 +00:00
										 |  |  | 	const struct iphdr *iph = (const struct iphdr *)skb->data; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2)); | 
					
						
							|  |  |  | 	struct xfrm_state *x; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-11 21:27:49 -07:00
										 |  |  | 	switch (icmp_hdr(skb)->type) { | 
					
						
							|  |  |  | 	case ICMP_DEST_UNREACH: | 
					
						
							|  |  |  | 		if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 	case ICMP_REDIRECT: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2012-07-11 21:27:49 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-22 16:53:22 -07:00
										 |  |  | 	spi = htonl(ntohs(ipch->cpi)); | 
					
						
							| 
									
										
										
										
											2011-04-22 04:53:02 +00:00
										 |  |  | 	x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:47 +09:00
										 |  |  | 			      spi, IPPROTO_COMP, AF_INET); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (!x) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2012-07-11 21:27:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-15 13:38:53 +01:00
										 |  |  | 	if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) { | 
					
						
							|  |  |  | 		atomic_inc(&flow_cache_genid); | 
					
						
							|  |  |  | 		rt_genid_bump(net); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-11 21:27:49 -07:00
										 |  |  | 		ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_COMP, 0); | 
					
						
							| 
									
										
										
										
											2013-01-15 13:38:53 +01:00
										 |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2012-07-11 21:27:49 -07:00
										 |  |  | 		ipv4_redirect(skb, net, 0, 0, IPPROTO_COMP, 0); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	xfrm_state_put(x); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:47 +09:00
										 |  |  | /* We always hold one tunnel user reference to indicate a tunnel */ | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-25 10:38:34 +00:00
										 |  |  | 	struct net *net = xs_net(x); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	struct xfrm_state *t; | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-25 10:38:34 +00:00
										 |  |  | 	t = xfrm_state_alloc(net); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (t == NULL) | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t->id.proto = IPPROTO_IPIP; | 
					
						
							|  |  |  | 	t->id.spi = x->props.saddr.a4; | 
					
						
							|  |  |  | 	t->id.daddr.a4 = x->id.daddr.a4; | 
					
						
							|  |  |  | 	memcpy(&t->sel, &x->sel, sizeof(t->sel)); | 
					
						
							|  |  |  | 	t->props.family = AF_INET; | 
					
						
							| 
									
										
										
										
											2007-11-13 21:39:08 -08:00
										 |  |  | 	t->props.mode = x->props.mode; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	t->props.saddr.a4 = x->props.saddr.a4; | 
					
						
							|  |  |  | 	t->props.flags = x->props.flags; | 
					
						
							| 
									
										
										
										
											2010-02-22 16:20:22 -08:00
										 |  |  | 	memcpy(&t->mark, &x->mark, sizeof(t->mark)); | 
					
						
							| 
									
										
										
										
											2005-06-20 13:18:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (xfrm_init_state(t)) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		goto error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	atomic_set(&t->tunnel_users, 1); | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	return t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  | 	t->km.state = XFRM_STATE_DEAD; | 
					
						
							|  |  |  | 	xfrm_state_put(t); | 
					
						
							|  |  |  | 	t = NULL; | 
					
						
							|  |  |  | 	goto out; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2006-03-20 22:33:17 -08:00
										 |  |  |  * Must be protected by xfrm_cfg_mutex.  State and tunnel user references are | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  |  * always incremented on success. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int ipcomp_tunnel_attach(struct xfrm_state *x) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-25 10:38:34 +00:00
										 |  |  | 	struct net *net = xs_net(x); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	int err = 0; | 
					
						
							|  |  |  | 	struct xfrm_state *t; | 
					
						
							| 
									
										
										
										
											2010-02-22 16:20:22 -08:00
										 |  |  | 	u32 mark = x->mark.v & x->mark.m; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 16:20:22 -08:00
										 |  |  | 	t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr.a4, | 
					
						
							| 
									
										
										
										
											2007-02-09 23:24:47 +09:00
										 |  |  | 			      x->props.saddr.a4, IPPROTO_IPIP, AF_INET); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (!t) { | 
					
						
							|  |  |  | 		t = ipcomp_tunnel_create(x); | 
					
						
							|  |  |  | 		if (!t) { | 
					
						
							|  |  |  | 			err = -EINVAL; | 
					
						
							|  |  |  | 			goto out; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		xfrm_state_insert(t); | 
					
						
							|  |  |  | 		xfrm_state_hold(t); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	x->tunnel = t; | 
					
						
							|  |  |  | 	atomic_inc(&t->tunnel_users); | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-25 02:54:40 -07:00
										 |  |  | static int ipcomp4_init_state(struct xfrm_state *x) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-07-27 03:59:24 -07:00
										 |  |  | 	int err = -EINVAL; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-13 21:39:08 -08:00
										 |  |  | 	x->props.header_len = 0; | 
					
						
							|  |  |  | 	switch (x->props.mode) { | 
					
						
							|  |  |  | 	case XFRM_MODE_TRANSPORT: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case XFRM_MODE_TUNNEL: | 
					
						
							|  |  |  | 		x->props.header_len += sizeof(struct iphdr); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-25 02:54:40 -07:00
										 |  |  | 	err = ipcomp_init_state(x); | 
					
						
							|  |  |  | 	if (err) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		goto out; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-22 15:05:15 -07:00
										 |  |  | 	if (x->props.mode == XFRM_MODE_TUNNEL) { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		err = ipcomp_tunnel_attach(x); | 
					
						
							|  |  |  | 		if (err) | 
					
						
							| 
									
										
										
										
											2010-02-15 19:24:30 +00:00
										 |  |  | 			goto out; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = 0; | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-30 19:11:50 -08:00
										 |  |  | static const struct xfrm_type ipcomp_type = { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	.description	= "IPCOMP4", | 
					
						
							|  |  |  | 	.owner		= THIS_MODULE, | 
					
						
							|  |  |  | 	.proto	     	= IPPROTO_COMP, | 
					
						
							| 
									
										
										
										
											2008-07-25 02:54:40 -07:00
										 |  |  | 	.init_state	= ipcomp4_init_state, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	.destructor	= ipcomp_destroy, | 
					
						
							|  |  |  | 	.input		= ipcomp_input, | 
					
						
							|  |  |  | 	.output		= ipcomp_output | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-14 12:21:47 +00:00
										 |  |  | static const struct net_protocol ipcomp4_protocol = { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	.handler	=	xfrm4_rcv, | 
					
						
							|  |  |  | 	.err_handler	=	ipcomp4_err, | 
					
						
							|  |  |  | 	.no_policy	=	1, | 
					
						
							| 
									
										
										
										
											2013-02-04 15:46:15 -05:00
										 |  |  | 	.netns_ok	=	1, | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int __init ipcomp4_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) { | 
					
						
							| 
									
										
										
										
											2012-03-11 18:36:11 +00:00
										 |  |  | 		pr_info("%s: can't add xfrm type\n", __func__); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		return -EAGAIN; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) { | 
					
						
							| 
									
										
										
										
											2012-03-11 18:36:11 +00:00
										 |  |  | 		pr_info("%s: can't add protocol\n", __func__); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		xfrm_unregister_type(&ipcomp_type, AF_INET); | 
					
						
							|  |  |  | 		return -EAGAIN; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void __exit ipcomp4_fini(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) | 
					
						
							| 
									
										
										
										
											2012-03-11 18:36:11 +00:00
										 |  |  | 		pr_info("%s: can't remove protocol\n", __func__); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0) | 
					
						
							| 
									
										
										
										
											2012-03-11 18:36:11 +00:00
										 |  |  | 		pr_info("%s: can't remove xfrm type\n", __func__); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module_init(ipcomp4_init); | 
					
						
							|  |  |  | module_exit(ipcomp4_fini); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); | 
					
						
							| 
									
										
										
										
											2008-07-25 02:54:40 -07:00
										 |  |  | MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp/IPv4) - RFC3173"); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-26 23:57:49 -07:00
										 |  |  | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP); |