xref: /freebsd/sbin/ipfw/ipfw2.h (revision 9768746b)
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_SKIPACTION,
325 };
326 
327 /*
328  * the following macro returns an error message if we run out of
329  * arguments.
330  */
331 #define NEED(_p, msg)      {if (!_p) errx(EX_USAGE, msg);}
332 #define NEED1(msg)      {if (!(*av)) errx(EX_USAGE, msg);}
333 
334 struct buf_pr {
335 	char	*buf;	/* allocated buffer */
336 	char	*ptr;	/* current pointer */
337 	size_t	size;	/* total buffer size */
338 	size_t	avail;	/* available storage */
339 	size_t	needed;	/* length needed */
340 };
341 
342 int pr_u64(struct buf_pr *bp, void *pd, int width);
343 int bp_alloc(struct buf_pr *b, size_t size);
344 void bp_free(struct buf_pr *b);
345 int bprintf(struct buf_pr *b, const char *format, ...);
346 
347 
348 /* memory allocation support */
349 void *safe_calloc(size_t number, size_t size);
350 void *safe_realloc(void *ptr, size_t size);
351 
352 /* string comparison functions used for historical compatibility */
353 int _substrcmp(const char *str1, const char* str2);
354 int _substrcmp2(const char *str1, const char* str2, const char* str3);
355 int stringnum_cmp(const char *a, const char *b);
356 
357 /* utility functions */
358 int match_token(struct _s_x *table, const char *string);
359 int match_token_relaxed(struct _s_x *table, const char *string);
360 int get_token(struct _s_x *table, const char *string, const char *errbase);
361 char const *match_value(struct _s_x *p, int value);
362 size_t concat_tokens(char *buf, size_t bufsize, struct _s_x *table,
363     const char *delimiter);
364 int fill_flags(struct _s_x *flags, char *p, char **e, uint32_t *set,
365     uint32_t *clear);
366 void print_flags_buffer(char *buf, size_t sz, struct _s_x *list, uint32_t set);
367 
368 struct _ip_fw3_opheader;
369 int do_cmd(int optname, void *optval, uintptr_t optlen);
370 int do_set3(int optname, struct _ip_fw3_opheader *op3, size_t optlen);
371 int do_get3(int optname, struct _ip_fw3_opheader *op3, size_t *optlen);
372 
373 struct in6_addr;
374 void n2mask(struct in6_addr *mask, int n);
375 int contigmask(const uint8_t *p, int len);
376 
377 /*
378  * Forward declarations to avoid include way too many headers.
379  * C does not allow duplicated typedefs, so we use the base struct
380  * that the typedef points to.
381  * Should the typedefs use a different type, the compiler will
382  * still detect the change when compiling the body of the
383  * functions involved, so we do not lose error checking.
384  */
385 struct _ipfw_insn;
386 struct _ipfw_insn_altq;
387 struct _ipfw_insn_u32;
388 struct _ipfw_insn_ip6;
389 struct _ipfw_insn_icmp6;
390 
391 /*
392  * The reserved set numer. This is a constant in ip_fw.h
393  * but we store it in a variable so other files do not depend
394  * in that header just for one constant.
395  */
396 extern int resvd_set_number;
397 
398 /* first-level command handlers */
399 void ipfw_add(char *av[]);
400 void ipfw_show_nat(int ac, char **av);
401 int ipfw_delete_nat(int i);
402 void ipfw_config_pipe(int ac, char **av);
403 void ipfw_config_nat(int ac, char **av);
404 void ipfw_sets_handler(char *av[]);
405 void ipfw_table_handler(int ac, char *av[]);
406 void ipfw_sysctl_handler(char *av[], int which);
407 void ipfw_delete(char *av[]);
408 void ipfw_flush(int force);
409 void ipfw_zero(int ac, char *av[], int optname);
410 void ipfw_list(int ac, char *av[], int show_counters);
411 void ipfw_internal_handler(int ac, char *av[]);
412 void ipfw_nat64clat_handler(int ac, char *av[]);
413 void ipfw_nat64lsn_handler(int ac, char *av[]);
414 void ipfw_nat64stl_handler(int ac, char *av[]);
415 void ipfw_nptv6_handler(int ac, char *av[]);
416 int ipfw_check_object_name(const char *name);
417 int ipfw_check_nat64prefix(const struct in6_addr *prefix, int length);
418 
419 #ifdef PF
420 /* altq.c */
421 void altq_set_enabled(int enabled);
422 u_int32_t altq_name_to_qid(const char *name);
423 void print_altq_cmd(struct buf_pr *bp, const struct _ipfw_insn_altq *altqptr);
424 #else
425 #define NO_ALTQ
426 #endif
427 
428 /* dummynet.c */
429 void dummynet_list(int ac, char *av[], int show_counters);
430 void dummynet_flush(void);
431 int ipfw_delete_pipe(int pipe_or_queue, int n);
432 
433 /* ipv6.c */
434 void print_unreach6_code(struct buf_pr *bp, uint16_t code);
435 void print_ip6(struct buf_pr *bp, const struct _ipfw_insn_ip6 *cmd);
436 void print_flow6id(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
437 void print_icmp6types(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
438 void print_ext6hdr(struct buf_pr *bp, const struct _ipfw_insn *cmd);
439 
440 struct tidx;
441 struct _ipfw_insn *add_srcip6(struct _ipfw_insn *cmd, char *av, int cblen,
442     struct tidx *tstate);
443 struct _ipfw_insn *add_dstip6(struct _ipfw_insn *cmd, char *av, int cblen,
444     struct tidx *tstate);
445 
446 void fill_flow6(struct _ipfw_insn_u32 *cmd, char *av, int cblen);
447 void fill_unreach6_code(u_short *codep, char *str);
448 void fill_icmp6types(struct _ipfw_insn_icmp6 *cmd, char *av, int cblen);
449 int fill_ext6hdr(struct _ipfw_insn *cmd, char *av);
450 
451 /* ipfw2.c */
452 void bp_flush(struct buf_pr *b);
453 void fill_table(struct _ipfw_insn *cmd, char *av, uint8_t opcode,
454     struct tidx *tstate);
455 
456 /* tables.c */
457 struct _ipfw_obj_ctlv;
458 struct _ipfw_obj_ntlv;
459 int table_check_name(const char *tablename);
460 void ipfw_list_ta(int ac, char *av[]);
461 void ipfw_list_values(int ac, char *av[]);
462 void table_fill_ntlv(struct _ipfw_obj_ntlv *ntlv, const char *name,
463     uint8_t set, uint16_t uidx);
464 
465