blob: 7a92c0f3191250118ce3572ca91ee9116460bce8 [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);
Herbert Xu007f0212007-10-09 13:25:59 -070033 skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
Herbert Xu7b277b12007-10-10 15:44:06 -070034 skb_set_network_header(skb, -x->props.header_len);
Herbert Xu37fedd32007-10-10 15:44:44 -070035 skb->transport_header = skb->network_header + hdr_len;
Herbert Xu7b277b12007-10-10 15:44:06 -070036 __skb_pull(skb, hdr_len);
37 memmove(ipv6_hdr(skb), iph, hdr_len);
Herbert Xub59f45d2006-05-27 23:05:54 -070038 return 0;
39}
40
Herbert Xu31a4ab92006-05-27 23:06:13 -070041/* Remove encapsulation header.
42 *
43 * The IP header will be moved over the top of the encapsulation header.
44 *
45 * On entry, skb->h shall point to where the IP header should be and skb->nh
46 * shall be set to where the IP header currently is. skb->data shall point
47 * to the start of the payload.
48 */
Herbert Xub59f45d2006-05-27 23:05:54 -070049static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
50{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070051 int ihl = skb->data - skb_transport_header(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010052 struct xfrm_offload *xo = xfrm_offload(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -070053
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070054 if (skb->transport_header != skb->network_header) {
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -070055 memmove(skb_transport_header(skb),
56 skb_network_header(skb), ihl);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -070057 skb->network_header = skb->transport_header;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070058 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070059 ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
Herbert Xu31a4ab92006-05-27 23:06:13 -070060 sizeof(struct ipv6hdr));
Steffen Klassert7785bba2017-02-15 09:40:00 +010061 if (!xo || !(xo->flags & XFRM_GRO))
62 skb_reset_transport_header(skb);
Herbert Xub59f45d2006-05-27 23:05:54 -070063 return 0;
64}
65
Steffen Klassertc35fe412017-04-14 10:06:01 +020066static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
67 struct sk_buff *skb,
68 netdev_features_t features)
69{
70 const struct net_offload *ops;
71 struct sk_buff *segs = ERR_PTR(-EINVAL);
72 struct xfrm_offload *xo = xfrm_offload(skb);
73
74 skb->transport_header += x->props.header_len;
75 ops = rcu_dereference(inet6_offloads[xo->proto]);
76 if (likely(ops && ops->callbacks.gso_segment))
77 segs = ops->callbacks.gso_segment(skb, features);
78
79 return segs;
80}
81
82static void xfrm6_transport_xmit(struct xfrm_state *x, struct sk_buff *skb)
83{
84 struct xfrm_offload *xo = xfrm_offload(skb);
85
86 skb_reset_mac_len(skb);
87 pskb_pull(skb, skb->mac_len + sizeof(struct ipv6hdr) + x->props.header_len);
88
89 if (xo->flags & XFRM_GSO_SEGMENT) {
90 skb_reset_transport_header(skb);
91 skb->transport_header -= x->props.header_len;
92 }
93}
94
95
Herbert Xub59f45d2006-05-27 23:05:54 -070096static struct xfrm_mode xfrm6_transport_mode = {
97 .input = xfrm6_transport_input,
98 .output = xfrm6_transport_output,
Steffen Klassertc35fe412017-04-14 10:06:01 +020099 .gso_segment = xfrm4_transport_gso_segment,
100 .xmit = xfrm6_transport_xmit,
Herbert Xub59f45d2006-05-27 23:05:54 -0700101 .owner = THIS_MODULE,
102 .encap = XFRM_MODE_TRANSPORT,
103};
104
105static int __init xfrm6_transport_init(void)
106{
107 return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
108}
109
110static void __exit xfrm6_transport_exit(void)
111{
112 int err;
113
114 err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
115 BUG_ON(err);
116}
117
118module_init(xfrm6_transport_init);
119module_exit(xfrm6_transport_exit);
120MODULE_LICENSE("GPL");
121MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);