├── .dockerignore ├── Dockerfile ├── LICENSE.txt ├── README.md ├── docker-compose.yml ├── kea-dhcp4.conf └── mysql └── dhcpdb_create.sql /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | LICENSE.txt 3 | README.md 4 | docker-compose.yml 5 | kea-dhcp4.conf 6 | mysql/ 7 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest as builder 2 | 3 | ARG KEA_DHCP_VERSION=1.3.0 4 | ARG LOG4_CPLUS_VERSION=1.2.1 5 | 6 | RUN apk add --no-cache --virtual .build-deps \ 7 | alpine-sdk \ 8 | bash \ 9 | boost-dev \ 10 | bzip2-dev \ 11 | file \ 12 | libressl-dev \ 13 | mariadb-dev \ 14 | zlib-dev && \ 15 | curl -sL https://sourceforge.net/projects/log4cplus/files/log4cplus-stable/${LOG4_CPLUS_VERSION}/log4cplus-${LOG4_CPLUS_VERSION}.tar.gz | tar -zx -C /tmp && \ 16 | cd /tmp/log4cplus-${LOG4_CPLUS_VERSION} && \ 17 | ./configure && \ 18 | make -s -j$(nproc) && \ 19 | make install && \ 20 | curl -sL https://ftp.isc.org/isc/kea/${KEA_DHCP_VERSION}/kea-${KEA_DHCP_VERSION}.tar.gz | tar -zx -C /tmp && \ 21 | cd /tmp/kea-${KEA_DHCP_VERSION} && \ 22 | ./configure \ 23 | --enable-shell \ 24 | --with-dhcp-mysql=/usr/bin/mysql_config && \ 25 | make -s -j$(nproc) && \ 26 | make install-strip && \ 27 | apk del --purge .build-deps && \ 28 | rm -rf /tmp/* 29 | 30 | FROM alpine:latest 31 | LABEL maintainer "mhiro2 " 32 | 33 | RUN apk --no-cache add \ 34 | bash \ 35 | boost \ 36 | bzip2 \ 37 | libressl \ 38 | mariadb-client-libs \ 39 | zlib 40 | 41 | COPY --from=builder /usr/local /usr/local/ 42 | 43 | ENTRYPOINT ["/usr/local/sbin/kea-dhcp4"] 44 | CMD ["-c", "/usr/local/etc/kea/kea-dhcp4.conf"] 45 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2017 Masaaki Hirotsu 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 16 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-kea-dhcp-server 2 | 3 | [![MIT License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](https://github.com/mhiro2/docker-kea-dhcp-server/blob/master/LICENSE.txt) 4 | 5 | ISC Kea DHCP server on Docker. 6 | 7 | ## Usage 8 | 9 | You must edit `kea-dhcp4.conf` file before run it. 10 | 11 | ``` 12 | $ docker-compose up -d 13 | ``` 14 | 15 | ## License 16 | 17 | MIT 18 | 19 | ## Author Information 20 | 21 | [Masaaki Hirotsu]() 22 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | kea: 5 | #build: . 6 | image: mhiro2/kea-dhcp-server:latest 7 | volumes: 8 | - /etc/localtime:/etc/localtime:ro 9 | - ./kea-dhcp4.conf:/usr/local/etc/kea/kea-dhcp4.conf 10 | network_mode: host 11 | depends_on: 12 | - kea-db 13 | restart: always 14 | 15 | kea-db: 16 | image: mysql:5.7 17 | ports: 18 | - 127.0.0.1:3306:3306 19 | volumes: 20 | - /etc/localtime:/etc/localtime:ro 21 | - kea-db_data:/var/lib/mysql 22 | - ./mysql:/docker-entrypoint-initdb.d 23 | environment: 24 | MYSQL_DATABASE: dhcpdb 25 | MYSQL_ROOT_PASSWORD: dhcpdb 26 | restart: always 27 | 28 | volumes: 29 | kea-db_data: 30 | 31 | -------------------------------------------------------------------------------- /kea-dhcp4.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Dhcp4": { 3 | "interfaces-config": { 4 | "interfaces": [ 5 | "*" 6 | ], 7 | "dhcp-socket-type": "raw" 8 | }, 9 | "lease-database": { 10 | "type": "mysql", 11 | "name": "dhcpdb", 12 | "host": "127.0.0.1", 13 | "user": "root", 14 | "password": "dhcpdb" 15 | }, 16 | "valid-lifetime": 4000, 17 | "subnet4": [ 18 | { 19 | "subnet": "192.168.10.0/24", 20 | "pools": [ 21 | { 22 | "pool": "192.168.10.10 - 192.168.10.100" 23 | } 24 | ], 25 | "option-data": [ 26 | { 27 | "name": "routers", 28 | "data": "192.168.10.1" 29 | }, 30 | { 31 | "name": "domain-name-servers", 32 | "data": "1.1.1.1" 33 | } 34 | ] 35 | } 36 | ] 37 | }, 38 | "Logging": { 39 | "loggers": [ 40 | { 41 | "name": "kea-dhcp4", 42 | "output_options": [ 43 | { 44 | "output": "stdout" 45 | } 46 | ], 47 | "severity": "INFO" 48 | } 49 | ] 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /mysql/dhcpdb_create.sql: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") 2 | # 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | 7 | # This is the Kea schema specification for MySQL. 8 | # 9 | # The schema is reasonably portable (with the exception of the engine 10 | # specification, which is MySQL-specific). Minor changes might be needed for 11 | # other databases. 12 | 13 | # To create the schema, either type the command: 14 | # 15 | # mysql -u -p < dhcpdb_create.mysql 16 | # 17 | # ... at the command prompt, or log in to the MySQL database and at the 'mysql>' 18 | # prompt, issue the command: 19 | # 20 | # source dhcpdb_create.mysql 21 | # 22 | # This script is also called from kea-admin, see kea-admin lease-init mysql 23 | # 24 | # Over time, Kea database schema will evolve. Each version is marked with 25 | # major.minor version. This file is organized sequentially, i.e. database 26 | # is initialized to 1.0, then upgraded to 2.0 etc. This may be somewhat 27 | # sub-optimal, but it ensues consistency with upgrade scripts. (It is much 28 | # easier to maintain init and upgrade scripts if they look the same). 29 | # Since initialization is done only once, it's performance is not an issue. 30 | 31 | # This line starts database initialization to 1.0. 32 | 33 | # Holds the IPv4 leases. 34 | CREATE TABLE lease4 ( 35 | address INT UNSIGNED PRIMARY KEY NOT NULL, # IPv4 address 36 | hwaddr VARBINARY(20), # Hardware address 37 | client_id VARBINARY(128), # Client ID 38 | valid_lifetime INT UNSIGNED, # Length of the lease (seconds) 39 | expire TIMESTAMP, # Expiration time of the lease 40 | subnet_id INT UNSIGNED, # Subnet identification 41 | fqdn_fwd BOOL, # Has forward DNS update been performed by a server 42 | fqdn_rev BOOL, # Has reverse DNS update been performed by a server 43 | hostname VARCHAR(255) # The FQDN of the client 44 | ) ENGINE = INNODB; 45 | 46 | 47 | # Create search indexes for lease4 table 48 | # index by hwaddr and subnet_id 49 | CREATE INDEX lease4_by_hwaddr_subnet_id ON lease4 (hwaddr, subnet_id); 50 | 51 | # index by client_id and subnet_id 52 | CREATE INDEX lease4_by_client_id_subnet_id ON lease4 (client_id, subnet_id); 53 | 54 | # Holds the IPv6 leases. 55 | # N.B. The use of a VARCHAR for the address is temporary for development: 56 | # it will eventually be replaced by BINARY(16). 57 | CREATE TABLE lease6 ( 58 | address VARCHAR(39) PRIMARY KEY NOT NULL, # IPv6 address 59 | duid VARBINARY(128), # DUID 60 | valid_lifetime INT UNSIGNED, # Length of the lease (seconds) 61 | expire TIMESTAMP, # Expiration time of the lease 62 | subnet_id INT UNSIGNED, # Subnet identification 63 | pref_lifetime INT UNSIGNED, # Preferred lifetime 64 | lease_type TINYINT, # Lease type (see lease6_types 65 | # table for possible values) 66 | iaid INT UNSIGNED, # See Section 10 of RFC 3315 67 | prefix_len TINYINT UNSIGNED, # For IA_PD only 68 | fqdn_fwd BOOL, # Has forward DNS update been performed by a server 69 | fqdn_rev BOOL, # Has reverse DNS update been performed by a server 70 | hostname VARCHAR(255) # The FQDN of the client 71 | 72 | ) ENGINE = INNODB; 73 | 74 | # Create search indexes for lease4 table 75 | # index by iaid, subnet_id, and duid 76 | CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid); 77 | 78 | # ... and a definition of lease6 types. This table is a convenience for 79 | # users of the database - if they want to view the lease table and use the 80 | # type names, they can join this table with the lease6 table. 81 | # Make sure those values match Lease6::LeaseType enum (see src/bin/dhcpsrv/ 82 | # lease_mgr.h) 83 | CREATE TABLE lease6_types ( 84 | lease_type TINYINT PRIMARY KEY NOT NULL, # Lease type code. 85 | name VARCHAR(5) # Name of the lease type 86 | ) ENGINE = INNODB; 87 | 88 | START TRANSACTION; 89 | INSERT INTO lease6_types VALUES (0, 'IA_NA'); # Non-temporary v6 addresses 90 | INSERT INTO lease6_types VALUES (1, 'IA_TA'); # Temporary v6 addresses 91 | INSERT INTO lease6_types VALUES (2, 'IA_PD'); # Prefix delegations 92 | COMMIT; 93 | 94 | # Finally, the version of the schema. We start at 1.0 during development. 95 | # This table is only modified during schema upgrades. For historical reasons 96 | # (related to the names of the columns in the BIND 10 DNS database file), the 97 | # first column is called 'version' and not 'major'. 98 | CREATE TABLE schema_version ( 99 | version INT PRIMARY KEY NOT NULL, # Major version number 100 | minor INT # Minor version number 101 | ) ENGINE = INNODB; 102 | START TRANSACTION; 103 | INSERT INTO schema_version VALUES (1, 0); 104 | COMMIT; 105 | 106 | # This line concludes database initialization to version 1.0. 107 | 108 | # This line starts database upgrade to version 2.0. 109 | ALTER TABLE lease6 110 | ADD COLUMN hwaddr varbinary(20), # Hardware/MAC address, typically only 6 111 | # bytes is used, but some hardware (e.g. 112 | # Infiniband) use up to 20. 113 | ADD COLUMN hwtype smallint unsigned, # hardware type (16 bits) 114 | ADD COLUMN hwaddr_source int unsigned; # Hardware source. See description 115 | # of lease_hwaddr_source below. 116 | 117 | # Kea keeps track of the hardware/MAC address source, i.e. how the address 118 | # was obtained. Depending on the technique and your network topology, it may 119 | # be more or less trustworthy. This table is a convenience for 120 | # users of the database - if they want to view the lease table and use the 121 | # type names, they can join this table with the lease6 table. For details, 122 | # see constants defined in src/lib/dhcp/dhcp/pkt.h for detailed explanation. 123 | CREATE TABLE lease_hwaddr_source ( 124 | hwaddr_source INT PRIMARY KEY NOT NULL, 125 | name VARCHAR(40) 126 | ) ENGINE = INNODB; 127 | 128 | # Hardware address obtained from raw sockets 129 | INSERT INTO lease_hwaddr_source VALUES (1, 'HWADDR_SOURCE_RAW'); 130 | 131 | # Hardware address converted from IPv6 link-local address with EUI-64 132 | INSERT INTO lease_hwaddr_source VALUES (2, 'HWADDR_SOURCE_IPV6_LINK_LOCAL'); 133 | 134 | # Hardware address extracted from client-id (duid) 135 | INSERT INTO lease_hwaddr_source VALUES (4, 'HWADDR_SOURCE_DUID'); 136 | 137 | # Hardware address extracted from client address relay option (RFC6939) 138 | INSERT INTO lease_hwaddr_source VALUES (8, 'HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION'); 139 | 140 | # Hardware address extracted from remote-id option (RFC4649) 141 | INSERT INTO lease_hwaddr_source VALUES (16, 'HWADDR_SOURCE_REMOTE_ID'); 142 | 143 | # Hardware address extracted from subscriber-id option (RFC4580) 144 | INSERT INTO lease_hwaddr_source VALUES (32, 'HWADDR_SOURCE_SUBSCRIBER_ID'); 145 | 146 | # Hardware address extracted from docsis options 147 | INSERT INTO lease_hwaddr_source VALUES (64, 'HWADDR_SOURCE_DOCSIS'); 148 | 149 | UPDATE schema_version SET version='2', minor='0'; 150 | 151 | # This line concludes database upgrade to version 2.0. 152 | 153 | # This line starts database upgrade to version 3.0. 154 | # Upgrade extending MySQL schema with the ability to store hosts. 155 | 156 | CREATE TABLE IF NOT EXISTS hosts ( 157 | host_id INT UNSIGNED NOT NULL AUTO_INCREMENT, 158 | dhcp_identifier VARBINARY(128) NOT NULL, 159 | dhcp_identifier_type TINYINT NOT NULL, 160 | dhcp4_subnet_id INT UNSIGNED NULL, 161 | dhcp6_subnet_id INT UNSIGNED NULL, 162 | ipv4_address INT UNSIGNED NULL, 163 | hostname VARCHAR(255) NULL, 164 | dhcp4_client_classes VARCHAR(255) NULL, 165 | dhcp6_client_classes VARCHAR(255) NULL, 166 | PRIMARY KEY (host_id), 167 | INDEX key_dhcp4_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC), 168 | INDEX key_dhcp6_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp6_subnet_id ASC) 169 | ) ENGINE=INNODB; 170 | -- ----------------------------------------------------- 171 | -- Table `ipv6_reservations` 172 | -- ----------------------------------------------------- 173 | CREATE TABLE IF NOT EXISTS ipv6_reservations ( 174 | reservation_id INT NOT NULL AUTO_INCREMENT, 175 | address VARCHAR(39) NOT NULL, 176 | prefix_len TINYINT(3) UNSIGNED NOT NULL DEFAULT 128, 177 | type TINYINT(4) UNSIGNED NOT NULL DEFAULT 0, 178 | dhcp6_iaid INT UNSIGNED NULL, 179 | host_id INT UNSIGNED NOT NULL, 180 | PRIMARY KEY (reservation_id), 181 | INDEX fk_ipv6_reservations_host_idx (host_id ASC), 182 | CONSTRAINT fk_ipv6_reservations_Host FOREIGN KEY (host_id) 183 | REFERENCES hosts (host_id) 184 | ON DELETE NO ACTION ON UPDATE NO ACTION 185 | ) ENGINE=INNODB; 186 | -- ----------------------------------------------------- 187 | -- Table `dhcp4_options` 188 | -- ----------------------------------------------------- 189 | CREATE TABLE IF NOT EXISTS dhcp4_options ( 190 | option_id INT UNSIGNED NOT NULL AUTO_INCREMENT, 191 | code TINYINT UNSIGNED NOT NULL, 192 | value BLOB NULL, 193 | formatted_value TEXT NULL, 194 | space VARCHAR(128) NULL, 195 | persistent TINYINT(1) NOT NULL DEFAULT 0, 196 | dhcp_client_class VARCHAR(128) NULL, 197 | dhcp4_subnet_id INT NULL, 198 | host_id INT UNSIGNED NULL, 199 | PRIMARY KEY (option_id), 200 | UNIQUE INDEX option_id_UNIQUE (option_id ASC), 201 | INDEX fk_options_host1_idx (host_id ASC), 202 | CONSTRAINT fk_options_host1 FOREIGN KEY (host_id) 203 | REFERENCES hosts (host_id) 204 | ON DELETE NO ACTION ON UPDATE NO ACTION 205 | ) ENGINE=INNODB; 206 | -- ----------------------------------------------------- 207 | -- Table `dhcp6_options` 208 | -- ----------------------------------------------------- 209 | CREATE TABLE IF NOT EXISTS dhcp6_options ( 210 | option_id INT UNSIGNED NOT NULL AUTO_INCREMENT, 211 | code INT UNSIGNED NOT NULL, 212 | value BLOB NULL, 213 | formatted_value TEXT NULL, 214 | space VARCHAR(128) NULL, 215 | persistent TINYINT(1) NOT NULL DEFAULT 0, 216 | dhcp_client_class VARCHAR(128) NULL, 217 | dhcp6_subnet_id INT NULL, 218 | host_id INT UNSIGNED NULL, 219 | PRIMARY KEY (option_id), 220 | UNIQUE INDEX option_id_UNIQUE (option_id ASC), 221 | INDEX fk_options_host1_idx (host_id ASC), 222 | CONSTRAINT fk_options_host10 FOREIGN KEY (host_id) 223 | REFERENCES hosts (host_id) 224 | ON DELETE NO ACTION ON UPDATE NO ACTION 225 | ) ENGINE=INNODB; 226 | 227 | DELIMITER $$ 228 | CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW 229 | -- Edit trigger body code below this line. Do not edit lines above this one 230 | BEGIN 231 | DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id; 232 | END 233 | $$ 234 | DELIMITER ; 235 | 236 | UPDATE schema_version 237 | SET version = '3', minor = '0'; 238 | # This line concludes database upgrade to version 3.0. 239 | 240 | # This line starts database upgrade to version 4.0. 241 | # Upgrade extending MySQL schema with the state columns for lease tables. 242 | 243 | # Add state column to the lease4 table. 244 | ALTER TABLE lease4 245 | ADD COLUMN state INT UNSIGNED DEFAULT 0; 246 | 247 | # Add state column to the lease6 table. 248 | ALTER TABLE lease6 249 | ADD COLUMN state INT UNSIGNED DEFAULT 0; 250 | 251 | # Create indexes for querying leases in a given state and segregated 252 | # by the expiration time. One of the applications is to retrieve all 253 | # expired leases. However, these indexes can be also used to retrieve 254 | # leases in a given state regardless of the expiration time. 255 | CREATE INDEX lease4_by_state_expire ON lease4 (state ASC, expire ASC); 256 | CREATE INDEX lease6_by_state_expire ON lease6 (state ASC, expire ASC); 257 | 258 | # Create table holding mapping of the lease states to their names. 259 | # This is not used in queries from the DHCP server but rather in 260 | # direct queries from the lease database management tools. 261 | CREATE TABLE IF NOT EXISTS lease_state ( 262 | state INT UNSIGNED PRIMARY KEY NOT NULL, 263 | name VARCHAR(64) NOT NULL 264 | ) ENGINE=INNODB; 265 | 266 | # Insert currently defined state names. 267 | INSERT INTO lease_state VALUES (0, 'default'); 268 | INSERT INTO lease_state VALUES (1, 'declined'); 269 | INSERT INTO lease_state VALUES (2, 'expired-reclaimed'); 270 | 271 | # Add a constraint that any state value added to the lease4 must 272 | # map to a value in the lease_state table. 273 | ALTER TABLE lease4 274 | ADD CONSTRAINT fk_lease4_state FOREIGN KEY (state) 275 | REFERENCES lease_state (state); 276 | 277 | # Add a constraint that any state value added to the lease6 must 278 | # map to a value in the lease_state table. 279 | ALTER TABLE lease6 280 | ADD CONSTRAINT fk_lease6_state FOREIGN KEY (state) 281 | REFERENCES lease_state (state); 282 | 283 | # Add a constraint that lease type in the lease6 table must map 284 | # to a lease type defined in the lease6_types table. 285 | ALTER TABLE lease6 286 | ADD CONSTRAINT fk_lease6_type FOREIGN KEY (lease_type) 287 | REFERENCES lease6_types (lease_type); 288 | 289 | # Modify the name of one of the HW address sources, and add a new one. 290 | UPDATE lease_hwaddr_source 291 | SET name = 'HWADDR_SOURCE_DOCSIS_CMTS' 292 | WHERE hwaddr_source = 64; 293 | 294 | INSERT INTO lease_hwaddr_source VALUES (128, 'HWADDR_SOURCE_DOCSIS_MODEM'); 295 | 296 | # Add UNSIGNED to match with the lease6. 297 | ALTER TABLE lease_hwaddr_source 298 | MODIFY COLUMN hwaddr_source INT UNSIGNED NOT NULL; 299 | 300 | # Add a constraint that non-null hwaddr_source in the lease6 table 301 | # must map to an entry in the lease_hwaddr_source. 302 | ALTER TABLE lease6 303 | ADD CONSTRAINT fk_lease6_hwaddr_source FOREIGN KEY (hwaddr_source) 304 | REFERENCES lease_hwaddr_source (hwaddr_source); 305 | 306 | # FUNCTION that returns a result set containing the column names for lease4 dumps 307 | DROP PROCEDURE IF EXISTS lease4DumpHeader; 308 | DELIMITER $$ 309 | CREATE PROCEDURE lease4DumpHeader() 310 | BEGIN 311 | SELECT 'address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname,state'; 312 | END $$ 313 | DELIMITER ; 314 | 315 | # FUNCTION that returns a result set containing the data for lease4 dumps 316 | DROP PROCEDURE IF EXISTS lease4DumpData; 317 | DELIMITER $$ 318 | CREATE PROCEDURE lease4DumpData() 319 | BEGIN 320 | SELECT 321 | INET_NTOA(l.address), 322 | IFNULL(HEX(l.hwaddr), ''), 323 | IFNULL(HEX(l.client_id), ''), 324 | l.valid_lifetime, 325 | l.expire, 326 | l.subnet_id, 327 | l.fqdn_fwd, 328 | l.fqdn_rev, 329 | l.hostname, 330 | s.name 331 | FROM 332 | lease4 l 333 | LEFT OUTER JOIN lease_state s on (l.state = s.state) 334 | ORDER BY l.address; 335 | END $$ 336 | DELIMITER ; 337 | 338 | # FUNCTION that returns a result set containing the column names for lease6 dumps 339 | DROP PROCEDURE IF EXISTS lease6DumpHeader; 340 | DELIMITER $$ 341 | CREATE PROCEDURE lease6DumpHeader() 342 | BEGIN 343 | SELECT 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,hwtype,hwaddr_source,state'; 344 | END $$ 345 | DELIMITER ; 346 | 347 | # FUNCTION that returns a result set containing the data for lease6 dumps 348 | DROP PROCEDURE IF EXISTS lease6DumpData; 349 | DELIMITER $$ 350 | CREATE PROCEDURE lease6DumpData() 351 | BEGIN 352 | SELECT 353 | l.address, 354 | IFNULL(HEX(l.duid), ''), 355 | l.valid_lifetime, 356 | l.expire, 357 | l.subnet_id, 358 | l.pref_lifetime, 359 | IFNULL(t.name, ''), 360 | l.iaid, 361 | l.prefix_len, 362 | l.fqdn_fwd, 363 | l.fqdn_rev, 364 | l.hostname, 365 | IFNULL(HEX(l.hwaddr), ''), 366 | IFNULL(l.hwtype, ''), 367 | IFNULL(h.name, ''), 368 | IFNULL(s.name, '') 369 | FROM lease6 l 370 | left outer join lease6_types t on (l.lease_type = t.lease_type) 371 | left outer join lease_state s on (l.state = s.state) 372 | left outer join lease_hwaddr_source h on (l.hwaddr_source = h.hwaddr_source) 373 | ORDER BY l.address; 374 | END $$ 375 | DELIMITER ; 376 | 377 | # Update the schema version number 378 | UPDATE schema_version 379 | SET version = '4', minor = '0'; 380 | 381 | # This line concludes database upgrade to version 4.0. 382 | 383 | # In the event hardware address cannot be determined, we need to satisfy 384 | # foreign key constraint between lease6 and lease_hardware_source 385 | INSERT INTO lease_hwaddr_source VALUES (0, 'HWADDR_SOURCE_UNKNOWN'); 386 | 387 | # Update the schema version number 388 | UPDATE schema_version 389 | SET version = '4', minor = '1'; 390 | 391 | # This line concludes database upgrade to version 4.1. 392 | 393 | # Update index used for searching DHCPv4 reservations by identifier and subnet id. 394 | # This index is now unique (to prevent duplicates) and includes DHCPv4 subnet 395 | # identifier. 396 | DROP INDEX key_dhcp4_identifier_subnet_id ON hosts; 397 | CREATE UNIQUE INDEX key_dhcp4_identifier_subnet_id ON hosts (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp4_subnet_id ASC); 398 | 399 | # Update index used for searching DHCPv6 reservations by identifier and subnet id. 400 | # This index is now unique to prevent duplicates. 401 | DROP INDEX key_dhcp6_identifier_subnet_id ON hosts; 402 | CREATE UNIQUE INDEX key_dhcp6_identifier_subnet_id ON hosts (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp6_subnet_id ASC); 403 | 404 | # Create index to search for reservations using IP address and subnet id. 405 | # This unique index guarantees that there is only one occurrence of the 406 | # particular IPv4 address for a given subnet. 407 | CREATE UNIQUE INDEX key_dhcp4_ipv4_address_subnet_id ON hosts (ipv4_address ASC , dhcp4_subnet_id ASC); 408 | 409 | # Create index to search for reservations using address/prefix and prefix 410 | # length. 411 | CREATE UNIQUE INDEX key_dhcp6_address_prefix_len ON ipv6_reservations (address ASC , prefix_len ASC); 412 | 413 | # Create a table mapping host identifiers to their names. Values in this 414 | # table are used as a foreign key in hosts table to guarantee that only 415 | # identifiers present in host_identifier_type table are used in hosts 416 | # table. 417 | CREATE TABLE IF NOT EXISTS host_identifier_type ( 418 | type TINYINT PRIMARY KEY NOT NULL, # Lease type code. 419 | name VARCHAR(32) # Name of the lease type 420 | ) ENGINE = INNODB; 421 | 422 | START TRANSACTION; 423 | INSERT INTO host_identifier_type VALUES (0, 'hw-address'); 424 | INSERT INTO host_identifier_type VALUES (1, 'duid'); 425 | INSERT INTO host_identifier_type VALUES (2, 'circuit-id'); 426 | COMMIT; 427 | 428 | # Add a constraint that any identifier type value added to the hosts 429 | # must map to a value in the host_identifier_type table. 430 | ALTER TABLE hosts 431 | ADD CONSTRAINT fk_host_identifier_type FOREIGN KEY (dhcp_identifier_type) 432 | REFERENCES host_identifier_type (type); 433 | 434 | # Store DHCPv6 option code as 16-bit unsigned integer. 435 | ALTER TABLE dhcp6_options MODIFY code SMALLINT UNSIGNED NOT NULL; 436 | 437 | # Subnet identifier is unsigned. 438 | ALTER TABLE dhcp4_options MODIFY dhcp4_subnet_id INT UNSIGNED NULL; 439 | ALTER TABLE dhcp6_options MODIFY dhcp6_subnet_id INT UNSIGNED NULL; 440 | 441 | # Scopes associate DHCP options stored in dhcp4_options and 442 | # dhcp6_options tables with hosts, subnets, classes or indicate 443 | # that they are global options. 444 | CREATE TABLE IF NOT EXISTS dhcp_option_scope ( 445 | scope_id TINYINT UNSIGNED PRIMARY KEY NOT NULL, 446 | scope_name VARCHAR(32) 447 | ) ENGINE = INNODB; 448 | 449 | START TRANSACTION; 450 | INSERT INTO dhcp_option_scope VALUES (0, 'global'); 451 | INSERT INTO dhcp_option_scope VALUES (1, 'subnet'); 452 | INSERT INTO dhcp_option_scope VALUES (2, 'client-class'); 453 | INSERT INTO dhcp_option_scope VALUES (3, 'host'); 454 | COMMIT; 455 | 456 | # Add scopes into table holding DHCPv4 options 457 | ALTER TABLE dhcp4_options ADD COLUMN scope_id TINYINT UNSIGNED NOT NULL; 458 | ALTER TABLE dhcp4_options 459 | ADD CONSTRAINT fk_dhcp4_option_scope FOREIGN KEY (scope_id) 460 | REFERENCES dhcp_option_scope (scope_id); 461 | 462 | # Add scopes into table holding DHCPv6 options 463 | ALTER TABLE dhcp6_options ADD COLUMN scope_id TINYINT UNSIGNED NOT NULL; 464 | ALTER TABLE dhcp6_options 465 | ADD CONSTRAINT fk_dhcp6_option_scope FOREIGN KEY (scope_id) 466 | REFERENCES dhcp_option_scope (scope_id); 467 | 468 | # Add UNSIGNED to reservation_id 469 | ALTER TABLE ipv6_reservations 470 | MODIFY reservation_id INT UNSIGNED NOT NULL AUTO_INCREMENT; 471 | 472 | # Add columns holding reservations for siaddr, sname and file fields 473 | # carried within DHCPv4 message. 474 | ALTER TABLE hosts ADD COLUMN dhcp4_next_server INT UNSIGNED NULL; 475 | ALTER TABLE hosts ADD COLUMN dhcp4_server_hostname VARCHAR(64) NULL; 476 | ALTER TABLE hosts ADD COLUMN dhcp4_boot_file_name VARCHAR(128) NULL; 477 | 478 | # Update the schema version number 479 | UPDATE schema_version 480 | SET version = '5', minor = '0'; 481 | # This line concludes database upgrade to version 5.0. 482 | 483 | # Add missing 'client-id' and new 'flex-id' host identifier types. 484 | INSERT INTO host_identifier_type VALUES (3, 'client-id'); 485 | INSERT INTO host_identifier_type VALUES (4, 'flex-id'); 486 | 487 | # Recreate the trigger removing dependent host entries. 488 | DROP TRIGGER host_BDEL; 489 | 490 | DELIMITER $$ 491 | CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW 492 | -- Edit trigger body code below this line. Do not edit lines above this one 493 | BEGIN 494 | DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id; 495 | DELETE FROM dhcp4_options WHERE dhcp4_options.host_id = OLD.host_id; 496 | DELETE FROM dhcp6_options WHERE dhcp6_options.host_id = OLD.host_id; 497 | END 498 | $$ 499 | DELIMITER ; 500 | 501 | # Update the schema version number 502 | UPDATE schema_version 503 | SET version = '5', minor = '1'; 504 | # This line concludes database upgrade to version 5.1. 505 | 506 | # Make subnet_id column types consistent with lease table columns 507 | ALTER TABLE dhcp4_options MODIFY dhcp4_subnet_id INT UNSIGNED; 508 | ALTER TABLE dhcp6_options MODIFY dhcp6_subnet_id INT UNSIGNED; 509 | 510 | # Update the schema version number 511 | UPDATE schema_version 512 | SET version = '5', minor = '2'; 513 | 514 | # This line concludes database upgrade to version 5.2. 515 | 516 | # Add user context into table holding hosts 517 | ALTER TABLE hosts ADD COLUMN user_context TEXT NULL; 518 | 519 | # Add user contexts into tables holding DHCP options 520 | ALTER TABLE dhcp4_options ADD COLUMN user_context TEXT NULL; 521 | ALTER TABLE dhcp6_options ADD COLUMN user_context TEXT NULL; 522 | 523 | # Create index for searching leases by subnet identifier. 524 | CREATE INDEX lease4_by_subnet_id ON lease4 (subnet_id); 525 | 526 | # Create for searching leases by subnet identifier and lease type. 527 | CREATE INDEX lease6_by_subnet_id_lease_type ON lease6 (subnet_id, lease_type); 528 | 529 | # The index by iaid_subnet_id_duid is not the best choice because there are 530 | # cases when we don't specify subnet identifier while searching leases. The 531 | # index will be universal if the subnet_id is the right most column in the 532 | # index. 533 | DROP INDEX lease6_by_iaid_subnet_id_duid on lease6; 534 | CREATE INDEX lease6_by_duid_iaid_subnet_id ON lease6 (duid, iaid, subnet_id); 535 | 536 | # Update the schema version number 537 | UPDATE schema_version 538 | SET version = '6', minor = '0'; 539 | # This line concludes database upgrade to version 6.0. 540 | 541 | # Notes: 542 | # 543 | # Indexes 544 | # ======= 545 | # It is likely that additional indexes will be needed. However, the 546 | # increase in lookup performance from these will come at the expense 547 | # of a decrease in performance during insert operations due to the need 548 | # to update the indexes. For this reason, the need for additional indexes 549 | # will be determined by experiment during performance tests. 550 | # 551 | # The most likely additional indexes will cover the following columns: 552 | # 553 | # hwaddr and client_id 554 | # For lease stability: if a client requests a new lease, try to find an 555 | # existing or recently expired lease for it so that it can keep using the 556 | # same IP address. 557 | # 558 | # Field Sizes 559 | # =========== 560 | # If any of the VARxxx field sizes are altered, the lengths in the MySQL 561 | # backend source file (mysql_lease_mgr.cc) must be correspondingly changed. 562 | # 563 | # Portability 564 | # =========== 565 | # The 'ENGINE = INNODB' on some tables is not portable to another database 566 | # and will need to be removed. 567 | # 568 | # Some columns contain binary data so are stored as VARBINARY instead of 569 | # VARCHAR. This may be non-portable between databases: in this case, the 570 | # definition should be changed to VARCHAR. 571 | --------------------------------------------------------------------------------