Commit 928497f0209027ccd649480a38a499fab9c3f6f6
Committed by
David S. Miller
1 parent
3b2eb6131e
Exists in
master
and in
7 other branches
xfrm_user: avoid a warning with some compiler
Attached is a small patch to remove a warning ("warning: ISO C90 forbids mixed declarations and code" with gcc 4.3.2). Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
net/xfrm/xfrm_user.c
1 | /* xfrm_user.c: User interface to configure xfrm engine. | 1 | /* xfrm_user.c: User interface to configure xfrm engine. |
2 | * | 2 | * |
3 | * Copyright (C) 2002 David S. Miller (davem@redhat.com) | 3 | * Copyright (C) 2002 David S. Miller (davem@redhat.com) |
4 | * | 4 | * |
5 | * Changes: | 5 | * Changes: |
6 | * Mitsuru KANDA @USAGI | 6 | * Mitsuru KANDA @USAGI |
7 | * Kazunori MIYAZAWA @USAGI | 7 | * Kazunori MIYAZAWA @USAGI |
8 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> | 8 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
9 | * IPv6 support | 9 | * IPv6 support |
10 | * | 10 | * |
11 | */ | 11 | */ |
12 | 12 | ||
13 | #include <linux/crypto.h> | 13 | #include <linux/crypto.h> |
14 | #include <linux/module.h> | 14 | #include <linux/module.h> |
15 | #include <linux/kernel.h> | 15 | #include <linux/kernel.h> |
16 | #include <linux/types.h> | 16 | #include <linux/types.h> |
17 | #include <linux/slab.h> | 17 | #include <linux/slab.h> |
18 | #include <linux/socket.h> | 18 | #include <linux/socket.h> |
19 | #include <linux/string.h> | 19 | #include <linux/string.h> |
20 | #include <linux/net.h> | 20 | #include <linux/net.h> |
21 | #include <linux/skbuff.h> | 21 | #include <linux/skbuff.h> |
22 | #include <linux/pfkeyv2.h> | 22 | #include <linux/pfkeyv2.h> |
23 | #include <linux/ipsec.h> | 23 | #include <linux/ipsec.h> |
24 | #include <linux/init.h> | 24 | #include <linux/init.h> |
25 | #include <linux/security.h> | 25 | #include <linux/security.h> |
26 | #include <net/sock.h> | 26 | #include <net/sock.h> |
27 | #include <net/xfrm.h> | 27 | #include <net/xfrm.h> |
28 | #include <net/netlink.h> | 28 | #include <net/netlink.h> |
29 | #include <asm/uaccess.h> | 29 | #include <asm/uaccess.h> |
30 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 30 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
31 | #include <linux/in6.h> | 31 | #include <linux/in6.h> |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | static inline int aead_len(struct xfrm_algo_aead *alg) | 34 | static inline int aead_len(struct xfrm_algo_aead *alg) |
35 | { | 35 | { |
36 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | 36 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); |
37 | } | 37 | } |
38 | 38 | ||
39 | static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) | 39 | static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) |
40 | { | 40 | { |
41 | struct nlattr *rt = attrs[type]; | 41 | struct nlattr *rt = attrs[type]; |
42 | struct xfrm_algo *algp; | 42 | struct xfrm_algo *algp; |
43 | 43 | ||
44 | if (!rt) | 44 | if (!rt) |
45 | return 0; | 45 | return 0; |
46 | 46 | ||
47 | algp = nla_data(rt); | 47 | algp = nla_data(rt); |
48 | if (nla_len(rt) < xfrm_alg_len(algp)) | 48 | if (nla_len(rt) < xfrm_alg_len(algp)) |
49 | return -EINVAL; | 49 | return -EINVAL; |
50 | 50 | ||
51 | switch (type) { | 51 | switch (type) { |
52 | case XFRMA_ALG_AUTH: | 52 | case XFRMA_ALG_AUTH: |
53 | case XFRMA_ALG_CRYPT: | 53 | case XFRMA_ALG_CRYPT: |
54 | case XFRMA_ALG_COMP: | 54 | case XFRMA_ALG_COMP: |
55 | break; | 55 | break; |
56 | 56 | ||
57 | default: | 57 | default: |
58 | return -EINVAL; | 58 | return -EINVAL; |
59 | } | 59 | } |
60 | 60 | ||
61 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | 61 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; |
62 | return 0; | 62 | return 0; |
63 | } | 63 | } |
64 | 64 | ||
65 | static int verify_auth_trunc(struct nlattr **attrs) | 65 | static int verify_auth_trunc(struct nlattr **attrs) |
66 | { | 66 | { |
67 | struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; | 67 | struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; |
68 | struct xfrm_algo_auth *algp; | 68 | struct xfrm_algo_auth *algp; |
69 | 69 | ||
70 | if (!rt) | 70 | if (!rt) |
71 | return 0; | 71 | return 0; |
72 | 72 | ||
73 | algp = nla_data(rt); | 73 | algp = nla_data(rt); |
74 | if (nla_len(rt) < xfrm_alg_auth_len(algp)) | 74 | if (nla_len(rt) < xfrm_alg_auth_len(algp)) |
75 | return -EINVAL; | 75 | return -EINVAL; |
76 | 76 | ||
77 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | 77 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; |
78 | return 0; | 78 | return 0; |
79 | } | 79 | } |
80 | 80 | ||
81 | static int verify_aead(struct nlattr **attrs) | 81 | static int verify_aead(struct nlattr **attrs) |
82 | { | 82 | { |
83 | struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; | 83 | struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; |
84 | struct xfrm_algo_aead *algp; | 84 | struct xfrm_algo_aead *algp; |
85 | 85 | ||
86 | if (!rt) | 86 | if (!rt) |
87 | return 0; | 87 | return 0; |
88 | 88 | ||
89 | algp = nla_data(rt); | 89 | algp = nla_data(rt); |
90 | if (nla_len(rt) < aead_len(algp)) | 90 | if (nla_len(rt) < aead_len(algp)) |
91 | return -EINVAL; | 91 | return -EINVAL; |
92 | 92 | ||
93 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | 93 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; |
94 | return 0; | 94 | return 0; |
95 | } | 95 | } |
96 | 96 | ||
97 | static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, | 97 | static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, |
98 | xfrm_address_t **addrp) | 98 | xfrm_address_t **addrp) |
99 | { | 99 | { |
100 | struct nlattr *rt = attrs[type]; | 100 | struct nlattr *rt = attrs[type]; |
101 | 101 | ||
102 | if (rt && addrp) | 102 | if (rt && addrp) |
103 | *addrp = nla_data(rt); | 103 | *addrp = nla_data(rt); |
104 | } | 104 | } |
105 | 105 | ||
106 | static inline int verify_sec_ctx_len(struct nlattr **attrs) | 106 | static inline int verify_sec_ctx_len(struct nlattr **attrs) |
107 | { | 107 | { |
108 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; | 108 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
109 | struct xfrm_user_sec_ctx *uctx; | 109 | struct xfrm_user_sec_ctx *uctx; |
110 | 110 | ||
111 | if (!rt) | 111 | if (!rt) |
112 | return 0; | 112 | return 0; |
113 | 113 | ||
114 | uctx = nla_data(rt); | 114 | uctx = nla_data(rt); |
115 | if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) | 115 | if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) |
116 | return -EINVAL; | 116 | return -EINVAL; |
117 | 117 | ||
118 | return 0; | 118 | return 0; |
119 | } | 119 | } |
120 | 120 | ||
121 | 121 | ||
122 | static int verify_newsa_info(struct xfrm_usersa_info *p, | 122 | static int verify_newsa_info(struct xfrm_usersa_info *p, |
123 | struct nlattr **attrs) | 123 | struct nlattr **attrs) |
124 | { | 124 | { |
125 | int err; | 125 | int err; |
126 | 126 | ||
127 | err = -EINVAL; | 127 | err = -EINVAL; |
128 | switch (p->family) { | 128 | switch (p->family) { |
129 | case AF_INET: | 129 | case AF_INET: |
130 | break; | 130 | break; |
131 | 131 | ||
132 | case AF_INET6: | 132 | case AF_INET6: |
133 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 133 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
134 | break; | 134 | break; |
135 | #else | 135 | #else |
136 | err = -EAFNOSUPPORT; | 136 | err = -EAFNOSUPPORT; |
137 | goto out; | 137 | goto out; |
138 | #endif | 138 | #endif |
139 | 139 | ||
140 | default: | 140 | default: |
141 | goto out; | 141 | goto out; |
142 | } | 142 | } |
143 | 143 | ||
144 | err = -EINVAL; | 144 | err = -EINVAL; |
145 | switch (p->id.proto) { | 145 | switch (p->id.proto) { |
146 | case IPPROTO_AH: | 146 | case IPPROTO_AH: |
147 | if ((!attrs[XFRMA_ALG_AUTH] && | 147 | if ((!attrs[XFRMA_ALG_AUTH] && |
148 | !attrs[XFRMA_ALG_AUTH_TRUNC]) || | 148 | !attrs[XFRMA_ALG_AUTH_TRUNC]) || |
149 | attrs[XFRMA_ALG_AEAD] || | 149 | attrs[XFRMA_ALG_AEAD] || |
150 | attrs[XFRMA_ALG_CRYPT] || | 150 | attrs[XFRMA_ALG_CRYPT] || |
151 | attrs[XFRMA_ALG_COMP]) | 151 | attrs[XFRMA_ALG_COMP]) |
152 | goto out; | 152 | goto out; |
153 | break; | 153 | break; |
154 | 154 | ||
155 | case IPPROTO_ESP: | 155 | case IPPROTO_ESP: |
156 | if (attrs[XFRMA_ALG_COMP]) | 156 | if (attrs[XFRMA_ALG_COMP]) |
157 | goto out; | 157 | goto out; |
158 | if (!attrs[XFRMA_ALG_AUTH] && | 158 | if (!attrs[XFRMA_ALG_AUTH] && |
159 | !attrs[XFRMA_ALG_AUTH_TRUNC] && | 159 | !attrs[XFRMA_ALG_AUTH_TRUNC] && |
160 | !attrs[XFRMA_ALG_CRYPT] && | 160 | !attrs[XFRMA_ALG_CRYPT] && |
161 | !attrs[XFRMA_ALG_AEAD]) | 161 | !attrs[XFRMA_ALG_AEAD]) |
162 | goto out; | 162 | goto out; |
163 | if ((attrs[XFRMA_ALG_AUTH] || | 163 | if ((attrs[XFRMA_ALG_AUTH] || |
164 | attrs[XFRMA_ALG_AUTH_TRUNC] || | 164 | attrs[XFRMA_ALG_AUTH_TRUNC] || |
165 | attrs[XFRMA_ALG_CRYPT]) && | 165 | attrs[XFRMA_ALG_CRYPT]) && |
166 | attrs[XFRMA_ALG_AEAD]) | 166 | attrs[XFRMA_ALG_AEAD]) |
167 | goto out; | 167 | goto out; |
168 | break; | 168 | break; |
169 | 169 | ||
170 | case IPPROTO_COMP: | 170 | case IPPROTO_COMP: |
171 | if (!attrs[XFRMA_ALG_COMP] || | 171 | if (!attrs[XFRMA_ALG_COMP] || |
172 | attrs[XFRMA_ALG_AEAD] || | 172 | attrs[XFRMA_ALG_AEAD] || |
173 | attrs[XFRMA_ALG_AUTH] || | 173 | attrs[XFRMA_ALG_AUTH] || |
174 | attrs[XFRMA_ALG_AUTH_TRUNC] || | 174 | attrs[XFRMA_ALG_AUTH_TRUNC] || |
175 | attrs[XFRMA_ALG_CRYPT]) | 175 | attrs[XFRMA_ALG_CRYPT]) |
176 | goto out; | 176 | goto out; |
177 | break; | 177 | break; |
178 | 178 | ||
179 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 179 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
180 | case IPPROTO_DSTOPTS: | 180 | case IPPROTO_DSTOPTS: |
181 | case IPPROTO_ROUTING: | 181 | case IPPROTO_ROUTING: |
182 | if (attrs[XFRMA_ALG_COMP] || | 182 | if (attrs[XFRMA_ALG_COMP] || |
183 | attrs[XFRMA_ALG_AUTH] || | 183 | attrs[XFRMA_ALG_AUTH] || |
184 | attrs[XFRMA_ALG_AUTH_TRUNC] || | 184 | attrs[XFRMA_ALG_AUTH_TRUNC] || |
185 | attrs[XFRMA_ALG_AEAD] || | 185 | attrs[XFRMA_ALG_AEAD] || |
186 | attrs[XFRMA_ALG_CRYPT] || | 186 | attrs[XFRMA_ALG_CRYPT] || |
187 | attrs[XFRMA_ENCAP] || | 187 | attrs[XFRMA_ENCAP] || |
188 | attrs[XFRMA_SEC_CTX] || | 188 | attrs[XFRMA_SEC_CTX] || |
189 | !attrs[XFRMA_COADDR]) | 189 | !attrs[XFRMA_COADDR]) |
190 | goto out; | 190 | goto out; |
191 | break; | 191 | break; |
192 | #endif | 192 | #endif |
193 | 193 | ||
194 | default: | 194 | default: |
195 | goto out; | 195 | goto out; |
196 | } | 196 | } |
197 | 197 | ||
198 | if ((err = verify_aead(attrs))) | 198 | if ((err = verify_aead(attrs))) |
199 | goto out; | 199 | goto out; |
200 | if ((err = verify_auth_trunc(attrs))) | 200 | if ((err = verify_auth_trunc(attrs))) |
201 | goto out; | 201 | goto out; |
202 | if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) | 202 | if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) |
203 | goto out; | 203 | goto out; |
204 | if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) | 204 | if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) |
205 | goto out; | 205 | goto out; |
206 | if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) | 206 | if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) |
207 | goto out; | 207 | goto out; |
208 | if ((err = verify_sec_ctx_len(attrs))) | 208 | if ((err = verify_sec_ctx_len(attrs))) |
209 | goto out; | 209 | goto out; |
210 | 210 | ||
211 | err = -EINVAL; | 211 | err = -EINVAL; |
212 | switch (p->mode) { | 212 | switch (p->mode) { |
213 | case XFRM_MODE_TRANSPORT: | 213 | case XFRM_MODE_TRANSPORT: |
214 | case XFRM_MODE_TUNNEL: | 214 | case XFRM_MODE_TUNNEL: |
215 | case XFRM_MODE_ROUTEOPTIMIZATION: | 215 | case XFRM_MODE_ROUTEOPTIMIZATION: |
216 | case XFRM_MODE_BEET: | 216 | case XFRM_MODE_BEET: |
217 | break; | 217 | break; |
218 | 218 | ||
219 | default: | 219 | default: |
220 | goto out; | 220 | goto out; |
221 | } | 221 | } |
222 | 222 | ||
223 | err = 0; | 223 | err = 0; |
224 | 224 | ||
225 | out: | 225 | out: |
226 | return err; | 226 | return err; |
227 | } | 227 | } |
228 | 228 | ||
229 | static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, | 229 | static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, |
230 | struct xfrm_algo_desc *(*get_byname)(char *, int), | 230 | struct xfrm_algo_desc *(*get_byname)(char *, int), |
231 | struct nlattr *rta) | 231 | struct nlattr *rta) |
232 | { | 232 | { |
233 | struct xfrm_algo *p, *ualg; | 233 | struct xfrm_algo *p, *ualg; |
234 | struct xfrm_algo_desc *algo; | 234 | struct xfrm_algo_desc *algo; |
235 | 235 | ||
236 | if (!rta) | 236 | if (!rta) |
237 | return 0; | 237 | return 0; |
238 | 238 | ||
239 | ualg = nla_data(rta); | 239 | ualg = nla_data(rta); |
240 | 240 | ||
241 | algo = get_byname(ualg->alg_name, 1); | 241 | algo = get_byname(ualg->alg_name, 1); |
242 | if (!algo) | 242 | if (!algo) |
243 | return -ENOSYS; | 243 | return -ENOSYS; |
244 | *props = algo->desc.sadb_alg_id; | 244 | *props = algo->desc.sadb_alg_id; |
245 | 245 | ||
246 | p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); | 246 | p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); |
247 | if (!p) | 247 | if (!p) |
248 | return -ENOMEM; | 248 | return -ENOMEM; |
249 | 249 | ||
250 | strcpy(p->alg_name, algo->name); | 250 | strcpy(p->alg_name, algo->name); |
251 | *algpp = p; | 251 | *algpp = p; |
252 | return 0; | 252 | return 0; |
253 | } | 253 | } |
254 | 254 | ||
255 | static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, | 255 | static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, |
256 | struct nlattr *rta) | 256 | struct nlattr *rta) |
257 | { | 257 | { |
258 | struct xfrm_algo *ualg; | 258 | struct xfrm_algo *ualg; |
259 | struct xfrm_algo_auth *p; | 259 | struct xfrm_algo_auth *p; |
260 | struct xfrm_algo_desc *algo; | 260 | struct xfrm_algo_desc *algo; |
261 | 261 | ||
262 | if (!rta) | 262 | if (!rta) |
263 | return 0; | 263 | return 0; |
264 | 264 | ||
265 | ualg = nla_data(rta); | 265 | ualg = nla_data(rta); |
266 | 266 | ||
267 | algo = xfrm_aalg_get_byname(ualg->alg_name, 1); | 267 | algo = xfrm_aalg_get_byname(ualg->alg_name, 1); |
268 | if (!algo) | 268 | if (!algo) |
269 | return -ENOSYS; | 269 | return -ENOSYS; |
270 | *props = algo->desc.sadb_alg_id; | 270 | *props = algo->desc.sadb_alg_id; |
271 | 271 | ||
272 | p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); | 272 | p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); |
273 | if (!p) | 273 | if (!p) |
274 | return -ENOMEM; | 274 | return -ENOMEM; |
275 | 275 | ||
276 | strcpy(p->alg_name, algo->name); | 276 | strcpy(p->alg_name, algo->name); |
277 | p->alg_key_len = ualg->alg_key_len; | 277 | p->alg_key_len = ualg->alg_key_len; |
278 | p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; | 278 | p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; |
279 | memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); | 279 | memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); |
280 | 280 | ||
281 | *algpp = p; | 281 | *algpp = p; |
282 | return 0; | 282 | return 0; |
283 | } | 283 | } |
284 | 284 | ||
285 | static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, | 285 | static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, |
286 | struct nlattr *rta) | 286 | struct nlattr *rta) |
287 | { | 287 | { |
288 | struct xfrm_algo_auth *p, *ualg; | 288 | struct xfrm_algo_auth *p, *ualg; |
289 | struct xfrm_algo_desc *algo; | 289 | struct xfrm_algo_desc *algo; |
290 | 290 | ||
291 | if (!rta) | 291 | if (!rta) |
292 | return 0; | 292 | return 0; |
293 | 293 | ||
294 | ualg = nla_data(rta); | 294 | ualg = nla_data(rta); |
295 | 295 | ||
296 | algo = xfrm_aalg_get_byname(ualg->alg_name, 1); | 296 | algo = xfrm_aalg_get_byname(ualg->alg_name, 1); |
297 | if (!algo) | 297 | if (!algo) |
298 | return -ENOSYS; | 298 | return -ENOSYS; |
299 | if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) | 299 | if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) |
300 | return -EINVAL; | 300 | return -EINVAL; |
301 | *props = algo->desc.sadb_alg_id; | 301 | *props = algo->desc.sadb_alg_id; |
302 | 302 | ||
303 | p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); | 303 | p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); |
304 | if (!p) | 304 | if (!p) |
305 | return -ENOMEM; | 305 | return -ENOMEM; |
306 | 306 | ||
307 | strcpy(p->alg_name, algo->name); | 307 | strcpy(p->alg_name, algo->name); |
308 | if (!p->alg_trunc_len) | 308 | if (!p->alg_trunc_len) |
309 | p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; | 309 | p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; |
310 | 310 | ||
311 | *algpp = p; | 311 | *algpp = p; |
312 | return 0; | 312 | return 0; |
313 | } | 313 | } |
314 | 314 | ||
315 | static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props, | 315 | static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props, |
316 | struct nlattr *rta) | 316 | struct nlattr *rta) |
317 | { | 317 | { |
318 | struct xfrm_algo_aead *p, *ualg; | 318 | struct xfrm_algo_aead *p, *ualg; |
319 | struct xfrm_algo_desc *algo; | 319 | struct xfrm_algo_desc *algo; |
320 | 320 | ||
321 | if (!rta) | 321 | if (!rta) |
322 | return 0; | 322 | return 0; |
323 | 323 | ||
324 | ualg = nla_data(rta); | 324 | ualg = nla_data(rta); |
325 | 325 | ||
326 | algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); | 326 | algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); |
327 | if (!algo) | 327 | if (!algo) |
328 | return -ENOSYS; | 328 | return -ENOSYS; |
329 | *props = algo->desc.sadb_alg_id; | 329 | *props = algo->desc.sadb_alg_id; |
330 | 330 | ||
331 | p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); | 331 | p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); |
332 | if (!p) | 332 | if (!p) |
333 | return -ENOMEM; | 333 | return -ENOMEM; |
334 | 334 | ||
335 | strcpy(p->alg_name, algo->name); | 335 | strcpy(p->alg_name, algo->name); |
336 | *algpp = p; | 336 | *algpp = p; |
337 | return 0; | 337 | return 0; |
338 | } | 338 | } |
339 | 339 | ||
340 | static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) | 340 | static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) |
341 | { | 341 | { |
342 | int len = 0; | 342 | int len = 0; |
343 | 343 | ||
344 | if (xfrm_ctx) { | 344 | if (xfrm_ctx) { |
345 | len += sizeof(struct xfrm_user_sec_ctx); | 345 | len += sizeof(struct xfrm_user_sec_ctx); |
346 | len += xfrm_ctx->ctx_len; | 346 | len += xfrm_ctx->ctx_len; |
347 | } | 347 | } |
348 | return len; | 348 | return len; |
349 | } | 349 | } |
350 | 350 | ||
351 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) | 351 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
352 | { | 352 | { |
353 | memcpy(&x->id, &p->id, sizeof(x->id)); | 353 | memcpy(&x->id, &p->id, sizeof(x->id)); |
354 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); | 354 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); |
355 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); | 355 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); |
356 | x->props.mode = p->mode; | 356 | x->props.mode = p->mode; |
357 | x->props.replay_window = p->replay_window; | 357 | x->props.replay_window = p->replay_window; |
358 | x->props.reqid = p->reqid; | 358 | x->props.reqid = p->reqid; |
359 | x->props.family = p->family; | 359 | x->props.family = p->family; |
360 | memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); | 360 | memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); |
361 | x->props.flags = p->flags; | 361 | x->props.flags = p->flags; |
362 | 362 | ||
363 | if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) | 363 | if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) |
364 | x->sel.family = p->family; | 364 | x->sel.family = p->family; |
365 | } | 365 | } |
366 | 366 | ||
367 | /* | 367 | /* |
368 | * someday when pfkey also has support, we could have the code | 368 | * someday when pfkey also has support, we could have the code |
369 | * somehow made shareable and move it to xfrm_state.c - JHS | 369 | * somehow made shareable and move it to xfrm_state.c - JHS |
370 | * | 370 | * |
371 | */ | 371 | */ |
372 | static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) | 372 | static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) |
373 | { | 373 | { |
374 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; | 374 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
375 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | 375 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; |
376 | struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; | 376 | struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; |
377 | struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; | 377 | struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; |
378 | 378 | ||
379 | if (rp) { | 379 | if (rp) { |
380 | struct xfrm_replay_state *replay; | 380 | struct xfrm_replay_state *replay; |
381 | replay = nla_data(rp); | 381 | replay = nla_data(rp); |
382 | memcpy(&x->replay, replay, sizeof(*replay)); | 382 | memcpy(&x->replay, replay, sizeof(*replay)); |
383 | memcpy(&x->preplay, replay, sizeof(*replay)); | 383 | memcpy(&x->preplay, replay, sizeof(*replay)); |
384 | } | 384 | } |
385 | 385 | ||
386 | if (lt) { | 386 | if (lt) { |
387 | struct xfrm_lifetime_cur *ltime; | 387 | struct xfrm_lifetime_cur *ltime; |
388 | ltime = nla_data(lt); | 388 | ltime = nla_data(lt); |
389 | x->curlft.bytes = ltime->bytes; | 389 | x->curlft.bytes = ltime->bytes; |
390 | x->curlft.packets = ltime->packets; | 390 | x->curlft.packets = ltime->packets; |
391 | x->curlft.add_time = ltime->add_time; | 391 | x->curlft.add_time = ltime->add_time; |
392 | x->curlft.use_time = ltime->use_time; | 392 | x->curlft.use_time = ltime->use_time; |
393 | } | 393 | } |
394 | 394 | ||
395 | if (et) | 395 | if (et) |
396 | x->replay_maxage = nla_get_u32(et); | 396 | x->replay_maxage = nla_get_u32(et); |
397 | 397 | ||
398 | if (rt) | 398 | if (rt) |
399 | x->replay_maxdiff = nla_get_u32(rt); | 399 | x->replay_maxdiff = nla_get_u32(rt); |
400 | } | 400 | } |
401 | 401 | ||
402 | static struct xfrm_state *xfrm_state_construct(struct net *net, | 402 | static struct xfrm_state *xfrm_state_construct(struct net *net, |
403 | struct xfrm_usersa_info *p, | 403 | struct xfrm_usersa_info *p, |
404 | struct nlattr **attrs, | 404 | struct nlattr **attrs, |
405 | int *errp) | 405 | int *errp) |
406 | { | 406 | { |
407 | struct xfrm_state *x = xfrm_state_alloc(net); | 407 | struct xfrm_state *x = xfrm_state_alloc(net); |
408 | int err = -ENOMEM; | 408 | int err = -ENOMEM; |
409 | 409 | ||
410 | if (!x) | 410 | if (!x) |
411 | goto error_no_put; | 411 | goto error_no_put; |
412 | 412 | ||
413 | copy_from_user_state(x, p); | 413 | copy_from_user_state(x, p); |
414 | 414 | ||
415 | if ((err = attach_aead(&x->aead, &x->props.ealgo, | 415 | if ((err = attach_aead(&x->aead, &x->props.ealgo, |
416 | attrs[XFRMA_ALG_AEAD]))) | 416 | attrs[XFRMA_ALG_AEAD]))) |
417 | goto error; | 417 | goto error; |
418 | if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, | 418 | if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, |
419 | attrs[XFRMA_ALG_AUTH_TRUNC]))) | 419 | attrs[XFRMA_ALG_AUTH_TRUNC]))) |
420 | goto error; | 420 | goto error; |
421 | if (!x->props.aalgo) { | 421 | if (!x->props.aalgo) { |
422 | if ((err = attach_auth(&x->aalg, &x->props.aalgo, | 422 | if ((err = attach_auth(&x->aalg, &x->props.aalgo, |
423 | attrs[XFRMA_ALG_AUTH]))) | 423 | attrs[XFRMA_ALG_AUTH]))) |
424 | goto error; | 424 | goto error; |
425 | } | 425 | } |
426 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, | 426 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, |
427 | xfrm_ealg_get_byname, | 427 | xfrm_ealg_get_byname, |
428 | attrs[XFRMA_ALG_CRYPT]))) | 428 | attrs[XFRMA_ALG_CRYPT]))) |
429 | goto error; | 429 | goto error; |
430 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, | 430 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, |
431 | xfrm_calg_get_byname, | 431 | xfrm_calg_get_byname, |
432 | attrs[XFRMA_ALG_COMP]))) | 432 | attrs[XFRMA_ALG_COMP]))) |
433 | goto error; | 433 | goto error; |
434 | 434 | ||
435 | if (attrs[XFRMA_ENCAP]) { | 435 | if (attrs[XFRMA_ENCAP]) { |
436 | x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), | 436 | x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), |
437 | sizeof(*x->encap), GFP_KERNEL); | 437 | sizeof(*x->encap), GFP_KERNEL); |
438 | if (x->encap == NULL) | 438 | if (x->encap == NULL) |
439 | goto error; | 439 | goto error; |
440 | } | 440 | } |
441 | 441 | ||
442 | if (attrs[XFRMA_COADDR]) { | 442 | if (attrs[XFRMA_COADDR]) { |
443 | x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), | 443 | x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), |
444 | sizeof(*x->coaddr), GFP_KERNEL); | 444 | sizeof(*x->coaddr), GFP_KERNEL); |
445 | if (x->coaddr == NULL) | 445 | if (x->coaddr == NULL) |
446 | goto error; | 446 | goto error; |
447 | } | 447 | } |
448 | 448 | ||
449 | xfrm_mark_get(attrs, &x->mark); | 449 | xfrm_mark_get(attrs, &x->mark); |
450 | 450 | ||
451 | err = xfrm_init_state(x); | 451 | err = xfrm_init_state(x); |
452 | if (err) | 452 | if (err) |
453 | goto error; | 453 | goto error; |
454 | 454 | ||
455 | if (attrs[XFRMA_SEC_CTX] && | 455 | if (attrs[XFRMA_SEC_CTX] && |
456 | security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) | 456 | security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) |
457 | goto error; | 457 | goto error; |
458 | 458 | ||
459 | x->km.seq = p->seq; | 459 | x->km.seq = p->seq; |
460 | x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; | 460 | x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; |
461 | /* sysctl_xfrm_aevent_etime is in 100ms units */ | 461 | /* sysctl_xfrm_aevent_etime is in 100ms units */ |
462 | x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; | 462 | x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; |
463 | x->preplay.bitmap = 0; | 463 | x->preplay.bitmap = 0; |
464 | x->preplay.seq = x->replay.seq+x->replay_maxdiff; | 464 | x->preplay.seq = x->replay.seq+x->replay_maxdiff; |
465 | x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; | 465 | x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; |
466 | 466 | ||
467 | /* override default values from above */ | 467 | /* override default values from above */ |
468 | 468 | ||
469 | xfrm_update_ae_params(x, attrs); | 469 | xfrm_update_ae_params(x, attrs); |
470 | 470 | ||
471 | return x; | 471 | return x; |
472 | 472 | ||
473 | error: | 473 | error: |
474 | x->km.state = XFRM_STATE_DEAD; | 474 | x->km.state = XFRM_STATE_DEAD; |
475 | xfrm_state_put(x); | 475 | xfrm_state_put(x); |
476 | error_no_put: | 476 | error_no_put: |
477 | *errp = err; | 477 | *errp = err; |
478 | return NULL; | 478 | return NULL; |
479 | } | 479 | } |
480 | 480 | ||
481 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, | 481 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
482 | struct nlattr **attrs) | 482 | struct nlattr **attrs) |
483 | { | 483 | { |
484 | struct net *net = sock_net(skb->sk); | 484 | struct net *net = sock_net(skb->sk); |
485 | struct xfrm_usersa_info *p = nlmsg_data(nlh); | 485 | struct xfrm_usersa_info *p = nlmsg_data(nlh); |
486 | struct xfrm_state *x; | 486 | struct xfrm_state *x; |
487 | int err; | 487 | int err; |
488 | struct km_event c; | 488 | struct km_event c; |
489 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 489 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
490 | u32 sessionid = NETLINK_CB(skb).sessionid; | 490 | u32 sessionid = NETLINK_CB(skb).sessionid; |
491 | u32 sid = NETLINK_CB(skb).sid; | 491 | u32 sid = NETLINK_CB(skb).sid; |
492 | 492 | ||
493 | err = verify_newsa_info(p, attrs); | 493 | err = verify_newsa_info(p, attrs); |
494 | if (err) | 494 | if (err) |
495 | return err; | 495 | return err; |
496 | 496 | ||
497 | x = xfrm_state_construct(net, p, attrs, &err); | 497 | x = xfrm_state_construct(net, p, attrs, &err); |
498 | if (!x) | 498 | if (!x) |
499 | return err; | 499 | return err; |
500 | 500 | ||
501 | xfrm_state_hold(x); | 501 | xfrm_state_hold(x); |
502 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) | 502 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) |
503 | err = xfrm_state_add(x); | 503 | err = xfrm_state_add(x); |
504 | else | 504 | else |
505 | err = xfrm_state_update(x); | 505 | err = xfrm_state_update(x); |
506 | 506 | ||
507 | xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid); | 507 | xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid); |
508 | 508 | ||
509 | if (err < 0) { | 509 | if (err < 0) { |
510 | x->km.state = XFRM_STATE_DEAD; | 510 | x->km.state = XFRM_STATE_DEAD; |
511 | __xfrm_state_put(x); | 511 | __xfrm_state_put(x); |
512 | goto out; | 512 | goto out; |
513 | } | 513 | } |
514 | 514 | ||
515 | c.seq = nlh->nlmsg_seq; | 515 | c.seq = nlh->nlmsg_seq; |
516 | c.pid = nlh->nlmsg_pid; | 516 | c.pid = nlh->nlmsg_pid; |
517 | c.event = nlh->nlmsg_type; | 517 | c.event = nlh->nlmsg_type; |
518 | 518 | ||
519 | km_state_notify(x, &c); | 519 | km_state_notify(x, &c); |
520 | out: | 520 | out: |
521 | xfrm_state_put(x); | 521 | xfrm_state_put(x); |
522 | return err; | 522 | return err; |
523 | } | 523 | } |
524 | 524 | ||
525 | static struct xfrm_state *xfrm_user_state_lookup(struct net *net, | 525 | static struct xfrm_state *xfrm_user_state_lookup(struct net *net, |
526 | struct xfrm_usersa_id *p, | 526 | struct xfrm_usersa_id *p, |
527 | struct nlattr **attrs, | 527 | struct nlattr **attrs, |
528 | int *errp) | 528 | int *errp) |
529 | { | 529 | { |
530 | struct xfrm_state *x = NULL; | 530 | struct xfrm_state *x = NULL; |
531 | struct xfrm_mark m; | 531 | struct xfrm_mark m; |
532 | int err; | 532 | int err; |
533 | u32 mark = xfrm_mark_get(attrs, &m); | 533 | u32 mark = xfrm_mark_get(attrs, &m); |
534 | 534 | ||
535 | if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { | 535 | if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { |
536 | err = -ESRCH; | 536 | err = -ESRCH; |
537 | x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); | 537 | x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family); |
538 | } else { | 538 | } else { |
539 | xfrm_address_t *saddr = NULL; | 539 | xfrm_address_t *saddr = NULL; |
540 | 540 | ||
541 | verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); | 541 | verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); |
542 | if (!saddr) { | 542 | if (!saddr) { |
543 | err = -EINVAL; | 543 | err = -EINVAL; |
544 | goto out; | 544 | goto out; |
545 | } | 545 | } |
546 | 546 | ||
547 | err = -ESRCH; | 547 | err = -ESRCH; |
548 | x = xfrm_state_lookup_byaddr(net, mark, | 548 | x = xfrm_state_lookup_byaddr(net, mark, |
549 | &p->daddr, saddr, | 549 | &p->daddr, saddr, |
550 | p->proto, p->family); | 550 | p->proto, p->family); |
551 | } | 551 | } |
552 | 552 | ||
553 | out: | 553 | out: |
554 | if (!x && errp) | 554 | if (!x && errp) |
555 | *errp = err; | 555 | *errp = err; |
556 | return x; | 556 | return x; |
557 | } | 557 | } |
558 | 558 | ||
559 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, | 559 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
560 | struct nlattr **attrs) | 560 | struct nlattr **attrs) |
561 | { | 561 | { |
562 | struct net *net = sock_net(skb->sk); | 562 | struct net *net = sock_net(skb->sk); |
563 | struct xfrm_state *x; | 563 | struct xfrm_state *x; |
564 | int err = -ESRCH; | 564 | int err = -ESRCH; |
565 | struct km_event c; | 565 | struct km_event c; |
566 | struct xfrm_usersa_id *p = nlmsg_data(nlh); | 566 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
567 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 567 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
568 | u32 sessionid = NETLINK_CB(skb).sessionid; | 568 | u32 sessionid = NETLINK_CB(skb).sessionid; |
569 | u32 sid = NETLINK_CB(skb).sid; | 569 | u32 sid = NETLINK_CB(skb).sid; |
570 | 570 | ||
571 | x = xfrm_user_state_lookup(net, p, attrs, &err); | 571 | x = xfrm_user_state_lookup(net, p, attrs, &err); |
572 | if (x == NULL) | 572 | if (x == NULL) |
573 | return err; | 573 | return err; |
574 | 574 | ||
575 | if ((err = security_xfrm_state_delete(x)) != 0) | 575 | if ((err = security_xfrm_state_delete(x)) != 0) |
576 | goto out; | 576 | goto out; |
577 | 577 | ||
578 | if (xfrm_state_kern(x)) { | 578 | if (xfrm_state_kern(x)) { |
579 | err = -EPERM; | 579 | err = -EPERM; |
580 | goto out; | 580 | goto out; |
581 | } | 581 | } |
582 | 582 | ||
583 | err = xfrm_state_delete(x); | 583 | err = xfrm_state_delete(x); |
584 | 584 | ||
585 | if (err < 0) | 585 | if (err < 0) |
586 | goto out; | 586 | goto out; |
587 | 587 | ||
588 | c.seq = nlh->nlmsg_seq; | 588 | c.seq = nlh->nlmsg_seq; |
589 | c.pid = nlh->nlmsg_pid; | 589 | c.pid = nlh->nlmsg_pid; |
590 | c.event = nlh->nlmsg_type; | 590 | c.event = nlh->nlmsg_type; |
591 | km_state_notify(x, &c); | 591 | km_state_notify(x, &c); |
592 | 592 | ||
593 | out: | 593 | out: |
594 | xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid); | 594 | xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid); |
595 | xfrm_state_put(x); | 595 | xfrm_state_put(x); |
596 | return err; | 596 | return err; |
597 | } | 597 | } |
598 | 598 | ||
599 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) | 599 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
600 | { | 600 | { |
601 | memcpy(&p->id, &x->id, sizeof(p->id)); | 601 | memcpy(&p->id, &x->id, sizeof(p->id)); |
602 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); | 602 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); |
603 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); | 603 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); |
604 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); | 604 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); |
605 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); | 605 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); |
606 | memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); | 606 | memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); |
607 | p->mode = x->props.mode; | 607 | p->mode = x->props.mode; |
608 | p->replay_window = x->props.replay_window; | 608 | p->replay_window = x->props.replay_window; |
609 | p->reqid = x->props.reqid; | 609 | p->reqid = x->props.reqid; |
610 | p->family = x->props.family; | 610 | p->family = x->props.family; |
611 | p->flags = x->props.flags; | 611 | p->flags = x->props.flags; |
612 | p->seq = x->km.seq; | 612 | p->seq = x->km.seq; |
613 | } | 613 | } |
614 | 614 | ||
615 | struct xfrm_dump_info { | 615 | struct xfrm_dump_info { |
616 | struct sk_buff *in_skb; | 616 | struct sk_buff *in_skb; |
617 | struct sk_buff *out_skb; | 617 | struct sk_buff *out_skb; |
618 | u32 nlmsg_seq; | 618 | u32 nlmsg_seq; |
619 | u16 nlmsg_flags; | 619 | u16 nlmsg_flags; |
620 | }; | 620 | }; |
621 | 621 | ||
622 | static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) | 622 | static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) |
623 | { | 623 | { |
624 | struct xfrm_user_sec_ctx *uctx; | 624 | struct xfrm_user_sec_ctx *uctx; |
625 | struct nlattr *attr; | 625 | struct nlattr *attr; |
626 | int ctx_size = sizeof(*uctx) + s->ctx_len; | 626 | int ctx_size = sizeof(*uctx) + s->ctx_len; |
627 | 627 | ||
628 | attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); | 628 | attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); |
629 | if (attr == NULL) | 629 | if (attr == NULL) |
630 | return -EMSGSIZE; | 630 | return -EMSGSIZE; |
631 | 631 | ||
632 | uctx = nla_data(attr); | 632 | uctx = nla_data(attr); |
633 | uctx->exttype = XFRMA_SEC_CTX; | 633 | uctx->exttype = XFRMA_SEC_CTX; |
634 | uctx->len = ctx_size; | 634 | uctx->len = ctx_size; |
635 | uctx->ctx_doi = s->ctx_doi; | 635 | uctx->ctx_doi = s->ctx_doi; |
636 | uctx->ctx_alg = s->ctx_alg; | 636 | uctx->ctx_alg = s->ctx_alg; |
637 | uctx->ctx_len = s->ctx_len; | 637 | uctx->ctx_len = s->ctx_len; |
638 | memcpy(uctx + 1, s->ctx_str, s->ctx_len); | 638 | memcpy(uctx + 1, s->ctx_str, s->ctx_len); |
639 | 639 | ||
640 | return 0; | 640 | return 0; |
641 | } | 641 | } |
642 | 642 | ||
643 | static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) | 643 | static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) |
644 | { | 644 | { |
645 | struct xfrm_algo *algo; | 645 | struct xfrm_algo *algo; |
646 | struct nlattr *nla; | 646 | struct nlattr *nla; |
647 | 647 | ||
648 | nla = nla_reserve(skb, XFRMA_ALG_AUTH, | 648 | nla = nla_reserve(skb, XFRMA_ALG_AUTH, |
649 | sizeof(*algo) + (auth->alg_key_len + 7) / 8); | 649 | sizeof(*algo) + (auth->alg_key_len + 7) / 8); |
650 | if (!nla) | 650 | if (!nla) |
651 | return -EMSGSIZE; | 651 | return -EMSGSIZE; |
652 | 652 | ||
653 | algo = nla_data(nla); | 653 | algo = nla_data(nla); |
654 | strcpy(algo->alg_name, auth->alg_name); | 654 | strcpy(algo->alg_name, auth->alg_name); |
655 | memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8); | 655 | memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8); |
656 | algo->alg_key_len = auth->alg_key_len; | 656 | algo->alg_key_len = auth->alg_key_len; |
657 | 657 | ||
658 | return 0; | 658 | return 0; |
659 | } | 659 | } |
660 | 660 | ||
661 | /* Don't change this without updating xfrm_sa_len! */ | 661 | /* Don't change this without updating xfrm_sa_len! */ |
662 | static int copy_to_user_state_extra(struct xfrm_state *x, | 662 | static int copy_to_user_state_extra(struct xfrm_state *x, |
663 | struct xfrm_usersa_info *p, | 663 | struct xfrm_usersa_info *p, |
664 | struct sk_buff *skb) | 664 | struct sk_buff *skb) |
665 | { | 665 | { |
666 | copy_to_user_state(x, p); | 666 | copy_to_user_state(x, p); |
667 | 667 | ||
668 | if (x->coaddr) | 668 | if (x->coaddr) |
669 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); | 669 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); |
670 | 670 | ||
671 | if (x->lastused) | 671 | if (x->lastused) |
672 | NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused); | 672 | NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused); |
673 | 673 | ||
674 | if (x->aead) | 674 | if (x->aead) |
675 | NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); | 675 | NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); |
676 | if (x->aalg) { | 676 | if (x->aalg) { |
677 | if (copy_to_user_auth(x->aalg, skb)) | 677 | if (copy_to_user_auth(x->aalg, skb)) |
678 | goto nla_put_failure; | 678 | goto nla_put_failure; |
679 | 679 | ||
680 | NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC, | 680 | NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC, |
681 | xfrm_alg_auth_len(x->aalg), x->aalg); | 681 | xfrm_alg_auth_len(x->aalg), x->aalg); |
682 | } | 682 | } |
683 | if (x->ealg) | 683 | if (x->ealg) |
684 | NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); | 684 | NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); |
685 | if (x->calg) | 685 | if (x->calg) |
686 | NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); | 686 | NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); |
687 | 687 | ||
688 | if (x->encap) | 688 | if (x->encap) |
689 | NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); | 689 | NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); |
690 | 690 | ||
691 | if (xfrm_mark_put(skb, &x->mark)) | 691 | if (xfrm_mark_put(skb, &x->mark)) |
692 | goto nla_put_failure; | 692 | goto nla_put_failure; |
693 | 693 | ||
694 | if (x->security && copy_sec_ctx(x->security, skb) < 0) | 694 | if (x->security && copy_sec_ctx(x->security, skb) < 0) |
695 | goto nla_put_failure; | 695 | goto nla_put_failure; |
696 | 696 | ||
697 | return 0; | 697 | return 0; |
698 | 698 | ||
699 | nla_put_failure: | 699 | nla_put_failure: |
700 | return -EMSGSIZE; | 700 | return -EMSGSIZE; |
701 | } | 701 | } |
702 | 702 | ||
703 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) | 703 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) |
704 | { | 704 | { |
705 | struct xfrm_dump_info *sp = ptr; | 705 | struct xfrm_dump_info *sp = ptr; |
706 | struct sk_buff *in_skb = sp->in_skb; | 706 | struct sk_buff *in_skb = sp->in_skb; |
707 | struct sk_buff *skb = sp->out_skb; | 707 | struct sk_buff *skb = sp->out_skb; |
708 | struct xfrm_usersa_info *p; | 708 | struct xfrm_usersa_info *p; |
709 | struct nlmsghdr *nlh; | 709 | struct nlmsghdr *nlh; |
710 | int err; | 710 | int err; |
711 | 711 | ||
712 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, | 712 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, |
713 | XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); | 713 | XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); |
714 | if (nlh == NULL) | 714 | if (nlh == NULL) |
715 | return -EMSGSIZE; | 715 | return -EMSGSIZE; |
716 | 716 | ||
717 | p = nlmsg_data(nlh); | 717 | p = nlmsg_data(nlh); |
718 | 718 | ||
719 | err = copy_to_user_state_extra(x, p, skb); | 719 | err = copy_to_user_state_extra(x, p, skb); |
720 | if (err) | 720 | if (err) |
721 | goto nla_put_failure; | 721 | goto nla_put_failure; |
722 | 722 | ||
723 | nlmsg_end(skb, nlh); | 723 | nlmsg_end(skb, nlh); |
724 | return 0; | 724 | return 0; |
725 | 725 | ||
726 | nla_put_failure: | 726 | nla_put_failure: |
727 | nlmsg_cancel(skb, nlh); | 727 | nlmsg_cancel(skb, nlh); |
728 | return err; | 728 | return err; |
729 | } | 729 | } |
730 | 730 | ||
731 | static int xfrm_dump_sa_done(struct netlink_callback *cb) | 731 | static int xfrm_dump_sa_done(struct netlink_callback *cb) |
732 | { | 732 | { |
733 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; | 733 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; |
734 | xfrm_state_walk_done(walk); | 734 | xfrm_state_walk_done(walk); |
735 | return 0; | 735 | return 0; |
736 | } | 736 | } |
737 | 737 | ||
738 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) | 738 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) |
739 | { | 739 | { |
740 | struct net *net = sock_net(skb->sk); | 740 | struct net *net = sock_net(skb->sk); |
741 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; | 741 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; |
742 | struct xfrm_dump_info info; | 742 | struct xfrm_dump_info info; |
743 | 743 | ||
744 | BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > | 744 | BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > |
745 | sizeof(cb->args) - sizeof(cb->args[0])); | 745 | sizeof(cb->args) - sizeof(cb->args[0])); |
746 | 746 | ||
747 | info.in_skb = cb->skb; | 747 | info.in_skb = cb->skb; |
748 | info.out_skb = skb; | 748 | info.out_skb = skb; |
749 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | 749 | info.nlmsg_seq = cb->nlh->nlmsg_seq; |
750 | info.nlmsg_flags = NLM_F_MULTI; | 750 | info.nlmsg_flags = NLM_F_MULTI; |
751 | 751 | ||
752 | if (!cb->args[0]) { | 752 | if (!cb->args[0]) { |
753 | cb->args[0] = 1; | 753 | cb->args[0] = 1; |
754 | xfrm_state_walk_init(walk, 0); | 754 | xfrm_state_walk_init(walk, 0); |
755 | } | 755 | } |
756 | 756 | ||
757 | (void) xfrm_state_walk(net, walk, dump_one_state, &info); | 757 | (void) xfrm_state_walk(net, walk, dump_one_state, &info); |
758 | 758 | ||
759 | return skb->len; | 759 | return skb->len; |
760 | } | 760 | } |
761 | 761 | ||
762 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, | 762 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, |
763 | struct xfrm_state *x, u32 seq) | 763 | struct xfrm_state *x, u32 seq) |
764 | { | 764 | { |
765 | struct xfrm_dump_info info; | 765 | struct xfrm_dump_info info; |
766 | struct sk_buff *skb; | 766 | struct sk_buff *skb; |
767 | 767 | ||
768 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); | 768 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
769 | if (!skb) | 769 | if (!skb) |
770 | return ERR_PTR(-ENOMEM); | 770 | return ERR_PTR(-ENOMEM); |
771 | 771 | ||
772 | info.in_skb = in_skb; | 772 | info.in_skb = in_skb; |
773 | info.out_skb = skb; | 773 | info.out_skb = skb; |
774 | info.nlmsg_seq = seq; | 774 | info.nlmsg_seq = seq; |
775 | info.nlmsg_flags = 0; | 775 | info.nlmsg_flags = 0; |
776 | 776 | ||
777 | if (dump_one_state(x, 0, &info)) { | 777 | if (dump_one_state(x, 0, &info)) { |
778 | kfree_skb(skb); | 778 | kfree_skb(skb); |
779 | return NULL; | 779 | return NULL; |
780 | } | 780 | } |
781 | 781 | ||
782 | return skb; | 782 | return skb; |
783 | } | 783 | } |
784 | 784 | ||
785 | static inline size_t xfrm_spdinfo_msgsize(void) | 785 | static inline size_t xfrm_spdinfo_msgsize(void) |
786 | { | 786 | { |
787 | return NLMSG_ALIGN(4) | 787 | return NLMSG_ALIGN(4) |
788 | + nla_total_size(sizeof(struct xfrmu_spdinfo)) | 788 | + nla_total_size(sizeof(struct xfrmu_spdinfo)) |
789 | + nla_total_size(sizeof(struct xfrmu_spdhinfo)); | 789 | + nla_total_size(sizeof(struct xfrmu_spdhinfo)); |
790 | } | 790 | } |
791 | 791 | ||
792 | static int build_spdinfo(struct sk_buff *skb, struct net *net, | 792 | static int build_spdinfo(struct sk_buff *skb, struct net *net, |
793 | u32 pid, u32 seq, u32 flags) | 793 | u32 pid, u32 seq, u32 flags) |
794 | { | 794 | { |
795 | struct xfrmk_spdinfo si; | 795 | struct xfrmk_spdinfo si; |
796 | struct xfrmu_spdinfo spc; | 796 | struct xfrmu_spdinfo spc; |
797 | struct xfrmu_spdhinfo sph; | 797 | struct xfrmu_spdhinfo sph; |
798 | struct nlmsghdr *nlh; | 798 | struct nlmsghdr *nlh; |
799 | u32 *f; | 799 | u32 *f; |
800 | 800 | ||
801 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); | 801 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); |
802 | if (nlh == NULL) /* shouldnt really happen ... */ | 802 | if (nlh == NULL) /* shouldnt really happen ... */ |
803 | return -EMSGSIZE; | 803 | return -EMSGSIZE; |
804 | 804 | ||
805 | f = nlmsg_data(nlh); | 805 | f = nlmsg_data(nlh); |
806 | *f = flags; | 806 | *f = flags; |
807 | xfrm_spd_getinfo(net, &si); | 807 | xfrm_spd_getinfo(net, &si); |
808 | spc.incnt = si.incnt; | 808 | spc.incnt = si.incnt; |
809 | spc.outcnt = si.outcnt; | 809 | spc.outcnt = si.outcnt; |
810 | spc.fwdcnt = si.fwdcnt; | 810 | spc.fwdcnt = si.fwdcnt; |
811 | spc.inscnt = si.inscnt; | 811 | spc.inscnt = si.inscnt; |
812 | spc.outscnt = si.outscnt; | 812 | spc.outscnt = si.outscnt; |
813 | spc.fwdscnt = si.fwdscnt; | 813 | spc.fwdscnt = si.fwdscnt; |
814 | sph.spdhcnt = si.spdhcnt; | 814 | sph.spdhcnt = si.spdhcnt; |
815 | sph.spdhmcnt = si.spdhmcnt; | 815 | sph.spdhmcnt = si.spdhmcnt; |
816 | 816 | ||
817 | NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); | 817 | NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); |
818 | NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); | 818 | NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); |
819 | 819 | ||
820 | return nlmsg_end(skb, nlh); | 820 | return nlmsg_end(skb, nlh); |
821 | 821 | ||
822 | nla_put_failure: | 822 | nla_put_failure: |
823 | nlmsg_cancel(skb, nlh); | 823 | nlmsg_cancel(skb, nlh); |
824 | return -EMSGSIZE; | 824 | return -EMSGSIZE; |
825 | } | 825 | } |
826 | 826 | ||
827 | static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | 827 | static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, |
828 | struct nlattr **attrs) | 828 | struct nlattr **attrs) |
829 | { | 829 | { |
830 | struct net *net = sock_net(skb->sk); | 830 | struct net *net = sock_net(skb->sk); |
831 | struct sk_buff *r_skb; | 831 | struct sk_buff *r_skb; |
832 | u32 *flags = nlmsg_data(nlh); | 832 | u32 *flags = nlmsg_data(nlh); |
833 | u32 spid = NETLINK_CB(skb).pid; | 833 | u32 spid = NETLINK_CB(skb).pid; |
834 | u32 seq = nlh->nlmsg_seq; | 834 | u32 seq = nlh->nlmsg_seq; |
835 | 835 | ||
836 | r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); | 836 | r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); |
837 | if (r_skb == NULL) | 837 | if (r_skb == NULL) |
838 | return -ENOMEM; | 838 | return -ENOMEM; |
839 | 839 | ||
840 | if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0) | 840 | if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0) |
841 | BUG(); | 841 | BUG(); |
842 | 842 | ||
843 | return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); | 843 | return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); |
844 | } | 844 | } |
845 | 845 | ||
846 | static inline size_t xfrm_sadinfo_msgsize(void) | 846 | static inline size_t xfrm_sadinfo_msgsize(void) |
847 | { | 847 | { |
848 | return NLMSG_ALIGN(4) | 848 | return NLMSG_ALIGN(4) |
849 | + nla_total_size(sizeof(struct xfrmu_sadhinfo)) | 849 | + nla_total_size(sizeof(struct xfrmu_sadhinfo)) |
850 | + nla_total_size(4); /* XFRMA_SAD_CNT */ | 850 | + nla_total_size(4); /* XFRMA_SAD_CNT */ |
851 | } | 851 | } |
852 | 852 | ||
853 | static int build_sadinfo(struct sk_buff *skb, struct net *net, | 853 | static int build_sadinfo(struct sk_buff *skb, struct net *net, |
854 | u32 pid, u32 seq, u32 flags) | 854 | u32 pid, u32 seq, u32 flags) |
855 | { | 855 | { |
856 | struct xfrmk_sadinfo si; | 856 | struct xfrmk_sadinfo si; |
857 | struct xfrmu_sadhinfo sh; | 857 | struct xfrmu_sadhinfo sh; |
858 | struct nlmsghdr *nlh; | 858 | struct nlmsghdr *nlh; |
859 | u32 *f; | 859 | u32 *f; |
860 | 860 | ||
861 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); | 861 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); |
862 | if (nlh == NULL) /* shouldnt really happen ... */ | 862 | if (nlh == NULL) /* shouldnt really happen ... */ |
863 | return -EMSGSIZE; | 863 | return -EMSGSIZE; |
864 | 864 | ||
865 | f = nlmsg_data(nlh); | 865 | f = nlmsg_data(nlh); |
866 | *f = flags; | 866 | *f = flags; |
867 | xfrm_sad_getinfo(net, &si); | 867 | xfrm_sad_getinfo(net, &si); |
868 | 868 | ||
869 | sh.sadhmcnt = si.sadhmcnt; | 869 | sh.sadhmcnt = si.sadhmcnt; |
870 | sh.sadhcnt = si.sadhcnt; | 870 | sh.sadhcnt = si.sadhcnt; |
871 | 871 | ||
872 | NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt); | 872 | NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt); |
873 | NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); | 873 | NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); |
874 | 874 | ||
875 | return nlmsg_end(skb, nlh); | 875 | return nlmsg_end(skb, nlh); |
876 | 876 | ||
877 | nla_put_failure: | 877 | nla_put_failure: |
878 | nlmsg_cancel(skb, nlh); | 878 | nlmsg_cancel(skb, nlh); |
879 | return -EMSGSIZE; | 879 | return -EMSGSIZE; |
880 | } | 880 | } |
881 | 881 | ||
882 | static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | 882 | static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, |
883 | struct nlattr **attrs) | 883 | struct nlattr **attrs) |
884 | { | 884 | { |
885 | struct net *net = sock_net(skb->sk); | 885 | struct net *net = sock_net(skb->sk); |
886 | struct sk_buff *r_skb; | 886 | struct sk_buff *r_skb; |
887 | u32 *flags = nlmsg_data(nlh); | 887 | u32 *flags = nlmsg_data(nlh); |
888 | u32 spid = NETLINK_CB(skb).pid; | 888 | u32 spid = NETLINK_CB(skb).pid; |
889 | u32 seq = nlh->nlmsg_seq; | 889 | u32 seq = nlh->nlmsg_seq; |
890 | 890 | ||
891 | r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); | 891 | r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); |
892 | if (r_skb == NULL) | 892 | if (r_skb == NULL) |
893 | return -ENOMEM; | 893 | return -ENOMEM; |
894 | 894 | ||
895 | if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0) | 895 | if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0) |
896 | BUG(); | 896 | BUG(); |
897 | 897 | ||
898 | return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); | 898 | return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); |
899 | } | 899 | } |
900 | 900 | ||
901 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, | 901 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
902 | struct nlattr **attrs) | 902 | struct nlattr **attrs) |
903 | { | 903 | { |
904 | struct net *net = sock_net(skb->sk); | 904 | struct net *net = sock_net(skb->sk); |
905 | struct xfrm_usersa_id *p = nlmsg_data(nlh); | 905 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
906 | struct xfrm_state *x; | 906 | struct xfrm_state *x; |
907 | struct sk_buff *resp_skb; | 907 | struct sk_buff *resp_skb; |
908 | int err = -ESRCH; | 908 | int err = -ESRCH; |
909 | 909 | ||
910 | x = xfrm_user_state_lookup(net, p, attrs, &err); | 910 | x = xfrm_user_state_lookup(net, p, attrs, &err); |
911 | if (x == NULL) | 911 | if (x == NULL) |
912 | goto out_noput; | 912 | goto out_noput; |
913 | 913 | ||
914 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | 914 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
915 | if (IS_ERR(resp_skb)) { | 915 | if (IS_ERR(resp_skb)) { |
916 | err = PTR_ERR(resp_skb); | 916 | err = PTR_ERR(resp_skb); |
917 | } else { | 917 | } else { |
918 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); | 918 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); |
919 | } | 919 | } |
920 | xfrm_state_put(x); | 920 | xfrm_state_put(x); |
921 | out_noput: | 921 | out_noput: |
922 | return err; | 922 | return err; |
923 | } | 923 | } |
924 | 924 | ||
925 | static int verify_userspi_info(struct xfrm_userspi_info *p) | 925 | static int verify_userspi_info(struct xfrm_userspi_info *p) |
926 | { | 926 | { |
927 | switch (p->info.id.proto) { | 927 | switch (p->info.id.proto) { |
928 | case IPPROTO_AH: | 928 | case IPPROTO_AH: |
929 | case IPPROTO_ESP: | 929 | case IPPROTO_ESP: |
930 | break; | 930 | break; |
931 | 931 | ||
932 | case IPPROTO_COMP: | 932 | case IPPROTO_COMP: |
933 | /* IPCOMP spi is 16-bits. */ | 933 | /* IPCOMP spi is 16-bits. */ |
934 | if (p->max >= 0x10000) | 934 | if (p->max >= 0x10000) |
935 | return -EINVAL; | 935 | return -EINVAL; |
936 | break; | 936 | break; |
937 | 937 | ||
938 | default: | 938 | default: |
939 | return -EINVAL; | 939 | return -EINVAL; |
940 | } | 940 | } |
941 | 941 | ||
942 | if (p->min > p->max) | 942 | if (p->min > p->max) |
943 | return -EINVAL; | 943 | return -EINVAL; |
944 | 944 | ||
945 | return 0; | 945 | return 0; |
946 | } | 946 | } |
947 | 947 | ||
948 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, | 948 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, |
949 | struct nlattr **attrs) | 949 | struct nlattr **attrs) |
950 | { | 950 | { |
951 | struct net *net = sock_net(skb->sk); | 951 | struct net *net = sock_net(skb->sk); |
952 | struct xfrm_state *x; | 952 | struct xfrm_state *x; |
953 | struct xfrm_userspi_info *p; | 953 | struct xfrm_userspi_info *p; |
954 | struct sk_buff *resp_skb; | 954 | struct sk_buff *resp_skb; |
955 | xfrm_address_t *daddr; | 955 | xfrm_address_t *daddr; |
956 | int family; | 956 | int family; |
957 | int err; | 957 | int err; |
958 | u32 mark; | 958 | u32 mark; |
959 | struct xfrm_mark m; | 959 | struct xfrm_mark m; |
960 | 960 | ||
961 | p = nlmsg_data(nlh); | 961 | p = nlmsg_data(nlh); |
962 | err = verify_userspi_info(p); | 962 | err = verify_userspi_info(p); |
963 | if (err) | 963 | if (err) |
964 | goto out_noput; | 964 | goto out_noput; |
965 | 965 | ||
966 | family = p->info.family; | 966 | family = p->info.family; |
967 | daddr = &p->info.id.daddr; | 967 | daddr = &p->info.id.daddr; |
968 | 968 | ||
969 | x = NULL; | 969 | x = NULL; |
970 | 970 | ||
971 | mark = xfrm_mark_get(attrs, &m); | 971 | mark = xfrm_mark_get(attrs, &m); |
972 | if (p->info.seq) { | 972 | if (p->info.seq) { |
973 | x = xfrm_find_acq_byseq(net, mark, p->info.seq); | 973 | x = xfrm_find_acq_byseq(net, mark, p->info.seq); |
974 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { | 974 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { |
975 | xfrm_state_put(x); | 975 | xfrm_state_put(x); |
976 | x = NULL; | 976 | x = NULL; |
977 | } | 977 | } |
978 | } | 978 | } |
979 | 979 | ||
980 | if (!x) | 980 | if (!x) |
981 | x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, | 981 | x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, |
982 | p->info.id.proto, daddr, | 982 | p->info.id.proto, daddr, |
983 | &p->info.saddr, 1, | 983 | &p->info.saddr, 1, |
984 | family); | 984 | family); |
985 | err = -ENOENT; | 985 | err = -ENOENT; |
986 | if (x == NULL) | 986 | if (x == NULL) |
987 | goto out_noput; | 987 | goto out_noput; |
988 | 988 | ||
989 | err = xfrm_alloc_spi(x, p->min, p->max); | 989 | err = xfrm_alloc_spi(x, p->min, p->max); |
990 | if (err) | 990 | if (err) |
991 | goto out; | 991 | goto out; |
992 | 992 | ||
993 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | 993 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
994 | if (IS_ERR(resp_skb)) { | 994 | if (IS_ERR(resp_skb)) { |
995 | err = PTR_ERR(resp_skb); | 995 | err = PTR_ERR(resp_skb); |
996 | goto out; | 996 | goto out; |
997 | } | 997 | } |
998 | 998 | ||
999 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); | 999 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); |
1000 | 1000 | ||
1001 | out: | 1001 | out: |
1002 | xfrm_state_put(x); | 1002 | xfrm_state_put(x); |
1003 | out_noput: | 1003 | out_noput: |
1004 | return err; | 1004 | return err; |
1005 | } | 1005 | } |
1006 | 1006 | ||
1007 | static int verify_policy_dir(u8 dir) | 1007 | static int verify_policy_dir(u8 dir) |
1008 | { | 1008 | { |
1009 | switch (dir) { | 1009 | switch (dir) { |
1010 | case XFRM_POLICY_IN: | 1010 | case XFRM_POLICY_IN: |
1011 | case XFRM_POLICY_OUT: | 1011 | case XFRM_POLICY_OUT: |
1012 | case XFRM_POLICY_FWD: | 1012 | case XFRM_POLICY_FWD: |
1013 | break; | 1013 | break; |
1014 | 1014 | ||
1015 | default: | 1015 | default: |
1016 | return -EINVAL; | 1016 | return -EINVAL; |
1017 | } | 1017 | } |
1018 | 1018 | ||
1019 | return 0; | 1019 | return 0; |
1020 | } | 1020 | } |
1021 | 1021 | ||
1022 | static int verify_policy_type(u8 type) | 1022 | static int verify_policy_type(u8 type) |
1023 | { | 1023 | { |
1024 | switch (type) { | 1024 | switch (type) { |
1025 | case XFRM_POLICY_TYPE_MAIN: | 1025 | case XFRM_POLICY_TYPE_MAIN: |
1026 | #ifdef CONFIG_XFRM_SUB_POLICY | 1026 | #ifdef CONFIG_XFRM_SUB_POLICY |
1027 | case XFRM_POLICY_TYPE_SUB: | 1027 | case XFRM_POLICY_TYPE_SUB: |
1028 | #endif | 1028 | #endif |
1029 | break; | 1029 | break; |
1030 | 1030 | ||
1031 | default: | 1031 | default: |
1032 | return -EINVAL; | 1032 | return -EINVAL; |
1033 | } | 1033 | } |
1034 | 1034 | ||
1035 | return 0; | 1035 | return 0; |
1036 | } | 1036 | } |
1037 | 1037 | ||
1038 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) | 1038 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) |
1039 | { | 1039 | { |
1040 | switch (p->share) { | 1040 | switch (p->share) { |
1041 | case XFRM_SHARE_ANY: | 1041 | case XFRM_SHARE_ANY: |
1042 | case XFRM_SHARE_SESSION: | 1042 | case XFRM_SHARE_SESSION: |
1043 | case XFRM_SHARE_USER: | 1043 | case XFRM_SHARE_USER: |
1044 | case XFRM_SHARE_UNIQUE: | 1044 | case XFRM_SHARE_UNIQUE: |
1045 | break; | 1045 | break; |
1046 | 1046 | ||
1047 | default: | 1047 | default: |
1048 | return -EINVAL; | 1048 | return -EINVAL; |
1049 | } | 1049 | } |
1050 | 1050 | ||
1051 | switch (p->action) { | 1051 | switch (p->action) { |
1052 | case XFRM_POLICY_ALLOW: | 1052 | case XFRM_POLICY_ALLOW: |
1053 | case XFRM_POLICY_BLOCK: | 1053 | case XFRM_POLICY_BLOCK: |
1054 | break; | 1054 | break; |
1055 | 1055 | ||
1056 | default: | 1056 | default: |
1057 | return -EINVAL; | 1057 | return -EINVAL; |
1058 | } | 1058 | } |
1059 | 1059 | ||
1060 | switch (p->sel.family) { | 1060 | switch (p->sel.family) { |
1061 | case AF_INET: | 1061 | case AF_INET: |
1062 | break; | 1062 | break; |
1063 | 1063 | ||
1064 | case AF_INET6: | 1064 | case AF_INET6: |
1065 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 1065 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
1066 | break; | 1066 | break; |
1067 | #else | 1067 | #else |
1068 | return -EAFNOSUPPORT; | 1068 | return -EAFNOSUPPORT; |
1069 | #endif | 1069 | #endif |
1070 | 1070 | ||
1071 | default: | 1071 | default: |
1072 | return -EINVAL; | 1072 | return -EINVAL; |
1073 | } | 1073 | } |
1074 | 1074 | ||
1075 | return verify_policy_dir(p->dir); | 1075 | return verify_policy_dir(p->dir); |
1076 | } | 1076 | } |
1077 | 1077 | ||
1078 | static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) | 1078 | static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) |
1079 | { | 1079 | { |
1080 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; | 1080 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
1081 | struct xfrm_user_sec_ctx *uctx; | 1081 | struct xfrm_user_sec_ctx *uctx; |
1082 | 1082 | ||
1083 | if (!rt) | 1083 | if (!rt) |
1084 | return 0; | 1084 | return 0; |
1085 | 1085 | ||
1086 | uctx = nla_data(rt); | 1086 | uctx = nla_data(rt); |
1087 | return security_xfrm_policy_alloc(&pol->security, uctx); | 1087 | return security_xfrm_policy_alloc(&pol->security, uctx); |
1088 | } | 1088 | } |
1089 | 1089 | ||
1090 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, | 1090 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, |
1091 | int nr) | 1091 | int nr) |
1092 | { | 1092 | { |
1093 | int i; | 1093 | int i; |
1094 | 1094 | ||
1095 | xp->xfrm_nr = nr; | 1095 | xp->xfrm_nr = nr; |
1096 | for (i = 0; i < nr; i++, ut++) { | 1096 | for (i = 0; i < nr; i++, ut++) { |
1097 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | 1097 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; |
1098 | 1098 | ||
1099 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); | 1099 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); |
1100 | memcpy(&t->saddr, &ut->saddr, | 1100 | memcpy(&t->saddr, &ut->saddr, |
1101 | sizeof(xfrm_address_t)); | 1101 | sizeof(xfrm_address_t)); |
1102 | t->reqid = ut->reqid; | 1102 | t->reqid = ut->reqid; |
1103 | t->mode = ut->mode; | 1103 | t->mode = ut->mode; |
1104 | t->share = ut->share; | 1104 | t->share = ut->share; |
1105 | t->optional = ut->optional; | 1105 | t->optional = ut->optional; |
1106 | t->aalgos = ut->aalgos; | 1106 | t->aalgos = ut->aalgos; |
1107 | t->ealgos = ut->ealgos; | 1107 | t->ealgos = ut->ealgos; |
1108 | t->calgos = ut->calgos; | 1108 | t->calgos = ut->calgos; |
1109 | /* If all masks are ~0, then we allow all algorithms. */ | 1109 | /* If all masks are ~0, then we allow all algorithms. */ |
1110 | t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); | 1110 | t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); |
1111 | t->encap_family = ut->family; | 1111 | t->encap_family = ut->family; |
1112 | } | 1112 | } |
1113 | } | 1113 | } |
1114 | 1114 | ||
1115 | static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) | 1115 | static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) |
1116 | { | 1116 | { |
1117 | int i; | 1117 | int i; |
1118 | 1118 | ||
1119 | if (nr > XFRM_MAX_DEPTH) | 1119 | if (nr > XFRM_MAX_DEPTH) |
1120 | return -EINVAL; | 1120 | return -EINVAL; |
1121 | 1121 | ||
1122 | for (i = 0; i < nr; i++) { | 1122 | for (i = 0; i < nr; i++) { |
1123 | /* We never validated the ut->family value, so many | 1123 | /* We never validated the ut->family value, so many |
1124 | * applications simply leave it at zero. The check was | 1124 | * applications simply leave it at zero. The check was |
1125 | * never made and ut->family was ignored because all | 1125 | * never made and ut->family was ignored because all |
1126 | * templates could be assumed to have the same family as | 1126 | * templates could be assumed to have the same family as |
1127 | * the policy itself. Now that we will have ipv4-in-ipv6 | 1127 | * the policy itself. Now that we will have ipv4-in-ipv6 |
1128 | * and ipv6-in-ipv4 tunnels, this is no longer true. | 1128 | * and ipv6-in-ipv4 tunnels, this is no longer true. |
1129 | */ | 1129 | */ |
1130 | if (!ut[i].family) | 1130 | if (!ut[i].family) |
1131 | ut[i].family = family; | 1131 | ut[i].family = family; |
1132 | 1132 | ||
1133 | switch (ut[i].family) { | 1133 | switch (ut[i].family) { |
1134 | case AF_INET: | 1134 | case AF_INET: |
1135 | break; | 1135 | break; |
1136 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 1136 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
1137 | case AF_INET6: | 1137 | case AF_INET6: |
1138 | break; | 1138 | break; |
1139 | #endif | 1139 | #endif |
1140 | default: | 1140 | default: |
1141 | return -EINVAL; | 1141 | return -EINVAL; |
1142 | } | 1142 | } |
1143 | } | 1143 | } |
1144 | 1144 | ||
1145 | return 0; | 1145 | return 0; |
1146 | } | 1146 | } |
1147 | 1147 | ||
1148 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) | 1148 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) |
1149 | { | 1149 | { |
1150 | struct nlattr *rt = attrs[XFRMA_TMPL]; | 1150 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
1151 | 1151 | ||
1152 | if (!rt) { | 1152 | if (!rt) { |
1153 | pol->xfrm_nr = 0; | 1153 | pol->xfrm_nr = 0; |
1154 | } else { | 1154 | } else { |
1155 | struct xfrm_user_tmpl *utmpl = nla_data(rt); | 1155 | struct xfrm_user_tmpl *utmpl = nla_data(rt); |
1156 | int nr = nla_len(rt) / sizeof(*utmpl); | 1156 | int nr = nla_len(rt) / sizeof(*utmpl); |
1157 | int err; | 1157 | int err; |
1158 | 1158 | ||
1159 | err = validate_tmpl(nr, utmpl, pol->family); | 1159 | err = validate_tmpl(nr, utmpl, pol->family); |
1160 | if (err) | 1160 | if (err) |
1161 | return err; | 1161 | return err; |
1162 | 1162 | ||
1163 | copy_templates(pol, utmpl, nr); | 1163 | copy_templates(pol, utmpl, nr); |
1164 | } | 1164 | } |
1165 | return 0; | 1165 | return 0; |
1166 | } | 1166 | } |
1167 | 1167 | ||
1168 | static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) | 1168 | static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) |
1169 | { | 1169 | { |
1170 | struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; | 1170 | struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; |
1171 | struct xfrm_userpolicy_type *upt; | 1171 | struct xfrm_userpolicy_type *upt; |
1172 | u8 type = XFRM_POLICY_TYPE_MAIN; | 1172 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1173 | int err; | 1173 | int err; |
1174 | 1174 | ||
1175 | if (rt) { | 1175 | if (rt) { |
1176 | upt = nla_data(rt); | 1176 | upt = nla_data(rt); |
1177 | type = upt->type; | 1177 | type = upt->type; |
1178 | } | 1178 | } |
1179 | 1179 | ||
1180 | err = verify_policy_type(type); | 1180 | err = verify_policy_type(type); |
1181 | if (err) | 1181 | if (err) |
1182 | return err; | 1182 | return err; |
1183 | 1183 | ||
1184 | *tp = type; | 1184 | *tp = type; |
1185 | return 0; | 1185 | return 0; |
1186 | } | 1186 | } |
1187 | 1187 | ||
1188 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) | 1188 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) |
1189 | { | 1189 | { |
1190 | xp->priority = p->priority; | 1190 | xp->priority = p->priority; |
1191 | xp->index = p->index; | 1191 | xp->index = p->index; |
1192 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); | 1192 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); |
1193 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); | 1193 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); |
1194 | xp->action = p->action; | 1194 | xp->action = p->action; |
1195 | xp->flags = p->flags; | 1195 | xp->flags = p->flags; |
1196 | xp->family = p->sel.family; | 1196 | xp->family = p->sel.family; |
1197 | /* XXX xp->share = p->share; */ | 1197 | /* XXX xp->share = p->share; */ |
1198 | } | 1198 | } |
1199 | 1199 | ||
1200 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) | 1200 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) |
1201 | { | 1201 | { |
1202 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); | 1202 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); |
1203 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); | 1203 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); |
1204 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); | 1204 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); |
1205 | p->priority = xp->priority; | 1205 | p->priority = xp->priority; |
1206 | p->index = xp->index; | 1206 | p->index = xp->index; |
1207 | p->sel.family = xp->family; | 1207 | p->sel.family = xp->family; |
1208 | p->dir = dir; | 1208 | p->dir = dir; |
1209 | p->action = xp->action; | 1209 | p->action = xp->action; |
1210 | p->flags = xp->flags; | 1210 | p->flags = xp->flags; |
1211 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ | 1211 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ |
1212 | } | 1212 | } |
1213 | 1213 | ||
1214 | static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) | 1214 | static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) |
1215 | { | 1215 | { |
1216 | struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); | 1216 | struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); |
1217 | int err; | 1217 | int err; |
1218 | 1218 | ||
1219 | if (!xp) { | 1219 | if (!xp) { |
1220 | *errp = -ENOMEM; | 1220 | *errp = -ENOMEM; |
1221 | return NULL; | 1221 | return NULL; |
1222 | } | 1222 | } |
1223 | 1223 | ||
1224 | copy_from_user_policy(xp, p); | 1224 | copy_from_user_policy(xp, p); |
1225 | 1225 | ||
1226 | err = copy_from_user_policy_type(&xp->type, attrs); | 1226 | err = copy_from_user_policy_type(&xp->type, attrs); |
1227 | if (err) | 1227 | if (err) |
1228 | goto error; | 1228 | goto error; |
1229 | 1229 | ||
1230 | if (!(err = copy_from_user_tmpl(xp, attrs))) | 1230 | if (!(err = copy_from_user_tmpl(xp, attrs))) |
1231 | err = copy_from_user_sec_ctx(xp, attrs); | 1231 | err = copy_from_user_sec_ctx(xp, attrs); |
1232 | if (err) | 1232 | if (err) |
1233 | goto error; | 1233 | goto error; |
1234 | 1234 | ||
1235 | xfrm_mark_get(attrs, &xp->mark); | 1235 | xfrm_mark_get(attrs, &xp->mark); |
1236 | 1236 | ||
1237 | return xp; | 1237 | return xp; |
1238 | error: | 1238 | error: |
1239 | *errp = err; | 1239 | *errp = err; |
1240 | xp->walk.dead = 1; | 1240 | xp->walk.dead = 1; |
1241 | xfrm_policy_destroy(xp); | 1241 | xfrm_policy_destroy(xp); |
1242 | return NULL; | 1242 | return NULL; |
1243 | } | 1243 | } |
1244 | 1244 | ||
1245 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, | 1245 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
1246 | struct nlattr **attrs) | 1246 | struct nlattr **attrs) |
1247 | { | 1247 | { |
1248 | struct net *net = sock_net(skb->sk); | 1248 | struct net *net = sock_net(skb->sk); |
1249 | struct xfrm_userpolicy_info *p = nlmsg_data(nlh); | 1249 | struct xfrm_userpolicy_info *p = nlmsg_data(nlh); |
1250 | struct xfrm_policy *xp; | 1250 | struct xfrm_policy *xp; |
1251 | struct km_event c; | 1251 | struct km_event c; |
1252 | int err; | 1252 | int err; |
1253 | int excl; | 1253 | int excl; |
1254 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 1254 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
1255 | u32 sessionid = NETLINK_CB(skb).sessionid; | 1255 | u32 sessionid = NETLINK_CB(skb).sessionid; |
1256 | u32 sid = NETLINK_CB(skb).sid; | 1256 | u32 sid = NETLINK_CB(skb).sid; |
1257 | 1257 | ||
1258 | err = verify_newpolicy_info(p); | 1258 | err = verify_newpolicy_info(p); |
1259 | if (err) | 1259 | if (err) |
1260 | return err; | 1260 | return err; |
1261 | err = verify_sec_ctx_len(attrs); | 1261 | err = verify_sec_ctx_len(attrs); |
1262 | if (err) | 1262 | if (err) |
1263 | return err; | 1263 | return err; |
1264 | 1264 | ||
1265 | xp = xfrm_policy_construct(net, p, attrs, &err); | 1265 | xp = xfrm_policy_construct(net, p, attrs, &err); |
1266 | if (!xp) | 1266 | if (!xp) |
1267 | return err; | 1267 | return err; |
1268 | 1268 | ||
1269 | /* shouldnt excl be based on nlh flags?? | 1269 | /* shouldnt excl be based on nlh flags?? |
1270 | * Aha! this is anti-netlink really i.e more pfkey derived | 1270 | * Aha! this is anti-netlink really i.e more pfkey derived |
1271 | * in netlink excl is a flag and you wouldnt need | 1271 | * in netlink excl is a flag and you wouldnt need |
1272 | * a type XFRM_MSG_UPDPOLICY - JHS */ | 1272 | * a type XFRM_MSG_UPDPOLICY - JHS */ |
1273 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; | 1273 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; |
1274 | err = xfrm_policy_insert(p->dir, xp, excl); | 1274 | err = xfrm_policy_insert(p->dir, xp, excl); |
1275 | xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid); | 1275 | xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid); |
1276 | 1276 | ||
1277 | if (err) { | 1277 | if (err) { |
1278 | security_xfrm_policy_free(xp->security); | 1278 | security_xfrm_policy_free(xp->security); |
1279 | kfree(xp); | 1279 | kfree(xp); |
1280 | return err; | 1280 | return err; |
1281 | } | 1281 | } |
1282 | 1282 | ||
1283 | c.event = nlh->nlmsg_type; | 1283 | c.event = nlh->nlmsg_type; |
1284 | c.seq = nlh->nlmsg_seq; | 1284 | c.seq = nlh->nlmsg_seq; |
1285 | c.pid = nlh->nlmsg_pid; | 1285 | c.pid = nlh->nlmsg_pid; |
1286 | km_policy_notify(xp, p->dir, &c); | 1286 | km_policy_notify(xp, p->dir, &c); |
1287 | 1287 | ||
1288 | xfrm_pol_put(xp); | 1288 | xfrm_pol_put(xp); |
1289 | 1289 | ||
1290 | return 0; | 1290 | return 0; |
1291 | } | 1291 | } |
1292 | 1292 | ||
1293 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) | 1293 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) |
1294 | { | 1294 | { |
1295 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; | 1295 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; |
1296 | int i; | 1296 | int i; |
1297 | 1297 | ||
1298 | if (xp->xfrm_nr == 0) | 1298 | if (xp->xfrm_nr == 0) |
1299 | return 0; | 1299 | return 0; |
1300 | 1300 | ||
1301 | for (i = 0; i < xp->xfrm_nr; i++) { | 1301 | for (i = 0; i < xp->xfrm_nr; i++) { |
1302 | struct xfrm_user_tmpl *up = &vec[i]; | 1302 | struct xfrm_user_tmpl *up = &vec[i]; |
1303 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; | 1303 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; |
1304 | 1304 | ||
1305 | memcpy(&up->id, &kp->id, sizeof(up->id)); | 1305 | memcpy(&up->id, &kp->id, sizeof(up->id)); |
1306 | up->family = kp->encap_family; | 1306 | up->family = kp->encap_family; |
1307 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); | 1307 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); |
1308 | up->reqid = kp->reqid; | 1308 | up->reqid = kp->reqid; |
1309 | up->mode = kp->mode; | 1309 | up->mode = kp->mode; |
1310 | up->share = kp->share; | 1310 | up->share = kp->share; |
1311 | up->optional = kp->optional; | 1311 | up->optional = kp->optional; |
1312 | up->aalgos = kp->aalgos; | 1312 | up->aalgos = kp->aalgos; |
1313 | up->ealgos = kp->ealgos; | 1313 | up->ealgos = kp->ealgos; |
1314 | up->calgos = kp->calgos; | 1314 | up->calgos = kp->calgos; |
1315 | } | 1315 | } |
1316 | 1316 | ||
1317 | return nla_put(skb, XFRMA_TMPL, | 1317 | return nla_put(skb, XFRMA_TMPL, |
1318 | sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); | 1318 | sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); |
1319 | } | 1319 | } |
1320 | 1320 | ||
1321 | static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) | 1321 | static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) |
1322 | { | 1322 | { |
1323 | if (x->security) { | 1323 | if (x->security) { |
1324 | return copy_sec_ctx(x->security, skb); | 1324 | return copy_sec_ctx(x->security, skb); |
1325 | } | 1325 | } |
1326 | return 0; | 1326 | return 0; |
1327 | } | 1327 | } |
1328 | 1328 | ||
1329 | static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) | 1329 | static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) |
1330 | { | 1330 | { |
1331 | if (xp->security) { | 1331 | if (xp->security) { |
1332 | return copy_sec_ctx(xp->security, skb); | 1332 | return copy_sec_ctx(xp->security, skb); |
1333 | } | 1333 | } |
1334 | return 0; | 1334 | return 0; |
1335 | } | 1335 | } |
1336 | static inline size_t userpolicy_type_attrsize(void) | 1336 | static inline size_t userpolicy_type_attrsize(void) |
1337 | { | 1337 | { |
1338 | #ifdef CONFIG_XFRM_SUB_POLICY | 1338 | #ifdef CONFIG_XFRM_SUB_POLICY |
1339 | return nla_total_size(sizeof(struct xfrm_userpolicy_type)); | 1339 | return nla_total_size(sizeof(struct xfrm_userpolicy_type)); |
1340 | #else | 1340 | #else |
1341 | return 0; | 1341 | return 0; |
1342 | #endif | 1342 | #endif |
1343 | } | 1343 | } |
1344 | 1344 | ||
1345 | #ifdef CONFIG_XFRM_SUB_POLICY | 1345 | #ifdef CONFIG_XFRM_SUB_POLICY |
1346 | static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) | 1346 | static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
1347 | { | 1347 | { |
1348 | struct xfrm_userpolicy_type upt = { | 1348 | struct xfrm_userpolicy_type upt = { |
1349 | .type = type, | 1349 | .type = type, |
1350 | }; | 1350 | }; |
1351 | 1351 | ||
1352 | return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); | 1352 | return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); |
1353 | } | 1353 | } |
1354 | 1354 | ||
1355 | #else | 1355 | #else |
1356 | static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) | 1356 | static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
1357 | { | 1357 | { |
1358 | return 0; | 1358 | return 0; |
1359 | } | 1359 | } |
1360 | #endif | 1360 | #endif |
1361 | 1361 | ||
1362 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) | 1362 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) |
1363 | { | 1363 | { |
1364 | struct xfrm_dump_info *sp = ptr; | 1364 | struct xfrm_dump_info *sp = ptr; |
1365 | struct xfrm_userpolicy_info *p; | 1365 | struct xfrm_userpolicy_info *p; |
1366 | struct sk_buff *in_skb = sp->in_skb; | 1366 | struct sk_buff *in_skb = sp->in_skb; |
1367 | struct sk_buff *skb = sp->out_skb; | 1367 | struct sk_buff *skb = sp->out_skb; |
1368 | struct nlmsghdr *nlh; | 1368 | struct nlmsghdr *nlh; |
1369 | 1369 | ||
1370 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, | 1370 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, |
1371 | XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); | 1371 | XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); |
1372 | if (nlh == NULL) | 1372 | if (nlh == NULL) |
1373 | return -EMSGSIZE; | 1373 | return -EMSGSIZE; |
1374 | 1374 | ||
1375 | p = nlmsg_data(nlh); | 1375 | p = nlmsg_data(nlh); |
1376 | copy_to_user_policy(xp, p, dir); | 1376 | copy_to_user_policy(xp, p, dir); |
1377 | if (copy_to_user_tmpl(xp, skb) < 0) | 1377 | if (copy_to_user_tmpl(xp, skb) < 0) |
1378 | goto nlmsg_failure; | 1378 | goto nlmsg_failure; |
1379 | if (copy_to_user_sec_ctx(xp, skb)) | 1379 | if (copy_to_user_sec_ctx(xp, skb)) |
1380 | goto nlmsg_failure; | 1380 | goto nlmsg_failure; |
1381 | if (copy_to_user_policy_type(xp->type, skb) < 0) | 1381 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
1382 | goto nlmsg_failure; | 1382 | goto nlmsg_failure; |
1383 | if (xfrm_mark_put(skb, &xp->mark)) | 1383 | if (xfrm_mark_put(skb, &xp->mark)) |
1384 | goto nla_put_failure; | 1384 | goto nla_put_failure; |
1385 | 1385 | ||
1386 | nlmsg_end(skb, nlh); | 1386 | nlmsg_end(skb, nlh); |
1387 | return 0; | 1387 | return 0; |
1388 | 1388 | ||
1389 | nla_put_failure: | 1389 | nla_put_failure: |
1390 | nlmsg_failure: | 1390 | nlmsg_failure: |
1391 | nlmsg_cancel(skb, nlh); | 1391 | nlmsg_cancel(skb, nlh); |
1392 | return -EMSGSIZE; | 1392 | return -EMSGSIZE; |
1393 | } | 1393 | } |
1394 | 1394 | ||
1395 | static int xfrm_dump_policy_done(struct netlink_callback *cb) | 1395 | static int xfrm_dump_policy_done(struct netlink_callback *cb) |
1396 | { | 1396 | { |
1397 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; | 1397 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; |
1398 | 1398 | ||
1399 | xfrm_policy_walk_done(walk); | 1399 | xfrm_policy_walk_done(walk); |
1400 | return 0; | 1400 | return 0; |
1401 | } | 1401 | } |
1402 | 1402 | ||
1403 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) | 1403 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) |
1404 | { | 1404 | { |
1405 | struct net *net = sock_net(skb->sk); | 1405 | struct net *net = sock_net(skb->sk); |
1406 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; | 1406 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; |
1407 | struct xfrm_dump_info info; | 1407 | struct xfrm_dump_info info; |
1408 | 1408 | ||
1409 | BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > | 1409 | BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > |
1410 | sizeof(cb->args) - sizeof(cb->args[0])); | 1410 | sizeof(cb->args) - sizeof(cb->args[0])); |
1411 | 1411 | ||
1412 | info.in_skb = cb->skb; | 1412 | info.in_skb = cb->skb; |
1413 | info.out_skb = skb; | 1413 | info.out_skb = skb; |
1414 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | 1414 | info.nlmsg_seq = cb->nlh->nlmsg_seq; |
1415 | info.nlmsg_flags = NLM_F_MULTI; | 1415 | info.nlmsg_flags = NLM_F_MULTI; |
1416 | 1416 | ||
1417 | if (!cb->args[0]) { | 1417 | if (!cb->args[0]) { |
1418 | cb->args[0] = 1; | 1418 | cb->args[0] = 1; |
1419 | xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); | 1419 | xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); |
1420 | } | 1420 | } |
1421 | 1421 | ||
1422 | (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); | 1422 | (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); |
1423 | 1423 | ||
1424 | return skb->len; | 1424 | return skb->len; |
1425 | } | 1425 | } |
1426 | 1426 | ||
1427 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, | 1427 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, |
1428 | struct xfrm_policy *xp, | 1428 | struct xfrm_policy *xp, |
1429 | int dir, u32 seq) | 1429 | int dir, u32 seq) |
1430 | { | 1430 | { |
1431 | struct xfrm_dump_info info; | 1431 | struct xfrm_dump_info info; |
1432 | struct sk_buff *skb; | 1432 | struct sk_buff *skb; |
1433 | 1433 | ||
1434 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | 1434 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1435 | if (!skb) | 1435 | if (!skb) |
1436 | return ERR_PTR(-ENOMEM); | 1436 | return ERR_PTR(-ENOMEM); |
1437 | 1437 | ||
1438 | info.in_skb = in_skb; | 1438 | info.in_skb = in_skb; |
1439 | info.out_skb = skb; | 1439 | info.out_skb = skb; |
1440 | info.nlmsg_seq = seq; | 1440 | info.nlmsg_seq = seq; |
1441 | info.nlmsg_flags = 0; | 1441 | info.nlmsg_flags = 0; |
1442 | 1442 | ||
1443 | if (dump_one_policy(xp, dir, 0, &info) < 0) { | 1443 | if (dump_one_policy(xp, dir, 0, &info) < 0) { |
1444 | kfree_skb(skb); | 1444 | kfree_skb(skb); |
1445 | return NULL; | 1445 | return NULL; |
1446 | } | 1446 | } |
1447 | 1447 | ||
1448 | return skb; | 1448 | return skb; |
1449 | } | 1449 | } |
1450 | 1450 | ||
1451 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, | 1451 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
1452 | struct nlattr **attrs) | 1452 | struct nlattr **attrs) |
1453 | { | 1453 | { |
1454 | struct net *net = sock_net(skb->sk); | 1454 | struct net *net = sock_net(skb->sk); |
1455 | struct xfrm_policy *xp; | 1455 | struct xfrm_policy *xp; |
1456 | struct xfrm_userpolicy_id *p; | 1456 | struct xfrm_userpolicy_id *p; |
1457 | u8 type = XFRM_POLICY_TYPE_MAIN; | 1457 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1458 | int err; | 1458 | int err; |
1459 | struct km_event c; | 1459 | struct km_event c; |
1460 | int delete; | 1460 | int delete; |
1461 | struct xfrm_mark m; | 1461 | struct xfrm_mark m; |
1462 | u32 mark = xfrm_mark_get(attrs, &m); | 1462 | u32 mark = xfrm_mark_get(attrs, &m); |
1463 | 1463 | ||
1464 | p = nlmsg_data(nlh); | 1464 | p = nlmsg_data(nlh); |
1465 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; | 1465 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; |
1466 | 1466 | ||
1467 | err = copy_from_user_policy_type(&type, attrs); | 1467 | err = copy_from_user_policy_type(&type, attrs); |
1468 | if (err) | 1468 | if (err) |
1469 | return err; | 1469 | return err; |
1470 | 1470 | ||
1471 | err = verify_policy_dir(p->dir); | 1471 | err = verify_policy_dir(p->dir); |
1472 | if (err) | 1472 | if (err) |
1473 | return err; | 1473 | return err; |
1474 | 1474 | ||
1475 | if (p->index) | 1475 | if (p->index) |
1476 | xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); | 1476 | xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); |
1477 | else { | 1477 | else { |
1478 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; | 1478 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
1479 | struct xfrm_sec_ctx *ctx; | 1479 | struct xfrm_sec_ctx *ctx; |
1480 | 1480 | ||
1481 | err = verify_sec_ctx_len(attrs); | 1481 | err = verify_sec_ctx_len(attrs); |
1482 | if (err) | 1482 | if (err) |
1483 | return err; | 1483 | return err; |
1484 | 1484 | ||
1485 | ctx = NULL; | 1485 | ctx = NULL; |
1486 | if (rt) { | 1486 | if (rt) { |
1487 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); | 1487 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
1488 | 1488 | ||
1489 | err = security_xfrm_policy_alloc(&ctx, uctx); | 1489 | err = security_xfrm_policy_alloc(&ctx, uctx); |
1490 | if (err) | 1490 | if (err) |
1491 | return err; | 1491 | return err; |
1492 | } | 1492 | } |
1493 | xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, | 1493 | xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, |
1494 | ctx, delete, &err); | 1494 | ctx, delete, &err); |
1495 | security_xfrm_policy_free(ctx); | 1495 | security_xfrm_policy_free(ctx); |
1496 | } | 1496 | } |
1497 | if (xp == NULL) | 1497 | if (xp == NULL) |
1498 | return -ENOENT; | 1498 | return -ENOENT; |
1499 | 1499 | ||
1500 | if (!delete) { | 1500 | if (!delete) { |
1501 | struct sk_buff *resp_skb; | 1501 | struct sk_buff *resp_skb; |
1502 | 1502 | ||
1503 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); | 1503 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); |
1504 | if (IS_ERR(resp_skb)) { | 1504 | if (IS_ERR(resp_skb)) { |
1505 | err = PTR_ERR(resp_skb); | 1505 | err = PTR_ERR(resp_skb); |
1506 | } else { | 1506 | } else { |
1507 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, | 1507 | err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, |
1508 | NETLINK_CB(skb).pid); | 1508 | NETLINK_CB(skb).pid); |
1509 | } | 1509 | } |
1510 | } else { | 1510 | } else { |
1511 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 1511 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
1512 | u32 sessionid = NETLINK_CB(skb).sessionid; | 1512 | u32 sessionid = NETLINK_CB(skb).sessionid; |
1513 | u32 sid = NETLINK_CB(skb).sid; | 1513 | u32 sid = NETLINK_CB(skb).sid; |
1514 | 1514 | ||
1515 | xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid, | 1515 | xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid, |
1516 | sid); | 1516 | sid); |
1517 | 1517 | ||
1518 | if (err != 0) | 1518 | if (err != 0) |
1519 | goto out; | 1519 | goto out; |
1520 | 1520 | ||
1521 | c.data.byid = p->index; | 1521 | c.data.byid = p->index; |
1522 | c.event = nlh->nlmsg_type; | 1522 | c.event = nlh->nlmsg_type; |
1523 | c.seq = nlh->nlmsg_seq; | 1523 | c.seq = nlh->nlmsg_seq; |
1524 | c.pid = nlh->nlmsg_pid; | 1524 | c.pid = nlh->nlmsg_pid; |
1525 | km_policy_notify(xp, p->dir, &c); | 1525 | km_policy_notify(xp, p->dir, &c); |
1526 | } | 1526 | } |
1527 | 1527 | ||
1528 | out: | 1528 | out: |
1529 | xfrm_pol_put(xp); | 1529 | xfrm_pol_put(xp); |
1530 | return err; | 1530 | return err; |
1531 | } | 1531 | } |
1532 | 1532 | ||
1533 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, | 1533 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
1534 | struct nlattr **attrs) | 1534 | struct nlattr **attrs) |
1535 | { | 1535 | { |
1536 | struct net *net = sock_net(skb->sk); | 1536 | struct net *net = sock_net(skb->sk); |
1537 | struct km_event c; | 1537 | struct km_event c; |
1538 | struct xfrm_usersa_flush *p = nlmsg_data(nlh); | 1538 | struct xfrm_usersa_flush *p = nlmsg_data(nlh); |
1539 | struct xfrm_audit audit_info; | 1539 | struct xfrm_audit audit_info; |
1540 | int err; | 1540 | int err; |
1541 | 1541 | ||
1542 | audit_info.loginuid = NETLINK_CB(skb).loginuid; | 1542 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1543 | audit_info.sessionid = NETLINK_CB(skb).sessionid; | 1543 | audit_info.sessionid = NETLINK_CB(skb).sessionid; |
1544 | audit_info.secid = NETLINK_CB(skb).sid; | 1544 | audit_info.secid = NETLINK_CB(skb).sid; |
1545 | err = xfrm_state_flush(net, p->proto, &audit_info); | 1545 | err = xfrm_state_flush(net, p->proto, &audit_info); |
1546 | if (err) { | 1546 | if (err) { |
1547 | if (err == -ESRCH) /* empty table */ | 1547 | if (err == -ESRCH) /* empty table */ |
1548 | return 0; | 1548 | return 0; |
1549 | return err; | 1549 | return err; |
1550 | } | 1550 | } |
1551 | c.data.proto = p->proto; | 1551 | c.data.proto = p->proto; |
1552 | c.event = nlh->nlmsg_type; | 1552 | c.event = nlh->nlmsg_type; |
1553 | c.seq = nlh->nlmsg_seq; | 1553 | c.seq = nlh->nlmsg_seq; |
1554 | c.pid = nlh->nlmsg_pid; | 1554 | c.pid = nlh->nlmsg_pid; |
1555 | c.net = net; | 1555 | c.net = net; |
1556 | km_state_notify(NULL, &c); | 1556 | km_state_notify(NULL, &c); |
1557 | 1557 | ||
1558 | return 0; | 1558 | return 0; |
1559 | } | 1559 | } |
1560 | 1560 | ||
1561 | static inline size_t xfrm_aevent_msgsize(void) | 1561 | static inline size_t xfrm_aevent_msgsize(void) |
1562 | { | 1562 | { |
1563 | return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) | 1563 | return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) |
1564 | + nla_total_size(sizeof(struct xfrm_replay_state)) | 1564 | + nla_total_size(sizeof(struct xfrm_replay_state)) |
1565 | + nla_total_size(sizeof(struct xfrm_lifetime_cur)) | 1565 | + nla_total_size(sizeof(struct xfrm_lifetime_cur)) |
1566 | + nla_total_size(sizeof(struct xfrm_mark)) | 1566 | + nla_total_size(sizeof(struct xfrm_mark)) |
1567 | + nla_total_size(4) /* XFRM_AE_RTHR */ | 1567 | + nla_total_size(4) /* XFRM_AE_RTHR */ |
1568 | + nla_total_size(4); /* XFRM_AE_ETHR */ | 1568 | + nla_total_size(4); /* XFRM_AE_ETHR */ |
1569 | } | 1569 | } |
1570 | 1570 | ||
1571 | static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) | 1571 | static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) |
1572 | { | 1572 | { |
1573 | struct xfrm_aevent_id *id; | 1573 | struct xfrm_aevent_id *id; |
1574 | struct nlmsghdr *nlh; | 1574 | struct nlmsghdr *nlh; |
1575 | 1575 | ||
1576 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); | 1576 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); |
1577 | if (nlh == NULL) | 1577 | if (nlh == NULL) |
1578 | return -EMSGSIZE; | 1578 | return -EMSGSIZE; |
1579 | 1579 | ||
1580 | id = nlmsg_data(nlh); | 1580 | id = nlmsg_data(nlh); |
1581 | memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); | 1581 | memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); |
1582 | id->sa_id.spi = x->id.spi; | 1582 | id->sa_id.spi = x->id.spi; |
1583 | id->sa_id.family = x->props.family; | 1583 | id->sa_id.family = x->props.family; |
1584 | id->sa_id.proto = x->id.proto; | 1584 | id->sa_id.proto = x->id.proto; |
1585 | memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); | 1585 | memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); |
1586 | id->reqid = x->props.reqid; | 1586 | id->reqid = x->props.reqid; |
1587 | id->flags = c->data.aevent; | 1587 | id->flags = c->data.aevent; |
1588 | 1588 | ||
1589 | NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); | 1589 | NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); |
1590 | NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); | 1590 | NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); |
1591 | 1591 | ||
1592 | if (id->flags & XFRM_AE_RTHR) | 1592 | if (id->flags & XFRM_AE_RTHR) |
1593 | NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); | 1593 | NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); |
1594 | 1594 | ||
1595 | if (id->flags & XFRM_AE_ETHR) | 1595 | if (id->flags & XFRM_AE_ETHR) |
1596 | NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH, | 1596 | NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH, |
1597 | x->replay_maxage * 10 / HZ); | 1597 | x->replay_maxage * 10 / HZ); |
1598 | 1598 | ||
1599 | if (xfrm_mark_put(skb, &x->mark)) | 1599 | if (xfrm_mark_put(skb, &x->mark)) |
1600 | goto nla_put_failure; | 1600 | goto nla_put_failure; |
1601 | 1601 | ||
1602 | return nlmsg_end(skb, nlh); | 1602 | return nlmsg_end(skb, nlh); |
1603 | 1603 | ||
1604 | nla_put_failure: | 1604 | nla_put_failure: |
1605 | nlmsg_cancel(skb, nlh); | 1605 | nlmsg_cancel(skb, nlh); |
1606 | return -EMSGSIZE; | 1606 | return -EMSGSIZE; |
1607 | } | 1607 | } |
1608 | 1608 | ||
1609 | static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, | 1609 | static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
1610 | struct nlattr **attrs) | 1610 | struct nlattr **attrs) |
1611 | { | 1611 | { |
1612 | struct net *net = sock_net(skb->sk); | 1612 | struct net *net = sock_net(skb->sk); |
1613 | struct xfrm_state *x; | 1613 | struct xfrm_state *x; |
1614 | struct sk_buff *r_skb; | 1614 | struct sk_buff *r_skb; |
1615 | int err; | 1615 | int err; |
1616 | struct km_event c; | 1616 | struct km_event c; |
1617 | u32 mark; | 1617 | u32 mark; |
1618 | struct xfrm_mark m; | 1618 | struct xfrm_mark m; |
1619 | struct xfrm_aevent_id *p = nlmsg_data(nlh); | 1619 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
1620 | struct xfrm_usersa_id *id = &p->sa_id; | 1620 | struct xfrm_usersa_id *id = &p->sa_id; |
1621 | 1621 | ||
1622 | r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); | 1622 | r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
1623 | if (r_skb == NULL) | 1623 | if (r_skb == NULL) |
1624 | return -ENOMEM; | 1624 | return -ENOMEM; |
1625 | 1625 | ||
1626 | mark = xfrm_mark_get(attrs, &m); | 1626 | mark = xfrm_mark_get(attrs, &m); |
1627 | 1627 | ||
1628 | x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); | 1628 | x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); |
1629 | if (x == NULL) { | 1629 | if (x == NULL) { |
1630 | kfree_skb(r_skb); | 1630 | kfree_skb(r_skb); |
1631 | return -ESRCH; | 1631 | return -ESRCH; |
1632 | } | 1632 | } |
1633 | 1633 | ||
1634 | /* | 1634 | /* |
1635 | * XXX: is this lock really needed - none of the other | 1635 | * XXX: is this lock really needed - none of the other |
1636 | * gets lock (the concern is things getting updated | 1636 | * gets lock (the concern is things getting updated |
1637 | * while we are still reading) - jhs | 1637 | * while we are still reading) - jhs |
1638 | */ | 1638 | */ |
1639 | spin_lock_bh(&x->lock); | 1639 | spin_lock_bh(&x->lock); |
1640 | c.data.aevent = p->flags; | 1640 | c.data.aevent = p->flags; |
1641 | c.seq = nlh->nlmsg_seq; | 1641 | c.seq = nlh->nlmsg_seq; |
1642 | c.pid = nlh->nlmsg_pid; | 1642 | c.pid = nlh->nlmsg_pid; |
1643 | 1643 | ||
1644 | if (build_aevent(r_skb, x, &c) < 0) | 1644 | if (build_aevent(r_skb, x, &c) < 0) |
1645 | BUG(); | 1645 | BUG(); |
1646 | err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid); | 1646 | err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid); |
1647 | spin_unlock_bh(&x->lock); | 1647 | spin_unlock_bh(&x->lock); |
1648 | xfrm_state_put(x); | 1648 | xfrm_state_put(x); |
1649 | return err; | 1649 | return err; |
1650 | } | 1650 | } |
1651 | 1651 | ||
1652 | static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, | 1652 | static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
1653 | struct nlattr **attrs) | 1653 | struct nlattr **attrs) |
1654 | { | 1654 | { |
1655 | struct net *net = sock_net(skb->sk); | 1655 | struct net *net = sock_net(skb->sk); |
1656 | struct xfrm_state *x; | 1656 | struct xfrm_state *x; |
1657 | struct km_event c; | 1657 | struct km_event c; |
1658 | int err = - EINVAL; | 1658 | int err = - EINVAL; |
1659 | u32 mark = 0; | 1659 | u32 mark = 0; |
1660 | struct xfrm_mark m; | 1660 | struct xfrm_mark m; |
1661 | struct xfrm_aevent_id *p = nlmsg_data(nlh); | 1661 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
1662 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; | 1662 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
1663 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | 1663 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; |
1664 | 1664 | ||
1665 | if (!lt && !rp) | 1665 | if (!lt && !rp) |
1666 | return err; | 1666 | return err; |
1667 | 1667 | ||
1668 | /* pedantic mode - thou shalt sayeth replaceth */ | 1668 | /* pedantic mode - thou shalt sayeth replaceth */ |
1669 | if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) | 1669 | if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) |
1670 | return err; | 1670 | return err; |
1671 | 1671 | ||
1672 | mark = xfrm_mark_get(attrs, &m); | 1672 | mark = xfrm_mark_get(attrs, &m); |
1673 | 1673 | ||
1674 | x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); | 1674 | x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); |
1675 | if (x == NULL) | 1675 | if (x == NULL) |
1676 | return -ESRCH; | 1676 | return -ESRCH; |
1677 | 1677 | ||
1678 | if (x->km.state != XFRM_STATE_VALID) | 1678 | if (x->km.state != XFRM_STATE_VALID) |
1679 | goto out; | 1679 | goto out; |
1680 | 1680 | ||
1681 | spin_lock_bh(&x->lock); | 1681 | spin_lock_bh(&x->lock); |
1682 | xfrm_update_ae_params(x, attrs); | 1682 | xfrm_update_ae_params(x, attrs); |
1683 | spin_unlock_bh(&x->lock); | 1683 | spin_unlock_bh(&x->lock); |
1684 | 1684 | ||
1685 | c.event = nlh->nlmsg_type; | 1685 | c.event = nlh->nlmsg_type; |
1686 | c.seq = nlh->nlmsg_seq; | 1686 | c.seq = nlh->nlmsg_seq; |
1687 | c.pid = nlh->nlmsg_pid; | 1687 | c.pid = nlh->nlmsg_pid; |
1688 | c.data.aevent = XFRM_AE_CU; | 1688 | c.data.aevent = XFRM_AE_CU; |
1689 | km_state_notify(x, &c); | 1689 | km_state_notify(x, &c); |
1690 | err = 0; | 1690 | err = 0; |
1691 | out: | 1691 | out: |
1692 | xfrm_state_put(x); | 1692 | xfrm_state_put(x); |
1693 | return err; | 1693 | return err; |
1694 | } | 1694 | } |
1695 | 1695 | ||
1696 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, | 1696 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
1697 | struct nlattr **attrs) | 1697 | struct nlattr **attrs) |
1698 | { | 1698 | { |
1699 | struct net *net = sock_net(skb->sk); | 1699 | struct net *net = sock_net(skb->sk); |
1700 | struct km_event c; | 1700 | struct km_event c; |
1701 | u8 type = XFRM_POLICY_TYPE_MAIN; | 1701 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1702 | int err; | 1702 | int err; |
1703 | struct xfrm_audit audit_info; | 1703 | struct xfrm_audit audit_info; |
1704 | 1704 | ||
1705 | err = copy_from_user_policy_type(&type, attrs); | 1705 | err = copy_from_user_policy_type(&type, attrs); |
1706 | if (err) | 1706 | if (err) |
1707 | return err; | 1707 | return err; |
1708 | 1708 | ||
1709 | audit_info.loginuid = NETLINK_CB(skb).loginuid; | 1709 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1710 | audit_info.sessionid = NETLINK_CB(skb).sessionid; | 1710 | audit_info.sessionid = NETLINK_CB(skb).sessionid; |
1711 | audit_info.secid = NETLINK_CB(skb).sid; | 1711 | audit_info.secid = NETLINK_CB(skb).sid; |
1712 | err = xfrm_policy_flush(net, type, &audit_info); | 1712 | err = xfrm_policy_flush(net, type, &audit_info); |
1713 | if (err) { | 1713 | if (err) { |
1714 | if (err == -ESRCH) /* empty table */ | 1714 | if (err == -ESRCH) /* empty table */ |
1715 | return 0; | 1715 | return 0; |
1716 | return err; | 1716 | return err; |
1717 | } | 1717 | } |
1718 | 1718 | ||
1719 | c.data.type = type; | 1719 | c.data.type = type; |
1720 | c.event = nlh->nlmsg_type; | 1720 | c.event = nlh->nlmsg_type; |
1721 | c.seq = nlh->nlmsg_seq; | 1721 | c.seq = nlh->nlmsg_seq; |
1722 | c.pid = nlh->nlmsg_pid; | 1722 | c.pid = nlh->nlmsg_pid; |
1723 | c.net = net; | 1723 | c.net = net; |
1724 | km_policy_notify(NULL, 0, &c); | 1724 | km_policy_notify(NULL, 0, &c); |
1725 | return 0; | 1725 | return 0; |
1726 | } | 1726 | } |
1727 | 1727 | ||
1728 | static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, | 1728 | static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
1729 | struct nlattr **attrs) | 1729 | struct nlattr **attrs) |
1730 | { | 1730 | { |
1731 | struct net *net = sock_net(skb->sk); | 1731 | struct net *net = sock_net(skb->sk); |
1732 | struct xfrm_policy *xp; | 1732 | struct xfrm_policy *xp; |
1733 | struct xfrm_user_polexpire *up = nlmsg_data(nlh); | 1733 | struct xfrm_user_polexpire *up = nlmsg_data(nlh); |
1734 | struct xfrm_userpolicy_info *p = &up->pol; | 1734 | struct xfrm_userpolicy_info *p = &up->pol; |
1735 | u8 type = XFRM_POLICY_TYPE_MAIN; | 1735 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1736 | int err = -ENOENT; | 1736 | int err = -ENOENT; |
1737 | struct xfrm_mark m; | 1737 | struct xfrm_mark m; |
1738 | u32 mark = xfrm_mark_get(attrs, &m); | 1738 | u32 mark = xfrm_mark_get(attrs, &m); |
1739 | 1739 | ||
1740 | err = copy_from_user_policy_type(&type, attrs); | 1740 | err = copy_from_user_policy_type(&type, attrs); |
1741 | if (err) | 1741 | if (err) |
1742 | return err; | 1742 | return err; |
1743 | 1743 | ||
1744 | err = verify_policy_dir(p->dir); | 1744 | err = verify_policy_dir(p->dir); |
1745 | if (err) | 1745 | if (err) |
1746 | return err; | 1746 | return err; |
1747 | 1747 | ||
1748 | if (p->index) | 1748 | if (p->index) |
1749 | xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); | 1749 | xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); |
1750 | else { | 1750 | else { |
1751 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; | 1751 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
1752 | struct xfrm_sec_ctx *ctx; | 1752 | struct xfrm_sec_ctx *ctx; |
1753 | 1753 | ||
1754 | err = verify_sec_ctx_len(attrs); | 1754 | err = verify_sec_ctx_len(attrs); |
1755 | if (err) | 1755 | if (err) |
1756 | return err; | 1756 | return err; |
1757 | 1757 | ||
1758 | ctx = NULL; | 1758 | ctx = NULL; |
1759 | if (rt) { | 1759 | if (rt) { |
1760 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); | 1760 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
1761 | 1761 | ||
1762 | err = security_xfrm_policy_alloc(&ctx, uctx); | 1762 | err = security_xfrm_policy_alloc(&ctx, uctx); |
1763 | if (err) | 1763 | if (err) |
1764 | return err; | 1764 | return err; |
1765 | } | 1765 | } |
1766 | xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, | 1766 | xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, |
1767 | &p->sel, ctx, 0, &err); | 1767 | &p->sel, ctx, 0, &err); |
1768 | security_xfrm_policy_free(ctx); | 1768 | security_xfrm_policy_free(ctx); |
1769 | } | 1769 | } |
1770 | if (xp == NULL) | 1770 | if (xp == NULL) |
1771 | return -ENOENT; | 1771 | return -ENOENT; |
1772 | 1772 | ||
1773 | if (unlikely(xp->walk.dead)) | 1773 | if (unlikely(xp->walk.dead)) |
1774 | goto out; | 1774 | goto out; |
1775 | 1775 | ||
1776 | err = 0; | 1776 | err = 0; |
1777 | if (up->hard) { | 1777 | if (up->hard) { |
1778 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 1778 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
1779 | uid_t sessionid = NETLINK_CB(skb).sessionid; | 1779 | uid_t sessionid = NETLINK_CB(skb).sessionid; |
1780 | u32 sid = NETLINK_CB(skb).sid; | 1780 | u32 sid = NETLINK_CB(skb).sid; |
1781 | xfrm_policy_delete(xp, p->dir); | 1781 | xfrm_policy_delete(xp, p->dir); |
1782 | xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid); | 1782 | xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid); |
1783 | 1783 | ||
1784 | } else { | 1784 | } else { |
1785 | // reset the timers here? | 1785 | // reset the timers here? |
1786 | WARN(1, "Dont know what to do with soft policy expire\n"); | 1786 | WARN(1, "Dont know what to do with soft policy expire\n"); |
1787 | } | 1787 | } |
1788 | km_policy_expired(xp, p->dir, up->hard, current->pid); | 1788 | km_policy_expired(xp, p->dir, up->hard, current->pid); |
1789 | 1789 | ||
1790 | out: | 1790 | out: |
1791 | xfrm_pol_put(xp); | 1791 | xfrm_pol_put(xp); |
1792 | return err; | 1792 | return err; |
1793 | } | 1793 | } |
1794 | 1794 | ||
1795 | static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, | 1795 | static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
1796 | struct nlattr **attrs) | 1796 | struct nlattr **attrs) |
1797 | { | 1797 | { |
1798 | struct net *net = sock_net(skb->sk); | 1798 | struct net *net = sock_net(skb->sk); |
1799 | struct xfrm_state *x; | 1799 | struct xfrm_state *x; |
1800 | int err; | 1800 | int err; |
1801 | struct xfrm_user_expire *ue = nlmsg_data(nlh); | 1801 | struct xfrm_user_expire *ue = nlmsg_data(nlh); |
1802 | struct xfrm_usersa_info *p = &ue->state; | 1802 | struct xfrm_usersa_info *p = &ue->state; |
1803 | struct xfrm_mark m; | 1803 | struct xfrm_mark m; |
1804 | u32 mark = xfrm_mark_get(attrs, &m);; | 1804 | u32 mark = xfrm_mark_get(attrs, &m); |
1805 | 1805 | ||
1806 | x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); | 1806 | x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); |
1807 | 1807 | ||
1808 | err = -ENOENT; | 1808 | err = -ENOENT; |
1809 | if (x == NULL) | 1809 | if (x == NULL) |
1810 | return err; | 1810 | return err; |
1811 | 1811 | ||
1812 | spin_lock_bh(&x->lock); | 1812 | spin_lock_bh(&x->lock); |
1813 | err = -EINVAL; | 1813 | err = -EINVAL; |
1814 | if (x->km.state != XFRM_STATE_VALID) | 1814 | if (x->km.state != XFRM_STATE_VALID) |
1815 | goto out; | 1815 | goto out; |
1816 | km_state_expired(x, ue->hard, current->pid); | 1816 | km_state_expired(x, ue->hard, current->pid); |
1817 | 1817 | ||
1818 | if (ue->hard) { | 1818 | if (ue->hard) { |
1819 | uid_t loginuid = NETLINK_CB(skb).loginuid; | 1819 | uid_t loginuid = NETLINK_CB(skb).loginuid; |
1820 | uid_t sessionid = NETLINK_CB(skb).sessionid; | 1820 | uid_t sessionid = NETLINK_CB(skb).sessionid; |
1821 | u32 sid = NETLINK_CB(skb).sid; | 1821 | u32 sid = NETLINK_CB(skb).sid; |
1822 | __xfrm_state_delete(x); | 1822 | __xfrm_state_delete(x); |
1823 | xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid); | 1823 | xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid); |
1824 | } | 1824 | } |
1825 | err = 0; | 1825 | err = 0; |
1826 | out: | 1826 | out: |
1827 | spin_unlock_bh(&x->lock); | 1827 | spin_unlock_bh(&x->lock); |
1828 | xfrm_state_put(x); | 1828 | xfrm_state_put(x); |
1829 | return err; | 1829 | return err; |
1830 | } | 1830 | } |
1831 | 1831 | ||
1832 | static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, | 1832 | static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, |
1833 | struct nlattr **attrs) | 1833 | struct nlattr **attrs) |
1834 | { | 1834 | { |
1835 | struct net *net = sock_net(skb->sk); | 1835 | struct net *net = sock_net(skb->sk); |
1836 | struct xfrm_policy *xp; | 1836 | struct xfrm_policy *xp; |
1837 | struct xfrm_user_tmpl *ut; | 1837 | struct xfrm_user_tmpl *ut; |
1838 | int i; | 1838 | int i; |
1839 | struct nlattr *rt = attrs[XFRMA_TMPL]; | 1839 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
1840 | struct xfrm_mark mark; | 1840 | struct xfrm_mark mark; |
1841 | 1841 | ||
1842 | struct xfrm_user_acquire *ua = nlmsg_data(nlh); | 1842 | struct xfrm_user_acquire *ua = nlmsg_data(nlh); |
1843 | struct xfrm_state *x = xfrm_state_alloc(net); | 1843 | struct xfrm_state *x = xfrm_state_alloc(net); |
1844 | int err = -ENOMEM; | 1844 | int err = -ENOMEM; |
1845 | 1845 | ||
1846 | if (!x) | 1846 | if (!x) |
1847 | goto nomem; | 1847 | goto nomem; |
1848 | 1848 | ||
1849 | xfrm_mark_get(attrs, &mark); | 1849 | xfrm_mark_get(attrs, &mark); |
1850 | 1850 | ||
1851 | err = verify_newpolicy_info(&ua->policy); | 1851 | err = verify_newpolicy_info(&ua->policy); |
1852 | if (err) | 1852 | if (err) |
1853 | goto bad_policy; | 1853 | goto bad_policy; |
1854 | 1854 | ||
1855 | /* build an XP */ | 1855 | /* build an XP */ |
1856 | xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); | 1856 | xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); |
1857 | if (!xp) | 1857 | if (!xp) |
1858 | goto free_state; | 1858 | goto free_state; |
1859 | 1859 | ||
1860 | memcpy(&x->id, &ua->id, sizeof(ua->id)); | 1860 | memcpy(&x->id, &ua->id, sizeof(ua->id)); |
1861 | memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); | 1861 | memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); |
1862 | memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); | 1862 | memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); |
1863 | xp->mark.m = x->mark.m = mark.m; | 1863 | xp->mark.m = x->mark.m = mark.m; |
1864 | xp->mark.v = x->mark.v = mark.v; | 1864 | xp->mark.v = x->mark.v = mark.v; |
1865 | ut = nla_data(rt); | 1865 | ut = nla_data(rt); |
1866 | /* extract the templates and for each call km_key */ | 1866 | /* extract the templates and for each call km_key */ |
1867 | for (i = 0; i < xp->xfrm_nr; i++, ut++) { | 1867 | for (i = 0; i < xp->xfrm_nr; i++, ut++) { |
1868 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | 1868 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; |
1869 | memcpy(&x->id, &t->id, sizeof(x->id)); | 1869 | memcpy(&x->id, &t->id, sizeof(x->id)); |
1870 | x->props.mode = t->mode; | 1870 | x->props.mode = t->mode; |
1871 | x->props.reqid = t->reqid; | 1871 | x->props.reqid = t->reqid; |
1872 | x->props.family = ut->family; | 1872 | x->props.family = ut->family; |
1873 | t->aalgos = ua->aalgos; | 1873 | t->aalgos = ua->aalgos; |
1874 | t->ealgos = ua->ealgos; | 1874 | t->ealgos = ua->ealgos; |
1875 | t->calgos = ua->calgos; | 1875 | t->calgos = ua->calgos; |
1876 | err = km_query(x, t, xp); | 1876 | err = km_query(x, t, xp); |
1877 | 1877 | ||
1878 | } | 1878 | } |
1879 | 1879 | ||
1880 | kfree(x); | 1880 | kfree(x); |
1881 | kfree(xp); | 1881 | kfree(xp); |
1882 | 1882 | ||
1883 | return 0; | 1883 | return 0; |
1884 | 1884 | ||
1885 | bad_policy: | 1885 | bad_policy: |
1886 | WARN(1, "BAD policy passed\n"); | 1886 | WARN(1, "BAD policy passed\n"); |
1887 | free_state: | 1887 | free_state: |
1888 | kfree(x); | 1888 | kfree(x); |
1889 | nomem: | 1889 | nomem: |
1890 | return err; | 1890 | return err; |
1891 | } | 1891 | } |
1892 | 1892 | ||
1893 | #ifdef CONFIG_XFRM_MIGRATE | 1893 | #ifdef CONFIG_XFRM_MIGRATE |
1894 | static int copy_from_user_migrate(struct xfrm_migrate *ma, | 1894 | static int copy_from_user_migrate(struct xfrm_migrate *ma, |
1895 | struct xfrm_kmaddress *k, | 1895 | struct xfrm_kmaddress *k, |
1896 | struct nlattr **attrs, int *num) | 1896 | struct nlattr **attrs, int *num) |
1897 | { | 1897 | { |
1898 | struct nlattr *rt = attrs[XFRMA_MIGRATE]; | 1898 | struct nlattr *rt = attrs[XFRMA_MIGRATE]; |
1899 | struct xfrm_user_migrate *um; | 1899 | struct xfrm_user_migrate *um; |
1900 | int i, num_migrate; | 1900 | int i, num_migrate; |
1901 | 1901 | ||
1902 | if (k != NULL) { | 1902 | if (k != NULL) { |
1903 | struct xfrm_user_kmaddress *uk; | 1903 | struct xfrm_user_kmaddress *uk; |
1904 | 1904 | ||
1905 | uk = nla_data(attrs[XFRMA_KMADDRESS]); | 1905 | uk = nla_data(attrs[XFRMA_KMADDRESS]); |
1906 | memcpy(&k->local, &uk->local, sizeof(k->local)); | 1906 | memcpy(&k->local, &uk->local, sizeof(k->local)); |
1907 | memcpy(&k->remote, &uk->remote, sizeof(k->remote)); | 1907 | memcpy(&k->remote, &uk->remote, sizeof(k->remote)); |
1908 | k->family = uk->family; | 1908 | k->family = uk->family; |
1909 | k->reserved = uk->reserved; | 1909 | k->reserved = uk->reserved; |
1910 | } | 1910 | } |
1911 | 1911 | ||
1912 | um = nla_data(rt); | 1912 | um = nla_data(rt); |
1913 | num_migrate = nla_len(rt) / sizeof(*um); | 1913 | num_migrate = nla_len(rt) / sizeof(*um); |
1914 | 1914 | ||
1915 | if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) | 1915 | if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) |
1916 | return -EINVAL; | 1916 | return -EINVAL; |
1917 | 1917 | ||
1918 | for (i = 0; i < num_migrate; i++, um++, ma++) { | 1918 | for (i = 0; i < num_migrate; i++, um++, ma++) { |
1919 | memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); | 1919 | memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); |
1920 | memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); | 1920 | memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); |
1921 | memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); | 1921 | memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); |
1922 | memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); | 1922 | memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); |
1923 | 1923 | ||
1924 | ma->proto = um->proto; | 1924 | ma->proto = um->proto; |
1925 | ma->mode = um->mode; | 1925 | ma->mode = um->mode; |
1926 | ma->reqid = um->reqid; | 1926 | ma->reqid = um->reqid; |
1927 | 1927 | ||
1928 | ma->old_family = um->old_family; | 1928 | ma->old_family = um->old_family; |
1929 | ma->new_family = um->new_family; | 1929 | ma->new_family = um->new_family; |
1930 | } | 1930 | } |
1931 | 1931 | ||
1932 | *num = i; | 1932 | *num = i; |
1933 | return 0; | 1933 | return 0; |
1934 | } | 1934 | } |
1935 | 1935 | ||
1936 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | 1936 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, |
1937 | struct nlattr **attrs) | 1937 | struct nlattr **attrs) |
1938 | { | 1938 | { |
1939 | struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); | 1939 | struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); |
1940 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; | 1940 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; |
1941 | struct xfrm_kmaddress km, *kmp; | 1941 | struct xfrm_kmaddress km, *kmp; |
1942 | u8 type; | 1942 | u8 type; |
1943 | int err; | 1943 | int err; |
1944 | int n = 0; | 1944 | int n = 0; |
1945 | 1945 | ||
1946 | if (attrs[XFRMA_MIGRATE] == NULL) | 1946 | if (attrs[XFRMA_MIGRATE] == NULL) |
1947 | return -EINVAL; | 1947 | return -EINVAL; |
1948 | 1948 | ||
1949 | kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; | 1949 | kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; |
1950 | 1950 | ||
1951 | err = copy_from_user_policy_type(&type, attrs); | 1951 | err = copy_from_user_policy_type(&type, attrs); |
1952 | if (err) | 1952 | if (err) |
1953 | return err; | 1953 | return err; |
1954 | 1954 | ||
1955 | err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); | 1955 | err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); |
1956 | if (err) | 1956 | if (err) |
1957 | return err; | 1957 | return err; |
1958 | 1958 | ||
1959 | if (!n) | 1959 | if (!n) |
1960 | return 0; | 1960 | return 0; |
1961 | 1961 | ||
1962 | xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp); | 1962 | xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp); |
1963 | 1963 | ||
1964 | return 0; | 1964 | return 0; |
1965 | } | 1965 | } |
1966 | #else | 1966 | #else |
1967 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | 1967 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, |
1968 | struct nlattr **attrs) | 1968 | struct nlattr **attrs) |
1969 | { | 1969 | { |
1970 | return -ENOPROTOOPT; | 1970 | return -ENOPROTOOPT; |
1971 | } | 1971 | } |
1972 | #endif | 1972 | #endif |
1973 | 1973 | ||
1974 | #ifdef CONFIG_XFRM_MIGRATE | 1974 | #ifdef CONFIG_XFRM_MIGRATE |
1975 | static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb) | 1975 | static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb) |
1976 | { | 1976 | { |
1977 | struct xfrm_user_migrate um; | 1977 | struct xfrm_user_migrate um; |
1978 | 1978 | ||
1979 | memset(&um, 0, sizeof(um)); | 1979 | memset(&um, 0, sizeof(um)); |
1980 | um.proto = m->proto; | 1980 | um.proto = m->proto; |
1981 | um.mode = m->mode; | 1981 | um.mode = m->mode; |
1982 | um.reqid = m->reqid; | 1982 | um.reqid = m->reqid; |
1983 | um.old_family = m->old_family; | 1983 | um.old_family = m->old_family; |
1984 | memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); | 1984 | memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); |
1985 | memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); | 1985 | memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); |
1986 | um.new_family = m->new_family; | 1986 | um.new_family = m->new_family; |
1987 | memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); | 1987 | memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); |
1988 | memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); | 1988 | memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); |
1989 | 1989 | ||
1990 | return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); | 1990 | return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); |
1991 | } | 1991 | } |
1992 | 1992 | ||
1993 | static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb) | 1993 | static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb) |
1994 | { | 1994 | { |
1995 | struct xfrm_user_kmaddress uk; | 1995 | struct xfrm_user_kmaddress uk; |
1996 | 1996 | ||
1997 | memset(&uk, 0, sizeof(uk)); | 1997 | memset(&uk, 0, sizeof(uk)); |
1998 | uk.family = k->family; | 1998 | uk.family = k->family; |
1999 | uk.reserved = k->reserved; | 1999 | uk.reserved = k->reserved; |
2000 | memcpy(&uk.local, &k->local, sizeof(uk.local)); | 2000 | memcpy(&uk.local, &k->local, sizeof(uk.local)); |
2001 | memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); | 2001 | memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); |
2002 | 2002 | ||
2003 | return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); | 2003 | return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); |
2004 | } | 2004 | } |
2005 | 2005 | ||
2006 | static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) | 2006 | static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) |
2007 | { | 2007 | { |
2008 | return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) | 2008 | return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) |
2009 | + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) | 2009 | + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) |
2010 | + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) | 2010 | + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) |
2011 | + userpolicy_type_attrsize(); | 2011 | + userpolicy_type_attrsize(); |
2012 | } | 2012 | } |
2013 | 2013 | ||
2014 | static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m, | 2014 | static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m, |
2015 | int num_migrate, struct xfrm_kmaddress *k, | 2015 | int num_migrate, struct xfrm_kmaddress *k, |
2016 | struct xfrm_selector *sel, u8 dir, u8 type) | 2016 | struct xfrm_selector *sel, u8 dir, u8 type) |
2017 | { | 2017 | { |
2018 | struct xfrm_migrate *mp; | 2018 | struct xfrm_migrate *mp; |
2019 | struct xfrm_userpolicy_id *pol_id; | 2019 | struct xfrm_userpolicy_id *pol_id; |
2020 | struct nlmsghdr *nlh; | 2020 | struct nlmsghdr *nlh; |
2021 | int i; | 2021 | int i; |
2022 | 2022 | ||
2023 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); | 2023 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); |
2024 | if (nlh == NULL) | 2024 | if (nlh == NULL) |
2025 | return -EMSGSIZE; | 2025 | return -EMSGSIZE; |
2026 | 2026 | ||
2027 | pol_id = nlmsg_data(nlh); | 2027 | pol_id = nlmsg_data(nlh); |
2028 | /* copy data from selector, dir, and type to the pol_id */ | 2028 | /* copy data from selector, dir, and type to the pol_id */ |
2029 | memset(pol_id, 0, sizeof(*pol_id)); | 2029 | memset(pol_id, 0, sizeof(*pol_id)); |
2030 | memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); | 2030 | memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); |
2031 | pol_id->dir = dir; | 2031 | pol_id->dir = dir; |
2032 | 2032 | ||
2033 | if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0)) | 2033 | if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0)) |
2034 | goto nlmsg_failure; | 2034 | goto nlmsg_failure; |
2035 | 2035 | ||
2036 | if (copy_to_user_policy_type(type, skb) < 0) | 2036 | if (copy_to_user_policy_type(type, skb) < 0) |
2037 | goto nlmsg_failure; | 2037 | goto nlmsg_failure; |
2038 | 2038 | ||
2039 | for (i = 0, mp = m ; i < num_migrate; i++, mp++) { | 2039 | for (i = 0, mp = m ; i < num_migrate; i++, mp++) { |
2040 | if (copy_to_user_migrate(mp, skb) < 0) | 2040 | if (copy_to_user_migrate(mp, skb) < 0) |
2041 | goto nlmsg_failure; | 2041 | goto nlmsg_failure; |
2042 | } | 2042 | } |
2043 | 2043 | ||
2044 | return nlmsg_end(skb, nlh); | 2044 | return nlmsg_end(skb, nlh); |
2045 | nlmsg_failure: | 2045 | nlmsg_failure: |
2046 | nlmsg_cancel(skb, nlh); | 2046 | nlmsg_cancel(skb, nlh); |
2047 | return -EMSGSIZE; | 2047 | return -EMSGSIZE; |
2048 | } | 2048 | } |
2049 | 2049 | ||
2050 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | 2050 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, |
2051 | struct xfrm_migrate *m, int num_migrate, | 2051 | struct xfrm_migrate *m, int num_migrate, |
2052 | struct xfrm_kmaddress *k) | 2052 | struct xfrm_kmaddress *k) |
2053 | { | 2053 | { |
2054 | struct net *net = &init_net; | 2054 | struct net *net = &init_net; |
2055 | struct sk_buff *skb; | 2055 | struct sk_buff *skb; |
2056 | 2056 | ||
2057 | skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); | 2057 | skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); |
2058 | if (skb == NULL) | 2058 | if (skb == NULL) |
2059 | return -ENOMEM; | 2059 | return -ENOMEM; |
2060 | 2060 | ||
2061 | /* build migrate */ | 2061 | /* build migrate */ |
2062 | if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) | 2062 | if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) |
2063 | BUG(); | 2063 | BUG(); |
2064 | 2064 | ||
2065 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); | 2065 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); |
2066 | } | 2066 | } |
2067 | #else | 2067 | #else |
2068 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | 2068 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, |
2069 | struct xfrm_migrate *m, int num_migrate, | 2069 | struct xfrm_migrate *m, int num_migrate, |
2070 | struct xfrm_kmaddress *k) | 2070 | struct xfrm_kmaddress *k) |
2071 | { | 2071 | { |
2072 | return -ENOPROTOOPT; | 2072 | return -ENOPROTOOPT; |
2073 | } | 2073 | } |
2074 | #endif | 2074 | #endif |
2075 | 2075 | ||
2076 | #define XMSGSIZE(type) sizeof(struct type) | 2076 | #define XMSGSIZE(type) sizeof(struct type) |
2077 | 2077 | ||
2078 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { | 2078 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { |
2079 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | 2079 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), |
2080 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | 2080 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
2081 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | 2081 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), |
2082 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), | 2082 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
2083 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | 2083 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
2084 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | 2084 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
2085 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), | 2085 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), |
2086 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), | 2086 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), |
2087 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), | 2087 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), |
2088 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), | 2088 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
2089 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | 2089 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), |
2090 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), | 2090 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), |
2091 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), | 2091 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
2092 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, | 2092 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, |
2093 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), | 2093 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
2094 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), | 2094 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
2095 | [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), | 2095 | [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), |
2096 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | 2096 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
2097 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), | 2097 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), |
2098 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), | 2098 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), |
2099 | }; | 2099 | }; |
2100 | 2100 | ||
2101 | #undef XMSGSIZE | 2101 | #undef XMSGSIZE |
2102 | 2102 | ||
2103 | static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { | 2103 | static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { |
2104 | [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, | 2104 | [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, |
2105 | [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, | 2105 | [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, |
2106 | [XFRMA_LASTUSED] = { .type = NLA_U64}, | 2106 | [XFRMA_LASTUSED] = { .type = NLA_U64}, |
2107 | [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, | 2107 | [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, |
2108 | [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, | 2108 | [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, |
2109 | [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, | 2109 | [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, |
2110 | [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, | 2110 | [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, |
2111 | [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, | 2111 | [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, |
2112 | [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, | 2112 | [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, |
2113 | [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, | 2113 | [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, |
2114 | [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, | 2114 | [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, |
2115 | [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, | 2115 | [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, |
2116 | [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, | 2116 | [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, |
2117 | [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, | 2117 | [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, |
2118 | [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, | 2118 | [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, |
2119 | [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, | 2119 | [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, |
2120 | [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, | 2120 | [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, |
2121 | [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, | 2121 | [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, |
2122 | [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, | 2122 | [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, |
2123 | [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, | 2123 | [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, |
2124 | [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, | 2124 | [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, |
2125 | }; | 2125 | }; |
2126 | 2126 | ||
2127 | static struct xfrm_link { | 2127 | static struct xfrm_link { |
2128 | int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); | 2128 | int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); |
2129 | int (*dump)(struct sk_buff *, struct netlink_callback *); | 2129 | int (*dump)(struct sk_buff *, struct netlink_callback *); |
2130 | int (*done)(struct netlink_callback *); | 2130 | int (*done)(struct netlink_callback *); |
2131 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { | 2131 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { |
2132 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | 2132 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, |
2133 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, | 2133 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, |
2134 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, | 2134 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, |
2135 | .dump = xfrm_dump_sa, | 2135 | .dump = xfrm_dump_sa, |
2136 | .done = xfrm_dump_sa_done }, | 2136 | .done = xfrm_dump_sa_done }, |
2137 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, | 2137 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
2138 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, | 2138 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, |
2139 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, | 2139 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, |
2140 | .dump = xfrm_dump_policy, | 2140 | .dump = xfrm_dump_policy, |
2141 | .done = xfrm_dump_policy_done }, | 2141 | .done = xfrm_dump_policy_done }, |
2142 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, | 2142 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, |
2143 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, | 2143 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, |
2144 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, | 2144 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, |
2145 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, | 2145 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
2146 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | 2146 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, |
2147 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, | 2147 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, |
2148 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, | 2148 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, |
2149 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, | 2149 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, |
2150 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, | 2150 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, |
2151 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, | 2151 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, |
2152 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, | 2152 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, |
2153 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, | 2153 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, |
2154 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, | 2154 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, |
2155 | }; | 2155 | }; |
2156 | 2156 | ||
2157 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | 2157 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
2158 | { | 2158 | { |
2159 | struct net *net = sock_net(skb->sk); | 2159 | struct net *net = sock_net(skb->sk); |
2160 | struct nlattr *attrs[XFRMA_MAX+1]; | 2160 | struct nlattr *attrs[XFRMA_MAX+1]; |
2161 | struct xfrm_link *link; | 2161 | struct xfrm_link *link; |
2162 | int type, err; | 2162 | int type, err; |
2163 | 2163 | ||
2164 | type = nlh->nlmsg_type; | 2164 | type = nlh->nlmsg_type; |
2165 | if (type > XFRM_MSG_MAX) | 2165 | if (type > XFRM_MSG_MAX) |
2166 | return -EINVAL; | 2166 | return -EINVAL; |
2167 | 2167 | ||
2168 | type -= XFRM_MSG_BASE; | 2168 | type -= XFRM_MSG_BASE; |
2169 | link = &xfrm_dispatch[type]; | 2169 | link = &xfrm_dispatch[type]; |
2170 | 2170 | ||
2171 | /* All operations require privileges, even GET */ | 2171 | /* All operations require privileges, even GET */ |
2172 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) | 2172 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) |
2173 | return -EPERM; | 2173 | return -EPERM; |
2174 | 2174 | ||
2175 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || | 2175 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || |
2176 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && | 2176 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && |
2177 | (nlh->nlmsg_flags & NLM_F_DUMP)) { | 2177 | (nlh->nlmsg_flags & NLM_F_DUMP)) { |
2178 | if (link->dump == NULL) | 2178 | if (link->dump == NULL) |
2179 | return -EINVAL; | 2179 | return -EINVAL; |
2180 | 2180 | ||
2181 | return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done); | 2181 | return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done); |
2182 | } | 2182 | } |
2183 | 2183 | ||
2184 | err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, | 2184 | err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, |
2185 | xfrma_policy); | 2185 | xfrma_policy); |
2186 | if (err < 0) | 2186 | if (err < 0) |
2187 | return err; | 2187 | return err; |
2188 | 2188 | ||
2189 | if (link->doit == NULL) | 2189 | if (link->doit == NULL) |
2190 | return -EINVAL; | 2190 | return -EINVAL; |
2191 | 2191 | ||
2192 | return link->doit(skb, nlh, attrs); | 2192 | return link->doit(skb, nlh, attrs); |
2193 | } | 2193 | } |
2194 | 2194 | ||
2195 | static void xfrm_netlink_rcv(struct sk_buff *skb) | 2195 | static void xfrm_netlink_rcv(struct sk_buff *skb) |
2196 | { | 2196 | { |
2197 | mutex_lock(&xfrm_cfg_mutex); | 2197 | mutex_lock(&xfrm_cfg_mutex); |
2198 | netlink_rcv_skb(skb, &xfrm_user_rcv_msg); | 2198 | netlink_rcv_skb(skb, &xfrm_user_rcv_msg); |
2199 | mutex_unlock(&xfrm_cfg_mutex); | 2199 | mutex_unlock(&xfrm_cfg_mutex); |
2200 | } | 2200 | } |
2201 | 2201 | ||
2202 | static inline size_t xfrm_expire_msgsize(void) | 2202 | static inline size_t xfrm_expire_msgsize(void) |
2203 | { | 2203 | { |
2204 | return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) | 2204 | return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) |
2205 | + nla_total_size(sizeof(struct xfrm_mark)); | 2205 | + nla_total_size(sizeof(struct xfrm_mark)); |
2206 | } | 2206 | } |
2207 | 2207 | ||
2208 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) | 2208 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) |
2209 | { | 2209 | { |
2210 | struct xfrm_user_expire *ue; | 2210 | struct xfrm_user_expire *ue; |
2211 | struct nlmsghdr *nlh; | 2211 | struct nlmsghdr *nlh; |
2212 | 2212 | ||
2213 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); | 2213 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); |
2214 | if (nlh == NULL) | 2214 | if (nlh == NULL) |
2215 | return -EMSGSIZE; | 2215 | return -EMSGSIZE; |
2216 | 2216 | ||
2217 | ue = nlmsg_data(nlh); | 2217 | ue = nlmsg_data(nlh); |
2218 | copy_to_user_state(x, &ue->state); | 2218 | copy_to_user_state(x, &ue->state); |
2219 | ue->hard = (c->data.hard != 0) ? 1 : 0; | 2219 | ue->hard = (c->data.hard != 0) ? 1 : 0; |
2220 | 2220 | ||
2221 | if (xfrm_mark_put(skb, &x->mark)) | 2221 | if (xfrm_mark_put(skb, &x->mark)) |
2222 | goto nla_put_failure; | 2222 | goto nla_put_failure; |
2223 | 2223 | ||
2224 | return nlmsg_end(skb, nlh); | 2224 | return nlmsg_end(skb, nlh); |
2225 | 2225 | ||
2226 | nla_put_failure: | 2226 | nla_put_failure: |
2227 | return -EMSGSIZE; | 2227 | return -EMSGSIZE; |
2228 | } | 2228 | } |
2229 | 2229 | ||
2230 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) | 2230 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) |
2231 | { | 2231 | { |
2232 | struct net *net = xs_net(x); | 2232 | struct net *net = xs_net(x); |
2233 | struct sk_buff *skb; | 2233 | struct sk_buff *skb; |
2234 | 2234 | ||
2235 | skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); | 2235 | skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); |
2236 | if (skb == NULL) | 2236 | if (skb == NULL) |
2237 | return -ENOMEM; | 2237 | return -ENOMEM; |
2238 | 2238 | ||
2239 | if (build_expire(skb, x, c) < 0) { | 2239 | if (build_expire(skb, x, c) < 0) { |
2240 | kfree_skb(skb); | 2240 | kfree_skb(skb); |
2241 | return -EMSGSIZE; | 2241 | return -EMSGSIZE; |
2242 | } | 2242 | } |
2243 | 2243 | ||
2244 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); | 2244 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
2245 | } | 2245 | } |
2246 | 2246 | ||
2247 | static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) | 2247 | static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) |
2248 | { | 2248 | { |
2249 | struct net *net = xs_net(x); | 2249 | struct net *net = xs_net(x); |
2250 | struct sk_buff *skb; | 2250 | struct sk_buff *skb; |
2251 | 2251 | ||
2252 | skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); | 2252 | skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
2253 | if (skb == NULL) | 2253 | if (skb == NULL) |
2254 | return -ENOMEM; | 2254 | return -ENOMEM; |
2255 | 2255 | ||
2256 | if (build_aevent(skb, x, c) < 0) | 2256 | if (build_aevent(skb, x, c) < 0) |
2257 | BUG(); | 2257 | BUG(); |
2258 | 2258 | ||
2259 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); | 2259 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); |
2260 | } | 2260 | } |
2261 | 2261 | ||
2262 | static int xfrm_notify_sa_flush(struct km_event *c) | 2262 | static int xfrm_notify_sa_flush(struct km_event *c) |
2263 | { | 2263 | { |
2264 | struct net *net = c->net; | 2264 | struct net *net = c->net; |
2265 | struct xfrm_usersa_flush *p; | 2265 | struct xfrm_usersa_flush *p; |
2266 | struct nlmsghdr *nlh; | 2266 | struct nlmsghdr *nlh; |
2267 | struct sk_buff *skb; | 2267 | struct sk_buff *skb; |
2268 | int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); | 2268 | int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); |
2269 | 2269 | ||
2270 | skb = nlmsg_new(len, GFP_ATOMIC); | 2270 | skb = nlmsg_new(len, GFP_ATOMIC); |
2271 | if (skb == NULL) | 2271 | if (skb == NULL) |
2272 | return -ENOMEM; | 2272 | return -ENOMEM; |
2273 | 2273 | ||
2274 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); | 2274 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); |
2275 | if (nlh == NULL) { | 2275 | if (nlh == NULL) { |
2276 | kfree_skb(skb); | 2276 | kfree_skb(skb); |
2277 | return -EMSGSIZE; | 2277 | return -EMSGSIZE; |
2278 | } | 2278 | } |
2279 | 2279 | ||
2280 | p = nlmsg_data(nlh); | 2280 | p = nlmsg_data(nlh); |
2281 | p->proto = c->data.proto; | 2281 | p->proto = c->data.proto; |
2282 | 2282 | ||
2283 | nlmsg_end(skb, nlh); | 2283 | nlmsg_end(skb, nlh); |
2284 | 2284 | ||
2285 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); | 2285 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
2286 | } | 2286 | } |
2287 | 2287 | ||
2288 | static inline size_t xfrm_sa_len(struct xfrm_state *x) | 2288 | static inline size_t xfrm_sa_len(struct xfrm_state *x) |
2289 | { | 2289 | { |
2290 | size_t l = 0; | 2290 | size_t l = 0; |
2291 | if (x->aead) | 2291 | if (x->aead) |
2292 | l += nla_total_size(aead_len(x->aead)); | 2292 | l += nla_total_size(aead_len(x->aead)); |
2293 | if (x->aalg) { | 2293 | if (x->aalg) { |
2294 | l += nla_total_size(sizeof(struct xfrm_algo) + | 2294 | l += nla_total_size(sizeof(struct xfrm_algo) + |
2295 | (x->aalg->alg_key_len + 7) / 8); | 2295 | (x->aalg->alg_key_len + 7) / 8); |
2296 | l += nla_total_size(xfrm_alg_auth_len(x->aalg)); | 2296 | l += nla_total_size(xfrm_alg_auth_len(x->aalg)); |
2297 | } | 2297 | } |
2298 | if (x->ealg) | 2298 | if (x->ealg) |
2299 | l += nla_total_size(xfrm_alg_len(x->ealg)); | 2299 | l += nla_total_size(xfrm_alg_len(x->ealg)); |
2300 | if (x->calg) | 2300 | if (x->calg) |
2301 | l += nla_total_size(sizeof(*x->calg)); | 2301 | l += nla_total_size(sizeof(*x->calg)); |
2302 | if (x->encap) | 2302 | if (x->encap) |
2303 | l += nla_total_size(sizeof(*x->encap)); | 2303 | l += nla_total_size(sizeof(*x->encap)); |
2304 | if (x->security) | 2304 | if (x->security) |
2305 | l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + | 2305 | l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + |
2306 | x->security->ctx_len); | 2306 | x->security->ctx_len); |
2307 | if (x->coaddr) | 2307 | if (x->coaddr) |
2308 | l += nla_total_size(sizeof(*x->coaddr)); | 2308 | l += nla_total_size(sizeof(*x->coaddr)); |
2309 | 2309 | ||
2310 | /* Must count x->lastused as it may become non-zero behind our back. */ | 2310 | /* Must count x->lastused as it may become non-zero behind our back. */ |
2311 | l += nla_total_size(sizeof(u64)); | 2311 | l += nla_total_size(sizeof(u64)); |
2312 | 2312 | ||
2313 | return l; | 2313 | return l; |
2314 | } | 2314 | } |
2315 | 2315 | ||
2316 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) | 2316 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) |
2317 | { | 2317 | { |
2318 | struct net *net = xs_net(x); | 2318 | struct net *net = xs_net(x); |
2319 | struct xfrm_usersa_info *p; | 2319 | struct xfrm_usersa_info *p; |
2320 | struct xfrm_usersa_id *id; | 2320 | struct xfrm_usersa_id *id; |
2321 | struct nlmsghdr *nlh; | 2321 | struct nlmsghdr *nlh; |
2322 | struct sk_buff *skb; | 2322 | struct sk_buff *skb; |
2323 | int len = xfrm_sa_len(x); | 2323 | int len = xfrm_sa_len(x); |
2324 | int headlen; | 2324 | int headlen; |
2325 | 2325 | ||
2326 | headlen = sizeof(*p); | 2326 | headlen = sizeof(*p); |
2327 | if (c->event == XFRM_MSG_DELSA) { | 2327 | if (c->event == XFRM_MSG_DELSA) { |
2328 | len += nla_total_size(headlen); | 2328 | len += nla_total_size(headlen); |
2329 | headlen = sizeof(*id); | 2329 | headlen = sizeof(*id); |
2330 | len += nla_total_size(sizeof(struct xfrm_mark)); | 2330 | len += nla_total_size(sizeof(struct xfrm_mark)); |
2331 | } | 2331 | } |
2332 | len += NLMSG_ALIGN(headlen); | 2332 | len += NLMSG_ALIGN(headlen); |
2333 | 2333 | ||
2334 | skb = nlmsg_new(len, GFP_ATOMIC); | 2334 | skb = nlmsg_new(len, GFP_ATOMIC); |
2335 | if (skb == NULL) | 2335 | if (skb == NULL) |
2336 | return -ENOMEM; | 2336 | return -ENOMEM; |
2337 | 2337 | ||
2338 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); | 2338 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2339 | if (nlh == NULL) | 2339 | if (nlh == NULL) |
2340 | goto nla_put_failure; | 2340 | goto nla_put_failure; |
2341 | 2341 | ||
2342 | p = nlmsg_data(nlh); | 2342 | p = nlmsg_data(nlh); |
2343 | if (c->event == XFRM_MSG_DELSA) { | 2343 | if (c->event == XFRM_MSG_DELSA) { |
2344 | struct nlattr *attr; | 2344 | struct nlattr *attr; |
2345 | 2345 | ||
2346 | id = nlmsg_data(nlh); | 2346 | id = nlmsg_data(nlh); |
2347 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); | 2347 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); |
2348 | id->spi = x->id.spi; | 2348 | id->spi = x->id.spi; |
2349 | id->family = x->props.family; | 2349 | id->family = x->props.family; |
2350 | id->proto = x->id.proto; | 2350 | id->proto = x->id.proto; |
2351 | 2351 | ||
2352 | attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); | 2352 | attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); |
2353 | if (attr == NULL) | 2353 | if (attr == NULL) |
2354 | goto nla_put_failure; | 2354 | goto nla_put_failure; |
2355 | 2355 | ||
2356 | p = nla_data(attr); | 2356 | p = nla_data(attr); |
2357 | } | 2357 | } |
2358 | 2358 | ||
2359 | if (copy_to_user_state_extra(x, p, skb)) | 2359 | if (copy_to_user_state_extra(x, p, skb)) |
2360 | goto nla_put_failure; | 2360 | goto nla_put_failure; |
2361 | 2361 | ||
2362 | nlmsg_end(skb, nlh); | 2362 | nlmsg_end(skb, nlh); |
2363 | 2363 | ||
2364 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); | 2364 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
2365 | 2365 | ||
2366 | nla_put_failure: | 2366 | nla_put_failure: |
2367 | /* Somebody screwed up with xfrm_sa_len! */ | 2367 | /* Somebody screwed up with xfrm_sa_len! */ |
2368 | WARN_ON(1); | 2368 | WARN_ON(1); |
2369 | kfree_skb(skb); | 2369 | kfree_skb(skb); |
2370 | return -1; | 2370 | return -1; |
2371 | } | 2371 | } |
2372 | 2372 | ||
2373 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) | 2373 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) |
2374 | { | 2374 | { |
2375 | 2375 | ||
2376 | switch (c->event) { | 2376 | switch (c->event) { |
2377 | case XFRM_MSG_EXPIRE: | 2377 | case XFRM_MSG_EXPIRE: |
2378 | return xfrm_exp_state_notify(x, c); | 2378 | return xfrm_exp_state_notify(x, c); |
2379 | case XFRM_MSG_NEWAE: | 2379 | case XFRM_MSG_NEWAE: |
2380 | return xfrm_aevent_state_notify(x, c); | 2380 | return xfrm_aevent_state_notify(x, c); |
2381 | case XFRM_MSG_DELSA: | 2381 | case XFRM_MSG_DELSA: |
2382 | case XFRM_MSG_UPDSA: | 2382 | case XFRM_MSG_UPDSA: |
2383 | case XFRM_MSG_NEWSA: | 2383 | case XFRM_MSG_NEWSA: |
2384 | return xfrm_notify_sa(x, c); | 2384 | return xfrm_notify_sa(x, c); |
2385 | case XFRM_MSG_FLUSHSA: | 2385 | case XFRM_MSG_FLUSHSA: |
2386 | return xfrm_notify_sa_flush(c); | 2386 | return xfrm_notify_sa_flush(c); |
2387 | default: | 2387 | default: |
2388 | printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", | 2388 | printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", |
2389 | c->event); | 2389 | c->event); |
2390 | break; | 2390 | break; |
2391 | } | 2391 | } |
2392 | 2392 | ||
2393 | return 0; | 2393 | return 0; |
2394 | 2394 | ||
2395 | } | 2395 | } |
2396 | 2396 | ||
2397 | static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, | 2397 | static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, |
2398 | struct xfrm_policy *xp) | 2398 | struct xfrm_policy *xp) |
2399 | { | 2399 | { |
2400 | return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) | 2400 | return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) |
2401 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | 2401 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) |
2402 | + nla_total_size(sizeof(struct xfrm_mark)) | 2402 | + nla_total_size(sizeof(struct xfrm_mark)) |
2403 | + nla_total_size(xfrm_user_sec_ctx_size(x->security)) | 2403 | + nla_total_size(xfrm_user_sec_ctx_size(x->security)) |
2404 | + userpolicy_type_attrsize(); | 2404 | + userpolicy_type_attrsize(); |
2405 | } | 2405 | } |
2406 | 2406 | ||
2407 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, | 2407 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, |
2408 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, | 2408 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, |
2409 | int dir) | 2409 | int dir) |
2410 | { | 2410 | { |
2411 | struct xfrm_user_acquire *ua; | 2411 | struct xfrm_user_acquire *ua; |
2412 | struct nlmsghdr *nlh; | 2412 | struct nlmsghdr *nlh; |
2413 | __u32 seq = xfrm_get_acqseq(); | 2413 | __u32 seq = xfrm_get_acqseq(); |
2414 | 2414 | ||
2415 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); | 2415 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); |
2416 | if (nlh == NULL) | 2416 | if (nlh == NULL) |
2417 | return -EMSGSIZE; | 2417 | return -EMSGSIZE; |
2418 | 2418 | ||
2419 | ua = nlmsg_data(nlh); | 2419 | ua = nlmsg_data(nlh); |
2420 | memcpy(&ua->id, &x->id, sizeof(ua->id)); | 2420 | memcpy(&ua->id, &x->id, sizeof(ua->id)); |
2421 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); | 2421 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); |
2422 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); | 2422 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); |
2423 | copy_to_user_policy(xp, &ua->policy, dir); | 2423 | copy_to_user_policy(xp, &ua->policy, dir); |
2424 | ua->aalgos = xt->aalgos; | 2424 | ua->aalgos = xt->aalgos; |
2425 | ua->ealgos = xt->ealgos; | 2425 | ua->ealgos = xt->ealgos; |
2426 | ua->calgos = xt->calgos; | 2426 | ua->calgos = xt->calgos; |
2427 | ua->seq = x->km.seq = seq; | 2427 | ua->seq = x->km.seq = seq; |
2428 | 2428 | ||
2429 | if (copy_to_user_tmpl(xp, skb) < 0) | 2429 | if (copy_to_user_tmpl(xp, skb) < 0) |
2430 | goto nlmsg_failure; | 2430 | goto nlmsg_failure; |
2431 | if (copy_to_user_state_sec_ctx(x, skb)) | 2431 | if (copy_to_user_state_sec_ctx(x, skb)) |
2432 | goto nlmsg_failure; | 2432 | goto nlmsg_failure; |
2433 | if (copy_to_user_policy_type(xp->type, skb) < 0) | 2433 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
2434 | goto nlmsg_failure; | 2434 | goto nlmsg_failure; |
2435 | if (xfrm_mark_put(skb, &xp->mark)) | 2435 | if (xfrm_mark_put(skb, &xp->mark)) |
2436 | goto nla_put_failure; | 2436 | goto nla_put_failure; |
2437 | 2437 | ||
2438 | return nlmsg_end(skb, nlh); | 2438 | return nlmsg_end(skb, nlh); |
2439 | 2439 | ||
2440 | nla_put_failure: | 2440 | nla_put_failure: |
2441 | nlmsg_failure: | 2441 | nlmsg_failure: |
2442 | nlmsg_cancel(skb, nlh); | 2442 | nlmsg_cancel(skb, nlh); |
2443 | return -EMSGSIZE; | 2443 | return -EMSGSIZE; |
2444 | } | 2444 | } |
2445 | 2445 | ||
2446 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, | 2446 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, |
2447 | struct xfrm_policy *xp, int dir) | 2447 | struct xfrm_policy *xp, int dir) |
2448 | { | 2448 | { |
2449 | struct net *net = xs_net(x); | 2449 | struct net *net = xs_net(x); |
2450 | struct sk_buff *skb; | 2450 | struct sk_buff *skb; |
2451 | 2451 | ||
2452 | skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); | 2452 | skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); |
2453 | if (skb == NULL) | 2453 | if (skb == NULL) |
2454 | return -ENOMEM; | 2454 | return -ENOMEM; |
2455 | 2455 | ||
2456 | if (build_acquire(skb, x, xt, xp, dir) < 0) | 2456 | if (build_acquire(skb, x, xt, xp, dir) < 0) |
2457 | BUG(); | 2457 | BUG(); |
2458 | 2458 | ||
2459 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); | 2459 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); |
2460 | } | 2460 | } |
2461 | 2461 | ||
2462 | /* User gives us xfrm_user_policy_info followed by an array of 0 | 2462 | /* User gives us xfrm_user_policy_info followed by an array of 0 |
2463 | * or more templates. | 2463 | * or more templates. |
2464 | */ | 2464 | */ |
2465 | static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, | 2465 | static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, |
2466 | u8 *data, int len, int *dir) | 2466 | u8 *data, int len, int *dir) |
2467 | { | 2467 | { |
2468 | struct net *net = sock_net(sk); | 2468 | struct net *net = sock_net(sk); |
2469 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; | 2469 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; |
2470 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); | 2470 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); |
2471 | struct xfrm_policy *xp; | 2471 | struct xfrm_policy *xp; |
2472 | int nr; | 2472 | int nr; |
2473 | 2473 | ||
2474 | switch (sk->sk_family) { | 2474 | switch (sk->sk_family) { |
2475 | case AF_INET: | 2475 | case AF_INET: |
2476 | if (opt != IP_XFRM_POLICY) { | 2476 | if (opt != IP_XFRM_POLICY) { |
2477 | *dir = -EOPNOTSUPP; | 2477 | *dir = -EOPNOTSUPP; |
2478 | return NULL; | 2478 | return NULL; |
2479 | } | 2479 | } |
2480 | break; | 2480 | break; |
2481 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 2481 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
2482 | case AF_INET6: | 2482 | case AF_INET6: |
2483 | if (opt != IPV6_XFRM_POLICY) { | 2483 | if (opt != IPV6_XFRM_POLICY) { |
2484 | *dir = -EOPNOTSUPP; | 2484 | *dir = -EOPNOTSUPP; |
2485 | return NULL; | 2485 | return NULL; |
2486 | } | 2486 | } |
2487 | break; | 2487 | break; |
2488 | #endif | 2488 | #endif |
2489 | default: | 2489 | default: |
2490 | *dir = -EINVAL; | 2490 | *dir = -EINVAL; |
2491 | return NULL; | 2491 | return NULL; |
2492 | } | 2492 | } |
2493 | 2493 | ||
2494 | *dir = -EINVAL; | 2494 | *dir = -EINVAL; |
2495 | 2495 | ||
2496 | if (len < sizeof(*p) || | 2496 | if (len < sizeof(*p) || |
2497 | verify_newpolicy_info(p)) | 2497 | verify_newpolicy_info(p)) |
2498 | return NULL; | 2498 | return NULL; |
2499 | 2499 | ||
2500 | nr = ((len - sizeof(*p)) / sizeof(*ut)); | 2500 | nr = ((len - sizeof(*p)) / sizeof(*ut)); |
2501 | if (validate_tmpl(nr, ut, p->sel.family)) | 2501 | if (validate_tmpl(nr, ut, p->sel.family)) |
2502 | return NULL; | 2502 | return NULL; |
2503 | 2503 | ||
2504 | if (p->dir > XFRM_POLICY_OUT) | 2504 | if (p->dir > XFRM_POLICY_OUT) |
2505 | return NULL; | 2505 | return NULL; |
2506 | 2506 | ||
2507 | xp = xfrm_policy_alloc(net, GFP_ATOMIC); | 2507 | xp = xfrm_policy_alloc(net, GFP_ATOMIC); |
2508 | if (xp == NULL) { | 2508 | if (xp == NULL) { |
2509 | *dir = -ENOBUFS; | 2509 | *dir = -ENOBUFS; |
2510 | return NULL; | 2510 | return NULL; |
2511 | } | 2511 | } |
2512 | 2512 | ||
2513 | copy_from_user_policy(xp, p); | 2513 | copy_from_user_policy(xp, p); |
2514 | xp->type = XFRM_POLICY_TYPE_MAIN; | 2514 | xp->type = XFRM_POLICY_TYPE_MAIN; |
2515 | copy_templates(xp, ut, nr); | 2515 | copy_templates(xp, ut, nr); |
2516 | 2516 | ||
2517 | *dir = p->dir; | 2517 | *dir = p->dir; |
2518 | 2518 | ||
2519 | return xp; | 2519 | return xp; |
2520 | } | 2520 | } |
2521 | 2521 | ||
2522 | static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) | 2522 | static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) |
2523 | { | 2523 | { |
2524 | return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) | 2524 | return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) |
2525 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | 2525 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) |
2526 | + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) | 2526 | + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) |
2527 | + nla_total_size(sizeof(struct xfrm_mark)) | 2527 | + nla_total_size(sizeof(struct xfrm_mark)) |
2528 | + userpolicy_type_attrsize(); | 2528 | + userpolicy_type_attrsize(); |
2529 | } | 2529 | } |
2530 | 2530 | ||
2531 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, | 2531 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, |
2532 | int dir, struct km_event *c) | 2532 | int dir, struct km_event *c) |
2533 | { | 2533 | { |
2534 | struct xfrm_user_polexpire *upe; | 2534 | struct xfrm_user_polexpire *upe; |
2535 | struct nlmsghdr *nlh; | 2535 | struct nlmsghdr *nlh; |
2536 | int hard = c->data.hard; | 2536 | int hard = c->data.hard; |
2537 | 2537 | ||
2538 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); | 2538 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); |
2539 | if (nlh == NULL) | 2539 | if (nlh == NULL) |
2540 | return -EMSGSIZE; | 2540 | return -EMSGSIZE; |
2541 | 2541 | ||
2542 | upe = nlmsg_data(nlh); | 2542 | upe = nlmsg_data(nlh); |
2543 | copy_to_user_policy(xp, &upe->pol, dir); | 2543 | copy_to_user_policy(xp, &upe->pol, dir); |
2544 | if (copy_to_user_tmpl(xp, skb) < 0) | 2544 | if (copy_to_user_tmpl(xp, skb) < 0) |
2545 | goto nlmsg_failure; | 2545 | goto nlmsg_failure; |
2546 | if (copy_to_user_sec_ctx(xp, skb)) | 2546 | if (copy_to_user_sec_ctx(xp, skb)) |
2547 | goto nlmsg_failure; | 2547 | goto nlmsg_failure; |
2548 | if (copy_to_user_policy_type(xp->type, skb) < 0) | 2548 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
2549 | goto nlmsg_failure; | 2549 | goto nlmsg_failure; |
2550 | if (xfrm_mark_put(skb, &xp->mark)) | 2550 | if (xfrm_mark_put(skb, &xp->mark)) |
2551 | goto nla_put_failure; | 2551 | goto nla_put_failure; |
2552 | upe->hard = !!hard; | 2552 | upe->hard = !!hard; |
2553 | 2553 | ||
2554 | return nlmsg_end(skb, nlh); | 2554 | return nlmsg_end(skb, nlh); |
2555 | 2555 | ||
2556 | nla_put_failure: | 2556 | nla_put_failure: |
2557 | nlmsg_failure: | 2557 | nlmsg_failure: |
2558 | nlmsg_cancel(skb, nlh); | 2558 | nlmsg_cancel(skb, nlh); |
2559 | return -EMSGSIZE; | 2559 | return -EMSGSIZE; |
2560 | } | 2560 | } |
2561 | 2561 | ||
2562 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | 2562 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
2563 | { | 2563 | { |
2564 | struct net *net = xp_net(xp); | 2564 | struct net *net = xp_net(xp); |
2565 | struct sk_buff *skb; | 2565 | struct sk_buff *skb; |
2566 | 2566 | ||
2567 | skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); | 2567 | skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); |
2568 | if (skb == NULL) | 2568 | if (skb == NULL) |
2569 | return -ENOMEM; | 2569 | return -ENOMEM; |
2570 | 2570 | ||
2571 | if (build_polexpire(skb, xp, dir, c) < 0) | 2571 | if (build_polexpire(skb, xp, dir, c) < 0) |
2572 | BUG(); | 2572 | BUG(); |
2573 | 2573 | ||
2574 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); | 2574 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
2575 | } | 2575 | } |
2576 | 2576 | ||
2577 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) | 2577 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
2578 | { | 2578 | { |
2579 | struct net *net = xp_net(xp); | 2579 | struct net *net = xp_net(xp); |
2580 | struct xfrm_userpolicy_info *p; | 2580 | struct xfrm_userpolicy_info *p; |
2581 | struct xfrm_userpolicy_id *id; | 2581 | struct xfrm_userpolicy_id *id; |
2582 | struct nlmsghdr *nlh; | 2582 | struct nlmsghdr *nlh; |
2583 | struct sk_buff *skb; | 2583 | struct sk_buff *skb; |
2584 | int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); | 2584 | int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
2585 | int headlen; | 2585 | int headlen; |
2586 | 2586 | ||
2587 | headlen = sizeof(*p); | 2587 | headlen = sizeof(*p); |
2588 | if (c->event == XFRM_MSG_DELPOLICY) { | 2588 | if (c->event == XFRM_MSG_DELPOLICY) { |
2589 | len += nla_total_size(headlen); | 2589 | len += nla_total_size(headlen); |
2590 | headlen = sizeof(*id); | 2590 | headlen = sizeof(*id); |
2591 | } | 2591 | } |
2592 | len += userpolicy_type_attrsize(); | 2592 | len += userpolicy_type_attrsize(); |
2593 | len += nla_total_size(sizeof(struct xfrm_mark)); | 2593 | len += nla_total_size(sizeof(struct xfrm_mark)); |
2594 | len += NLMSG_ALIGN(headlen); | 2594 | len += NLMSG_ALIGN(headlen); |
2595 | 2595 | ||
2596 | skb = nlmsg_new(len, GFP_ATOMIC); | 2596 | skb = nlmsg_new(len, GFP_ATOMIC); |
2597 | if (skb == NULL) | 2597 | if (skb == NULL) |
2598 | return -ENOMEM; | 2598 | return -ENOMEM; |
2599 | 2599 | ||
2600 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); | 2600 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2601 | if (nlh == NULL) | 2601 | if (nlh == NULL) |
2602 | goto nlmsg_failure; | 2602 | goto nlmsg_failure; |
2603 | 2603 | ||
2604 | p = nlmsg_data(nlh); | 2604 | p = nlmsg_data(nlh); |
2605 | if (c->event == XFRM_MSG_DELPOLICY) { | 2605 | if (c->event == XFRM_MSG_DELPOLICY) { |
2606 | struct nlattr *attr; | 2606 | struct nlattr *attr; |
2607 | 2607 | ||
2608 | id = nlmsg_data(nlh); | 2608 | id = nlmsg_data(nlh); |
2609 | memset(id, 0, sizeof(*id)); | 2609 | memset(id, 0, sizeof(*id)); |
2610 | id->dir = dir; | 2610 | id->dir = dir; |
2611 | if (c->data.byid) | 2611 | if (c->data.byid) |
2612 | id->index = xp->index; | 2612 | id->index = xp->index; |
2613 | else | 2613 | else |
2614 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); | 2614 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); |
2615 | 2615 | ||
2616 | attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); | 2616 | attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); |
2617 | if (attr == NULL) | 2617 | if (attr == NULL) |
2618 | goto nlmsg_failure; | 2618 | goto nlmsg_failure; |
2619 | 2619 | ||
2620 | p = nla_data(attr); | 2620 | p = nla_data(attr); |
2621 | } | 2621 | } |
2622 | 2622 | ||
2623 | copy_to_user_policy(xp, p, dir); | 2623 | copy_to_user_policy(xp, p, dir); |
2624 | if (copy_to_user_tmpl(xp, skb) < 0) | 2624 | if (copy_to_user_tmpl(xp, skb) < 0) |
2625 | goto nlmsg_failure; | 2625 | goto nlmsg_failure; |
2626 | if (copy_to_user_policy_type(xp->type, skb) < 0) | 2626 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
2627 | goto nlmsg_failure; | 2627 | goto nlmsg_failure; |
2628 | 2628 | ||
2629 | if (xfrm_mark_put(skb, &xp->mark)) | 2629 | if (xfrm_mark_put(skb, &xp->mark)) |
2630 | goto nla_put_failure; | 2630 | goto nla_put_failure; |
2631 | 2631 | ||
2632 | nlmsg_end(skb, nlh); | 2632 | nlmsg_end(skb, nlh); |
2633 | 2633 | ||
2634 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); | 2634 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
2635 | 2635 | ||
2636 | nla_put_failure: | 2636 | nla_put_failure: |
2637 | nlmsg_failure: | 2637 | nlmsg_failure: |
2638 | kfree_skb(skb); | 2638 | kfree_skb(skb); |
2639 | return -1; | 2639 | return -1; |
2640 | } | 2640 | } |
2641 | 2641 | ||
2642 | static int xfrm_notify_policy_flush(struct km_event *c) | 2642 | static int xfrm_notify_policy_flush(struct km_event *c) |
2643 | { | 2643 | { |
2644 | struct net *net = c->net; | 2644 | struct net *net = c->net; |
2645 | struct nlmsghdr *nlh; | 2645 | struct nlmsghdr *nlh; |
2646 | struct sk_buff *skb; | 2646 | struct sk_buff *skb; |
2647 | 2647 | ||
2648 | skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); | 2648 | skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); |
2649 | if (skb == NULL) | 2649 | if (skb == NULL) |
2650 | return -ENOMEM; | 2650 | return -ENOMEM; |
2651 | 2651 | ||
2652 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); | 2652 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); |
2653 | if (nlh == NULL) | 2653 | if (nlh == NULL) |
2654 | goto nlmsg_failure; | 2654 | goto nlmsg_failure; |
2655 | if (copy_to_user_policy_type(c->data.type, skb) < 0) | 2655 | if (copy_to_user_policy_type(c->data.type, skb) < 0) |
2656 | goto nlmsg_failure; | 2656 | goto nlmsg_failure; |
2657 | 2657 | ||
2658 | nlmsg_end(skb, nlh); | 2658 | nlmsg_end(skb, nlh); |
2659 | 2659 | ||
2660 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); | 2660 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
2661 | 2661 | ||
2662 | nlmsg_failure: | 2662 | nlmsg_failure: |
2663 | kfree_skb(skb); | 2663 | kfree_skb(skb); |
2664 | return -1; | 2664 | return -1; |
2665 | } | 2665 | } |
2666 | 2666 | ||
2667 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | 2667 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
2668 | { | 2668 | { |
2669 | 2669 | ||
2670 | switch (c->event) { | 2670 | switch (c->event) { |
2671 | case XFRM_MSG_NEWPOLICY: | 2671 | case XFRM_MSG_NEWPOLICY: |
2672 | case XFRM_MSG_UPDPOLICY: | 2672 | case XFRM_MSG_UPDPOLICY: |
2673 | case XFRM_MSG_DELPOLICY: | 2673 | case XFRM_MSG_DELPOLICY: |
2674 | return xfrm_notify_policy(xp, dir, c); | 2674 | return xfrm_notify_policy(xp, dir, c); |
2675 | case XFRM_MSG_FLUSHPOLICY: | 2675 | case XFRM_MSG_FLUSHPOLICY: |
2676 | return xfrm_notify_policy_flush(c); | 2676 | return xfrm_notify_policy_flush(c); |
2677 | case XFRM_MSG_POLEXPIRE: | 2677 | case XFRM_MSG_POLEXPIRE: |
2678 | return xfrm_exp_policy_notify(xp, dir, c); | 2678 | return xfrm_exp_policy_notify(xp, dir, c); |
2679 | default: | 2679 | default: |
2680 | printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", | 2680 | printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", |
2681 | c->event); | 2681 | c->event); |
2682 | } | 2682 | } |
2683 | 2683 | ||
2684 | return 0; | 2684 | return 0; |
2685 | 2685 | ||
2686 | } | 2686 | } |
2687 | 2687 | ||
2688 | static inline size_t xfrm_report_msgsize(void) | 2688 | static inline size_t xfrm_report_msgsize(void) |
2689 | { | 2689 | { |
2690 | return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); | 2690 | return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); |
2691 | } | 2691 | } |
2692 | 2692 | ||
2693 | static int build_report(struct sk_buff *skb, u8 proto, | 2693 | static int build_report(struct sk_buff *skb, u8 proto, |
2694 | struct xfrm_selector *sel, xfrm_address_t *addr) | 2694 | struct xfrm_selector *sel, xfrm_address_t *addr) |
2695 | { | 2695 | { |
2696 | struct xfrm_user_report *ur; | 2696 | struct xfrm_user_report *ur; |
2697 | struct nlmsghdr *nlh; | 2697 | struct nlmsghdr *nlh; |
2698 | 2698 | ||
2699 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); | 2699 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); |
2700 | if (nlh == NULL) | 2700 | if (nlh == NULL) |
2701 | return -EMSGSIZE; | 2701 | return -EMSGSIZE; |
2702 | 2702 | ||
2703 | ur = nlmsg_data(nlh); | 2703 | ur = nlmsg_data(nlh); |
2704 | ur->proto = proto; | 2704 | ur->proto = proto; |
2705 | memcpy(&ur->sel, sel, sizeof(ur->sel)); | 2705 | memcpy(&ur->sel, sel, sizeof(ur->sel)); |
2706 | 2706 | ||
2707 | if (addr) | 2707 | if (addr) |
2708 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); | 2708 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); |
2709 | 2709 | ||
2710 | return nlmsg_end(skb, nlh); | 2710 | return nlmsg_end(skb, nlh); |
2711 | 2711 | ||
2712 | nla_put_failure: | 2712 | nla_put_failure: |
2713 | nlmsg_cancel(skb, nlh); | 2713 | nlmsg_cancel(skb, nlh); |
2714 | return -EMSGSIZE; | 2714 | return -EMSGSIZE; |
2715 | } | 2715 | } |
2716 | 2716 | ||
2717 | static int xfrm_send_report(struct net *net, u8 proto, | 2717 | static int xfrm_send_report(struct net *net, u8 proto, |
2718 | struct xfrm_selector *sel, xfrm_address_t *addr) | 2718 | struct xfrm_selector *sel, xfrm_address_t *addr) |
2719 | { | 2719 | { |
2720 | struct sk_buff *skb; | 2720 | struct sk_buff *skb; |
2721 | 2721 | ||
2722 | skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); | 2722 | skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); |
2723 | if (skb == NULL) | 2723 | if (skb == NULL) |
2724 | return -ENOMEM; | 2724 | return -ENOMEM; |
2725 | 2725 | ||
2726 | if (build_report(skb, proto, sel, addr) < 0) | 2726 | if (build_report(skb, proto, sel, addr) < 0) |
2727 | BUG(); | 2727 | BUG(); |
2728 | 2728 | ||
2729 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); | 2729 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); |
2730 | } | 2730 | } |
2731 | 2731 | ||
2732 | static inline size_t xfrm_mapping_msgsize(void) | 2732 | static inline size_t xfrm_mapping_msgsize(void) |
2733 | { | 2733 | { |
2734 | return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); | 2734 | return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); |
2735 | } | 2735 | } |
2736 | 2736 | ||
2737 | static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, | 2737 | static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, |
2738 | xfrm_address_t *new_saddr, __be16 new_sport) | 2738 | xfrm_address_t *new_saddr, __be16 new_sport) |
2739 | { | 2739 | { |
2740 | struct xfrm_user_mapping *um; | 2740 | struct xfrm_user_mapping *um; |
2741 | struct nlmsghdr *nlh; | 2741 | struct nlmsghdr *nlh; |
2742 | 2742 | ||
2743 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); | 2743 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); |
2744 | if (nlh == NULL) | 2744 | if (nlh == NULL) |
2745 | return -EMSGSIZE; | 2745 | return -EMSGSIZE; |
2746 | 2746 | ||
2747 | um = nlmsg_data(nlh); | 2747 | um = nlmsg_data(nlh); |
2748 | 2748 | ||
2749 | memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); | 2749 | memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); |
2750 | um->id.spi = x->id.spi; | 2750 | um->id.spi = x->id.spi; |
2751 | um->id.family = x->props.family; | 2751 | um->id.family = x->props.family; |
2752 | um->id.proto = x->id.proto; | 2752 | um->id.proto = x->id.proto; |
2753 | memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); | 2753 | memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); |
2754 | memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); | 2754 | memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); |
2755 | um->new_sport = new_sport; | 2755 | um->new_sport = new_sport; |
2756 | um->old_sport = x->encap->encap_sport; | 2756 | um->old_sport = x->encap->encap_sport; |
2757 | um->reqid = x->props.reqid; | 2757 | um->reqid = x->props.reqid; |
2758 | 2758 | ||
2759 | return nlmsg_end(skb, nlh); | 2759 | return nlmsg_end(skb, nlh); |
2760 | } | 2760 | } |
2761 | 2761 | ||
2762 | static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, | 2762 | static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, |
2763 | __be16 sport) | 2763 | __be16 sport) |
2764 | { | 2764 | { |
2765 | struct net *net = xs_net(x); | 2765 | struct net *net = xs_net(x); |
2766 | struct sk_buff *skb; | 2766 | struct sk_buff *skb; |
2767 | 2767 | ||
2768 | if (x->id.proto != IPPROTO_ESP) | 2768 | if (x->id.proto != IPPROTO_ESP) |
2769 | return -EINVAL; | 2769 | return -EINVAL; |
2770 | 2770 | ||
2771 | if (!x->encap) | 2771 | if (!x->encap) |
2772 | return -EINVAL; | 2772 | return -EINVAL; |
2773 | 2773 | ||
2774 | skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); | 2774 | skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); |
2775 | if (skb == NULL) | 2775 | if (skb == NULL) |
2776 | return -ENOMEM; | 2776 | return -ENOMEM; |
2777 | 2777 | ||
2778 | if (build_mapping(skb, x, ipaddr, sport) < 0) | 2778 | if (build_mapping(skb, x, ipaddr, sport) < 0) |
2779 | BUG(); | 2779 | BUG(); |
2780 | 2780 | ||
2781 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC); | 2781 | return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC); |
2782 | } | 2782 | } |
2783 | 2783 | ||
2784 | static struct xfrm_mgr netlink_mgr = { | 2784 | static struct xfrm_mgr netlink_mgr = { |
2785 | .id = "netlink", | 2785 | .id = "netlink", |
2786 | .notify = xfrm_send_state_notify, | 2786 | .notify = xfrm_send_state_notify, |
2787 | .acquire = xfrm_send_acquire, | 2787 | .acquire = xfrm_send_acquire, |
2788 | .compile_policy = xfrm_compile_policy, | 2788 | .compile_policy = xfrm_compile_policy, |
2789 | .notify_policy = xfrm_send_policy_notify, | 2789 | .notify_policy = xfrm_send_policy_notify, |
2790 | .report = xfrm_send_report, | 2790 | .report = xfrm_send_report, |
2791 | .migrate = xfrm_send_migrate, | 2791 | .migrate = xfrm_send_migrate, |
2792 | .new_mapping = xfrm_send_mapping, | 2792 | .new_mapping = xfrm_send_mapping, |
2793 | }; | 2793 | }; |
2794 | 2794 | ||
2795 | static int __net_init xfrm_user_net_init(struct net *net) | 2795 | static int __net_init xfrm_user_net_init(struct net *net) |
2796 | { | 2796 | { |
2797 | struct sock *nlsk; | 2797 | struct sock *nlsk; |
2798 | 2798 | ||
2799 | nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX, | 2799 | nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX, |
2800 | xfrm_netlink_rcv, NULL, THIS_MODULE); | 2800 | xfrm_netlink_rcv, NULL, THIS_MODULE); |
2801 | if (nlsk == NULL) | 2801 | if (nlsk == NULL) |
2802 | return -ENOMEM; | 2802 | return -ENOMEM; |
2803 | net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ | 2803 | net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ |
2804 | rcu_assign_pointer(net->xfrm.nlsk, nlsk); | 2804 | rcu_assign_pointer(net->xfrm.nlsk, nlsk); |
2805 | return 0; | 2805 | return 0; |
2806 | } | 2806 | } |
2807 | 2807 | ||
2808 | static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) | 2808 | static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) |
2809 | { | 2809 | { |
2810 | struct net *net; | 2810 | struct net *net; |
2811 | list_for_each_entry(net, net_exit_list, exit_list) | 2811 | list_for_each_entry(net, net_exit_list, exit_list) |
2812 | rcu_assign_pointer(net->xfrm.nlsk, NULL); | 2812 | rcu_assign_pointer(net->xfrm.nlsk, NULL); |
2813 | synchronize_net(); | 2813 | synchronize_net(); |
2814 | list_for_each_entry(net, net_exit_list, exit_list) | 2814 | list_for_each_entry(net, net_exit_list, exit_list) |
2815 | netlink_kernel_release(net->xfrm.nlsk_stash); | 2815 | netlink_kernel_release(net->xfrm.nlsk_stash); |
2816 | } | 2816 | } |
2817 | 2817 | ||
2818 | static struct pernet_operations xfrm_user_net_ops = { | 2818 | static struct pernet_operations xfrm_user_net_ops = { |
2819 | .init = xfrm_user_net_init, | 2819 | .init = xfrm_user_net_init, |
2820 | .exit_batch = xfrm_user_net_exit, | 2820 | .exit_batch = xfrm_user_net_exit, |
2821 | }; | 2821 | }; |
2822 | 2822 | ||
2823 | static int __init xfrm_user_init(void) | 2823 | static int __init xfrm_user_init(void) |
2824 | { | 2824 | { |
2825 | int rv; | 2825 | int rv; |
2826 | 2826 | ||
2827 | printk(KERN_INFO "Initializing XFRM netlink socket\n"); | 2827 | printk(KERN_INFO "Initializing XFRM netlink socket\n"); |
2828 | 2828 | ||
2829 | rv = register_pernet_subsys(&xfrm_user_net_ops); | 2829 | rv = register_pernet_subsys(&xfrm_user_net_ops); |
2830 | if (rv < 0) | 2830 | if (rv < 0) |
2831 | return rv; | 2831 | return rv; |
2832 | rv = xfrm_register_km(&netlink_mgr); | 2832 | rv = xfrm_register_km(&netlink_mgr); |
2833 | if (rv < 0) | 2833 | if (rv < 0) |
2834 | unregister_pernet_subsys(&xfrm_user_net_ops); | 2834 | unregister_pernet_subsys(&xfrm_user_net_ops); |
2835 | return rv; | 2835 | return rv; |
2836 | } | 2836 | } |
2837 | 2837 | ||
2838 | static void __exit xfrm_user_exit(void) | 2838 | static void __exit xfrm_user_exit(void) |
2839 | { | 2839 | { |
2840 | xfrm_unregister_km(&netlink_mgr); | 2840 | xfrm_unregister_km(&netlink_mgr); |
2841 | unregister_pernet_subsys(&xfrm_user_net_ops); | 2841 | unregister_pernet_subsys(&xfrm_user_net_ops); |
2842 | } | 2842 | } |
2843 | 2843 | ||
2844 | module_init(xfrm_user_init); | 2844 | module_init(xfrm_user_init); |
2845 | module_exit(xfrm_user_exit); | 2845 | module_exit(xfrm_user_exit); |
2846 | MODULE_LICENSE("GPL"); | 2846 | MODULE_LICENSE("GPL"); |
2847 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); | 2847 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); |
2848 | 2848 | ||
2849 | 2849 |