Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 1 | /* 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 KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 8 | #include <xtables.h> |
| 9 | #include <linux/netfilter/xt_helper.h> |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 10 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 11 | static void helper_help(void) |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 12 | { |
| 13 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 14 | "helper match options:\n" |
| 15 | "[!] --helper string Match helper identified by string\n"); |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 16 | } |
| 17 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 18 | static const struct option helper_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 19 | { "helper", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 20 | { .name = NULL } |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 23 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 24 | helper_parse(int c, char **argv, int invert, unsigned int *flags, |
| 25 | const void *entry, struct xt_entry_match **match) |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 26 | { |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 27 | struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data; |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 28 | |
| 29 | switch (c) { |
| 30 | case '1': |
Nicolas Bouliane | 708f7b9 | 2005-01-02 23:34:48 +0000 | [diff] [blame] | 31 | if (*flags) |
| 32 | exit_error(PARAMETER_PROBLEM, |
| 33 | "helper match: Only use --helper ONCE!"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 34 | check_inverse(optarg, &invert, &invert, 0); |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 35 | strncpy(info->name, optarg, 29); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 36 | info->name[29] = '\0'; |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 37 | if (invert) |
| 38 | info->invert = 1; |
| 39 | *flags = 1; |
| 40 | break; |
| 41 | |
| 42 | default: |
| 43 | return 0; |
| 44 | } |
| 45 | return 1; |
| 46 | } |
| 47 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 48 | static void helper_check(unsigned int flags) |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 49 | { |
| 50 | if (!flags) |
| 51 | exit_error(PARAMETER_PROBLEM, |
| 52 | "helper match: You must specify `--helper'"); |
| 53 | } |
| 54 | |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 55 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 56 | helper_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 57 | { |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 58 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 59 | |
| 60 | printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name); |
| 61 | } |
| 62 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 63 | static void helper_save(const void *ip, const struct xt_entry_match *match) |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 64 | { |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 65 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 66 | |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 67 | printf("%s--helper ",info->invert ? "! " : ""); |
| 68 | save_string(info->name); |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 71 | static struct xtables_match helper_match = { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 72 | .family = NFPROTO_IPV4, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 73 | .name = "helper", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 74 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 75 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 76 | .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 KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 84 | static struct xtables_match helper_match6 = { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 85 | .family = NFPROTO_IPV6, |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 86 | .name = "helper", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 87 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 88 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 89 | .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 Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | void _init(void) |
| 98 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 99 | xtables_register_match(&helper_match); |
| 100 | xtables_register_match(&helper_match6); |
Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 101 | } |