1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: stable/12/sys/netpfil/ipfw/ip_fw_private.h 370380 2021-08-24 15:00:57Z ae $
28 */
29
30 #ifndef _IPFW2_PRIVATE_H
31 #define _IPFW2_PRIVATE_H
32
33 /*
34 * Internal constants and data structures used by ipfw components
35 * and not meant to be exported outside the kernel.
36 */
37
38 #ifdef _KERNEL
39
40 /*
41 * For platforms that do not have SYSCTL support, we wrap the
42 * SYSCTL_* into a function (one per file) to collect the values
43 * into an array at module initialization. The wrapping macros,
44 * SYSBEGIN() and SYSEND, are empty in the default case.
45 */
46 #ifndef SYSBEGIN
47 #define SYSBEGIN(x)
48 #endif
49 #ifndef SYSEND
50 #define SYSEND
51 #endif
52
53 /* Return values from ipfw_chk() */
54 enum {
55 IP_FW_PASS = 0,
56 IP_FW_DENY,
57 IP_FW_DIVERT,
58 IP_FW_TEE,
59 IP_FW_DUMMYNET,
60 IP_FW_NETGRAPH,
61 IP_FW_NGTEE,
62 IP_FW_NAT,
63 IP_FW_REASS,
64 IP_FW_NAT64,
65 };
66
67 /*
68 * Structure for collecting parameters to dummynet for ip6_output forwarding
69 */
70 struct _ip6dn_args {
71 struct ip6_pktopts *opt_or;
72 int flags_or;
73 struct ip6_moptions *im6o_or;
74 struct ifnet *origifp_or;
75 struct ifnet *ifp_or;
76 struct sockaddr_in6 dst_or;
77 u_long mtu_or;
78 };
79
80
81 /*
82 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
83 * all into a structure because this way it is easier and more
84 * efficient to pass variables around and extend the interface.
85 */
86 struct ip_fw_args {
87 uint32_t flags;
88 #define IPFW_ARGS_ETHER 0x0001 /* has valid ethernet header */
89 #define IPFW_ARGS_NH4 0x0002 /* has IPv4 next hop in hopstore */
90 #define IPFW_ARGS_NH6 0x0004 /* has IPv6 next hop in hopstore */
91 #define IPFW_ARGS_NH4PTR 0x0008 /* has IPv4 next hop in next_hop */
92 #define IPFW_ARGS_NH6PTR 0x0010 /* has IPv6 next hop in next_hop6 */
93 #define IPFW_ARGS_REF 0x0020 /* has valid ipfw_rule_ref */
94 /*
95 * On return, it points to the matching rule.
96 * On entry, rule.slot > 0 means the info is valid and
97 * contains the starting rule for an ipfw search.
98 * If chain_id == chain->id && slot >0 then jump to that slot.
99 * Otherwise, we locate the first rule >= rulenum:rule_id
100 */
101 struct ipfw_rule_ref rule; /* match/restart info */
102
103 struct ifnet *oif; /* output interface */
104 struct inpcb *inp;
105 union {
106 /*
107 * We don't support forwarding on layer2, thus we can
108 * keep eh pointer in this union.
109 * next_hop[6] pointers can be used to point to next hop
110 * stored in rule's opcode to avoid copying into hopstore.
111 * Also, it is expected that all 0x1-0x10 flags are mutually
112 * exclusive.
113 */
114 struct ether_header *eh; /* for bridged packets */
115 struct sockaddr_in *next_hop;
116 struct sockaddr_in6 *next_hop6;
117 /* ipfw next hop storage */
118 struct sockaddr_in hopstore;
119 struct ip_fw_nh6 {
120 struct in6_addr sin6_addr;
121 uint32_t sin6_scope_id;
122 uint16_t sin6_port;
123 } hopstore6;
124 };
125
126 struct mbuf *m; /* the mbuf chain */
127 struct ipfw_flow_id f_id; /* grabbed from IP header */
128 };
129
130 MALLOC_DECLARE(M_IPFW);
131
132 /*
133 * Hooks sometime need to know the direction of the packet
134 * (divert, dummynet, netgraph, ...)
135 * We use a generic definition here, with bit0-1 indicating the
136 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
137 * specific protocol
138 * indicating the protocol (if necessary)
139 */
140 enum {
141 DIR_MASK = 0x3,
142 DIR_OUT = 0,
143 DIR_IN = 1,
144 DIR_FWD = 2,
145 DIR_DROP = 3,
146 PROTO_LAYER2 = 0x4, /* set for layer 2 */
147 /* PROTO_DEFAULT = 0, */
148 PROTO_IPV4 = 0x08,
149 PROTO_IPV6 = 0x10,
150 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
151 /* PROTO_OLDBDG = 0x14, unused, old bridge */
152 };
153
154 /* wrapper for freeing a packet, in case we need to do more work */
155 #ifndef FREE_PKT
156 #if defined(__linux__) || defined(_WIN32)
157 #define FREE_PKT(m) netisr_dispatch(-1, m)
158 #else
159 #define FREE_PKT(m) m_freem(m)
160 #endif
161 #endif /* !FREE_PKT */
162
163 /*
164 * Function definitions.
165 */
166 int ipfw_chk(struct ip_fw_args *args);
167 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
168 u_int32_t, u_int32_t, int);
169
170 /* attach (arg = 1) or detach (arg = 0) hooks */
171 int ipfw_attach_hooks(int);
172 #ifdef NOTYET
173 void ipfw_nat_destroy(void);
174 #endif
175
176 /* In ip_fw_log.c */
177 struct ip;
178 struct ip_fw_chain;
179
180 void ipfw_bpf_init(int);
181 void ipfw_bpf_uninit(int);
182 void ipfw_bpf_mtap2(void *, u_int, struct mbuf *);
183 void ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
184 struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
185 u_short offset, uint32_t tablearg, struct ip *ip);
186 VNET_DECLARE(u_int64_t, norule_counter);
187 #define V_norule_counter VNET(norule_counter)
188 VNET_DECLARE(int, verbose_limit);
189 #define V_verbose_limit VNET(verbose_limit)
190
191 /* In ip_fw_dynamic.c */
192 struct sockopt_data;
193
194 enum { /* result for matching dynamic rules */
195 MATCH_REVERSE = 0,
196 MATCH_FORWARD,
197 MATCH_NONE,
198 MATCH_UNKNOWN,
199 };
200
201 /*
202 * Macro to determine that we need to do or redo dynamic state lookup.
203 * direction == MATCH_UNKNOWN means that this is first lookup, then we need
204 * to do lookup.
205 * Otherwise check the state name, if previous lookup was for "any" name,
206 * this means there is no state with specific name. Thus no need to do
207 * lookup. If previous name was not "any", redo lookup for specific name.
208 */
209 #define DYN_LOOKUP_NEEDED(p, cmd) \
210 ((p)->direction == MATCH_UNKNOWN || \
211 ((p)->kidx != 0 && (p)->kidx != (cmd)->arg1))
212 #define DYN_INFO_INIT(p) do { \
213 (p)->direction = MATCH_UNKNOWN; \
214 (p)->kidx = 0; \
215 } while (0)
216 struct ipfw_dyn_info {
217 uint16_t direction; /* match direction */
218 uint16_t kidx; /* state name kidx */
219 uint32_t hashval; /* hash value */
220 uint32_t version; /* bucket version */
221 uint32_t f_pos;
222 };
223 int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
224 const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
225 const void *ulp, int pktlen, struct ipfw_dyn_info *info,
226 uint32_t tablearg);
227 struct ip_fw *ipfw_dyn_lookup_state(const struct ip_fw_args *args,
228 const void *ulp, int pktlen, const ipfw_insn *cmd,
229 struct ipfw_dyn_info *info);
230
231 int ipfw_is_dyn_rule(struct ip_fw *rule);
232 void ipfw_expire_dyn_states(struct ip_fw_chain *, ipfw_range_tlv *);
233 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep);
234 int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd);
235
236 void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */
237 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
238 int ipfw_dyn_len(void);
239 uint32_t ipfw_dyn_get_count(uint32_t *, int *);
240 void ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint16_t eaction_id,
241 uint16_t default_id, uint16_t instance_id);
242
243 /* common variables */
244 VNET_DECLARE(int, fw_one_pass);
245 #define V_fw_one_pass VNET(fw_one_pass)
246
247 VNET_DECLARE(int, fw_verbose);
248 #define V_fw_verbose VNET(fw_verbose)
249
250 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
251 #define V_layer3_chain VNET(layer3_chain)
252
253 VNET_DECLARE(int, ipfw_vnet_ready);
254 #define V_ipfw_vnet_ready VNET(ipfw_vnet_ready)
255
256 VNET_DECLARE(u_int32_t, set_disable);
257 #define V_set_disable VNET(set_disable)
258
259 VNET_DECLARE(int, autoinc_step);
260 #define V_autoinc_step VNET(autoinc_step)
261
262 VNET_DECLARE(unsigned int, fw_tables_max);
263 #define V_fw_tables_max VNET(fw_tables_max)
264
265 VNET_DECLARE(unsigned int, fw_tables_sets);
266 #define V_fw_tables_sets VNET(fw_tables_sets)
267
268 struct tables_config;
269
270 #ifdef _KERNEL
271 /*
272 * Here we have the structure representing an ipfw rule.
273 *
274 * It starts with a general area
275 * followed by an array of one or more instructions, which the code
276 * accesses as an array of 32-bit values.
277 *
278 * Given a rule pointer r:
279 *
280 * r->cmd is the start of the first instruction.
281 * ACTION_PTR(r) is the start of the first action (things to do
282 * once a rule matched).
283 */
284 struct ip_fw_jump_cache {
285 union {
286 struct {
287 uint32_t id;
288 uint32_t pos;
289 };
290 uint64_t raw_value;
291 };
292 };
293
294 struct ip_fw {
295 uint16_t act_ofs; /* offset of action in 32-bit units */
296 uint16_t cmd_len; /* # of 32-bit words in cmd */
297 uint16_t rulenum; /* rule number */
298 uint8_t set; /* rule set (0..31) */
299 uint8_t flags; /* currently unused */
300 counter_u64_t cntr; /* Pointer to rule counters */
301 struct ip_fw_jump_cache cache; /* used by jump_fast */
302 uint32_t timestamp; /* tv_sec of last match */
303 uint32_t id; /* rule id */
304 uint32_t refcnt; /* number of references */
305
306 struct ip_fw *next; /* linked list of deleted rules */
307 ipfw_insn cmd[1]; /* storage for commands */
308 };
309
310 #define IPFW_RULE_CNTR_SIZE (2 * sizeof(uint64_t))
311
312 #endif
313
314 struct ip_fw_chain {
315 struct ip_fw **map; /* array of rule ptrs to ease lookup */
316 uint32_t id; /* ruleset id */
317 int n_rules; /* number of static rules */
318 void *tablestate; /* runtime table info */
319 void *valuestate; /* runtime table value info */
320 int *idxmap; /* skipto array of rules */
321 void **srvstate; /* runtime service mappings */
322 #if defined( __linux__ ) || defined( _WIN32 )
323 spinlock_t rwmtx;
324 #endif
325 int static_len; /* total len of static rules (v0) */
326 uint32_t gencnt; /* NAT generation count */
327 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
328 struct ip_fw *default_rule;
329 struct tables_config *tblcfg; /* tables module data */
330 void *ifcfg; /* interface module data */
331 int *idxmap_back; /* standby skipto array of rules */
332 struct namedobj_instance *srvmap; /* cfg name->number mappings */
333 #if defined( __linux__ ) || defined( _WIN32 )
334 spinlock_t uh_lock;
335 #else
336 struct rwlock uh_lock; /* lock for upper half */
337 #endif
338 };
339
340 /* 64-byte structure representing multi-field table value */
341 struct table_value {
342 uint32_t tag; /* O_TAG/O_TAGGED */
343 uint32_t pipe; /* O_PIPE/O_QUEUE */
344 uint16_t divert; /* O_DIVERT/O_TEE */
345 uint16_t skipto; /* skipto, CALLRET */
346 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
347 uint32_t fib; /* O_SETFIB */
348 uint32_t nat; /* O_NAT */
349 uint32_t nh4;
350 uint8_t dscp;
351 uint8_t spare0;
352 uint16_t spare1;
353 /* -- 32 bytes -- */
354 struct in6_addr nh6;
355 uint32_t limit; /* O_LIMIT */
356 uint32_t zoneid; /* scope zone id for nh6 */
357 uint64_t refcnt; /* Number of references */
358 };
359
360
361 struct named_object {
362 TAILQ_ENTRY(named_object) nn_next; /* namehash */
363 TAILQ_ENTRY(named_object) nv_next; /* valuehash */
364 char *name; /* object name */
365 uint16_t etlv; /* Export TLV id */
366 uint8_t subtype;/* object subtype within class */
367 uint8_t set; /* set object belongs to */
368 uint16_t kidx; /* object kernel index */
369 uint16_t spare;
370 uint32_t ocnt; /* object counter for internal use */
371 uint32_t refcnt; /* number of references */
372 };
373 TAILQ_HEAD(namedobjects_head, named_object);
374
375 struct sockopt; /* used by tcp_var.h */
376 struct sockopt_data {
377 caddr_t kbuf; /* allocated buffer */
378 size_t ksize; /* given buffer size */
379 size_t koff; /* data already used */
380 size_t kavail; /* number of bytes available */
381 size_t ktotal; /* total bytes pushed */
382 struct sockopt *sopt; /* socket data */
383 caddr_t sopt_val; /* sopt user buffer */
384 size_t valsize; /* original data size */
385 };
386
387 struct ipfw_ifc;
388
389 typedef void (ipfw_ifc_cb)(struct ip_fw_chain *ch, void *cbdata,
390 uint16_t ifindex);
391
392 struct ipfw_iface {
393 struct named_object no;
394 char ifname[64];
395 int resolved;
396 uint16_t ifindex;
397 uint16_t spare;
398 uint64_t gencnt;
399 TAILQ_HEAD(, ipfw_ifc) consumers;
400 };
401
402 struct ipfw_ifc {
403 TAILQ_ENTRY(ipfw_ifc) next;
404 struct ipfw_iface *iface;
405 ipfw_ifc_cb *cb;
406 void *cbdata;
407 };
408
409 /* Macro for working with various counters */
410 #define IPFW_INC_RULE_COUNTER(_cntr, _bytes) do { \
411 counter_u64_add((_cntr)->cntr, 1); \
412 counter_u64_add((_cntr)->cntr + 1, _bytes); \
413 if ((_cntr)->timestamp != time_uptime) \
414 (_cntr)->timestamp = time_uptime; \
415 } while (0)
416
417 #define IPFW_INC_DYN_COUNTER(_cntr, _bytes) do { \
418 (_cntr)->pcnt++; \
419 (_cntr)->bcnt += _bytes; \
420 } while (0)
421
422 #define IPFW_ZERO_RULE_COUNTER(_cntr) do { \
423 counter_u64_zero((_cntr)->cntr); \
424 counter_u64_zero((_cntr)->cntr + 1); \
425 (_cntr)->timestamp = 0; \
426 } while (0)
427
428 #define IPFW_ZERO_DYN_COUNTER(_cntr) do { \
429 (_cntr)->pcnt = 0; \
430 (_cntr)->bcnt = 0; \
431 } while (0)
432
433 #define TARG_VAL(ch, k, f) ((struct table_value *)((ch)->valuestate))[k].f
434 #define IP_FW_ARG_TABLEARG(ch, a, f) \
435 (((a) == IP_FW_TARG) ? TARG_VAL(ch, tablearg, f) : (a))
436 /*
437 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
438 * so the variable and the macros must be here.
439 */
440
441 #if defined( __linux__ ) || defined( _WIN32 )
442 #define IPFW_LOCK_INIT(_chain) do { \
443 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
444 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
445 } while (0)
446
447 #define IPFW_LOCK_DESTROY(_chain) do { \
448 rw_destroy(&(_chain)->rwmtx); \
449 rw_destroy(&(_chain)->uh_lock); \
450 } while (0)
451
452 #define IPFW_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_RLOCKED)
453 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
454
455 #define IPFW_RLOCK_TRACKER
456 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
457 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
458 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
459 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
460 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
461 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
462 #else /* FreeBSD */
463 #define IPFW_LOCK_INIT(_chain) do { \
464 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
465 } while (0)
466
467 #define IPFW_LOCK_DESTROY(_chain) do { \
468 rw_destroy(&(_chain)->uh_lock); \
469 } while (0)
470
471 #define IPFW_RLOCK_ASSERT(_chain) rm_assert(&V_pfil_lock, RA_RLOCKED)
472 #define IPFW_WLOCK_ASSERT(_chain) rm_assert(&V_pfil_lock, RA_WLOCKED)
473
474 #define IPFW_RLOCK_TRACKER struct rm_priotracker _tracker
475 #define IPFW_RLOCK(p) rm_rlock(&V_pfil_lock, &_tracker)
476 #define IPFW_RUNLOCK(p) rm_runlock(&V_pfil_lock, &_tracker)
477 #define IPFW_WLOCK(p) rm_wlock(&V_pfil_lock)
478 #define IPFW_WUNLOCK(p) rm_wunlock(&V_pfil_lock)
479 #define IPFW_PF_RLOCK(p)
480 #define IPFW_PF_RUNLOCK(p)
481 #endif
482
483 #define IPFW_UH_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_RLOCKED)
484 #define IPFW_UH_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_WLOCKED)
485 #define IPFW_UH_UNLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_UNLOCKED)
486
487 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
488 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
489 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
490 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
491
492 struct obj_idx {
493 uint16_t uidx; /* internal index supplied by userland */
494 uint16_t kidx; /* kernel object index */
495 uint16_t off; /* tlv offset from rule end in 4-byte words */
496 uint8_t spare;
497 uint8_t type; /* object type within its category */
498 };
499
500 struct rule_check_info {
501 uint16_t flags; /* rule-specific check flags */
502 uint16_t object_opcodes; /* num of opcodes referencing objects */
503 uint16_t urule_numoff; /* offset of rulenum in bytes */
504 uint8_t version; /* rule version */
505 uint8_t spare;
506 ipfw_obj_ctlv *ctlv; /* name TLV containter */
507 struct ip_fw *krule; /* resulting rule pointer */
508 caddr_t urule; /* original rule pointer */
509 struct obj_idx obuf[8]; /* table references storage */
510 };
511
512 /* Legacy interface support */
513 /*
514 * FreeBSD 8 export rule format
515 */
516 struct ip_fw_rule0 {
517 struct ip_fw *x_next; /* linked list of rules */
518 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
519 /* 'next_rule' is used to pass up 'set_disable' status */
520
521 uint16_t act_ofs; /* offset of action in 32-bit units */
522 uint16_t cmd_len; /* # of 32-bit words in cmd */
523 uint16_t rulenum; /* rule number */
524 uint8_t set; /* rule set (0..31) */
525 uint8_t _pad; /* padding */
526 uint32_t id; /* rule id */
527
528 /* These fields are present in all rules. */
529 uint64_t pcnt; /* Packet counter */
530 uint64_t bcnt; /* Byte counter */
531 uint32_t timestamp; /* tv_sec of last match */
532
533 ipfw_insn cmd[1]; /* storage for commands */
534 };
535
536 struct ip_fw_bcounter0 {
537 uint64_t pcnt; /* Packet counter */
538 uint64_t bcnt; /* Byte counter */
539 uint32_t timestamp; /* tv_sec of last match */
540 };
541
542 /* Kernel rule length */
543 /*
544 * RULE _K_ SIZE _V_ ->
545 * get kernel size from userland rool version _V_.
546 * RULE _U_ SIZE _V_ ->
547 * get user size version _V_ from kernel rule
548 * RULESIZE _V_ ->
549 * get user size rule length
550 */
551 /* FreeBSD8 <> current kernel format */
552 #define RULEUSIZE0(r) (sizeof(struct ip_fw_rule0) + (r)->cmd_len * 4 - 4)
553 #define RULEKSIZE0(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
554 /* FreeBSD11 <> current kernel format */
555 #define RULEUSIZE1(r) (roundup2(sizeof(struct ip_fw_rule) + \
556 (r)->cmd_len * 4 - 4, 8))
557 #define RULEKSIZE1(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
558
559 /*
560 * Tables/Objects index rewriting code
561 */
562
563 /* Default and maximum number of ipfw tables/objects. */
564 #define IPFW_TABLES_MAX 65536
565 #define IPFW_TABLES_DEFAULT 128
566 #define IPFW_OBJECTS_MAX 65536
567 #define IPFW_OBJECTS_DEFAULT 1024
568
569 #define CHAIN_TO_SRV(ch) ((ch)->srvmap)
570 #define SRV_OBJECT(ch, idx) ((ch)->srvstate[(idx)])
571
572 struct tid_info {
573 uint32_t set; /* table set */
574 uint16_t uidx; /* table index */
575 uint8_t type; /* table type */
576 uint8_t atype;
577 uint8_t spare;
578 int tlen; /* Total TLV size block */
579 void *tlvs; /* Pointer to first TLV */
580 };
581
582 /*
583 * Classifier callback. Checks if @cmd opcode contains kernel object reference.
584 * If true, returns its index and type.
585 * Returns 0 if match is found, 1 overwise.
586 */
587 typedef int (ipfw_obj_rw_cl)(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype);
588 /*
589 * Updater callback. Sets kernel object reference index to @puidx
590 */
591 typedef void (ipfw_obj_rw_upd)(ipfw_insn *cmd, uint16_t puidx);
592 /*
593 * Finder callback. Tries to find named object by name (specified via @ti).
594 * Stores found named object pointer in @pno.
595 * If object was not found, NULL is stored.
596 *
597 * Return 0 if input data was valid.
598 */
599 typedef int (ipfw_obj_fname_cb)(struct ip_fw_chain *ch,
600 struct tid_info *ti, struct named_object **pno);
601 /*
602 * Another finder callback. Tries to findex named object by kernel index.
603 *
604 * Returns pointer to named object or NULL.
605 */
606 typedef struct named_object *(ipfw_obj_fidx_cb)(struct ip_fw_chain *ch,
607 uint16_t kidx);
608 /*
609 * Object creator callback. Tries to create object specified by @ti.
610 * Stores newly-allocated object index in @pkidx.
611 *
612 * Returns 0 on success.
613 */
614 typedef int (ipfw_obj_create_cb)(struct ip_fw_chain *ch, struct tid_info *ti,
615 uint16_t *pkidx);
616 /*
617 * Object destroy callback. Intended to free resources allocated by
618 * create_object callback.
619 */
620 typedef void (ipfw_obj_destroy_cb)(struct ip_fw_chain *ch,
621 struct named_object *no);
622 /*
623 * Sets handler callback. Handles moving and swaping set of named object.
624 * SWAP_ALL moves all named objects from set `set' to `new_set' and vise versa;
625 * TEST_ALL checks that there aren't any named object with conflicting names;
626 * MOVE_ALL moves all named objects from set `set' to `new_set';
627 * COUNT_ONE used to count number of references used by object with kidx `set';
628 * TEST_ONE checks that named object with kidx `set' can be moved to `new_set`;
629 * MOVE_ONE moves named object with kidx `set' to set `new_set'.
630 */
631 enum ipfw_sets_cmd {
632 SWAP_ALL = 0, TEST_ALL, MOVE_ALL, COUNT_ONE, TEST_ONE, MOVE_ONE
633 };
634 typedef int (ipfw_obj_sets_cb)(struct ip_fw_chain *ch,
635 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
636
637
638 struct opcode_obj_rewrite {
639 uint32_t opcode; /* Opcode to act upon */
640 uint32_t etlv; /* Relevant export TLV id */
641 ipfw_obj_rw_cl *classifier; /* Check if rewrite is needed */
642 ipfw_obj_rw_upd *update; /* update cmd with new value */
643 ipfw_obj_fname_cb *find_byname; /* Find named object by name */
644 ipfw_obj_fidx_cb *find_bykidx; /* Find named object by kidx */
645 ipfw_obj_create_cb *create_object; /* Create named object */
646 ipfw_obj_destroy_cb *destroy_object;/* Destroy named object */
647 ipfw_obj_sets_cb *manage_sets; /* Swap or move sets */
648 };
649
650 #define IPFW_ADD_OBJ_REWRITER(f, c) do { \
651 if ((f) != 0) \
652 ipfw_add_obj_rewriter(c, \
653 sizeof(c) / sizeof(c[0])); \
654 } while(0)
655 #define IPFW_DEL_OBJ_REWRITER(l, c) do { \
656 if ((l) != 0) \
657 ipfw_del_obj_rewriter(c, \
658 sizeof(c) / sizeof(c[0])); \
659 } while(0)
660
661 /* In ip_fw_iface.c */
662 int ipfw_iface_init(void);
663 void ipfw_iface_destroy(void);
664 void vnet_ipfw_iface_destroy(struct ip_fw_chain *ch);
665 int ipfw_iface_ref(struct ip_fw_chain *ch, char *name,
666 struct ipfw_ifc *ic);
667 void ipfw_iface_unref(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
668 void ipfw_iface_add_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
669 void ipfw_iface_del_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
670
671 /* In ip_fw_sockopt.c */
672 void ipfw_init_skipto_cache(struct ip_fw_chain *chain);
673 void ipfw_destroy_skipto_cache(struct ip_fw_chain *chain);
674 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
675 int ipfw_ctl3(struct sockopt *sopt);
676 int ipfw_add_protected_rule(struct ip_fw_chain *chain, struct ip_fw *rule,
677 int locked);
678 void ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
679 struct ip_fw *rule);
680 void ipfw_reap_rules(struct ip_fw *head);
681 void ipfw_init_counters(void);
682 void ipfw_destroy_counters(void);
683 struct ip_fw *ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize);
684 void ipfw_free_rule(struct ip_fw *rule);
685 int ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt);
686 int ipfw_mark_object_kidx(uint32_t *bmask, uint16_t etlv, uint16_t kidx);
687 ipfw_insn *ipfw_get_action(struct ip_fw *);
688
689 typedef int (sopt_handler_f)(struct ip_fw_chain *ch,
690 ip_fw3_opheader *op3, struct sockopt_data *sd);
691 struct ipfw_sopt_handler {
692 uint16_t opcode;
693 uint8_t version;
694 uint8_t dir;
695 sopt_handler_f *handler;
696 uint64_t refcnt;
697 };
698 #define HDIR_SET 0x01 /* Handler is used to set some data */
699 #define HDIR_GET 0x02 /* Handler is used to retrieve data */
700 #define HDIR_BOTH HDIR_GET|HDIR_SET
701
702 void ipfw_init_sopt_handler(void);
703 void ipfw_destroy_sopt_handler(void);
704 void ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
705 int ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
706 caddr_t ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed);
707 caddr_t ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed);
708 #define IPFW_ADD_SOPT_HANDLER(f, c) do { \
709 if ((f) != 0) \
710 ipfw_add_sopt_handler(c, \
711 sizeof(c) / sizeof(c[0])); \
712 } while(0)
713 #define IPFW_DEL_SOPT_HANDLER(l, c) do { \
714 if ((l) != 0) \
715 ipfw_del_sopt_handler(c, \
716 sizeof(c) / sizeof(c[0])); \
717 } while(0)
718
719 struct namedobj_instance;
720 typedef int (objhash_cb_t)(struct namedobj_instance *ni, struct named_object *,
721 void *arg);
722 typedef uint32_t (objhash_hash_f)(struct namedobj_instance *ni, const void *key,
723 uint32_t kopt);
724 typedef int (objhash_cmp_f)(struct named_object *no, const void *key,
725 uint32_t kopt);
726 struct namedobj_instance *ipfw_objhash_create(uint32_t items);
727 void ipfw_objhash_destroy(struct namedobj_instance *);
728 void ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks);
729 void ipfw_objhash_bitmap_merge(struct namedobj_instance *ni,
730 void **idx, int *blocks);
731 void ipfw_objhash_bitmap_swap(struct namedobj_instance *ni,
732 void **idx, int *blocks);
733 void ipfw_objhash_bitmap_free(void *idx, int blocks);
734 void ipfw_objhash_set_hashf(struct namedobj_instance *ni, objhash_hash_f *f);
735 struct named_object *ipfw_objhash_lookup_name(struct namedobj_instance *ni,
736 uint32_t set, char *name);
737 struct named_object *ipfw_objhash_lookup_name_type(struct namedobj_instance *ni,
738 uint32_t set, uint32_t type, const char *name);
739 struct named_object *ipfw_objhash_lookup_kidx(struct namedobj_instance *ni,
740 uint16_t idx);
741 int ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
742 struct named_object *b);
743 void ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no);
744 void ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no);
745 uint32_t ipfw_objhash_count(struct namedobj_instance *ni);
746 uint32_t ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type);
747 int ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f,
748 void *arg);
749 int ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
750 void *arg, uint16_t type);
751 int ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx);
752 int ipfw_objhash_alloc_idx(void *n, uint16_t *pidx);
753 void ipfw_objhash_set_funcs(struct namedobj_instance *ni,
754 objhash_hash_f *hash_f, objhash_cmp_f *cmp_f);
755 int ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
756 uint32_t etlv, struct named_object **pno);
757 void ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv);
758 ipfw_obj_ntlv *ipfw_find_name_tlv_type(void *tlvs, int len, uint16_t uidx,
759 uint32_t etlv);
760 void ipfw_init_obj_rewriter(void);
761 void ipfw_destroy_obj_rewriter(void);
762 void ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
763 int ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
764
765 int create_objects_compat(struct ip_fw_chain *ch, ipfw_insn *cmd,
766 struct obj_idx *oib, struct obj_idx *pidx, struct tid_info *ti);
767 void update_opcode_kidx(ipfw_insn *cmd, uint16_t idx);
768 int classify_opcode_kidx(ipfw_insn *cmd, uint16_t *puidx);
769 void ipfw_init_srv(struct ip_fw_chain *ch);
770 void ipfw_destroy_srv(struct ip_fw_chain *ch);
771 int ipfw_check_object_name_generic(const char *name);
772 int ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
773 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
774
775 /* In ip_fw_eaction.c */
776 typedef int (ipfw_eaction_t)(struct ip_fw_chain *ch, struct ip_fw_args *args,
777 ipfw_insn *cmd, int *done);
778 int ipfw_eaction_init(struct ip_fw_chain *ch, int first);
779 void ipfw_eaction_uninit(struct ip_fw_chain *ch, int last);
780
781 uint16_t ipfw_add_eaction(struct ip_fw_chain *ch, ipfw_eaction_t handler,
782 const char *name);
783 int ipfw_del_eaction(struct ip_fw_chain *ch, uint16_t eaction_id);
784 int ipfw_run_eaction(struct ip_fw_chain *ch, struct ip_fw_args *args,
785 ipfw_insn *cmd, int *done);
786 int ipfw_reset_eaction(struct ip_fw_chain *ch, struct ip_fw *rule,
787 uint16_t eaction_id, uint16_t default_id, uint16_t instance_id);
788 int ipfw_reset_eaction_instance(struct ip_fw_chain *ch, uint16_t eaction_id,
789 uint16_t instance_id);
790
791 /* In ip_fw_table.c */
792 struct table_info;
793
794 typedef int (table_lookup_t)(struct table_info *ti, void *key, uint32_t keylen,
795 uint32_t *val);
796
797 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
798 void *paddr, uint32_t *val);
799 struct named_object *ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch,
800 uint16_t kidx);
801 int ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx);
802 void ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx);
803 int ipfw_init_tables(struct ip_fw_chain *ch, int first);
804 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables);
805 int ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int nsets);
806 void ipfw_destroy_tables(struct ip_fw_chain *ch, int last);
807
808 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
809
810 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
811
812 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
813 typedef int ipfw_nat_cfg_t(struct sockopt *);
814
815 VNET_DECLARE(int, ipfw_nat_ready);
816 #define V_ipfw_nat_ready VNET(ipfw_nat_ready)
817 #define IPFW_NAT_LOADED (V_ipfw_nat_ready)
818
819 extern ipfw_nat_t *ipfw_nat_ptr;
820 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
821 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
822 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
823 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
824
825 /* Helper functions for IP checksum adjustment */
826 static __inline uint16_t
cksum_add(uint16_t sum,uint16_t a)827 cksum_add(uint16_t sum, uint16_t a)
828 {
829 uint16_t res;
830
831 res = sum + a;
832 return (res + (res < a));
833 }
834
835 static __inline uint16_t
cksum_adjust(uint16_t oldsum,uint16_t old,uint16_t new)836 cksum_adjust(uint16_t oldsum, uint16_t old, uint16_t new)
837 {
838
839 return (~cksum_add(cksum_add(~oldsum, ~old), new));
840 }
841
842 #endif /* _KERNEL */
843 #endif /* _IPFW2_PRIVATE_H */
844