| 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 | |
| 11 | /* Function which prints out usage message. */ |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 12 | static void helper_help(void) |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 13 | { |
| 14 | printf( |
| 15 | "helper match v%s options:\n" |
| Hervé Eychenne | 9230c11 | 2003-03-03 22:23:22 +0000 | [diff] [blame] | 16 | "[!] --helper string Match helper identified by string\n" |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 17 | "\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 18 | IPTABLES_VERSION); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 21 | static const struct option helper_opts[] = { |
| Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 22 | { "helper", 1, NULL, '1' }, |
| 23 | { } |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 26 | /* Function which parses command options; returns true if it |
| 27 | ate an option */ |
| 28 | static int |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 29 | helper_parse(int c, char **argv, int invert, unsigned int *flags, |
| 30 | const void *entry, struct xt_entry_match **match) |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 31 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 32 | struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 33 | |
| 34 | switch (c) { |
| 35 | case '1': |
| Nicolas Bouliane | 708f7b9 | 2005-01-02 23:34:48 +0000 | [diff] [blame] | 36 | if (*flags) |
| 37 | exit_error(PARAMETER_PROBLEM, |
| 38 | "helper match: Only use --helper ONCE!"); |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 39 | check_inverse(optarg, &invert, &invert, 0); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 40 | strncpy(info->name, optarg, 29); |
| Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 41 | info->name[29] = '\0'; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 42 | 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 Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 54 | static void helper_check(unsigned int flags) |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 55 | { |
| 56 | if (!flags) |
| 57 | exit_error(PARAMETER_PROBLEM, |
| 58 | "helper match: You must specify `--helper'"); |
| 59 | } |
| 60 | |
| 61 | /* Prints out the info. */ |
| 62 | static void |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 63 | 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] | 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 | |
| 67 | printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name); |
| 68 | } |
| 69 | |
| 70 | /* Saves the union ipt_info in parsable form to stdout. */ |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 71 | 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] | 72 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 73 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 74 | |
| 75 | printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name); |
| 76 | } |
| 77 | |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 78 | static struct xtables_match helper_match = { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 79 | .family = AF_INET, |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 80 | .name = "helper", |
| 81 | .version = IPTABLES_VERSION, |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 82 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 83 | .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 KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 91 | static struct xtables_match helper_match6 = { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 92 | .family = AF_INET6, |
| 93 | .name = "helper", |
| 94 | .version = IPTABLES_VERSION, |
| 95 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 96 | .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 Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | void _init(void) |
| 105 | { |
| Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 106 | xtables_register_match(&helper_match); |
| 107 | xtables_register_match(&helper_match6); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 108 | } |