blob: 9c24494f27679342829c23d6f22fe751f9b2400f [file] [log] [blame]
Martin Josefsson110610b2002-01-11 18:41:02 +00001/* Shared library add-on to iptables to add related packet matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +00008#include <xtables.h>
9#include <linux/netfilter/xt_helper.h>
Martin Josefsson110610b2002-01-11 18:41:02 +000010
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"helper match v%s options:\n"
Hervé Eychenne9230c112003-03-03 22:23:22 +000017"[!] --helper string Match helper identified by string\n"
Martin Josefsson110610b2002-01-11 18:41:02 +000018"\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000019IPTABLES_VERSION);
Martin Josefsson110610b2002-01-11 18:41:02 +000020}
21
Jan Engelhardt661f1122007-07-30 14:46:51 +000022static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000023 { "helper", 1, NULL, '1' },
24 { }
Martin Josefsson110610b2002-01-11 18:41:02 +000025};
26
Martin Josefsson110610b2002-01-11 18:41:02 +000027/* Function which parses command options; returns true if it
28 ate an option */
29static int
30parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000031 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000032 struct xt_entry_match **match)
Martin Josefsson110610b2002-01-11 18:41:02 +000033{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000034 struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000035
36 switch (c) {
37 case '1':
Nicolas Bouliane708f7b92005-01-02 23:34:48 +000038 if (*flags)
39 exit_error(PARAMETER_PROBLEM,
40 "helper match: Only use --helper ONCE!");
Harald Welteb77f1da2002-03-14 11:35:58 +000041 check_inverse(optarg, &invert, &invert, 0);
Martin Josefsson110610b2002-01-11 18:41:02 +000042 strncpy(info->name, optarg, 29);
Karsten Desler073df8f2004-01-31 15:33:55 +000043 info->name[29] = '\0';
Martin Josefsson110610b2002-01-11 18:41:02 +000044 if (invert)
45 info->invert = 1;
46 *flags = 1;
47 break;
48
49 default:
50 return 0;
51 }
52 return 1;
53}
54
55/* Final check; must have specified --helper. */
56static void
57final_check(unsigned int flags)
58{
59 if (!flags)
60 exit_error(PARAMETER_PROBLEM,
61 "helper match: You must specify `--helper'");
62}
63
64/* Prints out the info. */
65static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000066print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000067 const struct xt_entry_match *match,
Martin Josefsson110610b2002-01-11 18:41:02 +000068 int numeric)
69{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000070 struct xt_helper_info *info = (struct xt_helper_info *)match->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000071
72 printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name);
73}
74
75/* Saves the union ipt_info in parsable form to stdout. */
76static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000077save(const void *ip, const struct xt_entry_match *match)
Martin Josefsson110610b2002-01-11 18:41:02 +000078{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000079 struct xt_helper_info *info = (struct xt_helper_info *)match->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000080
81 printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name);
82}
83
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000084static struct xtables_match helper = {
85 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +000086 .name = "helper",
87 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000088 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
Pablo Neira8caee8b2004-12-28 13:11:59 +000089 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +000090 .parse = &parse,
91 .final_check = &final_check,
92 .print = &print,
93 .save = &save,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000094 .extra_opts = opts,
95};
96
97static struct xtables_match helper6 = {
98 .family = AF_INET6,
99 .name = "helper",
100 .version = IPTABLES_VERSION,
101 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
102 .help = &help,
103 .parse = &parse,
104 .final_check = &final_check,
105 .print = &print,
106 .save = &save,
107 .extra_opts = opts,
Martin Josefsson110610b2002-01-11 18:41:02 +0000108};
109
110void _init(void)
111{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +0000112 xtables_register_match(&helper);
113 xtables_register_match(&helper6);
Martin Josefsson110610b2002-01-11 18:41:02 +0000114}