~csurbhi/ubuntu/maverick/iptables/iptable-fix.600195

« back to all changes in this revision

Viewing changes to extensions/libxt_MARK.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-06-24 15:06:04 UTC
  • mfrom: (5.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080624150604-5t7r1o1kxq0ycz81
Tags: 1.4.0-4ubuntu1
* Merge from debian unstable, remaining changes:
  - Took references to 2.4 kernel out of doc-base control files (Jordan
    Mantha, Malone #25972) (patches/all/091-fix-2.4-references.patch)
  - Use linux-libc-dev instead of local copy of kernel-headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Shared library add-on to iptables to add MARK target support. */
 
2
#include <stdio.h>
 
3
#include <string.h>
 
4
#include <stdlib.h>
 
5
#include <getopt.h>
 
6
 
 
7
#include <xtables.h>
 
8
#include <linux/netfilter/x_tables.h>
 
9
#include <linux/netfilter/xt_MARK.h>
 
10
 
 
11
/* Function which prints out usage message. */
 
12
static void MARK_help(void)
 
13
{
 
14
        printf(
 
15
"MARK target v%s options:\n"
 
16
"  --set-mark value                   Set nfmark value\n"
 
17
"  --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);
 
21
}
 
22
 
 
23
static const struct option MARK_opts[] = {
 
24
        { "set-mark", 1, NULL, '1' },
 
25
        { "and-mark", 1, NULL, '2' },
 
26
        { "or-mark", 1, NULL, '3' },
 
27
        { }
 
28
};
 
29
 
 
30
/* Function which parses command options; returns true if it
 
31
   ate an option */
 
32
static int
 
33
MARK_parse_v0(int c, char **argv, int invert, unsigned int *flags,
 
34
              const void *entry, struct xt_entry_target **target)
 
35
{
 
36
        struct xt_mark_target_info *markinfo
 
37
                = (struct xt_mark_target_info *)(*target)->data;
 
38
 
 
39
        switch (c) {
 
40
        case '1':
 
41
                if (string_to_number_l(optarg, 0, 0, 
 
42
                                     &markinfo->mark))
 
43
                        exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
 
44
                if (*flags)
 
45
                        exit_error(PARAMETER_PROBLEM,
 
46
                                   "MARK target: Can't specify --set-mark twice");
 
47
                *flags = 1;
 
48
                break;
 
49
        case '2':
 
50
                exit_error(PARAMETER_PROBLEM,
 
51
                           "MARK target: kernel too old for --and-mark");
 
52
        case '3':
 
53
                exit_error(PARAMETER_PROBLEM,
 
54
                           "MARK target: kernel too old for --or-mark");
 
55
        default:
 
56
                return 0;
 
57
        }
 
58
 
 
59
        return 1;
 
60
}
 
61
 
 
62
static void MARK_check(unsigned int flags)
 
63
{
 
64
        if (!flags)
 
65
                exit_error(PARAMETER_PROBLEM,
 
66
                           "MARK target: Parameter --set/and/or-mark"
 
67
                           " is required");
 
68
}
 
69
 
 
70
/* Function which parses command options; returns true if it
 
71
   ate an option */
 
72
static int
 
73
MARK_parse_v1(int c, char **argv, int invert, unsigned int *flags,
 
74
              const void *entry, struct xt_entry_target **target)
 
75
{
 
76
        struct xt_mark_target_info_v1 *markinfo
 
77
                = (struct xt_mark_target_info_v1 *)(*target)->data;
 
78
 
 
79
        switch (c) {
 
80
        case '1':
 
81
                markinfo->mode = XT_MARK_SET;
 
82
                break;
 
83
        case '2':
 
84
                markinfo->mode = XT_MARK_AND;
 
85
                break;
 
86
        case '3':
 
87
                markinfo->mode = XT_MARK_OR;
 
88
                break;
 
89
        default:
 
90
                return 0;
 
91
        }
 
92
 
 
93
        if (string_to_number_l(optarg, 0, 0, &markinfo->mark))
 
94
                exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
 
95
 
 
96
        if (*flags)
 
97
                exit_error(PARAMETER_PROBLEM,
 
98
                           "MARK target: Can't specify --set-mark twice");
 
99
 
 
100
        *flags = 1;
 
101
        return 1;
 
102
}
 
103
 
 
104
static void
 
105
print_mark(unsigned long mark)
 
106
{
 
107
        printf("0x%lx ", mark);
 
108
}
 
109
 
 
110
/* Prints out the targinfo. */
 
111
static void MARK_print_v0(const void *ip,
 
112
                          const struct xt_entry_target *target, int numeric)
 
113
{
 
114
        const struct xt_mark_target_info *markinfo =
 
115
                (const struct xt_mark_target_info *)target->data;
 
116
        printf("MARK set ");
 
117
        print_mark(markinfo->mark);
 
118
}
 
119
 
 
120
/* Saves the union ipt_targinfo in parsable form to stdout. */
 
121
static void MARK_save_v0(const void *ip, const struct xt_entry_target *target)
 
122
{
 
123
        const struct xt_mark_target_info *markinfo =
 
124
                (const struct xt_mark_target_info *)target->data;
 
125
 
 
126
        printf("--set-mark ");
 
127
        print_mark(markinfo->mark);
 
128
}
 
129
 
 
130
/* Prints out the targinfo. */
 
131
static void MARK_print_v1(const void *ip, const struct xt_entry_target *target,
 
132
                          int numeric)
 
133
{
 
134
        const struct xt_mark_target_info_v1 *markinfo =
 
135
                (const struct xt_mark_target_info_v1 *)target->data;
 
136
 
 
137
        switch (markinfo->mode) {
 
138
        case XT_MARK_SET:
 
139
                printf("MARK set ");
 
140
                break;
 
141
        case XT_MARK_AND:
 
142
                printf("MARK and ");
 
143
                break;
 
144
        case XT_MARK_OR: 
 
145
                printf("MARK or ");
 
146
                break;
 
147
        }
 
148
        print_mark(markinfo->mark);
 
149
}
 
150
 
 
151
/* Saves the union ipt_targinfo in parsable form to stdout. */
 
152
static void MARK_save_v1(const void *ip, const struct xt_entry_target *target)
 
153
{
 
154
        const struct xt_mark_target_info_v1 *markinfo =
 
155
                (const struct xt_mark_target_info_v1 *)target->data;
 
156
 
 
157
        switch (markinfo->mode) {
 
158
        case XT_MARK_SET:
 
159
                printf("--set-mark ");
 
160
                break;
 
161
        case XT_MARK_AND:
 
162
                printf("--and-mark ");
 
163
                break;
 
164
        case XT_MARK_OR: 
 
165
                printf("--or-mark ");
 
166
                break;
 
167
        }
 
168
        print_mark(markinfo->mark);
 
169
}
 
170
 
 
171
static struct xtables_target mark_target_v0 = {
 
172
        .family         = AF_INET,
 
173
        .name           = "MARK",
 
174
        .version        = IPTABLES_VERSION,
 
175
        .revision       = 0,
 
176
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info)),
 
177
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info)),
 
