libipt_realm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #if defined(__GLIBC__) && __GLIBC__ == 2
  6. #include <net/ethernet.h>
  7. #else
  8. #include <linux/if_ether.h>
  9. #endif
  10. #include <xtables.h>
  11. #include <linux/netfilter_ipv4/ipt_realm.h>
  12. enum {
  13. O_REALM = 0,
  14. };
  15. static void realm_help(void)
  16. {
  17. printf(
  18. "realm match options:\n"
  19. "[!] --realm value[/mask]\n"
  20. " Match realm\n");
  21. }
  22. static const struct xt_option_entry realm_opts[] = {
  23. {.name = "realm", .id = O_REALM, .type = XTTYPE_STRING,
  24. .flags = XTOPT_MAND | XTOPT_INVERT},
  25. XTOPT_TABLEEND,
  26. };
  27. /* array of realms from /etc/iproute2/rt_realms */
  28. static struct xtables_lmap *realms;
  29. static void realm_init(struct xt_entry_match *m)
  30. {
  31. const char file[] = "/etc/iproute2/rt_realms";
  32. realms = xtables_lmap_init(file);
  33. if (realms == NULL && errno != ENOENT)
  34. fprintf(stderr, "Warning: %s: %s\n", file, strerror(errno));
  35. }
  36. static void realm_parse(struct xt_option_call *cb)
  37. {
  38. struct xt_realm_info *realminfo = cb->data;
  39. int id;
  40. char *end;
  41. xtables_option_parse(cb);
  42. realminfo->id = strtoul(cb->arg, &end, 0);
  43. if (end != cb->arg && (*end == '/' || *end == '\0')) {
  44. if (*end == '/')
  45. realminfo->mask = strtoul(end+1, &end, 0);
  46. else
  47. realminfo->mask = 0xffffffff;
  48. if (*end != '\0' || end == cb->arg)
  49. xtables_error(PARAMETER_PROBLEM,
  50. "Bad realm value \"%s\"", cb->arg);
  51. } else {
  52. id = xtables_lmap_name2id(realms, cb->arg);
  53. if (id == -1)
  54. xtables_error(PARAMETER_PROBLEM,
  55. "Realm \"%s\" not found", cb->arg);
  56. realminfo->id = id;
  57. realminfo->mask = 0xffffffff;
  58. }
  59. if (cb->invert)
  60. realminfo->invert = 1;
  61. }
  62. static void
  63. print_realm(unsigned long id, unsigned long mask, int numeric)
  64. {
  65. const char* name = NULL;
  66. if (mask != 0xffffffff)
  67. printf(" 0x%lx/0x%lx", id, mask);
  68. else {
  69. if (numeric == 0)
  70. name = xtables_lmap_id2name(realms, id);
  71. if (name)
  72. printf(" %s", name);
  73. else
  74. printf(" 0x%lx", id);
  75. }
  76. }
  77. static void realm_print(const void *ip, const struct xt_entry_match *match,
  78. int numeric)
  79. {
  80. const struct xt_realm_info *ri = (const void *)match->data;
  81. if (ri->invert)
  82. printf(" !");
  83. printf(" realm");
  84. print_realm(ri->id, ri->mask, numeric);
  85. }
  86. static void realm_save(const void *ip, const struct xt_entry_match *match)
  87. {
  88. const struct xt_realm_info *ri = (const void *)match->data;
  89. if (ri->invert)
  90. printf(" !");
  91. printf(" --realm");
  92. print_realm(ri->id, ri->mask, 0);
  93. }
  94. static struct xtables_match realm_mt_reg = {
  95. .name = "realm",
  96. .version = XTABLES_VERSION,
  97. .family = NFPROTO_IPV4,
  98. .size = XT_ALIGN(sizeof(struct xt_realm_info)),
  99. .userspacesize = XT_ALIGN(sizeof(struct xt_realm_info)),
  100. .help = realm_help,
  101. .init = realm_init,
  102. .print = realm_print,
  103. .save = realm_save,
  104. .x6_parse = realm_parse,
  105. .x6_options = realm_opts,
  106. };
  107. void _init(void)
  108. {
  109. xtables_register_match(&realm_mt_reg);
  110. }