1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* ip cidr, for libreswan
*
* Copyright (C) 2019-2024 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2023 Brady Johnson <bradyjoh@redhat.com>
* Copyright (C) 2021 Antony Antony <antony@phenome.org>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
* License for more details.
*/
#include "passert.h"
#include "jambuf.h"
#include "lswlog.h"
#include "ip_cidr.h"
#include "ip_info.h"
const ip_cidr unset_cidr;
ip_cidr cidr_from_raw(where_t where,
const struct ip_info *afi,
const struct ip_bytes bytes,
unsigned prefix_len)
{
/* combine */
ip_cidr cidr = {
.is_set = true,
.version = afi->ip_version,
.bytes = bytes,
.prefix_len = prefix_len,
};
pexpect_cidr(cidr, where);
return cidr;
}
diag_t data_to_cidr(const void *data, size_t sizeof_data, unsigned prefix_len,
const struct ip_info *afi, ip_cidr *dst)
{
*dst = unset_cidr;
if (afi == NULL) {
return diag("unknown CIDR address family");
}
/* accept longer! */
if (sizeof_data < afi->ip_size) {
return diag("minimum %s CIDR address buffer length is %zu, not %zu",
afi->ip_name, afi->ip_size, sizeof_data);
}
if (prefix_len > afi->mask_cnt) {
return diag("maximum %s CIDR prefix length is %u, not %u",
afi->ip_name, afi->mask_cnt, prefix_len);
}
struct ip_bytes bytes = unset_ip_bytes;
memcpy(bytes.byte, data, afi->ip_size);
*dst = cidr_from_raw(HERE, afi, bytes, prefix_len);
return NULL;
}
ip_cidr cidr_from_address(ip_address address)
{
const struct ip_info *afi = address_info(address);
if (afi == NULL) {
return unset_cidr;
}
/* contains both routing-prefix and host-identifier */
return cidr_from_raw(HERE, afi, address.bytes,
afi->mask_cnt/*32|128*/);
}
const struct ip_info *cidr_type(const ip_cidr *cidr)
{
if (cidr == NULL) {
return NULL;
}
/* may return NULL */
return cidr_info(*cidr);
}
const struct ip_info *cidr_info(const ip_cidr cidr)
{
if (!cidr.is_set) {
return NULL;
}
/* may return NULL */
return ip_version_info(cidr.version);
}
ip_address cidr_address(const ip_cidr cidr)
{
const struct ip_info *afi = cidr_info(cidr);
if (afi == NULL) {
return unset_address;
}
return address_from_raw(HERE, afi, cidr.bytes);
}
ip_address cidr_prefix(const ip_cidr cidr)
{
const struct ip_info *afi = cidr_info(cidr);
if (afi == NULL) {
return unset_address;
}
return address_from_raw(HERE, afi,
ip_bytes_blit(afi, cidr.bytes,
&keep_routing_prefix,
&clear_host_identifier,
cidr.prefix_len));
}
int cidr_prefix_len(const ip_cidr cidr)
{
return cidr.prefix_len;
}
err_t cidr_check(const ip_cidr cidr)
{
if (!cidr.is_set) {
return "unset";
}
const struct ip_info *afi = cidr_info(cidr);
if (afi == NULL) {
return "unknown address family";
}
/* https://en.wikipedia.org/wiki/IPv6_address#Special_addresses */
/* ::/0 and/or 0.0.0.0/0 */
if (cidr.prefix_len == 0 && thingeq(cidr.bytes, unset_ip_bytes)) {
return "default route (no specific route)";
}
if (thingeq(cidr.bytes, unset_ip_bytes)) {
return "unspecified address";
}
return NULL;
}
bool cidr_is_specified(const ip_cidr cidr)
{
return cidr_check(cidr) == NULL;
}
shunk_t cidr_as_shunk(const ip_cidr *cidr)
{
const struct ip_info *afi = cidr_type(cidr);
if (afi == NULL) {
return null_shunk;
}
return shunk2(&cidr->bytes, afi->ip_size);
}
chunk_t cidr_as_chunk(ip_cidr *cidr)
{
const struct ip_info *afi = cidr_type(cidr);
if (afi == NULL) {
/* NULL+unset+unknown */
return empty_chunk;
}
return chunk2(&cidr->bytes, afi->ip_size);
}
size_t jam_cidr(struct jambuf *buf, const ip_cidr *cidr)
{
const struct ip_info *afi = cidr_type(cidr);
if (afi == NULL) {
return jam_string(buf, "<unset-cidr>");
}
size_t s = 0;
ip_address sa = cidr_address(*cidr);
s += jam_address(buf, &sa); /* sensitive? */
s += jam(buf, "/%u", cidr->prefix_len);
return s;
}
const char *str_cidr(const ip_cidr *cidr, cidr_buf *out)
{
struct jambuf buf = ARRAY_AS_JAMBUF(out->buf);
jam_cidr(&buf, cidr);
return out->buf;
}
void pexpect_cidr(const ip_cidr cidr, where_t where)
{
if (cidr.is_set == false ||
cidr.version == 0) {
llog_pexpect(&global_logger, where, "invalid "PRI_CIDR, pri_cidr(cidr));
}
}
bool cidr_eq_cidr(const ip_cidr l, const ip_cidr r)
{
bool l_set = cidr_is_specified(l);
bool r_set = cidr_is_specified(r);
if (! l_set && ! r_set) {
/* unset/NULL addresses are equal */
return true;
}
if (! l_set || ! r_set) {
return false;
}
/* must compare individual fields */
return (l.version == r.version &&
l.prefix_len == r.prefix_len &&
thingeq(l.bytes, r.bytes));
}
|