1 // SPDX-License-Identifier: GPL-2.0
3 * xfrm6_state.c: based on xfrm4_state.c
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific portion
16 #include <linux/pfkeyv2.h>
17 #include <linux/ipsec.h>
18 #include <linux/netfilter_ipv6.h>
19 #include <linux/export.h>
20 #include <net/dsfield.h>
22 #include <net/addrconf.h>
25 __xfrm6_init_tempsel(struct xfrm_selector
*sel
, const struct flowi
*fl
)
27 const struct flowi6
*fl6
= &fl
->u
.ip6
;
29 /* Initialize temporary selector matching only
30 * to current session. */
31 *(struct in6_addr
*)&sel
->daddr
= fl6
->daddr
;
32 *(struct in6_addr
*)&sel
->saddr
= fl6
->saddr
;
33 sel
->dport
= xfrm_flowi_dport(fl
, &fl6
->uli
);
34 sel
->dport_mask
= htons(0xffff);
35 sel
->sport
= xfrm_flowi_sport(fl
, &fl6
->uli
);
36 sel
->sport_mask
= htons(0xffff);
37 sel
->family
= AF_INET6
;
38 sel
->prefixlen_d
= 128;
39 sel
->prefixlen_s
= 128;
40 sel
->proto
= fl6
->flowi6_proto
;
41 sel
->ifindex
= fl6
->flowi6_oif
;
45 xfrm6_init_temprop(struct xfrm_state
*x
, const struct xfrm_tmpl
*tmpl
,
46 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
)
49 if (ipv6_addr_any((struct in6_addr
*)&x
->id
.daddr
))
50 memcpy(&x
->id
.daddr
, daddr
, sizeof(x
->sel
.daddr
));
51 memcpy(&x
->props
.saddr
, &tmpl
->saddr
, sizeof(x
->props
.saddr
));
52 if (ipv6_addr_any((struct in6_addr
*)&x
->props
.saddr
))
53 memcpy(&x
->props
.saddr
, saddr
, sizeof(x
->props
.saddr
));
54 x
->props
.mode
= tmpl
->mode
;
55 x
->props
.reqid
= tmpl
->reqid
;
56 x
->props
.family
= AF_INET6
;
59 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
61 __xfrm6_sort(void **dst
, void **src
, int n
, int (*cmp
)(void *p
), int maxclass
)
64 int class[XFRM_MAX_DEPTH
];
67 memset(count
, 0, sizeof(count
));
69 for (i
= 0; i
< n
; i
++) {
71 class[i
] = c
= cmp(src
[i
]);
75 for (i
= 2; i
< maxclass
; i
++)
76 count
[i
] += count
[i
- 1];
78 for (i
= 0; i
< n
; i
++) {
79 dst
[count
[class[i
] - 1]++] = src
[i
];
87 * Rule for xfrm_state:
89 * rule 1: select IPsec transport except AH
90 * rule 2: select MIPv6 RO or inbound trigger
91 * rule 3: select IPsec transport AH
92 * rule 4: select IPsec tunnel
95 static int __xfrm6_state_sort_cmp(void *p
)
97 struct xfrm_state
*v
= p
;
99 switch (v
->props
.mode
) {
100 case XFRM_MODE_TRANSPORT
:
101 if (v
->id
.proto
!= IPPROTO_AH
)
105 #if IS_ENABLED(CONFIG_IPV6_MIP6)
106 case XFRM_MODE_ROUTEOPTIMIZATION
:
107 case XFRM_MODE_IN_TRIGGER
:
110 case XFRM_MODE_TUNNEL
:
118 __xfrm6_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
)
120 return __xfrm6_sort((void **)dst
, (void **)src
, n
,
121 __xfrm6_state_sort_cmp
, 6);
125 * Rule for xfrm_tmpl:
127 * rule 1: select IPsec transport
128 * rule 2: select MIPv6 RO or inbound trigger
129 * rule 3: select IPsec tunnel
132 static int __xfrm6_tmpl_sort_cmp(void *p
)
134 struct xfrm_tmpl
*v
= p
;
136 case XFRM_MODE_TRANSPORT
:
138 #if IS_ENABLED(CONFIG_IPV6_MIP6)
139 case XFRM_MODE_ROUTEOPTIMIZATION
:
140 case XFRM_MODE_IN_TRIGGER
:
143 case XFRM_MODE_TUNNEL
:
151 __xfrm6_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
)
153 return __xfrm6_sort((void **)dst
, (void **)src
, n
,
154 __xfrm6_tmpl_sort_cmp
, 5);
157 int xfrm6_extract_header(struct sk_buff
*skb
)
159 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
161 XFRM_MODE_SKB_CB(skb
)->ihl
= sizeof(*iph
);
162 XFRM_MODE_SKB_CB(skb
)->id
= 0;
163 XFRM_MODE_SKB_CB(skb
)->frag_off
= htons(IP_DF
);
164 XFRM_MODE_SKB_CB(skb
)->tos
= ipv6_get_dsfield(iph
);
165 XFRM_MODE_SKB_CB(skb
)->ttl
= iph
->hop_limit
;
166 XFRM_MODE_SKB_CB(skb
)->optlen
= 0;
167 memcpy(XFRM_MODE_SKB_CB(skb
)->flow_lbl
, iph
->flow_lbl
,
168 sizeof(XFRM_MODE_SKB_CB(skb
)->flow_lbl
));
173 static struct xfrm_state_afinfo xfrm6_state_afinfo
= {
175 .proto
= IPPROTO_IPV6
,
176 .eth_proto
= htons(ETH_P_IPV6
),
177 .owner
= THIS_MODULE
,
178 .init_tempsel
= __xfrm6_init_tempsel
,
179 .init_temprop
= xfrm6_init_temprop
,
180 .tmpl_sort
= __xfrm6_tmpl_sort
,
181 .state_sort
= __xfrm6_state_sort
,
182 .output
= xfrm6_output
,
183 .output_finish
= xfrm6_output_finish
,
184 .extract_input
= xfrm6_extract_input
,
185 .extract_output
= xfrm6_extract_output
,
186 .transport_finish
= xfrm6_transport_finish
,
187 .local_error
= xfrm6_local_error
,
190 int __init
xfrm6_state_init(void)
192 return xfrm_state_register_afinfo(&xfrm6_state_afinfo
);
195 void xfrm6_state_fini(void)
197 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo
);