0999f9e956bb7dc246cb0aa6c9cb6760bd2e2480
[linux-2.6.git] / 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/icmp.h>
10 #include <net/inet_ecn.h>
11
12 int xfrm4_tunnel_check_size(struct sk_buff *skb)
13 {
14         int mtu, ret = 0;
15         struct dst_entry *dst;
16         struct iphdr *iph = skb->nh.iph;
17
18         if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
19                 goto out;
20
21         IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
22         
23         if (!(iph->frag_off & htons(IP_DF)))
24                 goto out;
25
26         dst = skb->dst;
27         mtu = dst_pmtu(dst) - dst->header_len - dst->trailer_len;
28         if (skb->len > mtu) {
29                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
30                 ret = -EMSGSIZE;
31         }
32 out:
33         return ret;
34 }
35
36 static int ipip_output(struct sk_buff **pskb)
37 {
38         struct sk_buff *skb = *pskb;
39         struct iphdr *iph;
40         
41         iph = skb->nh.iph;
42         iph->tot_len = htons(skb->len);
43         ip_send_check(iph);
44
45         return 0;
46 }
47
48 static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
49 {
50         return 0;
51 }
52
53 static struct xfrm_tunnel *ipip_handler;
54 static DECLARE_MUTEX(xfrm4_tunnel_sem);
55
56 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
57 {
58         int ret;
59
60         down(&xfrm4_tunnel_sem);
61         ret = 0;
62         if (ipip_handler != NULL)
63                 ret = -EINVAL;
64         if (!ret)
65                 ipip_handler = handler;
66         up(&xfrm4_tunnel_sem);
67
68         return ret;
69 }
70
71 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
72 {
73         int ret;
74
75         down(&xfrm4_tunnel_sem);
76         ret = 0;
77         if (ipip_handler != handler)
78                 ret = -EINVAL;
79         if (!ret)
80                 ipip_handler = NULL;
81         up(&xfrm4_tunnel_sem);
82
83         synchronize_net();
84
85         return ret;
86 }
87
88 static int ipip_rcv(struct sk_buff *skb)
89 {
90         struct xfrm_tunnel *handler = ipip_handler;
91
92         /* Tunnel devices take precedence.  */
93         if (handler && handler->handler(skb) == 0)
94                 return 0;
95
96         return xfrm4_rcv_encap(skb, 0);
97 }
98
99 static void ipip_err(struct sk_buff *skb, u32 info)
100 {
101         struct xfrm_tunnel *handler = ipip_handler;
102         u32 arg = info;
103
104         if (handler)
105                 handler->err_handler(skb, &arg);
106 }
107
108 static int ipip_init_state(struct xfrm_state *x, void *args)
109 {
110         if (!x->props.mode)
111                 return -EINVAL;
112         x->props.header_len = sizeof(struct iphdr);
113
114         return 0;
115 }
116
117 static void ipip_destroy(struct xfrm_state *x)
118 {
119 }
120
121 static struct xfrm_type ipip_type = {
122         .description    = "IPIP",
123         .owner          = THIS_MODULE,
124         .proto          = IPPROTO_IPIP,
125         .init_state     = ipip_init_state,
126         .destructor     = ipip_destroy,
127         .input          = ipip_xfrm_rcv,
128         .output         = ipip_output
129 };
130
131 static struct net_protocol ipip_protocol = {
132         .handler        =       ipip_rcv,
133         .err_handler    =       ipip_err,
134         .no_policy      =       1,
135 };
136
137 static int __init ipip_init(void)
138 {
139         if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
140                 printk(KERN_INFO "ipip init: can't add xfrm type\n");
141                 return -EAGAIN;
142         }
143         if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
144                 printk(KERN_INFO "ipip init: can't add protocol\n");
145                 xfrm_unregister_type(&ipip_type, AF_INET);
146                 return -EAGAIN;
147         }
148         return 0;
149 }
150
151 static void __exit ipip_fini(void)
152 {
153         if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
154                 printk(KERN_INFO "ipip close: can't remove protocol\n");
155         if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
156                 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
157 }
158
159 module_init(ipip_init);
160 module_exit(ipip_fini);
161 MODULE_LICENSE("GPL");