1 /* xfrm4_tunnel.c: Generic IP tunnel transformer.
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
6 #include <linux/skbuff.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
11 #include <net/protocol.h>
13 static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
18 iph->tot_len = htons(skb->len);
24 static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
29 static struct xfrm_tunnel *ipip_handler;
30 static DEFINE_MUTEX(xfrm4_tunnel_mutex);
32 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
36 mutex_lock(&xfrm4_tunnel_mutex);
38 if (ipip_handler != NULL)
41 ipip_handler = handler;
42 mutex_unlock(&xfrm4_tunnel_mutex);
47 EXPORT_SYMBOL(xfrm4_tunnel_register);
49 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
53 mutex_lock(&xfrm4_tunnel_mutex);
55 if (ipip_handler != handler)
59 mutex_unlock(&xfrm4_tunnel_mutex);
66 EXPORT_SYMBOL(xfrm4_tunnel_deregister);
68 static int ipip_rcv(struct sk_buff *skb)
70 struct xfrm_tunnel *handler = ipip_handler;
72 /* Tunnel devices take precedence. */
73 if (handler && handler->handler(skb) == 0)
76 return xfrm4_rcv(skb);
79 static void ipip_err(struct sk_buff *skb, u32 info)
81 struct xfrm_tunnel *handler = ipip_handler;
84 handler->err_handler(skb, info);
87 static int ipip_init_state(struct xfrm_state *x)
95 x->props.header_len = sizeof(struct iphdr);
100 static void ipip_destroy(struct xfrm_state *x)
104 static struct xfrm_type ipip_type = {
105 .description = "IPIP",
106 .owner = THIS_MODULE,
107 .proto = IPPROTO_IPIP,
108 .init_state = ipip_init_state,
109 .destructor = ipip_destroy,
110 .input = ipip_xfrm_rcv,
111 .output = ipip_output
114 static struct net_protocol ipip_protocol = {
116 .err_handler = ipip_err,
120 static int __init ipip_init(void)
122 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
123 printk(KERN_INFO "ipip init: can't add xfrm type\n");
126 if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
127 printk(KERN_INFO "ipip init: can't add protocol\n");
128 xfrm_unregister_type(&ipip_type, AF_INET);
134 static void __exit ipip_fini(void)
136 if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
137 printk(KERN_INFO "ipip close: can't remove protocol\n");
138 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
139 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
142 module_init(ipip_init);
143 module_exit(ipip_fini);
144 MODULE_LICENSE("GPL");