blob: efbb4ec06ff5da95850bbaa8955ed2fdc144ee67 [file] [log] [blame]
Harald Welted870b462000-11-13 14:37:20 +00001/* 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
11struct markinfo {
12 struct ip6t_entry_target t;
13 struct ip6t_mark_target_info mark;
14};
15
16/* Function which prints out usage message. */
17static void
18help(void)
19{
20 printf(
21"MARK target v%s options:\n"
22" --set-mark value Set nfmark value\n"
23"\n",
24NETFILTER_VERSION);
25}
26
27static struct option opts[] = {
28 { "set-mark", 1, 0, '1' },
29 { 0 }
30};
31
32/* Initialize the target. */
33static void
34init(struct ip6t_entry_target *t, unsigned int *nfcache)
35{
36}
37
38/* Function which parses command options; returns true if it
39 ate an option */
40static int
41parse(int c, char **argv, int invert, unsigned int *flags,
42 const struct ip6t_entry *entry,
43 struct ip6t_entry_target **target)
44{
45 struct ip6t_mark_target_info *markinfo
46 = (struct ip6t_mark_target_info *)(*target)->data;
47
48 switch (c) {
49 char *end;
50 case '1':
51 markinfo->mark = strtoul(optarg, &end, 0);
52 if (*end != '\0' || end == optarg)
53 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
54 if (*flags)
55 exit_error(PARAMETER_PROBLEM,
56 "MARK target: Can't specify --set-mark twice");
57 *flags = 1;
58 break;
59
60 default:
61 return 0;
62 }
63
64 return 1;
65}
66
67static void
68final_check(unsigned int flags)
69{
70 if (!flags)
71 exit_error(PARAMETER_PROBLEM,
72 "MARK target: Parameter --set-mark is required");
73}
74
75static void
76print_mark(unsigned long mark, int numeric)
77{
78 printf("0x%lx ", mark);
79}
80
81/* Prints out the targinfo. */
82static void
83print(const struct ip6t_ip6 *ip,
84 const struct ip6t_entry_target *target,
85 int numeric)
86{
87 const struct ip6t_mark_target_info *markinfo =
88 (const struct ip6t_mark_target_info *)target->data;
89 printf("MARK set ");
90 print_mark(markinfo->mark, numeric);
91}
92
93/* Saves the union ipt_targinfo in parsable form to stdout. */
94static void
95save(const struct ip6t_ip6 *ip, const struct ip6t_entry_target *target)
96{
97 const struct ip6t_mark_target_info *markinfo =
98 (const struct ip6t_mark_target_info *)target->data;
99
100 printf("--set-mark 0x%lx ", markinfo->mark);
101}
102
103struct ip6tables_target mark
104= { NULL,
105 "MARK",
106 NETFILTER_VERSION,
107 IP6T_ALIGN(sizeof(struct ip6t_mark_target_info)),
108 IP6T_ALIGN(sizeof(struct ip6t_mark_target_info)),
109 &help,
110 &init,
111 &parse,
112 &final_check,
113 &print,
114 &save,
115 opts
116};
117
118void _init(void)
119{
120 register_target6(&mark);
121}