Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | #ifdef HAVE_DNSSEC |
| 20 | |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 21 | static struct blockdata *keyblock_free; |
| 22 | static unsigned int blockdata_count, blockdata_hwm, blockdata_alloced; |
| 23 | |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 24 | static void blockdata_expand(int n) |
| 25 | { |
| 26 | struct blockdata *new = whine_malloc(n * sizeof(struct blockdata)); |
| 27 | |
Simon Kelley | ad9c6f0 | 2017-10-27 22:13:49 +0100 | [diff] [blame] | 28 | if (new) |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 29 | { |
| 30 | int i; |
| 31 | |
| 32 | new[n-1].next = keyblock_free; |
| 33 | keyblock_free = new; |
| 34 | |
| 35 | for (i = 0; i < n - 1; i++) |
| 36 | new[i].next = &new[i+1]; |
| 37 | |
| 38 | blockdata_alloced += n; |
| 39 | } |
| 40 | } |
| 41 | |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 42 | /* Preallocate some blocks, proportional to cachesize, to reduce heap fragmentation. */ |
| 43 | void blockdata_init(void) |
| 44 | { |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 45 | keyblock_free = NULL; |
| 46 | blockdata_alloced = 0; |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 47 | blockdata_count = 0; |
| 48 | blockdata_hwm = 0; |
Simon Kelley | b692f23 | 2014-05-09 10:29:43 +0100 | [diff] [blame] | 49 | |
| 50 | /* Note that daemon->cachesize is enforced to have non-zero size if OPT_DNSSEC_VALID is set */ |
| 51 | if (option_bool(OPT_DNSSEC_VALID)) |
Simon Kelley | 6340ca7 | 2018-01-30 21:39:01 +0000 | [diff] [blame] | 52 | blockdata_expand(daemon->cachesize); |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 53 | } |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 54 | |
| 55 | void blockdata_report(void) |
| 56 | { |
Simon Kelley | b692f23 | 2014-05-09 10:29:43 +0100 | [diff] [blame] | 57 | if (option_bool(OPT_DNSSEC_VALID)) |
| 58 | my_syslog(LOG_INFO, _("DNSSEC memory in use %u, max %u, allocated %u"), |
| 59 | blockdata_count * sizeof(struct blockdata), |
| 60 | blockdata_hwm * sizeof(struct blockdata), |
| 61 | blockdata_alloced * sizeof(struct blockdata)); |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 62 | } |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 63 | |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 64 | static struct blockdata *blockdata_alloc_real(int fd, char *data, size_t len) |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 65 | { |
| 66 | struct blockdata *block, *ret = NULL; |
| 67 | struct blockdata **prev = &ret; |
| 68 | size_t blen; |
| 69 | |
| 70 | while (len > 0) |
| 71 | { |
Simon Kelley | 8d718cb | 2014-02-03 16:27:37 +0000 | [diff] [blame] | 72 | if (!keyblock_free) |
| 73 | blockdata_expand(50); |
| 74 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 75 | if (keyblock_free) |
| 76 | { |
| 77 | block = keyblock_free; |
| 78 | keyblock_free = block->next; |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 79 | blockdata_count++; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 80 | } |
Simon Kelley | 5f93853 | 2014-02-03 16:44:32 +0000 | [diff] [blame] | 81 | else |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 82 | { |
| 83 | /* failed to alloc, free partial chain */ |
| 84 | blockdata_free(ret); |
| 85 | return NULL; |
| 86 | } |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 87 | |
| 88 | if (blockdata_hwm < blockdata_count) |
| 89 | blockdata_hwm = blockdata_count; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 90 | |
| 91 | blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len; |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 92 | if (data) |
| 93 | { |
| 94 | memcpy(block->key, data, blen); |
| 95 | data += blen; |
| 96 | } |
| 97 | else if (!read_write(fd, block->key, blen, 1)) |
| 98 | { |
| 99 | /* failed read free partial chain */ |
| 100 | blockdata_free(ret); |
| 101 | return NULL; |
| 102 | } |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 103 | len -= blen; |
| 104 | *prev = block; |
| 105 | prev = &block->next; |
| 106 | block->next = NULL; |
| 107 | } |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 112 | struct blockdata *blockdata_alloc(char *data, size_t len) |
| 113 | { |
| 114 | return blockdata_alloc_real(0, data, len); |
| 115 | } |
Simon Kelley | ad9c6f0 | 2017-10-27 22:13:49 +0100 | [diff] [blame] | 116 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 117 | void blockdata_free(struct blockdata *blocks) |
| 118 | { |
| 119 | struct blockdata *tmp; |
Simon Kelley | 82e3f45 | 2014-01-31 21:05:48 +0000 | [diff] [blame] | 120 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 121 | if (blocks) |
| 122 | { |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 123 | for (tmp = blocks; tmp->next; tmp = tmp->next) |
| 124 | blockdata_count--; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 125 | tmp->next = keyblock_free; |
Simon Kelley | c220768 | 2014-01-08 18:04:20 +0000 | [diff] [blame] | 126 | keyblock_free = blocks; |
| 127 | blockdata_count--; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Simon Kelley | 86bec2d | 2014-01-13 21:31:20 +0000 | [diff] [blame] | 131 | /* if data == NULL, return pointer to static block of sufficient size */ |
| 132 | void *blockdata_retrieve(struct blockdata *block, size_t len, void *data) |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 133 | { |
| 134 | size_t blen; |
| 135 | struct blockdata *b; |
Simon Kelley | 86bec2d | 2014-01-13 21:31:20 +0000 | [diff] [blame] | 136 | void *new, *d; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 137 | |
Simon Kelley | 86bec2d | 2014-01-13 21:31:20 +0000 | [diff] [blame] | 138 | static unsigned int buff_len = 0; |
| 139 | static unsigned char *buff = NULL; |
| 140 | |
| 141 | if (!data) |
| 142 | { |
| 143 | if (len > buff_len) |
| 144 | { |
| 145 | if (!(new = whine_malloc(len))) |
| 146 | return NULL; |
| 147 | if (buff) |
| 148 | free(buff); |
| 149 | buff = new; |
| 150 | } |
| 151 | data = buff; |
| 152 | } |
| 153 | |
| 154 | for (d = data, b = block; len > 0 && b; b = b->next) |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 155 | { |
| 156 | blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len; |
Simon Kelley | 86bec2d | 2014-01-13 21:31:20 +0000 | [diff] [blame] | 157 | memcpy(d, b->key, blen); |
| 158 | d += blen; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 159 | len -= blen; |
| 160 | } |
Simon Kelley | b6e9e7c | 2014-01-08 21:21:20 +0000 | [diff] [blame] | 161 | |
Simon Kelley | 86bec2d | 2014-01-13 21:31:20 +0000 | [diff] [blame] | 162 | return data; |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 163 | } |
Simon Kelley | a799ca0 | 2018-10-18 19:35:29 +0100 | [diff] [blame] | 164 | |
| 165 | |
| 166 | void blockdata_write(struct blockdata *block, size_t len, int fd) |
| 167 | { |
| 168 | for (; len > 0 && block; block = block->next) |
| 169 | { |
| 170 | size_t blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len; |
| 171 | read_write(fd, block->key, blen, 0); |
| 172 | len -= blen; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | struct blockdata *blockdata_read(int fd, size_t len) |
| 177 | { |
| 178 | return blockdata_alloc_real(fd, NULL, len); |
| 179 | } |
| 180 | |
Simon Kelley | 98c098b | 2014-01-08 17:31:16 +0000 | [diff] [blame] | 181 | #endif |