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