-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables-1.8.11-dnp3fw.patch
337 lines (337 loc) · 9.73 KB
/
iptables-1.8.11-dnp3fw.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
diff -Nur iptables-1.8.11.orig/extensions/libxt_dnp3.c iptables-1.8.11/extensions/libxt_dnp3.c
--- iptables-1.8.11.orig/extensions/libxt_dnp3.c 1970-01-01 00:00:00.000000000 +0000
+++ iptables-1.8.11/extensions/libxt_dnp3.c 2024-12-06 05:33:09.911727921 +0000
@@ -0,0 +1,306 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <netdb.h>
+#include <getopt.h>
+#include <ctype.h>
+#include <xtables.h>
+
+#include <linux/netfilter/xt_dnp3.h>
+
+
+enum {
+ O_CHECKSUM = 0,
+ O_DADDR,
+ O_SADDR,
+ O_FC,
+};
+
+static const struct option dnp3_opts[] = {
+ { .name = "chksum", .has_arg = false, .val = O_CHECKSUM },
+ { .name = "daddr", .has_arg = true, .val = O_DADDR },
+ { .name = "destination-addr", .has_arg = true, .val = O_DADDR },
+ { .name = "fc", .has_arg = true, .val = O_FC },
+ { .name = "function-code", .has_arg = true, .val = O_FC },
+ { .name = "saddr", .has_arg = true, .val = O_SADDR },
+ { .name = "source-addr", .has_arg = true, .val = O_SADDR },
+ XT_GETOPT_TABLEEND,
+};
+
+
+static void dnp3_help( void );
+
+static void dnp3_init( struct xt_entry_match *m );
+
+static int dnp3_parse( int c, char **argv, int invert, unsigned int *flags, const void *entry, struct xt_entry_match **match );
+
+static void dnp3_parse_address( const char *arg, uint16_t *addr );
+
+static void dnp3_parse_function( const char *arg, uint8_t *func );
+
+static int dnp3_parse_isnumber( const char *arg );
+
+static void dnp3_print( const void *ip, const struct xt_entry_match *match, int numeric );
+
+static void dnp3_output_address( const char *name, uint16_t min, uint16_t max, int invert, int flag );
+
+static void dnp3_output_function( const char *name, uint8_t *func, int invert, int flag );
+
+static void dnp3_save( const void *ip, const struct xt_entry_match *match );
+
+
+static void
+dnp3_help( void )
+{
+ printf(
+"dnp3 match options:\n"
+"[!] --destination-addr address[:address]\n"
+" --daddr ...\n"
+"\t\t\t\tdestination address(es)\n"
+"[!] --source-addr address[:address]\n"
+" --saddr ...\n"
+"\t\t\t\tsource address(es)\n"
+"[!] --function-code code[,code]\n"
+" --fc ...\n"
+"\t\t\t\tfunction code(s)\n");
+}
+
+
+static void
+dnp3_init( struct xt_entry_match *m )
+{
+ struct xt_dnp3 *dnp3info = (struct xt_dnp3 *) m->data;
+
+ ( void ) memset( dnp3info, 0, sizeof( *dnp3info ) );
+ dnp3info->daddr[1] = dnp3info->saddr[1]
+ = (uint16_t) ~0U;
+ dnp3info->set = dnp3info->invert = 0;
+}
+
+
+static int
+dnp3_parse( int c, char **argv, int invert, unsigned int *flags, const void *entry, struct xt_entry_match **match )
+{
+ struct xt_dnp3 *dnp3info = ( struct xt_dnp3 * ) (*match)->data;
+ uint8_t flag;
+
+ flag = 0;
+ switch( c ) {
+ case O_CHECKSUM:
+ flag = XT_DNP3_FLAG_CHECKSUM;
+ break;
+ case O_DADDR:
+ if( *flags & XT_DNP3_FLAG_DADDR ) {
+ xtables_error( PARAMETER_PROBLEM,
+ "Only single `--destination-addr` definition allowed" );
+ }
+ dnp3_parse_address( optarg, dnp3info->daddr );
+ flag = XT_DNP3_FLAG_DADDR;
+ break;
+ case O_SADDR:
+ if( *flags & XT_DNP3_FLAG_SADDR ) {
+ xtables_error( PARAMETER_PROBLEM,
+ "Only single `--source-addr` definition allowed" );
+ }
+ dnp3_parse_address( optarg, dnp3info->saddr );
+ flag = XT_DNP3_FLAG_SADDR;
+ break;
+ case O_FC:
+ if( ( *flags & XT_DNP3_FLAG_FC ) &&
+ ( ( dnp3info->invert & XT_DNP3_FLAG_FC ) ||
+ invert ) ) {
+ xtables_error( PARAMETER_PROBLEM,
+ "Only single `--function-code` definition allowed with inversion" );
+ }
+ dnp3_parse_function( optarg, dnp3info->fc );
+ flag = XT_DNP3_FLAG_FC;
+ break;
+ }
+ if( invert ) {
+ dnp3info->invert |= flag;
+ }
+ dnp3info->set |= flag;
+ *flags |= flag;
+
+ return 1;
+}
+
+
+static void
+dnp3_parse_address( const char *arg, uint16_t *addr )
+{
+ char *buffer, *ptr;
+
+ buffer = strdup( arg );
+ if( ( ptr = strchr( buffer, ':' ) ) == NULL ) {
+ addr[0] = addr[1] = xtables_parse_port( buffer, NULL );
+ }
+ else {
+ *ptr++ = '\0';
+
+ addr[0] = buffer[0] ? xtables_parse_port( buffer, NULL ) : 0;
+ addr[1] = ptr[0] ? xtables_parse_port( ptr, NULL ) : 0xffff;
+ if( addr[0] > addr[1] ) {
+ xtables_error( PARAMETER_PROBLEM,
+ "Invalid DNP3 address range (min > max)" );
+ }
+ }
+ free( buffer );
+}
+
+
+static void
+dnp3_parse_function( const char *arg, uint8_t *func )
+{
+ char *buffer, *ptr;
+ uint8_t val;
+
+ buffer = strdup( arg );
+ for( ptr = strtok( buffer, "," );
+ ptr;
+ ptr = strtok( NULL, "," ) ) {
+
+ if( dnp3_parse_isnumber( ptr ) == 0 ) {
+ xtables_error( PARAMETER_PROBLEM,
+ "Only numeric DNP3 function codes accepted" );
+ }
+ val = ( uint8_t ) strtoul( ptr, NULL, 10 );
+ func[ val / 8 ] |= ( 1 << ( val % 8 ) );
+ }
+ free( buffer );
+}
+
+
+static int
+dnp3_parse_isnumber( const char *arg )
+{
+ if( arg == NULL ) {
+ return 0;
+ }
+ while( *arg ) {
+ if( isdigit( *arg++ ) == 0 ) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+
+static void
+dnp3_print( const void *ip, const struct xt_entry_match *match, int numeric )
+{
+ struct xt_dnp3 *dnp3info = (struct xt_dnp3 *) match->data;
+
+ printf( " dnp3" );
+
+ printf( "%s%s",
+ ( dnp3info->invert & XT_DNP3_FLAG_CHECKSUM ) ? " !" : "",
+ ( dnp3info->set & XT_DNP3_FLAG_CHECKSUM ) ? " chksum" : "" );
+
+ dnp3_output_address( "daddr",
+ dnp3info->daddr[0],
+ dnp3info->daddr[1],
+ dnp3info->invert & XT_DNP3_FLAG_DADDR,
+ dnp3info->set & XT_DNP3_FLAG_DADDR );
+ dnp3_output_address( "saddr",
+ dnp3info->saddr[0],
+ dnp3info->saddr[1],
+ dnp3info->invert & XT_DNP3_FLAG_SADDR,
+ dnp3info->set & XT_DNP3_FLAG_SADDR );
+ dnp3_output_function( "fc",
+ dnp3info->fc,
+ dnp3info->invert & XT_DNP3_FLAG_FC,
+ dnp3info->set & XT_DNP3_FLAG_FC );
+}
+
+
+static void
+dnp3_output_address( const char *name, uint16_t min, uint16_t max, int invert, int flag )
+{
+ if( ! flag ) {
+ return;
+ }
+
+ printf( " %s%s ", invert ? "! " : "", name );
+ if( min != max ) {
+ printf( "%u:%u",
+ min,
+ max );
+ }
+ else {
+ printf( "%u", min );
+ }
+}
+
+static void
+dnp3_output_function( const char *name, uint8_t *func, int invert, int flag )
+{
+ uint8_t bit, byte, count;
+
+ if( ! flag ) {
+ return;
+ }
+
+ printf( " %s%s ", invert ? "! " : "", name );
+
+ count = 0;
+ for( byte = 0; byte < 32; ++byte ) {
+ if( func[ byte ] == 0 ) {
+ continue;
+ }
+ for( bit = 0; bit < 8; ++bit ) {
+ if( ( func[ byte ] & ( 1 << bit ) ) != 0 ) {
+ printf( "%s%u", ( ++count > 1 ) ? "," : "", ( ( 8 * byte ) + bit ) );
+ }
+ }
+ }
+}
+
+
+static void
+dnp3_save( const void *ip, const struct xt_entry_match *match )
+{
+ struct xt_dnp3 *dnp3info = (struct xt_dnp3 *) match->data;
+
+ printf( "%s%s",
+ ( dnp3info->invert & XT_DNP3_FLAG_CHECKSUM ) ? "! " : "",
+ ( dnp3info->set & XT_DNP3_FLAG_CHECKSUM ) ? "--chksum" : "" );
+
+ dnp3_output_address( "--daddr",
+ dnp3info->daddr[0],
+ dnp3info->daddr[0],
+ dnp3info->invert & XT_DNP3_FLAG_DADDR,
+ dnp3info->set & XT_DNP3_FLAG_DADDR );
+ dnp3_output_address( "--saddr",
+ dnp3info->saddr[0],
+ dnp3info->saddr[0],
+ dnp3info->invert & XT_DNP3_FLAG_SADDR,
+ dnp3info->set & XT_DNP3_FLAG_SADDR );
+ dnp3_output_function( "--fc",
+ dnp3info->fc,
+ dnp3info->invert & XT_DNP3_FLAG_FC,
+ dnp3info->set & XT_DNP3_FLAG_FC );
+}
+
+
+static struct xtables_match dnp3_match = {
+ .family = NFPROTO_UNSPEC,
+ .name = "dnp3",
+ .version = XTABLES_VERSION,
+ .size = XT_ALIGN( sizeof( struct xt_dnp3 ) ),
+ .userspacesize = XT_ALIGN( sizeof( struct xt_dnp3 ) ),
+ .help = dnp3_help,
+ .init = dnp3_init,
+ .parse = dnp3_parse,
+ .print = dnp3_print,
+ .save = dnp3_save,
+ .extra_opts = dnp3_opts,
+};
+
+
+void
+_init( void)
+{
+ xtables_register_match( &dnp3_match );
+}
diff -Nur iptables-1.8.11.orig/include/linux/netfilter/xt_dnp3.h iptables-1.8.11/include/linux/netfilter/xt_dnp3.h
--- iptables-1.8.11.orig/include/linux/netfilter/xt_dnp3.h 1970-01-01 00:00:00.000000000 +0000
+++ iptables-1.8.11/include/linux/netfilter/xt_dnp3.h 2024-12-06 05:33:23.783443771 +0000
@@ -0,0 +1,23 @@
+#ifndef _XT_DNP3_H
+#define _XT_DNP3_H
+
+
+#include <linux/types.h>
+
+
+struct xt_dnp3 {
+ __u16 daddr[2]; /* Destination address */
+ __u16 saddr[2]; /* Source address */
+ __u8 fc[32]; /* Function code */
+ __u32 set; /* Set flags */
+ __u32 invert; /* Invert flags */
+};
+
+#define XT_DNP3_FLAG_CHECKSUM (0x00000001)
+#define XT_DNP3_FLAG_DADDR (0x00000002)
+#define XT_DNP3_FLAG_SADDR (0x00000004)
+#define XT_DNP3_FLAG_FC (0x00000008)
+#define XT_DNP3_FLAG_MASK (0x0000000f)
+
+
+#endif