~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/key/af_key.c

Version: ~ [ linux-5.15-rc7 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 HLIST_HEAD(pfkey_table);
 39 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
 40 static rwlock_t pfkey_table_lock = RW_LOCK_UNLOCKED;
 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_opt {
 46         int     registered;
 47         int     promisc;
 48 };
 49 #define pfkey_sk(__sk) ((struct pfkey_opt *)(__sk)->sk_protinfo)
 50 
 51 static void pfkey_sock_destruct(struct sock *sk)
 52 {
 53         skb_queue_purge(&sk->sk_receive_queue);
 54 
 55         if (!sock_flag(sk, SOCK_DEAD)) {
 56                 printk("Attempt to release alive pfkey socket: %p\n", sk);
 57                 return;
 58         }
 59 
 60         BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
 61         BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
 62 
 63         kfree(pfkey_sk(sk));
 64 
 65         atomic_dec(&pfkey_socks_nr);
 66 }
 67 
 68 static void pfkey_table_grab(void)
 69 {
 70         write_lock_bh(&pfkey_table_lock);
 71 
 72         if (atomic_read(&pfkey_table_users)) {
 73                 DECLARE_WAITQUEUE(wait, current);
 74 
 75                 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
 76                 for(;;) {
 77                         set_current_state(TASK_UNINTERRUPTIBLE);
 78                         if (atomic_read(&pfkey_table_users) == 0)
 79                                 break;
 80                         write_unlock_bh(&pfkey_table_lock);
 81                         schedule();
 82                         write_lock_bh(&pfkey_table_lock);
 83                 }
 84 
 85                 __set_current_state(TASK_RUNNING);
 86                 remove_wait_queue(&pfkey_table_wait, &wait);
 87         }
 88 }
 89 
 90 static __inline__ void pfkey_table_ungrab(void)
 91 {
 92         write_unlock_bh(&pfkey_table_lock);
 93         wake_up(&pfkey_table_wait);
 94 }
 95 
 96 static __inline__ void pfkey_lock_table(void)
 97 {
 98         /* read_lock() synchronizes us to pfkey_table_grab */
 99 
100         read_lock(&pfkey_table_lock);
101         atomic_inc(&pfkey_table_users);
102         read_unlock(&pfkey_table_lock);
103 }
104 
105 static __inline__ void pfkey_unlock_table(void)
106 {
107         if (atomic_dec_and_test(&pfkey_table_users))
108                 wake_up(&pfkey_table_wait);
109 }
110 
111 
112 static struct proto_ops pfkey_ops;
113 
114 static void pfkey_insert(struct sock *sk)
115 {
116         pfkey_table_grab();
117         sk_add_node(sk, &pfkey_table);
118         pfkey_table_ungrab();
119 }
120 
121 static void pfkey_remove(struct sock *sk)
122 {
123         pfkey_table_grab();
124         sk_del_node_init(sk);
125         pfkey_table_ungrab();
126 }
127 
128 static int pfkey_create(struct socket *sock, int protocol)
129 {
130         struct sock *sk;
131         struct pfkey_opt *pfk;
132         int err;
133 
134         if (!capable(CAP_NET_ADMIN))
135                 return -EPERM;
136         if (sock->type != SOCK_RAW)
137                 return -ESOCKTNOSUPPORT;
138         if (protocol != PF_KEY_V2)
139                 return -EPROTONOSUPPORT;
140 
141         err = -ENOMEM;
142         sk = sk_alloc(PF_KEY, GFP_KERNEL, 1, NULL);
143         if (sk == NULL)
144                 goto out;
145         
146         sock->ops = &pfkey_ops;
147         sock_init_data(sock, sk);
148         sk_set_owner(sk, THIS_MODULE);
149 
150         err = -ENOMEM;
151         pfk = pfkey_sk(sk) = kmalloc(sizeof(*pfk), GFP_KERNEL);
152         if (!pfk) {
153                 sk_free(sk);
154                 goto out;
155         }
156         memset(pfk, 0, sizeof(*pfk));
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_opt *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                 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         sa->sadb_sa_state = SADB_SASTATE_DYING;
660         if (x->km.state == XFRM_STATE_VALID && !x->km.dying)
661                 sa->sadb_sa_state = SADB_SASTATE_MATURE;
662         else if (x->km.state == XFRM_STATE_ACQ)
663                 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
664         else if (x->km.state == XFRM_STATE_EXPIRED)
665                 sa->sadb_sa_state = SADB_SASTATE_DEAD;
666         sa->sadb_sa_auth = 0;
667         if (x->aalg) {
668                 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name);
669                 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
670         }
671         sa->sadb_sa_encrypt = 0;
672         BUG_ON(x->ealg && x->calg);
673         if (x->ealg) {
674                 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name);
675                 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
676         }
677         /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
678         if (x->calg) {
679                 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name);
680                 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
681         }
682 
683         sa->sadb_sa_flags = 0;
684         if (x->props.flags & XFRM_STATE_NOECN)
685                 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
686 
687         /* hard time */
688         if (hsc & 2) {
689                 lifetime = (struct sadb_lifetime *)  skb_put(skb, 
690                                                              sizeof(struct sadb_lifetime));
691                 lifetime->sadb_lifetime_len =
692                         sizeof(struct sadb_lifetime)/sizeof(uint64_t);
693                 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
694                 lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.hard_packet_limit);
695                 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
696                 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
697                 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
698         }
699         /* soft time */
700         if (hsc & 1) {
701                 lifetime = (struct sadb_lifetime *)  skb_put(skb, 
702                                                              sizeof(struct sadb_lifetime));
703                 lifetime->sadb_lifetime_len =
704                         sizeof(struct sadb_lifetime)/sizeof(uint64_t);
705                 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
706                 lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.soft_packet_limit);
707                 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
708                 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
709                 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
710         }
711         /* current time */
712         lifetime = (struct sadb_lifetime *)  skb_put(skb,
713                                                      sizeof(struct sadb_lifetime));
714         lifetime->sadb_lifetime_len =
715                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
716         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
717         lifetime->sadb_lifetime_allocations = x->curlft.packets;
718         lifetime->sadb_lifetime_bytes = x->curlft.bytes;
719         lifetime->sadb_lifetime_addtime = x->curlft.add_time;
720         lifetime->sadb_lifetime_usetime = x->curlft.use_time;
721         /* src address */
722         addr = (struct sadb_address*) skb_put(skb, 
723                                               sizeof(struct sadb_address)+sockaddr_size);
724         addr->sadb_address_len = 
725                 (sizeof(struct sadb_address)+sockaddr_size)/
726                         sizeof(uint64_t);
727         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
728         /* "if the ports are non-zero, then the sadb_address_proto field, 
729            normally zero, MUST be filled in with the transport 
730            protocol's number." - RFC2367 */
731         addr->sadb_address_proto = 0; 
732         addr->sadb_address_reserved = 0;
733         if (x->props.family == AF_INET) {
734                 addr->sadb_address_prefixlen = 32;
735 
736                 sin = (struct sockaddr_in *) (addr + 1);
737                 sin->sin_family = AF_INET;
738                 sin->sin_addr.s_addr = x->props.saddr.a4;
739                 sin->sin_port = 0;
740                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
741         }
742 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
743         else if (x->props.family == AF_INET6) {
744                 addr->sadb_address_prefixlen = 128;
745 
746                 sin6 = (struct sockaddr_in6 *) (addr + 1);
747                 sin6->sin6_family = AF_INET6;
748                 sin6->sin6_port = 0;
749                 sin6->sin6_flowinfo = 0;
750                 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
751                        sizeof(struct in6_addr));
752                 sin6->sin6_scope_id = 0;
753         }
754 #endif
755         else
756                 BUG();
757 
758         /* dst address */
759         addr = (struct sadb_address*) skb_put(skb, 
760                                               sizeof(struct sadb_address)+sockaddr_size);
761         addr->sadb_address_len = 
762                 (sizeof(struct sadb_address)+sockaddr_size)/
763                         sizeof(uint64_t);
764         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
765         addr->sadb_address_proto = 0; 
766         addr->sadb_address_prefixlen = 32; /* XXX */ 
767         addr->sadb_address_reserved = 0;
768         if (x->props.family == AF_INET) {
769                 sin = (struct sockaddr_in *) (addr + 1);
770                 sin->sin_family = AF_INET;
771                 sin->sin_addr.s_addr = x->id.daddr.a4;
772                 sin->sin_port = 0;
773                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
774 
775                 if (x->sel.saddr.a4 != x->props.saddr.a4) {
776                         addr = (struct sadb_address*) skb_put(skb, 
777                                 sizeof(struct sadb_address)+sockaddr_size);
778                         addr->sadb_address_len = 
779                                 (sizeof(struct sadb_address)+sockaddr_size)/
780                                 sizeof(uint64_t);
781                         addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
782                         addr->sadb_address_proto =
783                                 pfkey_proto_from_xfrm(x->sel.proto);
784                         addr->sadb_address_prefixlen = x->sel.prefixlen_s;
785                         addr->sadb_address_reserved = 0;
786 
787                         sin = (struct sockaddr_in *) (addr + 1);
788                         sin->sin_family = AF_INET;
789                         sin->sin_addr.s_addr = x->sel.saddr.a4;
790                         sin->sin_port = x->sel.sport;
791                         memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
792                 }
793         }
794 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
795         else if (x->props.family == AF_INET6) {
796                 addr->sadb_address_prefixlen = 128;
797 
798                 sin6 = (struct sockaddr_in6 *) (addr + 1);
799                 sin6->sin6_family = AF_INET6;
800                 sin6->sin6_port = 0;
801                 sin6->sin6_flowinfo = 0;
802                 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
803                 sin6->sin6_scope_id = 0;
804 
805                 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
806                             sizeof(struct in6_addr))) {
807                         addr = (struct sadb_address *) skb_put(skb, 
808                                 sizeof(struct sadb_address)+sockaddr_size);
809                         addr->sadb_address_len = 
810                                 (sizeof(struct sadb_address)+sockaddr_size)/
811                                 sizeof(uint64_t);
812                         addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
813                         addr->sadb_address_proto =
814                                 pfkey_proto_from_xfrm(x->sel.proto);
815                         addr->sadb_address_prefixlen = x->sel.prefixlen_s;
816                         addr->sadb_address_reserved = 0;
817 
818                         sin6 = (struct sockaddr_in6 *) (addr + 1);
819                         sin6->sin6_family = AF_INET6;
820                         sin6->sin6_port = x->sel.sport;
821                         sin6->sin6_flowinfo = 0;
822                         memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
823                                sizeof(struct in6_addr));
824                         sin6->sin6_scope_id = 0;
825                 }
826         }
827 #endif
828         else
829                 BUG();
830 
831         /* auth key */
832         if (add_keys && auth_key_size) {
833                 key = (struct sadb_key *) skb_put(skb, 
834                                                   sizeof(struct sadb_key)+auth_key_size);
835                 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
836                         sizeof(uint64_t);
837                 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
838                 key->sadb_key_bits = x->aalg->alg_key_len;
839                 key->sadb_key_reserved = 0;
840                 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
841         }
842         /* encrypt key */
843         if (add_keys && encrypt_key_size) {
844                 key = (struct sadb_key *) skb_put(skb, 
845                                                   sizeof(struct sadb_key)+encrypt_key_size);
846                 key->sadb_key_len = (sizeof(struct sadb_key) + 
847                                      encrypt_key_size) / sizeof(uint64_t);
848                 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
849                 key->sadb_key_bits = x->ealg->alg_key_len;
850                 key->sadb_key_reserved = 0;
851                 memcpy(key + 1, x->ealg->alg_key, 
852                        (x->ealg->alg_key_len+7)/8);
853         }
854 
855         /* sa */
856         sa2 = (struct sadb_x_sa2 *)  skb_put(skb, sizeof(struct sadb_x_sa2));
857         sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
858         sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
859         sa2->sadb_x_sa2_mode = x->props.mode + 1;
860         sa2->sadb_x_sa2_reserved1 = 0;
861         sa2->sadb_x_sa2_reserved2 = 0;
862         sa2->sadb_x_sa2_sequence = 0;
863         sa2->sadb_x_sa2_reqid = x->props.reqid;
864 
865         if (natt && natt->encap_type) {
866                 struct sadb_x_nat_t_type *n_type;
867                 struct sadb_x_nat_t_port *n_port;
868 
869                 /* type */
870                 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
871                 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
872                 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
873                 n_type->sadb_x_nat_t_type_type = natt->encap_type;
874                 n_type->sadb_x_nat_t_type_reserved[0] = 0;
875                 n_type->sadb_x_nat_t_type_reserved[1] = 0;
876                 n_type->sadb_x_nat_t_type_reserved[2] = 0;
877 
878                 /* source port */
879                 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
880                 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
881                 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
882                 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
883                 n_port->sadb_x_nat_t_port_reserved = 0;
884 
885                 /* dest port */
886                 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
887                 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
888                 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
889                 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
890                 n_port->sadb_x_nat_t_port_reserved = 0;
891         }
892 
893         return skb;
894 }
895 
896 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr, 
897                                                 void **ext_hdrs)
898 {
899         struct xfrm_state *x; 
900         struct sadb_lifetime *lifetime;
901         struct sadb_sa *sa;
902         struct sadb_key *key;
903         uint16_t proto;
904         
905 
906         sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
907         if (!sa ||
908             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
909                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
910                 return ERR_PTR(-EINVAL);
911         if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
912             !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
913                 return ERR_PTR(-EINVAL);
914         if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
915             !ext_hdrs[SADB_EXT_KEY_AUTH-1])
916                 return ERR_PTR(-EINVAL);
917         if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
918             !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
919                 return ERR_PTR(-EINVAL);
920 
921         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
922         if (proto == 0)
923                 return ERR_PTR(-EINVAL);
924 
925         /* RFC2367:
926 
927    Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
928    SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
929    sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
930    Therefore, the sadb_sa_state field of all submitted SAs MUST be
931    SADB_SASTATE_MATURE and the kernel MUST return an error if this is
932    not true.
933 
934            However, KAME setkey always uses SADB_SASTATE_LARVAL.
935            Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
936          */
937         if (sa->sadb_sa_auth > SADB_AALG_MAX ||
938             (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
939              sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
940             sa->sadb_sa_encrypt > SADB_EALG_MAX)
941                 return ERR_PTR(-EINVAL);
942         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
943         if (key != NULL &&
944             sa->sadb_sa_auth != SADB_X_AALG_NULL &&
945             ((key->sadb_key_bits+7) / 8 == 0 ||
946              (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
947                 return ERR_PTR(-EINVAL);
948         key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
949         if (key != NULL &&
950             sa->sadb_sa_encrypt != SADB_EALG_NULL &&
951             ((key->sadb_key_bits+7) / 8 == 0 ||
952              (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
953                 return ERR_PTR(-EINVAL);
954 
955         x = xfrm_state_alloc();
956         if (x == NULL)
957                 return ERR_PTR(-ENOBUFS);
958 
959         x->id.proto = proto;
960         x->id.spi = sa->sadb_sa_spi;
961         x->props.replay_window = sa->sadb_sa_replay;
962         if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
963                 x->props.flags |= XFRM_STATE_NOECN;
964 
965         lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
966         if (lifetime != NULL) {
967                 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
968                 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
969                 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
970                 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
971         }
972         lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
973         if (lifetime != NULL) {
974                 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
975                 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
976                 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
977                 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
978         }
979         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
980         if (sa->sadb_sa_auth) {
981                 int keysize = 0;
982                 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
983                 if (!a)
984                         goto out;
985                 if (key)
986                         keysize = (key->sadb_key_bits + 7) / 8;
987                 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
988                 if (!x->aalg)
989                         goto out;
990                 strcpy(x->aalg->alg_name, a->name);
991                 x->aalg->alg_key_len = 0;
992                 if (key) {
993                         x->aalg->alg_key_len = key->sadb_key_bits;
994                         memcpy(x->aalg->alg_key, key+1, keysize);
995                 }
996                 x->props.aalgo = sa->sadb_sa_auth;
997                 /* x->algo.flags = sa->sadb_sa_flags; */
998         }
999         if (sa->sadb_sa_encrypt) {
1000                 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1001                         struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1002                         if (!a)
1003                                 goto out;
1004                         x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1005                         if (!x->calg)
1006                                 goto out;
1007                         strcpy(x->calg->alg_name, a->name);
1008                         x->props.calgo = sa->sadb_sa_encrypt;
1009                 } else {
1010                         int keysize = 0;
1011                         struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1012                         if (!a)
1013                                 goto out;
1014                         key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1015                         if (key)
1016                                 keysize = (key->sadb_key_bits + 7) / 8;
1017                         x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1018                         if (!x->ealg)
1019                                 goto out;
1020                         strcpy(x->ealg->alg_name, a->name);
1021                         x->ealg->alg_key_len = 0;
1022                         if (key) {
1023                                 x->ealg->alg_key_len = key->sadb_key_bits;
1024                                 memcpy(x->ealg->alg_key, key+1, keysize);
1025                         }
1026                         x->props.ealgo = sa->sadb_sa_encrypt;
1027                 }
1028         }
1029         /* x->algo.flags = sa->sadb_sa_flags; */
1030 
1031         x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
1032                                                     &x->props.saddr);
1033         if (!x->props.family)
1034                 goto out;
1035         pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
1036                                   &x->id.daddr);
1037 
1038         if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1039                 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1040                 x->props.mode = sa2->sadb_x_sa2_mode;
1041                 if (x->props.mode)
1042                         x->props.mode--;
1043                 x->props.reqid = sa2->sadb_x_sa2_reqid;
1044         }
1045 
1046         if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1047                 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1048 
1049                 /* Nobody uses this, but we try. */
1050                 pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1051                 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1052         }
1053 
1054         if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1055                 struct sadb_x_nat_t_type* n_type;
1056                 struct xfrm_encap_tmpl *natt;
1057 
1058                 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1059                 if (!x->encap)
1060                         goto out;
1061 
1062                 natt = x->encap;
1063                 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1064                 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1065 
1066                 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1067                         struct sadb_x_nat_t_port* n_port =
1068                                 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1069                         natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1070                 }
1071                 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1072                         struct sadb_x_nat_t_port* n_port =
1073                                 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1074                         natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1075                 }
1076         }
1077 
1078         x->type = xfrm_get_type(proto, x->props.family);
1079         if (x->type == NULL)
1080                 goto out;
1081         if (x->type->init_state(x, NULL))
1082                 goto out;
1083         x->km.seq = hdr->sadb_msg_seq;
1084         x->km.state = XFRM_STATE_VALID;
1085         return x;
1086 
1087 out:
1088         x->km.state = XFRM_STATE_DEAD;
1089         xfrm_state_put(x);
1090         return ERR_PTR(-ENOBUFS);
1091 }
1092 
1093 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1094 {
1095         return -EOPNOTSUPP;
1096 }
1097 
1098 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1099 {
1100         struct sk_buff *resp_skb;
1101         struct sadb_x_sa2 *sa2;
1102         struct sadb_address *saddr, *daddr;
1103         struct sadb_msg *out_hdr;
1104         struct xfrm_state *x = NULL;
1105         u8 mode;
1106         u32 reqid;
1107         u8 proto;
1108         unsigned short family;
1109         xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1110 
1111         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1112                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1113                 return -EINVAL;
1114 
1115         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1116         if (proto == 0)
1117                 return -EINVAL;
1118 
1119         if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1120                 mode = sa2->sadb_x_sa2_mode - 1;
1121                 reqid = sa2->sadb_x_sa2_reqid;
1122         } else {
1123                 mode = 0;
1124                 reqid = 0;
1125         }
1126 
1127         saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1128         daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1129 
1130         family = ((struct sockaddr *)(saddr + 1))->sa_family;
1131         switch (family) {
1132         case AF_INET:
1133                 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1134                 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1135                 break;
1136 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1137         case AF_INET6:
1138                 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1139                 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1140                 break;
1141 #endif
1142         }
1143         if (xdaddr)
1144                 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1145 
1146         if (x == NULL)
1147                 return -ENOENT;
1148 
1149         resp_skb = ERR_PTR(-ENOENT);
1150 
1151         spin_lock_bh(&x->lock);
1152         if (x->km.state != XFRM_STATE_DEAD) {
1153                 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1154                 u32 min_spi, max_spi;
1155 
1156                 if (range != NULL) {
1157                         min_spi = range->sadb_spirange_min;
1158                         max_spi = range->sadb_spirange_max;
1159                 } else {
1160                         min_spi = htonl(0x100);
1161                         max_spi = htonl(0x0fffffff);
1162                 }
1163                 xfrm_alloc_spi(x, min_spi, max_spi);
1164                 if (x->id.spi)
1165                         resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1166         }
1167         spin_unlock_bh(&x->lock);
1168 
1169         if (IS_ERR(resp_skb)) {
1170                 xfrm_state_put(x);
1171                 return  PTR_ERR(resp_skb);
1172         }
1173 
1174         out_hdr = (struct sadb_msg *) resp_skb->data;
1175         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1176         out_hdr->sadb_msg_type = SADB_GETSPI;
1177         out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1178         out_hdr->sadb_msg_errno = 0;
1179         out_hdr->sadb_msg_reserved = 0;
1180         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1181         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1182 
1183         xfrm_state_put(x);
1184 
1185         pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1186 
1187         return 0;
1188 }
1189 
1190 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1191 {
1192         struct xfrm_state *x;
1193 
1194         if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1195                 return -EOPNOTSUPP;
1196 
1197         if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1198                 return 0;
1199 
1200         x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1201         if (x == NULL)
1202                 return 0;
1203 
1204         spin_lock_bh(&x->lock);
1205         if (x->km.state == XFRM_STATE_ACQ) {
1206                 x->km.state = XFRM_STATE_ERROR;
1207                 wake_up(&km_waitq);
1208         }
1209         spin_unlock_bh(&x->lock);
1210         xfrm_state_put(x);
1211         return 0;
1212 }
1213 
1214 
1215 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1216 {
1217         struct sk_buff *out_skb;
1218         struct sadb_msg *out_hdr;
1219         struct xfrm_state *x;
1220         int err;
1221 
1222         xfrm_probe_algs();
1223         
1224         x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1225         if (IS_ERR(x))
1226                 return PTR_ERR(x);
1227 
1228         if (hdr->sadb_msg_type == SADB_ADD)
1229                 err = xfrm_state_add(x);
1230         else
1231                 err = xfrm_state_update(x);
1232 
1233         if (err < 0) {
1234                 x->km.state = XFRM_STATE_DEAD;
1235                 xfrm_state_put(x);
1236                 return err;
1237         }
1238 
1239         out_skb = pfkey_xfrm_state2msg(x, 0, 3);
1240         if (IS_ERR(out_skb))
1241                 return  PTR_ERR(out_skb); /* XXX Should we return 0 here ? */
1242 
1243         out_hdr = (struct sadb_msg *) out_skb->data;
1244         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1245         out_hdr->sadb_msg_type = hdr->sadb_msg_type;
1246         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1247         out_hdr->sadb_msg_errno = 0;
1248         out_hdr->sadb_msg_reserved = 0;
1249         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1250         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1251 
1252         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
1253 
1254         return 0;
1255 }
1256 
1257 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1258 {
1259         struct xfrm_state *x;
1260 
1261         if (!ext_hdrs[SADB_EXT_SA-1] ||
1262             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1263                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1264                 return -EINVAL;
1265 
1266         x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1267         if (x == NULL)
1268                 return -ESRCH;
1269 
1270         if (xfrm_state_kern(x)) {
1271                 xfrm_state_put(x);
1272                 return -EPERM;
1273         }
1274         
1275         xfrm_state_delete(x);
1276         xfrm_state_put(x);
1277 
1278         pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, 
1279                         BROADCAST_ALL, sk);
1280 
1281         return 0;
1282 }
1283 
1284 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1285 {
1286         struct sk_buff *out_skb;
1287         struct sadb_msg *out_hdr;
1288         struct xfrm_state *x;
1289 
1290         if (!ext_hdrs[SADB_EXT_SA-1] ||
1291             !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1292                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1293                 return -EINVAL;
1294 
1295         x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1296         if (x == NULL)
1297                 return -ESRCH;
1298 
1299         out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1300         xfrm_state_put(x);
1301         if (IS_ERR(out_skb))
1302                 return  PTR_ERR(out_skb);
1303 
1304         out_hdr = (struct sadb_msg *) out_skb->data;
1305         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1306         out_hdr->sadb_msg_type = SADB_DUMP;
1307         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1308         out_hdr->sadb_msg_errno = 0;
1309         out_hdr->sadb_msg_reserved = 0;
1310         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1311         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1312         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1313 
1314         return 0;
1315 }
1316 
1317 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, int allocation)
1318 {
1319         struct sk_buff *skb;
1320         struct sadb_msg *hdr;
1321         int len, auth_len, enc_len, i;
1322 
1323         auth_len = xfrm_count_auth_supported();
1324         if (auth_len) {
1325                 auth_len *= sizeof(struct sadb_alg);
1326                 auth_len += sizeof(struct sadb_supported);
1327         }
1328         
1329         enc_len = xfrm_count_enc_supported();
1330         if (enc_len) {
1331                 enc_len *= sizeof(struct sadb_alg);
1332                 enc_len += sizeof(struct sadb_supported);
1333         }
1334         
1335         len = enc_len + auth_len + sizeof(struct sadb_msg);
1336 
1337         skb = alloc_skb(len + 16, allocation);
1338         if (!skb)
1339                 goto out_put_algs;
1340 
1341         hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1342         pfkey_hdr_dup(hdr, orig);
1343         hdr->sadb_msg_errno = 0;
1344         hdr->sadb_msg_len = len / sizeof(uint64_t);
1345 
1346         if (auth_len) {
1347                 struct sadb_supported *sp;
1348                 struct sadb_alg *ap;
1349 
1350                 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1351                 ap = (struct sadb_alg *) (sp + 1);
1352 
1353                 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1354                 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1355 
1356                 for (i = 0; ; i++) {
1357                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1358                         if (!aalg)
1359                                 break;
1360                         if (aalg->available)
1361                                 *ap++ = aalg->desc;
1362                 }
1363         }
1364 
1365         if (enc_len) {
1366                 struct sadb_supported *sp;
1367                 struct sadb_alg *ap;
1368 
1369                 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1370                 ap = (struct sadb_alg *) (sp + 1);
1371 
1372                 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1373                 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1374 
1375                 for (i = 0; ; i++) {
1376                         struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1377                         if (!ealg)
1378                                 break;
1379                         if (ealg->available)
1380                                 *ap++ = ealg->desc;
1381                 }
1382         }
1383 
1384 out_put_algs:
1385         return skb;
1386 }
1387 
1388 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1389 {
1390         struct pfkey_opt *pfk = pfkey_sk(sk);
1391         struct sk_buff *supp_skb;
1392 
1393         if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1394                 return -EINVAL;
1395 
1396         if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1397                 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1398                         return -EEXIST;
1399                 pfk->registered |= (1<<hdr->sadb_msg_satype);
1400         }
1401 
1402         xfrm_probe_algs();
1403         
1404         supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1405         if (!supp_skb) {
1406                 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1407                         pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1408 
1409                 return -ENOBUFS;
1410         }
1411 
1412         pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1413 
1414         return 0;
1415 }
1416 
1417 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1418 {
1419         unsigned proto;
1420         struct sk_buff *skb_out;
1421         struct sadb_msg *hdr_out;
1422 
1423         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1424         if (proto == 0)
1425                 return -EINVAL;
1426 
1427         skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
1428         if (!skb_out)
1429                 return -ENOBUFS;
1430 
1431         xfrm_state_flush(proto);
1432 
1433         hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
1434         pfkey_hdr_dup(hdr_out, hdr);
1435         hdr_out->sadb_msg_errno = (uint8_t) 0;
1436         hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1437 
1438         pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);
1439 
1440         return 0;
1441 }
1442 
1443 struct pfkey_dump_data
1444 {
1445         struct sk_buff *skb;
1446         struct sadb_msg *hdr;
1447         struct sock *sk;
1448 };
1449 
1450 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1451 {
1452         struct pfkey_dump_data *data = ptr;
1453         struct sk_buff *out_skb;
1454         struct sadb_msg *out_hdr;
1455 
1456         out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1457         if (IS_ERR(out_skb))
1458                 return PTR_ERR(out_skb);
1459 
1460         out_hdr = (struct sadb_msg *) out_skb->data;
1461         out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1462         out_hdr->sadb_msg_type = SADB_DUMP;
1463         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1464         out_hdr->sadb_msg_errno = 0;
1465         out_hdr->sadb_msg_reserved = 0;
1466         out_hdr->sadb_msg_seq = count;
1467         out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1468         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1469         return 0;
1470 }
1471 
1472 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1473 {
1474         u8 proto;
1475         struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1476 
1477         proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1478         if (proto == 0)
1479                 return -EINVAL;
1480 
1481         return xfrm_state_walk(proto, dump_sa, &data);
1482 }
1483 
1484 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1485 {
1486         struct pfkey_opt *pfk = pfkey_sk(sk);
1487         int satype = hdr->sadb_msg_satype;
1488 
1489         if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1490                 /* XXX we mangle packet... */
1491                 hdr->sadb_msg_errno = 0;
1492                 if (satype != 0 && satype != 1)
1493                         return -EINVAL;
1494                 pfk->promisc = satype;
1495         }
1496         pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1497         return 0;
1498 }
1499 
1500 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1501 {
1502         int i;
1503         u32 reqid = *(u32*)ptr;
1504 
1505         for (i=0; i<xp->xfrm_nr; i++) {
1506                 if (xp->xfrm_vec[i].reqid == reqid)
1507                         return -EEXIST;
1508         }
1509         return 0;
1510 }
1511 
1512 static u32 gen_reqid(void)
1513 {
1514         u32 start;
1515         static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1516 
1517         start = reqid;
1518         do {
1519                 ++reqid;
1520                 if (reqid == 0)
1521                         reqid = IPSEC_MANUAL_REQID_MAX+1;
1522                 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1523                         return reqid;
1524         } while (reqid != start);
1525         return 0;
1526 }
1527 
1528 static int
1529 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1530 {
1531         struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1532         struct sockaddr_in *sin;
1533 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1534         struct sockaddr_in6 *sin6;
1535 #endif
1536 
1537         if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1538                 return -ELOOP;
1539 
1540         if (rq->sadb_x_ipsecrequest_mode == 0)
1541                 return -EINVAL;
1542 
1543         t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1544         t->mode = rq->sadb_x_ipsecrequest_mode-1;
1545         if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1546                 t->optional = 1;
1547         else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1548                 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1549                 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1550                         t->reqid = 0;
1551                 if (!t->reqid && !(t->reqid = gen_reqid()))
1552                         return -ENOBUFS;
1553         }
1554 
1555         /* addresses present only in tunnel mode */
1556         if (t->mode) {
1557                 switch (xp->family) {
1558                 case AF_INET:
1559                         sin = (void*)(rq+1);
1560                         if (sin->sin_family != AF_INET)
1561                                 return -EINVAL;
1562                         t->saddr.a4 = sin->sin_addr.s_addr;
1563                         sin++;
1564                         if (sin->sin_family != AF_INET)
1565                                 return -EINVAL;
1566                         t->id.daddr.a4 = sin->sin_addr.s_addr;
1567                         break;
1568 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1569                 case AF_INET6:
1570                         sin6 = (void *)(rq+1);
1571                         if (sin6->sin6_family != AF_INET6)
1572                                 return -EINVAL;
1573                         memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1574                         sin6++;
1575                         if (sin6->sin6_family != AF_INET6)
1576                                 return -EINVAL;
1577                         memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1578                         break;
1579 #endif
1580                 default:
1581                         return -EINVAL;
1582                 }
1583         }
1584         /* No way to set this via kame pfkey */
1585         t->aalgos = t->ealgos = t->calgos = ~0;
1586         xp->xfrm_nr++;
1587         return 0;
1588 }
1589 
1590 static int
1591 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1592 {
1593         int err;
1594         int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1595         struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1596 
1597         while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1598                 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1599                         return err;
1600                 len -= rq->sadb_x_ipsecrequest_len;
1601                 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1602         }
1603         return 0;
1604 }
1605 
1606 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1607 {
1608         int sockaddr_size = pfkey_sockaddr_size(xp->family);
1609         int socklen = (xp->family == AF_INET ?
1610                        sizeof(struct sockaddr_in) :
1611                        sizeof(struct sockaddr_in6));
1612 
1613         return sizeof(struct sadb_msg) +
1614                 (sizeof(struct sadb_lifetime) * 3) +
1615                 (sizeof(struct sadb_address) * 2) + 
1616                 (sockaddr_size * 2) +
1617                 sizeof(struct sadb_x_policy) +
1618                 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1619                                 (socklen * 2)));
1620 }
1621 
1622 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1623 {
1624         struct sk_buff *skb;
1625         int size;
1626 
1627         size = pfkey_xfrm_policy2msg_size(xp);
1628 
1629         skb =  alloc_skb(size + 16, GFP_ATOMIC);
1630         if (skb == NULL)
1631                 return ERR_PTR(-ENOBUFS);
1632 
1633         return skb;
1634 }
1635 
1636 static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1637 {
1638         struct sadb_msg *hdr;
1639         struct sadb_address *addr;
1640         struct sadb_lifetime *lifetime;
1641         struct sadb_x_policy *pol;
1642         struct sockaddr_in   *sin;
1643 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1644         struct sockaddr_in6  *sin6;
1645 #endif
1646         int i;
1647         int size;
1648         int sockaddr_size = pfkey_sockaddr_size(xp->family);
1649         int socklen = (xp->family == AF_INET ?
1650                        sizeof(struct sockaddr_in) :
1651                        sizeof(struct sockaddr_in6));
1652 
1653         size = pfkey_xfrm_policy2msg_size(xp);
1654 
1655         /* call should fill header later */
1656         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1657         memset(hdr, 0, size);   /* XXX do we need this ? */
1658 
1659         /* src address */
1660         addr = (struct sadb_address*) skb_put(skb, 
1661                                               sizeof(struct sadb_address)+sockaddr_size);
1662         addr->sadb_address_len = 
1663                 (sizeof(struct sadb_address)+sockaddr_size)/
1664                         sizeof(uint64_t);
1665         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1666         addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1667         addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1668         addr->sadb_address_reserved = 0;
1669         /* src address */
1670         if (xp->family == AF_INET) {
1671                 sin = (struct sockaddr_in *) (addr + 1);
1672                 sin->sin_family = AF_INET;
1673                 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1674                 sin->sin_port = xp->selector.sport;
1675                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1676         }
1677 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1678         else if (xp->family == AF_INET6) {
1679                 sin6 = (struct sockaddr_in6 *) (addr + 1);
1680                 sin6->sin6_family = AF_INET6;
1681                 sin6->sin6_port = xp->selector.sport;
1682                 sin6->sin6_flowinfo = 0;
1683                 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1684                        sizeof(struct in6_addr));;
1685                 sin6->sin6_scope_id = 0;
1686         }
1687 #endif
1688         else
1689                 BUG();
1690 
1691         /* dst address */
1692         addr = (struct sadb_address*) skb_put(skb, 
1693                                               sizeof(struct sadb_address)+sockaddr_size);
1694         addr->sadb_address_len =
1695                 (sizeof(struct sadb_address)+sockaddr_size)/
1696                         sizeof(uint64_t);
1697         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1698         addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1699         addr->sadb_address_prefixlen = xp->selector.prefixlen_d; 
1700         addr->sadb_address_reserved = 0;
1701         if (xp->family == AF_INET) {
1702                 sin = (struct sockaddr_in *) (addr + 1);
1703                 sin->sin_family = AF_INET;
1704                 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1705                 sin->sin_port = xp->selector.dport;
1706                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1707         }
1708 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1709         else if (xp->family == AF_INET6) {
1710                 sin6 = (struct sockaddr_in6 *) (addr + 1);
1711                 sin6->sin6_family = AF_INET6;
1712                 sin6->sin6_port = xp->selector.dport;
1713                 sin6->sin6_flowinfo = 0;
1714                 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1715                        sizeof(struct in6_addr));
1716                 sin6->sin6_scope_id = 0;
1717         }
1718 #endif
1719         else
1720                 BUG();
1721 
1722         /* hard time */
1723         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1724                                                      sizeof(struct sadb_lifetime));
1725         lifetime->sadb_lifetime_len =
1726                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1727         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1728         lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.hard_packet_limit);
1729         lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1730         lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1731         lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1732         /* soft time */
1733         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1734                                                      sizeof(struct sadb_lifetime));
1735         lifetime->sadb_lifetime_len =
1736                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1737         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1738         lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.soft_packet_limit);
1739         lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1740         lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1741         lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1742         /* current time */
1743         lifetime = (struct sadb_lifetime *)  skb_put(skb, 
1744                                                      sizeof(struct sadb_lifetime));
1745         lifetime->sadb_lifetime_len =
1746                 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1747         lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1748         lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1749         lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1750         lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1751         lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1752 
1753         pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
1754         pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1755         pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1756         pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1757         if (xp->action == XFRM_POLICY_ALLOW) {
1758                 if (xp->xfrm_nr)
1759                         pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1760                 else
1761                         pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1762         }
1763         pol->sadb_x_policy_dir = dir+1;
1764         pol->sadb_x_policy_id = xp->index;
1765 
1766         for (i=0; i<xp->xfrm_nr; i++) {
1767                 struct sadb_x_ipsecrequest *rq;
1768                 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1769                 int req_size;
1770 
1771                 req_size = sizeof(struct sadb_x_ipsecrequest);
1772                 if (t->mode)
1773                         req_size += 2*socklen;
1774                 else
1775                         size -= 2*socklen;
1776                 rq = (void*)skb_put(skb, req_size);
1777                 pol->sadb_x_policy_len += req_size/8;
1778                 memset(rq, 0, sizeof(*rq));
1779                 rq->sadb_x_ipsecrequest_len = req_size;
1780                 rq->sadb_x_ipsecrequest_proto = t->id.proto;
1781                 rq->sadb_x_ipsecrequest_mode = t->mode+1;
1782                 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1783                 if (t->reqid)
1784                         rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1785                 if (t->optional)
1786                         rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
1787                 rq->sadb_x_ipsecrequest_reqid = t->reqid;
1788                 if (t->mode) {
1789                         switch (xp->family) {
1790                         case AF_INET:
1791                                 sin = (void*)(rq+1);
1792                                 sin->sin_family = AF_INET;
1793                                 sin->sin_addr.s_addr = t->saddr.a4;
1794                                 sin->sin_port = 0;
1795                                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1796                                 sin++;
1797                                 sin->sin_family = AF_INET;
1798                                 sin->sin_addr.s_addr = t->id.daddr.a4;
1799                                 sin->sin_port = 0;
1800                                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1801                                 break;
1802 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1803                         case AF_INET6:
1804                                 sin6 = (void*)(rq+1);
1805                                 sin6->sin6_family = AF_INET6;
1806                                 sin6->sin6_port = 0;
1807                                 sin6->sin6_flowinfo = 0;
1808                                 memcpy(&sin6->sin6_addr, t->saddr.a6,
1809                                        sizeof(struct in6_addr));
1810                                 sin6->sin6_scope_id = 0;
1811 
1812                                 sin6++;
1813                                 sin6->sin6_family = AF_INET6;
1814                                 sin6->sin6_port = 0;
1815                                 sin6->sin6_flowinfo = 0;
1816                                 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
1817                                        sizeof(struct in6_addr));
1818                                 sin6->sin6_scope_id = 0;
1819                                 break;
1820 #endif
1821                         default:
1822                                 break;
1823                         }
1824                 }
1825         }
1826         hdr->sadb_msg_len = size / sizeof(uint64_t);
1827         hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
1828 }
1829 
1830 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1831 {
1832         int err;
1833         struct sadb_lifetime *lifetime;
1834         struct sadb_address *sa;
1835         struct sadb_x_policy *pol;
1836         struct xfrm_policy *xp;
1837         struct sk_buff *out_skb;
1838         struct sadb_msg *out_hdr;
1839 
1840         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1841                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1842             !ext_hdrs[SADB_X_EXT_POLICY-1])
1843                 return -EINVAL;
1844 
1845         pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1846         if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
1847                 return -EINVAL;
1848         if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1849                 return -EINVAL;
1850 
1851         xp = xfrm_policy_alloc(GFP_KERNEL);
1852         if (xp == NULL)
1853                 return -ENOBUFS;
1854 
1855         xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
1856                       XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
1857 
1858         sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
1859         xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
1860         if (!xp->family) {
1861                 err = -EINVAL;
1862                 goto out;
1863         }
1864         xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
1865         xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1866         xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
1867         if (xp->selector.sport)
1868                 xp->selector.sport_mask = ~0;
1869 
1870         sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
1871         pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
1872         xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
1873 
1874         /* Amusing, we set this twice.  KAME apps appear to set same value
1875          * in both addresses.
1876          */
1877         xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1878 
1879         xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
1880         if (xp->selector.dport)
1881                 xp->selector.dport_mask = ~0;
1882 
1883         xp->lft.soft_byte_limit = XFRM_INF;
1884         xp->lft.hard_byte_limit = XFRM_INF;
1885         xp->lft.soft_packet_limit = XFRM_INF;
1886         xp->lft.hard_packet_limit = XFRM_INF;
1887         if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
1888                 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1889                 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1890                 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1891                 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1892         }
1893         if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
1894                 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1895                 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1896                 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1897                 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1898         }
1899         xp->xfrm_nr = 0;
1900         if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
1901             (err = parse_ipsecrequests(xp, pol)) < 0)
1902                 goto out;
1903 
1904         out_skb = pfkey_xfrm_policy2msg_prep(xp);
1905         if (IS_ERR(out_skb)) {
1906                 err =  PTR_ERR(out_skb);
1907                 goto out;
1908         }
1909 
1910         err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
1911                                  hdr->sadb_msg_type != SADB_X_SPDUPDATE);
1912         if (err) {
1913                 kfree_skb(out_skb);
1914                 goto out;
1915         }
1916 
1917         pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
1918 
1919         xfrm_pol_put(xp);
1920 
1921         out_hdr = (struct sadb_msg *) out_skb->data;
1922         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1923         out_hdr->sadb_msg_type = hdr->sadb_msg_type;
1924         out_hdr->sadb_msg_satype = 0;
1925         out_hdr->sadb_msg_errno = 0;
1926         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1927         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1928         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
1929         return 0;
1930 
1931 out:
1932         kfree(xp);
1933         return err;
1934 }
1935 
1936 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1937 {
1938         int err;
1939         struct sadb_address *sa;
1940         struct sadb_x_policy *pol;
1941         struct xfrm_policy *xp;
1942         struct sk_buff *out_skb;
1943         struct sadb_msg *out_hdr;
1944         struct xfrm_selector sel;
1945 
1946         if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1947                                      ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1948             !ext_hdrs[SADB_X_EXT_POLICY-1])
1949                 return -EINVAL;
1950 
1951         pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1952         if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1953                 return -EINVAL;
1954 
1955         memset(&sel, 0, sizeof(sel));
1956 
1957         sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
1958         pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
1959         sel.prefixlen_s = sa->sadb_address_prefixlen;
1960         sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1961         sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
1962         if (sel.sport)
1963                 sel.sport_mask = ~0;
1964 
1965         sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
1966         pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
1967         sel.prefixlen_d = sa->sadb_address_prefixlen;
1968         sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1969         sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
1970         if (sel.dport)
1971                 sel.dport_mask = ~0;
1972 
1973         xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
1974         if (xp == NULL)
1975                 return -ENOENT;
1976 
1977         err = 0;
1978 
1979         out_skb = pfkey_xfrm_policy2msg_prep(xp);
1980         if (IS_ERR(out_skb)) {
1981                 err =  PTR_ERR(out_skb);
1982                 goto out;
1983         }
1984         pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
1985 
1986         out_hdr = (struct sadb_msg *) out_skb->data;
1987         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1988         out_hdr->sadb_msg_type = SADB_X_SPDDELETE;
1989         out_hdr->sadb_msg_satype = 0;
1990         out_hdr->sadb_msg_errno = 0;
1991         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1992         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1993         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
1994         err = 0;
1995 
1996 out:
1997         xfrm_pol_put(xp);
1998         return err;
1999 }
2000 
2001 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2002 {
2003         int err;
2004         struct sadb_x_policy *pol;
2005         struct xfrm_policy *xp;
2006         struct sk_buff *out_skb;
2007         struct sadb_msg *out_hdr;
2008 
2009         if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2010                 return -EINVAL;
2011 
2012         xp = xfrm_policy_byid(0, pol->sadb_x_policy_id,
2013                               hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2014         if (xp == NULL)
2015                 return -ENOENT;
2016 
2017         err = 0;
2018 
2019         out_skb = pfkey_xfrm_policy2msg_prep(xp);
2020         if (IS_ERR(out_skb)) {
2021                 err =  PTR_ERR(out_skb);
2022                 goto out;
2023         }
2024         pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
2025 
2026         out_hdr = (struct sadb_msg *) out_skb->data;
2027         out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2028         out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2029         out_hdr->sadb_msg_satype = 0;
2030         out_hdr->sadb_msg_errno = 0;
2031         out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2032         out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2033         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
2034         err = 0;
2035 
2036 out:
2037         xfrm_pol_put(xp);
2038         return err;
2039 }
2040 
2041 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2042 {
2043         struct pfkey_dump_data *data = ptr;
2044         struct sk_buff *out_skb;
2045         struct sadb_msg *out_hdr;
2046 
2047         out_skb = pfkey_xfrm_policy2msg_prep(xp);
2048         if (IS_ERR(out_skb))
2049                 return PTR_ERR(out_skb);
2050 
2051         pfkey_xfrm_policy2msg(out_skb, xp, dir);
2052 
2053         out_hdr = (struct sadb_msg *) out_skb->data;
2054         out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2055         out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2056         out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2057         out_hdr->sadb_msg_errno = 0;
2058         out_hdr->sadb_msg_seq = count;
2059         out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2060         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2061         return 0;
2062 }
2063 
2064 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2065 {
2066         struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2067 
2068         return xfrm_policy_walk(dump_sp, &data);
2069 }
2070 
2071 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2072 {
2073         struct sk_buff *skb_out;
2074         struct sadb_msg *hdr_out;
2075 
2076         skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
2077         if (!skb_out)
2078                 return -ENOBUFS;
2079 
2080         xfrm_policy_flush();
2081 
2082         hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2083         pfkey_hdr_dup(hdr_out, hdr);
2084         hdr_out->sadb_msg_errno = (uint8_t) 0;
2085         hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2086         pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);
2087 
2088         return 0;
2089 }
2090 
2091 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2092                              struct sadb_msg *hdr, void **ext_hdrs);
2093 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2094         [SADB_RESERVED]         = pfkey_reserved,
2095         [SADB_GETSPI]           = pfkey_getspi,
2096         [SADB_UPDATE]           = pfkey_add,
2097         [SADB_ADD]              = pfkey_add,
2098         [SADB_DELETE]           = pfkey_delete,
2099         [SADB_GET]              = pfkey_get,
2100         [SADB_ACQUIRE]          = pfkey_acquire,
2101         [SADB_REGISTER]         = pfkey_register,
2102         [SADB_EXPIRE]           = NULL,
2103         [SADB_FLUSH]            = pfkey_flush,
2104         [SADB_DUMP]             = pfkey_dump,
2105         [SADB_X_PROMISC]        = pfkey_promisc,
2106         [SADB_X_PCHANGE]        = NULL,
2107         [SADB_X_SPDUPDATE]      = pfkey_spdadd,
2108         [SADB_X_SPDADD]         = pfkey_spdadd,
2109         [SADB_X_SPDDELETE]      = pfkey_spddelete,
2110         [SADB_X_SPDGET]         = pfkey_spdget,
2111         [SADB_X_SPDACQUIRE]     = NULL,
2112         [SADB_X_SPDDUMP]        = pfkey_spddump,
2113         [SADB_X_SPDFLUSH]       = pfkey_spdflush,
2114         [SADB_X_SPDSETIDX]      = pfkey_spdadd,
2115         [SADB_X_SPDDELETE2]     = pfkey_spdget,
2116 };
2117 
2118 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2119 {
2120         void *ext_hdrs[SADB_EXT_MAX];
2121         int err;
2122 
2123         pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2124                         BROADCAST_PROMISC_ONLY, NULL);
2125 
2126         memset(ext_hdrs, 0, sizeof(ext_hdrs));
2127         err = parse_exthdrs(skb, hdr, ext_hdrs);
2128         if (!err) {
2129                 err = -EOPNOTSUPP;
2130                 if (pfkey_funcs[hdr->sadb_msg_type])
2131                         err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2132         }
2133         return err;
2134 }
2135 
2136 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2137 {
2138         struct sadb_msg *hdr = NULL;
2139 
2140         if (skb->len < sizeof(*hdr)) {
2141                 *errp = -EMSGSIZE;
2142         } else {
2143                 hdr = (struct sadb_msg *) skb->data;
2144                 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2145                     hdr->sadb_msg_reserved != 0 ||
2146                     (hdr->sadb_msg_type <= SADB_RESERVED ||
2147                      hdr->sadb_msg_type > SADB_MAX)) {
2148                         hdr = NULL;
2149                         *errp = -EINVAL;
2150                 } else if (hdr->sadb_msg_len != (skb->len /
2151                                                  sizeof(uint64_t)) ||
2152                            hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2153                                                 sizeof(uint64_t))) {
2154                         hdr = NULL;
2155                         *errp = -EMSGSIZE;
2156                 } else {
2157                         *errp = 0;
2158                 }
2159         }
2160         return hdr;
2161 }
2162 
2163 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2164 {
2165         return t->aalgos & (1 << d->desc.sadb_alg_id);
2166 }
2167 
2168 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2169 {
2170         return t->ealgos & (1 << d->desc.sadb_alg_id);
2171 }
2172 
2173 static int count_ah_combs(struct xfrm_tmpl *t)
2174 {
2175         int i, sz = 0;
2176 
2177         for (i = 0; ; i++) {
2178                 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2179                 if (!aalg)
2180                         break;
2181                 if (aalg_tmpl_set(t, aalg) && aalg->available)
2182                         sz += sizeof(struct sadb_comb);
2183         }
2184         return sz + sizeof(struct sadb_prop);
2185 }
2186 
2187 static int count_esp_combs(struct xfrm_tmpl *t)
2188 {
2189         int i, k, sz = 0;
2190 
2191         for (i = 0; ; i++) {
2192                 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2193                 if (!ealg)
2194                         break;
2195                         
2196                 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2197                         continue;
2198                         
2199                 for (k = 1; ; k++) {
2200                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2201                         if (!aalg)
2202                                 break;
2203                                 
2204                         if (aalg_tmpl_set(t, aalg) && aalg->available)
2205                                 sz += sizeof(struct sadb_comb);
2206                 }
2207         }
2208         return sz + sizeof(struct sadb_prop);
2209 }
2210 
2211 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2212 {
2213         struct sadb_prop *p;
2214         int i;
2215 
2216         p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2217         p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2218         p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2219         p->sadb_prop_replay = 32;
2220         memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2221 
2222         for (i = 0; ; i++) {
2223                 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2224                 if (!aalg)
2225                         break;
2226 
2227                 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2228                         struct sadb_comb *c;
2229                         c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2230                         memset(c, 0, sizeof(*c));
2231                         p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2232                         c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2233                         c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2234                         c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2235                         c->sadb_comb_hard_addtime = 24*60*60;
2236                         c->sadb_comb_soft_addtime = 20*60*60;
2237                         c->sadb_comb_hard_usetime = 8*60*60;
2238                         c->sadb_comb_soft_usetime = 7*60*60;
2239                 }
2240         }
2241 }
2242 
2243 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2244 {
2245         struct sadb_prop *p;
2246         int i, k;
2247 
2248         p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2249         p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2250         p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2251         p->sadb_prop_replay = 32;
2252         memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2253 
2254         for (i=0; ; i++) {
2255                 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2256                 if (!ealg)
2257                         break;
2258         
2259                 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2260                         continue;
2261                         
2262                 for (k = 1; ; k++) {
2263                         struct sadb_comb *c;
2264                         struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2265                         if (!aalg)
2266                                 break;
2267                         if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2268                                 continue;
2269                         c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2270                         memset(c, 0, sizeof(*c));
2271                         p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2272                         c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2273                         c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2274                         c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2275                         c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2276                         c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2277                         c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2278                         c->sadb_comb_hard_addtime = 24*60*60;
2279                         c->sadb_comb_soft_addtime = 20*60*60;
2280                         c->sadb_comb_hard_usetime = 8*60*60;
2281                         c->sadb_comb_soft_usetime = 7*60*60;
2282                 }
2283         }
2284 }
2285 
2286 static int pfkey_send_notify(struct xfrm_state *x, int hard)
2287 {
2288         struct sk_buff *out_skb;
2289         struct sadb_msg *out_hdr;
2290         int hsc = (hard ? 2 : 1);
2291 
2292         out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2293         if (IS_ERR(out_skb))
2294                 return PTR_ERR(out_skb);
2295 
2296         out_hdr = (struct sadb_msg *) out_skb->data;
2297         out_hdr->sadb_msg_version = PF_KEY_V2;
2298         out_hdr->sadb_msg_type = SADB_EXPIRE;
2299         out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2300         out_hdr->sadb_msg_errno = 0;
2301         out_hdr->sadb_msg_reserved = 0;
2302         out_hdr->sadb_msg_seq = 0;
2303         out_hdr->sadb_msg_pid = 0;
2304 
2305         pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2306         return 0;
2307 }
2308 
2309 static u32 get_acqseq(void)
2310 {
2311         u32 res;
2312         static u32 acqseq;
2313         static spinlock_t acqseq_lock = SPIN_LOCK_UNLOCKED;
2314 
2315         spin_lock_bh(&acqseq_lock);
2316         res = (++acqseq ? : ++acqseq);
2317         spin_unlock_bh(&acqseq_lock);
2318         return res;
2319 }
2320 
2321 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2322 {
2323         struct sk_buff *skb;
2324         struct sadb_msg *hdr;
2325         struct sadb_address *addr;
2326         struct sadb_x_policy *pol;
2327         struct sockaddr_in *sin;
2328 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2329         struct sockaddr_in6 *sin6;
2330 #endif
2331         int sockaddr_size;
2332         int size;
2333         
2334         sockaddr_size = pfkey_sockaddr_size(x->props.family);
2335         if (!sockaddr_size)
2336                 return -EINVAL;
2337 
2338         size = sizeof(struct sadb_msg) +
2339                 (sizeof(struct sadb_address) * 2) +
2340                 (sockaddr_size * 2) +
2341                 sizeof(struct sadb_x_policy);
2342         
2343         if (x->id.proto == IPPROTO_AH)
2344                 size += count_ah_combs(t);
2345         else if (x->id.proto == IPPROTO_ESP)
2346                 size += count_esp_combs(t);
2347 
2348         skb =  alloc_skb(size + 16, GFP_ATOMIC);
2349         if (skb == NULL)
2350                 return -ENOMEM;
2351         
2352         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2353         hdr->sadb_msg_version = PF_KEY_V2;
2354         hdr->sadb_msg_type = SADB_ACQUIRE;
2355         hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2356         hdr->sadb_msg_len = size / sizeof(uint64_t);
2357         hdr->sadb_msg_errno = 0;
2358         hdr->sadb_msg_reserved = 0;
2359         hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2360         hdr->sadb_msg_pid = 0;
2361 
2362         /* src address */
2363         addr = (struct sadb_address*) skb_put(skb, 
2364                                               sizeof(struct sadb_address)+sockaddr_size);
2365         addr->sadb_address_len = 
2366                 (sizeof(struct sadb_address)+sockaddr_size)/
2367                         sizeof(uint64_t);
2368         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2369         addr->sadb_address_proto = 0;
2370         addr->sadb_address_reserved = 0;
2371         if (x->props.family == AF_INET) {
2372                 addr->sadb_address_prefixlen = 32;
2373 
2374                 sin = (struct sockaddr_in *) (addr + 1);
2375                 sin->sin_family = AF_INET;
2376                 sin->sin_addr.s_addr = x->props.saddr.a4;
2377                 sin->sin_port = 0;
2378                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2379         }
2380 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2381         else if (x->props.family == AF_INET6) {
2382                 addr->sadb_address_prefixlen = 128;
2383 
2384                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2385                 sin6->sin6_family = AF_INET6;
2386                 sin6->sin6_port = 0;
2387                 sin6->sin6_flowinfo = 0;
2388                 memcpy(&sin6->sin6_addr,
2389                        x->props.saddr.a6, sizeof(struct in6_addr));
2390                 sin6->sin6_scope_id = 0;
2391         }
2392 #endif
2393         else
2394                 BUG();
2395         
2396         /* dst address */
2397         addr = (struct sadb_address*) skb_put(skb, 
2398                                               sizeof(struct sadb_address)+sockaddr_size);
2399         addr->sadb_address_len =
2400                 (sizeof(struct sadb_address)+sockaddr_size)/
2401                         sizeof(uint64_t);
2402         addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2403         addr->sadb_address_proto = 0;
2404         addr->sadb_address_reserved = 0;
2405         if (x->props.family == AF_INET) {
2406                 addr->sadb_address_prefixlen = 32; 
2407 
2408                 sin = (struct sockaddr_in *) (addr + 1);
2409                 sin->sin_family = AF_INET;
2410                 sin->sin_addr.s_addr = x->id.daddr.a4;
2411                 sin->sin_port = 0;
2412                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2413         }
2414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2415         else if (x->props.family == AF_INET6) {
2416                 addr->sadb_address_prefixlen = 128; 
2417 
2418                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2419                 sin6->sin6_family = AF_INET6;
2420                 sin6->sin6_port = 0;
2421                 sin6->sin6_flowinfo = 0;
2422                 memcpy(&sin6->sin6_addr,
2423                        x->id.daddr.a6, sizeof(struct in6_addr));
2424                 sin6->sin6_scope_id = 0;
2425         }
2426 #endif
2427         else
2428                 BUG();
2429 
2430         pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
2431         pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2432         pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2433         pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2434         pol->sadb_x_policy_dir = dir+1;
2435         pol->sadb_x_policy_id = xp->index;
2436 
2437         /* Set sadb_comb's. */
2438         if (x->id.proto == IPPROTO_AH)
2439                 dump_ah_combs(skb, t);
2440         else if (x->id.proto == IPPROTO_ESP)
2441                 dump_esp_combs(skb, t);
2442 
2443         return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2444 }
2445 
2446 static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2447                                                 u8 *data, int len, int *dir)
2448 {
2449         struct xfrm_policy *xp;
2450         struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2451 
2452         switch (family) {
2453         case AF_INET:
2454                 if (opt != IP_IPSEC_POLICY) {
2455                         *dir = -EOPNOTSUPP;
2456                         return NULL;
2457                 }
2458                 break;
2459 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2460         case AF_INET6:
2461                 if (opt != IPV6_IPSEC_POLICY) {
2462                         *dir = -EOPNOTSUPP;
2463                         return NULL;
2464                 }
2465                 break;
2466 #endif
2467         default:
2468                 *dir = -EINVAL;
2469                 return NULL;
2470         }
2471 
2472         *dir = -EINVAL;
2473 
2474         if (len < sizeof(struct sadb_x_policy) ||
2475             pol->sadb_x_policy_len*8 > len ||
2476             pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2477             (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2478                 return NULL;
2479 
2480         xp = xfrm_policy_alloc(GFP_ATOMIC);
2481         if (xp == NULL) {
2482                 *dir = -ENOBUFS;
2483                 return NULL;
2484         }
2485 
2486         xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2487                       XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2488 
2489         xp->lft.soft_byte_limit = XFRM_INF;
2490         xp->lft.hard_byte_limit = XFRM_INF;
2491         xp->lft.soft_packet_limit = XFRM_INF;
2492         xp->lft.hard_packet_limit = XFRM_INF;
2493         xp->family = family;
2494 
2495         xp->xfrm_nr = 0;
2496         if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2497             (*dir = parse_ipsecrequests(xp, pol)) < 0)
2498                 goto out;
2499 
2500         *dir = pol->sadb_x_policy_dir-1;
2501         return xp;
2502 
2503 out:
2504         kfree(xp);
2505         return NULL;
2506 }
2507 
2508 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2509 {
2510         struct sk_buff *skb;
2511         struct sadb_msg *hdr;
2512         struct sadb_sa *sa;
2513         struct sadb_address *addr;
2514         struct sadb_x_nat_t_port *n_port;
2515         struct sockaddr_in *sin;
2516 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2517         struct sockaddr_in6 *sin6;
2518 #endif
2519         int sockaddr_size;
2520         int size;
2521         __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2522         struct xfrm_encap_tmpl *natt = NULL;
2523 
2524         sockaddr_size = pfkey_sockaddr_size(x->props.family);
2525         if (!sockaddr_size)
2526                 return -EINVAL;
2527 
2528         if (!satype)
2529                 return -EINVAL;
2530 
2531         if (!x->encap)
2532                 return -EINVAL;
2533 
2534         natt = x->encap;
2535 
2536         /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2537          *
2538          * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2539          * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2540          */
2541         
2542         size = sizeof(struct sadb_msg) +
2543                 sizeof(struct sadb_sa) +
2544                 (sizeof(struct sadb_address) * 2) +
2545                 (sockaddr_size * 2) +
2546                 (sizeof(struct sadb_x_nat_t_port) * 2);
2547         
2548         skb =  alloc_skb(size + 16, GFP_ATOMIC);
2549         if (skb == NULL)
2550                 return -ENOMEM;
2551         
2552         hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2553         hdr->sadb_msg_version = PF_KEY_V2;
2554         hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2555         hdr->sadb_msg_satype = satype;
2556         hdr->sadb_msg_len = size / sizeof(uint64_t);
2557         hdr->sadb_msg_errno = 0;
2558         hdr->sadb_msg_reserved = 0;
2559         hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2560         hdr->sadb_msg_pid = 0;
2561 
2562         /* SA */
2563         sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2564         sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2565         sa->sadb_sa_exttype = SADB_EXT_SA;
2566         sa->sadb_sa_spi = x->id.spi;
2567         sa->sadb_sa_replay = 0;
2568         sa->sadb_sa_state = 0;
2569         sa->sadb_sa_auth = 0;
2570         sa->sadb_sa_encrypt = 0;
2571         sa->sadb_sa_flags = 0;
2572 
2573         /* ADDRESS_SRC (old addr) */
2574         addr = (struct sadb_address*)
2575                 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2576         addr->sadb_address_len = 
2577                 (sizeof(struct sadb_address)+sockaddr_size)/
2578                         sizeof(uint64_t);
2579         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2580         addr->sadb_address_proto = 0;
2581         addr->sadb_address_reserved = 0;
2582         if (x->props.family == AF_INET) {
2583                 addr->sadb_address_prefixlen = 32;
2584 
2585                 sin = (struct sockaddr_in *) (addr + 1);
2586                 sin->sin_family = AF_INET;
2587                 sin->sin_addr.s_addr = x->props.saddr.a4;
2588                 sin->sin_port = 0;
2589                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2590         }
2591 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2592         else if (x->props.family == AF_INET6) {
2593                 addr->sadb_address_prefixlen = 128;
2594 
2595                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2596                 sin6->sin6_family = AF_INET6;
2597                 sin6->sin6_port = 0;
2598                 sin6->sin6_flowinfo = 0;
2599                 memcpy(&sin6->sin6_addr,
2600                        x->props.saddr.a6, sizeof(struct in6_addr));
2601                 sin6->sin6_scope_id = 0;
2602         }
2603 #endif
2604         else
2605                 BUG();
2606 
2607         /* NAT_T_SPORT (old port) */
2608         n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2609         n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2610         n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
2611         n_port->sadb_x_nat_t_port_port = natt->encap_sport;
2612         n_port->sadb_x_nat_t_port_reserved = 0;
2613 
2614         /* ADDRESS_DST (new addr) */
2615         addr = (struct sadb_address*)
2616                 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2617         addr->sadb_address_len = 
2618                 (sizeof(struct sadb_address)+sockaddr_size)/
2619                         sizeof(uint64_t);
2620         addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2621         addr->sadb_address_proto = 0;
2622         addr->sadb_address_reserved = 0;
2623         if (x->props.family == AF_INET) {
2624                 addr->sadb_address_prefixlen = 32;
2625 
2626                 sin = (struct sockaddr_in *) (addr + 1);
2627                 sin->sin_family = AF_INET;
2628                 sin->sin_addr.s_addr = ipaddr->a4;
2629                 sin->sin_port = 0;
2630                 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2631         }
2632 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2633         else if (x->props.family == AF_INET6) {
2634                 addr->sadb_address_prefixlen = 128;
2635 
2636                 sin6 = (struct sockaddr_in6 *) (addr + 1);
2637                 sin6->sin6_family = AF_INET6;
2638                 sin6->sin6_port = 0;
2639                 sin6->sin6_flowinfo = 0;
2640                 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
2641                 sin6->sin6_scope_id = 0;
2642         }
2643 #endif
2644         else
2645                 BUG();
2646 
2647         /* NAT_T_DPORT (new port) */
2648         n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2649         n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2650         n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
2651         n_port->sadb_x_nat_t_port_port = sport;
2652         n_port->sadb_x_nat_t_port_reserved = 0;
2653 
2654         return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2655 }
2656 
2657 static int pfkey_sendmsg(struct kiocb *kiocb,
2658                          struct socket *sock, struct msghdr *msg, int len)
2659 {
2660         struct sock *sk = sock->sk;
2661         struct sk_buff *skb = NULL;
2662         struct sadb_msg *hdr = NULL;
2663         int err;
2664 
2665         err = -EOPNOTSUPP;
2666         if (msg->msg_flags & MSG_OOB)
2667                 goto out;
2668 
2669         err = -EMSGSIZE;
2670         if ((unsigned)len > sk->sk_sndbuf - 32)
2671                 goto out;
2672 
2673         err = -ENOBUFS;
2674         skb = alloc_skb(len, GFP_KERNEL);
2675         if (skb == NULL)
2676                 goto out;
2677 
2678         err = -EFAULT;
2679         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
2680                 goto out;
2681 
2682         hdr = pfkey_get_base_msg(skb, &err);
2683         if (!hdr)
2684                 goto out;
2685 
2686         down(&xfrm_cfg_sem);
2687         err = pfkey_process(sk, skb, hdr);
2688         up(&xfrm_cfg_sem);
2689 
2690 out:
2691         if (err && hdr && pfkey_error(hdr, err, sk) == 0)
2692                 err = 0;
2693         if (skb)
2694                 kfree_skb(skb);
2695 
2696         return err ? : len;
2697 }
2698 
2699 static int pfkey_recvmsg(struct kiocb *kiocb,
2700                          struct socket *sock, struct msghdr *msg, int len,
2701                          int flags)
2702 {
2703         struct sock *sk = sock->sk;
2704         struct sk_buff *skb;
2705         int copied, err;
2706 
2707         err = -EINVAL;
2708         if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC))
2709                 goto out;
2710 
2711         msg->msg_namelen = 0;
2712         skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2713         if (skb == NULL)
2714                 goto out;
2715 
2716         copied = skb->len;
2717         if (copied > len) {
2718                 msg->msg_flags |= MSG_TRUNC;
2719                 copied = len;
2720         }
2721 
2722         skb->h.raw = skb->data;
2723         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2724         if (err)
2725                 goto out_free;
2726 
2727         sock_recv_timestamp(msg, sk, skb);
2728 
2729         err = (flags & MSG_TRUNC) ? skb->len : copied;
2730 
2731 out_free:
2732         skb_free_datagram(sk, skb);
2733 out:
2734         return err;
2735 }
2736 
2737 static struct proto_ops pfkey_ops = {
2738         .family         =       PF_KEY,
2739         .owner          =       THIS_MODULE,
2740         /* Operations that make no sense on pfkey sockets. */
2741         .bind           =       sock_no_bind,
2742         .connect        =       sock_no_connect,
2743         .socketpair     =       sock_no_socketpair,
2744         .accept         =       sock_no_accept,
2745         .getname        =       sock_no_getname,
2746         .ioctl          =       sock_no_ioctl,
2747         .listen         =       sock_no_listen,
2748         .shutdown       =       sock_no_shutdown,
2749         .setsockopt     =       sock_no_setsockopt,
2750         .getsockopt     =       sock_no_getsockopt,
2751         .mmap           =       sock_no_mmap,
2752         .sendpage       =       sock_no_sendpage,
2753 
2754         /* Now the operations that really occur. */
2755         .release        =       pfkey_release,
2756         .poll           =       datagram_poll,
2757         .sendmsg        =       pfkey_sendmsg,
2758         .recvmsg        =       pfkey_recvmsg,
2759 };
2760 
2761 static struct net_proto_family pfkey_family_ops = {
2762         .family =       PF_KEY,
2763         .create =       pfkey_create,
2764         .owner  =       THIS_MODULE,
2765 };
2766 
2767 #ifdef CONFIG_PROC_FS
2768 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
2769                            int length, int *eof, void *data)
2770 {
2771         off_t pos = 0;
2772         off_t begin = 0;
2773         int len = 0;
2774         struct sock *s;
2775         struct hlist_node *node;
2776 
2777         len += sprintf(buffer,"sk       RefCnt Rmem   Wmem   User   Inode\n");
2778 
2779         read_lock(&pfkey_table_lock);
2780 
2781         sk_for_each(s, node, &pfkey_table) {
2782                 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
2783                                s,
2784                                atomic_read(&s->sk_refcnt),
2785                                atomic_read(&s->sk_rmem_alloc),
2786                                atomic_read(&s->sk_wmem_alloc),
2787                                sock_i_uid(s),
2788                                sock_i_ino(s)
2789                                );
2790 
2791                 buffer[len++] = '\n';
2792                 
2793                 pos = begin + len;
2794                 if (pos < offset) {
2795                         len = 0;
2796                         begin = pos;
2797                 }
2798                 if(pos > offset + length)
2799                         goto done;
2800         }
2801         *eof = 1;
2802 
2803 done:
2804         read_unlock(&pfkey_table_lock);
2805 
2806         *start = buffer + (offset - begin);
2807         len -= (offset - begin);
2808 
2809         if (len > length)
2810                 len = length;
2811         if (len < 0)
2812                 len = 0;
2813 
2814         return len;
2815 }
2816 #endif
2817 
2818 static struct xfrm_mgr pfkeyv2_mgr =
2819 {
2820         .id             = "pfkeyv2",
2821         .notify         = pfkey_send_notify,
2822         .acquire        = pfkey_send_acquire,
2823         .compile_policy = pfkey_compile_policy,
2824         .new_mapping    = pfkey_send_new_mapping,
2825 };
2826 
2827 static void __exit ipsec_pfkey_exit(void)
2828 {
2829         xfrm_unregister_km(&pfkeyv2_mgr);
2830         remove_proc_entry("net/pfkey", 0);
2831         sock_unregister(PF_KEY);
2832 }
2833 
2834 static int __init ipsec_pfkey_init(void)
2835 {
2836         sock_register(&pfkey_family_ops);
2837 #ifdef CONFIG_PROC_FS
2838         create_proc_read_entry("net/pfkey", 0, 0, pfkey_read_proc, NULL);
2839 #endif
2840         xfrm_register_km(&pfkeyv2_mgr);
2841         return 0;
2842 }
2843 
2844 module_init(ipsec_pfkey_init);
2845 module_exit(ipsec_pfkey_exit);
2846 MODULE_LICENSE("GPL");
2847 MODULE_ALIAS_NETPROTO(PF_KEY);
2848 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp