Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / ipv4 / xfrm4_mode_beet.c
blob856d2dfdb44b6be0d9d9be732d1e26f7db69792c
1 /*
2 * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4.
4 * Copyright (c) 2006 Diego Beltrami <diego.beltrami@gmail.com>
5 * Miika Komu <miika@iki.fi>
6 * Herbert Xu <herbert@gondor.apana.org.au>
7 * Abhinav Pathak <abhinav.pathak@hiit.fi>
8 * Jeff Ahrenholz <ahrenholz@gmail.com>
9 */
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/stringify.h>
16 #include <net/dst.h>
17 #include <net/ip.h>
18 #include <net/xfrm.h>
20 static void xfrm4_beet_make_header(struct sk_buff *skb)
22 struct iphdr *iph = ip_hdr(skb);
24 iph->ihl = 5;
25 iph->version = 4;
27 iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
28 iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
30 iph->id = XFRM_MODE_SKB_CB(skb)->id;
31 iph->frag_off = XFRM_MODE_SKB_CB(skb)->frag_off;
32 iph->ttl = XFRM_MODE_SKB_CB(skb)->ttl;
35 /* Add encapsulation header.
37 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
39 static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
41 struct ip_beet_phdr *ph;
42 struct iphdr *top_iph;
43 int hdrlen, optlen;
45 hdrlen = 0;
46 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
47 if (unlikely(optlen))
48 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
50 skb_set_network_header(skb, -x->props.header_len -
51 hdrlen + (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
52 if (x->sel.family != AF_INET6)
53 skb->network_header += IPV4_BEET_PHMAXLEN;
54 skb->mac_header = skb->network_header +
55 offsetof(struct iphdr, protocol);
56 skb->transport_header = skb->network_header + sizeof(*top_iph);
58 xfrm4_beet_make_header(skb);
60 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
62 top_iph = ip_hdr(skb);
64 if (unlikely(optlen)) {
65 BUG_ON(optlen < 0);
67 ph->padlen = 4 - (optlen & 4);
68 ph->hdrlen = optlen / 8;
69 ph->nexthdr = top_iph->protocol;
70 if (ph->padlen)
71 memset(ph + 1, IPOPT_NOP, ph->padlen);
73 top_iph->protocol = IPPROTO_BEETPH;
74 top_iph->ihl = sizeof(struct iphdr) / 4;
77 top_iph->saddr = x->props.saddr.a4;
78 top_iph->daddr = x->id.daddr.a4;
80 return 0;
83 static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
85 struct iphdr *iph;
86 int optlen = 0;
87 int err = -EINVAL;
89 if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
90 struct ip_beet_phdr *ph;
91 int phlen;
93 if (!pskb_may_pull(skb, sizeof(*ph)))
94 goto out;
96 ph = (struct ip_beet_phdr *)skb->data;
98 phlen = sizeof(*ph) + ph->padlen;
99 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
100 if (optlen < 0 || optlen & 3 || optlen > 250)
101 goto out;
103 XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
105 if (!pskb_may_pull(skb, phlen))
106 goto out;
107 __skb_pull(skb, phlen);
110 skb_push(skb, sizeof(*iph));
111 skb_reset_network_header(skb);
112 skb_mac_header_rebuild(skb);
114 xfrm4_beet_make_header(skb);
116 iph = ip_hdr(skb);
118 iph->ihl += optlen / 4;
119 iph->tot_len = htons(skb->len);
120 iph->daddr = x->sel.daddr.a4;
121 iph->saddr = x->sel.saddr.a4;
122 iph->check = 0;
123 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
124 err = 0;
125 out:
126 return err;
129 static struct xfrm_mode xfrm4_beet_mode = {
130 .input2 = xfrm4_beet_input,
131 .input = xfrm_prepare_input,
132 .output2 = xfrm4_beet_output,
133 .output = xfrm4_prepare_output,
134 .owner = THIS_MODULE,
135 .encap = XFRM_MODE_BEET,
136 .flags = XFRM_MODE_FLAG_TUNNEL,
139 static int __init xfrm4_beet_init(void)
141 return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
144 static void __exit xfrm4_beet_exit(void)
146 int err;
148 err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
149 BUG_ON(err);
152 module_init(xfrm4_beet_init);
153 module_exit(xfrm4_beet_exit);
154 MODULE_LICENSE("GPL");
155 MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);