blob: d7e65dac2216500d9f9af78e95a471e4a631e4e7 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add MAC address support. */
Jan Engelhardt32b8e612010-07-23 21:16:14 +02002#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00003#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 KOZAKAIba2d8912007-07-24 07:09:51 +000013#include <xtables.h>
14#include <linux/netfilter/xt_mac.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000015
Jan Engelhardt181dead2007-10-04 16:27:07 +000016static void mac_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000017{
18 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020019"mac match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020020"[!] --mac-source XX:XX:XX:XX:XX:XX\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021" Match source MAC address\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000022}
23
Jan Engelhardt181dead2007-10-04 16:27:07 +000024static const struct option mac_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +020025 {.name = "mac-source", .has_arg = true, .val = '1'},
26 XT_GETOPT_TABLEEND,
Marc Bouchere6869a82000-03-20 06:03:29 +000027};
28
Marc Bouchere6869a82000-03-20 06:03:29 +000029static void
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +000030parse_mac(const char *mac, struct xt_mac_info *info)
Marc Bouchere6869a82000-03-20 06:03:29 +000031{
32 unsigned int i = 0;
33
34 if (strlen(mac) != ETH_ALEN*3-1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010035 xtables_error(PARAMETER_PROBLEM, "Bad mac address \"%s\"", mac);
Marc Bouchere6869a82000-03-20 06:03:29 +000036
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 Engelhardt1829ed42009-02-21 03:29:44 +010048 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000049 "Bad mac address `%s'", mac);
50 }
51}
52
Marc Bouchere6869a82000-03-20 06:03:29 +000053static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000054mac_parse(int c, char **argv, int invert, unsigned int *flags,
55 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +000056{
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +000057 struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000058
59 switch (c) {
60 case '1':
Jan Engelhardtbf971282009-11-03 19:55:11 +010061 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
Jan Engelhardtbbe83862009-10-24 00:45:33 +020062 parse_mac(optarg, macinfo);
Marc Bouchere6869a82000-03-20 06:03:29 +000063 if (invert)
64 macinfo->invert = 1;
65 *flags = 1;
66 break;
Marc Bouchere6869a82000-03-20 06:03:29 +000067 }
68
69 return 1;
70}
71
Jan Engelhardt161143d2008-09-01 14:18:01 +020072static void print_mac(const unsigned char macaddress[ETH_ALEN])
Marc Bouchere6869a82000-03-20 06:03:29 +000073{
74 unsigned int i;
75
Jan Engelhardt73866352010-12-18 02:04:59 +010076 printf(" %02X", macaddress[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +000077 for (i = 1; i < ETH_ALEN; i++)
78 printf(":%02X", macaddress[i]);
Marc Bouchere6869a82000-03-20 06:03:29 +000079}
80
Jan Engelhardt181dead2007-10-04 16:27:07 +000081static void mac_check(unsigned int flags)
Marc Bouchere6869a82000-03-20 06:03:29 +000082{
83 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010084 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000085 "You must specify `--mac-source'");
86}
87
Marc Bouchere6869a82000-03-20 06:03:29 +000088static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000089mac_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +000090{
Jan Engelhardt161143d2008-09-01 14:18:01 +020091 const struct xt_mac_info *info = (void *)match->data;
Jan Engelhardt73866352010-12-18 02:04:59 +010092 printf(" MAC");
Dave Zambonini78c57fa2003-06-14 14:27:51 +000093
Jan Engelhardt161143d2008-09-01 14:18:01 +020094 if (info->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +010095 printf(" !");
Dave Zambonini78c57fa2003-06-14 14:27:51 +000096
Jan Engelhardt161143d2008-09-01 14:18:01 +020097 print_mac(info->srcaddr);
Marc Bouchere6869a82000-03-20 06:03:29 +000098}
99
Jan Engelhardt181dead2007-10-04 16:27:07 +0000100static void mac_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000101{
Jan Engelhardt161143d2008-09-01 14:18:01 +0200102 const struct xt_mac_info *info = (void *)match->data;
103
104 if (info->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100105 printf(" !");
Dave Zambonini78c57fa2003-06-14 14:27:51 +0000106
Jan Engelhardt73866352010-12-18 02:04:59 +0100107 printf(" --mac-source");
Jan Engelhardt161143d2008-09-01 14:18:01 +0200108 print_mac(info->srcaddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000109}
110
Jan Engelhardt181dead2007-10-04 16:27:07 +0000111static struct xtables_match mac_match = {
Jan Engelhardtc5e85732009-06-12 20:55:44 +0200112 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000113 .name = "mac",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200114 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000115 .size = XT_ALIGN(sizeof(struct xt_mac_info)),
116 .userspacesize = XT_ALIGN(sizeof(struct xt_mac_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000117 .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 Bouchere6869a82000-03-20 06:03:29 +0000123};
124
125void _init(void)
126{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000127 xtables_register_match(&mac_match);
Marc Bouchere6869a82000-03-20 06:03:29 +0000128}