blob: bbd48b101baeebada8a69f5eda7f7ad5087910db [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;
Joakim Koskelaabf5cdb2008-08-06 02:40:25 -070043 struct ip_beet_phdr *ph;
44 struct iphdr *iphv4;
45 int optlen, hdr_len;
Diego Beltrami0a694522006-10-03 23:47:05 -070046
Joakim Koskelaabf5cdb2008-08-06 02:40:25 -070047 iphv4 = ip_hdr(skb);
48 hdr_len = 0;
49 optlen = XFRM_MODE_SKB_CB(skb)->optlen;
50 if (unlikely(optlen))
51 hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
52
53 skb_set_network_header(skb, -x->props.header_len - hdr_len);
54 if (x->sel.family != AF_INET6)
55 skb->network_header += IPV4_BEET_PHMAXLEN;
Herbert Xu36cf9ac2007-11-13 21:40:52 -080056 skb->mac_header = skb->network_header +
57 offsetof(struct ipv6hdr, nexthdr);
58 skb->transport_header = skb->network_header + sizeof(*top_iph);
Joakim Koskelaabf5cdb2008-08-06 02:40:25 -070059 ph = (struct ip_beet_phdr *)__skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl-hdr_len);
Herbert Xu227620e2007-11-13 21:41:28 -080060
61 xfrm6_beet_make_header(skb);
62
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070063 top_iph = ipv6_hdr(skb);
Joakim Koskelaabf5cdb2008-08-06 02:40:25 -070064 if (unlikely(optlen)) {
65
66 BUG_ON(optlen < 0);
67
68 ph->padlen = 4 - (optlen & 4);
69 ph->hdrlen = optlen / 8;
70 ph->nexthdr = top_iph->nexthdr;
71 if (ph->padlen)
72 memset(ph + 1, IPOPT_NOP, ph->padlen);
73
74 top_iph->nexthdr = IPPROTO_BEETPH;
75 }
Diego Beltrami0a694522006-10-03 23:47:05 -070076
77 ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
78 ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
Diego Beltrami0a694522006-10-03 23:47:05 -070079 return 0;
80}
81
82static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
83{
84 struct ipv6hdr *ip6h;
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030085 const unsigned char *old_mac;
Diego Beltrami0a694522006-10-03 23:47:05 -070086 int size = sizeof(struct ipv6hdr);
Herbert Xu227620e2007-11-13 21:41:28 -080087 int err;
Diego Beltrami0a694522006-10-03 23:47:05 -070088
Herbert Xu227620e2007-11-13 21:41:28 -080089 err = skb_cow_head(skb, size + skb->mac_len);
90 if (err)
Diego Beltrami0a694522006-10-03 23:47:05 -070091 goto out;
92
Herbert Xu227620e2007-11-13 21:41:28 -080093 __skb_push(skb, size);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070094 skb_reset_network_header(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -070095
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070096 old_mac = skb_mac_header(skb);
Arnaldo Carvalho de Melo39f69c62007-03-10 12:40:27 -030097 skb_set_mac_header(skb, -skb->mac_len);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070098 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
Diego Beltrami0a694522006-10-03 23:47:05 -070099
Herbert Xu227620e2007-11-13 21:41:28 -0800100 xfrm6_beet_make_header(skb);
101
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700102 ip6h = ipv6_hdr(skb);
Diego Beltrami0a694522006-10-03 23:47:05 -0700103 ip6h->payload_len = htons(skb->len - size);
104 ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
105 ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
106 err = 0;
107out:
108 return err;
109}
110
111static struct xfrm_mode xfrm6_beet_mode = {
Herbert Xu227620e2007-11-13 21:41:28 -0800112 .input2 = xfrm6_beet_input,
113 .input = xfrm_prepare_input,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800114 .output2 = xfrm6_beet_output,
115 .output = xfrm6_prepare_output,
Diego Beltrami0a694522006-10-03 23:47:05 -0700116 .owner = THIS_MODULE,
117 .encap = XFRM_MODE_BEET,
Herbert Xu1bfcb102007-10-17 21:31:50 -0700118 .flags = XFRM_MODE_FLAG_TUNNEL,
Diego Beltrami0a694522006-10-03 23:47:05 -0700119};
120
121static int __init xfrm6_beet_init(void)
122{
123 return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
124}
125
126static void __exit xfrm6_beet_exit(void)
127{
128 int err;
129
130 err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
131 BUG_ON(err);
132}
133
134module_init(xfrm6_beet_init);
135module_exit(xfrm6_beet_exit);
136MODULE_LICENSE("GPL");
137MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);