xref: /freebsd/sbin/ipfw/ipfw2.h (revision b61a573019428cbdc38ed03be9ecd2482011ea66)
1 /*-
2  * Copyright (c) 2002-2003 Luigi Rizzo
3  * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4  * Copyright (c) 1994 Ugen J.S.Antsilevich
5  *
6  * Idea and grammar partially left from:
7  * Copyright (c) 1993 Daniel Boulet
8  *
9  * Redistribution and use in source forms, with and without modification,
10  * are permitted provided that this entire comment appears intact.
11  *
12  * Redistribution in binary form may occur without any restrictions.
13  * Obviously, it would be nice if you gave credit where credit is due
14  * but requiring it would be too onerous.
15  *
16  * This software is provided ``AS IS'' without any warranties of any kind.
17  *
18  * NEW command line interface for IP firewall facility
19  *
20  * $FreeBSD$
21  */
22 
23 enum cmdline_prog {
24 	cmdline_prog_ipfw,
25 	cmdline_prog_dnctl
26 };
27 
28 /*
29  * Options that can be set on the command line.
30  * When reading commands from a file, a subset of the options can also
31  * be applied globally by specifying them before the file name.
32  * After that, each line can contain its own option that changes
33  * the global value.
34  * XXX The context is not restored after each line.
35  */
36 
37 struct cmdline_opts {
38 	/* boolean options: */
39 	int	do_value_as_ip;	/* show table value as IP */
40 	int	do_resolv;	/* try to resolve all ip to names */
41 	int	do_time;	/* Show time stamps */
42 	int	do_quiet;	/* Be quiet in add and flush */
43 	int	do_pipe;	/* this cmd refers to a pipe/queue/sched */
44 	int	do_nat; 	/* this cmd refers to a nat config */
45 	int	do_compact;	/* show rules in compact mode */
46 	int	do_force;	/* do not ask for confirmation */
47 	int	show_sets;	/* display the set each rule belongs to */
48 	int	test_only;	/* only check syntax */
49 	int	comment_only;	/* only print action and comment */
50 	int	verbose;	/* be verbose on some commands */
51 
52 	/* The options below can have multiple values. */
53 
54 	int	do_dynamic;	/* 1 - display dynamic rules */
55 				/* 2 - display/delete only dynamic rules */
56 	int	do_sort;	/* field to sort results (0 = no) */
57 		/* valid fields are 1 and above */
58 
59 	uint32_t use_set;	/* work with specified set number */
60 		/* 0 means all sets, otherwise apply to set use_set - 1 */
61 
62 	enum cmdline_prog	prog;	/* Are we ipfw or dnctl? */
63 };
64 
65 int is_ipfw(void);
66 
67 enum {
68 	TIMESTAMP_NONE = 0,
69 	TIMESTAMP_STRING,
70 	TIMESTAMP_NUMERIC,
71 };
72 
73 extern struct cmdline_opts g_co;
74 
75 /*
76  * _s_x is a structure that stores a string <-> token pairs, used in
77  * various places in the parser. Entries are stored in arrays,
78  * with an entry with s=NULL as terminator.
79  * The search routines are match_token() and match_value().
80  * Often, an element with x=0 contains an error string.
81  *
82  */
83 struct _s_x {
84 	char const *s;
85 	int x;
86 };
87 
88 extern struct _s_x f_ipdscp[];
89 
90 enum tokens {
91 	TOK_NULL=0,
92 
93 	TOK_OR,
94 	TOK_NOT,
95 	TOK_STARTBRACE,
96 	TOK_ENDBRACE,
97 
98 	TOK_ABORT6,
99 	TOK_ABORT,
100 	TOK_ACCEPT,
101 	TOK_COUNT,
102 	TOK_EACTION,
103 	TOK_PIPE,
104 	TOK_LINK,
105 	TOK_QUEUE,
106 	TOK_FLOWSET,
107 	TOK_SCHED,
108 	TOK_DIVERT,
109 	TOK_TEE,
110 	TOK_NETGRAPH,
111 	TOK_NGTEE,
112 	TOK_FORWARD,
113 	TOK_SKIPTO,
114 	TOK_DENY,
115 	TOK_REJECT,
116 	TOK_RESET,
117 	TOK_UNREACH,
118 	TOK_CHECKSTATE,
119 	TOK_NAT,
120 	TOK_REASS,
121 	TOK_CALL,
122 	TOK_RETURN,
123 
124 	TOK_ALTQ,
125 	TOK_LOG,
126 	TOK_TAG,
127 	TOK_UNTAG,
128 
129 	TOK_TAGGED,
130 	TOK_UID,
131 	TOK_GID,
132 	TOK_JAIL,
133 	TOK_IN,
134 	TOK_LIMIT,
135 	TOK_SETLIMIT,
136 	TOK_KEEPSTATE,
137 	TOK_RECORDSTATE,
138 	TOK_LAYER2,
139 	TOK_OUT,
140 	TOK_DIVERTED,
141 	TOK_DIVERTEDLOOPBACK,
142 	TOK_DIVERTEDOUTPUT,
143 	TOK_XMIT,
144 	TOK_RECV,
145 	TOK_VIA,
146 	TOK_FRAG,
147 	TOK_IPOPTS,
148 	TOK_IPLEN,
149 	TOK_IPID,
150 	TOK_IPPRECEDENCE,
151 	TOK_DSCP,
152 	TOK_IPTOS,
153 	TOK_IPTTL,
154 	TOK_IPVER,
155 	TOK_ESTAB,
156 	TOK_SETUP,
157 	TOK_TCPDATALEN,
158 	TOK_TCPFLAGS,
159 	TOK_TCPOPTS,
160 	TOK_TCPSEQ,
161 	TOK_TCPACK,
162 	TOK_TCPMSS,
163 	TOK_TCPWIN,
164 	TOK_ICMPTYPES,
165 	TOK_MAC,
166 	TOK_MACTYPE,
167 	TOK_VERREVPATH,
168 	TOK_VERSRCREACH,
169 	TOK_ANTISPOOF,
170 	TOK_IPSEC,
171 	TOK_COMMENT,
172 
173 	TOK_PLR,
174 	TOK_NOERROR,
175 	TOK_BUCKETS,
176 	TOK_DSTIP,
177 	TOK_SRCIP,
178 	TOK_DSTPORT,
179 	TOK_SRCPORT,
180 	TOK_DSTMAC,
181 	TOK_SRCMAC,
182 	TOK_ALL,
183 	TOK_MASK,
184 	TOK_FLOW_MASK,
185 	TOK_SCHED_MASK,
186 	TOK_BW,
187 	TOK_DELAY,
188 	TOK_PROFILE,
189 	TOK_BURST,
190 	TOK_RED,
191 	TOK_GRED,
192 	TOK_ECN,
193 	TOK_DROPTAIL,
194 	TOK_PROTO,
195 #ifdef NEW_AQM
196 	/* AQM tokens*/
197 	TOK_NO_ECN,
198 	TOK_CODEL,
199 	TOK_FQ_CODEL,
200 	TOK_TARGET,
201 	TOK_INTERVAL,
202 	TOK_FLOWS,
203 	TOK_QUANTUM,
204 
205 	TOK_PIE,
206 	TOK_FQ_PIE,
207 	TOK_TUPDATE,
208 	TOK_MAX_BURST,
209 	TOK_MAX_ECNTH,
210 	TOK_ALPHA,
211 	TOK_BETA,
212 	TOK_CAPDROP,
213 	TOK_NO_CAPDROP,
214 	TOK_ONOFF,
215 	TOK_DRE,
216 	TOK_TS,
217 	TOK_DERAND,
218 	TOK_NO_DERAND,
219 #endif
220 	/* dummynet tokens */
221 	TOK_WEIGHT,
222 	TOK_LMAX,
223 	TOK_PRI,
224 	TOK_TYPE,
225 	TOK_SLOTSIZE,
226 
227 	TOK_IP,
228 	TOK_IF,
229 	TOK_ALOG,
230 	TOK_DENY_INC,
231 	TOK_SAME_PORTS,
232 	TOK_UNREG_ONLY,
233 	TOK_UNREG_CGN,
234 	TOK_SKIP_GLOBAL,
235 	TOK_RESET_ADDR,
236 	TOK_ALIAS_REV,
237 	TOK_PROXY_ONLY,
238 	TOK_REDIR_ADDR,
239 	TOK_REDIR_PORT,
240 	TOK_REDIR_PROTO,
241 
242 	TOK_IPV6,
243 	TOK_FLOWID,
244 	TOK_ICMP6TYPES,
245 	TOK_EXT6HDR,
246 	TOK_DSTIP6,
247 	TOK_SRCIP6,
248 
249 	TOK_IPV4,
250 	TOK_UNREACH6,
251 	TOK_RESET6,
252 
253 	TOK_FIB,
254 	TOK_SETFIB,
255 	TOK_LOOKUP,
256 	TOK_SOCKARG,
257 	TOK_SETDSCP,
258 	TOK_FLOW,
259 	TOK_IFLIST,
260 	/* Table tokens */
261 	TOK_CREATE,
262 	TOK_DESTROY,
263 	TOK_LIST,
264 	TOK_INFO,
265 	TOK_DETAIL,
266 	TOK_MODIFY,
267 	TOK_FLUSH,
268 	TOK_SWAP,
269 	TOK_ADD,
270 	TOK_DEL,
271 	TOK_VALTYPE,
272 	TOK_ALGO,
273 	TOK_TALIST,
274 	TOK_ATOMIC,
275 	TOK_LOCK,
276 	TOK_UNLOCK,
277 	TOK_VLIST,
278 	TOK_OLIST,
279 	TOK_MISSING,
280 	TOK_ORFLUSH,
281 
282 	/* NAT64 tokens */
283 	TOK_NAT64STL,
284 	TOK_NAT64LSN,
285 	TOK_STATS,
286 	TOK_STATES,
287 	TOK_CONFIG,
288 	TOK_TABLE4,
289 	TOK_TABLE6,
290 	TOK_PREFIX4,
291 	TOK_PREFIX6,
292 	TOK_AGG_LEN,
293 	TOK_AGG_COUNT,
294 	TOK_MAX_PORTS,
295 	TOK_STATES_CHUNKS,
296 	TOK_JMAXLEN,
297 	TOK_PORT_RANGE,
298 	TOK_PORT_ALIAS,
299 	TOK_HOST_DEL_AGE,
300 	TOK_PG_DEL_AGE,
301 	TOK_TCP_SYN_AGE,
302 	TOK_TCP_CLOSE_AGE,
303 	TOK_TCP_EST_AGE,
304 	TOK_UDP_AGE,
305 	TOK_ICMP_AGE,
306 	TOK_LOGOFF,
307 	TOK_PRIVATE,
308 	TOK_PRIVATEOFF,
309 
310 	/* NAT64 CLAT tokens */
311 	TOK_NAT64CLAT,
312 	TOK_PLAT_PREFIX,
313 	TOK_CLAT_PREFIX,
314 
315 	/* NPTv6 tokens */
316 	TOK_NPTV6,
317 	TOK_INTPREFIX,
318 	TOK_EXTPREFIX,
319 	TOK_PREFIXLEN,
320 	TOK_EXTIF,
321 
322 	TOK_TCPSETMSS,
323 
324 	TOK_MARK,
325 	TOK_SETMARK,
326 
327 	TOK_SKIPACTION,
328 };
329 
330 /*
331  * the following macro returns an error message if we run out of
332  * arguments.
333  */
334 #define NEED(_p, msg)      {if (!_p) errx(EX_USAGE, msg);}
335 #define NEED1(msg)      {if (!(*av)) errx(EX_USAGE, msg);}
336 
337 struct buf_pr {
338 	char	*buf;	/* allocated buffer */
339 	char	*ptr;	/* current pointer */
340 	size_t	size;	/* total buffer size */
341 	size_t	avail;	/* available storage */
342 	size_t	needed;	/* length needed */
343 };
344 
345 int pr_u64(struct buf_pr *bp, void *pd, int width);
346 int bp_alloc(struct buf_pr *b, size_t size);
347 void bp_free(struct buf_pr *b);
348 int bprintf(struct buf_pr *b, const char *format, ...);
349 
350 
351 /* memory allocation support */
352 void *safe_calloc(size_t number, size_t size);
353 void *safe_realloc(void *ptr, size_t size);
354 
355 /* string comparison functions used for historical compatibility */
356 int _substrcmp(const char *str1, const char* str2);
357 int _substrcmp2(const char *str1, const char* str2, const char* str3);
358 int stringnum_cmp(const char *a, const char *b);
359 
360 /* utility functions */
361 int match_token(struct _s_x *table, const char *string);
362 int match_token_relaxed(struct _s_x *table, const char *string);
363 int get_token(struct _s_x *table, const char *string, const char *errbase);
364 char const *match_value(struct _s_x *p, int value);
365 size_t concat_tokens(char *buf, size_t bufsize, struct _s_x *table,
366     const char *delimiter);
367 int fill_flags(struct _s_x *flags, char *p, char **e, uint32_t *set,
368     uint32_t *clear);
369 void print_flags_buffer(char *buf, size_t sz, struct _s_x *list, uint32_t set);
370 
371 struct _ip_fw3_opheader;
372 int do_cmd(int optname, void *optval, uintptr_t optlen);
373 int do_set3(int optname, struct _ip_fw3_opheader *op3, size_t optlen);
374 int do_get3(int optname, struct _ip_fw3_opheader *op3, size_t *optlen);
375 
376 struct in6_addr;
377 void n2mask(struct in6_addr *mask, int n);
378 int contigmask(const uint8_t *p, int len);
379 
380 /*
381  * Forward declarations to avoid include way too many headers.
382  * C does not allow duplicated typedefs, so we use the base struct
383  * that the typedef points to.
384  * Should the typedefs use a different type, the compiler will
385  * still detect the change when compiling the body of the
386  * functions involved, so we do not lose error checking.
387  */
388 struct _ipfw_insn;
389 struct _ipfw_insn_altq;
390 struct _ipfw_insn_u32;
391 struct _ipfw_insn_ip6;
392 struct _ipfw_insn_icmp6;
393 
394 /*
395  * The reserved set numer. This is a constant in ip_fw.h
396  * but we store it in a variable so other files do not depend
397  * in that header just for one constant.
398  */
399 extern int resvd_set_number;
400 
401 /* first-level command handlers */
402 void ipfw_add(char *av[]);
403 void ipfw_show_nat(int ac, char **av);
404 int ipfw_delete_nat(int i);
405 void ipfw_config_pipe(int ac, char **av);
406 void ipfw_config_nat(int ac, char **av);
407 void ipfw_sets_handler(char *av[]);
408 void ipfw_table_handler(int ac, char *av[]);
409 void ipfw_sysctl_handler(char *av[], int which);
410 void ipfw_delete(char *av[]);
411 void ipfw_flush(int force);
412 void ipfw_zero(int ac, char *av[], int optname);
413 void ipfw_list(int ac, char *av[], int show_counters);
414 void ipfw_internal_handler(int ac, char *av[]);
415 void ipfw_nat64clat_handler(int ac, char *av[]);
416 void ipfw_nat64lsn_handler(int ac, char *av[]);
417 void ipfw_nat64stl_handler(int ac, char *av[]);
418 void ipfw_nptv6_handler(int ac, char *av[]);
419 int ipfw_check_object_name(const char *name);
420 int ipfw_check_nat64prefix(const struct in6_addr *prefix, int length);
421 
422 #ifdef PF
423 /* altq.c */
424 void altq_set_enabled(int enabled);
425 u_int32_t altq_name_to_qid(const char *name);
426 void print_altq_cmd(struct buf_pr *bp, const struct _ipfw_insn_altq *altqptr);
427 #else
428 #define NO_ALTQ
429 #endif
430 
431 /* dummynet.c */
432 void dummynet_list(int ac, char *av[], int show_counters);
433 void dummynet_flush(void);
434 int ipfw_delete_pipe(int pipe_or_queue, int n);
435 
436 /* ipv6.c */
437 void print_unreach6_code(struct buf_pr *bp, uint16_t code);
438 void print_ip6(struct buf_pr *bp, const struct _ipfw_insn_ip6 *cmd);
439 void print_flow6id(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
440 void print_icmp6types(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
441 void print_ext6hdr(struct buf_pr *bp, const struct _ipfw_insn *cmd);
442 
443 struct tidx;
444 struct _ipfw_insn *add_srcip6(struct _ipfw_insn *cmd, char *av, int cblen,
445     struct tidx *tstate);
446 struct _ipfw_insn *add_dstip6(struct _ipfw_insn *cmd, char *av, int cblen,
447     struct tidx *tstate);
448 
449 void fill_flow6(struct _ipfw_insn_u32 *cmd, char *av, int cblen);
450 void fill_unreach6_code(u_short *codep, char *str);
451 void fill_icmp6types(struct _ipfw_insn_icmp6 *cmd, char *av, int cblen);
452 int fill_ext6hdr(struct _ipfw_insn *cmd, char *av);
453 
454 /* ipfw2.c */
455 void bp_flush(struct buf_pr *b);
456 void fill_table(struct _ipfw_insn *cmd, char *av, uint8_t opcode,
457     struct tidx *tstate);
458 
459 /* tables.c */
460 struct _ipfw_obj_ctlv;
461 struct _ipfw_obj_ntlv;
462 int table_check_name(const char *tablename);
463 void ipfw_list_ta(int ac, char *av[]);
464 void ipfw_list_values(int ac, char *av[]);
465 void table_fill_ntlv(struct _ipfw_obj_ntlv *ntlv, const char *name,
466     uint8_t set, uint16_t uidx);
467 
468