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