iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libip6t_MARK.c
1 /* Shared library add-on to iptables to add MARK target support. */
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6
7 #include <ip6tables.h>
8 #include <linux/netfilter_ipv6/ip6_tables.h>
9 #include <linux/netfilter_ipv6/ip6t_MARK.h>
10
11 /* Function which prints out usage message. */
12 static void
13 help(void)
14 {
15         printf(
16 "MARK target v%s options:\n"
17 "  --set-mark value                   Set nfmark value\n"
18 "\n",
19 IPTABLES_VERSION);
20 }
21
22 static struct option opts[] = {
23         { .name = "set-mark", .has_arg = 1, .flag = 0, .val = '1' },
24         { .name = 0 }
25 };
26
27 /* Initialize the target. */
28 static void
29 init(struct ip6t_entry_target *t, unsigned int *nfcache)
30 {
31 }
32
33 /* Function which parses command options; returns true if it
34    ate an option */
35 static int
36 parse(int c, char **argv, int invert, unsigned int *flags,
37       const struct ip6t_entry *entry,
38       struct ip6t_entry_target **target)
39 {
40         struct ip6t_mark_target_info *markinfo
41                 = (struct ip6t_mark_target_info *)(*target)->data;
42
43         switch (c) {
44                 char *end;
45         case '1':
46                 markinfo->mark = strtoul(optarg, &end, 0);
47                 if (*end != '\0' || end == optarg)
48                         exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
49                 if (*flags)
50                         exit_error(PARAMETER_PROBLEM,
51                                    "MARK target: Can't specify --set-mark twice");
52                 *flags = 1;
53                 break;
54
55         default:
56                 return 0;
57         }
58
59         return 1;
60 }
61
62 static void
63 final_check(unsigned int flags)
64 {
65         if (!flags)
66                 exit_error(PARAMETER_PROBLEM,
67                            "MARK target: Parameter --set-mark is required");
68 }
69
70 /* Prints out the targinfo. */
71 static void
72 print(const struct ip6t_ip6 *ip,
73       const struct ip6t_entry_target *target,
74       int numeric)
75 {
76         const struct ip6t_mark_target_info *markinfo =
77                 (const struct ip6t_mark_target_info *)target->data;
78
79         printf("MARK set 0x%lx ", markinfo->mark);
80 }
81
82 /* Saves the union ipt_targinfo in parsable form to stdout. */
83 static void
84 save(const struct ip6t_ip6 *ip, const struct ip6t_entry_target *target)
85 {
86         const struct ip6t_mark_target_info *markinfo =
87                 (const struct ip6t_mark_target_info *)target->data;
88
89         printf("--set-mark 0x%lx ", markinfo->mark);
90 }
91
92 static
93 struct ip6tables_target mark = {
94         .name          = "MARK",
95         .version       = IPTABLES_VERSION,
96         .size          = IP6T_ALIGN(sizeof(struct ip6t_mark_target_info)),
97         .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_mark_target_info)),
98         .help          = &help,
99         .init          = &init,
100         .parse         = &parse,
101         .final_check   = &final_check,
102         .print         = &print,
103         .save          = &save,
104         .extra_opts    = opts
105 };
106
107 void _init(void)
108 {
109         register_target6(&mark);
110 }