[IPSEC]: Add XFRM_STATE_NOPMTUDISC flag
[linux-2.6.git] / net / key / af_key.c
1 /*
2  * net/key/af_key.c     An implementation of PF_KEYv2 sockets.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Maxim Giryaev   <gem@asplinux.ru>
10  *              David S. Miller <davem@redhat.com>
11  *              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12  *              Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13  *              Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14  *              Derek Atkins <derek@ihtfp.com>
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/in.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <net/xfrm.h>
30
31 #include <net/sock.h>
32
33 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
34 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
35
36
37 /* List of all pfkey sockets. */
38 static HLIST_HEAD(pfkey_table);
39 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
40 static DEFINE_RWLOCK(pfkey_table_lock);
41 static atomic_t pfkey_table_users = ATOMIC_INIT(0);
42
43 static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
44
45 struct pfkey_sock {
46         /* struct sock must be the first member of struct pfkey_sock */
47         struct sock     sk;
48         int             registered;
49         int             promisc;
50 };
51
52 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
53 {
54         return (struct pfkey_sock *)sk;
55 }
56
57 static void pfkey_sock_destruct(struct sock *sk)
58 {
59         skb_queue_purge(&sk->sk_receive_queue);
60
61         if (!sock_flag(sk, SOCK_DEAD)) {
62                 printk("Attempt to release alive pfkey socket: %p\n", sk);
63                 return;
64         }
65
66         BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
67         BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
68
69         atomic_dec(&pfkey_socks_nr);
70 }
71
72 static void pfkey_table_grab(void)
73 {
74         write_lock_bh(&pfkey_table_lock);
75
76         if (atomic_read(&pfkey_table_users)) {
77                 DECLARE_WAITQUEUE(wait, current);
78
79                 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
80                 for(;;) {
81                         set_current_state(TASK_UNINTERRUPTIBLE);
82                         if (atomic_read(&pfkey_table_users) == 0)
83                                 break;
84                         write_unlock_bh(&pfkey_table_lock);
85                         schedule();
86                         write_lock_bh(&pfkey_table_lock);
87                 }
88
89                 __set_current_state(TASK_RUNNING);
90                 remove_wait_queue(&pfkey_table_wait, &wait);
91         }
92 }
93
94 static __inline__ void pfkey_table_ungrab(void)
95 {
96         write_unlock_bh(&pfkey_table_lock);
97         wake_up(&pfkey_table_wait);
98 }
99
100 static __inline__ void pfkey_lock_table(void)
101 {
102         /* read_lock() synchronizes us to pfkey_table_grab */
103
104         read_lock(&pfkey_table_lock);
105         atomic_inc(&pfkey_table_users);
106         read_unlock(&pfkey_table_lock);
107 }
108
109 static __inline__ void pfkey_unlock_table(void)
110 {
111         if (atomic_dec_and_test(&pfkey_table_users))
112                 wake_up(&pfkey_table_wait);
113 }
114
115
116 static struct proto_ops pfkey_ops;
117
118 static void pfkey_insert(struct sock *sk)
119 {
120         pfkey_table_grab();
121         sk_add_node(sk, &pfkey_table);
122         pfkey_table_ungrab();
123 }
124
125 static void pfkey_remove(struct sock *sk)
126 {
127         pfkey_table_grab();
128         sk_del_node_init(sk);
129         pfkey_table_ungrab();
130 }
131
132 static struct proto key_proto = {
133         .name     = "KEY",
134         .owner    = THIS_MODULE,
135         .obj_size = sizeof(struct pfkey_sock),
136 };
137
138 static int pfkey_create(struct socket *sock, int protocol)
139 {
140         struct sock *sk;
141         int err;
142
143         if (!capable(CAP_NET_ADMIN))
144                 return -EPERM;
145         if (sock->type != SOCK_RAW)
146                 return -ESOCKTNOSUPPORT;
147         if (protocol != PF_KEY_V2)
148                 return -EPROTONOSUPPORT;
149
150         err = -ENOMEM;
151         sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
152         if (sk == NULL)
153                 goto out;
154         
155         sock->ops = &pfkey_ops;
156         sock_init_data(sock, sk);
157
158         sk->sk_family = PF_KEY;
159         sk->sk_destruct = pfkey_sock_destruct;
160
161         atomic_inc(&pfkey_socks_nr);
162
163         pfkey_insert(sk);
164
165         return 0;
166 out:
167         return err;
168 }
169
170 static int pfkey_release(struct socket *sock)
171 {
172         struct sock *sk = sock->sk;
173
174         if (!sk)
175                 return 0;
176
177         pfkey_remove(sk);
178
179         sock_orphan(sk);
180         sock->sk = NULL;
181         skb_queue_purge(&sk->sk_write_queue);
182         sock_put(sk);
183
184         return 0;
185 }
186
187 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
188                                int allocation, struct sock *sk)
189 {
190         int err = -ENOBUFS;
191
192         sock_hold(sk);
193         if (*skb2 == NULL) {
194                 if (atomic_read(&skb->users) != 1) {
195                         *skb2 = skb_clone(skb, allocation);
196                 } else {
197                         *skb2 = skb;
198                         atomic_inc(&skb->users);
199                 }
200         }
201         if (*skb2 != NULL) {
202                 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
203                         skb_orphan(*skb2);
204                         skb_set_owner_r(*skb2, sk);
205                         skb_queue_tail(&sk->sk_receive_queue, *skb2);
206                         sk->sk_data_ready(sk, (*skb2)->len);
207                         *skb2 = NULL;
208                         err = 0;
209                 }
210         }
211         sock_put(sk);
212         return err;
213 }
214
215 /* Send SKB to all pfkey sockets matching selected criteria.  */
216 #define BROADCAST_ALL           0
217 #define BROADCAST_ONE           1
218 #define BROADCAST_REGISTERED    2
219 #define BROADCAST_PROMISC_ONLY  4
220 static int pfkey_broadcast(struct sk_buff *skb, int allocation,
221                            int broadcast_flags, struct sock *one_sk)
222 {
223         struct sock *sk;
224         struct hlist_node *node;
225         struct sk_buff *skb2 = NULL;
226         int err = -ESRCH;
227
228         /* XXX Do we need something like netlink_overrun?  I think
229          * XXX PF_KEY socket apps will not mind current behavior.
230          */
231         if (!skb)
232                 return -ENOMEM;
233
234         pfkey_lock_table();
235         sk_for_each(sk, node, &pfkey_table) {
236                 struct pfkey_sock *pfk = pfkey_sk(sk);
237                 int err2;
238
239                 /* Yes, it means that if you are meant to receive this
240                  * pfkey message you receive it twice as promiscuous
241                  * socket.
242                  */
243                 if (pfk->promisc)
244                         pfkey_broadcast_one(skb, &skb2, allocation, sk);
245
246                 /* the exact target will be processed later */
247                 if (sk == one_sk)
248                         continue;
249                 if (broadcast_flags != BROADCAST_ALL) {
250                         if (broadcast_flags & BROADCAST_PROMISC_ONLY)
251                                 continue;
252                         if ((broadcast_flags & BROADCAST_REGISTERED) &&
253                             !pfk->registered)
254                                 continue;
255                         if (broadcast_flags & BROADCAST_ONE)
256                                 continue;
257                 }
258
259                 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
260
261                 /* Error is cleare after succecful sending to at least one
262                  * registered KM */
263                 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
264                         err = err2;
265         }
266         pfkey_unlock_table();
267
268         if (one_sk != NULL)
269                 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
270
271         if (skb2)
272                 kfree_skb(skb2);
273         kfree_skb(skb);
274         return err;
275 }
276
277 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
278 {
279         *new = *orig;
280 }
281
282 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
283 {
284         struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
285         struct sadb_msg *hdr;
286
287         if (!skb)
288                 return -ENOBUFS;
289
290         /* Woe be to the platform trying to support PFKEY yet
291          * having normal errnos outside the 1-255 range, inclusive.
292          */
293         err = -err;
294         if (err == ERESTARTSYS ||
295             err == ERESTARTNOHAND ||
296             err == ERESTARTNOINTR)
297                 err = EINTR;
298         if (err >= 512)
299                 err = EINVAL;
300         if (err <= 0 || err >= 256)
301                 BUG();
302
303         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
304         pfkey_hdr_dup(hdr, orig);
305         hdr->sadb_msg_errno = (uint8_t) err;
306         hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
307                              sizeof(uint64_t));
308
309         pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
310
311         return 0;
312 }
313
314 static u8 sadb_ext_min_len[] = {
315         [SADB_EXT_RESERVED]             = (u8) 0,
316         [SADB_EXT_SA]                   = (u8) sizeof(struct sadb_sa),
317         [SADB_EXT_LIFETIME_CURRENT]     = (u8) sizeof(struct sadb_lifetime),
318         [SADB_EXT_LIFETIME_HARD]        = (u8) sizeof(struct sadb_lifetime),
319         [SADB_EXT_LIFETIME_SOFT]        = (u8) sizeof(struct sadb_lifetime),
320         [SADB_EXT_ADDRESS_SRC]          = (u8) sizeof(struct sadb_address),
321         [SADB_EXT_ADDRESS_DST]          = (u8) sizeof(struct sadb_address),
322         [SADB_EXT_ADDRESS_PROXY]        = (u8) sizeof(struct sadb_address),
323         [SADB_EXT_KEY_AUTH]             = (u8) sizeof(struct sadb_key),
324         [SADB_EXT_KEY_ENCRYPT]          = (u8) sizeof(struct sadb_key),
325         [SADB_EXT_IDENTITY_SRC]         = (u8) sizeof(struct sadb_ident),
326         [SADB_EXT_IDENTITY_DST]         = (u8) sizeof(struct sadb_ident),
327         [SADB_EXT_SENSITIVITY]          = (u8) sizeof(struct sadb_sens),
328         [SADB_EXT_PROPOSAL]             = (u8) sizeof(struct sadb_prop),
329         [SADB_EXT_SUPPORTED_AUTH]       = (u8) sizeof(struct sadb_supported),
330         [SADB_EXT_SUPPORTED_ENCRYPT]    = (u8) sizeof(struct sadb_supported),
331         [SADB_EXT_SPIRANGE]             = (u8) sizeof(struct sadb_spirange),
332         [SADB_X_EXT_KMPRIVATE]          = (u8) sizeof(struct sadb_x_kmprivate),
333         [SADB_X_EXT_POLICY]             = (u8) sizeof(struct sadb_x_policy),
334         [SADB_X_EXT_SA2]                = (u8) sizeof(struct sadb_x_sa2),
335         [SADB_X_EXT_NAT_T_TYPE]         = (u8) sizeof(struct sadb_x_nat_t_type),
336         [SADB_X_EXT_NAT_T_SPORT]        = (u8) sizeof(struct sadb_x_nat_t_port),
337         [SADB_X_EXT_NAT_T_DPORT]        = (u8) sizeof(struct sadb_x_nat_t_port),
338         [SADB_X_EXT_NAT_T_OA]           = (u8) sizeof(struct sadb_address),
339 };
340
341 /* Verify sadb_address_{len,prefixlen} against sa_family.  */
342 static int verify_address_len(void *p)
343 {
344         struct sadb_address *sp = p;
345         struct sockaddr *addr = (struct sockaddr *)(sp + 1);
346         struct sockaddr_in *sin;
347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
348         struct sockaddr_in6 *sin6;
349 #endif
350         int len;
351
352         switch (addr->sa_family) {
353         case AF_INET:
354                 len  = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
355                 len /= sizeof(uint64_t);
356                 if (sp->sadb_address_len != len ||
357                     sp->sadb_address_prefixlen > 32)
358                         return -EINVAL;
359                 break;
360 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
361         case AF_INET6:
362                 len  = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
363                 len /= sizeof(uint64_t);
364                 if (sp->sadb_address_len != len ||
365                     sp->sadb_address_prefixlen > 128)
366                         return -EINVAL;
367                 break;
368 #endif
369         default:
370                 /* It is user using kernel to keep track of security
371                  * associations for another protocol, such as
372                  * OSPF/RSVP/RIPV2/MIP.  It is user's job to verify
373                  * lengths.
374                  *
375                  * XXX Actually, association/policy database is not yet
376                  * XXX able to cope with arbitrary sockaddr families.
377                  * XXX When it can, remove this -EINVAL.  -DaveM
378                  */
379                 return -EINVAL;
380                 break;
381         };
382
383         return 0;
384 }
385
386 static int present_and_same_family(struct sadb_address *src,
387                                    struct sadb_address *dst)
388 {
389         struct sockaddr *s_addr, *d_addr;
390
391         if (!src || !dst)
392                 return 0;
393
394         s_addr = (struct sockaddr *)(src + 1);
395         d_addr = (struct sockaddr *)(dst + 1);
396         if (s_addr->sa_family != d_addr->sa_family)
397                 return 0;
398         if (s_addr->sa_family != AF_INET
399 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
400             && s_addr->sa_family != AF_INET6
401 #endif
402                 )
403                 return 0;
404
405         return 1;
406 }
407
408 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
409 {
410         char *p = (char *) hdr;
411         int len = skb->len;
412
413         len -= sizeof(*hdr);
414         p += sizeof(*hdr);
415         while (len > 0) {
416                 struct sadb_ext *ehdr = (struct sadb_ext *) p;
417                 uint16_t ext_type;
418                 int ext_len;
419
420                 ext_len  = ehdr->sadb_ext_len;
421                 ext_len *= sizeof(uint64_t);
422                 ext_type = ehdr->sadb_ext_type;
423                 if (ext_len < sizeof(uint64_t) ||
424                     ext_len > len ||
425                     ext_type == SADB_EXT_RESERVED)
426                         return -EINVAL;
427
428                 if (ext_type <= SADB_EXT_MAX) {
429                         int min = (int) sadb_ext_min_len[ext_type];
430                         if (ext_len < min)
431                                 return -EINVAL;
432                         if (ext_hdrs[ext_type-1] != NULL)
433                                 return -EINVAL;
434                         if (ext_type == SADB_EXT_ADDRESS_SRC ||
435                             ext_type == SADB_EXT_ADDRESS_DST ||
436                             ext_type == SADB_EXT_ADDRESS_PROXY ||
437                             ext_type == SADB_X_EXT_NAT_T_OA) {
438                                 if (verify_address_len(p))
439                                         return -EINVAL;
440                         }                               
441                         ext_hdrs[ext_type-1] = p;
442                 }
443                 p   += ext_len;
444                 len -= ext_len;
445         }
446
447         return 0;
448 }
449
450 static uint16_t
451 pfkey_satype2proto(uint8_t satype)
452 {
453         switch (satype) {
454         case SADB_SATYPE_UNSPEC:
455                 return IPSEC_PROTO_ANY;
456         case SADB_SATYPE_AH:
457                 return IPPROTO_AH;
458         case SADB_SATYPE_ESP:
459                 return IPPROTO_ESP;
460         case SADB_X_SATYPE_IPCOMP:
461                 return IPPROTO_COMP;
462                 break;
463         default:
464                 return 0;
465         }
466         /* NOTREACHED */
467 }
468
469 static uint8_t
470 pfkey_proto2satype(uint16_t proto)
471 {
472         switch (proto) {
473         case IPPROTO_AH:
474                 return SADB_SATYPE_AH;
475         case IPPROTO_ESP:
476                 return SADB_SATYPE_ESP;
477         case IPPROTO_COMP:
478                 return SADB_X_SATYPE_IPCOMP;
479                 break;
480         default:
481                 return 0;
482         }
483         /* NOTREACHED */
484 }
485
486 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
487  * say specifically 'just raw sockets' as we encode them as 255.
488  */
489
490 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
491 {
492         return (proto == IPSEC_PROTO_ANY ? 0 : proto);
493 }
494
495 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
496 {
497         return (proto ? proto : IPSEC_PROTO_ANY);
498 }
499
500 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
501                                      xfrm_address_t *xaddr)
502 {
503         switch (((struct sockaddr*)(addr + 1))->sa_family) {
504         case AF_INET:
505                 xaddr->a4 = 
506                         ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
507                 return AF_INET;
508 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
509         case AF_INET6:
510                 memcpy(xaddr->a6, 
511                        &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
512                        sizeof(struct in6_addr));
513                 return AF_INET6;
514 #endif
515         default:
516                 return 0;
517         }
518         /* NOTREACHED */
519 }
520
521 static struct  xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
522 {
523         struct sadb_sa *sa;
524         struct sadb_address *addr;
525         uint16_t proto;
526         unsigned short family;
527         xfrm_address_t *xaddr;
528
529         sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
530         if (sa == NULL)
531                 return NULL;
532
533         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
534         if (proto == 0)
535                 return NULL;
536
537         /* sadb_address_len should be checked by caller */
538         addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
539         if (addr == NULL)
540                 return NULL;
541
542         family = ((struct sockaddr *)(addr + 1))->sa_family;
543         switch (family) {
544         case AF_INET:
545                 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
546                 break;
547 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
548         case AF_INET6:
549                 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
550                 break;
551 #endif
552         default:
553                 xaddr = NULL;
554         }
555
556         if (!xaddr)
557                 return NULL;
558
559         return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
560 }
561
562 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
563 static int
564 pfkey_sockaddr_size(sa_family_t family)
565 {
566         switch (family) {
567         case AF_INET:
568                 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
569 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
570         case AF_INET6:
571                 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
572 #endif
573         default:
574                 return 0;
575         }
576         /* NOTREACHED */
577 }
578
579 static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
580 {
581         struct sk_buff *skb;
582         struct sadb_msg *hdr;
583         struct sadb_sa *sa;
584         struct sadb_lifetime *lifetime;
585         struct sadb_address *addr;
586         struct sadb_key *key;
587         struct sadb_x_sa2 *sa2;
588         struct sockaddr_in *sin;
589 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
590         struct sockaddr_in6 *sin6;
591 #endif
592         int size;
593         int auth_key_size = 0;
594         int encrypt_key_size = 0;
595         int sockaddr_size;
596         struct xfrm_encap_tmpl *natt = NULL;
597
598         /* address family check */
599         sockaddr_size = pfkey_sockaddr_size(x->props.family);
600         if (!sockaddr_size)
601                 return ERR_PTR(-EINVAL);
602
603         /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
604            key(AE), (identity(SD),) (sensitivity)> */
605         size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) + 
606                 sizeof(struct sadb_lifetime) +
607                 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
608                 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
609                         sizeof(struct sadb_address)*2 + 
610                                 sockaddr_size*2 +
611                                         sizeof(struct sadb_x_sa2);
612         /* identity & sensitivity */
613
614         if ((x->props.family == AF_INET &&
615              x->sel.saddr.a4 != x->props.saddr.a4)
616 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617             || (x->props.family == AF_INET6 &&
618                 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
619 #endif
620                 )
621                 size += sizeof(struct sadb_address) + sockaddr_size;
622
623         if (add_keys) {
624                 if (x->aalg && x->aalg->alg_key_len) {
625                         auth_key_size = 
626                                 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8); 
627                         size += sizeof(struct sadb_key) + auth_key_size;
628                 }
629                 if (x->ealg && x->ealg->alg_key_len) {
630                         encrypt_key_size = 
631                                 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8); 
632                         size += sizeof(struct sadb_key) + encrypt_key_size;
633                 }
634         }
635         if (x->encap)
636                 natt = x->encap;
637
638         if (natt && natt->encap_type) {
639                 size += sizeof(struct sadb_x_nat_t_type);
640                 size += sizeof(struct sadb_x_nat_t_port);
641                 size += sizeof(struct sadb_x_nat_t_port);
642         }
643
644         skb =  alloc_skb(size + 16, GFP_ATOMIC);
645         if (skb == NULL)
646                 return ERR_PTR(-ENOBUFS);
647
648         /* call should fill header later */
649         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
650         memset(hdr, 0, size);   /* XXX do we need this ? */
651         hdr->sadb_msg_len = size / sizeof(uint64_t);
652
653         /* sa */
654         sa = (struct sadb_sa *)  skb_put(skb, sizeof(struct sadb_sa));
655         sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
656         sa->sadb_sa_exttype = SADB_EXT_SA;
657         sa->sadb_sa_spi = x->id.spi;
658         sa->sadb_sa_replay = x->props.replay_window;
659         switch (x->km.state) {
660         case XFRM_STATE_VALID:
661                 sa->sadb_sa_state = x->km.dying ?
662                         SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
663                 break;
664         case XFRM_STATE_ACQ:
665                 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
666                 break;
667         default:
668                 sa->sadb_sa_state = SADB_SASTATE_DEAD;
669                 break;
670         }
671         sa->sadb_sa_auth = 0;
672         if (x->aalg) {
673                 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
674                 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
675         }
676         sa->sadb_sa_encrypt = 0;
677         BUG_ON(x->ealg && x->calg);
678         if (x->ealg) {
679                 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
680                 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
681         }
682         /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
683         if (x->calg) {
684                 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
685                 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
686         }
687
688         sa->sadb_sa_flags = 0;
689         if (x->props.flags & XFRM_STATE_NOECN)
690                 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
691         if (x->props.flags & XFRM_STATE_DECAP_DSCP)
692                 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
693         if (x->props.flags & XFRM_STATE_NOPMTUDISC)
694                 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
695
696         /* hard time */
697         if (hsc & 2) {
698                 lifetime = (struct sadb_lifetime *)  skb_put(skb, 
699                                                              sizeof(struct sadb_lifetime));
700                 lifetime->sadb_lifetime_len =
701                         sizeof(struct sadb_lifetime)/sizeof(uint64_t);
702                 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
703                 lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.hard_packet_limit);
704                 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
705                 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
706                 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
707         }
708         /* soft time */
709         if (hsc & 1) {
710                 lifetime = (struct sadb_lifetime *)  skb_put(skb, 
711                                                              sizeof(struct sadb_lifetime));
712                 lifetime->sadb_lifetime_len =
713                         sizeof(struct sadb_lifetime)/sizeof(uint64_t);
714                 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
715                 lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.soft_packet_limit);
716                 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
717                 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
718                 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
719         }
720         /* current time */
721         lifetime = (struct sadb_lifetime *)  skb_put(skb,
722                                                      sizeof(struct sadb_lifetime));
723         lifetime->sadb_lifetime_len =
724                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
725         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
726         lifetime->sadb_lifetime_allocations = x->curlft.packets;
727         lifetime->sadb_lifetime_bytes = x->curlft.bytes;
728         lifetime->sadb_lifetime_addtime = x->curlft.add_time;
729         lifetime->sadb_lifetime_usetime = x->curlft.use_time;
730         /* src address */
731         addr = (struct sadb_address*) skb_put(skb, 
732                                               sizeof(struct sadb_address)+sockaddr_size);
733         addr->sadb_address_len = 
734                 (sizeof(struct sadb_address)+sockaddr_size)/
735                         sizeof(uint64_t);
736         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
737         /* "if the ports are non-zero, then the sadb_address_proto field, 
738            normally zero, MUST be filled in with the transport 
739            protocol's number." - RFC2367 */
740         addr->sadb_address_proto = 0; 
741         addr->sadb_address_reserved = 0;
742         if (x->props.family == AF_INET) {
743                 addr->sadb_address_prefixlen = 32;
744
745                 sin = (struct sockaddr_in *) (addr + 1);
746                 sin->sin_family = AF_INET;
747                 sin->sin_addr.s_addr = x->props.saddr.a4;
748                 sin->sin_port = 0;
749                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
750         }
751 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
752         else if (x->props.family == AF_INET6) {
753                 addr->sadb_address_prefixlen = 128;
754
755                 sin6 = (struct sockaddr_in6 *) (addr + 1);
756                 sin6->sin6_family = AF_INET6;
757                 sin6->sin6_port = 0;
758                 sin6->sin6_flowinfo = 0;
759                 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
760                        sizeof(struct in6_addr));
761                 sin6->sin6_scope_id = 0;
762         }
763 #endif
764         else
765                 BUG();
766
767         /* dst address */
768         addr = (struct sadb_address*) skb_put(skb, 
769                                               sizeof(struct sadb_address)+sockaddr_size);
770         addr->sadb_address_len = 
771                 (sizeof(struct sadb_address)+sockaddr_size)/
772                         sizeof(uint64_t);
773         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
774         addr->sadb_address_proto = 0; 
775         addr->sadb_address_prefixlen = 32; /* XXX */ 
776         addr->sadb_address_reserved = 0;
777         if (x->props.family == AF_INET) {
778                 sin = (struct sockaddr_in *) (addr + 1);
779                 sin->sin_family = AF_INET;
780                 sin->sin_addr.s_addr = x->id.daddr.a4;
781                 sin->sin_port = 0;
782                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
783
784                 if (x->sel.saddr.a4 != x->props.saddr.a4) {
785                         addr = (struct sadb_address*) skb_put(skb, 
786                                 sizeof(struct sadb_address)+sockaddr_size);
787                         addr->sadb_address_len = 
788                                 (sizeof(struct sadb_address)+sockaddr_size)/
789                                 sizeof(uint64_t);
790                         addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
791                         addr->sadb_address_proto =
792                                 pfkey_proto_from_xfrm(x->sel.proto);
793                         addr->sadb_address_prefixlen = x->sel.prefixlen_s;
794                         addr->sadb_address_reserved = 0;
795
796                         sin = (struct sockaddr_in *) (addr + 1);
797                         sin->sin_family = AF_INET;
798                         sin->sin_addr.s_addr = x->sel.saddr.a4;
799                         sin->sin_port = x->sel.sport;
800                         memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
801                 }
802         }
803 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
804         else if (x->props.family == AF_INET6) {
805                 addr->sadb_address_prefixlen = 128;
806
807                 sin6 = (struct sockaddr_in6 *) (addr + 1);
808                 sin6->sin6_family = AF_INET6;
809                 sin6->sin6_port = 0;
810                 sin6->sin6_flowinfo = 0;
811                 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
812                 sin6->sin6_scope_id = 0;
813
814                 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
815                             sizeof(struct in6_addr))) {
816                         addr = (struct sadb_address *) skb_put(skb, 
817                                 sizeof(struct sadb_address)+sockaddr_size);
818                         addr->sadb_address_len = 
819                                 (sizeof(struct sadb_address)+sockaddr_size)/
820                                 sizeof(uint64_t);
821                         addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
822                         addr->sadb_address_proto =
823                                 pfkey_proto_from_xfrm(x->sel.proto);
824                         addr->sadb_address_prefixlen = x->sel.prefixlen_s;
825                         addr->sadb_address_reserved = 0;
826
827                         sin6 = (struct sockaddr_in6 *) (addr + 1);
828                         sin6->sin6_family = AF_INET6;
829                         sin6->sin6_port = x->sel.sport;
830                         sin6->sin6_flowinfo = 0;
831                         memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
832                                sizeof(struct in6_addr));
833                         sin6->sin6_scope_id = 0;
834                 }
835         }
836 #endif
837         else
838                 BUG();
839
840         /* auth key */
841         if (add_keys && auth_key_size) {
842                 key = (struct sadb_key *) skb_put(skb, 
843                                                   sizeof(struct sadb_key)+auth_key_size);
844                 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
845                         sizeof(uint64_t);
846                 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
847                 key->sadb_key_bits = x->aalg->alg_key_len;
848                 key->sadb_key_reserved = 0;
849                 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
850         }
851         /* encrypt key */
852         if (add_keys && encrypt_key_size) {
853                 key = (struct sadb_key *) skb_put(skb, 
854                                                   sizeof(struct sadb_key)+encrypt_key_size);
855                 key->sadb_key_len = (sizeof(struct sadb_key) + 
856                                      encrypt_key_size) / sizeof(uint64_t);
857                 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
858                 key->sadb_key_bits = x->ealg->alg_key_len;
859                 key->sadb_key_reserved = 0;
860                 memcpy(key + 1, x->ealg->alg_key, 
861                        (x->ealg->alg_key_len+7)/8);
862         }
863
864         /* sa */
865         sa2 = (struct sadb_x_sa2 *)  skb_put(skb, sizeof(struct sadb_x_sa2));
866         sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
867         sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
868         sa2->sadb_x_sa2_mode = x->props.mode + 1;
869         sa2->sadb_x_sa2_reserved1 = 0;
870         sa2->sadb_x_sa2_reserved2 = 0;
871         sa2->sadb_x_sa2_sequence = 0;
872         sa2->sadb_x_sa2_reqid = x->props.reqid;
873
874         if (natt && natt->encap_type) {
875                 struct sadb_x_nat_t_type *n_type;
876                 struct sadb_x_nat_t_port *n_port;
877
878                 /* type */
879                 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
880                 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
881                 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
882                 n_type->sadb_x_nat_t_type_type = natt->encap_type;
883                 n_type->sadb_x_nat_t_type_reserved[0] = 0;
884                 n_type->sadb_x_nat_t_type_reserved[1] = 0;
885                 n_type->sadb_x_nat_t_type_reserved[2] = 0;
886
887                 /* source port */
888                 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
889                 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
890                 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
891                 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
892                 n_port->sadb_x_nat_t_port_reserved = 0;
893
894                 /* dest port */
895                 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
896                 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
897                 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
898                 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
899                 n_port->sadb_x_nat_t_port_reserved = 0;
900         }
901
902         return skb;
903 }
904
905 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr, 
906                                                 void **ext_hdrs)
907 {
908         struct xfrm_state *x; 
909         struct sadb_lifetime *lifetime;
910         struct sadb_sa *sa;
911         struct sadb_key *key;
912         uint16_t proto;
913         int err;
914         
915
916         sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
917         if (!sa ||
918             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
919                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
920                 return ERR_PTR(-EINVAL);
921         if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
922             !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
923                 return ERR_PTR(-EINVAL);
924         if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
925             !ext_hdrs[SADB_EXT_KEY_AUTH-1])
926                 return ERR_PTR(-EINVAL);
927         if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
928             !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
929                 return ERR_PTR(-EINVAL);
930
931         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
932         if (proto == 0)
933                 return ERR_PTR(-EINVAL);
934
935         /* default error is no buffer space */
936         err = -ENOBUFS;
937
938         /* RFC2367:
939
940    Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
941    SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
942    sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
943    Therefore, the sadb_sa_state field of all submitted SAs MUST be
944    SADB_SASTATE_MATURE and the kernel MUST return an error if this is
945    not true.
946
947            However, KAME setkey always uses SADB_SASTATE_LARVAL.
948            Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
949          */
950         if (sa->sadb_sa_auth > SADB_AALG_MAX ||
951             (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
952              sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
953             sa->sadb_sa_encrypt > SADB_EALG_MAX)
954                 return ERR_PTR(-EINVAL);
955         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
956         if (key != NULL &&
957             sa->sadb_sa_auth != SADB_X_AALG_NULL &&
958             ((key->sadb_key_bits+7) / 8 == 0 ||
959              (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
960                 return ERR_PTR(-EINVAL);
961         key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
962         if (key != NULL &&
963             sa->sadb_sa_encrypt != SADB_EALG_NULL &&
964             ((key->sadb_key_bits+7) / 8 == 0 ||
965              (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
966                 return ERR_PTR(-EINVAL);
967
968         x = xfrm_state_alloc();
969         if (x == NULL)
970                 return ERR_PTR(-ENOBUFS);
971
972         x->id.proto = proto;
973         x->id.spi = sa->sadb_sa_spi;
974         x->props.replay_window = sa->sadb_sa_replay;
975         if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
976                 x->props.flags |= XFRM_STATE_NOECN;
977         if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
978                 x->props.flags |= XFRM_STATE_DECAP_DSCP;
979         if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
980                 x->props.flags |= XFRM_STATE_NOPMTUDISC;
981
982         lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
983         if (lifetime != NULL) {
984                 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
985                 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
986                 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
987                 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
988         }
989         lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
990         if (lifetime != NULL) {
991                 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
992                 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
993                 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
994                 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
995         }
996         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
997         if (sa->sadb_sa_auth) {
998                 int keysize = 0;
999                 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1000                 if (!a) {
1001                         err = -ENOSYS;
1002                         goto out;
1003                 }
1004                 if (key)
1005                         keysize = (key->sadb_key_bits + 7) / 8;
1006                 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1007                 if (!x->aalg)
1008                         goto out;
1009                 strcpy(x->aalg->alg_name, a->name);
1010                 x->aalg->alg_key_len = 0;
1011                 if (key) {
1012                         x->aalg->alg_key_len = key->sadb_key_bits;
1013                         memcpy(x->aalg->alg_key, key+1, keysize);
1014                 }
1015                 x->props.aalgo = sa->sadb_sa_auth;
1016                 /* x->algo.flags = sa->sadb_sa_flags; */
1017         }
1018         if (sa->sadb_sa_encrypt) {
1019                 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1020                         struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1021                         if (!a) {
1022                                 err = -ENOSYS;
1023                                 goto out;
1024                         }
1025                         x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1026                         if (!x->calg)
1027                                 goto out;
1028                         strcpy(x->calg->alg_name, a->name);
1029                         x->props.calgo = sa->sadb_sa_encrypt;
1030                 } else {
1031                         int keysize = 0;
1032                         struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1033                         if (!a) {
1034                                 err = -ENOSYS;
1035                                 goto out;
1036                         }
1037                         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1038                         if (key)
1039                                 keysize = (key->sadb_key_bits + 7) / 8;
1040                         x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1041                         if (!x->ealg)
1042                                 goto out;
1043                         strcpy(x->ealg->alg_name, a->name);
1044                         x->ealg->alg_key_len = 0;
1045                         if (key) {
1046                                 x->ealg->alg_key_len = key->sadb_key_bits;
1047                                 memcpy(x->ealg->alg_key, key+1, keysize);
1048                         }
1049                         x->props.ealgo = sa->sadb_sa_encrypt;
1050                 }
1051         }
1052         /* x->algo.flags = sa->sadb_sa_flags; */
1053
1054         x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
1055                                                     &x->props.saddr);
1056         if (!x->props.family) {
1057                 err = -EAFNOSUPPORT;
1058                 goto out;
1059         }
1060         pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
1061                                   &x->id.daddr);
1062
1063         if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1064                 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1065                 x->props.mode = sa2->sadb_x_sa2_mode;
1066                 if (x->props.mode)
1067                         x->props.mode--;
1068                 x->props.reqid = sa2->sadb_x_sa2_reqid;
1069         }
1070
1071         if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1072                 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1073
1074                 /* Nobody uses this, but we try. */
1075                 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1076                 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1077         }
1078
1079         if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1080                 struct sadb_x_nat_t_type* n_type;
1081                 struct xfrm_encap_tmpl *natt;
1082
1083                 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1084                 if (!x->encap)
1085                         goto out;
1086
1087                 natt = x->encap;
1088                 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1089                 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1090
1091                 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1092                         struct sadb_x_nat_t_port* n_port =
1093                                 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1094                         natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1095                 }
1096                 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1097                         struct sadb_x_nat_t_port* n_port =
1098                                 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1099                         natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1100                 }
1101         }
1102
1103         err = xfrm_init_state(x);
1104         if (err)
1105                 goto out;
1106
1107         x->km.seq = hdr->sadb_msg_seq;
1108         return x;
1109
1110 out:
1111         x->km.state = XFRM_STATE_DEAD;
1112         xfrm_state_put(x);
1113         return ERR_PTR(err);
1114 }
1115
1116 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1117 {
1118         return -EOPNOTSUPP;
1119 }
1120
1121 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1122 {
1123         struct sk_buff *resp_skb;
1124         struct sadb_x_sa2 *sa2;
1125         struct sadb_address *saddr, *daddr;
1126         struct sadb_msg *out_hdr;
1127         struct xfrm_state *x = NULL;
1128         u8 mode;
1129         u32 reqid;
1130         u8 proto;
1131         unsigned short family;
1132         xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1133
1134         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1135                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1136                 return -EINVAL;
1137
1138         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1139         if (proto == 0)
1140                 return -EINVAL;
1141
1142         if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1143                 mode = sa2->sadb_x_sa2_mode - 1;
1144                 reqid = sa2->sadb_x_sa2_reqid;
1145         } else {
1146                 mode = 0;
1147                 reqid = 0;
1148         }
1149
1150         saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1151         daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1152
1153         family = ((struct sockaddr *)(saddr + 1))->sa_family;
1154         switch (family) {
1155         case AF_INET:
1156                 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1157                 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1158                 break;
1159 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1160         case AF_INET6:
1161                 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1162                 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1163                 break;
1164 #endif
1165         }
1166
1167         if (hdr->sadb_msg_seq) {
1168                 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1169                 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1170                         xfrm_state_put(x);
1171                         x = NULL;
1172                 }
1173         }
1174
1175         if (!x)
1176                 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1177
1178         if (x == NULL)
1179                 return -ENOENT;
1180
1181         resp_skb = ERR_PTR(-ENOENT);
1182
1183         spin_lock_bh(&x->lock);
1184         if (x->km.state != XFRM_STATE_DEAD) {
1185                 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1186                 u32 min_spi, max_spi;
1187
1188                 if (range != NULL) {
1189                         min_spi = range->sadb_spirange_min;
1190                         max_spi = range->sadb_spirange_max;
1191                 } else {
1192                         min_spi = 0x100;
1193                         max_spi = 0x0fffffff;
1194                 }
1195                 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1196                 if (x->id.spi)
1197                         resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1198         }
1199         spin_unlock_bh(&x->lock);
1200
1201         if (IS_ERR(resp_skb)) {
1202                 xfrm_state_put(x);
1203                 return  PTR_ERR(resp_skb);
1204         }
1205
1206         out_hdr = (struct sadb_msg *) resp_skb->data;
1207         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1208         out_hdr->sadb_msg_type = SADB_GETSPI;
1209         out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1210         out_hdr->sadb_msg_errno = 0;
1211         out_hdr->sadb_msg_reserved = 0;
1212         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1213         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1214
1215         xfrm_state_put(x);
1216
1217         pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1218
1219         return 0;
1220 }
1221
1222 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1223 {
1224         struct xfrm_state *x;
1225
1226         if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1227                 return -EOPNOTSUPP;
1228
1229         if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1230                 return 0;
1231
1232         x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1233         if (x == NULL)
1234                 return 0;
1235
1236         spin_lock_bh(&x->lock);
1237         if (x->km.state == XFRM_STATE_ACQ) {
1238                 x->km.state = XFRM_STATE_ERROR;
1239                 wake_up(&km_waitq);
1240         }
1241         spin_unlock_bh(&x->lock);
1242         xfrm_state_put(x);
1243         return 0;
1244 }
1245
1246 static inline int event2poltype(int event)
1247 {
1248         switch (event) {
1249         case XFRM_MSG_DELPOLICY:
1250                 return SADB_X_SPDDELETE;
1251         case XFRM_MSG_NEWPOLICY:
1252                 return SADB_X_SPDADD;
1253         case XFRM_MSG_UPDPOLICY:
1254                 return SADB_X_SPDUPDATE;
1255         case XFRM_MSG_POLEXPIRE:
1256         //      return SADB_X_SPDEXPIRE;
1257         default:
1258                 printk("pfkey: Unknown policy event %d\n", event);
1259                 break;
1260         }
1261
1262         return 0;
1263 }
1264
1265 static inline int event2keytype(int event)
1266 {
1267         switch (event) {
1268         case XFRM_MSG_DELSA:
1269                 return SADB_DELETE;
1270         case XFRM_MSG_NEWSA:
1271                 return SADB_ADD;
1272         case XFRM_MSG_UPDSA:
1273                 return SADB_UPDATE;
1274         case XFRM_MSG_EXPIRE:
1275                 return SADB_EXPIRE;
1276         default:
1277                 printk("pfkey: Unknown SA event %d\n", event);
1278                 break;
1279         }
1280
1281         return 0;
1282 }
1283
1284 /* ADD/UPD/DEL */
1285 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1286 {
1287         struct sk_buff *skb;
1288         struct sadb_msg *hdr;
1289         int hsc = 3;
1290
1291         if (c->event == XFRM_MSG_DELSA)
1292                 hsc = 0;
1293
1294         skb = pfkey_xfrm_state2msg(x, 0, hsc);
1295
1296         if (IS_ERR(skb))
1297                 return PTR_ERR(skb);
1298
1299         hdr = (struct sadb_msg *) skb->data;
1300         hdr->sadb_msg_version = PF_KEY_V2;
1301         hdr->sadb_msg_type = event2keytype(c->event);
1302         hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1303         hdr->sadb_msg_errno = 0;
1304         hdr->sadb_msg_reserved = 0;
1305         hdr->sadb_msg_seq = c->seq;
1306         hdr->sadb_msg_pid = c->pid;
1307
1308         pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1309
1310         return 0;
1311 }
1312
1313 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1314 {
1315         struct xfrm_state *x;
1316         int err;
1317         struct km_event c;
1318
1319         xfrm_probe_algs();
1320         
1321         x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1322         if (IS_ERR(x))
1323                 return PTR_ERR(x);
1324
1325         xfrm_state_hold(x);
1326         if (hdr->sadb_msg_type == SADB_ADD)
1327                 err = xfrm_state_add(x);
1328         else
1329                 err = xfrm_state_update(x);
1330
1331         if (err < 0) {
1332                 x->km.state = XFRM_STATE_DEAD;
1333                 xfrm_state_put(x);
1334                 goto out;
1335         }
1336
1337         if (hdr->sadb_msg_type == SADB_ADD)
1338                 c.event = XFRM_MSG_NEWSA;
1339         else
1340                 c.event = XFRM_MSG_UPDSA;
1341         c.seq = hdr->sadb_msg_seq;
1342         c.pid = hdr->sadb_msg_pid;
1343         km_state_notify(x, &c);
1344 out:
1345         xfrm_state_put(x);
1346         return err;
1347 }
1348
1349 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1350 {
1351         struct xfrm_state *x;
1352         struct km_event c;
1353         int err;
1354
1355         if (!ext_hdrs[SADB_EXT_SA-1] ||
1356             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1357                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1358                 return -EINVAL;
1359
1360         x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1361         if (x == NULL)
1362                 return -ESRCH;
1363
1364         if (xfrm_state_kern(x)) {
1365                 xfrm_state_put(x);
1366                 return -EPERM;
1367         }
1368         
1369         err = xfrm_state_delete(x);
1370         if (err < 0) {
1371                 xfrm_state_put(x);
1372                 return err;
1373         }
1374
1375         c.seq = hdr->sadb_msg_seq;
1376         c.pid = hdr->sadb_msg_pid;
1377         c.event = XFRM_MSG_DELSA;
1378         km_state_notify(x, &c);
1379         xfrm_state_put(x);
1380
1381         return err;
1382 }
1383
1384 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1385 {
1386         __u8 proto;
1387         struct sk_buff *out_skb;
1388         struct sadb_msg *out_hdr;
1389         struct xfrm_state *x;
1390
1391         if (!ext_hdrs[SADB_EXT_SA-1] ||
1392             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1393                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1394                 return -EINVAL;
1395
1396         x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1397         if (x == NULL)
1398                 return -ESRCH;
1399
1400         out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1401         proto = x->id.proto;
1402         xfrm_state_put(x);
1403         if (IS_ERR(out_skb))
1404                 return  PTR_ERR(out_skb);
1405
1406         out_hdr = (struct sadb_msg *) out_skb->data;
1407         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1408         out_hdr->sadb_msg_type = SADB_DUMP;
1409         out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1410         out_hdr->sadb_msg_errno = 0;
1411         out_hdr->sadb_msg_reserved = 0;
1412         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1413         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1414         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1415
1416         return 0;
1417 }
1418
1419 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, int allocation)
1420 {
1421         struct sk_buff *skb;
1422         struct sadb_msg *hdr;
1423         int len, auth_len, enc_len, i;
1424
1425         auth_len = xfrm_count_auth_supported();
1426         if (auth_len) {
1427                 auth_len *= sizeof(struct sadb_alg);
1428                 auth_len += sizeof(struct sadb_supported);
1429         }
1430         
1431         enc_len = xfrm_count_enc_supported();
1432         if (enc_len) {
1433                 enc_len *= sizeof(struct sadb_alg);
1434                 enc_len += sizeof(struct sadb_supported);
1435         }
1436         
1437         len = enc_len + auth_len + sizeof(struct sadb_msg);
1438
1439         skb = alloc_skb(len + 16, allocation);
1440         if (!skb)
1441                 goto out_put_algs;
1442
1443         hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1444         pfkey_hdr_dup(hdr, orig);
1445         hdr->sadb_msg_errno = 0;
1446         hdr->sadb_msg_len = len / sizeof(uint64_t);
1447
1448         if (auth_len) {
1449                 struct sadb_supported *sp;
1450                 struct sadb_alg *ap;
1451
1452                 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1453                 ap = (struct sadb_alg *) (sp + 1);
1454
1455                 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1456                 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1457
1458                 for (i = 0; ; i++) {
1459                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1460                         if (!aalg)
1461                                 break;
1462                         if (aalg->available)
1463                                 *ap++ = aalg->desc;
1464                 }
1465         }
1466
1467         if (enc_len) {
1468                 struct sadb_supported *sp;
1469                 struct sadb_alg *ap;
1470
1471                 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1472                 ap = (struct sadb_alg *) (sp + 1);
1473
1474                 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1475                 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1476
1477                 for (i = 0; ; i++) {
1478                         struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1479                         if (!ealg)
1480                                 break;
1481                         if (ealg->available)
1482                                 *ap++ = ealg->desc;
1483                 }
1484         }
1485
1486 out_put_algs:
1487         return skb;
1488 }
1489
1490 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1491 {
1492         struct pfkey_sock *pfk = pfkey_sk(sk);
1493         struct sk_buff *supp_skb;
1494
1495         if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1496                 return -EINVAL;
1497
1498         if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1499                 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1500                         return -EEXIST;
1501                 pfk->registered |= (1<<hdr->sadb_msg_satype);
1502         }
1503
1504         xfrm_probe_algs();
1505         
1506         supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1507         if (!supp_skb) {
1508                 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1509                         pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1510
1511                 return -ENOBUFS;
1512         }
1513
1514         pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1515
1516         return 0;
1517 }
1518
1519 static int key_notify_sa_flush(struct km_event *c)
1520 {
1521         struct sk_buff *skb;
1522         struct sadb_msg *hdr;
1523
1524         skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1525         if (!skb)
1526                 return -ENOBUFS;
1527         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1528         hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1529         hdr->sadb_msg_seq = c->seq;
1530         hdr->sadb_msg_pid = c->pid;
1531         hdr->sadb_msg_version = PF_KEY_V2;
1532         hdr->sadb_msg_errno = (uint8_t) 0;
1533         hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1534
1535         pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1536
1537         return 0;
1538 }
1539
1540 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1541 {
1542         unsigned proto;
1543         struct km_event c;
1544
1545         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1546         if (proto == 0)
1547                 return -EINVAL;
1548
1549         xfrm_state_flush(proto);
1550         c.data.proto = proto;
1551         c.seq = hdr->sadb_msg_seq;
1552         c.pid = hdr->sadb_msg_pid;
1553         c.event = XFRM_MSG_FLUSHSA;
1554         km_state_notify(NULL, &c);
1555
1556         return 0;
1557 }
1558
1559 struct pfkey_dump_data
1560 {
1561         struct sk_buff *skb;
1562         struct sadb_msg *hdr;
1563         struct sock *sk;
1564 };
1565
1566 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1567 {
1568         struct pfkey_dump_data *data = ptr;
1569         struct sk_buff *out_skb;
1570         struct sadb_msg *out_hdr;
1571
1572         out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1573         if (IS_ERR(out_skb))
1574                 return PTR_ERR(out_skb);
1575
1576         out_hdr = (struct sadb_msg *) out_skb->data;
1577         out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1578         out_hdr->sadb_msg_type = SADB_DUMP;
1579         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1580         out_hdr->sadb_msg_errno = 0;
1581         out_hdr->sadb_msg_reserved = 0;
1582         out_hdr->sadb_msg_seq = count;
1583         out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1584         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1585         return 0;
1586 }
1587
1588 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1589 {
1590         u8 proto;
1591         struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1592
1593         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1594         if (proto == 0)
1595                 return -EINVAL;
1596
1597         return xfrm_state_walk(proto, dump_sa, &data);
1598 }
1599
1600 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1601 {
1602         struct pfkey_sock *pfk = pfkey_sk(sk);
1603         int satype = hdr->sadb_msg_satype;
1604
1605         if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1606                 /* XXX we mangle packet... */
1607                 hdr->sadb_msg_errno = 0;
1608                 if (satype != 0 && satype != 1)
1609                         return -EINVAL;
1610                 pfk->promisc = satype;
1611         }
1612         pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1613         return 0;
1614 }
1615
1616 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1617 {
1618         int i;
1619         u32 reqid = *(u32*)ptr;
1620
1621         for (i=0; i<xp->xfrm_nr; i++) {
1622                 if (xp->xfrm_vec[i].reqid == reqid)
1623                         return -EEXIST;
1624         }
1625         return 0;
1626 }
1627
1628 static u32 gen_reqid(void)
1629 {
1630         u32 start;
1631         static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1632
1633         start = reqid;
1634         do {
1635                 ++reqid;
1636                 if (reqid == 0)
1637                         reqid = IPSEC_MANUAL_REQID_MAX+1;
1638                 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1639                         return reqid;
1640         } while (reqid != start);
1641         return 0;
1642 }
1643
1644 static int
1645 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1646 {
1647         struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1648         struct sockaddr_in *sin;
1649 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1650         struct sockaddr_in6 *sin6;
1651 #endif
1652
1653         if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1654                 return -ELOOP;
1655
1656         if (rq->sadb_x_ipsecrequest_mode == 0)
1657                 return -EINVAL;
1658
1659         t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1660         t->mode = rq->sadb_x_ipsecrequest_mode-1;
1661         if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1662                 t->optional = 1;
1663         else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1664                 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1665                 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1666                         t->reqid = 0;
1667                 if (!t->reqid && !(t->reqid = gen_reqid()))
1668                         return -ENOBUFS;
1669         }
1670
1671         /* addresses present only in tunnel mode */
1672         if (t->mode) {
1673                 switch (xp->family) {
1674                 case AF_INET:
1675                         sin = (void*)(rq+1);
1676                         if (sin->sin_family != AF_INET)
1677                                 return -EINVAL;
1678                         t->saddr.a4 = sin->sin_addr.s_addr;
1679                         sin++;
1680                         if (sin->sin_family != AF_INET)
1681                                 return -EINVAL;
1682                         t->id.daddr.a4 = sin->sin_addr.s_addr;
1683                         break;
1684 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1685                 case AF_INET6:
1686                         sin6 = (void *)(rq+1);
1687                         if (sin6->sin6_family != AF_INET6)
1688                                 return -EINVAL;
1689                         memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1690                         sin6++;
1691                         if (sin6->sin6_family != AF_INET6)
1692                                 return -EINVAL;
1693                         memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1694                         break;
1695 #endif
1696                 default:
1697                         return -EINVAL;
1698                 }
1699         }
1700         /* No way to set this via kame pfkey */
1701         t->aalgos = t->ealgos = t->calgos = ~0;
1702         xp->xfrm_nr++;
1703         return 0;
1704 }
1705
1706 static int
1707 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1708 {
1709         int err;
1710         int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1711         struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1712
1713         while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1714                 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1715                         return err;
1716                 len -= rq->sadb_x_ipsecrequest_len;
1717                 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1718         }
1719         return 0;
1720 }
1721
1722 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1723 {
1724         int sockaddr_size = pfkey_sockaddr_size(xp->family);
1725         int socklen = (xp->family == AF_INET ?
1726                        sizeof(struct sockaddr_in) :
1727                        sizeof(struct sockaddr_in6));
1728
1729         return sizeof(struct sadb_msg) +
1730                 (sizeof(struct sadb_lifetime) * 3) +
1731                 (sizeof(struct sadb_address) * 2) + 
1732                 (sockaddr_size * 2) +
1733                 sizeof(struct sadb_x_policy) +
1734                 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1735                                 (socklen * 2)));
1736 }
1737
1738 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1739 {
1740         struct sk_buff *skb;
1741         int size;
1742
1743         size = pfkey_xfrm_policy2msg_size(xp);
1744
1745         skb =  alloc_skb(size + 16, GFP_ATOMIC);
1746         if (skb == NULL)
1747                 return ERR_PTR(-ENOBUFS);
1748
1749         return skb;
1750 }
1751
1752 static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1753 {
1754         struct sadb_msg *hdr;
1755         struct sadb_address *addr;
1756         struct sadb_lifetime *lifetime;
1757         struct sadb_x_policy *pol;
1758         struct sockaddr_in   *sin;
1759 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1760         struct sockaddr_in6  *sin6;
1761 #endif
1762         int i;
1763         int size;
1764         int sockaddr_size = pfkey_sockaddr_size(xp->family);
1765         int socklen = (xp->family == AF_INET ?
1766                        sizeof(struct sockaddr_in) :
1767                        sizeof(struct sockaddr_in6));
1768
1769         size = pfkey_xfrm_policy2msg_size(xp);
1770
1771         /* call should fill header later */
1772         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1773         memset(hdr, 0, size);   /* XXX do we need this ? */
1774
1775         /* src address */
1776         addr = (struct sadb_address*) skb_put(skb, 
1777                                               sizeof(struct sadb_address)+sockaddr_size);
1778         addr->sadb_address_len = 
1779                 (sizeof(struct sadb_address)+sockaddr_size)/
1780                         sizeof(uint64_t);
1781         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1782         addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1783         addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1784         addr->sadb_address_reserved = 0;
1785         /* src address */
1786         if (xp->family == AF_INET) {
1787                 sin = (struct sockaddr_in *) (addr + 1);
1788                 sin->sin_family = AF_INET;
1789                 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1790                 sin->sin_port = xp->selector.sport;
1791                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1792         }
1793 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1794         else if (xp->family == AF_INET6) {
1795                 sin6 = (struct sockaddr_in6 *) (addr + 1);
1796                 sin6->sin6_family = AF_INET6;
1797                 sin6->sin6_port = xp->selector.sport;
1798                 sin6->sin6_flowinfo = 0;
1799                 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1800                        sizeof(struct in6_addr));
1801                 sin6->sin6_scope_id = 0;
1802         }
1803 #endif
1804         else
1805                 BUG();
1806
1807         /* dst address */
1808         addr = (struct sadb_address*) skb_put(skb, 
1809                                               sizeof(struct sadb_address)+sockaddr_size);
1810         addr->sadb_address_len =
1811                 (sizeof(struct sadb_address)+sockaddr_size)/
1812                         sizeof(uint64_t);
1813         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1814         addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1815         addr->sadb_address_prefixlen = xp->selector.prefixlen_d; 
1816         addr->sadb_address_reserved = 0;
1817         if (xp->family == AF_INET) {
1818                 sin = (struct sockaddr_in *) (addr + 1);
1819                 sin->sin_family = AF_INET;
1820                 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1821                 sin->sin_port = xp->selector.dport;
1822                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1823         }
1824 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1825         else if (xp->family == AF_INET6) {
1826                 sin6 = (struct sockaddr_in6 *) (addr + 1);
1827                 sin6->sin6_family = AF_INET6;
1828                 sin6->sin6_port = xp->selector.dport;
1829                 sin6->sin6_flowinfo = 0;
1830                 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1831                        sizeof(struct in6_addr));
1832                 sin6->sin6_scope_id = 0;
1833         }
1834 #endif
1835         else
1836                 BUG();
1837
1838         /* hard time */
1839         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1840                                                      sizeof(struct sadb_lifetime));
1841         lifetime->sadb_lifetime_len =
1842                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1843         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1844         lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.hard_packet_limit);
1845         lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1846         lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1847         lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1848         /* soft time */
1849         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1850                                                      sizeof(struct sadb_lifetime));
1851         lifetime->sadb_lifetime_len =
1852                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1853         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1854         lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.soft_packet_limit);
1855         lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1856         lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1857         lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1858         /* current time */
1859         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1860                                                      sizeof(struct sadb_lifetime));
1861         lifetime->sadb_lifetime_len =
1862                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1863         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1864         lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1865         lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1866         lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1867         lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1868
1869         pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
1870         pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1871         pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1872         pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1873         if (xp->action == XFRM_POLICY_ALLOW) {
1874                 if (xp->xfrm_nr)
1875                         pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1876                 else
1877                         pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1878         }
1879         pol->sadb_x_policy_dir = dir+1;
1880         pol->sadb_x_policy_id = xp->index;
1881         pol->sadb_x_policy_priority = xp->priority;
1882
1883         for (i=0; i<xp->xfrm_nr; i++) {
1884                 struct sadb_x_ipsecrequest *rq;
1885                 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1886                 int req_size;
1887
1888                 req_size = sizeof(struct sadb_x_ipsecrequest);
1889                 if (t->mode)
1890                         req_size += 2*socklen;
1891                 else
1892                         size -= 2*socklen;
1893                 rq = (void*)skb_put(skb, req_size);
1894                 pol->sadb_x_policy_len += req_size/8;
1895                 memset(rq, 0, sizeof(*rq));
1896                 rq->sadb_x_ipsecrequest_len = req_size;
1897                 rq->sadb_x_ipsecrequest_proto = t->id.proto;
1898                 rq->sadb_x_ipsecrequest_mode = t->mode+1;
1899                 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1900                 if (t->reqid)
1901                         rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1902                 if (t->optional)
1903                         rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
1904                 rq->sadb_x_ipsecrequest_reqid = t->reqid;
1905                 if (t->mode) {
1906                         switch (xp->family) {
1907                         case AF_INET:
1908                                 sin = (void*)(rq+1);
1909                                 sin->sin_family = AF_INET;
1910                                 sin->sin_addr.s_addr = t->saddr.a4;
1911                                 sin->sin_port = 0;
1912                                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1913                                 sin++;
1914                                 sin->sin_family = AF_INET;
1915                                 sin->sin_addr.s_addr = t->id.daddr.a4;
1916                                 sin->sin_port = 0;
1917                                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1918                                 break;
1919 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1920                         case AF_INET6:
1921                                 sin6 = (void*)(rq+1);
1922                                 sin6->sin6_family = AF_INET6;
1923                                 sin6->sin6_port = 0;
1924                                 sin6->sin6_flowinfo = 0;
1925                                 memcpy(&sin6->sin6_addr, t->saddr.a6,
1926                                        sizeof(struct in6_addr));
1927                                 sin6->sin6_scope_id = 0;
1928
1929                                 sin6++;
1930                                 sin6->sin6_family = AF_INET6;
1931                                 sin6->sin6_port = 0;
1932                                 sin6->sin6_flowinfo = 0;
1933                                 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
1934                                        sizeof(struct in6_addr));
1935                                 sin6->sin6_scope_id = 0;
1936                                 break;
1937 #endif
1938                         default:
1939                                 break;
1940                         }
1941                 }
1942         }
1943         hdr->sadb_msg_len = size / sizeof(uint64_t);
1944         hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
1945 }
1946
1947 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1948 {
1949         struct sk_buff *out_skb;
1950         struct sadb_msg *out_hdr;
1951         int err;
1952
1953         out_skb = pfkey_xfrm_policy2msg_prep(xp);
1954         if (IS_ERR(out_skb)) {
1955                 err = PTR_ERR(out_skb);
1956                 goto out;
1957         }
1958         pfkey_xfrm_policy2msg(out_skb, xp, dir);
1959
1960         out_hdr = (struct sadb_msg *) out_skb->data;
1961         out_hdr->sadb_msg_version = PF_KEY_V2;
1962
1963         if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
1964                 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
1965         else
1966                 out_hdr->sadb_msg_type = event2poltype(c->event);
1967         out_hdr->sadb_msg_errno = 0;
1968         out_hdr->sadb_msg_seq = c->seq;
1969         out_hdr->sadb_msg_pid = c->pid;
1970         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1971 out:
1972         return 0;
1973
1974 }
1975
1976 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1977 {
1978         int err;
1979         struct sadb_lifetime *lifetime;
1980         struct sadb_address *sa;
1981         struct sadb_x_policy *pol;
1982         struct xfrm_policy *xp;
1983         struct km_event c;
1984
1985         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1986                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1987             !ext_hdrs[SADB_X_EXT_POLICY-1])
1988                 return -EINVAL;
1989
1990         pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1991         if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
1992                 return -EINVAL;
1993         if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1994                 return -EINVAL;
1995
1996         xp = xfrm_policy_alloc(GFP_KERNEL);
1997         if (xp == NULL)
1998                 return -ENOBUFS;
1999
2000         xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2001                       XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2002         xp->priority = pol->sadb_x_policy_priority;
2003
2004         sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
2005         xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2006         if (!xp->family) {
2007                 err = -EINVAL;
2008                 goto out;
2009         }
2010         xp->selector.family = xp->family;
2011         xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2012         xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2013         xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2014         if (xp->selector.sport)
2015                 xp->selector.sport_mask = ~0;
2016
2017         sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
2018         pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2019         xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2020
2021         /* Amusing, we set this twice.  KAME apps appear to set same value
2022          * in both addresses.
2023          */
2024         xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2025
2026         xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2027         if (xp->selector.dport)
2028                 xp->selector.dport_mask = ~0;
2029
2030         xp->lft.soft_byte_limit = XFRM_INF;
2031         xp->lft.hard_byte_limit = XFRM_INF;
2032         xp->lft.soft_packet_limit = XFRM_INF;
2033         xp->lft.hard_packet_limit = XFRM_INF;
2034         if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2035                 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2036                 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2037                 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2038                 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2039         }
2040         if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2041                 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2042                 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2043                 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2044                 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2045         }
2046         xp->xfrm_nr = 0;
2047         if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2048             (err = parse_ipsecrequests(xp, pol)) < 0)
2049                 goto out;
2050
2051         err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2052                                  hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2053         if (err) {
2054                 kfree(xp);
2055                 return err;
2056         }
2057
2058         if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2059                 c.event = XFRM_MSG_UPDPOLICY;
2060         else 
2061                 c.event = XFRM_MSG_NEWPOLICY;
2062
2063         c.seq = hdr->sadb_msg_seq;
2064         c.pid = hdr->sadb_msg_pid;
2065
2066         km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2067         xfrm_pol_put(xp);
2068         return 0;
2069
2070 out:
2071         kfree(xp);
2072         return err;
2073 }
2074
2075 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2076 {
2077         int err;
2078         struct sadb_address *sa;
2079         struct sadb_x_policy *pol;
2080         struct xfrm_policy *xp;
2081         struct xfrm_selector sel;
2082         struct km_event c;
2083
2084         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2085                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2086             !ext_hdrs[SADB_X_EXT_POLICY-1])
2087                 return -EINVAL;
2088
2089         pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2090         if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2091                 return -EINVAL;
2092
2093         memset(&sel, 0, sizeof(sel));
2094
2095         sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
2096         sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2097         sel.prefixlen_s = sa->sadb_address_prefixlen;
2098         sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2099         sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2100         if (sel.sport)
2101                 sel.sport_mask = ~0;
2102
2103         sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
2104         pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2105         sel.prefixlen_d = sa->sadb_address_prefixlen;
2106         sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2107         sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2108         if (sel.dport)
2109                 sel.dport_mask = ~0;
2110
2111         xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
2112         if (xp == NULL)
2113                 return -ENOENT;
2114
2115         err = 0;
2116
2117         c.seq = hdr->sadb_msg_seq;
2118         c.pid = hdr->sadb_msg_pid;
2119         c.event = XFRM_MSG_DELPOLICY;
2120         km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2121
2122         xfrm_pol_put(xp);
2123         return err;
2124 }
2125
2126 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2127 {
2128         int err;
2129         struct sk_buff *out_skb;
2130         struct sadb_msg *out_hdr;
2131         err = 0;
2132
2133         out_skb = pfkey_xfrm_policy2msg_prep(xp);
2134         if (IS_ERR(out_skb)) {
2135                 err =  PTR_ERR(out_skb);
2136                 goto out;
2137         }
2138         pfkey_xfrm_policy2msg(out_skb, xp, dir);
2139
2140         out_hdr = (struct sadb_msg *) out_skb->data;
2141         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2142         out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2143         out_hdr->sadb_msg_satype = 0;
2144         out_hdr->sadb_msg_errno = 0;
2145         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2146         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2147         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2148         err = 0;
2149
2150 out:
2151         return err;
2152 }
2153
2154 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2155 {
2156         int err;
2157         struct sadb_x_policy *pol;
2158         struct xfrm_policy *xp;
2159         struct km_event c;
2160
2161         if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2162                 return -EINVAL;
2163
2164         xp = xfrm_policy_byid(0, pol->sadb_x_policy_id,
2165                               hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2166         if (xp == NULL)
2167                 return -ENOENT;
2168
2169         err = 0;
2170
2171         c.seq = hdr->sadb_msg_seq;
2172         c.pid = hdr->sadb_msg_pid;
2173         if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
2174                 c.data.byid = 1;
2175                 c.event = XFRM_MSG_DELPOLICY;
2176                 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2177         } else {
2178                 err = key_pol_get_resp(sk, xp, hdr, pol->sadb_x_policy_dir-1);
2179         }
2180
2181         xfrm_pol_put(xp);
2182         return err;
2183 }
2184
2185 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2186 {
2187         struct pfkey_dump_data *data = ptr;
2188         struct sk_buff *out_skb;
2189         struct sadb_msg *out_hdr;
2190
2191         out_skb = pfkey_xfrm_policy2msg_prep(xp);
2192         if (IS_ERR(out_skb))
2193                 return PTR_ERR(out_skb);
2194
2195         pfkey_xfrm_policy2msg(out_skb, xp, dir);
2196
2197         out_hdr = (struct sadb_msg *) out_skb->data;
2198         out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2199         out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2200         out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2201         out_hdr->sadb_msg_errno = 0;
2202         out_hdr->sadb_msg_seq = count;
2203         out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2204         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2205         return 0;
2206 }
2207
2208 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2209 {
2210         struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2211
2212         return xfrm_policy_walk(dump_sp, &data);
2213 }
2214
2215 static int key_notify_policy_flush(struct km_event *c)
2216 {
2217         struct sk_buff *skb_out;
2218         struct sadb_msg *hdr;
2219
2220         skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2221         if (!skb_out)
2222                 return -ENOBUFS;
2223         hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2224         hdr->sadb_msg_seq = c->seq;
2225         hdr->sadb_msg_pid = c->pid;
2226         hdr->sadb_msg_version = PF_KEY_V2;
2227         hdr->sadb_msg_errno = (uint8_t) 0;
2228         hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2229         pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2230         return 0;
2231
2232 }
2233
2234 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2235 {
2236         struct km_event c;
2237
2238         xfrm_policy_flush();
2239         c.event = XFRM_MSG_FLUSHPOLICY;
2240         c.pid = hdr->sadb_msg_pid;
2241         c.seq = hdr->sadb_msg_seq;
2242         km_policy_notify(NULL, 0, &c);
2243
2244         return 0;
2245 }
2246
2247 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2248                              struct sadb_msg *hdr, void **ext_hdrs);
2249 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2250         [SADB_RESERVED]         = pfkey_reserved,
2251         [SADB_GETSPI]           = pfkey_getspi,
2252         [SADB_UPDATE]           = pfkey_add,
2253         [SADB_ADD]              = pfkey_add,
2254         [SADB_DELETE]           = pfkey_delete,
2255         [SADB_GET]              = pfkey_get,
2256         [SADB_ACQUIRE]          = pfkey_acquire,
2257         [SADB_REGISTER]         = pfkey_register,
2258         [SADB_EXPIRE]           = NULL,
2259         [SADB_FLUSH]            = pfkey_flush,
2260         [SADB_DUMP]             = pfkey_dump,
2261         [SADB_X_PROMISC]        = pfkey_promisc,
2262         [SADB_X_PCHANGE]        = NULL,
2263         [SADB_X_SPDUPDATE]      = pfkey_spdadd,
2264         [SADB_X_SPDADD]         = pfkey_spdadd,
2265         [SADB_X_SPDDELETE]      = pfkey_spddelete,
2266         [SADB_X_SPDGET]         = pfkey_spdget,
2267         [SADB_X_SPDACQUIRE]     = NULL,
2268         [SADB_X_SPDDUMP]        = pfkey_spddump,
2269         [SADB_X_SPDFLUSH]       = pfkey_spdflush,
2270         [SADB_X_SPDSETIDX]      = pfkey_spdadd,
2271         [SADB_X_SPDDELETE2]     = pfkey_spdget,
2272 };
2273
2274 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2275 {
2276         void *ext_hdrs[SADB_EXT_MAX];
2277         int err;
2278
2279         pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2280                         BROADCAST_PROMISC_ONLY, NULL);
2281
2282         memset(ext_hdrs, 0, sizeof(ext_hdrs));
2283         err = parse_exthdrs(skb, hdr, ext_hdrs);
2284         if (!err) {
2285                 err = -EOPNOTSUPP;
2286                 if (pfkey_funcs[hdr->sadb_msg_type])
2287                         err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2288         }
2289         return err;
2290 }
2291
2292 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2293 {
2294         struct sadb_msg *hdr = NULL;
2295
2296         if (skb->len < sizeof(*hdr)) {
2297                 *errp = -EMSGSIZE;
2298         } else {
2299                 hdr = (struct sadb_msg *) skb->data;
2300                 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2301                     hdr->sadb_msg_reserved != 0 ||
2302                     (hdr->sadb_msg_type <= SADB_RESERVED ||
2303                      hdr->sadb_msg_type > SADB_MAX)) {
2304                         hdr = NULL;
2305                         *errp = -EINVAL;
2306                 } else if (hdr->sadb_msg_len != (skb->len /
2307                                                  sizeof(uint64_t)) ||
2308                            hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2309                                                 sizeof(uint64_t))) {
2310                         hdr = NULL;
2311                         *errp = -EMSGSIZE;
2312                 } else {
2313                         *errp = 0;
2314                 }
2315         }
2316         return hdr;
2317 }
2318
2319 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2320 {
2321         return t->aalgos & (1 << d->desc.sadb_alg_id);
2322 }
2323
2324 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2325 {
2326         return t->ealgos & (1 << d->desc.sadb_alg_id);
2327 }
2328
2329 static int count_ah_combs(struct xfrm_tmpl *t)
2330 {
2331         int i, sz = 0;
2332
2333         for (i = 0; ; i++) {
2334                 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2335                 if (!aalg)
2336                         break;
2337                 if (aalg_tmpl_set(t, aalg) && aalg->available)
2338                         sz += sizeof(struct sadb_comb);
2339         }
2340         return sz + sizeof(struct sadb_prop);
2341 }
2342
2343 static int count_esp_combs(struct xfrm_tmpl *t)
2344 {
2345         int i, k, sz = 0;
2346
2347         for (i = 0; ; i++) {
2348                 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2349                 if (!ealg)
2350                         break;
2351                         
2352                 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2353                         continue;
2354                         
2355                 for (k = 1; ; k++) {
2356                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2357                         if (!aalg)
2358                                 break;
2359                                 
2360                         if (aalg_tmpl_set(t, aalg) && aalg->available)
2361                                 sz += sizeof(struct sadb_comb);
2362                 }
2363         }
2364         return sz + sizeof(struct sadb_prop);
2365 }
2366
2367 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2368 {
2369         struct sadb_prop *p;
2370         int i;
2371
2372         p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2373         p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2374         p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2375         p->sadb_prop_replay = 32;
2376         memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2377
2378         for (i = 0; ; i++) {
2379                 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2380                 if (!aalg)
2381                         break;
2382
2383                 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2384                         struct sadb_comb *c;
2385                         c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2386                         memset(c, 0, sizeof(*c));
2387                         p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2388                         c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2389                         c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2390                         c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2391                         c->sadb_comb_hard_addtime = 24*60*60;
2392                         c->sadb_comb_soft_addtime = 20*60*60;
2393                         c->sadb_comb_hard_usetime = 8*60*60;
2394                         c->sadb_comb_soft_usetime = 7*60*60;
2395                 }
2396         }
2397 }
2398
2399 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2400 {
2401         struct sadb_prop *p;
2402         int i, k;
2403
2404         p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2405         p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2406         p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2407         p->sadb_prop_replay = 32;
2408         memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2409
2410         for (i=0; ; i++) {
2411                 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2412                 if (!ealg)
2413                         break;
2414         
2415                 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2416                         continue;
2417                         
2418                 for (k = 1; ; k++) {
2419                         struct sadb_comb *c;
2420                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2421                         if (!aalg)
2422                                 break;
2423                         if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2424                                 continue;
2425                         c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2426                         memset(c, 0, sizeof(*c));
2427                         p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2428                         c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2429                         c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2430                         c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2431                         c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2432                         c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2433                         c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2434                         c->sadb_comb_hard_addtime = 24*60*60;
2435                         c->sadb_comb_soft_addtime = 20*60*60;
2436                         c->sadb_comb_hard_usetime = 8*60*60;
2437                         c->sadb_comb_soft_usetime = 7*60*60;
2438                 }
2439         }
2440 }
2441
2442 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2443 {
2444         return 0;
2445 }
2446
2447 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2448 {
2449         struct sk_buff *out_skb;
2450         struct sadb_msg *out_hdr;
2451         int hard;
2452         int hsc;
2453
2454         hard = c->data.hard;
2455         if (hard)
2456                 hsc = 2;
2457         else
2458                 hsc = 1;
2459
2460         out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2461         if (IS_ERR(out_skb))
2462                 return PTR_ERR(out_skb);
2463
2464         out_hdr = (struct sadb_msg *) out_skb->data;
2465         out_hdr->sadb_msg_version = PF_KEY_V2;
2466         out_hdr->sadb_msg_type = SADB_EXPIRE;
2467         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2468         out_hdr->sadb_msg_errno = 0;
2469         out_hdr->sadb_msg_reserved = 0;
2470         out_hdr->sadb_msg_seq = 0;
2471         out_hdr->sadb_msg_pid = 0;
2472
2473         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2474         return 0;
2475 }
2476
2477 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2478 {
2479         switch (c->event) {
2480         case XFRM_MSG_EXPIRE:
2481                 return key_notify_sa_expire(x, c);
2482         case XFRM_MSG_DELSA:
2483         case XFRM_MSG_NEWSA:
2484         case XFRM_MSG_UPDSA:
2485                 return key_notify_sa(x, c);
2486         case XFRM_MSG_FLUSHSA:
2487                 return key_notify_sa_flush(c);
2488         default:
2489                 printk("pfkey: Unknown SA event %d\n", c->event);
2490                 break;
2491         }
2492
2493         return 0;
2494 }
2495
2496 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2497 {
2498         switch (c->event) {
2499         case XFRM_MSG_POLEXPIRE:
2500                 return key_notify_policy_expire(xp, c);
2501         case XFRM_MSG_DELPOLICY:
2502         case XFRM_MSG_NEWPOLICY:
2503         case XFRM_MSG_UPDPOLICY:
2504                 return key_notify_policy(xp, dir, c);
2505         case XFRM_MSG_FLUSHPOLICY:
2506                 return key_notify_policy_flush(c);
2507         default:
2508                 printk("pfkey: Unknown policy event %d\n", c->event);
2509                 break;
2510         }
2511
2512         return 0;
2513 }
2514
2515 static u32 get_acqseq(void)
2516 {
2517         u32 res;
2518         static u32 acqseq;
2519         static DEFINE_SPINLOCK(acqseq_lock);
2520
2521         spin_lock_bh(&acqseq_lock);
2522         res = (++acqseq ? : ++acqseq);
2523         spin_unlock_bh(&acqseq_lock);
2524         return res;
2525 }
2526
2527 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2528 {
2529         struct sk_buff *skb;
2530         struct sadb_msg *hdr;
2531         struct sadb_address *addr;
2532         struct sadb_x_policy *pol;
2533         struct sockaddr_in *sin;
2534 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2535         struct sockaddr_in6 *sin6;
2536 #endif
2537         int sockaddr_size;
2538         int size;
2539         
2540         sockaddr_size = pfkey_sockaddr_size(x->props.family);
2541         if (!sockaddr_size)
2542                 return -EINVAL;
2543
2544         size = sizeof(struct sadb_msg) +
2545                 (sizeof(struct sadb_address) * 2) +
2546                 (sockaddr_size * 2) +
2547                 sizeof(struct sadb_x_policy);
2548         
2549         if (x->id.proto == IPPROTO_AH)
2550                 size += count_ah_combs(t);
2551         else if (x->id.proto == IPPROTO_ESP)
2552                 size += count_esp_combs(t);
2553
2554         skb =  alloc_skb(size + 16, GFP_ATOMIC);
2555         if (skb == NULL)
2556                 return -ENOMEM;
2557         
2558         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2559         hdr->sadb_msg_version = PF_KEY_V2;
2560         hdr->sadb_msg_type = SADB_ACQUIRE;
2561         hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2562         hdr->sadb_msg_len = size / sizeof(uint64_t);
2563         hdr->sadb_msg_errno = 0;
2564         hdr->sadb_msg_reserved = 0;
2565         hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2566         hdr->sadb_msg_pid = 0;
2567
2568         /* src address */
2569         addr = (struct sadb_address*) skb_put(skb, 
2570                                               sizeof(struct sadb_address)+sockaddr_size);
2571         addr->sadb_address_len = 
2572                 (sizeof(struct sadb_address)+sockaddr_size)/
2573                         sizeof(uint64_t);
2574         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2575         addr->sadb_address_proto = 0;
2576         addr->sadb_address_reserved = 0;
2577         if (x->props.family == AF_INET) {
2578                 addr->sadb_address_prefixlen = 32;
2579
2580                 sin = (struct sockaddr_in *) (addr + 1);
2581                 sin->sin_family = AF_INET;
2582                 sin->sin_addr.s_addr = x->props.saddr.a4;
2583                 sin->sin_port = 0;
2584                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2585         }
2586 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2587         else if (x->props.family == AF_INET6) {
2588                 addr->sadb_address_prefixlen = 128;
2589
2590                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2591                 sin6->sin6_family = AF_INET6;
2592                 sin6->sin6_port = 0;
2593                 sin6->sin6_flowinfo = 0;
2594                 memcpy(&sin6->sin6_addr,
2595                        x->props.saddr.a6, sizeof(struct in6_addr));
2596                 sin6->sin6_scope_id = 0;
2597         }
2598 #endif
2599         else
2600                 BUG();
2601         
2602         /* dst address */
2603         addr = (struct sadb_address*) skb_put(skb, 
2604                                               sizeof(struct sadb_address)+sockaddr_size);
2605         addr->sadb_address_len =
2606                 (sizeof(struct sadb_address)+sockaddr_size)/
2607                         sizeof(uint64_t);
2608         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2609         addr->sadb_address_proto = 0;
2610         addr->sadb_address_reserved = 0;
2611         if (x->props.family == AF_INET) {
2612                 addr->sadb_address_prefixlen = 32; 
2613
2614                 sin = (struct sockaddr_in *) (addr + 1);
2615                 sin->sin_family = AF_INET;
2616                 sin->sin_addr.s_addr = x->id.daddr.a4;
2617                 sin->sin_port = 0;
2618                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2619         }
2620 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2621         else if (x->props.family == AF_INET6) {
2622                 addr->sadb_address_prefixlen = 128; 
2623
2624                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2625                 sin6->sin6_family = AF_INET6;
2626                 sin6->sin6_port = 0;
2627                 sin6->sin6_flowinfo = 0;
2628                 memcpy(&sin6->sin6_addr,
2629                        x->id.daddr.a6, sizeof(struct in6_addr));
2630                 sin6->sin6_scope_id = 0;
2631         }
2632 #endif
2633         else
2634                 BUG();
2635
2636         pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
2637         pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2638         pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2639         pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2640         pol->sadb_x_policy_dir = dir+1;
2641         pol->sadb_x_policy_id = xp->index;
2642
2643         /* Set sadb_comb's. */
2644         if (x->id.proto == IPPROTO_AH)
2645                 dump_ah_combs(skb, t);
2646         else if (x->id.proto == IPPROTO_ESP)
2647                 dump_esp_combs(skb, t);
2648
2649         return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2650 }
2651
2652 static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2653                                                 u8 *data, int len, int *dir)
2654 {
2655         struct xfrm_policy *xp;
2656         struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2657
2658         switch (family) {
2659         case AF_INET:
2660                 if (opt != IP_IPSEC_POLICY) {
2661                         *dir = -EOPNOTSUPP;
2662                         return NULL;
2663                 }
2664                 break;
2665 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2666         case AF_INET6:
2667                 if (opt != IPV6_IPSEC_POLICY) {
2668                         *dir = -EOPNOTSUPP;
2669                         return NULL;
2670                 }
2671                 break;
2672 #endif
2673         default:
2674                 *dir = -EINVAL;
2675                 return NULL;
2676         }
2677
2678         *dir = -EINVAL;
2679
2680         if (len < sizeof(struct sadb_x_policy) ||
2681             pol->sadb_x_policy_len*8 > len ||
2682             pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2683             (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2684                 return NULL;
2685
2686         xp = xfrm_policy_alloc(GFP_ATOMIC);
2687         if (xp == NULL) {
2688                 *dir = -ENOBUFS;
2689                 return NULL;
2690         }
2691
2692         xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2693                       XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2694
2695         xp->lft.soft_byte_limit = XFRM_INF;
2696         xp->lft.hard_byte_limit = XFRM_INF;
2697         xp->lft.soft_packet_limit = XFRM_INF;
2698         xp->lft.hard_packet_limit = XFRM_INF;
2699         xp->family = family;
2700
2701         xp->xfrm_nr = 0;
2702         if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2703             (*dir = parse_ipsecrequests(xp, pol)) < 0)
2704                 goto out;
2705
2706         *dir = pol->sadb_x_policy_dir-1;
2707         return xp;
2708
2709 out:
2710         kfree(xp);
2711         return NULL;
2712 }
2713
2714 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2715 {
2716         struct sk_buff *skb;
2717         struct sadb_msg *hdr;
2718         struct sadb_sa *sa;
2719         struct sadb_address *addr;
2720         struct sadb_x_nat_t_port *n_port;
2721         struct sockaddr_in *sin;
2722 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2723         struct sockaddr_in6 *sin6;
2724 #endif
2725         int sockaddr_size;
2726         int size;
2727         __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2728         struct xfrm_encap_tmpl *natt = NULL;
2729
2730         sockaddr_size = pfkey_sockaddr_size(x->props.family);
2731         if (!sockaddr_size)
2732                 return -EINVAL;
2733
2734         if (!satype)
2735                 return -EINVAL;
2736
2737         if (!x->encap)
2738                 return -EINVAL;
2739
2740         natt = x->encap;
2741
2742         /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2743          *
2744          * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2745          * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2746          */
2747         
2748         size = sizeof(struct sadb_msg) +
2749                 sizeof(struct sadb_sa) +
2750                 (sizeof(struct sadb_address) * 2) +
2751                 (sockaddr_size * 2) +
2752                 (sizeof(struct sadb_x_nat_t_port) * 2);
2753         
2754         skb =  alloc_skb(size + 16, GFP_ATOMIC);
2755         if (skb == NULL)
2756                 return -ENOMEM;
2757         
2758         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2759         hdr->sadb_msg_version = PF_KEY_V2;
2760         hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2761         hdr->sadb_msg_satype = satype;
2762         hdr->sadb_msg_len = size / sizeof(uint64_t);
2763         hdr->sadb_msg_errno = 0;
2764         hdr->sadb_msg_reserved = 0;
2765         hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2766         hdr->sadb_msg_pid = 0;
2767
2768         /* SA */
2769         sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2770         sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2771         sa->sadb_sa_exttype = SADB_EXT_SA;
2772         sa->sadb_sa_spi = x->id.spi;
2773         sa->sadb_sa_replay = 0;
2774         sa->sadb_sa_state = 0;
2775         sa->sadb_sa_auth = 0;
2776         sa->sadb_sa_encrypt = 0;
2777         sa->sadb_sa_flags = 0;
2778
2779         /* ADDRESS_SRC (old addr) */
2780         addr = (struct sadb_address*)
2781                 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2782         addr->sadb_address_len = 
2783                 (sizeof(struct sadb_address)+sockaddr_size)/
2784                         sizeof(uint64_t);
2785         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2786         addr->sadb_address_proto = 0;
2787         addr->sadb_address_reserved = 0;
2788         if (x->props.family == AF_INET) {
2789                 addr->sadb_address_prefixlen = 32;
2790
2791                 sin = (struct sockaddr_in *) (addr + 1);
2792                 sin->sin_family = AF_INET;
2793                 sin->sin_addr.s_addr = x->props.saddr.a4;
2794                 sin->sin_port = 0;
2795                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2796         }
2797 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2798         else if (x->props.family == AF_INET6) {
2799                 addr->sadb_address_prefixlen = 128;
2800
2801                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2802                 sin6->sin6_family = AF_INET6;
2803                 sin6->sin6_port = 0;
2804                 sin6->sin6_flowinfo = 0;
2805                 memcpy(&sin6->sin6_addr,
2806                        x->props.saddr.a6, sizeof(struct in6_addr));
2807                 sin6->sin6_scope_id = 0;
2808         }
2809 #endif
2810         else
2811                 BUG();
2812
2813         /* NAT_T_SPORT (old port) */
2814         n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2815         n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2816         n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
2817         n_port->sadb_x_nat_t_port_port = natt->encap_sport;
2818         n_port->sadb_x_nat_t_port_reserved = 0;
2819
2820         /* ADDRESS_DST (new addr) */
2821         addr = (struct sadb_address*)
2822                 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2823         addr->sadb_address_len = 
2824                 (sizeof(struct sadb_address)+sockaddr_size)/
2825                         sizeof(uint64_t);
2826         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2827         addr->sadb_address_proto = 0;
2828         addr->sadb_address_reserved = 0;
2829         if (x->props.family == AF_INET) {
2830                 addr->sadb_address_prefixlen = 32;
2831
2832                 sin = (struct sockaddr_in *) (addr + 1);
2833                 sin->sin_family = AF_INET;
2834                 sin->sin_addr.s_addr = ipaddr->a4;
2835                 sin->sin_port = 0;
2836                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2837         }
2838 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2839         else if (x->props.family == AF_INET6) {
2840                 addr->sadb_address_prefixlen = 128;
2841
2842                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2843                 sin6->sin6_family = AF_INET6;
2844                 sin6->sin6_port = 0;
2845                 sin6->sin6_flowinfo = 0;
2846                 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
2847                 sin6->sin6_scope_id = 0;
2848         }
2849 #endif
2850         else
2851                 BUG();
2852
2853         /* NAT_T_DPORT (new port) */
2854         n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2855         n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2856         n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
2857         n_port->sadb_x_nat_t_port_port = sport;
2858         n_port->sadb_x_nat_t_port_reserved = 0;
2859
2860         return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2861 }
2862
2863 static int pfkey_sendmsg(struct kiocb *kiocb,
2864                          struct socket *sock, struct msghdr *msg, size_t len)
2865 {
2866         struct sock *sk = sock->sk;
2867         struct sk_buff *skb = NULL;
2868         struct sadb_msg *hdr = NULL;
2869         int err;
2870
2871         err = -EOPNOTSUPP;
2872         if (msg->msg_flags & MSG_OOB)
2873                 goto out;
2874
2875         err = -EMSGSIZE;
2876         if ((unsigned)len > sk->sk_sndbuf - 32)
2877                 goto out;
2878
2879         err = -ENOBUFS;
2880         skb = alloc_skb(len, GFP_KERNEL);
2881         if (skb == NULL)
2882                 goto out;
2883
2884         err = -EFAULT;
2885         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
2886                 goto out;
2887
2888         hdr = pfkey_get_base_msg(skb, &err);
2889         if (!hdr)
2890                 goto out;
2891
2892         down(&xfrm_cfg_sem);
2893         err = pfkey_process(sk, skb, hdr);
2894         up(&xfrm_cfg_sem);
2895
2896 out:
2897         if (err && hdr && pfkey_error(hdr, err, sk) == 0)
2898                 err = 0;
2899         if (skb)
2900                 kfree_skb(skb);
2901
2902         return err ? : len;
2903 }
2904
2905 static int pfkey_recvmsg(struct kiocb *kiocb,
2906                          struct socket *sock, struct msghdr *msg, size_t len,
2907                          int flags)
2908 {
2909         struct sock *sk = sock->sk;
2910         struct sk_buff *skb;
2911         int copied, err;
2912
2913         err = -EINVAL;
2914         if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
2915                 goto out;
2916
2917         msg->msg_namelen = 0;
2918         skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2919         if (skb == NULL)
2920                 goto out;
2921
2922         copied = skb->len;
2923         if (copied > len) {
2924                 msg->msg_flags |= MSG_TRUNC;
2925                 copied = len;
2926         }
2927
2928         skb->h.raw = skb->data;
2929         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2930         if (err)
2931                 goto out_free;
2932
2933         sock_recv_timestamp(msg, sk, skb);
2934
2935         err = (flags & MSG_TRUNC) ? skb->len : copied;
2936
2937 out_free:
2938         skb_free_datagram(sk, skb);
2939 out:
2940         return err;
2941 }
2942
2943 static struct proto_ops pfkey_ops = {
2944         .family         =       PF_KEY,
2945         .owner          =       THIS_MODULE,
2946         /* Operations that make no sense on pfkey sockets. */
2947         .bind           =       sock_no_bind,
2948         .connect        =       sock_no_connect,
2949         .socketpair     =       sock_no_socketpair,
2950         .accept         =       sock_no_accept,
2951         .getname        =       sock_no_getname,
2952         .ioctl          =       sock_no_ioctl,
2953         .listen         =       sock_no_listen,
2954         .shutdown       =       sock_no_shutdown,
2955         .setsockopt     =       sock_no_setsockopt,
2956         .getsockopt     =       sock_no_getsockopt,
2957         .mmap           =       sock_no_mmap,
2958         .sendpage       =       sock_no_sendpage,
2959
2960         /* Now the operations that really occur. */
2961         .release        =       pfkey_release,
2962         .poll           =       datagram_poll,
2963         .sendmsg        =       pfkey_sendmsg,
2964         .recvmsg        =       pfkey_recvmsg,
2965 };
2966
2967 static struct net_proto_family pfkey_family_ops = {
2968         .family =       PF_KEY,
2969         .create =       pfkey_create,
2970         .owner  =       THIS_MODULE,
2971 };
2972
2973 #ifdef CONFIG_PROC_FS
2974 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
2975                            int length, int *eof, void *data)
2976 {
2977         off_t pos = 0;
2978         off_t begin = 0;
2979         int len = 0;
2980         struct sock *s;
2981         struct hlist_node *node;
2982
2983         len += sprintf(buffer,"sk       RefCnt Rmem   Wmem   User   Inode\n");
2984
2985         read_lock(&pfkey_table_lock);
2986
2987         sk_for_each(s, node, &pfkey_table) {
2988                 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
2989                                s,
2990                                atomic_read(&s->sk_refcnt),
2991                                atomic_read(&s->sk_rmem_alloc),
2992                                atomic_read(&s->sk_wmem_alloc),
2993                                sock_i_uid(s),
2994                                sock_i_ino(s)
2995                                );
2996
2997                 buffer[len++] = '\n';
2998                 
2999                 pos = begin + len;
3000                 if (pos < offset) {
3001                         len = 0;
3002                         begin = pos;
3003                 }
3004                 if(pos > offset + length)
3005                         goto done;
3006         }
3007         *eof = 1;
3008
3009 done:
3010         read_unlock(&pfkey_table_lock);
3011
3012         *start = buffer + (offset - begin);
3013         len -= (offset - begin);
3014
3015         if (len > length)
3016                 len = length;
3017         if (len < 0)
3018                 len = 0;
3019
3020         return len;
3021 }
3022 #endif
3023
3024 static struct xfrm_mgr pfkeyv2_mgr =
3025 {
3026         .id             = "pfkeyv2",
3027         .notify         = pfkey_send_notify,
3028         .acquire        = pfkey_send_acquire,
3029         .compile_policy = pfkey_compile_policy,
3030         .new_mapping    = pfkey_send_new_mapping,
3031         .notify_policy  = pfkey_send_policy_notify,
3032 };
3033
3034 static void __exit ipsec_pfkey_exit(void)
3035 {
3036         xfrm_unregister_km(&pfkeyv2_mgr);
3037         remove_proc_entry("net/pfkey", NULL);
3038         sock_unregister(PF_KEY);
3039         proto_unregister(&key_proto);
3040 }
3041
3042 static int __init ipsec_pfkey_init(void)
3043 {
3044         int err = proto_register(&key_proto, 0);
3045
3046         if (err != 0)
3047                 goto out;
3048
3049         err = sock_register(&pfkey_family_ops);
3050         if (err != 0)
3051                 goto out_unregister_key_proto;
3052 #ifdef CONFIG_PROC_FS
3053         err = -ENOMEM;
3054         if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3055                 goto out_sock_unregister;
3056 #endif
3057         err = xfrm_register_km(&pfkeyv2_mgr);
3058         if (err != 0)
3059                 goto out_remove_proc_entry;
3060 out:
3061         return err;
3062 out_remove_proc_entry:
3063 #ifdef CONFIG_PROC_FS
3064         remove_proc_entry("net/pfkey", NULL);
3065 out_sock_unregister:
3066 #endif
3067         sock_unregister(PF_KEY);
3068 out_unregister_key_proto:
3069         proto_unregister(&key_proto);
3070         goto out;
3071 }
3072
3073 module_init(ipsec_pfkey_init);
3074 module_exit(ipsec_pfkey_exit);
3075 MODULE_LICENSE("GPL");
3076 MODULE_ALIAS_NETPROTO(PF_KEY);