2 * xfrm6_state.c: based on xfrm4_state.c
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific portion
15 #include <linux/pfkeyv2.h>
16 #include <linux/ipsec.h>
17 #include <linux/netfilter_ipv6.h>
18 #include <net/dsfield.h>
20 #include <net/addrconf.h>
23 __xfrm6_init_tempsel(struct xfrm_selector
*sel
, struct flowi
*fl
)
25 /* Initialize temporary selector matching only
26 * to current session. */
27 ipv6_addr_copy((struct in6_addr
*)&sel
->daddr
, &fl
->fl6_dst
);
28 ipv6_addr_copy((struct in6_addr
*)&sel
->saddr
, &fl
->fl6_src
);
29 sel
->dport
= xfrm_flowi_dport(fl
);
30 sel
->dport_mask
= htons(0xffff);
31 sel
->sport
= xfrm_flowi_sport(fl
);
32 sel
->sport_mask
= htons(0xffff);
33 sel
->family
= AF_INET6
;
34 sel
->prefixlen_d
= 128;
35 sel
->prefixlen_s
= 128;
36 sel
->proto
= fl
->proto
;
37 sel
->ifindex
= fl
->oif
;
41 xfrm6_init_temprop(struct xfrm_state
*x
, struct xfrm_tmpl
*tmpl
,
42 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
)
45 if (ipv6_addr_any((struct in6_addr
*)&x
->id
.daddr
))
46 memcpy(&x
->id
.daddr
, daddr
, sizeof(x
->sel
.daddr
));
47 memcpy(&x
->props
.saddr
, &tmpl
->saddr
, sizeof(x
->props
.saddr
));
48 if (ipv6_addr_any((struct in6_addr
*)&x
->props
.saddr
))
49 memcpy(&x
->props
.saddr
, saddr
, sizeof(x
->props
.saddr
));
50 x
->props
.mode
= tmpl
->mode
;
51 x
->props
.reqid
= tmpl
->reqid
;
52 x
->props
.family
= AF_INET6
;
55 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
57 __xfrm6_sort(void **dst
, void **src
, int n
, int (*cmp
)(void *p
), int maxclass
)
60 int class[XFRM_MAX_DEPTH
];
63 memset(count
, 0, sizeof(count
));
65 for (i
= 0; i
< n
; i
++) {
67 class[i
] = c
= cmp(src
[i
]);
71 for (i
= 2; i
< maxclass
; i
++)
72 count
[i
] += count
[i
- 1];
74 for (i
= 0; i
< n
; i
++) {
75 dst
[count
[class[i
] - 1]++] = src
[i
];
83 * Rule for xfrm_state:
85 * rule 1: select IPsec transport except AH
86 * rule 2: select MIPv6 RO or inbound trigger
87 * rule 3: select IPsec transport AH
88 * rule 4: select IPsec tunnel
91 static int __xfrm6_state_sort_cmp(void *p
)
93 struct xfrm_state
*v
= p
;
95 switch (v
->props
.mode
) {
96 case XFRM_MODE_TRANSPORT
:
97 if (v
->id
.proto
!= IPPROTO_AH
)
101 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
102 case XFRM_MODE_ROUTEOPTIMIZATION
:
103 case XFRM_MODE_IN_TRIGGER
:
106 case XFRM_MODE_TUNNEL
:
114 __xfrm6_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
)
116 return __xfrm6_sort((void **)dst
, (void **)src
, n
,
117 __xfrm6_state_sort_cmp
, 6);
121 * Rule for xfrm_tmpl:
123 * rule 1: select IPsec transport
124 * rule 2: select MIPv6 RO or inbound trigger
125 * rule 3: select IPsec tunnel
128 static int __xfrm6_tmpl_sort_cmp(void *p
)
130 struct xfrm_tmpl
*v
= p
;
132 case XFRM_MODE_TRANSPORT
:
134 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
135 case XFRM_MODE_ROUTEOPTIMIZATION
:
136 case XFRM_MODE_IN_TRIGGER
:
139 case XFRM_MODE_TUNNEL
:
147 __xfrm6_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
)
149 return __xfrm6_sort((void **)dst
, (void **)src
, n
,
150 __xfrm6_tmpl_sort_cmp
, 5);
153 int xfrm6_extract_header(struct sk_buff
*skb
)
155 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
157 XFRM_MODE_SKB_CB(skb
)->ihl
= sizeof(*iph
);
158 XFRM_MODE_SKB_CB(skb
)->id
= 0;
159 XFRM_MODE_SKB_CB(skb
)->frag_off
= htons(IP_DF
);
160 XFRM_MODE_SKB_CB(skb
)->tos
= ipv6_get_dsfield(iph
);
161 XFRM_MODE_SKB_CB(skb
)->ttl
= iph
->hop_limit
;
162 XFRM_MODE_SKB_CB(skb
)->optlen
= 0;
163 memcpy(XFRM_MODE_SKB_CB(skb
)->flow_lbl
, iph
->flow_lbl
,
164 sizeof(XFRM_MODE_SKB_CB(skb
)->flow_lbl
));
169 static struct xfrm_state_afinfo xfrm6_state_afinfo
= {
171 .proto
= IPPROTO_IPV6
,
172 .eth_proto
= htons(ETH_P_IPV6
),
173 .owner
= THIS_MODULE
,
174 .init_tempsel
= __xfrm6_init_tempsel
,
175 .init_temprop
= xfrm6_init_temprop
,
176 .tmpl_sort
= __xfrm6_tmpl_sort
,
177 .state_sort
= __xfrm6_state_sort
,
178 .output
= xfrm6_output
,
179 .extract_input
= xfrm6_extract_input
,
180 .extract_output
= xfrm6_extract_output
,
181 .transport_finish
= xfrm6_transport_finish
,
184 int __init
xfrm6_state_init(void)
186 return xfrm_state_register_afinfo(&xfrm6_state_afinfo
);
189 void xfrm6_state_fini(void)
191 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo
);