Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2 | #if defined(__GLIBC__) && __GLIBC__ == 2 |
| 3 | #include <net/ethernet.h> |
| 4 | #else |
| 5 | #include <linux/if_ether.h> |
| 6 | #endif |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 7 | #include <xtables.h> |
| 8 | #include <linux/netfilter/xt_mac.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 9 | |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 10 | enum { |
| 11 | O_MAC = 0, |
| 12 | }; |
| 13 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 14 | static void mac_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | { |
| 16 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 17 | "mac match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 18 | "[!] --mac-source XX:XX:XX:XX:XX:XX\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 19 | " Match source MAC address\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 22 | #define s struct xt_mac_info |
| 23 | static const struct xt_option_entry mac_opts[] = { |
| 24 | {.name = "mac-source", .id = O_MAC, .type = XTTYPE_ETHERMAC, |
| 25 | .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT, |
| 26 | XTOPT_POINTER(s, srcaddr)}, |
| 27 | XTOPT_TABLEEND, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 28 | }; |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 29 | #undef s |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 30 | |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 31 | static void mac_parse(struct xt_option_call *cb) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 32 | { |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 33 | struct xt_mac_info *macinfo = cb->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 34 | |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 35 | xtables_option_parse(cb); |
| 36 | if (cb->invert) |
| 37 | macinfo->invert = 1; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 40 | static void print_mac(const unsigned char *macaddress) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 41 | { |
| 42 | unsigned int i; |
| 43 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 44 | printf(" %02X", macaddress[0]); |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 45 | for (i = 1; i < ETH_ALEN; ++i) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 46 | printf(":%02X", macaddress[i]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 50 | mac_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 51 | { |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 52 | const struct xt_mac_info *info = (void *)match->data; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 53 | printf(" MAC"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 54 | |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 55 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 56 | printf(" !"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 57 | |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 58 | print_mac(info->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 61 | static void mac_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 62 | { |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 63 | const struct xt_mac_info *info = (void *)match->data; |
| 64 | |
| 65 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 66 | printf(" !"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 67 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 68 | printf(" --mac-source"); |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 69 | print_mac(info->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 72 | static struct xtables_match mac_match = { |
Jan Engelhardt | c5e8573 | 2009-06-12 20:55:44 +0200 | [diff] [blame] | 73 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 74 | .name = "mac", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 75 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 76 | .size = XT_ALIGN(sizeof(struct xt_mac_info)), |
| 77 | .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 78 | .help = mac_help, |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 79 | .x6_parse = mac_parse, |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 80 | .print = mac_print, |
| 81 | .save = mac_save, |
Jan Engelhardt | 7e79d13 | 2011-05-07 03:18:11 +0200 | [diff] [blame] | 82 | .x6_options = mac_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | void _init(void) |
| 86 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 87 | xtables_register_match(&mac_match); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 88 | } |