changing trunk/trunk to trunk
[iptables.git] / extensions / libxt_CLASSIFY.c
1 /* Shared library add-on to iptables to add CLASSIFY 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_CLASSIFY.h>
10 #include <linux/types.h>
11 #include <linux/pkt_sched.h>
12
13 /* Function which prints out usage message. */
14 static void
15 CLASSIFY_help(void)
16 {
17         printf(
18 "CLASSIFY target options:\n"
19 "  --set-class [MAJOR:MINOR]    Set skb->priority value\n"
20 "  --add-mark                   Add value of skb->mark to skb->priority (PlanetLab specific)\n");
21 }
22
23 static const struct option CLASSIFY_opts[] = {
24         { "set-class", 1, NULL, '1' },
25         { "add-mark", 0, 0, '2' },
26         { .name = NULL }
27 };
28
29 static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
30 {
31         unsigned int i, j;
32
33         if (sscanf(s, "%x:%x", &i, &j) != 2)
34                 return 1;
35         
36         *p = TC_H_MAKE(i<<16, j);
37         return 0;
38 }
39
40 /* Function which parses command options; returns true if it
41    ate an option */
42 static int
43 CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags,
44       const void *entry,
45       struct xt_entry_target **target)
46 {
47         struct xt_classify_target_info *clinfo
48                 = (struct xt_classify_target_info *)(*target)->data;
49
50         clinfo->add_mark = 0;
51
52         switch (c) {
53         case '1':
54                 if (CLASSIFY_string_to_priority(optarg, &clinfo->priority))
55                         exit_error(PARAMETER_PROBLEM,
56                                    "Bad class value `%s'", optarg);
57                 if (*flags)
58                         exit_error(PARAMETER_PROBLEM,
59                                    "CLASSIFY: Can't specify --set-class twice");
60                 *flags = 1;
61                 break;
62
63         case '2':
64                 clinfo->add_mark = 1;
65                 break;
66
67         default:
68                 return 0;
69         }
70
71         return 1;
72 }
73
74 static void
75 CLASSIFY_final_check(unsigned int flags)
76 {
77         if (!flags)
78                 exit_error(PARAMETER_PROBLEM,
79                            "CLASSIFY: Parameter --set-class is required");
80 }
81
82 static void
83 CLASSIFY_print_class(unsigned int priority, int numeric)
84 {
85         printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
86 }
87
88 /* Prints out the targinfo. */
89 static void
90 CLASSIFY_print(const void *ip,
91       const struct xt_entry_target *target,
92       int numeric)
93 {
94         const struct xt_classify_target_info *clinfo =
95                 (const struct xt_classify_target_info *)target->data;
96         printf("CLASSIFY set ");
97         CLASSIFY_print_class(clinfo->priority, numeric);
98
99         if (clinfo->add_mark)
100                printf ("add-mark ");
101 }
102
103 /* Saves the union ipt_targinfo in parsable form to stdout. */
104 static void
105 CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
106 {
107         const struct xt_classify_target_info *clinfo =
108                 (const struct xt_classify_target_info *)target->data;
109
110         printf("--set-class %.4x:%.4x ",
111                TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
112
113         if (clinfo->add_mark)
114                printf("--add-mark ");
115 }
116
117 static struct xtables_target classify_target = { 
118         .family         = AF_UNSPEC,
119         .name           = "CLASSIFY",
120         .version        = XTABLES_VERSION,
121         .size           = XT_ALIGN(sizeof(struct xt_classify_target_info)),
122         .userspacesize  = XT_ALIGN(sizeof(struct xt_classify_target_info)),
123         .help           = CLASSIFY_help,
124         .parse          = CLASSIFY_parse,
125         .final_check    = CLASSIFY_final_check,
126         .print          = CLASSIFY_print,
127         .save           = CLASSIFY_save,
128         .extra_opts     = CLASSIFY_opts,
129 };
130
131 void _init(void)
132 {
133         xtables_register_target(&classify_target);
134 }