tizen 2.3.1 release
[external/iptables.git] / extensions / libxt_CLASSIFY.c
1 /*
2  * Copyright (c) 2003-2013 Patrick McHardy <kaber@trash.net>
3  */
4
5 #include <stdio.h>
6 #include <xtables.h>
7 #include <linux/netfilter/xt_CLASSIFY.h>
8 #include <linux/pkt_sched.h>
9
10 enum {
11         O_SET_CLASS = 0,
12 };
13
14 static void
15 CLASSIFY_help(void)
16 {
17         printf(
18 "CLASSIFY target options:\n"
19 "--set-class MAJOR:MINOR    Set skb->priority value (always hexadecimal!)\n");
20 }
21
22 static const struct xt_option_entry CLASSIFY_opts[] = {
23         {.name = "set-class", .id = O_SET_CLASS, .type = XTTYPE_STRING,
24          .flags = XTOPT_MAND},
25         XTOPT_TABLEEND,
26 };
27
28 static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
29 {
30         unsigned int i, j;
31
32         if (sscanf(s, "%x:%x", &i, &j) != 2)
33                 return 1;
34         
35         *p = TC_H_MAKE(i<<16, j);
36         return 0;
37 }
38
39 static void CLASSIFY_parse(struct xt_option_call *cb)
40 {
41         struct xt_classify_target_info *clinfo = cb->data;
42
43         xtables_option_parse(cb);
44         if (CLASSIFY_string_to_priority(cb->arg, &clinfo->priority))
45                 xtables_error(PARAMETER_PROBLEM,
46                            "Bad class value \"%s\"", cb->arg);
47 }
48
49 static void
50 CLASSIFY_print_class(unsigned int priority, int numeric)
51 {
52         printf(" %x:%x", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
53 }
54
55 static void
56 CLASSIFY_print(const void *ip,
57       const struct xt_entry_target *target,
58       int numeric)
59 {
60         const struct xt_classify_target_info *clinfo =
61                 (const struct xt_classify_target_info *)target->data;
62         printf(" CLASSIFY set");
63         CLASSIFY_print_class(clinfo->priority, numeric);
64 }
65
66 static void
67 CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
68 {
69         const struct xt_classify_target_info *clinfo =
70                 (const struct xt_classify_target_info *)target->data;
71
72         printf(" --set-class %.4x:%.4x",
73                TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
74 }
75
76 static struct xtables_target classify_target = { 
77         .family         = NFPROTO_UNSPEC,
78         .name           = "CLASSIFY",
79         .version        = XTABLES_VERSION,
80         .size           = XT_ALIGN(sizeof(struct xt_classify_target_info)),
81         .userspacesize  = XT_ALIGN(sizeof(struct xt_classify_target_info)),
82         .help           = CLASSIFY_help,
83         .print          = CLASSIFY_print,
84         .save           = CLASSIFY_save,
85         .x6_parse       = CLASSIFY_parse,
86         .x6_options     = CLASSIFY_opts,
87 };
88
89 void _init(void)
90 {
91         xtables_register_target(&classify_target);
92 }