]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netpfil/ipfw/ip_fw_table.c
MFV r316877: 7571 non-present readonly numeric ZFS props do not have default value
[FreeBSD/FreeBSD.git] / sys / netpfil / ipfw / ip_fw_table.c
1 /*-
2  * Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
3  * Copyright (c) 2014 Yandex LLC
4  * Copyright (c) 2014 Alexander V. Chernikov
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Lookup table support for ipfw.
33  *
34  * This file contains handlers for all generic tables' operations:
35  * add/del/flush entries, list/dump tables etc..
36  *
37  * Table data modification is protected by both UH and runtime lock
38  * while reading configuration/data is protected by UH lock.
39  *
40  * Lookup algorithms for all table types are located in ip_fw_table_algo.c
41  */
42
43 #include "opt_ipfw.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/rwlock.h>
51 #include <sys/rmlock.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/queue.h>
55 #include <net/if.h>     /* ip_fw.h requires IFNAMSIZ */
56 #include <net/pfil.h>
57
58 #include <netinet/in.h>
59 #include <netinet/ip_var.h>     /* struct ipfw_rule_ref */
60 #include <netinet/ip_fw.h>
61
62 #include <netpfil/ipfw/ip_fw_private.h>
63 #include <netpfil/ipfw/ip_fw_table.h>
64
65  /*
66  * Table has the following `type` concepts:
67  *
68  * `no.type` represents lookup key type (addr, ifp, uid, etc..)
69  * vmask represents bitmask of table values which are present at the moment.
70  * Special IPFW_VTYPE_LEGACY ( (uint32_t)-1 ) represents old
71  * single-value-for-all approach.
72  */
73 struct table_config {
74         struct named_object     no;
75         uint8_t         tflags;         /* type flags */
76         uint8_t         locked;         /* 1 if locked from changes */
77         uint8_t         linked;         /* 1 if already linked */
78         uint8_t         ochanged;       /* used by set swapping */
79         uint8_t         vshared;        /* 1 if using shared value array */
80         uint8_t         spare[3];
81         uint32_t        count;          /* Number of records */
82         uint32_t        limit;          /* Max number of records */
83         uint32_t        vmask;          /* bitmask with supported values */
84         uint32_t        ocount;         /* used by set swapping */
85         uint64_t        gencnt;         /* generation count */
86         char            tablename[64];  /* table name */
87         struct table_algo       *ta;    /* Callbacks for given algo */
88         void            *astate;        /* algorithm state */
89         struct table_info       ti_copy;        /* data to put to table_info */
90         struct namedobj_instance        *vi;
91 };
92
93 static int find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
94     struct table_config **tc);
95 static struct table_config *find_table(struct namedobj_instance *ni,
96     struct tid_info *ti);
97 static struct table_config *alloc_table_config(struct ip_fw_chain *ch,
98     struct tid_info *ti, struct table_algo *ta, char *adata, uint8_t tflags);
99 static void free_table_config(struct namedobj_instance *ni,
100     struct table_config *tc);
101 static int create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
102     char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int ref);
103 static void link_table(struct ip_fw_chain *ch, struct table_config *tc);
104 static void unlink_table(struct ip_fw_chain *ch, struct table_config *tc);
105 static int find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
106     struct tentry_info *tei, uint32_t count, int op, struct table_config **ptc);
107 #define OP_ADD  1
108 #define OP_DEL  0
109 static int export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
110     struct sockopt_data *sd);
111 static void export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
112     ipfw_xtable_info *i);
113 static int dump_table_tentry(void *e, void *arg);
114 static int dump_table_xentry(void *e, void *arg);
115
116 static int swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
117     struct tid_info *b);
118
119 static int check_table_name(const char *name);
120 static int check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
121     struct table_config *tc, struct table_info *ti, uint32_t count);
122 static int destroy_table(struct ip_fw_chain *ch, struct tid_info *ti);
123
124 static struct table_algo *find_table_algo(struct tables_config *tableconf,
125     struct tid_info *ti, char *name);
126
127 static void objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti);
128 static void ntlv_to_ti(struct _ipfw_obj_ntlv *ntlv, struct tid_info *ti);
129
130 #define CHAIN_TO_NI(chain)      (CHAIN_TO_TCFG(chain)->namehash)
131 #define KIDX_TO_TI(ch, k)       (&(((struct table_info *)(ch)->tablestate)[k]))
132
133 #define TA_BUF_SZ       128     /* On-stack buffer for add/delete state */
134
135 void
136 rollback_toperation_state(struct ip_fw_chain *ch, void *object)
137 {
138         struct tables_config *tcfg;
139         struct op_state *os;
140
141         tcfg = CHAIN_TO_TCFG(ch);
142         TAILQ_FOREACH(os, &tcfg->state_list, next)
143                 os->func(object, os);
144 }
145
146 void
147 add_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
148 {
149         struct tables_config *tcfg;
150
151         tcfg = CHAIN_TO_TCFG(ch);
152         TAILQ_INSERT_HEAD(&tcfg->state_list, &ts->opstate, next);
153 }
154
155 void
156 del_toperation_state(struct ip_fw_chain *ch, struct tableop_state *ts)
157 {
158         struct tables_config *tcfg;
159
160         tcfg = CHAIN_TO_TCFG(ch);
161         TAILQ_REMOVE(&tcfg->state_list, &ts->opstate, next);
162 }
163
164 void
165 tc_ref(struct table_config *tc)
166 {
167
168         tc->no.refcnt++;
169 }
170
171 void
172 tc_unref(struct table_config *tc)
173 {
174
175         tc->no.refcnt--;
176 }
177
178 static struct table_value *
179 get_table_value(struct ip_fw_chain *ch, struct table_config *tc, uint32_t kidx)
180 {
181         struct table_value *pval;
182
183         pval = (struct table_value *)ch->valuestate;
184
185         return (&pval[kidx]);
186 }
187
188
189 /*
190  * Checks if we're able to insert/update entry @tei into table
191  * w.r.t @tc limits.
192  * May alter @tei to indicate insertion error / insert
193  * options.
194  *
195  * Returns 0 if operation can be performed/
196  */
197 static int
198 check_table_limit(struct table_config *tc, struct tentry_info *tei)
199 {
200
201         if (tc->limit == 0 || tc->count < tc->limit)
202                 return (0);
203
204         if ((tei->flags & TEI_FLAGS_UPDATE) == 0) {
205                 /* Notify userland on error cause */
206                 tei->flags |= TEI_FLAGS_LIMIT;
207                 return (EFBIG);
208         }
209
210         /*
211          * We have UPDATE flag set.
212          * Permit updating record (if found),
213          * but restrict adding new one since we've
214          * already hit the limit.
215          */
216         tei->flags |= TEI_FLAGS_DONTADD;
217
218         return (0);
219 }
220
221 /*
222  * Convert algorithm callback return code into
223  * one of pre-defined states known by userland.
224  */
225 static void
226 store_tei_result(struct tentry_info *tei, int op, int error, uint32_t num)
227 {
228         int flag;
229
230         flag = 0;
231
232         switch (error) {
233         case 0:
234                 if (op == OP_ADD && num != 0)
235                         flag = TEI_FLAGS_ADDED;
236                 if (op == OP_DEL)
237                         flag = TEI_FLAGS_DELETED;
238                 break;
239         case ENOENT:
240                 flag = TEI_FLAGS_NOTFOUND;
241                 break;
242         case EEXIST:
243                 flag = TEI_FLAGS_EXISTS;
244                 break;
245         default:
246                 flag = TEI_FLAGS_ERROR;
247         }
248
249         tei->flags |= flag;
250 }
251
252 /*
253  * Creates and references table with default parameters.
254  * Saves table config, algo and allocated kidx info @ptc, @pta and
255  * @pkidx if non-zero.
256  * Used for table auto-creation to support old binaries.
257  *
258  * Returns 0 on success.
259  */
260 static int
261 create_table_compat(struct ip_fw_chain *ch, struct tid_info *ti,
262     uint16_t *pkidx)
263 {
264         ipfw_xtable_info xi;
265         int error;
266
267         memset(&xi, 0, sizeof(xi));
268         /* Set default value mask for legacy clients */
269         xi.vmask = IPFW_VTYPE_LEGACY;
270
271         error = create_table_internal(ch, ti, NULL, &xi, pkidx, 1);
272         if (error != 0)
273                 return (error);
274
275         return (0);
276 }
277
278 /*
279  * Find and reference existing table optionally
280  * creating new one.
281  *
282  * Saves found table config into @ptc.
283  * Note function may drop/acquire UH_WLOCK.
284  * Returns 0 if table was found/created and referenced
285  * or non-zero return code.
286  */
287 static int
288 find_ref_table(struct ip_fw_chain *ch, struct tid_info *ti,
289     struct tentry_info *tei, uint32_t count, int op,
290     struct table_config **ptc)
291 {
292         struct namedobj_instance *ni;
293         struct table_config *tc;
294         uint16_t kidx;
295         int error;
296
297         IPFW_UH_WLOCK_ASSERT(ch);
298
299         ni = CHAIN_TO_NI(ch);
300         tc = NULL;
301         if ((tc = find_table(ni, ti)) != NULL) {
302                 /* check table type */
303                 if (tc->no.subtype != ti->type)
304                         return (EINVAL);
305
306                 if (tc->locked != 0)
307                         return (EACCES);
308
309                 /* Try to exit early on limit hit */
310                 if (op == OP_ADD && count == 1 &&
311                     check_table_limit(tc, tei) != 0)
312                         return (EFBIG);
313
314                 /* Reference and return */
315                 tc->no.refcnt++;
316                 *ptc = tc;
317                 return (0);
318         }
319
320         if (op == OP_DEL)
321                 return (ESRCH);
322
323         /* Compatibility mode: create new table for old clients */
324         if ((tei->flags & TEI_FLAGS_COMPAT) == 0)
325                 return (ESRCH);
326
327         IPFW_UH_WUNLOCK(ch);
328         error = create_table_compat(ch, ti, &kidx);
329         IPFW_UH_WLOCK(ch);
330         
331         if (error != 0)
332                 return (error);
333
334         tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
335         KASSERT(tc != NULL, ("create_table_compat returned bad idx %d", kidx));
336
337         /* OK, now we've got referenced table. */
338         *ptc = tc;
339         return (0);
340 }
341
342 /*
343  * Rolls back already @added to @tc entries using state array @ta_buf_m.
344  * Assume the following layout:
345  * 1) ADD state (ta_buf_m[0] ... t_buf_m[added - 1]) for handling update cases
346  * 2) DEL state (ta_buf_m[count[ ... t_buf_m[count + added - 1])
347  *   for storing deleted state
348  */
349 static void
350 rollback_added_entries(struct ip_fw_chain *ch, struct table_config *tc,
351     struct table_info *tinfo, struct tentry_info *tei, caddr_t ta_buf_m,
352     uint32_t count, uint32_t added)
353 {
354         struct table_algo *ta;
355         struct tentry_info *ptei;
356         caddr_t v, vv;
357         size_t ta_buf_sz;
358         int error, i;
359         uint32_t num;
360
361         IPFW_UH_WLOCK_ASSERT(ch);
362
363         ta = tc->ta;
364         ta_buf_sz = ta->ta_buf_size;
365         v = ta_buf_m;
366         vv = v + count * ta_buf_sz;
367         for (i = 0; i < added; i++, v += ta_buf_sz, vv += ta_buf_sz) {
368                 ptei = &tei[i];
369                 if ((ptei->flags & TEI_FLAGS_UPDATED) != 0) {
370
371                         /*
372                          * We have old value stored by previous
373                          * call in @ptei->value. Do add once again
374                          * to restore it.
375                          */
376                         error = ta->add(tc->astate, tinfo, ptei, v, &num);
377                         KASSERT(error == 0, ("rollback UPDATE fail"));
378                         KASSERT(num == 0, ("rollback UPDATE fail2"));
379                         continue;
380                 }
381
382                 error = ta->prepare_del(ch, ptei, vv);
383                 KASSERT(error == 0, ("pre-rollback INSERT failed"));
384                 error = ta->del(tc->astate, tinfo, ptei, vv, &num);
385                 KASSERT(error == 0, ("rollback INSERT failed"));
386                 tc->count -= num;
387         }
388 }
389
390 /*
391  * Prepares add/del state for all @count entries in @tei.
392  * Uses either stack buffer (@ta_buf) or allocates a new one.
393  * Stores pointer to allocated buffer back to @ta_buf.
394  *
395  * Returns 0 on success.
396  */
397 static int
398 prepare_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
399     struct tentry_info *tei, uint32_t count, int op, caddr_t *ta_buf)
400 {
401         caddr_t ta_buf_m, v;
402         size_t ta_buf_sz, sz;
403         struct tentry_info *ptei;
404         int error, i;
405
406         error = 0;
407         ta_buf_sz = ta->ta_buf_size;
408         if (count == 1) {
409                 /* Single add/delete, use on-stack buffer */
410                 memset(*ta_buf, 0, TA_BUF_SZ);
411                 ta_buf_m = *ta_buf;
412         } else {
413
414                 /*
415                  * Multiple adds/deletes, allocate larger buffer
416                  *
417                  * Note we need 2xcount buffer for add case:
418                  * we have hold both ADD state
419                  * and DELETE state (this may be needed
420                  * if we need to rollback all changes)
421                  */
422                 sz = count * ta_buf_sz;
423                 ta_buf_m = malloc((op == OP_ADD) ? sz * 2 : sz, M_TEMP,
424                     M_WAITOK | M_ZERO);
425         }
426
427         v = ta_buf_m;
428         for (i = 0; i < count; i++, v += ta_buf_sz) {
429                 ptei = &tei[i];
430                 error = (op == OP_ADD) ?
431                     ta->prepare_add(ch, ptei, v) : ta->prepare_del(ch, ptei, v);
432
433                 /*
434                  * Some syntax error (incorrect mask, or address, or
435                  * anything). Return error regardless of atomicity
436                  * settings.
437                  */
438                 if (error != 0)
439                         break;
440         }
441
442         *ta_buf = ta_buf_m;
443         return (error);
444 }
445
446 /*
447  * Flushes allocated state for each @count entries in @tei.
448  * Frees @ta_buf_m if differs from stack buffer @ta_buf.
449  */
450 static void
451 flush_batch_buffer(struct ip_fw_chain *ch, struct table_algo *ta,
452     struct tentry_info *tei, uint32_t count, int rollback,
453     caddr_t ta_buf_m, caddr_t ta_buf)
454 {
455         caddr_t v;
456         struct tentry_info *ptei;
457         size_t ta_buf_sz;
458         int i;
459
460         ta_buf_sz = ta->ta_buf_size;
461
462         /* Run cleaning callback anyway */
463         v = ta_buf_m;
464         for (i = 0; i < count; i++, v += ta_buf_sz) {
465                 ptei = &tei[i];
466                 ta->flush_entry(ch, ptei, v);
467                 if (ptei->ptv != NULL) {
468                         free(ptei->ptv, M_IPFW);
469                         ptei->ptv = NULL;
470                 }
471         }
472
473         /* Clean up "deleted" state in case of rollback */
474         if (rollback != 0) {
475                 v = ta_buf_m + count * ta_buf_sz;
476                 for (i = 0; i < count; i++, v += ta_buf_sz)
477                         ta->flush_entry(ch, &tei[i], v);
478         }
479
480         if (ta_buf_m != ta_buf)
481                 free(ta_buf_m, M_TEMP);
482 }
483
484
485 static void
486 rollback_add_entry(void *object, struct op_state *_state)
487 {
488         struct ip_fw_chain *ch;
489         struct tableop_state *ts;
490
491         ts = (struct tableop_state *)_state;
492
493         if (ts->tc != object && ts->ch != object)
494                 return;
495
496         ch = ts->ch;
497
498         IPFW_UH_WLOCK_ASSERT(ch);
499
500         /* Call specifid unlockers */
501         rollback_table_values(ts);
502
503         /* Indicate we've called */
504         ts->modified = 1;
505 }
506
507 /*
508  * Adds/updates one or more entries in table @ti.
509  *
510  * Function may drop/reacquire UH wlock multiple times due to
511  * items alloc, algorithm callbacks (check_space), value linkage
512  * (new values, value storage realloc), etc..
513  * Other processes like other adds (which may involve storage resize),
514  * table swaps (which changes table data and may change algo type),
515  * table modify (which may change value mask) may be executed
516  * simultaneously so we need to deal with it.
517  *
518  * The following approach was implemented:
519  * we have per-chain linked list, protected with UH lock.
520  * add_table_entry prepares special on-stack structure wthich is passed
521  * to its descendants. Users add this structure to this list before unlock.
522  * After performing needed operations and acquiring UH lock back, each user
523  * checks if structure has changed. If true, it rolls local state back and
524  * returns without error to the caller.
525  * add_table_entry() on its own checks if structure has changed and restarts
526  * its operation from the beginning (goto restart).
527  *
528  * Functions which are modifying fields of interest (currently
529  *   resize_shared_value_storage() and swap_tables() )
530  * traverses given list while holding UH lock immediately before
531  * performing their operations calling function provided be list entry
532  * ( currently rollback_add_entry  ) which performs rollback for all necessary
533  * state and sets appropriate values in structure indicating rollback
534  * has happened.
535  *
536  * Algo interaction:
537  * Function references @ti first to ensure table won't
538  * disappear or change its type.
539  * After that, prepare_add callback is called for each @tei entry.
540  * Next, we try to add each entry under UH+WHLOCK
541  * using add() callback.
542  * Finally, we free all state by calling flush_entry callback
543  * for each @tei.
544  *
545  * Returns 0 on success.
546  */
547 int
548 add_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
549     struct tentry_info *tei, uint8_t flags, uint32_t count)
550 {
551         struct table_config *tc;
552         struct table_algo *ta;
553         uint16_t kidx;
554         int error, first_error, i, rollback;
555         uint32_t num, numadd;
556         struct tentry_info *ptei;
557         struct tableop_state ts;
558         char ta_buf[TA_BUF_SZ];
559         caddr_t ta_buf_m, v;
560
561         memset(&ts, 0, sizeof(ts));
562         ta = NULL;
563         IPFW_UH_WLOCK(ch);
564
565         /*
566          * Find and reference existing table.
567          */
568 restart:
569         if (ts.modified != 0) {
570                 IPFW_UH_WUNLOCK(ch);
571                 flush_batch_buffer(ch, ta, tei, count, rollback,
572                     ta_buf_m, ta_buf);
573                 memset(&ts, 0, sizeof(ts));
574                 ta = NULL;
575                 IPFW_UH_WLOCK(ch);
576         }
577
578         error = find_ref_table(ch, ti, tei, count, OP_ADD, &tc);
579         if (error != 0) {
580                 IPFW_UH_WUNLOCK(ch);
581                 return (error);
582         }
583         ta = tc->ta;
584
585         /* Fill in tablestate */
586         ts.ch = ch;
587         ts.opstate.func = rollback_add_entry;
588         ts.tc = tc;
589         ts.vshared = tc->vshared;
590         ts.vmask = tc->vmask;
591         ts.ta = ta;
592         ts.tei = tei;
593         ts.count = count;
594         rollback = 0;
595         add_toperation_state(ch, &ts);
596         IPFW_UH_WUNLOCK(ch);
597
598         /* Allocate memory and prepare record(s) */
599         /* Pass stack buffer by default */
600         ta_buf_m = ta_buf;
601         error = prepare_batch_buffer(ch, ta, tei, count, OP_ADD, &ta_buf_m);
602
603         IPFW_UH_WLOCK(ch);
604         del_toperation_state(ch, &ts);
605         /* Drop reference we've used in first search */
606         tc->no.refcnt--;
607
608         /* Check prepare_batch_buffer() error */
609         if (error != 0)
610                 goto cleanup;
611
612         /*
613          * Check if table swap has happened.
614          * (so table algo might be changed).
615          * Restart operation to achieve consistent behavior.
616          */
617         if (ts.modified != 0)
618                 goto restart;
619
620         /*
621          * Link all values values to shared/per-table value array.
622          *
623          * May release/reacquire UH_WLOCK.
624          */
625         error = ipfw_link_table_values(ch, &ts);
626         if (error != 0)
627                 goto cleanup;
628         if (ts.modified != 0)
629                 goto restart;
630
631         /*
632          * Ensure we are able to add all entries without additional
633          * memory allocations. May release/reacquire UH_WLOCK.
634          */
635         kidx = tc->no.kidx;
636         error = check_table_space(ch, &ts, tc, KIDX_TO_TI(ch, kidx), count);
637         if (error != 0)
638                 goto cleanup;
639         if (ts.modified != 0)
640                 goto restart;
641
642         /* We've got valid table in @tc. Let's try to add data */
643         kidx = tc->no.kidx;
644         ta = tc->ta;
645         numadd = 0;
646         first_error = 0;
647
648         IPFW_WLOCK(ch);
649
650         v = ta_buf_m;
651         for (i = 0; i < count; i++, v += ta->ta_buf_size) {
652                 ptei = &tei[i];
653                 num = 0;
654                 /* check limit before adding */
655                 if ((error = check_table_limit(tc, ptei)) == 0) {
656                         error = ta->add(tc->astate, KIDX_TO_TI(ch, kidx),
657                             ptei, v, &num);
658                         /* Set status flag to inform userland */
659                         store_tei_result(ptei, OP_ADD, error, num);
660                 }
661                 if (error == 0) {
662                         /* Update number of records to ease limit checking */
663                         tc->count += num;
664                         numadd += num;
665                         continue;
666                 }
667
668                 if (first_error == 0)
669                         first_error = error;
670
671                 /*
672                  * Some error have happened. Check our atomicity
673                  * settings: continue if atomicity is not required,
674                  * rollback changes otherwise.
675                  */
676                 if ((flags & IPFW_CTF_ATOMIC) == 0)
677                         continue;
678
679                 rollback_added_entries(ch, tc, KIDX_TO_TI(ch, kidx),
680                     tei, ta_buf_m, count, i);
681
682                 rollback = 1;
683                 break;
684         }
685
686         IPFW_WUNLOCK(ch);
687
688         ipfw_garbage_table_values(ch, tc, tei, count, rollback);
689
690         /* Permit post-add algorithm grow/rehash. */
691         if (numadd != 0)
692                 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
693
694         /* Return first error to user, if any */
695         error = first_error;
696
697 cleanup:
698         IPFW_UH_WUNLOCK(ch);
699
700         flush_batch_buffer(ch, ta, tei, count, rollback, ta_buf_m, ta_buf);
701         
702         return (error);
703 }
704
705 /*
706  * Deletes one or more entries in table @ti.
707  *
708  * Returns 0 on success.
709  */
710 int
711 del_table_entry(struct ip_fw_chain *ch, struct tid_info *ti,
712     struct tentry_info *tei, uint8_t flags, uint32_t count)
713 {
714         struct table_config *tc;
715         struct table_algo *ta;
716         struct tentry_info *ptei;
717         uint16_t kidx;
718         int error, first_error, i;
719         uint32_t num, numdel;
720         char ta_buf[TA_BUF_SZ];
721         caddr_t ta_buf_m, v;
722
723         /*
724          * Find and reference existing table.
725          */
726         IPFW_UH_WLOCK(ch);
727         error = find_ref_table(ch, ti, tei, count, OP_DEL, &tc);
728         if (error != 0) {
729                 IPFW_UH_WUNLOCK(ch);
730                 return (error);
731         }
732         ta = tc->ta;
733         IPFW_UH_WUNLOCK(ch);
734
735         /* Allocate memory and prepare record(s) */
736         /* Pass stack buffer by default */
737         ta_buf_m = ta_buf;
738         error = prepare_batch_buffer(ch, ta, tei, count, OP_DEL, &ta_buf_m);
739         if (error != 0)
740                 goto cleanup;
741
742         IPFW_UH_WLOCK(ch);
743
744         /* Drop reference we've used in first search */
745         tc->no.refcnt--;
746
747         /*
748          * Check if table algo is still the same.
749          * (changed ta may be the result of table swap).
750          */
751         if (ta != tc->ta) {
752                 IPFW_UH_WUNLOCK(ch);
753                 error = EINVAL;
754                 goto cleanup;
755         }
756
757         kidx = tc->no.kidx;
758         numdel = 0;
759         first_error = 0;
760
761         IPFW_WLOCK(ch);
762         v = ta_buf_m;
763         for (i = 0; i < count; i++, v += ta->ta_buf_size) {
764                 ptei = &tei[i];
765                 num = 0;
766                 error = ta->del(tc->astate, KIDX_TO_TI(ch, kidx), ptei, v,
767                     &num);
768                 /* Save state for userland */
769                 store_tei_result(ptei, OP_DEL, error, num);
770                 if (error != 0 && first_error == 0)
771                         first_error = error;
772                 tc->count -= num;
773                 numdel += num;
774         }
775         IPFW_WUNLOCK(ch);
776
777         /* Unlink non-used values */
778         ipfw_garbage_table_values(ch, tc, tei, count, 0);
779
780         if (numdel != 0) {
781                 /* Run post-del hook to permit shrinking */
782                 check_table_space(ch, NULL, tc, KIDX_TO_TI(ch, kidx), 0);
783         }
784
785         IPFW_UH_WUNLOCK(ch);
786
787         /* Return first error to user, if any */
788         error = first_error;
789
790 cleanup:
791         flush_batch_buffer(ch, ta, tei, count, 0, ta_buf_m, ta_buf);
792
793         return (error);
794 }
795
796 /*
797  * Ensure that table @tc has enough space to add @count entries without
798  * need for reallocation.
799  *
800  * Callbacks order:
801  * 0) need_modify() (UH_WLOCK) - checks if @count items can be added w/o resize.
802  *
803  * 1) alloc_modify (no locks, M_WAITOK) - alloc new state based on @pflags.
804  * 2) prepare_modifyt (UH_WLOCK) - copy old data into new storage
805  * 3) modify (UH_WLOCK + WLOCK) - switch pointers
806  * 4) flush_modify (UH_WLOCK) - free state, if needed
807  *
808  * Returns 0 on success.
809  */
810 static int
811 check_table_space(struct ip_fw_chain *ch, struct tableop_state *ts,
812     struct table_config *tc, struct table_info *ti, uint32_t count)
813 {
814         struct table_algo *ta;
815         uint64_t pflags;
816         char ta_buf[TA_BUF_SZ];
817         int error;
818
819         IPFW_UH_WLOCK_ASSERT(ch);
820
821         error = 0;
822         ta = tc->ta;
823         if (ta->need_modify == NULL)
824                 return (0);
825
826         /* Acquire reference not to loose @tc between locks/unlocks */
827         tc->no.refcnt++;
828
829         /*
830          * TODO: think about avoiding race between large add/large delete
831          * operation on algorithm which implements shrinking along with
832          * growing.
833          */
834         while (true) {
835                 pflags = 0;
836                 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
837                         error = 0;
838                         break;
839                 }
840
841                 /* We have to shrink/grow table */
842                 if (ts != NULL)
843                         add_toperation_state(ch, ts);
844                 IPFW_UH_WUNLOCK(ch);
845
846                 memset(&ta_buf, 0, sizeof(ta_buf));
847                 error = ta->prepare_mod(ta_buf, &pflags);
848
849                 IPFW_UH_WLOCK(ch);
850                 if (ts != NULL)
851                         del_toperation_state(ch, ts);
852
853                 if (error != 0)
854                         break;
855
856                 if (ts != NULL && ts->modified != 0) {
857
858                         /*
859                          * Swap operation has happened
860                          * so we're currently operating on other
861                          * table data. Stop doing this.
862                          */
863                         ta->flush_mod(ta_buf);
864                         break;
865                 }
866
867                 /* Check if we still need to alter table */
868                 ti = KIDX_TO_TI(ch, tc->no.kidx);
869                 if (ta->need_modify(tc->astate, ti, count, &pflags) == 0) {
870                         IPFW_UH_WUNLOCK(ch);
871
872                         /*
873                          * Other thread has already performed resize.
874                          * Flush our state and return.
875                          */
876                         ta->flush_mod(ta_buf);
877                         break;
878                 }
879         
880                 error = ta->fill_mod(tc->astate, ti, ta_buf, &pflags);
881                 if (error == 0) {
882                         /* Do actual modification */
883                         IPFW_WLOCK(ch);
884                         ta->modify(tc->astate, ti, ta_buf, pflags);
885                         IPFW_WUNLOCK(ch);
886                 }
887
888                 /* Anyway, flush data and retry */
889                 ta->flush_mod(ta_buf);
890         }
891
892         tc->no.refcnt--;
893         return (error);
894 }
895
896 /*
897  * Adds or deletes record in table.
898  * Data layout (v0):
899  * Request: [ ip_fw3_opheader ipfw_table_xentry ]
900  *
901  * Returns 0 on success
902  */
903 static int
904 manage_table_ent_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
905     struct sockopt_data *sd)
906 {
907         ipfw_table_xentry *xent;
908         struct tentry_info tei;
909         struct tid_info ti;
910         struct table_value v;
911         int error, hdrlen, read;
912
913         hdrlen = offsetof(ipfw_table_xentry, k);
914
915         /* Check minimum header size */
916         if (sd->valsize < (sizeof(*op3) + hdrlen))
917                 return (EINVAL);
918
919         read = sizeof(ip_fw3_opheader);
920
921         /* Check if xentry len field is valid */
922         xent = (ipfw_table_xentry *)(op3 + 1);
923         if (xent->len < hdrlen || xent->len + read > sd->valsize)
924                 return (EINVAL);
925         
926         memset(&tei, 0, sizeof(tei));
927         tei.paddr = &xent->k;
928         tei.masklen = xent->masklen;
929         ipfw_import_table_value_legacy(xent->value, &v);
930         tei.pvalue = &v;
931         /* Old requests compatibility */
932         tei.flags = TEI_FLAGS_COMPAT;
933         if (xent->type == IPFW_TABLE_ADDR) {
934                 if (xent->len - hdrlen == sizeof(in_addr_t))
935                         tei.subtype = AF_INET;
936                 else
937                         tei.subtype = AF_INET6;
938         }
939
940         memset(&ti, 0, sizeof(ti));
941         ti.uidx = xent->tbl;
942         ti.type = xent->type;
943
944         error = (op3->opcode == IP_FW_TABLE_XADD) ?
945             add_table_entry(ch, &ti, &tei, 0, 1) :
946             del_table_entry(ch, &ti, &tei, 0, 1);
947
948         return (error);
949 }
950
951 /*
952  * Adds or deletes record in table.
953  * Data layout (v1)(current):
954  * Request: [ ipfw_obj_header
955  *   ipfw_obj_ctlv(IPFW_TLV_TBLENT_LIST) [ ipfw_obj_tentry x N ]
956  * ]
957  *
958  * Returns 0 on success
959  */
960 static int
961 manage_table_ent_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
962     struct sockopt_data *sd)
963 {
964         ipfw_obj_tentry *tent, *ptent;
965         ipfw_obj_ctlv *ctlv;
966         ipfw_obj_header *oh;
967         struct tentry_info *ptei, tei, *tei_buf;
968         struct tid_info ti;
969         int error, i, kidx, read;
970
971         /* Check minimum header size */
972         if (sd->valsize < (sizeof(*oh) + sizeof(*ctlv)))
973                 return (EINVAL);
974
975         /* Check if passed data is too long */
976         if (sd->valsize != sd->kavail)
977                 return (EINVAL);
978
979         oh = (ipfw_obj_header *)sd->kbuf;
980
981         /* Basic length checks for TLVs */
982         if (oh->ntlv.head.length != sizeof(oh->ntlv))
983                 return (EINVAL);
984
985         read = sizeof(*oh);
986
987         ctlv = (ipfw_obj_ctlv *)(oh + 1);
988         if (ctlv->head.length + read != sd->valsize)
989                 return (EINVAL);
990
991         read += sizeof(*ctlv);
992         tent = (ipfw_obj_tentry *)(ctlv + 1);
993         if (ctlv->count * sizeof(*tent) + read != sd->valsize)
994                 return (EINVAL);
995
996         if (ctlv->count == 0)
997                 return (0);
998
999         /*
1000          * Mark entire buffer as "read".
1001          * This instructs sopt api write it back
1002          * after function return.
1003          */
1004         ipfw_get_sopt_header(sd, sd->valsize);
1005
1006         /* Perform basic checks for each entry */
1007         ptent = tent;
1008         kidx = tent->idx;
1009         for (i = 0; i < ctlv->count; i++, ptent++) {
1010                 if (ptent->head.length != sizeof(*ptent))
1011                         return (EINVAL);
1012                 if (ptent->idx != kidx)
1013                         return (ENOTSUP);
1014         }
1015
1016         /* Convert data into kernel request objects */
1017         objheader_to_ti(oh, &ti);
1018         ti.type = oh->ntlv.type;
1019         ti.uidx = kidx;
1020
1021         /* Use on-stack buffer for single add/del */
1022         if (ctlv->count == 1) {
1023                 memset(&tei, 0, sizeof(tei));
1024                 tei_buf = &tei;
1025         } else
1026                 tei_buf = malloc(ctlv->count * sizeof(tei), M_TEMP,
1027                     M_WAITOK | M_ZERO);
1028
1029         ptei = tei_buf;
1030         ptent = tent;
1031         for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1032                 ptei->paddr = &ptent->k;
1033                 ptei->subtype = ptent->subtype;
1034                 ptei->masklen = ptent->masklen;
1035                 if (ptent->head.flags & IPFW_TF_UPDATE)
1036                         ptei->flags |= TEI_FLAGS_UPDATE;
1037
1038                 ipfw_import_table_value_v1(&ptent->v.value);
1039                 ptei->pvalue = (struct table_value *)&ptent->v.value;
1040         }
1041
1042         error = (oh->opheader.opcode == IP_FW_TABLE_XADD) ?
1043             add_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count) :
1044             del_table_entry(ch, &ti, tei_buf, ctlv->flags, ctlv->count);
1045
1046         /* Translate result back to userland */
1047         ptei = tei_buf;
1048         ptent = tent;
1049         for (i = 0; i < ctlv->count; i++, ptent++, ptei++) {
1050                 if (ptei->flags & TEI_FLAGS_ADDED)
1051                         ptent->result = IPFW_TR_ADDED;
1052                 else if (ptei->flags & TEI_FLAGS_DELETED)
1053                         ptent->result = IPFW_TR_DELETED;
1054                 else if (ptei->flags & TEI_FLAGS_UPDATED)
1055                         ptent->result = IPFW_TR_UPDATED;
1056                 else if (ptei->flags & TEI_FLAGS_LIMIT)
1057                         ptent->result = IPFW_TR_LIMIT;
1058                 else if (ptei->flags & TEI_FLAGS_ERROR)
1059                         ptent->result = IPFW_TR_ERROR;
1060                 else if (ptei->flags & TEI_FLAGS_NOTFOUND)
1061                         ptent->result = IPFW_TR_NOTFOUND;
1062                 else if (ptei->flags & TEI_FLAGS_EXISTS)
1063                         ptent->result = IPFW_TR_EXISTS;
1064                 ipfw_export_table_value_v1(ptei->pvalue, &ptent->v.value);
1065         }
1066
1067         if (tei_buf != &tei)
1068                 free(tei_buf, M_TEMP);
1069
1070         return (error);
1071 }
1072
1073 /*
1074  * Looks up an entry in given table.
1075  * Data layout (v0)(current):
1076  * Request: [ ipfw_obj_header ipfw_obj_tentry ]
1077  * Reply: [ ipfw_obj_header ipfw_obj_tentry ]
1078  *
1079  * Returns 0 on success
1080  */
1081 static int
1082 find_table_entry(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1083     struct sockopt_data *sd)
1084 {
1085         ipfw_obj_tentry *tent;
1086         ipfw_obj_header *oh;
1087         struct tid_info ti;
1088         struct table_config *tc;
1089         struct table_algo *ta;
1090         struct table_info *kti;
1091         struct table_value *pval;
1092         struct namedobj_instance *ni;
1093         int error;
1094         size_t sz;
1095
1096         /* Check minimum header size */
1097         sz = sizeof(*oh) + sizeof(*tent);
1098         if (sd->valsize != sz)
1099                 return (EINVAL);
1100
1101         oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1102         tent = (ipfw_obj_tentry *)(oh + 1);
1103
1104         /* Basic length checks for TLVs */
1105         if (oh->ntlv.head.length != sizeof(oh->ntlv))
1106                 return (EINVAL);
1107
1108         objheader_to_ti(oh, &ti);
1109         ti.type = oh->ntlv.type;
1110         ti.uidx = tent->idx;
1111
1112         IPFW_UH_RLOCK(ch);
1113         ni = CHAIN_TO_NI(ch);
1114
1115         /*
1116          * Find existing table and check its type .
1117          */
1118         ta = NULL;
1119         if ((tc = find_table(ni, &ti)) == NULL) {
1120                 IPFW_UH_RUNLOCK(ch);
1121                 return (ESRCH);
1122         }
1123
1124         /* check table type */
1125         if (tc->no.subtype != ti.type) {
1126                 IPFW_UH_RUNLOCK(ch);
1127                 return (EINVAL);
1128         }
1129
1130         kti = KIDX_TO_TI(ch, tc->no.kidx);
1131         ta = tc->ta;
1132
1133         if (ta->find_tentry == NULL)
1134                 return (ENOTSUP);
1135
1136         error = ta->find_tentry(tc->astate, kti, tent);
1137         if (error == 0) {
1138                 pval = get_table_value(ch, tc, tent->v.kidx);
1139                 ipfw_export_table_value_v1(pval, &tent->v.value);
1140         }
1141         IPFW_UH_RUNLOCK(ch);
1142
1143         return (error);
1144 }
1145
1146 /*
1147  * Flushes all entries or destroys given table.
1148  * Data layout (v0)(current):
1149  * Request: [ ipfw_obj_header ]
1150  *
1151  * Returns 0 on success
1152  */
1153 static int
1154 flush_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1155     struct sockopt_data *sd)
1156 {
1157         int error;
1158         struct _ipfw_obj_header *oh;
1159         struct tid_info ti;
1160
1161         if (sd->valsize != sizeof(*oh))
1162                 return (EINVAL);
1163
1164         oh = (struct _ipfw_obj_header *)op3;
1165         objheader_to_ti(oh, &ti);
1166
1167         if (op3->opcode == IP_FW_TABLE_XDESTROY)
1168                 error = destroy_table(ch, &ti);
1169         else if (op3->opcode == IP_FW_TABLE_XFLUSH)
1170                 error = flush_table(ch, &ti);
1171         else
1172                 return (ENOTSUP);
1173
1174         return (error);
1175 }
1176
1177 static void
1178 restart_flush(void *object, struct op_state *_state)
1179 {
1180         struct tableop_state *ts;
1181
1182         ts = (struct tableop_state *)_state;
1183
1184         if (ts->tc != object)
1185                 return;
1186
1187         /* Indicate we've called */
1188         ts->modified = 1;
1189 }
1190
1191 /*
1192  * Flushes given table.
1193  *
1194  * Function create new table instance with the same
1195  * parameters, swaps it with old one and
1196  * flushes state without holding runtime WLOCK.
1197  *
1198  * Returns 0 on success.
1199  */
1200 int
1201 flush_table(struct ip_fw_chain *ch, struct tid_info *ti)
1202 {
1203         struct namedobj_instance *ni;
1204         struct table_config *tc;
1205         struct table_algo *ta;
1206         struct table_info ti_old, ti_new, *tablestate;
1207         void *astate_old, *astate_new;
1208         char algostate[64], *pstate;
1209         struct tableop_state ts;
1210         int error, need_gc;
1211         uint16_t kidx;
1212         uint8_t tflags;
1213
1214         /*
1215          * Stage 1: save table algorithm.
1216          * Reference found table to ensure it won't disappear.
1217          */
1218         IPFW_UH_WLOCK(ch);
1219         ni = CHAIN_TO_NI(ch);
1220         if ((tc = find_table(ni, ti)) == NULL) {
1221                 IPFW_UH_WUNLOCK(ch);
1222                 return (ESRCH);
1223         }
1224         need_gc = 0;
1225         astate_new = NULL;
1226         memset(&ti_new, 0, sizeof(ti_new));
1227 restart:
1228         /* Set up swap handler */
1229         memset(&ts, 0, sizeof(ts));
1230         ts.opstate.func = restart_flush;
1231         ts.tc = tc;
1232
1233         ta = tc->ta;
1234         /* Do not flush readonly tables */
1235         if ((ta->flags & TA_FLAG_READONLY) != 0) {
1236                 IPFW_UH_WUNLOCK(ch);
1237                 return (EACCES);
1238         }
1239         /* Save startup algo parameters */
1240         if (ta->print_config != NULL) {
1241                 ta->print_config(tc->astate, KIDX_TO_TI(ch, tc->no.kidx),
1242                     algostate, sizeof(algostate));
1243                 pstate = algostate;
1244         } else
1245                 pstate = NULL;
1246         tflags = tc->tflags;
1247         tc->no.refcnt++;
1248         add_toperation_state(ch, &ts);
1249         IPFW_UH_WUNLOCK(ch);
1250
1251         /*
1252          * Stage 1.5: if this is not the first attempt, destroy previous state
1253          */
1254         if (need_gc != 0) {
1255                 ta->destroy(astate_new, &ti_new);
1256                 need_gc = 0;
1257         }
1258
1259         /*
1260          * Stage 2: allocate new table instance using same algo.
1261          */
1262         memset(&ti_new, 0, sizeof(struct table_info));
1263         error = ta->init(ch, &astate_new, &ti_new, pstate, tflags);
1264
1265         /*
1266          * Stage 3: swap old state pointers with newly-allocated ones.
1267          * Decrease refcount.
1268          */
1269         IPFW_UH_WLOCK(ch);
1270         tc->no.refcnt--;
1271         del_toperation_state(ch, &ts);
1272
1273         if (error != 0) {
1274                 IPFW_UH_WUNLOCK(ch);
1275                 return (error);
1276         }
1277
1278         /*
1279          * Restart operation if table swap has happened:
1280          * even if algo may be the same, algo init parameters
1281          * may change. Restart operation instead of doing
1282          * complex checks.
1283          */
1284         if (ts.modified != 0) {
1285                 /* Delay destroying data since we're holding UH lock */
1286                 need_gc = 1;
1287                 goto restart;
1288         }
1289
1290         ni = CHAIN_TO_NI(ch);
1291         kidx = tc->no.kidx;
1292         tablestate = (struct table_info *)ch->tablestate;
1293
1294         IPFW_WLOCK(ch);
1295         ti_old = tablestate[kidx];
1296         tablestate[kidx] = ti_new;
1297         IPFW_WUNLOCK(ch);
1298
1299         astate_old = tc->astate;
1300         tc->astate = astate_new;
1301         tc->ti_copy = ti_new;
1302         tc->count = 0;
1303
1304         /* Notify algo on real @ti address */
1305         if (ta->change_ti != NULL)
1306                 ta->change_ti(tc->astate, &tablestate[kidx]);
1307
1308         /*
1309          * Stage 4: unref values.
1310          */
1311         ipfw_unref_table_values(ch, tc, ta, astate_old, &ti_old);
1312         IPFW_UH_WUNLOCK(ch);
1313
1314         /*
1315          * Stage 5: perform real flush/destroy.
1316          */
1317         ta->destroy(astate_old, &ti_old);
1318
1319         return (0);
1320 }
1321
1322 /*
1323  * Swaps two tables.
1324  * Data layout (v0)(current):
1325  * Request: [ ipfw_obj_header ipfw_obj_ntlv ]
1326  *
1327  * Returns 0 on success
1328  */
1329 static int
1330 swap_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1331     struct sockopt_data *sd)
1332 {
1333         int error;
1334         struct _ipfw_obj_header *oh;
1335         struct tid_info ti_a, ti_b;
1336
1337         if (sd->valsize != sizeof(*oh) + sizeof(ipfw_obj_ntlv))
1338                 return (EINVAL);
1339
1340         oh = (struct _ipfw_obj_header *)op3;
1341         ntlv_to_ti(&oh->ntlv, &ti_a);
1342         ntlv_to_ti((ipfw_obj_ntlv *)(oh + 1), &ti_b);
1343
1344         error = swap_tables(ch, &ti_a, &ti_b);
1345
1346         return (error);
1347 }
1348
1349 /*
1350  * Swaps two tables of the same type/valtype.
1351  *
1352  * Checks if tables are compatible and limits
1353  * permits swap, than actually perform swap.
1354  *
1355  * Each table consists of 2 different parts:
1356  * config:
1357  *   @tc (with name, set, kidx) and rule bindings, which is "stable".
1358  *   number of items
1359  *   table algo
1360  * runtime:
1361  *   runtime data @ti (ch->tablestate)
1362  *   runtime cache in @tc
1363  *   algo-specific data (@tc->astate)
1364  *
1365  * So we switch:
1366  *  all runtime data
1367  *   number of items
1368  *   table algo
1369  *
1370  * After that we call @ti change handler for each table.
1371  *
1372  * Note that referencing @tc won't protect tc->ta from change.
1373  * XXX: Do we need to restrict swap between locked tables?
1374  * XXX: Do we need to exchange ftype?
1375  *
1376  * Returns 0 on success.
1377  */
1378 static int
1379 swap_tables(struct ip_fw_chain *ch, struct tid_info *a,
1380     struct tid_info *b)
1381 {
1382         struct namedobj_instance *ni;
1383         struct table_config *tc_a, *tc_b;
1384         struct table_algo *ta;
1385         struct table_info ti, *tablestate;
1386         void *astate;
1387         uint32_t count;
1388
1389         /*
1390          * Stage 1: find both tables and ensure they are of
1391          * the same type.
1392          */
1393         IPFW_UH_WLOCK(ch);
1394         ni = CHAIN_TO_NI(ch);
1395         if ((tc_a = find_table(ni, a)) == NULL) {
1396                 IPFW_UH_WUNLOCK(ch);
1397                 return (ESRCH);
1398         }
1399         if ((tc_b = find_table(ni, b)) == NULL) {
1400                 IPFW_UH_WUNLOCK(ch);
1401                 return (ESRCH);
1402         }
1403
1404         /* It is very easy to swap between the same table */
1405         if (tc_a == tc_b) {
1406                 IPFW_UH_WUNLOCK(ch);
1407                 return (0);
1408         }
1409
1410         /* Check type and value are the same */
1411         if (tc_a->no.subtype!=tc_b->no.subtype || tc_a->tflags!=tc_b->tflags) {
1412                 IPFW_UH_WUNLOCK(ch);
1413                 return (EINVAL);
1414         }
1415
1416         /* Check limits before swap */
1417         if ((tc_a->limit != 0 && tc_b->count > tc_a->limit) ||
1418             (tc_b->limit != 0 && tc_a->count > tc_b->limit)) {
1419                 IPFW_UH_WUNLOCK(ch);
1420                 return (EFBIG);
1421         }
1422
1423         /* Check if one of the tables is readonly */
1424         if (((tc_a->ta->flags | tc_b->ta->flags) & TA_FLAG_READONLY) != 0) {
1425                 IPFW_UH_WUNLOCK(ch);
1426                 return (EACCES);
1427         }
1428
1429         /* Notify we're going to swap */
1430         rollback_toperation_state(ch, tc_a);
1431         rollback_toperation_state(ch, tc_b);
1432
1433         /* Everything is fine, prepare to swap */
1434         tablestate = (struct table_info *)ch->tablestate;
1435         ti = tablestate[tc_a->no.kidx];
1436         ta = tc_a->ta;
1437         astate = tc_a->astate;
1438         count = tc_a->count;
1439
1440         IPFW_WLOCK(ch);
1441         /* a <- b */
1442         tablestate[tc_a->no.kidx] = tablestate[tc_b->no.kidx];
1443         tc_a->ta = tc_b->ta;
1444         tc_a->astate = tc_b->astate;
1445         tc_a->count = tc_b->count;
1446         /* b <- a */
1447         tablestate[tc_b->no.kidx] = ti;
1448         tc_b->ta = ta;
1449         tc_b->astate = astate;
1450         tc_b->count = count;
1451         IPFW_WUNLOCK(ch);
1452
1453         /* Ensure tc.ti copies are in sync */
1454         tc_a->ti_copy = tablestate[tc_a->no.kidx];
1455         tc_b->ti_copy = tablestate[tc_b->no.kidx];
1456
1457         /* Notify both tables on @ti change */
1458         if (tc_a->ta->change_ti != NULL)
1459                 tc_a->ta->change_ti(tc_a->astate, &tablestate[tc_a->no.kidx]);
1460         if (tc_b->ta->change_ti != NULL)
1461                 tc_b->ta->change_ti(tc_b->astate, &tablestate[tc_b->no.kidx]);
1462
1463         IPFW_UH_WUNLOCK(ch);
1464
1465         return (0);
1466 }
1467
1468 /*
1469  * Destroys table specified by @ti.
1470  * Data layout (v0)(current):
1471  * Request: [ ip_fw3_opheader ]
1472  *
1473  * Returns 0 on success
1474  */
1475 static int
1476 destroy_table(struct ip_fw_chain *ch, struct tid_info *ti)
1477 {
1478         struct namedobj_instance *ni;
1479         struct table_config *tc;
1480
1481         IPFW_UH_WLOCK(ch);
1482
1483         ni = CHAIN_TO_NI(ch);
1484         if ((tc = find_table(ni, ti)) == NULL) {
1485                 IPFW_UH_WUNLOCK(ch);
1486                 return (ESRCH);
1487         }
1488
1489         /* Do not permit destroying referenced tables */
1490         if (tc->no.refcnt > 0) {
1491                 IPFW_UH_WUNLOCK(ch);
1492                 return (EBUSY);
1493         }
1494
1495         IPFW_WLOCK(ch);
1496         unlink_table(ch, tc);
1497         IPFW_WUNLOCK(ch);
1498
1499         /* Free obj index */
1500         if (ipfw_objhash_free_idx(ni, tc->no.kidx) != 0)
1501                 printf("Error unlinking kidx %d from table %s\n",
1502                     tc->no.kidx, tc->tablename);
1503
1504         /* Unref values used in tables while holding UH lock */
1505         ipfw_unref_table_values(ch, tc, tc->ta, tc->astate, &tc->ti_copy);
1506         IPFW_UH_WUNLOCK(ch);
1507
1508         free_table_config(ni, tc);
1509
1510         return (0);
1511 }
1512
1513 static uint32_t
1514 roundup2p(uint32_t v)
1515 {
1516
1517         v--;
1518         v |= v >> 1;
1519         v |= v >> 2;
1520         v |= v >> 4;
1521         v |= v >> 8;
1522         v |= v >> 16;
1523         v++;
1524
1525         return (v);
1526 }
1527
1528 /*
1529  * Grow tables index.
1530  *
1531  * Returns 0 on success.
1532  */
1533 int
1534 ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables)
1535 {
1536         unsigned int ntables_old, tbl;
1537         struct namedobj_instance *ni;
1538         void *new_idx, *old_tablestate, *tablestate;
1539         struct table_info *ti;
1540         struct table_config *tc;
1541         int i, new_blocks;
1542
1543         /* Check new value for validity */
1544         if (ntables == 0)
1545                 return (EINVAL);
1546         if (ntables > IPFW_TABLES_MAX)
1547                 ntables = IPFW_TABLES_MAX;
1548         /* Alight to nearest power of 2 */
1549         ntables = (unsigned int)roundup2p(ntables); 
1550
1551         /* Allocate new pointers */
1552         tablestate = malloc(ntables * sizeof(struct table_info),
1553             M_IPFW, M_WAITOK | M_ZERO);
1554
1555         ipfw_objhash_bitmap_alloc(ntables, (void *)&new_idx, &new_blocks);
1556
1557         IPFW_UH_WLOCK(ch);
1558
1559         tbl = (ntables >= V_fw_tables_max) ? V_fw_tables_max : ntables;
1560         ni = CHAIN_TO_NI(ch);
1561
1562         /* Temporary restrict decreasing max_tables */
1563         if (ntables < V_fw_tables_max) {
1564
1565                 /*
1566                  * FIXME: Check if we really can shrink
1567                  */
1568                 IPFW_UH_WUNLOCK(ch);
1569                 return (EINVAL);
1570         }
1571
1572         /* Copy table info/indices */
1573         memcpy(tablestate, ch->tablestate, sizeof(struct table_info) * tbl);
1574         ipfw_objhash_bitmap_merge(ni, &new_idx, &new_blocks);
1575
1576         IPFW_WLOCK(ch);
1577
1578         /* Change pointers */
1579         old_tablestate = ch->tablestate;
1580         ch->tablestate = tablestate;
1581         ipfw_objhash_bitmap_swap(ni, &new_idx, &new_blocks);
1582
1583         ntables_old = V_fw_tables_max;
1584         V_fw_tables_max = ntables;
1585
1586         IPFW_WUNLOCK(ch);
1587
1588         /* Notify all consumers that their @ti pointer has changed */
1589         ti = (struct table_info *)ch->tablestate;
1590         for (i = 0; i < tbl; i++, ti++) {
1591                 if (ti->lookup == NULL)
1592                         continue;
1593                 tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, i);
1594                 if (tc == NULL || tc->ta->change_ti == NULL)
1595                         continue;
1596
1597                 tc->ta->change_ti(tc->astate, ti);
1598         }
1599
1600         IPFW_UH_WUNLOCK(ch);
1601
1602         /* Free old pointers */
1603         free(old_tablestate, M_IPFW);
1604         ipfw_objhash_bitmap_free(new_idx, new_blocks);
1605
1606         return (0);
1607 }
1608
1609 /*
1610  * Lookup table's named object by its @kidx.
1611  */
1612 struct named_object *
1613 ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch, uint16_t kidx)
1614 {
1615
1616         return (ipfw_objhash_lookup_kidx(CHAIN_TO_NI(ch), kidx));
1617 }
1618
1619 /*
1620  * Take reference to table specified in @ntlv.
1621  * On success return its @kidx.
1622  */
1623 int
1624 ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx)
1625 {
1626         struct tid_info ti;
1627         struct table_config *tc;
1628         int error;
1629
1630         IPFW_UH_WLOCK_ASSERT(ch);
1631
1632         ntlv_to_ti(ntlv, &ti);
1633         error = find_table_err(CHAIN_TO_NI(ch), &ti, &tc);
1634         if (error != 0)
1635                 return (error);
1636
1637         if (tc == NULL)
1638                 return (ESRCH);
1639
1640         tc_ref(tc);
1641         *kidx = tc->no.kidx;
1642
1643         return (0);
1644 }
1645
1646 void
1647 ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx)
1648 {
1649
1650         struct namedobj_instance *ni;
1651         struct named_object *no;
1652
1653         IPFW_UH_WLOCK_ASSERT(ch);
1654         ni = CHAIN_TO_NI(ch);
1655         no = ipfw_objhash_lookup_kidx(ni, kidx);
1656         KASSERT(no != NULL, ("Table with index %d not found", kidx));
1657         no->refcnt--;
1658 }
1659
1660 /*
1661  * Lookup an arbitrary key @paddr of length @plen in table @tbl.
1662  * Stores found value in @val.
1663  *
1664  * Returns 1 if key was found.
1665  */
1666 int
1667 ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
1668     void *paddr, uint32_t *val)
1669 {
1670         struct table_info *ti;
1671
1672         ti = KIDX_TO_TI(ch, tbl);
1673
1674         return (ti->lookup(ti, paddr, plen, val));
1675 }
1676
1677 /*
1678  * Info/List/dump support for tables.
1679  *
1680  */
1681
1682 /*
1683  * High-level 'get' cmds sysctl handlers
1684  */
1685
1686 /*
1687  * Lists all tables currently available in kernel.
1688  * Data layout (v0)(current):
1689  * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
1690  * Reply: [ ipfw_obj_lheader ipfw_xtable_info x N ]
1691  *
1692  * Returns 0 on success
1693  */
1694 static int
1695 list_tables(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1696     struct sockopt_data *sd)
1697 {
1698         struct _ipfw_obj_lheader *olh;
1699         int error;
1700
1701         olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
1702         if (olh == NULL)
1703                 return (EINVAL);
1704         if (sd->valsize < olh->size)
1705                 return (EINVAL);
1706
1707         IPFW_UH_RLOCK(ch);
1708         error = export_tables(ch, olh, sd);
1709         IPFW_UH_RUNLOCK(ch);
1710
1711         return (error);
1712 }
1713
1714 /*
1715  * Store table info to buffer provided by @sd.
1716  * Data layout (v0)(current):
1717  * Request: [ ipfw_obj_header ipfw_xtable_info(empty)]
1718  * Reply: [ ipfw_obj_header ipfw_xtable_info ]
1719  *
1720  * Returns 0 on success.
1721  */
1722 static int
1723 describe_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1724     struct sockopt_data *sd)
1725 {
1726         struct _ipfw_obj_header *oh;
1727         struct table_config *tc;
1728         struct tid_info ti;
1729         size_t sz;
1730
1731         sz = sizeof(*oh) + sizeof(ipfw_xtable_info);
1732         oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
1733         if (oh == NULL)
1734                 return (EINVAL);
1735
1736         objheader_to_ti(oh, &ti);
1737
1738         IPFW_UH_RLOCK(ch);
1739         if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
1740                 IPFW_UH_RUNLOCK(ch);
1741                 return (ESRCH);
1742         }
1743
1744         export_table_info(ch, tc, (ipfw_xtable_info *)(oh + 1));
1745         IPFW_UH_RUNLOCK(ch);
1746
1747         return (0);
1748 }
1749
1750 /*
1751  * Modifies existing table.
1752  * Data layout (v0)(current):
1753  * Request: [ ipfw_obj_header ipfw_xtable_info ]
1754  *
1755  * Returns 0 on success
1756  */
1757 static int
1758 modify_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1759     struct sockopt_data *sd)
1760 {
1761         struct _ipfw_obj_header *oh;
1762         ipfw_xtable_info *i;
1763         char *tname;
1764         struct tid_info ti;
1765         struct namedobj_instance *ni;
1766         struct table_config *tc;
1767
1768         if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1769                 return (EINVAL);
1770
1771         oh = (struct _ipfw_obj_header *)sd->kbuf;
1772         i = (ipfw_xtable_info *)(oh + 1);
1773
1774         /*
1775          * Verify user-supplied strings.
1776          * Check for null-terminated/zero-length strings/
1777          */
1778         tname = oh->ntlv.name;
1779         if (check_table_name(tname) != 0)
1780                 return (EINVAL);
1781
1782         objheader_to_ti(oh, &ti);
1783         ti.type = i->type;
1784
1785         IPFW_UH_WLOCK(ch);
1786         ni = CHAIN_TO_NI(ch);
1787         if ((tc = find_table(ni, &ti)) == NULL) {
1788                 IPFW_UH_WUNLOCK(ch);
1789                 return (ESRCH);
1790         }
1791
1792         /* Do not support any modifications for readonly tables */
1793         if ((tc->ta->flags & TA_FLAG_READONLY) != 0) {
1794                 IPFW_UH_WUNLOCK(ch);
1795                 return (EACCES);
1796         }
1797
1798         if ((i->mflags & IPFW_TMFLAGS_LIMIT) != 0)
1799                 tc->limit = i->limit;
1800         if ((i->mflags & IPFW_TMFLAGS_LOCK) != 0)
1801                 tc->locked = ((i->flags & IPFW_TGFLAGS_LOCKED) != 0);
1802         IPFW_UH_WUNLOCK(ch);
1803
1804         return (0);
1805 }
1806
1807 /*
1808  * Creates new table.
1809  * Data layout (v0)(current):
1810  * Request: [ ipfw_obj_header ipfw_xtable_info ]
1811  *
1812  * Returns 0 on success
1813  */
1814 static int
1815 create_table(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1816     struct sockopt_data *sd)
1817 {
1818         struct _ipfw_obj_header *oh;
1819         ipfw_xtable_info *i;
1820         char *tname, *aname;
1821         struct tid_info ti;
1822         struct namedobj_instance *ni;
1823
1824         if (sd->valsize != sizeof(*oh) + sizeof(ipfw_xtable_info))
1825                 return (EINVAL);
1826
1827         oh = (struct _ipfw_obj_header *)sd->kbuf;
1828         i = (ipfw_xtable_info *)(oh + 1);
1829
1830         /*
1831          * Verify user-supplied strings.
1832          * Check for null-terminated/zero-length strings/
1833          */
1834         tname = oh->ntlv.name;
1835         aname = i->algoname;
1836         if (check_table_name(tname) != 0 ||
1837             strnlen(aname, sizeof(i->algoname)) == sizeof(i->algoname))
1838                 return (EINVAL);
1839
1840         if (aname[0] == '\0') {
1841                 /* Use default algorithm */
1842                 aname = NULL;
1843         }
1844
1845         objheader_to_ti(oh, &ti);
1846         ti.type = i->type;
1847
1848         ni = CHAIN_TO_NI(ch);
1849
1850         IPFW_UH_RLOCK(ch);
1851         if (find_table(ni, &ti) != NULL) {
1852                 IPFW_UH_RUNLOCK(ch);
1853                 return (EEXIST);
1854         }
1855         IPFW_UH_RUNLOCK(ch);
1856
1857         return (create_table_internal(ch, &ti, aname, i, NULL, 0));
1858 }
1859
1860 /*
1861  * Creates new table based on @ti and @aname.
1862  *
1863  * Assume @aname to be checked and valid.
1864  * Stores allocated table kidx inside @pkidx (if non-NULL).
1865  * Reference created table if @compat is non-zero.
1866  *
1867  * Returns 0 on success.
1868  */
1869 static int
1870 create_table_internal(struct ip_fw_chain *ch, struct tid_info *ti,
1871     char *aname, ipfw_xtable_info *i, uint16_t *pkidx, int compat)
1872 {
1873         struct namedobj_instance *ni;
1874         struct table_config *tc, *tc_new, *tmp;
1875         struct table_algo *ta;
1876         uint16_t kidx;
1877
1878         ni = CHAIN_TO_NI(ch);
1879
1880         ta = find_table_algo(CHAIN_TO_TCFG(ch), ti, aname);
1881         if (ta == NULL)
1882                 return (ENOTSUP);
1883
1884         tc = alloc_table_config(ch, ti, ta, aname, i->tflags);
1885         if (tc == NULL)
1886                 return (ENOMEM);
1887
1888         tc->vmask = i->vmask;
1889         tc->limit = i->limit;
1890         if (ta->flags & TA_FLAG_READONLY)
1891                 tc->locked = 1;
1892         else
1893                 tc->locked = (i->flags & IPFW_TGFLAGS_LOCKED) != 0;
1894
1895         IPFW_UH_WLOCK(ch);
1896
1897         /* Check if table has been already created */
1898         tc_new = find_table(ni, ti);
1899         if (tc_new != NULL) {
1900
1901                 /*
1902                  * Compat: do not fail if we're
1903                  * requesting to create existing table
1904                  * which has the same type
1905                  */
1906                 if (compat == 0 || tc_new->no.subtype != tc->no.subtype) {
1907                         IPFW_UH_WUNLOCK(ch);
1908                         free_table_config(ni, tc);
1909                         return (EEXIST);
1910                 }
1911
1912                 /* Exchange tc and tc_new for proper refcounting & freeing */
1913                 tmp = tc;
1914                 tc = tc_new;
1915                 tc_new = tmp;
1916         } else {
1917                 /* New table */
1918                 if (ipfw_objhash_alloc_idx(ni, &kidx) != 0) {
1919                         IPFW_UH_WUNLOCK(ch);
1920                         printf("Unable to allocate table index."
1921                             " Consider increasing net.inet.ip.fw.tables_max");
1922                         free_table_config(ni, tc);
1923                         return (EBUSY);
1924                 }
1925                 tc->no.kidx = kidx;
1926                 tc->no.etlv = IPFW_TLV_TBL_NAME;
1927
1928                 link_table(ch, tc);
1929         }
1930
1931         if (compat != 0)
1932                 tc->no.refcnt++;
1933         if (pkidx != NULL)
1934                 *pkidx = tc->no.kidx;
1935
1936         IPFW_UH_WUNLOCK(ch);
1937
1938         if (tc_new != NULL)
1939                 free_table_config(ni, tc_new);
1940
1941         return (0);
1942 }
1943
1944 static void
1945 ntlv_to_ti(ipfw_obj_ntlv *ntlv, struct tid_info *ti)
1946 {
1947
1948         memset(ti, 0, sizeof(struct tid_info));
1949         ti->set = ntlv->set;
1950         ti->uidx = ntlv->idx;
1951         ti->tlvs = ntlv;
1952         ti->tlen = ntlv->head.length;
1953 }
1954
1955 static void
1956 objheader_to_ti(struct _ipfw_obj_header *oh, struct tid_info *ti)
1957 {
1958
1959         ntlv_to_ti(&oh->ntlv, ti);
1960 }
1961
1962 struct namedobj_instance *
1963 ipfw_get_table_objhash(struct ip_fw_chain *ch)
1964 {
1965
1966         return (CHAIN_TO_NI(ch));
1967 }
1968
1969 /*
1970  * Exports basic table info as name TLV.
1971  * Used inside dump_static_rules() to provide info
1972  * about all tables referenced by current ruleset.
1973  *
1974  * Returns 0 on success.
1975  */
1976 int
1977 ipfw_export_table_ntlv(struct ip_fw_chain *ch, uint16_t kidx,
1978     struct sockopt_data *sd)
1979 {
1980         struct namedobj_instance *ni;
1981         struct named_object *no;
1982         ipfw_obj_ntlv *ntlv;
1983
1984         ni = CHAIN_TO_NI(ch);
1985
1986         no = ipfw_objhash_lookup_kidx(ni, kidx);
1987         KASSERT(no != NULL, ("invalid table kidx passed"));
1988
1989         ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
1990         if (ntlv == NULL)
1991                 return (ENOMEM);
1992
1993         ntlv->head.type = IPFW_TLV_TBL_NAME;
1994         ntlv->head.length = sizeof(*ntlv);
1995         ntlv->idx = no->kidx;
1996         strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
1997
1998         return (0);
1999 }
2000
2001 struct dump_args {
2002         struct ip_fw_chain *ch;
2003         struct table_info *ti;
2004         struct table_config *tc;
2005         struct sockopt_data *sd;
2006         uint32_t cnt;
2007         uint16_t uidx;
2008         int error;
2009         uint32_t size;
2010         ipfw_table_entry *ent;
2011         ta_foreach_f *f;
2012         void *farg;
2013         ipfw_obj_tentry tent;
2014 };
2015
2016 static int
2017 count_ext_entries(void *e, void *arg)
2018 {
2019         struct dump_args *da;
2020
2021         da = (struct dump_args *)arg;
2022         da->cnt++;
2023
2024         return (0);
2025 }
2026
2027 /*
2028  * Gets number of items from table either using
2029  * internal counter or calling algo callback for
2030  * externally-managed tables.
2031  *
2032  * Returns number of records.
2033  */
2034 static uint32_t
2035 table_get_count(struct ip_fw_chain *ch, struct table_config *tc)
2036 {
2037         struct table_info *ti;
2038         struct table_algo *ta;
2039         struct dump_args da;
2040
2041         ti = KIDX_TO_TI(ch, tc->no.kidx);
2042         ta = tc->ta;
2043
2044         /* Use internal counter for self-managed tables */
2045         if ((ta->flags & TA_FLAG_READONLY) == 0)
2046                 return (tc->count);
2047
2048         /* Use callback to quickly get number of items */
2049         if ((ta->flags & TA_FLAG_EXTCOUNTER) != 0)
2050                 return (ta->get_count(tc->astate, ti));
2051
2052         /* Count number of iterms ourselves */
2053         memset(&da, 0, sizeof(da));
2054         ta->foreach(tc->astate, ti, count_ext_entries, &da);
2055
2056         return (da.cnt);
2057 }
2058
2059 /*
2060  * Exports table @tc info into standard ipfw_xtable_info format.
2061  */
2062 static void
2063 export_table_info(struct ip_fw_chain *ch, struct table_config *tc,
2064     ipfw_xtable_info *i)
2065 {
2066         struct table_info *ti;
2067         struct table_algo *ta;
2068         
2069         i->type = tc->no.subtype;
2070         i->tflags = tc->tflags;
2071         i->vmask = tc->vmask;
2072         i->set = tc->no.set;
2073         i->kidx = tc->no.kidx;
2074         i->refcnt = tc->no.refcnt;
2075         i->count = table_get_count(ch, tc);
2076         i->limit = tc->limit;
2077         i->flags |= (tc->locked != 0) ? IPFW_TGFLAGS_LOCKED : 0;
2078         i->size = i->count * sizeof(ipfw_obj_tentry);
2079         i->size += sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2080         strlcpy(i->tablename, tc->tablename, sizeof(i->tablename));
2081         ti = KIDX_TO_TI(ch, tc->no.kidx);
2082         ta = tc->ta;
2083         if (ta->print_config != NULL) {
2084                 /* Use algo function to print table config to string */
2085                 ta->print_config(tc->astate, ti, i->algoname,
2086                     sizeof(i->algoname));
2087         } else
2088                 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2089         /* Dump algo-specific data, if possible */
2090         if (ta->dump_tinfo != NULL) {
2091                 ta->dump_tinfo(tc->astate, ti, &i->ta_info);
2092                 i->ta_info.flags |= IPFW_TATFLAGS_DATA;
2093         }
2094 }
2095
2096 struct dump_table_args {
2097         struct ip_fw_chain *ch;
2098         struct sockopt_data *sd;
2099 };
2100
2101 static int
2102 export_table_internal(struct namedobj_instance *ni, struct named_object *no,
2103     void *arg)
2104 {
2105         ipfw_xtable_info *i;
2106         struct dump_table_args *dta;
2107
2108         dta = (struct dump_table_args *)arg;
2109
2110         i = (ipfw_xtable_info *)ipfw_get_sopt_space(dta->sd, sizeof(*i));
2111         KASSERT(i != NULL, ("previously checked buffer is not enough"));
2112
2113         export_table_info(dta->ch, (struct table_config *)no, i);
2114         return (0);
2115 }
2116
2117 /*
2118  * Export all tables as ipfw_xtable_info structures to
2119  * storage provided by @sd.
2120  *
2121  * If supplied buffer is too small, fills in required size
2122  * and returns ENOMEM.
2123  * Returns 0 on success.
2124  */
2125 static int
2126 export_tables(struct ip_fw_chain *ch, ipfw_obj_lheader *olh,
2127     struct sockopt_data *sd)
2128 {
2129         uint32_t size;
2130         uint32_t count;
2131         struct dump_table_args dta;
2132
2133         count = ipfw_objhash_count(CHAIN_TO_NI(ch));
2134         size = count * sizeof(ipfw_xtable_info) + sizeof(ipfw_obj_lheader);
2135
2136         /* Fill in header regadless of buffer size */
2137         olh->count = count;
2138         olh->objsize = sizeof(ipfw_xtable_info);
2139
2140         if (size > olh->size) {
2141                 olh->size = size;
2142                 return (ENOMEM);
2143         }
2144
2145         olh->size = size;
2146
2147         dta.ch = ch;
2148         dta.sd = sd;
2149
2150         ipfw_objhash_foreach(CHAIN_TO_NI(ch), export_table_internal, &dta);
2151
2152         return (0);
2153 }
2154
2155 /*
2156  * Dumps all table data
2157  * Data layout (v1)(current):
2158  * Request: [ ipfw_obj_header ], size = ipfw_xtable_info.size
2159  * Reply: [ ipfw_obj_header ipfw_xtable_info ipfw_obj_tentry x N ]
2160  *
2161  * Returns 0 on success
2162  */
2163 static int
2164 dump_table_v1(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2165     struct sockopt_data *sd)
2166 {
2167         struct _ipfw_obj_header *oh;
2168         ipfw_xtable_info *i;
2169         struct tid_info ti;
2170         struct table_config *tc;
2171         struct table_algo *ta;
2172         struct dump_args da;
2173         uint32_t sz;
2174
2175         sz = sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info);
2176         oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
2177         if (oh == NULL)
2178                 return (EINVAL);
2179
2180         i = (ipfw_xtable_info *)(oh + 1);
2181         objheader_to_ti(oh, &ti);
2182
2183         IPFW_UH_RLOCK(ch);
2184         if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2185                 IPFW_UH_RUNLOCK(ch);
2186                 return (ESRCH);
2187         }
2188         export_table_info(ch, tc, i);
2189
2190         if (sd->valsize < i->size) {
2191
2192                 /*
2193                  * Submitted buffer size is not enough.
2194                  * WE've already filled in @i structure with
2195                  * relevant table info including size, so we
2196                  * can return. Buffer will be flushed automatically.
2197                  */
2198                 IPFW_UH_RUNLOCK(ch);
2199                 return (ENOMEM);
2200         }
2201
2202         /*
2203          * Do the actual dump in eXtended format
2204          */
2205         memset(&da, 0, sizeof(da));
2206         da.ch = ch;
2207         da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2208         da.tc = tc;
2209         da.sd = sd;
2210
2211         ta = tc->ta;
2212
2213         ta->foreach(tc->astate, da.ti, dump_table_tentry, &da);
2214         IPFW_UH_RUNLOCK(ch);
2215
2216         return (da.error);
2217 }
2218
2219 /*
2220  * Dumps all table data
2221  * Data layout (version 0)(legacy):
2222  * Request: [ ipfw_xtable ], size = IP_FW_TABLE_XGETSIZE()
2223  * Reply: [ ipfw_xtable ipfw_table_xentry x N ]
2224  *
2225  * Returns 0 on success
2226  */
2227 static int
2228 dump_table_v0(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2229     struct sockopt_data *sd)
2230 {
2231         ipfw_xtable *xtbl;
2232         struct tid_info ti;
2233         struct table_config *tc;
2234         struct table_algo *ta;
2235         struct dump_args da;
2236         size_t sz, count;
2237
2238         xtbl = (ipfw_xtable *)ipfw_get_sopt_header(sd, sizeof(ipfw_xtable));
2239         if (xtbl == NULL)
2240                 return (EINVAL);
2241
2242         memset(&ti, 0, sizeof(ti));
2243         ti.uidx = xtbl->tbl;
2244         
2245         IPFW_UH_RLOCK(ch);
2246         if ((tc = find_table(CHAIN_TO_NI(ch), &ti)) == NULL) {
2247                 IPFW_UH_RUNLOCK(ch);
2248                 return (0);
2249         }
2250         count = table_get_count(ch, tc);
2251         sz = count * sizeof(ipfw_table_xentry) + sizeof(ipfw_xtable);
2252
2253         xtbl->cnt = count;
2254         xtbl->size = sz;
2255         xtbl->type = tc->no.subtype;
2256         xtbl->tbl = ti.uidx;
2257
2258         if (sd->valsize < sz) {
2259
2260                 /*
2261                  * Submitted buffer size is not enough.
2262                  * WE've already filled in @i structure with
2263                  * relevant table info including size, so we
2264                  * can return. Buffer will be flushed automatically.
2265                  */
2266                 IPFW_UH_RUNLOCK(ch);
2267                 return (ENOMEM);
2268         }
2269
2270         /* Do the actual dump in eXtended format */
2271         memset(&da, 0, sizeof(da));
2272         da.ch = ch;
2273         da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2274         da.tc = tc;
2275         da.sd = sd;
2276
2277         ta = tc->ta;
2278
2279         ta->foreach(tc->astate, da.ti, dump_table_xentry, &da);
2280         IPFW_UH_RUNLOCK(ch);
2281
2282         return (0);
2283 }
2284
2285 /*
2286  * Legacy function to retrieve number of items in table.
2287  */
2288 static int
2289 get_table_size(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2290     struct sockopt_data *sd)
2291 {
2292         uint32_t *tbl;
2293         struct tid_info ti;
2294         size_t sz;
2295         int error;
2296
2297         sz = sizeof(*op3) + sizeof(uint32_t);
2298         op3 = (ip_fw3_opheader *)ipfw_get_sopt_header(sd, sz);
2299         if (op3 == NULL)
2300                 return (EINVAL);
2301
2302         tbl = (uint32_t *)(op3 + 1);
2303         memset(&ti, 0, sizeof(ti));
2304         ti.uidx = *tbl;
2305         IPFW_UH_RLOCK(ch);
2306         error = ipfw_count_xtable(ch, &ti, tbl);
2307         IPFW_UH_RUNLOCK(ch);
2308         return (error);
2309 }
2310
2311 /*
2312  * Legacy IP_FW_TABLE_GETSIZE handler
2313  */
2314 int
2315 ipfw_count_table(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2316 {
2317         struct table_config *tc;
2318
2319         if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2320                 return (ESRCH);
2321         *cnt = table_get_count(ch, tc);
2322         return (0);
2323 }
2324
2325 /*
2326  * Legacy IP_FW_TABLE_XGETSIZE handler
2327  */
2328 int
2329 ipfw_count_xtable(struct ip_fw_chain *ch, struct tid_info *ti, uint32_t *cnt)
2330 {
2331         struct table_config *tc;
2332         uint32_t count;
2333
2334         if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL) {
2335                 *cnt = 0;
2336                 return (0); /* 'table all list' requires success */
2337         }
2338
2339         count = table_get_count(ch, tc);
2340         *cnt = count * sizeof(ipfw_table_xentry);
2341         if (count > 0)
2342                 *cnt += sizeof(ipfw_xtable);
2343         return (0);
2344 }
2345
2346 static int
2347 dump_table_entry(void *e, void *arg)
2348 {
2349         struct dump_args *da;
2350         struct table_config *tc;
2351         struct table_algo *ta;
2352         ipfw_table_entry *ent;
2353         struct table_value *pval;
2354         int error;
2355
2356         da = (struct dump_args *)arg;
2357
2358         tc = da->tc;
2359         ta = tc->ta;
2360
2361         /* Out of memory, returning */
2362         if (da->cnt == da->size)
2363                 return (1);
2364         ent = da->ent++;
2365         ent->tbl = da->uidx;
2366         da->cnt++;
2367
2368         error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2369         if (error != 0)
2370                 return (error);
2371
2372         ent->addr = da->tent.k.addr.s_addr;
2373         ent->masklen = da->tent.masklen;
2374         pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2375         ent->value = ipfw_export_table_value_legacy(pval);
2376
2377         return (0);
2378 }
2379
2380 /*
2381  * Dumps table in pre-8.1 legacy format.
2382  */
2383 int
2384 ipfw_dump_table_legacy(struct ip_fw_chain *ch, struct tid_info *ti,
2385     ipfw_table *tbl)
2386 {
2387         struct table_config *tc;
2388         struct table_algo *ta;
2389         struct dump_args da;
2390
2391         tbl->cnt = 0;
2392
2393         if ((tc = find_table(CHAIN_TO_NI(ch), ti)) == NULL)
2394                 return (0);     /* XXX: We should return ESRCH */
2395
2396         ta = tc->ta;
2397
2398         /* This dump format supports IPv4 only */
2399         if (tc->no.subtype != IPFW_TABLE_ADDR)
2400                 return (0);
2401
2402         memset(&da, 0, sizeof(da));
2403         da.ch = ch;
2404         da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2405         da.tc = tc;
2406         da.ent = &tbl->ent[0];
2407         da.size = tbl->size;
2408
2409         tbl->cnt = 0;
2410         ta->foreach(tc->astate, da.ti, dump_table_entry, &da);
2411         tbl->cnt = da.cnt;
2412
2413         return (0);
2414 }
2415
2416 /*
2417  * Dumps table entry in eXtended format (v1)(current).
2418  */
2419 static int
2420 dump_table_tentry(void *e, void *arg)
2421 {
2422         struct dump_args *da;
2423         struct table_config *tc;
2424         struct table_algo *ta;
2425         struct table_value *pval;
2426         ipfw_obj_tentry *tent;
2427         int error;
2428
2429         da = (struct dump_args *)arg;
2430
2431         tc = da->tc;
2432         ta = tc->ta;
2433
2434         tent = (ipfw_obj_tentry *)ipfw_get_sopt_space(da->sd, sizeof(*tent));
2435         /* Out of memory, returning */
2436         if (tent == NULL) {
2437                 da->error = ENOMEM;
2438                 return (1);
2439         }
2440         tent->head.length = sizeof(ipfw_obj_tentry);
2441         tent->idx = da->uidx;
2442
2443         error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2444         if (error != 0)
2445                 return (error);
2446
2447         pval = get_table_value(da->ch, da->tc, tent->v.kidx);
2448         ipfw_export_table_value_v1(pval, &tent->v.value);
2449
2450         return (0);
2451 }
2452
2453 /*
2454  * Dumps table entry in eXtended format (v0).
2455  */
2456 static int
2457 dump_table_xentry(void *e, void *arg)
2458 {
2459         struct dump_args *da;
2460         struct table_config *tc;
2461         struct table_algo *ta;
2462         ipfw_table_xentry *xent;
2463         ipfw_obj_tentry *tent;
2464         struct table_value *pval;
2465         int error;
2466
2467         da = (struct dump_args *)arg;
2468
2469         tc = da->tc;
2470         ta = tc->ta;
2471
2472         xent = (ipfw_table_xentry *)ipfw_get_sopt_space(da->sd, sizeof(*xent));
2473         /* Out of memory, returning */
2474         if (xent == NULL)
2475                 return (1);
2476         xent->len = sizeof(ipfw_table_xentry);
2477         xent->tbl = da->uidx;
2478
2479         memset(&da->tent, 0, sizeof(da->tent));
2480         tent = &da->tent;
2481         error = ta->dump_tentry(tc->astate, da->ti, e, tent);
2482         if (error != 0)
2483                 return (error);
2484
2485         /* Convert current format to previous one */
2486         xent->masklen = tent->masklen;
2487         pval = get_table_value(da->ch, da->tc, da->tent.v.kidx);
2488         xent->value = ipfw_export_table_value_legacy(pval);
2489         /* Apply some hacks */
2490         if (tc->no.subtype == IPFW_TABLE_ADDR && tent->subtype == AF_INET) {
2491                 xent->k.addr6.s6_addr32[3] = tent->k.addr.s_addr;
2492                 xent->flags = IPFW_TCF_INET;
2493         } else
2494                 memcpy(&xent->k, &tent->k, sizeof(xent->k));
2495
2496         return (0);
2497 }
2498
2499 /*
2500  * Helper function to export table algo data
2501  * to tentry format before calling user function.
2502  *
2503  * Returns 0 on success.
2504  */
2505 static int
2506 prepare_table_tentry(void *e, void *arg)
2507 {
2508         struct dump_args *da;
2509         struct table_config *tc;
2510         struct table_algo *ta;
2511         int error;
2512
2513         da = (struct dump_args *)arg;
2514
2515         tc = da->tc;
2516         ta = tc->ta;
2517
2518         error = ta->dump_tentry(tc->astate, da->ti, e, &da->tent);
2519         if (error != 0)
2520                 return (error);
2521
2522         da->f(&da->tent, da->farg);
2523
2524         return (0);
2525 }
2526
2527 /*
2528  * Allow external consumers to read table entries in standard format.
2529  */
2530 int
2531 ipfw_foreach_table_tentry(struct ip_fw_chain *ch, uint16_t kidx,
2532     ta_foreach_f *f, void *arg)
2533 {
2534         struct namedobj_instance *ni;
2535         struct table_config *tc;
2536         struct table_algo *ta;
2537         struct dump_args da;
2538
2539         ni = CHAIN_TO_NI(ch);
2540
2541         tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, kidx);
2542         if (tc == NULL)
2543                 return (ESRCH);
2544
2545         ta = tc->ta;
2546
2547         memset(&da, 0, sizeof(da));
2548         da.ch = ch;
2549         da.ti = KIDX_TO_TI(ch, tc->no.kidx);
2550         da.tc = tc;
2551         da.f = f;
2552         da.farg = arg;
2553
2554         ta->foreach(tc->astate, da.ti, prepare_table_tentry, &da);
2555
2556         return (0);
2557 }
2558
2559 /*
2560  * Table algorithms
2561  */ 
2562
2563 /*
2564  * Finds algorithm by index, table type or supplied name.
2565  *
2566  * Returns pointer to algo or NULL.
2567  */
2568 static struct table_algo *
2569 find_table_algo(struct tables_config *tcfg, struct tid_info *ti, char *name)
2570 {
2571         int i, l;
2572         struct table_algo *ta;
2573
2574         if (ti->type > IPFW_TABLE_MAXTYPE)
2575                 return (NULL);
2576
2577         /* Search by index */
2578         if (ti->atype != 0) {
2579                 if (ti->atype > tcfg->algo_count)
2580                         return (NULL);
2581                 return (tcfg->algo[ti->atype]);
2582         }
2583
2584         if (name == NULL) {
2585                 /* Return default algorithm for given type if set */
2586                 return (tcfg->def_algo[ti->type]);
2587         }
2588
2589         /* Search by name */
2590         /* TODO: better search */
2591         for (i = 1; i <= tcfg->algo_count; i++) {
2592                 ta = tcfg->algo[i];
2593
2594                 /*
2595                  * One can supply additional algorithm
2596                  * parameters so we compare only the first word
2597                  * of supplied name:
2598                  * 'addr:chash hsize=32'
2599                  * '^^^^^^^^^'
2600                  *
2601                  */
2602                 l = strlen(ta->name);
2603                 if (strncmp(name, ta->name, l) != 0)
2604                         continue;
2605                 if (name[l] != '\0' && name[l] != ' ')
2606                         continue;
2607                 /* Check if we're requesting proper table type */
2608                 if (ti->type != 0 && ti->type != ta->type)
2609                         return (NULL);
2610                 return (ta);
2611         }
2612
2613         return (NULL);
2614 }
2615
2616 /*
2617  * Register new table algo @ta.
2618  * Stores algo id inside @idx.
2619  *
2620  * Returns 0 on success.
2621  */
2622 int
2623 ipfw_add_table_algo(struct ip_fw_chain *ch, struct table_algo *ta, size_t size,
2624     int *idx)
2625 {
2626         struct tables_config *tcfg;
2627         struct table_algo *ta_new;
2628         size_t sz;
2629
2630         if (size > sizeof(struct table_algo))
2631                 return (EINVAL);
2632
2633         /* Check for the required on-stack size for add/del */
2634         sz = roundup2(ta->ta_buf_size, sizeof(void *));
2635         if (sz > TA_BUF_SZ)
2636                 return (EINVAL);
2637
2638         KASSERT(ta->type <= IPFW_TABLE_MAXTYPE,("Increase IPFW_TABLE_MAXTYPE"));
2639
2640         /* Copy algorithm data to stable storage. */
2641         ta_new = malloc(sizeof(struct table_algo), M_IPFW, M_WAITOK | M_ZERO);
2642         memcpy(ta_new, ta, size);
2643
2644         tcfg = CHAIN_TO_TCFG(ch);
2645
2646         KASSERT(tcfg->algo_count < 255, ("Increase algo array size"));
2647
2648         tcfg->algo[++tcfg->algo_count] = ta_new;
2649         ta_new->idx = tcfg->algo_count;
2650
2651         /* Set algorithm as default one for given type */
2652         if ((ta_new->flags & TA_FLAG_DEFAULT) != 0 &&
2653             tcfg->def_algo[ta_new->type] == NULL)
2654                 tcfg->def_algo[ta_new->type] = ta_new;
2655
2656         *idx = ta_new->idx;
2657         
2658         return (0);
2659 }
2660
2661 /*
2662  * Unregisters table algo using @idx as id.
2663  * XXX: It is NOT safe to call this function in any place
2664  * other than ipfw instance destroy handler.
2665  */
2666 void
2667 ipfw_del_table_algo(struct ip_fw_chain *ch, int idx)
2668 {
2669         struct tables_config *tcfg;
2670         struct table_algo *ta;
2671
2672         tcfg = CHAIN_TO_TCFG(ch);
2673
2674         KASSERT(idx <= tcfg->algo_count, ("algo idx %d out of range 1..%d",
2675             idx, tcfg->algo_count));
2676
2677         ta = tcfg->algo[idx];
2678         KASSERT(ta != NULL, ("algo idx %d is NULL", idx));
2679
2680         if (tcfg->def_algo[ta->type] == ta)
2681                 tcfg->def_algo[ta->type] = NULL;
2682
2683         free(ta, M_IPFW);
2684 }
2685
2686 /*
2687  * Lists all table algorithms currently available.
2688  * Data layout (v0)(current):
2689  * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
2690  * Reply: [ ipfw_obj_lheader ipfw_ta_info x N ]
2691  *
2692  * Returns 0 on success
2693  */
2694 static int
2695 list_table_algo(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
2696     struct sockopt_data *sd)
2697 {
2698         struct _ipfw_obj_lheader *olh;
2699         struct tables_config *tcfg;
2700         ipfw_ta_info *i;
2701         struct table_algo *ta;
2702         uint32_t count, n, size;
2703
2704         olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
2705         if (olh == NULL)
2706                 return (EINVAL);
2707         if (sd->valsize < olh->size)
2708                 return (EINVAL);
2709
2710         IPFW_UH_RLOCK(ch);
2711         tcfg = CHAIN_TO_TCFG(ch);
2712         count = tcfg->algo_count;
2713         size = count * sizeof(ipfw_ta_info) + sizeof(ipfw_obj_lheader);
2714
2715         /* Fill in header regadless of buffer size */
2716         olh->count = count;
2717         olh->objsize = sizeof(ipfw_ta_info);
2718
2719         if (size > olh->size) {
2720                 olh->size = size;
2721                 IPFW_UH_RUNLOCK(ch);
2722                 return (ENOMEM);
2723         }
2724         olh->size = size;
2725
2726         for (n = 1; n <= count; n++) {
2727                 i = (ipfw_ta_info *)ipfw_get_sopt_space(sd, sizeof(*i));
2728                 KASSERT(i != NULL, ("previously checked buffer is not enough"));
2729                 ta = tcfg->algo[n];
2730                 strlcpy(i->algoname, ta->name, sizeof(i->algoname));
2731                 i->type = ta->type;
2732                 i->refcnt = ta->refcnt;
2733         }
2734
2735         IPFW_UH_RUNLOCK(ch);
2736
2737         return (0);
2738 }
2739
2740 static int
2741 classify_srcdst(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2742 {
2743         /* Basic IPv4/IPv6 or u32 lookups */
2744         *puidx = cmd->arg1;
2745         /* Assume ADDR by default */
2746         *ptype = IPFW_TABLE_ADDR;
2747         int v;
2748                 
2749         if (F_LEN(cmd) > F_INSN_SIZE(ipfw_insn_u32)) {
2750                 /*
2751                  * generic lookup. The key must be
2752                  * in 32bit big-endian format.
2753                  */
2754                 v = ((ipfw_insn_u32 *)cmd)->d[1];
2755                 switch (v) {
2756                 case 0:
2757                 case 1:
2758                         /* IPv4 src/dst */
2759                         break;
2760                 case 2:
2761                 case 3:
2762                         /* src/dst port */
2763                         *ptype = IPFW_TABLE_NUMBER;
2764                         break;
2765                 case 4:
2766                         /* uid/gid */
2767                         *ptype = IPFW_TABLE_NUMBER;
2768                         break;
2769                 case 5:
2770                         /* jid */
2771                         *ptype = IPFW_TABLE_NUMBER;
2772                         break;
2773                 case 6:
2774                         /* dscp */
2775                         *ptype = IPFW_TABLE_NUMBER;
2776                         break;
2777                 }
2778         }
2779
2780         return (0);
2781 }
2782
2783 static int
2784 classify_via(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2785 {
2786         ipfw_insn_if *cmdif;
2787
2788         /* Interface table, possibly */
2789         cmdif = (ipfw_insn_if *)cmd;
2790         if (cmdif->name[0] != '\1')
2791                 return (1);
2792
2793         *ptype = IPFW_TABLE_INTERFACE;
2794         *puidx = cmdif->p.kidx;
2795
2796         return (0);
2797 }
2798
2799 static int
2800 classify_flow(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
2801 {
2802
2803         *puidx = cmd->arg1;
2804         *ptype = IPFW_TABLE_FLOW;
2805
2806         return (0);
2807 }
2808
2809 static void
2810 update_arg1(ipfw_insn *cmd, uint16_t idx)
2811 {
2812
2813         cmd->arg1 = idx;
2814 }
2815
2816 static void
2817 update_via(ipfw_insn *cmd, uint16_t idx)
2818 {
2819         ipfw_insn_if *cmdif;
2820
2821         cmdif = (ipfw_insn_if *)cmd;
2822         cmdif->p.kidx = idx;
2823 }
2824
2825 static int
2826 table_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
2827     struct named_object **pno)
2828 {
2829         struct table_config *tc;
2830         int error;
2831
2832         IPFW_UH_WLOCK_ASSERT(ch);
2833
2834         error = find_table_err(CHAIN_TO_NI(ch), ti, &tc);
2835         if (error != 0)
2836                 return (error);
2837
2838         *pno = &tc->no;
2839         return (0);
2840 }
2841
2842 /* XXX: sets-sets! */
2843 static struct named_object *
2844 table_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
2845 {
2846         struct namedobj_instance *ni;
2847         struct table_config *tc;
2848
2849         IPFW_UH_WLOCK_ASSERT(ch);
2850         ni = CHAIN_TO_NI(ch);
2851         tc = (struct table_config *)ipfw_objhash_lookup_kidx(ni, idx);
2852         KASSERT(tc != NULL, ("Table with index %d not found", idx));
2853
2854         return (&tc->no);
2855 }
2856
2857 static int
2858 table_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2859     enum ipfw_sets_cmd cmd)
2860 {
2861
2862         switch (cmd) {
2863         case SWAP_ALL:
2864         case TEST_ALL:
2865         case MOVE_ALL:
2866                 /*
2867                  * Always return success, the real action and decision
2868                  * should make table_manage_sets_all().
2869                  */
2870                 return (0);
2871         case TEST_ONE:
2872         case MOVE_ONE:
2873                 /*
2874                  * NOTE: we need to use ipfw_objhash_del/ipfw_objhash_add
2875                  * if set number will be used in hash function. Currently
2876                  * we can just use generic handler that replaces set value.
2877                  */
2878                 if (V_fw_tables_sets == 0)
2879                         return (0);
2880                 break;
2881         case COUNT_ONE:
2882                 /*
2883                  * Return EOPNOTSUPP for COUNT_ONE when per-set sysctl is
2884                  * disabled. This allow skip table's opcodes from additional
2885                  * checks when specific rules moved to another set.
2886                  */
2887                 if (V_fw_tables_sets == 0)
2888                         return (EOPNOTSUPP);
2889         }
2890         /* Use generic sets handler when per-set sysctl is enabled. */
2891         return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2892             set, new_set, cmd));
2893 }
2894
2895 /*
2896  * We register several opcode rewriters for lookup tables.
2897  * All tables opcodes have the same ETLV type, but different subtype.
2898  * To avoid invoking sets handler several times for XXX_ALL commands,
2899  * we use separate manage_sets handler. O_RECV has the lowest value,
2900  * so it should be called first.
2901  */
2902 static int
2903 table_manage_sets_all(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
2904     enum ipfw_sets_cmd cmd)
2905 {
2906
2907         switch (cmd) {
2908         case SWAP_ALL:
2909         case TEST_ALL:
2910                 /*
2911                  * Return success for TEST_ALL, since nothing prevents
2912                  * move rules from one set to another. All tables are
2913                  * accessible from all sets when per-set tables sysctl
2914                  * is disabled.
2915                  */
2916         case MOVE_ALL:
2917                 if (V_fw_tables_sets == 0)
2918                         return (0);
2919                 break;
2920         default:
2921                 return (table_manage_sets(ch, set, new_set, cmd));
2922         }
2923         /* Use generic sets handler when per-set sysctl is enabled. */
2924         return (ipfw_obj_manage_sets(CHAIN_TO_NI(ch), IPFW_TLV_TBL_NAME,
2925             set, new_set, cmd));
2926 }
2927
2928 static struct opcode_obj_rewrite opcodes[] = {
2929         {
2930                 .opcode = O_IP_SRC_LOOKUP,
2931                 .etlv = IPFW_TLV_TBL_NAME,
2932                 .classifier = classify_srcdst,
2933                 .update = update_arg1,
2934                 .find_byname = table_findbyname,
2935                 .find_bykidx = table_findbykidx,
2936                 .create_object = create_table_compat,
2937                 .manage_sets = table_manage_sets,
2938         },
2939         {
2940                 .opcode = O_IP_DST_LOOKUP,
2941                 .etlv = IPFW_TLV_TBL_NAME,
2942                 .classifier = classify_srcdst,
2943                 .update = update_arg1,
2944                 .find_byname = table_findbyname,
2945                 .find_bykidx = table_findbykidx,
2946                 .create_object = create_table_compat,
2947                 .manage_sets = table_manage_sets,
2948         },
2949         {
2950                 .opcode = O_IP_FLOW_LOOKUP,
2951                 .etlv = IPFW_TLV_TBL_NAME,
2952                 .classifier = classify_flow,
2953                 .update = update_arg1,
2954                 .find_byname = table_findbyname,
2955                 .find_bykidx = table_findbykidx,
2956                 .create_object = create_table_compat,
2957                 .manage_sets = table_manage_sets,
2958         },
2959         {
2960                 .opcode = O_XMIT,
2961                 .etlv = IPFW_TLV_TBL_NAME,
2962                 .classifier = classify_via,
2963                 .update = update_via,
2964                 .find_byname = table_findbyname,
2965                 .find_bykidx = table_findbykidx,
2966                 .create_object = create_table_compat,
2967                 .manage_sets = table_manage_sets,
2968         },
2969         {
2970                 .opcode = O_RECV,
2971                 .etlv = IPFW_TLV_TBL_NAME,
2972                 .classifier = classify_via,
2973                 .update = update_via,
2974                 .find_byname = table_findbyname,
2975                 .find_bykidx = table_findbykidx,
2976                 .create_object = create_table_compat,
2977                 .manage_sets = table_manage_sets_all,
2978         },
2979         {
2980                 .opcode = O_VIA,
2981                 .etlv = IPFW_TLV_TBL_NAME,
2982                 .classifier = classify_via,
2983                 .update = update_via,
2984                 .find_byname = table_findbyname,
2985                 .find_bykidx = table_findbykidx,
2986                 .create_object = create_table_compat,
2987                 .manage_sets = table_manage_sets,
2988         },
2989 };
2990
2991 static int
2992 test_sets_cb(struct namedobj_instance *ni __unused, struct named_object *no,
2993     void *arg __unused)
2994 {
2995
2996         /* Check that there aren't any tables in not default set */
2997         if (no->set != 0)
2998                 return (EBUSY);
2999         return (0);
3000 }
3001
3002 /*
3003  * Switch between "set 0" and "rule's set" table binding,
3004  * Check all ruleset bindings and permits changing
3005  * IFF each binding has both rule AND table in default set (set 0).
3006  *
3007  * Returns 0 on success.
3008  */
3009 int
3010 ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int sets)
3011 {
3012         struct opcode_obj_rewrite *rw;
3013         struct namedobj_instance *ni;
3014         struct named_object *no;
3015         struct ip_fw *rule;
3016         ipfw_insn *cmd;
3017         int cmdlen, i, l;
3018         uint16_t kidx;
3019         uint8_t subtype;
3020
3021         IPFW_UH_WLOCK(ch);
3022
3023         if (V_fw_tables_sets == sets) {
3024                 IPFW_UH_WUNLOCK(ch);
3025                 return (0);
3026         }
3027         ni = CHAIN_TO_NI(ch);
3028         if (sets == 0) {
3029                 /*
3030                  * Prevent disabling sets support if we have some tables
3031                  * in not default sets.
3032                  */
3033                 if (ipfw_objhash_foreach_type(ni, test_sets_cb,
3034                     NULL, IPFW_TLV_TBL_NAME) != 0) {
3035                         IPFW_UH_WUNLOCK(ch);
3036                         return (EBUSY);
3037                 }
3038         }
3039         /*
3040          * Scan all rules and examine tables opcodes.
3041          */
3042         for (i = 0; i < ch->n_rules; i++) {
3043                 rule = ch->map[i];
3044
3045                 l = rule->cmd_len;
3046                 cmd = rule->cmd;
3047                 cmdlen = 0;
3048                 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
3049                         cmdlen = F_LEN(cmd);
3050                         /* Check only tables opcodes */
3051                         for (kidx = 0, rw = opcodes;
3052                             rw < opcodes + nitems(opcodes); rw++) {
3053                                 if (rw->opcode != cmd->opcode)
3054                                         continue;
3055                                 if (rw->classifier(cmd, &kidx, &subtype) == 0)
3056                                         break;
3057                         }
3058                         if (kidx == 0)
3059                                 continue;
3060                         no = ipfw_objhash_lookup_kidx(ni, kidx);
3061                         /* Check if both table object and rule has the set 0 */
3062                         if (no->set != 0 || rule->set != 0) {
3063                                 IPFW_UH_WUNLOCK(ch);
3064                                 return (EBUSY);
3065                         }
3066
3067                 }
3068         }
3069         V_fw_tables_sets = sets;
3070         IPFW_UH_WUNLOCK(ch);
3071         return (0);
3072 }
3073
3074 /*
3075  * Checks table name for validity.
3076  * Enforce basic length checks, the rest
3077  * should be done in userland.
3078  *
3079  * Returns 0 if name is considered valid.
3080  */
3081 static int
3082 check_table_name(const char *name)
3083 {
3084
3085         /*
3086          * TODO: do some more complicated checks
3087          */
3088         return (ipfw_check_object_name_generic(name));
3089 }
3090
3091 /*
3092  * Finds table config based on either legacy index
3093  * or name in ntlv.
3094  * Note @ti structure contains unchecked data from userland.
3095  *
3096  * Returns 0 in success and fills in @tc with found config
3097  */
3098 static int
3099 find_table_err(struct namedobj_instance *ni, struct tid_info *ti,
3100     struct table_config **tc)
3101 {
3102         char *name, bname[16];
3103         struct named_object *no;
3104         ipfw_obj_ntlv *ntlv;
3105         uint32_t set;
3106
3107         if (ti->tlvs != NULL) {
3108                 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3109                     IPFW_TLV_TBL_NAME);
3110                 if (ntlv == NULL)
3111                         return (EINVAL);
3112                 name = ntlv->name;
3113
3114                 /*
3115                  * Use set provided by @ti instead of @ntlv one.
3116                  * This is needed due to different sets behavior
3117                  * controlled by V_fw_tables_sets.
3118                  */
3119                 set = (V_fw_tables_sets != 0) ? ti->set : 0;
3120         } else {
3121                 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3122                 name = bname;
3123                 set = 0;
3124         }
3125
3126         no = ipfw_objhash_lookup_name(ni, set, name);
3127         *tc = (struct table_config *)no;
3128
3129         return (0);
3130 }
3131
3132 /*
3133  * Finds table config based on either legacy index
3134  * or name in ntlv.
3135  * Note @ti structure contains unchecked data from userland.
3136  *
3137  * Returns pointer to table_config or NULL.
3138  */
3139 static struct table_config *
3140 find_table(struct namedobj_instance *ni, struct tid_info *ti)
3141 {
3142         struct table_config *tc;
3143
3144         if (find_table_err(ni, ti, &tc) != 0)
3145                 return (NULL);
3146
3147         return (tc);
3148 }
3149
3150 /*
3151  * Allocate new table config structure using
3152  * specified @algo and @aname.
3153  *
3154  * Returns pointer to config or NULL.
3155  */
3156 static struct table_config *
3157 alloc_table_config(struct ip_fw_chain *ch, struct tid_info *ti,
3158     struct table_algo *ta, char *aname, uint8_t tflags)
3159 {
3160         char *name, bname[16];
3161         struct table_config *tc;
3162         int error;
3163         ipfw_obj_ntlv *ntlv;
3164         uint32_t set;
3165
3166         if (ti->tlvs != NULL) {
3167                 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
3168                     IPFW_TLV_TBL_NAME);
3169                 if (ntlv == NULL)
3170                         return (NULL);
3171                 name = ntlv->name;
3172                 set = ntlv->set;
3173         } else {
3174                 /* Compat part: convert number to string representation */
3175                 snprintf(bname, sizeof(bname), "%d", ti->uidx);
3176                 name = bname;
3177                 set = 0;
3178         }
3179
3180         tc = malloc(sizeof(struct table_config), M_IPFW, M_WAITOK | M_ZERO);
3181         tc->no.name = tc->tablename;
3182         tc->no.subtype = ta->type;
3183         tc->no.set = set;
3184         tc->tflags = tflags;
3185         tc->ta = ta;
3186         strlcpy(tc->tablename, name, sizeof(tc->tablename));
3187         /* Set "shared" value type by default */
3188         tc->vshared = 1;
3189
3190         /* Preallocate data structures for new tables */
3191         error = ta->init(ch, &tc->astate, &tc->ti_copy, aname, tflags);
3192         if (error != 0) {
3193                 free(tc, M_IPFW);
3194                 return (NULL);
3195         }
3196         
3197         return (tc);
3198 }
3199
3200 /*
3201  * Destroys table state and config.
3202  */
3203 static void
3204 free_table_config(struct namedobj_instance *ni, struct table_config *tc)
3205 {
3206
3207         KASSERT(tc->linked == 0, ("free() on linked config"));
3208         /* UH lock MUST NOT be held */
3209
3210         /*
3211          * We're using ta without any locking/referencing.
3212          * TODO: fix this if we're going to use unloadable algos.
3213          */
3214         tc->ta->destroy(tc->astate, &tc->ti_copy);
3215         free(tc, M_IPFW);
3216 }
3217
3218 /*
3219  * Links @tc to @chain table named instance.
3220  * Sets appropriate type/states in @chain table info.
3221  */
3222 static void
3223 link_table(struct ip_fw_chain *ch, struct table_config *tc)
3224 {
3225         struct namedobj_instance *ni;
3226         struct table_info *ti;
3227         uint16_t kidx;
3228
3229         IPFW_UH_WLOCK_ASSERT(ch);
3230
3231         ni = CHAIN_TO_NI(ch);
3232         kidx = tc->no.kidx;
3233
3234         ipfw_objhash_add(ni, &tc->no);
3235
3236         ti = KIDX_TO_TI(ch, kidx);
3237         *ti = tc->ti_copy;
3238
3239         /* Notify algo on real @ti address */
3240         if (tc->ta->change_ti != NULL)
3241                 tc->ta->change_ti(tc->astate, ti);
3242
3243         tc->linked = 1;
3244         tc->ta->refcnt++;
3245 }
3246
3247 /*
3248  * Unlinks @tc from @chain table named instance.
3249  * Zeroes states in @chain and stores them in @tc.
3250  */
3251 static void
3252 unlink_table(struct ip_fw_chain *ch, struct table_config *tc)
3253 {
3254         struct namedobj_instance *ni;
3255         struct table_info *ti;
3256         uint16_t kidx;
3257
3258         IPFW_UH_WLOCK_ASSERT(ch);
3259         IPFW_WLOCK_ASSERT(ch);
3260
3261         ni = CHAIN_TO_NI(ch);
3262         kidx = tc->no.kidx;
3263
3264         /* Clear state. @ti copy is already saved inside @tc */
3265         ipfw_objhash_del(ni, &tc->no);
3266         ti = KIDX_TO_TI(ch, kidx);
3267         memset(ti, 0, sizeof(struct table_info));
3268         tc->linked = 0;
3269         tc->ta->refcnt--;
3270
3271         /* Notify algo on real @ti address */
3272         if (tc->ta->change_ti != NULL)
3273                 tc->ta->change_ti(tc->astate, NULL);
3274 }
3275
3276 static struct ipfw_sopt_handler scodes[] = {
3277         { IP_FW_TABLE_XCREATE,  0,      HDIR_SET,       create_table },
3278         { IP_FW_TABLE_XDESTROY, 0,      HDIR_SET,       flush_table_v0 },
3279         { IP_FW_TABLE_XFLUSH,   0,      HDIR_SET,       flush_table_v0 },
3280         { IP_FW_TABLE_XMODIFY,  0,      HDIR_BOTH,      modify_table },
3281         { IP_FW_TABLE_XINFO,    0,      HDIR_GET,       describe_table },
3282         { IP_FW_TABLES_XLIST,   0,      HDIR_GET,       list_tables },
3283         { IP_FW_TABLE_XLIST,    0,      HDIR_GET,       dump_table_v0 },
3284         { IP_FW_TABLE_XLIST,    1,      HDIR_GET,       dump_table_v1 },
3285         { IP_FW_TABLE_XADD,     0,      HDIR_BOTH,      manage_table_ent_v0 },
3286         { IP_FW_TABLE_XADD,     1,      HDIR_BOTH,      manage_table_ent_v1 },
3287         { IP_FW_TABLE_XDEL,     0,      HDIR_BOTH,      manage_table_ent_v0 },
3288         { IP_FW_TABLE_XDEL,     1,      HDIR_BOTH,      manage_table_ent_v1 },
3289         { IP_FW_TABLE_XFIND,    0,      HDIR_GET,       find_table_entry },
3290         { IP_FW_TABLE_XSWAP,    0,      HDIR_SET,       swap_table },
3291         { IP_FW_TABLES_ALIST,   0,      HDIR_GET,       list_table_algo },
3292         { IP_FW_TABLE_XGETSIZE, 0,      HDIR_GET,       get_table_size },
3293 };
3294
3295 static int
3296 destroy_table_locked(struct namedobj_instance *ni, struct named_object *no,
3297     void *arg)
3298 {
3299
3300         unlink_table((struct ip_fw_chain *)arg, (struct table_config *)no);
3301         if (ipfw_objhash_free_idx(ni, no->kidx) != 0)
3302                 printf("Error unlinking kidx %d from table %s\n",
3303                     no->kidx, no->name);
3304         free_table_config(ni, (struct table_config *)no);
3305         return (0);
3306 }
3307
3308 /*
3309  * Shuts tables module down.
3310  */
3311 void
3312 ipfw_destroy_tables(struct ip_fw_chain *ch, int last)
3313 {
3314
3315         IPFW_DEL_SOPT_HANDLER(last, scodes);
3316         IPFW_DEL_OBJ_REWRITER(last, opcodes);
3317
3318         /* Remove all tables from working set */
3319         IPFW_UH_WLOCK(ch);
3320         IPFW_WLOCK(ch);
3321         ipfw_objhash_foreach(CHAIN_TO_NI(ch), destroy_table_locked, ch);
3322         IPFW_WUNLOCK(ch);
3323         IPFW_UH_WUNLOCK(ch);
3324
3325         /* Free pointers itself */
3326         free(ch->tablestate, M_IPFW);
3327
3328         ipfw_table_value_destroy(ch, last);
3329         ipfw_table_algo_destroy(ch);
3330
3331         ipfw_objhash_destroy(CHAIN_TO_NI(ch));
3332         free(CHAIN_TO_TCFG(ch), M_IPFW);
3333 }
3334
3335 /*
3336  * Starts tables module.
3337  */
3338 int
3339 ipfw_init_tables(struct ip_fw_chain *ch, int first)
3340 {
3341         struct tables_config *tcfg;
3342
3343         /* Allocate pointers */
3344         ch->tablestate = malloc(V_fw_tables_max * sizeof(struct table_info),
3345             M_IPFW, M_WAITOK | M_ZERO);
3346
3347         tcfg = malloc(sizeof(struct tables_config), M_IPFW, M_WAITOK | M_ZERO);
3348         tcfg->namehash = ipfw_objhash_create(V_fw_tables_max);
3349         ch->tblcfg = tcfg;
3350
3351         ipfw_table_value_init(ch, first);
3352         ipfw_table_algo_init(ch);
3353
3354         IPFW_ADD_OBJ_REWRITER(first, opcodes);
3355         IPFW_ADD_SOPT_HANDLER(first, scodes);
3356         return (0);
3357 }
3358
3359
3360