Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libebtc.c, January 2004 |
| 3 | * |
| 4 | * Contains the functions with which to make a table in userspace. |
| 5 | * |
| 6 | * Author: Bart De Schuymer |
| 7 | * |
| 8 | * This code is stongly inspired on the iptables code which is |
| 9 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <getopt.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <netinet/ether.h> |
| 32 | #include "include/ebtables_u.h" |
| 33 | #include "include/ethernetdb.h" |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <sys/wait.h> |
| 37 | |
| 38 | static void decrease_chain_jumps(struct ebt_u_replace *replace); |
| 39 | static void remove_udc(struct ebt_u_replace *replace); |
| 40 | static int iterate_entries(struct ebt_u_replace *replace, int type); |
| 41 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 42 | /* The standard names */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 43 | const char *ebt_hooknames[NF_BR_NUMHOOKS] = |
| 44 | { |
| 45 | [NF_BR_PRE_ROUTING]"PREROUTING", |
| 46 | [NF_BR_LOCAL_IN]"INPUT", |
| 47 | [NF_BR_FORWARD]"FORWARD", |
| 48 | [NF_BR_LOCAL_OUT]"OUTPUT", |
| 49 | [NF_BR_POST_ROUTING]"POSTROUTING", |
| 50 | [NF_BR_BROUTING]"BROUTING" |
| 51 | }; |
| 52 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 53 | /* The four target names */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 54 | const char* ebt_standard_targets[NUM_STANDARD_TARGETS] = |
| 55 | { |
| 56 | "ACCEPT", |
| 57 | "DROP", |
| 58 | "CONTINUE", |
| 59 | "RETURN", |
| 60 | }; |
| 61 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 62 | /* The lists of supported tables, matches, watchers and targets */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 63 | struct ebt_u_table *ebt_tables; |
| 64 | struct ebt_u_match *ebt_matches; |
| 65 | struct ebt_u_watcher *ebt_watchers; |
| 66 | struct ebt_u_target *ebt_targets; |
| 67 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 68 | /* Find the right structure belonging to a name */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 69 | struct ebt_u_target *ebt_find_target(const char *name) |
| 70 | { |
| 71 | struct ebt_u_target *t = ebt_targets; |
| 72 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 73 | while (t && strcmp(t->name, name)) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 74 | t = t->next; |
| 75 | return t; |
| 76 | } |
| 77 | |
| 78 | struct ebt_u_match *ebt_find_match(const char *name) |
| 79 | { |
| 80 | struct ebt_u_match *m = ebt_matches; |
| 81 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 82 | while (m && strcmp(m->name, name)) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 83 | m = m->next; |
| 84 | return m; |
| 85 | } |
| 86 | |
| 87 | struct ebt_u_watcher *ebt_find_watcher(const char *name) |
| 88 | { |
| 89 | struct ebt_u_watcher *w = ebt_watchers; |
| 90 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 91 | while (w && strcmp(w->name, name)) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 92 | w = w->next; |
| 93 | return w; |
| 94 | } |
| 95 | |
| 96 | struct ebt_u_table *ebt_find_table(const char *name) |
| 97 | { |
| 98 | struct ebt_u_table *t = ebt_tables; |
| 99 | |
| 100 | while (t && strcmp(t->name, name)) |
| 101 | t = t->next; |
| 102 | return t; |
| 103 | } |
| 104 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 105 | /* Prints all registered extensions */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 106 | void ebt_list_extensions() |
| 107 | { |
| 108 | struct ebt_u_table *tbl = ebt_tables; |
| 109 | struct ebt_u_target *t = ebt_targets; |
| 110 | struct ebt_u_match *m = ebt_matches; |
| 111 | struct ebt_u_watcher *w = ebt_watchers; |
| 112 | |
| 113 | PRINT_VERSION; |
| 114 | printf("Supported userspace extensions:\n\nSupported tables:\n"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 115 | while (tbl) { |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 116 | printf("%s\n", tbl->name); |
| 117 | tbl = tbl->next; |
| 118 | } |
| 119 | printf("\nSupported targets:\n"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 120 | while (t) { |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 121 | printf("%s\n", t->name); |
| 122 | t = t->next; |
| 123 | } |
| 124 | printf("\nSupported matches:\n"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 125 | while (m) { |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 126 | printf("%s\n", m->name); |
| 127 | m = m->next; |
| 128 | } |
| 129 | printf("\nSupported watchers:\n"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 130 | while (w) { |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 131 | printf("%s\n", w->name); |
| 132 | w = w->next; |
| 133 | } |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 136 | /* Get the table from the kernel or from a binary file |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 137 | * init: 1 = ask the kernel for the initial contents of a table, i.e. the |
| 138 | * way it looks when the table is insmod'ed |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 139 | * 0 = get the current data in the table */ |
| 140 | int ebt_get_kernel_table(struct ebt_u_replace *replace, int init) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 141 | { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 142 | if (!ebt_find_table(replace->name)) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 143 | ebt_print_error("Bad table name"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 144 | return -1; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 145 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 146 | /* Get the kernel's information */ |
| 147 | if (ebt_get_table(replace, init)) { |
| 148 | if (ebt_errormsg[0] != '\0') |
| 149 | return -1; |
| 150 | ebtables_insmod("ebtables"); |
| 151 | if (ebt_get_table(replace, init)) { |
| 152 | ebt_print_error("The kernel doesn't support the ebtables %s table", replace->name); |
| 153 | return -1; |
| 154 | } |
| 155 | } |
| 156 | return 0; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 159 | /* Put sane values into a new entry */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 160 | void ebt_initialize_entry(struct ebt_u_entry *e) |
| 161 | { |
| 162 | e->bitmask = EBT_NOPROTO; |
| 163 | e->invflags = 0; |
| 164 | e->ethproto = 0; |
| 165 | strcpy(e->in, ""); |
| 166 | strcpy(e->out, ""); |
| 167 | strcpy(e->logical_in, ""); |
| 168 | strcpy(e->logical_out, ""); |
| 169 | e->m_list = NULL; |
| 170 | e->w_list = NULL; |
| 171 | e->t = (struct ebt_entry_target *)ebt_find_target(EBT_STANDARD_TARGET); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 172 | ebt_find_target(EBT_STANDARD_TARGET)->used = 1; |
Bart De Schuymer | ab611e2 | 2005-02-14 20:20:03 +0000 | [diff] [blame] | 173 | e->cnt.pcnt = e->cnt.bcnt = e->cnt_surplus.pcnt = e->cnt_surplus.bcnt = 0; |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 174 | |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 175 | if (!e->t) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 176 | ebt_print_bug("Couldn't load standard target"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 177 | ((struct ebt_standard_target *)((struct ebt_u_target *)e->t)->t)->verdict = EBT_CONTINUE; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 180 | /* Free up the memory of the table held in userspace, *replace can be reused */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 181 | void ebt_cleanup_replace(struct ebt_u_replace *replace) |
| 182 | { |
| 183 | int i; |
| 184 | struct ebt_u_entries *entries; |
| 185 | struct ebt_u_chain_list *udc1, *udc2; |
| 186 | struct ebt_cntchanges *cc1, *cc2; |
| 187 | struct ebt_u_entry *u_e1, *u_e2; |
| 188 | |
| 189 | replace->name[0] = '\0'; |
| 190 | replace->valid_hooks = 0; |
| 191 | replace->nentries = 0; |
| 192 | replace->num_counters = 0; |
| 193 | replace->flags = 0; |
| 194 | replace->command = 0; |
| 195 | replace->selected_chain = -1; |
| 196 | if (replace->filename) { |
| 197 | free(replace->filename); |
| 198 | replace->filename = NULL; |
| 199 | } |
| 200 | if (replace->counters) { |
| 201 | free(replace->counters); |
| 202 | replace->counters = NULL; |
| 203 | } |
| 204 | |
| 205 | i = -1; |
| 206 | while (1) { |
| 207 | i++; |
| 208 | entries = ebt_nr_to_chain(replace, i); |
| 209 | if (!entries) { |
| 210 | if (i < NF_BR_NUMHOOKS) |
| 211 | continue; |
| 212 | else |
| 213 | break; |
| 214 | } |
| 215 | entries->nentries = 0; |
| 216 | entries->counter_offset = 0; |
| 217 | u_e1 = entries->entries; |
| 218 | entries->entries = NULL; |
| 219 | while (u_e1) { |
| 220 | ebt_free_u_entry(u_e1); |
| 221 | u_e2 = u_e1->next; |
| 222 | free(u_e1); |
| 223 | u_e1 = u_e2; |
| 224 | } |
| 225 | } |
| 226 | udc1 = replace->udc; |
| 227 | while (udc1) { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 228 | free(udc1->udc); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 229 | udc2 = udc1->next; |
| 230 | free(udc1); |
| 231 | udc1 = udc2; |
| 232 | } |
| 233 | replace->udc = NULL; |
| 234 | cc1 = replace->counterchanges; |
| 235 | while (cc1) { |
| 236 | cc2 = cc1->next; |
| 237 | free(cc2); |
| 238 | cc1 = cc2; |
| 239 | } |
| 240 | replace->counterchanges = NULL; |
| 241 | } |
| 242 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 243 | /* Should be called, e.g., between 2 rule adds */ |
| 244 | void ebt_reinit_extensions() |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 245 | { |
| 246 | struct ebt_u_match *m; |
| 247 | struct ebt_u_watcher *w; |
| 248 | struct ebt_u_target *t; |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 249 | int size; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 250 | |
| 251 | /* The init functions should determine by themselves whether they are |
| 252 | * called for the first time or not (when necessary). */ |
| 253 | for (m = ebt_matches; m; m = m->next) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 254 | size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 255 | if (m->used) { |
| 256 | m->m = (struct ebt_entry_match *)malloc(size); |
| 257 | if (!m->m) |
| 258 | ebt_print_memory(); |
| 259 | strcpy(m->m->u.name, m->name); |
| 260 | m->m->match_size = EBT_ALIGN(m->size); |
| 261 | m->used = 0; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 262 | } |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 263 | m->flags = 0; /* An error can occur before used is set, while flags is changed. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 264 | m->init(m->m); |
| 265 | } |
| 266 | for (w = ebt_watchers; w; w = w->next) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 267 | size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 268 | if (w->used) { |
| 269 | w->w = (struct ebt_entry_watcher *)malloc(size); |
| 270 | if (!w->w) |
| 271 | ebt_print_memory(); |
| 272 | strcpy(w->w->u.name, w->name); |
| 273 | w->w->watcher_size = EBT_ALIGN(w->size); |
| 274 | w->used = 0; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 275 | } |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 276 | w->flags = 0; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 277 | w->init(w->w); |
| 278 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 279 | for (t = ebt_targets; t; t = t->next) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 280 | size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 281 | if (t->used) { |
| 282 | t->t = (struct ebt_entry_target *)malloc(size); |
| 283 | if (!t->t) |
| 284 | ebt_print_memory(); |
| 285 | strcpy(t->t->u.name, t->name); |
| 286 | t->t->target_size = EBT_ALIGN(t->size); |
| 287 | t->used = 0; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 288 | } |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 289 | t->flags = 0; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 290 | t->init(t->t); |
| 291 | } |
| 292 | } |
| 293 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 294 | /* This doesn't free e, because the calling function might need e->next */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 295 | void ebt_free_u_entry(struct ebt_u_entry *e) |
| 296 | { |
| 297 | struct ebt_u_match_list *m_l, *m_l2; |
| 298 | struct ebt_u_watcher_list *w_l, *w_l2; |
| 299 | |
| 300 | m_l = e->m_list; |
| 301 | while (m_l) { |
| 302 | m_l2 = m_l->next; |
| 303 | free(m_l->m); |
| 304 | free(m_l); |
| 305 | m_l = m_l2; |
| 306 | } |
| 307 | w_l = e->w_list; |
| 308 | while (w_l) { |
| 309 | w_l2 = w_l->next; |
| 310 | free(w_l->w); |
| 311 | free(w_l); |
| 312 | w_l = w_l2; |
| 313 | } |
| 314 | free(e->t); |
| 315 | } |
| 316 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 317 | /* Blatently stolen (again) from iptables.c userspace program |
| 318 | * find out where the modprobe utility is located */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 319 | static char *get_modprobe(void) |
| 320 | { |
| 321 | int procfile; |
| 322 | char *ret; |
| 323 | |
| 324 | procfile = open(PROC_SYS_MODPROBE, O_RDONLY); |
| 325 | if (procfile < 0) |
| 326 | return NULL; |
| 327 | |
| 328 | ret = malloc(1024); |
| 329 | if (ret) { |
| 330 | switch (read(procfile, ret, 1024)) { |
| 331 | case -1: goto fail; |
| 332 | case 1024: goto fail; /* Partial read. Wierd */ |
| 333 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 334 | if (ret[strlen(ret)-1] == '\n') |
| 335 | ret[strlen(ret)-1] = 0; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 336 | close(procfile); |
| 337 | return ret; |
| 338 | } |
| 339 | fail: |
| 340 | free(ret); |
| 341 | close(procfile); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | char *ebt_modprobe; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 346 | /* Try to load the kernel module */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 347 | int ebtables_insmod(const char *modname) |
| 348 | { |
| 349 | char *buf = NULL; |
| 350 | char *argv[3]; |
| 351 | |
| 352 | /* If they don't explicitly set it, read out of kernel */ |
| 353 | if (!ebt_modprobe) { |
| 354 | buf = get_modprobe(); |
| 355 | if (!buf) |
| 356 | return -1; |
| 357 | ebt_modprobe = buf; |
| 358 | } |
| 359 | |
| 360 | switch (fork()) { |
| 361 | case 0: |
| 362 | argv[0] = (char *)ebt_modprobe; |
| 363 | argv[1] = (char *)modname; |
| 364 | argv[2] = NULL; |
| 365 | execv(argv[0], argv); |
| 366 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 367 | /* Not usually reached */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 368 | exit(0); |
| 369 | case -1: |
| 370 | return -1; |
| 371 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 372 | default: /* Parent */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 373 | wait(NULL); |
| 374 | } |
| 375 | |
| 376 | free(buf); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 380 | /* Gives back a pointer to the chain base, based on nr. |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 381 | * If nr >= NF_BR_NUMHOOKS you'll get back a user-defined chain. |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 382 | * Returns NULL on failure. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 383 | struct ebt_u_entries *ebt_nr_to_chain(const struct ebt_u_replace *replace, |
| 384 | int nr) |
| 385 | { |
| 386 | if (nr == -1) |
| 387 | return NULL; |
| 388 | if (nr < NF_BR_NUMHOOKS) |
| 389 | return replace->hook_entry[nr]; |
| 390 | else { |
| 391 | int i; |
| 392 | struct ebt_u_chain_list *cl = replace->udc; |
| 393 | |
| 394 | i = nr - NF_BR_NUMHOOKS; |
| 395 | while (i > 0 && cl) { |
| 396 | cl = cl->next; |
| 397 | i--; |
| 398 | } |
| 399 | if (cl) |
| 400 | return cl->udc; |
| 401 | else |
| 402 | return NULL; |
| 403 | } |
| 404 | } |
| 405 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 406 | /* Gives back a pointer to the chain base of selected_chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 407 | struct ebt_u_entries *ebt_to_chain(const struct ebt_u_replace *replace) |
| 408 | { |
| 409 | return ebt_nr_to_chain(replace, replace->selected_chain); |
| 410 | } |
| 411 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 412 | /* Parse the chain name and return a pointer to the chain base. |
| 413 | * Returns NULL on failure. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 414 | struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace, |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 415 | const char* arg) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 416 | { |
| 417 | int i; |
| 418 | struct ebt_u_chain_list *cl = replace->udc; |
| 419 | |
| 420 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 421 | if (!(replace->valid_hooks & (1 << i))) |
| 422 | continue; |
| 423 | if (!strcmp(arg, replace->hook_entry[i]->name)) |
| 424 | return replace->hook_entry[i]; |
| 425 | } |
| 426 | while(cl) { |
| 427 | if (!strcmp(arg, cl->udc->name)) |
| 428 | return cl->udc; |
| 429 | cl = cl->next; |
| 430 | } |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 434 | /* Parse the chain name and return the corresponding chain nr |
| 435 | * returns -1 on failure */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 436 | int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg) |
| 437 | { |
| 438 | int i; |
| 439 | struct ebt_u_chain_list *cl = replace->udc; |
| 440 | |
| 441 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 442 | if (!(replace->valid_hooks & (1 << i))) |
| 443 | continue; |
| 444 | if (!strcmp(arg, replace->hook_entry[i]->name)) |
| 445 | return i; |
| 446 | } |
| 447 | while(cl) { |
| 448 | if (!strcmp(arg, cl->udc->name)) |
| 449 | return i; |
| 450 | i++; |
| 451 | cl = cl->next; |
| 452 | } |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | ************ |
| 458 | ************ |
| 459 | **COMMANDS** |
| 460 | ************ |
| 461 | ************ |
| 462 | */ |
| 463 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 464 | /* Change the policy of selected_chain. |
| 465 | * Handing a bad policy to this function is a bug. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 466 | void ebt_change_policy(struct ebt_u_replace *replace, int policy) |
| 467 | { |
| 468 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 469 | |
| 470 | if (policy < -NUM_STANDARD_TARGETS || policy == EBT_CONTINUE) |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 471 | ebt_print_bug("Wrong policy: %d", policy); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 472 | entries->policy = policy; |
| 473 | } |
| 474 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 475 | /* Flush one chain or the complete table |
| 476 | * If selected_chain == -1: flush the complete table */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 477 | void ebt_flush_chains(struct ebt_u_replace *replace) |
| 478 | { |
| 479 | int i, j, numdel; |
| 480 | struct ebt_u_entry *u_e, *tmp; |
| 481 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 482 | struct ebt_cntchanges *cc = replace->counterchanges; |
| 483 | struct ebt_cntchanges **prev_cc = &(replace->counterchanges); |
| 484 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 485 | /* Flush whole table */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 486 | if (!entries) { |
| 487 | if (replace->nentries == 0) |
| 488 | return; |
| 489 | replace->nentries = 0; |
| 490 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 491 | /* Free everything and zero (n)entries */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 492 | i = -1; |
| 493 | while (1) { |
| 494 | i++; |
| 495 | entries = ebt_nr_to_chain(replace, i); |
| 496 | if (!entries) { |
| 497 | if (i < NF_BR_NUMHOOKS) |
| 498 | continue; |
| 499 | else |
| 500 | break; |
| 501 | } |
| 502 | entries->nentries = 0; |
| 503 | entries->counter_offset = 0; |
| 504 | u_e = entries->entries; |
| 505 | entries->entries = NULL; |
| 506 | while (u_e) { |
| 507 | ebt_free_u_entry(u_e); |
| 508 | tmp = u_e->next; |
| 509 | free(u_e); |
| 510 | u_e = tmp; |
| 511 | } |
| 512 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 513 | /* Update the counters */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 514 | while (cc) { |
| 515 | if (cc->type == CNT_ADD) { |
| 516 | *prev_cc = cc->next; |
| 517 | free(cc); |
| 518 | cc = *prev_cc; |
| 519 | continue; |
| 520 | } |
| 521 | cc->type = CNT_DEL; |
| 522 | prev_cc = &(cc->next); |
| 523 | cc = cc->next; |
| 524 | } |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | if (entries->nentries == 0) |
| 529 | return; |
| 530 | replace->nentries -= entries->nentries; |
| 531 | numdel = entries->nentries; |
| 532 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 533 | /* Delete the counters belonging to the specified chain, |
| 534 | * update counter_offset */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 535 | i = -1; |
| 536 | while (1) { |
| 537 | i++; |
| 538 | entries = ebt_nr_to_chain(replace, i); |
| 539 | if (!entries) { |
| 540 | if (i < NF_BR_NUMHOOKS) |
| 541 | continue; |
| 542 | else |
| 543 | break; |
| 544 | } |
| 545 | if (i > replace->selected_chain) { |
| 546 | entries->counter_offset -= numdel; |
| 547 | continue; |
| 548 | } |
| 549 | j = entries->nentries; |
| 550 | while (j) { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 551 | /* Don't count deleted entries */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 552 | if (cc->type == CNT_DEL) |
| 553 | goto letscontinue; |
| 554 | if (i == replace->selected_chain) { |
| 555 | if (cc->type == CNT_ADD) { |
| 556 | *prev_cc = cc->next; |
| 557 | free(cc); |
| 558 | cc = *prev_cc; |
| 559 | j--; |
| 560 | continue; |
| 561 | } |
| 562 | cc->type = CNT_DEL; |
| 563 | } |
| 564 | j--; |
| 565 | letscontinue: |
| 566 | prev_cc = &(cc->next); |
| 567 | cc = cc->next; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | entries = ebt_to_chain(replace); |
| 572 | entries->nentries = 0; |
| 573 | u_e = entries->entries; |
| 574 | while (u_e) { |
| 575 | ebt_free_u_entry(u_e); |
| 576 | tmp = u_e->next; |
| 577 | free(u_e); |
| 578 | u_e = tmp; |
| 579 | } |
| 580 | entries->entries = NULL; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Bart De Schuymer | ab611e2 | 2005-02-14 20:20:03 +0000 | [diff] [blame] | 583 | #define OPT_COUNT 0x1000 /* This value is also defined in ebtables.c */ |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 584 | /* Returns the rule number on success (starting from 0), -1 on failure |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 585 | * |
| 586 | * This function expects the ebt_{match,watcher,target} members of new_entry |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 587 | * to contain pointers to ebt_u_{match,watcher,target} */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 588 | int ebt_check_rule_exists(struct ebt_u_replace *replace, |
| 589 | struct ebt_u_entry *new_entry) |
| 590 | { |
| 591 | struct ebt_u_entry *u_e; |
| 592 | struct ebt_u_match_list *m_l, *m_l2; |
| 593 | struct ebt_u_match *m; |
| 594 | struct ebt_u_watcher_list *w_l, *w_l2; |
| 595 | struct ebt_u_watcher *w; |
| 596 | struct ebt_u_target *t = (struct ebt_u_target *)new_entry->t; |
| 597 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 598 | int i, j, k; |
| 599 | |
| 600 | u_e = entries->entries; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 601 | /* Check for an existing rule (if there are duplicate rules, |
| 602 | * take the first occurance) */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 603 | for (i = 0; i < entries->nentries; i++, u_e = u_e->next) { |
| 604 | if (!u_e) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 605 | ebt_print_bug("Hmm, trouble"); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 606 | if (u_e->ethproto != new_entry->ethproto) |
| 607 | continue; |
| 608 | if (strcmp(u_e->in, new_entry->in)) |
| 609 | continue; |
| 610 | if (strcmp(u_e->out, new_entry->out)) |
| 611 | continue; |
| 612 | if (strcmp(u_e->logical_in, new_entry->logical_in)) |
| 613 | continue; |
| 614 | if (strcmp(u_e->logical_out, new_entry->logical_out)) |
| 615 | continue; |
| 616 | if (new_entry->bitmask & EBT_SOURCEMAC && |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 617 | memcmp(u_e->sourcemac, new_entry->sourcemac, ETH_ALEN)) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 618 | continue; |
| 619 | if (new_entry->bitmask & EBT_DESTMAC && |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 620 | memcmp(u_e->destmac, new_entry->destmac, ETH_ALEN)) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 621 | continue; |
| 622 | if (new_entry->bitmask != u_e->bitmask || |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 623 | new_entry->invflags != u_e->invflags) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 624 | continue; |
Bart De Schuymer | ab611e2 | 2005-02-14 20:20:03 +0000 | [diff] [blame] | 625 | if (replace->flags & OPT_COUNT && (new_entry->cnt.pcnt != |
| 626 | u_e->cnt.pcnt || new_entry->cnt.bcnt != u_e->cnt.bcnt)) |
| 627 | continue; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 628 | /* Compare all matches */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 629 | m_l = new_entry->m_list; |
| 630 | j = 0; |
| 631 | while (m_l) { |
| 632 | m = (struct ebt_u_match *)(m_l->m); |
| 633 | m_l2 = u_e->m_list; |
| 634 | while (m_l2 && strcmp(m_l2->m->u.name, m->m->u.name)) |
| 635 | m_l2 = m_l2->next; |
| 636 | if (!m_l2 || !m->compare(m->m, m_l2->m)) |
| 637 | goto letscontinue; |
| 638 | j++; |
| 639 | m_l = m_l->next; |
| 640 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 641 | /* Now be sure they have the same nr of matches */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 642 | k = 0; |
| 643 | m_l = u_e->m_list; |
| 644 | while (m_l) { |
| 645 | k++; |
| 646 | m_l = m_l->next; |
| 647 | } |
| 648 | if (j != k) |
| 649 | continue; |
| 650 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 651 | /* Compare all watchers */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 652 | w_l = new_entry->w_list; |
| 653 | j = 0; |
| 654 | while (w_l) { |
| 655 | w = (struct ebt_u_watcher *)(w_l->w); |
| 656 | w_l2 = u_e->w_list; |
| 657 | while (w_l2 && strcmp(w_l2->w->u.name, w->w->u.name)) |
| 658 | w_l2 = w_l2->next; |
| 659 | if (!w_l2 || !w->compare(w->w, w_l2->w)) |
| 660 | goto letscontinue; |
| 661 | j++; |
| 662 | w_l = w_l->next; |
| 663 | } |
| 664 | k = 0; |
| 665 | w_l = u_e->w_list; |
| 666 | while (w_l) { |
| 667 | k++; |
| 668 | w_l = w_l->next; |
| 669 | } |
| 670 | if (j != k) |
| 671 | continue; |
| 672 | if (strcmp(t->t->u.name, u_e->t->u.name)) |
| 673 | continue; |
| 674 | if (!t->compare(t->t, u_e->t)) |
| 675 | continue; |
| 676 | return i; |
Bart De Schuymer | aef0894 | 2004-09-09 21:41:29 +0000 | [diff] [blame] | 677 | letscontinue:; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 678 | } |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | /* Add a rule, rule_nr is the rule to update |
| 683 | * rule_nr specifies where the rule should be inserted |
| 684 | * rule_nr > 0 : insert the rule right before the rule_nr'th rule |
| 685 | * (the first rule is rule 1) |
| 686 | * rule_nr < 0 : insert the rule right before the (n+rule_nr+1)'th rule, |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 687 | * where n denotes the number of rules in the chain |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 688 | * rule_nr == 0: add a new rule at the end of the chain |
| 689 | * |
| 690 | * This function expects the ebt_{match,watcher,target} members of new_entry |
| 691 | * to contain pointers to ebt_u_{match,watcher,target} and updates these |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 692 | * pointers so that they point to ebt_{match,watcher,target}, before adding |
| 693 | * the rule to the chain. Don't free() the ebt_{match,watcher,target} after a |
| 694 | * successful call to ebt_add_rule() */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 695 | void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry, |
| 696 | int rule_nr) |
| 697 | { |
| 698 | int i, j; |
| 699 | struct ebt_u_entry **u_e; |
| 700 | struct ebt_u_match_list *m_l; |
| 701 | struct ebt_u_watcher_list *w_l; |
| 702 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 703 | struct ebt_cntchanges *cc = replace->counterchanges, *new_cc; |
| 704 | struct ebt_cntchanges **prev_cc = &(replace->counterchanges); |
| 705 | |
| 706 | if (rule_nr <= 0) |
| 707 | rule_nr += entries->nentries; |
| 708 | else |
| 709 | rule_nr--; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 710 | if (rule_nr > entries->nentries || rule_nr < 0) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 711 | ebt_print_error("The specified rule number is incorrect"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 712 | return; |
| 713 | } |
| 714 | /* We're adding one rule */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 715 | replace->nentries++; |
| 716 | entries->nentries++; |
| 717 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 718 | /* Handle counter stuff */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 719 | for (i = 0; i < replace->selected_chain; i++) { |
| 720 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
| 721 | continue; |
| 722 | j = ebt_nr_to_chain(replace, i)->nentries; |
| 723 | while (j) { |
| 724 | if (cc->type != CNT_DEL) |
| 725 | j--; |
| 726 | prev_cc = &(cc->next); |
| 727 | cc = cc->next; |
| 728 | } |
| 729 | } |
| 730 | j = rule_nr; |
| 731 | while (j) { |
| 732 | if (cc->type != CNT_DEL) |
| 733 | j--; |
| 734 | prev_cc = &(cc->next); |
| 735 | cc = cc->next; |
| 736 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 737 | if (cc && cc->type == CNT_DEL) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 738 | cc->type = CNT_OWRITE; |
| 739 | else { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 740 | new_cc = (struct ebt_cntchanges *) |
| 741 | malloc(sizeof(struct ebt_cntchanges)); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 742 | if (!new_cc) |
| 743 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 744 | new_cc->type = CNT_ADD; |
| 745 | new_cc->next = cc; |
| 746 | *prev_cc = new_cc; |
| 747 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 748 | /* Go to the right position in the chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 749 | u_e = &entries->entries; |
| 750 | for (i = 0; i < rule_nr; i++) |
| 751 | u_e = &(*u_e)->next; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 752 | /* Insert the rule */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 753 | new_entry->next = *u_e; |
| 754 | *u_e = new_entry; |
| 755 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 756 | /* Put the ebt_{match, watcher, target} pointers in place */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 757 | m_l = new_entry->m_list; |
| 758 | while (m_l) { |
| 759 | m_l->m = ((struct ebt_u_match *)m_l->m)->m; |
| 760 | m_l = m_l->next; |
| 761 | } |
| 762 | w_l = new_entry->w_list; |
| 763 | while (w_l) { |
| 764 | w_l->w = ((struct ebt_u_watcher *)w_l->w)->w; |
| 765 | w_l = w_l->next; |
| 766 | } |
| 767 | new_entry->t = ((struct ebt_u_target *)new_entry->t)->t; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 768 | /* Update the counter_offset of chains behind this one */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 769 | i = replace->selected_chain; |
| 770 | while (1) { |
| 771 | i++; |
| 772 | entries = ebt_nr_to_chain(replace, i); |
| 773 | if (!entries) { |
| 774 | if (i < NF_BR_NUMHOOKS) |
| 775 | continue; |
| 776 | else |
| 777 | break; |
| 778 | } else |
| 779 | entries->counter_offset++; |
| 780 | } |
| 781 | } |
| 782 | |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 783 | static int check_and_change_rule_number(struct ebt_u_replace *replace, |
| 784 | struct ebt_u_entry *new_entry, int *begin, int *end) |
| 785 | { |
| 786 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 787 | |
| 788 | if (*begin < 0) |
| 789 | *begin += entries->nentries + 1; |
| 790 | if (*end < 0) |
| 791 | *end += entries->nentries + 1; |
| 792 | |
| 793 | if (*begin < 0 || *begin > *end || *end > entries->nentries) { |
| 794 | ebt_print_error("Sorry, wrong rule numbers"); |
| 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | if ((*begin * *end == 0) && (*begin + *end != 0)) |
| 799 | ebt_print_bug("begin and end should be either both zero, " |
| 800 | "either both non-zero"); |
| 801 | if (*begin != 0 && *end != 0) { |
| 802 | (*begin)--; |
| 803 | (*end)--; |
| 804 | } else { |
| 805 | *begin = ebt_check_rule_exists(replace, new_entry); |
| 806 | *end = *begin; |
| 807 | if (*begin == -1) { |
| 808 | ebt_print_error("Sorry, rule does not exist"); |
| 809 | return -1; |
| 810 | } |
| 811 | } |
| 812 | return 0; |
| 813 | } |
| 814 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 815 | /* Delete a rule or rules |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 816 | * begin == end == 0: delete the rule corresponding to new_entry |
| 817 | * |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 818 | * The first rule has rule nr 1, the last rule has rule nr -1, etc. |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 819 | * This function expects the ebt_{match,watcher,target} members of new_entry |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 820 | * to contain pointers to ebt_u_{match,watcher,target}. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 821 | void ebt_delete_rule(struct ebt_u_replace *replace, |
| 822 | struct ebt_u_entry *new_entry, int begin, int end) |
| 823 | { |
| 824 | int i, j, nr_deletes; |
| 825 | struct ebt_u_entry **u_e, *u_e2; |
| 826 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 827 | struct ebt_cntchanges *cc = replace->counterchanges; |
| 828 | struct ebt_cntchanges **prev_cc = &(replace->counterchanges); |
| 829 | |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 830 | if (check_and_change_rule_number(replace, new_entry, &begin, &end)) |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 831 | return; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 832 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 833 | /* We're deleting rules */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 834 | nr_deletes = end - begin + 1; |
| 835 | replace->nentries -= nr_deletes; |
| 836 | entries->nentries -= nr_deletes; |
| 837 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 838 | /* Handle counter stuff */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 839 | for (i = 0; i < replace->selected_chain; i++) { |
| 840 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
| 841 | continue; |
| 842 | j = ebt_nr_to_chain(replace, i)->nentries; |
| 843 | while (j) { |
| 844 | if (cc->type != CNT_DEL) |
| 845 | j--; |
| 846 | prev_cc = &(cc->next); |
| 847 | cc = cc->next; |
| 848 | } |
| 849 | } |
| 850 | j = begin; |
| 851 | while (j) { |
| 852 | if (cc->type != CNT_DEL) |
| 853 | j--; |
| 854 | prev_cc = &(cc->next); |
| 855 | cc = cc->next; |
| 856 | } |
| 857 | j = nr_deletes; |
| 858 | while (j) { |
| 859 | if (cc->type != CNT_DEL) { |
| 860 | j--; |
| 861 | if (cc->type == CNT_ADD) { |
| 862 | *prev_cc = cc->next; |
| 863 | free(cc); |
| 864 | cc = *prev_cc; |
| 865 | continue; |
| 866 | } |
| 867 | cc->type = CNT_DEL; |
| 868 | } |
| 869 | prev_cc = &(cc->next); |
| 870 | cc = cc->next; |
| 871 | } |
| 872 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 873 | /* Go to the right position in the chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 874 | u_e = &entries->entries; |
| 875 | for (j = 0; j < begin; j++) |
| 876 | u_e = &(*u_e)->next; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 877 | /* Remove the rules */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 878 | j = nr_deletes; |
| 879 | while(j--) { |
| 880 | u_e2 = *u_e; |
| 881 | *u_e = (*u_e)->next; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 882 | /* Free everything */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 883 | ebt_free_u_entry(u_e2); |
| 884 | free(u_e2); |
| 885 | } |
| 886 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 887 | /* Update the counter_offset of chains behind this one */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 888 | j = replace->selected_chain; |
| 889 | while (1) { |
| 890 | j++; |
| 891 | entries = ebt_nr_to_chain(replace, j); |
| 892 | if (!entries) { |
| 893 | if (j < NF_BR_NUMHOOKS) |
| 894 | continue; |
| 895 | else |
| 896 | break; |
| 897 | } else |
| 898 | entries->counter_offset -= nr_deletes; |
| 899 | } |
| 900 | } |
| 901 | |
Bart De Schuymer | ab611e2 | 2005-02-14 20:20:03 +0000 | [diff] [blame] | 902 | #define OPT_CNT_INCR 0x2000 /* This value is also defined in ebtables.c */ |
| 903 | #define OPT_CNT_DECR 0x4000 /* This value is also defined in ebtables.c */ |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 904 | /* Change the counters of a rule or rules |
| 905 | * begin == end == 0: change counters of the rule corresponding to new_entry |
| 906 | * |
| 907 | * The first rule has rule nr 1, the last rule has rule nr -1, etc. |
| 908 | * This function expects the ebt_{match,watcher,target} members of new_entry |
| 909 | * to contain pointers to ebt_u_{match,watcher,target}. */ |
| 910 | void ebt_change_counters(struct ebt_u_replace *replace, |
| 911 | struct ebt_u_entry *new_entry, int begin, int end, |
| 912 | struct ebt_counter *cnt) |
| 913 | { |
| 914 | int i, j; |
| 915 | struct ebt_u_entry *u_e; |
| 916 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 917 | struct ebt_cntchanges *cc = replace->counterchanges; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 918 | |
| 919 | if (check_and_change_rule_number(replace, new_entry, &begin, &end)) |
| 920 | return; |
| 921 | |
| 922 | for (i = 0; i < replace->selected_chain; i++) { |
| 923 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
| 924 | continue; |
| 925 | j = ebt_nr_to_chain(replace, i)->nentries; |
| 926 | while (j) { |
| 927 | if (cc->type != CNT_DEL) |
| 928 | j--; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 929 | cc = cc->next; |
| 930 | } |
| 931 | } |
| 932 | i = begin; |
| 933 | while (i) { |
| 934 | if (cc->type != CNT_DEL) |
| 935 | i--; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 936 | cc = cc->next; |
| 937 | } |
| 938 | u_e = entries->entries; |
| 939 | for (i = 0; i < begin; i++) |
| 940 | u_e = u_e->next; |
| 941 | i = end - begin + 1; |
Bart De Schuymer | ab611e2 | 2005-02-14 20:20:03 +0000 | [diff] [blame] | 942 | while (i) { |
| 943 | if (cc->type != CNT_DEL) { |
| 944 | i--; |
| 945 | if (cc->type != CNT_ADD && cc->type != CNT_OWRITE) { |
| 946 | /* In daemon mode, we want to make sure we don't lose |
| 947 | * any counted packets/bytes (the kernel is still counting |
| 948 | * while we are changing things in userspace). Therefore, |
| 949 | * we need to add the amount to the counters in the kernel, |
| 950 | * not the counters copied earlier from the kernel. */ |
| 951 | u_e->cnt_surplus = *cnt; |
| 952 | if (replace->flags & OPT_CNT_INCR) { |
| 953 | cc->type = CNT_INCR; |
| 954 | } else if (replace->flags & OPT_CNT_DECR) |
| 955 | cc->type = CNT_DECR; |
| 956 | else { |
| 957 | cc->type = CNT_CHANGE; |
| 958 | u_e->cnt = *cnt; |
| 959 | u_e->cnt_surplus.pcnt = u_e->cnt_surplus.bcnt = 0; |
| 960 | } |
| 961 | } else { /* A -C after a -A remains a -A */ |
| 962 | if (replace->flags & OPT_CNT_INCR) { |
| 963 | u_e->cnt.pcnt += cnt->pcnt; |
| 964 | u_e->cnt.bcnt += cnt->bcnt; |
| 965 | } else if (replace->flags & OPT_CNT_DECR) { |
| 966 | u_e->cnt.pcnt -= cnt->pcnt; |
| 967 | u_e->cnt.bcnt -= cnt->bcnt; |
| 968 | } else |
| 969 | u_e->cnt = *cnt; |
| 970 | } |
| 971 | u_e = u_e->next; |
| 972 | } |
| 973 | cc = cc->next; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 977 | /* Selected_chain == -1 : zero all counters |
| 978 | * Otherwise, zero the counters of selected_chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 979 | void ebt_zero_counters(struct ebt_u_replace *replace) |
| 980 | { |
| 981 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 982 | struct ebt_cntchanges *cc = replace->counterchanges; |
Bart De Schuymer | 93f36ba | 2005-01-24 21:11:24 +0000 | [diff] [blame] | 983 | struct ebt_u_entry *next; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 984 | int i, j; |
| 985 | |
| 986 | if (!entries) { |
| 987 | while (cc) { |
| 988 | if (cc->type == CNT_NORM) |
| 989 | cc->type = CNT_ZERO; |
| 990 | cc = cc->next; |
| 991 | } |
Bart De Schuymer | 93f36ba | 2005-01-24 21:11:24 +0000 | [diff] [blame] | 992 | i = -1; |
| 993 | while (1) { |
| 994 | i++; |
| 995 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
| 996 | continue; |
| 997 | entries = ebt_nr_to_chain(replace, i); |
| 998 | if (!entries) { |
| 999 | if (i < NF_BR_NUMHOOKS) |
| 1000 | ebt_print_bug("i < NF_BR_NUMHOOKS"); |
| 1001 | break; |
| 1002 | } |
| 1003 | next = entries->entries; |
| 1004 | while (next) { |
| 1005 | next->cnt.bcnt = next->cnt.pcnt = 0; |
| 1006 | next = next->next; |
| 1007 | } |
| 1008 | } |
| 1009 | |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1010 | } else { |
Bart De Schuymer | 93f36ba | 2005-01-24 21:11:24 +0000 | [diff] [blame] | 1011 | next = entries->entries; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1012 | if (entries->nentries == 0) |
| 1013 | return; |
| 1014 | |
| 1015 | for (i = 0; i < replace->selected_chain; i++) { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1016 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1017 | continue; |
| 1018 | j = ebt_nr_to_chain(replace, i)->nentries; |
| 1019 | while (j) { |
| 1020 | if (cc->type != CNT_DEL) |
| 1021 | j--; |
| 1022 | cc = cc->next; |
| 1023 | } |
| 1024 | } |
| 1025 | j = entries->nentries; |
| 1026 | while (j) { |
| 1027 | if (cc->type != CNT_DEL) { |
| 1028 | j--; |
| 1029 | if (cc->type == CNT_NORM) |
| 1030 | cc->type = CNT_ZERO; |
| 1031 | } |
| 1032 | cc = cc->next; |
| 1033 | } |
Bart De Schuymer | 93f36ba | 2005-01-24 21:11:24 +0000 | [diff] [blame] | 1034 | while (next) { |
| 1035 | next->cnt.bcnt = next->cnt.pcnt = 0; |
| 1036 | next = next->next; |
| 1037 | } |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1041 | /* Add a new chain and specify its policy */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1042 | void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy) |
| 1043 | { |
| 1044 | struct ebt_u_chain_list *cl, **cl2; |
| 1045 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1046 | if (ebt_get_chainnr(replace, name) != -1) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1047 | ebt_print_error("Chain %s already exists", optarg); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1048 | return; |
| 1049 | } else if (ebt_find_target(name)) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1050 | ebt_print_error("Target with name %s exists", optarg); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } else if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN) { |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1053 | ebt_print_error("Chain name length can't exceed %d", |
| 1054 | EBT_CHAIN_MAXNAMELEN - 1); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1055 | return; |
| 1056 | } |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1057 | cl = (struct ebt_u_chain_list *) |
| 1058 | malloc(sizeof(struct ebt_u_chain_list)); |
| 1059 | if (!cl) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1060 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1061 | cl->next = NULL; |
| 1062 | cl->udc = (struct ebt_u_entries *) |
| 1063 | malloc(sizeof(struct ebt_u_entries)); |
| 1064 | if (!cl->udc) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1065 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1066 | cl->udc->nentries = 0; |
| 1067 | cl->udc->policy = policy; |
| 1068 | cl->udc->counter_offset = replace->nentries; |
| 1069 | cl->udc->hook_mask = 0; |
| 1070 | strcpy(cl->udc->name, name); |
| 1071 | cl->udc->entries = NULL; |
| 1072 | cl->kernel_start = NULL; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1073 | /* Put the new chain at the end */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1074 | cl2 = &(replace->udc); |
| 1075 | while (*cl2) |
| 1076 | cl2 = &((*cl2)->next); |
| 1077 | *cl2 = cl; |
| 1078 | } |
| 1079 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1080 | /* Selected_chain == -1: delete all non-referenced udc |
| 1081 | * selected_chain < NF_BR_NUMHOOKS is illegal */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1082 | void ebt_delete_chain(struct ebt_u_replace *replace) |
| 1083 | { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1084 | int chain_nr = replace->selected_chain, print_error = 1; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1085 | |
| 1086 | if (chain_nr != -1 && chain_nr < NF_BR_NUMHOOKS) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1087 | ebt_print_bug("You can't remove a standard chain"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1088 | if (chain_nr == -1) { |
| 1089 | print_error = 0; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1090 | replace->selected_chain = NF_BR_NUMHOOKS; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1091 | } |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1092 | do { |
| 1093 | if (ebt_to_chain(replace) == NULL) { |
| 1094 | if (chain_nr == -1) |
| 1095 | break; |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1096 | ebt_print_bug("udc nr %d doesn't exist", chain_nr); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1097 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1098 | /* If the chain is referenced, don't delete it, |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1099 | * also decrement jumps to a chain behind the |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1100 | * one we're deleting */ |
| 1101 | if (ebt_check_for_references(replace, print_error)) { |
| 1102 | if (chain_nr != -1) |
| 1103 | break; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1104 | replace->selected_chain++; |
| 1105 | continue; |
| 1106 | } |
| 1107 | decrease_chain_jumps(replace); |
| 1108 | ebt_flush_chains(replace); |
| 1109 | remove_udc(replace); |
Bart De Schuymer | c6c0dc3 | 2004-12-16 19:30:32 +0000 | [diff] [blame] | 1110 | } while (chain_nr == -1); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1111 | replace->selected_chain = chain_nr; /* Put back to -1 */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1114 | /* Rename an existing chain. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1115 | void ebt_rename_chain(struct ebt_u_replace *replace, const char *name) |
| 1116 | { |
| 1117 | struct ebt_u_entries *entries = ebt_to_chain(replace); |
| 1118 | |
| 1119 | if (!entries) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1120 | ebt_print_bug("ebt_rename_chain: entries == NULL"); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1121 | strcpy(entries->name, name); |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | /* |
| 1126 | ************************* |
| 1127 | ************************* |
| 1128 | **SPECIALIZED*FUNCTIONS** |
| 1129 | ************************* |
| 1130 | ************************* |
| 1131 | */ |
| 1132 | |
| 1133 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1134 | /* Executes the final_check() function for all extensions used by the rule |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1135 | * ebt_check_for_loops should have been executed earlier, to make sure the |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1136 | * hook_mask is correct. The time argument to final_check() is set to 1, |
| 1137 | * meaning it's the second time the final_check() function is executed. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1138 | void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e, |
| 1139 | struct ebt_u_entries *entries) |
| 1140 | { |
| 1141 | struct ebt_u_match_list *m_l; |
| 1142 | struct ebt_u_watcher_list *w_l; |
| 1143 | struct ebt_u_target *t; |
| 1144 | struct ebt_u_match *m; |
| 1145 | struct ebt_u_watcher *w; |
| 1146 | |
| 1147 | m_l = e->m_list; |
| 1148 | w_l = e->w_list; |
| 1149 | while (m_l) { |
| 1150 | m = ebt_find_match(m_l->m->u.name); |
| 1151 | m->final_check(e, m_l->m, replace->name, |
| 1152 | entries->hook_mask, 1); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1153 | if (ebt_errormsg[0] != '\0') |
| 1154 | return; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1155 | m_l = m_l->next; |
| 1156 | } |
| 1157 | while (w_l) { |
| 1158 | w = ebt_find_watcher(w_l->w->u.name); |
| 1159 | w->final_check(e, w_l->w, replace->name, |
| 1160 | entries->hook_mask, 1); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1161 | if (ebt_errormsg[0] != '\0') |
| 1162 | return; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1163 | w_l = w_l->next; |
| 1164 | } |
| 1165 | t = ebt_find_target(e->t->u.name); |
| 1166 | t->final_check(e, e->t, replace->name, |
| 1167 | entries->hook_mask, 1); |
| 1168 | } |
| 1169 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1170 | /* Returns 1 (if it returns) when the chain is referenced, 0 when it isn't. |
| 1171 | * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */ |
| 1172 | int ebt_check_for_references(struct ebt_u_replace *replace, int print_err) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1173 | { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1174 | if (print_err) |
| 1175 | return iterate_entries(replace, 1); |
| 1176 | else |
| 1177 | return iterate_entries(replace, 2); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1180 | /* chain_nr: nr of the udc (>= NF_BR_NUMHOOKS) |
| 1181 | * Returns 1 (if it returns) when the chain is referenced, 0 when it isn't. |
| 1182 | * print_err: 0 (resp. 1) = don't (resp. do) print error when referenced */ |
| 1183 | int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr, |
| 1184 | int print_err) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1185 | { |
| 1186 | int tmp = replace->selected_chain, ret; |
| 1187 | |
| 1188 | replace->selected_chain = chain_nr; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1189 | if (print_err) |
| 1190 | ret = iterate_entries(replace, 1); |
| 1191 | else |
| 1192 | ret = iterate_entries(replace, 2); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1193 | replace->selected_chain = tmp; |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | struct ebt_u_stack |
| 1198 | { |
| 1199 | int chain_nr; |
| 1200 | int n; |
| 1201 | struct ebt_u_entry *e; |
| 1202 | struct ebt_u_entries *entries; |
| 1203 | }; |
| 1204 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1205 | /* Checks for loops |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1206 | * As a by-product, the hook_mask member of each chain is filled in |
| 1207 | * correctly. The check functions of the extensions need this hook_mask |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1208 | * to know from which standard chains they can be called. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1209 | void ebt_check_for_loops(struct ebt_u_replace *replace) |
| 1210 | { |
| 1211 | int chain_nr , i, j , k, sp = 0, verdict; |
| 1212 | struct ebt_u_entries *entries, *entries2; |
| 1213 | struct ebt_u_stack *stack = NULL; |
| 1214 | struct ebt_u_entry *e; |
| 1215 | |
| 1216 | i = -1; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1217 | /* Initialize hook_mask to 0 */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1218 | while (1) { |
| 1219 | i++; |
| 1220 | if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i))) |
| 1221 | continue; |
| 1222 | entries = ebt_nr_to_chain(replace, i); |
| 1223 | if (!entries) |
| 1224 | break; |
| 1225 | entries->hook_mask = 0; |
| 1226 | } |
| 1227 | if (i > NF_BR_NUMHOOKS) { |
| 1228 | stack = (struct ebt_u_stack *)malloc((i - NF_BR_NUMHOOKS) * |
| 1229 | sizeof(struct ebt_u_stack)); |
| 1230 | if (!stack) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1231 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1234 | /* Check for loops, starting from every base chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1235 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 1236 | if (!(replace->valid_hooks & (1 << i))) |
| 1237 | continue; |
| 1238 | entries = ebt_nr_to_chain(replace, i); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1239 | /* (1 << NF_BR_NUMHOOKS) implies it's a standard chain |
| 1240 | * (usefull in the final_check() funtions) */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1241 | entries->hook_mask = (1 << i) | (1 << NF_BR_NUMHOOKS); |
| 1242 | chain_nr = i; |
| 1243 | |
| 1244 | e = entries->entries; |
| 1245 | for (j = 0; j < entries->nentries; j++) { |
| 1246 | if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) |
| 1247 | goto letscontinue; |
| 1248 | verdict = ((struct ebt_standard_target *)(e->t))->verdict; |
| 1249 | if (verdict < 0) |
| 1250 | goto letscontinue; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1251 | entries2 = ebt_nr_to_chain(replace, verdict + NF_BR_NUMHOOKS); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1252 | entries2->hook_mask |= entries->hook_mask; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1253 | /* Now see if we've been here before */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1254 | for (k = 0; k < sp; k++) |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1255 | if (stack[k].chain_nr == verdict + NF_BR_NUMHOOKS) { |
| 1256 | ebt_print_error("Loop from chain '%s' to chain '%s'", |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1257 | ebt_nr_to_chain(replace, chain_nr)->name, |
| 1258 | ebt_nr_to_chain(replace, stack[k].chain_nr)->name); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1259 | goto free_stack; |
| 1260 | } |
| 1261 | /* Jump to the chain, make sure we know how to get back */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1262 | stack[sp].chain_nr = chain_nr; |
| 1263 | stack[sp].n = j; |
| 1264 | stack[sp].entries = entries; |
| 1265 | stack[sp].e = e; |
| 1266 | sp++; |
| 1267 | j = -1; |
| 1268 | e = entries2->entries; |
| 1269 | chain_nr = verdict + NF_BR_NUMHOOKS; |
| 1270 | entries = entries2; |
| 1271 | continue; |
| 1272 | letscontinue: |
| 1273 | e = e->next; |
| 1274 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1275 | /* We are at the end of a standard chain */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1276 | if (sp == 0) |
| 1277 | continue; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1278 | /* Go back to the chain one level higher */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1279 | sp--; |
| 1280 | j = stack[sp].n; |
| 1281 | chain_nr = stack[sp].chain_nr; |
| 1282 | e = stack[sp].e; |
| 1283 | entries = stack[sp].entries; |
| 1284 | goto letscontinue; |
| 1285 | } |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1286 | free_stack: |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1287 | free(stack); |
| 1288 | return; |
| 1289 | } |
| 1290 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1291 | /* The user will use the match, so put it in new_entry. The ebt_u_match |
| 1292 | * pointer is put in the ebt_entry_match pointer. ebt_add_rule will |
| 1293 | * fill in the final value for new->m. Unless the rule is added to a chain, |
| 1294 | * the pointer will keep pointing to the ebt_u_match (until the new_entry |
| 1295 | * is freed). I know, I should use a union for these 2 pointer types... */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1296 | void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m) |
| 1297 | { |
| 1298 | struct ebt_u_match_list **m_list, *new; |
| 1299 | |
| 1300 | for (m_list = &new_entry->m_list; *m_list; m_list = &(*m_list)->next); |
| 1301 | new = (struct ebt_u_match_list *) |
| 1302 | malloc(sizeof(struct ebt_u_match_list)); |
| 1303 | if (!new) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1304 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1305 | *m_list = new; |
| 1306 | new->next = NULL; |
| 1307 | new->m = (struct ebt_entry_match *)m; |
| 1308 | } |
| 1309 | |
| 1310 | void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w) |
| 1311 | { |
| 1312 | struct ebt_u_watcher_list **w_list; |
| 1313 | struct ebt_u_watcher_list *new; |
| 1314 | |
| 1315 | for (w_list = &new_entry->w_list; *w_list; w_list = &(*w_list)->next); |
| 1316 | new = (struct ebt_u_watcher_list *) |
| 1317 | malloc(sizeof(struct ebt_u_watcher_list)); |
| 1318 | if (!new) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1319 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1320 | *w_list = new; |
| 1321 | new->next = NULL; |
| 1322 | new->w = (struct ebt_entry_watcher *)w; |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | /* |
| 1327 | ******************* |
| 1328 | ******************* |
| 1329 | **OTHER*FUNCTIONS** |
| 1330 | ******************* |
| 1331 | ******************* |
| 1332 | */ |
| 1333 | |
| 1334 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1335 | /* type = 0 => update chain jumps |
| 1336 | * type = 1 => check for reference, print error when referenced |
| 1337 | * type = 2 => check for reference, don't print error when referenced |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1338 | * |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1339 | * Returns 1 when type == 1 and the chain is referenced |
| 1340 | * returns 0 otherwise */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1341 | static int iterate_entries(struct ebt_u_replace *replace, int type) |
| 1342 | { |
| 1343 | int i = -1, j, chain_nr = replace->selected_chain - NF_BR_NUMHOOKS; |
| 1344 | struct ebt_u_entries *entries; |
| 1345 | struct ebt_u_entry *e; |
| 1346 | |
| 1347 | if (chain_nr < 0) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1348 | ebt_print_bug("iterate_entries: udc = %d < 0", chain_nr); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1349 | while (1) { |
| 1350 | i++; |
| 1351 | entries = ebt_nr_to_chain(replace, i); |
| 1352 | if (!entries) { |
| 1353 | if (i < NF_BR_NUMHOOKS) |
| 1354 | continue; |
| 1355 | else |
| 1356 | break; |
| 1357 | } |
| 1358 | e = entries->entries; |
| 1359 | j = 0; |
| 1360 | while (e) { |
| 1361 | int chain_jmp; |
| 1362 | |
| 1363 | j++; |
| 1364 | if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) { |
| 1365 | e = e->next; |
| 1366 | continue; |
| 1367 | } |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1368 | chain_jmp = ((struct ebt_standard_target *)e->t)-> |
| 1369 | verdict; |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1370 | switch (type) { |
| 1371 | case 1: |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1372 | case 2: |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1373 | if (chain_jmp == chain_nr) { |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1374 | if (type == 2) |
| 1375 | return 1; |
| 1376 | ebt_print_error("Can't delete the chain '%s', it's referenced in chain '%s', rule %d", |
| 1377 | ebt_nr_to_chain(replace, chain_nr + NF_BR_NUMHOOKS)->name, entries->name, j); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1378 | return 1; |
| 1379 | } |
| 1380 | break; |
| 1381 | case 0: |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1382 | /* Adjust the chain jumps when necessary */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1383 | if (chain_jmp > chain_nr) |
| 1384 | ((struct ebt_standard_target *)e->t)->verdict--; |
| 1385 | break; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1386 | } /* End switch */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1387 | e = e->next; |
| 1388 | } |
| 1389 | } |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static void decrease_chain_jumps(struct ebt_u_replace *replace) |
| 1394 | { |
| 1395 | iterate_entries(replace, 0); |
| 1396 | } |
| 1397 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1398 | /* Selected_chain >= NF_BR_NUMHOOKS */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1399 | static void remove_udc(struct ebt_u_replace *replace) |
| 1400 | { |
| 1401 | struct ebt_u_chain_list *cl, **cl2; |
| 1402 | struct ebt_u_entries *entries; |
| 1403 | struct ebt_u_entry *u_e, *tmp; |
| 1404 | int chain_nr = replace->selected_chain; |
| 1405 | |
| 1406 | if (chain_nr < NF_BR_NUMHOOKS) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1407 | ebt_print_bug("remove_udc: chain_nr = %d < %d", chain_nr, |
| 1408 | NF_BR_NUMHOOKS); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1409 | /* First free the rules */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1410 | entries = ebt_nr_to_chain(replace, chain_nr); |
| 1411 | u_e = entries->entries; |
| 1412 | while (u_e) { |
| 1413 | ebt_free_u_entry(u_e); |
| 1414 | tmp = u_e->next; |
| 1415 | free(u_e); |
| 1416 | u_e = tmp; |
| 1417 | } |
| 1418 | |
| 1419 | /* next, remove the chain */ |
| 1420 | cl2 = &(replace->udc); |
| 1421 | while ((*cl2)->udc != entries) |
| 1422 | cl2 = &((*cl2)->next); |
| 1423 | cl = (*cl2); |
| 1424 | (*cl2) = (*cl2)->next; |
| 1425 | free(cl->udc); |
| 1426 | free(cl); |
| 1427 | } |
| 1428 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1429 | /* Used in initialization code of modules */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1430 | void ebt_register_match(struct ebt_u_match *m) |
| 1431 | { |
| 1432 | int size = EBT_ALIGN(m->size) + sizeof(struct ebt_entry_match); |
| 1433 | struct ebt_u_match **i; |
| 1434 | |
| 1435 | m->m = (struct ebt_entry_match *)malloc(size); |
| 1436 | if (!m->m) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1437 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1438 | strcpy(m->m->u.name, m->name); |
| 1439 | m->m->match_size = EBT_ALIGN(m->size); |
| 1440 | m->init(m->m); |
| 1441 | |
| 1442 | for (i = &ebt_matches; *i; i = &((*i)->next)); |
| 1443 | m->next = NULL; |
| 1444 | *i = m; |
| 1445 | } |
| 1446 | |
| 1447 | void ebt_register_watcher(struct ebt_u_watcher *w) |
| 1448 | { |
| 1449 | int size = EBT_ALIGN(w->size) + sizeof(struct ebt_entry_watcher); |
| 1450 | struct ebt_u_watcher **i; |
| 1451 | |
| 1452 | w->w = (struct ebt_entry_watcher *)malloc(size); |
| 1453 | if (!w->w) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1454 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1455 | strcpy(w->w->u.name, w->name); |
| 1456 | w->w->watcher_size = EBT_ALIGN(w->size); |
| 1457 | w->init(w->w); |
| 1458 | |
| 1459 | for (i = &ebt_watchers; *i; i = &((*i)->next)); |
| 1460 | w->next = NULL; |
| 1461 | *i = w; |
| 1462 | } |
| 1463 | |
| 1464 | void ebt_register_target(struct ebt_u_target *t) |
| 1465 | { |
| 1466 | int size = EBT_ALIGN(t->size) + sizeof(struct ebt_entry_target); |
| 1467 | struct ebt_u_target **i; |
| 1468 | |
| 1469 | t->t = (struct ebt_entry_target *)malloc(size); |
| 1470 | if (!t->t) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1471 | ebt_print_memory(); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1472 | strcpy(t->t->u.name, t->name); |
| 1473 | t->t->target_size = EBT_ALIGN(t->size); |
| 1474 | t->init(t->t); |
| 1475 | |
| 1476 | for (i = &ebt_targets; *i; i = &((*i)->next)); |
| 1477 | t->next = NULL; |
| 1478 | *i = t; |
| 1479 | } |
| 1480 | |
| 1481 | void ebt_register_table(struct ebt_u_table *t) |
| 1482 | { |
| 1483 | t->next = ebt_tables; |
| 1484 | ebt_tables = t; |
| 1485 | } |
| 1486 | |
| 1487 | void ebt_iterate_matches(void (*f)(struct ebt_u_match *)) |
| 1488 | { |
| 1489 | struct ebt_u_match *i; |
| 1490 | |
| 1491 | for (i = ebt_matches; i; i = i->next) |
| 1492 | f(i); |
| 1493 | } |
| 1494 | |
| 1495 | void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *)) |
| 1496 | { |
| 1497 | struct ebt_u_watcher *i; |
| 1498 | |
| 1499 | for (i = ebt_watchers; i; i = i->next) |
| 1500 | f(i); |
| 1501 | } |
| 1502 | |
| 1503 | void ebt_iterate_targets(void (*f)(struct ebt_u_target *)) |
| 1504 | { |
| 1505 | struct ebt_u_target *i; |
| 1506 | |
| 1507 | for (i = ebt_targets; i; i = i->next) |
| 1508 | f(i); |
| 1509 | } |
| 1510 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1511 | /* Don't use this function, use ebt_print_bug() */ |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1512 | void __ebt_print_bug(char *file, int line, char *format, ...) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1513 | { |
| 1514 | va_list l; |
| 1515 | |
| 1516 | va_start(l, format); |
| 1517 | printf(PROGNAME" v"PROGVERSION":%s:%d:--BUG--: \n", file, line); |
| 1518 | vprintf(format, l); |
| 1519 | printf("\n"); |
| 1520 | va_end(l); |
| 1521 | exit (-1); |
| 1522 | } |
| 1523 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1524 | /* The error messages are put in here when ebt_silent == 1 |
| 1525 | * ebt_errormsg[0] == '\0' implies there was no error */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1526 | char ebt_errormsg[ERRORMSG_MAXLEN]; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1527 | /* When error messages should not be printed on the screen, after which |
| 1528 | * the program exit()s, set ebt_silent to 1. */ |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1529 | int ebt_silent; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1530 | /* Don't use this function, use ebt_print_error() */ |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 1531 | void __ebt_print_error(char *format, ...) |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1532 | { |
| 1533 | va_list l; |
| 1534 | |
| 1535 | va_start(l, format); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1536 | if (ebt_silent && ebt_errormsg[0] == '\0') { |
| 1537 | vsnprintf(ebt_errormsg, ERRORMSG_MAXLEN, format, l); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1538 | va_end(l); |
| 1539 | } else { |
| 1540 | vprintf(format, l); |
Bart De Schuymer | 87889eb | 2005-01-24 22:25:27 +0000 | [diff] [blame] | 1541 | printf(".\n"); |
Bart De Schuymer | 80c82bb | 2004-01-14 20:06:44 +0000 | [diff] [blame] | 1542 | va_end(l); |
| 1543 | exit (-1); |
| 1544 | } |
| 1545 | } |