Coverage Report

Created: 2024-06-18 06:23

/src/hpn-ssh/addr.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: addr.c,v 1.7 2023/03/27 03:31:05 djm Exp $ */
2
3
/*
4
 * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include "includes.h"
20
21
#include <sys/types.h>
22
#include <sys/socket.h>
23
#include <netinet/in.h>
24
#include <arpa/inet.h>
25
26
#include <netdb.h>
27
#include <string.h>
28
#include <stdlib.h>
29
#include <stdio.h>
30
31
#include "addr.h"
32
33
0
#define _SA(x)  ((struct sockaddr *)(x))
34
35
int
36
addr_unicast_masklen(int af)
37
0
{
38
0
  switch (af) {
39
0
  case AF_INET:
40
0
    return 32;
41
0
  case AF_INET6:
42
0
    return 128;
43
0
  default:
44
0
    return -1;
45
0
  }
46
0
}
47
48
static inline int
49
masklen_valid(int af, u_int masklen)
50
0
{
51
0
  switch (af) {
52
0
  case AF_INET:
53
0
    return masklen <= 32 ? 0 : -1;
54
0
  case AF_INET6:
55
0
    return masklen <= 128 ? 0 : -1;
56
0
  default:
57
0
    return -1;
58
0
  }
59
0
}
60
61
int
62
addr_xaddr_to_sa(const struct xaddr *xa, struct sockaddr *sa, socklen_t *len,
63
    u_int16_t port)
64
0
{
65
0
  struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
66
0
  struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
67
68
0
  if (xa == NULL || sa == NULL || len == NULL)
69
0
    return -1;
70
71
0
  switch (xa->af) {
72
0
  case AF_INET:
73
0
    if (*len < sizeof(*in4))
74
0
      return -1;
75
0
    memset(sa, '\0', sizeof(*in4));
76
0
    *len = sizeof(*in4);
77
#ifdef SOCK_HAS_LEN
78
    in4->sin_len = sizeof(*in4);
79
#endif
80
0
    in4->sin_family = AF_INET;
81
0
    in4->sin_port = htons(port);
82
0
    memcpy(&in4->sin_addr, &xa->v4, sizeof(in4->sin_addr));
83
0
    break;
84
0
  case AF_INET6:
85
0
    if (*len < sizeof(*in6))
86
0
      return -1;
87
0
    memset(sa, '\0', sizeof(*in6));
88
0
    *len = sizeof(*in6);
89
#ifdef SOCK_HAS_LEN
90
    in6->sin6_len = sizeof(*in6);
91
#endif
92
0
    in6->sin6_family = AF_INET6;
93
0
    in6->sin6_port = htons(port);
94
0
    memcpy(&in6->sin6_addr, &xa->v6, sizeof(in6->sin6_addr));
95
0
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
96
0
    in6->sin6_scope_id = xa->scope_id;
97
0
#endif
98
0
    break;
99
0
  default:
100
0
    return -1;
101
0
  }
102
0
  return 0;
103
0
}
104
105
/*
106
 * Convert struct sockaddr to struct xaddr
107
 * Returns 0 on success, -1 on failure.
108
 */
109
int
110
addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
111
0
{
112
0
  struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
113
0
  struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
114
115
0
  memset(xa, '\0', sizeof(*xa));
116
117
0
  switch (sa->sa_family) {
118
0
  case AF_INET:
119
0
    if (slen < (socklen_t)sizeof(*in4))
120
0
      return -1;
121
0
    xa->af = AF_INET;
122
0
    memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
123
0
    break;
124
0
  case AF_INET6:
125
0
    if (slen < (socklen_t)sizeof(*in6))
126
0
      return -1;
127
0
    xa->af = AF_INET6;
128
0
    memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
129
0
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
130
0
    xa->scope_id = in6->sin6_scope_id;
131
0
#endif
132
0
    break;
133
0
  default:
134
0
    return -1;
135
0
  }
136
137
0
  return 0;
138
0
}
139
140
int
141
addr_invert(struct xaddr *n)
142
0
{
143
0
  int i;
144
145
0
  if (n == NULL)
146
0
    return -1;
147
148
0
  switch (n->af) {
149
0
  case AF_INET:
150
0
    n->v4.s_addr = ~n->v4.s_addr;
151
0
    return 0;
152
0
  case AF_INET6:
153
0
    for (i = 0; i < 4; i++)
154
0
      n->addr32[i] = ~n->addr32[i];
155
0
    return 0;
156
0
  default:
157
0
    return -1;
158
0
  }
159
0
}
160
161
/*
162
 * Calculate a netmask of length 'l' for address family 'af' and
163
 * store it in 'n'.
164
 * Returns 0 on success, -1 on failure.
165
 */
