Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add CONNMARK target support. |
| 2 | * |
| 3 | * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com> |
| 4 | * by Henrik Nordstrom <hno@marasystems.com> |
| 5 | * |
| 6 | * Version 1.1 |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <getopt.h> |
| 26 | |
| 27 | #include <iptables.h> |
| 28 | #include <linux/netfilter_ipv4/ip_tables.h> |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 29 | #include "../include/linux/netfilter_ipv4/ipt_CONNMARK.h" |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 30 | |
| 31 | #if 0 |
| 32 | struct markinfo { |
| 33 | struct ipt_entry_target t; |
| 34 | struct ipt_connmark_target_info mark; |
| 35 | }; |
| 36 | #endif |
| 37 | |
| 38 | /* Function which prints out usage message. */ |
| 39 | static void |
| 40 | help(void) |
| 41 | { |
| 42 | printf( |
| 43 | "CONNMARK target v%s options:\n" |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 44 | " --set-mark value[/mask] Set conntrack mark value\n" |
| 45 | " --save-mark [--mask mask] Save the packet nfmark in the connection\n" |
| 46 | " --restore-mark [--mask mask] Restore saved nfmark value\n" |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 47 | "\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 48 | IPTABLES_VERSION); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static struct option opts[] = { |
| 52 | { "set-mark", 1, 0, '1' }, |
| 53 | { "save-mark", 0, 0, '2' }, |
| 54 | { "restore-mark", 0, 0, '3' }, |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 55 | { "mask", 1, 0, '4' }, |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 56 | { 0 } |
| 57 | }; |
| 58 | |
| 59 | /* Initialize the target. */ |
| 60 | static void |
| 61 | init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | /* Function which parses command options; returns true if it |
| 66 | ate an option */ |
| 67 | static int |
| 68 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 69 | const struct ipt_entry *entry, |
| 70 | struct ipt_entry_target **target) |
| 71 | { |
| 72 | struct ipt_connmark_target_info *markinfo |
| 73 | = (struct ipt_connmark_target_info *)(*target)->data; |
| 74 | |
Deti Fliegl | 361bac2 | 2005-11-03 18:43:14 +0000 | [diff] [blame] | 75 | markinfo->mask = 0xffffffffUL; |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 76 | |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 77 | switch (c) { |
| 78 | char *end; |
| 79 | case '1': |
| 80 | markinfo->mode = IPT_CONNMARK_SET; |
Deti Fliegl | 361bac2 | 2005-11-03 18:43:14 +0000 | [diff] [blame] | 81 | |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 82 | markinfo->mark = strtoul(optarg, &end, 0); |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 83 | if (*end == '/' && end[1] != '\0') |
| 84 | markinfo->mask = strtoul(end+1, &end, 0); |
Deti Fliegl | 361bac2 | 2005-11-03 18:43:14 +0000 | [diff] [blame] | 85 | |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 86 | if (*end != '\0' || end == optarg) |
| 87 | exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg); |
| 88 | if (*flags) |
| 89 | exit_error(PARAMETER_PROBLEM, |
| 90 | "CONNMARK target: Can't specify --set-mark twice"); |
| 91 | *flags = 1; |
| 92 | break; |
| 93 | case '2': |
| 94 | markinfo->mode = IPT_CONNMARK_SAVE; |
| 95 | if (*flags) |
| 96 | exit_error(PARAMETER_PROBLEM, |
| 97 | "CONNMARK target: Can't specify --save-mark twice"); |
| 98 | *flags = 1; |
| 99 | break; |
| 100 | case '3': |
| 101 | markinfo->mode = IPT_CONNMARK_RESTORE; |
| 102 | if (*flags) |
| 103 | exit_error(PARAMETER_PROBLEM, |
| 104 | "CONNMARK target: Can't specify --restore-mark twice"); |
| 105 | *flags = 1; |
| 106 | break; |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 107 | case '4': |
| 108 | if (!*flags) |
| 109 | exit_error(PARAMETER_PROBLEM, |
| 110 | "CONNMARK target: Can't specify --mask without a operation"); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 111 | markinfo->mask = strtoul(optarg, &end, 0); |
Deti Fliegl | 361bac2 | 2005-11-03 18:43:14 +0000 | [diff] [blame] | 112 | |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 113 | if (*end != '\0' || end == optarg) |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 114 | exit_error(PARAMETER_PROBLEM, "Bad MASK value `%s'", optarg); |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 115 | break; |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 116 | default: |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | final_check(unsigned int flags) |
| 125 | { |
| 126 | if (!flags) |
| 127 | exit_error(PARAMETER_PROBLEM, |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 128 | "CONNMARK target: No operation specified"); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 131 | static void |
| 132 | print_mark(unsigned long mark) |
| 133 | { |
| 134 | printf("0x%lx", mark); |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | print_mask(const char *text, unsigned long mask) |
| 139 | { |
Deti Fliegl | 361bac2 | 2005-11-03 18:43:14 +0000 | [diff] [blame] | 140 | if (mask != 0xffffffffUL) |
Tom Eastep | f3aa491 | 2005-06-11 16:17:45 +0000 | [diff] [blame] | 141 | printf("%s0x%lx", text, mask); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 142 | } |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 143 | |
| 144 | |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 145 | /* Prints out the target info. */ |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 146 | static void |
| 147 | print(const struct ipt_ip *ip, |
| 148 | const struct ipt_entry_target *target, |
| 149 | int numeric) |
| 150 | { |
| 151 | const struct ipt_connmark_target_info *markinfo = |
| 152 | (const struct ipt_connmark_target_info *)target->data; |
| 153 | switch (markinfo->mode) { |
| 154 | case IPT_CONNMARK_SET: |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 155 | printf("CONNMARK set "); |
| 156 | print_mark(markinfo->mark); |
| 157 | print_mask("/", markinfo->mask); |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 158 | printf(" "); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 159 | break; |
| 160 | case IPT_CONNMARK_SAVE: |
| 161 | printf("CONNMARK save "); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 162 | print_mask("mask ", markinfo->mask); |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 163 | printf(" "); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 164 | break; |
| 165 | case IPT_CONNMARK_RESTORE: |
| 166 | printf("CONNMARK restore "); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 167 | print_mask("mask ", markinfo->mask); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 168 | break; |
| 169 | default: |
| 170 | printf("ERROR: UNKNOWN CONNMARK MODE "); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 175 | /* Saves the target into in parsable form to stdout. */ |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 176 | static void |
| 177 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 178 | { |
| 179 | const struct ipt_connmark_target_info *markinfo = |
| 180 | (const struct ipt_connmark_target_info *)target->data; |
| 181 | |
| 182 | switch (markinfo->mode) { |
| 183 | case IPT_CONNMARK_SET: |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 184 | printf("--set-mark "); |
| 185 | print_mark(markinfo->mark); |
| 186 | print_mask("/", markinfo->mask); |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 187 | printf(" "); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 188 | break; |
| 189 | case IPT_CONNMARK_SAVE: |
| 190 | printf("--save-mark "); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 191 | print_mask("--mask ", markinfo->mask); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 192 | break; |
| 193 | case IPT_CONNMARK_RESTORE: |
| 194 | printf("--restore-mark "); |
Martin Josefsson | 02964b8 | 2005-02-12 21:40:16 +0000 | [diff] [blame] | 195 | print_mask("--mask ", markinfo->mask); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 196 | break; |
| 197 | default: |
| 198 | printf("ERROR: UNKNOWN CONNMARK MODE "); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 203 | static struct iptables_target connmark_target = { |
| 204 | .name = "CONNMARK", |
| 205 | .version = IPTABLES_VERSION, |
| 206 | .size = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)), |
| 207 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)), |
| 208 | .help = &help, |
| 209 | .init = &init, |
| 210 | .parse = &parse, |
| 211 | .final_check = &final_check, |
| 212 | .print = &print, |
| 213 | .save = &save, |
| 214 | .extra_opts = opts |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | void _init(void) |
| 218 | { |
Henrik Nordstrom | a6ef994 | 2004-02-03 08:19:04 +0000 | [diff] [blame] | 219 | register_target(&connmark_target); |
Harald Welte | 0e81d5d | 2002-02-25 11:26:01 +0000 | [diff] [blame] | 220 | } |