Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1 | /* |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 2 | * ebtables.c, v2.0 July 2002 |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 3 | * |
| 4 | * Author: Bart De Schuymer |
| 5 | * |
| 6 | * This code is stongly inspired on the iptables code which is |
| 7 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | #include <string.h> |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Bart De Schuymer | d458648 | 2002-08-11 16:15:55 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 29 | #include <netinet/ether.h> |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 30 | #include "include/ebtables_u.h" |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 31 | #include "include/ethernetdb.h" |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <sys/wait.h> |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 35 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 36 | /* |
| 37 | * Don't use this function, use print_bug() |
| 38 | */ |
Bart De Schuymer | d458648 | 2002-08-11 16:15:55 +0000 | [diff] [blame] | 39 | void __print_bug(char *file, int line, char *format, ...) |
| 40 | { |
| 41 | va_list l; |
| 42 | |
| 43 | va_start(l, format); |
| 44 | printf(PROGNAME" v"PROGVERSION":%s:%d:--BUG--: \n", file, line); |
| 45 | vprintf(format, l); |
| 46 | printf("\n"); |
| 47 | va_end(l); |
| 48 | exit (-1); |
| 49 | } |
| 50 | |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 51 | #ifndef PROC_SYS_MODPROBE |
| 52 | #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe" |
| 53 | #endif |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 54 | #define ATOMIC_ENV_VARIABLE "EBTABLES_ATOMIC_FILE" |
Bart De Schuymer | 57a3f6a | 2003-04-01 16:59:33 +0000 | [diff] [blame] | 55 | #define PRINT_VERSION printf(PROGNAME" v"PROGVERSION" ("PROGDATE")\n") |
| 56 | |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 57 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 58 | char *hooknames[NF_BR_NUMHOOKS] = |
| 59 | { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 60 | [NF_BR_PRE_ROUTING]"PREROUTING", |
| 61 | [NF_BR_LOCAL_IN]"INPUT", |
| 62 | [NF_BR_FORWARD]"FORWARD", |
| 63 | [NF_BR_LOCAL_OUT]"OUTPUT", |
| 64 | [NF_BR_POST_ROUTING]"POSTROUTING", |
| 65 | [NF_BR_BROUTING]"BROUTING" |
| 66 | }; |
| 67 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 68 | /* |
| 69 | * default command line options |
| 70 | * do not mess around with the already assigned numbers unless |
| 71 | * you know what you are doing |
| 72 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 73 | static struct option ebt_original_options[] = |
| 74 | { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 75 | { "append" , required_argument, 0, 'A' }, |
| 76 | { "insert" , required_argument, 0, 'I' }, |
| 77 | { "delete" , required_argument, 0, 'D' }, |
| 78 | { "list" , optional_argument, 0, 'L' }, |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 79 | { "Lc" , no_argument , 0, 4 }, |
| 80 | { "Ln" , no_argument , 0, 5 }, |
| 81 | { "Lx" , no_argument , 0, 6 }, |
Bart De Schuymer | 22d03a2 | 2003-05-03 20:28:22 +0000 | [diff] [blame] | 82 | { "Lmac2" , no_argument , 0, 12 }, |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 83 | { "zero" , optional_argument, 0, 'Z' }, |
| 84 | { "flush" , optional_argument, 0, 'F' }, |
| 85 | { "policy" , required_argument, 0, 'P' }, |
| 86 | { "in-interface" , required_argument, 0, 'i' }, |
| 87 | { "in-if" , required_argument, 0, 'i' }, |
| 88 | { "logical-in" , required_argument, 0, 2 }, |
| 89 | { "logical-out" , required_argument, 0, 3 }, |
| 90 | { "out-interface" , required_argument, 0, 'o' }, |
| 91 | { "out-if" , required_argument, 0, 'o' }, |
| 92 | { "version" , no_argument , 0, 'V' }, |
| 93 | { "help" , no_argument , 0, 'h' }, |
| 94 | { "jump" , required_argument, 0, 'j' }, |
| 95 | { "proto" , required_argument, 0, 'p' }, |
| 96 | { "protocol" , required_argument, 0, 'p' }, |
| 97 | { "db" , required_argument, 0, 'b' }, |
| 98 | { "source" , required_argument, 0, 's' }, |
| 99 | { "src" , required_argument, 0, 's' }, |
| 100 | { "destination" , required_argument, 0, 'd' }, |
| 101 | { "dst" , required_argument, 0, 'd' }, |
| 102 | { "table" , required_argument, 0, 't' }, |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 103 | { "modprobe" , required_argument, 0, 'M' }, |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 104 | { "new-chain" , required_argument, 0, 'N' }, |
| 105 | { "rename-chain" , required_argument, 0, 'E' }, |
| 106 | { "delete-chain" , required_argument, 0, 'X' }, |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 107 | { "atomic-init" , no_argument , 0, 7 }, |
| 108 | { "atomic-commit" , no_argument , 0, 8 }, |
| 109 | { "atomic-file" , required_argument, 0, 9 }, |
| 110 | { "atomic-save" , no_argument , 0, 10 }, |
Bart De Schuymer | 8d1d894 | 2002-07-15 20:09:09 +0000 | [diff] [blame] | 111 | { "init-table" , no_argument , 0, 11 }, |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 112 | { 0 } |
| 113 | }; |
| 114 | |
| 115 | static struct option *ebt_options = ebt_original_options; |
| 116 | |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 117 | char* standard_targets[NUM_STANDARD_TARGETS] = |
| 118 | { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 119 | "ACCEPT", |
| 120 | "DROP", |
| 121 | "CONTINUE", |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 122 | "RETURN", |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 125 | unsigned char mac_type_unicast[ETH_ALEN] = {0,0,0,0,0,0}; |
| 126 | unsigned char msk_type_unicast[ETH_ALEN] = {1,0,0,0,0,0}; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 127 | unsigned char mac_type_multicast[ETH_ALEN] = {1,0,0,0,0,0}; |
| 128 | unsigned char msk_type_multicast[ETH_ALEN] = {1,0,0,0,0,0}; |
| 129 | unsigned char mac_type_broadcast[ETH_ALEN] = {255,255,255,255,255,255}; |
| 130 | unsigned char msk_type_broadcast[ETH_ALEN] = {255,255,255,255,255,255}; |
| 131 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 132 | /* |
| 133 | * holds all the data |
| 134 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 135 | static struct ebt_u_replace replace; |
| 136 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 137 | /* |
| 138 | * the chosen table |
| 139 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 140 | static struct ebt_u_table *table = NULL; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 141 | /* |
| 142 | * the lists of supported tables, matches, watchers and targets |
| 143 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 144 | static struct ebt_u_table *tables = NULL; |
| 145 | static struct ebt_u_match *matches = NULL; |
| 146 | static struct ebt_u_watcher *watchers = NULL; |
| 147 | static struct ebt_u_target *targets = NULL; |
| 148 | |
| 149 | struct ebt_u_target *find_target(const char *name) |
| 150 | { |
| 151 | struct ebt_u_target *t = targets; |
| 152 | |
| 153 | while(t && strcmp(t->name, name)) |
| 154 | t = t->next; |
| 155 | return t; |
| 156 | } |
| 157 | |
| 158 | struct ebt_u_match *find_match(const char *name) |
| 159 | { |
| 160 | struct ebt_u_match *m = matches; |
| 161 | |
| 162 | while(m && strcmp(m->name, name)) |
| 163 | m = m->next; |
| 164 | return m; |
| 165 | } |
| 166 | |
| 167 | struct ebt_u_watcher *find_watcher(const char *name) |
| 168 | { |
| 169 | struct ebt_u_watcher *w = watchers; |
| 170 | |
| 171 | while(w && strcmp(w->name, name)) |
| 172 | w = w->next; |
| 173 | return w; |
| 174 | } |
| 175 | |
| 176 | struct ebt_u_table *find_table(char *name) |
| 177 | { |
| 178 | struct ebt_u_table *t = tables; |
| 179 | |
| 180 | while (t && strcmp(t->name, name)) |
| 181 | t = t->next; |
| 182 | return t; |
| 183 | } |
| 184 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 185 | /* |
| 186 | * The pointers in here are special: |
| 187 | * The struct ebt_target * pointer is actually a struct ebt_u_target * pointer. |
| 188 | * instead of making yet a few other structs, we just do a cast. |
| 189 | * We need a struct ebt_u_target pointer because we know the address of the data |
| 190 | * they point to won't change. We want to allow that the struct ebt_u_target.t |
| 191 | * member can change. |
| 192 | * Same holds for the struct ebt_match and struct ebt_watcher pointers |
| 193 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 194 | struct ebt_u_entry *new_entry; |
| 195 | |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 196 | static void initialize_entry(struct ebt_u_entry *e) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 197 | { |
| 198 | e->bitmask = EBT_NOPROTO; |
| 199 | e->invflags = 0; |
| 200 | e->ethproto = 0; |
| 201 | strcpy(e->in, ""); |
| 202 | strcpy(e->out, ""); |
| 203 | strcpy(e->logical_in, ""); |
| 204 | strcpy(e->logical_out, ""); |
| 205 | e->m_list = NULL; |
| 206 | e->w_list = NULL; |
Bart De Schuymer | c27432e | 2003-01-09 22:01:07 +0000 | [diff] [blame] | 207 | /* |
| 208 | * the init function of the standard target should have put the verdict |
| 209 | * on CONTINUE |
| 210 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 211 | e->t = (struct ebt_entry_target *)find_target(EBT_STANDARD_TARGET); |
| 212 | if (!e->t) |
Bart De Schuymer | d458648 | 2002-08-11 16:15:55 +0000 | [diff] [blame] | 213 | print_bug("Couldn't load standard target"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 216 | /* |
| 217 | * this doesn't free e, becoz the calling function might need e->next |
| 218 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 219 | static void free_u_entry(struct ebt_u_entry *e) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 220 | { |
| 221 | struct ebt_u_match_list *m_l, *m_l2; |
| 222 | struct ebt_u_watcher_list *w_l, *w_l2; |
| 223 | |
| 224 | m_l = e->m_list; |
| 225 | while (m_l) { |
| 226 | m_l2 = m_l->next; |
| 227 | free(m_l->m); |
| 228 | free(m_l); |
| 229 | m_l = m_l2; |
| 230 | } |
| 231 | w_l = e->w_list; |
| 232 | while (w_l) { |
| 233 | w_l2 = w_l->next; |
| 234 | free(w_l->w); |
| 235 | free(w_l); |
| 236 | w_l = w_l2; |
| 237 | } |
| 238 | free(e->t); |
| 239 | } |
| 240 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 241 | /* |
| 242 | * the user will use the match, so put it in new_entry |
| 243 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 244 | static void add_match(struct ebt_u_match *m) |
| 245 | { |
| 246 | struct ebt_u_match_list **m_list, *new; |
| 247 | |
| 248 | m->used = 1; |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 249 | for (m_list = &new_entry->m_list; *m_list; m_list = &(*m_list)->next); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 250 | new = (struct ebt_u_match_list *) |
| 251 | malloc(sizeof(struct ebt_u_match_list)); |
| 252 | if (!new) |
| 253 | print_memory(); |
| 254 | *m_list = new; |
| 255 | new->next = NULL; |
| 256 | new->m = (struct ebt_entry_match *)m; |
| 257 | } |
| 258 | |
| 259 | static void add_watcher(struct ebt_u_watcher *w) |
| 260 | { |
| 261 | struct ebt_u_watcher_list **w_list; |
| 262 | struct ebt_u_watcher_list *new; |
| 263 | |
| 264 | w->used = 1; |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 265 | for (w_list = &new_entry->w_list; *w_list; w_list = &(*w_list)->next); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 266 | new = (struct ebt_u_watcher_list *) |
| 267 | malloc(sizeof(struct ebt_u_watcher_list)); |
| 268 | if (!new) |
| 269 | print_memory(); |
| 270 | *w_list = new; |
| 271 | new->next = NULL; |
| 272 | new->w = (struct ebt_entry_watcher *)w; |
| 273 | } |
| 274 | |
| 275 | static int global_option_offset = 0; |
| 276 | #define OPTION_OFFSET 256 |
| 277 | static struct option * |
| 278 | merge_options(struct option *oldopts, const struct option *newopts, |
| 279 | unsigned int *options_offset) |
| 280 | { |
| 281 | unsigned int num_old, num_new, i; |
| 282 | struct option *merge; |
| 283 | |
| 284 | if (!newopts || !oldopts || !options_offset) |
| 285 | print_bug("merge wrong"); |
| 286 | for (num_old = 0; oldopts[num_old].name; num_old++); |
| 287 | for (num_new = 0; newopts[num_new].name; num_new++); |
| 288 | |
| 289 | global_option_offset += OPTION_OFFSET; |
| 290 | *options_offset = global_option_offset; |
| 291 | |
| 292 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
| 293 | if (!merge) |
| 294 | print_memory(); |
| 295 | memcpy(merge, oldopts, num_old * sizeof(struct option)); |
| 296 | for (i = 0; i < num_new; i++) { |
| 297 | merge[num_old + i] = newopts[i]; |
| 298 | merge[num_old + i].val += *options_offset; |
| 299 | } |
| 300 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 301 | /* only free dynamically allocated stuff */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 302 | if (oldopts != ebt_original_options) |
| 303 | free(oldopts); |
| 304 | |
| 305 | return merge; |
| 306 | } |
| 307 | |
| 308 | void register_match(struct ebt_u_match *m) |
| 309 | { |
| 310 | int size = m->size + sizeof(struct ebt_entry_match); |
| 311 | struct ebt_u_match **i; |
| 312 | |
| 313 | m->m = (struct ebt_entry_match *)malloc(size); |
| 314 | if (!m->m) |
| 315 | print_memory(); |
| 316 | strcpy(m->m->u.name, m->name); |
| 317 | m->m->match_size = m->size; |
| 318 | ebt_options = merge_options |
| 319 | (ebt_options, m->extra_ops, &(m->option_offset)); |
| 320 | m->init(m->m); |
| 321 | |
| 322 | for (i = &matches; *i; i = &((*i)->next)); |
| 323 | m->next = NULL; |
| 324 | *i = m; |
| 325 | } |
| 326 | |
| 327 | void register_watcher(struct ebt_u_watcher *w) |
| 328 | { |
| 329 | int size = w->size + sizeof(struct ebt_entry_watcher); |
| 330 | struct ebt_u_watcher **i; |
| 331 | |
| 332 | w->w = (struct ebt_entry_watcher *)malloc(size); |
| 333 | if (!w->w) |
| 334 | print_memory(); |
| 335 | strcpy(w->w->u.name, w->name); |
| 336 | w->w->watcher_size = w->size; |
| 337 | ebt_options = merge_options |
| 338 | (ebt_options, w->extra_ops, &(w->option_offset)); |
| 339 | w->init(w->w); |
| 340 | |
| 341 | for (i = &watchers; *i; i = &((*i)->next)); |
| 342 | w->next = NULL; |
| 343 | *i = w; |
| 344 | } |
| 345 | |
| 346 | void register_target(struct ebt_u_target *t) |
| 347 | { |
| 348 | int size = t->size + sizeof(struct ebt_entry_target); |
| 349 | struct ebt_u_target **i; |
| 350 | |
| 351 | t->t = (struct ebt_entry_target *)malloc(size); |
| 352 | if (!t->t) |
| 353 | print_memory(); |
| 354 | strcpy(t->t->u.name, t->name); |
| 355 | t->t->target_size = t->size; |
| 356 | ebt_options = merge_options |
| 357 | (ebt_options, t->extra_ops, &(t->option_offset)); |
| 358 | t->init(t->t); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 359 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 360 | for (i = &targets; *i; i = &((*i)->next)); |
| 361 | t->next = NULL; |
| 362 | *i = t; |
| 363 | } |
| 364 | |
| 365 | void register_table(struct ebt_u_table *t) |
| 366 | { |
| 367 | t->next = tables; |
| 368 | tables = t; |
| 369 | } |
| 370 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 371 | /* |
| 372 | * blatently stolen (again) from iptables.c userspace program |
| 373 | * find out where the modprobe utility is located |
| 374 | */ |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 375 | static char *get_modprobe(void) |
| 376 | { |
| 377 | int procfile; |
| 378 | char *ret; |
| 379 | |
| 380 | procfile = open(PROC_SYS_MODPROBE, O_RDONLY); |
| 381 | if (procfile < 0) |
| 382 | return NULL; |
| 383 | |
| 384 | ret = malloc(1024); |
| 385 | if (ret) { |
| 386 | switch (read(procfile, ret, 1024)) { |
| 387 | case -1: goto fail; |
| 388 | case 1024: goto fail; /* Partial read. Wierd */ |
| 389 | } |
| 390 | if (ret[strlen(ret)-1]=='\n') |
| 391 | ret[strlen(ret)-1]=0; |
| 392 | close(procfile); |
| 393 | return ret; |
| 394 | } |
| 395 | fail: |
| 396 | free(ret); |
| 397 | close(procfile); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 401 | int ebtables_insmod(const char *modname, const char *modprobe) |
| 402 | { |
| 403 | char *buf = NULL; |
| 404 | char *argv[3]; |
| 405 | |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 406 | /* If they don't explicitly set it, read out of kernel */ |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 407 | if (!modprobe) { |
| 408 | buf = get_modprobe(); |
| 409 | if (!buf) |
| 410 | return -1; |
| 411 | modprobe = buf; |
| 412 | } |
| 413 | |
| 414 | switch (fork()) { |
| 415 | case 0: |
| 416 | argv[0] = (char *)modprobe; |
| 417 | argv[1] = (char *)modname; |
| 418 | argv[2] = NULL; |
| 419 | execv(argv[0], argv); |
| 420 | |
| 421 | /* not usually reached */ |
| 422 | exit(0); |
| 423 | case -1: |
| 424 | return -1; |
| 425 | |
| 426 | default: /* parent */ |
| 427 | wait(NULL); |
| 428 | } |
| 429 | |
| 430 | free(buf); |
| 431 | return 0; |
| 432 | } |
| 433 | |
Bart De Schuymer | 9a0fbf2 | 2003-01-11 16:16:54 +0000 | [diff] [blame] | 434 | static void list_extensions() |
| 435 | { |
| 436 | struct ebt_u_table *tbl = tables; |
| 437 | struct ebt_u_target *t = targets; |
| 438 | struct ebt_u_match *m = matches; |
| 439 | struct ebt_u_watcher *w = watchers; |
| 440 | |
Bart De Schuymer | 57a3f6a | 2003-04-01 16:59:33 +0000 | [diff] [blame] | 441 | PRINT_VERSION; |
Bart De Schuymer | 9a0fbf2 | 2003-01-11 16:16:54 +0000 | [diff] [blame] | 442 | printf("Supported userspace extensions:\n\nSupported tables:\n"); |
| 443 | while(tbl) { |
| 444 | printf("%s\n", tbl->name); |
| 445 | tbl = tbl->next; |
| 446 | } |
| 447 | printf("\nSupported targets:\n"); |
| 448 | while(t) { |
| 449 | printf("%s\n", t->name); |
| 450 | t = t->next; |
| 451 | } |
| 452 | printf("\nSupported matches:\n"); |
| 453 | while(m) { |
| 454 | printf("%s\n", m->name); |
| 455 | m = m->next; |
| 456 | } |
| 457 | printf("\nSupported watchers:\n"); |
| 458 | while(w) { |
| 459 | printf("%s\n", w->name); |
| 460 | w = w->next; |
| 461 | } |
| 462 | exit(0); |
| 463 | } |
| 464 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 465 | /* |
| 466 | * we use replace.flags, so we can't use the following values: |
| 467 | * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO |
| 468 | */ |
Bart De Schuymer | 22d03a2 | 2003-05-03 20:28:22 +0000 | [diff] [blame] | 469 | #define LIST_N 0x04 |
| 470 | #define LIST_C 0x08 |
| 471 | #define LIST_X 0x10 |
| 472 | #define LIST_MAC2 0x20 |
| 473 | |
| 474 | void print_mac(const char *mac) |
| 475 | { |
| 476 | if (replace.flags & LIST_MAC2) { |
| 477 | int j; |
| 478 | for (j = 0; j < ETH_ALEN; j++) |
| 479 | printf("%02x%s", (unsigned char)mac[j], |
| 480 | (j==ETH_ALEN-1) ? "" : ":"); |
| 481 | } else |
| 482 | printf("%s", ether_ntoa((struct ether_addr *) mac)); |
| 483 | } |
| 484 | |
| 485 | void print_mac_and_mask(const char *mac, const char *mask) |
| 486 | { |
| 487 | char hlpmsk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 488 | |
| 489 | print_mac(mac); |
| 490 | if (memcmp(mask, hlpmsk, 6)) { |
| 491 | printf("/"); |
| 492 | print_mac(mask); |
| 493 | } |
| 494 | } |
| 495 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 496 | /* |
| 497 | * helper function for list_rules() |
| 498 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 499 | static void list_em(struct ebt_u_entries *entries) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 500 | { |
| 501 | int i, j, space = 0, digits; |
| 502 | struct ebt_u_entry *hlp; |
| 503 | struct ebt_u_match_list *m_l; |
| 504 | struct ebt_u_watcher_list *w_l; |
| 505 | struct ebt_u_match *m; |
| 506 | struct ebt_u_watcher *w; |
| 507 | struct ebt_u_target *t; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 508 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 509 | hlp = entries->entries; |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 510 | if (replace.flags & LIST_X && entries->policy != EBT_ACCEPT) { |
| 511 | printf("ebtables -t %s -P %s %s\n", replace.name, |
| 512 | entries->name, standard_targets[-entries->policy - 1]); |
| 513 | } else if (!(replace.flags & LIST_X)) { |
Bart De Schuymer | c87c964 | 2002-08-01 15:34:16 +0000 | [diff] [blame] | 514 | printf("\nBridge chain: %s, entries: %d, policy: %s\n", |
| 515 | entries->name, entries->nentries, |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 516 | standard_targets[-entries->policy - 1]); |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 517 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 518 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 519 | i = entries->nentries; |
Bart De Schuymer | f8f8f29 | 2002-06-25 15:43:57 +0000 | [diff] [blame] | 520 | while (i > 9) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 521 | space++; |
| 522 | i /= 10; |
| 523 | } |
| 524 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 525 | for (i = 0; i < entries->nentries; i++) { |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 526 | if (replace.flags & LIST_N) { |
| 527 | digits = 0; |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 528 | /* A little work to get nice rule numbers. */ |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 529 | j = i + 1; |
| 530 | while (j > 9) { |
| 531 | digits++; |
| 532 | j /= 10; |
| 533 | } |
| 534 | for (j = 0; j < space - digits; j++) |
| 535 | printf(" "); |
| 536 | printf("%d. ", i + 1); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 537 | } |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 538 | if (replace.flags & LIST_X) |
| 539 | printf("ebtables -t %s -A %s ", |
| 540 | replace.name, entries->name); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 541 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 542 | /* |
| 543 | * Don't print anything about the protocol if no protocol was |
| 544 | * specified, obviously this means any protocol will do. |
| 545 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 546 | if (!(hlp->bitmask & EBT_NOPROTO)) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 547 | printf("-p "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 548 | if (hlp->invflags & EBT_IPROTO) |
| 549 | printf("! "); |
| 550 | if (hlp->bitmask & EBT_802_3) |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 551 | printf("Length "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 552 | else { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 553 | struct ethertypeent *ent; |
| 554 | |
| 555 | ent = getethertypebynumber(ntohs(hlp->ethproto)); |
| 556 | if (!ent) |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 557 | printf("0x%x ", ntohs(hlp->ethproto)); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 558 | else |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 559 | printf("%s ", ent->e_name); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | if (hlp->bitmask & EBT_SOURCEMAC) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 563 | printf("-s "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 564 | if (hlp->invflags & EBT_ISOURCE) |
| 565 | printf("! "); |
| 566 | if (!memcmp(hlp->sourcemac, mac_type_unicast, 6) && |
| 567 | !memcmp(hlp->sourcemsk, msk_type_unicast, 6)) { |
| 568 | printf("Unicast"); |
| 569 | goto endsrc; |
| 570 | } |
| 571 | if (!memcmp(hlp->sourcemac, mac_type_multicast, 6) && |
| 572 | !memcmp(hlp->sourcemsk, msk_type_multicast, 6)) { |
| 573 | printf("Multicast"); |
| 574 | goto endsrc; |
| 575 | } |
| 576 | if (!memcmp(hlp->sourcemac, mac_type_broadcast, 6) && |
| 577 | !memcmp(hlp->sourcemsk, msk_type_broadcast, 6)) { |
| 578 | printf("Broadcast"); |
| 579 | goto endsrc; |
| 580 | } |
Bart De Schuymer | 22d03a2 | 2003-05-03 20:28:22 +0000 | [diff] [blame] | 581 | print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 582 | endsrc: |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 583 | printf(" "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 584 | } |
| 585 | if (hlp->bitmask & EBT_DESTMAC) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 586 | printf("-d "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 587 | if (hlp->invflags & EBT_IDEST) |
| 588 | printf("! "); |
| 589 | if (!memcmp(hlp->destmac, mac_type_unicast, 6) && |
| 590 | !memcmp(hlp->destmsk, msk_type_unicast, 6)) { |
| 591 | printf("Unicast"); |
| 592 | goto enddst; |
| 593 | } |
| 594 | if (!memcmp(hlp->destmac, mac_type_multicast, 6) && |
| 595 | !memcmp(hlp->destmsk, msk_type_multicast, 6)) { |
| 596 | printf("Multicast"); |
| 597 | goto enddst; |
| 598 | } |
| 599 | if (!memcmp(hlp->destmac, mac_type_broadcast, 6) && |
| 600 | !memcmp(hlp->destmsk, msk_type_broadcast, 6)) { |
| 601 | printf("Broadcast"); |
| 602 | goto enddst; |
| 603 | } |
Bart De Schuymer | 22d03a2 | 2003-05-03 20:28:22 +0000 | [diff] [blame] | 604 | print_mac_and_mask(hlp->destmac, hlp->destmsk); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 605 | enddst: |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 606 | printf(" "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 607 | } |
| 608 | if (hlp->in[0] != '\0') { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 609 | printf("-i "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 610 | if (hlp->invflags & EBT_IIN) |
| 611 | printf("! "); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 612 | printf("%s ", hlp->in); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 613 | } |
| 614 | if (hlp->logical_in[0] != '\0') { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 615 | printf("--logical-in "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 616 | if (hlp->invflags & EBT_ILOGICALIN) |
| 617 | printf("! "); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 618 | printf("%s ", hlp->logical_in); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 619 | } |
| 620 | if (hlp->logical_out[0] != '\0') { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 621 | printf("--logical-out "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 622 | if (hlp->invflags & EBT_ILOGICALOUT) |
| 623 | printf("! "); |
Bart De Schuymer | b3d8f26 | 2002-07-07 14:26:15 +0000 | [diff] [blame] | 624 | printf("%s ", hlp->logical_out); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 625 | } |
| 626 | if (hlp->out[0] != '\0') { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 627 | printf("-o "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 628 | if (hlp->invflags & EBT_IOUT) |
| 629 | printf("! "); |
Bart De Schuymer | b3d8f26 | 2002-07-07 14:26:15 +0000 | [diff] [blame] | 630 | printf("%s ", hlp->out); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | m_l = hlp->m_list; |
| 634 | while (m_l) { |
| 635 | m = find_match(m_l->m->u.name); |
| 636 | if (!m) |
| 637 | print_bug("Match not found"); |
| 638 | m->print(hlp, m_l->m); |
| 639 | m_l = m_l->next; |
| 640 | } |
| 641 | w_l = hlp->w_list; |
| 642 | while (w_l) { |
| 643 | w = find_watcher(w_l->w->u.name); |
| 644 | if (!w) |
| 645 | print_bug("Watcher not found"); |
| 646 | w->print(hlp, w_l->w); |
| 647 | w_l = w_l->next; |
| 648 | } |
| 649 | |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 650 | printf("-j "); |
| 651 | if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET)) |
| 652 | printf("%s ", hlp->t->u.name); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 653 | t = find_target(hlp->t->u.name); |
| 654 | if (!t) |
| 655 | print_bug("Target not found"); |
| 656 | t->print(hlp, hlp->t); |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 657 | if (replace.flags & LIST_C) |
Bart De Schuymer | 73fccca | 2002-10-17 22:00:23 +0000 | [diff] [blame] | 658 | printf(", pcnt = %llu -- bcnt = %llu", |
| 659 | replace.counters[entries->counter_offset + i].pcnt, |
| 660 | replace.counters[entries->counter_offset + i].bcnt); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 661 | printf("\n"); |
| 662 | hlp = hlp->next; |
| 663 | } |
| 664 | } |
| 665 | |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 666 | struct ebt_u_entries *nr_to_chain(int nr) |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 667 | { |
| 668 | if (nr == -1) |
| 669 | return NULL; |
| 670 | if (nr < NF_BR_NUMHOOKS) |
| 671 | return replace.hook_entry[nr]; |
| 672 | else { |
| 673 | int i; |
| 674 | struct ebt_u_chain_list *cl = replace.udc; |
| 675 | |
| 676 | i = nr - NF_BR_NUMHOOKS; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 677 | while (i > 0 && cl) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 678 | cl = cl->next; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 679 | i--; |
| 680 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 681 | if (cl) |
| 682 | return cl->udc; |
| 683 | else |
| 684 | return NULL; |
| 685 | } |
| 686 | } |
| 687 | |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 688 | static inline struct ebt_u_entries *to_chain() |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 689 | { |
| 690 | return nr_to_chain(replace.selected_hook); |
| 691 | } |
| 692 | |
| 693 | struct ebt_u_stack |
| 694 | { |
| 695 | int chain_nr; |
| 696 | int n; |
| 697 | struct ebt_u_entry *e; |
| 698 | struct ebt_u_entries *entries; |
| 699 | }; |
| 700 | |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 701 | static void check_for_loops() |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 702 | { |
| 703 | int chain_nr , i, j , k, sp = 0, verdict; |
| 704 | struct ebt_u_entries *entries, *entries2; |
| 705 | struct ebt_u_stack *stack = NULL; |
| 706 | struct ebt_u_entry *e; |
| 707 | |
| 708 | i = -1; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 709 | /* |
| 710 | * initialize hook_mask to 0 |
| 711 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 712 | while (1) { |
| 713 | i++; |
| 714 | if (i < NF_BR_NUMHOOKS && !(replace.valid_hooks & (1 << i))) |
| 715 | continue; |
| 716 | entries = nr_to_chain(i); |
| 717 | if (!entries) |
| 718 | break; |
| 719 | entries->hook_mask = 0; |
| 720 | } |
| 721 | if (i > NF_BR_NUMHOOKS) { |
| 722 | stack = (struct ebt_u_stack *)malloc((i - NF_BR_NUMHOOKS) * |
| 723 | sizeof(struct ebt_u_stack)); |
| 724 | if (!stack) |
| 725 | print_memory(); |
| 726 | } |
| 727 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 728 | /* |
| 729 | * check for loops, starting from every base chain |
| 730 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 731 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 732 | if (!(replace.valid_hooks & (1 << i))) |
| 733 | continue; |
| 734 | entries = nr_to_chain(i); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 735 | /* |
| 736 | * (1 << NF_BR_NUMHOOKS) implies it's a standard chain |
| 737 | * (usefull in the final_check() funtions) |
| 738 | */ |
Bart De Schuymer | b26649e | 2002-07-25 14:51:54 +0000 | [diff] [blame] | 739 | entries->hook_mask = (1 << i) | (1 << NF_BR_NUMHOOKS); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 740 | chain_nr = i; |
| 741 | |
| 742 | e = entries->entries; |
| 743 | for (j = 0; j < entries->nentries; j++) { |
| 744 | if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) |
| 745 | goto letscontinue; |
| 746 | verdict = ((struct ebt_standard_target *)(e->t))->verdict; |
| 747 | if (verdict < 0) |
| 748 | goto letscontinue; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 749 | entries2 = nr_to_chain(verdict + NF_BR_NUMHOOKS); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 750 | entries2->hook_mask |= entries->hook_mask; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 751 | /* |
| 752 | * now see if we've been here before |
| 753 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 754 | for (k = 0; k < sp; k++) |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 755 | if (stack[k].chain_nr == verdict + NF_BR_NUMHOOKS) |
Bart De Schuymer | 1a0a9c3 | 2002-06-24 21:10:16 +0000 | [diff] [blame] | 756 | print_error("Loop from chain %s to chain %s", |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 757 | nr_to_chain(chain_nr)->name, |
| 758 | nr_to_chain(stack[k].chain_nr)->name); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 759 | /* |
| 760 | * jump to the chain, make sure we know how to get back |
| 761 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 762 | stack[sp].chain_nr = chain_nr; |
| 763 | stack[sp].n = j; |
| 764 | stack[sp].entries = entries; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 765 | stack[sp].e = e; |
| 766 | sp++; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 767 | j = -1; |
| 768 | e = entries2->entries; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 769 | chain_nr = verdict + NF_BR_NUMHOOKS; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 770 | entries = entries2; |
| 771 | continue; |
| 772 | letscontinue: |
| 773 | e = e->next; |
| 774 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 775 | /* |
| 776 | * we are at the end of a standard chain |
| 777 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 778 | if (sp == 0) |
| 779 | continue; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 780 | /* |
| 781 | * go back to the chain one level higher |
| 782 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 783 | sp--; |
| 784 | j = stack[sp].n; |
| 785 | chain_nr = stack[sp].chain_nr; |
| 786 | e = stack[sp].e; |
| 787 | entries = stack[sp].entries; |
| 788 | goto letscontinue; |
| 789 | } |
| 790 | free(stack); |
| 791 | return; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 794 | /* |
| 795 | * parse the chain name and return the corresponding nr |
| 796 | * returns -1 on failure |
| 797 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 798 | int get_hooknr(char* arg) |
| 799 | { |
| 800 | int i; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 801 | struct ebt_u_chain_list *cl = replace.udc; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 802 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 803 | for (i = 0; i < NF_BR_NUMHOOKS; i++) { |
| 804 | if (!(replace.valid_hooks & (1 << i))) |
| 805 | continue; |
| 806 | if (!strcmp(arg, replace.hook_entry[i]->name)) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 807 | return i; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 808 | } |
| 809 | while(cl) { |
| 810 | if (!strcmp(arg, cl->udc->name)) |
| 811 | return i; |
| 812 | i++; |
| 813 | cl = cl->next; |
| 814 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 815 | return -1; |
| 816 | } |
| 817 | |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 818 | static void print_help() |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 819 | { |
| 820 | struct ebt_u_match_list *m_l; |
| 821 | struct ebt_u_watcher_list *w_l; |
| 822 | |
Bart De Schuymer | 57a3f6a | 2003-04-01 16:59:33 +0000 | [diff] [blame] | 823 | PRINT_VERSION; |
| 824 | printf( |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 825 | "Usage:\n" |
| 826 | "ebtables -[ADI] chain rule-specification [options]\n" |
| 827 | "ebtables -P chain target\n" |
| 828 | "ebtables -[LFZ] [chain]\n" |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 829 | "ebtables -[NX] [chain]\n" |
| 830 | "ebtables -E old-chain-name new-chain-name\n\n" |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 831 | "Commands:\n" |
Bart De Schuymer | 23f6dcf | 2002-08-17 09:14:07 +0000 | [diff] [blame] | 832 | "--append -A chain : append to chain\n" |
| 833 | "--delete -D chain : delete matching rule from chain\n" |
| 834 | "--delete -D chain rulenum : delete rule at position rulenum from chain\n" |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 835 | "--insert -I chain rulenum : insert rule at position rulenum in chain\n" |
Bart De Schuymer | 23f6dcf | 2002-08-17 09:14:07 +0000 | [diff] [blame] | 836 | "--list -L [chain] : list the rules in a chain or in all chains\n" |
| 837 | "--flush -F [chain] : delete all rules in chain or in all chains\n" |
| 838 | "--init-table : replace the kernel table with the initial table\n" |
| 839 | "--zero -Z [chain] : put counters on zero in chain or in all chains\n" |
| 840 | "--policy -P chain target : change policy on chain to target\n" |
| 841 | "--new-chain -N chain : create a user defined chain\n" |
| 842 | "--rename-chain -E old new : rename a chain\n" |
| 843 | "--delete-chain -X chain : delete a user defined chain\n" |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 844 | "--atomic-commit : update the kernel w/t table contained in <FILE>\n" |
| 845 | "--atomic-init : put the initial kernel table into <FILE>\n" |
| 846 | "--atomic-save : put the current kernel table into <FILE>\n" |
Bart De Schuymer | 9781996 | 2002-12-11 21:23:07 +0000 | [diff] [blame] | 847 | "--atomic-file file : set <FILE> to file\n\n" |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 848 | "Options:\n" |
| 849 | "--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n" |
| 850 | "--src -s [!] address[/mask]: source mac address\n" |
| 851 | "--dst -d [!] address[/mask]: destination mac address\n" |
| 852 | "--in-if -i [!] name : network input interface name\n" |
| 853 | "--out-if -o [!] name : network output interface name\n" |
| 854 | "--logical-in [!] name : logical bridge input interface name\n" |
| 855 | "--logical-out [!] name : logical bridge output interface name\n" |
Bart De Schuymer | 5cbc8e0 | 2002-07-14 21:15:28 +0000 | [diff] [blame] | 856 | "--modprobe -M program : try to insert modules using this program\n" |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 857 | "--version -V : print package version\n\n" |
| 858 | "Environment variable:\n" |
| 859 | ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value" |
| 860 | "\n\n"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 861 | |
| 862 | m_l = new_entry->m_list; |
| 863 | while (m_l) { |
| 864 | ((struct ebt_u_match *)m_l->m)->help(); |
| 865 | printf("\n"); |
| 866 | m_l = m_l->next; |
| 867 | } |
| 868 | w_l = new_entry->w_list; |
| 869 | while (w_l) { |
| 870 | ((struct ebt_u_watcher *)w_l->w)->help(); |
| 871 | printf("\n"); |
| 872 | w_l = w_l->next; |
| 873 | } |
| 874 | ((struct ebt_u_target *)new_entry->t)->help(); |
| 875 | printf("\n"); |
| 876 | if (table->help) |
| 877 | table->help(hooknames); |
| 878 | exit(0); |
| 879 | } |
| 880 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 881 | /* |
| 882 | * execute command L |
| 883 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 884 | static void list_rules() |
| 885 | { |
| 886 | int i; |
| 887 | |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 888 | if (!(replace.flags & LIST_X)) |
| 889 | printf("Bridge table: %s\n", table->name); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 890 | if (replace.selected_hook != -1) { |
| 891 | list_em(to_chain()); |
| 892 | } else { |
| 893 | struct ebt_u_chain_list *cl = replace.udc; |
| 894 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 895 | /* |
| 896 | * create new chains and rename standard chains when necessary |
| 897 | */ |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 898 | if (replace.flags & LIST_X) { |
| 899 | while (cl) { |
| 900 | printf("ebtables -t %s -N %s\n", replace.name, |
| 901 | cl->udc->name); |
| 902 | cl = cl->next; |
| 903 | } |
| 904 | cl = replace.udc; |
| 905 | for (i = 0; i < NF_BR_NUMHOOKS; i++) |
| 906 | if (replace.valid_hooks & (1 << i) && |
| 907 | strcmp(replace.hook_entry[i]->name, hooknames[i])) |
| 908 | printf("ebtables -t %s -E %s %s\n", |
| 909 | replace.name, hooknames[i], |
| 910 | replace.hook_entry[i]->name); |
| 911 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 912 | i = 0; |
| 913 | while (1) { |
| 914 | if (i < NF_BR_NUMHOOKS) { |
| 915 | if (replace.valid_hooks & (1 << i)) |
| 916 | list_em(replace.hook_entry[i]); |
| 917 | i++; |
| 918 | continue; |
| 919 | } else { |
| 920 | if (!cl) |
| 921 | break; |
| 922 | list_em(cl->udc); |
| 923 | cl = cl->next; |
| 924 | } |
| 925 | } |
| 926 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 929 | /* |
| 930 | * execute command P |
| 931 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 932 | static void change_policy(int policy) |
| 933 | { |
| 934 | int i; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 935 | struct ebt_u_entries *entries = to_chain(); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 936 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 937 | /* |
| 938 | * don't do anything if the policy is the same |
| 939 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 940 | if (entries->policy != policy) { |
| 941 | entries->policy = policy; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 942 | replace.num_counters = replace.nentries; |
| 943 | if (replace.nentries) { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 944 | /* |
| 945 | * '+ 1' for the CNT_END |
| 946 | */ |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 947 | if (!(replace.counterchanges = (unsigned short *) malloc( |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 948 | (replace.nentries + 1) * sizeof(unsigned short)))) |
| 949 | print_memory(); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 950 | /* |
| 951 | * done nothing special to the rules |
| 952 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 953 | for (i = 0; i < replace.nentries; i++) |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 954 | replace.counterchanges[i] = CNT_NORM; |
| 955 | replace.counterchanges[replace.nentries] = CNT_END; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 956 | } |
| 957 | else |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 958 | replace.counterchanges = NULL; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 959 | } |
| 960 | else |
| 961 | exit(0); |
| 962 | } |
| 963 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 964 | /* |
| 965 | * flush one chain or the complete table |
| 966 | * -1 == nothing to do |
| 967 | * 0 == give back to kernel |
| 968 | */ |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 969 | static int flush_chains() |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 970 | { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 971 | int i, j, oldnentries, numdel; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 972 | unsigned short *cnt; |
| 973 | struct ebt_u_entry *u_e, *tmp; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 974 | struct ebt_u_entries *entries = to_chain(); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 975 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 976 | /* |
| 977 | * flush whole table |
| 978 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 979 | if (!entries) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 980 | if (replace.nentries == 0) |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 981 | return -1; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 982 | replace.nentries = 0; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 983 | /* |
| 984 | * no need for the kernel to give us counters back |
| 985 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 986 | replace.num_counters = 0; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 987 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 988 | /* |
| 989 | * free everything and zero (n)entries |
| 990 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 991 | i = -1; |
| 992 | while (1) { |
| 993 | i++; |
| 994 | entries = nr_to_chain(i); |
| 995 | if (!entries) { |
| 996 | if (i < NF_BR_NUMHOOKS) |
| 997 | continue; |
| 998 | else |
| 999 | break; |
| 1000 | } |
| 1001 | entries->nentries = 0; |
| 1002 | entries->counter_offset = 0; |
| 1003 | u_e = entries->entries; |
| 1004 | entries->entries = NULL; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1005 | while (u_e) { |
| 1006 | free_u_entry(u_e); |
| 1007 | tmp = u_e->next; |
| 1008 | free(u_e); |
| 1009 | u_e = tmp; |
| 1010 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1011 | } |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1012 | return 0; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1015 | if (entries->nentries == 0) |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1016 | return -1; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1017 | oldnentries = replace.nentries; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1018 | replace.nentries -= entries->nentries; |
| 1019 | numdel = entries->nentries; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1020 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1021 | if (replace.nentries) { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1022 | /* |
| 1023 | * +1 for CNT_END |
| 1024 | */ |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1025 | if ( !(replace.counterchanges = (unsigned short *) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1026 | malloc((oldnentries + 1) * sizeof(unsigned short))) ) |
| 1027 | print_memory(); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1028 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1029 | /* |
| 1030 | * delete the counters belonging to the specified chain, |
| 1031 | * update counter_offset |
| 1032 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1033 | i = -1; |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1034 | cnt = replace.counterchanges; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1035 | while (1) { |
| 1036 | i++; |
| 1037 | entries = nr_to_chain(i); |
| 1038 | if (!entries) { |
| 1039 | if (i < NF_BR_NUMHOOKS) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1040 | continue; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1041 | else |
| 1042 | break; |
| 1043 | } |
| 1044 | if (i > replace.selected_hook) |
| 1045 | entries->counter_offset -= numdel; |
| 1046 | if (replace.nentries) { |
| 1047 | for (j = 0; j < entries->nentries; j++) { |
| 1048 | if (i == replace.selected_hook) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1049 | *cnt = CNT_DEL; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1050 | else |
| 1051 | *cnt = CNT_NORM; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1052 | cnt++; |
| 1053 | } |
| 1054 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | if (replace.nentries) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1058 | *cnt = CNT_END; |
| 1059 | replace.num_counters = oldnentries; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1060 | } else |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1061 | replace.num_counters = 0; |
| 1062 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1063 | entries = to_chain(); |
| 1064 | entries->nentries = 0; |
| 1065 | u_e = entries->entries; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1066 | while (u_e) { |
| 1067 | free_u_entry(u_e); |
| 1068 | tmp = u_e->next; |
| 1069 | free(u_e); |
| 1070 | u_e = tmp; |
| 1071 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1072 | entries->entries = NULL; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1073 | return 0; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1074 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1075 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1076 | /* |
| 1077 | * -1 == no match |
| 1078 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1079 | static int check_rule_exists(int rule_nr) |
| 1080 | { |
| 1081 | struct ebt_u_entry *u_e; |
| 1082 | struct ebt_u_match_list *m_l, *m_l2; |
| 1083 | struct ebt_u_match *m; |
| 1084 | struct ebt_u_watcher_list *w_l, *w_l2; |
| 1085 | struct ebt_u_watcher *w; |
| 1086 | struct ebt_u_target *t = (struct ebt_u_target *)new_entry->t; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1087 | struct ebt_u_entries *entries = to_chain(); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1088 | int i, j, k; |
| 1089 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1090 | /* |
| 1091 | * handle '-D chain rulenr' command |
| 1092 | */ |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1093 | if (rule_nr != 0) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1094 | if (rule_nr > entries->nentries) |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1095 | return -1; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1096 | /* |
| 1097 | * user starts counting from 1 |
| 1098 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1099 | return rule_nr - 1; |
| 1100 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1101 | u_e = entries->entries; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1102 | /* |
| 1103 | * check for an existing rule (if there are duplicate rules, |
| 1104 | * take the first occurance) |
| 1105 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1106 | for (i = 0; i < entries->nentries; i++, u_e = u_e->next) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1107 | if (!u_e) |
| 1108 | print_bug("Hmm, trouble"); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1109 | if (u_e->ethproto != new_entry->ethproto) |
| 1110 | continue; |
| 1111 | if (strcmp(u_e->in, new_entry->in)) |
| 1112 | continue; |
| 1113 | if (strcmp(u_e->out, new_entry->out)) |
| 1114 | continue; |
| 1115 | if (strcmp(u_e->logical_in, new_entry->logical_in)) |
| 1116 | continue; |
| 1117 | if (strcmp(u_e->logical_out, new_entry->logical_out)) |
| 1118 | continue; |
| 1119 | if (new_entry->bitmask & EBT_SOURCEMAC && |
| 1120 | memcmp(u_e->sourcemac, new_entry->sourcemac, ETH_ALEN)) |
| 1121 | continue; |
| 1122 | if (new_entry->bitmask & EBT_DESTMAC && |
| 1123 | memcmp(u_e->destmac, new_entry->destmac, ETH_ALEN)) |
| 1124 | continue; |
| 1125 | if (new_entry->bitmask != u_e->bitmask || |
| 1126 | new_entry->invflags != u_e->invflags) |
| 1127 | continue; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1128 | /* |
| 1129 | * compare all matches |
| 1130 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1131 | m_l = new_entry->m_list; |
| 1132 | j = 0; |
| 1133 | while (m_l) { |
| 1134 | m = (struct ebt_u_match *)(m_l->m); |
| 1135 | m_l2 = u_e->m_list; |
| 1136 | while (m_l2 && strcmp(m_l2->m->u.name, m->m->u.name)) |
| 1137 | m_l2 = m_l2->next; |
| 1138 | if (!m_l2 || !m->compare(m->m, m_l2->m)) |
| 1139 | goto letscontinue; |
| 1140 | j++; |
| 1141 | m_l = m_l->next; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1142 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1143 | /* |
| 1144 | * now be sure they have the same nr of matches |
| 1145 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1146 | k = 0; |
| 1147 | m_l = u_e->m_list; |
| 1148 | while (m_l) { |
| 1149 | k++; |
| 1150 | m_l = m_l->next; |
| 1151 | } |
| 1152 | if (j != k) |
| 1153 | continue; |
| 1154 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1155 | /* |
| 1156 | * compare all watchers |
| 1157 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1158 | w_l = new_entry->w_list; |
| 1159 | j = 0; |
| 1160 | while (w_l) { |
| 1161 | w = (struct ebt_u_watcher *)(w_l->w); |
| 1162 | w_l2 = u_e->w_list; |
| 1163 | while (w_l2 && strcmp(w_l2->w->u.name, w->w->u.name)) |
| 1164 | w_l2 = w_l2->next; |
| 1165 | if (!w_l2 || !w->compare(w->w, w_l2->w)) |
| 1166 | goto letscontinue; |
| 1167 | j++; |
| 1168 | w_l = w_l->next; |
| 1169 | } |
| 1170 | k = 0; |
| 1171 | w_l = u_e->w_list; |
| 1172 | while (w_l) { |
| 1173 | k++; |
| 1174 | w_l = w_l->next; |
| 1175 | } |
| 1176 | if (j != k) |
| 1177 | continue; |
| 1178 | if (strcmp(t->t->u.name, u_e->t->u.name)) |
| 1179 | continue; |
| 1180 | if (!t->compare(t->t, u_e->t)) |
| 1181 | continue; |
| 1182 | return i; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1183 | letscontinue: |
| 1184 | } |
| 1185 | return -1; |
| 1186 | } |
| 1187 | |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 1188 | /* execute command A or I */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1189 | static void add_rule(int rule_nr) |
| 1190 | { |
| 1191 | int i, j; |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1192 | struct ebt_u_entry **u_e; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1193 | unsigned short *cnt; |
| 1194 | struct ebt_u_match_list *m_l; |
| 1195 | struct ebt_u_watcher_list *w_l; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1196 | struct ebt_u_entries *entries = to_chain(), *entries2; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1197 | |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1198 | if (rule_nr <= 0) |
| 1199 | rule_nr += entries->nentries; |
| 1200 | else |
| 1201 | rule_nr--; |
| 1202 | if (rule_nr > entries->nentries || rule_nr < 0) |
| 1203 | print_error("The specified rule number is incorrect"); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1204 | /* |
| 1205 | * we're adding one rule |
| 1206 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1207 | replace.num_counters = replace.nentries; |
| 1208 | replace.nentries++; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1209 | entries->nentries++; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1210 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1211 | /* |
| 1212 | * handle counter stuff |
| 1213 | * +1 for CNT_END |
| 1214 | */ |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1215 | if ( !(replace.counterchanges = (unsigned short *) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1216 | malloc((replace.nentries + 1) * sizeof(unsigned short))) ) |
| 1217 | print_memory(); |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1218 | cnt = replace.counterchanges; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1219 | for (i = 0; i < replace.selected_hook; i++) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1220 | if (i < NF_BR_NUMHOOKS && !(replace.valid_hooks & (1 << i))) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1221 | continue; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1222 | entries2 = nr_to_chain(i); |
| 1223 | for (j = 0; j < entries2->nentries; j++) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1224 | *cnt = CNT_NORM; |
| 1225 | cnt++; |
| 1226 | } |
| 1227 | } |
| 1228 | for (i = 0; i < rule_nr; i++) { |
| 1229 | *cnt = CNT_NORM; |
| 1230 | cnt++; |
| 1231 | } |
| 1232 | *cnt = CNT_ADD; |
| 1233 | cnt++; |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1234 | while (cnt != replace.counterchanges + replace.nentries) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1235 | *cnt = CNT_NORM; |
| 1236 | cnt++; |
| 1237 | } |
| 1238 | *cnt = CNT_END; |
| 1239 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1240 | /* |
| 1241 | * go to the right position in the chain |
| 1242 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1243 | u_e = &entries->entries; |
| 1244 | for (i = 0; i < rule_nr; i++) |
| 1245 | u_e = &(*u_e)->next; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1246 | /* |
| 1247 | * insert the rule |
| 1248 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1249 | new_entry->next = *u_e; |
| 1250 | *u_e = new_entry; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1251 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1252 | /* |
| 1253 | * put the ebt_[match, watcher, target] pointers in place |
| 1254 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1255 | m_l = new_entry->m_list; |
| 1256 | while (m_l) { |
| 1257 | m_l->m = ((struct ebt_u_match *)m_l->m)->m; |
| 1258 | m_l = m_l->next; |
| 1259 | } |
| 1260 | w_l = new_entry->w_list; |
| 1261 | while (w_l) { |
| 1262 | w_l->w = ((struct ebt_u_watcher *)w_l->w)->w; |
| 1263 | w_l = w_l->next; |
| 1264 | } |
| 1265 | new_entry->t = ((struct ebt_u_target *)new_entry->t)->t; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1266 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1267 | /* |
| 1268 | * update the counter_offset of chains behind this one |
| 1269 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1270 | i = replace.selected_hook; |
| 1271 | while (1) { |
| 1272 | i++; |
| 1273 | entries = nr_to_chain(i); |
| 1274 | if (!entries) { |
| 1275 | if (i < NF_BR_NUMHOOKS) |
| 1276 | continue; |
| 1277 | else |
| 1278 | break; |
| 1279 | } else |
| 1280 | entries->counter_offset++; |
| 1281 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1284 | /* |
| 1285 | * execute command D |
| 1286 | */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1287 | static void delete_rule(int begin, int end) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1288 | { |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1289 | int j, lentmp = 0, nr_deletes; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1290 | unsigned short *cnt; |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1291 | struct ebt_u_entry **u_e, *u_e2; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1292 | struct ebt_u_entries *entries = to_chain(), *entries2; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1293 | |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1294 | if (begin < 0) { |
| 1295 | if (begin < entries->nentries) |
| 1296 | goto rule_error; |
| 1297 | begin += entries->nentries + 1; |
| 1298 | } |
| 1299 | if (end < 0) |
| 1300 | end += entries->nentries + 1; |
| 1301 | if (begin > end) |
| 1302 | rule_error: |
| 1303 | print_error("Sorry, wrong rule numbers"); |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1304 | if ((begin = check_rule_exists(begin)) == -1 || |
| 1305 | (end = check_rule_exists(end)) == -1) |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1306 | print_error("Sorry, rule does not exist"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1307 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1308 | /* |
| 1309 | * we're deleting rules |
| 1310 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1311 | replace.num_counters = replace.nentries; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1312 | nr_deletes = end - begin + 1; |
| 1313 | replace.nentries -= nr_deletes; |
| 1314 | entries->nentries -= nr_deletes; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1315 | |
| 1316 | if (replace.nentries) { |
| 1317 | for (j = 0; j < replace.selected_hook; j++) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1318 | if (j < NF_BR_NUMHOOKS && |
| 1319 | !(replace.valid_hooks & (1 << j))) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1320 | continue; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1321 | entries2 = nr_to_chain(j); |
| 1322 | lentmp += entries2->nentries; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1323 | } |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1324 | lentmp += begin; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1325 | /* |
| 1326 | * +1 for CNT_END |
| 1327 | */ |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1328 | if ( !(replace.counterchanges = (unsigned short *)malloc( |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1329 | (replace.num_counters + 1) * sizeof(unsigned short))) ) |
| 1330 | print_memory(); |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1331 | cnt = replace.counterchanges; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1332 | for (j = 0; j < lentmp; j++, cnt++) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1333 | *cnt = CNT_NORM; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1334 | for (j = 0; j < nr_deletes; j++, cnt++) |
| 1335 | *cnt = CNT_DEL; |
| 1336 | |
| 1337 | for (j = 0; j < replace.num_counters - lentmp - nr_deletes; |
| 1338 | j++, cnt++) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1339 | *cnt = CNT_NORM; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1340 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1341 | *cnt = CNT_END; |
| 1342 | } |
| 1343 | else |
| 1344 | replace.num_counters = 0; |
| 1345 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1346 | /* |
| 1347 | * go to the right position in the chain |
| 1348 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1349 | u_e = &entries->entries; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1350 | for (j = 0; j < begin; j++) |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1351 | u_e = &(*u_e)->next; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1352 | /* |
| 1353 | * remove the rules |
| 1354 | */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1355 | j = nr_deletes; |
| 1356 | while(j--) { |
| 1357 | u_e2 = *u_e; |
| 1358 | *u_e = (*u_e)->next; |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 1359 | /* free everything */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1360 | free_u_entry(u_e2); |
| 1361 | free(u_e2); |
| 1362 | } |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1363 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1364 | /* |
| 1365 | * update the counter_offset of chains behind this one |
| 1366 | */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1367 | j = replace.selected_hook; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1368 | while (1) { |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1369 | j++; |
| 1370 | entries = nr_to_chain(j); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1371 | if (!entries) { |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1372 | if (j < NF_BR_NUMHOOKS) |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1373 | continue; |
| 1374 | else |
| 1375 | break; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1376 | } else |
| 1377 | entries->counter_offset -= nr_deletes; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1378 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1381 | /* |
| 1382 | * execute command Z |
| 1383 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 1384 | static void zero_counters(int zerochain) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1385 | { |
| 1386 | |
| 1387 | if (zerochain == -1) { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1388 | /* |
| 1389 | * tell main() we don't update the counters |
| 1390 | * this results in tricking the kernel to zero its counters, |
| 1391 | * naively expecting userspace to update its counters. Muahahaha |
| 1392 | */ |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1393 | replace.counterchanges = NULL; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1394 | replace.num_counters = 0; |
| 1395 | } else { |
| 1396 | int i, j; |
| 1397 | unsigned short *cnt; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1398 | struct ebt_u_entries *entries = nr_to_chain(zerochain), *e2; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1399 | |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1400 | if (entries->nentries == 0) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1401 | exit(0); |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1402 | replace.counterchanges = (unsigned short *) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1403 | malloc((replace.nentries + 1) * sizeof(unsigned short)); |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1404 | if (!replace.counterchanges) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1405 | print_memory(); |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1406 | cnt = replace.counterchanges; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1407 | for (i = 0; i < zerochain; i++) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1408 | if (i < NF_BR_NUMHOOKS && |
| 1409 | !(replace.valid_hooks & (1 << i))) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1410 | continue; |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1411 | e2 = nr_to_chain(i); |
| 1412 | for (j = 0; j < e2->nentries; j++) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1413 | *cnt = CNT_NORM; |
| 1414 | cnt++; |
| 1415 | } |
| 1416 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1417 | for (i = 0; i < entries->nentries; i++) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1418 | *cnt = CNT_ZERO; |
| 1419 | cnt++; |
| 1420 | } |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1421 | while (cnt != replace.counterchanges + replace.nentries) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1422 | *cnt = CNT_NORM; |
| 1423 | cnt++; |
| 1424 | } |
| 1425 | *cnt = CNT_END; |
| 1426 | } |
| 1427 | } |
| 1428 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1429 | /* |
| 1430 | * Checks the type for validity and calls getethertypebynumber() |
| 1431 | */ |
| 1432 | struct ethertypeent *parseethertypebynumber(int type) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1433 | { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1434 | if (type < 1536) |
| 1435 | print_error("Ethernet protocols have values >= 0x0600"); |
| 1436 | if (type > 0xffff) |
| 1437 | print_error("Ethernet protocols have values <= 0xffff"); |
| 1438 | return getethertypebynumber(type); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1441 | /* |
| 1442 | * put the mac address into 6 (ETH_ALEN) bytes |
| 1443 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1444 | int getmac_and_mask(char *from, char *to, char *mask) |
| 1445 | { |
| 1446 | char *p; |
| 1447 | int i; |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 1448 | struct ether_addr *addr; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (strcasecmp(from, "Unicast") == 0) { |
| 1451 | memcpy(to, mac_type_unicast, ETH_ALEN); |
| 1452 | memcpy(mask, msk_type_unicast, ETH_ALEN); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | if (strcasecmp(from, "Multicast") == 0) { |
| 1456 | memcpy(to, mac_type_multicast, ETH_ALEN); |
| 1457 | memcpy(mask, msk_type_multicast, ETH_ALEN); |
| 1458 | return 0; |
| 1459 | } |
| 1460 | if (strcasecmp(from, "Broadcast") == 0) { |
| 1461 | memcpy(to, mac_type_broadcast, ETH_ALEN); |
| 1462 | memcpy(mask, msk_type_broadcast, ETH_ALEN); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | if ( (p = strrchr(from, '/')) != NULL) { |
| 1466 | *p = '\0'; |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 1467 | if (!(addr = ether_aton(p + 1))) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1468 | return -1; |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 1469 | memcpy(mask, addr, ETH_ALEN); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1470 | } else |
| 1471 | memset(mask, 0xff, ETH_ALEN); |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 1472 | if (!(addr = ether_aton(from))) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1473 | return -1; |
Bart De Schuymer | 4183041 | 2002-06-05 19:41:28 +0000 | [diff] [blame] | 1474 | memcpy(to, addr, ETH_ALEN); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1475 | for (i = 0; i < ETH_ALEN; i++) |
| 1476 | to[i] &= mask[i]; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1480 | /* |
| 1481 | * executes the final_check() function for all extensions used by the rule |
| 1482 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 1483 | static void do_final_checks(struct ebt_u_entry *e, struct ebt_u_entries *entries) |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 1484 | { |
| 1485 | struct ebt_u_match_list *m_l; |
| 1486 | struct ebt_u_watcher_list *w_l; |
| 1487 | struct ebt_u_target *t; |
| 1488 | struct ebt_u_match *m; |
| 1489 | struct ebt_u_watcher *w; |
| 1490 | |
| 1491 | m_l = e->m_list; |
| 1492 | w_l = e->w_list; |
| 1493 | while (m_l) { |
| 1494 | m = find_match(m_l->m->u.name); |
| 1495 | m->final_check(e, m_l->m, replace.name, |
| 1496 | entries->hook_mask, 1); |
| 1497 | m_l = m_l->next; |
| 1498 | } |
| 1499 | while (w_l) { |
| 1500 | w = find_watcher(w_l->w->u.name); |
| 1501 | w->final_check(e, w_l->w, replace.name, |
| 1502 | entries->hook_mask, 1); |
| 1503 | w_l = w_l->next; |
| 1504 | } |
| 1505 | t = find_target(e->t->u.name); |
| 1506 | t->final_check(e, e->t, replace.name, |
| 1507 | entries->hook_mask, 1); |
| 1508 | } |
| 1509 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1510 | /* |
| 1511 | * used for the -X command |
| 1512 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 1513 | static void check_for_references(int chain_nr) |
Bart De Schuymer | b909f9b | 2002-06-26 18:35:31 +0000 | [diff] [blame] | 1514 | { |
| 1515 | int i = -1, j; |
| 1516 | struct ebt_u_entries *entries; |
| 1517 | struct ebt_u_entry *e; |
| 1518 | |
| 1519 | while (1) { |
| 1520 | i++; |
| 1521 | entries = nr_to_chain(i); |
| 1522 | if (!entries) { |
| 1523 | if (i < NF_BR_NUMHOOKS) |
| 1524 | continue; |
| 1525 | else |
| 1526 | break; |
| 1527 | } |
| 1528 | e = entries->entries; |
| 1529 | j = 0; |
| 1530 | while (e) { |
Bart De Schuymer | c27432e | 2003-01-09 22:01:07 +0000 | [diff] [blame] | 1531 | int chain_jmp; |
| 1532 | |
Bart De Schuymer | b909f9b | 2002-06-26 18:35:31 +0000 | [diff] [blame] | 1533 | j++; |
| 1534 | if (strcmp(e->t->u.name, EBT_STANDARD_TARGET)) { |
| 1535 | e = e->next; |
| 1536 | continue; |
| 1537 | } |
Bart De Schuymer | c27432e | 2003-01-09 22:01:07 +0000 | [diff] [blame] | 1538 | chain_jmp = ((struct ebt_standard_target *)e->t)->verdict; |
| 1539 | if (chain_jmp == chain_nr) |
Bart De Schuymer | b909f9b | 2002-06-26 18:35:31 +0000 | [diff] [blame] | 1540 | print_error("Can't delete the chain, it's referenced " |
| 1541 | "in chain %s, rule %d", entries->name, j); |
Bart De Schuymer | c27432e | 2003-01-09 22:01:07 +0000 | [diff] [blame] | 1542 | /* adjust the chain jumps when necessary */ |
| 1543 | if (chain_jmp > chain_nr) |
| 1544 | ((struct ebt_standard_target *)e->t)->verdict--; |
Bart De Schuymer | b909f9b | 2002-06-26 18:35:31 +0000 | [diff] [blame] | 1545 | e = e->next; |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1550 | static int parse_delete_rule(const char *argv, int *rule_nr, int *rule_nr_end) |
| 1551 | { |
| 1552 | char *colon = strchr(argv, ':'), *buffer; |
| 1553 | |
| 1554 | if (colon) { |
| 1555 | *colon = '\0'; |
| 1556 | if (*(colon + 1) == '\0') |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1557 | *rule_nr_end = -1; /* until the last rule */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1558 | else { |
| 1559 | *rule_nr_end = strtol(colon + 1, &buffer, 10); |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1560 | if (*buffer != '\0' || *rule_nr_end == 0) |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1561 | return -1; |
| 1562 | } |
| 1563 | } |
| 1564 | if (colon == argv) |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1565 | *rule_nr = 1; /* beginning with the first rule */ |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1566 | else { |
| 1567 | *rule_nr = strtol(argv, &buffer, 10); |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1568 | if (*buffer != '\0' || *rule_nr == 0) |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1569 | return -1; |
| 1570 | } |
| 1571 | if (!colon) |
| 1572 | *rule_nr_end = *rule_nr; |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1573 | return 0; |
| 1574 | } |
| 1575 | |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 1576 | static int invert = 0; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1577 | int check_inverse(const char option[]) |
| 1578 | { |
| 1579 | if (strcmp(option, "!") == 0) { |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 1580 | if (invert == 1) |
| 1581 | print_error("double use of '!' not allowed"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1582 | optind++; |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 1583 | invert = 1; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1584 | return 1; |
| 1585 | } |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 1586 | return invert; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | void check_option(unsigned int *flags, unsigned int mask) |
| 1590 | { |
| 1591 | if (*flags & mask) |
| 1592 | print_error("Multiple use of same option not allowed"); |
| 1593 | *flags |= mask; |
| 1594 | } |
| 1595 | |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1596 | static void get_kernel_table(const char *modprobe) |
| 1597 | { |
| 1598 | if ( !(table = find_table(replace.name)) ) |
| 1599 | print_error("Bad table name"); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1600 | /* |
| 1601 | * get the kernel's information |
| 1602 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1603 | if (get_table(&replace)) { |
| 1604 | ebtables_insmod("ebtables", modprobe); |
| 1605 | if (get_table(&replace)) |
| 1606 | print_error("The kernel doesn't support the ebtables " |
| 1607 | "%s table", replace.name); |
| 1608 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1609 | /* |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 1610 | * when listing a table contained in a file, we don't demand that |
| 1611 | * the user knows the table's name |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1612 | */ |
Bart De Schuymer | 62a7fdb | 2002-08-24 21:01:21 +0000 | [diff] [blame] | 1613 | if ( !(table = find_table(replace.name)) ) |
| 1614 | print_error("Bad table name"); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Bart De Schuymer | c507514 | 2002-08-18 14:21:19 +0000 | [diff] [blame] | 1617 | #define print_if_l_error print_error("Interface name length must be less " \ |
| 1618 | "than %d", IFNAMSIZ) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1619 | #define OPT_COMMAND 0x01 |
| 1620 | #define OPT_TABLE 0x02 |
| 1621 | #define OPT_IN 0x04 |
| 1622 | #define OPT_OUT 0x08 |
| 1623 | #define OPT_JUMP 0x10 |
| 1624 | #define OPT_PROTOCOL 0x20 |
| 1625 | #define OPT_SOURCE 0x40 |
| 1626 | #define OPT_DEST 0x80 |
| 1627 | #define OPT_ZERO 0x100 |
| 1628 | #define OPT_LOGICALIN 0x200 |
| 1629 | #define OPT_LOGICALOUT 0x400 |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 1630 | /* the main thing */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1631 | int main(int argc, char *argv[]) |
| 1632 | { |
Bart De Schuymer | 923a573 | 2002-08-11 12:01:33 +0000 | [diff] [blame] | 1633 | char *buffer; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1634 | int c, i; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1635 | /* |
| 1636 | * this special one for the -Z option (we can have -Z <this> -L <that>) |
| 1637 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1638 | int zerochain = -1; |
Bart De Schuymer | f8f8f29 | 2002-06-25 15:43:57 +0000 | [diff] [blame] | 1639 | int policy = 0; |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1640 | int rule_nr = 0; /* used for -[D,I] */ |
| 1641 | int rule_nr_end = 0; /* used for -I */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1642 | struct ebt_u_target *t; |
| 1643 | struct ebt_u_match *m; |
| 1644 | struct ebt_u_watcher *w; |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1645 | struct ebt_u_match_list *m_l; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1646 | struct ebt_u_watcher_list *w_l; |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1647 | struct ebt_u_entries *entries; |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 1648 | const char *modprobe = NULL; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1649 | |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 1650 | opterr = 0; |
| 1651 | |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 1652 | replace.filename = getenv(ATOMIC_ENV_VARIABLE); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1653 | /* |
| 1654 | * initialize the table name, OPT_ flags, selected hook and command |
| 1655 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1656 | strcpy(replace.name, "filter"); |
| 1657 | replace.flags = 0; |
| 1658 | replace.selected_hook = -1; |
| 1659 | replace.command = 'h'; |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 1660 | replace.counterchanges = NULL; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1661 | |
| 1662 | new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry)); |
| 1663 | if (!new_entry) |
| 1664 | print_memory(); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1665 | /* |
| 1666 | * put some sane values in our new entry |
| 1667 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1668 | initialize_entry(new_entry); |
| 1669 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1670 | /* |
| 1671 | * The scenario induced by this loop makes that: |
| 1672 | * '-t' ,'-M' and --atomic (if specified) have to come |
| 1673 | * before '-A' and the like |
| 1674 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1675 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1676 | /* |
| 1677 | * getopt saves the day |
| 1678 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1679 | while ((c = getopt_long(argc, argv, |
Bart De Schuymer | aac3114 | 2002-08-11 11:57:52 +0000 | [diff] [blame] | 1680 | "-A:D:I:N:E:X:L::Z::F::P:Vhi:o:j:p:s:d:t:M:", ebt_options, NULL)) != -1) { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1681 | switch (c) { |
| 1682 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1683 | case 'A': /* add a rule */ |
| 1684 | case 'D': /* delete a rule */ |
| 1685 | case 'P': /* define policy */ |
| 1686 | case 'I': /* insert a rule */ |
| 1687 | case 'N': /* make a user defined chain */ |
| 1688 | case 'E': /* rename chain */ |
| 1689 | case 'X': /* delete chain */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1690 | replace.command = c; |
| 1691 | if (replace.flags & OPT_COMMAND) |
| 1692 | print_error("Multiple commands not allowed"); |
| 1693 | replace.flags |= OPT_COMMAND; |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1694 | get_kernel_table(modprobe); |
| 1695 | if (optarg[0] == '-' || !strcmp(optarg, "!")) |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1696 | print_error("No chain name specified"); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1697 | if (c == 'N') { |
| 1698 | struct ebt_u_chain_list *cl, **cl2; |
| 1699 | |
| 1700 | if (get_hooknr(optarg) != -1) |
| 1701 | print_error("Chain %s already exists", |
| 1702 | optarg); |
| 1703 | if (find_target(optarg)) |
| 1704 | print_error("Target with name %s exists" |
| 1705 | , optarg); |
| 1706 | if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN) |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1707 | print_error("Chain name length can't exceed %d", |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1708 | EBT_CHAIN_MAXNAMELEN - 1); |
| 1709 | cl = (struct ebt_u_chain_list *) |
| 1710 | malloc(sizeof(struct ebt_u_chain_list)); |
| 1711 | if (!cl) |
| 1712 | print_memory(); |
| 1713 | cl->next = NULL; |
| 1714 | cl->udc = (struct ebt_u_entries *) |
| 1715 | malloc(sizeof(struct ebt_u_entries)); |
| 1716 | if (!cl->udc) |
| 1717 | print_memory(); |
| 1718 | cl->udc->nentries = 0; |
| 1719 | cl->udc->policy = EBT_ACCEPT; |
| 1720 | cl->udc->counter_offset = replace.nentries; |
| 1721 | cl->udc->hook_mask = 0; |
| 1722 | strcpy(cl->udc->name, optarg); |
| 1723 | cl->udc->entries = NULL; |
| 1724 | cl->kernel_start = NULL; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1725 | /* |
| 1726 | * put the new chain at the end |
| 1727 | */ |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1728 | cl2 = &replace.udc; |
| 1729 | while (*cl2) |
| 1730 | cl2 = &((*cl2)->next); |
| 1731 | *cl2 = cl; |
| 1732 | break; |
| 1733 | } |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1734 | if ((replace.selected_hook = get_hooknr(optarg)) == -1) |
| 1735 | print_error("Chain %s doesn't exist", optarg); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1736 | if (c == 'E') { |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1737 | if (optind >= argc || argv[optind][0] == '-' || |
| 1738 | !strcmp(argv[optind], "!")) |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1739 | print_error("No new chain name specified"); |
| 1740 | if (strlen(argv[optind]) >= EBT_CHAIN_MAXNAMELEN) |
| 1741 | print_error("Chain name len can't exceed %d", |
| 1742 | EBT_CHAIN_MAXNAMELEN - 1); |
| 1743 | if (get_hooknr(argv[optind]) != -1) |
| 1744 | print_error("Chain %s already exists", |
| 1745 | argv[optind]); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1746 | if (find_target(argv[optind])) |
| 1747 | print_error("Target with name %s exists" |
| 1748 | , argv[optind]); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1749 | entries = to_chain(); |
| 1750 | strcpy(entries->name, argv[optind]); |
| 1751 | optind++; |
| 1752 | break; |
| 1753 | } |
| 1754 | if (c == 'X') { |
| 1755 | struct ebt_u_chain_list *cl, **cl2; |
| 1756 | |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1757 | if (replace.selected_hook < NF_BR_NUMHOOKS) |
| 1758 | print_error("You can't remove a standard chain"); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1759 | /* |
Bart De Schuymer | c27432e | 2003-01-09 22:01:07 +0000 | [diff] [blame] | 1760 | * if the chain is referenced, don't delete it, |
| 1761 | * also decrement jumps to a chain behind the |
| 1762 | * one we're deleting |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1763 | */ |
Bart De Schuymer | b909f9b | 2002-06-26 18:35:31 +0000 | [diff] [blame] | 1764 | check_for_references(replace.selected_hook - NF_BR_NUMHOOKS); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1765 | flush_chains(); |
| 1766 | entries = to_chain(); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1767 | cl2 = &(replace.udc); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1768 | while ((*cl2)->udc != entries) |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1769 | cl2 = &((*cl2)->next); |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1770 | cl = (*cl2); |
| 1771 | (*cl2) = (*cl2)->next; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1772 | free(cl->udc); |
| 1773 | free(cl); |
| 1774 | break; |
| 1775 | } |
| 1776 | |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1777 | if (c == 'D' && optind < argc && |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1778 | (argv[optind][0] != '-' || |
| 1779 | (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) { |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 1780 | if (parse_delete_rule(argv[optind], |
| 1781 | &rule_nr, &rule_nr_end)) |
| 1782 | print_error("Problem with the " |
| 1783 | "specified rule number(s)"); |
| 1784 | optind++; |
| 1785 | } |
| 1786 | if (c == 'I') { |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1787 | if (optind >= argc || (argv[optind][0] == '-' && |
| 1788 | (argv[optind][1] < '0' || argv[optind][1] > '9'))) |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1789 | print_error("No rulenr for -I" |
| 1790 | " specified"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1791 | rule_nr = strtol(argv[optind], &buffer, 10); |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 1792 | if (*buffer != '\0') |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1793 | print_error("Problem with the " |
| 1794 | "specified rule number"); |
| 1795 | optind++; |
| 1796 | } |
| 1797 | if (c == 'P') { |
| 1798 | if (optind >= argc) |
| 1799 | print_error("No policy specified"); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1800 | policy = 0; |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1801 | for (i = 0; i < NUM_STANDARD_TARGETS; i++) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1802 | if (!strcmp(argv[optind], |
| 1803 | standard_targets[i])) { |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1804 | policy = -i -1; |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1805 | if (policy == EBT_CONTINUE) |
| 1806 | policy = 0; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | } |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 1809 | if (policy == 0) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1810 | print_error("Wrong policy"); |
| 1811 | optind++; |
| 1812 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1813 | break; |
| 1814 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1815 | case 'L': /* list */ |
| 1816 | case 'F': /* flush */ |
| 1817 | case 'Z': /* zero counters */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1818 | if (c == 'Z') { |
| 1819 | if (replace.flags & OPT_ZERO) |
| 1820 | print_error("Multiple commands" |
| 1821 | " not allowed"); |
| 1822 | if ( (replace.flags & OPT_COMMAND && |
| 1823 | replace.command != 'L')) |
| 1824 | print_error("command -Z only allowed " |
| 1825 | "together with command -L"); |
| 1826 | replace.flags |= OPT_ZERO; |
| 1827 | } else { |
| 1828 | replace.command = c; |
| 1829 | if (replace.flags & OPT_COMMAND) |
| 1830 | print_error("Multiple commands" |
| 1831 | " not allowed"); |
| 1832 | replace.flags |= OPT_COMMAND; |
| 1833 | } |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1834 | get_kernel_table(modprobe); |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1835 | i = -1; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1836 | if (optarg) { |
| 1837 | if ( (i = get_hooknr(optarg)) == -1 ) |
| 1838 | print_error("Bad chain"); |
| 1839 | } else |
| 1840 | if (optind < argc && argv[optind][0] != '-') { |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 1841 | if ((i = get_hooknr(argv[optind])) == -1) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1842 | print_error("Bad chain"); |
| 1843 | optind++; |
| 1844 | } |
| 1845 | if (i != -1) { |
| 1846 | if (c == 'Z') |
| 1847 | zerochain = i; |
| 1848 | else |
| 1849 | replace.selected_hook = i; |
| 1850 | } |
| 1851 | break; |
| 1852 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1853 | case 'V': /* version */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1854 | replace.command = 'V'; |
| 1855 | if (replace.flags & OPT_COMMAND) |
| 1856 | print_error("Multiple commands not allowed"); |
Bart De Schuymer | 57a3f6a | 2003-04-01 16:59:33 +0000 | [diff] [blame] | 1857 | PRINT_VERSION; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1858 | exit(0); |
| 1859 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1860 | case 'M': /* modprobe */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1861 | if (replace.command != 'h') |
| 1862 | print_error("Please put the -M option earlier"); |
Bart De Schuymer | c853103 | 2002-06-14 21:55:29 +0000 | [diff] [blame] | 1863 | modprobe = optarg; |
| 1864 | break; |
| 1865 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1866 | case 'h': /* help */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1867 | if (replace.flags & OPT_COMMAND) |
| 1868 | print_error("Multiple commands not allowed"); |
| 1869 | replace.command = 'h'; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1870 | /* |
| 1871 | * All other arguments should be extension names |
| 1872 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1873 | while (optind < argc) { |
| 1874 | struct ebt_u_match *m; |
| 1875 | struct ebt_u_watcher *w; |
| 1876 | |
Bart De Schuymer | 9a0fbf2 | 2003-01-11 16:16:54 +0000 | [diff] [blame] | 1877 | if (!strcasecmp("list_extensions", |
| 1878 | argv[optind])) |
| 1879 | list_extensions(); |
| 1880 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1881 | if ((m = find_match(argv[optind]))) |
| 1882 | add_match(m); |
| 1883 | else if ((w = find_watcher(argv[optind]))) |
| 1884 | add_watcher(w); |
| 1885 | else { |
| 1886 | if (!(t = find_target(argv[optind]))) |
| 1887 | print_error("Extension %s " |
| 1888 | "not found", argv[optind]); |
| 1889 | if (replace.flags & OPT_JUMP) |
| 1890 | print_error("Sorry, you can " |
| 1891 | "only see help for one " |
| 1892 | "target extension each time"); |
| 1893 | replace.flags |= OPT_JUMP; |
| 1894 | new_entry->t = |
| 1895 | (struct ebt_entry_target *)t; |
| 1896 | } |
| 1897 | optind++; |
| 1898 | } |
| 1899 | break; |
| 1900 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1901 | case 't': /* table */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 1902 | if (replace.command != 'h') |
| 1903 | print_error("Please put the -t option first"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1904 | check_option(&replace.flags, OPT_TABLE); |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 1905 | if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1906 | print_error("Table name too long"); |
| 1907 | strcpy(replace.name, optarg); |
| 1908 | break; |
| 1909 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 1910 | case 'i': /* input interface */ |
| 1911 | case 2 : /* logical input interface */ |
| 1912 | case 'o': /* output interface */ |
| 1913 | case 3 : /* logical output interface */ |
| 1914 | case 'j': /* target */ |
| 1915 | case 'p': /* net family protocol */ |
| 1916 | case 's': /* source mac */ |
| 1917 | case 'd': /* destination mac */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1918 | if ((replace.flags & OPT_COMMAND) == 0) |
| 1919 | print_error("No command specified"); |
| 1920 | if ( replace.command != 'A' && |
| 1921 | replace.command != 'D' && replace.command != 'I') |
| 1922 | print_error("Command and option do not match"); |
| 1923 | if (c == 'i') { |
| 1924 | check_option(&replace.flags, OPT_IN); |
| 1925 | if (replace.selected_hook > 2 && |
| 1926 | replace.selected_hook < NF_BR_BROUTING) |
| 1927 | print_error("Use in-interface only in " |
| 1928 | "INPUT, FORWARD, PREROUTING and" |
| 1929 | "BROUTING chains"); |
| 1930 | if (check_inverse(optarg)) |
| 1931 | new_entry->invflags |= EBT_IIN; |
| 1932 | |
| 1933 | if (optind > argc) |
| 1934 | print_error("No in-interface " |
| 1935 | "specified"); |
| 1936 | if (strlen(argv[optind - 1]) >= IFNAMSIZ) |
Bart De Schuymer | c507514 | 2002-08-18 14:21:19 +0000 | [diff] [blame] | 1937 | print_if_l_error; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1938 | strcpy(new_entry->in, argv[optind - 1]); |
| 1939 | break; |
| 1940 | } |
| 1941 | if (c == 2) { |
| 1942 | check_option(&replace.flags, OPT_LOGICALIN); |
| 1943 | if (replace.selected_hook > 2 && |
| 1944 | replace.selected_hook < NF_BR_BROUTING) |
| 1945 | print_error("Use logical in-interface " |
| 1946 | "only in INPUT, FORWARD, " |
| 1947 | "PREROUTING and BROUTING chains"); |
| 1948 | if (check_inverse(optarg)) |
| 1949 | new_entry->invflags |= EBT_ILOGICALIN; |
| 1950 | |
| 1951 | if (optind > argc) |
| 1952 | print_error("No logical in-interface " |
| 1953 | "specified"); |
| 1954 | if (strlen(argv[optind - 1]) >= IFNAMSIZ) |
Bart De Schuymer | c507514 | 2002-08-18 14:21:19 +0000 | [diff] [blame] | 1955 | print_if_l_error; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1956 | strcpy(new_entry->logical_in, argv[optind - 1]); |
| 1957 | break; |
| 1958 | } |
| 1959 | if (c == 'o') { |
| 1960 | check_option(&replace.flags, OPT_OUT); |
| 1961 | if (replace.selected_hook < 2) |
| 1962 | print_error("Use out-interface only" |
| 1963 | " in OUTPUT, FORWARD and " |
| 1964 | "POSTROUTING chains"); |
| 1965 | if (check_inverse(optarg)) |
| 1966 | new_entry->invflags |= EBT_IOUT; |
| 1967 | |
| 1968 | if (optind > argc) |
| 1969 | print_error("No out-interface " |
| 1970 | "specified"); |
| 1971 | |
| 1972 | if (strlen(argv[optind - 1]) >= IFNAMSIZ) |
Bart De Schuymer | c507514 | 2002-08-18 14:21:19 +0000 | [diff] [blame] | 1973 | print_if_l_error; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1974 | strcpy(new_entry->out, argv[optind - 1]); |
| 1975 | break; |
| 1976 | } |
| 1977 | if (c == 3) { |
| 1978 | check_option(&replace.flags, OPT_LOGICALOUT); |
| 1979 | if (replace.selected_hook < 2) |
| 1980 | print_error("Use logical out-interface " |
| 1981 | "only in OUTPUT, FORWARD and " |
| 1982 | "POSTROUTING chains"); |
| 1983 | if (check_inverse(optarg)) |
| 1984 | new_entry->invflags |= EBT_ILOGICALOUT; |
| 1985 | |
| 1986 | if (optind > argc) |
| 1987 | print_error("No logical out-interface " |
| 1988 | "specified"); |
| 1989 | |
| 1990 | if (strlen(argv[optind - 1]) >= IFNAMSIZ) |
Bart De Schuymer | c507514 | 2002-08-18 14:21:19 +0000 | [diff] [blame] | 1991 | print_if_l_error; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1992 | strcpy(new_entry->logical_out, |
| 1993 | argv[optind - 1]); |
| 1994 | break; |
| 1995 | } |
| 1996 | if (c == 'j') { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1997 | check_option(&replace.flags, OPT_JUMP); |
| 1998 | for (i = 0; i < NUM_STANDARD_TARGETS; i++) |
| 1999 | if (!strcmp(optarg, |
| 2000 | standard_targets[i])) { |
| 2001 | t = find_target( |
| 2002 | EBT_STANDARD_TARGET); |
| 2003 | ((struct ebt_standard_target *) |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2004 | t->t)->verdict = -i - 1; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2005 | break; |
| 2006 | } |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2007 | if (-i - 1 == EBT_RETURN) { |
| 2008 | if (replace.selected_hook < NF_BR_NUMHOOKS) |
| 2009 | print_error("Return target" |
| 2010 | " only for user defined chains"); |
| 2011 | } |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2012 | if (i != NUM_STANDARD_TARGETS) |
| 2013 | break; |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 2014 | if ((i = get_hooknr(optarg)) != -1) { |
| 2015 | if (i < NF_BR_NUMHOOKS) |
| 2016 | print_error("don't jump" |
| 2017 | " to a standard chain"); |
| 2018 | t = find_target( |
| 2019 | EBT_STANDARD_TARGET); |
| 2020 | ((struct ebt_standard_target *) |
| 2021 | t->t)->verdict = i - NF_BR_NUMHOOKS; |
| 2022 | break; |
| 2023 | } |
| 2024 | else { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2025 | /* |
| 2026 | * must be an extension then |
| 2027 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2028 | struct ebt_u_target *t; |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 2029 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2030 | t = find_target(optarg); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2031 | /* |
| 2032 | * -j standard not allowed either |
| 2033 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2034 | if (!t || t == |
| 2035 | (struct ebt_u_target *)new_entry->t) |
| 2036 | print_error("Illegal target " |
| 2037 | "name"); |
| 2038 | new_entry->t = |
| 2039 | (struct ebt_entry_target *)t; |
| 2040 | } |
| 2041 | break; |
| 2042 | } |
| 2043 | if (c == 's') { |
| 2044 | check_option(&replace.flags, OPT_SOURCE); |
| 2045 | if (check_inverse(optarg)) |
| 2046 | new_entry->invflags |= EBT_ISOURCE; |
| 2047 | |
| 2048 | if (optind > argc) |
| 2049 | print_error("No source mac " |
| 2050 | "specified"); |
| 2051 | if (getmac_and_mask(argv[optind - 1], |
| 2052 | new_entry->sourcemac, new_entry->sourcemsk)) |
| 2053 | print_error("Problem with specified " |
| 2054 | "source mac"); |
| 2055 | new_entry->bitmask |= EBT_SOURCEMAC; |
| 2056 | break; |
| 2057 | } |
| 2058 | if (c == 'd') { |
| 2059 | check_option(&replace.flags, OPT_DEST); |
| 2060 | if (check_inverse(optarg)) |
| 2061 | new_entry->invflags |= EBT_IDEST; |
| 2062 | |
| 2063 | if (optind > argc) |
| 2064 | print_error("No destination mac " |
| 2065 | "specified"); |
| 2066 | if (getmac_and_mask(argv[optind - 1], |
| 2067 | new_entry->destmac, new_entry->destmsk)) |
| 2068 | print_error("Problem with specified " |
| 2069 | "destination mac"); |
| 2070 | new_entry->bitmask |= EBT_DESTMAC; |
| 2071 | break; |
| 2072 | } |
| 2073 | check_option(&replace.flags, OPT_PROTOCOL); |
| 2074 | if (check_inverse(optarg)) |
| 2075 | new_entry->invflags |= EBT_IPROTO; |
| 2076 | |
| 2077 | if (optind > argc) |
| 2078 | print_error("No protocol specified"); |
| 2079 | new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO); |
| 2080 | i = strtol(argv[optind - 1], &buffer, 16); |
| 2081 | if (*buffer == '\0' && (i < 0 || i > 0xFFFF)) |
| 2082 | print_error("Problem with the specified " |
| 2083 | "protocol"); |
| 2084 | new_entry->ethproto = i; |
Bart De Schuymer | 28bf6f6 | 2002-06-26 18:57:24 +0000 | [diff] [blame] | 2085 | if (*buffer != '\0') { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2086 | struct ethertypeent *ent; |
| 2087 | |
| 2088 | if (!strcasecmp(argv[optind - 1], "LENGTH")) { |
| 2089 | new_entry->bitmask |= EBT_802_3; |
| 2090 | break; |
| 2091 | } |
| 2092 | ent = getethertypebyname(argv[optind - 1]); |
| 2093 | if (!ent) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2094 | print_error("Problem with the specified" |
| 2095 | " protocol"); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2096 | new_entry->ethproto = ent->e_ethertype; |
Bart De Schuymer | 28bf6f6 | 2002-06-26 18:57:24 +0000 | [diff] [blame] | 2097 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2098 | if (new_entry->ethproto < 1536 && |
| 2099 | !(new_entry->bitmask & EBT_802_3)) |
| 2100 | print_error("Sorry, protocols have values above" |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 2101 | " or equal to 0x0600"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 2104 | case 4 : /* Lc */ |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2105 | check_option(&replace.flags, LIST_C); |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2106 | if (replace.command != 'L') |
| 2107 | print_error("Use --Lc with -L"); |
| 2108 | if (replace.flags & LIST_X) |
| 2109 | print_error("--Lx not compatible with --Lc"); |
| 2110 | replace.flags |= LIST_C; |
| 2111 | break; |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 2112 | case 5 : /* Ln */ |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2113 | check_option(&replace.flags, LIST_N); |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2114 | if (replace.command != 'L') |
| 2115 | print_error("Use --Ln with -L"); |
| 2116 | if (replace.flags & LIST_X) |
| 2117 | print_error("--Lx not compatible with --Ln"); |
| 2118 | replace.flags |= LIST_N; |
| 2119 | break; |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 2120 | case 6 : /* Lx */ |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2121 | check_option(&replace.flags, LIST_X); |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2122 | if (replace.command != 'L') |
| 2123 | print_error("Use --Lx with -L"); |
| 2124 | if (replace.flags & LIST_C) |
| 2125 | print_error("--Lx not compatible with --Lc"); |
| 2126 | if (replace.flags & LIST_N) |
| 2127 | print_error("--Lx not compatible with --Ln"); |
| 2128 | replace.flags |= LIST_X; |
| 2129 | break; |
Bart De Schuymer | 22d03a2 | 2003-05-03 20:28:22 +0000 | [diff] [blame] | 2130 | case 12 : /* Lmac2 */ |
| 2131 | check_option(&replace.flags, LIST_MAC2); |
| 2132 | if (replace.command != 'L') |
| 2133 | print_error("Use --Lmac2 with -L"); |
| 2134 | replace.flags |= LIST_MAC2; |
| 2135 | break; |
Bart De Schuymer | 9895a8e | 2003-01-11 10:14:24 +0000 | [diff] [blame] | 2136 | case 8 : /* atomic-commit */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2137 | replace.command = c; |
| 2138 | if (replace.flags & OPT_COMMAND) |
| 2139 | print_error("Multiple commands not allowed"); |
| 2140 | replace.flags |= OPT_COMMAND; |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2141 | if (!replace.filename) |
| 2142 | print_error("No atomic file specified"); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2143 | /* |
| 2144 | * get the information from the file |
| 2145 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2146 | get_table(&replace); |
Bart De Schuymer | 868bf64 | 2002-07-16 18:14:20 +0000 | [diff] [blame] | 2147 | if (replace.nentries) { |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 2148 | replace.counterchanges = (unsigned short *) |
Bart De Schuymer | 868bf64 | 2002-07-16 18:14:20 +0000 | [diff] [blame] | 2149 | malloc(sizeof(unsigned short) * (replace.nentries + 1)); |
| 2150 | for (i = 0; i < replace.nentries; i++) |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 2151 | replace.counterchanges[i] = CNT_NORM; |
| 2152 | replace.counterchanges[i] = CNT_END; |
Bart De Schuymer | 868bf64 | 2002-07-16 18:14:20 +0000 | [diff] [blame] | 2153 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2154 | /* |
| 2155 | * we don't want the kernel giving us its counters, they would |
| 2156 | * overwrite the counters extracted from the file |
| 2157 | */ |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 2158 | replace.num_counters = 0; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2159 | /* |
| 2160 | * make sure the table will be written to the kernel |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2161 | * possible memory leak here |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2162 | */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2163 | replace.filename = NULL; |
Bart De Schuymer | c5c3c2d | 2002-08-16 19:48:03 +0000 | [diff] [blame] | 2164 | ebtables_insmod("ebtables", modprobe); |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2165 | break; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2166 | case 7 : /* atomic-init */ |
| 2167 | case 10: /* atomic-save */ |
| 2168 | case 11: /* init-table */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2169 | replace.command = c; |
| 2170 | if (replace.flags & OPT_COMMAND) |
| 2171 | print_error("Multiple commands not allowed"); |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2172 | if (c != 11 && !replace.filename) |
| 2173 | print_error("No atomic file specified"); |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2174 | replace.flags |= OPT_COMMAND; |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2175 | { |
| 2176 | char *tmp = replace.filename; |
| 2177 | |
| 2178 | tmp = replace.filename; |
| 2179 | /* get the kernel table */ |
| 2180 | replace.filename = NULL; |
| 2181 | get_kernel_table(modprobe); |
| 2182 | replace.filename = tmp; |
| 2183 | } |
Bart De Schuymer | a8d920b | 2002-07-16 18:30:44 +0000 | [diff] [blame] | 2184 | if (replace.nentries) { |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 2185 | replace.counterchanges = (unsigned short *) |
Bart De Schuymer | 5f16dc7 | 2002-07-16 18:08:36 +0000 | [diff] [blame] | 2186 | malloc(sizeof(unsigned short) * (replace.nentries + 1)); |
| 2187 | for (i = 0; i < replace.nentries; i++) |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 2188 | replace.counterchanges[i] = CNT_NORM; |
| 2189 | replace.counterchanges[i] = CNT_END; |
Bart De Schuymer | 5f16dc7 | 2002-07-16 18:08:36 +0000 | [diff] [blame] | 2190 | } |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2191 | break; |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2192 | case 9 : /* atomic */ |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2193 | if (replace.flags & OPT_COMMAND) |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 2194 | print_error("--atomic has to come before" |
| 2195 | " the command"); |
Bart De Schuymer | 5885b36 | 2002-12-03 20:51:36 +0000 | [diff] [blame] | 2196 | /* another possible memory leak here */ |
Bart De Schuymer | 6242374 | 2002-07-14 19:06:20 +0000 | [diff] [blame] | 2197 | replace.filename = (char *)malloc(strlen(optarg) + 1); |
| 2198 | strcpy(replace.filename, optarg); |
| 2199 | break; |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 2200 | case 1 : |
| 2201 | if (!strcmp(optarg, "!")) |
| 2202 | check_inverse(optarg); |
| 2203 | else |
| 2204 | print_error("Bad argument : %s", optarg); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2205 | /* |
| 2206 | * check_inverse() did optind++ |
| 2207 | */ |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 2208 | optind--; |
| 2209 | continue; |
Bart De Schuymer | 9af14f9 | 2002-07-10 20:49:10 +0000 | [diff] [blame] | 2210 | default: |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2211 | /* |
| 2212 | * is it a target option? |
| 2213 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2214 | t = (struct ebt_u_target *)new_entry->t; |
| 2215 | if ((t->parse(c - t->option_offset, argv, argc, |
| 2216 | new_entry, &t->flags, &t->t))) |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 2217 | goto check_extension; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2218 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2219 | /* |
| 2220 | * is it a match_option? |
| 2221 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2222 | for (m = matches; m; m = m->next) |
| 2223 | if (m->parse(c - m->option_offset, argv, |
| 2224 | argc, new_entry, &m->flags, &m->m)) |
| 2225 | break; |
| 2226 | |
| 2227 | if (m != NULL) { |
| 2228 | if (m->used == 0) |
| 2229 | add_match(m); |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 2230 | goto check_extension; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2233 | /* |
| 2234 | * is it a watcher option? |
| 2235 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2236 | for (w = watchers; w; w = w->next) |
| 2237 | if (w->parse(c-w->option_offset, argv, |
| 2238 | argc, new_entry, &w->flags, &w->w)) |
| 2239 | break; |
| 2240 | |
| 2241 | if (w == NULL) |
| 2242 | print_error("Unknown argument"); |
Bart De Schuymer | dd5594b | 2002-06-26 18:05:20 +0000 | [diff] [blame] | 2243 | if (w->used == 0) |
| 2244 | add_watcher(w); |
| 2245 | check_extension: |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2246 | if (replace.command != 'A' && replace.command != 'I' && |
| 2247 | replace.command != 'D') |
Bart De Schuymer | c56a31a | 2002-07-25 08:16:08 +0000 | [diff] [blame] | 2248 | print_error("Extensions only for -A, -I and -D"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2249 | } |
Bart De Schuymer | a615b96 | 2002-11-03 14:54:09 +0000 | [diff] [blame] | 2250 | invert = 0; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Bart De Schuymer | 25c741d | 2002-06-23 18:54:34 +0000 | [diff] [blame] | 2253 | if ( !table && !(table = find_table(replace.name)) ) |
| 2254 | print_error("Bad table name"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2255 | |
| 2256 | if ( (replace.flags & OPT_COMMAND) && replace.command != 'L' && |
| 2257 | replace.flags & OPT_ZERO ) |
| 2258 | print_error("Command -Z only allowed together with command -L"); |
| 2259 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2260 | /* |
| 2261 | * do this after parsing everything, so we can print specific info |
| 2262 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2263 | if (replace.command == 'h' && !(replace.flags & OPT_ZERO)) |
| 2264 | print_help(); |
| 2265 | |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2266 | /* |
| 2267 | * do the final checks |
| 2268 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2269 | if (replace.command == 'A' || replace.command == 'I' || |
| 2270 | replace.command == 'D') { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2271 | /* |
| 2272 | * this will put the hook_mask right for the chains |
| 2273 | */ |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2274 | check_for_loops(); |
| 2275 | entries = to_chain(); |
| 2276 | m_l = new_entry->m_list; |
| 2277 | w_l = new_entry->w_list; |
| 2278 | t = (struct ebt_u_target *)new_entry->t; |
| 2279 | while (m_l) { |
| 2280 | m = (struct ebt_u_match *)(m_l->m); |
| 2281 | m->final_check(new_entry, m->m, replace.name, |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2282 | entries->hook_mask, 0); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2283 | m_l = m_l->next; |
| 2284 | } |
| 2285 | while (w_l) { |
| 2286 | w = (struct ebt_u_watcher *)(w_l->w); |
| 2287 | w->final_check(new_entry, w->w, replace.name, |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2288 | entries->hook_mask, 0); |
Bart De Schuymer | 60332e0 | 2002-06-23 08:01:47 +0000 | [diff] [blame] | 2289 | w_l = w_l->next; |
| 2290 | } |
| 2291 | t->final_check(new_entry, t->t, replace.name, |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2292 | entries->hook_mask, 0); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2293 | } |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2294 | /* |
| 2295 | * so, the extensions can work with the host endian |
| 2296 | * the kernel does not have to do this ofcourse |
| 2297 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2298 | new_entry->ethproto = htons(new_entry->ethproto); |
| 2299 | |
Bart De Schuymer | c34f467 | 2002-08-18 18:03:52 +0000 | [diff] [blame] | 2300 | if (replace.command == 'P') { |
| 2301 | if (replace.selected_hook < NF_BR_NUMHOOKS && |
| 2302 | policy == EBT_RETURN) |
| 2303 | print_error("Policy RETURN only allowed for user " |
| 2304 | "defined chains"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2305 | change_policy(policy); |
Bart De Schuymer | c34f467 | 2002-08-18 18:03:52 +0000 | [diff] [blame] | 2306 | } else if (replace.command == 'L') { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2307 | list_rules(); |
| 2308 | if (replace.flags & OPT_ZERO) |
| 2309 | zero_counters(zerochain); |
| 2310 | else |
| 2311 | exit(0); |
| 2312 | } |
| 2313 | if (replace.flags & OPT_ZERO) |
| 2314 | zero_counters(zerochain); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 2315 | else if (replace.command == 'F') { |
| 2316 | if (flush_chains() == -1) |
| 2317 | exit(0); |
| 2318 | } else if (replace.command == 'A' || replace.command == 'I') { |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2319 | add_rule(rule_nr); |
Bart De Schuymer | 1ab4156 | 2002-06-23 17:09:54 +0000 | [diff] [blame] | 2320 | check_for_loops(); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2321 | /* |
| 2322 | * do the final_check(), for all entries |
| 2323 | * needed when adding a rule that has a chain target |
| 2324 | */ |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2325 | i = -1; |
| 2326 | while (1) { |
| 2327 | struct ebt_u_entry *e; |
| 2328 | |
| 2329 | i++; |
| 2330 | entries = nr_to_chain(i); |
| 2331 | if (!entries) { |
| 2332 | if (i < NF_BR_NUMHOOKS) |
| 2333 | continue; |
| 2334 | else |
| 2335 | break; |
| 2336 | } |
| 2337 | e = entries->entries; |
| 2338 | while (e) { |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2339 | /* |
| 2340 | * userspace extensions use host endian |
| 2341 | */ |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 2342 | e->ethproto = ntohs(e->ethproto); |
| 2343 | do_final_checks(e, entries); |
| 2344 | e->ethproto = htons(e->ethproto); |
| 2345 | e = e->next; |
| 2346 | } |
| 2347 | } |
Bart De Schuymer | c3b97f5 | 2003-04-17 17:03:49 +0000 | [diff] [blame] | 2348 | } else if (replace.command == 'D') |
Bart De Schuymer | cc44005 | 2002-11-06 21:10:33 +0000 | [diff] [blame] | 2349 | delete_rule(rule_nr, rule_nr_end); |
Bart De Schuymer | c7bfa27 | 2002-11-20 19:40:13 +0000 | [diff] [blame] | 2350 | /* |
| 2351 | * commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save, |
| 2352 | * --init-table fall through |
| 2353 | */ |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (table->check) |
| 2356 | table->check(&replace); |
| 2357 | |
| 2358 | deliver_table(&replace); |
| 2359 | |
Bart De Schuymer | ed05343 | 2002-07-21 19:35:39 +0000 | [diff] [blame] | 2360 | if (replace.counterchanges) |
| 2361 | deliver_counters(&replace); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 2362 | return 0; |
| 2363 | } |