xref: /linux/net/ipv4/xfrm4_tunnel.c (revision c4541b41c0e4b75b11125fed16db642fc03cb31c)
11da177e4SLinus Torvalds /* xfrm4_tunnel.c: Generic IP tunnel transformer.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2003 David S. Miller (davem@redhat.com)
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/skbuff.h>
71da177e4SLinus Torvalds #include <linux/module.h>
84a3e2f71SArjan van de Ven #include <linux/mutex.h>
91da177e4SLinus Torvalds #include <net/xfrm.h>
101da177e4SLinus Torvalds #include <net/ip.h>
111da177e4SLinus Torvalds #include <net/protocol.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
141da177e4SLinus Torvalds {
157b277b1aSHerbert Xu 	skb_push(skb, -skb_network_offset(skb));
161da177e4SLinus Torvalds 	return 0;
171da177e4SLinus Torvalds }
181da177e4SLinus Torvalds 
19e695633eSHerbert Xu static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
201da177e4SLinus Torvalds {
2104663d0bSHerbert Xu 	return ip_hdr(skb)->protocol;
221da177e4SLinus Torvalds }
231da177e4SLinus Torvalds 
2472cb6962SHerbert Xu static int ipip_init_state(struct xfrm_state *x)
251da177e4SLinus Torvalds {
267e49e6deSMasahide NAKAMURA 	if (x->props.mode != XFRM_MODE_TUNNEL)
271da177e4SLinus Torvalds 		return -EINVAL;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 	if (x->encap)
301da177e4SLinus Torvalds 		return -EINVAL;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 	x->props.header_len = sizeof(struct iphdr);
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	return 0;
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static void ipip_destroy(struct xfrm_state *x)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static struct xfrm_type ipip_type = {
421da177e4SLinus Torvalds 	.description	= "IPIP",
431da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
441da177e4SLinus Torvalds 	.proto	     	= IPPROTO_IPIP,
451da177e4SLinus Torvalds 	.init_state	= ipip_init_state,
461da177e4SLinus Torvalds 	.destructor	= ipip_destroy,
471da177e4SLinus Torvalds 	.input		= ipip_xfrm_rcv,
481da177e4SLinus Torvalds 	.output		= ipip_output
491da177e4SLinus Torvalds };
501da177e4SLinus Torvalds 
51*c4541b41SHerbert Xu static int xfrm_tunnel_rcv(struct sk_buff *skb)
52*c4541b41SHerbert Xu {
53*c4541b41SHerbert Xu 	return xfrm4_rcv_spi(skb, IPPROTO_IP, ip_hdr(skb)->saddr);
54*c4541b41SHerbert Xu }
55*c4541b41SHerbert Xu 
56d2acc347SHerbert Xu static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
57d2acc347SHerbert Xu {
58d2acc347SHerbert Xu 	return -ENOENT;
59d2acc347SHerbert Xu }
60d2acc347SHerbert Xu 
61d2acc347SHerbert Xu static struct xfrm_tunnel xfrm_tunnel_handler = {
62*c4541b41SHerbert Xu 	.handler	=	xfrm_tunnel_rcv,
63d2acc347SHerbert Xu 	.err_handler	=	xfrm_tunnel_err,
64d2acc347SHerbert Xu 	.priority	=	2,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
67c0d56408SKazunori MIYAZAWA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
68c0d56408SKazunori MIYAZAWA static struct xfrm_tunnel xfrm64_tunnel_handler = {
69*c4541b41SHerbert Xu 	.handler	=	xfrm_tunnel_rcv,
70c0d56408SKazunori MIYAZAWA 	.err_handler	=	xfrm_tunnel_err,
71c0d56408SKazunori MIYAZAWA 	.priority	=	2,
72c0d56408SKazunori MIYAZAWA };
73c0d56408SKazunori MIYAZAWA #endif
74c0d56408SKazunori MIYAZAWA 
751da177e4SLinus Torvalds static int __init ipip_init(void)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
781da177e4SLinus Torvalds 		printk(KERN_INFO "ipip init: can't add xfrm type\n");
791da177e4SLinus Torvalds 		return -EAGAIN;
801da177e4SLinus Torvalds 	}
81c0d56408SKazunori MIYAZAWA 
82c0d56408SKazunori MIYAZAWA 	if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
83c0d56408SKazunori MIYAZAWA 		printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET\n");
841da177e4SLinus Torvalds 		xfrm_unregister_type(&ipip_type, AF_INET);
851da177e4SLinus Torvalds 		return -EAGAIN;
861da177e4SLinus Torvalds 	}
87c0d56408SKazunori MIYAZAWA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
88c0d56408SKazunori MIYAZAWA 	if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
89c0d56408SKazunori MIYAZAWA 		printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET6\n");
90c0d56408SKazunori MIYAZAWA 		xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
91c0d56408SKazunori MIYAZAWA 		xfrm_unregister_type(&ipip_type, AF_INET);
92c0d56408SKazunori MIYAZAWA 		return -EAGAIN;
93c0d56408SKazunori MIYAZAWA 	}
94c0d56408SKazunori MIYAZAWA #endif
951da177e4SLinus Torvalds 	return 0;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static void __exit ipip_fini(void)
991da177e4SLinus Torvalds {
100c0d56408SKazunori MIYAZAWA #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
101c0d56408SKazunori MIYAZAWA 	if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
102c0d56408SKazunori MIYAZAWA 		printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET6\n");
103c0d56408SKazunori MIYAZAWA #endif
104c0d56408SKazunori MIYAZAWA 	if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
105c0d56408SKazunori MIYAZAWA 		printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET\n");
1061da177e4SLinus Torvalds 	if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
1071da177e4SLinus Torvalds 		printk(KERN_INFO "ipip close: can't remove xfrm type\n");
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds module_init(ipip_init);
1111da177e4SLinus Torvalds module_exit(ipip_fini);
1121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
113d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);
114