blob: c12d51f5b5893ecfb060d0683de75c8d74f63812 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
4 *
5 * Authors:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
10 * IPv6 support
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter_ipv6.h>
17#include <net/ipv6.h>
18#include <net/xfrm.h>
19
20int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
21{
22 return xfrm6_extract_header(skb);
23}
24
25int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
26 struct ip6_tnl *t)
27{
28 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
29 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
30 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
31 return xfrm_input(skb, nexthdr, spi, 0);
32}
33EXPORT_SYMBOL(xfrm6_rcv_spi);
34
35static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
36 struct sk_buff *skb)
37{
38 if (xfrm_trans_queue(skb, ip6_rcv_finish))
39 __kfree_skb(skb);
40 return -1;
41}
42
43int xfrm6_transport_finish(struct sk_buff *skb, int async)
44{
45 struct xfrm_offload *xo = xfrm_offload(skb);
46 int nhlen = skb->data - skb_network_header(skb);
47
48 skb_network_header(skb)[IP6CB(skb)->nhoff] =
49 XFRM_MODE_SKB_CB(skb)->protocol;
50
51#ifndef CONFIG_NETFILTER
52 if (!async)
53 return 1;
54#endif
55
56 __skb_push(skb, nhlen);
57 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
58 skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
59
60 if (xo && (xo->flags & XFRM_GRO)) {
61 /* The full l2 header needs to be preserved so that re-injecting the packet at l2
62 * works correctly in the presence of vlan tags.
63 */
64 skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
65 skb_reset_network_header(skb);
66 skb_reset_transport_header(skb);
67 return -1;
68 }
69
70 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
71 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
72 xfrm6_transport_finish2);
73 return -1;
74}
75
76int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
77{
78 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
79 0, t);
80}
81EXPORT_SYMBOL(xfrm6_rcv_tnl);
82
83int xfrm6_rcv(struct sk_buff *skb)
84{
85 return xfrm6_rcv_tnl(skb, NULL);
86}
87EXPORT_SYMBOL(xfrm6_rcv);
88int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
89 xfrm_address_t *saddr, u8 proto)
90{
91 struct net *net = dev_net(skb->dev);
92 struct xfrm_state *x = NULL;
93 struct sec_path *sp;
94 int i = 0;
95
96 sp = secpath_set(skb);
97 if (!sp) {
98 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
99 goto drop;
100 }
101
102 if (1 + sp->len == XFRM_MAX_DEPTH) {
103 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
104 goto drop;
105 }
106
107 for (i = 0; i < 3; i++) {
108 xfrm_address_t *dst, *src;
109
110 switch (i) {
111 case 0:
112 dst = daddr;
113 src = saddr;
114 break;
115 case 1:
116 /* lookup state with wild-card source address */
117 dst = daddr;
118 src = (xfrm_address_t *)&in6addr_any;
119 break;
120 default:
121 /* lookup state with wild-card addresses */
122 dst = (xfrm_address_t *)&in6addr_any;
123 src = (xfrm_address_t *)&in6addr_any;
124 break;
125 }
126
127 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
128 if (!x)
129 continue;
130
131 spin_lock(&x->lock);
132
133 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
134 likely(x->km.state == XFRM_STATE_VALID) &&
135 !xfrm_state_check_expire(x)) {
136 spin_unlock(&x->lock);
137 if (x->type->input(x, skb) > 0) {
138 /* found a valid state */
139 break;
140 }
141 } else
142 spin_unlock(&x->lock);
143
144 xfrm_state_put(x);
145 x = NULL;
146 }
147
148 if (!x) {
149 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
150 xfrm_audit_state_notfound_simple(skb, AF_INET6);
151 goto drop;
152 }
153
154 sp->xvec[sp->len++] = x;
155
156 spin_lock(&x->lock);
157
158 x->curlft.bytes += skb->len;
159 x->curlft.packets++;
160
161 spin_unlock(&x->lock);
162
163 return 1;
164
165drop:
166 return -1;
167}
168EXPORT_SYMBOL(xfrm6_input_addr);