Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm6_mode_transport.c - Transport mode encapsulation for IPv6. |
| 3 | * |
| 4 | * Copyright (C) 2002 USAGI/WIDE Project |
| 5 | * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/stringify.h> |
| 13 | #include <net/dst.h> |
| 14 | #include <net/ipv6.h> |
| 15 | #include <net/xfrm.h> |
Steffen Klassert | c35fe41 | 2017-04-14 10:06:01 +0200 | [diff] [blame] | 16 | #include <net/protocol.h> |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 17 | |
| 18 | /* Add encapsulation header. |
| 19 | * |
| 20 | * The IP header and mutable extension headers will be moved forward to make |
| 21 | * space for the encapsulation header. |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 22 | */ |
Jamal Hadi Salim | eb878e8 | 2006-08-31 17:42:59 -0700 | [diff] [blame] | 23 | static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb) |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 24 | { |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 25 | struct ipv6hdr *iph; |
| 26 | u8 *prevhdr; |
| 27 | int hdr_len; |
| 28 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 29 | iph = ipv6_hdr(skb); |
Ilan Tayari | f1bd7d6 | 2017-04-14 10:07:39 +0200 | [diff] [blame] | 30 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 31 | |
Masahide NAKAMURA | aee5adb | 2006-08-23 17:57:28 -0700 | [diff] [blame] | 32 | hdr_len = x->type->hdr_offset(x, skb, &prevhdr); |
Ben Hutchings | 6e80ac5 | 2017-05-31 13:15:41 +0100 | [diff] [blame] | 33 | if (hdr_len < 0) |
| 34 | return hdr_len; |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 35 | skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data); |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 36 | skb_set_network_header(skb, -x->props.header_len); |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 37 | skb->transport_header = skb->network_header + hdr_len; |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 38 | __skb_pull(skb, hdr_len); |
| 39 | memmove(ipv6_hdr(skb), iph, hdr_len); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 43 | /* Remove encapsulation header. |
| 44 | * |
| 45 | * The IP header will be moved over the top of the encapsulation header. |
| 46 | * |
| 47 | * On entry, skb->h shall point to where the IP header should be and skb->nh |
| 48 | * shall be set to where the IP header currently is. skb->data shall point |
| 49 | * to the start of the payload. |
| 50 | */ |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 51 | static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb) |
| 52 | { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 53 | int ihl = skb->data - skb_transport_header(skb); |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 54 | struct xfrm_offload *xo = xfrm_offload(skb); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 55 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 56 | if (skb->transport_header != skb->network_header) { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 57 | memmove(skb_transport_header(skb), |
| 58 | skb_network_header(skb), ihl); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 59 | skb->network_header = skb->transport_header; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 60 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 61 | ipv6_hdr(skb)->payload_len = htons(skb->len + ihl - |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 62 | sizeof(struct ipv6hdr)); |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 63 | if (!xo || !(xo->flags & XFRM_GRO)) |
| 64 | skb_reset_transport_header(skb); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Steffen Klassert | c35fe41 | 2017-04-14 10:06:01 +0200 | [diff] [blame] | 68 | static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x, |
| 69 | struct sk_buff *skb, |
| 70 | netdev_features_t features) |
| 71 | { |
| 72 | const struct net_offload *ops; |
| 73 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 74 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 75 | |
| 76 | skb->transport_header += x->props.header_len; |
| 77 | ops = rcu_dereference(inet6_offloads[xo->proto]); |
| 78 | if (likely(ops && ops->callbacks.gso_segment)) |
| 79 | segs = ops->callbacks.gso_segment(skb, features); |
| 80 | |
| 81 | return segs; |
| 82 | } |
| 83 | |
| 84 | static void xfrm6_transport_xmit(struct xfrm_state *x, struct sk_buff *skb) |
| 85 | { |
| 86 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 87 | |
| 88 | skb_reset_mac_len(skb); |
| 89 | pskb_pull(skb, skb->mac_len + sizeof(struct ipv6hdr) + x->props.header_len); |
| 90 | |
| 91 | if (xo->flags & XFRM_GSO_SEGMENT) { |
| 92 | skb_reset_transport_header(skb); |
| 93 | skb->transport_header -= x->props.header_len; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 98 | static struct xfrm_mode xfrm6_transport_mode = { |
| 99 | .input = xfrm6_transport_input, |
| 100 | .output = xfrm6_transport_output, |
Steffen Klassert | c35fe41 | 2017-04-14 10:06:01 +0200 | [diff] [blame] | 101 | .gso_segment = xfrm4_transport_gso_segment, |
| 102 | .xmit = xfrm6_transport_xmit, |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 103 | .owner = THIS_MODULE, |
| 104 | .encap = XFRM_MODE_TRANSPORT, |
| 105 | }; |
| 106 | |
| 107 | static int __init xfrm6_transport_init(void) |
| 108 | { |
| 109 | return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6); |
| 110 | } |
| 111 | |
| 112 | static void __exit xfrm6_transport_exit(void) |
| 113 | { |
| 114 | int err; |
| 115 | |
| 116 | err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6); |
| 117 | BUG_ON(err); |
| 118 | } |
| 119 | |
| 120 | module_init(xfrm6_transport_init); |
| 121 | module_exit(xfrm6_transport_exit); |
| 122 | MODULE_LICENSE("GPL"); |
| 123 | MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT); |