blob: 856d2dfdb44b6be0d9d9be732d1e26f7db69792c [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
Herbert Xu227620e2007-11-13 21:41:28 -080020static void xfrm4_beet_make_header(struct sk_buff *skb)
21{
22 struct iphdr *iph = ip_hdr(skb);
23
24 iph->ihl = 5;
25 iph->version = 4;
26
27 iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
28 iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
29
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;
33}
34
Diego Beltrami0a694522006-10-03 23:47:05 -070035/* Add encapsulation header.
36 *
37 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
Diego Beltrami0a694522006-10-03 23:47:05 -070038 */
39static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
40{
Herbert Xu37fedd32007-10-10 15:44:44 -070041 struct ip_beet_phdr *ph;
Herbert Xu732c8bd2008-03-26 16:51:09 -070042 struct iphdr *top_iph;
Diego Beltrami0a694522006-10-03 23:47:05 -070043 int hdrlen, optlen;
44
Diego Beltrami0a694522006-10-03 23:47:05 -070045 hdrlen = 0;
Herbert Xu732c8bd2008-03-26 16:51:09 -070046 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070047 if (unlikely(optlen))
48 hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
49
Joakim Koskelaeb49e632008-08-06 02:39:30 -070050 skb_set_network_header(skb, -x->props.header_len -
Weilong Chen5797deb2013-12-23 14:37:31 +080051 hdrlen + (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
Joakim Koskelaeb49e632008-08-06 02:39:30 -070052 if (x->sel.family != AF_INET6)
53 skb->network_header += IPV4_BEET_PHMAXLEN;
Herbert Xu37fedd32007-10-10 15:44:44 -070054 skb->mac_header = skb->network_header +
55 offsetof(struct iphdr, protocol);
Herbert Xu732c8bd2008-03-26 16:51:09 -070056 skb->transport_header = skb->network_header + sizeof(*top_iph);
Diego Beltrami0a694522006-10-03 23:47:05 -070057
Herbert Xu227620e2007-11-13 21:41:28 -080058 xfrm4_beet_make_header(skb);
59
Johannes Bergaf728682017-06-16 14:29:22 +020060 ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
Herbert Xu37fedd32007-10-10 15:44:44 -070061
62 top_iph = ip_hdr(skb);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080063
Diego Beltrami0a694522006-10-03 23:47:05 -070064 if (unlikely(optlen)) {
Diego Beltrami0a694522006-10-03 23:47:05 -070065 BUG_ON(optlen < 0);
66
Diego Beltrami0a694522006-10-03 23:47:05 -070067 ph->padlen = 4 - (optlen & 4);
Patrick McHardy05d22442007-04-23 22:39:02 -070068 ph->hdrlen = optlen / 8;
Diego Beltrami0a694522006-10-03 23:47:05 -070069 ph->nexthdr = top_iph->protocol;
Patrick McHardy04fef982007-04-05 15:54:39 -070070 if (ph->padlen)
71 memset(ph + 1, IPOPT_NOP, ph->padlen);
Diego Beltrami0a694522006-10-03 23:47:05 -070072
73 top_iph->protocol = IPPROTO_BEETPH;
74 top_iph->ihl = sizeof(struct iphdr) / 4;
75 }
76
77 top_iph->saddr = x->props.saddr.a4;
78 top_iph->daddr = x->id.daddr.a4;
79
80 return 0;
81}
82
83static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
84{
Herbert Xu227620e2007-11-13 21:41:28 -080085 struct iphdr *iph;
Diego Beltrami0a694522006-10-03 23:47:05 -070086 int optlen = 0;
Diego Beltrami0a694522006-10-03 23:47:05 -070087 int err = -EINVAL;
88
Herbert Xu227620e2007-11-13 21:41:28 -080089 if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
Patrick McHardy254d0d22007-04-05 16:03:33 -070090 struct ip_beet_phdr *ph;
Herbert Xu227620e2007-11-13 21:41:28 -080091 int phlen;
Diego Beltrami0a694522006-10-03 23:47:05 -070092
93 if (!pskb_may_pull(skb, sizeof(*ph)))
94 goto out;
Herbert Xu227620e2007-11-13 21:41:28 -080095
96 ph = (struct ip_beet_phdr *)skb->data;
Diego Beltrami0a694522006-10-03 23:47:05 -070097
Patrick McHardyd4b1e842007-04-05 15:59:41 -070098 phlen = sizeof(*ph) + ph->padlen;
Patrick McHardy05d22442007-04-23 22:39:02 -070099 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
Diego Beltrami0a694522006-10-03 23:47:05 -0700100 if (optlen < 0 || optlen & 3 || optlen > 250)
101 goto out;
102
Herbert Xu227620e2007-11-13 21:41:28 -0800103 XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
Diego Beltrami0a694522006-10-03 23:47:05 -0700104
Adrian Bunk322c8a32008-02-05 02:51:39 -0800105 if (!pskb_may_pull(skb, phlen))
Herbert Xu227620e2007-11-13 21:41:28 -0800106 goto out;
107 __skb_pull(skb, phlen);
Diego Beltrami0a694522006-10-03 23:47:05 -0700108 }
109
Herbert Xu227620e2007-11-13 21:41:28 -0800110 skb_push(skb, sizeof(*iph));
111 skb_reset_network_header(skb);
Eric Dumazet03606892012-02-23 10:55:02 +0000112 skb_mac_header_rebuild(skb);
Herbert Xu227620e2007-11-13 21:41:28 -0800113
114 xfrm4_beet_make_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700115
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700116 iph = ip_hdr(skb);
Herbert Xu227620e2007-11-13 21:41:28 -0800117
118 iph->ihl += optlen / 4;
119 iph->tot_len = htons(skb->len);
Diego Beltrami0a694522006-10-03 23:47:05 -0700120 iph->daddr = x->sel.daddr.a4;
121 iph->saddr = x->sel.saddr.a4;
Diego Beltrami0a694522006-10-03 23:47:05 -0700122 iph->check = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700123 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
Diego Beltrami0a694522006-10-03 23:47:05 -0700124 err = 0;
125out:
126 return err;
127}
128
129static struct xfrm_mode xfrm4_beet_mode = {
Herbert Xu227620e2007-11-13 21:41:28 -0800130 .input2 = xfrm4_beet_input,
131 .input = xfrm_prepare_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800132 .output2 = xfrm4_beet_output,
133 .output = xfrm4_prepare_output,
Diego Beltrami0a694522006-10-03 23:47:05 -0700134 .owner = THIS_MODULE,
135 .encap = XFRM_MODE_BEET,
Herbert Xu1bfcb102007-10-17 21:31:50 -0700136 .flags = XFRM_MODE_FLAG_TUNNEL,
Diego Beltrami0a694522006-10-03 23:47:05 -0700137};
138
139static int __init xfrm4_beet_init(void)
140{
141 return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
142}
143
144static void __exit xfrm4_beet_exit(void)
145{
146 int err;
147
148 err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
149 BUG_ON(err);
150}
151
152module_init(xfrm4_beet_init);
153module_exit(xfrm4_beet_exit);
154MODULE_LICENSE("GPL");
155MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);