blob: 61da13bd4658d704d8ce1a284a1056f7607a362e [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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 KOZAKAIba2d8912007-07-24 07:09:51 +000012#include <xtables.h>
13#include <linux/netfilter/xt_mac.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000014
15/* Function which prints out usage message. */
16static void
17help(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 Welte80fe35d2002-05-29 13:08:15 +000023"\n", IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000024}
25
26static struct option opts[] = {
27 { "mac-source", 1, 0, '1' },
28 {0}
29};
30
Marc Bouchere6869a82000-03-20 06:03:29 +000031static void
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +000032parse_mac(const char *mac, struct xt_mac_info *info)
Marc Bouchere6869a82000-03-20 06:03:29 +000033{
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 */
57static int
58parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000059 const void *entry,
Marc Bouchere6869a82000-03-20 06:03:29 +000060 unsigned int *nfcache,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000061 struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +000062{
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +000063 struct xt_mac_info *macinfo = (struct xt_mac_info *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +000064
65 switch (c) {
66 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000067 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +000068 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 Zambonini78c57fa2003-06-14 14:27:51 +000081static void print_mac(unsigned char macaddress[ETH_ALEN])
Marc Bouchere6869a82000-03-20 06:03:29 +000082{
83 unsigned int i;
84
Dave Zambonini78c57fa2003-06-14 14:27:51 +000085 printf("%02X", macaddress[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +000086 for (i = 1; i < ETH_ALEN; i++)
87 printf(":%02X", macaddress[i]);
88 printf(" ");
89}
90
91/* Final check; must have specified --mac. */
92static 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. */
100static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000101print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000102 const struct xt_entry_match *match,
Marc Bouchere6869a82000-03-20 06:03:29 +0000103 int numeric)
104{
105 printf("MAC ");
Dave Zambonini78c57fa2003-06-14 14:27:51 +0000106
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000107 if (((struct xt_mac_info *)match->data)->invert)
Dave Zambonini78c57fa2003-06-14 14:27:51 +0000108 printf("! ");
109
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000110 print_mac(((struct xt_mac_info *)match->data)->srcaddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000111}
112
113/* Saves the union ipt_matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000114static void save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000115{
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000116 if (((struct xt_mac_info *)match->data)->invert)
Dave Zambonini78c57fa2003-06-14 14:27:51 +0000117 printf("! ");
118
Michael Schwendt010491f2002-09-12 07:14:55 +0000119 printf("--mac-source ");
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000120 print_mac(((struct xt_mac_info *)match->data)->srcaddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000121}
122
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000123static struct xtables_match mac = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000124 .next = NULL,
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000125 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000126 .name = "mac",
127 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000128 .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
138static 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 Neira8caee8b2004-12-28 13:11:59 +0000145 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000146 .parse = &parse,
147 .final_check = &final_check,
148 .print = &print,
149 .save = &save,
150 .extra_opts = opts
Marc Bouchere6869a82000-03-20 06:03:29 +0000151};
152
153void _init(void)
154{
Yasuyuki KOZAKAIba2d8912007-07-24 07:09:51 +0000155 xtables_register_match(&mac);
156 xtables_register_match(&mac6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000157}