166
int
167
addr_netmask(int af, u_int l, struct xaddr *n)
168
0
{
169
0
  int i;
170
171
0
  if (masklen_valid(af, l) != 0 || n == NULL)
172
0
    return -1;
173
174
0
  memset(n, '\0', sizeof(*n));
175
0
  switch (af) {
176
0
  case AF_INET:
177
0
    n->af = AF_INET;
178
0
    if (l == 0)
179
0
      return 0;
180
0
    n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
181
0
    return 0;
182
0
  case AF_INET6:
183
0
    n->af = AF_INET6;
184
0
    for (i = 0; i < 4 && l >= 32; i++, l -= 32)
185
0
      n->addr32[i] = 0xffffffffU;
186
0
    if (i < 4 && l != 0)
187
0
      n->addr32[i] = htonl((0xffffffff << (32 - l)) &
188
0
          0xffffffff);
189
0
    return 0;
190
0
  default:
191
0
    return -1;
192
0
  }
193
0
}
194
195
int
196
addr_hostmask(int af, u_int l, struct xaddr *n)
197
0
{
198
0
  if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
199
0
    return -1;
200
0
  return 0;
201
0
}
202
203
/*
204
 * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
205
 * Returns 0 on success, -1 on failure.
206
 */
207
int
208
addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
209
0
{
210
0
  int i;
211
212
0
  if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
213
0
    return -1;
214
215
0
  memcpy(dst, a, sizeof(*dst));
216
0
  switch (a->af) {
217
0
  case AF_INET:
218
0
    dst->v4.s_addr &= b->v4.s_addr;
219
0
    return 0;
220
0
  case AF_INET6:
221
0
    dst->scope_id = a->scope_id;
222
0
    for (i = 0; i < 4; i++)
223
0
      dst->addr32[i] &= b->addr32[i];
224
0
    return 0;
225
0
  default:
226
0
    return -1;
227
0
  }
228
0
}
229
230
int
231
addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
232
0
{
233
0
  int i;
234
235
0
  if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
236
0
    return (-1);
237
238
0
  memcpy(dst, a, sizeof(*dst));
239
0
  switch (a->af) {
240
0
  case AF_INET:
241
0
    dst->v4.s_addr |= b->v4.s_addr;
242
0
    return (0);
243
0
  case AF_INET6:
244
0
    for (i = 0; i < 4; i++)
245
0
      dst->addr32[i] |= b->addr32[i];
246
0
    return (0);
247
0
  default:
248
0
    return (-1);
249
0
  }
250
0
}
251
252
int
253
addr_cmp(const struct xaddr *a, const struct xaddr *b)
254
0
{
255
0
  int i;
256
257
0
  if (a->af != b->af)
258
0
    return (a->af == AF_INET6 ? 1 : -1);
259
260
0
  switch (a->af) {
261
0
  case AF_INET:
262
    /*
263
     * Can't just subtract here as 255.255.255.255 - 0.0.0.0 is
264
     * too big to fit into a signed int
265
     */
266
0
    if (a->v4.s_addr == b->v4.s_addr)
267
0
      return 0;
268
0
    return (ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1);
269
0
  case AF_INET6:
270
    /*
271
     * Do this a byte at a time to avoid the above issue and
272
     * any endian problems
273
     */
274
0
    for (i = 0; i < 16; i++)
275
0
      if (a->addr8[i] - b->addr8[i] != 0)
276
0
        return (a->addr8[i] - b->addr8[i]);
277
0
    if (a->scope_id == b->scope_id)
278
0
      return (0);
279
0
    return (a->scope_id > b->scope_id ? 1 : -1);
280
0
  default:
281
0
    return (-1);
282
0
  }
283
0
}
284
285
int
286
addr_is_all0s(const struct xaddr *a)
287
0
{
288
0
  int i;
289
290
0
  switch (a->af) {
291
0
  case AF_INET:
292
0
    return (a->v4.s_addr == 0 ? 0 : -1);
293
0
  case AF_INET6:
294
0
    for (i = 0; i < 4; i++)
295
0
      if (a->addr32[i] != 0)
296
0
        return -1;
297
0
    return 0;
298
0
  default:
299
0
    return -1;
300
0
  }
301
0
}
302
303
/* Increment the specified address. Note, does not do overflow checking */
304
void
305
addr_increment(struct xaddr *a)
306
0
{
307
0
  int i;
308
0
  uint32_t n;
309
310
0
  switch (a->af) {
311
0
  case AF_INET:
312
0
    a->v4.s_addr = htonl(ntohl(a->v4.s_addr) + 1);
313
0
    break;
314
0
  case AF_INET6:
315
0
    for (i = 0; i < 4; i++) {
316
      /* Increment with carry */
317
0
      n = ntohl(a->addr32[3 - i]) + 1;
318
0
      a->addr32[3 - i] = htonl(n);
319
0
      if (n != 0)
320
0
        break;
321
0
    }
322
0
    break;
323
0
  }
324
0
}
325
326
/*
327
 * Test whether host portion of address 'a', as determined by 'masklen'
328
 * is all zeros.
329
 * Returns 0 if host portion of address is all-zeros,
330
 * -1 if not all zeros or on failure.
331
 */
