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