jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IP address pool functions. |
jjako | c321396 | 2004-09-09 20:23:50 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003, 2004 Mondru AB. |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 4 | * |
| 5 | * 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. |
| 9 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef _IPPOOL_H |
| 13 | #define _IPPOOL_H |
| 14 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 15 | #include "../lib/in46_addr.h" |
Harald Welte | d1bf1e1 | 2017-08-03 00:00:23 +0200 | [diff] [blame] | 16 | #include "../gtp/gtp.h" |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 17 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 18 | /* 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 |
| 26 | starting at 10.15.0.0. |
| 27 | |
| 28 | The above also applies to IPv6 which can be specified as described |
| 29 | in RFC2373. |
| 30 | */ |
| 31 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 32 | #define IPPOOL_NONETWORK 0x01 |
| 33 | #define IPPOOL_NOBROADCAST 0x02 |
jjako | 504ee45 | 2003-08-20 15:25:54 +0000 | [diff] [blame] | 34 | #define IPPOOL_NOGATEWAY 0x04 |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 35 | |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 36 | #define IPPOOL_STATSIZE 0x10000 |
| 37 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 38 | struct ippoolm_t; /* Forward declaration */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 39 | |
| 40 | struct ippool_t { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 41 | unsigned int listsize; /* Total number of addresses */ |
| 42 | int allowdyn; /* Allow dynamic IP address allocation */ |
| 43 | int allowstat; /* Allow static IP address allocation */ |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 44 | struct in46_addr stataddr; /* Static address range network address */ |
| 45 | size_t stataddrprefixlen; /* IPv6 prefix length of stataddr */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 46 | struct ippoolm_t *member; /* Listsize array of members */ |
| 47 | unsigned int hashsize; /* Size of hash table */ |
| 48 | int hashlog; /* Log2 size of hash table */ |
| 49 | int hashmask; /* Bitmask for calculating hash */ |
| 50 | struct ippoolm_t **hash; /* Hashsize array of pointer to member */ |
| 51 | struct ippoolm_t *firstdyn; /* Pointer to first free dynamic member */ |
| 52 | struct ippoolm_t *lastdyn; /* Pointer to last free dynamic member */ |
| 53 | struct ippoolm_t *firststat; /* Pointer to first free static member */ |
| 54 | struct ippoolm_t *laststat; /* Pointer to last free static member */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct ippoolm_t { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 58 | struct in46_addr addr; /* IP address of this member */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 59 | 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 */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 65 | /* The above structures require approximately 20+4 = 24 bytes for |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 66 | 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 Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 70 | extern unsigned long int ippool_hash(struct in46_addr *addr); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 71 | |
| 72 | /* Create new address pool */ |
Harald Welte | 53165ed | 2017-08-02 22:58:54 +0200 | [diff] [blame] | 73 | extern int ippool_new(struct ippool_t **this, const char *dyn, const char *stat, |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 74 | int allowdyn, int allowstat, int flags); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 75 | |
| 76 | /* Delete existing address pool */ |
| 77 | extern int ippool_free(struct ippool_t *this); |
| 78 | |
| 79 | /* Find an IP address in the pool */ |
| 80 | extern int ippool_getip(struct ippool_t *this, struct ippoolm_t **member, |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 81 | struct in46_addr *addr); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 82 | |
| 83 | /* Get an IP address. If addr = 0.0.0.0 get a dynamic IP address. Otherwise |
| 84 | check to see if the given address is available */ |
| 85 | extern int ippool_newip(struct ippool_t *this, struct ippoolm_t **member, |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 86 | struct in46_addr *addr, int statip); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 87 | |
| 88 | /* Return a previously allocated IP address */ |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 89 | extern int ippool_freeip(struct ippool_t *this, struct ippoolm_t *member); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* Get net and mask based on ascii string */ |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 92 | int ippool_aton(struct in46_addr *addr, size_t *prefixlen, const char *pool, int number); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 93 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 94 | /* Increase IPv4/IPv6 address by 1 */ |
| 95 | extern void in46a_inc(struct in46_addr *addr); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 96 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 97 | #endif /* !_IPPOOL_H */ |