~ubuntu-branches/ubuntu/quantal/iptables/quantal

« back to all changes in this revision

Viewing changes to extensions/libxt_cluster.c

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2011-11-07 13:46:11 UTC
  • mfrom: (5.1.9) (2.1.15 sid)
  • Revision ID: package-import@ubuntu.com-20111107134611-kc82oc327fb9ctq6
Tags: 1.4.12-1ubuntu1
* Merge from Debian unstable (LP: #887192). Remaining changes:
  - 9000-howtos.patch: add howtos/ and install them
  - 9001-build-libipq_pic.la.patch: Build libipq_pic.la with -fPIC
  - debian/control: Build-Depends on linuxdoc-tools
  - debian/iptables.install: install NAT and packetfilter howtos into
    /usr/share/doc
  - debian/iptables-dev.install: install netfilter howto into /usr/share/doc
  - debian/iptables-dev.doc-base.netfilter-extensions,
    debian/iptables-dev.doc-base.netfilter-hacking,
    debian/iptables.doc-base.nat, debian/iptables.doc-base.packet-filter: add
    howtos
  - debian/iptables-dev.install: install lib/*.la in usr/lib
  - No longer apply the following:
    + 9002-xt_recent-reap.patch: this requires a rewrite of the patch. For
      now adjust series to not apply, file a bug, assigning to the kernel
      team. Once they have resubmitted upstream, we can take the new patch.
    + 9003-compilation-error.patch: no longer needed with newer iptables

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * it under the terms of the GNU General Public License version 2 as
6
6
 * published by the Free Software Foundation.
7
7
 */
8
 
#include <stdbool.h>
9
8
#include <stdio.h>
10
 
#include <string.h>
11
 
#include <stdlib.h>
12
 
#include <getopt.h>
13
 
#include <stddef.h>
14
 
 
15
9
#include <xtables.h>
16
 
#include <linux/netfilter/x_tables.h>
17
10
#include <linux/netfilter/xt_cluster.h>
18
11
 
19
 
/* hack to keep for check */
20
 
static unsigned int total_nodes;
21
 
static unsigned int node_mask;
22
 
 
23
12
static void
24
13
cluster_help(void)
25
14
{
32
21
}
33
22
 
34
23
enum {
35
 
        CLUSTER_OPT_TOTAL_NODES,
36
 
        CLUSTER_OPT_LOCAL_NODE,
37
 
        CLUSTER_OPT_NODE_MASK,
38
 
        CLUSTER_OPT_HASH_SEED,
39
 
};
40
 
 
41
 
static const struct option cluster_opts[] = {
42
 
        {.name = "cluster-total-nodes",    .has_arg = true, .val = CLUSTER_OPT_TOTAL_NODES},
43
 
        {.name = "cluster-local-node",     .has_arg = true, .val = CLUSTER_OPT_LOCAL_NODE},
44
 
        {.name = "cluster-local-nodemask", .has_arg = true, .val = CLUSTER_OPT_NODE_MASK},
45
 
        {.name = "cluster-hash-seed",      .has_arg = true, .val = CLUSTER_OPT_HASH_SEED},
46
 
        XT_GETOPT_TABLEEND,
47
 
};
48
 
 
49
 
static int 
50
 
cluster_parse(int c, char **argv, int invert, unsigned int *flags,
51
 
              const void *entry, struct xt_entry_match **match)
 
24
        O_CL_TOTAL_NODES = 0,
 
25
        O_CL_LOCAL_NODE,
 
26
        O_CL_LOCAL_NODEMASK,
 
27
        O_CL_HASH_SEED,
 
28
        F_CL_TOTAL_NODES    = 1 << O_CL_TOTAL_NODES,
 
29
        F_CL_LOCAL_NODE     = 1 << O_CL_LOCAL_NODE,
 
30
        F_CL_LOCAL_NODEMASK = 1 << O_CL_LOCAL_NODEMASK,
 
31
        F_CL_HASH_SEED      = 1 << O_CL_HASH_SEED,
 
32
};
 
