xref: /freebsd/sbin/ipfw/ipfw2.h (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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_ALL,
181 	TOK_MASK,
182 	TOK_FLOW_MASK,
183 	TOK_SCHED_MASK,
184 	TOK_BW,
185 	TOK_DELAY,
186 	TOK_PROFILE,
187 	TOK_BURST,
188 	TOK_RED,
189 	TOK_GRED,
190 	TOK_ECN,
191 	TOK_DROPTAIL,
192 	TOK_PROTO,
193 #ifdef NEW_AQM
194 	/* AQM tokens*/
195 	TOK_NO_ECN,
196 	TOK_CODEL,
197 	TOK_FQ_CODEL,
198 	TOK_TARGET,
199 	TOK_INTERVAL,
200 	TOK_FLOWS,
201 	TOK_QUANTUM,
202 
203 	TOK_PIE,
204 	TOK_FQ_PIE,
205 	TOK_TUPDATE,
206 	TOK_MAX_BURST,
207 	TOK_MAX_ECNTH,
208 	TOK_ALPHA,
209 	TOK_BETA,
210 	TOK_CAPDROP,
211 	TOK_NO_CAPDROP,
212 	TOK_ONOFF,
213 	TOK_DRE,
214 	TOK_TS,
215 	TOK_DERAND,
216 	TOK_NO_DERAND,
217 #endif
218 	/* dummynet tokens */
219 	TOK_WEIGHT,
220 	TOK_LMAX,
221 	TOK_PRI,
222 	TOK_TYPE,
223 	TOK_SLOTSIZE,
224 
225 	TOK_IP,
226 	TOK_IF,
227 	TOK_ALOG,
228 	TOK_DENY_INC,
229 	TOK_SAME_PORTS,
230 	TOK_UNREG_ONLY,
231 	TOK_UNREG_CGN,
232 	TOK_SKIP_GLOBAL,
233 	TOK_RESET_ADDR,
234 	TOK_ALIAS_REV,
235 	TOK_PROXY_ONLY,
236 	TOK_REDIR_ADDR,
237 	TOK_REDIR_PORT,
238 	TOK_REDIR_PROTO,
239 
240 	TOK_IPV6,
241 	TOK_FLOWID,
242 	TOK_ICMP6TYPES,
243 	TOK_EXT6HDR,
244 	TOK_DSTIP6,
245 	TOK_SRCIP6,
246 
247 	TOK_IPV4,
248 	TOK_UNREACH6,
249 	TOK_RESET6,
250 
251 	TOK_FIB,
252 	TOK_SETFIB,
253 	TOK_LOOKUP,
254 	TOK_SOCKARG,
255 	TOK_SETDSCP,
256 	TOK_FLOW,
257 	TOK_IFLIST,
258 	/* Table tokens */
259 	TOK_CREATE,
260 	TOK_DESTROY,
261 	TOK_LIST,
262 	TOK_INFO,
263 	TOK_DETAIL,
264 	TOK_MODIFY,
265 	TOK_FLUSH,
266 	TOK_SWAP,
267 	TOK_ADD,
268 	TOK_DEL,
269 	TOK_VALTYPE,
270 	TOK_ALGO,
271 	TOK_TALIST,
272 	TOK_ATOMIC,
273 	TOK_LOCK,
274 	TOK_UNLOCK,
275 	TOK_VLIST,
276 	TOK_OLIST,
277 	TOK_MISSING,
278 	TOK_ORFLUSH,
279 
280 	/* NAT64 tokens */
281 	TOK_NAT64STL,
282 	TOK_NAT64LSN,
283 	TOK_STATS,
284 	TOK_STATES,
285 	TOK_CONFIG,
286 	TOK_TABLE4,
287 	TOK_TABLE6,
288 	TOK_PREFIX4,
289 	TOK_PREFIX6,
290 	TOK_AGG_LEN,
291 	TOK_AGG_COUNT,
292 	TOK_MAX_PORTS,
293 	TOK_STATES_CHUNKS,
294 	TOK_JMAXLEN,
295 	TOK_PORT_RANGE,
296 	TOK_PORT_ALIAS,
297 	TOK_HOST_DEL_AGE,
298 	TOK_PG_DEL_AGE,
299 	TOK_TCP_SYN_AGE,
300 	TOK_TCP_CLOSE_AGE,
301 	TOK_TCP_EST_AGE,
302 	TOK_UDP_AGE,
303 	TOK_ICMP_AGE,
304 	TOK_LOGOFF,
305 	TOK_PRIVATE,
306 	TOK_PRIVATEOFF,
307 
308 	/* NAT64 CLAT tokens */
309 	TOK_NAT64CLAT,
310 	TOK_PLAT_PREFIX,
311 	TOK_CLAT_PREFIX,
312 
313 	/* NPTv6 tokens */
314 	TOK_NPTV6,
315 	TOK_INTPREFIX,
316 	TOK_EXTPREFIX,
317 	TOK_PREFIXLEN,
318 	TOK_EXTIF,
319 
320 	TOK_TCPSETMSS,
321 
322 	TOK_SKIPACTION,
323 };
324 
325 /*
326  * the following macro returns an error message if we run out of
327  * arguments.
328  */
329 #define NEED(_p, msg)      {if (!_p) errx(EX_USAGE, msg);}
330 #define NEED1(msg)      {if (!(*av)) errx(EX_USAGE, msg);}
331 
332 struct buf_pr {
333 	char	*buf;	/* allocated buffer */
334 	char	*ptr;	/* current pointer */
335 	size_t	size;	/* total buffer size */
336 	size_t	avail;	/* available storage */
337 	size_t	needed;	/* length needed */
338 };
339 
340 int pr_u64(struct buf_pr *bp, void *pd, int width);
341 int bp_alloc(struct buf_pr *b, size_t size);
342 void bp_free(struct buf_pr *b);
343 int bprintf(struct buf_pr *b, const char *format, ...);
344 
345 
346 /* memory allocation support */
347 void *safe_calloc(size_t number, size_t size);
348 void *safe_realloc(void *ptr, size_t size);
349 
350 /* string comparison functions used for historical compatibility */
351 int _substrcmp(const char *str1, const char* str2);
352 int _substrcmp2(const char *str1, const char* str2, const char* str3);
353 int stringnum_cmp(const char *a, const char *b);
354 
355 /* utility functions */
356 int match_token(struct _s_x *table, const char *string);
357 int match_token_relaxed(struct _s_x *table, const char *string);
358 int get_token(struct _s_x *table, const char *string, const char *errbase);
359 char const *match_value(struct _s_x *p, int value);
360 size_t concat_tokens(char *buf, size_t bufsize, struct _s_x *table,
361     const char *delimiter);
362 int fill_flags(struct _s_x *flags, char *p, char **e, uint32_t *set,
363     uint32_t *clear);
364 void print_flags_buffer(char *buf, size_t sz, struct _s_x *list, uint32_t set);
365 
366 struct _ip_fw3_opheader;
367 int do_cmd(int optname, void *optval, uintptr_t optlen);
368 int do_set3(int optname, struct _ip_fw3_opheader *op3, size_t optlen);
369 int do_get3(int optname, struct _ip_fw3_opheader *op3, size_t *optlen);
370 
371 struct in6_addr;
372 void n2mask(struct in6_addr *mask, int n);
373 int contigmask(const uint8_t *p, int len);
374 
375 /*
376  * Forward declarations to avoid include way too many headers.
377  * C does not allow duplicated typedefs, so we use the base struct
378  * that the typedef points to.
379  * Should the typedefs use a different type, the compiler will
380  * still detect the change when compiling the body of the
381  * functions involved, so we do not lose error checking.
382  */
383 struct _ipfw_insn;
384 struct _ipfw_insn_altq;
385 struct _ipfw_insn_u32;
386 struct _ipfw_insn_ip6;
387 struct _ipfw_insn_icmp6;
388 
389 /*
390  * The reserved set numer. This is a constant in ip_fw.h
391  * but we store it in a variable so other files do not depend
392  * in that header just for one constant.
393  */
394 extern int resvd_set_number;
395 
396 /* first-level command handlers */
397 void ipfw_add(char *av[]);
398 void ipfw_show_nat(int ac, char **av);
399 int ipfw_delete_nat(int i);
400 void ipfw_config_pipe(int ac, char **av);
401 void ipfw_config_nat(int ac, char **av);
402 void ipfw_sets_handler(char *av[]);
403 void ipfw_table_handler(int ac, char *av[]);
404 void ipfw_sysctl_handler(char *av[], int which);
405 void ipfw_delete(char *av[]);
406 void ipfw_flush(int force);
407 void ipfw_zero(int ac, char *av[], int optname);
408 void ipfw_list(int ac, char *av[], int show_counters);
409 void ipfw_internal_handler(int ac, char *av[]);
410 void ipfw_nat64clat_handler(int ac, char *av[]);
411 void ipfw_nat64lsn_handler(int ac, char *av[]);
412 void ipfw_nat64stl_handler(int ac, char *av[]);
413 void ipfw_nptv6_handler(int ac, char *av[]);
414 int ipfw_check_object_name(const char *name);
415 int ipfw_check_nat64prefix(const struct in6_addr *prefix, int length);
416 
417 #ifdef PF
418 /* altq.c */
419 void altq_set_enabled(int enabled);
420 u_int32_t altq_name_to_qid(const char *name);
421 void print_altq_cmd(struct buf_pr *bp, const struct _ipfw_insn_altq *altqptr);
422 #else
423 #define NO_ALTQ
424 #endif
425 
426 /* dummynet.c */
427 void dummynet_list(int ac, char *av[], int show_counters);
428 void dummynet_flush(void);
429 int ipfw_delete_pipe(int pipe_or_queue, int n);
430 
431 /* ipv6.c */
432 void print_unreach6_code(struct buf_pr *bp, uint16_t code);
433 void print_ip6(struct buf_pr *bp, const struct _ipfw_insn_ip6 *cmd);
434 void print_flow6id(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
435 void print_icmp6types(struct buf_pr *bp, const struct _ipfw_insn_u32 *cmd);
436 void print_ext6hdr(struct buf_pr *bp, const struct _ipfw_insn *cmd);
437 
438 struct tidx;
439 struct _ipfw_insn *add_srcip6(struct _ipfw_insn *cmd, char *av, int cblen,
440     struct tidx *tstate);
441 struct _ipfw_insn *add_dstip6(struct _ipfw_insn *cmd, char *av, int cblen,
442     struct tidx *tstate);
443 
444 void fill_flow6(struct _ipfw_insn_u32 *cmd, char *av, int cblen);
445 void fill_unreach6_code(u_short *codep, char *str);
446 void fill_icmp6types(struct _ipfw_insn_icmp6 *cmd, char *av, int cblen);
447 int fill_ext6hdr(struct _ipfw_insn *cmd, char *av);
448 
449 /* ipfw2.c */
450 void bp_flush(struct buf_pr *b);
451 void fill_table(struct _ipfw_insn *cmd, char *av, uint8_t opcode,
452     struct tidx *tstate);
453 
454 /* tables.c */
455 struct _ipfw_obj_ctlv;
456 struct _ipfw_obj_ntlv;
457 int table_check_name(const char *tablename);
458 void ipfw_list_ta(int ac, char *av[]);
459 void ipfw_list_values(int ac, char *av[]);
460 void table_fill_ntlv(struct _ipfw_obj_ntlv *ntlv, const char *name,
461     uint8_t set, uint16_t uidx);
462 
463