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