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