Coverage Report

Created: 2025-07-02 06:55

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