blob: 2e17b3facd761620434c252fb4fbb7ce53e20776 [file] [log] [blame]
Henrik Nordstroma6ef9942004-02-03 08:19:04 +00001/* 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 Welte0e81d5d2002-02-25 11:26:01 +000022#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 Josefsson02964b82005-02-12 21:40:16 +000029#include "../include/linux/netfilter_ipv4/ipt_CONNMARK.h"
Harald Welte0e81d5d2002-02-25 11:26:01 +000030
31#if 0
32struct 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. */
39static void
40help(void)
41{
42 printf(
43"CONNMARK target v%s options:\n"
Henrik Nordstroma6ef9942004-02-03 08:19:04 +000044" --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 Welte0e81d5d2002-02-25 11:26:01 +000047"\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000048IPTABLES_VERSION);
Harald Welte0e81d5d2002-02-25 11:26:01 +000049}
50
51static struct option opts[] = {
52 { "set-mark", 1, 0, '1' },
53 { "save-mark", 0, 0, '2' },
54 { "restore-mark", 0, 0, '3' },
Henrik Nordstroma6ef9942004-02-03 08:19:04 +000055 { "mask", 1, 0, '4' },
Harald Welte0e81d5d2002-02-25 11:26:01 +000056 { 0 }
57};
58
59/* Initialize the target. */
60static void
61init(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 */
67static int
68parse(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 Fliegl361bac22005-11-03 18:43:14 +000075 markinfo->mask = 0xffffffffUL;
Martin Josefsson02964b82005-02-12 21:40:16 +000076
Harald Welte0e81d5d2002-02-25 11:26:01 +000077 switch (c) {
78 char *end;
79 case '1':
80 markinfo->mode = IPT_CONNMARK_SET;
Deti Fliegl361bac22005-11-03 18:43:14 +000081
Harald Welte0e81d5d2002-02-25 11:26:01 +000082 markinfo->mark = strtoul(optarg, &end, 0);
Henrik Nordstroma6ef9942004-02-03 08:19:04 +000083 if (*end == '/' && end[1] != '\0')
84 markinfo->mask = strtoul(end+1, &end, 0);
Deti Fliegl361bac22005-11-03 18:43:14 +000085
Harald Welte0e81d5d2002-02-25 11:26:01 +000086 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 Nordstroma6ef9942004-02-03 08:19:04 +0000107 case '4':
108 if (!*flags)
109 exit_error(PARAMETER_PROBLEM,
110 "CONNMARK target: Can't specify --mask without a operation");
Martin Josefsson02964b82005-02-12 21:40:16 +0000111 markinfo->mask = strtoul(optarg, &end, 0);
Deti Fliegl361bac22005-11-03 18:43:14 +0000112
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000113 if (*end != '\0' || end == optarg)
Martin Josefsson02964b82005-02-12 21:40:16 +0000114 exit_error(PARAMETER_PROBLEM, "Bad MASK value `%s'", optarg);
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000115 break;
Harald Welte0e81d5d2002-02-25 11:26:01 +0000116 default:
117 return 0;
118 }
119
120 return 1;
121}
122
123static void
124final_check(unsigned int flags)
125{
126 if (!flags)
127 exit_error(PARAMETER_PROBLEM,
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000128 "CONNMARK target: No operation specified");
Harald Welte0e81d5d2002-02-25 11:26:01 +0000129}
130
Martin Josefsson02964b82005-02-12 21:40:16 +0000131static void
132print_mark(unsigned long mark)
133{
134 printf("0x%lx", mark);
135}
136
137static void
138print_mask(const char *text, unsigned long mask)
139{
Deti Fliegl361bac22005-11-03 18:43:14 +0000140 if (mask != 0xffffffffUL)
Tom Eastepf3aa4912005-06-11 16:17:45 +0000141 printf("%s0x%lx", text, mask);
Martin Josefsson02964b82005-02-12 21:40:16 +0000142}
Martin Josefsson02964b82005-02-12 21:40:16 +0000143
144
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000145/* Prints out the target info. */
Harald Welte0e81d5d2002-02-25 11:26:01 +0000146static void
147print(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 Josefsson02964b82005-02-12 21:40:16 +0000155 printf("CONNMARK set ");
156 print_mark(markinfo->mark);
157 print_mask("/", markinfo->mask);
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000158 printf(" ");
Harald Welte0e81d5d2002-02-25 11:26:01 +0000159 break;
160 case IPT_CONNMARK_SAVE:
161 printf("CONNMARK save ");
Martin Josefsson02964b82005-02-12 21:40:16 +0000162 print_mask("mask ", markinfo->mask);
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000163 printf(" ");
Harald Welte0e81d5d2002-02-25 11:26:01 +0000164 break;
165 case IPT_CONNMARK_RESTORE:
166 printf("CONNMARK restore ");
Martin Josefsson02964b82005-02-12 21:40:16 +0000167 print_mask("mask ", markinfo->mask);
Harald Welte0e81d5d2002-02-25 11:26:01 +0000168 break;
169 default:
170 printf("ERROR: UNKNOWN CONNMARK MODE ");
171 break;
172 }
173}
174
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000175/* Saves the target into in parsable form to stdout. */
Harald Welte0e81d5d2002-02-25 11:26:01 +0000176static void
177save(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 Josefsson02964b82005-02-12 21:40:16 +0000184 printf("--set-mark ");
185 print_mark(markinfo->mark);
186 print_mask("/", markinfo->mask);
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000187 printf(" ");
Harald Welte0e81d5d2002-02-25 11:26:01 +0000188 break;
189 case IPT_CONNMARK_SAVE:
190 printf("--save-mark ");
Martin Josefsson02964b82005-02-12 21:40:16 +0000191 print_mask("--mask ", markinfo->mask);
Harald Welte0e81d5d2002-02-25 11:26:01 +0000192 break;
193 case IPT_CONNMARK_RESTORE:
194 printf("--restore-mark ");
Martin Josefsson02964b82005-02-12 21:40:16 +0000195 print_mask("--mask ", markinfo->mask);
Harald Welte0e81d5d2002-02-25 11:26:01 +0000196 break;
197 default:
198 printf("ERROR: UNKNOWN CONNMARK MODE ");
199 break;
200 }
201}
202
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000203static 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 Welte0e81d5d2002-02-25 11:26:01 +0000215};
216
217void _init(void)
218{
Henrik Nordstroma6ef9942004-02-03 08:19:04 +0000219 register_target(&connmark_target);
Harald Welte0e81d5d2002-02-25 11:26:01 +0000220}