33
 
 
34
#define s struct xt_cluster_match_info
 
35
static const struct xt_option_entry cluster_opts[] = {
 
36
        {.name = "cluster-total-nodes", .id = O_CL_TOTAL_NODES,
 
37
         .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX,
 
38
         .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, total_nodes)},
 
39
        {.name = "cluster-local-node", .id = O_CL_LOCAL_NODE,
 
40
         .excl = F_CL_LOCAL_NODEMASK, .flags = XTOPT_INVERT,
 
41
         .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX},
 
42
        {.name = "cluster-local-nodemask", .id = O_CL_LOCAL_NODEMASK,
 
43
         .excl = F_CL_LOCAL_NODE, .type = XTTYPE_UINT32,
 
44
         .min = 1, .max = XT_CLUSTER_NODES_MAX,
 
45
         .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, node_mask)},
 
46
        {.name = "cluster-hash-seed", .id = O_CL_HASH_SEED,
 
47
         .type = XTTYPE_UINT32, .flags = XTOPT_MAND | XTOPT_PUT,
 
48
         XTOPT_POINTER(s, hash_seed)},
 
49
        XTOPT_TABLEEND,
 
50
};
 
51
 
 
52
static void cluster_parse(struct xt_option_call *cb)
52
53
{
53
 
        struct xt_cluster_match_info *info = (void *)(*match)->data;
54
 
        unsigned int num;
55
 
 
56
 
        switch (c) {
57
 
        case CLUSTER_OPT_TOTAL_NODES:
58
 
                if (*flags & (1 << c)) {
59
 
                        xtables_error(PARAMETER_PROBLEM,
60
 
                                      "Can only specify "
61
 
                                      "`--cluster-total-nodes' once");
62
 
                }
63
 
                if (!xtables_strtoui(optarg, NULL, &num, 1,
64
 
                                     XT_CLUSTER_NODES_MAX)) {
65
 
                        xtables_error(PARAMETER_PROBLEM,
66
 
                                      "Unable to parse `%s' in "
67
 
                                      "`--cluster-total-nodes'", optarg);
68
 
                }
69
 
                total_nodes = num;
70
 
                info->total_nodes = total_nodes = num;
71
 
                *flags |= 1 << c;
72
 
                break;
73
 
        case CLUSTER_OPT_LOCAL_NODE:
74
 
                if (*flags & (1 << c)) {
75
 
                        xtables_error(PARAMETER_PROBLEM,
76
 
                                      "Can only specify "
77
 
                                      "`--cluster-local-node' once");
78
 
                }
79
 
                if (*flags & (1 << CLUSTER_OPT_NODE_MASK)) {
80
 
                        xtables_error(PARAMETER_PROBLEM, "You cannot use "
81
 
                                      "`--cluster-local-nodemask' and "
82
 
                                      "`--cluster-local-node'");
83
 
                }
84
 
                xtables_check_inverse(optarg, &invert, &optind, 0, argv);
85
 
 
86
 
                if (!xtables_strtoui(optarg, NULL, &num, 1,
87
 
                                     XT_CLUSTER_NODES_MAX)) {
88
 
                        xtables_error(PARAMETER_PROBLEM,
89
 
                                      "Unable to parse `%s' in "
90
 
                                      "`--cluster-local-node'", optarg);
91
 
                }
92
 
                if (invert)
93
 
                        info->flags |= (1 << XT_CLUSTER_F_INV);
94
 
 
95
 
                info->node_mask = node_mask = (1 << (num - 1));
96
 
                *flags |= 1 << c;
97
 
                break;
98
 
        case CLUSTER_OPT_NODE_MASK:
99
 
                if (*flags & (1 << c)) {
100
 
                        xtables_error(PARAMETER_PROBLEM,
101
 
                                      "Can only specify "
102
 
                                      "`--cluster-local-node' once");
103
 
                }
104
 
                if (*flags & (1 << CLUSTER_OPT_LOCAL_NODE)) {
105
 
                        xtables_error(PARAMETER_PROBLEM, "You cannot use "
106
 
                                      "`--cluster-local-nodemask' and "
107
 
                                      "`--cluster-local-node'");
108
 
                }
109
 
                xtables_check_inverse(optarg, &invert, &optind, 0, argv);
110
 
 
111
 
                if (!xtables_strtoui(optarg, NULL, &num, 1,
112
 
                                     XT_CLUSTER_NODES_MAX)) {
113
 
                        xtables_error(PARAMETER_PROBLEM,
114
 
                                      "Unable to parse `%s' in "
115
 
                                      "`--cluster-local-node'", optarg);
116
 
                }
117
 
                if (invert)
118
 
                        info->flags |= (1 << XT_CLUSTER_F_INV);
119
 
 
120
 
                info->node_mask = node_mask = num;
121
 
                *flags |= 1 << c;
122
 
                break;
123
 
 
124
 
        case CLUSTER_OPT_HASH_SEED:
125
 
                if (*flags & (1 << c)) {
126
 
                        xtables_error(PARAMETER_PROBLEM,
127
 
                                      "Can only specify "
128
 
                                      "`--cluster-hash-seed' once");
129
 
                }
130
 
                if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX)) {
131
 
                        xtables_error(PARAMETER_PROBLEM,
132
 
                                      "Unable to parse `%s'", optarg);
133
 
                }
