blob: efb274bd81a37e4bd5c50c65179c38af0f1b6598 [file] [log] [blame]
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * IP address pool functions.
jjakoc3213962004-09-09 20:23:50 +00003 * Copyright (C) 2003, 2004 Mondru AB.
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02004 *
jjakoa7cd2492003-04-11 09:40:12 +00005 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02009 *
jjakoa7cd2492003-04-11 09:40:12 +000010 */
11
12#ifndef _IPPOOL_H
13#define _IPPOOL_H
14
Harald Welted12eab92017-08-02 19:49:47 +020015#include "../lib/in46_addr.h"
Harald Welted1bf1e12017-08-03 00:00:23 +020016#include "../gtp/gtp.h"
Harald Welted12eab92017-08-02 19:49:47 +020017
jjakoa7cd2492003-04-11 09:40:12 +000018/* Assuming that the address space is fragmented we need a hash table
19 in order to return the addresses.
20
21 The list pool should provide for both IPv4 and IPv6 addresses.
22
23 When initialising a new address pool it should be possible to pass
24 a string of CIDR format networks: "10.0.0.0/24 10.15.0.0/20" would
25 translate to 256 addresses starting at 10.0.0.0 and 1024 addresses
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +020026 starting at 10.15.0.0.
jjakoa7cd2492003-04-11 09:40:12 +000027
28 The above also applies to IPv6 which can be specified as described
29 in RFC2373.
30*/
31
jjakoa7cd2492003-04-11 09:40:12 +000032#define IPPOOL_NONETWORK 0x01
33#define IPPOOL_NOBROADCAST 0x02
34
jjako88c22162003-07-06 19:33:18 +000035#define IPPOOL_STATSIZE 0x10000
36
Harald Weltebed35df2011-11-02 13:06:18 +010037struct ippoolm_t; /* Forward declaration */
jjakoa7cd2492003-04-11 09:40:12 +000038
39struct ippool_t {
Harald Weltebed35df2011-11-02 13:06:18 +010040 unsigned int listsize; /* Total number of addresses */
41 int allowdyn; /* Allow dynamic IP address allocation */
42 int allowstat; /* Allow static IP address allocation */
Harald Welted12eab92017-08-02 19:49:47 +020043 struct in46_addr stataddr; /* Static address range network address */
44 size_t stataddrprefixlen; /* IPv6 prefix length of stataddr */
Harald Weltebed35df2011-11-02 13:06:18 +010045 struct ippoolm_t *member; /* Listsize array of members */
46 unsigned int hashsize; /* Size of hash table */
47 int hashlog; /* Log2 size of hash table */
48 int hashmask; /* Bitmask for calculating hash */
49 struct ippoolm_t **hash; /* Hashsize array of pointer to member */
50 struct ippoolm_t *firstdyn; /* Pointer to first free dynamic member */
51 struct ippoolm_t *lastdyn; /* Pointer to last free dynamic member */
52 struct ippoolm_t *firststat; /* Pointer to first free static member */
53 struct ippoolm_t *laststat; /* Pointer to last free static member */
jjakoa7cd2492003-04-11 09:40:12 +000054};
55
56struct ippoolm_t {
Harald Welted12eab92017-08-02 19:49:47 +020057 struct in46_addr addr; /* IP address of this member */
Harald Welteb513b952017-08-12 12:46:39 +020058 struct ippool_t *pool; /* Pool to which we belong */
Harald Weltebed35df2011-11-02 13:06:18 +010059 int inuse; /* 0=available; 1= dynamic; 2 = static */
60 struct ippoolm_t *nexthash; /* Linked list part of hash table */
61 struct ippoolm_t *prev, *next; /* Linked list of free dynamic or static */
62 void *peer; /* Pointer to peer protocol handler */
jjakoa7cd2492003-04-11 09:40:12 +000063};
64
jjako88c22162003-07-06 19:33:18 +000065/* The above structures require approximately 20+4 = 24 bytes for
jjakoa7cd2492003-04-11 09:40:12 +000066 each address (IPv4). For IPv6 the corresponding value is 32+4 = 36
67 bytes for each address. */
68
69/* Hash an IP address using code based on Bob Jenkins lookupa */
Harald Welted12eab92017-08-02 19:49:47 +020070extern unsigned long int ippool_hash(struct in46_addr *addr);
jjakoa7cd2492003-04-11 09:40:12 +000071
72/* Create new address pool */
Harald Welte4857f3c2017-08-12 12:55:04 +020073extern int ippool_new(struct ippool_t **this, const struct in46_prefix *dyn,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +020074 const struct in46_prefix *stat, int flags,
75 struct in46_prefix *blacklist, size_t blacklist_size);
jjakoa7cd2492003-04-11 09:40:12 +000076
77/* Delete existing address pool */
78extern int ippool_free(struct ippool_t *this);
79
80/* Find an IP address in the pool */
81extern int ippool_getip(struct ippool_t *this, struct ippoolm_t **member,
Harald Welted12eab92017-08-02 19:49:47 +020082 struct in46_addr *addr);
jjakoa7cd2492003-04-11 09:40:12 +000083
84/* Get an IP address. If addr = 0.0.0.0 get a dynamic IP address. Otherwise
85 check to see if the given address is available */
86extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member,
Harald Welted12eab92017-08-02 19:49:47 +020087 struct in46_addr *addr, int statip);
jjakoa7cd2492003-04-11 09:40:12 +000088
89/* Return a previously allocated IP address */
jjako88c22162003-07-06 19:33:18 +000090extern int ippool_freeip(struct ippool_t *this, struct ippoolm_t *member);
jjakoa7cd2492003-04-11 09:40:12 +000091
92/* Get net and mask based on ascii string */
Harald Welted12eab92017-08-02 19:49:47 +020093int ippool_aton(struct in46_addr *addr, size_t *prefixlen, const char *pool, int number);
jjakoa7cd2492003-04-11 09:40:12 +000094
Harald Welted12eab92017-08-02 19:49:47 +020095/* Increase IPv4/IPv6 address by 1 */
96extern void in46a_inc(struct in46_addr *addr);
jjakoa7cd2492003-04-11 09:40:12 +000097
Harald Weltebed35df2011-11-02 13:06:18 +010098#endif /* !_IPPOOL_H */