Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 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 | * 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 |
| 29 | * filled in by x->type->output and skb->nh will be set to the nextheader field |
| 30 | * of the extension header directly preceding the encapsulation header, or in |
| 31 | * its absence, that of the top IP header. The value of skb->data will always |
| 32 | * point to the top IP header. |
| 33 | */ |
| 34 | static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb) |
| 35 | { |
| 36 | struct ipv6hdr *iph, *top_iph; |
| 37 | u8 *prevhdr; |
| 38 | int hdr_len; |
| 39 | |
| 40 | skb_push(skb, x->props.header_len); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 41 | iph = ipv6_hdr(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 42 | |
| 43 | hdr_len = ip6_find_1stfragopt(skb, &prevhdr); |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame^] | 44 | skb_set_network_header(skb, |
| 45 | (prevhdr - x->props.header_len) - skb->data); |
Arnaldo Carvalho de Melo | 967b05f | 2007-03-13 13:51:52 -0300 | [diff] [blame] | 46 | skb_set_transport_header(skb, hdr_len); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 47 | memmove(skb->data, iph, hdr_len); |
| 48 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 49 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 50 | top_iph = ipv6_hdr(skb); |
Arnaldo Carvalho de Melo | 39b8916 | 2007-04-10 21:06:25 -0700 | [diff] [blame] | 51 | skb->h.raw = skb->nh.raw + sizeof(struct ipv6hdr); |
| 52 | skb->nh.raw += offsetof(struct ipv6hdr, nexthdr); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 53 | |
| 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); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb) |
| 61 | { |
| 62 | struct ipv6hdr *ip6h; |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 63 | const unsigned char *old_mac; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 64 | int size = sizeof(struct ipv6hdr); |
| 65 | int err = -EINVAL; |
| 66 | |
| 67 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 68 | goto out; |
| 69 | |
| 70 | skb_push(skb, size); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 71 | memmove(skb->data, skb_network_header(skb), size); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 72 | skb_reset_network_header(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 73 | |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 74 | old_mac = skb_mac_header(skb); |
Arnaldo Carvalho de Melo | 39f69c6 | 2007-03-10 12:40:27 -0300 | [diff] [blame] | 75 | skb_set_mac_header(skb, -skb->mac_len); |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 76 | memmove(skb_mac_header(skb), old_mac, skb->mac_len); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 77 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 78 | ip6h = ipv6_hdr(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 79 | ip6h->payload_len = htons(skb->len - size); |
| 80 | ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6); |
| 81 | ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6); |
| 82 | err = 0; |
| 83 | out: |
| 84 | return err; |
| 85 | } |
| 86 | |
| 87 | static struct xfrm_mode xfrm6_beet_mode = { |
| 88 | .input = xfrm6_beet_input, |
| 89 | .output = xfrm6_beet_output, |
| 90 | .owner = THIS_MODULE, |
| 91 | .encap = XFRM_MODE_BEET, |
| 92 | }; |
| 93 | |
| 94 | static int __init xfrm6_beet_init(void) |
| 95 | { |
| 96 | return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6); |
| 97 | } |
| 98 | |
| 99 | static void __exit xfrm6_beet_exit(void) |
| 100 | { |
| 101 | int err; |
| 102 | |
| 103 | err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6); |
| 104 | BUG_ON(err); |
| 105 | } |
| 106 | |
| 107 | module_init(xfrm6_beet_init); |
| 108 | module_exit(xfrm6_beet_exit); |
| 109 | MODULE_LICENSE("GPL"); |
| 110 | MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET); |