1 /* Shared library add-on to iptables to add MAC address support. */
7 #if defined(__GLIBC__) && __GLIBC__ == 2
8 #include <net/ethernet.h>
10 #include <linux/if_ether.h>
13 #include <linux/netfilter/xt_mac.h>
15 /* Function which prints out usage message. */
16 static void mac_help(void)
19 "mac match options:\n"
20 " --mac-source [!] XX:XX:XX:XX:XX:XX\n"
21 " Match source MAC address\n");
24 static const struct option mac_opts[] = {
25 { "mac-source", 1, NULL, '1' },
30 parse_mac(const char *mac, struct xt_mac_info *info)
34 if (strlen(mac) != ETH_ALEN*3-1)
35 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
37 for (i = 0; i < ETH_ALEN; i++) {
41 number = strtol(mac + i*3, &end, 16);
43 if (end == mac + i*3 + 2
46 info->srcaddr[i] = number;
48 exit_error(PARAMETER_PROBLEM,
49 "Bad mac address `%s'", mac);
53 /* Function which parses command options; returns true if it
56 mac_parse(int c, char **argv, int invert, unsigned int *flags,
57 const void *entry, struct xt_entry_match **match)
59 struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data;
63 check_inverse(optarg, &invert, &optind, 0);
64 parse_mac(argv[optind-1], macinfo);
77 static void print_mac(unsigned char macaddress[ETH_ALEN])
81 printf("%02X", macaddress[0]);
82 for (i = 1; i < ETH_ALEN; i++)
83 printf(":%02X", macaddress[i]);
87 /* Final check; must have specified --mac. */
88 static void mac_check(unsigned int flags)
91 exit_error(PARAMETER_PROBLEM,
92 "You must specify `--mac-source'");
95 /* Prints out the matchinfo. */
97 mac_print(const void *ip, const struct xt_entry_match *match, int numeric)
101 if (((struct xt_mac_info *)match->data)->invert)
104 print_mac(((struct xt_mac_info *)match->data)->srcaddr);
107 /* Saves the union ipt_matchinfo in parsable form to stdout. */
108 static void mac_save(const void *ip, const struct xt_entry_match *match)
110 if (((struct xt_mac_info *)match->data)->invert)
113 printf("--mac-source ");
114 print_mac(((struct xt_mac_info *)match->data)->srcaddr);
117 static struct xtables_match mac_match = {
120 .version = XTABLES_VERSION,
121 .size = XT_ALIGN(sizeof(struct xt_mac_info)),
122 .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)),
125 .final_check = mac_check,
128 .extra_opts = mac_opts,
131 static struct xtables_match mac_match6 = {
134 .version = XTABLES_VERSION,
135 .size = XT_ALIGN(sizeof(struct xt_mac_info)),
136 .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)),
139 .final_check = mac_check,
142 .extra_opts = mac_opts,
147 xtables_register_match(&mac_match);
148 xtables_register_match(&mac_match6);