~ubuntu-branches/ubuntu/lucid/iptables/lucid

« back to all changes in this revision

Viewing changes to extensions/libxt_MARK.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen, Iain Lane, Soren Hansen
  • Date: 2008-11-15 01:27:37 UTC
  • mfrom: (5.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115012737-o3kdn2z1o9ercq10
Tags: 1.4.1.1-4ubuntu1
[ Iain Lane ]
* Merge from debian unstable (LP: #294220), remaining changes:
  - debian/patches/0901-build-libipq_pic.a.patch - Build libipq_pic.a with
    -fPIC. Upstream changed build system and patch modified accordingly.

[ Soren Hansen ]
* Revert changes between 1.4.1.1-3 and 1.4.1.1-4, thus bringing back
  the howtos.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Shared library add-on to iptables to add MARK target support. */
 
2
#include <stdbool.h>
2
3
#include <stdio.h>
3
4
#include <string.h>
4
5
#include <stdlib.h>
8
9
#include <linux/netfilter/x_tables.h>
9
10
#include <linux/netfilter/xt_MARK.h>
10
11
 
 
12
enum {
 
13
        F_MARK = 1 << 0,
 
14
};
 
15
 
11
16
/* Function which prints out usage message. */
12
17
static void MARK_help(void)
13
18
{
14
19
        printf(
15
 
"MARK target v%s options:\n"
 
20
"MARK target options:\n"
16
21
"  --set-mark value                   Set nfmark value\n"
17
22
"  --and-mark value                   Binary AND the nfmark with value\n"
18
 
"  --or-mark  value                   Binary OR  the nfmark with value\n"
19
 
"\n",
20
 
IPTABLES_VERSION);
 
23
"  --or-mark  value                   Binary OR  the nfmark with value\n");
21
24
}
22
25
 
23
26
static const struct option MARK_opts[] = {
24
27
        { "set-mark", 1, NULL, '1' },
25
28
        { "and-mark", 1, NULL, '2' },
26
29
        { "or-mark", 1, NULL, '3' },
27
 
        { }
28
 
};
 
30
        { .name = NULL }
 
31
};
 
32
 
 
33
static const struct option mark_tg_opts[] = {
 
34
        {.name = "set-xmark", .has_arg = true, .val = 'X'},
 
35
        {.name = "set-mark",  .has_arg = true, .val = '='},
 
36
        {.name = "and-mark",  .has_arg = true, .val = '&'},
 
37
        {.name = "or-mark",   .has_arg = true, .val = '|'},
 
38
        {.name = "xor-mark",  .has_arg = true, .val = '^'},
 
39
        { .name = NULL }
 
40
};
 
41
 
 
42
static void mark_tg_help(void)
 
43
{
 
44
        printf(
 
45
"MARK target options:\n"
 
46
"  --set-xmark value[/mask]  Clear bits in mask and XOR value into nfmark\n"
 
47
"  --set-mark value[/mask]   Clear bits in mask and OR value into nfmark\n"
 
48
"  --and-mark bits           Binary AND the nfmark with bits\n"
 
49
"  --or-mark bits            Binary OR the nfmark with bits\n"
 
50
"  --xor-mask bits           Binary XOR the nfmark with bits\n"
 
51
"\n");
 
52
}
29
53
 
30
54
/* Function which parses command options; returns true if it
31
55
   ate an option */
101
125
        return 1;
102
126
}
103
127
 
 
128
static int mark_tg_parse(int c, char **argv, int invert, unsigned int *flags,
 
129
                         const void *entry, struct xt_entry_target **target)
 
