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