srclimit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2020 Darren Tucker <dtucker@openbsd.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #include <sys/socket.h>
  18. #include <sys/types.h>
  19. #include <limits.h>
  20. #include <netdb.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "addr.h"
  24. #include "canohost.h"
  25. #include "log.h"
  26. #include "misc.h"
  27. #include "srclimit.h"
  28. #include "xmalloc.h"
  29. static int max_children, max_persource, ipv4_masklen, ipv6_masklen;
  30. /* Per connection state, used to enforce unauthenticated connection limit. */
  31. static struct child_info {
  32. int id;
  33. struct xaddr addr;
  34. } *child;
  35. void
  36. srclimit_init(int max, int persource, int ipv4len, int ipv6len)
  37. {
  38. int i;
  39. max_children = max;
  40. ipv4_masklen = ipv4len;
  41. ipv6_masklen = ipv6len;
  42. max_persource = persource;
  43. if (max_persource == INT_MAX) /* no limit */
  44. return;
  45. debug("%s: max connections %d, per source %d, masks %d,%d", __func__,
  46. max, persource, ipv4len, ipv6len);
  47. if (max <= 0)
  48. fatal("%s: invalid number of sockets: %d", __func__, max);
  49. child = xcalloc(max_children, sizeof(*child));
  50. for (i = 0; i < max_children; i++)
  51. child[i].id = -1;
  52. }
  53. /* returns 1 if connection allowed, 0 if not allowed. */
  54. int
  55. srclimit_check_allow(int sock, int id)
  56. {
  57. struct xaddr xa, xb, xmask;
  58. struct sockaddr_storage addr;
  59. socklen_t addrlen = sizeof(addr);
  60. struct sockaddr *sa = (struct sockaddr *)&addr;
  61. int i, bits, first_unused, count = 0;
  62. char xas[NI_MAXHOST];
  63. if (max_persource == INT_MAX) /* no limit */
  64. return 1;
  65. debug("%s: sock %d id %d limit %d", __func__, sock, id, max_persource);
  66. if (getpeername(sock, sa, &addrlen) != 0)
  67. return 1; /* not remote socket? */
  68. if (addr_sa_to_xaddr(sa, addrlen, &xa) != 0)
  69. return 1; /* unknown address family? */
  70. /* Mask address off address to desired size. */
  71. bits = xa.af == AF_INET ? ipv4_masklen : ipv6_masklen;
  72. if (addr_netmask(xa.af, bits, &xmask) != 0 ||
  73. addr_and(&xb, &xa, &xmask) != 0) {
  74. debug3("%s: invalid mask %d bits", __func__, bits);
  75. return 1;
  76. }
  77. first_unused = max_children;
  78. /* Count matching entries and find first unused one. */
  79. for (i = 0; i < max_children; i++) {
  80. if (child[i].id == -1) {
  81. if (i < first_unused)
  82. first_unused = i;
  83. } else if (addr_cmp(&child[i].addr, &xb) == 0) {
  84. count++;
  85. }
  86. }
  87. if (addr_ntop(&xa, xas, sizeof(xas)) != 0) {
  88. debug3("%s: addr ntop failed", __func__);
  89. return 1;
  90. }
  91. debug3("%s: new unauthenticated connection from %s/%d, at %d of %d",
  92. __func__, xas, bits, count, max_persource);
  93. if (first_unused == max_children) { /* no free slot found */
  94. debug3("%s: no free slot", __func__);
  95. return 0;
  96. }
  97. if (first_unused < 0 || first_unused >= max_children)
  98. fatal("%s: internal error: first_unused out of range",
  99. __func__);
  100. if (count >= max_persource)
  101. return 0;
  102. /* Connection allowed, store masked address. */
  103. child[first_unused].id = id;
  104. memcpy(&child[first_unused].addr, &xb, sizeof(xb));
  105. return 1;
  106. }
  107. void
  108. srclimit_done(int id)
  109. {
  110. int i;
  111. if (max_persource == INT_MAX) /* no limit */
  112. return;
  113. debug("%s: id %d", __func__, id);
  114. /* Clear corresponding state entry. */
  115. for (i = 0; i < max_children; i++) {
  116. if (child[i].id == id) {
  117. child[i].id = -1;
  118. return;
  119. }
  120. }
  121. }