blob: b60c9826b4d384d9777ccd72bea7f3da34bd76cf [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
Jan Engelhardt181dead2007-10-04 16:27:07 +000011static void helper_help(void)
Martin Josefsson110610b2002-01-11 18:41:02 +000012{
13 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020014"helper match options:\n"
15"[!] --helper string Match helper identified by string\n");
Martin Josefsson110610b2002-01-11 18:41:02 +000016}
17
Jan Engelhardt181dead2007-10-04 16:27:07 +000018static const struct option helper_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000019 { "helper", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000020 { .name = NULL }
Martin Josefsson110610b2002-01-11 18:41:02 +000021};
22
Martin Josefsson110610b2002-01-11 18:41:02 +000023static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000024helper_parse(int c, char **argv, int invert, unsigned int *flags,
25 const void *entry, struct xt_entry_match **match)
Martin Josefsson110610b2002-01-11 18:41:02 +000026{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000027 struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000028
29 switch (c) {
30 case '1':
Nicolas Bouliane708f7b92005-01-02 23:34:48 +000031 if (*flags)
32 exit_error(PARAMETER_PROBLEM,
33 "helper match: Only use --helper ONCE!");
Harald Welteb77f1da2002-03-14 11:35:58 +000034 check_inverse(optarg, &invert, &invert, 0);
Martin Josefsson110610b2002-01-11 18:41:02 +000035 strncpy(info->name, optarg, 29);
Karsten Desler073df8f2004-01-31 15:33:55 +000036 info->name[29] = '\0';
Martin Josefsson110610b2002-01-11 18:41:02 +000037 if (invert)
38 info->invert = 1;
39 *flags = 1;
40 break;
41
42 default:
43 return 0;
44 }
45 return 1;
46}
47
Jan Engelhardt181dead2007-10-04 16:27:07 +000048static void helper_check(unsigned int flags)
Martin Josefsson110610b2002-01-11 18:41:02 +000049{
50 if (!flags)
51 exit_error(PARAMETER_PROBLEM,
52 "helper match: You must specify `--helper'");
53}
54
Martin Josefsson110610b2002-01-11 18:41:02 +000055static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000056helper_print(const void *ip, const struct xt_entry_match *match, int numeric)
Martin Josefsson110610b2002-01-11 18:41:02 +000057{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000058 struct xt_helper_info *info = (struct xt_helper_info *)match->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000059
60 printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name);
61}
62
Jan Engelhardt181dead2007-10-04 16:27:07 +000063static void helper_save(const void *ip, const struct xt_entry_match *match)
Martin Josefsson110610b2002-01-11 18:41:02 +000064{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000065 struct xt_helper_info *info = (struct xt_helper_info *)match->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000066
Max Kellermanna5d09942008-01-29 13:44:34 +000067 printf("%s--helper ",info->invert ? "! " : "");
68 save_string(info->name);
Martin Josefsson110610b2002-01-11 18:41:02 +000069}
70
Jan Engelhardt181dead2007-10-04 16:27:07 +000071static struct xtables_match helper_match = {
Jan Engelhardt03d99482008-11-18 12:27:54 +010072 .family = NFPROTO_IPV4,
Pablo Neira8caee8b2004-12-28 13:11:59 +000073 .name = "helper",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020074 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000075 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +000076 .help = helper_help,
77 .parse = helper_parse,
78 .final_check = helper_check,
79 .print = helper_print,
80 .save = helper_save,
81 .extra_opts = helper_opts,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000082};
83
Jan Engelhardt181dead2007-10-04 16:27:07 +000084static struct xtables_match helper_match6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +010085 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000086 .name = "helper",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020087 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000088 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +000089 .help = helper_help,
90 .parse = helper_parse,
91 .final_check = helper_check,
92 .print = helper_print,
93 .save = helper_save,
94 .extra_opts = helper_opts,
Martin Josefsson110610b2002-01-11 18:41:02 +000095};
96
97void _init(void)
98{
Jan Engelhardt181dead2007-10-04 16:27:07 +000099 xtables_register_match(&helper_match);
100 xtables_register_match(&helper_match6);
Martin Josefsson110610b2002-01-11 18:41:02 +0000101}