2 * 25-Jul-1998 Major changes to allow for ip chain table
4 * 3-Jan-2000 Named tables to allow packet selection for different uses.
8 * Format of an IP firewall descriptor
10 * src, dst, src_mask, dst_mask are always stored in network byte order.
11 * flags are stored in host byte order (of course).
12 * Port numbers are stored in HOST byte order.
20 #include <linux/types.h>
23 #include <linux/skbuff.h>
25 #include <linux/compiler.h>
26 #include <linux/netfilter_ipv4.h>
28 #define IPT_FUNCTION_MAXNAMELEN 30
29 #define IPT_TABLE_MAXNAMELEN 32
31 /* Yes, Virginia, you have to zero the padding. */
33 /* Source and destination IP addr */
34 struct in_addr src
, dst
;
35 /* Mask for src and dest IP addr */
36 struct in_addr smsk
, dmsk
;
37 char iniface
[IFNAMSIZ
], outiface
[IFNAMSIZ
];
38 unsigned char iniface_mask
[IFNAMSIZ
], outiface_mask
[IFNAMSIZ
];
40 /* Protocol, 0 = ANY */
49 struct ipt_entry_match
55 /* Used by userspace */
56 char name
[IPT_FUNCTION_MAXNAMELEN
-1];
63 /* Used inside the kernel */
64 struct ipt_match
*match
;
71 unsigned char data
[0];
74 struct ipt_entry_target
78 u_int16_t target_size
;
80 /* Used by userspace */
81 char name
[IPT_FUNCTION_MAXNAMELEN
-1];
86 u_int16_t target_size
;
88 /* Used inside the kernel */
89 struct ipt_target
*target
;
93 u_int16_t target_size
;
96 unsigned char data
[0];
99 struct ipt_standard_target
101 struct ipt_entry_target target
;
107 u_int64_t pcnt
, bcnt
; /* Packet and byte counters */
110 /* Values for "flag" field in struct ipt_ip (general ip structure). */
111 #define IPT_F_FRAG 0x01 /* Set if rule is a fragment rule */
112 #define IPT_F_MASK 0x01 /* All possible flag bits mask. */
114 /* Values for "inv" field in struct ipt_ip. */
115 #define IPT_INV_VIA_IN 0x01 /* Invert the sense of IN IFACE. */
116 #define IPT_INV_VIA_OUT 0x02 /* Invert the sense of OUT IFACE */
117 #define IPT_INV_TOS 0x04 /* Invert the sense of TOS. */
118 #define IPT_INV_SRCIP 0x08 /* Invert the sense of SRC IP. */
119 #define IPT_INV_DSTIP 0x10 /* Invert the sense of DST OP. */
120 #define IPT_INV_FRAG 0x20 /* Invert the sense of FRAG. */
121 #define IPT_INV_PROTO 0x40 /* Invert the sense of PROTO. */
122 #define IPT_INV_MASK 0x7F /* All possible flag bits mask. */
124 /* This structure defines each of the firewall rules. Consists of 3
125 parts which are 1) general IP header stuff 2) match specific
126 stuff 3) the target to perform if the rule matches */
131 /* Mark with fields that we care about. */
132 unsigned int nfcache
;
134 /* Size of ipt_entry + matches */
135 u_int16_t target_offset
;
136 /* Size of ipt_entry + matches + target */
137 u_int16_t next_offset
;
140 unsigned int comefrom
;
142 /* Packet and byte counters. */
143 struct ipt_counters counters
;
145 /* The matches (if any), then the target. */
146 unsigned char elems
[0];
150 * New IP firewall options for [gs]etsockopt at the RAW IP level.
151 * Unlike BSD Linux inherits IP options so you don't have to use a raw
152 * socket for this. Instead we check rights in the calls. */
153 #define IPT_BASE_CTL 64 /* base for firewall socket options */
155 #define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
156 #define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1)
157 #define IPT_SO_SET_MAX IPT_SO_SET_ADD_COUNTERS
159 #define IPT_SO_GET_INFO (IPT_BASE_CTL)
160 #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)
161 #define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
162 #define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
163 #define IPT_SO_GET_MAX IPT_SO_GET_REVISION_TARGET
165 /* CONTINUE verdict for targets */
166 #define IPT_CONTINUE 0xFFFFFFFF
168 /* For standard target */
169 #define IPT_RETURN (-NF_REPEAT - 1)
171 /* TCP matching stuff */
174 u_int16_t spts
[2]; /* Source port range. */
175 u_int16_t dpts
[2]; /* Destination port range. */
176 u_int8_t option
; /* TCP Option iff non-zero*/
177 u_int8_t flg_mask
; /* TCP flags mask byte */
178 u_int8_t flg_cmp
; /* TCP flags compare byte */
179 u_int8_t invflags
; /* Inverse flags */
182 /* Values for "inv" field in struct ipt_tcp. */
183 #define IPT_TCP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
184 #define IPT_TCP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
185 #define IPT_TCP_INV_FLAGS 0x04 /* Invert the sense of TCP flags. */
186 #define IPT_TCP_INV_OPTION 0x08 /* Invert the sense of option test. */
187 #define IPT_TCP_INV_MASK 0x0F /* All possible flags. */
189 /* UDP matching stuff */
192 u_int16_t spts
[2]; /* Source port range. */
193 u_int16_t dpts
[2]; /* Destination port range. */
194 u_int8_t invflags
; /* Inverse flags */
197 /* Values for "invflags" field in struct ipt_udp. */
198 #define IPT_UDP_INV_SRCPT 0x01 /* Invert the sense of source ports. */
199 #define IPT_UDP_INV_DSTPT 0x02 /* Invert the sense of dest ports. */
200 #define IPT_UDP_INV_MASK 0x03 /* All possible flags. */
202 /* ICMP matching stuff */
205 u_int8_t type
; /* type to match */
206 u_int8_t code
[2]; /* range of code */
207 u_int8_t invflags
; /* Inverse flags */
210 /* Values for "inv" field for struct ipt_icmp. */
211 #define IPT_ICMP_INV 0x01 /* Invert the sense of type/code test */
213 /* The argument to IPT_SO_GET_INFO */
216 /* Which table: caller fills this in. */
217 char name
[IPT_TABLE_MAXNAMELEN
];
219 /* Kernel fills these in. */
220 /* Which hook entry points are valid: bitmask */
221 unsigned int valid_hooks
;
223 /* Hook entry points: one per netfilter hook. */
224 unsigned int hook_entry
[NF_IP_NUMHOOKS
];
226 /* Underflow points. */
227 unsigned int underflow
[NF_IP_NUMHOOKS
];
229 /* Number of entries */
230 unsigned int num_entries
;
232 /* Size of entries. */
236 /* The argument to IPT_SO_SET_REPLACE. */
240 char name
[IPT_TABLE_MAXNAMELEN
];
242 /* Which hook entry points are valid: bitmask. You can't
244 unsigned int valid_hooks
;
246 /* Number of entries */
247 unsigned int num_entries
;
249 /* Total size of new entries */
252 /* Hook entry points. */
253 unsigned int hook_entry
[NF_IP_NUMHOOKS
];
255 /* Underflow points. */
256 unsigned int underflow
[NF_IP_NUMHOOKS
];
258 /* Information about old entries: */
259 /* Number of counters (must be equal to current number of entries). */
260 unsigned int num_counters
;
261 /* The old entries' counters. */
262 struct ipt_counters __user
*counters
;
264 /* The entries (hang off end: not really an array). */
265 struct ipt_entry entries
[0];
268 /* The argument to IPT_SO_ADD_COUNTERS. */
269 struct ipt_counters_info
272 char name
[IPT_TABLE_MAXNAMELEN
];
274 unsigned int num_counters
;
276 /* The counters (actually `number' of these). */
277 struct ipt_counters counters
[0];
280 /* The argument to IPT_SO_GET_ENTRIES. */
281 struct ipt_get_entries
283 /* Which table: user fills this in. */
284 char name
[IPT_TABLE_MAXNAMELEN
];
286 /* User fills this in: total entry size. */
290 struct ipt_entry entrytable
[0];
293 /* The argument to IPT_SO_GET_REVISION_*. Returns highest revision
294 * kernel supports, if >= revision. */
295 struct ipt_get_revision
297 char name
[IPT_FUNCTION_MAXNAMELEN
-1];
302 /* Standard return verdict, or do jump. */
303 #define IPT_STANDARD_TARGET ""
305 #define IPT_ERROR_TARGET "ERROR"
307 /* Helper functions */
308 static __inline__
struct ipt_entry_target
*
309 ipt_get_target(struct ipt_entry
*e
)
311 return (void *)e
+ e
->target_offset
;
314 /* fn returns 0 to continue iteration */
315 #define IPT_MATCH_ITERATE(e, fn, args...) \
319 struct ipt_entry_match *__match; \
321 for (__i = sizeof(struct ipt_entry); \
322 __i < (e)->target_offset; \
323 __i += __match->u.match_size) { \
324 __match = (void *)(e) + __i; \
326 __ret = fn(__match , ## args); \
333 /* fn returns 0 to continue iteration */
334 #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
338 struct ipt_entry *__entry; \
340 for (__i = 0; __i < (size); __i += __entry->next_offset) { \
341 __entry = (void *)(entries) + __i; \
343 __ret = fn(__entry , ## args); \
351 * Main firewall chains definitions and global var's definitions.
355 #include <linux/init.h>
356 extern void ipt_init(void) __init
;
360 struct list_head list
;
362 const char name
[IPT_FUNCTION_MAXNAMELEN
-1];
366 /* Return true or false: return FALSE and set *hotdrop = 1 to
367 force immediate packet drop. */
368 /* Arguments changed since 2.4, as this must now handle
369 non-linear skbs, using skb_copy_bits and
370 skb_ip_make_writable. */
371 int (*match
)(const struct sk_buff
*skb
,
372 const struct net_device
*in
,
373 const struct net_device
*out
,
374 const void *matchinfo
,
378 /* Called when user tries to insert an entry of this type. */
379 /* Should return true or false. */
380 int (*checkentry
)(const char *tablename
,
381 const struct ipt_ip
*ip
,
383 unsigned int matchinfosize
,
384 unsigned int hook_mask
);
386 /* Called when entry of this type deleted. */
387 void (*destroy
)(void *matchinfo
, unsigned int matchinfosize
);
389 /* Set this to THIS_MODULE. */
393 /* Registration hooks for targets. */
396 struct list_head list
;
398 const char name
[IPT_FUNCTION_MAXNAMELEN
-1];
402 /* Called when user tries to insert an entry of this type:
403 hook_mask is a bitmask of hooks from which it can be
405 /* Should return true or false. */
406 int (*checkentry
)(const char *tablename
,
407 const struct ipt_entry
*e
,
409 unsigned int targinfosize
,
410 unsigned int hook_mask
);
412 /* Called when entry of this type deleted. */
413 void (*destroy
)(void *targinfo
, unsigned int targinfosize
);
415 /* Returns verdict. Argument order changed since 2.4, as this
416 must now handle non-linear skbs, using skb_copy_bits and
417 skb_ip_make_writable. */
418 unsigned int (*target
)(struct sk_buff
**pskb
,
419 const struct net_device
*in
,
420 const struct net_device
*out
,
421 unsigned int hooknum
,
422 const void *targinfo
,
425 /* Set this to THIS_MODULE. */
429 extern int ipt_register_target(struct ipt_target
*target
);
430 extern void ipt_unregister_target(struct ipt_target
*target
);
432 extern int ipt_register_match(struct ipt_match
*match
);
433 extern void ipt_unregister_match(struct ipt_match
*match
);
435 /* Furniture shopping... */
438 struct list_head list
;
440 /* A unique name... */
441 char name
[IPT_TABLE_MAXNAMELEN
];
443 /* What hooks you will enter on */
444 unsigned int valid_hooks
;
446 /* Lock for the curtain */
449 /* Man behind the curtain... */
450 struct ipt_table_info
*private;
452 /* Set to THIS_MODULE. */
456 /* net/sched/ipt.c: Gimme access to your targets! Gets target->me. */
457 extern struct ipt_target
*ipt_find_target(const char *name
, u8 revision
);
459 /* Standard entry. */
462 struct ipt_entry entry
;
463 struct ipt_standard_target target
;
466 struct ipt_error_target
468 struct ipt_entry_target target
;
469 char errorname
[IPT_FUNCTION_MAXNAMELEN
];
474 struct ipt_entry entry
;
475 struct ipt_error_target target
;
478 extern int ipt_register_table(struct ipt_table
*table
,
479 const struct ipt_replace
*repl
);
480 extern void ipt_unregister_table(struct ipt_table
*table
);
481 extern unsigned int ipt_do_table(struct sk_buff
**pskb
,
483 const struct net_device
*in
,
484 const struct net_device
*out
,
485 struct ipt_table
*table
,
488 #define IPT_ALIGN(s) (((s) + (__alignof__(struct ipt_entry)-1)) & ~(__alignof__(struct ipt_entry)-1))
489 #endif /*__KERNEL__*/
490 #endif /* _IPTABLES_H */