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

« back to all changes in this revision

Viewing changes to extensions/libipt_MASQUERADE.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Valcárcel Scerpella
  • Date: 2009-05-06 16:35:21 UTC
  • mfrom: (5.1.6 upstream) (2.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20090506163521-2hbruo0m33h04wxf
Tags: 1.4.3.2-2ubuntu1
* Merge from debian unstable (LP: #372920), remaining changes:
  - Don't fail to run iptables-save if iptables module isn't loaded.
  - debian/patches/0901-build-libipq_pic.a.patch - Build libipq_pic.a with
    -fPIC. Upstream changed build system and patch modified accordingly.
  - Revert changes between 1.4.1.1-3 and 1.4.1.1-4, thus bringing back
    the howtos.
* Droped unexistent patches from debian/patches/series
* Droped 0903-autoload-module-in-iptables-save.diff, fixed upstream
* Added linuxdoc-tools to Build-Depends
* Modified debian/iptables{,-dev}.install to match DM syntax 
  (removed debian/tmp)

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include <string.h>
5
5
#include <stdlib.h>
6
6
#include <getopt.h>
7
 
#include <iptables.h>
 
7
#include <xtables.h>
 
8
#include <limits.h> /* INT_MAX in ip_tables.h */
8
9
#include <linux/netfilter_ipv4/ip_tables.h>
9
 
#include <linux/netfilter/nf_nat.h>
 
10
#include <net/netfilter/nf_nat.h>
10
11
 
11
 
/* Function which prints out usage message. */
12
12
static void MASQUERADE_help(void)
13
13
{
14
14
        printf(
25
25
        { .name = NULL }
26
26
};
27
27
 
28
 
/* Initialize the target. */
29
28
static void MASQUERADE_init(struct xt_entry_target *t)
30
29
{
31
 
        struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
 
30
        struct nf_nat_multi_range *mr = (struct nf_nat_multi_range *)t->data;
32
31
 
33
32
        /* Actually, it's 0, but it's ignored at the moment. */
34
33
        mr->rangesize = 1;
37
36
 
38
37
/* Parses ports */
39
38
static void
40
 
parse_ports(const char *arg, struct ip_nat_multi_range *mr)
 
39
parse_ports(const char *arg, struct nf_nat_multi_range *mr)
41
40
{
42
41
        const char *dash;
43
42
        int port;
46
45
 
47
46
        port = atoi(arg);
48
47
        if (port <= 0 || port > 65535)
49
 
                exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg);
 
48
                xtables_error(PARAMETER_PROBLEM, "Port \"%s\" not valid\n", arg);
50
49
 
51
50
        dash = strchr(arg, '-');
52
51
        if (!dash) {
58
57
 
59
58
                maxport = atoi(dash + 1);
60
59
                if (maxport == 0 || maxport > 65535)
61
 
                        exit_error(PARAMETER_PROBLEM,
 
60
                        xtables_error(PARAMETER_PROBLEM,
62
61
                                   "Port `%s' not valid\n", dash+1);
63
62
                if (maxport < port)
64
63
                        /* People are stupid.  Present reader excepted. */
65
 
                        exit_error(PARAMETER_PROBLEM,
 
64
                        xtables_error(PARAMETER_PROBLEM,
66
65
                                   "Port range `%s' funky\n", arg);
67
66
                mr->range[0].min.tcp.port = htons(port);
68
67
                mr->range[0].max.tcp.port = htons(maxport);
69
68
        }
70
69
}
71
70
 
72
 
/* Function which parses command options; returns true if it
73
 
   ate an option */
74
71
static int MASQUERADE_parse(int c, char **argv, int invert, unsigned int *flags,
75
72
                            const void *e, struct xt_entry_target **target)
76
73
{
77
74
        const struct ipt_entry *entry = e;
78
75
        int portok;
79
 
        struct ip_nat_multi_range *mr
80
 
                = (struct ip_nat_multi_range *)(*target)->data;
 
76
        struct nf_nat_multi_range *mr
 
77
                = (struct nf_nat_multi_range *)(*target)->data;
81
78
 
82
79
        if (entry->ip.proto == IPPROTO_TCP
83
80
            || entry->ip.proto == IPPROTO_UDP
 
81
            || entry->ip.proto == IPPROTO_SCTP
 
82
            || entry->ip.proto == IPPROTO_DCCP
84
83
            || entry->ip.proto == IPPROTO_ICMP)
85
84
                portok = 1;
86
85
        else
89
88
        switch (c) {
90
89
        case '1':
91
90
                if (!portok)
92
 
                        exit_error(PARAMETER_PROBLEM,
93
 
                                   "Need TCP or UDP with port specification");
 
91
                        xtables_error(PARAMETER_PROBLEM,
 
92
                                   "Need TCP, UDP, SCTP or DCCP with port specification");
94
93
 
95
 
                if (check_inverse(optarg, &invert, NULL, 0))
96
 
                        exit_error(PARAMETER_PROBLEM,
 
94
                if (xtables_check_inverse(optarg, &invert, NULL, 0))
 
95
                        xtables_error(PARAMETER_PROBLEM,
97
96
                                   "Unexpected `!' after --to-ports");
98
97
 
99
98
                parse_ports(optarg, mr);
108
107
        }
109
108
}
110
109
 
111
 
/* Prints out the targinfo. */
112
110
static void
113
111
MASQUERADE_print(const void *ip, const struct xt_entry_target *target,
114
112
                 int numeric)
115
113
{
116
 
        struct ip_nat_multi_range *mr
117
 
                = (struct ip_nat_multi_range *)target->data;
118
 
        struct ip_nat_range *r = &mr->range[0];
 
114
        struct nf_nat_multi_range *mr
 
115
                = (struct nf_nat_multi_range *)target->data;
 
116
        struct nf_nat_range *r = &mr->range[0];
119
117
 
120
118
        if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
121
119
                printf("masq ports: ");
129
127
                printf("random ");
130
128
}
131
129
 
132
 
/* Saves the union ipt_targinfo in parsable form to stdout. */
133
130
static void
134
131
MASQUERADE_save(const void *ip, const struct xt_entry_target *target)
135
132
{
136
 
        struct ip_nat_multi_range *mr
137
 
                = (struct ip_nat_multi_range *)target->data;
138
 
        struct ip_nat_range *r = &mr->range[0];
 
133
        struct nf_nat_multi_range *mr
 
134
                = (struct nf_nat_multi_range *)target->data;
 
135
        struct nf_nat_range *r = &mr->range[0];
139
136
 
140
137
        if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
141
138
                printf("--to-ports %hu", ntohs(r->min.tcp.port));
151
148
static struct xtables_target masquerade_tg_reg = {
152
149
        .name           = "MASQUERADE",
153
150
        .version        = XTABLES_VERSION,
154
 
        .family         = PF_INET,
155
 
        .size           = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
156
 
        .userspacesize  = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
 
151
        .family         = NFPROTO_IPV4,
 
152
        .size           = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
 
153
        .userspacesize  = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
157
154
        .help           = MASQUERADE_help,
158
155
        .init           = MASQUERADE_init,
159
156
        .parse          = MASQUERADE_parse,