]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv6/xfrm6_mode_beet.c
4988ed9c76c62da8dffc1db86b29d8bc073253ac
[linux-2.6-omap-h63xx.git] / net / ipv6 / xfrm6_mode_beet.c
1 /*
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  */
26 static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
27 {
28         struct ipv6hdr *top_iph;
29
30         skb_set_network_header(skb, -x->props.header_len);
31         skb->mac_header = skb->network_header +
32                           offsetof(struct ipv6hdr, nexthdr);
33         skb->transport_header = skb->network_header + sizeof(*top_iph);
34         top_iph = ipv6_hdr(skb);
35
36         top_iph->version = 6;
37
38         memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
39                sizeof(top_iph->flow_lbl));
40         top_iph->nexthdr = XFRM_MODE_SKB_CB(skb)->protocol;
41
42         ipv6_change_dsfield(top_iph, 0, XFRM_MODE_SKB_CB(skb)->tos);
43         top_iph->hop_limit = XFRM_MODE_SKB_CB(skb)->ttl;
44         ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
45         ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
46         return 0;
47 }
48
49 static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
50 {
51         struct ipv6hdr *ip6h;
52         const unsigned char *old_mac;
53         int size = sizeof(struct ipv6hdr);
54         int err = -EINVAL;
55
56         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
57                 goto out;
58
59         skb_push(skb, size);
60         memmove(skb->data, skb_network_header(skb), size);
61         skb_reset_network_header(skb);
62
63         old_mac = skb_mac_header(skb);
64         skb_set_mac_header(skb, -skb->mac_len);
65         memmove(skb_mac_header(skb), old_mac, skb->mac_len);
66
67         ip6h = ipv6_hdr(skb);
68         ip6h->payload_len = htons(skb->len - size);
69         ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
70         ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
71         err = 0;
72 out:
73         return err;
74 }
75
76 static struct xfrm_mode xfrm6_beet_mode = {
77         .input = xfrm6_beet_input,
78         .output2 = xfrm6_beet_output,
79         .output = xfrm6_prepare_output,
80         .owner = THIS_MODULE,
81         .encap = XFRM_MODE_BEET,
82         .flags = XFRM_MODE_FLAG_TUNNEL,
83 };
84
85 static int __init xfrm6_beet_init(void)
86 {
87         return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
88 }
89
90 static void __exit xfrm6_beet_exit(void)
91 {
92         int err;
93
94         err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
95         BUG_ON(err);
96 }
97
98 module_init(xfrm6_beet_init);
99 module_exit(xfrm6_beet_exit);
100 MODULE_LICENSE("GPL");
101 MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);