libxt_set.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _LIBXT_SET_H
  2. #define _LIBXT_SET_H
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <errno.h>
  8. #ifdef DEBUG
  9. #define DEBUGP(x, args...) fprintf(stderr, x , ## args)
  10. #else
  11. #define DEBUGP(x, args...)
  12. #endif
  13. static int
  14. get_version(unsigned *version)
  15. {
  16. int res, sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  17. struct ip_set_req_version req_version;
  18. socklen_t size = sizeof(req_version);
  19. if (sockfd < 0)
  20. xtables_error(OTHER_PROBLEM,
  21. "Can't open socket to ipset.\n");
  22. if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
  23. xtables_error(OTHER_PROBLEM,
  24. "Could not set close on exec: %s\n",
  25. strerror(errno));
  26. }
  27. req_version.op = IP_SET_OP_VERSION;
  28. res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req_version, &size);
  29. if (res != 0)
  30. xtables_error(OTHER_PROBLEM,
  31. "Kernel module xt_set is not loaded in.\n");
  32. *version = req_version.version;
  33. return sockfd;
  34. }
  35. static void
  36. get_set_byid(char *setname, ip_set_id_t idx)
  37. {
  38. struct ip_set_req_get_set req;
  39. socklen_t size = sizeof(struct ip_set_req_get_set);
  40. int res, sockfd;
  41. sockfd = get_version(&req.version);
  42. req.op = IP_SET_OP_GET_BYINDEX;
  43. req.set.index = idx;
  44. res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req, &size);
  45. close(sockfd);
  46. if (res != 0)
  47. xtables_error(OTHER_PROBLEM,
  48. "Problem when communicating with ipset, errno=%d.\n",
  49. errno);
  50. if (size != sizeof(struct ip_set_req_get_set))
  51. xtables_error(OTHER_PROBLEM,
  52. "Incorrect return size from kernel during ipset lookup, "
  53. "(want %zu, got %zu)\n",
  54. sizeof(struct ip_set_req_get_set), (size_t)size);
  55. if (req.set.name[0] == '\0')
  56. xtables_error(PARAMETER_PROBLEM,
  57. "Set with index %i in kernel doesn't exist.\n", idx);
  58. strncpy(setname, req.set.name, IPSET_MAXNAMELEN);
  59. }
  60. static void
  61. get_set_byname(const char *setname, struct xt_set_info *info)
  62. {
  63. struct ip_set_req_get_set req;
  64. socklen_t size = sizeof(struct ip_set_req_get_set);
  65. int res, sockfd;
  66. sockfd = get_version(&req.version);
  67. req.op = IP_SET_OP_GET_BYNAME;
  68. strncpy(req.set.name, setname, IPSET_MAXNAMELEN);
  69. req.set.name[IPSET_MAXNAMELEN - 1] = '\0';
  70. res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req, &size);
  71. close(sockfd);
  72. if (res != 0)
  73. xtables_error(OTHER_PROBLEM,
  74. "Problem when communicating with ipset, errno=%d.\n",
  75. errno);
  76. if (size != sizeof(struct ip_set_req_get_set))
  77. xtables_error(OTHER_PROBLEM,
  78. "Incorrect return size from kernel during ipset lookup, "
  79. "(want %zu, got %zu)\n",
  80. sizeof(struct ip_set_req_get_set), (size_t)size);
  81. if (req.set.index == IPSET_INVALID_ID)
  82. xtables_error(PARAMETER_PROBLEM,
  83. "Set %s doesn't exist.\n", setname);
  84. info->index = req.set.index;
  85. }
  86. static void
  87. parse_dirs_v0(const char *opt_arg, struct xt_set_info_v0 *info)
  88. {
  89. char *saved = strdup(opt_arg);
  90. char *ptr, *tmp = saved;
  91. int i = 0;
  92. while (i < (IPSET_DIM_MAX - 1) && tmp != NULL) {
  93. ptr = strsep(&tmp, ",");
  94. if (strncmp(ptr, "src", 3) == 0)
  95. info->u.flags[i++] |= IPSET_SRC;
  96. else if (strncmp(ptr, "dst", 3) == 0)
  97. info->u.flags[i++] |= IPSET_DST;
  98. else
  99. xtables_error(PARAMETER_PROBLEM,
  100. "You must spefify (the comma separated list of) 'src' or 'dst'.");
  101. }
  102. if (tmp)
  103. xtables_error(PARAMETER_PROBLEM,
  104. "Can't be more src/dst options than %i.",
  105. IPSET_DIM_MAX);
  106. free(saved);
  107. }
  108. static void
  109. parse_dirs(const char *opt_arg, struct xt_set_info *info)
  110. {
  111. char *saved = strdup(opt_arg);
  112. char *ptr, *tmp = saved;
  113. while (info->dim < IPSET_DIM_MAX && tmp != NULL) {
  114. info->dim++;
  115. ptr = strsep(&tmp, ",");
  116. if (strncmp(ptr, "src", 3) == 0)
  117. info->flags |= (1 << info->dim);
  118. else if (strncmp(ptr, "dst", 3) != 0)
  119. xtables_error(PARAMETER_PROBLEM,
  120. "You must spefify (the comma separated list of) 'src' or 'dst'.");
  121. }
  122. if (tmp)
  123. xtables_error(PARAMETER_PROBLEM,
  124. "Can't be more src/dst options than %i.",
  125. IPSET_DIM_MAX);
  126. free(saved);
  127. }
  128. #endif /*_LIBXT_SET_H*/