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