blob: a73e710740c22396c4bff28337c53ea6b8624565 [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{
Patrick McHardyea2f10a2007-04-05 16:04:04 -070032 struct iphdr *iph, *top_iph;
Diego Beltrami0a694522006-10-03 23:47:05 -070033 int hdrlen, optlen;
34
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070035 iph = ip_hdr(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070036 skb->transport_header = skb->network_header;
Diego Beltrami0a694522006-10-03 23:47:05 -070037
38 hdrlen = 0;
39 optlen = iph->ihl * 4 - sizeof(*iph);
40 if (unlikely(optlen))
41 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
42
Patrick McHardyac758e32007-04-09 11:47:58 -070043 skb_push(skb, x->props.header_len - IPV4_BEET_PHMAXLEN + hdrlen);
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -070044 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070045 top_iph = ip_hdr(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070046 skb->transport_header += sizeof(*iph) - hdrlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070047
Patrick McHardyc5027c92007-04-05 15:54:02 -070048 memmove(top_iph, iph, sizeof(*iph));
Diego Beltrami0a694522006-10-03 23:47:05 -070049 if (unlikely(optlen)) {
50 struct ip_beet_phdr *ph;
51
52 BUG_ON(optlen < 0);
53
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070054 ph = (struct ip_beet_phdr *)skb_transport_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070055 ph->padlen = 4 - (optlen & 4);
Patrick McHardy05d22442007-04-23 22:39:02 -070056 ph->hdrlen = optlen / 8;
Diego Beltrami0a694522006-10-03 23:47:05 -070057 ph->nexthdr = top_iph->protocol;
Patrick McHardy04fef982007-04-05 15:54:39 -070058 if (ph->padlen)
59 memset(ph + 1, IPOPT_NOP, ph->padlen);
Diego Beltrami0a694522006-10-03 23:47:05 -070060
61 top_iph->protocol = IPPROTO_BEETPH;
62 top_iph->ihl = sizeof(struct iphdr) / 4;
63 }
64
65 top_iph->saddr = x->props.saddr.a4;
66 top_iph->daddr = x->id.daddr.a4;
67
68 return 0;
69}
70
71static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
72{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070073 struct iphdr *iph = ip_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070074 int phlen = 0;
75 int optlen = 0;
Patrick McHardyea2f10a2007-04-05 16:04:04 -070076 u8 ph_nexthdr = 0;
Diego Beltrami0a694522006-10-03 23:47:05 -070077 int err = -EINVAL;
78
Diego Beltrami0a694522006-10-03 23:47:05 -070079 if (unlikely(iph->protocol == IPPROTO_BEETPH)) {
Patrick McHardy254d0d22007-04-05 16:03:33 -070080 struct ip_beet_phdr *ph;
Diego Beltrami0a694522006-10-03 23:47:05 -070081
82 if (!pskb_may_pull(skb, sizeof(*ph)))
83 goto out;
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -070084 ph = (struct ip_beet_phdr *)(ipip_hdr(skb) + 1);
Diego Beltrami0a694522006-10-03 23:47:05 -070085
Patrick McHardyd4b1e842007-04-05 15:59:41 -070086 phlen = sizeof(*ph) + ph->padlen;
Patrick McHardy05d22442007-04-23 22:39:02 -070087 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
Diego Beltrami0a694522006-10-03 23:47:05 -070088 if (optlen < 0 || optlen & 3 || optlen > 250)
89 goto out;
90
Patrick McHardyd4b1e842007-04-05 15:59:41 -070091 if (!pskb_may_pull(skb, phlen + optlen))
Diego Beltrami0a694522006-10-03 23:47:05 -070092 goto out;
Patrick McHardy254d0d22007-04-05 16:03:33 -070093 skb->len -= phlen + optlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070094
95 ph_nexthdr = ph->nexthdr;
96 }
97
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -070098 skb_set_network_header(skb, phlen - sizeof(*iph));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070099 memmove(skb_network_header(skb), iph, sizeof(*iph));
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -0700100 skb_set_transport_header(skb, phlen + optlen);
David S. Miller239254f2007-04-19 19:55:44 -0700101 skb->data = skb_transport_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700102
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700103 iph = ip_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700104 iph->ihl = (sizeof(*iph) + optlen) / 4;
Patrick McHardyd4b1e842007-04-05 15:59:41 -0700105 iph->tot_len = htons(skb->len + iph->ihl * 4);
Diego Beltrami0a694522006-10-03 23:47:05 -0700106 iph->daddr = x->sel.daddr.a4;
107 iph->saddr = x->sel.saddr.a4;
108 if (ph_nexthdr)
109 iph->protocol = ph_nexthdr;
Diego Beltrami0a694522006-10-03 23:47:05 -0700110 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700111 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Diego Beltrami0a694522006-10-03 23:47:05 -0700112 err = 0;
113out:
114 return err;
115}
116
117static struct xfrm_mode xfrm4_beet_mode = {
118 .input = xfrm4_beet_input,
119 .output = xfrm4_beet_output,
120 .owner = THIS_MODULE,
121 .encap = XFRM_MODE_BEET,
122};
123
124static int __init xfrm4_beet_init(void)
125{
126 return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
127}
128
129static void __exit xfrm4_beet_exit(void)
130{
131 int err;
132
133 err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
134 BUG_ON(err);
135}
136
137module_init(xfrm4_beet_init);
138module_exit(xfrm4_beet_exit);
139MODULE_LICENSE("GPL");
140MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);