blob: 390930a99ca86ae7226199381923a43b5244252b [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. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000012static void helper_help(void)
Martin Josefsson110610b2002-01-11 18:41:02 +000013{
14 printf(
15"helper match v%s options:\n"
Hervé Eychenne9230c112003-03-03 22:23:22 +000016"[!] --helper string Match helper identified by string\n"
Martin Josefsson110610b2002-01-11 18:41:02 +000017"\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000018IPTABLES_VERSION);
Martin Josefsson110610b2002-01-11 18:41:02 +000019}
20
Jan Engelhardt181dead2007-10-04 16:27:07 +000021static const struct option helper_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000022 { "helper", 1, NULL, '1' },
23 { }
Martin Josefsson110610b2002-01-11 18:41:02 +000024};
25
Martin Josefsson110610b2002-01-11 18:41:02 +000026/* Function which parses command options; returns true if it
27 ate an option */
28static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000029helper_parse(int c, char **argv, int invert, unsigned int *flags,
30 const void *entry, struct xt_entry_match **match)
Martin Josefsson110610b2002-01-11 18:41:02 +000031{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000032 struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000033
34 switch (c) {
35 case '1':
Nicolas Bouliane708f7b92005-01-02 23:34:48 +000036 if (*flags)
37 exit_error(PARAMETER_PROBLEM,
38 "helper match: Only use --helper ONCE!");
Harald Welteb77f1da2002-03-14 11:35:58 +000039 check_inverse(optarg, &invert, &invert, 0);
Martin Josefsson110610b2002-01-11 18:41:02 +000040 strncpy(info->name, optarg, 29);
Karsten Desler073df8f2004-01-31 15:33:55 +000041 info->name[29] = '\0';
Martin Josefsson110610b2002-01-11 18:41:02 +000042 if (invert)
43 info->invert = 1;
44 *flags = 1;
45 break;
46
47 default:
48 return 0;
49 }
50 return 1;
51}
52
53/* Final check; must have specified --helper. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000054static void helper_check(unsigned int flags)
Martin Josefsson110610b2002-01-11 18:41:02 +000055{
56 if (!flags)
57 exit_error(PARAMETER_PROBLEM,
58 "helper match: You must specify `--helper'");
59}
60
61/* Prints out the info. */
62static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000063helper_print(const void *ip, const struct xt_entry_match *match, int numeric)
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
67 printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name);
68}
69
70/* Saves the union ipt_info in parsable form to stdout. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000071static void helper_save(const void *ip, const struct xt_entry_match *match)
Martin Josefsson110610b2002-01-11 18:41:02 +000072{
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000073 struct xt_helper_info *info = (struct xt_helper_info *)match->data;
Martin Josefsson110610b2002-01-11 18:41:02 +000074
75 printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name);
76}
77
Jan Engelhardt181dead2007-10-04 16:27:07 +000078static struct xtables_match helper_match = {
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000079 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +000080 .name = "helper",
81 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000082 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +000083 .help = helper_help,
84 .parse = helper_parse,
85 .final_check = helper_check,
86 .print = helper_print,
87 .save = helper_save,
88 .extra_opts = helper_opts,
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000089};
90
Jan Engelhardt181dead2007-10-04 16:27:07 +000091static struct xtables_match helper_match6 = {
Yasuyuki KOZAKAIf8137b12007-08-04 08:26:59 +000092 .family = AF_INET6,
93 .name = "helper",
94 .version = IPTABLES_VERSION,
95 .size = XT_ALIGN(sizeof(struct xt_helper_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +000096 .help = helper_help,
97 .parse = helper_parse,
98 .final_check = helper_check,
99 .print = helper_print,
100 .save = helper_save,
101 .extra_opts = helper_opts,
Martin Josefsson110610b2002-01-11 18:41:02 +0000102};
103
104void _init(void)
105{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000106 xtables_register_match(&helper_match);
107 xtables_register_match(&helper_match6);
Martin Josefsson110610b2002-01-11 18:41:02 +0000108}