134
 
                info->hash_seed = num;
135
 
                *flags |= 1 << c;
136
 
                break;
137
 
        default:
138
 
                return 0;
 
54
        struct xt_cluster_match_info *info = cb->data;
 
55
 
 
56
        xtables_option_parse(cb);
 
57
        switch (cb->entry->id) {
 
58
        case O_CL_LOCAL_NODE:
 
59
                if (cb->invert)
 
60
                        info->flags |= XT_CLUSTER_F_INV;
 
61
                info->node_mask = 1 << (cb->val.u32 - 1);
 
62
                break;
 
63
        case O_CL_LOCAL_NODEMASK:
 
64
                if (cb->invert)
 
65
                        info->flags |= XT_CLUSTER_F_INV;
 
66
                break;
139
67
        }
140
 
 
141
 
        return 1;
142
68
}
143
69
 
144
 
static void
145
 
cluster_check(unsigned int flags)
 
70
static void cluster_check(struct xt_fcheck_call *cb)
146
71
{
147
 
        if ((flags & ((1 << CLUSTER_OPT_TOTAL_NODES) |
148
 
                      (1 << CLUSTER_OPT_LOCAL_NODE) |
149
 
                      (1 << CLUSTER_OPT_HASH_SEED)))
150
 
                == ((1 << CLUSTER_OPT_TOTAL_NODES) |
151
 
                    (1 << CLUSTER_OPT_LOCAL_NODE) |
152
 
                    (1 << CLUSTER_OPT_HASH_SEED))) {
153
 
                if (node_mask >= (1ULL << total_nodes)) {
 
72
        const struct xt_cluster_match_info *info = cb->data;
 
73
        unsigned int test;
 
74
 
 
75
        test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODE | F_CL_HASH_SEED;
 
76
        if ((cb->xflags & test) == test) {
 
77
                if (info->node_mask >= (1ULL << info->total_nodes))
154
78
                        xtables_error(PARAMETER_PROBLEM,
155
79
                                      "cluster match: "
156
80
                                      "`--cluster-local-node' "
157
81
                                      "must be <= `--cluster-total-nodes'");
158
 
                }
159
82
                return;
160
83
        }
161
 
        if ((flags & ((1 << CLUSTER_OPT_TOTAL_NODES) |
162
 
                      (1 << CLUSTER_OPT_NODE_MASK) |
163
 
                      (1 << CLUSTER_OPT_HASH_SEED)))
164
 
                == ((1 << CLUSTER_OPT_TOTAL_NODES) |
165
 
                    (1 << CLUSTER_OPT_NODE_MASK) |
166
 
                    (1 << CLUSTER_OPT_HASH_SEED))) {
167
 
                if (node_mask >= (1ULL << total_nodes)) {
 
84
 
 
85
        test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODEMASK | F_CL_HASH_SEED;
 
86
        if ((cb->xflags & test) == test) {
 
87
                if (info->node_mask >= (1ULL << info->total_nodes))
168
88
                        xtables_error(PARAMETER_PROBLEM,
169
89
                                      "cluster match: "
170
90
                                      "`--cluster-local-nodemask' too big "
171
91
                                      "for `--cluster-total-nodes'");
172
 
                }
173
92
                return;
174
93
        }
175
 
        if (!(flags & (1 << CLUSTER_OPT_TOTAL_NODES))) {
176
 
                xtables_error(PARAMETER_PROBLEM,
177
 
                              "cluster match: `--cluster-total-nodes' "
178
 
                              "is missing");
179
 
        }
180
 
        if (!(flags & (1 << CLUSTER_OPT_HASH_SEED))) {
181
 
                xtables_error(PARAMETER_PROBLEM,
182
 
                              "cluster match: `--cluster-hash-seed' "
183
 
                              "is missing");
184
 
        }
185
 
        if (!(flags & ((1 << (CLUSTER_OPT_LOCAL_NODE) |
186
 
                       (1 << (CLUSTER_OPT_NODE_MASK)))))) {
 
94
        if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
187
95
                xtables_error(PARAMETER_PROBLEM,
188
96
                              "cluster match: `--cluster-local-node' or"
189
97
                              "`--cluster-local-nodemask' is missing");
190
 
        }
191
98
}
192
99
 
193
100
static void
195
102
{
196
103
        const struct xt_cluster_match_info *info = (void *)match->data;
197
104
 
198
 
        printf("cluster ");
 
105
        printf(" cluster ");
199
106
        if (info->flags & XT_CLUSTER_F_INV)
200
 
                printf("!node_mask=0x%08x ", info->node_mask);
 
107
                printf("!node_mask=0x%08x", info->node_mask);
201
108
        else
202
 
                printf("node_mask=0x%08x ", info->node_mask);
 
109
                printf("node_mask=0x%08x", info->node_mask);
203
110
 
204
 
        printf("total_nodes=%u hash_seed=0x%08x ", 
 
111
        printf(" total_nodes=%u hash_seed=0x%08x",
205
112
                info->total_nodes, info->hash_seed);
206
113
}
207
114
 
211
118
        const struct xt_cluster_match_info *info = (void *)match->data;
212
119
 
213
120
        if (info->flags & XT_CLUSTER_F_INV)
214
 
                printf("! --cluster-local-nodemask 0x%08x ", info->node_mask);
 
121
                printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
215
122
        else
216
 
                printf("--cluster-local-nodemask 0x%08x ", info->node_mask);
 
123
                printf(" --cluster-local-nodemask 0x%08x", info->node_mask);
217
124
 
218
 
        printf("--cluster-total-nodes %u --cluster-hash-seed 0x%08x ",
 
125
        printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
219
126
                info->total_nodes, info->hash_seed);
220
127
}
221
128
 
226
133
        .size           = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
227
134
        .userspacesize  = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
228
135
        .help           = cluster_help,
229
 
        .parse          = cluster_parse,
230
 
        .final_check    = cluster_check,
231
136
        .print          = cluster_print,
232
137
        .save           = cluster_save,
233
 
        .extra_opts     = cluster_opts,
 
138
        .x6_parse       = cluster_parse,
 
139
        .x6_fcheck      = cluster_check,
 
140
        .x6_options     = cluster_opts,
234
141
};
235
142
 
236
143
void _init(void)