~ubuntu-branches/ubuntu/trusty/conntrack/trusty-proposed

« back to all changes in this revision

Viewing changes to extensions/libct_proto_sctp.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt, Max Kellermann
  • Date: 2008-04-14 23:09:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080414230922-9xoi1gl38tc8lyng
Tags: 1:0.9.6-4
[ Max Kellermann ]
fix compilation on SPARC (printf argument mismatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * (C) 2005 by Harald Welte <laforge@netfilter.org>
3
 
 *     2006 by Pablo Neira Ayuso <pablo@netfilter.org>
4
 
 *
5
 
 *      This program is free software; you can redistribute it and/or modify
6
 
 *      it under the terms of the GNU General Public License as published by
7
 
 *      the Free Software Foundation; either version 2 of the License, or
8
 
 *      (at your option) any later version.
9
 
 *
10
 
 */
11
 
#include <stdio.h>
12
 
#include <getopt.h>
13
 
#include <stdlib.h>
14
 
#include <string.h>
15
 
#include <netinet/in.h> /* For htons */
16
 
#include "conntrack.h"
17
 
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
18
 
#include <libnetfilter_conntrack/libnetfilter_conntrack_sctp.h>
19
 
 
20
 
static struct option opts[] = {
21
 
        {"orig-port-src", 1, 0, '1'},
22
 
        {"orig-port-dst", 1, 0, '2'},
23
 
        {"reply-port-src", 1, 0, '3'},
24
 
        {"reply-port-dst", 1, 0, '4'},
25
 
        {"state", 1, 0, '5'},
26
 
        {"tuple-port-src", 1, 0, '6'},
27
 
        {"tuple-port-dst", 1, 0, '7'},
28
 
        {0, 0, 0, 0}
29
 
};
30
 
 
31
 
static const char *states[] = {
32
 
        "NONE",
33
 
        "CLOSED",
34
 
        "COOKIE_WAIT",
35
 
        "COOKIE_ECHOED",
36
 
        "ESTABLISHED",
37
 
        "SHUTDOWN_SENT",
38
 
        "SHUTDOWN_RECV",
39
 
        "SHUTDOWN_ACK_SENT",
40
 
};
41
 
 
42
 
static void help()
43
 
{
44
 
        fprintf(stdout, "--orig-port-src        original source port\n");
45
 
        fprintf(stdout, "--orig-port-dst        original destination port\n");
46
 
        fprintf(stdout, "--reply-port-src       reply source port\n");
47
 
        fprintf(stdout, "--reply-port-dst       reply destination port\n");
48
 
        fprintf(stdout, "--state                SCTP state, fe. ESTABLISHED\n");
49
 
        fprintf(stdout, "--tuple-port-src       expectation tuple src port\n");
50
 
        fprintf(stdout, "--tuple-port-src       expectation tuple dst port\n");
51
 
}
52
 
 
53
 
static int parse_options(char c, char *argv[], 
54
 
                         struct nfct_tuple *orig,
55
 
                         struct nfct_tuple *reply,
56
 
                         struct nfct_tuple *exptuple,
57
 
                         struct nfct_tuple *mask,
58
 
                         union nfct_protoinfo *proto,
59
 
                         unsigned int *flags)
60
 
{
61
 
        switch(c) {
62
 
                case '1':
63
 
                        if (optarg) {
64
 
                                orig->l4src.sctp.port = htons(atoi(optarg));
65
 
                                *flags |= SCTP_ORIG_SPORT;
66
 
                        }
67
 
                        break;
68
 
                case '2':
69
 
                        if (optarg) {
70
 
                                orig->l4dst.sctp.port = htons(atoi(optarg));
71
 
                                *flags |= SCTP_ORIG_DPORT;
72
 
                        }
73
 
                        break;
74
 
                case '3':
75
 
                        if (optarg) {
76
 
                                reply->l4src.sctp.port = htons(atoi(optarg));
77
 
                                *flags |= SCTP_REPL_SPORT;
78
 
                        }
79
 
                        break;
80
 
                case '4':
81
 
                        if (optarg) {
82
 
                                reply->l4dst.sctp.port = htons(atoi(optarg));
83
 
                                *flags |= SCTP_REPL_DPORT;
84
 
                        }
85
 
                        break;
86
 
                case '5':
87
 
                        if (optarg) {
88
 
                                int i;
89
 
                                for (i=0; i<10; i++) {
90
 
                                        if (strcmp(optarg, states[i]) == 0) {
91
 
                                                /* FIXME: Add state to
92
 
                                                 * nfct_protoinfo
93
 
                                                proto->sctp.state = i; */
94
 
                                                break;
95
 
                                        }
96
 
                                }
97
 
                                if (i == 10) {
98
 
                                        printf("doh?\n");
99
 
                                        return 0;
100
 
                                }
101
 
                                *flags |= SCTP_STATE;
102
 
                        }
103
 
                        break;
104
 
                case '6':
105
 
                        if (optarg) {
106
 
                                exptuple->l4src.sctp.port = htons(atoi(optarg));
107
 
                                *flags |= SCTP_EXPTUPLE_SPORT;
108
 
                        }
109
 
                        break;
110
 
                case '7':
111
 
                        if (optarg) {
112
 
                                exptuple->l4dst.sctp.port = htons(atoi(optarg));
113
 
                                *flags |= SCTP_EXPTUPLE_DPORT;
114
 
                        }
115
 
 
116
 
        }
117
 
        return 1;
118
 
}
119
 
 
120
 
static int final_check(unsigned int flags,
121
 
                       unsigned int command,
122
 
                       struct nfct_tuple *orig,
123
 
                       struct nfct_tuple *reply)
124
 
{
125
 
        int ret = 0;
126
 
        
127
 
        if ((flags & (SCTP_ORIG_SPORT|SCTP_ORIG_DPORT)) 
128
 
            && !(flags & (SCTP_REPL_SPORT|SCTP_REPL_DPORT))) {
129
 
                reply->l4src.sctp.port = orig->l4dst.sctp.port;
130
 
                reply->l4dst.sctp.port = orig->l4src.sctp.port;
131
 
                ret = 1;
132
 
        } else if (!(flags & (SCTP_ORIG_SPORT|SCTP_ORIG_DPORT))
133
 
                    && (flags & (SCTP_REPL_SPORT|SCTP_REPL_DPORT))) {
134
 
                orig->l4src.sctp.port = reply->l4dst.sctp.port;
135
 
                orig->l4dst.sctp.port = reply->l4src.sctp.port;
136
 
                ret = 1;
137
 
        }
138
 
        if ((flags & (SCTP_ORIG_SPORT|SCTP_ORIG_DPORT)) 
139
 
            && ((flags & (SCTP_REPL_SPORT|SCTP_REPL_DPORT))))
140
 
                ret = 1;
141
 
 
142
 
        /* --state is missing and we are trying to create a conntrack */
143
 
        if (ret && (command & CT_CREATE) && (!(flags & SCTP_STATE)))
144
 
                ret = 0;
145
 
 
146
 
        return ret;
147
 
}
148
 
 
149
 
static struct ctproto_handler sctp = {
150
 
        .name                   = "sctp",
151
 
        .protonum               = IPPROTO_SCTP,
152
 
        .parse_opts             = parse_options,
153
 
        .final_check            = final_check,
154
 
        .help                   = help,
155
 
        .opts                   = opts,
156
 
        .version                = VERSION,
157
 
};
158
 
 
159
 
static void __attribute__ ((constructor)) init(void);
160
 
 
161
 
static void init(void)
162
 
{
163
 
        register_proto(&sctp);
164
 
}