]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/key/af_key.c An implementation of PF_KEYv2 sockets. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Maxim Giryaev <gem@asplinux.ru> | |
10 | * David S. Miller <davem@redhat.com> | |
11 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
12 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> | |
13 | * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org> | |
14 | * Derek Atkins <derek@ihtfp.com> | |
15 | */ | |
16 | ||
4fc268d2 | 17 | #include <linux/capability.h> |
1da177e4 LT |
18 | #include <linux/module.h> |
19 | #include <linux/kernel.h> | |
20 | #include <linux/socket.h> | |
21 | #include <linux/pfkeyv2.h> | |
22 | #include <linux/ipsec.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/rtnetlink.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/in6.h> | |
27 | #include <linux/proc_fs.h> | |
28 | #include <linux/init.h> | |
457c4cbc | 29 | #include <net/net_namespace.h> |
1da177e4 LT |
30 | #include <net/xfrm.h> |
31 | ||
32 | #include <net/sock.h> | |
33 | ||
34 | #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x)) | |
35 | #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x)) | |
36 | ||
37 | ||
38 | /* List of all pfkey sockets. */ | |
39 | static HLIST_HEAD(pfkey_table); | |
40 | static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait); | |
41 | static DEFINE_RWLOCK(pfkey_table_lock); | |
42 | static atomic_t pfkey_table_users = ATOMIC_INIT(0); | |
43 | ||
44 | static atomic_t pfkey_socks_nr = ATOMIC_INIT(0); | |
45 | ||
46 | struct pfkey_sock { | |
47 | /* struct sock must be the first member of struct pfkey_sock */ | |
48 | struct sock sk; | |
49 | int registered; | |
50 | int promisc; | |
83321d6b TT |
51 | |
52 | struct { | |
53 | uint8_t msg_version; | |
54 | uint32_t msg_pid; | |
55 | int (*dump)(struct pfkey_sock *sk); | |
56 | void (*done)(struct pfkey_sock *sk); | |
57 | union { | |
58 | struct xfrm_policy_walk policy; | |
59 | struct xfrm_state_walk state; | |
60 | } u; | |
12a169e7 | 61 | struct sk_buff *skb; |
83321d6b | 62 | } dump; |
1da177e4 LT |
63 | }; |
64 | ||
65 | static inline struct pfkey_sock *pfkey_sk(struct sock *sk) | |
66 | { | |
67 | return (struct pfkey_sock *)sk; | |
68 | } | |
69 | ||
83321d6b TT |
70 | static int pfkey_can_dump(struct sock *sk) |
71 | { | |
72 | if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf) | |
73 | return 1; | |
74 | return 0; | |
75 | } | |
76 | ||
05238204 | 77 | static void pfkey_terminate_dump(struct pfkey_sock *pfk) |
83321d6b | 78 | { |
05238204 | 79 | if (pfk->dump.dump) { |
12a169e7 HX |
80 | if (pfk->dump.skb) { |
81 | kfree_skb(pfk->dump.skb); | |
82 | pfk->dump.skb = NULL; | |
83 | } | |
05238204 TT |
84 | pfk->dump.done(pfk); |
85 | pfk->dump.dump = NULL; | |
86 | pfk->dump.done = NULL; | |
87 | } | |
83321d6b TT |
88 | } |
89 | ||
1da177e4 LT |
90 | static void pfkey_sock_destruct(struct sock *sk) |
91 | { | |
05238204 | 92 | pfkey_terminate_dump(pfkey_sk(sk)); |
1da177e4 LT |
93 | skb_queue_purge(&sk->sk_receive_queue); |
94 | ||
95 | if (!sock_flag(sk, SOCK_DEAD)) { | |
96 | printk("Attempt to release alive pfkey socket: %p\n", sk); | |
97 | return; | |
98 | } | |
99 | ||
547b792c IJ |
100 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
101 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); | |
1da177e4 LT |
102 | |
103 | atomic_dec(&pfkey_socks_nr); | |
104 | } | |
105 | ||
106 | static void pfkey_table_grab(void) | |
107 | { | |
108 | write_lock_bh(&pfkey_table_lock); | |
109 | ||
110 | if (atomic_read(&pfkey_table_users)) { | |
111 | DECLARE_WAITQUEUE(wait, current); | |
112 | ||
113 | add_wait_queue_exclusive(&pfkey_table_wait, &wait); | |
114 | for(;;) { | |
115 | set_current_state(TASK_UNINTERRUPTIBLE); | |
116 | if (atomic_read(&pfkey_table_users) == 0) | |
117 | break; | |
118 | write_unlock_bh(&pfkey_table_lock); | |
119 | schedule(); | |
120 | write_lock_bh(&pfkey_table_lock); | |
121 | } | |
122 | ||
123 | __set_current_state(TASK_RUNNING); | |
124 | remove_wait_queue(&pfkey_table_wait, &wait); | |
125 | } | |
126 | } | |
127 | ||
128 | static __inline__ void pfkey_table_ungrab(void) | |
129 | { | |
130 | write_unlock_bh(&pfkey_table_lock); | |
131 | wake_up(&pfkey_table_wait); | |
132 | } | |
133 | ||
134 | static __inline__ void pfkey_lock_table(void) | |
135 | { | |
136 | /* read_lock() synchronizes us to pfkey_table_grab */ | |
137 | ||
138 | read_lock(&pfkey_table_lock); | |
139 | atomic_inc(&pfkey_table_users); | |
140 | read_unlock(&pfkey_table_lock); | |
141 | } | |
142 | ||
143 | static __inline__ void pfkey_unlock_table(void) | |
144 | { | |
145 | if (atomic_dec_and_test(&pfkey_table_users)) | |
146 | wake_up(&pfkey_table_wait); | |
147 | } | |
148 | ||
149 | ||
90ddc4f0 | 150 | static const struct proto_ops pfkey_ops; |
1da177e4 LT |
151 | |
152 | static void pfkey_insert(struct sock *sk) | |
153 | { | |
154 | pfkey_table_grab(); | |
155 | sk_add_node(sk, &pfkey_table); | |
156 | pfkey_table_ungrab(); | |
157 | } | |
158 | ||
159 | static void pfkey_remove(struct sock *sk) | |
160 | { | |
161 | pfkey_table_grab(); | |
162 | sk_del_node_init(sk); | |
163 | pfkey_table_ungrab(); | |
164 | } | |
165 | ||
166 | static struct proto key_proto = { | |
167 | .name = "KEY", | |
168 | .owner = THIS_MODULE, | |
169 | .obj_size = sizeof(struct pfkey_sock), | |
170 | }; | |
171 | ||
1b8d7ae4 | 172 | static int pfkey_create(struct net *net, struct socket *sock, int protocol) |
1da177e4 LT |
173 | { |
174 | struct sock *sk; | |
175 | int err; | |
176 | ||
1b8d7ae4 EB |
177 | if (net != &init_net) |
178 | return -EAFNOSUPPORT; | |
179 | ||
1da177e4 LT |
180 | if (!capable(CAP_NET_ADMIN)) |
181 | return -EPERM; | |
182 | if (sock->type != SOCK_RAW) | |
183 | return -ESOCKTNOSUPPORT; | |
184 | if (protocol != PF_KEY_V2) | |
185 | return -EPROTONOSUPPORT; | |
186 | ||
187 | err = -ENOMEM; | |
6257ff21 | 188 | sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto); |
1da177e4 LT |
189 | if (sk == NULL) |
190 | goto out; | |
8ff24541 | 191 | |
1da177e4 LT |
192 | sock->ops = &pfkey_ops; |
193 | sock_init_data(sock, sk); | |
194 | ||
195 | sk->sk_family = PF_KEY; | |
196 | sk->sk_destruct = pfkey_sock_destruct; | |
197 | ||
198 | atomic_inc(&pfkey_socks_nr); | |
199 | ||
200 | pfkey_insert(sk); | |
201 | ||
202 | return 0; | |
203 | out: | |
204 | return err; | |
205 | } | |
206 | ||
207 | static int pfkey_release(struct socket *sock) | |
208 | { | |
209 | struct sock *sk = sock->sk; | |
210 | ||
211 | if (!sk) | |
212 | return 0; | |
213 | ||
214 | pfkey_remove(sk); | |
215 | ||
216 | sock_orphan(sk); | |
217 | sock->sk = NULL; | |
218 | skb_queue_purge(&sk->sk_write_queue); | |
219 | sock_put(sk); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2, | |
dd0fc66f | 225 | gfp_t allocation, struct sock *sk) |
1da177e4 LT |
226 | { |
227 | int err = -ENOBUFS; | |
228 | ||
229 | sock_hold(sk); | |
230 | if (*skb2 == NULL) { | |
231 | if (atomic_read(&skb->users) != 1) { | |
232 | *skb2 = skb_clone(skb, allocation); | |
233 | } else { | |
234 | *skb2 = skb; | |
235 | atomic_inc(&skb->users); | |
236 | } | |
237 | } | |
238 | if (*skb2 != NULL) { | |
239 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) { | |
240 | skb_orphan(*skb2); | |
241 | skb_set_owner_r(*skb2, sk); | |
242 | skb_queue_tail(&sk->sk_receive_queue, *skb2); | |
243 | sk->sk_data_ready(sk, (*skb2)->len); | |
244 | *skb2 = NULL; | |
245 | err = 0; | |
246 | } | |
247 | } | |
248 | sock_put(sk); | |
249 | return err; | |
250 | } | |
251 | ||
252 | /* Send SKB to all pfkey sockets matching selected criteria. */ | |
253 | #define BROADCAST_ALL 0 | |
254 | #define BROADCAST_ONE 1 | |
255 | #define BROADCAST_REGISTERED 2 | |
256 | #define BROADCAST_PROMISC_ONLY 4 | |
dd0fc66f | 257 | static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, |
1da177e4 LT |
258 | int broadcast_flags, struct sock *one_sk) |
259 | { | |
260 | struct sock *sk; | |
261 | struct hlist_node *node; | |
262 | struct sk_buff *skb2 = NULL; | |
263 | int err = -ESRCH; | |
264 | ||
265 | /* XXX Do we need something like netlink_overrun? I think | |
266 | * XXX PF_KEY socket apps will not mind current behavior. | |
267 | */ | |
268 | if (!skb) | |
269 | return -ENOMEM; | |
270 | ||
271 | pfkey_lock_table(); | |
272 | sk_for_each(sk, node, &pfkey_table) { | |
273 | struct pfkey_sock *pfk = pfkey_sk(sk); | |
274 | int err2; | |
275 | ||
276 | /* Yes, it means that if you are meant to receive this | |
277 | * pfkey message you receive it twice as promiscuous | |
278 | * socket. | |
279 | */ | |
280 | if (pfk->promisc) | |
281 | pfkey_broadcast_one(skb, &skb2, allocation, sk); | |
282 | ||
283 | /* the exact target will be processed later */ | |
284 | if (sk == one_sk) | |
285 | continue; | |
286 | if (broadcast_flags != BROADCAST_ALL) { | |
287 | if (broadcast_flags & BROADCAST_PROMISC_ONLY) | |
288 | continue; | |
289 | if ((broadcast_flags & BROADCAST_REGISTERED) && | |
290 | !pfk->registered) | |
291 | continue; | |
292 | if (broadcast_flags & BROADCAST_ONE) | |
293 | continue; | |
294 | } | |
295 | ||
296 | err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk); | |
297 | ||
298 | /* Error is cleare after succecful sending to at least one | |
299 | * registered KM */ | |
300 | if ((broadcast_flags & BROADCAST_REGISTERED) && err) | |
301 | err = err2; | |
302 | } | |
303 | pfkey_unlock_table(); | |
304 | ||
305 | if (one_sk != NULL) | |
306 | err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk); | |
307 | ||
308 | if (skb2) | |
309 | kfree_skb(skb2); | |
310 | kfree_skb(skb); | |
311 | return err; | |
312 | } | |
313 | ||
05238204 TT |
314 | static int pfkey_do_dump(struct pfkey_sock *pfk) |
315 | { | |
12a169e7 | 316 | struct sadb_msg *hdr; |
05238204 TT |
317 | int rc; |
318 | ||
319 | rc = pfk->dump.dump(pfk); | |
320 | if (rc == -ENOBUFS) | |
321 | return 0; | |
322 | ||
12a169e7 HX |
323 | if (pfk->dump.skb) { |
324 | if (!pfkey_can_dump(&pfk->sk)) | |
325 | return 0; | |
326 | ||
327 | hdr = (struct sadb_msg *) pfk->dump.skb->data; | |
328 | hdr->sadb_msg_seq = 0; | |
329 | hdr->sadb_msg_errno = rc; | |
330 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, | |
331 | &pfk->sk); | |
332 | pfk->dump.skb = NULL; | |
333 | } | |
334 | ||
05238204 TT |
335 | pfkey_terminate_dump(pfk); |
336 | return rc; | |
337 | } | |
338 | ||
1da177e4 LT |
339 | static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig) |
340 | { | |
341 | *new = *orig; | |
342 | } | |
343 | ||
344 | static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk) | |
345 | { | |
346 | struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL); | |
347 | struct sadb_msg *hdr; | |
348 | ||
349 | if (!skb) | |
350 | return -ENOBUFS; | |
351 | ||
352 | /* Woe be to the platform trying to support PFKEY yet | |
353 | * having normal errnos outside the 1-255 range, inclusive. | |
354 | */ | |
355 | err = -err; | |
356 | if (err == ERESTARTSYS || | |
357 | err == ERESTARTNOHAND || | |
358 | err == ERESTARTNOINTR) | |
359 | err = EINTR; | |
360 | if (err >= 512) | |
361 | err = EINVAL; | |
09a62660 | 362 | BUG_ON(err <= 0 || err >= 256); |
1da177e4 LT |
363 | |
364 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); | |
365 | pfkey_hdr_dup(hdr, orig); | |
366 | hdr->sadb_msg_errno = (uint8_t) err; | |
367 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / | |
368 | sizeof(uint64_t)); | |
369 | ||
370 | pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | static u8 sadb_ext_min_len[] = { | |
376 | [SADB_EXT_RESERVED] = (u8) 0, | |
377 | [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa), | |
378 | [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime), | |
379 | [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime), | |
380 | [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime), | |
381 | [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address), | |
382 | [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address), | |
383 | [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address), | |
384 | [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key), | |
385 | [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key), | |
386 | [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident), | |
387 | [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident), | |
388 | [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens), | |
389 | [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop), | |
390 | [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported), | |
391 | [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported), | |
392 | [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange), | |
393 | [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate), | |
394 | [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy), | |
395 | [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2), | |
396 | [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type), | |
397 | [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port), | |
398 | [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port), | |
399 | [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address), | |
df71837d | 400 | [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx), |
1da177e4 LT |
401 | }; |
402 | ||
403 | /* Verify sadb_address_{len,prefixlen} against sa_family. */ | |
404 | static int verify_address_len(void *p) | |
405 | { | |
406 | struct sadb_address *sp = p; | |
407 | struct sockaddr *addr = (struct sockaddr *)(sp + 1); | |
408 | struct sockaddr_in *sin; | |
409 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
410 | struct sockaddr_in6 *sin6; | |
411 | #endif | |
412 | int len; | |
413 | ||
414 | switch (addr->sa_family) { | |
415 | case AF_INET: | |
356f89e1 | 416 | len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t)); |
1da177e4 LT |
417 | if (sp->sadb_address_len != len || |
418 | sp->sadb_address_prefixlen > 32) | |
419 | return -EINVAL; | |
420 | break; | |
421 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
422 | case AF_INET6: | |
356f89e1 | 423 | len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t)); |
1da177e4 LT |
424 | if (sp->sadb_address_len != len || |
425 | sp->sadb_address_prefixlen > 128) | |
426 | return -EINVAL; | |
427 | break; | |
428 | #endif | |
429 | default: | |
430 | /* It is user using kernel to keep track of security | |
431 | * associations for another protocol, such as | |
432 | * OSPF/RSVP/RIPV2/MIP. It is user's job to verify | |
433 | * lengths. | |
434 | * | |
435 | * XXX Actually, association/policy database is not yet | |
436 | * XXX able to cope with arbitrary sockaddr families. | |
437 | * XXX When it can, remove this -EINVAL. -DaveM | |
438 | */ | |
439 | return -EINVAL; | |
440 | break; | |
3ff50b79 | 441 | } |
1da177e4 LT |
442 | |
443 | return 0; | |
444 | } | |
445 | ||
df71837d TJ |
446 | static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx) |
447 | { | |
356f89e1 IJ |
448 | return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) + |
449 | sec_ctx->sadb_x_ctx_len, | |
450 | sizeof(uint64_t)); | |
df71837d TJ |
451 | } |
452 | ||
453 | static inline int verify_sec_ctx_len(void *p) | |
454 | { | |
455 | struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p; | |
298bb621 | 456 | int len = sec_ctx->sadb_x_ctx_len; |
df71837d | 457 | |
298bb621 | 458 | if (len > PAGE_SIZE) |
df71837d TJ |
459 | return -EINVAL; |
460 | ||
461 | len = pfkey_sec_ctx_len(sec_ctx); | |
462 | ||
463 | if (sec_ctx->sadb_x_sec_len != len) | |
464 | return -EINVAL; | |
465 | ||
466 | return 0; | |
467 | } | |
468 | ||
469 | static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx) | |
470 | { | |
471 | struct xfrm_user_sec_ctx *uctx = NULL; | |
472 | int ctx_size = sec_ctx->sadb_x_ctx_len; | |
473 | ||
474 | uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL); | |
475 | ||
476 | if (!uctx) | |
477 | return NULL; | |
478 | ||
479 | uctx->len = pfkey_sec_ctx_len(sec_ctx); | |
480 | uctx->exttype = sec_ctx->sadb_x_sec_exttype; | |
481 | uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi; | |
482 | uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg; | |
483 | uctx->ctx_len = sec_ctx->sadb_x_ctx_len; | |
484 | memcpy(uctx + 1, sec_ctx + 1, | |
485 | uctx->ctx_len); | |
486 | ||
487 | return uctx; | |
488 | } | |
489 | ||
1da177e4 LT |
490 | static int present_and_same_family(struct sadb_address *src, |
491 | struct sadb_address *dst) | |
492 | { | |
493 | struct sockaddr *s_addr, *d_addr; | |
494 | ||
495 | if (!src || !dst) | |
496 | return 0; | |
497 | ||
498 | s_addr = (struct sockaddr *)(src + 1); | |
499 | d_addr = (struct sockaddr *)(dst + 1); | |
500 | if (s_addr->sa_family != d_addr->sa_family) | |
501 | return 0; | |
502 | if (s_addr->sa_family != AF_INET | |
503 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
504 | && s_addr->sa_family != AF_INET6 | |
505 | #endif | |
506 | ) | |
507 | return 0; | |
508 | ||
509 | return 1; | |
510 | } | |
511 | ||
512 | static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
513 | { | |
514 | char *p = (char *) hdr; | |
515 | int len = skb->len; | |
516 | ||
517 | len -= sizeof(*hdr); | |
518 | p += sizeof(*hdr); | |
519 | while (len > 0) { | |
520 | struct sadb_ext *ehdr = (struct sadb_ext *) p; | |
521 | uint16_t ext_type; | |
522 | int ext_len; | |
523 | ||
524 | ext_len = ehdr->sadb_ext_len; | |
525 | ext_len *= sizeof(uint64_t); | |
526 | ext_type = ehdr->sadb_ext_type; | |
527 | if (ext_len < sizeof(uint64_t) || | |
528 | ext_len > len || | |
529 | ext_type == SADB_EXT_RESERVED) | |
530 | return -EINVAL; | |
531 | ||
532 | if (ext_type <= SADB_EXT_MAX) { | |
533 | int min = (int) sadb_ext_min_len[ext_type]; | |
534 | if (ext_len < min) | |
535 | return -EINVAL; | |
536 | if (ext_hdrs[ext_type-1] != NULL) | |
537 | return -EINVAL; | |
538 | if (ext_type == SADB_EXT_ADDRESS_SRC || | |
539 | ext_type == SADB_EXT_ADDRESS_DST || | |
540 | ext_type == SADB_EXT_ADDRESS_PROXY || | |
541 | ext_type == SADB_X_EXT_NAT_T_OA) { | |
542 | if (verify_address_len(p)) | |
543 | return -EINVAL; | |
8ff24541 | 544 | } |
df71837d TJ |
545 | if (ext_type == SADB_X_EXT_SEC_CTX) { |
546 | if (verify_sec_ctx_len(p)) | |
547 | return -EINVAL; | |
548 | } | |
1da177e4 LT |
549 | ext_hdrs[ext_type-1] = p; |
550 | } | |
551 | p += ext_len; | |
552 | len -= ext_len; | |
553 | } | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
558 | static uint16_t | |
559 | pfkey_satype2proto(uint8_t satype) | |
560 | { | |
561 | switch (satype) { | |
562 | case SADB_SATYPE_UNSPEC: | |
563 | return IPSEC_PROTO_ANY; | |
564 | case SADB_SATYPE_AH: | |
565 | return IPPROTO_AH; | |
566 | case SADB_SATYPE_ESP: | |
567 | return IPPROTO_ESP; | |
568 | case SADB_X_SATYPE_IPCOMP: | |
569 | return IPPROTO_COMP; | |
570 | break; | |
571 | default: | |
572 | return 0; | |
573 | } | |
574 | /* NOTREACHED */ | |
575 | } | |
576 | ||
577 | static uint8_t | |
578 | pfkey_proto2satype(uint16_t proto) | |
579 | { | |
580 | switch (proto) { | |
581 | case IPPROTO_AH: | |
582 | return SADB_SATYPE_AH; | |
583 | case IPPROTO_ESP: | |
584 | return SADB_SATYPE_ESP; | |
585 | case IPPROTO_COMP: | |
586 | return SADB_X_SATYPE_IPCOMP; | |
587 | break; | |
588 | default: | |
589 | return 0; | |
590 | } | |
591 | /* NOTREACHED */ | |
592 | } | |
593 | ||
594 | /* BTW, this scheme means that there is no way with PFKEY2 sockets to | |
595 | * say specifically 'just raw sockets' as we encode them as 255. | |
596 | */ | |
597 | ||
598 | static uint8_t pfkey_proto_to_xfrm(uint8_t proto) | |
599 | { | |
600 | return (proto == IPSEC_PROTO_ANY ? 0 : proto); | |
601 | } | |
602 | ||
603 | static uint8_t pfkey_proto_from_xfrm(uint8_t proto) | |
604 | { | |
605 | return (proto ? proto : IPSEC_PROTO_ANY); | |
606 | } | |
607 | ||
9e8b4ed8 YH |
608 | static inline int pfkey_sockaddr_len(sa_family_t family) |
609 | { | |
610 | switch (family) { | |
611 | case AF_INET: | |
612 | return sizeof(struct sockaddr_in); | |
613 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
614 | case AF_INET6: | |
615 | return sizeof(struct sockaddr_in6); | |
616 | #endif | |
617 | } | |
618 | return 0; | |
619 | } | |
620 | ||
5f95ac91 YH |
621 | static |
622 | int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr) | |
1da177e4 | 623 | { |
5f95ac91 | 624 | switch (sa->sa_family) { |
1da177e4 | 625 | case AF_INET: |
8ff24541 | 626 | xaddr->a4 = |
5f95ac91 | 627 | ((struct sockaddr_in *)sa)->sin_addr.s_addr; |
1da177e4 LT |
628 | return AF_INET; |
629 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
630 | case AF_INET6: | |
8ff24541 | 631 | memcpy(xaddr->a6, |
5f95ac91 | 632 | &((struct sockaddr_in6 *)sa)->sin6_addr, |
1da177e4 LT |
633 | sizeof(struct in6_addr)); |
634 | return AF_INET6; | |
635 | #endif | |
1da177e4 | 636 | } |
5f95ac91 YH |
637 | return 0; |
638 | } | |
639 | ||
640 | static | |
641 | int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr) | |
642 | { | |
643 | return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1), | |
644 | xaddr); | |
1da177e4 LT |
645 | } |
646 | ||
647 | static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs) | |
648 | { | |
649 | struct sadb_sa *sa; | |
650 | struct sadb_address *addr; | |
651 | uint16_t proto; | |
652 | unsigned short family; | |
653 | xfrm_address_t *xaddr; | |
654 | ||
655 | sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1]; | |
656 | if (sa == NULL) | |
657 | return NULL; | |
658 | ||
659 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); | |
660 | if (proto == 0) | |
661 | return NULL; | |
662 | ||
663 | /* sadb_address_len should be checked by caller */ | |
664 | addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1]; | |
665 | if (addr == NULL) | |
666 | return NULL; | |
667 | ||
668 | family = ((struct sockaddr *)(addr + 1))->sa_family; | |
669 | switch (family) { | |
670 | case AF_INET: | |
671 | xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr; | |
672 | break; | |
673 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
674 | case AF_INET6: | |
675 | xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr; | |
676 | break; | |
677 | #endif | |
678 | default: | |
679 | xaddr = NULL; | |
680 | } | |
681 | ||
682 | if (!xaddr) | |
683 | return NULL; | |
684 | ||
685 | return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family); | |
686 | } | |
687 | ||
688 | #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1))) | |
9e8b4ed8 | 689 | |
1da177e4 LT |
690 | static int |
691 | pfkey_sockaddr_size(sa_family_t family) | |
692 | { | |
9e8b4ed8 | 693 | return PFKEY_ALIGN8(pfkey_sockaddr_len(family)); |
1da177e4 LT |
694 | } |
695 | ||
55569ce2 KM |
696 | static inline int pfkey_mode_from_xfrm(int mode) |
697 | { | |
698 | switch(mode) { | |
699 | case XFRM_MODE_TRANSPORT: | |
700 | return IPSEC_MODE_TRANSPORT; | |
701 | case XFRM_MODE_TUNNEL: | |
702 | return IPSEC_MODE_TUNNEL; | |
703 | case XFRM_MODE_BEET: | |
704 | return IPSEC_MODE_BEET; | |
705 | default: | |
706 | return -1; | |
707 | } | |
708 | } | |
709 | ||
710 | static inline int pfkey_mode_to_xfrm(int mode) | |
711 | { | |
712 | switch(mode) { | |
713 | case IPSEC_MODE_ANY: /*XXX*/ | |
714 | case IPSEC_MODE_TRANSPORT: | |
715 | return XFRM_MODE_TRANSPORT; | |
716 | case IPSEC_MODE_TUNNEL: | |
717 | return XFRM_MODE_TUNNEL; | |
718 | case IPSEC_MODE_BEET: | |
719 | return XFRM_MODE_BEET; | |
720 | default: | |
721 | return -1; | |
722 | } | |
723 | } | |
724 | ||
e5b56652 YH |
725 | static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port, |
726 | struct sockaddr *sa, | |
727 | unsigned short family) | |
728 | { | |
729 | switch (family) { | |
730 | case AF_INET: | |
731 | { | |
732 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; | |
733 | sin->sin_family = AF_INET; | |
734 | sin->sin_port = port; | |
735 | sin->sin_addr.s_addr = xaddr->a4; | |
736 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
737 | return 32; | |
738 | } | |
739 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
740 | case AF_INET6: | |
741 | { | |
742 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; | |
743 | sin6->sin6_family = AF_INET6; | |
744 | sin6->sin6_port = port; | |
745 | sin6->sin6_flowinfo = 0; | |
746 | ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6); | |
747 | sin6->sin6_scope_id = 0; | |
748 | return 128; | |
749 | } | |
750 | #endif | |
751 | } | |
752 | return 0; | |
753 | } | |
754 | ||
050f009e HX |
755 | static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x, |
756 | int add_keys, int hsc) | |
1da177e4 LT |
757 | { |
758 | struct sk_buff *skb; | |
759 | struct sadb_msg *hdr; | |
760 | struct sadb_sa *sa; | |
761 | struct sadb_lifetime *lifetime; | |
762 | struct sadb_address *addr; | |
763 | struct sadb_key *key; | |
764 | struct sadb_x_sa2 *sa2; | |
df71837d TJ |
765 | struct sadb_x_sec_ctx *sec_ctx; |
766 | struct xfrm_sec_ctx *xfrm_ctx; | |
767 | int ctx_size = 0; | |
1da177e4 LT |
768 | int size; |
769 | int auth_key_size = 0; | |
770 | int encrypt_key_size = 0; | |
771 | int sockaddr_size; | |
772 | struct xfrm_encap_tmpl *natt = NULL; | |
55569ce2 | 773 | int mode; |
1da177e4 LT |
774 | |
775 | /* address family check */ | |
776 | sockaddr_size = pfkey_sockaddr_size(x->props.family); | |
777 | if (!sockaddr_size) | |
778 | return ERR_PTR(-EINVAL); | |
779 | ||
780 | /* base, SA, (lifetime (HSC),) address(SD), (address(P),) | |
781 | key(AE), (identity(SD),) (sensitivity)> */ | |
8ff24541 | 782 | size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) + |
1da177e4 LT |
783 | sizeof(struct sadb_lifetime) + |
784 | ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) + | |
785 | ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) + | |
8ff24541 | 786 | sizeof(struct sadb_address)*2 + |
1da177e4 LT |
787 | sockaddr_size*2 + |
788 | sizeof(struct sadb_x_sa2); | |
df71837d TJ |
789 | |
790 | if ((xfrm_ctx = x->security)) { | |
791 | ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); | |
792 | size += sizeof(struct sadb_x_sec_ctx) + ctx_size; | |
793 | } | |
794 | ||
1da177e4 | 795 | /* identity & sensitivity */ |
81b302a3 | 796 | if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family)) |
1da177e4 LT |
797 | size += sizeof(struct sadb_address) + sockaddr_size; |
798 | ||
799 | if (add_keys) { | |
800 | if (x->aalg && x->aalg->alg_key_len) { | |
8ff24541 YH |
801 | auth_key_size = |
802 | PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8); | |
1da177e4 LT |
803 | size += sizeof(struct sadb_key) + auth_key_size; |
804 | } | |
805 | if (x->ealg && x->ealg->alg_key_len) { | |
8ff24541 YH |
806 | encrypt_key_size = |
807 | PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8); | |
1da177e4 LT |
808 | size += sizeof(struct sadb_key) + encrypt_key_size; |
809 | } | |
810 | } | |
811 | if (x->encap) | |
812 | natt = x->encap; | |
813 | ||
814 | if (natt && natt->encap_type) { | |
815 | size += sizeof(struct sadb_x_nat_t_type); | |
816 | size += sizeof(struct sadb_x_nat_t_port); | |
817 | size += sizeof(struct sadb_x_nat_t_port); | |
818 | } | |
819 | ||
820 | skb = alloc_skb(size + 16, GFP_ATOMIC); | |
821 | if (skb == NULL) | |
822 | return ERR_PTR(-ENOBUFS); | |
823 | ||
824 | /* call should fill header later */ | |
825 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); | |
826 | memset(hdr, 0, size); /* XXX do we need this ? */ | |
827 | hdr->sadb_msg_len = size / sizeof(uint64_t); | |
828 | ||
829 | /* sa */ | |
830 | sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa)); | |
831 | sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); | |
832 | sa->sadb_sa_exttype = SADB_EXT_SA; | |
833 | sa->sadb_sa_spi = x->id.spi; | |
834 | sa->sadb_sa_replay = x->props.replay_window; | |
4f09f0bb HX |
835 | switch (x->km.state) { |
836 | case XFRM_STATE_VALID: | |
837 | sa->sadb_sa_state = x->km.dying ? | |
838 | SADB_SASTATE_DYING : SADB_SASTATE_MATURE; | |
839 | break; | |
840 | case XFRM_STATE_ACQ: | |
1da177e4 | 841 | sa->sadb_sa_state = SADB_SASTATE_LARVAL; |
4f09f0bb HX |
842 | break; |
843 | default: | |
1da177e4 | 844 | sa->sadb_sa_state = SADB_SASTATE_DEAD; |
4f09f0bb HX |
845 | break; |
846 | } | |
1da177e4 LT |
847 | sa->sadb_sa_auth = 0; |
848 | if (x->aalg) { | |
849 | struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0); | |
850 | sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0; | |
851 | } | |
852 | sa->sadb_sa_encrypt = 0; | |
853 | BUG_ON(x->ealg && x->calg); | |
854 | if (x->ealg) { | |
855 | struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0); | |
856 | sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0; | |
857 | } | |
858 | /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */ | |
859 | if (x->calg) { | |
860 | struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0); | |
861 | sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0; | |
862 | } | |
863 | ||
864 | sa->sadb_sa_flags = 0; | |
865 | if (x->props.flags & XFRM_STATE_NOECN) | |
866 | sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN; | |
867 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) | |
868 | sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP; | |
dd87147e HX |
869 | if (x->props.flags & XFRM_STATE_NOPMTUDISC) |
870 | sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC; | |
1da177e4 LT |
871 | |
872 | /* hard time */ | |
873 | if (hsc & 2) { | |
8ff24541 | 874 | lifetime = (struct sadb_lifetime *) skb_put(skb, |
1da177e4 LT |
875 | sizeof(struct sadb_lifetime)); |
876 | lifetime->sadb_lifetime_len = | |
877 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
878 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; | |
879 | lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit); | |
880 | lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit); | |
881 | lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds; | |
882 | lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds; | |
883 | } | |
884 | /* soft time */ | |
885 | if (hsc & 1) { | |
8ff24541 | 886 | lifetime = (struct sadb_lifetime *) skb_put(skb, |
1da177e4 LT |
887 | sizeof(struct sadb_lifetime)); |
888 | lifetime->sadb_lifetime_len = | |
889 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
890 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; | |
891 | lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit); | |
892 | lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit); | |
893 | lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds; | |
894 | lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds; | |
895 | } | |
896 | /* current time */ | |
897 | lifetime = (struct sadb_lifetime *) skb_put(skb, | |
898 | sizeof(struct sadb_lifetime)); | |
899 | lifetime->sadb_lifetime_len = | |
900 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
901 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | |
902 | lifetime->sadb_lifetime_allocations = x->curlft.packets; | |
903 | lifetime->sadb_lifetime_bytes = x->curlft.bytes; | |
904 | lifetime->sadb_lifetime_addtime = x->curlft.add_time; | |
905 | lifetime->sadb_lifetime_usetime = x->curlft.use_time; | |
906 | /* src address */ | |
8ff24541 | 907 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 | 908 | sizeof(struct sadb_address)+sockaddr_size); |
8ff24541 | 909 | addr->sadb_address_len = |
1da177e4 LT |
910 | (sizeof(struct sadb_address)+sockaddr_size)/ |
911 | sizeof(uint64_t); | |
912 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; | |
8ff24541 YH |
913 | /* "if the ports are non-zero, then the sadb_address_proto field, |
914 | normally zero, MUST be filled in with the transport | |
1da177e4 | 915 | protocol's number." - RFC2367 */ |
8ff24541 | 916 | addr->sadb_address_proto = 0; |
1da177e4 | 917 | addr->sadb_address_reserved = 0; |
1da177e4 | 918 | |
e5b56652 YH |
919 | addr->sadb_address_prefixlen = |
920 | pfkey_sockaddr_fill(&x->props.saddr, 0, | |
921 | (struct sockaddr *) (addr + 1), | |
922 | x->props.family); | |
923 | if (!addr->sadb_address_prefixlen) | |
1da177e4 LT |
924 | BUG(); |
925 | ||
926 | /* dst address */ | |
8ff24541 | 927 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 | 928 | sizeof(struct sadb_address)+sockaddr_size); |
8ff24541 | 929 | addr->sadb_address_len = |
1da177e4 LT |
930 | (sizeof(struct sadb_address)+sockaddr_size)/ |
931 | sizeof(uint64_t); | |
932 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; | |
8ff24541 | 933 | addr->sadb_address_proto = 0; |
1da177e4 | 934 | addr->sadb_address_reserved = 0; |
1da177e4 | 935 | |
e5b56652 YH |
936 | addr->sadb_address_prefixlen = |
937 | pfkey_sockaddr_fill(&x->id.daddr, 0, | |
938 | (struct sockaddr *) (addr + 1), | |
939 | x->props.family); | |
940 | if (!addr->sadb_address_prefixlen) | |
941 | BUG(); | |
1da177e4 | 942 | |
e5b56652 YH |
943 | if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, |
944 | x->props.family)) { | |
945 | addr = (struct sadb_address*) skb_put(skb, | |
946 | sizeof(struct sadb_address)+sockaddr_size); | |
947 | addr->sadb_address_len = | |
948 | (sizeof(struct sadb_address)+sockaddr_size)/ | |
949 | sizeof(uint64_t); | |
950 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY; | |
951 | addr->sadb_address_proto = | |
952 | pfkey_proto_from_xfrm(x->sel.proto); | |
953 | addr->sadb_address_prefixlen = x->sel.prefixlen_s; | |
954 | addr->sadb_address_reserved = 0; | |
1da177e4 | 955 | |
e5b56652 YH |
956 | pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport, |
957 | (struct sockaddr *) (addr + 1), | |
958 | x->props.family); | |
1da177e4 | 959 | } |
1da177e4 LT |
960 | |
961 | /* auth key */ | |
962 | if (add_keys && auth_key_size) { | |
8ff24541 | 963 | key = (struct sadb_key *) skb_put(skb, |
1da177e4 LT |
964 | sizeof(struct sadb_key)+auth_key_size); |
965 | key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) / | |
966 | sizeof(uint64_t); | |
967 | key->sadb_key_exttype = SADB_EXT_KEY_AUTH; | |
968 | key->sadb_key_bits = x->aalg->alg_key_len; | |
969 | key->sadb_key_reserved = 0; | |
970 | memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8); | |
971 | } | |
972 | /* encrypt key */ | |
973 | if (add_keys && encrypt_key_size) { | |
8ff24541 | 974 | key = (struct sadb_key *) skb_put(skb, |
1da177e4 | 975 | sizeof(struct sadb_key)+encrypt_key_size); |
8ff24541 | 976 | key->sadb_key_len = (sizeof(struct sadb_key) + |
1da177e4 LT |
977 | encrypt_key_size) / sizeof(uint64_t); |
978 | key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT; | |
979 | key->sadb_key_bits = x->ealg->alg_key_len; | |
980 | key->sadb_key_reserved = 0; | |
8ff24541 | 981 | memcpy(key + 1, x->ealg->alg_key, |
1da177e4 LT |
982 | (x->ealg->alg_key_len+7)/8); |
983 | } | |
984 | ||
985 | /* sa */ | |
986 | sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2)); | |
987 | sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t); | |
988 | sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2; | |
55569ce2 KM |
989 | if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) { |
990 | kfree_skb(skb); | |
991 | return ERR_PTR(-EINVAL); | |
992 | } | |
993 | sa2->sadb_x_sa2_mode = mode; | |
1da177e4 LT |
994 | sa2->sadb_x_sa2_reserved1 = 0; |
995 | sa2->sadb_x_sa2_reserved2 = 0; | |
996 | sa2->sadb_x_sa2_sequence = 0; | |
997 | sa2->sadb_x_sa2_reqid = x->props.reqid; | |
998 | ||
999 | if (natt && natt->encap_type) { | |
1000 | struct sadb_x_nat_t_type *n_type; | |
1001 | struct sadb_x_nat_t_port *n_port; | |
1002 | ||
1003 | /* type */ | |
1004 | n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type)); | |
1005 | n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t); | |
1006 | n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE; | |
1007 | n_type->sadb_x_nat_t_type_type = natt->encap_type; | |
1008 | n_type->sadb_x_nat_t_type_reserved[0] = 0; | |
1009 | n_type->sadb_x_nat_t_type_reserved[1] = 0; | |
1010 | n_type->sadb_x_nat_t_type_reserved[2] = 0; | |
1011 | ||
1012 | /* source port */ | |
1013 | n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); | |
1014 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); | |
1015 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; | |
1016 | n_port->sadb_x_nat_t_port_port = natt->encap_sport; | |
1017 | n_port->sadb_x_nat_t_port_reserved = 0; | |
1018 | ||
1019 | /* dest port */ | |
1020 | n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); | |
1021 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); | |
1022 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; | |
1023 | n_port->sadb_x_nat_t_port_port = natt->encap_dport; | |
1024 | n_port->sadb_x_nat_t_port_reserved = 0; | |
1025 | } | |
1026 | ||
df71837d TJ |
1027 | /* security context */ |
1028 | if (xfrm_ctx) { | |
1029 | sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, | |
1030 | sizeof(struct sadb_x_sec_ctx) + ctx_size); | |
1031 | sec_ctx->sadb_x_sec_len = | |
1032 | (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); | |
1033 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; | |
1034 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; | |
1035 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; | |
1036 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; | |
1037 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, | |
1038 | xfrm_ctx->ctx_len); | |
1039 | } | |
1040 | ||
1da177e4 LT |
1041 | return skb; |
1042 | } | |
1043 | ||
050f009e HX |
1044 | |
1045 | static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x) | |
1046 | { | |
1047 | struct sk_buff *skb; | |
1048 | ||
050f009e | 1049 | skb = __pfkey_xfrm_state2msg(x, 1, 3); |
050f009e HX |
1050 | |
1051 | return skb; | |
1052 | } | |
1053 | ||
1054 | static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x, | |
1055 | int hsc) | |
1056 | { | |
1057 | return __pfkey_xfrm_state2msg(x, 0, hsc); | |
1058 | } | |
1059 | ||
8ff24541 | 1060 | static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr, |
1da177e4 LT |
1061 | void **ext_hdrs) |
1062 | { | |
8ff24541 | 1063 | struct xfrm_state *x; |
1da177e4 LT |
1064 | struct sadb_lifetime *lifetime; |
1065 | struct sadb_sa *sa; | |
1066 | struct sadb_key *key; | |
df71837d | 1067 | struct sadb_x_sec_ctx *sec_ctx; |
1da177e4 LT |
1068 | uint16_t proto; |
1069 | int err; | |
8ff24541 | 1070 | |
1da177e4 LT |
1071 | |
1072 | sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1]; | |
1073 | if (!sa || | |
1074 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
1075 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) | |
1076 | return ERR_PTR(-EINVAL); | |
1077 | if (hdr->sadb_msg_satype == SADB_SATYPE_ESP && | |
1078 | !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]) | |
1079 | return ERR_PTR(-EINVAL); | |
1080 | if (hdr->sadb_msg_satype == SADB_SATYPE_AH && | |
1081 | !ext_hdrs[SADB_EXT_KEY_AUTH-1]) | |
1082 | return ERR_PTR(-EINVAL); | |
1083 | if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] != | |
1084 | !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) | |
1085 | return ERR_PTR(-EINVAL); | |
1086 | ||
1087 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); | |
1088 | if (proto == 0) | |
1089 | return ERR_PTR(-EINVAL); | |
1090 | ||
1091 | /* default error is no buffer space */ | |
1092 | err = -ENOBUFS; | |
1093 | ||
1094 | /* RFC2367: | |
1095 | ||
1096 | Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message. | |
1097 | SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not | |
1098 | sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state. | |
1099 | Therefore, the sadb_sa_state field of all submitted SAs MUST be | |
1100 | SADB_SASTATE_MATURE and the kernel MUST return an error if this is | |
1101 | not true. | |
1102 | ||
8ff24541 | 1103 | However, KAME setkey always uses SADB_SASTATE_LARVAL. |
1da177e4 LT |
1104 | Hence, we have to _ignore_ sadb_sa_state, which is also reasonable. |
1105 | */ | |
1106 | if (sa->sadb_sa_auth > SADB_AALG_MAX || | |
1107 | (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP && | |
1108 | sa->sadb_sa_encrypt > SADB_X_CALG_MAX) || | |
1109 | sa->sadb_sa_encrypt > SADB_EALG_MAX) | |
1110 | return ERR_PTR(-EINVAL); | |
1111 | key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1]; | |
1112 | if (key != NULL && | |
1113 | sa->sadb_sa_auth != SADB_X_AALG_NULL && | |
1114 | ((key->sadb_key_bits+7) / 8 == 0 || | |
1115 | (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t))) | |
1116 | return ERR_PTR(-EINVAL); | |
1117 | key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; | |
1118 | if (key != NULL && | |
1119 | sa->sadb_sa_encrypt != SADB_EALG_NULL && | |
1120 | ((key->sadb_key_bits+7) / 8 == 0 || | |
1121 | (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t))) | |
1122 | return ERR_PTR(-EINVAL); | |
1123 | ||
1124 | x = xfrm_state_alloc(); | |
1125 | if (x == NULL) | |
1126 | return ERR_PTR(-ENOBUFS); | |
1127 | ||
1128 | x->id.proto = proto; | |
1129 | x->id.spi = sa->sadb_sa_spi; | |
1130 | x->props.replay_window = sa->sadb_sa_replay; | |
1131 | if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN) | |
1132 | x->props.flags |= XFRM_STATE_NOECN; | |
1133 | if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP) | |
1134 | x->props.flags |= XFRM_STATE_DECAP_DSCP; | |
dd87147e HX |
1135 | if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC) |
1136 | x->props.flags |= XFRM_STATE_NOPMTUDISC; | |
1da177e4 LT |
1137 | |
1138 | lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1]; | |
1139 | if (lifetime != NULL) { | |
1140 | x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); | |
1141 | x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); | |
1142 | x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; | |
1143 | x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; | |
1144 | } | |
1145 | lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]; | |
1146 | if (lifetime != NULL) { | |
1147 | x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); | |
1148 | x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); | |
1149 | x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; | |
1150 | x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; | |
1151 | } | |
df71837d TJ |
1152 | |
1153 | sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1]; | |
1154 | if (sec_ctx != NULL) { | |
1155 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); | |
1156 | ||
1157 | if (!uctx) | |
1158 | goto out; | |
1159 | ||
1160 | err = security_xfrm_state_alloc(x, uctx); | |
1161 | kfree(uctx); | |
1162 | ||
1163 | if (err) | |
1164 | goto out; | |
1165 | } | |
1166 | ||
1da177e4 LT |
1167 | key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1]; |
1168 | if (sa->sadb_sa_auth) { | |
1169 | int keysize = 0; | |
1170 | struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth); | |
1171 | if (!a) { | |
1172 | err = -ENOSYS; | |
1173 | goto out; | |
1174 | } | |
1175 | if (key) | |
1176 | keysize = (key->sadb_key_bits + 7) / 8; | |
1177 | x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL); | |
1178 | if (!x->aalg) | |
1179 | goto out; | |
1180 | strcpy(x->aalg->alg_name, a->name); | |
1181 | x->aalg->alg_key_len = 0; | |
1182 | if (key) { | |
1183 | x->aalg->alg_key_len = key->sadb_key_bits; | |
1184 | memcpy(x->aalg->alg_key, key+1, keysize); | |
1185 | } | |
1186 | x->props.aalgo = sa->sadb_sa_auth; | |
1187 | /* x->algo.flags = sa->sadb_sa_flags; */ | |
1188 | } | |
1189 | if (sa->sadb_sa_encrypt) { | |
1190 | if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { | |
1191 | struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt); | |
1192 | if (!a) { | |
1193 | err = -ENOSYS; | |
1194 | goto out; | |
1195 | } | |
1196 | x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL); | |
1197 | if (!x->calg) | |
1198 | goto out; | |
1199 | strcpy(x->calg->alg_name, a->name); | |
1200 | x->props.calgo = sa->sadb_sa_encrypt; | |
1201 | } else { | |
1202 | int keysize = 0; | |
1203 | struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt); | |
1204 | if (!a) { | |
1205 | err = -ENOSYS; | |
1206 | goto out; | |
1207 | } | |
1208 | key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; | |
1209 | if (key) | |
1210 | keysize = (key->sadb_key_bits + 7) / 8; | |
1211 | x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL); | |
1212 | if (!x->ealg) | |
1213 | goto out; | |
1214 | strcpy(x->ealg->alg_name, a->name); | |
1215 | x->ealg->alg_key_len = 0; | |
1216 | if (key) { | |
1217 | x->ealg->alg_key_len = key->sadb_key_bits; | |
1218 | memcpy(x->ealg->alg_key, key+1, keysize); | |
1219 | } | |
1220 | x->props.ealgo = sa->sadb_sa_encrypt; | |
1221 | } | |
1222 | } | |
1223 | /* x->algo.flags = sa->sadb_sa_flags; */ | |
1224 | ||
8ff24541 | 1225 | x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
1da177e4 LT |
1226 | &x->props.saddr); |
1227 | if (!x->props.family) { | |
1228 | err = -EAFNOSUPPORT; | |
1229 | goto out; | |
1230 | } | |
8ff24541 | 1231 | pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], |
1da177e4 LT |
1232 | &x->id.daddr); |
1233 | ||
1234 | if (ext_hdrs[SADB_X_EXT_SA2-1]) { | |
1235 | struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1]; | |
55569ce2 KM |
1236 | int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); |
1237 | if (mode < 0) { | |
1238 | err = -EINVAL; | |
1239 | goto out; | |
1240 | } | |
1241 | x->props.mode = mode; | |
1da177e4 LT |
1242 | x->props.reqid = sa2->sadb_x_sa2_reqid; |
1243 | } | |
1244 | ||
1245 | if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) { | |
1246 | struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]; | |
1247 | ||
1248 | /* Nobody uses this, but we try. */ | |
1249 | x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr); | |
1250 | x->sel.prefixlen_s = addr->sadb_address_prefixlen; | |
1251 | } | |
1252 | ||
4da51056 | 1253 | if (!x->sel.family) |
4a4b6271 JL |
1254 | x->sel.family = x->props.family; |
1255 | ||
1da177e4 LT |
1256 | if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) { |
1257 | struct sadb_x_nat_t_type* n_type; | |
1258 | struct xfrm_encap_tmpl *natt; | |
1259 | ||
1260 | x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL); | |
1261 | if (!x->encap) | |
1262 | goto out; | |
1263 | ||
1264 | natt = x->encap; | |
1265 | n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]; | |
1266 | natt->encap_type = n_type->sadb_x_nat_t_type_type; | |
1267 | ||
1268 | if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) { | |
1269 | struct sadb_x_nat_t_port* n_port = | |
1270 | ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]; | |
1271 | natt->encap_sport = n_port->sadb_x_nat_t_port_port; | |
1272 | } | |
1273 | if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) { | |
1274 | struct sadb_x_nat_t_port* n_port = | |
1275 | ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]; | |
1276 | natt->encap_dport = n_port->sadb_x_nat_t_port_port; | |
1277 | } | |
1278 | } | |
1279 | ||
72cb6962 HX |
1280 | err = xfrm_init_state(x); |
1281 | if (err) | |
1da177e4 | 1282 | goto out; |
72cb6962 | 1283 | |
1da177e4 | 1284 | x->km.seq = hdr->sadb_msg_seq; |
1da177e4 LT |
1285 | return x; |
1286 | ||
1287 | out: | |
1288 | x->km.state = XFRM_STATE_DEAD; | |
1289 | xfrm_state_put(x); | |
1290 | return ERR_PTR(err); | |
1291 | } | |
1292 | ||
1293 | static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1294 | { | |
1295 | return -EOPNOTSUPP; | |
1296 | } | |
1297 | ||
1298 | static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1299 | { | |
1300 | struct sk_buff *resp_skb; | |
1301 | struct sadb_x_sa2 *sa2; | |
1302 | struct sadb_address *saddr, *daddr; | |
1303 | struct sadb_msg *out_hdr; | |
658b219e | 1304 | struct sadb_spirange *range; |
1da177e4 | 1305 | struct xfrm_state *x = NULL; |
55569ce2 | 1306 | int mode; |
658b219e HX |
1307 | int err; |
1308 | u32 min_spi, max_spi; | |
1da177e4 LT |
1309 | u32 reqid; |
1310 | u8 proto; | |
1311 | unsigned short family; | |
1312 | xfrm_address_t *xsaddr = NULL, *xdaddr = NULL; | |
1313 | ||
1314 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
1315 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) | |
1316 | return -EINVAL; | |
1317 | ||
1318 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); | |
1319 | if (proto == 0) | |
1320 | return -EINVAL; | |
1321 | ||
1322 | if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) { | |
55569ce2 KM |
1323 | mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); |
1324 | if (mode < 0) | |
1325 | return -EINVAL; | |
1da177e4 LT |
1326 | reqid = sa2->sadb_x_sa2_reqid; |
1327 | } else { | |
1328 | mode = 0; | |
1329 | reqid = 0; | |
1330 | } | |
1331 | ||
1332 | saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1]; | |
1333 | daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1]; | |
1334 | ||
1335 | family = ((struct sockaddr *)(saddr + 1))->sa_family; | |
1336 | switch (family) { | |
1337 | case AF_INET: | |
1338 | xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr; | |
1339 | xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr; | |
1340 | break; | |
1341 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
1342 | case AF_INET6: | |
1343 | xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr; | |
1344 | xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr; | |
1345 | break; | |
1346 | #endif | |
1347 | } | |
1348 | ||
1349 | if (hdr->sadb_msg_seq) { | |
1350 | x = xfrm_find_acq_byseq(hdr->sadb_msg_seq); | |
1351 | if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) { | |
1352 | xfrm_state_put(x); | |
1353 | x = NULL; | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | if (!x) | |
1358 | x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family); | |
1359 | ||
1360 | if (x == NULL) | |
1361 | return -ENOENT; | |
1362 | ||
658b219e HX |
1363 | min_spi = 0x100; |
1364 | max_spi = 0x0fffffff; | |
1da177e4 | 1365 | |
658b219e HX |
1366 | range = ext_hdrs[SADB_EXT_SPIRANGE-1]; |
1367 | if (range) { | |
1368 | min_spi = range->sadb_spirange_min; | |
1369 | max_spi = range->sadb_spirange_max; | |
1da177e4 | 1370 | } |
658b219e HX |
1371 | |
1372 | err = xfrm_alloc_spi(x, min_spi, max_spi); | |
050f009e | 1373 | resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x); |
1da177e4 LT |
1374 | |
1375 | if (IS_ERR(resp_skb)) { | |
1376 | xfrm_state_put(x); | |
1377 | return PTR_ERR(resp_skb); | |
1378 | } | |
1379 | ||
1380 | out_hdr = (struct sadb_msg *) resp_skb->data; | |
1381 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; | |
1382 | out_hdr->sadb_msg_type = SADB_GETSPI; | |
1383 | out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); | |
1384 | out_hdr->sadb_msg_errno = 0; | |
1385 | out_hdr->sadb_msg_reserved = 0; | |
1386 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; | |
1387 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; | |
1388 | ||
1389 | xfrm_state_put(x); | |
1390 | ||
1391 | pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk); | |
1392 | ||
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1397 | { | |
1398 | struct xfrm_state *x; | |
1399 | ||
1400 | if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8) | |
1401 | return -EOPNOTSUPP; | |
1402 | ||
1403 | if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0) | |
1404 | return 0; | |
1405 | ||
1406 | x = xfrm_find_acq_byseq(hdr->sadb_msg_seq); | |
1407 | if (x == NULL) | |
1408 | return 0; | |
1409 | ||
1410 | spin_lock_bh(&x->lock); | |
1411 | if (x->km.state == XFRM_STATE_ACQ) { | |
1412 | x->km.state = XFRM_STATE_ERROR; | |
1413 | wake_up(&km_waitq); | |
1414 | } | |
1415 | spin_unlock_bh(&x->lock); | |
1416 | xfrm_state_put(x); | |
1417 | return 0; | |
1418 | } | |
1419 | ||
26b15dad JHS |
1420 | static inline int event2poltype(int event) |
1421 | { | |
1422 | switch (event) { | |
f60f6b8f | 1423 | case XFRM_MSG_DELPOLICY: |
26b15dad | 1424 | return SADB_X_SPDDELETE; |
f60f6b8f | 1425 | case XFRM_MSG_NEWPOLICY: |
26b15dad | 1426 | return SADB_X_SPDADD; |
f60f6b8f | 1427 | case XFRM_MSG_UPDPOLICY: |
26b15dad | 1428 | return SADB_X_SPDUPDATE; |
f60f6b8f | 1429 | case XFRM_MSG_POLEXPIRE: |
26b15dad JHS |
1430 | // return SADB_X_SPDEXPIRE; |
1431 | default: | |
1432 | printk("pfkey: Unknown policy event %d\n", event); | |
1433 | break; | |
1434 | } | |
1435 | ||
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | static inline int event2keytype(int event) | |
1440 | { | |
1441 | switch (event) { | |
f60f6b8f | 1442 | case XFRM_MSG_DELSA: |
26b15dad | 1443 | return SADB_DELETE; |
f60f6b8f | 1444 | case XFRM_MSG_NEWSA: |
26b15dad | 1445 | return SADB_ADD; |
f60f6b8f | 1446 | case XFRM_MSG_UPDSA: |
26b15dad | 1447 | return SADB_UPDATE; |
f60f6b8f | 1448 | case XFRM_MSG_EXPIRE: |
26b15dad JHS |
1449 | return SADB_EXPIRE; |
1450 | default: | |
1451 | printk("pfkey: Unknown SA event %d\n", event); | |
1452 | break; | |
1453 | } | |
1454 | ||
1455 | return 0; | |
1456 | } | |
1457 | ||
1458 | /* ADD/UPD/DEL */ | |
1459 | static int key_notify_sa(struct xfrm_state *x, struct km_event *c) | |
1460 | { | |
1461 | struct sk_buff *skb; | |
1462 | struct sadb_msg *hdr; | |
26b15dad | 1463 | |
050f009e | 1464 | skb = pfkey_xfrm_state2msg(x); |
26b15dad JHS |
1465 | |
1466 | if (IS_ERR(skb)) | |
1467 | return PTR_ERR(skb); | |
1468 | ||
1469 | hdr = (struct sadb_msg *) skb->data; | |
1470 | hdr->sadb_msg_version = PF_KEY_V2; | |
1471 | hdr->sadb_msg_type = event2keytype(c->event); | |
1472 | hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); | |
1473 | hdr->sadb_msg_errno = 0; | |
1474 | hdr->sadb_msg_reserved = 0; | |
1475 | hdr->sadb_msg_seq = c->seq; | |
1476 | hdr->sadb_msg_pid = c->pid; | |
1477 | ||
1478 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL); | |
1479 | ||
1480 | return 0; | |
1481 | } | |
1da177e4 LT |
1482 | |
1483 | static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1484 | { | |
1da177e4 LT |
1485 | struct xfrm_state *x; |
1486 | int err; | |
26b15dad | 1487 | struct km_event c; |
1da177e4 | 1488 | |
1da177e4 LT |
1489 | x = pfkey_msg2xfrm_state(hdr, ext_hdrs); |
1490 | if (IS_ERR(x)) | |
1491 | return PTR_ERR(x); | |
1492 | ||
26b15dad | 1493 | xfrm_state_hold(x); |
1da177e4 LT |
1494 | if (hdr->sadb_msg_type == SADB_ADD) |
1495 | err = xfrm_state_add(x); | |
1496 | else | |
1497 | err = xfrm_state_update(x); | |
1498 | ||
ab5f5e8b | 1499 | xfrm_audit_state_add(x, err ? 0 : 1, |
2532386f EP |
1500 | audit_get_loginuid(current), |
1501 | audit_get_sessionid(current), 0); | |
161a09e7 | 1502 | |
1da177e4 LT |
1503 | if (err < 0) { |
1504 | x->km.state = XFRM_STATE_DEAD; | |
21380b81 | 1505 | __xfrm_state_put(x); |
7d6dfe1f | 1506 | goto out; |
1da177e4 LT |
1507 | } |
1508 | ||
26b15dad | 1509 | if (hdr->sadb_msg_type == SADB_ADD) |
f60f6b8f | 1510 | c.event = XFRM_MSG_NEWSA; |
26b15dad | 1511 | else |
f60f6b8f | 1512 | c.event = XFRM_MSG_UPDSA; |
26b15dad JHS |
1513 | c.seq = hdr->sadb_msg_seq; |
1514 | c.pid = hdr->sadb_msg_pid; | |
1515 | km_state_notify(x, &c); | |
7d6dfe1f | 1516 | out: |
26b15dad | 1517 | xfrm_state_put(x); |
26b15dad | 1518 | return err; |
1da177e4 LT |
1519 | } |
1520 | ||
1521 | static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1522 | { | |
1523 | struct xfrm_state *x; | |
26b15dad JHS |
1524 | struct km_event c; |
1525 | int err; | |
1da177e4 LT |
1526 | |
1527 | if (!ext_hdrs[SADB_EXT_SA-1] || | |
1528 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
1529 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) | |
1530 | return -EINVAL; | |
1531 | ||
1532 | x = pfkey_xfrm_state_lookup(hdr, ext_hdrs); | |
1533 | if (x == NULL) | |
1534 | return -ESRCH; | |
1535 | ||
c8c05a8e CZ |
1536 | if ((err = security_xfrm_state_delete(x))) |
1537 | goto out; | |
1538 | ||
1da177e4 | 1539 | if (xfrm_state_kern(x)) { |
c8c05a8e CZ |
1540 | err = -EPERM; |
1541 | goto out; | |
1da177e4 | 1542 | } |
161a09e7 | 1543 | |
26b15dad | 1544 | err = xfrm_state_delete(x); |
161a09e7 | 1545 | |
c8c05a8e CZ |
1546 | if (err < 0) |
1547 | goto out; | |
1da177e4 | 1548 | |
26b15dad JHS |
1549 | c.seq = hdr->sadb_msg_seq; |
1550 | c.pid = hdr->sadb_msg_pid; | |
f60f6b8f | 1551 | c.event = XFRM_MSG_DELSA; |
26b15dad | 1552 | km_state_notify(x, &c); |
c8c05a8e | 1553 | out: |
ab5f5e8b | 1554 | xfrm_audit_state_delete(x, err ? 0 : 1, |
2532386f EP |
1555 | audit_get_loginuid(current), |
1556 | audit_get_sessionid(current), 0); | |
26b15dad | 1557 | xfrm_state_put(x); |
1da177e4 | 1558 | |
26b15dad | 1559 | return err; |
1da177e4 LT |
1560 | } |
1561 | ||
1562 | static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1563 | { | |
1564 | __u8 proto; | |
1565 | struct sk_buff *out_skb; | |
1566 | struct sadb_msg *out_hdr; | |
1567 | struct xfrm_state *x; | |
1568 | ||
1569 | if (!ext_hdrs[SADB_EXT_SA-1] || | |
1570 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
1571 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) | |
1572 | return -EINVAL; | |
1573 | ||
1574 | x = pfkey_xfrm_state_lookup(hdr, ext_hdrs); | |
1575 | if (x == NULL) | |
1576 | return -ESRCH; | |
1577 | ||
050f009e | 1578 | out_skb = pfkey_xfrm_state2msg(x); |
1da177e4 LT |
1579 | proto = x->id.proto; |
1580 | xfrm_state_put(x); | |
1581 | if (IS_ERR(out_skb)) | |
1582 | return PTR_ERR(out_skb); | |
1583 | ||
1584 | out_hdr = (struct sadb_msg *) out_skb->data; | |
1585 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; | |
435000be | 1586 | out_hdr->sadb_msg_type = SADB_GET; |
1da177e4 LT |
1587 | out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); |
1588 | out_hdr->sadb_msg_errno = 0; | |
1589 | out_hdr->sadb_msg_reserved = 0; | |
1590 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; | |
1591 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; | |
1592 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk); | |
1593 | ||
1594 | return 0; | |
1595 | } | |
1596 | ||
00fa0233 | 1597 | static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, |
dd0fc66f | 1598 | gfp_t allocation) |
1da177e4 LT |
1599 | { |
1600 | struct sk_buff *skb; | |
1601 | struct sadb_msg *hdr; | |
1602 | int len, auth_len, enc_len, i; | |
1603 | ||
1604 | auth_len = xfrm_count_auth_supported(); | |
1605 | if (auth_len) { | |
1606 | auth_len *= sizeof(struct sadb_alg); | |
1607 | auth_len += sizeof(struct sadb_supported); | |
1608 | } | |
8ff24541 | 1609 | |
1da177e4 LT |
1610 | enc_len = xfrm_count_enc_supported(); |
1611 | if (enc_len) { | |
1612 | enc_len *= sizeof(struct sadb_alg); | |
1613 | enc_len += sizeof(struct sadb_supported); | |
1614 | } | |
8ff24541 | 1615 | |
1da177e4 LT |
1616 | len = enc_len + auth_len + sizeof(struct sadb_msg); |
1617 | ||
1618 | skb = alloc_skb(len + 16, allocation); | |
1619 | if (!skb) | |
1620 | goto out_put_algs; | |
1621 | ||
1622 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr)); | |
1623 | pfkey_hdr_dup(hdr, orig); | |
1624 | hdr->sadb_msg_errno = 0; | |
1625 | hdr->sadb_msg_len = len / sizeof(uint64_t); | |
1626 | ||
1627 | if (auth_len) { | |
1628 | struct sadb_supported *sp; | |
1629 | struct sadb_alg *ap; | |
1630 | ||
1631 | sp = (struct sadb_supported *) skb_put(skb, auth_len); | |
1632 | ap = (struct sadb_alg *) (sp + 1); | |
1633 | ||
1634 | sp->sadb_supported_len = auth_len / sizeof(uint64_t); | |
1635 | sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; | |
1636 | ||
1637 | for (i = 0; ; i++) { | |
1638 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); | |
1639 | if (!aalg) | |
1640 | break; | |
1641 | if (aalg->available) | |
1642 | *ap++ = aalg->desc; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | if (enc_len) { | |
1647 | struct sadb_supported *sp; | |
1648 | struct sadb_alg *ap; | |
1649 | ||
1650 | sp = (struct sadb_supported *) skb_put(skb, enc_len); | |
1651 | ap = (struct sadb_alg *) (sp + 1); | |
1652 | ||
1653 | sp->sadb_supported_len = enc_len / sizeof(uint64_t); | |
1654 | sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT; | |
1655 | ||
1656 | for (i = 0; ; i++) { | |
1657 | struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); | |
1658 | if (!ealg) | |
1659 | break; | |
1660 | if (ealg->available) | |
1661 | *ap++ = ealg->desc; | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | out_put_algs: | |
1666 | return skb; | |
1667 | } | |
1668 | ||
1669 | static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1670 | { | |
1671 | struct pfkey_sock *pfk = pfkey_sk(sk); | |
1672 | struct sk_buff *supp_skb; | |
1673 | ||
1674 | if (hdr->sadb_msg_satype > SADB_SATYPE_MAX) | |
1675 | return -EINVAL; | |
1676 | ||
1677 | if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) { | |
1678 | if (pfk->registered&(1<<hdr->sadb_msg_satype)) | |
1679 | return -EEXIST; | |
1680 | pfk->registered |= (1<<hdr->sadb_msg_satype); | |
1681 | } | |
1682 | ||
1683 | xfrm_probe_algs(); | |
8ff24541 | 1684 | |
1da177e4 LT |
1685 | supp_skb = compose_sadb_supported(hdr, GFP_KERNEL); |
1686 | if (!supp_skb) { | |
1687 | if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) | |
1688 | pfk->registered &= ~(1<<hdr->sadb_msg_satype); | |
1689 | ||
1690 | return -ENOBUFS; | |
1691 | } | |
1692 | ||
1693 | pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk); | |
1694 | ||
1695 | return 0; | |
1696 | } | |
1697 | ||
26b15dad JHS |
1698 | static int key_notify_sa_flush(struct km_event *c) |
1699 | { | |
1700 | struct sk_buff *skb; | |
1701 | struct sadb_msg *hdr; | |
1702 | ||
1703 | skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); | |
1704 | if (!skb) | |
1705 | return -ENOBUFS; | |
1706 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); | |
bf08867f | 1707 | hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto); |
151bb0ff | 1708 | hdr->sadb_msg_type = SADB_FLUSH; |
26b15dad JHS |
1709 | hdr->sadb_msg_seq = c->seq; |
1710 | hdr->sadb_msg_pid = c->pid; | |
1711 | hdr->sadb_msg_version = PF_KEY_V2; | |
1712 | hdr->sadb_msg_errno = (uint8_t) 0; | |
1713 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); | |
1714 | ||
1715 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL); | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
1da177e4 LT |
1720 | static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) |
1721 | { | |
1722 | unsigned proto; | |
26b15dad | 1723 | struct km_event c; |
161a09e7 | 1724 | struct xfrm_audit audit_info; |
4aa2e62c | 1725 | int err; |
1da177e4 LT |
1726 | |
1727 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); | |
1728 | if (proto == 0) | |
1729 | return -EINVAL; | |
1730 | ||
0c11b942 | 1731 | audit_info.loginuid = audit_get_loginuid(current); |
2532386f | 1732 | audit_info.sessionid = audit_get_sessionid(current); |
161a09e7 | 1733 | audit_info.secid = 0; |
4aa2e62c JL |
1734 | err = xfrm_state_flush(proto, &audit_info); |
1735 | if (err) | |
1736 | return err; | |
bf08867f | 1737 | c.data.proto = proto; |
26b15dad JHS |
1738 | c.seq = hdr->sadb_msg_seq; |
1739 | c.pid = hdr->sadb_msg_pid; | |
f60f6b8f | 1740 | c.event = XFRM_MSG_FLUSHSA; |
26b15dad | 1741 | km_state_notify(NULL, &c); |
1da177e4 LT |
1742 | |
1743 | return 0; | |
1744 | } | |
1745 | ||
1da177e4 LT |
1746 | static int dump_sa(struct xfrm_state *x, int count, void *ptr) |
1747 | { | |
83321d6b | 1748 | struct pfkey_sock *pfk = ptr; |
1da177e4 LT |
1749 | struct sk_buff *out_skb; |
1750 | struct sadb_msg *out_hdr; | |
1751 | ||
83321d6b TT |
1752 | if (!pfkey_can_dump(&pfk->sk)) |
1753 | return -ENOBUFS; | |
1754 | ||
050f009e | 1755 | out_skb = pfkey_xfrm_state2msg(x); |
1da177e4 LT |
1756 | if (IS_ERR(out_skb)) |
1757 | return PTR_ERR(out_skb); | |
1758 | ||
1759 | out_hdr = (struct sadb_msg *) out_skb->data; | |
83321d6b | 1760 | out_hdr->sadb_msg_version = pfk->dump.msg_version; |
1da177e4 LT |
1761 | out_hdr->sadb_msg_type = SADB_DUMP; |
1762 | out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); | |
1763 | out_hdr->sadb_msg_errno = 0; | |
1764 | out_hdr->sadb_msg_reserved = 0; | |
12a169e7 | 1765 | out_hdr->sadb_msg_seq = count + 1; |
83321d6b | 1766 | out_hdr->sadb_msg_pid = pfk->dump.msg_pid; |
12a169e7 HX |
1767 | |
1768 | if (pfk->dump.skb) | |
1769 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, | |
1770 | &pfk->sk); | |
1771 | pfk->dump.skb = out_skb; | |
1772 | ||
1da177e4 LT |
1773 | return 0; |
1774 | } | |
1775 | ||
83321d6b TT |
1776 | static int pfkey_dump_sa(struct pfkey_sock *pfk) |
1777 | { | |
1778 | return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk); | |
1779 | } | |
1780 | ||
1781 | static void pfkey_dump_sa_done(struct pfkey_sock *pfk) | |
1782 | { | |
1783 | xfrm_state_walk_done(&pfk->dump.u.state); | |
1784 | } | |
1785 | ||
1da177e4 LT |
1786 | static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) |
1787 | { | |
1788 | u8 proto; | |
83321d6b TT |
1789 | struct pfkey_sock *pfk = pfkey_sk(sk); |
1790 | ||
1791 | if (pfk->dump.dump != NULL) | |
1792 | return -EBUSY; | |
1da177e4 LT |
1793 | |
1794 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); | |
1795 | if (proto == 0) | |
1796 | return -EINVAL; | |
1797 | ||
83321d6b TT |
1798 | pfk->dump.msg_version = hdr->sadb_msg_version; |
1799 | pfk->dump.msg_pid = hdr->sadb_msg_pid; | |
1800 | pfk->dump.dump = pfkey_dump_sa; | |
1801 | pfk->dump.done = pfkey_dump_sa_done; | |
1802 | xfrm_state_walk_init(&pfk->dump.u.state, proto); | |
4c563f76 | 1803 | |
83321d6b | 1804 | return pfkey_do_dump(pfk); |
1da177e4 LT |
1805 | } |
1806 | ||
1807 | static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
1808 | { | |
1809 | struct pfkey_sock *pfk = pfkey_sk(sk); | |
1810 | int satype = hdr->sadb_msg_satype; | |
1811 | ||
1812 | if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) { | |
1813 | /* XXX we mangle packet... */ | |
1814 | hdr->sadb_msg_errno = 0; | |
1815 | if (satype != 0 && satype != 1) | |
1816 | return -EINVAL; | |
1817 | pfk->promisc = satype; | |
1818 | } | |
1819 | pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL); | |
1820 | return 0; | |
1821 | } | |
1822 | ||
1823 | static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr) | |
1824 | { | |
1825 | int i; | |
1826 | u32 reqid = *(u32*)ptr; | |
1827 | ||
1828 | for (i=0; i<xp->xfrm_nr; i++) { | |
1829 | if (xp->xfrm_vec[i].reqid == reqid) | |
1830 | return -EEXIST; | |
1831 | } | |
1832 | return 0; | |
1833 | } | |
1834 | ||
1835 | static u32 gen_reqid(void) | |
1836 | { | |
4c563f76 | 1837 | struct xfrm_policy_walk walk; |
1da177e4 | 1838 | u32 start; |
4c563f76 | 1839 | int rc; |
1da177e4 LT |
1840 | static u32 reqid = IPSEC_MANUAL_REQID_MAX; |
1841 | ||
1842 | start = reqid; | |
1843 | do { | |
1844 | ++reqid; | |
1845 | if (reqid == 0) | |
1846 | reqid = IPSEC_MANUAL_REQID_MAX+1; | |
4c563f76 TT |
1847 | xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN); |
1848 | rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid); | |
1849 | xfrm_policy_walk_done(&walk); | |
1850 | if (rc != -EEXIST) | |
1da177e4 LT |
1851 | return reqid; |
1852 | } while (reqid != start); | |
1853 | return 0; | |
1854 | } | |
1855 | ||
1856 | static int | |
1857 | parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq) | |
1858 | { | |
1859 | struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr; | |
55569ce2 | 1860 | int mode; |
1da177e4 LT |
1861 | |
1862 | if (xp->xfrm_nr >= XFRM_MAX_DEPTH) | |
1863 | return -ELOOP; | |
1864 | ||
1865 | if (rq->sadb_x_ipsecrequest_mode == 0) | |
1866 | return -EINVAL; | |
1867 | ||
1868 | t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */ | |
55569ce2 KM |
1869 | if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0) |
1870 | return -EINVAL; | |
1871 | t->mode = mode; | |
1da177e4 LT |
1872 | if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE) |
1873 | t->optional = 1; | |
1874 | else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) { | |
1875 | t->reqid = rq->sadb_x_ipsecrequest_reqid; | |
1876 | if (t->reqid > IPSEC_MANUAL_REQID_MAX) | |
1877 | t->reqid = 0; | |
1878 | if (!t->reqid && !(t->reqid = gen_reqid())) | |
1879 | return -ENOBUFS; | |
1880 | } | |
1881 | ||
1882 | /* addresses present only in tunnel mode */ | |
7e49e6de | 1883 | if (t->mode == XFRM_MODE_TUNNEL) { |
5f95ac91 YH |
1884 | u8 *sa = (u8 *) (rq + 1); |
1885 | int family, socklen; | |
1886 | ||
1887 | family = pfkey_sockaddr_extract((struct sockaddr *)sa, | |
1888 | &t->saddr); | |
1889 | if (!family) | |
1da177e4 | 1890 | return -EINVAL; |
5f95ac91 YH |
1891 | |
1892 | socklen = pfkey_sockaddr_len(family); | |
1893 | if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen), | |
1894 | &t->id.daddr) != family) | |
1895 | return -EINVAL; | |
1896 | t->encap_family = family; | |
2718aa7c MK |
1897 | } else |
1898 | t->encap_family = xp->family; | |
1899 | ||
1da177e4 | 1900 | /* No way to set this via kame pfkey */ |
c5d18e98 | 1901 | t->allalgs = 1; |
1da177e4 LT |
1902 | xp->xfrm_nr++; |
1903 | return 0; | |
1904 | } | |
1905 | ||
1906 | static int | |
1907 | parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol) | |
1908 | { | |
1909 | int err; | |
1910 | int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy); | |
1911 | struct sadb_x_ipsecrequest *rq = (void*)(pol+1); | |
1912 | ||
1913 | while (len >= sizeof(struct sadb_x_ipsecrequest)) { | |
1914 | if ((err = parse_ipsecrequest(xp, rq)) < 0) | |
1915 | return err; | |
1916 | len -= rq->sadb_x_ipsecrequest_len; | |
1917 | rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len); | |
1918 | } | |
1919 | return 0; | |
1920 | } | |
1921 | ||
df71837d TJ |
1922 | static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp) |
1923 | { | |
1924 | struct xfrm_sec_ctx *xfrm_ctx = xp->security; | |
1925 | ||
1926 | if (xfrm_ctx) { | |
1927 | int len = sizeof(struct sadb_x_sec_ctx); | |
1928 | len += xfrm_ctx->ctx_len; | |
1929 | return PFKEY_ALIGN8(len); | |
1930 | } | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1da177e4 LT |
1934 | static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp) |
1935 | { | |
2718aa7c | 1936 | struct xfrm_tmpl *t; |
1da177e4 | 1937 | int sockaddr_size = pfkey_sockaddr_size(xp->family); |
2718aa7c MK |
1938 | int socklen = 0; |
1939 | int i; | |
1940 | ||
1941 | for (i=0; i<xp->xfrm_nr; i++) { | |
1942 | t = xp->xfrm_vec + i; | |
9e8b4ed8 | 1943 | socklen += pfkey_sockaddr_len(t->encap_family); |
2718aa7c | 1944 | } |
1da177e4 LT |
1945 | |
1946 | return sizeof(struct sadb_msg) + | |
1947 | (sizeof(struct sadb_lifetime) * 3) + | |
8ff24541 | 1948 | (sizeof(struct sadb_address) * 2) + |
1da177e4 LT |
1949 | (sockaddr_size * 2) + |
1950 | sizeof(struct sadb_x_policy) + | |
2718aa7c MK |
1951 | (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) + |
1952 | (socklen * 2) + | |
df71837d | 1953 | pfkey_xfrm_policy2sec_ctx_size(xp); |
1da177e4 LT |
1954 | } |
1955 | ||
1956 | static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp) | |
1957 | { | |
1958 | struct sk_buff *skb; | |
1959 | int size; | |
1960 | ||
1961 | size = pfkey_xfrm_policy2msg_size(xp); | |
1962 | ||
1963 | skb = alloc_skb(size + 16, GFP_ATOMIC); | |
1964 | if (skb == NULL) | |
1965 | return ERR_PTR(-ENOBUFS); | |
1966 | ||
1967 | return skb; | |
1968 | } | |
1969 | ||
55569ce2 | 1970 | static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir) |
1da177e4 LT |
1971 | { |
1972 | struct sadb_msg *hdr; | |
1973 | struct sadb_address *addr; | |
1974 | struct sadb_lifetime *lifetime; | |
1975 | struct sadb_x_policy *pol; | |
df71837d TJ |
1976 | struct sadb_x_sec_ctx *sec_ctx; |
1977 | struct xfrm_sec_ctx *xfrm_ctx; | |
1da177e4 LT |
1978 | int i; |
1979 | int size; | |
1980 | int sockaddr_size = pfkey_sockaddr_size(xp->family); | |
9e8b4ed8 | 1981 | int socklen = pfkey_sockaddr_len(xp->family); |
1da177e4 LT |
1982 | |
1983 | size = pfkey_xfrm_policy2msg_size(xp); | |
1984 | ||
1985 | /* call should fill header later */ | |
1986 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); | |
1987 | memset(hdr, 0, size); /* XXX do we need this ? */ | |
1988 | ||
1989 | /* src address */ | |
8ff24541 | 1990 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 | 1991 | sizeof(struct sadb_address)+sockaddr_size); |
8ff24541 | 1992 | addr->sadb_address_len = |
1da177e4 LT |
1993 | (sizeof(struct sadb_address)+sockaddr_size)/ |
1994 | sizeof(uint64_t); | |
1995 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; | |
1996 | addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); | |
1997 | addr->sadb_address_prefixlen = xp->selector.prefixlen_s; | |
1998 | addr->sadb_address_reserved = 0; | |
e5b56652 YH |
1999 | if (!pfkey_sockaddr_fill(&xp->selector.saddr, |
2000 | xp->selector.sport, | |
2001 | (struct sockaddr *) (addr + 1), | |
2002 | xp->family)) | |
1da177e4 LT |
2003 | BUG(); |
2004 | ||
2005 | /* dst address */ | |
8ff24541 | 2006 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 LT |
2007 | sizeof(struct sadb_address)+sockaddr_size); |
2008 | addr->sadb_address_len = | |
2009 | (sizeof(struct sadb_address)+sockaddr_size)/ | |
2010 | sizeof(uint64_t); | |
2011 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; | |
2012 | addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); | |
8ff24541 | 2013 | addr->sadb_address_prefixlen = xp->selector.prefixlen_d; |
1da177e4 | 2014 | addr->sadb_address_reserved = 0; |
e5b56652 YH |
2015 | |
2016 | pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport, | |
2017 | (struct sockaddr *) (addr + 1), | |
2018 | xp->family); | |
1da177e4 LT |
2019 | |
2020 | /* hard time */ | |
8ff24541 | 2021 | lifetime = (struct sadb_lifetime *) skb_put(skb, |
1da177e4 LT |
2022 | sizeof(struct sadb_lifetime)); |
2023 | lifetime->sadb_lifetime_len = | |
2024 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
2025 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; | |
2026 | lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit); | |
2027 | lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit); | |
2028 | lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds; | |
2029 | lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds; | |
2030 | /* soft time */ | |
8ff24541 | 2031 | lifetime = (struct sadb_lifetime *) skb_put(skb, |
1da177e4 LT |
2032 | sizeof(struct sadb_lifetime)); |
2033 | lifetime->sadb_lifetime_len = | |
2034 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
2035 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; | |
2036 | lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit); | |
2037 | lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit); | |
2038 | lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds; | |
2039 | lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds; | |
2040 | /* current time */ | |
8ff24541 | 2041 | lifetime = (struct sadb_lifetime *) skb_put(skb, |
1da177e4 LT |
2042 | sizeof(struct sadb_lifetime)); |
2043 | lifetime->sadb_lifetime_len = | |
2044 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); | |
2045 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; | |
2046 | lifetime->sadb_lifetime_allocations = xp->curlft.packets; | |
2047 | lifetime->sadb_lifetime_bytes = xp->curlft.bytes; | |
2048 | lifetime->sadb_lifetime_addtime = xp->curlft.add_time; | |
2049 | lifetime->sadb_lifetime_usetime = xp->curlft.use_time; | |
2050 | ||
2051 | pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy)); | |
2052 | pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); | |
2053 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; | |
2054 | pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD; | |
2055 | if (xp->action == XFRM_POLICY_ALLOW) { | |
2056 | if (xp->xfrm_nr) | |
2057 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; | |
2058 | else | |
2059 | pol->sadb_x_policy_type = IPSEC_POLICY_NONE; | |
2060 | } | |
2061 | pol->sadb_x_policy_dir = dir+1; | |
2062 | pol->sadb_x_policy_id = xp->index; | |
2063 | pol->sadb_x_policy_priority = xp->priority; | |
2064 | ||
2065 | for (i=0; i<xp->xfrm_nr; i++) { | |
2066 | struct sadb_x_ipsecrequest *rq; | |
2067 | struct xfrm_tmpl *t = xp->xfrm_vec + i; | |
2068 | int req_size; | |
55569ce2 | 2069 | int mode; |
1da177e4 LT |
2070 | |
2071 | req_size = sizeof(struct sadb_x_ipsecrequest); | |
e5b56652 YH |
2072 | if (t->mode == XFRM_MODE_TUNNEL) { |
2073 | socklen = pfkey_sockaddr_len(t->encap_family); | |
2074 | req_size += socklen * 2; | |
2075 | } else { | |
1da177e4 | 2076 | size -= 2*socklen; |
e5b56652 YH |
2077 | socklen = 0; |
2078 | } | |
1da177e4 LT |
2079 | rq = (void*)skb_put(skb, req_size); |
2080 | pol->sadb_x_policy_len += req_size/8; | |
2081 | memset(rq, 0, sizeof(*rq)); | |
2082 | rq->sadb_x_ipsecrequest_len = req_size; | |
2083 | rq->sadb_x_ipsecrequest_proto = t->id.proto; | |
55569ce2 KM |
2084 | if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0) |
2085 | return -EINVAL; | |
fefaa75e | 2086 | rq->sadb_x_ipsecrequest_mode = mode; |
1da177e4 LT |
2087 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE; |
2088 | if (t->reqid) | |
2089 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE; | |
2090 | if (t->optional) | |
2091 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE; | |
2092 | rq->sadb_x_ipsecrequest_reqid = t->reqid; | |
e5b56652 | 2093 | |
7e49e6de | 2094 | if (t->mode == XFRM_MODE_TUNNEL) { |
e5b56652 YH |
2095 | u8 *sa = (void *)(rq + 1); |
2096 | pfkey_sockaddr_fill(&t->saddr, 0, | |
2097 | (struct sockaddr *)sa, | |
2098 | t->encap_family); | |
2099 | pfkey_sockaddr_fill(&t->id.daddr, 0, | |
2100 | (struct sockaddr *) (sa + socklen), | |
2101 | t->encap_family); | |
1da177e4 LT |
2102 | } |
2103 | } | |
df71837d TJ |
2104 | |
2105 | /* security context */ | |
2106 | if ((xfrm_ctx = xp->security)) { | |
2107 | int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp); | |
2108 | ||
2109 | sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size); | |
2110 | sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t); | |
2111 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; | |
2112 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; | |
2113 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; | |
2114 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; | |
2115 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, | |
2116 | xfrm_ctx->ctx_len); | |
2117 | } | |
2118 | ||
1da177e4 LT |
2119 | hdr->sadb_msg_len = size / sizeof(uint64_t); |
2120 | hdr->sadb_msg_reserved = atomic_read(&xp->refcnt); | |
55569ce2 KM |
2121 | |
2122 | return 0; | |
1da177e4 LT |
2123 | } |
2124 | ||
26b15dad JHS |
2125 | static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
2126 | { | |
2127 | struct sk_buff *out_skb; | |
2128 | struct sadb_msg *out_hdr; | |
2129 | int err; | |
2130 | ||
2131 | out_skb = pfkey_xfrm_policy2msg_prep(xp); | |
2132 | if (IS_ERR(out_skb)) { | |
2133 | err = PTR_ERR(out_skb); | |
2134 | goto out; | |
2135 | } | |
55569ce2 KM |
2136 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
2137 | if (err < 0) | |
2138 | return err; | |
26b15dad JHS |
2139 | |
2140 | out_hdr = (struct sadb_msg *) out_skb->data; | |
2141 | out_hdr->sadb_msg_version = PF_KEY_V2; | |
2142 | ||
f60f6b8f | 2143 | if (c->data.byid && c->event == XFRM_MSG_DELPOLICY) |
26b15dad JHS |
2144 | out_hdr->sadb_msg_type = SADB_X_SPDDELETE2; |
2145 | else | |
2146 | out_hdr->sadb_msg_type = event2poltype(c->event); | |
2147 | out_hdr->sadb_msg_errno = 0; | |
2148 | out_hdr->sadb_msg_seq = c->seq; | |
2149 | out_hdr->sadb_msg_pid = c->pid; | |
2150 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL); | |
2151 | out: | |
2152 | return 0; | |
2153 | ||
2154 | } | |
2155 | ||
1da177e4 LT |
2156 | static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) |
2157 | { | |
df71837d | 2158 | int err = 0; |
1da177e4 LT |
2159 | struct sadb_lifetime *lifetime; |
2160 | struct sadb_address *sa; | |
2161 | struct sadb_x_policy *pol; | |
2162 | struct xfrm_policy *xp; | |
26b15dad | 2163 | struct km_event c; |
df71837d | 2164 | struct sadb_x_sec_ctx *sec_ctx; |
1da177e4 LT |
2165 | |
2166 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
2167 | ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || | |
2168 | !ext_hdrs[SADB_X_EXT_POLICY-1]) | |
2169 | return -EINVAL; | |
2170 | ||
2171 | pol = ext_hdrs[SADB_X_EXT_POLICY-1]; | |
2172 | if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC) | |
2173 | return -EINVAL; | |
2174 | if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) | |
2175 | return -EINVAL; | |
2176 | ||
2177 | xp = xfrm_policy_alloc(GFP_KERNEL); | |
2178 | if (xp == NULL) | |
2179 | return -ENOBUFS; | |
2180 | ||
2181 | xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? | |
2182 | XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); | |
2183 | xp->priority = pol->sadb_x_policy_priority; | |
2184 | ||
8ff24541 | 2185 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
1da177e4 LT |
2186 | xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr); |
2187 | if (!xp->family) { | |
2188 | err = -EINVAL; | |
2189 | goto out; | |
2190 | } | |
2191 | xp->selector.family = xp->family; | |
2192 | xp->selector.prefixlen_s = sa->sadb_address_prefixlen; | |
2193 | xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2194 | xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port; | |
2195 | if (xp->selector.sport) | |
8f83f23e | 2196 | xp->selector.sport_mask = htons(0xffff); |
1da177e4 | 2197 | |
8ff24541 | 2198 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], |
1da177e4 LT |
2199 | pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr); |
2200 | xp->selector.prefixlen_d = sa->sadb_address_prefixlen; | |
2201 | ||
2202 | /* Amusing, we set this twice. KAME apps appear to set same value | |
2203 | * in both addresses. | |
2204 | */ | |
2205 | xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2206 | ||
2207 | xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port; | |
2208 | if (xp->selector.dport) | |
8f83f23e | 2209 | xp->selector.dport_mask = htons(0xffff); |
1da177e4 | 2210 | |
df71837d TJ |
2211 | sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1]; |
2212 | if (sec_ctx != NULL) { | |
2213 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); | |
2214 | ||
2215 | if (!uctx) { | |
2216 | err = -ENOBUFS; | |
2217 | goto out; | |
2218 | } | |
2219 | ||
03e1ad7b | 2220 | err = security_xfrm_policy_alloc(&xp->security, uctx); |
df71837d TJ |
2221 | kfree(uctx); |
2222 | ||
2223 | if (err) | |
2224 | goto out; | |
2225 | } | |
2226 | ||
1da177e4 LT |
2227 | xp->lft.soft_byte_limit = XFRM_INF; |
2228 | xp->lft.hard_byte_limit = XFRM_INF; | |
2229 | xp->lft.soft_packet_limit = XFRM_INF; | |
2230 | xp->lft.hard_packet_limit = XFRM_INF; | |
2231 | if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) { | |
2232 | xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); | |
2233 | xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); | |
2234 | xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; | |
2235 | xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; | |
2236 | } | |
2237 | if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) { | |
2238 | xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); | |
2239 | xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); | |
2240 | xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; | |
2241 | xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; | |
2242 | } | |
2243 | xp->xfrm_nr = 0; | |
2244 | if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && | |
2245 | (err = parse_ipsecrequests(xp, pol)) < 0) | |
2246 | goto out; | |
2247 | ||
1da177e4 LT |
2248 | err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp, |
2249 | hdr->sadb_msg_type != SADB_X_SPDUPDATE); | |
df71837d | 2250 | |
ab5f5e8b | 2251 | xfrm_audit_policy_add(xp, err ? 0 : 1, |
2532386f EP |
2252 | audit_get_loginuid(current), |
2253 | audit_get_sessionid(current), 0); | |
161a09e7 | 2254 | |
df71837d TJ |
2255 | if (err) |
2256 | goto out; | |
1da177e4 | 2257 | |
26b15dad | 2258 | if (hdr->sadb_msg_type == SADB_X_SPDUPDATE) |
f60f6b8f | 2259 | c.event = XFRM_MSG_UPDPOLICY; |
8ff24541 | 2260 | else |
f60f6b8f | 2261 | c.event = XFRM_MSG_NEWPOLICY; |
1da177e4 | 2262 | |
26b15dad JHS |
2263 | c.seq = hdr->sadb_msg_seq; |
2264 | c.pid = hdr->sadb_msg_pid; | |
1da177e4 | 2265 | |
26b15dad JHS |
2266 | km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); |
2267 | xfrm_pol_put(xp); | |
1da177e4 LT |
2268 | return 0; |
2269 | ||
2270 | out: | |
12a169e7 | 2271 | xp->walk.dead = 1; |
64c31b3f | 2272 | xfrm_policy_destroy(xp); |
1da177e4 LT |
2273 | return err; |
2274 | } | |
2275 | ||
2276 | static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
2277 | { | |
2278 | int err; | |
2279 | struct sadb_address *sa; | |
2280 | struct sadb_x_policy *pol; | |
03e1ad7b | 2281 | struct xfrm_policy *xp; |
1da177e4 | 2282 | struct xfrm_selector sel; |
26b15dad | 2283 | struct km_event c; |
df71837d | 2284 | struct sadb_x_sec_ctx *sec_ctx; |
2db3e47e | 2285 | struct xfrm_sec_ctx *pol_ctx = NULL; |
1da177e4 LT |
2286 | |
2287 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], | |
2288 | ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || | |
2289 | !ext_hdrs[SADB_X_EXT_POLICY-1]) | |
2290 | return -EINVAL; | |
2291 | ||
2292 | pol = ext_hdrs[SADB_X_EXT_POLICY-1]; | |
2293 | if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) | |
2294 | return -EINVAL; | |
2295 | ||
2296 | memset(&sel, 0, sizeof(sel)); | |
2297 | ||
8ff24541 | 2298 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
1da177e4 LT |
2299 | sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); |
2300 | sel.prefixlen_s = sa->sadb_address_prefixlen; | |
2301 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2302 | sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port; | |
2303 | if (sel.sport) | |
8f83f23e | 2304 | sel.sport_mask = htons(0xffff); |
1da177e4 | 2305 | |
8ff24541 | 2306 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], |
1da177e4 LT |
2307 | pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); |
2308 | sel.prefixlen_d = sa->sadb_address_prefixlen; | |
2309 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2310 | sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port; | |
2311 | if (sel.dport) | |
8f83f23e | 2312 | sel.dport_mask = htons(0xffff); |
1da177e4 | 2313 | |
df71837d | 2314 | sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1]; |
df71837d TJ |
2315 | if (sec_ctx != NULL) { |
2316 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); | |
2317 | ||
2318 | if (!uctx) | |
2319 | return -ENOMEM; | |
2320 | ||
03e1ad7b | 2321 | err = security_xfrm_policy_alloc(&pol_ctx, uctx); |
df71837d | 2322 | kfree(uctx); |
df71837d TJ |
2323 | if (err) |
2324 | return err; | |
2db3e47e | 2325 | } |
161a09e7 | 2326 | |
03e1ad7b PM |
2327 | xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, |
2328 | pol->sadb_x_policy_dir - 1, &sel, pol_ctx, | |
2329 | 1, &err); | |
2330 | security_xfrm_policy_free(pol_ctx); | |
1da177e4 LT |
2331 | if (xp == NULL) |
2332 | return -ENOENT; | |
2333 | ||
ab5f5e8b | 2334 | xfrm_audit_policy_delete(xp, err ? 0 : 1, |
2532386f EP |
2335 | audit_get_loginuid(current), |
2336 | audit_get_sessionid(current), 0); | |
13fcfbb0 DM |
2337 | |
2338 | if (err) | |
c8c05a8e | 2339 | goto out; |
13fcfbb0 | 2340 | |
26b15dad JHS |
2341 | c.seq = hdr->sadb_msg_seq; |
2342 | c.pid = hdr->sadb_msg_pid; | |
f60f6b8f | 2343 | c.event = XFRM_MSG_DELPOLICY; |
26b15dad JHS |
2344 | km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); |
2345 | ||
c8c05a8e | 2346 | out: |
26b15dad JHS |
2347 | xfrm_pol_put(xp); |
2348 | return err; | |
2349 | } | |
2350 | ||
2351 | static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir) | |
2352 | { | |
2353 | int err; | |
2354 | struct sk_buff *out_skb; | |
2355 | struct sadb_msg *out_hdr; | |
2356 | err = 0; | |
2357 | ||
1da177e4 LT |
2358 | out_skb = pfkey_xfrm_policy2msg_prep(xp); |
2359 | if (IS_ERR(out_skb)) { | |
2360 | err = PTR_ERR(out_skb); | |
2361 | goto out; | |
2362 | } | |
55569ce2 KM |
2363 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
2364 | if (err < 0) | |
2365 | goto out; | |
1da177e4 LT |
2366 | |
2367 | out_hdr = (struct sadb_msg *) out_skb->data; | |
2368 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; | |
26b15dad | 2369 | out_hdr->sadb_msg_type = hdr->sadb_msg_type; |
1da177e4 LT |
2370 | out_hdr->sadb_msg_satype = 0; |
2371 | out_hdr->sadb_msg_errno = 0; | |
2372 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; | |
2373 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; | |
26b15dad | 2374 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk); |
1da177e4 LT |
2375 | err = 0; |
2376 | ||
2377 | out: | |
1da177e4 LT |
2378 | return err; |
2379 | } | |
2380 | ||
08de61be SS |
2381 | #ifdef CONFIG_NET_KEY_MIGRATE |
2382 | static int pfkey_sockaddr_pair_size(sa_family_t family) | |
2383 | { | |
9e8b4ed8 | 2384 | return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2); |
08de61be SS |
2385 | } |
2386 | ||
2387 | static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq, | |
2388 | xfrm_address_t *saddr, xfrm_address_t *daddr, | |
2389 | u16 *family) | |
2390 | { | |
5f95ac91 YH |
2391 | u8 *sa = (u8 *) (rq + 1); |
2392 | int af, socklen; | |
2393 | ||
08de61be | 2394 | if (rq->sadb_x_ipsecrequest_len < |
5f95ac91 | 2395 | pfkey_sockaddr_pair_size(((struct sockaddr *)sa)->sa_family)) |
08de61be SS |
2396 | return -EINVAL; |
2397 | ||
5f95ac91 YH |
2398 | af = pfkey_sockaddr_extract((struct sockaddr *) sa, |
2399 | saddr); | |
2400 | if (!af) | |
2401 | return -EINVAL; | |
2402 | ||
2403 | socklen = pfkey_sockaddr_len(af); | |
2404 | if (pfkey_sockaddr_extract((struct sockaddr *) (sa + socklen), | |
2405 | daddr) != af) | |
08de61be | 2406 | return -EINVAL; |
08de61be | 2407 | |
5f95ac91 | 2408 | *family = af; |
08de61be SS |
2409 | return 0; |
2410 | } | |
2411 | ||
2412 | static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len, | |
2413 | struct xfrm_migrate *m) | |
2414 | { | |
2415 | int err; | |
2416 | struct sadb_x_ipsecrequest *rq2; | |
55569ce2 | 2417 | int mode; |
08de61be SS |
2418 | |
2419 | if (len <= sizeof(struct sadb_x_ipsecrequest) || | |
2420 | len < rq1->sadb_x_ipsecrequest_len) | |
2421 | return -EINVAL; | |
2422 | ||
2423 | /* old endoints */ | |
2424 | err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr, | |
2425 | &m->old_family); | |
2426 | if (err) | |
2427 | return err; | |
2428 | ||
2429 | rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len); | |
2430 | len -= rq1->sadb_x_ipsecrequest_len; | |
2431 | ||
2432 | if (len <= sizeof(struct sadb_x_ipsecrequest) || | |
2433 | len < rq2->sadb_x_ipsecrequest_len) | |
2434 | return -EINVAL; | |
2435 | ||
2436 | /* new endpoints */ | |
2437 | err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr, | |
2438 | &m->new_family); | |
2439 | if (err) | |
2440 | return err; | |
2441 | ||
2442 | if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto || | |
2443 | rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode || | |
2444 | rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid) | |
2445 | return -EINVAL; | |
2446 | ||
2447 | m->proto = rq1->sadb_x_ipsecrequest_proto; | |
55569ce2 KM |
2448 | if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0) |
2449 | return -EINVAL; | |
2450 | m->mode = mode; | |
08de61be SS |
2451 | m->reqid = rq1->sadb_x_ipsecrequest_reqid; |
2452 | ||
2453 | return ((int)(rq1->sadb_x_ipsecrequest_len + | |
2454 | rq2->sadb_x_ipsecrequest_len)); | |
2455 | } | |
2456 | ||
2457 | static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, | |
2458 | struct sadb_msg *hdr, void **ext_hdrs) | |
2459 | { | |
2460 | int i, len, ret, err = -EINVAL; | |
2461 | u8 dir; | |
2462 | struct sadb_address *sa; | |
2463 | struct sadb_x_policy *pol; | |
2464 | struct sadb_x_ipsecrequest *rq; | |
2465 | struct xfrm_selector sel; | |
2466 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; | |
2467 | ||
2468 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1], | |
2469 | ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) || | |
2470 | !ext_hdrs[SADB_X_EXT_POLICY - 1]) { | |
2471 | err = -EINVAL; | |
2472 | goto out; | |
2473 | } | |
2474 | ||
2475 | pol = ext_hdrs[SADB_X_EXT_POLICY - 1]; | |
2476 | if (!pol) { | |
2477 | err = -EINVAL; | |
2478 | goto out; | |
2479 | } | |
2480 | ||
2481 | if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) { | |
2482 | err = -EINVAL; | |
2483 | goto out; | |
2484 | } | |
2485 | ||
2486 | dir = pol->sadb_x_policy_dir - 1; | |
2487 | memset(&sel, 0, sizeof(sel)); | |
2488 | ||
2489 | /* set source address info of selector */ | |
2490 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1]; | |
2491 | sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); | |
2492 | sel.prefixlen_s = sa->sadb_address_prefixlen; | |
2493 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2494 | sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port; | |
2495 | if (sel.sport) | |
582ee43d | 2496 | sel.sport_mask = htons(0xffff); |
08de61be SS |
2497 | |
2498 | /* set destination address info of selector */ | |
2499 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1], | |
2500 | pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); | |
2501 | sel.prefixlen_d = sa->sadb_address_prefixlen; | |
2502 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); | |
2503 | sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port; | |
2504 | if (sel.dport) | |
582ee43d | 2505 | sel.dport_mask = htons(0xffff); |
08de61be SS |
2506 | |
2507 | rq = (struct sadb_x_ipsecrequest *)(pol + 1); | |
2508 | ||
2509 | /* extract ipsecrequests */ | |
2510 | i = 0; | |
2511 | len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy); | |
2512 | ||
2513 | while (len > 0 && i < XFRM_MAX_DEPTH) { | |
2514 | ret = ipsecrequests_to_migrate(rq, len, &m[i]); | |
2515 | if (ret < 0) { | |
2516 | err = ret; | |
2517 | goto out; | |
2518 | } else { | |
2519 | rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret); | |
2520 | len -= ret; | |
2521 | i++; | |
2522 | } | |
2523 | } | |
2524 | ||
2525 | if (!i || len > 0) { | |
2526 | err = -EINVAL; | |
2527 | goto out; | |
2528 | } | |
2529 | ||
2530 | return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i); | |
2531 | ||
2532 | out: | |
2533 | return err; | |
2534 | } | |
2535 | #else | |
2536 | static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, | |
2537 | struct sadb_msg *hdr, void **ext_hdrs) | |
2538 | { | |
2539 | return -ENOPROTOOPT; | |
2540 | } | |
2541 | #endif | |
2542 | ||
2543 | ||
1da177e4 LT |
2544 | static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) |
2545 | { | |
77d8d7a6 | 2546 | unsigned int dir; |
215a2dd3 | 2547 | int err = 0, delete; |
1da177e4 LT |
2548 | struct sadb_x_policy *pol; |
2549 | struct xfrm_policy *xp; | |
26b15dad | 2550 | struct km_event c; |
1da177e4 LT |
2551 | |
2552 | if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL) | |
2553 | return -EINVAL; | |
2554 | ||
77d8d7a6 HX |
2555 | dir = xfrm_policy_id2dir(pol->sadb_x_policy_id); |
2556 | if (dir >= XFRM_POLICY_MAX) | |
2557 | return -EINVAL; | |
2558 | ||
215a2dd3 | 2559 | delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2); |
f7b6983f | 2560 | xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id, |
215a2dd3 | 2561 | delete, &err); |
1da177e4 LT |
2562 | if (xp == NULL) |
2563 | return -ENOENT; | |
2564 | ||
215a2dd3 | 2565 | if (delete) { |
ab5f5e8b | 2566 | xfrm_audit_policy_delete(xp, err ? 0 : 1, |
2532386f EP |
2567 | audit_get_loginuid(current), |
2568 | audit_get_sessionid(current), 0); | |
1da177e4 | 2569 | |
215a2dd3 EP |
2570 | if (err) |
2571 | goto out; | |
2572 | c.seq = hdr->sadb_msg_seq; | |
2573 | c.pid = hdr->sadb_msg_pid; | |
bf08867f | 2574 | c.data.byid = 1; |
f60f6b8f | 2575 | c.event = XFRM_MSG_DELPOLICY; |
77d8d7a6 | 2576 | km_policy_notify(xp, dir, &c); |
26b15dad | 2577 | } else { |
77d8d7a6 | 2578 | err = key_pol_get_resp(sk, xp, hdr, dir); |
1da177e4 | 2579 | } |
1da177e4 | 2580 | |
215a2dd3 | 2581 | out: |
1da177e4 LT |
2582 | xfrm_pol_put(xp); |
2583 | return err; | |
2584 | } | |
2585 | ||
2586 | static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr) | |
2587 | { | |
83321d6b | 2588 | struct pfkey_sock *pfk = ptr; |
1da177e4 LT |
2589 | struct sk_buff *out_skb; |
2590 | struct sadb_msg *out_hdr; | |
55569ce2 | 2591 | int err; |
1da177e4 | 2592 | |
83321d6b TT |
2593 | if (!pfkey_can_dump(&pfk->sk)) |
2594 | return -ENOBUFS; | |
2595 | ||
1da177e4 LT |
2596 | out_skb = pfkey_xfrm_policy2msg_prep(xp); |
2597 | if (IS_ERR(out_skb)) | |
2598 | return PTR_ERR(out_skb); | |
2599 | ||
55569ce2 KM |
2600 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
2601 | if (err < 0) | |
2602 | return err; | |
1da177e4 LT |
2603 | |
2604 | out_hdr = (struct sadb_msg *) out_skb->data; | |
83321d6b | 2605 | out_hdr->sadb_msg_version = pfk->dump.msg_version; |
1da177e4 LT |
2606 | out_hdr->sadb_msg_type = SADB_X_SPDDUMP; |
2607 | out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC; | |
2608 | out_hdr->sadb_msg_errno = 0; | |
12a169e7 | 2609 | out_hdr->sadb_msg_seq = count + 1; |
83321d6b | 2610 | out_hdr->sadb_msg_pid = pfk->dump.msg_pid; |
12a169e7 HX |
2611 | |
2612 | if (pfk->dump.skb) | |
2613 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, | |
2614 | &pfk->sk); | |
2615 | pfk->dump.skb = out_skb; | |
2616 | ||
1da177e4 LT |
2617 | return 0; |
2618 | } | |
2619 | ||
83321d6b TT |
2620 | static int pfkey_dump_sp(struct pfkey_sock *pfk) |
2621 | { | |
2622 | return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk); | |
2623 | } | |
2624 | ||
2625 | static void pfkey_dump_sp_done(struct pfkey_sock *pfk) | |
2626 | { | |
2627 | xfrm_policy_walk_done(&pfk->dump.u.policy); | |
2628 | } | |
2629 | ||
1da177e4 LT |
2630 | static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) |
2631 | { | |
83321d6b | 2632 | struct pfkey_sock *pfk = pfkey_sk(sk); |
4c563f76 | 2633 | |
83321d6b TT |
2634 | if (pfk->dump.dump != NULL) |
2635 | return -EBUSY; | |
1da177e4 | 2636 | |
83321d6b TT |
2637 | pfk->dump.msg_version = hdr->sadb_msg_version; |
2638 | pfk->dump.msg_pid = hdr->sadb_msg_pid; | |
2639 | pfk->dump.dump = pfkey_dump_sp; | |
2640 | pfk->dump.done = pfkey_dump_sp_done; | |
2641 | xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN); | |
2642 | ||
2643 | return pfkey_do_dump(pfk); | |
1da177e4 LT |
2644 | } |
2645 | ||
26b15dad | 2646 | static int key_notify_policy_flush(struct km_event *c) |
1da177e4 LT |
2647 | { |
2648 | struct sk_buff *skb_out; | |
26b15dad | 2649 | struct sadb_msg *hdr; |
1da177e4 | 2650 | |
26b15dad | 2651 | skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); |
1da177e4 LT |
2652 | if (!skb_out) |
2653 | return -ENOBUFS; | |
26b15dad | 2654 | hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg)); |
151bb0ff | 2655 | hdr->sadb_msg_type = SADB_X_SPDFLUSH; |
26b15dad JHS |
2656 | hdr->sadb_msg_seq = c->seq; |
2657 | hdr->sadb_msg_pid = c->pid; | |
2658 | hdr->sadb_msg_version = PF_KEY_V2; | |
2659 | hdr->sadb_msg_errno = (uint8_t) 0; | |
2660 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); | |
2661 | pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL); | |
2662 | return 0; | |
1da177e4 | 2663 | |
26b15dad JHS |
2664 | } |
2665 | ||
2666 | static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs) | |
2667 | { | |
2668 | struct km_event c; | |
161a09e7 | 2669 | struct xfrm_audit audit_info; |
4aa2e62c | 2670 | int err; |
1da177e4 | 2671 | |
0c11b942 | 2672 | audit_info.loginuid = audit_get_loginuid(current); |
2532386f | 2673 | audit_info.sessionid = audit_get_sessionid(current); |
161a09e7 | 2674 | audit_info.secid = 0; |
4aa2e62c JL |
2675 | err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info); |
2676 | if (err) | |
2677 | return err; | |
f7b6983f | 2678 | c.data.type = XFRM_POLICY_TYPE_MAIN; |
f60f6b8f | 2679 | c.event = XFRM_MSG_FLUSHPOLICY; |
26b15dad JHS |
2680 | c.pid = hdr->sadb_msg_pid; |
2681 | c.seq = hdr->sadb_msg_seq; | |
2682 | km_policy_notify(NULL, 0, &c); | |
1da177e4 LT |
2683 | |
2684 | return 0; | |
2685 | } | |
2686 | ||
2687 | typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb, | |
2688 | struct sadb_msg *hdr, void **ext_hdrs); | |
2689 | static pfkey_handler pfkey_funcs[SADB_MAX + 1] = { | |
2690 | [SADB_RESERVED] = pfkey_reserved, | |
2691 | [SADB_GETSPI] = pfkey_getspi, | |
2692 | [SADB_UPDATE] = pfkey_add, | |
2693 | [SADB_ADD] = pfkey_add, | |
2694 | [SADB_DELETE] = pfkey_delete, | |
2695 | [SADB_GET] = pfkey_get, | |
2696 | [SADB_ACQUIRE] = pfkey_acquire, | |
2697 | [SADB_REGISTER] = pfkey_register, | |
2698 | [SADB_EXPIRE] = NULL, | |
2699 | [SADB_FLUSH] = pfkey_flush, | |
2700 | [SADB_DUMP] = pfkey_dump, | |
2701 | [SADB_X_PROMISC] = pfkey_promisc, | |
2702 | [SADB_X_PCHANGE] = NULL, | |
2703 | [SADB_X_SPDUPDATE] = pfkey_spdadd, | |
2704 | [SADB_X_SPDADD] = pfkey_spdadd, | |
2705 | [SADB_X_SPDDELETE] = pfkey_spddelete, | |
2706 | [SADB_X_SPDGET] = pfkey_spdget, | |
2707 | [SADB_X_SPDACQUIRE] = NULL, | |
2708 | [SADB_X_SPDDUMP] = pfkey_spddump, | |
2709 | [SADB_X_SPDFLUSH] = pfkey_spdflush, | |
2710 | [SADB_X_SPDSETIDX] = pfkey_spdadd, | |
2711 | [SADB_X_SPDDELETE2] = pfkey_spdget, | |
08de61be | 2712 | [SADB_X_MIGRATE] = pfkey_migrate, |
1da177e4 LT |
2713 | }; |
2714 | ||
2715 | static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr) | |
2716 | { | |
2717 | void *ext_hdrs[SADB_EXT_MAX]; | |
2718 | int err; | |
2719 | ||
2720 | pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, | |
2721 | BROADCAST_PROMISC_ONLY, NULL); | |
2722 | ||
2723 | memset(ext_hdrs, 0, sizeof(ext_hdrs)); | |
2724 | err = parse_exthdrs(skb, hdr, ext_hdrs); | |
2725 | if (!err) { | |
2726 | err = -EOPNOTSUPP; | |
2727 | if (pfkey_funcs[hdr->sadb_msg_type]) | |
2728 | err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs); | |
2729 | } | |
2730 | return err; | |
2731 | } | |
2732 | ||
2733 | static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp) | |
2734 | { | |
2735 | struct sadb_msg *hdr = NULL; | |
2736 | ||
2737 | if (skb->len < sizeof(*hdr)) { | |
2738 | *errp = -EMSGSIZE; | |
2739 | } else { | |
2740 | hdr = (struct sadb_msg *) skb->data; | |
2741 | if (hdr->sadb_msg_version != PF_KEY_V2 || | |
2742 | hdr->sadb_msg_reserved != 0 || | |
2743 | (hdr->sadb_msg_type <= SADB_RESERVED || | |
2744 | hdr->sadb_msg_type > SADB_MAX)) { | |
2745 | hdr = NULL; | |
2746 | *errp = -EINVAL; | |
2747 | } else if (hdr->sadb_msg_len != (skb->len / | |
2748 | sizeof(uint64_t)) || | |
2749 | hdr->sadb_msg_len < (sizeof(struct sadb_msg) / | |
2750 | sizeof(uint64_t))) { | |
2751 | hdr = NULL; | |
2752 | *errp = -EMSGSIZE; | |
2753 | } else { | |
2754 | *errp = 0; | |
2755 | } | |
2756 | } | |
2757 | return hdr; | |
2758 | } | |
2759 | ||
2760 | static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d) | |
2761 | { | |
f398035f HX |
2762 | unsigned int id = d->desc.sadb_alg_id; |
2763 | ||
2764 | if (id >= sizeof(t->aalgos) * 8) | |
2765 | return 0; | |
2766 | ||
2767 | return (t->aalgos >> id) & 1; | |
1da177e4 LT |
2768 | } |
2769 | ||
2770 | static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d) | |
2771 | { | |
f398035f HX |
2772 | unsigned int id = d->desc.sadb_alg_id; |
2773 | ||
2774 | if (id >= sizeof(t->ealgos) * 8) | |
2775 | return 0; | |
2776 | ||
2777 | return (t->ealgos >> id) & 1; | |
1da177e4 LT |
2778 | } |
2779 | ||
2780 | static int count_ah_combs(struct xfrm_tmpl *t) | |
2781 | { | |
2782 | int i, sz = 0; | |
2783 | ||
2784 | for (i = 0; ; i++) { | |
2785 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); | |
2786 | if (!aalg) | |
2787 | break; | |
2788 | if (aalg_tmpl_set(t, aalg) && aalg->available) | |
2789 | sz += sizeof(struct sadb_comb); | |
2790 | } | |
2791 | return sz + sizeof(struct sadb_prop); | |
2792 | } | |
2793 | ||
2794 | static int count_esp_combs(struct xfrm_tmpl *t) | |
2795 | { | |
2796 | int i, k, sz = 0; | |
2797 | ||
2798 | for (i = 0; ; i++) { | |
2799 | struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); | |
2800 | if (!ealg) | |
2801 | break; | |
8ff24541 | 2802 | |
1da177e4 LT |
2803 | if (!(ealg_tmpl_set(t, ealg) && ealg->available)) |
2804 | continue; | |
8ff24541 | 2805 | |
1da177e4 LT |
2806 | for (k = 1; ; k++) { |
2807 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); | |
2808 | if (!aalg) | |
2809 | break; | |
8ff24541 | 2810 | |
1da177e4 LT |
2811 | if (aalg_tmpl_set(t, aalg) && aalg->available) |
2812 | sz += sizeof(struct sadb_comb); | |
2813 | } | |
2814 | } | |
2815 | return sz + sizeof(struct sadb_prop); | |
2816 | } | |
2817 | ||
2818 | static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t) | |
2819 | { | |
2820 | struct sadb_prop *p; | |
2821 | int i; | |
2822 | ||
2823 | p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop)); | |
2824 | p->sadb_prop_len = sizeof(struct sadb_prop)/8; | |
2825 | p->sadb_prop_exttype = SADB_EXT_PROPOSAL; | |
2826 | p->sadb_prop_replay = 32; | |
2827 | memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); | |
2828 | ||
2829 | for (i = 0; ; i++) { | |
2830 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); | |
2831 | if (!aalg) | |
2832 | break; | |
2833 | ||
2834 | if (aalg_tmpl_set(t, aalg) && aalg->available) { | |
2835 | struct sadb_comb *c; | |
2836 | c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb)); | |
2837 | memset(c, 0, sizeof(*c)); | |
2838 | p->sadb_prop_len += sizeof(struct sadb_comb)/8; | |
2839 | c->sadb_comb_auth = aalg->desc.sadb_alg_id; | |
2840 | c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; | |
2841 | c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; | |
2842 | c->sadb_comb_hard_addtime = 24*60*60; | |
2843 | c->sadb_comb_soft_addtime = 20*60*60; | |
2844 | c->sadb_comb_hard_usetime = 8*60*60; | |
2845 | c->sadb_comb_soft_usetime = 7*60*60; | |
2846 | } | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t) | |
2851 | { | |
2852 | struct sadb_prop *p; | |
2853 | int i, k; | |
2854 | ||
2855 | p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop)); | |
2856 | p->sadb_prop_len = sizeof(struct sadb_prop)/8; | |
2857 | p->sadb_prop_exttype = SADB_EXT_PROPOSAL; | |
2858 | p->sadb_prop_replay = 32; | |
2859 | memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); | |
2860 | ||
2861 | for (i=0; ; i++) { | |
2862 | struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); | |
2863 | if (!ealg) | |
2864 | break; | |
8ff24541 | 2865 | |
1da177e4 LT |
2866 | if (!(ealg_tmpl_set(t, ealg) && ealg->available)) |
2867 | continue; | |
8ff24541 | 2868 | |
1da177e4 LT |
2869 | for (k = 1; ; k++) { |
2870 | struct sadb_comb *c; | |
2871 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); | |
2872 | if (!aalg) | |
2873 | break; | |
2874 | if (!(aalg_tmpl_set(t, aalg) && aalg->available)) | |
2875 | continue; | |
2876 | c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb)); | |
2877 | memset(c, 0, sizeof(*c)); | |
2878 | p->sadb_prop_len += sizeof(struct sadb_comb)/8; | |
2879 | c->sadb_comb_auth = aalg->desc.sadb_alg_id; | |
2880 | c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; | |
2881 | c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; | |
2882 | c->sadb_comb_encrypt = ealg->desc.sadb_alg_id; | |
2883 | c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits; | |
2884 | c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits; | |
2885 | c->sadb_comb_hard_addtime = 24*60*60; | |
2886 | c->sadb_comb_soft_addtime = 20*60*60; | |
2887 | c->sadb_comb_hard_usetime = 8*60*60; | |
2888 | c->sadb_comb_soft_usetime = 7*60*60; | |
2889 | } | |
2890 | } | |
2891 | } | |
2892 | ||
26b15dad JHS |
2893 | static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c) |
2894 | { | |
2895 | return 0; | |
2896 | } | |
2897 | ||
2898 | static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c) | |
1da177e4 LT |
2899 | { |
2900 | struct sk_buff *out_skb; | |
2901 | struct sadb_msg *out_hdr; | |
26b15dad JHS |
2902 | int hard; |
2903 | int hsc; | |
2904 | ||
bf08867f | 2905 | hard = c->data.hard; |
26b15dad JHS |
2906 | if (hard) |
2907 | hsc = 2; | |
2908 | else | |
2909 | hsc = 1; | |
1da177e4 | 2910 | |
050f009e | 2911 | out_skb = pfkey_xfrm_state2msg_expire(x, hsc); |
1da177e4 LT |
2912 | if (IS_ERR(out_skb)) |
2913 | return PTR_ERR(out_skb); | |
2914 | ||
2915 | out_hdr = (struct sadb_msg *) out_skb->data; | |
2916 | out_hdr->sadb_msg_version = PF_KEY_V2; | |
2917 | out_hdr->sadb_msg_type = SADB_EXPIRE; | |
2918 | out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); | |
2919 | out_hdr->sadb_msg_errno = 0; | |
2920 | out_hdr->sadb_msg_reserved = 0; | |
2921 | out_hdr->sadb_msg_seq = 0; | |
2922 | out_hdr->sadb_msg_pid = 0; | |
2923 | ||
2924 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL); | |
2925 | return 0; | |
2926 | } | |
2927 | ||
26b15dad JHS |
2928 | static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c) |
2929 | { | |
99c6f60e JHS |
2930 | if (atomic_read(&pfkey_socks_nr) == 0) |
2931 | return 0; | |
2932 | ||
26b15dad | 2933 | switch (c->event) { |
f60f6b8f | 2934 | case XFRM_MSG_EXPIRE: |
26b15dad | 2935 | return key_notify_sa_expire(x, c); |
f60f6b8f HX |
2936 | case XFRM_MSG_DELSA: |
2937 | case XFRM_MSG_NEWSA: | |
2938 | case XFRM_MSG_UPDSA: | |
26b15dad | 2939 | return key_notify_sa(x, c); |
f60f6b8f | 2940 | case XFRM_MSG_FLUSHSA: |
26b15dad | 2941 | return key_notify_sa_flush(c); |
d51d081d JHS |
2942 | case XFRM_MSG_NEWAE: /* not yet supported */ |
2943 | break; | |
26b15dad JHS |
2944 | default: |
2945 | printk("pfkey: Unknown SA event %d\n", c->event); | |
2946 | break; | |
2947 | } | |
2948 | ||
2949 | return 0; | |
2950 | } | |
2951 | ||
2952 | static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | |
2953 | { | |
f7b6983f MN |
2954 | if (xp && xp->type != XFRM_POLICY_TYPE_MAIN) |
2955 | return 0; | |
2956 | ||
26b15dad | 2957 | switch (c->event) { |
f60f6b8f | 2958 | case XFRM_MSG_POLEXPIRE: |
26b15dad | 2959 | return key_notify_policy_expire(xp, c); |
f60f6b8f HX |
2960 | case XFRM_MSG_DELPOLICY: |
2961 | case XFRM_MSG_NEWPOLICY: | |
2962 | case XFRM_MSG_UPDPOLICY: | |
26b15dad | 2963 | return key_notify_policy(xp, dir, c); |
f60f6b8f | 2964 | case XFRM_MSG_FLUSHPOLICY: |
f7b6983f MN |
2965 | if (c->data.type != XFRM_POLICY_TYPE_MAIN) |
2966 | break; | |
26b15dad JHS |
2967 | return key_notify_policy_flush(c); |
2968 | default: | |
2969 | printk("pfkey: Unknown policy event %d\n", c->event); | |
2970 | break; | |
2971 | } | |
2972 | ||
2973 | return 0; | |
2974 | } | |
2975 | ||
1da177e4 LT |
2976 | static u32 get_acqseq(void) |
2977 | { | |
2978 | u32 res; | |
2979 | static u32 acqseq; | |
2980 | static DEFINE_SPINLOCK(acqseq_lock); | |
2981 | ||
2982 | spin_lock_bh(&acqseq_lock); | |
2983 | res = (++acqseq ? : ++acqseq); | |
2984 | spin_unlock_bh(&acqseq_lock); | |
2985 | return res; | |
2986 | } | |
2987 | ||
2988 | static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir) | |
2989 | { | |
2990 | struct sk_buff *skb; | |
2991 | struct sadb_msg *hdr; | |
2992 | struct sadb_address *addr; | |
2993 | struct sadb_x_policy *pol; | |
1da177e4 LT |
2994 | int sockaddr_size; |
2995 | int size; | |
4e2ba18e VY |
2996 | struct sadb_x_sec_ctx *sec_ctx; |
2997 | struct xfrm_sec_ctx *xfrm_ctx; | |
2998 | int ctx_size = 0; | |
8ff24541 | 2999 | |
1da177e4 LT |
3000 | sockaddr_size = pfkey_sockaddr_size(x->props.family); |
3001 | if (!sockaddr_size) | |
3002 | return -EINVAL; | |
3003 | ||
3004 | size = sizeof(struct sadb_msg) + | |
3005 | (sizeof(struct sadb_address) * 2) + | |
3006 | (sockaddr_size * 2) + | |
3007 | sizeof(struct sadb_x_policy); | |
8ff24541 | 3008 | |
1da177e4 LT |
3009 | if (x->id.proto == IPPROTO_AH) |
3010 | size += count_ah_combs(t); | |
3011 | else if (x->id.proto == IPPROTO_ESP) | |
3012 | size += count_esp_combs(t); | |
3013 | ||
4e2ba18e VY |
3014 | if ((xfrm_ctx = x->security)) { |
3015 | ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); | |
3016 | size += sizeof(struct sadb_x_sec_ctx) + ctx_size; | |
3017 | } | |
3018 | ||
1da177e4 LT |
3019 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
3020 | if (skb == NULL) | |
3021 | return -ENOMEM; | |
8ff24541 | 3022 | |
1da177e4 LT |
3023 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); |
3024 | hdr->sadb_msg_version = PF_KEY_V2; | |
3025 | hdr->sadb_msg_type = SADB_ACQUIRE; | |
3026 | hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); | |
3027 | hdr->sadb_msg_len = size / sizeof(uint64_t); | |
3028 | hdr->sadb_msg_errno = 0; | |
3029 | hdr->sadb_msg_reserved = 0; | |
3030 | hdr->sadb_msg_seq = x->km.seq = get_acqseq(); | |
3031 | hdr->sadb_msg_pid = 0; | |
3032 | ||
3033 | /* src address */ | |
8ff24541 | 3034 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 | 3035 | sizeof(struct sadb_address)+sockaddr_size); |
8ff24541 | 3036 | addr->sadb_address_len = |
1da177e4 LT |
3037 | (sizeof(struct sadb_address)+sockaddr_size)/ |
3038 | sizeof(uint64_t); | |
3039 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; | |
3040 | addr->sadb_address_proto = 0; | |
3041 | addr->sadb_address_reserved = 0; | |
e5b56652 YH |
3042 | addr->sadb_address_prefixlen = |
3043 | pfkey_sockaddr_fill(&x->props.saddr, 0, | |
3044 | (struct sockaddr *) (addr + 1), | |
3045 | x->props.family); | |
3046 | if (!addr->sadb_address_prefixlen) | |
1da177e4 | 3047 | BUG(); |
8ff24541 | 3048 | |
1da177e4 | 3049 | /* dst address */ |
8ff24541 | 3050 | addr = (struct sadb_address*) skb_put(skb, |
1da177e4 LT |
3051 | sizeof(struct sadb_address)+sockaddr_size); |
3052 | addr->sadb_address_len = | |
3053 | (sizeof(struct sadb_address)+sockaddr_size)/ | |
3054 | sizeof(uint64_t); | |
3055 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; | |
3056 | addr->sadb_address_proto = 0; | |
3057 | addr->sadb_address_reserved = 0; | |
e5b56652 YH |
3058 | addr->sadb_address_prefixlen = |
3059 | pfkey_sockaddr_fill(&x->id.daddr, 0, | |
3060 | (struct sockaddr *) (addr + 1), | |
3061 | x->props.family); | |
3062 | if (!addr->sadb_address_prefixlen) | |
1da177e4 LT |
3063 | BUG(); |
3064 | ||
3065 | pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy)); | |
3066 | pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); | |
3067 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; | |
3068 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; | |
3069 | pol->sadb_x_policy_dir = dir+1; | |
3070 | pol->sadb_x_policy_id = xp->index; | |
3071 | ||
3072 | /* Set sadb_comb's. */ | |
3073 | if (x->id.proto == IPPROTO_AH) | |
3074 | dump_ah_combs(skb, t); | |
3075 | else if (x->id.proto == IPPROTO_ESP) | |
3076 | dump_esp_combs(skb, t); | |
3077 | ||
4e2ba18e VY |
3078 | /* security context */ |
3079 | if (xfrm_ctx) { | |
3080 | sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, | |
3081 | sizeof(struct sadb_x_sec_ctx) + ctx_size); | |
3082 | sec_ctx->sadb_x_sec_len = | |
3083 | (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); | |
3084 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; | |
3085 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; | |
3086 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; | |
3087 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; | |
3088 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, | |
3089 | xfrm_ctx->ctx_len); | |
3090 | } | |
3091 | ||
1da177e4 LT |
3092 | return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL); |
3093 | } | |
3094 | ||
cb969f07 | 3095 | static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt, |
8ff24541 | 3096 | u8 *data, int len, int *dir) |
1da177e4 LT |
3097 | { |
3098 | struct xfrm_policy *xp; | |
3099 | struct sadb_x_policy *pol = (struct sadb_x_policy*)data; | |
df71837d | 3100 | struct sadb_x_sec_ctx *sec_ctx; |
1da177e4 | 3101 | |
cb969f07 | 3102 | switch (sk->sk_family) { |
1da177e4 LT |
3103 | case AF_INET: |
3104 | if (opt != IP_IPSEC_POLICY) { | |
3105 | *dir = -EOPNOTSUPP; | |
3106 | return NULL; | |
3107 | } | |
3108 | break; | |
3109 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
3110 | case AF_INET6: | |
3111 | if (opt != IPV6_IPSEC_POLICY) { | |
3112 | *dir = -EOPNOTSUPP; | |
3113 | return NULL; | |
3114 | } | |
3115 | break; | |
3116 | #endif | |
3117 | default: | |
3118 | *dir = -EINVAL; | |
3119 | return NULL; | |
3120 | } | |
3121 | ||
3122 | *dir = -EINVAL; | |
3123 | ||
3124 | if (len < sizeof(struct sadb_x_policy) || | |
3125 | pol->sadb_x_policy_len*8 > len || | |
3126 | pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS || | |
3127 | (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND)) | |
3128 | return NULL; | |
3129 | ||
3130 | xp = xfrm_policy_alloc(GFP_ATOMIC); | |
3131 | if (xp == NULL) { | |
3132 | *dir = -ENOBUFS; | |
3133 | return NULL; | |
3134 | } | |
3135 | ||
3136 | xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? | |
3137 | XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); | |
3138 | ||
3139 | xp->lft.soft_byte_limit = XFRM_INF; | |
3140 | xp->lft.hard_byte_limit = XFRM_INF; | |
3141 | xp->lft.soft_packet_limit = XFRM_INF; | |
3142 | xp->lft.hard_packet_limit = XFRM_INF; | |
cb969f07 | 3143 | xp->family = sk->sk_family; |
1da177e4 LT |
3144 | |
3145 | xp->xfrm_nr = 0; | |
3146 | if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && | |
3147 | (*dir = parse_ipsecrequests(xp, pol)) < 0) | |
3148 | goto out; | |
3149 | ||
df71837d TJ |
3150 | /* security context too */ |
3151 | if (len >= (pol->sadb_x_policy_len*8 + | |
3152 | sizeof(struct sadb_x_sec_ctx))) { | |
3153 | char *p = (char *)pol; | |
3154 | struct xfrm_user_sec_ctx *uctx; | |
3155 | ||
3156 | p += pol->sadb_x_policy_len*8; | |
3157 | sec_ctx = (struct sadb_x_sec_ctx *)p; | |
3158 | if (len < pol->sadb_x_policy_len*8 + | |
cb969f07 VY |
3159 | sec_ctx->sadb_x_sec_len) { |
3160 | *dir = -EINVAL; | |
df71837d | 3161 | goto out; |
cb969f07 | 3162 | } |
df71837d TJ |
3163 | if ((*dir = verify_sec_ctx_len(p))) |
3164 | goto out; | |
3165 | uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); | |
03e1ad7b | 3166 | *dir = security_xfrm_policy_alloc(&xp->security, uctx); |
df71837d TJ |
3167 | kfree(uctx); |
3168 | ||
3169 | if (*dir) | |
3170 | goto out; | |
3171 | } | |
3172 | ||
1da177e4 LT |
3173 | *dir = pol->sadb_x_policy_dir-1; |
3174 | return xp; | |
3175 | ||
3176 | out: | |
64c31b3f | 3177 | xfrm_policy_destroy(xp); |
1da177e4 LT |
3178 | return NULL; |
3179 | } | |
3180 | ||
5d36b180 | 3181 | static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) |
1da177e4 LT |
3182 | { |
3183 | struct sk_buff *skb; | |
3184 | struct sadb_msg *hdr; | |
3185 | struct sadb_sa *sa; | |
3186 | struct sadb_address *addr; | |
3187 | struct sadb_x_nat_t_port *n_port; | |
1da177e4 LT |
3188 | int sockaddr_size; |
3189 | int size; | |
3190 | __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0); | |
3191 | struct xfrm_encap_tmpl *natt = NULL; | |
3192 | ||
3193 | sockaddr_size = pfkey_sockaddr_size(x->props.family); | |
3194 | if (!sockaddr_size) | |
3195 | return -EINVAL; | |
3196 | ||
3197 | if (!satype) | |
3198 | return -EINVAL; | |
3199 | ||
3200 | if (!x->encap) | |
3201 | return -EINVAL; | |
3202 | ||
3203 | natt = x->encap; | |
3204 | ||
3205 | /* Build an SADB_X_NAT_T_NEW_MAPPING message: | |
3206 | * | |
3207 | * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) | | |
3208 | * ADDRESS_DST (new addr) | NAT_T_DPORT (new port) | |
3209 | */ | |
8ff24541 | 3210 | |
1da177e4 LT |
3211 | size = sizeof(struct sadb_msg) + |
3212 | sizeof(struct sadb_sa) + | |
3213 | (sizeof(struct sadb_address) * 2) + | |
3214 | (sockaddr_size * 2) + | |
3215 | (sizeof(struct sadb_x_nat_t_port) * 2); | |
8ff24541 | 3216 | |
1da177e4 LT |
3217 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
3218 | if (skb == NULL) | |
3219 | return -ENOMEM; | |
8ff24541 | 3220 | |
1da177e4 LT |
3221 | hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); |
3222 | hdr->sadb_msg_version = PF_KEY_V2; | |
3223 | hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING; | |
3224 | hdr->sadb_msg_satype = satype; | |
3225 | hdr->sadb_msg_len = size / sizeof(uint64_t); | |
3226 | hdr->sadb_msg_errno = 0; | |
3227 | hdr->sadb_msg_reserved = 0; | |
3228 | hdr->sadb_msg_seq = x->km.seq = get_acqseq(); | |
3229 | hdr->sadb_msg_pid = 0; | |
3230 | ||
3231 | /* SA */ | |
3232 | sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa)); | |
3233 | sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); | |
3234 | sa->sadb_sa_exttype = SADB_EXT_SA; | |
3235 | sa->sadb_sa_spi = x->id.spi; | |
3236 | sa->sadb_sa_replay = 0; | |
3237 | sa->sadb_sa_state = 0; | |
3238 | sa->sadb_sa_auth = 0; | |
3239 | sa->sadb_sa_encrypt = 0; | |
3240 | sa->sadb_sa_flags = 0; | |
3241 | ||
3242 | /* ADDRESS_SRC (old addr) */ | |
3243 | addr = (struct sadb_address*) | |
3244 | skb_put(skb, sizeof(struct sadb_address)+sockaddr_size); | |
8ff24541 | 3245 | addr->sadb_address_len = |
1da177e4 LT |
3246 | (sizeof(struct sadb_address)+sockaddr_size)/ |
3247 | sizeof(uint64_t); | |
3248 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; | |
3249 | addr->sadb_address_proto = 0; | |
3250 | addr->sadb_address_reserved = 0; | |
e5b56652 YH |
3251 | addr->sadb_address_prefixlen = |
3252 | pfkey_sockaddr_fill(&x->props.saddr, 0, | |
3253 | (struct sockaddr *) (addr + 1), | |
3254 | x->props.family); | |
3255 | if (!addr->sadb_address_prefixlen) | |
1da177e4 LT |
3256 | BUG(); |
3257 | ||
3258 | /* NAT_T_SPORT (old port) */ | |
3259 | n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); | |
3260 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); | |
3261 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; | |
3262 | n_port->sadb_x_nat_t_port_port = natt->encap_sport; | |
3263 | n_port->sadb_x_nat_t_port_reserved = 0; | |
3264 | ||
3265 | /* ADDRESS_DST (new addr) */ | |
3266 | addr = (struct sadb_address*) | |
3267 | skb_put(skb, sizeof(struct sadb_address)+sockaddr_size); | |
8ff24541 | 3268 | addr->sadb_address_len = |
1da177e4 LT |
3269 | (sizeof(struct sadb_address)+sockaddr_size)/ |
3270 | sizeof(uint64_t); | |
3271 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; | |
3272 | addr->sadb_address_proto = 0; | |
3273 | addr->sadb_address_reserved = 0; | |
e5b56652 YH |
3274 | addr->sadb_address_prefixlen = |
3275 | pfkey_sockaddr_fill(ipaddr, 0, | |
3276 | (struct sockaddr *) (addr + 1), | |
3277 | x->props.family); | |
3278 | if (!addr->sadb_address_prefixlen) | |
1da177e4 LT |
3279 | BUG(); |
3280 | ||
3281 | /* NAT_T_DPORT (new port) */ | |
3282 | n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); | |
3283 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); | |
3284 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; | |
3285 | n_port->sadb_x_nat_t_port_port = sport; | |
3286 | n_port->sadb_x_nat_t_port_reserved = 0; | |
3287 | ||
3288 | return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL); | |
3289 | } | |
3290 | ||
08de61be SS |
3291 | #ifdef CONFIG_NET_KEY_MIGRATE |
3292 | static int set_sadb_address(struct sk_buff *skb, int sasize, int type, | |
3293 | struct xfrm_selector *sel) | |
3294 | { | |
3295 | struct sadb_address *addr; | |
08de61be SS |
3296 | addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize); |
3297 | addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8; | |
3298 | addr->sadb_address_exttype = type; | |
3299 | addr->sadb_address_proto = sel->proto; | |
3300 | addr->sadb_address_reserved = 0; | |
3301 | ||
3302 | switch (type) { | |
3303 | case SADB_EXT_ADDRESS_SRC: | |
e5b56652 YH |
3304 | addr->sadb_address_prefixlen = sel->prefixlen_s; |
3305 | pfkey_sockaddr_fill(&sel->saddr, 0, | |
3306 | (struct sockaddr *)(addr + 1), | |
3307 | sel->family); | |
08de61be SS |
3308 | break; |
3309 | case SADB_EXT_ADDRESS_DST: | |
e5b56652 YH |
3310 | addr->sadb_address_prefixlen = sel->prefixlen_d; |
3311 | pfkey_sockaddr_fill(&sel->daddr, 0, | |
3312 | (struct sockaddr *)(addr + 1), | |
3313 | sel->family); | |
08de61be SS |
3314 | break; |
3315 | default: | |
3316 | return -EINVAL; | |
3317 | } | |
3318 | ||
3319 | return 0; | |
3320 | } | |
3321 | ||
3322 | static int set_ipsecrequest(struct sk_buff *skb, | |
3323 | uint8_t proto, uint8_t mode, int level, | |
3324 | uint32_t reqid, uint8_t family, | |
3325 | xfrm_address_t *src, xfrm_address_t *dst) | |
3326 | { | |
3327 | struct sadb_x_ipsecrequest *rq; | |
e5b56652 YH |
3328 | u8 *sa; |
3329 | int socklen = pfkey_sockaddr_len(family); | |
08de61be SS |
3330 | int size_req; |
3331 | ||
3332 | size_req = sizeof(struct sadb_x_ipsecrequest) + | |
3333 | pfkey_sockaddr_pair_size(family); | |
3334 | ||
3335 | rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req); | |
3336 | memset(rq, 0, size_req); | |
3337 | rq->sadb_x_ipsecrequest_len = size_req; | |
3338 | rq->sadb_x_ipsecrequest_proto = proto; | |
3339 | rq->sadb_x_ipsecrequest_mode = mode; | |
3340 | rq->sadb_x_ipsecrequest_level = level; | |
3341 | rq->sadb_x_ipsecrequest_reqid = reqid; | |
3342 | ||
e5b56652 YH |
3343 | sa = (u8 *) (rq + 1); |
3344 | if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) || | |
3345 | !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family)) | |
08de61be | 3346 | return -EINVAL; |
08de61be SS |
3347 | |
3348 | return 0; | |
3349 | } | |
3350 | #endif | |
3351 | ||
3352 | #ifdef CONFIG_NET_KEY_MIGRATE | |
3353 | static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
3354 | struct xfrm_migrate *m, int num_bundles) | |
3355 | { | |
3356 | int i; | |
3357 | int sasize_sel; | |
3358 | int size = 0; | |
3359 | int size_pol = 0; | |
3360 | struct sk_buff *skb; | |
3361 | struct sadb_msg *hdr; | |
3362 | struct sadb_x_policy *pol; | |
3363 | struct xfrm_migrate *mp; | |
3364 | ||
3365 | if (type != XFRM_POLICY_TYPE_MAIN) | |
3366 | return 0; | |
3367 | ||
3368 | if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH) | |
3369 | return -EINVAL; | |
3370 | ||
3371 | /* selector */ | |
3372 | sasize_sel = pfkey_sockaddr_size(sel->family); | |
3373 | if (!sasize_sel) | |
3374 | return -EINVAL; | |
3375 | size += (sizeof(struct sadb_address) + sasize_sel) * 2; | |
3376 | ||
3377 | /* policy info */ | |
3378 | size_pol += sizeof(struct sadb_x_policy); | |
3379 | ||
3380 | /* ipsecrequests */ | |
3381 | for (i = 0, mp = m; i < num_bundles; i++, mp++) { | |
3382 | /* old locator pair */ | |
3383 | size_pol += sizeof(struct sadb_x_ipsecrequest) + | |
3384 | pfkey_sockaddr_pair_size(mp->old_family); | |
3385 | /* new locator pair */ | |
3386 | size_pol += sizeof(struct sadb_x_ipsecrequest) + | |
3387 | pfkey_sockaddr_pair_size(mp->new_family); | |
3388 | } | |
3389 | ||
3390 | size += sizeof(struct sadb_msg) + size_pol; | |
3391 | ||
3392 | /* alloc buffer */ | |
3393 | skb = alloc_skb(size, GFP_ATOMIC); | |
3394 | if (skb == NULL) | |
3395 | return -ENOMEM; | |
3396 | ||
3397 | hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg)); | |
3398 | hdr->sadb_msg_version = PF_KEY_V2; | |
3399 | hdr->sadb_msg_type = SADB_X_MIGRATE; | |
3400 | hdr->sadb_msg_satype = pfkey_proto2satype(m->proto); | |
3401 | hdr->sadb_msg_len = size / 8; | |
3402 | hdr->sadb_msg_errno = 0; | |
3403 | hdr->sadb_msg_reserved = 0; | |
3404 | hdr->sadb_msg_seq = 0; | |
3405 | hdr->sadb_msg_pid = 0; | |
3406 | ||
3407 | /* selector src */ | |
3408 | set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel); | |
3409 | ||
3410 | /* selector dst */ | |
3411 | set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel); | |
3412 | ||
3413 | /* policy information */ | |
3414 | pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy)); | |
3415 | pol->sadb_x_policy_len = size_pol / 8; | |
3416 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; | |
3417 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; | |
3418 | pol->sadb_x_policy_dir = dir + 1; | |
3419 | pol->sadb_x_policy_id = 0; | |
3420 | pol->sadb_x_policy_priority = 0; | |
3421 | ||
3422 | for (i = 0, mp = m; i < num_bundles; i++, mp++) { | |
3423 | /* old ipsecrequest */ | |
55569ce2 KM |
3424 | int mode = pfkey_mode_from_xfrm(mp->mode); |
3425 | if (mode < 0) | |
d4782c32 | 3426 | goto err; |
55569ce2 | 3427 | if (set_ipsecrequest(skb, mp->proto, mode, |
08de61be SS |
3428 | (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), |
3429 | mp->reqid, mp->old_family, | |
d4782c32 PM |
3430 | &mp->old_saddr, &mp->old_daddr) < 0) |
3431 | goto err; | |
08de61be SS |
3432 | |
3433 | /* new ipsecrequest */ | |
55569ce2 | 3434 | if (set_ipsecrequest(skb, mp->proto, mode, |
08de61be SS |
3435 | (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), |
3436 | mp->reqid, mp->new_family, | |
d4782c32 PM |
3437 | &mp->new_saddr, &mp->new_daddr) < 0) |
3438 | goto err; | |
08de61be SS |
3439 | } |
3440 | ||
3441 | /* broadcast migrate message to sockets */ | |
3442 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL); | |
3443 | ||
3444 | return 0; | |
d4782c32 PM |
3445 | |
3446 | err: | |
3447 | kfree_skb(skb); | |
3448 | return -EINVAL; | |
08de61be SS |
3449 | } |
3450 | #else | |
3451 | static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
3452 | struct xfrm_migrate *m, int num_bundles) | |
3453 | { | |
3454 | return -ENOPROTOOPT; | |
3455 | } | |
3456 | #endif | |
3457 | ||
1da177e4 LT |
3458 | static int pfkey_sendmsg(struct kiocb *kiocb, |
3459 | struct socket *sock, struct msghdr *msg, size_t len) | |
3460 | { | |
3461 | struct sock *sk = sock->sk; | |
3462 | struct sk_buff *skb = NULL; | |
3463 | struct sadb_msg *hdr = NULL; | |
3464 | int err; | |
3465 | ||
3466 | err = -EOPNOTSUPP; | |
3467 | if (msg->msg_flags & MSG_OOB) | |
3468 | goto out; | |
3469 | ||
3470 | err = -EMSGSIZE; | |
3471 | if ((unsigned)len > sk->sk_sndbuf - 32) | |
3472 | goto out; | |
3473 | ||
3474 | err = -ENOBUFS; | |
3475 | skb = alloc_skb(len, GFP_KERNEL); | |
3476 | if (skb == NULL) | |
3477 | goto out; | |
3478 | ||
3479 | err = -EFAULT; | |
3480 | if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) | |
3481 | goto out; | |
3482 | ||
3483 | hdr = pfkey_get_base_msg(skb, &err); | |
3484 | if (!hdr) | |
3485 | goto out; | |
3486 | ||
4a3e2f71 | 3487 | mutex_lock(&xfrm_cfg_mutex); |
1da177e4 | 3488 | err = pfkey_process(sk, skb, hdr); |
4a3e2f71 | 3489 | mutex_unlock(&xfrm_cfg_mutex); |
1da177e4 LT |
3490 | |
3491 | out: | |
3492 | if (err && hdr && pfkey_error(hdr, err, sk) == 0) | |
3493 | err = 0; | |
3494 | if (skb) | |
3495 | kfree_skb(skb); | |
3496 | ||
3497 | return err ? : len; | |
3498 | } | |
3499 | ||
3500 | static int pfkey_recvmsg(struct kiocb *kiocb, | |
3501 | struct socket *sock, struct msghdr *msg, size_t len, | |
3502 | int flags) | |
3503 | { | |
3504 | struct sock *sk = sock->sk; | |
83321d6b | 3505 | struct pfkey_sock *pfk = pfkey_sk(sk); |
1da177e4 LT |
3506 | struct sk_buff *skb; |
3507 | int copied, err; | |
3508 | ||
3509 | err = -EINVAL; | |
3510 | if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) | |
3511 | goto out; | |
3512 | ||
3513 | msg->msg_namelen = 0; | |
3514 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); | |
3515 | if (skb == NULL) | |
3516 | goto out; | |
3517 | ||
3518 | copied = skb->len; | |
3519 | if (copied > len) { | |
3520 | msg->msg_flags |= MSG_TRUNC; | |
3521 | copied = len; | |
3522 | } | |
3523 | ||
badff6d0 | 3524 | skb_reset_transport_header(skb); |
1da177e4 LT |
3525 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
3526 | if (err) | |
3527 | goto out_free; | |
3528 | ||
3529 | sock_recv_timestamp(msg, sk, skb); | |
3530 | ||
3531 | err = (flags & MSG_TRUNC) ? skb->len : copied; | |
3532 | ||
83321d6b TT |
3533 | if (pfk->dump.dump != NULL && |
3534 | 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) | |
3535 | pfkey_do_dump(pfk); | |
3536 | ||
1da177e4 LT |
3537 | out_free: |
3538 | skb_free_datagram(sk, skb); | |
3539 | out: | |
3540 | return err; | |
3541 | } | |
3542 | ||
90ddc4f0 | 3543 | static const struct proto_ops pfkey_ops = { |
1da177e4 LT |
3544 | .family = PF_KEY, |
3545 | .owner = THIS_MODULE, | |
3546 | /* Operations that make no sense on pfkey sockets. */ | |
3547 | .bind = sock_no_bind, | |
3548 | .connect = sock_no_connect, | |
3549 | .socketpair = sock_no_socketpair, | |
3550 | .accept = sock_no_accept, | |
3551 | .getname = sock_no_getname, | |
3552 | .ioctl = sock_no_ioctl, | |
3553 | .listen = sock_no_listen, | |
3554 | .shutdown = sock_no_shutdown, | |
3555 | .setsockopt = sock_no_setsockopt, | |
3556 | .getsockopt = sock_no_getsockopt, | |
3557 | .mmap = sock_no_mmap, | |
3558 | .sendpage = sock_no_sendpage, | |
3559 | ||
3560 | /* Now the operations that really occur. */ | |
3561 | .release = pfkey_release, | |
3562 | .poll = datagram_poll, | |
3563 | .sendmsg = pfkey_sendmsg, | |
3564 | .recvmsg = pfkey_recvmsg, | |
3565 | }; | |
3566 | ||
3567 | static struct net_proto_family pfkey_family_ops = { | |
3568 | .family = PF_KEY, | |
3569 | .create = pfkey_create, | |
3570 | .owner = THIS_MODULE, | |
3571 | }; | |
3572 | ||
3573 | #ifdef CONFIG_PROC_FS | |
bd2f7476 | 3574 | static int pfkey_seq_show(struct seq_file *f, void *v) |
1da177e4 | 3575 | { |
1da177e4 | 3576 | struct sock *s; |
1da177e4 | 3577 | |
bd2f7476 PE |
3578 | s = (struct sock *)v; |
3579 | if (v == SEQ_START_TOKEN) | |
3580 | seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n"); | |
3581 | else | |
3582 | seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n", | |
1da177e4 LT |
3583 | s, |
3584 | atomic_read(&s->sk_refcnt), | |
3585 | atomic_read(&s->sk_rmem_alloc), | |
3586 | atomic_read(&s->sk_wmem_alloc), | |
3587 | sock_i_uid(s), | |
3588 | sock_i_ino(s) | |
3589 | ); | |
bd2f7476 PE |
3590 | return 0; |
3591 | } | |
3592 | ||
3593 | static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos) | |
3594 | { | |
3595 | struct sock *s; | |
3596 | struct hlist_node *node; | |
3597 | loff_t pos = *ppos; | |
1da177e4 | 3598 | |
bd2f7476 PE |
3599 | read_lock(&pfkey_table_lock); |
3600 | if (pos == 0) | |
3601 | return SEQ_START_TOKEN; | |
8ff24541 | 3602 | |
bd2f7476 PE |
3603 | sk_for_each(s, node, &pfkey_table) |
3604 | if (pos-- == 1) | |
3605 | return s; | |
1da177e4 | 3606 | |
bd2f7476 PE |
3607 | return NULL; |
3608 | } | |
3609 | ||
3610 | static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos) | |
3611 | { | |
3612 | ++*ppos; | |
3613 | return (v == SEQ_START_TOKEN) ? | |
3614 | sk_head(&pfkey_table) : | |
3615 | sk_next((struct sock *)v); | |
3616 | } | |
1da177e4 | 3617 | |
bd2f7476 PE |
3618 | static void pfkey_seq_stop(struct seq_file *f, void *v) |
3619 | { | |
3620 | read_unlock(&pfkey_table_lock); | |
3621 | } | |
1da177e4 | 3622 | |
bd2f7476 PE |
3623 | static struct seq_operations pfkey_seq_ops = { |
3624 | .start = pfkey_seq_start, | |
3625 | .next = pfkey_seq_next, | |
3626 | .stop = pfkey_seq_stop, | |
3627 | .show = pfkey_seq_show, | |
3628 | }; | |
1da177e4 | 3629 | |
bd2f7476 PE |
3630 | static int pfkey_seq_open(struct inode *inode, struct file *file) |
3631 | { | |
3632 | return seq_open(file, &pfkey_seq_ops); | |
1da177e4 | 3633 | } |
61145aa1 | 3634 | |
bd2f7476 PE |
3635 | static struct file_operations pfkey_proc_ops = { |
3636 | .open = pfkey_seq_open, | |
3637 | .read = seq_read, | |
3638 | .llseek = seq_lseek, | |
3639 | .release = seq_release, | |
3640 | }; | |
3641 | ||
61145aa1 PE |
3642 | static int pfkey_init_proc(void) |
3643 | { | |
bd2f7476 PE |
3644 | struct proc_dir_entry *e; |
3645 | ||
d9595a7b | 3646 | e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops); |
bd2f7476 | 3647 | if (e == NULL) |
61145aa1 | 3648 | return -ENOMEM; |
bd2f7476 | 3649 | |
bd2f7476 | 3650 | return 0; |
61145aa1 PE |
3651 | } |
3652 | ||
3653 | static void pfkey_exit_proc(void) | |
3654 | { | |
d9595a7b | 3655 | proc_net_remove(&init_net, "pfkey"); |
61145aa1 PE |
3656 | } |
3657 | #else | |
3658 | static inline int pfkey_init_proc(void) | |
3659 | { | |
3660 | return 0; | |
3661 | } | |
3662 | ||
3663 | static inline void pfkey_exit_proc(void) | |
3664 | { | |
3665 | } | |
1da177e4 LT |
3666 | #endif |
3667 | ||
3668 | static struct xfrm_mgr pfkeyv2_mgr = | |
3669 | { | |
3670 | .id = "pfkeyv2", | |
3671 | .notify = pfkey_send_notify, | |
3672 | .acquire = pfkey_send_acquire, | |
3673 | .compile_policy = pfkey_compile_policy, | |
3674 | .new_mapping = pfkey_send_new_mapping, | |
26b15dad | 3675 | .notify_policy = pfkey_send_policy_notify, |
08de61be | 3676 | .migrate = pfkey_send_migrate, |
1da177e4 LT |
3677 | }; |
3678 | ||
3679 | static void __exit ipsec_pfkey_exit(void) | |
3680 | { | |
3681 | xfrm_unregister_km(&pfkeyv2_mgr); | |
61145aa1 | 3682 | pfkey_exit_proc(); |
1da177e4 LT |
3683 | sock_unregister(PF_KEY); |
3684 | proto_unregister(&key_proto); | |
3685 | } | |
3686 | ||
3687 | static int __init ipsec_pfkey_init(void) | |
3688 | { | |
3689 | int err = proto_register(&key_proto, 0); | |
3690 | ||
3691 | if (err != 0) | |
3692 | goto out; | |
3693 | ||
3694 | err = sock_register(&pfkey_family_ops); | |
3695 | if (err != 0) | |
3696 | goto out_unregister_key_proto; | |
61145aa1 PE |
3697 | err = pfkey_init_proc(); |
3698 | if (err != 0) | |
1da177e4 | 3699 | goto out_sock_unregister; |
1da177e4 LT |
3700 | err = xfrm_register_km(&pfkeyv2_mgr); |
3701 | if (err != 0) | |
3702 | goto out_remove_proc_entry; | |
3703 | out: | |
3704 | return err; | |
3705 | out_remove_proc_entry: | |
61145aa1 | 3706 | pfkey_exit_proc(); |
1da177e4 | 3707 | out_sock_unregister: |
1da177e4 LT |
3708 | sock_unregister(PF_KEY); |
3709 | out_unregister_key_proto: | |
3710 | proto_unregister(&key_proto); | |
3711 | goto out; | |
3712 | } | |
3713 | ||
3714 | module_init(ipsec_pfkey_init); | |
3715 | module_exit(ipsec_pfkey_exit); | |
3716 | MODULE_LICENSE("GPL"); | |
3717 | MODULE_ALIAS_NETPROTO(PF_KEY); |