libxt_mac.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdio.h>
  2. #if defined(__GLIBC__) && __GLIBC__ == 2
  3. #include <net/ethernet.h>
  4. #else
  5. #include <linux/if_ether.h>
  6. #endif
  7. #include <xtables.h>
  8. #include <linux/netfilter/xt_mac.h>
  9. enum {
  10. O_MAC = 0,
  11. };
  12. static void mac_help(void)
  13. {
  14. printf(
  15. "mac match options:\n"
  16. "[!] --mac-source XX:XX:XX:XX:XX:XX\n"
  17. " Match source MAC address\n");
  18. }
  19. #define s struct xt_mac_info
  20. static const struct xt_option_entry mac_opts[] = {
  21. {.name = "mac-source", .id = O_MAC, .type = XTTYPE_ETHERMAC,
  22. .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT,
  23. XTOPT_POINTER(s, srcaddr)},
  24. XTOPT_TABLEEND,
  25. };
  26. #undef s
  27. static void mac_parse(struct xt_option_call *cb)
  28. {
  29. struct xt_mac_info *macinfo = cb->data;
  30. xtables_option_parse(cb);
  31. if (cb->invert)
  32. macinfo->invert = 1;
  33. }
  34. static void print_mac(const unsigned char *macaddress)
  35. {
  36. unsigned int i;
  37. printf(" %02X", macaddress[0]);
  38. for (i = 1; i < ETH_ALEN; ++i)
  39. printf(":%02X", macaddress[i]);
  40. }
  41. static void
  42. mac_print(const void *ip, const struct xt_entry_match *match, int numeric)
  43. {
  44. const struct xt_mac_info *info = (void *)match->data;
  45. printf(" MAC");
  46. if (info->invert)
  47. printf(" !");
  48. print_mac(info->srcaddr);
  49. }
  50. static void mac_save(const void *ip, const struct xt_entry_match *match)
  51. {
  52. const struct xt_mac_info *info = (void *)match->data;
  53. if (info->invert)
  54. printf(" !");
  55. printf(" --mac-source");
  56. print_mac(info->srcaddr);
  57. }
  58. static struct xtables_match mac_match = {
  59. .family = NFPROTO_UNSPEC,
  60. .name = "mac",
  61. .version = XTABLES_VERSION,
  62. .size = XT_ALIGN(sizeof(struct xt_mac_info)),
  63. .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)),
  64. .help = mac_help,
  65. .x6_parse = mac_parse,
  66. .print = mac_print,
  67. .save = mac_save,
  68. .x6_options = mac_opts,
  69. };
  70. void _init(void)
  71. {
  72. xtables_register_match(&mac_match);
  73. }