332
int
333
addr_host_is_all0s(const struct xaddr *a, u_int masklen)
334
0
{
335
0
  struct xaddr tmp_addr, tmp_mask, tmp_result;
336
337
0
  memcpy(&tmp_addr, a, sizeof(tmp_addr));
338
0
  if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
339
0
    return -1;
340
0
  if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
341
0
    return -1;
342
0
  return addr_is_all0s(&tmp_result);
343
0
}
344
345
#if 0
346
int
347
addr_host_to_all0s(struct xaddr *a, u_int masklen)
348
{
349
  struct xaddr tmp_mask;
350
351
  if (addr_netmask(a->af, masklen, &tmp_mask) == -1)
352
    return (-1);
353
  if (addr_and(a, a, &tmp_mask) == -1)
354
    return (-1);
355
  return (0);
356
}
357
#endif
358
359
int
360
addr_host_to_all1s(struct xaddr *a, u_int masklen)
361
0
{
362
0
  struct xaddr tmp_mask;
363
364
0
  if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
365
0
    return (-1);
366
0
  if (addr_or(a, a, &tmp_mask) == -1)
367
0
    return (-1);
368
0
  return (0);
369
0
}
370
371
/*
372
 * Parse string address 'p' into 'n'.
373
 * Returns 0 on success, -1 on failure.
374
 */
375
int
376
addr_pton(const char *p, struct xaddr *n)
377
0
{
378
0
  struct addrinfo hints, *ai;
379
380
0
  memset(&hints, '\0', sizeof(hints));
381
0
  hints.ai_flags = AI_NUMERICHOST;
382
383
0
  if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
384
0
    return -1;
385
386
0
  if (ai == NULL)
387
0
    return -1;
388
389
0
  if (ai->ai_addr == NULL) {
390
0
    freeaddrinfo(ai);
391
0
    return -1;
392
0
  }
393
394
0
  if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen,
395
0
      n) == -1) {
396
0
    freeaddrinfo(ai);
397
0
    return -1;
398
0
  }
399
400
0
  freeaddrinfo(ai);
401
0
  return 0;
402
0
}
403
404
int
405
addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, socklen_t slen)
406
0
{
407
0
  struct addrinfo hints, *ai;
408
409
0
  memset(&hints, '\0', sizeof(hints));
410
0
  hints.ai_flags = AI_NUMERICHOST;
411
412
0
  if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0)
413
0
    return -1;
414
415
0
  if (ai == NULL)
416
0
    return -1;
417
418
0
  if (ai->ai_addr == NULL) {
419
0
    freeaddrinfo(ai);
420
0
    return -1;
421
0
  }
422
423
0
  if (sa != NULL) {
424
0
    if (slen < ai->ai_addrlen) {
425
0
      freeaddrinfo(ai);
426
0
      return -1;
427
0
    }
428
0
    memcpy(sa, &ai->ai_addr, ai->ai_addrlen);
429
0
  }
430
431
0
  freeaddrinfo(ai);
432
0
  return 0;
433
0
}
434
435
int
436
addr_ntop(const struct xaddr *n, char *p, size_t len)
437
0
{
438
0
  struct sockaddr_storage ss;
439
0
  socklen_t slen = sizeof(ss);
440
441
0
  if (addr_xaddr_to_sa(n, _SA(&ss), &slen, 0) == -1)
442
0
    return -1;
443
0
  if (p == NULL || len == 0)
444
0
    return -1;
445
0
  if (getnameinfo(_SA(&ss), slen, p, len, NULL, 0,
446
0
      NI_NUMERICHOST) != 0)
447
0
    return -1;
448
449
0
  return 0;
450
0
}
451
452
/*
453
 * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
454
 * Return -1 on parse error, -2 on inconsistency or 0 on success.
455
 */
456
int
457
addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
458
0
{
459
0
  struct xaddr tmp;
460
0
  long unsigned int masklen = 999;
461
0
  char addrbuf[64], *mp, *cp;
462
463
  /* Don't modify argument */
464
0
  if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf))
465
0
    return -1;
466
467
0
  if ((mp = strchr(addrbuf, '/')) != NULL) {
468
0
    *mp = '\0';
469
0
    mp++;
470
0
    masklen = strtoul(mp, &cp, 10);
471
0
    if (*mp < '0' || *mp > '9' || *cp != '\0' || masklen > 128)
472
0
      return -1;
473
0
  }
474
475
0
  if (addr_pton(addrbuf, &tmp) == -1)
476
0
    return -1;
477
478
0
  if (mp == NULL)
479
0
    masklen = addr_unicast_masklen(tmp.af);
480
0
  if (masklen_valid(tmp.af, masklen) == -1)
481
0
    return -2;
482
0
  if (addr_host_is_all0s(&tmp, masklen) != 0)
483
0
    return -2;
484
485
0
  if (n != NULL)
486
0
    memcpy(n, &tmp, sizeof(*n));
487
0
  if (l != NULL)
488
0
    *l = masklen;
489
490
0
  return 0;
491
0
}
492
493
int
494
addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
495
0
{
496
0
  struct xaddr tmp_mask, tmp_result;
497
498
0
  if (host->af != net->af)
499
0
    return -1;
500
501
0
  if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
502
0
    return -1;
503
0
  if (addr_and(&tmp_result, host, &tmp_mask) == -1)
504
0
    return -1;
505
0
  return addr_cmp(&tmp_result, net);
506
0
}