net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 1 | /* |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2 | * m_ipt.c iptables based targets |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 3 | * utilities mostly ripped from iptables <duh, its the linux way> |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can distribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 10 | * Authors: J Hadi Salim (hadi@cyberus.ca) |
Stephen Hemminger | de539ec | 2007-03-06 13:03:19 -0800 | [diff] [blame] | 11 | */ |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 12 | |
| 13 | #include <syslog.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <netinet/in.h> |
| 16 | #include <arpa/inet.h> |
| 17 | #include <iptables.h> |
Stephen Hemminger | ece02ea | 2007-12-10 09:40:45 -0800 | [diff] [blame] | 18 | #include <linux/netfilter.h> |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 19 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 20 | #include "utils.h" |
| 21 | #include "tc_util.h" |
| 22 | #include <linux/tc_act/tc_ipt.h> |
| 23 | #include <stdio.h> |
| 24 | #include <dlfcn.h> |
| 25 | #include <getopt.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <netdb.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <ctype.h> |
| 31 | #include <stdarg.h> |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <sys/wait.h> |
| 35 | |
Mike Frysinger | 95dd595 | 2007-03-05 17:50:49 -0800 | [diff] [blame] | 36 | static const char *pname = "tc-ipt"; |
| 37 | static const char *tname = "mangle"; |
| 38 | static const char *pversion = "0.1"; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 39 | |
| 40 | static const char *ipthooks[] = { |
| 41 | "NF_IP_PRE_ROUTING", |
| 42 | "NF_IP_LOCAL_IN", |
| 43 | "NF_IP_FORWARD", |
| 44 | "NF_IP_LOCAL_OUT", |
| 45 | "NF_IP_POST_ROUTING", |
| 46 | }; |
| 47 | |
| 48 | static struct option original_opts[] = { |
| 49 | {"jump", 1, 0, 'j'}, |
| 50 | {0, 0, 0, 0} |
| 51 | }; |
| 52 | |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 53 | static struct xtables_target *t_list; |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 54 | static struct option *opts = original_opts; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 55 | static unsigned int global_option_offset; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 56 | #define OPTION_OFFSET 256 |
| 57 | |
Stephen Hemminger | de539ec | 2007-03-06 13:03:19 -0800 | [diff] [blame] | 58 | char *lib_dir; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 59 | |
| 60 | void |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 61 | xtables_register_target(struct xtables_target *me) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 62 | { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 63 | me->next = t_list; |
| 64 | t_list = me; |
| 65 | |
| 66 | } |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 67 | |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 68 | static void exit_tryhelp(int status) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 69 | { |
| 70 | fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n", |
| 71 | pname, pname); |
| 72 | exit(status); |
| 73 | } |
| 74 | |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 75 | static void exit_error(enum xtables_exittype status, char *msg, ...) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 76 | { |
| 77 | va_list args; |
| 78 | |
| 79 | va_start(args, msg); |
| 80 | fprintf(stderr, "%s v%s: ", pname, pversion); |
| 81 | vfprintf(stderr, msg, args); |
| 82 | va_end(args); |
| 83 | fprintf(stderr, "\n"); |
| 84 | if (status == PARAMETER_PROBLEM) |
| 85 | exit_tryhelp(status); |
| 86 | if (status == VERSION_PROBLEM) |
| 87 | fprintf(stderr, |
| 88 | "Perhaps iptables or your kernel needs to be upgraded.\n"); |
| 89 | exit(status); |
| 90 | } |
| 91 | |
| 92 | /* stolen from iptables 1.2.11 |
| 93 | They should really have them as a library so i can link to them |
| 94 | Email them next time i remember |
| 95 | */ |
| 96 | |
Denys Fedoryschenko | a589dcd | 2009-02-07 08:49:32 -0500 | [diff] [blame] | 97 | static void free_opts(struct option *local_opts) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 98 | { |
Denys Fedoryschenko | a589dcd | 2009-02-07 08:49:32 -0500 | [diff] [blame] | 99 | if (local_opts != original_opts) { |
| 100 | free(local_opts); |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 101 | opts = original_opts; |
| 102 | global_option_offset = 0; |
| 103 | } |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static struct option * |
| 107 | merge_options(struct option *oldopts, const struct option *newopts, |
| 108 | unsigned int *option_offset) |
| 109 | { |
| 110 | struct option *merge; |
| 111 | unsigned int num_old, num_new, i; |
| 112 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 113 | for (num_old = 0; oldopts[num_old].name; num_old++); |
| 114 | for (num_new = 0; newopts[num_new].name; num_new++); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 115 | |
| 116 | *option_offset = global_option_offset + OPTION_OFFSET; |
| 117 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 118 | merge = malloc(sizeof(struct option) * (num_new + num_old + 1)); |
| 119 | memcpy(merge, oldopts, num_old * sizeof(struct option)); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 120 | for (i = 0; i < num_new; i++) { |
| 121 | merge[num_old + i] = newopts[i]; |
| 122 | merge[num_old + i].val += *option_offset; |
| 123 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 124 | memset(merge + num_old + num_new, 0, sizeof(struct option)); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 125 | |
| 126 | return merge; |
| 127 | } |
| 128 | |
| 129 | static void * |
| 130 | fw_calloc(size_t count, size_t size) |
| 131 | { |
| 132 | void *p; |
| 133 | |
| 134 | if ((p = (void *) calloc(count, size)) == NULL) { |
| 135 | perror("iptables: calloc failed"); |
| 136 | exit(1); |
| 137 | } |
| 138 | return p; |
| 139 | } |
| 140 | |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 141 | static struct xtables_target * |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 142 | find_t(char *name) |
| 143 | { |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 144 | struct xtables_target *m; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 145 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 146 | for (m = t_list; m; m = m->next) { |
| 147 | if (strcmp(m->name, name) == 0) |
| 148 | return m; |
| 149 | } |
| 150 | |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 154 | static struct xtables_target * |
Stephen Hemminger | de539ec | 2007-03-06 13:03:19 -0800 | [diff] [blame] | 155 | get_target_name(const char *name) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 156 | { |
| 157 | void *handle; |
| 158 | char *error; |
| 159 | char *new_name, *lname; |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 160 | struct xtables_target *m; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 161 | char path[strlen(lib_dir) + sizeof("/libipt_.so") + strlen(name)]; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 162 | |
Mike Frysinger | f2e27cf | 2009-11-06 06:09:22 -0500 | [diff] [blame] | 163 | #ifdef NO_SHARED_LIBS |
| 164 | return NULL; |
| 165 | #endif |
| 166 | |
Phil Sutter | f89bb02 | 2016-07-18 16:48:43 +0200 | [diff] [blame] | 167 | new_name = calloc(1, strlen(name) + 1); |
| 168 | lname = calloc(1, strlen(name) + 1); |
| 169 | if (!new_name) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 170 | exit_error(PARAMETER_PROBLEM, "get_target_name"); |
Phil Sutter | f89bb02 | 2016-07-18 16:48:43 +0200 | [diff] [blame] | 171 | if (!lname) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 172 | exit_error(PARAMETER_PROBLEM, "get_target_name"); |
| 173 | |
| 174 | strcpy(new_name, name); |
| 175 | strcpy(lname, name); |
| 176 | |
| 177 | if (isupper(lname[0])) { |
| 178 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 179 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 180 | for (i = 0; i < strlen(name); i++) { |
| 181 | lname[i] = tolower(lname[i]); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (islower(new_name[0])) { |
| 186 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 187 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 188 | for (i = 0; i < strlen(new_name); i++) { |
| 189 | new_name[i] = toupper(new_name[i]); |
| 190 | } |
| 191 | } |
| 192 | |
Denys Fedoryshchenko | 53c0178 | 2007-12-24 11:51:11 -0500 | [diff] [blame] | 193 | /* try libxt_xx first */ |
| 194 | sprintf(path, "%s/libxt_%s.so", lib_dir, new_name); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 195 | handle = dlopen(path, RTLD_LAZY); |
| 196 | if (!handle) { |
Denys Fedoryshchenko | 53c0178 | 2007-12-24 11:51:11 -0500 | [diff] [blame] | 197 | /* try libipt_xx next */ |
| 198 | sprintf(path, "%s/libipt_%s.so", lib_dir, new_name); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 199 | handle = dlopen(path, RTLD_LAZY); |
Denys Fedoryshchenko | 53c0178 | 2007-12-24 11:51:11 -0500 | [diff] [blame] | 200 | |
| 201 | if (!handle) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 202 | sprintf(path, "%s/libxt_%s.so", lib_dir, lname); |
Denys Fedoryshchenko | 53c0178 | 2007-12-24 11:51:11 -0500 | [diff] [blame] | 203 | handle = dlopen(path, RTLD_LAZY); |
| 204 | } |
| 205 | |
| 206 | if (!handle) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 207 | sprintf(path, "%s/libipt_%s.so", lib_dir, lname); |
Denys Fedoryshchenko | 53c0178 | 2007-12-24 11:51:11 -0500 | [diff] [blame] | 208 | handle = dlopen(path, RTLD_LAZY); |
| 209 | } |
| 210 | /* ok, lets give up .. */ |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 211 | if (!handle) { |
| 212 | fputs(dlerror(), stderr); |
| 213 | printf("\n"); |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 214 | free(new_name); |
Thomas Jarosch | 1a6543c | 2011-10-03 05:23:30 +0000 | [diff] [blame] | 215 | free(lname); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | m = dlsym(handle, new_name); |
| 221 | if ((error = dlerror()) != NULL) { |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 222 | m = (struct xtables_target *) dlsym(handle, lname); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 223 | if ((error = dlerror()) != NULL) { |
| 224 | m = find_t(new_name); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 225 | if (m == NULL) { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 226 | m = find_t(lname); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 227 | if (m == NULL) { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 228 | fputs(error, stderr); |
| 229 | fprintf(stderr, "\n"); |
| 230 | dlclose(handle); |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 231 | free(new_name); |
Thomas Jarosch | 1a6543c | 2011-10-03 05:23:30 +0000 | [diff] [blame] | 232 | free(lname); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 239 | free(new_name); |
Thomas Jarosch | 1a6543c | 2011-10-03 05:23:30 +0000 | [diff] [blame] | 240 | free(lname); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 241 | return m; |
| 242 | } |
| 243 | |
net[shemminger]!shemminger | 894b1c6 | 2005-03-30 18:19:55 +0000 | [diff] [blame] | 244 | static void set_revision(char *name, u_int8_t revision) |
| 245 | { |
| 246 | /* Old kernel sources don't have ".revision" field, |
| 247 | * but we stole a byte from name. */ |
| 248 | name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0'; |
| 249 | name[IPT_FUNCTION_MAXNAMELEN - 1] = revision; |
| 250 | } |
| 251 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 252 | /* |
net[shemminger]!shemminger | 894b1c6 | 2005-03-30 18:19:55 +0000 | [diff] [blame] | 253 | * we may need to check for version mismatch |
| 254 | */ |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 255 | static int build_st(struct xtables_target *target, struct ipt_entry_target *t) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 256 | { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 257 | if (target) { |
| 258 | size_t size; |
| 259 | |
| 260 | size = |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 261 | XT_ALIGN(sizeof(struct ipt_entry_target)) + target->size; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 262 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 263 | if (t == NULL) { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 264 | target->t = fw_calloc(1, size); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 265 | target->t->u.target_size = size; |
net[shemminger]!shemminger | 894b1c6 | 2005-03-30 18:19:55 +0000 | [diff] [blame] | 266 | |
| 267 | if (target->init != NULL) |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 268 | target->init(target->t); |
net[shemminger]!shemminger | 894b1c6 | 2005-03-30 18:19:55 +0000 | [diff] [blame] | 269 | set_revision(target->t->u.user.name, target->revision); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 270 | } else { |
| 271 | target->t = t; |
| 272 | } |
| 273 | strcpy(target->t->u.user.name, target->name); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | return -1; |
| 278 | } |
| 279 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 280 | static int parse_ipt(struct action_util *a, int *argc_p, |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 281 | char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 282 | { |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 283 | struct xtables_target *m = NULL; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 284 | struct ipt_entry fw; |
| 285 | struct rtattr *tail; |
| 286 | int c; |
| 287 | int rargc = *argc_p; |
| 288 | char **argv = *argv_p; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 289 | int argc = 0, iargc = 0; |
| 290 | char k[16]; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 291 | int size = 0; |
| 292 | int iok = 0, ok = 0; |
| 293 | __u32 hook = 0, index = 0; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 294 | |
Stephen Hemminger | de539ec | 2007-03-06 13:03:19 -0800 | [diff] [blame] | 295 | lib_dir = getenv("IPTABLES_LIB_DIR"); |
| 296 | if (!lib_dir) |
| 297 | lib_dir = IPT_LIB_DIR; |
| 298 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 299 | { |
| 300 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 301 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 302 | for (i = 0; i < rargc; i++) { |
| 303 | if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | iargc = argc = i; |
| 308 | } |
| 309 | |
| 310 | if (argc <= 2) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 311 | fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 315 | while (1) { |
| 316 | c = getopt_long(argc, argv, "j:", opts, NULL); |
| 317 | if (c == -1) |
| 318 | break; |
| 319 | switch (c) { |
| 320 | case 'j': |
| 321 | m = get_target_name(optarg); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 322 | if (m != NULL) { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 323 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 324 | if (build_st(m, NULL) < 0) { |
| 325 | printf(" %s error\n", m->name); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | opts = |
| 329 | merge_options(opts, m->extra_opts, |
| 330 | &m->option_offset); |
| 331 | } else { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 332 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | ok++; |
| 336 | break; |
| 337 | |
| 338 | default: |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 339 | memset(&fw, 0, sizeof(fw)); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 340 | if (m) { |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 341 | m->parse(c - m->option_offset, argv, 0, |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 342 | &m->tflags, NULL, &m->t); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 343 | } else { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 344 | fprintf(stderr, " failed to find target %s\n\n", optarg); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 345 | return -1; |
| 346 | |
| 347 | } |
| 348 | ok++; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (iargc > optind) { |
| 355 | if (matches(argv[optind], "index") == 0) { |
| 356 | if (get_u32(&index, argv[optind + 1], 10)) { |
| 357 | fprintf(stderr, "Illegal \"index\"\n"); |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 358 | free_opts(opts); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 359 | return -1; |
| 360 | } |
| 361 | iok++; |
| 362 | |
| 363 | optind += 2; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (!ok && !iok) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 368 | fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 369 | return -1; |
| 370 | } |
| 371 | |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 372 | /* check that we passed the correct parameters to the target */ |
| 373 | if (m) |
| 374 | m->final_check(m->tflags); |
| 375 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 376 | { |
| 377 | struct tcmsg *t = NLMSG_DATA(n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 378 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 379 | if (t->tcm_parent != TC_H_ROOT |
| 380 | && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { |
| 381 | hook = NF_IP_PRE_ROUTING; |
| 382 | } else { |
| 383 | hook = NF_IP_POST_ROUTING; |
| 384 | } |
| 385 | } |
| 386 | |
2!tgraf | fc78a8e | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 387 | tail = NLMSG_TAIL(n); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 388 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 389 | fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); |
| 390 | fprintf(stdout, "\ttarget: "); |
| 391 | |
| 392 | if (m) |
| 393 | m->print(NULL, m->t, 0); |
| 394 | fprintf(stdout, " index %d\n", index); |
| 395 | |
| 396 | if (strlen(tname) > 16) { |
| 397 | size = 16; |
| 398 | k[15] = 0; |
| 399 | } else { |
| 400 | size = 1 + strlen(tname); |
| 401 | } |
| 402 | strncpy(k, tname, size); |
| 403 | |
| 404 | addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); |
| 405 | addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); |
| 406 | addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); |
| 407 | if (m) |
| 408 | addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); |
2!tgraf | fc78a8e | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 409 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 410 | |
| 411 | argc -= optind; |
| 412 | argv += optind; |
| 413 | *argc_p = rargc - iargc; |
| 414 | *argv_p = argv; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 415 | |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 416 | optind = 0; |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 417 | free_opts(opts); |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 418 | /* Clear flags if target will be used again */ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 419 | m->tflags = 0; |
| 420 | m->used = 0; |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 421 | /* Free allocated memory */ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 422 | if (m->t) |
| 423 | free(m->t); |
Denys Fedoryshchenko | 6e34e7d | 2009-01-06 19:41:50 -0800 | [diff] [blame] | 424 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | |
| 428 | } |
| 429 | |
| 430 | static int |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 431 | print_ipt(struct action_util *au, FILE * f, struct rtattr *arg) |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 432 | { |
| 433 | struct rtattr *tb[TCA_IPT_MAX + 1]; |
| 434 | struct ipt_entry_target *t = NULL; |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 435 | |
| 436 | if (arg == NULL) |
| 437 | return -1; |
| 438 | |
Patrick McHardy | c6ab5b8 | 2007-06-20 01:37:53 +0200 | [diff] [blame] | 439 | lib_dir = getenv("IPTABLES_LIB_DIR"); |
| 440 | if (!lib_dir) |
| 441 | lib_dir = IPT_LIB_DIR; |
| 442 | |
2!tgraf | 7893400 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 443 | parse_rtattr_nested(tb, TCA_IPT_MAX, arg); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 444 | |
| 445 | if (tb[TCA_IPT_TABLE] == NULL) { |
| 446 | fprintf(f, "[NULL ipt table name ] assuming mangle "); |
| 447 | } else { |
| 448 | fprintf(f, "tablename: %s ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 449 | rta_getattr_str(tb[TCA_IPT_TABLE])); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (tb[TCA_IPT_HOOK] == NULL) { |
| 453 | fprintf(f, "[NULL ipt hook name ]\n "); |
| 454 | return -1; |
| 455 | } else { |
| 456 | __u32 hook; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 457 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 458 | hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 459 | fprintf(f, " hook: %s\n", ipthooks[hook]); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (tb[TCA_IPT_TARG] == NULL) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 463 | fprintf(f, "\t[NULL ipt target parameters ]\n"); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 464 | return -1; |
| 465 | } else { |
Alexander Aring | 9b32f89 | 2016-05-29 20:27:13 +0200 | [diff] [blame] | 466 | struct xtables_target *m = NULL; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 467 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 468 | t = RTA_DATA(tb[TCA_IPT_TARG]); |
| 469 | m = get_target_name(t->u.user.name); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 470 | if (m != NULL) { |
| 471 | if (build_st(m, t) < 0) { |
| 472 | fprintf(stderr, " %s error\n", m->name); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | opts = |
| 477 | merge_options(opts, m->extra_opts, |
| 478 | &m->option_offset); |
| 479 | } else { |
| 480 | fprintf(stderr, " failed to find target %s\n\n", |
| 481 | t->u.user.name); |
| 482 | return -1; |
| 483 | } |
| 484 | fprintf(f, "\ttarget "); |
| 485 | m->print(NULL, m->t, 0); |
| 486 | if (tb[TCA_IPT_INDEX] == NULL) { |
| 487 | fprintf(f, " [NULL ipt target index ]\n"); |
| 488 | } else { |
| 489 | __u32 index; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 490 | |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 491 | index = rta_getattr_u32(tb[TCA_IPT_INDEX]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 492 | fprintf(f, "\n\tindex %d", index); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (tb[TCA_IPT_CNT]) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 496 | struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]); |
| 497 | |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 498 | fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); |
| 499 | } |
| 500 | if (show_stats) { |
| 501 | if (tb[TCA_IPT_TM]) { |
| 502 | struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 503 | |
| 504 | print_tm(f, tm); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 505 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 506 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 507 | fprintf(f, "\n"); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 508 | |
| 509 | } |
shemminger | 6d4662d | 2005-06-23 17:36:38 +0000 | [diff] [blame] | 510 | free_opts(opts); |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | struct action_util ipt_action_util = { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 516 | .id = "ipt", |
| 517 | .parse_aopt = parse_ipt, |
| 518 | .print_aopt = print_ipt, |
net[shemminger]!shemminger | 1ffd7fd | 2005-01-17 23:26:23 +0000 | [diff] [blame] | 519 | }; |