blob: 98b05f47232252a32b1b76eb9a96aa5a61999627 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_state.c: based on xfrm4_state.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific portion
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <net/xfrm.h>
15#include <linux/pfkeyv2.h>
16#include <linux/ipsec.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080017#include <net/dsfield.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/ipv6.h>
Patrick McHardyee51b1b2006-01-13 14:34:36 -080019#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static struct xfrm_state_afinfo xfrm6_state_afinfo;
22
23static void
24__xfrm6_init_tempsel(struct xfrm_state *x, struct flowi *fl,
25 struct xfrm_tmpl *tmpl,
26 xfrm_address_t *daddr, xfrm_address_t *saddr)
27{
28 /* Initialize temporary selector matching only
29 * to current session. */
30 ipv6_addr_copy((struct in6_addr *)&x->sel.daddr, &fl->fl6_dst);
31 ipv6_addr_copy((struct in6_addr *)&x->sel.saddr, &fl->fl6_src);
32 x->sel.dport = xfrm_flowi_dport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070033 x->sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 x->sel.sport = xfrm_flowi_sport(fl);
Al Viro8f83f232006-09-27 18:46:11 -070035 x->sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 x->sel.prefixlen_d = 128;
37 x->sel.prefixlen_s = 128;
38 x->sel.proto = fl->proto;
39 x->sel.ifindex = fl->oif;
40 x->id = tmpl->id;
41 if (ipv6_addr_any((struct in6_addr*)&x->id.daddr))
42 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
43 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
44 if (ipv6_addr_any((struct in6_addr*)&x->props.saddr))
45 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 x->props.mode = tmpl->mode;
47 x->props.reqid = tmpl->reqid;
48 x->props.family = AF_INET6;
49}
50
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -070051static int
52__xfrm6_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n)
53{
54 int i;
55 int j = 0;
56
57 /* Rule 1: select IPsec transport except AH */
58 for (i = 0; i < n; i++) {
59 if (src[i]->props.mode == XFRM_MODE_TRANSPORT &&
60 src[i]->id.proto != IPPROTO_AH) {
61 dst[j++] = src[i];
62 src[i] = NULL;
63 }
64 }
65 if (j == n)
66 goto end;
67
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -070068 /* Rule 2: select MIPv6 RO or inbound trigger */
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -070069#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -070070 for (i = 0; i < n; i++) {
71 if (src[i] &&
72 (src[i]->props.mode == XFRM_MODE_ROUTEOPTIMIZATION ||
73 src[i]->props.mode == XFRM_MODE_IN_TRIGGER)) {
74 dst[j++] = src[i];
75 src[i] = NULL;
76 }
77 }
78 if (j == n)
79 goto end;
80#endif
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -070081
82 /* Rule 3: select IPsec transport AH */
83 for (i = 0; i < n; i++) {
84 if (src[i] &&
85 src[i]->props.mode == XFRM_MODE_TRANSPORT &&
86 src[i]->id.proto == IPPROTO_AH) {
87 dst[j++] = src[i];
88 src[i] = NULL;
89 }
90 }
91 if (j == n)
92 goto end;
93
94 /* Rule 4: select IPsec tunnel */
95 for (i = 0; i < n; i++) {
96 if (src[i] &&
Herbert Xu1bfcb102007-10-17 21:31:50 -070097 (src[i]->props.mode == XFRM_MODE_TUNNEL ||
98 src[i]->props.mode == XFRM_MODE_BEET)) {
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -070099 dst[j++] = src[i];
100 src[i] = NULL;
101 }
102 }
103 if (likely(j == n))
104 goto end;
105
106 /* Final rule */
107 for (i = 0; i < n; i++) {
108 if (src[i]) {
109 dst[j++] = src[i];
110 src[i] = NULL;
111 }
112 }
113
114 end:
115 return 0;
116}
117
118static int
119__xfrm6_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n)
120{
121 int i;
122 int j = 0;
123
124 /* Rule 1: select IPsec transport */
125 for (i = 0; i < n; i++) {
126 if (src[i]->mode == XFRM_MODE_TRANSPORT) {
127 dst[j++] = src[i];
128 src[i] = NULL;
129 }
130 }
131 if (j == n)
132 goto end;
133
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -0700134 /* Rule 2: select MIPv6 RO or inbound trigger */
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700135#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA64d9fdd2006-08-23 22:54:07 -0700136 for (i = 0; i < n; i++) {
137 if (src[i] &&
138 (src[i]->mode == XFRM_MODE_ROUTEOPTIMIZATION ||
139 src[i]->mode == XFRM_MODE_IN_TRIGGER)) {
140 dst[j++] = src[i];
141 src[i] = NULL;
142 }
143 }
144 if (j == n)
145 goto end;
146#endif
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700147
148 /* Rule 3: select IPsec tunnel */
149 for (i = 0; i < n; i++) {
150 if (src[i] &&
Herbert Xu1bfcb102007-10-17 21:31:50 -0700151 (src[i]->mode == XFRM_MODE_TUNNEL ||
152 src[i]->mode == XFRM_MODE_BEET)) {
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700153 dst[j++] = src[i];
154 src[i] = NULL;
155 }
156 }
157 if (likely(j == n))
158 goto end;
159
160 /* Final rule */
161 for (i = 0; i < n; i++) {
162 if (src[i]) {
163 dst[j++] = src[i];
164 src[i] = NULL;
165 }
166 }
167
168 end:
169 return 0;
170}
171
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800172int xfrm6_extract_header(struct sk_buff *skb)
173{
174 struct ipv6hdr *iph = ipv6_hdr(skb);
175
176 XFRM_MODE_SKB_CB(skb)->id = 0;
177 XFRM_MODE_SKB_CB(skb)->frag_off = htons(IP_DF);
178 XFRM_MODE_SKB_CB(skb)->tos = ipv6_get_dsfield(iph);
179 XFRM_MODE_SKB_CB(skb)->ttl = iph->hop_limit;
180 XFRM_MODE_SKB_CB(skb)->protocol = iph->nexthdr;
181 memcpy(XFRM_MODE_SKB_CB(skb)->flow_lbl, iph->flow_lbl,
182 sizeof(XFRM_MODE_SKB_CB(skb)->flow_lbl));
183
184 return 0;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static struct xfrm_state_afinfo xfrm6_state_afinfo = {
188 .family = AF_INET6,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800189 .proto = IPPROTO_IPV6,
Herbert Xu17c2a422007-10-17 21:33:12 -0700190 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .init_tempsel = __xfrm6_init_tempsel,
Masahide NAKAMURA58c949d2006-08-23 22:51:02 -0700192 .tmpl_sort = __xfrm6_tmpl_sort,
193 .state_sort = __xfrm6_state_sort,
Miika Komucdca7262007-02-06 14:24:56 -0800194 .output = xfrm6_output,
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800195 .extract_output = xfrm6_extract_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198void __init xfrm6_state_init(void)
199{
200 xfrm_state_register_afinfo(&xfrm6_state_afinfo);
201}
202
203void xfrm6_state_fini(void)
204{
205 xfrm_state_unregister_afinfo(&xfrm6_state_afinfo);
206}
207