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 | |
| 20 | /* Add encapsulation header. |
| 21 | * |
| 22 | * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt. |
| 23 | * The following fields in it shall be filled in by x->type->output: |
| 24 | * tot_len |
| 25 | * check |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 26 | */ |
| 27 | static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb) |
| 28 | { |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame^] | 29 | struct ip_beet_phdr *ph; |
Patrick McHardy | ea2f10a | 2007-04-05 16:04:04 -0700 | [diff] [blame] | 30 | struct iphdr *iph, *top_iph; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 31 | int hdrlen, optlen; |
| 32 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 33 | iph = ip_hdr(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 34 | |
| 35 | hdrlen = 0; |
| 36 | optlen = iph->ihl * 4 - sizeof(*iph); |
| 37 | if (unlikely(optlen)) |
| 38 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); |
| 39 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 40 | skb_set_network_header(skb, IPV4_BEET_PHMAXLEN - x->props.header_len - |
| 41 | hdrlen); |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame^] | 42 | skb->mac_header = skb->network_header + |
| 43 | offsetof(struct iphdr, protocol); |
| 44 | skb->transport_header = skb->network_header + sizeof(*iph); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 45 | |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame^] | 46 | ph = (struct ip_beet_phdr *)__skb_pull(skb, sizeof(*iph) - hdrlen); |
| 47 | |
| 48 | top_iph = ip_hdr(skb); |
Patrick McHardy | c5027c9 | 2007-04-05 15:54:02 -0700 | [diff] [blame] | 49 | memmove(top_iph, iph, sizeof(*iph)); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 50 | if (unlikely(optlen)) { |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 51 | BUG_ON(optlen < 0); |
| 52 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 53 | ph->padlen = 4 - (optlen & 4); |
Patrick McHardy | 05d2244 | 2007-04-23 22:39:02 -0700 | [diff] [blame] | 54 | ph->hdrlen = optlen / 8; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 55 | ph->nexthdr = top_iph->protocol; |
Patrick McHardy | 04fef98 | 2007-04-05 15:54:39 -0700 | [diff] [blame] | 56 | if (ph->padlen) |
| 57 | memset(ph + 1, IPOPT_NOP, ph->padlen); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 58 | |
| 59 | top_iph->protocol = IPPROTO_BEETPH; |
| 60 | top_iph->ihl = sizeof(struct iphdr) / 4; |
| 61 | } |
| 62 | |
| 63 | top_iph->saddr = x->props.saddr.a4; |
| 64 | top_iph->daddr = x->id.daddr.a4; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb) |
| 70 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 71 | struct iphdr *iph = ip_hdr(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 72 | int phlen = 0; |
| 73 | int optlen = 0; |
Patrick McHardy | ea2f10a | 2007-04-05 16:04:04 -0700 | [diff] [blame] | 74 | u8 ph_nexthdr = 0; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 75 | int err = -EINVAL; |
| 76 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 77 | if (unlikely(iph->protocol == IPPROTO_BEETPH)) { |
Patrick McHardy | 254d0d2 | 2007-04-05 16:03:33 -0700 | [diff] [blame] | 78 | struct ip_beet_phdr *ph; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 79 | |
| 80 | if (!pskb_may_pull(skb, sizeof(*ph))) |
| 81 | goto out; |
Arnaldo Carvalho de Melo | b0061ce | 2007-04-25 18:02:22 -0700 | [diff] [blame] | 82 | ph = (struct ip_beet_phdr *)(ipip_hdr(skb) + 1); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 83 | |
Patrick McHardy | d4b1e84 | 2007-04-05 15:59:41 -0700 | [diff] [blame] | 84 | phlen = sizeof(*ph) + ph->padlen; |
Patrick McHardy | 05d2244 | 2007-04-23 22:39:02 -0700 | [diff] [blame] | 85 | optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 86 | if (optlen < 0 || optlen & 3 || optlen > 250) |
| 87 | goto out; |
| 88 | |
Patrick McHardy | d4b1e84 | 2007-04-05 15:59:41 -0700 | [diff] [blame] | 89 | if (!pskb_may_pull(skb, phlen + optlen)) |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 90 | goto out; |
Patrick McHardy | 254d0d2 | 2007-04-05 16:03:33 -0700 | [diff] [blame] | 91 | skb->len -= phlen + optlen; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 92 | |
| 93 | ph_nexthdr = ph->nexthdr; |
| 94 | } |
| 95 | |
Arnaldo Carvalho de Melo | b0061ce | 2007-04-25 18:02:22 -0700 | [diff] [blame] | 96 | skb_set_network_header(skb, phlen - sizeof(*iph)); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 97 | memmove(skb_network_header(skb), iph, sizeof(*iph)); |
Arnaldo Carvalho de Melo | b0061ce | 2007-04-25 18:02:22 -0700 | [diff] [blame] | 98 | skb_set_transport_header(skb, phlen + optlen); |
David S. Miller | 239254f | 2007-04-19 19:55:44 -0700 | [diff] [blame] | 99 | skb->data = skb_transport_header(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 101 | iph = ip_hdr(skb); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 102 | iph->ihl = (sizeof(*iph) + optlen) / 4; |
Patrick McHardy | d4b1e84 | 2007-04-05 15:59:41 -0700 | [diff] [blame] | 103 | iph->tot_len = htons(skb->len + iph->ihl * 4); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 104 | iph->daddr = x->sel.daddr.a4; |
| 105 | iph->saddr = x->sel.saddr.a4; |
| 106 | if (ph_nexthdr) |
| 107 | iph->protocol = ph_nexthdr; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 108 | iph->check = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 109 | iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 110 | err = 0; |
| 111 | out: |
| 112 | return err; |
| 113 | } |
| 114 | |
| 115 | static struct xfrm_mode xfrm4_beet_mode = { |
| 116 | .input = xfrm4_beet_input, |
| 117 | .output = xfrm4_beet_output, |
| 118 | .owner = THIS_MODULE, |
| 119 | .encap = XFRM_MODE_BEET, |
| 120 | }; |
| 121 | |
| 122 | static int __init xfrm4_beet_init(void) |
| 123 | { |
| 124 | return xfrm_register_mode(&xfrm4_beet_mode, AF_INET); |
| 125 | } |
| 126 | |
| 127 | static void __exit xfrm4_beet_exit(void) |
| 128 | { |
| 129 | int err; |
| 130 | |
| 131 | err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET); |
| 132 | BUG_ON(err); |
| 133 | } |
| 134 | |
| 135 | module_init(xfrm4_beet_init); |
| 136 | module_exit(xfrm4_beet_exit); |
| 137 | MODULE_LICENSE("GPL"); |
| 138 | MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET); |