5bb249127646fe011fdebe512e45919813900216
[iptables.git] / extensions / libipt_connmark.c
1 /* Shared library add-on to iptables to add connmark matching support.
2  *
3  * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
4  * by Henrik Nordstrom <hno@marasystems.com>
5  *
6  * Version 1.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 #include <stdio.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27
28 #include <iptables.h>
29 #include "../include/linux/netfilter_ipv4/ipt_connmark.h"
30
31 /* Function which prints out usage message. */
32 static void
33 help(void)
34 {
35         printf(
36 "CONNMARK match v%s options:\n"
37 "[!] --mark value[/mask]         Match nfmark value with optional mask\n"
38 "\n",
39 IPTABLES_VERSION);
40 }
41
42 static struct option opts[] = {
43         { "mark", 1, 0, '1' },
44         {0}
45 };
46
47 /* Initialize the match. */
48 static void
49 init(struct ipt_entry_match *m, unsigned int *nfcache)
50 {
51         /* Can't cache this. */
52         *nfcache |= NFC_UNKNOWN;
53 }
54
55 /* Function which parses command options; returns true if it
56    ate an option */
57 static int
58 parse(int c, char **argv, int invert, unsigned int *flags,
59       const struct ipt_entry *entry,
60       unsigned int *nfcache,
61       struct ipt_entry_match **match)
62 {
63         struct ipt_connmark_info *markinfo = (struct ipt_connmark_info *)(*match)->data;
64
65         switch (c) {
66                 char *end;
67         case '1':
68                 check_inverse(optarg, &invert, &optind, 0);
69 #ifdef KERNEL_64_USERSPACE_32
70                 markinfo->mark = strtoull(optarg, &end, 0);
71                 markinfo->mask = ~0ULL;
72                 if (*end == '/')
73                         markinfo->mask = strtoull(end+1, &end, 0);
74 #else
75                 markinfo->mark = strtoul(optarg, &end, 0);
76                 markinfo->mask = ~0UL;
77                 if (*end == '/')
78                         markinfo->mask = strtoul(end+1, &end, 0);
79 #endif
80                 if (*end != '\0' || end == optarg)
81                         exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
82                 if (invert)
83                         markinfo->invert = 1;
84                 *flags = 1;
85                 break;
86
87         default:
88                 return 0;
89         }
90         return 1;
91 }
92
93 #ifdef KERNEL_64_USERSPACE_32
94 static void
95 print_mark(unsigned long long mark, unsigned long long mask, int numeric)
96 {
97         if(mask != ~0ULL)
98                 printf("0x%llx/0x%llx ", mark, mask);
99         else
100                 printf("0x%llx ", mark);
101 }
102 #else
103 static void
104 print_mark(unsigned long mark, unsigned long mask, int numeric)
105 {
106         if(mask != ~0UL)
107                 printf("0x%lx/0x%lx ", mark, mask);
108         else
109                 printf("0x%lx ", mark);
110 }
111 #endif
112
113 /* Final check; must have specified --mark. */
114 static void
115 final_check(unsigned int flags)
116 {
117         if (!flags)
118                 exit_error(PARAMETER_PROBLEM,
119                            "MARK match: You must specify `--mark'");
120 }
121
122 /* Prints out the matchinfo. */
123 static void
124 print(const struct ipt_ip *ip,
125       const struct ipt_entry_match *match,
126       int numeric)
127 {
128         struct ipt_connmark_info *info = (struct ipt_connmark_info *)match->data;
129
130         printf("CONNMARK match ");
131         if (info->invert)
132                 printf("!");
133         print_mark(info->mark, info->mask, numeric);
134 }
135
136 /* Saves the matchinfo in parsable form to stdout. */
137 static void
138 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
139 {
140         struct ipt_connmark_info *info = (struct ipt_connmark_info *)match->data;
141
142         if (info->invert)
143                 printf("! ");
144
145         printf("--mark ");
146         print_mark(info->mark, info->mask, 0);
147 }
148
149 static struct iptables_match connmark_match = {
150     .name          = "connmark",
151     .version       = IPTABLES_VERSION,
152     .size          = IPT_ALIGN(sizeof(struct ipt_connmark_info)),
153     .userspacesize = IPT_ALIGN(sizeof(struct ipt_connmark_info)),
154     .help          = &help,
155     .init          = &init,
156     .parse         = &parse,
157     .final_check   = &final_check,
158     .print         = &print,
159     .save          = &save,
160     .extra_opts    = opts
161 };
162
163 void _init(void)
164 {
165         register_match(&connmark_match);
166 }