libxt_socket.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Shared library add-on to iptables to add early socket matching support.
  3. *
  4. * Copyright (C) 2007 BalaBit IT Ltd.
  5. */
  6. #include <stdio.h>
  7. #include <xtables.h>
  8. #include <linux/netfilter/xt_socket.h>
  9. enum {
  10. O_TRANSPARENT = 0,
  11. };
  12. static const struct xt_option_entry socket_mt_opts[] = {
  13. {.name = "transparent", .id = O_TRANSPARENT, .type = XTTYPE_NONE},
  14. XTOPT_TABLEEND,
  15. };
  16. static void socket_mt_help(void)
  17. {
  18. printf(
  19. "socket match options:\n"
  20. " --transparent Ignore non-transparent sockets\n\n");
  21. }
  22. static void socket_mt_parse(struct xt_option_call *cb)
  23. {
  24. struct xt_socket_mtinfo1 *info = cb->data;
  25. xtables_option_parse(cb);
  26. switch (cb->entry->id) {
  27. case O_TRANSPARENT:
  28. info->flags |= XT_SOCKET_TRANSPARENT;
  29. break;
  30. }
  31. }
  32. static void
  33. socket_mt_save(const void *ip, const struct xt_entry_match *match)
  34. {
  35. const struct xt_socket_mtinfo1 *info = (const void *)match->data;
  36. if (info->flags & XT_SOCKET_TRANSPARENT)
  37. printf(" --transparent");
  38. }
  39. static void
  40. socket_mt_print(const void *ip, const struct xt_entry_match *match,
  41. int numeric)
  42. {
  43. printf(" socket");
  44. socket_mt_save(ip, match);
  45. }
  46. static struct xtables_match socket_mt_reg[] = {
  47. {
  48. .name = "socket",
  49. .revision = 0,
  50. .family = NFPROTO_IPV4,
  51. .version = XTABLES_VERSION,
  52. .size = XT_ALIGN(0),
  53. .userspacesize = XT_ALIGN(0),
  54. },
  55. {
  56. .name = "socket",
  57. .revision = 1,
  58. .family = NFPROTO_UNSPEC,
  59. .version = XTABLES_VERSION,
  60. .size = XT_ALIGN(sizeof(struct xt_socket_mtinfo1)),
  61. .userspacesize = XT_ALIGN(sizeof(struct xt_socket_mtinfo1)),
  62. .help = socket_mt_help,
  63. .print = socket_mt_print,
  64. .save = socket_mt_save,
  65. .x6_parse = socket_mt_parse,
  66. .x6_options = socket_mt_opts,
  67. },
  68. };
  69. void _init(void)
  70. {
  71. xtables_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
  72. }