| 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. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "helper match v%s options:\n" |
| Hervé Eychenne | 9230c11 | 2003-03-03 22:23:22 +0000 | [diff] [blame] | 17 | "[!] --helper string Match helper identified by string\n" |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 18 | "\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 19 | IPTABLES_VERSION); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| Jan Engelhardt | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame] | 22 | static const struct option opts[] = { |
| Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 23 | { "helper", 1, NULL, '1' }, |
| 24 | { } |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 27 | /* Function which parses command options; returns true if it |
| 28 | ate an option */ |
| 29 | static int |
| 30 | parse(int c, char **argv, int invert, unsigned int *flags, |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 31 | const void *entry, |
| Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 32 | struct xt_entry_match **match) |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 33 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 34 | struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 35 | |
| 36 | switch (c) { |
| 37 | case '1': |
| Nicolas Bouliane | 708f7b9 | 2005-01-02 23:34:48 +0000 | [diff] [blame] | 38 | if (*flags) |
| 39 | exit_error(PARAMETER_PROBLEM, |
| 40 | "helper match: Only use --helper ONCE!"); |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 41 | check_inverse(optarg, &invert, &invert, 0); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 42 | strncpy(info->name, optarg, 29); |
| Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 43 | info->name[29] = '\0'; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 44 | 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. */ |
| 56 | static void |
| 57 | final_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. */ |
| 65 | static void |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 66 | print(const void *ip, |
| Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 67 | const struct xt_entry_match *match, |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 68 | int numeric) |
| 69 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 70 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 71 | |
| 72 | printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name); |
| 73 | } |
| 74 | |
| 75 | /* Saves the union ipt_info in parsable form to stdout. */ |
| 76 | static void |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 77 | save(const void *ip, const struct xt_entry_match *match) |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 78 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 79 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 80 | |
| 81 | printf("%s--helper \"%s\" ",info->invert ? "! " : "", info->name); |
| 82 | } |
| 83 | |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 84 | static struct xtables_match helper = { |
| 85 | .family = AF_INET, |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 86 | .name = "helper", |
| 87 | .version = IPTABLES_VERSION, |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 88 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 89 | .help = &help, |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 90 | .parse = &parse, |
| 91 | .final_check = &final_check, |
| 92 | .print = &print, |
| 93 | .save = &save, |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 94 | .extra_opts = opts, |
| 95 | }; |
| 96 | |
| 97 | static 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 Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | void _init(void) |
| 111 | { |
| Yasuyuki KOZAKAI | f8137b1 | 2007-08-04 08:26:59 +0000 | [diff] [blame] | 112 | xtables_register_match(&helper); |
| 113 | xtables_register_match(&helper6); |
| Martin Josefsson | 110610b | 2002-01-11 18:41:02 +0000 | [diff] [blame] | 114 | } |