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 | |
| 15 | /* Function which prints out usage message. */ |
| 16 | static void |
| 17 | help(void) |
| 18 | { |
| 19 | printf( |
| 20 | "MAC v%s options:\n" |
| 21 | " --mac-source [!] XX:XX:XX:XX:XX:XX\n" |
| 22 | " Match source MAC address\n" |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 23 | "\n", IPTABLES_VERSION); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static struct option opts[] = { |
| 27 | { "mac-source", 1, 0, '1' }, |
| 28 | {0} |
| 29 | }; |
| 30 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | static void |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 32 | parse_mac(const char *mac, struct xt_mac_info *info) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 33 | { |
| 34 | unsigned int i = 0; |
| 35 | |
| 36 | if (strlen(mac) != ETH_ALEN*3-1) |
| 37 | exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac); |
| 38 | |
| 39 | for (i = 0; i < ETH_ALEN; i++) { |
| 40 | long number; |
| 41 | char *end; |
| 42 | |
| 43 | number = strtol(mac + i*3, &end, 16); |
| 44 | |
| 45 | if (end == mac + i*3 + 2 |
| 46 | && number >= 0 |
| 47 | && number <= 255) |
| 48 | info->srcaddr[i] = number; |
| 49 | else |
| 50 | exit_error(PARAMETER_PROBLEM, |
| 51 | "Bad mac address `%s'", mac); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /* Function which parses command options; returns true if it |
| 56 | ate an option */ |
| 57 | static int |
| 58 | parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 59 | const void *entry, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 60 | unsigned int *nfcache, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 61 | struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 62 | { |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 63 | struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 64 | |
| 65 | switch (c) { |
| 66 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 67 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 68 | parse_mac(argv[optind-1], macinfo); |
| 69 | if (invert) |
| 70 | macinfo->invert = 1; |
| 71 | *flags = 1; |
| 72 | break; |
| 73 | |
| 74 | default: |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | return 1; |
| 79 | } |
| 80 | |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 81 | static void print_mac(unsigned char macaddress[ETH_ALEN]) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 82 | { |
| 83 | unsigned int i; |
| 84 | |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 85 | printf("%02X", macaddress[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 86 | for (i = 1; i < ETH_ALEN; i++) |
| 87 | printf(":%02X", macaddress[i]); |
| 88 | printf(" "); |
| 89 | } |
| 90 | |
| 91 | /* Final check; must have specified --mac. */ |
| 92 | static void final_check(unsigned int flags) |
| 93 | { |
| 94 | if (!flags) |
| 95 | exit_error(PARAMETER_PROBLEM, |
| 96 | "You must specify `--mac-source'"); |
| 97 | } |
| 98 | |
| 99 | /* Prints out the matchinfo. */ |
| 100 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 101 | print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 102 | const struct xt_entry_match *match, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 103 | int numeric) |
| 104 | { |
| 105 | printf("MAC "); |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 106 | |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 107 | if (((struct xt_mac_info *)match->data)->invert) |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 108 | printf("! "); |
| 109 | |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 110 | print_mac(((struct xt_mac_info *)match->data)->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 114 | static void save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 115 | { |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 116 | if (((struct xt_mac_info *)match->data)->invert) |
Dave Zambonini | 78c57fa | 2003-06-14 14:27:51 +0000 | [diff] [blame] | 117 | printf("! "); |
| 118 | |
Michael Schwendt | 010491f | 2002-09-12 07:14:55 +0000 | [diff] [blame] | 119 | printf("--mac-source "); |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 120 | print_mac(((struct xt_mac_info *)match->data)->srcaddr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 123 | static struct xtables_match mac = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 124 | .next = NULL, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 125 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 126 | .name = "mac", |
| 127 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 128 | .size = XT_ALIGN(sizeof(struct xt_mac_info)), |
| 129 | .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)), |
| 130 | .help = &help, |
| 131 | .parse = &parse, |
| 132 | .final_check = &final_check, |
| 133 | .print = &print, |
| 134 | .save = &save, |
| 135 | .extra_opts = opts |
| 136 | }; |
| 137 | |
| 138 | static struct xtables_match mac6 = { |
| 139 | .next = NULL, |
| 140 | .family = AF_INET6, |
| 141 | .name = "mac", |
| 142 | .version = IPTABLES_VERSION, |
| 143 | .size = XT_ALIGN(sizeof(struct xt_mac_info)), |
| 144 | .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)), |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 145 | .help = &help, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 146 | .parse = &parse, |
| 147 | .final_check = &final_check, |
| 148 | .print = &print, |
| 149 | .save = &save, |
| 150 | .extra_opts = opts |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | void _init(void) |
| 154 | { |
Yasuyuki KOZAKAI | ba2d891 | 2007-07-24 07:09:51 +0000 | [diff] [blame^] | 155 | xtables_register_match(&mac); |
| 156 | xtables_register_match(&mac6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 157 | } |