Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add MAC address support. */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 2 | #include <stdbool.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <netdb.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <getopt.h> |
| 8 | #if defined(__GLIBC__) && __GLIBC__ == 2 |
| 9 | #include <net/ethernet.h> |
| 10 | #else |
| 11 | #include <linux/if_ether.h> |
| 12 | #endif |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 13 | #include <xtables.h> |
| 14 | #include <linux/netfilter/xt_mac.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 16 | static void mac_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 17 | { |
| 18 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 19 | "mac match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 20 | "[!] --mac-source XX:XX:XX:XX:XX:XX\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | " Match source MAC address\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 24 | static const struct option mac_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 25 | {.name = "mac-source", .has_arg = true, .val = '1'}, |
| 26 | XT_GETOPT_TABLEEND, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 29 | static void |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 30 | parse_mac(const char *mac, struct xt_mac_info *info) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | { |
| 32 | unsigned int i = 0; |
| 33 | |
| 34 | if (strlen(mac) != ETH_ALEN*3-1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 35 | xtables_error(PARAMETER_PROBLEM, "Bad mac address \"%s\"", mac); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 36 | |
| 37 | for (i = 0; i < ETH_ALEN; i++) { |
| 38 | long number; |
| 39 | char *end; |
| 40 | |
| 41 | number = strtol(mac + i*3, &end, 16); |
| 42 | |
| 43 | if (end == mac + i*3 + 2 |
| 44 | && number >= 0 |
| 45 | && number <= 255) |
| 46 | info->srcaddr[i] = number; |
| 47 | else |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 48 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | "Bad mac address `%s'", mac); |
| 50 | } |
| 51 | } |
| 52 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 53 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 54 | mac_parse(int c, char **argv, int invert, unsigned int *flags, |
| 55 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 56 | { |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 57 | struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 58 | |
| 59 | switch (c) { |
| 60 | case '1': |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 61 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 62 | parse_mac(optarg, macinfo); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 63 | if (invert) |
| 64 | macinfo->invert = 1; |
| 65 | *flags = 1; |
| 66 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return 1; |
| 70 | } |
| 71 | |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 72 | static void print_mac(const unsigned char macaddress[ETH_ALEN]) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned int i; |
| 75 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 76 | printf(" %02X", macaddress[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 77 | for (i = 1; i < ETH_ALEN; i++) |
| 78 | printf(":%02X", macaddress[i]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 81 | static void mac_check(unsigned int flags) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 82 | { |
| 83 | if (!flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 84 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 85 | "You must specify `--mac-source'"); |
| 86 | } |
| 87 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 88 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 89 | 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] | 90 | { |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 91 | const struct xt_mac_info *info = (void *)match->data; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 92 | printf(" MAC"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 93 | |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 94 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 95 | printf(" !"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 96 | |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 97 | print_mac(info->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 100 | 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] | 101 | { |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 102 | const struct xt_mac_info *info = (void *)match->data; |
| 103 | |
| 104 | if (info->invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 105 | printf(" !"); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 106 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 107 | printf(" --mac-source"); |
Jan Engelhardt | 161143d | 2008-09-01 14:18:01 +0200 | [diff] [blame] | 108 | print_mac(info->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 111 | static struct xtables_match mac_match = { |
Jan Engelhardt | c5e8573 | 2009-06-12 20:55:44 +0200 | [diff] [blame] | 112 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 113 | .name = "mac", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 114 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame] | 115 | .size = XT_ALIGN(sizeof(struct xt_mac_info)), |
| 116 | .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 117 | .help = mac_help, |
| 118 | .parse = mac_parse, |
| 119 | .final_check = mac_check, |
| 120 | .print = mac_print, |
| 121 | .save = mac_save, |
| 122 | .extra_opts = mac_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | void _init(void) |
| 126 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 127 | xtables_register_match(&mac_match); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 128 | } |