~ubuntu-branches/ubuntu/lucid/xtables-addons/lucid

« back to all changes in this revision

Viewing changes to extensions/libxt_psd.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2009-09-10 21:42:05 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090910214205-neqgwq7y5nctaty7
Tags: 1.18-1
* New Upstream Version
  This version has support for 2.6.31 (Closes: #545542)
* Bump standards version (no changes)
* Depend on quilt (Closes: #533653)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Shared library add-on to iptables to add PSD support
 
3
 
 
4
  Copyright (C) 2000,2001 astaro AG
 
5
 
 
6
  This file is distributed under the terms of the GNU General Public
 
7
  License (GPL). Copies of the GPL can be obtained from:
 
8
     ftp://prep.ai.mit.edu/pub/gnu/GPL
 
9
 
 
10
  2000-05-04 Markus Hennig <hennig@astaro.de> : initial
 
11
  2000-08-18 Dennis Koslowski <koslowski@astaro.de> : first release
 
12
  2000-12-01 Dennis Koslowski <koslowski@astaro.de> : UDP scans detection added
 
13
  2001-02-04 Jan Rekorajski <baggins@pld.org.pl> : converted from target to match
 
14
  2003-03-02 Harald Welte <laforge@netfilter.org>: fix 'storage' bug
 
15
  2008-04-03 Mohd Nawawi <nawawi@tracenetworkcorporation.com>: update to 2.6.24 / 1.4 code
 
16
  2008-06-24 Mohd Nawawi <nawawi@tracenetworkcorporation.com>: update to 2.6.24 / 1.4.1 code
 
17
  2009-08-07 Mohd Nawawi Mohamad Jamili <nawawi@tracenetworkcorporation.com> : ported to xtables-addons
 
18
*/
 
19
 
 
20
#include <stdbool.h>
 
21
#include <stdint.h>
 
22
#include <stdio.h>
 
23
#include <netdb.h>
 
24
#include <string.h>
 
25
#include <stdlib.h>
 
26
#include <syslog.h>
 
27
#include <getopt.h>
 
28
#include <xtables.h>
 
29
#include <linux/netfilter/x_tables.h>
 
30
#include "xt_psd.h"
 
31
 
 
32
/* Function which prints out usage message. */
 
33
static void psd_mt_help(void) {
 
34
        printf(
 
35
                "psd match options:\n"
 
36
                " --psd-weight-threshold threshhold  Portscan detection weight threshold\n"
 
37
                " --psd-delay-threshold  delay       Portscan detection delay threshold\n"
 
38
                " --psd-lo-ports-weight  lo          Privileged ports weight\n"
 
39
                " --psd-hi-ports-weight  hi          High ports weight\n\n");
 
40
}
 
41
 
 
42
static const struct option psd_mt_opts[] = {
 
43
        {.name = "psd-weight-threshold", .has_arg = true, .val = '1'},
 
44
        {.name = "psd-delay-threshold", .has_arg = true, .val = '2'},
 
45
        {.name = "psd-lo-ports-weight", .has_arg = true, .val = '3'},
 
46
        {.name = "psd-hi-ports-weight", .has_arg = true, .val = '4'},
 
47
        {NULL}
 
48
};
 
49
 
 
50
/* Initialize the target. */
 
51
static void psd_mt_init(struct xt_entry_match *match) {
 
52
        struct xt_psd_info *psdinfo = (struct xt_psd_info *)match->data;
 
53
        psdinfo->weight_threshold = SCAN_WEIGHT_THRESHOLD;
 
54
        psdinfo->delay_threshold = SCAN_DELAY_THRESHOLD;
 
55
        psdinfo->lo_ports_weight = PORT_WEIGHT_PRIV;
 
56
        psdinfo->hi_ports_weight = PORT_WEIGHT_HIGH;
 
57
}
 
58
 
 
59
#define XT_PSD_OPT_CTRESH 0x01
 
60
#define XT_PSD_OPT_DTRESH 0x02
 
61
#define XT_PSD_OPT_LPWEIGHT 0x04
 
62
#define XT_PSD_OPT_HPWEIGHT 0x08
 
63
 
 
64
static int psd_mt_parse(int c, char **argv, int invert, unsigned int *flags,
 
65
                     const void *entry, struct xt_entry_match **match)
 
66
{
 
67
        struct xt_psd_info *psdinfo = (struct xt_psd_info *)(*match)->data;
 
68
        unsigned int num;
 
69
 
 
70
        switch (c) {
 
71
                /* PSD-weight-threshold */
 
72
                case '1':
 
73
                        if (*flags & XT_PSD_OPT_CTRESH)
 
74
                                xtables_error(PARAMETER_PROBLEM,"Can't specify --psd-weight-threshold twice");
 
75
                        if (!xtables_strtoui(optarg, NULL, &num, 0, PSD_MAX_RATE))
 
76
                                xtables_error(PARAMETER_PROBLEM, "bad --psd-weight-threshold '%s'", optarg);
 
77
                        psdinfo->weight_threshold = num;
 
78
                        *flags |= XT_PSD_OPT_CTRESH;
 
79
                        return true;
 
80
 
 
81
                /* PSD-delay-threshold */
 
82
                case '2':
 
83
                        if (*flags & XT_PSD_OPT_DTRESH)
 
84
                                xtables_error(PARAMETER_PROBLEM, "Can't specify --psd-delay-threshold twice");
 
85
                        if (!xtables_strtoui(optarg, NULL, &num, 0, PSD_MAX_RATE))
 
86
                                xtables_error(PARAMETER_PROBLEM, "bad --psd-delay-threshold '%s'", optarg);
 
87
                        psdinfo->delay_threshold = num;
 
88
                        *flags |= XT_PSD_OPT_DTRESH;
 
89
                        return true;
 
90
 
 
91
                /* PSD-lo-ports-weight */
 
92
                case '3':
 
93
                        if (*flags & XT_PSD_OPT_LPWEIGHT)
 
94
                                xtables_error(PARAMETER_PROBLEM, "Can't specify --psd-lo-ports-weight twice");
 
95
                        if (!xtables_strtoui(optarg, NULL, &num, 0, PSD_MAX_RATE))
 
96
                                xtables_error(PARAMETER_PROBLEM, "bad --psd-lo-ports-weight '%s'", optarg);
 
97
                        psdinfo->lo_ports_weight = num;
 
98
                        *flags |= XT_PSD_OPT_LPWEIGHT;
 
99
                        return true;
 
100
 
 
101
                /* PSD-hi-ports-weight */
 
102
                case '4':
 
103
                        if (*flags & XT_PSD_OPT_HPWEIGHT)
 
104
                                xtables_error(PARAMETER_PROBLEM, "Can't specify --psd-hi-ports-weight twice");
 
105
                        if (!xtables_strtoui(optarg, NULL, &num, 0, PSD_MAX_RATE))
 
106
                                xtables_error(PARAMETER_PROBLEM, "bad --psd-hi-ports-weight '%s'", optarg);
 
107
                        psdinfo->hi_ports_weight = num;
 
108
                        *flags |= XT_PSD_OPT_HPWEIGHT;
 
109
                        return true;
 
110
        }
 
111
        return false;
 
112
}
 
113
 
 
114
/* Final check; nothing. */
 
115
static void psd_mt_final_check(unsigned int flags) {}
 
116
 
 
117
/* Prints out the targinfo. */
 
118
static void psd_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
 
119
{
 
120
        const struct xt_psd_info *psdinfo = (const struct xt_psd_info *)match->data;
 
121
        printf("psd ");
 
122
        printf("weight-threshold: %u ", psdinfo->weight_threshold);
 
123
        printf("delay-threshold: %u ", psdinfo->delay_threshold);
 
124
        printf("lo-ports-weight: %u ", psdinfo->lo_ports_weight);
 
125
        printf("hi-ports-weight: %u ", psdinfo->hi_ports_weight);
 
126
}
 
127
 
 
128
/* Saves the union ipt_targinfo in parsable form to stdout. */
 
129
static void psd_mt_save(const void *ip, const struct xt_entry_match *match)
 
130
{
 
131
        const struct xt_psd_info *psdinfo = (const struct xt_psd_info *)match->data;
 
132
        printf("--psd-weight-threshold %u ", psdinfo->weight_threshold);
 
133
        printf("--psd-delay-threshold %u ", psdinfo->delay_threshold);
 
134
        printf("--psd-lo-ports-weight %u ", psdinfo->lo_ports_weight);
 
135
        printf("--psd-hi-ports-weight %u ", psdinfo->hi_ports_weight);
 
136
}
 
137
 
 
138
static struct xtables_match psd_mt_reg = {
 
139
        .name                   = "psd",
 
140
        .version                = XTABLES_VERSION,
 
141
        .revision       = 1,
 
142
        .family                 = PF_INET,
 
143
        .size                   = XT_ALIGN(sizeof(struct xt_psd_info)),
 
144
        .userspacesize  = XT_ALIGN(sizeof(struct xt_psd_info)),
 
145
        .help                   = psd_mt_help,
 
146
        .init                   = psd_mt_init,
 
147
        .parse                  = psd_mt_parse,
 
148
        .final_check    = psd_mt_final_check,
 
149
        .print                  = psd_mt_print,
 
150
        .save                   = psd_mt_save,
 
151
        .extra_opts             = psd_mt_opts,
 
152
};
 
153
 
 
154
static __attribute__((constructor)) void psd_mt_ldr(void)
 
155
{
 
156
        xtables_register_match(&psd_mt_reg);
 
157
}
 
158