blob: 16efc66a7c3f5eccda3e47983c3dc15f97ce609e [file] [log] [blame]
Diego Beltrami0a694522006-10-03 23:47:05 -07001/*
2 * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4.
3 *
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 */
10
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>
19
20/* Add encapsulation header.
21 *
22 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
23 * The following fields in it shall be filled in by x->type->output:
24 * tot_len
25 * check
26 *
27 * On exit, skb->h will be set to the start of the payload to be processed
28 * by x->type->output and skb->nh will be set to the top IP header.
29 */
30static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
31{
32 struct iphdr *iph, *top_iph = NULL;
33 int hdrlen, optlen;
34
35 iph = skb->nh.iph;
36 skb->h.ipiph = iph;
37
38 hdrlen = 0;
39 optlen = iph->ihl * 4 - sizeof(*iph);
40 if (unlikely(optlen))
41 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
42
43 skb->nh.raw = skb_push(skb, x->props.header_len + hdrlen);
44 top_iph = skb->nh.iph;
Patrick McHardyc5027c92007-04-05 15:54:02 -070045 skb->h.raw += sizeof(*iph) - hdrlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070046
Patrick McHardyc5027c92007-04-05 15:54:02 -070047 memmove(top_iph, iph, sizeof(*iph));
Diego Beltrami0a694522006-10-03 23:47:05 -070048 if (unlikely(optlen)) {
49 struct ip_beet_phdr *ph;
50
51 BUG_ON(optlen < 0);
52
53 ph = (struct ip_beet_phdr *)skb->h.raw;
54 ph->padlen = 4 - (optlen & 4);
55 ph->hdrlen = (optlen + ph->padlen + sizeof(*ph)) / 8;
56 ph->nexthdr = top_iph->protocol;
57
58 top_iph->protocol = IPPROTO_BEETPH;
59 top_iph->ihl = sizeof(struct iphdr) / 4;
60 }
61
62 top_iph->saddr = x->props.saddr.a4;
63 top_iph->daddr = x->id.daddr.a4;
64
65 return 0;
66}
67
68static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
69{
70 struct iphdr *iph = skb->nh.iph;
71 int phlen = 0;
72 int optlen = 0;
73 __u8 ph_nexthdr = 0, protocol = 0;
74 int err = -EINVAL;
75
76 protocol = iph->protocol;
77
78 if (unlikely(iph->protocol == IPPROTO_BEETPH)) {
79 struct ip_beet_phdr *ph = (struct ip_beet_phdr*)(iph + 1);
80
81 if (!pskb_may_pull(skb, sizeof(*ph)))
82 goto out;
83
84 phlen = ph->hdrlen * 8;
85 optlen = phlen - ph->padlen - sizeof(*ph);
86 if (optlen < 0 || optlen & 3 || optlen > 250)
87 goto out;
88
89 if (!pskb_may_pull(skb, phlen))
90 goto out;
91
92 ph_nexthdr = ph->nexthdr;
93 }
94
95 skb_push(skb, sizeof(*iph) - phlen + optlen);
96 memmove(skb->data, skb->nh.raw, sizeof(*iph));
97 skb->nh.raw = skb->data;
98
99 iph = skb->nh.iph;
100 iph->ihl = (sizeof(*iph) + optlen) / 4;
101 iph->tot_len = htons(skb->len);
102 iph->daddr = x->sel.daddr.a4;
103 iph->saddr = x->sel.saddr.a4;
104 if (ph_nexthdr)
105 iph->protocol = ph_nexthdr;
106 else
107 iph->protocol = protocol;
108 iph->check = 0;
109 iph->check = ip_fast_csum(skb->nh.raw, iph->ihl);
110 err = 0;
111out:
112 return err;
113}
114
115static struct xfrm_mode xfrm4_beet_mode = {
116 .input = xfrm4_beet_input,
117 .output = xfrm4_beet_output,
118 .owner = THIS_MODULE,
119 .encap = XFRM_MODE_BEET,
120};
121
122static int __init xfrm4_beet_init(void)
123{
124 return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
125}
126
127static void __exit xfrm4_beet_exit(void)
128{
129 int err;
130
131 err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
132 BUG_ON(err);
133}
134
135module_init(xfrm4_beet_init);
136module_exit(xfrm4_beet_exit);
137MODULE_LICENSE("GPL");
138MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);