1 /* xfrm4_tunnel.c: Generic IP tunnel transformer.
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
6 #include <linux/skbuff.h>
10 #include <net/inet_ecn.h>
12 int xfrm4_tunnel_check_size(struct sk_buff *skb)
15 struct dst_entry *dst;
16 struct iphdr *iph = skb->nh.iph;
18 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
21 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
23 if (!(iph->frag_off & htons(IP_DF)))
27 mtu = dst_pmtu(dst) - dst->header_len - dst->trailer_len;
29 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
36 static int ipip_output(struct sk_buff **pskb)
38 struct sk_buff *skb = *pskb;
39 struct dst_entry *dst = skb->dst;
40 struct xfrm_state *x = dst->xfrm;
41 struct iphdr *iph, *top_iph;
44 if ((err = xfrm4_tunnel_check_size(skb)) != 0)
49 spin_lock_bh(&x->lock);
53 top_iph = (struct iphdr *) skb_push(skb, x->props.header_len);
56 top_iph->tos = INET_ECN_encapsulate(tos, iph->tos);
57 top_iph->tot_len = htons(skb->len);
58 top_iph->frag_off = iph->frag_off & ~htons(IP_MF|IP_OFFSET);
59 if (!(iph->frag_off & htons(IP_DF)))
60 __ip_select_ident(top_iph, dst, 0);
61 top_iph->ttl = iph->ttl;
62 top_iph->protocol = IPPROTO_IPIP;
64 top_iph->saddr = x->props.saddr.a4;
65 top_iph->daddr = x->id.daddr.a4;
66 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
67 ip_send_check(top_iph);
69 skb->nh.raw = skb->data;
70 x->curlft.bytes += skb->len;
73 spin_unlock_bh(&x->lock);
75 if ((skb->dst = dst_pop(dst)) == NULL) {
80 return NET_XMIT_BYPASS;
87 static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
92 static struct xfrm_tunnel *ipip_handler;
93 static DECLARE_MUTEX(xfrm4_tunnel_sem);
95 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
99 down(&xfrm4_tunnel_sem);
101 if (ipip_handler != NULL)
104 ipip_handler = handler;
105 up(&xfrm4_tunnel_sem);
110 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
114 down(&xfrm4_tunnel_sem);
116 if (ipip_handler != handler)
120 up(&xfrm4_tunnel_sem);
127 static int ipip_rcv(struct sk_buff *skb)
129 struct xfrm_tunnel *handler = ipip_handler;
131 /* Tunnel devices take precedence. */
132 if (handler && handler->handler(skb) == 0)
135 return xfrm4_rcv_encap(skb, 0);
138 static void ipip_err(struct sk_buff *skb, u32 info)
140 struct xfrm_tunnel *handler = ipip_handler;
144 handler->err_handler(skb, &arg);
147 static int ipip_init_state(struct xfrm_state *x, void *args)
151 x->props.header_len = sizeof(struct iphdr);
156 static void ipip_destroy(struct xfrm_state *x)
160 static struct xfrm_type ipip_type = {
161 .description = "IPIP",
162 .owner = THIS_MODULE,
163 .proto = IPPROTO_IPIP,
164 .init_state = ipip_init_state,
165 .destructor = ipip_destroy,
166 .input = ipip_xfrm_rcv,
167 .output = ipip_output
170 static struct inet_protocol ipip_protocol = {
172 .err_handler = ipip_err,
176 static int __init ipip_init(void)
178 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
179 printk(KERN_INFO "ipip init: can't add xfrm type\n");
182 if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
183 printk(KERN_INFO "ipip init: can't add protocol\n");
184 xfrm_unregister_type(&ipip_type, AF_INET);
190 static void __exit ipip_fini(void)
192 if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
193 printk(KERN_INFO "ipip close: can't remove protocol\n");
194 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
195 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
198 module_init(ipip_init);
199 module_exit(ipip_fini);
200 MODULE_LICENSE("GPL");