blob: bca018d19ec6e540f6dee7850149cc3d84e8fddb [file] [log] [blame]
Diego Beltrami0a694522006-10-03 23:47:05 -07001/*
2 * xfrm6_mode_beet.c - BEET mode encapsulation for IPv6.
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/dsfield.h>
17#include <net/dst.h>
18#include <net/inet_ecn.h>
19#include <net/ipv6.h>
20#include <net/xfrm.h>
21
22/* Add encapsulation header.
23 *
24 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
25 * The following fields in it shall be filled in by x->type->output:
26 * payload_len
27 *
28 * On exit, skb->h will be set to the start of the encapsulation header to be
Herbert Xu007f0212007-10-09 13:25:59 -070029 * filled in by x->type->output and the mac header will be set to the
30 * nextheader field of the extension header directly preceding the
31 * encapsulation header, or in its absence, that of the top IP header.
Herbert Xu7b277b12007-10-10 15:44:06 -070032 * The value of the network header will always point to the top IP header
33 * while skb->data will point to the payload.
Diego Beltrami0a694522006-10-03 23:47:05 -070034 */
35static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
36{
37 struct ipv6hdr *iph, *top_iph;
38 u8 *prevhdr;
39 int hdr_len;
40
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070041 iph = ipv6_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070042
43 hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
Diego Beltrami0a694522006-10-03 23:47:05 -070044
Herbert Xubee0b402007-10-10 15:42:23 -070045 skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
Herbert Xu7b277b12007-10-10 15:44:06 -070046 skb_set_network_header(skb, -x->props.header_len);
47 skb_set_transport_header(skb, hdr_len - x->props.header_len);
48 __skb_pull(skb, hdr_len);
49
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070050 top_iph = ipv6_hdr(skb);
Herbert Xu7b277b12007-10-10 15:44:06 -070051 memmove(top_iph, iph, hdr_len);
Diego Beltrami0a694522006-10-03 23:47:05 -070052
53 ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
54 ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
55
56 return 0;
57}
58
59static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
60{
61 struct ipv6hdr *ip6h;
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030062 const unsigned char *old_mac;
Diego Beltrami0a694522006-10-03 23:47:05 -070063 int size = sizeof(struct ipv6hdr);
64 int err = -EINVAL;
65
66 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
67 goto out;
68
69 skb_push(skb, size);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070070 memmove(skb->data, skb_network_header(skb), size);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070071 skb_reset_network_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070072
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070073 old_mac = skb_mac_header(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030074 skb_set_mac_header(skb, -skb->mac_len);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070075 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
Diego Beltrami0a694522006-10-03 23:47:05 -070076
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070077 ip6h = ipv6_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070078 ip6h->payload_len = htons(skb->len - size);
79 ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
80 ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
81 err = 0;
82out:
83 return err;
84}
85
86static struct xfrm_mode xfrm6_beet_mode = {
87 .input = xfrm6_beet_input,
88 .output = xfrm6_beet_output,
89 .owner = THIS_MODULE,
90 .encap = XFRM_MODE_BEET,
91};
92
93static int __init xfrm6_beet_init(void)
94{
95 return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
96}
97
98static void __exit xfrm6_beet_exit(void)
99{
100 int err;
101
102 err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
103 BUG_ON(err);
104}
105
106module_init(xfrm6_beet_init);
107module_exit(xfrm6_beet_exit);
108MODULE_LICENSE("GPL");
109MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);