blob: d6ce400f585f69675a17bd96ae4036492dcf1efb [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
Herbert Xu227620e2007-11-13 21:41:28 -080022static void xfrm6_beet_make_header(struct sk_buff *skb)
23{
24 struct ipv6hdr *iph = ipv6_hdr(skb);
25
26 iph->version = 6;
27
28 memcpy(iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
29 sizeof(iph->flow_lbl));
30 iph->nexthdr = XFRM_MODE_SKB_CB(skb)->protocol;
31
32 ipv6_change_dsfield(iph, 0, XFRM_MODE_SKB_CB(skb)->tos);
33 iph->hop_limit = XFRM_MODE_SKB_CB(skb)->ttl;
34}
35
Diego Beltrami0a694522006-10-03 23:47:05 -070036/* Add encapsulation header.
37 *
38 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
Diego Beltrami0a694522006-10-03 23:47:05 -070039 */
40static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
41{
Herbert Xu36cf9ac2007-11-13 21:40:52 -080042 struct ipv6hdr *top_iph;
Diego Beltrami0a694522006-10-03 23:47:05 -070043
Herbert Xu7b277b12007-10-10 15:44:06 -070044 skb_set_network_header(skb, -x->props.header_len);
Herbert Xu36cf9ac2007-11-13 21:40:52 -080045 skb->mac_header = skb->network_header +
46 offsetof(struct ipv6hdr, nexthdr);
47 skb->transport_header = skb->network_header + sizeof(*top_iph);
Herbert Xu732c8bd2008-03-26 16:51:09 -070048 __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl);
Herbert Xu227620e2007-11-13 21:41:28 -080049
50 xfrm6_beet_make_header(skb);
51
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070052 top_iph = ipv6_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070053
54 ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
55 ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
Diego Beltrami0a694522006-10-03 23:47:05 -070056 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);
Herbert Xu227620e2007-11-13 21:41:28 -080064 int err;
Diego Beltrami0a694522006-10-03 23:47:05 -070065
Herbert Xu227620e2007-11-13 21:41:28 -080066 err = skb_cow_head(skb, size + skb->mac_len);
67 if (err)
Diego Beltrami0a694522006-10-03 23:47:05 -070068 goto out;
69
Herbert Xu227620e2007-11-13 21:41:28 -080070 __skb_push(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
Herbert Xu227620e2007-11-13 21:41:28 -080077 xfrm6_beet_make_header(skb);
78
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070079 ip6h = ipv6_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070080 ip6h->payload_len = htons(skb->len - size);
81 ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
82 ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
83 err = 0;
84out:
85 return err;
86}
87
88static struct xfrm_mode xfrm6_beet_mode = {
Herbert Xu227620e2007-11-13 21:41:28 -080089 .input2 = xfrm6_beet_input,
90 .input = xfrm_prepare_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -080091 .output2 = xfrm6_beet_output,
92 .output = xfrm6_prepare_output,
Diego Beltrami0a694522006-10-03 23:47:05 -070093 .owner = THIS_MODULE,
94 .encap = XFRM_MODE_BEET,
Herbert Xu1bfcb102007-10-17 21:31:50 -070095 .flags = XFRM_MODE_FLAG_TUNNEL,
Diego Beltrami0a694522006-10-03 23:47:05 -070096};
97
98static int __init xfrm6_beet_init(void)
99{
100 return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
101}
102
103static void __exit xfrm6_beet_exit(void)
104{
105 int err;
106
107 err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
108 BUG_ON(err);
109}
110
111module_init(xfrm6_beet_init);
112module_exit(xfrm6_beet_exit);
113MODULE_LICENSE("GPL");
114MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);