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