blob: a43d060a9e5ef3828bf01cded4d9eed3e609b324 [file] [log] [blame]
Bart De Schuymerff587202005-02-08 20:02:28 +00001/* 802_3
2 *
3 * Author:
4 * Chris Vitale <csv@bluetail.com>
5 *
6 * May 2003
7 */
8
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +00009#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include <getopt.h>
13#include "../include/ebtables_u.h"
14#include "../include/ethernetdb.h"
15#include <linux/netfilter_bridge/ebt_802_3.h>
16
17#define _802_3_SAP '1'
18#define _802_3_TYPE '2'
19
20static struct option opts[] =
21{
22 { "802_3-sap" , required_argument, 0, _802_3_SAP },
23 { "802_3-type" , required_argument, 0, _802_3_TYPE },
24 { 0 }
25};
26
27static void print_help()
28{
29 printf(
30"802_3 options:\n"
31"--802_3-sap [!] protocol : 802.3 DSAP/SSAP- 1 byte value (hex)\n"
32" DSAP and SSAP are always the same. One SAP applies to both fields\n"
33"--802_3-type [!] protocol : 802.3 SNAP Type- 2 byte value (hex)\n"
34" Type implies SAP value 0xaa\n");
35}
36
37static void init(struct ebt_entry_match *match)
38{
39 struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data;
40
41 info->invflags = 0;
42 info->bitmask = 0;
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000043}
44
45static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
46 unsigned int *flags, struct ebt_entry_match **match)
47{
48 struct ebt_802_3_info *info = (struct ebt_802_3_info *) (*match)->data;
49 unsigned int i;
50 char *end;
51
52 switch (c) {
53 case _802_3_SAP:
Bart De Schuymerff587202005-02-08 20:02:28 +000054 ebt_check_option2(flags, _802_3_SAP);
55 if (ebt_check_inverse2(optarg))
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000056 info->invflags |= EBT_802_3_SAP;
Bart De Schuymerff587202005-02-08 20:02:28 +000057 i = strtoul(optarg, &end, 16);
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000058 if (i > 255 || *end != '\0')
Bart De Schuymerff587202005-02-08 20:02:28 +000059 ebt_print_error2("Problem with specified "
Bart De Schuymer64182a32004-01-21 20:39:54 +000060 "sap hex value, %x",i);
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000061 info->sap = i; /* one byte, so no byte order worries */
62 info->bitmask |= EBT_802_3_SAP;
63 break;
64 case _802_3_TYPE:
Bart De Schuymerff587202005-02-08 20:02:28 +000065 ebt_check_option2(flags, _802_3_TYPE);
66 if (ebt_check_inverse2(optarg))
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000067 info->invflags |= EBT_802_3_TYPE;
Bart De Schuymerff587202005-02-08 20:02:28 +000068 i = strtoul(optarg, &end, 16);
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000069 if (i > 65535 || *end != '\0') {
Bart De Schuymerff587202005-02-08 20:02:28 +000070 ebt_print_error2("Problem with the specified "
Bart De Schuymer64182a32004-01-21 20:39:54 +000071 "type hex value, %x",i);
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000072 }
73 info->type = htons(i);
74 info->bitmask |= EBT_802_3_TYPE;
75 break;
76 default:
77 return 0;
78 }
79 return 1;
80}
81
82static void final_check(const struct ebt_u_entry *entry,
83 const struct ebt_entry_match *match, const char *name,
84 unsigned int hookmask, unsigned int time)
85{
86 if (!(entry->bitmask & EBT_802_3))
Bart De Schuymer64182a32004-01-21 20:39:54 +000087 ebt_print_error("For 802.3 DSAP/SSAP filtering the protocol "
88 "must be LENGTH");
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +000089}
90
91static void print(const struct ebt_u_entry *entry,
92 const struct ebt_entry_match *match)
93{
94 struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data;
95
96 if (info->bitmask & EBT_802_3_SAP) {
97 printf("--802_3-sap ");
98 if (info->invflags & EBT_802_3_SAP)
99 printf("! ");
100 printf("0x%.2x ", info->sap);
101 }
102 if (info->bitmask & EBT_802_3_TYPE) {
103 printf("--802_3-type ");
104 if (info->invflags & EBT_802_3_TYPE)
105 printf("! ");
106 printf("0x%.4x ", ntohs(info->type));
107 }
108}
109
110static int compare(const struct ebt_entry_match *m1,
111 const struct ebt_entry_match *m2)
112{
113 struct ebt_802_3_info *info1 = (struct ebt_802_3_info *)m1->data;
114 struct ebt_802_3_info *info2 = (struct ebt_802_3_info *)m2->data;
115
116 if (info1->bitmask != info2->bitmask)
117 return 0;
118 if (info1->invflags != info2->invflags)
119 return 0;
120 if (info1->bitmask & EBT_802_3_SAP) {
121 if (info1->sap != info2->sap)
122 return 0;
123 }
124 if (info1->bitmask & EBT_802_3_TYPE) {
125 if (info1->type != info2->type)
126 return 0;
127 }
128 return 1;
129}
130
131static struct ebt_u_match _802_3_match =
132{
Bart De Schuymer7cf1cca2003-08-30 16:20:19 +0000133 .name = EBT_802_3_MATCH,
134 .size = sizeof(struct ebt_802_3_info),
135 .help = print_help,
136 .init = init,
137 .parse = parse,
138 .final_check = final_check,
139 .print = print,
140 .compare = compare,
141 .extra_ops = opts,
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +0000142};
143
Bart De Schuymer64182a32004-01-21 20:39:54 +0000144void _init(void)
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +0000145{
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000146 ebt_register_match(&_802_3_match);
Bart De Schuymer8e0e25d2003-05-12 17:00:26 +0000147}