blob: 94842adce144111d9cf14096329d2760e41ce06e [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.
Diego Beltrami0a694522006-10-03 23:47:05 -070023 */
24static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
25{
Herbert Xu37fedd32007-10-10 15:44:44 -070026 struct ip_beet_phdr *ph;
Patrick McHardyea2f10a2007-04-05 16:04:04 -070027 struct iphdr *iph, *top_iph;
Diego Beltrami0a694522006-10-03 23:47:05 -070028 int hdrlen, optlen;
29
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070030 iph = ip_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070031
32 hdrlen = 0;
33 optlen = iph->ihl * 4 - sizeof(*iph);
34 if (unlikely(optlen))
35 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
36
Herbert Xu7b277b12007-10-10 15:44:06 -070037 skb_set_network_header(skb, IPV4_BEET_PHMAXLEN - x->props.header_len -
38 hdrlen);
Herbert Xu37fedd32007-10-10 15:44:44 -070039 skb->mac_header = skb->network_header +
40 offsetof(struct iphdr, protocol);
41 skb->transport_header = skb->network_header + sizeof(*iph);
Diego Beltrami0a694522006-10-03 23:47:05 -070042
Herbert Xu37fedd32007-10-10 15:44:44 -070043 ph = (struct ip_beet_phdr *)__skb_pull(skb, sizeof(*iph) - hdrlen);
44
45 top_iph = ip_hdr(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080046
47 top_iph->ihl = 5;
48 top_iph->version = 4;
49
50 top_iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
51 top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
52
53 top_iph->id = XFRM_MODE_SKB_CB(skb)->id;
54 top_iph->frag_off = XFRM_MODE_SKB_CB(skb)->frag_off;
55 top_iph->ttl = XFRM_MODE_SKB_CB(skb)->ttl;
56
Diego Beltrami0a694522006-10-03 23:47:05 -070057 if (unlikely(optlen)) {
Diego Beltrami0a694522006-10-03 23:47:05 -070058 BUG_ON(optlen < 0);
59
Diego Beltrami0a694522006-10-03 23:47:05 -070060 ph->padlen = 4 - (optlen & 4);
Patrick McHardy05d22442007-04-23 22:39:02 -070061 ph->hdrlen = optlen / 8;
Diego Beltrami0a694522006-10-03 23:47:05 -070062 ph->nexthdr = top_iph->protocol;
Patrick McHardy04fef982007-04-05 15:54:39 -070063 if (ph->padlen)
64 memset(ph + 1, IPOPT_NOP, ph->padlen);
Diego Beltrami0a694522006-10-03 23:47:05 -070065
66 top_iph->protocol = IPPROTO_BEETPH;
67 top_iph->ihl = sizeof(struct iphdr) / 4;
68 }
69
70 top_iph->saddr = x->props.saddr.a4;
71 top_iph->daddr = x->id.daddr.a4;
72
73 return 0;
74}
75
76static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
77{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070078 struct iphdr *iph = ip_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070079 int phlen = 0;
80 int optlen = 0;
Patrick McHardyea2f10a2007-04-05 16:04:04 -070081 u8 ph_nexthdr = 0;
Diego Beltrami0a694522006-10-03 23:47:05 -070082 int err = -EINVAL;
83
Diego Beltrami0a694522006-10-03 23:47:05 -070084 if (unlikely(iph->protocol == IPPROTO_BEETPH)) {
Patrick McHardy254d0d22007-04-05 16:03:33 -070085 struct ip_beet_phdr *ph;
Diego Beltrami0a694522006-10-03 23:47:05 -070086
87 if (!pskb_may_pull(skb, sizeof(*ph)))
88 goto out;
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -070089 ph = (struct ip_beet_phdr *)(ipip_hdr(skb) + 1);
Diego Beltrami0a694522006-10-03 23:47:05 -070090
Patrick McHardyd4b1e842007-04-05 15:59:41 -070091 phlen = sizeof(*ph) + ph->padlen;
Patrick McHardy05d22442007-04-23 22:39:02 -070092 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
Diego Beltrami0a694522006-10-03 23:47:05 -070093 if (optlen < 0 || optlen & 3 || optlen > 250)
94 goto out;
95
Patrick McHardyd4b1e842007-04-05 15:59:41 -070096 if (!pskb_may_pull(skb, phlen + optlen))
Diego Beltrami0a694522006-10-03 23:47:05 -070097 goto out;
Patrick McHardy254d0d22007-04-05 16:03:33 -070098 skb->len -= phlen + optlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070099
100 ph_nexthdr = ph->nexthdr;
101 }
102
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -0700103 skb_set_network_header(skb, phlen - sizeof(*iph));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700104 memmove(skb_network_header(skb), iph, sizeof(*iph));
Arnaldo Carvalho de Melob0061ce2007-04-25 18:02:22 -0700105 skb_set_transport_header(skb, phlen + optlen);
David S. Miller239254f2007-04-19 19:55:44 -0700106 skb->data = skb_transport_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700107
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700108 iph = ip_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700109 iph->ihl = (sizeof(*iph) + optlen) / 4;
Patrick McHardyd4b1e842007-04-05 15:59:41 -0700110 iph->tot_len = htons(skb->len + iph->ihl * 4);
Diego Beltrami0a694522006-10-03 23:47:05 -0700111 iph->daddr = x->sel.daddr.a4;
112 iph->saddr = x->sel.saddr.a4;
113 if (ph_nexthdr)
114 iph->protocol = ph_nexthdr;
Diego Beltrami0a694522006-10-03 23:47:05 -0700115 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700116 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Diego Beltrami0a694522006-10-03 23:47:05 -0700117 err = 0;
118out:
119 return err;
120}
121
122static struct xfrm_mode xfrm4_beet_mode = {
123 .input = xfrm4_beet_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800124 .output2 = xfrm4_beet_output,
125 .output = xfrm4_prepare_output,
Diego Beltrami0a694522006-10-03 23:47:05 -0700126 .owner = THIS_MODULE,
127 .encap = XFRM_MODE_BEET,
Herbert Xu1bfcb102007-10-17 21:31:50 -0700128 .flags = XFRM_MODE_FLAG_TUNNEL,
Diego Beltrami0a694522006-10-03 23:47:05 -0700129};
130
131static int __init xfrm4_beet_init(void)
132{
133 return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
134}
135
136static void __exit xfrm4_beet_exit(void)
137{
138 int err;
139
140 err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
141 BUG_ON(err);
142}
143
144module_init(xfrm4_beet_init);
145module_exit(xfrm4_beet_exit);
146MODULE_LICENSE("GPL");
147MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);