blob: 5d62d3ae534734c8b6ea928ae58d7f1a98ec955f [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <sys/socket.h>
4#include <netinet/in.h>
5#include <string.h>
6#include <linux/netfilter_bridge/ebtables.h>
7#include <getopt.h>
8#include "../include/ebtables_u.h"
9#include <linux/netfilter_bridge/ebt_ip.h>
10
11#define IP_SOURCE '1'
12#define IP_DEST '2'
13#define IP_myTOS '3' // include/bits/in.h seems to already define IP_TOS
14#define IP_PROTO '4'
15
16static struct option opts[] =
17{
18 { "ip-source" , required_argument, 0, IP_SOURCE },
19 { "ip-src" , required_argument, 0, IP_SOURCE },
20 { "ip-destination", required_argument, 0, IP_DEST },
21 { "ip-dst" , required_argument, 0, IP_DEST },
22 { "ip-tos" , required_argument, 0, IP_myTOS },
23 { "ip-protocol" , required_argument, 0, IP_PROTO },
24 { "ip-proto" , required_argument, 0, IP_PROTO },
25 { 0 }
26};
27
28// put the ip string into 4 bytes
29static int undot_ip(char *ip, unsigned char *ip2)
30{
31 char *p, *q, *end;
32 int onebyte, i;
33 char buf[20];
34
35 strncpy(buf, ip, sizeof(buf) - 1);
36
37 p = buf;
38 for (i = 0; i < 3; i++) {
39 if ((q = strchr(p, '.')) == NULL)
40 return -1;
41 *q = '\0';
42 onebyte = strtol(p, &end, 10);
43 if (*end != '\0' || onebyte > 255 || onebyte < 0)
44 return -1;
45 ip2[i] = (unsigned char)onebyte;
46 p = q + 1;
47 }
48
49 onebyte = strtol(p, &end, 10);
50 if (*end != '\0' || onebyte >255 || onebyte < 0)
51 return -1;
52 ip2[3] = (unsigned char)onebyte;
53
54 return 0;
55}
56
57// put the mask into 4 bytes
58static int ip_mask(char *mask, unsigned char *mask2)
59{
60 char *end;
61 int bits;
62 __u32 mask22;
63
64 if (undot_ip(mask, mask2)) {
65 // not the /a.b.c.e format, maybe the /x format
66 bits = strtol(mask, &end, 10);
67 if (*end != '\0' || bits > 32 || bits < 0)
68 return -1;
69 if (bits != 0) {
70 mask22 = htonl(0xFFFFFFFF << (32 - bits));
71 memcpy(mask2, &mask22, 4);
72 } else {
73 mask22 = 0xFFFFFFFF;
74 memcpy(mask2, &mask22, 4);
75 }
76 }
77 return 0;
78}
79
80// set the ip mask and ip address
81void parse_ip_address(char *address, __u32 *addr, __u32 *msk)
82{
83 char *p;
84 int i;
85
86 // first the mask
87 if ((p = strrchr(address, '/')) != NULL) {
88 *p = '\0';
89 i = ip_mask(p + 1, (unsigned char *)msk);
90 if (i)
91 print_error("Problem with the ip mask");
92 }
93 else
94 *msk = 0xFFFFFFFF;
95
96 i = undot_ip(address, (unsigned char *)addr);
97 if (i)
98 print_error("Problem with the ip address");
99 *addr = *addr & *msk;
100}
101
102// transform the ip mask into a string ready for output
103char *mask_to_dotted(__u32 mask)
104{
105 int i;
106 static char buf[20];
107 __u32 maskaddr, bits;
108
109 maskaddr = ntohl(mask);
110
111 // don't print /32
112 if (mask == 0xFFFFFFFFL)
113 return "";
114
115 i = 32;
116 bits = 0xFFFFFFFEL; // case 0xFFFFFFFF has just been dealt with
117 while (--i >= 0 && maskaddr != bits)
118 bits <<= 1;
119
120 if (i > 0)
121 sprintf(buf, "/%d", i);
122 else if (!i)
123 *buf = '\0';
124 else
125 // mask was not a decent combination of 1's and 0's
126 sprintf(buf, "/%d.%d.%d.%d", ((unsigned char *)&mask)[0],
127 ((unsigned char *)&mask)[1], ((unsigned char *)&mask)[2],
128 ((unsigned char *)&mask)[3]);
129
130 return buf;
131}
132
133static void print_help()
134{
135 printf(
136"ip options:\n"
137"--ip-src [!] address[/mask]: ip source specification\n"
138"--ip-dst [!] address[/mask]: ip destination specification\n"
139"--ip-tos [!] tos : ip tos specification\n"
140"--ip-proto [!] protocol : ip protocol specification\n");
141}
142
143static void init(struct ebt_entry_match *match)
144{
145 struct ebt_ip_info *ipinfo = (struct ebt_ip_info *)match->data;
146
147 ipinfo->invflags = 0;
148 ipinfo->bitmask = 0;
149}
150
151#define OPT_SOURCE 0x01
152#define OPT_DEST 0x02
153#define OPT_TOS 0x04
154#define OPT_PROTO 0x08
155static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
156 unsigned int *flags, struct ebt_entry_match **match)
157{
158 struct ebt_ip_info *ipinfo = (struct ebt_ip_info *)(*match)->data;
159 char *end, *buffer;
160 int i;
161
162 switch (c) {
163 case IP_SOURCE:
164 check_option(flags, OPT_SOURCE);
165 ipinfo->bitmask |= EBT_IP_SOURCE;
166
167 case IP_DEST:
168 if (c == IP_DEST) {
169 check_option(flags, OPT_DEST);
170 ipinfo->bitmask |= EBT_IP_DEST;
171 }
172 if (check_inverse(optarg)) {
173 if (c == IP_SOURCE)
174 ipinfo->invflags |= EBT_IP_SOURCE;
175 else
176 ipinfo->invflags |= EBT_IP_DEST;
177 }
178
179 if (optind > argc)
180 print_error("Missing ip address argument");
181 if (c == IP_SOURCE)
182 parse_ip_address(argv[optind - 1], &ipinfo->saddr,
183 &ipinfo->smsk);
184 else
185 parse_ip_address(argv[optind - 1], &ipinfo->daddr,
186 &ipinfo->dmsk);
187 break;
188
189 case IP_myTOS:
190 check_option(flags, OPT_TOS);
191 if (check_inverse(optarg))
192 ipinfo->invflags |= EBT_IP_TOS;
193
194 if (optind > argc)
195 print_error("Missing ip tos argument");
196 i = strtol(argv[optind - 1], &end, 16);
197 if (i < 0 || i > 255 || *buffer != '\0')
198 print_error("Problem with specified ip tos");
199 ipinfo->tos = i;
200 ipinfo->bitmask |= EBT_IP_TOS;
201 break;
202
203 case IP_PROTO:
204 check_option(flags, OPT_PROTO);
205 if (check_inverse(optarg))
206 ipinfo->invflags |= EBT_IP_PROTO;
207 if (optind > argc)
208 print_error("Missing ip protocol argument");
209 i = strtol(argv[optind - 1], &end, 10);
210 if (i < 0 || i > 255 || *end != '\0')
211 print_error("Problem with specified ip protocol");
212 ipinfo->protocol = i;
213 ipinfo->bitmask |= EBT_IP_PROTO;
214 break;
215 default:
216 return 0;
217 }
218 return 1;
219}
220
221static void final_check(const struct ebt_u_entry *entry,
222 const struct ebt_entry_match *match, const char *name, unsigned int hook)
223{
224 if (entry->bitmask & EBT_NOPROTO || entry->bitmask & EBT_802_3 ||
225 entry->ethproto != ETH_P_IP)
226 print_error("For IP filtering the protocol must be "
227 "specified as IPv4");
228}
229
230static void print(const struct ebt_u_entry *entry,
231 const struct ebt_entry_match *match)
232{
233 struct ebt_ip_info *ipinfo = (struct ebt_ip_info *)match->data;
234 int j;
235
236 if (ipinfo->bitmask & EBT_IP_SOURCE) {
237 printf("source ip: ");
238 if (ipinfo->invflags & EBT_IP_SOURCE)
239 printf("! ");
240 for (j = 0; j < 4; j++)
241 printf("%d%s",((unsigned char *)&ipinfo->saddr)[j],
242 (j == 3) ? "" : ".");
243 printf("%s, ", mask_to_dotted(ipinfo->smsk));
244 }
245 if (ipinfo->bitmask & EBT_IP_DEST) {
246 printf("dest ip: ");
247 if (ipinfo->invflags & EBT_IP_DEST)
248 printf("! ");
249 for (j = 0; j < 4; j++)
250 printf("%d%s", ((unsigned char *)&ipinfo->daddr)[j],
251 (j == 3) ? "" : ".");
252 printf("%s, ", mask_to_dotted(ipinfo->dmsk));
253 }
254 if (ipinfo->bitmask & EBT_IP_TOS) {
255 printf("ip TOS: ");
256 if (ipinfo->invflags & EBT_IP_TOS)
257 printf("! ");
258 printf("0x%02X, ", ipinfo->tos);
259 }
260 if (ipinfo->bitmask & EBT_IP_PROTO) {
261 printf("ip proto: ");
262 if (ipinfo->invflags & EBT_IP_DEST)
263 printf("! ");
264 printf("%d, ", ipinfo->protocol);
265 }
266}
267
268static int compare(const struct ebt_entry_match *m1,
269 const struct ebt_entry_match *m2)
270{
271 struct ebt_ip_info *ipinfo1 = (struct ebt_ip_info *)m1->data;
272 struct ebt_ip_info *ipinfo2 = (struct ebt_ip_info *)m2->data;
273
274 if (ipinfo1->bitmask != ipinfo2->bitmask)
275 return 0;
276 if (ipinfo1->invflags != ipinfo2->invflags)
277 return 0;
278 if (ipinfo1->bitmask & EBT_IP_SOURCE) {
279 if (ipinfo1->saddr != ipinfo2->saddr)
280 return 0;
281 if (ipinfo1->smsk != ipinfo2->smsk)
282 return 0;
283 }
284 if (ipinfo1->bitmask & EBT_IP_DEST) {
285 if (ipinfo1->daddr != ipinfo2->daddr)
286 return 0;
287 if (ipinfo1->dmsk != ipinfo2->dmsk)
288 return 0;
289 }
290 if (ipinfo1->bitmask & EBT_IP_TOS) {
291 if (ipinfo1->tos != ipinfo2->tos)
292 return 0;
293 }
294 if (ipinfo1->bitmask & EBT_IP_PROTO) {
295 if (ipinfo1->protocol != ipinfo2->protocol)
296 return 0;
297 }
298 return 1;
299}
300
301static struct ebt_u_match ip_match =
302{
303 EBT_IP_MATCH,
304 sizeof(struct ebt_ip_info),
305 print_help,
306 init,
307 parse,
308 final_check,
309 print,
310 compare,
311 opts,
312};
313
314static void _init(void) __attribute((constructor));
315static void _init(void)
316{
317 register_match(&ip_match);
318}