blob: 91014e470ba9716e756ed09390b19bc0319b86fb [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>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00006#include <getopt.h>
7#include "../include/ebtables_u.h"
8#include <linux/netfilter_bridge/ebt_ip.h>
9
10#define IP_SOURCE '1'
11#define IP_DEST '2'
12#define IP_myTOS '3' // include/bits/in.h seems to already define IP_TOS
13#define IP_PROTO '4'
14
15static struct option opts[] =
16{
17 { "ip-source" , required_argument, 0, IP_SOURCE },
18 { "ip-src" , required_argument, 0, IP_SOURCE },
19 { "ip-destination", required_argument, 0, IP_DEST },
20 { "ip-dst" , required_argument, 0, IP_DEST },
21 { "ip-tos" , required_argument, 0, IP_myTOS },
22 { "ip-protocol" , required_argument, 0, IP_PROTO },
23 { "ip-proto" , required_argument, 0, IP_PROTO },
24 { 0 }
25};
26
27// put the ip string into 4 bytes
28static int undot_ip(char *ip, unsigned char *ip2)
29{
30 char *p, *q, *end;
31 int onebyte, i;
32 char buf[20];
33
34 strncpy(buf, ip, sizeof(buf) - 1);
35
36 p = buf;
37 for (i = 0; i < 3; i++) {
38 if ((q = strchr(p, '.')) == NULL)
39 return -1;
40 *q = '\0';
41 onebyte = strtol(p, &end, 10);
42 if (*end != '\0' || onebyte > 255 || onebyte < 0)
43 return -1;
44 ip2[i] = (unsigned char)onebyte;
45 p = q + 1;
46 }
47
48 onebyte = strtol(p, &end, 10);
49 if (*end != '\0' || onebyte >255 || onebyte < 0)
50 return -1;
51 ip2[3] = (unsigned char)onebyte;
52
53 return 0;
54}
55
56// put the mask into 4 bytes
57static int ip_mask(char *mask, unsigned char *mask2)
58{
59 char *end;
60 int bits;
61 __u32 mask22;
62
63 if (undot_ip(mask, mask2)) {
64 // not the /a.b.c.e format, maybe the /x format
65 bits = strtol(mask, &end, 10);
66 if (*end != '\0' || bits > 32 || bits < 0)
67 return -1;
68 if (bits != 0) {
69 mask22 = htonl(0xFFFFFFFF << (32 - bits));
70 memcpy(mask2, &mask22, 4);
71 } else {
72 mask22 = 0xFFFFFFFF;
73 memcpy(mask2, &mask22, 4);
74 }
75 }
76 return 0;
77}
78
79// set the ip mask and ip address
80void parse_ip_address(char *address, __u32 *addr, __u32 *msk)
81{
82 char *p;
83 int i;
84
85 // first the mask
86 if ((p = strrchr(address, '/')) != NULL) {
87 *p = '\0';
88 i = ip_mask(p + 1, (unsigned char *)msk);
89 if (i)
90 print_error("Problem with the ip mask");
91 }
92 else
93 *msk = 0xFFFFFFFF;
94
95 i = undot_ip(address, (unsigned char *)addr);
96 if (i)
97 print_error("Problem with the ip address");
98 *addr = *addr & *msk;
99}
100
101// transform the ip mask into a string ready for output
102char *mask_to_dotted(__u32 mask)
103{
104 int i;
105 static char buf[20];
106 __u32 maskaddr, bits;
107
108 maskaddr = ntohl(mask);
109
110 // don't print /32
111 if (mask == 0xFFFFFFFFL)
112 return "";
113
114 i = 32;
115 bits = 0xFFFFFFFEL; // case 0xFFFFFFFF has just been dealt with
116 while (--i >= 0 && maskaddr != bits)
117 bits <<= 1;
118
119 if (i > 0)
120 sprintf(buf, "/%d", i);
121 else if (!i)
122 *buf = '\0';
123 else
124 // mask was not a decent combination of 1's and 0's
125 sprintf(buf, "/%d.%d.%d.%d", ((unsigned char *)&mask)[0],
126 ((unsigned char *)&mask)[1], ((unsigned char *)&mask)[2],
127 ((unsigned char *)&mask)[3]);
128
129 return buf;
130}
131
132static void print_help()
133{
134 printf(
135"ip options:\n"
136"--ip-src [!] address[/mask]: ip source specification\n"
137"--ip-dst [!] address[/mask]: ip destination specification\n"
138"--ip-tos [!] tos : ip tos specification\n"
139"--ip-proto [!] protocol : ip protocol specification\n");
140}
141
142static void init(struct ebt_entry_match *match)
143{
144 struct ebt_ip_info *ipinfo = (struct ebt_ip_info *)match->data;
145
146 ipinfo->invflags = 0;
147 ipinfo->bitmask = 0;
148}
149
150#define OPT_SOURCE 0x01
151#define OPT_DEST 0x02
152#define OPT_TOS 0x04
153#define OPT_PROTO 0x08
154static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
155 unsigned int *flags, struct ebt_entry_match **match)
156{
157 struct ebt_ip_info *ipinfo = (struct ebt_ip_info *)(*match)->data;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000158 char *end;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000159 int i;
160
161 switch (c) {
162 case IP_SOURCE:
163 check_option(flags, OPT_SOURCE);
164 ipinfo->bitmask |= EBT_IP_SOURCE;
165
166 case IP_DEST:
167 if (c == IP_DEST) {
168 check_option(flags, OPT_DEST);
169 ipinfo->bitmask |= EBT_IP_DEST;
170 }
171 if (check_inverse(optarg)) {
172 if (c == IP_SOURCE)
173 ipinfo->invflags |= EBT_IP_SOURCE;
174 else
175 ipinfo->invflags |= EBT_IP_DEST;
176 }
177
178 if (optind > argc)
179 print_error("Missing ip address argument");
180 if (c == IP_SOURCE)
181 parse_ip_address(argv[optind - 1], &ipinfo->saddr,
182 &ipinfo->smsk);
183 else
184 parse_ip_address(argv[optind - 1], &ipinfo->daddr,
185 &ipinfo->dmsk);
186 break;
187
188 case IP_myTOS:
189 check_option(flags, OPT_TOS);
190 if (check_inverse(optarg))
191 ipinfo->invflags |= EBT_IP_TOS;
192
193 if (optind > argc)
194 print_error("Missing ip tos argument");
195 i = strtol(argv[optind - 1], &end, 16);
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000196 if (i < 0 || i > 255 || *end != '\0')
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000197 print_error("Problem with specified ip tos");
198 ipinfo->tos = i;
199 ipinfo->bitmask |= EBT_IP_TOS;
200 break;
201
202 case IP_PROTO:
203 check_option(flags, OPT_PROTO);
204 if (check_inverse(optarg))
205 ipinfo->invflags |= EBT_IP_PROTO;
206 if (optind > argc)
207 print_error("Missing ip protocol argument");
208 i = strtol(argv[optind - 1], &end, 10);
209 if (i < 0 || i > 255 || *end != '\0')
210 print_error("Problem with specified ip protocol");
211 ipinfo->protocol = i;
212 ipinfo->bitmask |= EBT_IP_PROTO;
213 break;
214 default:
215 return 0;
216 }
217 return 1;
218}
219
220static void final_check(const struct ebt_u_entry *entry,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000221 const struct ebt_entry_match *match, const char *name,
222 unsigned int hook_mask, unsigned int time)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000223{
224 if (entry->bitmask & EBT_NOPROTO || entry->bitmask & EBT_802_3 ||
Bart De Schuymerb2632c52002-08-09 18:57:05 +0000225 entry->ethproto != ETH_P_IP || entry->invflags & EBT_IPROTO)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000226 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) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000237 printf("--ip-src ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000238 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) ? "" : ".");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000243 printf("%s ", mask_to_dotted(ipinfo->smsk));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000244 }
245 if (ipinfo->bitmask & EBT_IP_DEST) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000246 printf("--ip-dst ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000247 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) ? "" : ".");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000252 printf("%s ", mask_to_dotted(ipinfo->dmsk));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000253 }
254 if (ipinfo->bitmask & EBT_IP_TOS) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000255 printf("--ip-tos ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000256 if (ipinfo->invflags & EBT_IP_TOS)
257 printf("! ");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000258 printf("0x%02X ", ipinfo->tos);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000259 }
260 if (ipinfo->bitmask & EBT_IP_PROTO) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000261 printf("--ip-proto ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000262 if (ipinfo->invflags & EBT_IP_DEST)
263 printf("! ");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000264 printf("%d ", ipinfo->protocol);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000265 }
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}