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