Linux 5.1.15
[linux/fpc-iii.git] / net / ipv6 / xfrm6_mode_transport.c
blob3c29da5defe6c357ff04ca4adead1a9fee208f08
1 /*
2 * xfrm6_mode_transport.c - Transport mode encapsulation for IPv6.
4 * Copyright (C) 2002 USAGI/WIDE Project
5 * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
6 */
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>
16 #include <net/protocol.h>
18 /* Add encapsulation header.
20 * The IP header and mutable extension headers will be moved forward to make
21 * space for the encapsulation header.
23 static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
25 struct ipv6hdr *iph;
26 u8 *prevhdr;
27 int hdr_len;
29 iph = ipv6_hdr(skb);
30 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
32 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
33 if (hdr_len < 0)
34 return hdr_len;
35 skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
36 skb_set_network_header(skb, -x->props.header_len);
37 skb->transport_header = skb->network_header + hdr_len;
38 __skb_pull(skb, hdr_len);
39 memmove(ipv6_hdr(skb), iph, hdr_len);
40 return 0;
43 /* Remove encapsulation header.
45 * The IP header will be moved over the top of the encapsulation header.
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.
51 static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
53 int ihl = skb->data - skb_transport_header(skb);
55 if (skb->transport_header != skb->network_header) {
56 memmove(skb_transport_header(skb),
57 skb_network_header(skb), ihl);
58 skb->network_header = skb->transport_header;
60 ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
61 sizeof(struct ipv6hdr));
62 skb_reset_transport_header(skb);
63 return 0;
66 static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
67 struct sk_buff *skb,
68 netdev_features_t features)
70 const struct net_offload *ops;
71 struct sk_buff *segs = ERR_PTR(-EINVAL);
72 struct xfrm_offload *xo = xfrm_offload(skb);
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);
79 return segs;
82 static void xfrm6_transport_xmit(struct xfrm_state *x, struct sk_buff *skb)
84 struct xfrm_offload *xo = xfrm_offload(skb);
86 skb_reset_mac_len(skb);
87 pskb_pull(skb, skb->mac_len + sizeof(struct ipv6hdr) + x->props.header_len);
89 if (xo->flags & XFRM_GSO_SEGMENT) {
90 skb_reset_transport_header(skb);
91 skb->transport_header -= x->props.header_len;
96 static struct xfrm_mode xfrm6_transport_mode = {
97 .input = xfrm6_transport_input,
98 .output = xfrm6_transport_output,
99 .gso_segment = xfrm4_transport_gso_segment,
100 .xmit = xfrm6_transport_xmit,
101 .owner = THIS_MODULE,
102 .encap = XFRM_MODE_TRANSPORT,
105 static int __init xfrm6_transport_init(void)
107 return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
110 static void __exit xfrm6_transport_exit(void)
112 int err;
114 err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
115 BUG_ON(err);
118 module_init(xfrm6_transport_init);
119 module_exit(xfrm6_transport_exit);
120 MODULE_LICENSE("GPL");
121 MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);