www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / opensource / ebtables / extensions / ebt_mark.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <getopt.h>
5 #include "../include/ebtables_u.h"
6 #include <linux/netfilter_bridge/ebt_mark_t.h>
7
8 static int mark_supplied;
9
10 #define MARK_TARGET  '1'
11 #define MARK_SETMARK '2'
12 static struct option opts[] =
13 {
14         { "mark-target" , required_argument, 0, MARK_TARGET },
15         { "set-mark"    , required_argument, 0, MARK_SETMARK },
16         { 0 }
17 };
18
19 static void print_help()
20 {
21         printf(
22         "mark target options:\n"
23         " --set-mark value     : Set nfmark value\n"
24         " --mark-target target : ACCEPT, DROP, RETURN or CONTINUE\n");
25 }
26
27 static void init(struct ebt_entry_target *target)
28 {
29         struct ebt_mark_t_info *markinfo =
30            (struct ebt_mark_t_info *)target->data;
31
32         markinfo->target = EBT_ACCEPT;
33         markinfo->mark = 0;
34         mark_supplied = 0;
35 }
36
37 #define OPT_MARK_TARGET   0x01
38 #define OPT_MARK_SETMARK  0x02
39 static int parse(int c, char **argv, int argc,
40    const struct ebt_u_entry *entry, unsigned int *flags,
41    struct ebt_entry_target **target)
42 {
43         struct ebt_mark_t_info *markinfo =
44            (struct ebt_mark_t_info *)(*target)->data;
45         char *end;
46
47         switch (c) {
48         case MARK_TARGET:
49                 check_option(flags, OPT_MARK_TARGET);
50                 if (FILL_TARGET(optarg, markinfo->target))
51                         print_error("Illegal --mark-target target");
52                 break;
53         case MARK_SETMARK:
54                 check_option(flags, OPT_MARK_SETMARK);
55                 markinfo->mark = strtoul(optarg, &end, 0);
56                 if (*end != '\0' || end == optarg)
57                         print_error("Bad MARK value '%s'", optarg);
58                 mark_supplied = 1;
59                 break;
60          default:
61                 return 0;
62         }
63         return 1;
64 }
65
66 static void final_check(const struct ebt_u_entry *entry,
67    const struct ebt_entry_target *target, const char *name,
68    unsigned int hookmask, unsigned int time)
69 {
70         struct ebt_mark_t_info *markinfo =
71            (struct ebt_mark_t_info *)target->data;
72
73         if (time == 0 && mark_supplied == 0)
74                 print_error("No mark value supplied");
75         if (BASE_CHAIN && markinfo->target == EBT_RETURN)
76                 print_error("--mark-target RETURN not allowed on base chain");
77 }
78
79 static void print(const struct ebt_u_entry *entry,
80    const struct ebt_entry_target *target)
81 {
82         struct ebt_mark_t_info *markinfo =
83            (struct ebt_mark_t_info *)target->data;
84
85         printf("--set-mark 0x%lx", markinfo->mark);
86         if (markinfo->target == EBT_ACCEPT)
87                 return;
88         printf(" --mark-target %s", TARGET_NAME(markinfo->target));
89 }
90
91 static int compare(const struct ebt_entry_target *t1,
92    const struct ebt_entry_target *t2)
93 {
94         struct ebt_mark_t_info *markinfo1 =
95            (struct ebt_mark_t_info *)t1->data;
96         struct ebt_mark_t_info *markinfo2 =
97            (struct ebt_mark_t_info *)t2->data;
98
99         return markinfo1->target == markinfo2->target &&
100            markinfo1->mark == markinfo2->mark;
101 }
102
103 static struct ebt_u_target mark_target =
104 {
105         .name           = EBT_MARK_TARGET,
106         .size           = sizeof(struct ebt_mark_t_info),
107         .help           = print_help,
108         .init           = init,
109         .parse          = parse,
110         .final_check    = final_check,
111         .print          = print,
112         .compare        = compare,
113         .extra_ops      = opts,
114 };
115
116 static void _init(void) __attribute__ ((constructor));
117 static void _init(void)
118 {
119         register_target(&mark_target);
120 }