blob: 9c7f1371c8edecdc71987477f1c3912a3108aee6 [file] [log] [blame]
Harald Welte7559e072000-11-16 11:49:38 +00001/* Shared library add-on to ip6tables to add NFMARK matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7
8#include <ip6tables.h>
Martin Josefssonc5617bf2004-05-26 21:56:26 +00009/* For 64bit kernel / 32bit userspace */
10#include "../include/linux/netfilter_ipv6/ip6t_mark.h"
Harald Welte7559e072000-11-16 11:49:38 +000011
12/* Function which prints out usage message. */
13static void
14help(void)
15{
16 printf(
17"MARK match v%s options:\n"
18"[!] --mark value[/mask] Match nfmark value with optional mask\n"
19"\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000020IPTABLES_VERSION);
Harald Welte7559e072000-11-16 11:49:38 +000021}
22
23static struct option opts[] = {
24 { "mark", 1, 0, '1' },
25 {0}
26};
27
Harald Welte7559e072000-11-16 11:49:38 +000028/* Function which parses command options; returns true if it
29 ate an option */
30static int
31parse(int c, char **argv, int invert, unsigned int *flags,
32 const struct ip6t_entry *entry,
33 unsigned int *nfcache,
34 struct ip6t_entry_match **match)
35{
36 struct ip6t_mark_info *markinfo = (struct ip6t_mark_info *)(*match)->data;
37
38 switch (c) {
39 char *end;
40 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000041 check_inverse(optarg, &invert, &optind, 0);
Harald Welte7559e072000-11-16 11:49:38 +000042 markinfo->mark = strtoul(optarg, &end, 0);
43 if (*end == '/') {
44 markinfo->mask = strtoul(end+1, &end, 0);
45 } else
46 markinfo->mask = 0xffffffff;
47 if (*end != '\0' || end == optarg)
48 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
49 if (invert)
50 markinfo->invert = 1;
51 *flags = 1;
52 break;
53
54 default:
55 return 0;
56 }
57 return 1;
58}
59
Martin Josefssonc5617bf2004-05-26 21:56:26 +000060static void
61print_mark(unsigned long mark, unsigned long mask, int numeric)
62{
Harald Welte7559e072000-11-16 11:49:38 +000063 if(mask != 0xffffffff)
64 printf("0x%lx/0x%lx ", mark, mask);
65 else
66 printf("0x%lx ", mark);
67}
68
69/* Final check; must have specified --mark. */
70static void
71final_check(unsigned int flags)
72{
73 if (!flags)
74 exit_error(PARAMETER_PROBLEM,
75 "MARK match: You must specify `--mark'");
76}
77
78/* Prints out the matchinfo. */
79static void
80print(const struct ip6t_ip6 *ip,
81 const struct ip6t_entry_match *match,
82 int numeric)
83{
Martin Josefssonc5617bf2004-05-26 21:56:26 +000084 struct ip6t_mark_info *info = (struct ip6t_mark_info *)match->data;
85
Harald Welte7559e072000-11-16 11:49:38 +000086 printf("MARK match ");
Martin Josefssonc5617bf2004-05-26 21:56:26 +000087
88 if (info->invert)
89 printf("!");
90
91 print_mark(info->mark, info->mask, numeric);
Harald Welte7559e072000-11-16 11:49:38 +000092}
93
94/* Saves the union ip6t_matchinfo in parsable form to stdout. */
95static void
96save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
97{
Martin Josefssonc5617bf2004-05-26 21:56:26 +000098 struct ip6t_mark_info *info = (struct ip6t_mark_info *)match->data;
99
100 if (info->invert)
101 printf("! ");
102
Harald Welte7559e072000-11-16 11:49:38 +0000103 printf("--mark ");
Martin Josefssonc5617bf2004-05-26 21:56:26 +0000104 print_mark(info->mark, info->mask, 0);
Harald Welte7559e072000-11-16 11:49:38 +0000105}
106
Harald Welte02aa7332005-02-01 15:38:20 +0000107static struct ip6tables_match mark = {
108 .name = "mark",
109 .version = IPTABLES_VERSION,
110 .size = IP6T_ALIGN(sizeof(struct ip6t_mark_info)),
111 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_mark_info)),
112 .help = &help,
Harald Welte02aa7332005-02-01 15:38:20 +0000113 .parse = &parse,
114 .final_check = &final_check,
115 .print = &print,
116 .save = &save,
117 .extra_opts = opts,
Harald Welte7559e072000-11-16 11:49:38 +0000118};
119
120void _init(void)
121{
122 register_match6(&mark);
123}