Home | History | Annotate | Line # | Download | only in server
mdb6.c revision 1.1.1.1
      1  1.1  christos /*	$NetBSD: mdb6.c,v 1.1.1.1 2018/04/07 22:34:28 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2007-2017 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
     11  1.1  christos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     12  1.1  christos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
     13  1.1  christos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     14  1.1  christos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
     15  1.1  christos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     16  1.1  christos  * PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.1  christos #include <sys/cdefs.h>
     20  1.1  christos __RCSID("$NetBSD: mdb6.c,v 1.1.1.1 2018/04/07 22:34:28 christos Exp $");
     21  1.1  christos 
     22  1.1  christos 
     23  1.1  christos /*!
     24  1.1  christos  * \todo assert()
     25  1.1  christos  * \todo simplify functions, as pool is now in iaaddr
     26  1.1  christos  */
     27  1.1  christos 
     28  1.1  christos /*! \file server/mdb6.c
     29  1.1  christos  *
     30  1.1  christos  * \page ipv6structures IPv6 Structures Overview
     31  1.1  christos  *
     32  1.1  christos  * A brief description of the IPv6 structures as reverse engineered.
     33  1.1  christos  *
     34  1.1  christos  * There are four major data structures in the lease configuraion.
     35  1.1  christos  *
     36  1.1  christos  * - shared_network - The shared network is the outer enclosing scope for a
     37  1.1  christos  *                    network region that shares a broadcast domain.  It is
     38  1.1  christos  *                    composed of one or more subnets all of which are valid
     39  1.1  christos  *                    in the given region.  The share network may be
     40  1.1  christos  *                    explicitly defined or implicitly created if there is
     41  1.1  christos  *                    only a subnet statement.  This structrure is shared
     42  1.1  christos  *                    with v4.  Each shared network statment or naked subnet
     43  1.1  christos  *                    will map to one of these structures
     44  1.1  christos  *
     45  1.1  christos  * - subnet     - The subnet structure mostly specifies the address range
     46  1.1  christos  *                that could be valid in a given region.  This structute
     47  1.1  christos  *                doesn't include the addresses that the server can delegate
     48  1.1  christos  *                those are in the ipv6_pool.  This structure is also shared
     49  1.1  christos  *                with v4.  Each subnet statement will map to one of these
     50  1.1  christos  *                structures.
     51  1.1  christos  *
     52  1.1  christos  * - ipv6_pond  - The pond structure is a grouping of the address and prefix
     53  1.1  christos  *                information via the pointers to the ipv6_pool and the
     54  1.1  christos  *                allowability of this pool for given clinets via the permit
     55  1.1  christos  *                lists and the valid TIMEs.  This is equivilent to the v4
     56  1.1  christos  *                pool structure and would have been named ip6_pool except
     57  1.1  christos  *                that the name was already in use.  Generally each pool6
     58  1.1  christos  *                statement will map to one of these structures. In addition
     59  1.1  christos  *                there may be one or for each group of naked range6 and
     60  1.1  christos  *                prefix6 statements within a shared network that share
     61  1.1  christos  *                the same group of statements.
     62  1.1  christos  *
     63  1.1  christos  * - ipv6_pool - this contains information about a pool of addresses or prefixes
     64  1.1  christos  *               that the server is using.  This includes a hash table that
     65  1.1  christos  *               tracks the active items and a pair of heap tables one for
     66  1.1  christos  *               active items and one for non-active items.  The heap tables
     67  1.1  christos  *               are used to determine the next items to be modified due to
     68  1.1  christos  *               timing events (expire mostly).
     69  1.1  christos  *
     70  1.1  christos  * The linkages then look like this:
     71  1.1  christos  * \verbatim
     72  1.1  christos  *+--------------+   +-------------+
     73  1.1  christos  *|Shared Network|   | ipv6_pond   |
     74  1.1  christos  *|   group      |   |   group     |
     75  1.1  christos  *|              |   | permit info |
     76  1.1  christos  *|              |   |    next    ---->
     77  1.1  christos  *|    ponds    ---->|             |
     78  1.1  christos  *|              |<----  shared    |
     79  1.1  christos  *|   Subnets    |   |    pools    |
     80  1.1  christos  *+-----|--------+   +------|------+
     81  1.1  christos  *      |  ^                |    ^
     82  1.1  christos  *      |  |                v    |
     83  1.1  christos  *      |  |         +-----------|-+
     84  1.1  christos  *      |  |         | ipv6_pool | |
     85  1.1  christos  *      |  |         |    type   | |
     86  1.1  christos  *      |  |         |   ipv6_pond |
     87  1.1  christos  *      |  |         |             |
     88  1.1  christos  *      |  |         |    next    ---->
     89  1.1  christos  *      |  |         |             |
     90  1.1  christos  *      |  |         |   subnet    |
     91  1.1  christos  *      |  |         +-----|-------+
     92  1.1  christos  *      |  |               |
     93  1.1  christos  *      |  |               v
     94  1.1  christos  *      |  |         +-------------+
     95  1.1  christos  *      |  |         |   subnet    |
     96  1.1  christos  *      |  +----------   shared    |
     97  1.1  christos  *      +----------->|             |
     98  1.1  christos  *                   |   group     |
     99  1.1  christos  *                   +-------------+
    100  1.1  christos  *
    101  1.1  christos  * The shared network contains a list of all the subnets that are on a broadcast
    102  1.1  christos  * doamin.  These can be used to determine if an address makes sense in a given
    103  1.1  christos  * domain, but the subnets do not contain the addresses the server can delegate.
    104  1.1  christos  * Those are stored in the ponds and pools.
    105  1.1  christos  *
    106  1.1  christos  * In the simple case to find an acceptable address the server would first find
    107  1.1  christos  * the shared network the client is on based on either the interface used to
    108  1.1  christos  * receive the request or the relay agent's information.  From the shared
    109  1.1  christos  * network the server will walk through it's list of ponds.  For each pond it
    110  1.1  christos  * will evaluate the permit information against the (already done) classification.
    111  1.1  christos  * If it finds an acceptable pond it will then walk through the pools for that
    112  1.1  christos  * pond.  The server first checks the type of the pool (NA, TA and PD) agaisnt the
    113  1.1  christos  * request and if they match it attemps to find an address within that pool.  On
    114  1.1  christos  * success the address is used, on failure the server steps to the next pool and
    115  1.1  christos  * if necessary to the next pond.
    116  1.1  christos  *
    117  1.1  christos  * When the server is successful in finding an address it will execute any
    118  1.1  christos  * statements assocaited with the pond, then the subnet, then the shared
    119  1.1  christos  * network the group field is for in the above picture).
    120  1.1  christos  *
    121  1.1  christos  * In configurations that don't include either a shared network or a pool6
    122  1.1  christos  * statement (or both) the missing pieces are created.
    123  1.1  christos  *
    124  1.1  christos  *
    125  1.1  christos  * There are three major data structuress involved in the lease database:
    126  1.1  christos  *
    127  1.1  christos  * - ipv6_pool - see above
    128  1.1  christos  * - ia_xx   - this contains information about a single IA from a request
    129  1.1  christos  *             normally it will contain one pointer to a lease for the client
    130  1.1  christos  *             but it may contain more in some circumstances.  There are 3
    131  1.1  christos  *             hash tables to aid in accessing these one each for NA, TA and PD.
    132  1.1  christos  * - iasubopt - the v6 lease structure.  These are created dynamically when
    133  1.1  christos  *              a client asks for something and will eventually be destroyed
    134  1.1  christos  *              if the client doesn't re-ask for that item.  A lease has space
    135  1.1  christos  *              for backpointers to the IA and to the pool to which it belongs.
    136  1.1  christos  *              The pool backpointer is always filled, the IA pointer may not be.
    137  1.1  christos  *
    138  1.1  christos  * In normal use we then have something like this:
    139  1.1  christos  *
    140  1.1  christos  * \verbatim
    141  1.1  christos  * ia hash tables
    142  1.1  christos  *  ia_na_active                           +----------------+
    143  1.1  christos  *  ia_ta_active          +------------+   | pool           |
    144  1.1  christos  *  ia_pd_active          | iasubopt   |<--|  active hash   |
    145  1.1  christos  * +-----------------+    | aka lease  |<--|  active heap   |
    146  1.1  christos  * | ia_xx           |    |  pool ptr  |-->|                |
    147  1.1  christos  * |  iasubopt array |<---|  iaptr     |<--|  inactive heap |
    148  1.1  christos  * |   lease ptr     |--->|            |   |                |
    149  1.1  christos  * +-----------------+    +------------+   +----------------+
    150  1.1  christos  * \endverbatim
    151  1.1  christos  *
    152  1.1  christos  * For the pool either the inactive heap will have a pointer
    153  1.1  christos  * or both the active heap and the active hash will have pointers.
    154  1.1  christos  *
    155  1.1  christos  * I think there are several major items to notice.   The first is
    156  1.1  christos  * that as a lease moves around it will be added to and removed
    157  1.1  christos  * from the address hash table in the pool and between the active
    158  1.1  christos  * and inactive hash tables.  The hash table and the active heap
    159  1.1  christos  * are used when the lease is either active or abandoned.  The
    160  1.1  christos  * inactive heap is used for all other states.  In particular a
    161  1.1  christos  * lease that has expired or been released will be cleaned
    162  1.1  christos  * (DDNS removal etc) and then moved to the inactive heap.  After
    163  1.1  christos  * some time period (currently 1 hour) it will be freed.
    164  1.1  christos  *
    165  1.1  christos  * The second is that when a client requests specific addresses,
    166  1.1  christos  * either because it previously owned them or if the server supplied
    167  1.1  christos  * them as part of a solicit, the server will try to lookup the ia_xx
    168  1.1  christos  * associated with the client and find the addresses there.  If it
    169  1.1  christos  * does find appropriate leases it moves them from the old IA to
    170  1.1  christos  * a new IA and eventually replaces the old IA with the new IA
    171  1.1  christos  * in the IA hash tables.
    172  1.1  christos  *
    173  1.1  christos  */
    174  1.1  christos #include "config.h"
    175  1.1  christos 
    176  1.1  christos #include <sys/types.h>
    177  1.1  christos #include <time.h>
    178  1.1  christos #include <netinet/in.h>
    179  1.1  christos 
    180  1.1  christos #include <stdarg.h>
    181  1.1  christos #include "dhcpd.h"
    182  1.1  christos #include "omapip/omapip.h"
    183  1.1  christos #include "omapip/hash.h"
    184  1.1  christos #include <isc/md5.h>
    185  1.1  christos 
    186  1.1  christos HASH_FUNCTIONS(ia, unsigned char *, struct ia_xx, ia_hash_t,
    187  1.1  christos 	       ia_reference, ia_dereference, do_string_hash)
    188  1.1  christos 
    189  1.1  christos ia_hash_t *ia_na_active;
    190  1.1  christos ia_hash_t *ia_ta_active;
    191  1.1  christos ia_hash_t *ia_pd_active;
    192  1.1  christos 
    193  1.1  christos HASH_FUNCTIONS(iasubopt, struct in6_addr *, struct iasubopt, iasubopt_hash_t,
    194  1.1  christos 	       iasubopt_reference, iasubopt_dereference, do_string_hash)
    195  1.1  christos 
    196  1.1  christos struct ipv6_pool **pools;
    197  1.1  christos int num_pools;
    198  1.1  christos 
    199  1.1  christos /*
    200  1.1  christos  * Create a new IAADDR/PREFIX structure.
    201  1.1  christos  *
    202  1.1  christos  * - iasubopt must be a pointer to a (struct iasubopt *) pointer previously
    203  1.1  christos  *   initialized to NULL
    204  1.1  christos  */
    205  1.1  christos isc_result_t
    206  1.1  christos iasubopt_allocate(struct iasubopt **iasubopt, const char *file, int line) {
    207  1.1  christos 	struct iasubopt *tmp;
    208  1.1  christos 
    209  1.1  christos 	if (iasubopt == NULL) {
    210  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    211  1.1  christos 		return DHCP_R_INVALIDARG;
    212  1.1  christos 	}
    213  1.1  christos 	if (*iasubopt != NULL) {
    214  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    215  1.1  christos 		return DHCP_R_INVALIDARG;
    216  1.1  christos 	}
    217  1.1  christos 
    218  1.1  christos 	tmp = dmalloc(sizeof(*tmp), file, line);
    219  1.1  christos 	if (tmp == NULL) {
    220  1.1  christos 		return ISC_R_NOMEMORY;
    221  1.1  christos 	}
    222  1.1  christos 
    223  1.1  christos 	tmp->refcnt = 1;
    224  1.1  christos 	tmp->state = FTS_FREE;
    225  1.1  christos 	tmp->active_index = 0;
    226  1.1  christos 	tmp->inactive_index = 0;
    227  1.1  christos 	tmp->plen = 255;
    228  1.1  christos 
    229  1.1  christos 	*iasubopt = tmp;
    230  1.1  christos 	return ISC_R_SUCCESS;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /*
    234  1.1  christos  * Reference an IAADDR/PREFIX structure.
    235  1.1  christos  *
    236  1.1  christos  * - iasubopt must be a pointer to a (struct iasubopt *) pointer previously
    237  1.1  christos  *   initialized to NULL
    238  1.1  christos  */
    239  1.1  christos isc_result_t
    240  1.1  christos iasubopt_reference(struct iasubopt **iasubopt, struct iasubopt *src,
    241  1.1  christos 		 const char *file, int line) {
    242  1.1  christos 	if (iasubopt == NULL) {
    243  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    244  1.1  christos 		return DHCP_R_INVALIDARG;
    245  1.1  christos 	}
    246  1.1  christos 	if (*iasubopt != NULL) {
    247  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    248  1.1  christos 		return DHCP_R_INVALIDARG;
    249  1.1  christos 	}
    250  1.1  christos 	if (src == NULL) {
    251  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    252  1.1  christos 		return DHCP_R_INVALIDARG;
    253  1.1  christos 	}
    254  1.1  christos 	*iasubopt = src;
    255  1.1  christos 	src->refcnt++;
    256  1.1  christos 	return ISC_R_SUCCESS;
    257  1.1  christos }
    258  1.1  christos 
    259  1.1  christos 
    260  1.1  christos /*
    261  1.1  christos  * Dereference an IAADDR/PREFIX structure.
    262  1.1  christos  *
    263  1.1  christos  * If it is the last reference, then the memory for the
    264  1.1  christos  * structure is freed.
    265  1.1  christos  */
    266  1.1  christos isc_result_t
    267  1.1  christos iasubopt_dereference(struct iasubopt **iasubopt, const char *file, int line) {
    268  1.1  christos 	struct iasubopt *tmp;
    269  1.1  christos 
    270  1.1  christos 	if ((iasubopt == NULL) || (*iasubopt == NULL)) {
    271  1.1  christos 		log_error("%s(%d): NULL pointer", file, line);
    272  1.1  christos 		return DHCP_R_INVALIDARG;
    273  1.1  christos 	}
    274  1.1  christos 
    275  1.1  christos 	tmp = *iasubopt;
    276  1.1  christos 	*iasubopt = NULL;
    277  1.1  christos 
    278  1.1  christos 	tmp->refcnt--;
    279  1.1  christos 	if (tmp->refcnt < 0) {
    280  1.1  christos 		log_error("%s(%d): negative refcnt", file, line);
    281  1.1  christos 		tmp->refcnt = 0;
    282  1.1  christos 	}
    283  1.1  christos 	if (tmp->refcnt == 0) {
    284  1.1  christos 		if (tmp->ia != NULL) {
    285  1.1  christos 			ia_dereference(&(tmp->ia), file, line);
    286  1.1  christos 		}
    287  1.1  christos 		if (tmp->ipv6_pool != NULL) {
    288  1.1  christos 			ipv6_pool_dereference(&(tmp->ipv6_pool), file, line);
    289  1.1  christos 		}
    290  1.1  christos 		if (tmp->scope != NULL) {
    291  1.1  christos 			binding_scope_dereference(&tmp->scope, file, line);
    292  1.1  christos 		}
    293  1.1  christos 
    294  1.1  christos 		if (tmp->on_star.on_expiry != NULL) {
    295  1.1  christos 			executable_statement_dereference
    296  1.1  christos 				(&tmp->on_star.on_expiry, MDL);
    297  1.1  christos 		}
    298  1.1  christos 		if (tmp->on_star.on_commit != NULL) {
    299  1.1  christos 			executable_statement_dereference
    300  1.1  christos 				(&tmp->on_star.on_commit, MDL);
    301  1.1  christos 		}
    302  1.1  christos 		if (tmp->on_star.on_release != NULL) {
    303  1.1  christos 			executable_statement_dereference
    304  1.1  christos 				(&tmp->on_star.on_release, MDL);
    305  1.1  christos 		}
    306  1.1  christos 
    307  1.1  christos 		dfree(tmp, file, line);
    308  1.1  christos 	}
    309  1.1  christos 
    310  1.1  christos 	return ISC_R_SUCCESS;
    311  1.1  christos }
    312  1.1  christos 
    313  1.1  christos /*
    314  1.1  christos  * Make the key that we use for IA.
    315  1.1  christos  */
    316  1.1  christos isc_result_t
    317  1.1  christos ia_make_key(struct data_string *key, u_int32_t iaid,
    318  1.1  christos 	    const char *duid, unsigned int duid_len,
    319  1.1  christos 	    const char *file, int line) {
    320  1.1  christos 
    321  1.1  christos 	memset(key, 0, sizeof(*key));
    322  1.1  christos 	key->len = duid_len + sizeof(iaid);
    323  1.1  christos 	if (!buffer_allocate(&(key->buffer), key->len, file, line)) {
    324  1.1  christos 		return ISC_R_NOMEMORY;
    325  1.1  christos 	}
    326  1.1  christos 	key->data = key->buffer->data;
    327  1.1  christos 	memcpy((char *)key->data, &iaid, sizeof(iaid));
    328  1.1  christos 	memcpy((char *)key->data + sizeof(iaid), duid, duid_len);
    329  1.1  christos 
    330  1.1  christos 	return ISC_R_SUCCESS;
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos /*
    334  1.1  christos  * Create a new IA structure.
    335  1.1  christos  *
    336  1.1  christos  * - ia must be a pointer to a (struct ia_xx *) pointer previously
    337  1.1  christos  *   initialized to NULL
    338  1.1  christos  * - iaid and duid are values from the client
    339  1.1  christos  *
    340  1.1  christos  * XXXsk: we don't concern ourself with the byte order of the IAID,
    341  1.1  christos  *        which might be a problem if we transfer this structure
    342  1.1  christos  *        between machines of different byte order
    343  1.1  christos  */
    344  1.1  christos isc_result_t
    345  1.1  christos ia_allocate(struct ia_xx **ia, u_int32_t iaid,
    346  1.1  christos 	    const char *duid, unsigned int duid_len,
    347  1.1  christos 	    const char *file, int line) {
    348  1.1  christos 	struct ia_xx *tmp;
    349  1.1  christos 
    350  1.1  christos 	if (ia == NULL) {
    351  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    352  1.1  christos 		return DHCP_R_INVALIDARG;
    353  1.1  christos 	}
    354  1.1  christos 	if (*ia != NULL) {
    355  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    356  1.1  christos 		return DHCP_R_INVALIDARG;
    357  1.1  christos 	}
    358  1.1  christos 
    359  1.1  christos 	tmp = dmalloc(sizeof(*tmp), file, line);
    360  1.1  christos 	if (tmp == NULL) {
    361  1.1  christos 		return ISC_R_NOMEMORY;
    362  1.1  christos 	}
    363  1.1  christos 
    364  1.1  christos 	if (ia_make_key(&tmp->iaid_duid, iaid,
    365  1.1  christos 			duid, duid_len, file, line) != ISC_R_SUCCESS) {
    366  1.1  christos 		dfree(tmp, file, line);
    367  1.1  christos 		return ISC_R_NOMEMORY;
    368  1.1  christos 	}
    369  1.1  christos 
    370  1.1  christos 	tmp->refcnt = 1;
    371  1.1  christos 
    372  1.1  christos 	*ia = tmp;
    373  1.1  christos 	return ISC_R_SUCCESS;
    374  1.1  christos }
    375  1.1  christos 
    376  1.1  christos /*
    377  1.1  christos  * Reference an IA structure.
    378  1.1  christos  *
    379  1.1  christos  * - ia must be a pointer to a (struct ia_xx *) pointer previously
    380  1.1  christos  *   initialized to NULL
    381  1.1  christos  */
    382  1.1  christos isc_result_t
    383  1.1  christos ia_reference(struct ia_xx **ia, struct ia_xx *src,
    384  1.1  christos 	     const char *file, int line) {
    385  1.1  christos 	if (ia == NULL) {
    386  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    387  1.1  christos 		return DHCP_R_INVALIDARG;
    388  1.1  christos 	}
    389  1.1  christos 	if (*ia != NULL) {
    390  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    391  1.1  christos 		return DHCP_R_INVALIDARG;
    392  1.1  christos 	}
    393  1.1  christos 	if (src == NULL) {
    394  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    395  1.1  christos 		return DHCP_R_INVALIDARG;
    396  1.1  christos 	}
    397  1.1  christos 	*ia = src;
    398  1.1  christos 	src->refcnt++;
    399  1.1  christos 	return ISC_R_SUCCESS;
    400  1.1  christos }
    401  1.1  christos 
    402  1.1  christos /*
    403  1.1  christos  * Dereference an IA structure.
    404  1.1  christos  *
    405  1.1  christos  * If it is the last reference, then the memory for the
    406  1.1  christos  * structure is freed.
    407  1.1  christos  */
    408  1.1  christos isc_result_t
    409  1.1  christos ia_dereference(struct ia_xx **ia, const char *file, int line) {
    410  1.1  christos 	struct ia_xx *tmp;
    411  1.1  christos 	int i;
    412  1.1  christos 
    413  1.1  christos 	if ((ia == NULL) || (*ia == NULL)) {
    414  1.1  christos 		log_error("%s(%d): NULL pointer", file, line);
    415  1.1  christos 		return DHCP_R_INVALIDARG;
    416  1.1  christos 	}
    417  1.1  christos 
    418  1.1  christos 	tmp = *ia;
    419  1.1  christos 	*ia = NULL;
    420  1.1  christos 
    421  1.1  christos 	tmp->refcnt--;
    422  1.1  christos 	if (tmp->refcnt < 0) {
    423  1.1  christos 		log_error("%s(%d): negative refcnt", file, line);
    424  1.1  christos 		tmp->refcnt = 0;
    425  1.1  christos 	}
    426  1.1  christos 	if (tmp->refcnt == 0) {
    427  1.1  christos 		if (tmp->iasubopt != NULL) {
    428  1.1  christos 			for (i=0; i<tmp->num_iasubopt; i++) {
    429  1.1  christos 				iasubopt_dereference(&(tmp->iasubopt[i]),
    430  1.1  christos 						     file, line);
    431  1.1  christos 			}
    432  1.1  christos 			dfree(tmp->iasubopt, file, line);
    433  1.1  christos 		}
    434  1.1  christos 		data_string_forget(&(tmp->iaid_duid), file, line);
    435  1.1  christos 		dfree(tmp, file, line);
    436  1.1  christos 	}
    437  1.1  christos 	return ISC_R_SUCCESS;
    438  1.1  christos }
    439  1.1  christos 
    440  1.1  christos 
    441  1.1  christos /*
    442  1.1  christos  * Add an IAADDR/PREFIX entry to an IA structure.
    443  1.1  christos  */
    444  1.1  christos isc_result_t
    445  1.1  christos ia_add_iasubopt(struct ia_xx *ia, struct iasubopt *iasubopt,
    446  1.1  christos 		const char *file, int line) {
    447  1.1  christos 	int max;
    448  1.1  christos 	struct iasubopt **new;
    449  1.1  christos 
    450  1.1  christos 	/*
    451  1.1  christos 	 * Grow our array if we need to.
    452  1.1  christos 	 *
    453  1.1  christos 	 * Note: we pick 4 as the increment, as that seems a reasonable
    454  1.1  christos 	 *       guess as to how many addresses/prefixes we might expect
    455  1.1  christos 	 *       on an interface.
    456  1.1  christos 	 */
    457  1.1  christos 	if (ia->max_iasubopt <= ia->num_iasubopt) {
    458  1.1  christos 		max = ia->max_iasubopt + 4;
    459  1.1  christos 		new = dmalloc(max * sizeof(struct iasubopt *), file, line);
    460  1.1  christos 		if (new == NULL) {
    461  1.1  christos 			return ISC_R_NOMEMORY;
    462  1.1  christos 		}
    463  1.1  christos 		memcpy(new, ia->iasubopt,
    464  1.1  christos 		       ia->num_iasubopt * sizeof(struct iasubopt *));
    465  1.1  christos 		ia->iasubopt = new;
    466  1.1  christos 		ia->max_iasubopt = max;
    467  1.1  christos 	}
    468  1.1  christos 
    469  1.1  christos 	iasubopt_reference(&(ia->iasubopt[ia->num_iasubopt]), iasubopt,
    470  1.1  christos 			   file, line);
    471  1.1  christos 	ia->num_iasubopt++;
    472  1.1  christos 
    473  1.1  christos 	return ISC_R_SUCCESS;
    474  1.1  christos }
    475  1.1  christos 
    476  1.1  christos /*
    477  1.1  christos  * Remove an IAADDR/PREFIX entry to an IA structure.
    478  1.1  christos  *
    479  1.1  christos  * Note: if a suboption appears more than once, then only ONE will be removed.
    480  1.1  christos  */
    481  1.1  christos void
    482  1.1  christos ia_remove_iasubopt(struct ia_xx *ia, struct iasubopt *iasubopt,
    483  1.1  christos 		   const char *file, int line) {
    484  1.1  christos 	int i, j;
    485  1.1  christos         if (ia == NULL || iasubopt == NULL)
    486  1.1  christos             return;
    487  1.1  christos 
    488  1.1  christos 	for (i=0; i<ia->num_iasubopt; i++) {
    489  1.1  christos 		if (ia->iasubopt[i] == iasubopt) {
    490  1.1  christos 			/* remove this sub option */
    491  1.1  christos 			iasubopt_dereference(&(ia->iasubopt[i]), file, line);
    492  1.1  christos 			/* move remaining suboption pointers down one */
    493  1.1  christos 			for (j=i+1; j < ia->num_iasubopt; j++) {
    494  1.1  christos 				ia->iasubopt[j-1] = ia->iasubopt[j];
    495  1.1  christos 			}
    496  1.1  christos 			/* decrease our total count */
    497  1.1  christos 			/* remove the back-reference in the suboption itself */
    498  1.1  christos 			ia_dereference(&iasubopt->ia, file, line);
    499  1.1  christos 			ia->num_iasubopt--;
    500  1.1  christos 			return;
    501  1.1  christos 		}
    502  1.1  christos 	}
    503  1.1  christos 	log_error("%s(%d): IAADDR/PREFIX not in IA", file, line);
    504  1.1  christos }
    505  1.1  christos 
    506  1.1  christos /*
    507  1.1  christos  * Remove all addresses/prefixes from an IA.
    508  1.1  christos  */
    509  1.1  christos void
    510  1.1  christos ia_remove_all_lease(struct ia_xx *ia, const char *file, int line) {
    511  1.1  christos 	int i;
    512  1.1  christos 
    513  1.1  christos 	for (i=0; i<ia->num_iasubopt; i++) {
    514  1.1  christos 		ia_dereference(&(ia->iasubopt[i]->ia), file, line);
    515  1.1  christos 		iasubopt_dereference(&(ia->iasubopt[i]), file, line);
    516  1.1  christos 	}
    517  1.1  christos 	ia->num_iasubopt = 0;
    518  1.1  christos }
    519  1.1  christos 
    520  1.1  christos /*
    521  1.1  christos  * Compare two IA.
    522  1.1  christos  */
    523  1.1  christos isc_boolean_t
    524  1.1  christos ia_equal(const struct ia_xx *a, const struct ia_xx *b)
    525  1.1  christos {
    526  1.1  christos 	isc_boolean_t found;
    527  1.1  christos 	int i, j;
    528  1.1  christos 
    529  1.1  christos 	/*
    530  1.1  christos 	 * Handle cases where one or both of the inputs is NULL.
    531  1.1  christos 	 */
    532  1.1  christos 	if (a == NULL) {
    533  1.1  christos 		if (b == NULL) {
    534  1.1  christos 			return ISC_TRUE;
    535  1.1  christos 		} else {
    536  1.1  christos 			return ISC_FALSE;
    537  1.1  christos 		}
    538  1.1  christos 	}
    539  1.1  christos 
    540  1.1  christos 	/*
    541  1.1  christos 	 * Check the type is the same.
    542  1.1  christos 	 */
    543  1.1  christos 	if (a->ia_type != b->ia_type) {
    544  1.1  christos 		return ISC_FALSE;
    545  1.1  christos 	}
    546  1.1  christos 
    547  1.1  christos 	/*
    548  1.1  christos 	 * Check the DUID is the same.
    549  1.1  christos 	 */
    550  1.1  christos 	if (a->iaid_duid.len != b->iaid_duid.len) {
    551  1.1  christos 		return ISC_FALSE;
    552  1.1  christos 	}
    553  1.1  christos 	if (memcmp(a->iaid_duid.data,
    554  1.1  christos 		   b->iaid_duid.data, a->iaid_duid.len) != 0) {
    555  1.1  christos 		return ISC_FALSE;
    556  1.1  christos 	}
    557  1.1  christos 
    558  1.1  christos 	/*
    559  1.1  christos 	 * Make sure we have the same number of addresses/prefixes in each.
    560  1.1  christos 	 */
    561  1.1  christos 	if (a->num_iasubopt != b->num_iasubopt) {
    562  1.1  christos 		return ISC_FALSE;
    563  1.1  christos 	}
    564  1.1  christos 
    565  1.1  christos 	/*
    566  1.1  christos 	 * Check that each address/prefix is present in both.
    567  1.1  christos 	 */
    568  1.1  christos 	for (i=0; i<a->num_iasubopt; i++) {
    569  1.1  christos 		found = ISC_FALSE;
    570  1.1  christos 		for (j=0; j<a->num_iasubopt; j++) {
    571  1.1  christos 			if (a->iasubopt[i]->plen != b->iasubopt[i]->plen)
    572  1.1  christos 				continue;
    573  1.1  christos 			if (memcmp(&(a->iasubopt[i]->addr),
    574  1.1  christos 			           &(b->iasubopt[j]->addr),
    575  1.1  christos 				   sizeof(struct in6_addr)) == 0) {
    576  1.1  christos 				found = ISC_TRUE;
    577  1.1  christos 				break;
    578  1.1  christos 			}
    579  1.1  christos 		}
    580  1.1  christos 		if (!found) {
    581  1.1  christos 			return ISC_FALSE;
    582  1.1  christos 		}
    583  1.1  christos 	}
    584  1.1  christos 
    585  1.1  christos 	/*
    586  1.1  christos 	 * These are the same in every way we care about.
    587  1.1  christos 	 */
    588  1.1  christos 	return ISC_TRUE;
    589  1.1  christos }
    590  1.1  christos 
    591  1.1  christos /*
    592  1.1  christos  * Helper function for lease heaps.
    593  1.1  christos  * Makes the top of the heap the oldest lease.
    594  1.1  christos  */
    595  1.1  christos static isc_boolean_t
    596  1.1  christos lease_older(void *a, void *b) {
    597  1.1  christos 	struct iasubopt *la = (struct iasubopt *)a;
    598  1.1  christos 	struct iasubopt *lb = (struct iasubopt *)b;
    599  1.1  christos 
    600  1.1  christos 	if (la->hard_lifetime_end_time == lb->hard_lifetime_end_time) {
    601  1.1  christos 		return difftime(la->soft_lifetime_end_time,
    602  1.1  christos 				lb->soft_lifetime_end_time) < 0;
    603  1.1  christos 	} else {
    604  1.1  christos 		return difftime(la->hard_lifetime_end_time,
    605  1.1  christos 				lb->hard_lifetime_end_time) < 0;
    606  1.1  christos 	}
    607  1.1  christos }
    608  1.1  christos 
    609  1.1  christos /*
    610  1.1  christos  * Helper functions for lease address/prefix heaps.
    611  1.1  christos  * Callback when an address's position in the heap changes.
    612  1.1  christos  */
    613  1.1  christos static void
    614  1.1  christos active_changed(void *iasubopt, unsigned int new_heap_index) {
    615  1.1  christos 	((struct iasubopt *)iasubopt)->active_index = new_heap_index;
    616  1.1  christos }
    617  1.1  christos 
    618  1.1  christos static void
    619  1.1  christos inactive_changed(void *iasubopt, unsigned int new_heap_index) {
    620  1.1  christos 	((struct iasubopt *)iasubopt)->inactive_index = new_heap_index;
    621  1.1  christos }
    622  1.1  christos 
    623  1.1  christos /*!
    624  1.1  christos  *
    625  1.1  christos  * \brief Create a new IPv6 lease pool structure
    626  1.1  christos  *
    627  1.1  christos  * Allocate space for a new ipv6_pool structure and return a reference
    628  1.1  christos  * to it, includes setting the reference count to 1.
    629  1.1  christos  *
    630  1.1  christos  * \param     pool       = space for returning a referenced pointer to the pool.
    631  1.1  christos  *			   This must point to a space that has been initialzied
    632  1.1  christos  *			   to NULL by the caller.
    633  1.1  christos  * \param[in] type       = The type of the pool NA, TA or PD
    634  1.1  christos  * \param[in] start_addr = The first address in the range for the pool
    635  1.1  christos  * \param[in] bits       = The contiguous bits of the pool
    636  1.1  christos 
    637  1.1  christos  *
    638  1.1  christos  * \return
    639  1.1  christos  * ISC_R_SUCCESS     = The pool was successfully created, pool points to it.
    640  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pool has not been
    641  1.1  christos  *		       modified
    642  1.1  christos  * ISC_R_NOMEMORY    = The system wasn't able to allocate memory, pool has
    643  1.1  christos  *		       not been modified.
    644  1.1  christos  */
    645  1.1  christos isc_result_t
    646  1.1  christos ipv6_pool_allocate(struct ipv6_pool **pool, u_int16_t type,
    647  1.1  christos 		   const struct in6_addr *start_addr, int bits,
    648  1.1  christos 		   int units, const char *file, int line) {
    649  1.1  christos 	struct ipv6_pool *tmp;
    650  1.1  christos 
    651  1.1  christos 	if (pool == NULL) {
    652  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    653  1.1  christos 		return DHCP_R_INVALIDARG;
    654  1.1  christos 	}
    655  1.1  christos 	if (*pool != NULL) {
    656  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    657  1.1  christos 		return DHCP_R_INVALIDARG;
    658  1.1  christos 	}
    659  1.1  christos 
    660  1.1  christos 	tmp = dmalloc(sizeof(*tmp), file, line);
    661  1.1  christos 	if (tmp == NULL) {
    662  1.1  christos 		return ISC_R_NOMEMORY;
    663  1.1  christos 	}
    664  1.1  christos 
    665  1.1  christos 	tmp->refcnt = 1;
    666  1.1  christos 	tmp->pool_type = type;
    667  1.1  christos 	tmp->start_addr = *start_addr;
    668  1.1  christos 	tmp->bits = bits;
    669  1.1  christos 	tmp->units = units;
    670  1.1  christos 	if (!iasubopt_new_hash(&tmp->leases, DEFAULT_HASH_SIZE, file, line)) {
    671  1.1  christos 		dfree(tmp, file, line);
    672  1.1  christos 		return ISC_R_NOMEMORY;
    673  1.1  christos 	}
    674  1.1  christos 	if (isc_heap_create(dhcp_gbl_ctx.mctx, lease_older, active_changed,
    675  1.1  christos 			    0, &(tmp->active_timeouts)) != ISC_R_SUCCESS) {
    676  1.1  christos 		iasubopt_free_hash_table(&(tmp->leases), file, line);
    677  1.1  christos 		dfree(tmp, file, line);
    678  1.1  christos 		return ISC_R_NOMEMORY;
    679  1.1  christos 	}
    680  1.1  christos 	if (isc_heap_create(dhcp_gbl_ctx.mctx, lease_older, inactive_changed,
    681  1.1  christos 			    0, &(tmp->inactive_timeouts)) != ISC_R_SUCCESS) {
    682  1.1  christos 		isc_heap_destroy(&(tmp->active_timeouts));
    683  1.1  christos 		iasubopt_free_hash_table(&(tmp->leases), file, line);
    684  1.1  christos 		dfree(tmp, file, line);
    685  1.1  christos 		return ISC_R_NOMEMORY;
    686  1.1  christos 	}
    687  1.1  christos 
    688  1.1  christos 	*pool = tmp;
    689  1.1  christos 	return ISC_R_SUCCESS;
    690  1.1  christos }
    691  1.1  christos 
    692  1.1  christos /*!
    693  1.1  christos  *
    694  1.1  christos  * \brief reference an IPv6 pool structure.
    695  1.1  christos  *
    696  1.1  christos  * This function genreates a reference to an ipv6_pool structure
    697  1.1  christos  * and increments the reference count on the structure.
    698  1.1  christos  *
    699  1.1  christos  * \param[out] pool = space for returning a referenced pointer to the pool.
    700  1.1  christos  *		      This must point to a space that has been initialzied
    701  1.1  christos  *		      to NULL by the caller.
    702  1.1  christos  * \param[in]  src  = A pointer to the pool to reference.  This must not be
    703  1.1  christos  *		      NULL.
    704  1.1  christos  *
    705  1.1  christos  * \return
    706  1.1  christos  * ISC_R_SUCCESS     = The pool was successfully referenced, pool now points
    707  1.1  christos  *		       to src.
    708  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pool has not been
    709  1.1  christos  *		       modified.
    710  1.1  christos  */
    711  1.1  christos isc_result_t
    712  1.1  christos ipv6_pool_reference(struct ipv6_pool **pool, struct ipv6_pool *src,
    713  1.1  christos 		    const char *file, int line) {
    714  1.1  christos 	if (pool == NULL) {
    715  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    716  1.1  christos 		return DHCP_R_INVALIDARG;
    717  1.1  christos 	}
    718  1.1  christos 	if (*pool != NULL) {
    719  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
    720  1.1  christos 		return DHCP_R_INVALIDARG;
    721  1.1  christos 	}
    722  1.1  christos 	if (src == NULL) {
    723  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
    724  1.1  christos 		return DHCP_R_INVALIDARG;
    725  1.1  christos 	}
    726  1.1  christos 	*pool = src;
    727  1.1  christos 	src->refcnt++;
    728  1.1  christos 	return ISC_R_SUCCESS;
    729  1.1  christos }
    730  1.1  christos 
    731  1.1  christos /*
    732  1.1  christos  * Note: Each IAADDR/PREFIX in a pool is referenced by the pool. This is needed
    733  1.1  christos  * to prevent the lease from being garbage collected out from under the
    734  1.1  christos  * pool.
    735  1.1  christos  *
    736  1.1  christos  * The references are made from the hash and from the heap. The following
    737  1.1  christos  * helper functions dereference these when a pool is destroyed.
    738  1.1  christos  */
    739  1.1  christos 
    740  1.1  christos /*
    741  1.1  christos  * Helper function for pool cleanup.
    742  1.1  christos  * Dereference each of the hash entries in a pool.
    743  1.1  christos  */
    744  1.1  christos static isc_result_t
    745  1.1  christos dereference_hash_entry(const void *name, unsigned len, void *value) {
    746  1.1  christos 	struct iasubopt *iasubopt = (struct iasubopt *)value;
    747  1.1  christos 
    748  1.1  christos 	iasubopt_dereference(&iasubopt, MDL);
    749  1.1  christos 	return ISC_R_SUCCESS;
    750  1.1  christos }
    751  1.1  christos 
    752  1.1  christos /*
    753  1.1  christos  * Helper function for pool cleanup.
    754  1.1  christos  * Dereference each of the heap entries in a pool.
    755  1.1  christos  */
    756  1.1  christos static void
    757  1.1  christos dereference_heap_entry(void *value, void *dummy) {
    758  1.1  christos 	struct iasubopt *iasubopt = (struct iasubopt *)value;
    759  1.1  christos 
    760  1.1  christos 	iasubopt_dereference(&iasubopt, MDL);
    761  1.1  christos }
    762  1.1  christos 
    763  1.1  christos /*!
    764  1.1  christos  *
    765  1.1  christos  * \brief de-reference an IPv6 pool structure.
    766  1.1  christos  *
    767  1.1  christos  * This function decrements the reference count in an ipv6_pool structure.
    768  1.1  christos  * If this was the last reference then the memory for the structure is
    769  1.1  christos  * freed.
    770  1.1  christos  *
    771  1.1  christos  * \param[in] pool = A pointer to the pointer to the pool that should be
    772  1.1  christos  *		     de-referenced.  On success the pointer to the pool
    773  1.1  christos  *		     is cleared.  It must not be NULL and must not point
    774  1.1  christos  *		     to NULL.
    775  1.1  christos  *
    776  1.1  christos  * \return
    777  1.1  christos  * ISC_R_SUCCESS     = The pool was successfully de-referenced, pool now points
    778  1.1  christos  *		       to NULL
    779  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pool has not been
    780  1.1  christos  *		       modified.
    781  1.1  christos  */
    782  1.1  christos isc_result_t
    783  1.1  christos ipv6_pool_dereference(struct ipv6_pool **pool, const char *file, int line) {
    784  1.1  christos 	struct ipv6_pool *tmp;
    785  1.1  christos 
    786  1.1  christos 	if ((pool == NULL) || (*pool == NULL)) {
    787  1.1  christos 		log_error("%s(%d): NULL pointer", file, line);
    788  1.1  christos 		return DHCP_R_INVALIDARG;
    789  1.1  christos 	}
    790  1.1  christos 
    791  1.1  christos 	tmp = *pool;
    792  1.1  christos 	*pool = NULL;
    793  1.1  christos 
    794  1.1  christos 	tmp->refcnt--;
    795  1.1  christos 	if (tmp->refcnt < 0) {
    796  1.1  christos 		log_error("%s(%d): negative refcnt", file, line);
    797  1.1  christos 		tmp->refcnt = 0;
    798  1.1  christos 	}
    799  1.1  christos 	if (tmp->refcnt == 0) {
    800  1.1  christos 		iasubopt_hash_foreach(tmp->leases, dereference_hash_entry);
    801  1.1  christos 		iasubopt_free_hash_table(&(tmp->leases), file, line);
    802  1.1  christos 		isc_heap_foreach(tmp->active_timeouts,
    803  1.1  christos 				 dereference_heap_entry, NULL);
    804  1.1  christos 		isc_heap_destroy(&(tmp->active_timeouts));
    805  1.1  christos 		isc_heap_foreach(tmp->inactive_timeouts,
    806  1.1  christos 				 dereference_heap_entry, NULL);
    807  1.1  christos 		isc_heap_destroy(&(tmp->inactive_timeouts));
    808  1.1  christos 		dfree(tmp, file, line);
    809  1.1  christos 	}
    810  1.1  christos 
    811  1.1  christos 	return ISC_R_SUCCESS;
    812  1.1  christos }
    813  1.1  christos 
    814  1.1  christos /*
    815  1.1  christos  * Create an address by hashing the input, and using that for
    816  1.1  christos  * the non-network part.
    817  1.1  christos  */
    818  1.1  christos static void
    819  1.1  christos build_address6(struct in6_addr *addr,
    820  1.1  christos 	       const struct in6_addr *net_start_addr, int net_bits,
    821  1.1  christos 	       const struct data_string *input) {
    822  1.1  christos 	isc_md5_t ctx;
    823  1.1  christos 	int net_bytes;
    824  1.1  christos 	int i;
    825  1.1  christos 	char *str;
    826  1.1  christos 	const char *net_str;
    827  1.1  christos 
    828  1.1  christos 	/*
    829  1.1  christos 	 * Use MD5 to get a nice 128 bit hash of the input.
    830  1.1  christos 	 * Yes, we know MD5 isn't cryptographically sound.
    831  1.1  christos 	 * No, we don't care.
    832  1.1  christos 	 */
    833  1.1  christos 	isc_md5_init(&ctx);
    834  1.1  christos 	isc_md5_update(&ctx, input->data, input->len);
    835  1.1  christos 	isc_md5_final(&ctx, (unsigned char *)addr);
    836  1.1  christos 
    837  1.1  christos 	/*
    838  1.1  christos 	 * Copy the [0..128] network bits over.
    839  1.1  christos 	 */
    840  1.1  christos 	str = (char *)addr;
    841  1.1  christos 	net_str = (const char *)net_start_addr;
    842  1.1  christos 	net_bytes = net_bits / 8;
    843  1.1  christos 	for (i = 0; i < net_bytes; i++) {
    844  1.1  christos 		str[i] = net_str[i];
    845  1.1  christos 	}
    846  1.1  christos 	switch (net_bits % 8) {
    847  1.1  christos 		case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
    848  1.1  christos 		case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
    849  1.1  christos 		case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
    850  1.1  christos 		case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
    851  1.1  christos 		case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
    852  1.1  christos 		case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
    853  1.1  christos 		case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
    854  1.1  christos 	}
    855  1.1  christos 
    856  1.1  christos 	/*
    857  1.1  christos 	 * Set the universal/local bit ("u bit") to zero for /64s.  The
    858  1.1  christos 	 * individual/group bit ("g bit") is unchanged, because the g-bit
    859  1.1  christos 	 * has no meaning when the u-bit is cleared.
    860  1.1  christos 	 */
    861  1.1  christos 	if (net_bits == 64)
    862  1.1  christos 		str[8] &= ~0x02;
    863  1.1  christos }
    864  1.1  christos 
    865  1.1  christos #ifdef EUI_64
    866  1.1  christos int
    867  1.1  christos valid_eui_64_duid(const struct data_string* uid, int offset) {
    868  1.1  christos 	if (uid->len == (offset + EUI_64_ID_LEN)) {
    869  1.1  christos 		const unsigned char* duid = uid->data + offset;
    870  1.1  christos 		return (((duid[0] == 0x00 && duid[1] == 0x03)  &&
    871  1.1  christos 			(duid[2] == 0x00 && duid[3] == 0x1b)));
    872  1.1  christos 	}
    873  1.1  christos 
    874  1.1  christos     return(0);
    875  1.1  christos }
    876  1.1  christos 
    877  1.1  christos 
    878  1.1  christos /*
    879  1.1  christos  * Create an EUI-64 address
    880  1.1  christos  */
    881  1.1  christos static isc_result_t
    882  1.1  christos build_address6_eui_64(struct in6_addr *addr,
    883  1.1  christos 		      const struct in6_addr *net_start_addr, int net_bits,
    884  1.1  christos 		      const struct data_string *iaid_duid, int duid_beg) {
    885  1.1  christos 
    886  1.1  christos 	if (net_bits != 64) {
    887  1.1  christos 		log_error("build_address_eui_64: network is not 64 bits");
    888  1.1  christos 		return (ISC_R_FAILURE);
    889  1.1  christos 	}
    890  1.1  christos 
    891  1.1  christos 	if (valid_eui_64_duid(iaid_duid, duid_beg)) {
    892  1.1  christos 		const unsigned char *duid = iaid_duid->data + duid_beg;
    893  1.1  christos 
    894  1.1  christos 		/* copy network prefix to the high 64 bits */
    895  1.1  christos 		memcpy(addr->s6_addr, net_start_addr->s6_addr, 8);
    896  1.1  christos 
    897  1.1  christos 		/* copy Link-layer address to low 64 bits */
    898  1.1  christos 		memcpy(addr->s6_addr + 8, duid + 4, 8);
    899  1.1  christos 
    900  1.1  christos 		/* RFC-3315 Any address assigned by a server that is based
    901  1.1  christos 		 * on an EUI-64 identifier MUST include an interface identifier
    902  1.1  christos 		 * with the "u" (universal/local) and "g" (individual/group)
    903  1.1  christos 		 * bits of the interface identifier set appropriately, as
    904  1.1  christos 		 * indicated in section 2.5.1 of RFC 2373 [5]. */
    905  1.1  christos 		addr->s6_addr[8] |= 0x02;
    906  1.1  christos 		return (ISC_R_SUCCESS);
    907  1.1  christos 	}
    908  1.1  christos 
    909  1.1  christos 	log_error("build_address_eui_64: iaid_duid not a valid EUI-64: %s",
    910  1.1  christos 		  print_hex_1(iaid_duid->len, iaid_duid->data, 60));
    911  1.1  christos 	return (ISC_R_FAILURE);
    912  1.1  christos }
    913  1.1  christos 
    914  1.1  christos int
    915  1.1  christos valid_for_eui_64_pool(struct ipv6_pool* pool, struct data_string* uid,
    916  1.1  christos 		      int duid_beg, struct in6_addr* ia_addr) {
    917  1.1  christos         struct in6_addr test_addr;
    918  1.1  christos 	/* If it's not an EUI-64 pool bail */
    919  1.1  christos         if (!pool->ipv6_pond->use_eui_64) {
    920  1.1  christos                 return (0);
    921  1.1  christos         }
    922  1.1  christos 
    923  1.1  christos         if (!valid_eui_64_duid(uid, duid_beg)) {
    924  1.1  christos                 /* Dynamic lease in a now eui_64 pond, toss it*/
    925  1.1  christos                 return (0);
    926  1.1  christos         }
    927  1.1  christos 
    928  1.1  christos         /*  Call build_address6_eui_64() and compare it's result to
    929  1.1  christos 	 *  this lease and see if they match. */
    930  1.1  christos         memset (&test_addr, 0, sizeof(test_addr));
    931  1.1  christos         build_address6_eui_64(&test_addr, &pool->start_addr, pool->bits,
    932  1.1  christos                               uid, duid_beg);
    933  1.1  christos 
    934  1.1  christos         return (!memcmp(ia_addr, &test_addr, sizeof(test_addr)));
    935  1.1  christos }
    936  1.1  christos #endif
    937  1.1  christos 
    938  1.1  christos 
    939  1.1  christos /*
    940  1.1  christos  * Create a temporary address by a variant of RFC 4941 algo.
    941  1.1  christos  * Note: this should not be used for prefixes shorter than 64 bits.
    942  1.1  christos  */
    943  1.1  christos static void
    944  1.1  christos build_temporary6(struct in6_addr *addr,
    945  1.1  christos 		 const struct in6_addr *net_start_addr, int net_bits,
    946  1.1  christos 		 const struct data_string *input) {
    947  1.1  christos 	static u_int32_t history[2];
    948  1.1  christos 	static u_int32_t counter = 0;
    949  1.1  christos 	isc_md5_t ctx;
    950  1.1  christos 	unsigned char md[16];
    951  1.1  christos 
    952  1.1  christos 	/*
    953  1.1  christos 	 * First time/time to reseed.
    954  1.1  christos 	 * Please use a good pseudo-random generator here!
    955  1.1  christos 	 */
    956  1.1  christos 	if (counter == 0) {
    957  1.1  christos 		isc_random_get(&history[0]);
    958  1.1  christos 		isc_random_get(&history[1]);
    959  1.1  christos 	}
    960  1.1  christos 
    961  1.1  christos 	/*
    962  1.1  christos 	 * Use MD5 as recommended by RFC 4941.
    963  1.1  christos 	 */
    964  1.1  christos 	isc_md5_init(&ctx);
    965  1.1  christos 	isc_md5_update(&ctx, (unsigned char *)&history[0], 8UL);
    966  1.1  christos 	isc_md5_update(&ctx, input->data, input->len);
    967  1.1  christos 	isc_md5_final(&ctx, md);
    968  1.1  christos 
    969  1.1  christos 	/*
    970  1.1  christos 	 * Build the address.
    971  1.1  christos 	 */
    972  1.1  christos 	if (net_bits == 64) {
    973  1.1  christos 		memcpy(&addr->s6_addr[0], &net_start_addr->s6_addr[0], 8);
    974  1.1  christos 		memcpy(&addr->s6_addr[8], md, 8);
    975  1.1  christos 		addr->s6_addr[8] &= ~0x02;
    976  1.1  christos 	} else {
    977  1.1  christos 		int net_bytes;
    978  1.1  christos 		int i;
    979  1.1  christos 		char *str;
    980  1.1  christos 		const char *net_str;
    981  1.1  christos 
    982  1.1  christos 		/*
    983  1.1  christos 		 * Copy the [0..128] network bits over.
    984  1.1  christos 		 */
    985  1.1  christos 		str = (char *)addr;
    986  1.1  christos 		net_str = (const char *)net_start_addr;
    987  1.1  christos 		net_bytes = net_bits / 8;
    988  1.1  christos 		for (i = 0; i < net_bytes; i++) {
    989  1.1  christos 			str[i] = net_str[i];
    990  1.1  christos 		}
    991  1.1  christos 		memcpy(str + net_bytes, md, 16 - net_bytes);
    992  1.1  christos 		switch (net_bits % 8) {
    993  1.1  christos 		case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
    994  1.1  christos 		case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
    995  1.1  christos 		case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
    996  1.1  christos 		case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
    997  1.1  christos 		case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
    998  1.1  christos 		case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
    999  1.1  christos 		case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
   1000  1.1  christos 		}
   1001  1.1  christos 	}
   1002  1.1  christos 
   1003  1.1  christos 
   1004  1.1  christos 	/*
   1005  1.1  christos 	 * Save history for the next call.
   1006  1.1  christos 	 */
   1007  1.1  christos 	memcpy((unsigned char *)&history[0], md + 8, 8);
   1008  1.1  christos 	counter++;
   1009  1.1  christos }
   1010  1.1  christos 
   1011  1.1  christos /* Reserved Subnet Router Anycast ::0:0:0:0. */
   1012  1.1  christos static struct in6_addr rtany;
   1013  1.1  christos /* Reserved Subnet Anycasts ::fdff:ffff:ffff:ff80-::fdff:ffff:ffff:ffff. */
   1014  1.1  christos static struct in6_addr resany;
   1015  1.1  christos 
   1016  1.1  christos /*
   1017  1.1  christos  * Create a lease for the given address and client duid.
   1018  1.1  christos  *
   1019  1.1  christos  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
   1020  1.1  christos  *   initialized to NULL
   1021  1.1  christos  *
   1022  1.1  christos  * Right now we simply hash the DUID, and if we get a collision, we hash
   1023  1.1  christos  * again until we find a free address. We try this a fixed number of times,
   1024  1.1  christos  * to avoid getting stuck in a loop (this is important on small pools
   1025  1.1  christos  * where we can run out of space).
   1026  1.1  christos  *
   1027  1.1  christos  * We return the number of attempts that it took to find an available
   1028  1.1  christos  * lease. This tells callers when a pool is are filling up, as
   1029  1.1  christos  * well as an indication of how full the pool is; statistically the
   1030  1.1  christos  * more full a pool is the more attempts must be made before finding
   1031  1.1  christos  * a free lease. Realistically this will only happen in very full
   1032  1.1  christos  * pools.
   1033  1.1  christos  *
   1034  1.1  christos  * We probably want different algorithms depending on the network size, in
   1035  1.1  christos  * the long term.
   1036  1.1  christos  */
   1037  1.1  christos isc_result_t
   1038  1.1  christos create_lease6(struct ipv6_pool *pool, struct iasubopt **addr,
   1039  1.1  christos 	      unsigned int *attempts,
   1040  1.1  christos 	      const struct data_string *uid, time_t soft_lifetime_end_time) {
   1041  1.1  christos 	struct data_string ds;
   1042  1.1  christos 	struct in6_addr tmp;
   1043  1.1  christos 	struct iasubopt *test_iaaddr;
   1044  1.1  christos 	struct data_string new_ds;
   1045  1.1  christos 	struct iasubopt *iaaddr;
   1046  1.1  christos 	isc_result_t result;
   1047  1.1  christos 	isc_boolean_t reserved_iid;
   1048  1.1  christos 	static isc_boolean_t init_resiid = ISC_FALSE;
   1049  1.1  christos 
   1050  1.1  christos 	/*
   1051  1.1  christos 	 * Fill the reserved IIDs.
   1052  1.1  christos 	 */
   1053  1.1  christos 	if (!init_resiid) {
   1054  1.1  christos 		memset(&rtany, 0, 16);
   1055  1.1  christos 		memset(&resany, 0, 8);
   1056  1.1  christos 		resany.s6_addr[8] = 0xfd;
   1057  1.1  christos 		memset(&resany.s6_addr[9], 0xff, 6);
   1058  1.1  christos 		init_resiid = ISC_TRUE;
   1059  1.1  christos 	}
   1060  1.1  christos 
   1061  1.1  christos 	/*
   1062  1.1  christos 	 * Use the UID as our initial seed for the hash
   1063  1.1  christos 	 */
   1064  1.1  christos 	memset(&ds, 0, sizeof(ds));
   1065  1.1  christos 	data_string_copy(&ds, (struct data_string *)uid, MDL);
   1066  1.1  christos 
   1067  1.1  christos 	*attempts = 0;
   1068  1.1  christos 	for (;;) {
   1069  1.1  christos 		/*
   1070  1.1  christos 		 * Give up at some point.
   1071  1.1  christos 		 */
   1072  1.1  christos 		if (++(*attempts) > 100) {
   1073  1.1  christos 			data_string_forget(&ds, MDL);
   1074  1.1  christos 			return ISC_R_NORESOURCES;
   1075  1.1  christos 		}
   1076  1.1  christos 
   1077  1.1  christos 		/*
   1078  1.1  christos 		 * Build a resource.
   1079  1.1  christos 		 */
   1080  1.1  christos 		switch (pool->pool_type) {
   1081  1.1  christos 		case D6O_IA_NA:
   1082  1.1  christos 			/* address */
   1083  1.1  christos 			build_address6(&tmp, &pool->start_addr,
   1084  1.1  christos 				       pool->bits, &ds);
   1085  1.1  christos 			break;
   1086  1.1  christos 		case D6O_IA_TA:
   1087  1.1  christos 			/* temporary address */
   1088  1.1  christos 			build_temporary6(&tmp, &pool->start_addr,
   1089  1.1  christos 					 pool->bits, &ds);
   1090  1.1  christos 			break;
   1091  1.1  christos 		case D6O_IA_PD:
   1092  1.1  christos 			/* prefix */
   1093  1.1  christos 			log_error("create_lease6: prefix pool.");
   1094  1.1  christos 			return DHCP_R_INVALIDARG;
   1095  1.1  christos 		default:
   1096  1.1  christos 			log_error("create_lease6: untyped pool.");
   1097  1.1  christos 			return DHCP_R_INVALIDARG;
   1098  1.1  christos 		}
   1099  1.1  christos 
   1100  1.1  christos 		/*
   1101  1.1  christos 		 * Avoid reserved interface IDs. (cf. RFC 5453)
   1102  1.1  christos 		 */
   1103  1.1  christos 		reserved_iid = ISC_FALSE;
   1104  1.1  christos 		if (memcmp(&tmp.s6_addr[8], &rtany.s6_addr[8], 8) == 0) {
   1105  1.1  christos 			reserved_iid = ISC_TRUE;
   1106  1.1  christos 		}
   1107  1.1  christos 		if (!reserved_iid &&
   1108  1.1  christos 		    (memcmp(&tmp.s6_addr[8], &resany.s6_addr[8], 7) == 0) &&
   1109  1.1  christos 		    ((tmp.s6_addr[15] & 0x80) == 0x80)) {
   1110  1.1  christos 			reserved_iid = ISC_TRUE;
   1111  1.1  christos 		}
   1112  1.1  christos 
   1113  1.1  christos 		/*
   1114  1.1  christos 		 * If this address is not in use, we're happy with it
   1115  1.1  christos 		 */
   1116  1.1  christos 		test_iaaddr = NULL;
   1117  1.1  christos 		if (!reserved_iid &&
   1118  1.1  christos 		    (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
   1119  1.1  christos 					  &tmp, sizeof(tmp), MDL) == 0)) {
   1120  1.1  christos 			break;
   1121  1.1  christos 		}
   1122  1.1  christos 		if (test_iaaddr != NULL)
   1123  1.1  christos 			iasubopt_dereference(&test_iaaddr, MDL);
   1124  1.1  christos 
   1125  1.1  christos 		/*
   1126  1.1  christos 		 * Otherwise, we create a new input, adding the address
   1127  1.1  christos 		 */
   1128  1.1  christos 		memset(&new_ds, 0, sizeof(new_ds));
   1129  1.1  christos 		new_ds.len = ds.len + sizeof(tmp);
   1130  1.1  christos 		if (!buffer_allocate(&new_ds.buffer, new_ds.len, MDL)) {
   1131  1.1  christos 			data_string_forget(&ds, MDL);
   1132  1.1  christos 			return ISC_R_NOMEMORY;
   1133  1.1  christos 		}
   1134  1.1  christos 		new_ds.data = new_ds.buffer->data;
   1135  1.1  christos 		memcpy(new_ds.buffer->data, ds.data, ds.len);
   1136  1.1  christos 		memcpy(new_ds.buffer->data + ds.len, &tmp, sizeof(tmp));
   1137  1.1  christos 		data_string_forget(&ds, MDL);
   1138  1.1  christos 		data_string_copy(&ds, &new_ds, MDL);
   1139  1.1  christos 		data_string_forget(&new_ds, MDL);
   1140  1.1  christos 	}
   1141  1.1  christos 
   1142  1.1  christos 	data_string_forget(&ds, MDL);
   1143  1.1  christos 
   1144  1.1  christos 	/*
   1145  1.1  christos 	 * We're happy with the address, create an IAADDR
   1146  1.1  christos 	 * to hold it.
   1147  1.1  christos 	 */
   1148  1.1  christos 	iaaddr = NULL;
   1149  1.1  christos 	result = iasubopt_allocate(&iaaddr, MDL);
   1150  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1151  1.1  christos 		return result;
   1152  1.1  christos 	}
   1153  1.1  christos 	iaaddr->plen = 0;
   1154  1.1  christos 	memcpy(&iaaddr->addr, &tmp, sizeof(iaaddr->addr));
   1155  1.1  christos 
   1156  1.1  christos 	/*
   1157  1.1  christos 	 * Add the lease to the pool (note state is free, not active?!).
   1158  1.1  christos 	 */
   1159  1.1  christos 	result = add_lease6(pool, iaaddr, soft_lifetime_end_time);
   1160  1.1  christos 	if (result == ISC_R_SUCCESS) {
   1161  1.1  christos 		iasubopt_reference(addr, iaaddr, MDL);
   1162  1.1  christos 	}
   1163  1.1  christos 	iasubopt_dereference(&iaaddr, MDL);
   1164  1.1  christos 	return result;
   1165  1.1  christos }
   1166  1.1  christos 
   1167  1.1  christos #ifdef EUI_64
   1168  1.1  christos /*!
   1169  1.1  christos  * \brief Assign an EUI-64 address from a pool for a given iaid-duid
   1170  1.1  christos  *
   1171  1.1  christos  *  \param pool - pool from which the address is assigned
   1172  1.1  christos  *  \param iaddr - pointer to the iasubopt to contain the assigned address is
   1173  1.1  christos  *  \param uid - data_string containing the iaid-duid tuple
   1174  1.1  christos  *  \param soft_lifetime_end_time - lifetime of the lease for a solicit?
   1175  1.1  christos  *
   1176  1.1  christos  *  \return status indicating success or nature of the failure
   1177  1.1  christos */
   1178  1.1  christos isc_result_t
   1179  1.1  christos create_lease6_eui_64(struct ipv6_pool *pool, struct iasubopt **addr,
   1180  1.1  christos 	      const struct data_string *uid,
   1181  1.1  christos 	      time_t soft_lifetime_end_time) {
   1182  1.1  christos 	struct in6_addr tmp;
   1183  1.1  christos 	struct iasubopt *test_iaaddr;
   1184  1.1  christos 	struct iasubopt *iaaddr;
   1185  1.1  christos 	isc_result_t result;
   1186  1.1  christos 	static isc_boolean_t init_resiid = ISC_FALSE;
   1187  1.1  christos 
   1188  1.1  christos 	/*  Fill the reserved IIDs.  */
   1189  1.1  christos 	if (!init_resiid) {
   1190  1.1  christos 		memset(&rtany, 0, 16);
   1191  1.1  christos 		memset(&resany, 0, 8);
   1192  1.1  christos 		resany.s6_addr[8] = 0xfd;
   1193  1.1  christos 		memset(&resany.s6_addr[9], 0xff, 6);
   1194  1.1  christos 		init_resiid = ISC_TRUE;
   1195  1.1  christos 	}
   1196  1.1  christos 
   1197  1.1  christos 	/* Pool must be IA_NA */
   1198  1.1  christos 	if (pool->pool_type != D6O_IA_NA) {
   1199  1.1  christos 		log_error("create_lease6_eui_64: pool type is not IA_NA.");
   1200  1.1  christos 		return (DHCP_R_INVALIDARG);
   1201  1.1  christos 	}
   1202  1.1  christos 
   1203  1.1  christos 	/* Attempt to build the address */
   1204  1.1  christos 	if (build_address6_eui_64 (&tmp, &pool->start_addr, pool->bits,
   1205  1.1  christos 				   uid, IAID_LEN) != ISC_R_SUCCESS) {
   1206  1.1  christos 		log_error("create_lease6_eui_64: build_address6_eui_64 failed");
   1207  1.1  christos 		return (ISC_R_FAILURE);
   1208  1.1  christos 	}
   1209  1.1  christos 
   1210  1.1  christos 	/* Avoid reserved interface IDs. (cf. RFC 5453) */
   1211  1.1  christos 	if ((memcmp(&tmp.s6_addr[8], &rtany.s6_addr[8], 8) == 0)  ||
   1212  1.1  christos 	    ((memcmp(&tmp.s6_addr[8], &resany.s6_addr[8], 7) == 0) &&
   1213  1.1  christos 	    ((tmp.s6_addr[15] & 0x80) == 0x80))) {
   1214  1.1  christos 		log_error("create_lease6_eui_64: "
   1215  1.1  christos 			  "address conflicts with reserved IID");
   1216  1.1  christos 		return (ISC_R_FAILURE);
   1217  1.1  christos 	}
   1218  1.1  christos 
   1219  1.1  christos 	/* If this address is not in use, we're happy with it */
   1220  1.1  christos 	test_iaaddr = NULL;
   1221  1.1  christos 	if (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
   1222  1.1  christos 				  &tmp, sizeof(tmp), MDL) != 0) {
   1223  1.1  christos 
   1224  1.1  christos 		/* See if it's ours. Static leases won't have an ia */
   1225  1.1  christos 		int ours = 0;
   1226  1.1  christos 		if (!test_iaaddr->ia) {
   1227  1.1  christos 			log_error("create_lease6_eui_64: "
   1228  1.1  christos 				  "address  %s is assigned to static lease",
   1229  1.1  christos 				  pin6_addr(&test_iaaddr->addr));
   1230  1.1  christos 		} else {
   1231  1.1  christos 			/* Not sure if this can actually happen */
   1232  1.1  christos 			struct data_string* found = &test_iaaddr->ia->iaid_duid;
   1233  1.1  christos 			ours = ((found->len == uid->len) &&
   1234  1.1  christos 				(!memcmp(found->data, uid->data, uid->len)));
   1235  1.1  christos 			log_error("create_lease6_eui_64: "
   1236  1.1  christos 				  "address  %s belongs to %s",
   1237  1.1  christos 				  pin6_addr(&test_iaaddr->addr),
   1238  1.1  christos 				  print_hex_1(found->len, found->data, 60));
   1239  1.1  christos 		}
   1240  1.1  christos 
   1241  1.1  christos 		iasubopt_dereference(&test_iaaddr, MDL);
   1242  1.1  christos 		if (!ours) {
   1243  1.1  christos 			/* Cant' use it */
   1244  1.1  christos 			return (ISC_R_FAILURE);
   1245  1.1  christos 		}
   1246  1.1  christos 	}
   1247  1.1  christos 
   1248  1.1  christos 	/* We're happy with the address, create an IAADDR to hold it. */
   1249  1.1  christos 	iaaddr = NULL;
   1250  1.1  christos 	result = iasubopt_allocate(&iaaddr, MDL);
   1251  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1252  1.1  christos 		log_error("create_lease6_eui_64: could not allocate iasubop");
   1253  1.1  christos 		return result;
   1254  1.1  christos 	}
   1255  1.1  christos 	iaaddr->plen = 0;
   1256  1.1  christos 	memcpy(&iaaddr->addr, &tmp, sizeof(iaaddr->addr));
   1257  1.1  christos 
   1258  1.1  christos 	/* Add the lease to the pool and the reply */
   1259  1.1  christos 	result = add_lease6(pool, iaaddr, soft_lifetime_end_time);
   1260  1.1  christos 	if (result == ISC_R_SUCCESS) {
   1261  1.1  christos 		iasubopt_reference(addr, iaaddr, MDL);
   1262  1.1  christos 	}
   1263  1.1  christos 
   1264  1.1  christos 	iasubopt_dereference(&iaaddr, MDL);
   1265  1.1  christos 	return result;
   1266  1.1  christos }
   1267  1.1  christos #endif
   1268  1.1  christos 
   1269  1.1  christos /*!
   1270  1.1  christos  *
   1271  1.1  christos  * \brief Cleans up leases when reading from a lease file
   1272  1.1  christos  *
   1273  1.1  christos  * This function is only expected to be run when reading leases in from a file.
   1274  1.1  christos  * It checks to see if a lease already exists for the new leases's address.
   1275  1.1  christos  * We don't add expired leases to the structures when reading a lease file
   1276  1.1  christos  * which limits what can happen.  We have two variables the owners of the leases
   1277  1.1  christos  * being the same or different and the new lease being active or non-active:
   1278  1.1  christos  * Owners active
   1279  1.1  christos  * same   no     remove old lease and its connections
   1280  1.1  christos  * same   yes    nothing to do, other code will update the structures.
   1281  1.1  christos  * diff   no     nothing to do
   1282  1.1  christos  * diff   yes    this combination shouldn't happen, we should only have a
   1283  1.1  christos  *               single active lease per address at a time and that lease
   1284  1.1  christos  *               should move to non-active before any other lease can
   1285  1.1  christos  *               become active for that address.
   1286  1.1  christos  *               Currently we delete the previous lease and pass an error
   1287  1.1  christos  *               to the caller who should log an error.
   1288  1.1  christos  *
   1289  1.1  christos  * When we remove a lease we remove it from the hash table and active heap
   1290  1.1  christos  * (remember only active leases are in the structures at this time) for the
   1291  1.1  christos  * pool, and from the IA's array.  If, after we've removed the pointer from
   1292  1.1  christos  * IA's array to the lease, the IA has no more pointers we remove it from
   1293  1.1  christos  * the appropriate hash table as well.
   1294  1.1  christos  *
   1295  1.1  christos  * \param[in] ia_table = the hash table for the IA
   1296  1.1  christos  * \param[in] pool     = the pool to update
   1297  1.1  christos  * \param[in] lease    = the new lease we want to add
   1298  1.1  christos  * \param[in] ia       = the new ia we are building
   1299  1.1  christos  *
   1300  1.1  christos  * \return
   1301  1.1  christos  * ISC_R_SUCCESS = the incoming lease and any previous lease were in
   1302  1.1  christos  *                 an expected state - one of the first 3 options above.
   1303  1.1  christos  *                 If necessary the old lease was removed.
   1304  1.1  christos  * ISC_R_FAILURE = there is already an active lease for the address in
   1305  1.1  christos  *                 the incoming lease.  This shouldn't happen if it does
   1306  1.1  christos  *                 flag an error for the caller to log.
   1307  1.1  christos  */
   1308  1.1  christos 
   1309  1.1  christos isc_result_t
   1310  1.1  christos cleanup_lease6(ia_hash_t *ia_table,
   1311  1.1  christos 	       struct ipv6_pool *pool,
   1312  1.1  christos 	       struct iasubopt *lease,
   1313  1.1  christos 	       struct ia_xx *ia) {
   1314  1.1  christos 
   1315  1.1  christos 	struct iasubopt *test_iasubopt, *tmp_iasubopt;
   1316  1.1  christos 	struct ia_xx *old_ia;
   1317  1.1  christos 	isc_result_t status = ISC_R_SUCCESS;
   1318  1.1  christos 
   1319  1.1  christos 	test_iasubopt = NULL;
   1320  1.1  christos 	old_ia = NULL;
   1321  1.1  christos 
   1322  1.1  christos 	/*
   1323  1.1  christos 	 * Look up the address - if we don't find a lease
   1324  1.1  christos 	 * we don't need to do anything.
   1325  1.1  christos 	 */
   1326  1.1  christos 	if (iasubopt_hash_lookup(&test_iasubopt, pool->leases,
   1327  1.1  christos 				 &lease->addr, sizeof(lease->addr),
   1328  1.1  christos 				 MDL) == 0) {
   1329  1.1  christos 		return (ISC_R_SUCCESS);
   1330  1.1  christos 	}
   1331  1.1  christos 
   1332  1.1  christos 	if (test_iasubopt->ia == NULL) {
   1333  1.1  christos 		/* no old ia, no work to do */
   1334  1.1  christos 		iasubopt_dereference(&test_iasubopt, MDL);
   1335  1.1  christos 		return (status);
   1336  1.1  christos 	}
   1337  1.1  christos 
   1338  1.1  christos 	ia_reference(&old_ia, test_iasubopt->ia, MDL);
   1339  1.1  christos 
   1340  1.1  christos 	if ((old_ia->iaid_duid.len == ia->iaid_duid.len) &&
   1341  1.1  christos 	    (memcmp((unsigned char *)ia->iaid_duid.data,
   1342  1.1  christos 		    (unsigned char *)old_ia->iaid_duid.data,
   1343  1.1  christos 		    ia->iaid_duid.len) == 0)) {
   1344  1.1  christos 		/* same IA */
   1345  1.1  christos 		if ((lease->state == FTS_ACTIVE) ||
   1346  1.1  christos 		    (lease->state == FTS_ABANDONED)) {
   1347  1.1  christos 			/* still active, no need to delete */
   1348  1.1  christos 			goto cleanup;
   1349  1.1  christos 		}
   1350  1.1  christos 	} else {
   1351  1.1  christos 		/* different IA */
   1352  1.1  christos 		if ((lease->state != FTS_ACTIVE) &&
   1353  1.1  christos 		    (lease->state != FTS_ABANDONED)) {
   1354  1.1  christos 			/* new lease isn't active, no work */
   1355  1.1  christos 			goto cleanup;
   1356  1.1  christos 		}
   1357  1.1  christos 
   1358  1.1  christos 		/*
   1359  1.1  christos 		 * We appear to have two active leases, this shouldn't happen.
   1360  1.1  christos 		 * Before a second lease can be set to active the first lease
   1361  1.1  christos 		 * should be set to inactive (released, expired etc). For now
   1362  1.1  christos 		 * delete the previous lease and indicate a failure to the
   1363  1.1  christos 		 * caller so it can generate a warning.
   1364  1.1  christos 		 * In the future we may try and determine which is the better
   1365  1.1  christos 		 * lease to keep.
   1366  1.1  christos 		 */
   1367  1.1  christos 
   1368  1.1  christos 		status = ISC_R_FAILURE;
   1369  1.1  christos 	}
   1370  1.1  christos 
   1371  1.1  christos 	/*
   1372  1.1  christos 	 * Remove the old lease from the active heap and from the hash table
   1373  1.1  christos 	 * then remove the lease from the IA and clean up the IA if necessary.
   1374  1.1  christos 	 */
   1375  1.1  christos 	isc_heap_delete(pool->active_timeouts, test_iasubopt->active_index);
   1376  1.1  christos 	pool->num_active--;
   1377  1.1  christos 	if (pool->ipv6_pond)
   1378  1.1  christos 		pool->ipv6_pond->num_active--;
   1379  1.1  christos 
   1380  1.1  christos 	if (lease->state == FTS_ABANDONED) {
   1381  1.1  christos 		pool->num_abandoned--;
   1382  1.1  christos 		if (pool->ipv6_pond)
   1383  1.1  christos 			pool->ipv6_pond->num_abandoned--;
   1384  1.1  christos 	}
   1385  1.1  christos 
   1386  1.1  christos 	iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
   1387  1.1  christos 			     sizeof(test_iasubopt->addr), MDL);
   1388  1.1  christos 	ia_remove_iasubopt(old_ia, test_iasubopt, MDL);
   1389  1.1  christos 	if (old_ia->num_iasubopt <= 0) {
   1390  1.1  christos 		ia_hash_delete(ia_table,
   1391  1.1  christos 			       (unsigned char *)old_ia->iaid_duid.data,
   1392  1.1  christos 			       old_ia->iaid_duid.len, MDL);
   1393  1.1  christos 	}
   1394  1.1  christos 
   1395  1.1  christos 	/*
   1396  1.1  christos 	 * We derefenrece the subopt here as we've just removed it from
   1397  1.1  christos 	 * the hash table in the pool.  We need to make a copy as we
   1398  1.1  christos 	 * need to derefernece it again later.
   1399  1.1  christos 	 */
   1400  1.1  christos 	tmp_iasubopt = test_iasubopt;
   1401  1.1  christos 	iasubopt_dereference(&tmp_iasubopt, MDL);
   1402  1.1  christos 
   1403  1.1  christos       cleanup:
   1404  1.1  christos 	ia_dereference(&old_ia, MDL);
   1405  1.1  christos 
   1406  1.1  christos 	/*
   1407  1.1  christos 	 * Clean up the reference, this is in addition to the deference
   1408  1.1  christos 	 * above after removing the entry from the hash table
   1409  1.1  christos 	 */
   1410  1.1  christos 	iasubopt_dereference(&test_iasubopt, MDL);
   1411  1.1  christos 
   1412  1.1  christos 	return (status);
   1413  1.1  christos }
   1414  1.1  christos 
   1415  1.1  christos /*
   1416  1.1  christos  * Put a lease in the pool directly. This is intended to be used when
   1417  1.1  christos  * loading leases from the file.
   1418  1.1  christos  */
   1419  1.1  christos isc_result_t
   1420  1.1  christos add_lease6(struct ipv6_pool *pool, struct iasubopt *lease,
   1421  1.1  christos 	   time_t valid_lifetime_end_time) {
   1422  1.1  christos 	isc_result_t insert_result;
   1423  1.1  christos 	struct iasubopt *test_iasubopt;
   1424  1.1  christos 	struct iasubopt *tmp_iasubopt;
   1425  1.1  christos 
   1426  1.1  christos 	/* If a state was not assigned by the caller, assume active. */
   1427  1.1  christos 	if (lease->state == 0)
   1428  1.1  christos 		lease->state = FTS_ACTIVE;
   1429  1.1  christos 
   1430  1.1  christos 	ipv6_pool_reference(&lease->ipv6_pool, pool, MDL);
   1431  1.1  christos 
   1432  1.1  christos 	/*
   1433  1.1  christos 	 * If this IAADDR/PREFIX is already in our structures, remove the
   1434  1.1  christos 	 * old one.
   1435  1.1  christos 	 */
   1436  1.1  christos 	test_iasubopt = NULL;
   1437  1.1  christos 	if (iasubopt_hash_lookup(&test_iasubopt, pool->leases,
   1438  1.1  christos 				 &lease->addr, sizeof(lease->addr), MDL)) {
   1439  1.1  christos 		/* XXX: we should probably ask the lease what heap it is on
   1440  1.1  christos 		 * (as a consistency check).
   1441  1.1  christos 		 * XXX: we should probably have one function to "put this lease
   1442  1.1  christos 		 * on its heap" rather than doing these if's everywhere.  If
   1443  1.1  christos 		 * you add more states to this list, don't.
   1444  1.1  christos 		 */
   1445  1.1  christos 		if ((test_iasubopt->state == FTS_ACTIVE) ||
   1446  1.1  christos 		    (test_iasubopt->state == FTS_ABANDONED)) {
   1447  1.1  christos 			isc_heap_delete(pool->active_timeouts,
   1448  1.1  christos 					test_iasubopt->active_index);
   1449  1.1  christos 			pool->num_active--;
   1450  1.1  christos 			if (pool->ipv6_pond)
   1451  1.1  christos 				pool->ipv6_pond->num_active--;
   1452  1.1  christos 
   1453  1.1  christos 			if (test_iasubopt->state == FTS_ABANDONED) {
   1454  1.1  christos 				pool->num_abandoned--;
   1455  1.1  christos 				if (pool->ipv6_pond)
   1456  1.1  christos 					pool->ipv6_pond->num_abandoned--;
   1457  1.1  christos 			}
   1458  1.1  christos 		} else {
   1459  1.1  christos 			isc_heap_delete(pool->inactive_timeouts,
   1460  1.1  christos 					test_iasubopt->inactive_index);
   1461  1.1  christos 			pool->num_inactive--;
   1462  1.1  christos 		}
   1463  1.1  christos 
   1464  1.1  christos 		iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
   1465  1.1  christos 				     sizeof(test_iasubopt->addr), MDL);
   1466  1.1  christos 
   1467  1.1  christos 		/*
   1468  1.1  christos 		 * We're going to do a bit of evil trickery here.
   1469  1.1  christos 		 *
   1470  1.1  christos 		 * We need to dereference the entry once to remove our
   1471  1.1  christos 		 * current reference (in test_iasubopt), and then one
   1472  1.1  christos 		 * more time to remove the reference left when the
   1473  1.1  christos 		 * address was added to the pool before.
   1474  1.1  christos 		 */
   1475  1.1  christos 		tmp_iasubopt = test_iasubopt;
   1476  1.1  christos 		iasubopt_dereference(&test_iasubopt, MDL);
   1477  1.1  christos 		iasubopt_dereference(&tmp_iasubopt, MDL);
   1478  1.1  christos 	}
   1479  1.1  christos 
   1480  1.1  christos 	/*
   1481  1.1  christos 	 * Add IAADDR/PREFIX to our structures.
   1482  1.1  christos 	 */
   1483  1.1  christos 	tmp_iasubopt = NULL;
   1484  1.1  christos 	iasubopt_reference(&tmp_iasubopt, lease, MDL);
   1485  1.1  christos 	if ((tmp_iasubopt->state == FTS_ACTIVE) ||
   1486  1.1  christos 	    (tmp_iasubopt->state == FTS_ABANDONED)) {
   1487  1.1  christos 		tmp_iasubopt->hard_lifetime_end_time = valid_lifetime_end_time;
   1488  1.1  christos 		iasubopt_hash_add(pool->leases, &tmp_iasubopt->addr,
   1489  1.1  christos 				  sizeof(tmp_iasubopt->addr), lease, MDL);
   1490  1.1  christos 		insert_result = isc_heap_insert(pool->active_timeouts,
   1491  1.1  christos 						tmp_iasubopt);
   1492  1.1  christos 		if (insert_result == ISC_R_SUCCESS) {
   1493  1.1  christos 			pool->num_active++;
   1494  1.1  christos 			if (pool->ipv6_pond)
   1495  1.1  christos 				pool->ipv6_pond->num_active++;
   1496  1.1  christos 
   1497  1.1  christos 			if (tmp_iasubopt->state == FTS_ABANDONED) {
   1498  1.1  christos 				pool->num_abandoned++;
   1499  1.1  christos 				if (pool->ipv6_pond)
   1500  1.1  christos 					pool->ipv6_pond->num_abandoned++;
   1501  1.1  christos 			}
   1502  1.1  christos 		}
   1503  1.1  christos 
   1504  1.1  christos 	} else {
   1505  1.1  christos 		tmp_iasubopt->soft_lifetime_end_time = valid_lifetime_end_time;
   1506  1.1  christos 		insert_result = isc_heap_insert(pool->inactive_timeouts,
   1507  1.1  christos 						tmp_iasubopt);
   1508  1.1  christos 		if (insert_result == ISC_R_SUCCESS)
   1509  1.1  christos 			pool->num_inactive++;
   1510  1.1  christos 	}
   1511  1.1  christos 	if (insert_result != ISC_R_SUCCESS) {
   1512  1.1  christos 		iasubopt_hash_delete(pool->leases, &lease->addr,
   1513  1.1  christos 				     sizeof(lease->addr), MDL);
   1514  1.1  christos 		iasubopt_dereference(&tmp_iasubopt, MDL);
   1515  1.1  christos 		return insert_result;
   1516  1.1  christos 	}
   1517  1.1  christos 
   1518  1.1  christos 	/*
   1519  1.1  christos 	 * Note: we intentionally leave tmp_iasubopt referenced; there
   1520  1.1  christos 	 * is a reference in the heap/hash, after all.
   1521  1.1  christos 	 */
   1522  1.1  christos 
   1523  1.1  christos 	return ISC_R_SUCCESS;
   1524  1.1  christos }
   1525  1.1  christos 
   1526  1.1  christos /*
   1527  1.1  christos  * Determine if an address is present in a pool or not.
   1528  1.1  christos  */
   1529  1.1  christos isc_boolean_t
   1530  1.1  christos lease6_exists(const struct ipv6_pool *pool, const struct in6_addr *addr) {
   1531  1.1  christos 	struct iasubopt *test_iaaddr;
   1532  1.1  christos 
   1533  1.1  christos 	test_iaaddr = NULL;
   1534  1.1  christos 	if (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
   1535  1.1  christos 				 (void *)addr, sizeof(*addr), MDL)) {
   1536  1.1  christos 		iasubopt_dereference(&test_iaaddr, MDL);
   1537  1.1  christos 		return ISC_TRUE;
   1538  1.1  christos 	} else {
   1539  1.1  christos 		return ISC_FALSE;
   1540  1.1  christos 	}
   1541  1.1  christos }
   1542  1.1  christos 
   1543  1.1  christos /*!
   1544  1.1  christos  *
   1545  1.1  christos  * \brief Check if address is available to a lease
   1546  1.1  christos  *
   1547  1.1  christos  * Determine if the address in the lease is available to that
   1548  1.1  christos  * lease.  Either the address isn't in use or it is in use
   1549  1.1  christos  * but by that lease.
   1550  1.1  christos  *
   1551  1.1  christos  * \param[in] lease = lease to check
   1552  1.1  christos  *
   1553  1.1  christos  * \return
   1554  1.1  christos  * ISC_TRUE  = The lease is allowed to use that address
   1555  1.1  christos  * ISC_FALSE = The lease isn't allowed to use that address
   1556  1.1  christos  */
   1557  1.1  christos isc_boolean_t
   1558  1.1  christos lease6_usable(struct iasubopt *lease) {
   1559  1.1  christos 	struct iasubopt *test_iaaddr;
   1560  1.1  christos 	isc_boolean_t status = ISC_TRUE;
   1561  1.1  christos 
   1562  1.1  christos 	test_iaaddr = NULL;
   1563  1.1  christos 	if (iasubopt_hash_lookup(&test_iaaddr, lease->ipv6_pool->leases,
   1564  1.1  christos 				 (void *)&lease->addr,
   1565  1.1  christos 				 sizeof(lease->addr), MDL)) {
   1566  1.1  christos 		if (test_iaaddr != lease) {
   1567  1.1  christos 			status = ISC_FALSE;
   1568  1.1  christos 		}
   1569  1.1  christos 		iasubopt_dereference(&test_iaaddr, MDL);
   1570  1.1  christos 	}
   1571  1.1  christos 
   1572  1.1  christos 	return (status);
   1573  1.1  christos }
   1574  1.1  christos 
   1575  1.1  christos /*
   1576  1.1  christos  * Put the lease on our active pool.
   1577  1.1  christos  */
   1578  1.1  christos static isc_result_t
   1579  1.1  christos move_lease_to_active(struct ipv6_pool *pool, struct iasubopt *lease) {
   1580  1.1  christos 	isc_result_t insert_result;
   1581  1.1  christos 
   1582  1.1  christos 	insert_result = isc_heap_insert(pool->active_timeouts, lease);
   1583  1.1  christos 	if (insert_result == ISC_R_SUCCESS) {
   1584  1.1  christos        		iasubopt_hash_add(pool->leases, &lease->addr,
   1585  1.1  christos 				  sizeof(lease->addr), lease, MDL);
   1586  1.1  christos 		isc_heap_delete(pool->inactive_timeouts,
   1587  1.1  christos 				lease->inactive_index);
   1588  1.1  christos 		pool->num_active++;
   1589  1.1  christos 		pool->num_inactive--;
   1590  1.1  christos 		lease->state = FTS_ACTIVE;
   1591  1.1  christos 		if (pool->ipv6_pond)
   1592  1.1  christos 			pool->ipv6_pond->num_active++;
   1593  1.1  christos 
   1594  1.1  christos 	}
   1595  1.1  christos 	return insert_result;
   1596  1.1  christos }
   1597  1.1  christos 
   1598  1.1  christos /*!
   1599  1.1  christos  *
   1600  1.1  christos  * \brief Renew a lease in the pool.
   1601  1.1  christos  *
   1602  1.1  christos  * The hard_lifetime_end_time of the lease should be set to
   1603  1.1  christos  * the current expiration time.
   1604  1.1  christos  * The soft_lifetime_end_time of the lease should be set to
   1605  1.1  christos  * the desired expiration time.
   1606  1.1  christos  *
   1607  1.1  christos  * This routine will compare the two and call the correct
   1608  1.1  christos  * heap routine to move the lease.  If the lease is active
   1609  1.1  christos  * and the new expiration time is greater (the normal case)
   1610  1.1  christos  * then we call isc_heap_decreased() as a larger time is a
   1611  1.1  christos  * lower priority.  If the new expiration time is less then
   1612  1.1  christos  * we call isc_heap_increased().
   1613  1.1  christos  *
   1614  1.1  christos  * If the lease is abandoned then it will be on the active list
   1615  1.1  christos  * and we will always call isc_heap_increased() as the previous
   1616  1.1  christos  * expiration would have been all 1s (as close as we can get
   1617  1.1  christos  * to infinite).
   1618  1.1  christos  *
   1619  1.1  christos  * If the lease is moving to active we call that routine
   1620  1.1  christos  * which will move it from the inactive list to the active list.
   1621  1.1  christos  *
   1622  1.1  christos  * \param pool  = a pool the lease belongs to
   1623  1.1  christos  * \param lease = the lease to be renewed
   1624  1.1  christos  *
   1625  1.1  christos  * \return result of the renew operation (ISC_R_SUCCESS if successful,
   1626  1.1  christos            ISC_R_NOMEMORY when run out of memory)
   1627  1.1  christos  */
   1628  1.1  christos isc_result_t
   1629  1.1  christos renew_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
   1630  1.1  christos 	time_t old_end_time = lease->hard_lifetime_end_time;
   1631  1.1  christos 	lease->hard_lifetime_end_time = lease->soft_lifetime_end_time;
   1632  1.1  christos 	lease->soft_lifetime_end_time = 0;
   1633  1.1  christos 
   1634  1.1  christos 	if (lease->state == FTS_ACTIVE) {
   1635  1.1  christos 		if (old_end_time <= lease->hard_lifetime_end_time) {
   1636  1.1  christos 			isc_heap_decreased(pool->active_timeouts,
   1637  1.1  christos 					   lease->active_index);
   1638  1.1  christos 		} else {
   1639  1.1  christos 			isc_heap_increased(pool->active_timeouts,
   1640  1.1  christos 					   lease->active_index);
   1641  1.1  christos 		}
   1642  1.1  christos 		return ISC_R_SUCCESS;
   1643  1.1  christos 	} else if (lease->state == FTS_ABANDONED) {
   1644  1.1  christos 		char tmp_addr[INET6_ADDRSTRLEN];
   1645  1.1  christos                 lease->state = FTS_ACTIVE;
   1646  1.1  christos                 isc_heap_increased(pool->active_timeouts, lease->active_index);
   1647  1.1  christos 		log_info("Reclaiming previously abandoned address %s",
   1648  1.1  christos 			 inet_ntop(AF_INET6, &(lease->addr), tmp_addr,
   1649  1.1  christos 				   sizeof(tmp_addr)));
   1650  1.1  christos 
   1651  1.1  christos 		pool->num_abandoned--;
   1652  1.1  christos 		if (pool->ipv6_pond)
   1653  1.1  christos 			pool->ipv6_pond->num_abandoned--;
   1654  1.1  christos 
   1655  1.1  christos                 return ISC_R_SUCCESS;
   1656  1.1  christos 	} else {
   1657  1.1  christos 		return move_lease_to_active(pool, lease);
   1658  1.1  christos 	}
   1659  1.1  christos }
   1660  1.1  christos 
   1661  1.1  christos /*
   1662  1.1  christos  * Put the lease on our inactive pool, with the specified state.
   1663  1.1  christos  */
   1664  1.1  christos static isc_result_t
   1665  1.1  christos move_lease_to_inactive(struct ipv6_pool *pool, struct iasubopt *lease,
   1666  1.1  christos 		       binding_state_t state) {
   1667  1.1  christos 	isc_result_t insert_result;
   1668  1.1  christos 
   1669  1.1  christos 	insert_result = isc_heap_insert(pool->inactive_timeouts, lease);
   1670  1.1  christos 	if (insert_result == ISC_R_SUCCESS) {
   1671  1.1  christos 		/*
   1672  1.1  christos 		 * Handle expire and release statements
   1673  1.1  christos 		 * To get here we must be active and have done a commit so
   1674  1.1  christos 		 * we should run the proper statements if they exist, though
   1675  1.1  christos 		 * that will change when we remove the inactive heap.
   1676  1.1  christos 		 * In addition we get rid of the references for both as we
   1677  1.1  christos 		 * can only do one (expire or release) on a lease
   1678  1.1  christos 		 */
   1679  1.1  christos 		if (lease->on_star.on_expiry != NULL) {
   1680  1.1  christos 			if (state == FTS_EXPIRED) {
   1681  1.1  christos 				execute_statements(NULL, NULL, NULL,
   1682  1.1  christos 						   NULL, NULL, NULL,
   1683  1.1  christos 						   &lease->scope,
   1684  1.1  christos 						   lease->on_star.on_expiry,
   1685  1.1  christos 						   &lease->on_star);
   1686  1.1  christos 			}
   1687  1.1  christos 			executable_statement_dereference
   1688  1.1  christos 				(&lease->on_star.on_expiry, MDL);
   1689  1.1  christos 		}
   1690  1.1  christos 
   1691  1.1  christos 		if (lease->on_star.on_release != NULL) {
   1692  1.1  christos 			if (state == FTS_RELEASED) {
   1693  1.1  christos 				execute_statements(NULL, NULL, NULL,
   1694  1.1  christos 						   NULL, NULL, NULL,
   1695  1.1  christos 						   &lease->scope,
   1696  1.1  christos 						   lease->on_star.on_release,
   1697  1.1  christos 						   &lease->on_star);
   1698  1.1  christos 			}
   1699  1.1  christos 			executable_statement_dereference
   1700  1.1  christos 				(&lease->on_star.on_release, MDL);
   1701  1.1  christos 		}
   1702  1.1  christos 
   1703  1.1  christos #if defined (NSUPDATE)
   1704  1.1  christos 		/* Process events upon expiration. */
   1705  1.1  christos 		if (pool->pool_type != D6O_IA_PD) {
   1706  1.1  christos 			(void) ddns_removals(NULL, lease, NULL, ISC_FALSE);
   1707  1.1  christos 		}
   1708  1.1  christos #endif
   1709  1.1  christos 
   1710  1.1  christos 		/* Binding scopes are no longer valid after expiry or
   1711  1.1  christos 		 * release.
   1712  1.1  christos 		 */
   1713  1.1  christos 		if (lease->scope != NULL) {
   1714  1.1  christos 			binding_scope_dereference(&lease->scope, MDL);
   1715  1.1  christos 		}
   1716  1.1  christos 
   1717  1.1  christos 		iasubopt_hash_delete(pool->leases,
   1718  1.1  christos 				     &lease->addr, sizeof(lease->addr), MDL);
   1719  1.1  christos 		isc_heap_delete(pool->active_timeouts, lease->active_index);
   1720  1.1  christos 		lease->state = state;
   1721  1.1  christos 		pool->num_active--;
   1722  1.1  christos 		pool->num_inactive++;
   1723  1.1  christos 		if (pool->ipv6_pond)
   1724  1.1  christos 			pool->ipv6_pond->num_active--;
   1725  1.1  christos 
   1726  1.1  christos 		if (lease->state == FTS_ABANDONED) {
   1727  1.1  christos 			pool->num_abandoned--;
   1728  1.1  christos 			if (pool->ipv6_pond)
   1729  1.1  christos 				pool->ipv6_pond->num_abandoned--;
   1730  1.1  christos 		}
   1731  1.1  christos 	}
   1732  1.1  christos 	return insert_result;
   1733  1.1  christos }
   1734  1.1  christos 
   1735  1.1  christos /*
   1736  1.1  christos  * Expire the oldest lease if it's lifetime_end_time is
   1737  1.1  christos  * older than the given time.
   1738  1.1  christos  *
   1739  1.1  christos  * - leasep must be a pointer to a (struct iasubopt *) pointer previously
   1740  1.1  christos  *   initialized to NULL
   1741  1.1  christos  *
   1742  1.1  christos  * On return leasep has a reference to the removed entry. It is left
   1743  1.1  christos  * pointing to NULL if the oldest lease has not expired.
   1744  1.1  christos  */
   1745  1.1  christos isc_result_t
   1746  1.1  christos expire_lease6(struct iasubopt **leasep, struct ipv6_pool *pool, time_t now) {
   1747  1.1  christos 	struct iasubopt *tmp;
   1748  1.1  christos 	isc_result_t result;
   1749  1.1  christos 
   1750  1.1  christos 	if (leasep == NULL) {
   1751  1.1  christos 		log_error("%s(%d): NULL pointer reference", MDL);
   1752  1.1  christos 		return DHCP_R_INVALIDARG;
   1753  1.1  christos 	}
   1754  1.1  christos 	if (*leasep != NULL) {
   1755  1.1  christos 		log_error("%s(%d): non-NULL pointer", MDL);
   1756  1.1  christos 		return DHCP_R_INVALIDARG;
   1757  1.1  christos 	}
   1758  1.1  christos 
   1759  1.1  christos 	if (pool->num_active > 0) {
   1760  1.1  christos 		tmp = (struct iasubopt *)
   1761  1.1  christos 				isc_heap_element(pool->active_timeouts, 1);
   1762  1.1  christos 		if (now > tmp->hard_lifetime_end_time) {
   1763  1.1  christos 			result = move_lease_to_inactive(pool, tmp,
   1764  1.1  christos 							FTS_EXPIRED);
   1765  1.1  christos 			if (result == ISC_R_SUCCESS) {
   1766  1.1  christos 				iasubopt_reference(leasep, tmp, MDL);
   1767  1.1  christos 			}
   1768  1.1  christos 			return result;
   1769  1.1  christos 		}
   1770  1.1  christos 	}
   1771  1.1  christos 	return ISC_R_SUCCESS;
   1772  1.1  christos }
   1773  1.1  christos 
   1774  1.1  christos 
   1775  1.1  christos /*
   1776  1.1  christos  * For a declined lease, leave it on the "active" pool, but mark
   1777  1.1  christos  * it as declined. Give it an infinite (well, really long) life.
   1778  1.1  christos  */
   1779  1.1  christos isc_result_t
   1780  1.1  christos decline_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
   1781  1.1  christos 	isc_result_t result;
   1782  1.1  christos 
   1783  1.1  christos 	if ((lease->state != FTS_ACTIVE) &&
   1784  1.1  christos 	    (lease->state != FTS_ABANDONED)) {
   1785  1.1  christos 		result = move_lease_to_active(pool, lease);
   1786  1.1  christos 		if (result != ISC_R_SUCCESS) {
   1787  1.1  christos 			return result;
   1788  1.1  christos 		}
   1789  1.1  christos 	}
   1790  1.1  christos 	lease->state = FTS_ABANDONED;
   1791  1.1  christos 
   1792  1.1  christos 	pool->num_abandoned++;
   1793  1.1  christos 	if (pool->ipv6_pond)
   1794  1.1  christos 		pool->ipv6_pond->num_abandoned++;
   1795  1.1  christos 
   1796  1.1  christos 	lease->hard_lifetime_end_time = MAX_TIME;
   1797  1.1  christos 	isc_heap_decreased(pool->active_timeouts, lease->active_index);
   1798  1.1  christos 	return ISC_R_SUCCESS;
   1799  1.1  christos }
   1800  1.1  christos 
   1801  1.1  christos /*
   1802  1.1  christos  * Put the returned lease on our inactive pool.
   1803  1.1  christos  */
   1804  1.1  christos isc_result_t
   1805  1.1  christos release_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
   1806  1.1  christos 	if (lease->state == FTS_ACTIVE) {
   1807  1.1  christos 		return move_lease_to_inactive(pool, lease, FTS_RELEASED);
   1808  1.1  christos 	} else {
   1809  1.1  christos 		return ISC_R_SUCCESS;
   1810  1.1  christos 	}
   1811  1.1  christos }
   1812  1.1  christos 
   1813  1.1  christos /*
   1814  1.1  christos  * Create a prefix by hashing the input, and using that for
   1815  1.1  christos  * the part subject to allocation.
   1816  1.1  christos  */
   1817  1.1  christos void
   1818  1.1  christos build_prefix6(struct in6_addr *pref,
   1819  1.1  christos 	      const struct in6_addr *net_start_pref,
   1820  1.1  christos 	      int pool_bits, int pref_bits,
   1821  1.1  christos 	      const struct data_string *input) {
   1822  1.1  christos 	isc_md5_t ctx;
   1823  1.1  christos 	int net_bytes;
   1824  1.1  christos 	int i;
   1825  1.1  christos 	char *str;
   1826  1.1  christos 	const char *net_str;
   1827  1.1  christos 
   1828  1.1  christos 	/*
   1829  1.1  christos 	 * Use MD5 to get a nice 128 bit hash of the input.
   1830  1.1  christos 	 * Yes, we know MD5 isn't cryptographically sound.
   1831  1.1  christos 	 * No, we don't care.
   1832  1.1  christos 	 */
   1833  1.1  christos 	isc_md5_init(&ctx);
   1834  1.1  christos 	isc_md5_update(&ctx, input->data, input->len);
   1835  1.1  christos 	isc_md5_final(&ctx, (unsigned char *)pref);
   1836  1.1  christos 
   1837  1.1  christos 	/*
   1838  1.1  christos 	 * Copy the network bits over.
   1839  1.1  christos 	 */
   1840  1.1  christos 	str = (char *)pref;
   1841  1.1  christos 	net_str = (const char *)net_start_pref;
   1842  1.1  christos 	net_bytes = pool_bits / 8;
   1843  1.1  christos 	for (i = 0; i < net_bytes; i++) {
   1844  1.1  christos 		str[i] = net_str[i];
   1845  1.1  christos 	}
   1846  1.1  christos 	i = net_bytes;
   1847  1.1  christos 	switch (pool_bits % 8) {
   1848  1.1  christos 		case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
   1849  1.1  christos 		case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
   1850  1.1  christos 		case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
   1851  1.1  christos 		case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
   1852  1.1  christos 		case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
   1853  1.1  christos 		case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
   1854  1.1  christos 		case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
   1855  1.1  christos 	}
   1856  1.1  christos 	/*
   1857  1.1  christos 	 * Zero the remaining bits.
   1858  1.1  christos 	 */
   1859  1.1  christos 	net_bytes = pref_bits / 8;
   1860  1.1  christos 	for (i=net_bytes+1; i<16; i++) {
   1861  1.1  christos 		str[i] = 0;
   1862  1.1  christos 	}
   1863  1.1  christos 	i = net_bytes;
   1864  1.1  christos 	switch (pref_bits % 8) {
   1865  1.1  christos 		case 0: str[i] &= 0; break;
   1866  1.1  christos 		case 1: str[i] &= 0x80; break;
   1867  1.1  christos 		case 2: str[i] &= 0xC0; break;
   1868  1.1  christos 		case 3: str[i] &= 0xE0; break;
   1869  1.1  christos 		case 4: str[i] &= 0xF0; break;
   1870  1.1  christos 		case 5: str[i] &= 0xF8; break;
   1871  1.1  christos 		case 6: str[i] &= 0xFC; break;
   1872  1.1  christos 		case 7: str[i] &= 0xFE; break;
   1873  1.1  christos 	}
   1874  1.1  christos }
   1875  1.1  christos 
   1876  1.1  christos /*
   1877  1.1  christos  * Create a lease for the given prefix and client duid.
   1878  1.1  christos  *
   1879  1.1  christos  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
   1880  1.1  christos  *   initialized to NULL
   1881  1.1  christos  *
   1882  1.1  christos  * Right now we simply hash the DUID, and if we get a collision, we hash
   1883  1.1  christos  * again until we find a free prefix. We try this a fixed number of times,
   1884  1.1  christos  * to avoid getting stuck in a loop (this is important on small pools
   1885  1.1  christos  * where we can run out of space).
   1886  1.1  christos  *
   1887  1.1  christos  * We return the number of attempts that it took to find an available
   1888  1.1  christos  * prefix. This tells callers when a pool is are filling up, as
   1889  1.1  christos  * well as an indication of how full the pool is; statistically the
   1890  1.1  christos  * more full a pool is the more attempts must be made before finding
   1891  1.1  christos  * a free prefix. Realistically this will only happen in very full
   1892  1.1  christos  * pools.
   1893  1.1  christos  *
   1894  1.1  christos  * We probably want different algorithms depending on the network size, in
   1895  1.1  christos  * the long term.
   1896  1.1  christos  */
   1897  1.1  christos isc_result_t
   1898  1.1  christos create_prefix6(struct ipv6_pool *pool, struct iasubopt **pref,
   1899  1.1  christos 	       unsigned int *attempts,
   1900  1.1  christos 	       const struct data_string *uid,
   1901  1.1  christos 	       time_t soft_lifetime_end_time) {
   1902  1.1  christos 	struct data_string ds;
   1903  1.1  christos 	struct in6_addr tmp;
   1904  1.1  christos 	struct iasubopt *test_iapref;
   1905  1.1  christos 	struct data_string new_ds;
   1906  1.1  christos 	struct iasubopt *iapref;
   1907  1.1  christos 	isc_result_t result;
   1908  1.1  christos 
   1909  1.1  christos 	/*
   1910  1.1  christos 	 * Use the UID as our initial seed for the hash
   1911  1.1  christos 	 */
   1912  1.1  christos 	memset(&ds, 0, sizeof(ds));
   1913  1.1  christos 	data_string_copy(&ds, (struct data_string *)uid, MDL);
   1914  1.1  christos 
   1915  1.1  christos 	*attempts = 0;
   1916  1.1  christos 	for (;;) {
   1917  1.1  christos 		/*
   1918  1.1  christos 		 * Give up at some point.
   1919  1.1  christos 		 */
   1920  1.1  christos 		if (++(*attempts) > 10) {
   1921  1.1  christos 			data_string_forget(&ds, MDL);
   1922  1.1  christos 			return ISC_R_NORESOURCES;
   1923  1.1  christos 		}
   1924  1.1  christos 
   1925  1.1  christos 		/*
   1926  1.1  christos 		 * Build a prefix
   1927  1.1  christos 		 */
   1928  1.1  christos 		build_prefix6(&tmp, &pool->start_addr,
   1929  1.1  christos 			      pool->bits, pool->units, &ds);
   1930  1.1  christos 
   1931  1.1  christos 		/*
   1932  1.1  christos 		 * If this prefix is not in use, we're happy with it
   1933  1.1  christos 		 */
   1934  1.1  christos 		test_iapref = NULL;
   1935  1.1  christos 		if (iasubopt_hash_lookup(&test_iapref, pool->leases,
   1936  1.1  christos 					 &tmp, sizeof(tmp), MDL) == 0) {
   1937  1.1  christos 			break;
   1938  1.1  christos 		}
   1939  1.1  christos 		iasubopt_dereference(&test_iapref, MDL);
   1940  1.1  christos 
   1941  1.1  christos 		/*
   1942  1.1  christos 		 * Otherwise, we create a new input, adding the prefix
   1943  1.1  christos 		 */
   1944  1.1  christos 		memset(&new_ds, 0, sizeof(new_ds));
   1945  1.1  christos 		new_ds.len = ds.len + sizeof(tmp);
   1946  1.1  christos 		if (!buffer_allocate(&new_ds.buffer, new_ds.len, MDL)) {
   1947  1.1  christos 			data_string_forget(&ds, MDL);
   1948  1.1  christos 			return ISC_R_NOMEMORY;
   1949  1.1  christos 		}
   1950  1.1  christos 		new_ds.data = new_ds.buffer->data;
   1951  1.1  christos 		memcpy(new_ds.buffer->data, ds.data, ds.len);
   1952  1.1  christos 		memcpy(new_ds.buffer->data + ds.len, &tmp, sizeof(tmp));
   1953  1.1  christos 		data_string_forget(&ds, MDL);
   1954  1.1  christos 		data_string_copy(&ds, &new_ds, MDL);
   1955  1.1  christos 		data_string_forget(&new_ds, MDL);
   1956  1.1  christos 	}
   1957  1.1  christos 
   1958  1.1  christos 	data_string_forget(&ds, MDL);
   1959  1.1  christos 
   1960  1.1  christos 	/*
   1961  1.1  christos 	 * We're happy with the prefix, create an IAPREFIX
   1962  1.1  christos 	 * to hold it.
   1963  1.1  christos 	 */
   1964  1.1  christos 	iapref = NULL;
   1965  1.1  christos 	result = iasubopt_allocate(&iapref, MDL);
   1966  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1967  1.1  christos 		return result;
   1968  1.1  christos 	}
   1969  1.1  christos 	iapref->plen = (u_int8_t)pool->units;
   1970  1.1  christos 	memcpy(&iapref->addr, &tmp, sizeof(iapref->addr));
   1971  1.1  christos 
   1972  1.1  christos 	/*
   1973  1.1  christos 	 * Add the prefix to the pool (note state is free, not active?!).
   1974  1.1  christos 	 */
   1975  1.1  christos 	result = add_lease6(pool, iapref, soft_lifetime_end_time);
   1976  1.1  christos 	if (result == ISC_R_SUCCESS) {
   1977  1.1  christos 		iasubopt_reference(pref, iapref, MDL);
   1978  1.1  christos 	}
   1979  1.1  christos 	iasubopt_dereference(&iapref, MDL);
   1980  1.1  christos 	return result;
   1981  1.1  christos }
   1982  1.1  christos 
   1983  1.1  christos /*
   1984  1.1  christos  * Determine if a prefix is present in a pool or not.
   1985  1.1  christos  */
   1986  1.1  christos isc_boolean_t
   1987  1.1  christos prefix6_exists(const struct ipv6_pool *pool,
   1988  1.1  christos 	       const struct in6_addr *pref, u_int8_t plen) {
   1989  1.1  christos 	struct iasubopt *test_iapref;
   1990  1.1  christos 
   1991  1.1  christos 	if ((int)plen != pool->units)
   1992  1.1  christos 		return ISC_FALSE;
   1993  1.1  christos 
   1994  1.1  christos 	test_iapref = NULL;
   1995  1.1  christos 	if (iasubopt_hash_lookup(&test_iapref, pool->leases,
   1996  1.1  christos 				 (void *)pref, sizeof(*pref), MDL)) {
   1997  1.1  christos 		iasubopt_dereference(&test_iapref, MDL);
   1998  1.1  christos 		return ISC_TRUE;
   1999  1.1  christos 	} else {
   2000  1.1  christos 		return ISC_FALSE;
   2001  1.1  christos 	}
   2002  1.1  christos }
   2003  1.1  christos 
   2004  1.1  christos /*
   2005  1.1  christos  * Mark an IPv6 address/prefix as unavailable from a pool.
   2006  1.1  christos  *
   2007  1.1  christos  * This is used for host entries and the addresses of the server itself.
   2008  1.1  christos  */
   2009  1.1  christos isc_result_t
   2010  1.1  christos mark_lease_unavailable(struct ipv6_pool *pool, const struct in6_addr *addr) {
   2011  1.1  christos 	struct iasubopt *dummy_iasubopt;
   2012  1.1  christos 	isc_result_t result;
   2013  1.1  christos 
   2014  1.1  christos 	dummy_iasubopt = NULL;
   2015  1.1  christos 	result = iasubopt_allocate(&dummy_iasubopt, MDL);
   2016  1.1  christos 	if (result == ISC_R_SUCCESS) {
   2017  1.1  christos 		dummy_iasubopt->addr = *addr;
   2018  1.1  christos 		iasubopt_hash_add(pool->leases, &dummy_iasubopt->addr,
   2019  1.1  christos 				  sizeof(*addr), dummy_iasubopt, MDL);
   2020  1.1  christos 	}
   2021  1.1  christos 	return result;
   2022  1.1  christos }
   2023  1.1  christos 
   2024  1.1  christos /*
   2025  1.1  christos  * Add a pool.
   2026  1.1  christos  */
   2027  1.1  christos isc_result_t
   2028  1.1  christos add_ipv6_pool(struct ipv6_pool *pool) {
   2029  1.1  christos 	struct ipv6_pool **new_pools;
   2030  1.1  christos 
   2031  1.1  christos 	new_pools = dmalloc(sizeof(struct ipv6_pool *) * (num_pools+1), MDL);
   2032  1.1  christos 	if (new_pools == NULL) {
   2033  1.1  christos 		return ISC_R_NOMEMORY;
   2034  1.1  christos 	}
   2035  1.1  christos 
   2036  1.1  christos 	if (num_pools > 0) {
   2037  1.1  christos 		memcpy(new_pools, pools,
   2038  1.1  christos 		       sizeof(struct ipv6_pool *) * num_pools);
   2039  1.1  christos 		dfree(pools, MDL);
   2040  1.1  christos 	}
   2041  1.1  christos 	pools = new_pools;
   2042  1.1  christos 
   2043  1.1  christos 	pools[num_pools] = NULL;
   2044  1.1  christos 	ipv6_pool_reference(&pools[num_pools], pool, MDL);
   2045  1.1  christos 	num_pools++;
   2046  1.1  christos 	return ISC_R_SUCCESS;
   2047  1.1  christos }
   2048  1.1  christos 
   2049  1.1  christos static void
   2050  1.1  christos cleanup_old_expired(struct ipv6_pool *pool) {
   2051  1.1  christos 	struct iasubopt *tmp;
   2052  1.1  christos 	struct ia_xx *ia;
   2053  1.1  christos 	struct ia_xx *ia_active;
   2054  1.1  christos 	unsigned char *tmpd;
   2055  1.1  christos 	time_t timeout;
   2056  1.1  christos 
   2057  1.1  christos 	while (pool->num_inactive > 0) {
   2058  1.1  christos 		tmp = (struct iasubopt *)
   2059  1.1  christos 				isc_heap_element(pool->inactive_timeouts, 1);
   2060  1.1  christos 		if (tmp->hard_lifetime_end_time != 0) {
   2061  1.1  christos 			timeout = tmp->hard_lifetime_end_time;
   2062  1.1  christos 			timeout += EXPIRED_IPV6_CLEANUP_TIME;
   2063  1.1  christos 		} else {
   2064  1.1  christos 			timeout = tmp->soft_lifetime_end_time;
   2065  1.1  christos 		}
   2066  1.1  christos 		if (cur_time < timeout) {
   2067  1.1  christos 			break;
   2068  1.1  christos 		}
   2069  1.1  christos 
   2070  1.1  christos 		isc_heap_delete(pool->inactive_timeouts, tmp->inactive_index);
   2071  1.1  christos 		pool->num_inactive--;
   2072  1.1  christos 
   2073  1.1  christos 		if (tmp->ia != NULL) {
   2074  1.1  christos 			/*
   2075  1.1  christos 			 * Check to see if this IA is in an active list,
   2076  1.1  christos 			 * but has no remaining resources. If so, remove it
   2077  1.1  christos 			 * from the active list.
   2078  1.1  christos 			 */
   2079  1.1  christos 			ia = NULL;
   2080  1.1  christos 			ia_reference(&ia, tmp->ia, MDL);
   2081  1.1  christos 			ia_remove_iasubopt(ia, tmp, MDL);
   2082  1.1  christos 			ia_active = NULL;
   2083  1.1  christos 			tmpd = (unsigned char *)ia->iaid_duid.data;
   2084  1.1  christos 			if ((ia->ia_type == D6O_IA_NA) &&
   2085  1.1  christos 			    (ia->num_iasubopt <= 0) &&
   2086  1.1  christos 			    (ia_hash_lookup(&ia_active, ia_na_active, tmpd,
   2087  1.1  christos 					    ia->iaid_duid.len, MDL) == 0) &&
   2088  1.1  christos 			    (ia_active == ia)) {
   2089  1.1  christos 				ia_hash_delete(ia_na_active, tmpd,
   2090  1.1  christos 					       ia->iaid_duid.len, MDL);
   2091  1.1  christos 			}
   2092  1.1  christos 			if ((ia->ia_type == D6O_IA_TA) &&
   2093  1.1  christos 			    (ia->num_iasubopt <= 0) &&
   2094  1.1  christos 			    (ia_hash_lookup(&ia_active, ia_ta_active, tmpd,
   2095  1.1  christos 					    ia->iaid_duid.len, MDL) == 0) &&
   2096  1.1  christos 			    (ia_active == ia)) {
   2097  1.1  christos 				ia_hash_delete(ia_ta_active, tmpd,
   2098  1.1  christos 					       ia->iaid_duid.len, MDL);
   2099  1.1  christos 			}
   2100  1.1  christos 			if ((ia->ia_type == D6O_IA_PD) &&
   2101  1.1  christos 			    (ia->num_iasubopt <= 0) &&
   2102  1.1  christos 			    (ia_hash_lookup(&ia_active, ia_pd_active, tmpd,
   2103  1.1  christos 					    ia->iaid_duid.len, MDL) == 0) &&
   2104  1.1  christos 			    (ia_active == ia)) {
   2105  1.1  christos 				ia_hash_delete(ia_pd_active, tmpd,
   2106  1.1  christos 					       ia->iaid_duid.len, MDL);
   2107  1.1  christos 			}
   2108  1.1  christos 			ia_dereference(&ia, MDL);
   2109  1.1  christos 		}
   2110  1.1  christos 		iasubopt_dereference(&tmp, MDL);
   2111  1.1  christos 	}
   2112  1.1  christos }
   2113  1.1  christos 
   2114  1.1  christos static void
   2115  1.1  christos lease_timeout_support(void *vpool) {
   2116  1.1  christos 	struct ipv6_pool *pool;
   2117  1.1  christos 	struct iasubopt *lease;
   2118  1.1  christos 
   2119  1.1  christos 	pool = (struct ipv6_pool *)vpool;
   2120  1.1  christos 	for (;;) {
   2121  1.1  christos 		/*
   2122  1.1  christos 		 * Get the next lease scheduled to expire.
   2123  1.1  christos 		 *
   2124  1.1  christos 		 * Note that if there are no leases in the pool,
   2125  1.1  christos 		 * expire_lease6() will return ISC_R_SUCCESS with
   2126  1.1  christos 		 * a NULL lease.
   2127  1.1  christos 		 *
   2128  1.1  christos 		 * expire_lease6() will call move_lease_to_inactive() which
   2129  1.1  christos 		 * calls ddns_removals() do we want that on the standard
   2130  1.1  christos 		 * expiration timer or a special 'depref' timer?  Original
   2131  1.1  christos 		 * query from DH, moved here by SAR.
   2132  1.1  christos 		 */
   2133  1.1  christos 		lease = NULL;
   2134  1.1  christos 		if (expire_lease6(&lease, pool, cur_time) != ISC_R_SUCCESS) {
   2135  1.1  christos 			break;
   2136  1.1  christos 		}
   2137  1.1  christos 		if (lease == NULL) {
   2138  1.1  christos 			break;
   2139  1.1  christos 		}
   2140  1.1  christos 
   2141  1.1  christos 		write_ia(lease->ia);
   2142  1.1  christos 
   2143  1.1  christos 		iasubopt_dereference(&lease, MDL);
   2144  1.1  christos 	}
   2145  1.1  christos 
   2146  1.1  christos 	/*
   2147  1.1  christos 	 * If appropriate commit and rotate the lease file
   2148  1.1  christos 	 * As commit_leases_timed() checks to see if we've done any writes
   2149  1.1  christos 	 * we don't bother tracking if this function called write _ia
   2150  1.1  christos 	 */
   2151  1.1  christos 	(void) commit_leases_timed();
   2152  1.1  christos 
   2153  1.1  christos 	/*
   2154  1.1  christos 	 * Do some cleanup of our expired leases.
   2155  1.1  christos 	 */
   2156  1.1  christos 	cleanup_old_expired(pool);
   2157  1.1  christos 
   2158  1.1  christos 	/*
   2159  1.1  christos 	 * Schedule next round of expirations.
   2160  1.1  christos 	 */
   2161  1.1  christos 	schedule_lease_timeout(pool);
   2162  1.1  christos }
   2163  1.1  christos 
   2164  1.1  christos /*
   2165  1.1  christos  * For a given pool, add a timer that will remove the next
   2166  1.1  christos  * lease to expire.
   2167  1.1  christos  */
   2168  1.1  christos void
   2169  1.1  christos schedule_lease_timeout(struct ipv6_pool *pool) {
   2170  1.1  christos 	struct iasubopt *tmp;
   2171  1.1  christos 	time_t timeout;
   2172  1.1  christos 	time_t next_timeout;
   2173  1.1  christos 	struct timeval tv;
   2174  1.1  christos 
   2175  1.1  christos 	next_timeout = MAX_TIME;
   2176  1.1  christos 
   2177  1.1  christos 	if (pool->num_active > 0) {
   2178  1.1  christos 		tmp = (struct iasubopt *)
   2179  1.1  christos 				isc_heap_element(pool->active_timeouts, 1);
   2180  1.1  christos 		if (tmp->hard_lifetime_end_time < next_timeout) {
   2181  1.1  christos 			next_timeout = tmp->hard_lifetime_end_time + 1;
   2182  1.1  christos 		}
   2183  1.1  christos 	}
   2184  1.1  christos 
   2185  1.1  christos 	if (pool->num_inactive > 0) {
   2186  1.1  christos 		tmp = (struct iasubopt *)
   2187  1.1  christos 				isc_heap_element(pool->inactive_timeouts, 1);
   2188  1.1  christos 		if (tmp->hard_lifetime_end_time != 0) {
   2189  1.1  christos 			timeout = tmp->hard_lifetime_end_time;
   2190  1.1  christos 			timeout += EXPIRED_IPV6_CLEANUP_TIME;
   2191  1.1  christos 		} else {
   2192  1.1  christos 			timeout = tmp->soft_lifetime_end_time + 1;
   2193  1.1  christos 		}
   2194  1.1  christos 		if (timeout < next_timeout) {
   2195  1.1  christos 			next_timeout = timeout;
   2196  1.1  christos 		}
   2197  1.1  christos 	}
   2198  1.1  christos 
   2199  1.1  christos 	if (next_timeout < MAX_TIME) {
   2200  1.1  christos 		tv.tv_sec = next_timeout;
   2201  1.1  christos 		tv.tv_usec = 0;
   2202  1.1  christos 		add_timeout(&tv, lease_timeout_support, pool,
   2203  1.1  christos 			    (tvref_t)ipv6_pool_reference,
   2204  1.1  christos 			    (tvunref_t)ipv6_pool_dereference);
   2205  1.1  christos 	}
   2206  1.1  christos }
   2207  1.1  christos 
   2208  1.1  christos /*
   2209  1.1  christos  * Schedule timeouts across all pools.
   2210  1.1  christos  */
   2211  1.1  christos void
   2212  1.1  christos schedule_all_ipv6_lease_timeouts(void) {
   2213  1.1  christos 	int i;
   2214  1.1  christos 
   2215  1.1  christos 	for (i=0; i<num_pools; i++) {
   2216  1.1  christos 		schedule_lease_timeout(pools[i]);
   2217  1.1  christos 	}
   2218  1.1  christos }
   2219  1.1  christos 
   2220  1.1  christos /*
   2221  1.1  christos  * Given an address and the length of the network mask, return
   2222  1.1  christos  * only the network portion.
   2223  1.1  christos  *
   2224  1.1  christos  * Examples:
   2225  1.1  christos  *
   2226  1.1  christos  *   "fe80::216:6fff:fe49:7d9b", length 64 = "fe80::"
   2227  1.1  christos  *   "2001:888:1936:2:216:6fff:fe49:7d9b", length 48 = "2001:888:1936::"
   2228  1.1  christos  */
   2229  1.1  christos static void
   2230  1.1  christos ipv6_network_portion(struct in6_addr *result,
   2231  1.1  christos 		     const struct in6_addr *addr, int bits) {
   2232  1.1  christos 	unsigned char *addrp;
   2233  1.1  christos 	int mask_bits;
   2234  1.1  christos 	int bytes;
   2235  1.1  christos 	int extra_bits;
   2236  1.1  christos 	int i;
   2237  1.1  christos 
   2238  1.1  christos 	static const unsigned char bitmasks[] = {
   2239  1.1  christos 		0x00, 0xFE, 0xFC, 0xF8,
   2240  1.1  christos 		0xF0, 0xE0, 0xC0, 0x80,
   2241  1.1  christos 	};
   2242  1.1  christos 
   2243  1.1  christos 	/*
   2244  1.1  christos 	 *  Sanity check our bits. ;)
   2245  1.1  christos 	 */
   2246  1.1  christos 	if ((bits < 0) || (bits > 128)) {
   2247  1.1  christos 		log_fatal("ipv6_network_portion: bits %d not between 0 and 128",
   2248  1.1  christos 			  bits);
   2249  1.1  christos 	}
   2250  1.1  christos 
   2251  1.1  christos 	/*
   2252  1.1  christos 	 * Copy our address portion.
   2253  1.1  christos 	 */
   2254  1.1  christos 	*result = *addr;
   2255  1.1  christos 	addrp = ((unsigned char *)result) + 15;
   2256  1.1  christos 
   2257  1.1  christos 	/*
   2258  1.1  christos 	 * Zero out masked portion.
   2259  1.1  christos 	 */
   2260  1.1  christos 	mask_bits = 128 - bits;
   2261  1.1  christos 	bytes = mask_bits / 8;
   2262  1.1  christos 	extra_bits = mask_bits % 8;
   2263  1.1  christos 
   2264  1.1  christos 	for (i=0; i<bytes; i++) {
   2265  1.1  christos 		*addrp = 0;
   2266  1.1  christos 		addrp--;
   2267  1.1  christos 	}
   2268  1.1  christos 	if (extra_bits) {
   2269  1.1  christos 		*addrp &= bitmasks[extra_bits];
   2270  1.1  christos 	}
   2271  1.1  christos }
   2272  1.1  christos 
   2273  1.1  christos /*
   2274  1.1  christos  * Determine if the given address/prefix is in the pool.
   2275  1.1  christos  */
   2276  1.1  christos isc_boolean_t
   2277  1.1  christos ipv6_in_pool(const struct in6_addr *addr, const struct ipv6_pool *pool) {
   2278  1.1  christos 	struct in6_addr tmp;
   2279  1.1  christos 
   2280  1.1  christos 	ipv6_network_portion(&tmp, addr, pool->bits);
   2281  1.1  christos 	if (memcmp(&tmp, &pool->start_addr, sizeof(tmp)) == 0) {
   2282  1.1  christos 		return ISC_TRUE;
   2283  1.1  christos 	} else {
   2284  1.1  christos 		return ISC_FALSE;
   2285  1.1  christos 	}
   2286  1.1  christos }
   2287  1.1  christos 
   2288  1.1  christos /*
   2289  1.1  christos  * Find the pool that contains the given address.
   2290  1.1  christos  *
   2291  1.1  christos  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
   2292  1.1  christos  *   initialized to NULL
   2293  1.1  christos  */
   2294  1.1  christos isc_result_t
   2295  1.1  christos find_ipv6_pool(struct ipv6_pool **pool, u_int16_t type,
   2296  1.1  christos 	       const struct in6_addr *addr) {
   2297  1.1  christos 	int i;
   2298  1.1  christos 
   2299  1.1  christos 	if (pool == NULL) {
   2300  1.1  christos 		log_error("%s(%d): NULL pointer reference", MDL);
   2301  1.1  christos 		return DHCP_R_INVALIDARG;
   2302  1.1  christos 	}
   2303  1.1  christos 	if (*pool != NULL) {
   2304  1.1  christos 		log_error("%s(%d): non-NULL pointer", MDL);
   2305  1.1  christos 		return DHCP_R_INVALIDARG;
   2306  1.1  christos 	}
   2307  1.1  christos 
   2308  1.1  christos 	for (i=0; i<num_pools; i++) {
   2309  1.1  christos 		if (pools[i]->pool_type != type)
   2310  1.1  christos 			continue;
   2311  1.1  christos 		if (ipv6_in_pool(addr, pools[i])) {
   2312  1.1  christos 			ipv6_pool_reference(pool, pools[i], MDL);
   2313  1.1  christos 			return ISC_R_SUCCESS;
   2314  1.1  christos 		}
   2315  1.1  christos 	}
   2316  1.1  christos 	return ISC_R_NOTFOUND;
   2317  1.1  christos }
   2318  1.1  christos 
   2319  1.1  christos /*
   2320  1.1  christos  * Helper function for the various functions that act across all
   2321  1.1  christos  * pools.
   2322  1.1  christos  */
   2323  1.1  christos static isc_result_t
   2324  1.1  christos change_leases(struct ia_xx *ia,
   2325  1.1  christos 	      isc_result_t (*change_func)(struct ipv6_pool *,
   2326  1.1  christos 					  struct iasubopt *)) {
   2327  1.1  christos 	isc_result_t retval;
   2328  1.1  christos 	isc_result_t renew_retval;
   2329  1.1  christos 	struct ipv6_pool *pool;
   2330  1.1  christos 	struct in6_addr *addr;
   2331  1.1  christos 	int i;
   2332  1.1  christos 
   2333  1.1  christos 	retval = ISC_R_SUCCESS;
   2334  1.1  christos 	for (i=0; i<ia->num_iasubopt; i++) {
   2335  1.1  christos 		pool = NULL;
   2336  1.1  christos 		addr = &ia->iasubopt[i]->addr;
   2337  1.1  christos 		if (find_ipv6_pool(&pool, ia->ia_type,
   2338  1.1  christos 				   addr) == ISC_R_SUCCESS) {
   2339  1.1  christos 			renew_retval = change_func(pool, ia->iasubopt[i]);
   2340  1.1  christos 			if (renew_retval != ISC_R_SUCCESS) {
   2341  1.1  christos 				retval = renew_retval;
   2342  1.1  christos 			}
   2343  1.1  christos 		}
   2344  1.1  christos 		/* XXXsk: should we warn if we don't find a pool? */
   2345  1.1  christos 	}
   2346  1.1  christos 	return retval;
   2347  1.1  christos }
   2348  1.1  christos 
   2349  1.1  christos /*
   2350  1.1  christos  * Renew all leases in an IA from all pools.
   2351  1.1  christos  *
   2352  1.1  christos  * The new lifetime should be in the soft_lifetime_end_time
   2353  1.1  christos  * and will be moved to hard_lifetime_end_time by renew_lease6.
   2354  1.1  christos  */
   2355  1.1  christos isc_result_t
   2356  1.1  christos renew_leases(struct ia_xx *ia) {
   2357  1.1  christos 	return change_leases(ia, renew_lease6);
   2358  1.1  christos }
   2359  1.1  christos 
   2360  1.1  christos /*
   2361  1.1  christos  * Release all leases in an IA from all pools.
   2362  1.1  christos  */
   2363  1.1  christos isc_result_t
   2364  1.1  christos release_leases(struct ia_xx *ia) {
   2365  1.1  christos 	return change_leases(ia, release_lease6);
   2366  1.1  christos }
   2367  1.1  christos 
   2368  1.1  christos /*
   2369  1.1  christos  * Decline all leases in an IA from all pools.
   2370  1.1  christos  */
   2371  1.1  christos isc_result_t
   2372  1.1  christos decline_leases(struct ia_xx *ia) {
   2373  1.1  christos 	return change_leases(ia, decline_lease6);
   2374  1.1  christos }
   2375  1.1  christos 
   2376  1.1  christos #ifdef DHCPv6
   2377  1.1  christos /*
   2378  1.1  christos  * Helper function to output leases.
   2379  1.1  christos  */
   2380  1.1  christos static int write_error;
   2381  1.1  christos 
   2382  1.1  christos static isc_result_t
   2383  1.1  christos write_ia_leases(const void *name, unsigned len, void *value) {
   2384  1.1  christos 	struct ia_xx *ia = (struct ia_xx *)value;
   2385  1.1  christos 
   2386  1.1  christos 	if (!write_error) {
   2387  1.1  christos 		if (!write_ia(ia)) {
   2388  1.1  christos 			write_error = 1;
   2389  1.1  christos 		}
   2390  1.1  christos 	}
   2391  1.1  christos 	return ISC_R_SUCCESS;
   2392  1.1  christos }
   2393  1.1  christos 
   2394  1.1  christos /*
   2395  1.1  christos  * Write all DHCPv6 information.
   2396  1.1  christos  */
   2397  1.1  christos int
   2398  1.1  christos write_leases6(void) {
   2399  1.1  christos 	int nas, tas, pds;
   2400  1.1  christos 
   2401  1.1  christos 	write_error = 0;
   2402  1.1  christos 	write_server_duid();
   2403  1.1  christos 	nas = ia_hash_foreach(ia_na_active, write_ia_leases);
   2404  1.1  christos 	if (write_error) {
   2405  1.1  christos 		return 0;
   2406  1.1  christos 	}
   2407  1.1  christos 	tas = ia_hash_foreach(ia_ta_active, write_ia_leases);
   2408  1.1  christos 	if (write_error) {
   2409  1.1  christos 		return 0;
   2410  1.1  christos 	}
   2411  1.1  christos 	pds = ia_hash_foreach(ia_pd_active, write_ia_leases);
   2412  1.1  christos 	if (write_error) {
   2413  1.1  christos 		return 0;
   2414  1.1  christos 	}
   2415  1.1  christos 
   2416  1.1  christos 	log_info("Wrote %d NA, %d TA, %d PD leases to lease file.",
   2417  1.1  christos 		 nas, tas, pds);
   2418  1.1  christos 	return 1;
   2419  1.1  christos }
   2420  1.1  christos #endif /* DHCPv6 */
   2421  1.1  christos 
   2422  1.1  christos static isc_result_t
   2423  1.1  christos mark_hosts_unavailable_support(const void *name, unsigned len, void *value) {
   2424  1.1  christos 	struct host_decl *h;
   2425  1.1  christos 	struct data_string fixed_addr;
   2426  1.1  christos 	struct in6_addr addr;
   2427  1.1  christos 	struct ipv6_pool *p;
   2428  1.1  christos 
   2429  1.1  christos 	h = (struct host_decl *)value;
   2430  1.1  christos 
   2431  1.1  christos 	/*
   2432  1.1  christos 	 * If the host has no address, we don't need to mark anything.
   2433  1.1  christos 	 */
   2434  1.1  christos 	if (h->fixed_addr == NULL) {
   2435  1.1  christos 		return ISC_R_SUCCESS;
   2436  1.1  christos 	}
   2437  1.1  christos 
   2438  1.1  christos 	/*
   2439  1.1  christos 	 * Evaluate the fixed address.
   2440  1.1  christos 	 */
   2441  1.1  christos 	memset(&fixed_addr, 0, sizeof(fixed_addr));
   2442  1.1  christos 	if (!evaluate_option_cache(&fixed_addr, NULL, NULL, NULL, NULL, NULL,
   2443  1.1  christos 				   &global_scope, h->fixed_addr, MDL)) {
   2444  1.1  christos 		log_error("mark_hosts_unavailable: "
   2445  1.1  christos 			  "error evaluating host address.");
   2446  1.1  christos 		return ISC_R_SUCCESS;
   2447  1.1  christos 	}
   2448  1.1  christos 	if (fixed_addr.len != 16) {
   2449  1.1  christos 		log_error("mark_hosts_unavailable: "
   2450  1.1  christos 			  "host address is not 128 bits.");
   2451  1.1  christos 		return ISC_R_SUCCESS;
   2452  1.1  christos 	}
   2453  1.1  christos 	memcpy(&addr, fixed_addr.data, 16);
   2454  1.1  christos 	data_string_forget(&fixed_addr, MDL);
   2455  1.1  christos 
   2456  1.1  christos 	/*
   2457  1.1  christos 	 * Find the pool holding this host, and mark the address.
   2458  1.1  christos 	 * (I suppose it is arguably valid to have a host that does not
   2459  1.1  christos 	 * sit in any pool.)
   2460  1.1  christos 	 */
   2461  1.1  christos 	p = NULL;
   2462  1.1  christos 	if (find_ipv6_pool(&p, D6O_IA_NA, &addr) == ISC_R_SUCCESS) {
   2463  1.1  christos 		mark_lease_unavailable(p, &addr);
   2464  1.1  christos 		ipv6_pool_dereference(&p, MDL);
   2465  1.1  christos 	}
   2466  1.1  christos 	if (find_ipv6_pool(&p, D6O_IA_TA, &addr) == ISC_R_SUCCESS) {
   2467  1.1  christos 		mark_lease_unavailable(p, &addr);
   2468  1.1  christos 		ipv6_pool_dereference(&p, MDL);
   2469  1.1  christos 	}
   2470  1.1  christos 
   2471  1.1  christos 	return ISC_R_SUCCESS;
   2472  1.1  christos }
   2473  1.1  christos 
   2474  1.1  christos void
   2475  1.1  christos mark_hosts_unavailable(void) {
   2476  1.1  christos 	hash_foreach(host_name_hash, mark_hosts_unavailable_support);
   2477  1.1  christos }
   2478  1.1  christos 
   2479  1.1  christos static isc_result_t
   2480  1.1  christos mark_phosts_unavailable_support(const void *name, unsigned len, void *value) {
   2481  1.1  christos 	struct host_decl *h;
   2482  1.1  christos 	struct iaddrcidrnetlist *l;
   2483  1.1  christos 	struct in6_addr pref;
   2484  1.1  christos 	struct ipv6_pool *p;
   2485  1.1  christos 
   2486  1.1  christos 	h = (struct host_decl *)value;
   2487  1.1  christos 
   2488  1.1  christos 	/*
   2489  1.1  christos 	 * If the host has no prefix, we don't need to mark anything.
   2490  1.1  christos 	 */
   2491  1.1  christos 	if (h->fixed_prefix == NULL) {
   2492  1.1  christos 		return ISC_R_SUCCESS;
   2493  1.1  christos 	}
   2494  1.1  christos 
   2495  1.1  christos 	/*
   2496  1.1  christos 	 * Get the fixed prefixes.
   2497  1.1  christos 	 */
   2498  1.1  christos 	for (l = h->fixed_prefix; l != NULL; l = l->next) {
   2499  1.1  christos 		if (l->cidrnet.lo_addr.len != 16) {
   2500  1.1  christos 			continue;
   2501  1.1  christos 		}
   2502  1.1  christos 		memcpy(&pref, l->cidrnet.lo_addr.iabuf, 16);
   2503  1.1  christos 
   2504  1.1  christos 		/*
   2505  1.1  christos 		 * Find the pool holding this host, and mark the prefix.
   2506  1.1  christos 		 * (I suppose it is arguably valid to have a host that does not
   2507  1.1  christos 		 * sit in any pool.)
   2508  1.1  christos 		 */
   2509  1.1  christos 		p = NULL;
   2510  1.1  christos 		if (find_ipv6_pool(&p, D6O_IA_PD, &pref) != ISC_R_SUCCESS) {
   2511  1.1  christos 			continue;
   2512  1.1  christos 		}
   2513  1.1  christos 		if (l->cidrnet.bits != p->units) {
   2514  1.1  christos 			ipv6_pool_dereference(&p, MDL);
   2515  1.1  christos 			continue;
   2516  1.1  christos 		}
   2517  1.1  christos 		mark_lease_unavailable(p, &pref);
   2518  1.1  christos 		ipv6_pool_dereference(&p, MDL);
   2519  1.1  christos 	}
   2520  1.1  christos 
   2521  1.1  christos 	return ISC_R_SUCCESS;
   2522  1.1  christos }
   2523  1.1  christos 
   2524  1.1  christos void
   2525  1.1  christos mark_phosts_unavailable(void) {
   2526  1.1  christos 	hash_foreach(host_name_hash, mark_phosts_unavailable_support);
   2527  1.1  christos }
   2528  1.1  christos 
   2529  1.1  christos void
   2530  1.1  christos mark_interfaces_unavailable(void) {
   2531  1.1  christos 	struct interface_info *ip;
   2532  1.1  christos 	int i;
   2533  1.1  christos 	struct ipv6_pool *p;
   2534  1.1  christos 
   2535  1.1  christos 	ip = interfaces;
   2536  1.1  christos 	while (ip != NULL) {
   2537  1.1  christos 		for (i=0; i<ip->v6address_count; i++) {
   2538  1.1  christos 			p = NULL;
   2539  1.1  christos 			if (find_ipv6_pool(&p, D6O_IA_NA, &ip->v6addresses[i])
   2540  1.1  christos 							== ISC_R_SUCCESS) {
   2541  1.1  christos 				mark_lease_unavailable(p,
   2542  1.1  christos 						       &ip->v6addresses[i]);
   2543  1.1  christos 				ipv6_pool_dereference(&p, MDL);
   2544  1.1  christos 			}
   2545  1.1  christos 			if (find_ipv6_pool(&p, D6O_IA_TA, &ip->v6addresses[i])
   2546  1.1  christos 							== ISC_R_SUCCESS) {
   2547  1.1  christos 				mark_lease_unavailable(p,
   2548  1.1  christos 						       &ip->v6addresses[i]);
   2549  1.1  christos 				ipv6_pool_dereference(&p, MDL);
   2550  1.1  christos 			}
   2551  1.1  christos 		}
   2552  1.1  christos 		ip = ip->next;
   2553  1.1  christos 	}
   2554  1.1  christos }
   2555  1.1  christos 
   2556  1.1  christos /*!
   2557  1.1  christos  * \brief Create a new IPv6 pond structure.
   2558  1.1  christos  *
   2559  1.1  christos  * Allocate space for a new ipv6_pond structure and return a reference
   2560  1.1  christos  * to it, includes setting the reference count to 1.
   2561  1.1  christos  *
   2562  1.1  christos  * \param pond = space for returning a referenced pointer to the pond.
   2563  1.1  christos  *		 This must point to a space that has been initialzied
   2564  1.1  christos  *		 to NULL by the caller.
   2565  1.1  christos  *
   2566  1.1  christos  * \return
   2567  1.1  christos  * ISC_R_SUCCESS     = The pond was successfully created, pond points to it.
   2568  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pond has not been
   2569  1.1  christos  *		       modified
   2570  1.1  christos  * ISC_R_NOMEMORY    = The system wasn't able to allocate memory, pond has
   2571  1.1  christos  *		       not been modified.
   2572  1.1  christos  */
   2573  1.1  christos isc_result_t
   2574  1.1  christos ipv6_pond_allocate(struct ipv6_pond **pond, const char *file, int line) {
   2575  1.1  christos 	struct ipv6_pond *tmp;
   2576  1.1  christos 
   2577  1.1  christos 	if (pond == NULL) {
   2578  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
   2579  1.1  christos 		return DHCP_R_INVALIDARG;
   2580  1.1  christos 	}
   2581  1.1  christos 	if (*pond != NULL) {
   2582  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
   2583  1.1  christos 		return DHCP_R_INVALIDARG;
   2584  1.1  christos 	}
   2585  1.1  christos 
   2586  1.1  christos 	tmp = dmalloc(sizeof(*tmp), file, line);
   2587  1.1  christos 	if (tmp == NULL) {
   2588  1.1  christos 		return ISC_R_NOMEMORY;
   2589  1.1  christos 	}
   2590  1.1  christos 
   2591  1.1  christos 	tmp->refcnt = 1;
   2592  1.1  christos 
   2593  1.1  christos 	*pond = tmp;
   2594  1.1  christos 	return ISC_R_SUCCESS;
   2595  1.1  christos }
   2596  1.1  christos 
   2597  1.1  christos /*!
   2598  1.1  christos  *
   2599  1.1  christos  * \brief reference an IPv6 pond structure.
   2600  1.1  christos  *
   2601  1.1  christos  * This function genreates a reference to an ipv6_pond structure
   2602  1.1  christos  * and increments the reference count on the structure.
   2603  1.1  christos  *
   2604  1.1  christos  * \param[out] pond = space for returning a referenced pointer to the pond.
   2605  1.1  christos  *		      This must point to a space that has been initialzied
   2606  1.1  christos  *		      to NULL by the caller.
   2607  1.1  christos  * \param[in]  src  = A pointer to the pond to reference.  This must not be
   2608  1.1  christos  *		      NULL.
   2609  1.1  christos  *
   2610  1.1  christos  * \return
   2611  1.1  christos  * ISC_R_SUCCESS     = The pond was successfully referenced, pond now points
   2612  1.1  christos  *		       to src.
   2613  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pond has not been
   2614  1.1  christos  *		       modified.
   2615  1.1  christos  */
   2616  1.1  christos isc_result_t
   2617  1.1  christos ipv6_pond_reference(struct ipv6_pond **pond, struct ipv6_pond *src,
   2618  1.1  christos 		    const char *file, int line) {
   2619  1.1  christos 	if (pond == NULL) {
   2620  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
   2621  1.1  christos 		return DHCP_R_INVALIDARG;
   2622  1.1  christos 	}
   2623  1.1  christos 	if (*pond != NULL) {
   2624  1.1  christos 		log_error("%s(%d): non-NULL pointer", file, line);
   2625  1.1  christos 		return DHCP_R_INVALIDARG;
   2626  1.1  christos 	}
   2627  1.1  christos 	if (src == NULL) {
   2628  1.1  christos 		log_error("%s(%d): NULL pointer reference", file, line);
   2629  1.1  christos 		return DHCP_R_INVALIDARG;
   2630  1.1  christos 	}
   2631  1.1  christos 	*pond = src;
   2632  1.1  christos 	src->refcnt++;
   2633  1.1  christos 	return ISC_R_SUCCESS;
   2634  1.1  christos }
   2635  1.1  christos 
   2636  1.1  christos /*!
   2637  1.1  christos  *
   2638  1.1  christos  * \brief de-reference an IPv6 pond structure.
   2639  1.1  christos  *
   2640  1.1  christos  * This function decrements the reference count in an ipv6_pond structure.
   2641  1.1  christos  * If this was the last reference then the memory for the structure is
   2642  1.1  christos  * freed.
   2643  1.1  christos  *
   2644  1.1  christos  * \param[in] pond = A pointer to the pointer to the pond that should be
   2645  1.1  christos  *		     de-referenced.  On success the pointer to the pond
   2646  1.1  christos  *		     is cleared.  It must not be NULL and must not point
   2647  1.1  christos  *		     to NULL.
   2648  1.1  christos  *
   2649  1.1  christos  * \return
   2650  1.1  christos  * ISC_R_SUCCESS     = The pond was successfully de-referenced, pond now points
   2651  1.1  christos  *		       to NULL
   2652  1.1  christos  * DHCP_R_INVALIDARG = One of the arugments was invalid, pond has not been
   2653  1.1  christos  *		       modified.
   2654  1.1  christos  */
   2655  1.1  christos 
   2656  1.1  christos isc_result_t
   2657  1.1  christos ipv6_pond_dereference(struct ipv6_pond **pond, const char *file, int line) {
   2658  1.1  christos 	struct ipv6_pond *tmp;
   2659  1.1  christos 
   2660  1.1  christos 	if ((pond == NULL) || (*pond == NULL)) {
   2661  1.1  christos 		log_error("%s(%d): NULL pointer", file, line);
   2662  1.1  christos 		return DHCP_R_INVALIDARG;
   2663  1.1  christos 	}
   2664  1.1  christos 
   2665  1.1  christos 	tmp = *pond;
   2666  1.1  christos 	*pond = NULL;
   2667  1.1  christos 
   2668  1.1  christos 	tmp->refcnt--;
   2669  1.1  christos 	if (tmp->refcnt < 0) {
   2670  1.1  christos 		log_error("%s(%d): negative refcnt", file, line);
   2671  1.1  christos 		tmp->refcnt = 0;
   2672  1.1  christos 	}
   2673  1.1  christos 	if (tmp->refcnt == 0) {
   2674  1.1  christos 		dfree(tmp, file, line);
   2675  1.1  christos 	}
   2676  1.1  christos 
   2677  1.1  christos 	return ISC_R_SUCCESS;
   2678  1.1  christos }
   2679  1.1  christos 
   2680  1.1  christos #ifdef EUI_64
   2681  1.1  christos /*
   2682  1.1  christos  * Enables/disables EUI-64 address assignment for a pond
   2683  1.1  christos  *
   2684  1.1  christos  * Excecutes statements down to the pond's scope and sets the pond's
   2685  1.1  christos  * use_eui_64 flag accordingly. In addition it iterates over the
   2686  1.1  christos  * pond's pools ensuring they are all /64.  Anything else is deemed
   2687  1.1  christos  * invalid for EUI-64.  It returns the number of invalid pools
   2688  1.1  christos  * detected.  This is done post-parsing as use-eui-64 can be set
   2689  1.1  christos  * down to the pool scope and we can't reliably do it until the
   2690  1.1  christos  * entire configuration has been parsed.
   2691  1.1  christos  */
   2692  1.1  christos int
   2693  1.1  christos set_eui_64(struct ipv6_pond *pond) {
   2694  1.1  christos 	int invalid_cnt = 0;
   2695  1.1  christos 	struct option_state* options = NULL;
   2696  1.1  christos 	struct option_cache *oc = NULL;
   2697  1.1  christos 	option_state_allocate(&options, MDL);
   2698  1.1  christos 	execute_statements_in_scope(NULL, NULL, NULL, NULL, NULL, options,
   2699  1.1  christos 				    &global_scope, pond->group, NULL, NULL);
   2700  1.1  christos 
   2701  1.1  christos 	pond->use_eui_64 =
   2702  1.1  christos 		((oc = lookup_option(&server_universe, options, SV_USE_EUI_64))
   2703  1.1  christos 		 &&
   2704  1.1  christos 		 (evaluate_boolean_option_cache (NULL, NULL, NULL, NULL,
   2705  1.1  christos 						 options, NULL, &global_scope,
   2706  1.1  christos 						 oc, MDL)));
   2707  1.1  christos 	if (pond->use_eui_64) {
   2708  1.1  christos 		// Check all pools are valid
   2709  1.1  christos 		int i = 0;
   2710  1.1  christos 		struct ipv6_pool* p;
   2711  1.1  christos 		while((p = pond->ipv6_pools[i++]) != NULL) {
   2712  1.1  christos 			if (p->bits != 64) {
   2713  1.1  christos 				log_error("Pool %s/%d cannot use EUI-64,"
   2714  1.1  christos 					  " prefix must 64",
   2715  1.1  christos 					  pin6_addr(&p->start_addr), p->bits);
   2716  1.1  christos 				invalid_cnt++;
   2717  1.1  christos 			} else {
   2718  1.1  christos 				log_debug("Pool: %s/%d - will use EUI-64",
   2719  1.1  christos 					  pin6_addr(&p->start_addr), p->bits);
   2720  1.1  christos 			}
   2721  1.1  christos 		}
   2722  1.1  christos 	}
   2723  1.1  christos 
   2724  1.1  christos         /* Don't need the options anymore. */
   2725  1.1  christos         option_state_dereference(&options, MDL);
   2726  1.1  christos 	return (invalid_cnt);
   2727  1.1  christos }
   2728  1.1  christos #endif
   2729  1.1  christos 
   2730  1.1  christos /*
   2731  1.1  christos  * Emits a log for each pond that has been flagged as being a "jumbo range"
   2732  1.1  christos  * A pond is considered a "jumbo range" when the total number of elements
   2733  1.1  christos  * exceeds the maximum value of POND_TRACK_MAX (currently maximum value
   2734  1.1  christos  * that can be stored by ipv6_pond.num_total).  Since we disable threshold
   2735  1.1  christos  * logging for jumbo ranges, we need to report this to the user.  This
   2736  1.1  christos  * function allows us to report jumbo ponds after config parsing, so the
   2737  1.1  christos  * logs can be seen both on the console (-T) and the log facility (i.e syslog).
   2738  1.1  christos  *
   2739  1.1  christos  * Note, threshold logging is done at the pond level, so we need emit a list
   2740  1.1  christos  * of the addresses ranges of the pools in the pond affected.
   2741  1.1  christos  */
   2742  1.1  christos void
   2743  1.1  christos report_jumbo_ranges() {
   2744  1.1  christos 	struct shared_network* s;
   2745  1.1  christos 	char log_buf[1084];
   2746  1.1  christos #ifdef EUI_64
   2747  1.1  christos 	int invalid_cnt = 0;
   2748  1.1  christos #endif
   2749  1.1  christos 
   2750  1.1  christos 	/* Loop thru all the networks looking for jumbo range ponds */
   2751  1.1  christos 	for (s = shared_networks; s; s = s -> next) {
   2752  1.1  christos 		struct ipv6_pond* pond = s->ipv6_pond;
   2753  1.1  christos 		while (pond) {
   2754  1.1  christos #ifdef EUI_64
   2755  1.1  christos 			/* while we're here, set the pond's use_eui_64 flag */
   2756  1.1  christos 			invalid_cnt += set_eui_64(pond);
   2757  1.1  christos #endif
   2758  1.1  christos 			/* if its a jumbo and has pools(sanity check) */
   2759  1.1  christos 			if (pond->jumbo_range == 1 && (pond->ipv6_pools)) {
   2760  1.1  christos 				struct ipv6_pool* pool;
   2761  1.1  christos 				char *bufptr = log_buf;
   2762  1.1  christos 				size_t space_left = sizeof(log_buf) - 1;
   2763  1.1  christos 				int i = 0;
   2764  1.1  christos 				int used = 0;
   2765  1.1  christos 
   2766  1.1  christos 				/* Build list containing the start-address/CIDR
   2767  1.1  christos 				 * of each pool */
   2768  1.1  christos 				*bufptr = '\0';
   2769  1.1  christos 				while ((pool = pond->ipv6_pools[i++]) &&
   2770  1.1  christos 				        (space_left > (INET6_ADDRSTRLEN + 6))) {
   2771  1.1  christos 					/* more than one so add a comma */
   2772  1.1  christos 					if (i > 1) {
   2773  1.1  christos 						*bufptr++ = ',';
   2774  1.1  christos 						*bufptr++ = ' ';
   2775  1.1  christos 						*bufptr = '\0';
   2776  1.1  christos 						space_left -= 2;
   2777  1.1  christos 					}
   2778  1.1  christos 
   2779  1.1  christos 					/* add the address */
   2780  1.1  christos 					inet_ntop(AF_INET6, &pool->start_addr,
   2781  1.1  christos 						  bufptr, INET6_ADDRSTRLEN);
   2782  1.1  christos 
   2783  1.1  christos 					used = strlen(bufptr);
   2784  1.1  christos 					bufptr += used;
   2785  1.1  christos 					space_left -= used;
   2786  1.1  christos 
   2787  1.1  christos 					/* add the CIDR */
   2788  1.1  christos 					sprintf (bufptr, "/%d",pool->bits);
   2789  1.1  christos 					used = strlen(bufptr);
   2790  1.1  christos 					bufptr += used;
   2791  1.1  christos 					space_left -= used;
   2792  1.1  christos 					*bufptr = '\0';
   2793  1.1  christos 				}
   2794  1.1  christos 
   2795  1.1  christos 				log_info("Threshold logging disabled for shared"
   2796  1.1  christos 					 " subnet of ranges: %s", log_buf);
   2797  1.1  christos 			}
   2798  1.1  christos 			pond = pond->next;
   2799  1.1  christos 		}
   2800  1.1  christos 
   2801  1.1  christos 	}
   2802  1.1  christos 
   2803  1.1  christos #ifdef EUI_64
   2804  1.1  christos 	if (invalid_cnt) {
   2805  1.1  christos 		log_fatal ("%d pool(s) are invalid for EUI-64 use",
   2806  1.1  christos 			   invalid_cnt);
   2807  1.1  christos 	}
   2808  1.1  christos #endif
   2809  1.1  christos }
   2810  1.1  christos 
   2811  1.1  christos 
   2812  1.1  christos /*
   2813  1.1  christos  * \brief Tests that 16-bit hardware type is less than 256
   2814  1.1  christos  *
   2815  1.1  christos  * XXX: DHCPv6 gives a 16-bit field for the htype.  DHCPv4 gives an
   2816  1.1  christos  * 8-bit field.  To change the semantics of the generic 'hardware'
   2817  1.1  christos  * structure, we would have to adjust many DHCPv4 sources (from
   2818  1.1  christos  * interface to DHCPv4 lease code), and we would have to update the
   2819  1.1  christos  * 'hardware' config directive (probably being reverse compatible and
   2820  1.1  christos  * providing a new upgrade/replacement primitive).  This is a little
   2821  1.1  christos  * too much to change for now.  Hopefully we will revisit this before
   2822  1.1  christos  * hardware types exceeding 8 bits are assigned.
   2823  1.1  christos  *
   2824  1.1  christos  * Uses a static variable to limit log occurence to once per startup
   2825  1.1  christos  *
   2826  1.1  christos  * \param htype hardware type value to test
   2827  1.1  christos  *
   2828  1.1  christos  * \return returns 0 if the value is too large
   2829  1.1  christos  *
   2830  1.1  christos */
   2831  1.1  christos int htype_bounds_check(uint16_t htype) {
   2832  1.1  christos 	static int log_once = 0;
   2833  1.1  christos 
   2834  1.1  christos 	if (htype & 0xFF00) {
   2835  1.1  christos 		if (!log_once) {
   2836  1.1  christos 			log_error("Attention: At least one client advertises a "
   2837  1.1  christos 			  "hardware type of %d, which exceeds the software "
   2838  1.1  christos 			  "limitation of 255.", htype);
   2839  1.1  christos 			log_once = 1;
   2840  1.1  christos 		}
   2841  1.1  christos 
   2842  1.1  christos 		return(0);
   2843  1.1  christos 	}
   2844  1.1  christos 
   2845  1.1  christos 	return(1);
   2846  1.1  christos }
   2847  1.1  christos 
   2848  1.1  christos /*!
   2849  1.1  christos  * \brief Look for hosts by MAC address if it's available
   2850  1.1  christos  *
   2851  1.1  christos  * Checks the inbound packet against host declarations which specified:
   2852  1.1  christos  *
   2853  1.1  christos  *      "hardware ethernet <MAC>;"
   2854  1.1  christos  *
   2855  1.1  christos  * For directly connected clients, the function will use the MAC address
   2856  1.1  christos  * contained in packet:haddr if it's populated.  \TODO - While the logic is in
   2857  1.1  christos  * place for this search, the socket layer does not yet populate packet:haddr,
   2858  1.1  christos  * this is to be done under rt41523.
   2859  1.1  christos  *
   2860  1.1  christos  * For relayed clients, the function will use the MAC address from the
   2861  1.1  christos  * client-linklayer-address option if it has been supplied by the relay
   2862  1.1  christos  * directly connected to the client.
   2863  1.1  christos  *
   2864  1.1  christos  * \param hp[out] - pointer to storage for the host delcaration if found
   2865  1.1  christos  * \param packet - received packet
   2866  1.1  christos  * \param opt_state - option state to search
   2867  1.1  christos  * \param file - source file
   2868  1.1  christos  * \param line - line number
   2869  1.1  christos  *
   2870  1.1  christos  * \return non-zero if a matching host was found, zero otherwise
   2871  1.1  christos */
   2872  1.1  christos int find_hosts_by_haddr6(struct host_decl **hp,
   2873  1.1  christos 			 struct packet *packet,
   2874  1.1  christos 			 struct option_state *opt_state,
   2875  1.1  christos 			 const char *file, int line) {
   2876  1.1  christos 	int found = 0;
   2877  1.1  christos 	int htype;
   2878  1.1  christos 	int hlen;
   2879  1.1  christos 
   2880  1.1  christos 	/* For directly connected clients, use packet:haddr if populated */
   2881  1.1  christos 	if (packet->dhcpv6_container_packet == NULL) {
   2882  1.1  christos 		if (packet->haddr) {
   2883  1.1  christos 			htype = packet->haddr->hbuf[0];
   2884  1.1  christos 			hlen = packet->haddr->hlen - 1,
   2885  1.1  christos 			log_debug("find_hosts_by_haddr6: using packet->haddr,"
   2886  1.1  christos 				  " type: %d, len: %d", htype, hlen);
   2887  1.1  christos 			found = find_hosts_by_haddr (hp, htype,
   2888  1.1  christos 						     &packet->haddr->hbuf[1],
   2889  1.1  christos 						     hlen, MDL);
   2890  1.1  christos 		}
   2891  1.1  christos 	} else {
   2892  1.1  christos 		/* The first container packet is the from the relay directly
   2893  1.1  christos 		 * connected to the client. Per RFC 6939, that is only relay
   2894  1.1  christos 		 * that may supply the client linklayer address option. */
   2895  1.1  christos 		struct packet *relay_packet = packet->dhcpv6_container_packet;
   2896  1.1  christos 		struct option_state *relay_state = relay_packet->options;
   2897  1.1  christos 		struct data_string rel_addr;
   2898  1.1  christos 		struct option_cache *oc;
   2899  1.1  christos 
   2900  1.1  christos 		/* Look for the option in the first relay packet */
   2901  1.1  christos 		oc = lookup_option(&dhcpv6_universe, relay_state,
   2902  1.1  christos 				   D6O_CLIENT_LINKLAYER_ADDR);
   2903  1.1  christos 		if (!oc) {
   2904  1.1  christos 			/* Not there, so bail */
   2905  1.1  christos 			return (0);
   2906  1.1  christos 		}
   2907  1.1  christos 
   2908  1.1  christos 		/* The option is present, fetch the address data */
   2909  1.1  christos 		memset(&rel_addr, 0, sizeof(rel_addr));
   2910  1.1  christos 		if (!evaluate_option_cache(&rel_addr, relay_packet, NULL, NULL,
   2911  1.1  christos 					   relay_state, NULL, &global_scope,
   2912  1.1  christos 					   oc, MDL)) {
   2913  1.1  christos 			log_error("find_hosts_by_add6:"
   2914  1.1  christos 				  "Error evaluating option cache");
   2915  1.1  christos 			return (0);
   2916  1.1  christos 		}
   2917  1.1  christos 
   2918  1.1  christos 		/* The relay address data should be:
   2919  1.1  christos 		 *   byte 0 - 1 = hardware type
   2920  1.1  christos 		 *   bytes 2 - hlen = hardware address
   2921  1.1  christos                  * where  hlen ( hardware address len) is option data len - 2 */
   2922  1.1  christos 		hlen = rel_addr.len - 2;
   2923  1.1  christos 		if (hlen > 0 && hlen <= HARDWARE_ADDR_LEN) {
   2924  1.1  christos 			htype = getUShort(rel_addr.data);
   2925  1.1  christos 			if (htype_bounds_check(htype)) {
   2926  1.1  christos 				/* Looks valid, let's search with it */
   2927  1.1  christos 				log_debug("find_hosts_by_haddr6:"
   2928  1.1  christos 					  "using relayed haddr"
   2929  1.1  christos 					  " type: %d, len: %d", htype, hlen);
   2930  1.1  christos 				found = find_hosts_by_haddr (hp, htype,
   2931  1.1  christos 							     &rel_addr.data[2],
   2932  1.1  christos 							     hlen, MDL);
   2933  1.1  christos 			}
   2934  1.1  christos 		}
   2935  1.1  christos 
   2936  1.1  christos 		data_string_forget(&rel_addr, MDL);
   2937  1.1  christos         }
   2938  1.1  christos 
   2939  1.1  christos 	return (found);
   2940  1.1  christos }
   2941  1.1  christos 
   2942  1.1  christos /*
   2943  1.1  christos  * find_host_by_duid_chaddr() synthesizes a DHCPv4-like 'hardware'
   2944  1.1  christos  * parameter from a DHCPv6 supplied DUID (client-identifier option),
   2945  1.1  christos  * and may seek to use client or relay supplied hardware addresses.
   2946  1.1  christos  */
   2947  1.1  christos int
   2948  1.1  christos find_hosts_by_duid_chaddr(struct host_decl **host,
   2949  1.1  christos 			  const struct data_string *client_id) {
   2950  1.1  christos 	int htype, hlen;
   2951  1.1  christos 	const unsigned char *chaddr;
   2952  1.1  christos 
   2953  1.1  christos 	/*
   2954  1.1  christos 	 * The DUID-LL and DUID-LLT must have a 2-byte DUID type and 2-byte
   2955  1.1  christos 	 * htype.
   2956  1.1  christos 	 */
   2957  1.1  christos 	if (client_id->len < 4)
   2958  1.1  christos 		return 0;
   2959  1.1  christos 
   2960  1.1  christos 	/*
   2961  1.1  christos 	 * The third and fourth octets of the DUID-LL and DUID-LLT
   2962  1.1  christos 	 * is the hardware type, but in 16 bits.
   2963  1.1  christos 	 */
   2964  1.1  christos 	htype = getUShort(client_id->data + 2);
   2965  1.1  christos 	hlen = 0;
   2966  1.1  christos 	chaddr = NULL;
   2967  1.1  christos 
   2968  1.1  christos 	/* The first two octets of the DUID identify the type. */
   2969  1.1  christos 	switch(getUShort(client_id->data)) {
   2970  1.1  christos 	      case DUID_LLT:
   2971  1.1  christos 		if (client_id->len > 8) {
   2972  1.1  christos 			hlen = client_id->len - 8;
   2973  1.1  christos 			chaddr = client_id->data + 8;
   2974  1.1  christos 		}
   2975  1.1  christos 		break;
   2976  1.1  christos 
   2977  1.1  christos 	      case DUID_LL:
   2978  1.1  christos 		/*
   2979  1.1  christos 		 * Note that client_id->len must be greater than or equal
   2980  1.1  christos 		 * to four to get to this point in the function.
   2981  1.1  christos 		 */
   2982  1.1  christos 		hlen = client_id->len - 4;
   2983  1.1  christos 		chaddr = client_id->data + 4;
   2984  1.1  christos 		break;
   2985  1.1  christos 
   2986  1.1  christos 	      default:
   2987  1.1  christos 		break;
   2988  1.1  christos 	}
   2989  1.1  christos 
   2990  1.1  christos 	if ((hlen == 0) || (hlen > HARDWARE_ADDR_LEN) ||
   2991  1.1  christos 	    !htype_bounds_check(htype)) {
   2992  1.1  christos 		return (0);
   2993  1.1  christos 	}
   2994  1.1  christos 
   2995  1.1  christos 	return find_hosts_by_haddr(host, htype, chaddr, hlen, MDL);
   2996  1.1  christos }
   2997  1.1  christos 
   2998  1.1  christos /*
   2999  1.1  christos  * \brief Finds a host record that matches the packet, if any
   3000  1.1  christos  *
   3001  1.1  christos  * This function centralizes the logic for matching v6 client
   3002  1.1  christos  * packets to host declarations.  We check in the following order
   3003  1.1  christos  * for matches with:
   3004  1.1  christos  *
   3005  1.1  christos  * 1. client_id if specified
   3006  1.1  christos  * 2. MAC address when explicitly available
   3007  1.1  christos  * 3. packet option
   3008  1.1  christos  * 4. synthesized hardware address - this is done last as some
   3009  1.1  christos  * synthesis methods are not consided to be reliable
   3010  1.1  christos  *
   3011  1.1  christos  * \param[out] host - pointer to storage for the located host
   3012  1.1  christos  * \param packet - inbound client packet
   3013  1.1  christos  * \param client_id - client identifier (if one)
   3014  1.1  christos  * \param file - source file
   3015  1.1  christos  * \param line - source file line number
   3016  1.1  christos  * \return non-zero if a host is found, zero otherwise
   3017  1.1  christos */
   3018  1.1  christos int
   3019  1.1  christos find_hosts6(struct host_decl** host, struct packet* packet,
   3020  1.1  christos             const struct data_string* client_id, char* file, int line) {
   3021  1.1  christos         return (find_hosts_by_uid(host, client_id->data, client_id->len, MDL)
   3022  1.1  christos                 || find_hosts_by_haddr6(host, packet, packet->options, MDL)
   3023  1.1  christos                 || find_hosts_by_option(host, packet, packet->options, MDL)
   3024  1.1  christos                 || find_hosts_by_duid_chaddr(host, client_id));
   3025  1.1  christos }
   3026  1.1  christos 
   3027  1.1  christos /* unittest moved to server/tests/mdb6_unittest.c */
   3028