www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / net / ipv4 / xfrm4_tunnel.c
1 /* xfrm4_tunnel.c: Generic IP tunnel transformer.
2  *
3  * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4  */
5
6 #include <linux/skbuff.h>
7 #include <net/xfrm.h>
8 #include <net/ip.h>
9 #include <net/protocol.h>
10
11 static int ipip_output(struct sk_buff **pskb)
12 {
13         struct sk_buff *skb = *pskb;
14         struct iphdr *iph;
15         
16         iph = skb->nh.iph;
17         iph->tot_len = htons(skb->len);
18         ip_send_check(iph);
19
20         return 0;
21 }
22
23 static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
24 {
25         return 0;
26 }
27
28 static struct xfrm_tunnel *ipip_handler;
29 static DECLARE_MUTEX(xfrm4_tunnel_sem);
30
31 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
32 {
33         int ret;
34
35         down(&xfrm4_tunnel_sem);
36         ret = 0;
37         if (ipip_handler != NULL)
38                 ret = -EINVAL;
39         if (!ret)
40                 ipip_handler = handler;
41         up(&xfrm4_tunnel_sem);
42
43         return ret;
44 }
45
46 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
47 {
48         int ret;
49
50         down(&xfrm4_tunnel_sem);
51         ret = 0;
52         if (ipip_handler != handler)
53                 ret = -EINVAL;
54         if (!ret)
55                 ipip_handler = NULL;
56         up(&xfrm4_tunnel_sem);
57
58         synchronize_net();
59
60         return ret;
61 }
62
63 static int ipip_rcv(struct sk_buff *skb)
64 {
65         struct xfrm_tunnel *handler = ipip_handler;
66
67         /* Tunnel devices take precedence.  */
68         if (handler && handler->handler(skb) == 0)
69                 return 0;
70
71         return xfrm4_rcv_encap(skb, 0);
72 }
73
74 static void ipip_err(struct sk_buff *skb, u32 info)
75 {
76         struct xfrm_tunnel *handler = ipip_handler;
77         u32 arg = info;
78
79         if (handler)
80                 handler->err_handler(skb, &arg);
81 }
82
83 static int ipip_init_state(struct xfrm_state *x, void *args)
84 {
85         if (!x->props.mode)
86                 return -EINVAL;
87         x->props.header_len = sizeof(struct iphdr);
88
89         return 0;
90 }
91
92 static void ipip_destroy(struct xfrm_state *x)
93 {
94 }
95
96 static struct xfrm_type ipip_type = {
97         .description    = "IPIP",
98         .owner          = THIS_MODULE,
99         .proto          = IPPROTO_IPIP,
100         .init_state     = ipip_init_state,
101         .destructor     = ipip_destroy,
102         .input          = ipip_xfrm_rcv,
103         .output         = ipip_output
104 };
105
106 static struct net_protocol ipip_protocol = {
107         .handler        =       ipip_rcv,
108         .err_handler    =       ipip_err,
109         .no_policy      =       1,
110 };
111
112 static int __init ipip_init(void)
113 {
114         if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
115                 printk(KERN_INFO "ipip init: can't add xfrm type\n");
116                 return -EAGAIN;
117         }
118         if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
119                 printk(KERN_INFO "ipip init: can't add protocol\n");
120                 xfrm_unregister_type(&ipip_type, AF_INET);
121                 return -EAGAIN;
122         }
123         return 0;
124 }
125
126 static void __exit ipip_fini(void)
127 {
128         if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
129                 printk(KERN_INFO "ipip close: can't remove protocol\n");
130         if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
131                 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
132 }
133
134 module_init(ipip_init);
135 module_exit(ipip_fini);
136 MODULE_LICENSE("GPL");