130
{
 
131
        struct xt_mark_tginfo2 *info = (void *)(*target)->data;
 
132
        unsigned int value, mask = ~0U;
 
133
        char *end;
 
134
 
 
135
        switch (c) {
 
136
        case 'X': /* --set-xmark */
 
137
        case '=': /* --set-mark */
 
138
                param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
 
139
                param_act(P_NO_INVERT, "MARK", "--set-xmark/--set-mark", invert);
 
140
                if (!strtonum(optarg, &end, &value, 0, ~0U))
 
141
                        param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
 
142
                if (*end == '/')
 
143
                        if (!strtonum(end + 1, &end, &mask, 0, ~0U))
 
144
                                param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
 
145
                if (*end != '\0')
 
146
                        param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
 
147
                info->mark = value;
 
148
                info->mask = mask;
 
149
 
 
150
                if (c == '=')
 
151
                        info->mask = value | mask;
 
152
                break;
 
153
 
 
154
        case '&': /* --and-mark */
 
155
                param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
 
156
                param_act(P_NO_INVERT, "MARK", "--and-mark", invert);
 
157
                if (!strtonum(optarg, NULL, &mask, 0, ~0U))
 
158
                        param_act(P_BAD_VALUE, "MARK", "--and-mark", optarg);
 
159
                info->mark = 0;
 
160
                info->mask = ~mask;
 
161
                break;
 
162
 
 
163
        case '|': /* --or-mark */
 
164
                param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
 
165
                param_act(P_NO_INVERT, "MARK", "--or-mark", invert);
 
166
                if (!strtonum(optarg, NULL, &value, 0, ~0U))
 
167
                        param_act(P_BAD_VALUE, "MARK", "--or-mark", optarg);
 
168
                info->mark = value;
 
169
                info->mask = value;
 
170
                break;
 
171
 
 
172
        case '^': /* --xor-mark */
 
173
                param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
 
174
                param_act(P_NO_INVERT, "MARK", "--xor-mark", invert);
 
175
                if (!strtonum(optarg, NULL, &value, 0, ~0U))
 
176
                        param_act(P_BAD_VALUE, "MARK", "--xor-mark", optarg);
 
177
                info->mark = value;
 
178
                info->mask = 0;
 
179
                break;
 
180
 
 
181
        default:
 
182
                return false;
 
183
        }
 
184
 
 
185
        *flags |= F_MARK;
 
186
        return true;
 
187
}
 
188
 
 
189
static void mark_tg_check(unsigned int flags)
 
190
{
 
191
        if (flags == 0)
 
192
                exit_error(PARAMETER_PROBLEM, "MARK: One of the --set-xmark, "
 
193
                           "--{and,or,xor,set}-mark options is required");
 
194
}
 
195
 
104
196
static void
105
197
print_mark(unsigned long mark)
106
198
{
148
240
        print_mark(markinfo->mark);
149
241
}
150
242
 
 
243
static void mark_tg_print(const void *ip, const struct xt_entry_target *target,
 
244
                          int numeric)
 
245
{
 
246
        const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
247
 
 
248
        if (info->mark == 0)
 
249
                printf("MARK and 0x%x ", (unsigned int)(u_int32_t)~info->mask);
 
250
        else if (info->mark == info->mask)
 
251
                printf("MARK or 0x%x ", info->mark);
 
252
        else if (info->mask == 0)
 
253
                printf("MARK xor 0x%x ", info->mark);
 
254
        else
 
255
                printf("MARK xset 0x%x/0x%x ", info->mark, info->mask);
 
256
}
 
257
 
151
258
/* Saves the union ipt_targinfo in parsable form to stdout. */
152
259
static void MARK_save_v1(const void *ip, const struct xt_entry_target *target)
153
260
{
168
275
        print_mark(markinfo->mark);
169
276
}
170
277
 
 
278
static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
 
279
{
 
280
        const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
281
 
 
282
        printf("--set-xmark 0x%x/0x%x ", info->mark, info->mask);
 
283
}
 
284
 
171
285
static struct xtables_target mark_target_v0 = {
172
286
        .family         = AF_INET,
173
287
        .name           = "MARK",
174
 
        .version        = IPTABLES_VERSION,
 
288
        .version        = XTABLES_VERSION,
175
289
        .revision       = 0,
176
290
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info)),
177
291
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info)),
186
300
static struct xtables_target mark_target_v1 = {
187
301
        .family         = AF_INET,
188
302
        .name           = "MARK",
189
 
        .version        = IPTABLES_VERSION,
 
303
        .version        = XTABLES_VERSION,
190
304
        .revision       = 1,
191
305
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
192
306
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
201
315
static struct xtables_target mark_target6_v0 = {
202
316
        .family         = AF_INET6,
203
317
        .name           = "MARK",
204
 
        .version        = IPTABLES_VERSION,
 
318
        .version        = XTABLES_VERSION,
205
319
        .revision       = 0,
206
320
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info)),
207
321
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info)),
213
327
        .extra_opts     = MARK_opts,
214
328
};
215
329
 
 
330
static struct xtables_target mark_tg_reg_v2 = {
 
331
        .version       = XTABLES_VERSION,
 
332
        .name          = "MARK",
 
333
        .revision      = 2,
 
334
        .family        = AF_UNSPEC,
 
335
        .size          = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
 
336
        .userspacesize = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
 
337
        .help          = mark_tg_help,
 
338
        .parse         = mark_tg_parse,
 
339
        .final_check   = mark_tg_check,
 
340
        .print         = mark_tg_print,
 
341
        .save          = mark_tg_save,
 
342
        .extra_opts    = mark_tg_opts,
 
343
};
 
344
 
216
345
void _init(void)
217
346
{
218
347
        xtables_register_target(&mark_target_v0);
219
348
        xtables_register_target(&mark_target_v1);
220
349
        xtables_register_target(&mark_target6_v0);
 
350
        xtables_register_target(&mark_tg_reg_v2);
221
351
}