Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4. |
| 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/dst.h> |
| 17 | #include <net/ip.h> |
| 18 | #include <net/xfrm.h> |
| 19 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 20 | static void xfrm4_beet_make_header(struct sk_buff *skb) |
| 21 | { |
| 22 | struct iphdr *iph = ip_hdr(skb); |
| 23 | |
| 24 | iph->ihl = 5; |
| 25 | iph->version = 4; |
| 26 | |
| 27 | iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol; |
| 28 | iph->tos = XFRM_MODE_SKB_CB(skb)->tos; |
| 29 | |
| 30 | iph->id = XFRM_MODE_SKB_CB(skb)->id; |
| 31 | iph->frag_off = XFRM_MODE_SKB_CB(skb)->frag_off; |
| 32 | iph->ttl = XFRM_MODE_SKB_CB(skb)->ttl; |
| 33 | } |
| 34 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 35 | /* Add encapsulation header. |
| 36 | * |
| 37 | * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt. |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 38 | */ |
| 39 | static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb) |
| 40 | { |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 41 | struct ip_beet_phdr *ph; |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 42 | struct iphdr *top_iph; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 43 | int hdrlen, optlen; |
| 44 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 45 | hdrlen = 0; |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 46 | optlen = XFRM_MODE_SKB_CB(skb)->optlen; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 47 | if (unlikely(optlen)) |
| 48 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); |
| 49 | |
Joakim Koskela | eb49e63 | 2008-08-06 02:39:30 -0700 | [diff] [blame] | 50 | skb_set_network_header(skb, -x->props.header_len - |
| 51 | hdrlen + (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph))); |
| 52 | if (x->sel.family != AF_INET6) |
| 53 | skb->network_header += IPV4_BEET_PHMAXLEN; |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 54 | skb->mac_header = skb->network_header + |
| 55 | offsetof(struct iphdr, protocol); |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 56 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 57 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 58 | xfrm4_beet_make_header(skb); |
| 59 | |
Herbert Xu | 732c8bd | 2008-03-26 16:51:09 -0700 | [diff] [blame] | 60 | ph = (struct ip_beet_phdr *) |
| 61 | __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen); |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 62 | |
| 63 | top_iph = ip_hdr(skb); |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 64 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 65 | if (unlikely(optlen)) { |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 66 | BUG_ON(optlen < 0); |
| 67 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 68 | ph->padlen = 4 - (optlen & 4); |
Patrick McHardy | 05d2244 | 2007-04-23 22:39:02 -0700 | [diff] [blame] | 69 | ph->hdrlen = optlen / 8; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 70 | ph->nexthdr = top_iph->protocol; |
Patrick McHardy | 04fef98 | 2007-04-05 15:54:39 -0700 | [diff] [blame] | 71 | if (ph->padlen) |
| 72 | memset(ph + 1, IPOPT_NOP, ph->padlen); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 73 | |
| 74 | top_iph->protocol = IPPROTO_BEETPH; |
| 75 | top_iph->ihl = sizeof(struct iphdr) / 4; |
| 76 | } |
| 77 | |
| 78 | top_iph->saddr = x->props.saddr.a4; |
| 79 | top_iph->daddr = x->id.daddr.a4; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb) |
| 85 | { |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 86 | struct iphdr *iph; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 87 | int optlen = 0; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 88 | int err = -EINVAL; |
| 89 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 90 | if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) { |
Patrick McHardy | 254d0d2 | 2007-04-05 16:03:33 -0700 | [diff] [blame] | 91 | struct ip_beet_phdr *ph; |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 92 | int phlen; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 93 | |
| 94 | if (!pskb_may_pull(skb, sizeof(*ph))) |
| 95 | goto out; |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 96 | |
| 97 | ph = (struct ip_beet_phdr *)skb->data; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 98 | |
Patrick McHardy | d4b1e84 | 2007-04-05 15:59:41 -0700 | [diff] [blame] | 99 | phlen = sizeof(*ph) + ph->padlen; |
Patrick McHardy | 05d2244 | 2007-04-23 22:39:02 -0700 | [diff] [blame] | 100 | optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 101 | if (optlen < 0 || optlen & 3 || optlen > 250) |
| 102 | goto out; |
| 103 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 104 | XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 105 | |
Adrian Bunk | 322c8a3 | 2008-02-05 02:51:39 -0800 | [diff] [blame] | 106 | if (!pskb_may_pull(skb, phlen)) |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 107 | goto out; |
| 108 | __skb_pull(skb, phlen); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 111 | skb_push(skb, sizeof(*iph)); |
| 112 | skb_reset_network_header(skb); |
| 113 | |
| 114 | memmove(skb->data - skb->mac_len, skb_mac_header(skb), |
| 115 | skb->mac_len); |
| 116 | skb_set_mac_header(skb, -skb->mac_len); |
| 117 | |
| 118 | xfrm4_beet_make_header(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 119 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 120 | iph = ip_hdr(skb); |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 121 | |
| 122 | iph->ihl += optlen / 4; |
| 123 | iph->tot_len = htons(skb->len); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 124 | iph->daddr = x->sel.daddr.a4; |
| 125 | iph->saddr = x->sel.saddr.a4; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 126 | iph->check = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 127 | iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 128 | err = 0; |
| 129 | out: |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static struct xfrm_mode xfrm4_beet_mode = { |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 134 | .input2 = xfrm4_beet_input, |
| 135 | .input = xfrm_prepare_input, |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 136 | .output2 = xfrm4_beet_output, |
| 137 | .output = xfrm4_prepare_output, |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 138 | .owner = THIS_MODULE, |
| 139 | .encap = XFRM_MODE_BEET, |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 140 | .flags = XFRM_MODE_FLAG_TUNNEL, |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static int __init xfrm4_beet_init(void) |
| 144 | { |
| 145 | return xfrm_register_mode(&xfrm4_beet_mode, AF_INET); |
| 146 | } |
| 147 | |
| 148 | static void __exit xfrm4_beet_exit(void) |
| 149 | { |
| 150 | int err; |
| 151 | |
| 152 | err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET); |
| 153 | BUG_ON(err); |
| 154 | } |
| 155 | |
| 156 | module_init(xfrm4_beet_init); |
| 157 | module_exit(xfrm4_beet_exit); |
| 158 | MODULE_LICENSE("GPL"); |
| 159 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET); |