2010-11-05 04:35:57 +03:00
|
|
|
/* $NetBSD: ip_id.c,v 1.14 2010/11/05 01:35:57 rmind Exp $ */
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2010-11-05 01:00:51 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 The NetBSD Foundation, Inc.
|
2003-09-06 07:36:30 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2010-11-05 01:00:51 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
|
2003-09-06 07:36:30 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2010-11-05 01:00:51 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2003-09-06 07:36:30 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-11-05 04:35:57 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ip_id.c,v 1.14 2010/11/05 01:35:57 rmind Exp $");
|
2003-09-06 07:36:30 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2010-11-05 04:35:57 +03:00
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/mutex.h>
|
2003-09-06 07:36:30 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2008-02-06 06:20:50 +03:00
|
|
|
#include <netinet/in_var.h>
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2010-11-05 04:35:57 +03:00
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
|
2008-02-06 06:20:50 +03:00
|
|
|
#define IPID_MAXID 65535
|
|
|
|
#define IPID_NUMIDS 32768
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2010-11-05 04:35:57 +03:00
|
|
|
struct ipid_state {
|
|
|
|
kmutex_t ids_lock;
|
|
|
|
uint16_t ids_start_slot;
|
|
|
|
uint16_t ids_slots[IPID_MAXID];
|
|
|
|
};
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2008-02-06 06:20:50 +03:00
|
|
|
static inline uint32_t
|
|
|
|
ipid_random(void)
|
|
|
|
{
|
|
|
|
return arc4random();
|
2003-09-06 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-02-06 06:20:50 +03:00
|
|
|
* Initalizes the
|
2003-09-06 07:36:30 +04:00
|
|
|
* the msb flag. The msb flag is used to generate two distinct
|
|
|
|
* cycles of random numbers and thus avoiding reuse of ids.
|
|
|
|
*
|
|
|
|
* This function is called from id_randomid() when needed, an
|
|
|
|
* application does not have to worry about it.
|
|
|
|
*/
|
2010-11-05 04:35:57 +03:00
|
|
|
ipid_state_t *
|
|
|
|
ip_id_init(void)
|
2003-09-06 07:36:30 +04:00
|
|
|
{
|
2010-11-05 04:35:57 +03:00
|
|
|
ipid_state_t *ids;
|
2008-02-06 06:20:50 +03:00
|
|
|
size_t i;
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2010-11-05 04:35:57 +03:00
|
|
|
ids = kmem_alloc(sizeof(ipid_state_t), KM_SLEEP);
|
|
|
|
mutex_init(&ids->ids_lock, MUTEX_DEFAULT, IPL_SOFTNET);
|
|
|
|
|
|
|
|
ids->ids_start_slot = ipid_random();
|
|
|
|
for (i = 0; i < __arraycount(ids->ids_slots); i++) {
|
|
|
|
ids->ids_slots[i] = i;
|
|
|
|
}
|
2003-09-06 07:36:30 +04:00
|
|
|
|
|
|
|
/*
|
2008-02-06 06:20:50 +03:00
|
|
|
* Shuffle the array.
|
2003-09-06 07:36:30 +04:00
|
|
|
*/
|
2010-11-05 04:35:57 +03:00
|
|
|
for (i = __arraycount(ids->ids_slots); --i > 0;) {
|
2008-02-06 06:20:50 +03:00
|
|
|
size_t k = ipid_random() % (i + 1);
|
2010-11-05 04:35:57 +03:00
|
|
|
uint16_t t = ids->ids_slots[i];
|
|
|
|
ids->ids_slots[i] = ids->ids_slots[k];
|
|
|
|
ids->ids_slots[k] = t;
|
2003-09-06 07:36:30 +04:00
|
|
|
}
|
2010-11-05 04:35:57 +03:00
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ip_id_fini(ipid_state_t *ids)
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex_destroy(&ids->ids_lock);
|
|
|
|
kmem_free(ids, sizeof(ipid_state_t));
|
2003-09-06 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
2008-02-06 06:20:50 +03:00
|
|
|
uint16_t
|
2010-11-05 04:35:57 +03:00
|
|
|
ip_randomid(ipid_state_t *ids, uint16_t salt)
|
2003-09-06 07:36:30 +04:00
|
|
|
{
|
2008-02-06 06:20:50 +03:00
|
|
|
uint32_t r, k, id;
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2010-11-05 04:35:57 +03:00
|
|
|
/* A random number. */
|
2008-02-06 06:20:50 +03:00
|
|
|
r = ipid_random();
|
2003-09-06 07:36:30 +04:00
|
|
|
|
2008-02-06 06:20:50 +03:00
|
|
|
/*
|
|
|
|
* We do a modified Fisher-Yates shuffle but only one position at a
|
|
|
|
* time. Instead of the last entry, we swap with the first entry and
|
|
|
|
* then advance the start of the window by 1. The next time that
|
|
|
|
* swapped-out entry can be used is at least 32768 iterations in the
|
|
|
|
* future.
|
2010-11-05 04:35:57 +03:00
|
|
|
*
|
2008-02-06 06:20:50 +03:00
|
|
|
* The easiest way to visual this is to imagine a card deck with 52
|
|
|
|
* cards. First thing we do is split that into two sets, each with
|
|
|
|
* half of the cards; call them deck A and deck B. Pick a card
|
|
|
|
* randomly from deck A and remember it, then place it at the
|
|
|
|
* bottom of deck B. Then take the top card from deck B and add it
|
|
|
|
* to deck A. Pick another card randomly from deck A and ...
|
|
|
|
*/
|
2010-11-05 04:35:57 +03:00
|
|
|
mutex_enter(&ids->ids_lock);
|
|
|
|
k = (r & (IPID_NUMIDS - 1)) + ids->ids_start_slot;
|
|
|
|
if (k >= IPID_MAXID) {
|
2008-02-06 06:20:50 +03:00
|
|
|
k -= IPID_MAXID;
|
|
|
|
}
|
2010-11-05 04:35:57 +03:00
|
|
|
id = ids->ids_slots[k];
|
|
|
|
if (k != ids->ids_start_slot) {
|
|
|
|
ids->ids_slots[k] = ids->ids_slots[ids->ids_start_slot];
|
|
|
|
ids->ids_slots[ids->ids_start_slot] = id;
|
|
|
|
}
|
|
|
|
if (++ids->ids_start_slot == IPID_MAXID) {
|
|
|
|
ids->ids_start_slot = 0;
|
|
|
|
}
|
|
|
|
mutex_exit(&ids->ids_lock);
|
|
|
|
|
2008-02-06 06:20:50 +03:00
|
|
|
/*
|
|
|
|
* Add an optional salt to the id to further obscure it.
|
|
|
|
*/
|
|
|
|
id += salt;
|
2010-11-05 04:35:57 +03:00
|
|
|
if (id >= IPID_MAXID) {
|
2008-02-06 06:20:50 +03:00
|
|
|
id -= IPID_MAXID;
|
2010-11-05 04:35:57 +03:00
|
|
|
}
|
|
|
|
return (uint16_t)htons(id + 1);
|
2003-09-06 07:36:30 +04:00
|
|
|
}
|