1 /* $Id: handler.h,v 1.11 2008/02/06 08:09:00 mk Exp $ */
   2 /*      $KAME: handler.h,v 1.44 2002/07/10 23:22:03 itojun Exp $        */
   3 
   4 /*
   5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
   6  * All rights reserved.
   7  * 
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 1. Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  * 2. Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in the
  15  *    documentation and/or other materials provided with the distribution.
  16  * 3. Neither the name of the project nor the names of its contributors
  17  *    may be used to endorse or promote products derived from this software
  18  *    without specific prior written permission.
  19  * 
  20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30  * SUCH DAMAGE.
  31  */
  32 
  33 #include "ike_pfkey.h"
  34 
  35 /* Phase 1 handler */
  36 /*
  37  * main mode:
  38  *      initiator               responder
  39  *  0   (---)                   (---)
  40  *  1   start                   start (1st msg received)
  41  *  2   (---)                   1st valid msg received
  42  *  3   1st msg sent            1st msg sent
  43  *  4   1st valid msg received  2st valid msg received
  44  *  5   2nd msg sent            2nd msg sent
  45  *  6   2nd valid msg received  3rd valid msg received
  46  *  7   3rd msg sent            3rd msg sent
  47  *  8   3rd valid msg received  (---)
  48  *  9   SA established          SA established
  49  *
  50  * aggressive mode:
  51  *      initiator               responder
  52  *  0   (---)                   (---)
  53  *  1   start                   start (1st msg received)
  54  *  2   (---)                   1st valid msg received
  55  *  3   1st msg sent            1st msg sent
  56  *  4   1st valid msg received  2st valid msg received
  57  *  5   (---)                   (---)
  58  *  6   (---)                   (---)
  59  *  7   (---)                   (---)
  60  *  8   (---)                   (---)
  61  *  9   SA established          SA established
  62  *
  63  * base mode:
  64  *      initiator               responder
  65  *  0   (---)                   (---)
  66  *  1   start                   start (1st msg received)
  67  *  2   (---)                   1st valid msg received
  68  *  3   1st msg sent            1st msg sent
  69  *  4   1st valid msg received  2st valid msg received
  70  *  5   2nd msg sent            (---)
  71  *  6   (---)                   (---)
  72  *  7   (---)                   (---)
  73  *  8   (---)                   (---)
  74  *  9   SA established          SA established
  75  */
  76 #define PHASE1ST_SPAWN                  0
  77 #define PHASE1ST_START                  1
  78 #define PHASE1ST_MSG1RECEIVED           2
  79 #define PHASE1ST_MSG1SENT               3
  80 #define PHASE1ST_MSG2RECEIVED           4
  81 #define PHASE1ST_MSG2SENT               5
  82 #define PHASE1ST_MSG3RECEIVED           6
  83 #define PHASE1ST_MSG3SENT               7
  84 #define PHASE1ST_MSG4RECEIVED           8
  85 #define PHASE1ST_ESTABLISHED            9
  86 #define PHASE1ST_EXPIRED                10
  87 #define PHASE1ST_MAX                    11
  88 
  89 /* About address semantics in each case.
  90  *                      initiator(addr=I)       responder(addr=R)
  91  *                      src     dst             src     dst
  92  *                      (local) (remote)        (local) (remote)
  93  * phase 1 handler      I       R               R       I
  94  * phase 2 handler      I       R               R       I
  95  * getspi msg           R       I               I       R
  96  * acquire msg          I       R
  97  * ID payload           I       R               I       R
  98  */
  99 struct ph1handle {
 100         isakmp_index_t  index;
 101 
 102         int status;             /* status of this SA */
 103         int side;               /* INITIATOR or RESPONDER */
 104 
 105         struct sockaddr *remote;        /* remote address to negosiate ph1 */
 106         struct sockaddr *local; /* local address to negosiate ph1 */
 107         /* XXX copy from rmconf due to anonymous configuration.
 108          * If anonymous will be forbidden, we do delete them. */
 109 
 110         struct rcf_remote *rmconf;      /* pointer to remote configuration */
 111         struct isakmpsa *proposal;
 112 
 113         struct isakmpsa *approval;      /* pointer to SA(s) approved. */
 114         rc_vchar_t *authstr;    /* place holder of string for auth. */
 115         /* for example pre-shared key */
 116 
 117         uint8_t version;        /* ISAKMP version */
 118         uint8_t etype;          /* Exchange type actually for use */
 119         uint8_t flags;          /* Flags */
 120         uint32_t msgid; /* message id */
 121 
 122 #ifdef ENABLE_NATT
 123         struct ph1natt_options *natt_options;   /* Selected NAT-T IKE version */
 124         uint32_t natt_flags;            /* NAT-T related flags */
 125 #ifdef ENABLE_FRAG
 126         int frag;                       /* IKE phase 1 fragmentation */
 127         struct isakmp_frag_item *frag_chain;    /* Received fragments */
 128 #endif
 129 #endif
 130 
 131         int dpd_support;        /* Does remote supports DPD ? */
 132         time_t dpd_lastack;     /* Last ack received */
 133         uint16_t dpd_seq;       /* DPD seq number to receive */
 134         uint8_t dpd_fails;      /* number of failures */
 135         struct sched *dpd_r_u;
 136 
 137         struct sched *sce;      /* schedule for expire */
 138 
 139         struct sched *scr;      /* schedule for resend */
 140         int retry_counter;      /* for resend. */
 141         rc_vchar_t *sendbuf;    /* buffer for re-sending */
 142 
 143         rc_vchar_t *dhpriv;     /* DH; private value */
 144         rc_vchar_t *dhpub;      /* DH; public value */
 145         rc_vchar_t *dhpub_p;    /* DH; partner's public value */
 146         rc_vchar_t *dhgxy;      /* DH; shared secret */
 147         rc_vchar_t *nonce;      /* nonce value */
 148         rc_vchar_t *nonce_p;    /* partner's nonce value */
 149         rc_vchar_t *skeyid;     /* SKEYID */
 150         rc_vchar_t *skeyid_d;   /* SKEYID_d */
 151         rc_vchar_t *skeyid_a;   /* SKEYID_a, i.e. hash */
 152         rc_vchar_t *skeyid_e;   /* SKEYID_e, i.e. encryption */
 153         rc_vchar_t *key;        /* cipher key */
 154         rc_vchar_t *hash;       /* HASH minus general header */
 155         rc_vchar_t *sig;        /* SIG minus general header */
 156         rc_vchar_t *sig_p;      /* peer's SIG minus general header */
 157         cert_t *cert;           /* CERT minus general header */
 158         cert_t *cert_p;         /* peer's CERT minus general header */
 159         cert_t *crl_p;          /* peer's CRL minus general header */
 160         cert_t *cr_p;           /* peer's CR not including general */
 161         rc_vchar_t *id;         /* ID minus gen header */
 162         rc_vchar_t *id_p;       /* partner's ID minus general header */
 163         /* i.e. strut ipsecdoi_id_b*. */
 164         struct isakmp_ivm *ivm; /* IVs */
 165 
 166         rc_vchar_t *sa;         /* whole SA payload to send/to be sent */
 167         /* to calculate HASH */
 168         /* NOT INCLUDING general header. */
 169 
 170         rc_vchar_t *sa_ret;     /* SA payload to reply/to be replyed */
 171         /* NOT INCLUDING general header. */
 172         /* NOTE: Should be release after use. */
 173 
 174 #ifdef HAVE_GSSAPI
 175         void *gssapi_state;     /* GSS-API specific state. */
 176         /* Allocated when needed */
 177         rc_vchar_t *gi_i;       /* optional initiator GSS id */
 178         rc_vchar_t *gi_r;       /* optional responder GSS id */
 179 #endif
 180 
 181         struct isakmp_pl_hash *pl_hash; /* pointer to hash payload */
 182 
 183         time_t created;         /* timestamp for establish */
 184 #ifdef ENABLE_STATS
 185         struct timeval start;
 186         struct timeval end;
 187 #endif
 188 
 189         uint32_t msgid2;        /* msgid counter for Phase 2 */
 190         int ph2cnt;             /* the number which is negotiated by this phase 1 */
 191             LIST_HEAD(_ph2ofph1_, ph2handle) ph2tree;
 192 
 193             LIST_ENTRY(ph1handle) chain;
 194 };
 195 
 196 /* Phase 2 handler */
 197 /* allocated per a SA or SA bundles of a pair of peer's IP addresses. */
 198 /*
 199  *      initiator               responder
 200  *  0   (---)                   (---)
 201  *  1   start                   start (1st msg received)
 202  *  2   acquire msg get         1st valid msg received
 203  *  3   getspi request sent     getspi request sent
 204  *  4   getspi done             getspi done
 205  *  5   1st msg sent            1st msg sent
 206  *  6   1st valid msg received  2nd valid msg received
 207  *  7   (commit bit)            (commit bit)
 208  *  8   SAs added               SAs added
 209  *  9   SAs established         SAs established
 210  * 10   SAs expired             SAs expired
 211  */
 212 #define PHASE2ST_SPAWN          0
 213 #define PHASE2ST_START          1
 214 #define PHASE2ST_STATUS2        2
 215 #define PHASE2ST_GETSPISENT     3
 216 #define PHASE2ST_GETSPIDONE     4
 217 #define PHASE2ST_MSG1SENT       5
 218 #define PHASE2ST_STATUS6        6
 219 #define PHASE2ST_COMMIT         7
 220 #define PHASE2ST_ADDSA          8
 221 #define PHASE2ST_ESTABLISHED    9
 222 #define PHASE2ST_EXPIRED        10
 223 #define PHASE2ST_MAX            11
 224 
 225 struct ph2handle {
 226         struct sockaddr *src;   /* my address of SA. */
 227         struct sockaddr *dst;   /* peer's address of SA. */
 228 
 229         /*
 230          * copy ip address from ID payloads when ID type is ip address.
 231          * In other case, they must be null.
 232          */
 233         struct sockaddr *src_id;
 234         struct sockaddr *dst_id;
 235 
 236         struct sadb_request sadb_request;
 237 #if 0
 238         uint32_t spid;          /* policy id by kernel */
 239 #endif
 240         struct rcf_selector *selector;
 241 
 242         int status;             /* ipsec sa status */
 243         uint8_t side;           /* INITIATOR or RESPONDER */
 244 
 245         struct sched *sce;      /* schedule for expire */
 246         struct sched *scr;      /* schedule for resend */
 247         int retry_counter;      /* for resend. */
 248         rc_vchar_t *sendbuf;    /* buffer for re-sending */
 249         rc_vchar_t *msg1;       /* buffer for re-sending */
 250         /* used for responder's first message */
 251 
 252         int retry_checkph1;     /* counter to wait phase 1 finished. */
 253         /* NOTE: actually it's timer. */
 254 
 255         uint32_t seq;           /* sequence number used by PF_KEY */
 256         /*
 257          * NOTE: In responder side, we can't identify each SAs
 258          * with same destination address for example, when
 259          * socket based SA is required.  So we set a identifier
 260          * number to "seq", and sent kernel by pfkey.
 261          */
 262         rc_type satype;         /* satype in rc_type */
 263         /*
 264          * saved satype in the original PF_KEY request from
 265          * the kernel in order to reply a error.
 266          */
 267 
 268         uint8_t flags;          /* Flags for phase 2 */
 269         uint32_t msgid; /* msgid for phase 2 */
 270 
 271 #if 0
 272         struct sainfo *sainfo;  /* place holder of sainfo */
 273 #endif
 274         struct saprop *proposal;        /* SA(s) proposal. */
 275         struct saprop *approval;        /* SA(s) approved. */
 276 #ifdef notyet
 277         caddr_t spidx_gen;      /* policy from peer's proposal */
 278 #endif
 279 
 280         struct dhgroup *pfsgrp; /* DH; prime number */
 281         rc_vchar_t *dhpriv;     /* DH; private value */
 282         rc_vchar_t *dhpub;      /* DH; public value */
 283         rc_vchar_t *dhpub_p;    /* DH; partner's public value */
 284         rc_vchar_t *dhgxy;      /* DH; shared secret */
 285         rc_vchar_t *id;         /* ID minus gen header */
 286         rc_vchar_t *id_p;       /* peer's ID minus general header */
 287         rc_vchar_t *nonce;      /* nonce value in phase 2 */
 288         rc_vchar_t *nonce_p;    /* partner's nonce value in phase 2 */
 289 
 290         rc_vchar_t *sa;         /* whole SA payload to send/to be sent */
 291         /* to calculate HASH */
 292         /* NOT INCLUDING general header. */
 293 
 294         rc_vchar_t *sa_ret;     /* SA payload to reply/to be replyed */
 295         /* NOT INCLUDING general header. */
 296         /* NOTE: Should be release after use. */
 297 
 298         struct isakmp_ivm *ivm; /* IVs */
 299 
 300 #ifdef ENABLE_STATS
 301         struct timeval start;
 302         struct timeval end;
 303 #endif
 304 
 305         /* byte counts for peer/self during current and previous cycle */
 306         uint64_t prev_peercount;
 307         uint64_t cur_peercount;
 308         uint64_t prev_selfcount;
 309         uint64_t cur_selfcount;
 310 
 311         struct ph1handle *ph1;  /* back pointer to isakmp status */
 312 
 313                   LIST_ENTRY(ph2handle) chain;
 314                   LIST_ENTRY(ph2handle) ph1bind;        /* chain to ph1handle */
 315 };
 316 
 317 /*
 318  * for handling initial contact.
 319  */
 320 struct contacted {
 321         struct sockaddr *remote;        /* remote address to negosiate ph1 */
 322                  LIST_ENTRY(contacted) chain;
 323 };
 324 
 325 /*
 326  * for checking a packet retransmited.
 327  */
 328 struct recvdpkt {
 329         struct sockaddr *remote;        /* the remote address */
 330         struct sockaddr *local; /* the local address */
 331         rc_vchar_t *hash;       /* hash of the received packet */
 332         rc_vchar_t *sendbuf;    /* buffer for the response */
 333         int retry_counter;      /* max retry to send */
 334         int lifetime;           /* max duration of retransmission */
 335         time_t time_send;       /* timestamp to send a packet */
 336         time_t created;         /* timestamp to create a queue */
 337 
 338         struct sched *scr;      /* schedule for resend, may not used */
 339 
 340               LIST_ENTRY(recvdpkt) chain;
 341 };
 342 
 343 /* for parsing ISAKMP header. */
 344 struct isakmp_parse_t {
 345         unsigned char type;             /* payload type of mine */
 346         int len;                /* ntohs(ptr->len) */
 347         struct isakmp_gen *ptr;
 348 };
 349 
 350 /*
 351  * for IV management.
 352  *
 353  * - normal case
 354  * initiator                                     responder
 355  * -------------------------                     --------------------------
 356  * initialize iv(A), ive(A).                     initialize iv(A), ive(A).
 357  * encode by ive(A).
 358  * save to iv(B).            ---[packet(B)]-->   save to ive(B).
 359  *                                               decode by iv(A).
 360  *                                               packet consistency.
 361  *                                               sync iv(B) with ive(B).
 362  *                                               check auth, integrity.
 363  *                                               encode by ive(B).
 364  * save to ive(C).          <--[packet(C)]---    save to iv(C).
 365  * decoded by iv(B).
 366  *      :
 367  *
 368  * - In the case that a error is found while cipher processing,
 369  * initiator                                     responder
 370  * -------------------------                     --------------------------
 371  * initialize iv(A), ive(A).                     initialize iv(A), ive(A).
 372  * encode by ive(A).
 373  * save to iv(B).            ---[packet(B)]-->   save to ive(B).
 374  *                                               decode by iv(A).
 375  *                                               packet consistency.
 376  *                                               sync iv(B) with ive(B).
 377  *                                               check auth, integrity.
 378  *                                               error found.
 379  *                                               create notify.
 380  *                                               get ive2(X) from iv(B).
 381  *                                               encode by ive2(X).
 382  * get iv2(X) from iv(B).   <--[packet(Y)]---    save to iv2(Y).
 383  * save to ive2(Y).
 384  * decoded by iv2(X).
 385  *      :
 386  *
 387  * The reason why the responder synchronizes iv with ive after checking the
 388  * packet consistency is that it is required to leave the IV for decoding
 389  * packet.  Because there is a potential of error while checking the packet
 390  * consistency.  Also the reason why that is before authentication and
 391  * integirty check is that the IV for informational exchange has to be made
 392  * by the IV which is after packet decoded and checking the packet consistency.
 393  * Otherwise IV mismatched happens between the intitiator and the responder.
 394  */
 395 struct isakmp_ivm {
 396         rc_vchar_t *iv;         /* for decoding packet */
 397         /* if phase 1, it's for computing phase2 iv */
 398         rc_vchar_t *ive;        /* for encoding packet */
 399 };
 400 
 401 /* for dumping */
 402 struct ph1dump {
 403         isakmp_index_t index;
 404         int status;
 405         int side;
 406         struct sockaddr_storage remote;
 407         struct sockaddr_storage local;
 408         uint8_t version;
 409         uint8_t etype;
 410         time_t created;
 411         int ph2cnt;
 412 };
 413 
 414 struct sockaddr;
 415 struct ph1handle;
 416 struct ph2handle;
 417 struct policyindex;
 418 
 419 extern struct ph1handle *getph1byindex (isakmp_index_t *);
 420 extern struct ph1handle *getph1byindex0 (isakmp_index_t *);
 421 extern struct ph1handle *getph1byaddr (struct sockaddr *,
 422                                            struct sockaddr *);
 423 extern struct ph1handle *getph1byaddrwop (struct sockaddr *,
 424                                               struct sockaddr *);
 425 extern rc_vchar_t *dumpph1 (void);
 426 extern struct ph1handle *newph1 (void);
 427 extern void delph1 (struct ph1handle *);
 428 extern int insph1 (struct ph1handle *);
 429 extern void remph1 (struct ph1handle *);
 430 extern void flushph1 (void);
 431 extern void initph1tree (void);
 432 
 433 extern struct ph2handle *getph2byspidx (struct policyindex *);
 434 extern struct ph2handle *getph2byspid (uint32_t);
 435 extern struct ph2handle *getph2byseq (uint32_t);
 436 extern struct ph2handle *getph2bymsgid (struct ph1handle *, uint32_t);
 437 extern struct ph2handle *getph2bysaidx (struct sockaddr *,
 438                                             struct sockaddr *, unsigned int,
 439                                             uint32_t);
 440 extern struct ph2handle *newph2 (void);
 441 extern void initph2 (struct ph2handle *);
 442 extern void delph2 (struct ph2handle *);
 443 extern int insph2 (struct ph2handle *);
 444 extern void remph2 (struct ph2handle *);
 445 extern void flushph2 (void);
 446 extern void deleteallph2 (struct sockaddr *, struct sockaddr *, unsigned int);
 447 extern void initph2tree (void);
 448 
 449 extern void bindph12 (struct ph1handle *, struct ph2handle *);
 450 extern void unbindph12 (struct ph2handle *);
 451 
 452 extern struct contacted *getcontacted (struct sockaddr *);
 453 extern int inscontacted (struct sockaddr *);
 454 extern void initctdtree (void);
 455 
 456 extern int check_recvdpkt (struct sockaddr *,
 457                                struct sockaddr *, rc_vchar_t *);
 458 extern int add_recvdpkt (struct sockaddr *, struct sockaddr *,
 459                              rc_vchar_t *, rc_vchar_t *, struct rcf_remote *);
 460 extern void init_recvdpkt (void);