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