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