blob: 9ad07a91708ef7a1008d469766ab39b9b882883f [file] [log] [blame]
Herbert Xub59f45d2006-05-27 23:05:54 -07001/*
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 Klassertc35fe412017-04-14 10:06:01 +020016#include <net/protocol.h>
Herbert Xub59f45d2006-05-27 23:05:54 -070017
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 Xub59f45d2006-05-27 23:05:54 -070022 */
Jamal Hadi Salimeb878e82006-08-31 17:42:59 -070023static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
Herbert Xub59f45d2006-05-27 23:05:54 -070024{
Herbert Xub59f45d2006-05-27 23:05:54 -070025 struct ipv6hdr *iph;
26 u8 *prevhdr;
27 int hdr_len;
28
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070029 iph = ipv6_hdr(skb);
Ilan Tayarif1bd7d62017-04-14 10:07:39 +020030 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
Herbert Xub59f45d2006-05-27 23:05:54 -070031
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -070032 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
Ben Hutchings6e80ac52017-05-31 13:15:41 +010033 if (hdr_len < 0)
34 return hdr_len;
Herbert Xu007f0212007-10-09 13:25:59 -070035 skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
Herbert Xu7b277b12007-10-10 15:44:06 -070036 skb_set_network_header(skb, -x->props.header_len);
Herbert Xu37fedd32007-10-10 15:44:44 -070037 skb->transport_header = skb->network_header + hdr_len;
Herbert Xu7b277b12007-10-10 15:44:06 -070038 __skb_pull(skb, hdr_len);
39 memmove(ipv6_hdr(skb), iph, hdr_len);
Herbert Xub59f45d2006-05-27 23:05:54 -070040 return 0;
41}
42
Herbert Xu31a4ab92006-05-27 23:06:13 -070043/* 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 Xub59f45d2006-05-27 23:05:54 -070051static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
52{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070053 int ihl = skb->data - skb_transport_header(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010054 struct xfrm_offload *xo = xfrm_offload(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -070055
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070056 if (skb->transport_header != skb->network_header) {
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070057 memmove(skb_transport_header(skb),
58 skb_network_header(skb), ihl);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070059 skb->network_header = skb->transport_header;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070060 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070061 ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
Herbert Xu31a4ab92006-05-27 23:06:13 -070062 sizeof(struct ipv6hdr));
Steffen Klassert7785bba2017-02-15 09:40:00 +010063 if (!xo || !(xo->flags & XFRM_GRO))
64 skb_reset_transport_header(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070065 return 0;
66}
67
Steffen Klassertc35fe412017-04-14 10:06:01 +020068static 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
84static 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 Xub59f45d2006-05-27 23:05:54 -070098static struct xfrm_mode xfrm6_transport_mode = {
99 .input = xfrm6_transport_input,
100 .output = xfrm6_transport_output,
Steffen Klassertc35fe412017-04-14 10:06:01 +0200101 .gso_segment = xfrm4_transport_gso_segment,
102 .xmit = xfrm6_transport_xmit,
Herbert Xub59f45d2006-05-27 23:05:54 -0700103 .owner = THIS_MODULE,
104 .encap = XFRM_MODE_TRANSPORT,
105};
106
107static int __init xfrm6_transport_init(void)
108{
109 return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
110}
111
112static 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
120module_init(xfrm6_transport_init);
121module_exit(xfrm6_transport_exit);
122MODULE_LICENSE("GPL");
123MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);