178
        .help           = MARK_help,
 
179
        .parse          = MARK_parse_v0,
 
180
        .final_check    = MARK_check,
 
181
        .print          = MARK_print_v0,
 
182
        .save           = MARK_save_v0,
 
183
        .extra_opts     = MARK_opts,
 
184
};
 
185
 
 
186
static struct xtables_target mark_target_v1 = {
 
187
        .family         = AF_INET,
 
188
        .name           = "MARK",
 
189
        .version        = IPTABLES_VERSION,
 
190
        .revision       = 1,
 
191
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
 
192
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
 
193
        .help           = MARK_help,
 
194
        .parse          = MARK_parse_v1,
 
195
        .final_check    = MARK_check,
 
196
        .print          = MARK_print_v1,
 
197
        .save           = MARK_save_v1,
 
198
        .extra_opts     = MARK_opts,
 
199
};
 
200
 
 
201
static struct xtables_target mark_target6_v0 = {
 
202
        .family         = AF_INET6,
 
203
        .name           = "MARK",
 
204
        .version        = IPTABLES_VERSION,
 
205
        .revision       = 0,
 
206
        .size           = XT_ALIGN(sizeof(struct xt_mark_target_info)),
 
207
        .userspacesize  = XT_ALIGN(sizeof(struct xt_mark_target_info)),
 
208
        .help           = MARK_help,
 
209
        .parse          = MARK_parse_v0,
 
210
        .final_check    = MARK_check,
 
211
        .print          = MARK_print_v0,
 
212
        .save           = MARK_save_v0,
 
213
        .extra_opts     = MARK_opts,
 
214
};
 
215
 
 
216
void _init(void)
 
217
{
 
218
        xtables_register_target(&mark_target_v0);
 
219
        xtables_register_target(&mark_target_v1);
 
220
        xtables_register_target(&mark_target6_v0);
 
221
}