1 /* routines for state objects
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * RCSID $Id: state.c,v 1.13 2006/04/29 18:16:02 as Exp $
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #include <sys/queue.h>
31 #include "constants.h"
33 #include "connections.h"
37 #include "packet.h" /* so we can calculate sizeof(struct isakmp_hdr) */
38 #include "keys.h" /* for free_public_key */
42 #include "demux.h" /* needs packet.h */
43 #include "ipsec_doi.h" /* needs demux.h and state.h */
47 #include "crypto.h" /* requires sha1.h and md5.h */
50 * Global variables: had to go somewhere, might as well be this file.
53 u_int16_t pluto_port
= IKE_UDP_PORT
; /* Pluto's port */
56 * This file has the functions that handle the
57 * state hash table and the Message ID list.
62 * A Message ID is contained in each IKE message header.
63 * For Phase 1 exchanges (Main and Aggressive), it will be zero.
64 * For other exchanges, which must be under the protection of an
65 * ISAKMP SA, the Message ID must be unique within that ISAKMP SA.
66 * Effectively, this labels the message as belonging to a particular
68 * BTW, we feel this uniqueness allows rekeying to be somewhat simpler
69 * than specified by draft-jenkins-ipsec-rekeying-06.txt.
71 * A MessageID is a 32 bit unsigned number. We represent the value
72 * internally in network order -- they are just blobs to us.
73 * They are unsigned numbers to make hashing and comparing easy.
75 * The following mechanism is used to allocate message IDs. This
76 * requires that we keep track of which numbers have already been used
77 * so that we don't allocate one in use.
82 msgid_t msgid
; /* network order */
83 struct msgid_list
*next
;
87 reserve_msgid(struct state
*isakmp_sa
, msgid_t msgid
)
91 passert(msgid
!= MAINMODE_MSGID
);
92 passert(IS_ISAKMP_ENCRYPTED(isakmp_sa
->st_state
));
94 for (p
= isakmp_sa
->st_used_msgids
; p
!= NULL
; p
= p
->next
)
95 if (p
->msgid
== msgid
)
98 p
= alloc_thing(struct msgid_list
, "msgid");
100 p
->next
= isakmp_sa
->st_used_msgids
;
101 isakmp_sa
->st_used_msgids
= p
;
106 generate_msgid(struct state
*isakmp_sa
)
108 int timeout
= 100; /* only try so hard for unique msgid */
111 passert(IS_ISAKMP_ENCRYPTED(isakmp_sa
->st_state
));
115 get_rnd_bytes((void *) &msgid
, sizeof(msgid
));
116 if (msgid
!= 0 && reserve_msgid(isakmp_sa
, msgid
))
121 plog("gave up looking for unique msgid; using 0x%08lx"
122 , (unsigned long) msgid
);
130 /* state table functions */
132 #define STATE_TABLE_SIZE 32
134 static struct state
*statetable
[STATE_TABLE_SIZE
];
136 static struct state
**
137 state_hash(const u_char
*icookie
, const u_char
*rcookie
, const ip_address
*peer
)
140 const unsigned char *byte_ptr
;
141 size_t length
= addrbytesptr(peer
, &byte_ptr
);
143 DBG(DBG_RAW
| DBG_CONTROL
,
144 DBG_dump("ICOOKIE:", icookie
, COOKIE_SIZE
);
145 DBG_dump("RCOOKIE:", rcookie
, COOKIE_SIZE
);
146 DBG_dump("peer:", byte_ptr
, length
));
148 /* XXX the following hash is pretty pathetic */
150 for (j
= 0; j
< COOKIE_SIZE
; j
++)
151 i
= i
* 407 + icookie
[j
] + rcookie
[j
];
153 for (j
= 0; j
< length
; j
++)
154 i
= i
* 613 + byte_ptr
[j
];
156 i
= i
% STATE_TABLE_SIZE
;
158 DBG(DBG_CONTROL
, DBG_log("state hash entry %d", i
));
160 return &statetable
[i
];
163 /* Get a state object.
164 * Caller must schedule an event for this object so that it doesn't leak.
165 * Caller must insert_state().
170 static const struct state blank_state
; /* initialized all to zero & NULL */
171 static so_serial_t next_so
= SOS_FIRST
;
174 st
= clone_thing(blank_state
, "struct state in new_state()");
175 st
->st_serialno
= next_so
++;
176 passert(next_so
> SOS_FIRST
); /* overflow can't happen! */
177 st
->st_whack_sock
= NULL_FD
;
178 DBG(DBG_CONTROL
, DBG_log("creating state object #%lu at %p",
179 st
->st_serialno
, (void *) st
));
184 * Initialize the state table (and mask*).
191 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
192 statetable
[i
] = (struct state
*) NULL
;
195 /* Find the state object with this serial number.
196 * This allows state object references that don't turn into dangerous
197 * dangling pointers: reference a state by its serial number.
198 * Returns NULL if there is no such state.
199 * If this turns out to be a significant CPU hog, it could be
200 * improved to use a hash table rather than sequential seartch.
203 state_with_serialno(so_serial_t sn
)
210 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
211 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
212 if (st
->st_serialno
== sn
)
218 /* Insert a state object in the hash table. The object is inserted
219 * at the begining of list.
220 * Needs cookies, connection, and msgid.
223 insert_state(struct state
*st
)
225 struct state
**p
= state_hash(st
->st_icookie
, st
->st_rcookie
226 , &st
->st_connection
->spd
.that
.host_addr
);
228 passert(st
->st_hashchain_prev
== NULL
&& st
->st_hashchain_next
== NULL
);
232 passert((*p
)->st_hashchain_prev
== NULL
);
233 (*p
)->st_hashchain_prev
= st
;
235 st
->st_hashchain_next
= *p
;
238 /* Ensure that somebody is in charge of killing this state:
239 * if no event is scheduled for it, schedule one to discard the state.
240 * If nothing goes wrong, this event will be replaced by
241 * a more appropriate one.
243 if (st
->st_event
== NULL
)
244 event_schedule(EVENT_SO_DISCARD
, 0, st
);
247 /* unlink a state object from the hash table, but don't free it
250 unhash_state(struct state
*st
)
252 /* unlink from forward chain */
253 struct state
**p
= st
->st_hashchain_prev
== NULL
254 ?
state_hash(st
->st_icookie
, st
->st_rcookie
255 , &st
->st_connection
->spd
.that
.host_addr
)
256 : &st
->st_hashchain_prev
->st_hashchain_next
;
258 /* unlink from forward chain */
260 *p
= st
->st_hashchain_next
;
262 /* unlink from backward chain */
263 if (st
->st_hashchain_next
!= NULL
)
265 passert(st
->st_hashchain_next
->st_hashchain_prev
== st
);
266 st
->st_hashchain_next
->st_hashchain_prev
= st
->st_hashchain_prev
;
269 st
->st_hashchain_next
= st
->st_hashchain_prev
= NULL
;
272 /* Free the Whack socket file descriptor.
273 * This has the side effect of telling Whack that we're done.
276 release_whack(struct state
*st
)
278 close_any(st
->st_whack_sock
);
281 /* delete a state object */
283 delete_state(struct state
*st
)
285 struct connection
*const c
= st
->st_connection
;
286 struct state
*old_cur_state
= cur_state
== st? NULL
: cur_state
;
290 /* If DPD is enabled on this state object, clear any pending events */
291 if(st
->st_dpd_event
!= NULL
)
292 delete_dpd_event(st
);
294 /* if there is a suspended state transition, disconnect us */
295 if (st
->st_suspended_md
!= NULL
)
297 passert(st
->st_suspended_md
->st
== st
);
298 st
->st_suspended_md
->st
= NULL
;
301 /* tell the other side of any IPSEC SAs that are going down */
302 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
)
303 || IS_ISAKMP_SA_ESTABLISHED(st
->st_state
))
306 delete_event(st
); /* delete any pending timer event */
308 /* Ditch anything pending on ISAKMP SA being established.
309 * Note: this must be done before the unhash_state to prevent
310 * flush_pending_by_state inadvertently and prematurely
311 * deleting our connection.
313 flush_pending_by_state(st
);
315 /* effectively, this deletes any ISAKMP SA that this state represents */
318 /* tell kernel to delete any IPSEC SA
319 * ??? we ought to tell peer to delete IPSEC SAs
321 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
))
322 delete_ipsec_sa(st
, FALSE
);
323 else if (IS_ONLY_INBOUND_IPSEC_SA_ESTABLISHED(st
->st_state
))
324 delete_ipsec_sa(st
, TRUE
);
326 if (c
->newest_ipsec_sa
== st
->st_serialno
)
327 c
->newest_ipsec_sa
= SOS_NOBODY
;
329 if (c
->newest_isakmp_sa
== st
->st_serialno
)
330 c
->newest_isakmp_sa
= SOS_NOBODY
;
332 st
->st_connection
= NULL
; /* we might be about to free it */
333 cur_state
= old_cur_state
; /* without st_connection, st isn't complete */
334 connection_discard(c
);
338 /* from here on we are just freeing RAM */
341 struct msgid_list
*p
= st
->st_used_msgids
;
345 struct msgid_list
*q
= p
;
351 unreference_key(&st
->st_peer_pubkey
);
353 if (st
->st_sec_in_use
)
354 mpz_clear(&(st
->st_sec
));
356 pfreeany(st
->st_tpacket
.ptr
);
357 pfreeany(st
->st_rpacket
.ptr
);
358 pfreeany(st
->st_p1isa
.ptr
);
359 pfreeany(st
->st_gi
.ptr
);
360 pfreeany(st
->st_gr
.ptr
);
361 pfreeany(st
->st_shared
.ptr
);
362 pfreeany(st
->st_ni
.ptr
);
363 pfreeany(st
->st_nr
.ptr
);
364 pfreeany(st
->st_skeyid
.ptr
);
365 pfreeany(st
->st_skeyid_d
.ptr
);
366 pfreeany(st
->st_skeyid_a
.ptr
);
367 pfreeany(st
->st_skeyid_e
.ptr
);
368 pfreeany(st
->st_enc_key
.ptr
);
369 pfreeany(st
->st_ah
.our_keymat
);
370 pfreeany(st
->st_ah
.peer_keymat
);
371 pfreeany(st
->st_esp
.our_keymat
);
372 pfreeany(st
->st_esp
.peer_keymat
);
378 * Is a connection in use by some state?
381 states_use_connection(struct connection
*c
)
383 /* are there any states still using it? */
384 struct state
*st
= NULL
;
387 for (i
= 0; st
== NULL
&& i
< STATE_TABLE_SIZE
; i
++)
388 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
389 if (st
->st_connection
== c
)
396 * delete all states that were created for a given connection.
397 * if relations == TRUE, then also delete states that share
398 * the same phase 1 SA.
401 delete_states_by_connection(struct connection
*c
, bool relations
)
404 /* this kludge avoids an n^2 algorithm */
405 enum connection_kind ck
= c
->kind
;
406 struct spd_route
*sr
;
408 /* save this connection's isakmp SA, since it will get set to later SOS_NOBODY */
409 so_serial_t parent_sa
= c
->newest_isakmp_sa
;
411 if (ck
== CK_INSTANCE
)
412 c
->kind
= CK_GOING_AWAY
;
414 /* We take two passes so that we delete any ISAKMP SAs last.
415 * This allows Delete Notifications to be sent.
416 * ?? We could probably double the performance by caching any
417 * ISAKMP SA states found in the first pass, avoiding a second.
419 for (pass
= 0; pass
!= 2; pass
++)
423 /* For each hash chain... */
424 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
428 /* For each state in the hash chain... */
429 for (st
= statetable
[i
]; st
!= NULL
; )
431 struct state
*this = st
;
433 st
= st
->st_hashchain_next
; /* before this is deleted */
436 if ((this->st_connection
== c
437 || (relations
&& parent_sa
!= SOS_NOBODY
438 && this->st_clonedfrom
== parent_sa
))
439 && (pass
== 1 || !IS_ISAKMP_SA_ESTABLISHED(this->st_state
)))
441 struct state
*old_cur_state
442 = cur_state
== this? NULL
: cur_state
;
444 lset_t old_cur_debugging
= cur_debugging
;
448 plog("deleting state (%s)"
449 , enum_show(&state_names
, this->st_state
));
451 cur_state
= old_cur_state
;
453 cur_debugging
= old_cur_debugging
;
463 passert(sr
->eroute_owner
== SOS_NOBODY
);
464 passert(sr
->routing
!= RT_ROUTED_TUNNEL
);
468 if (ck
== CK_INSTANCE
)
471 delete_connection(c
, relations
);
475 /* Walk through the state table, and delete each state whose phase 1 (IKE)
476 * peer is among those given.
479 delete_states_by_peer(ip_address
*peer
)
481 char peerstr
[ADDRTOT_BUF
];
484 addrtot(peer
, 0, peerstr
, sizeof(peerstr
));
486 /* For each hash chain... */
487 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
491 /* For each state in the hash chain... */
492 for (st
= statetable
[i
]; st
!= NULL
; )
494 struct state
*this = st
;
495 struct spd_route
*sr
;
496 struct connection
*c
= this->st_connection
;
498 st
= st
->st_hashchain_next
; /* before this is deleted */
500 /* ??? Is it not the case that the peer is the same for all spds? */
501 for (sr
= &c
->spd
; sr
!= NULL
; sr
= sr
->next
)
503 if (sameaddr(&sr
->that
.host_addr
, peer
))
505 plog("peer %s for connection %s deleting - claimed to have crashed"
508 delete_states_by_connection(c
, TRUE
);
509 break; /* can only delete it once */
516 /* Duplicate a Phase 1 state object, to create a Phase 2 object.
517 * Caller must schedule an event for this object so that it doesn't leak.
518 * Caller must insert_state().
521 duplicate_state(struct state
*st
)
525 DBG(DBG_CONTROL
, DBG_log("duplicating state object #%lu",
528 /* record use of the Phase 1 state */
529 st
->st_outbound_count
++;
530 st
->st_outbound_time
= now();
534 memcpy(nst
->st_icookie
, st
->st_icookie
, COOKIE_SIZE
);
535 memcpy(nst
->st_rcookie
, st
->st_rcookie
, COOKIE_SIZE
);
537 nst
->st_connection
= st
->st_connection
;
538 nst
->st_doi
= st
->st_doi
;
539 nst
->st_situation
= st
->st_situation
;
540 nst
->st_clonedfrom
= st
->st_serialno
;
541 nst
->st_oakley
= st
->st_oakley
;
542 nst
->st_modecfg
= st
->st_modecfg
;
544 # define clone_chunk(ch, name) \
545 clonetochunk(nst->ch, st->ch.ptr, st->ch.len, name)
547 clone_chunk(st_skeyid_d
, "st_skeyid_d in duplicate_state");
548 clone_chunk(st_skeyid_a
, "st_skeyid_a in duplicate_state");
549 clone_chunk(st_skeyid_e
, "st_skeyid_e in duplicate_state");
550 clone_chunk(st_enc_key
, "st_enc_key in duplicate_state");
558 void for_each_state(void *(f
)(struct state
*, void *data
), void *data
)
560 struct state
*st
, *ocs
= cur_state
;
562 for (i
=0; i
<STATE_TABLE_SIZE
; i
++) {
563 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
) {
573 * Find a state object.
576 find_state(const u_char
*icookie
577 , const u_char
*rcookie
578 , const ip_address
*peer
579 , msgid_t
/*network order*/ msgid
)
581 struct state
*st
= *state_hash(icookie
, rcookie
, peer
);
583 while (st
!= (struct state
*) NULL
)
584 if (sameaddr(peer
, &st
->st_connection
->spd
.that
.host_addr
)
585 && memcmp(icookie
, st
->st_icookie
, COOKIE_SIZE
) == 0
586 && memcmp(rcookie
, st
->st_rcookie
, COOKIE_SIZE
) == 0
587 && msgid
== st
->st_msgid
)
590 st
= st
->st_hashchain_next
;
594 DBG_log("state object not found");
596 DBG_log("state object #%lu found, in %s"
598 , enum_show(&state_names
, st
->st_state
)));
603 /* Find the state that sent a packet
604 * ??? this could be expensive -- it should be rate-limited to avoid DoS
607 find_sender(size_t packet_len
, u_char
*packet
)
612 if (packet_len
>= sizeof(struct isakmp_hdr
))
613 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
614 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
615 if (st
->st_tpacket
.ptr
!= NULL
616 && st
->st_tpacket
.len
== packet_len
617 && memcmp(st
->st_tpacket
.ptr
, packet
, packet_len
) == 0)
624 find_phase2_state_to_delete(const struct state
*p1st
633 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
635 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
637 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
)
638 && p1st
->st_connection
->host_pair
== st
->st_connection
->host_pair
639 && same_peer_ids(p1st
->st_connection
, st
->st_connection
, NULL
))
641 struct ipsec_proto_info
*pr
= protoid
== PROTO_IPSEC_AH
642 ?
&st
->st_ah
: &st
->st_esp
;
646 if (pr
->attrs
.spi
== spi
)
648 if (pr
->our_spi
== spi
)
657 /* Find newest Phase 1 negotiation state object for suitable for connection c
660 find_phase1_state(const struct connection
*c
, lset_t ok_states
)
667 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
668 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
669 if (LHAS(ok_states
, st
->st_state
)
670 && c
->host_pair
== st
->st_connection
->host_pair
671 && same_peer_ids(c
, st
->st_connection
, NULL
)
672 && (best
== NULL
|| best
->st_serialno
< st
->st_serialno
))
679 state_eroute_usage(ip_subnet
*ours
, ip_subnet
*his
680 , unsigned long count
, time_t nw
)
685 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
687 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
689 struct connection
*c
= st
->st_connection
;
692 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
)
693 && c
->spd
.eroute_owner
== st
->st_serialno
694 && c
->spd
.routing
== RT_ROUTED_TUNNEL
695 && samesubnet(&c
->spd
.this.client
, ours
)
696 && samesubnet(&c
->spd
.that
.client
, his
))
698 if (st
->st_outbound_count
!= count
)
700 st
->st_outbound_count
= count
;
701 st
->st_outbound_time
= nw
;
709 char ourst
[SUBNETTOT_BUF
];
710 char hist
[SUBNETTOT_BUF
];
712 subnettot(ours
, 0, ourst
, sizeof(ourst
));
713 subnettot(his
, 0, hist
, sizeof(hist
));
714 DBG_log("unknown tunnel eroute %s -> %s found in scan"
719 void fmt_state(struct state
*st
, time_t n
720 , char *state_buf
, size_t state_buf_len
721 , char *state_buf2
, size_t state_buf2_len
)
723 /* what the heck is interesting about a state? */
724 const struct connection
*c
= st
->st_connection
;
726 long delta
= st
->st_event
->ev_time
>= n
727 ?
(long)(st
->st_event
->ev_time
- n
)
728 : -(long)(n
- st
->st_event
->ev_time
);
730 char inst
[CONN_INST_BUF
];
731 const char *np1
= c
->newest_isakmp_sa
== st
->st_serialno
732 ?
"; newest ISAKMP" : "";
733 const char *np2
= c
->newest_ipsec_sa
== st
->st_serialno
734 ?
"; newest IPSEC" : "";
736 const char *eo
= c
->spd
.eroute_owner
== st
->st_serialno
737 ?
"; eroute owner" : "";
739 passert(st
->st_event
!= 0);
741 fmt_conn_instance(c
, inst
);
743 snprintf(state_buf
, state_buf_len
744 , "#%lu: \"%s\"%s %s (%s); %s in %lds%s%s%s"
747 , enum_name(&state_names
, st
->st_state
)
748 , state_story
[st
->st_state
- STATE_MAIN_R0
]
749 , enum_name(&timer_event_names
, st
->st_event
->ev_type
)
753 /* print out SPIs if SAs are established */
754 if (state_buf2_len
!= 0)
755 state_buf2
[0] = '\0'; /* default to empty */
756 if (IS_IPSEC_SA_ESTABLISHED(st
->st_state
))
760 char buf
[SATOT_BUF
*6 + 2*20 + 1];
761 const char *p_end
= buf
+ sizeof(buf
);
764 # define add_said(adst, aspi, aproto) { \
767 initsaid(adst, aspi, aproto, &s); \
771 p += satot(&s, 0, p, p_end - p) - 1; \
775 # define add_sa_info(st, inbound) { \
779 if (get_sa_info(st, inbound, &bytes, &use_time)) \
781 p += snprintf(p, p_end - p, " (%'u bytes", bytes); \
782 if (bytes > 0 && use_time != UNDEFINED_TIME) \
783 p += snprintf(p, p_end - p, ", %ds ago", (int)(now - use_time)); \
784 p += snprintf(p, p_end - p, ")"); \
789 if (st
->st_ah
.present
)
791 add_said(&c
->spd
.that
.host_addr
, st
->st_ah
.attrs
.spi
, SA_AH
);
792 add_said(&c
->spd
.this.host_addr
, st
->st_ah
.our_spi
, SA_AH
);
794 if (st
->st_esp
.present
)
796 time_t now
= time(NULL
);
798 add_said(&c
->spd
.that
.host_addr
, st
->st_esp
.attrs
.spi
, SA_ESP
);
799 add_sa_info(st
, FALSE
);
800 add_said(&c
->spd
.this.host_addr
, st
->st_esp
.our_spi
, SA_ESP
);
801 add_sa_info(st
, TRUE
);
803 if (st
->st_ipcomp
.present
)
805 add_said(&c
->spd
.that
.host_addr
, st
->st_ipcomp
.attrs
.spi
, SA_COMP
);
806 add_said(&c
->spd
.this.host_addr
, st
->st_ipcomp
.our_spi
, SA_COMP
);
809 tunnel
= st
->st_ah
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
810 || st
->st_esp
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
811 || st
->st_ipcomp
.attrs
.encapsulation
== ENCAPSULATION_MODE_TUNNEL
;
812 p
+= snprintf(p
, p_end
- p
, "; %s", tunnel?
"tunnel":"transport");
815 snprintf(state_buf2
, state_buf2_len
832 * isakmp_sa (XXX probably wrong)
836 state_compare(const void *a
, const void *b
)
838 const struct state
*sap
= *(const struct state
*const *)a
;
839 struct connection
*ca
= sap
->st_connection
;
840 const struct state
*sbp
= *(const struct state
*const *)b
;
841 struct connection
*cb
= sbp
->st_connection
;
843 /* DBG_log("comparing %s to %s", ca->name, cb->name); */
845 return connection_compare(ca
, cb
);
849 show_states_status(const char *name
)
853 char state_buf
[LOG_WIDTH
];
854 char state_buf2
[LOG_WIDTH
];
856 struct state
**array
;
858 /* make count of states */
860 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
864 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
866 if (name
== NULL
|| streq(name
, st
->st_connection
->name
))
871 /* build the array */
872 array
= alloc_bytes(sizeof(struct state
*)*count
, "state array");
874 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
878 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
880 if (name
== NULL
|| streq(name
, st
->st_connection
->name
))
886 qsort(array
, count
, sizeof(struct state
*), state_compare
);
888 /* now print sorted results */
889 for (i
= 0; i
< count
; i
++)
895 fmt_state(st
, n
, state_buf
, sizeof(state_buf
)
896 , state_buf2
, sizeof(state_buf2
));
897 whack_log(RC_COMMENT
, state_buf
);
898 if (state_buf2
[0] != '\0')
899 whack_log(RC_COMMENT
, state_buf2
);
901 /* show any associated pending Phase 2s */
902 if (IS_PHASE1(st
->st_state
))
903 show_pending_phase2(st
->st_connection
->host_pair
, st
);
906 whack_log(RC_COMMENT
, BLANK_FORMAT
); /* spacer */
912 /* Given that we've used up a range of unused CPI's,
913 * search for a new range of currently unused ones.
914 * Note: this is very expensive when not trivial!
915 * If we can't find one easily, choose 0 (a bad SPI,
916 * no matter what order) indicating failure.
919 find_my_cpi_gap(cpi_t
*latest_cpi
, cpi_t
*first_busy_cpi
)
922 cpi_t base
= *latest_cpi
;
927 closest
= ~0; /* not close at all */
928 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
932 for (st
= statetable
[i
]; st
!= NULL
; st
= st
->st_hashchain_next
)
934 if (st
->st_ipcomp
.present
)
936 cpi_t c
= ntohl(st
->st_ipcomp
.our_spi
) - base
;
942 /* oops: next spot is occupied; start over */
946 *latest_cpi
= *first_busy_cpi
= 0;
950 if (base
> IPCOMP_LAST_NEGOTIATED
)
951 base
= IPCOMP_FIRST_NEGOTIATED
;
952 goto startover
; /* really a tail call */
959 *latest_cpi
= base
; /* base is first in next free range */
960 *first_busy_cpi
= closest
+ base
; /* and this is the roof */
963 /* Muck with high-order 16 bits of this SPI in order to make
964 * the corresponding SAID unique.
965 * Its low-order 16 bits hold a well-known IPCOMP CPI.
966 * Oh, and remember that SPIs are stored in network order.
967 * Kludge!!! So I name it with the non-English word "uniquify".
968 * If we can't find one easily, return 0 (a bad SPI,
969 * no matter what order) indicating failure.
972 uniquify_his_cpi(ipsec_spi_t cpi
, struct state
*st
)
979 /* network order makes first two bytes our target */
980 get_rnd_bytes((u_char
*)&cpi
, 2);
982 /* Make sure that the result is unique.
983 * Hard work. If there is no unique value, we'll loop forever!
985 for (i
= 0; i
< STATE_TABLE_SIZE
; i
++)
989 for (s
= statetable
[i
]; s
!= NULL
; s
= s
->st_hashchain_next
)
991 if (s
->st_ipcomp
.present
992 && sameaddr(&s
->st_connection
->spd
.that
.host_addr
993 , &st
->st_connection
->spd
.that
.host_addr
)
994 && cpi
== s
->st_ipcomp
.attrs
.spi
)
997 return 0; /* FAILURE */