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