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 *skb)
38 struct dst_entry *dst = skb->dst;
39 struct xfrm_state *x = dst->xfrm;
40 struct iphdr *iph, *top_iph;
43 if ((err = xfrm4_tunnel_check_size(skb)) != 0)
48 spin_lock_bh(&x->lock);
52 top_iph = (struct iphdr *) skb_push(skb, x->props.header_len);
55 top_iph->tos = INET_ECN_encapsulate(tos, iph->tos);
56 top_iph->tot_len = htons(skb->len);
57 top_iph->frag_off = iph->frag_off & ~htons(IP_MF|IP_OFFSET);
58 if (!(iph->frag_off & htons(IP_DF)))
59 __ip_select_ident(top_iph, dst, 0);
60 top_iph->ttl = iph->ttl;
61 top_iph->protocol = IPPROTO_IPIP;
63 top_iph->saddr = x->props.saddr.a4;
64 top_iph->daddr = x->id.daddr.a4;
65 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
66 ip_send_check(top_iph);
68 skb->nh.raw = skb->data;
69 x->curlft.bytes += skb->len;
72 spin_unlock_bh(&x->lock);
74 if ((skb->dst = dst_pop(dst)) == NULL) {
79 return NET_XMIT_BYPASS;
86 static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
91 static struct xfrm_tunnel *ipip_handler;
92 static DECLARE_MUTEX(xfrm4_tunnel_sem);
94 int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
98 down(&xfrm4_tunnel_sem);
100 if (ipip_handler != NULL)
103 ipip_handler = handler;
104 up(&xfrm4_tunnel_sem);
109 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
113 down(&xfrm4_tunnel_sem);
115 if (ipip_handler != handler)
119 up(&xfrm4_tunnel_sem);
126 static int ipip_rcv(struct sk_buff *skb)
128 struct xfrm_tunnel *handler = ipip_handler;
130 /* Tunnel devices take precedence. */
131 if (handler && handler->handler(skb) == 0)
134 return xfrm4_rcv_encap(skb, 0);
137 static void ipip_err(struct sk_buff *skb, u32 info)
139 struct xfrm_tunnel *handler = ipip_handler;
143 handler->err_handler(skb, &arg);
146 static int ipip_init_state(struct xfrm_state *x, void *args)
150 x->props.header_len = sizeof(struct iphdr);
155 static void ipip_destroy(struct xfrm_state *x)
159 static struct xfrm_type ipip_type = {
160 .description = "IPIP",
161 .owner = THIS_MODULE,
162 .proto = IPPROTO_IPIP,
163 .init_state = ipip_init_state,
164 .destructor = ipip_destroy,
165 .input = ipip_xfrm_rcv,
166 .output = ipip_output
169 static struct inet_protocol ipip_protocol = {
171 .err_handler = ipip_err,
175 static int __init ipip_init(void)
177 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
178 printk(KERN_INFO "ipip init: can't add xfrm type\n");
181 if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
182 printk(KERN_INFO "ipip init: can't add protocol\n");
183 xfrm_unregister_type(&ipip_type, AF_INET);
189 static void __exit ipip_fini(void)
191 if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
192 printk(KERN_INFO "ipip close: can't remove protocol\n");
193 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
194 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
197 module_init(ipip_init);
198 module_exit(ipip_fini);
199 MODULE_LICENSE("GPL");