lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [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 | |
| 8 | #include <xtables.h> |
| 9 | #include <linux/netfilter/xt_helper.h> |
| 10 | |
| 11 | static void helper_help(void) |
| 12 | { |
| 13 | printf( |
| 14 | "helper match options:\n" |
| 15 | "[!] --helper string Match helper identified by string\n"); |
| 16 | } |
| 17 | |
| 18 | static const struct option helper_opts[] = { |
| 19 | { "helper", 1, NULL, '1' }, |
| 20 | { .name = NULL } |
| 21 | }; |
| 22 | |
| 23 | static int |
| 24 | helper_parse(int c, char **argv, int invert, unsigned int *flags, |
| 25 | const void *entry, struct xt_entry_match **match) |
| 26 | { |
| 27 | struct xt_helper_info *info = (struct xt_helper_info *)(*match)->data; |
| 28 | |
| 29 | switch (c) { |
| 30 | case '1': |
| 31 | if (*flags) |
| 32 | xtables_error(PARAMETER_PROBLEM, |
| 33 | "helper match: Only use --helper ONCE!"); |
| 34 | xtables_check_inverse(optarg, &invert, &invert, 0); |
| 35 | strncpy(info->name, optarg, 29); |
| 36 | info->name[29] = '\0'; |
| 37 | if (invert) |
| 38 | info->invert = 1; |
| 39 | *flags = 1; |
| 40 | break; |
| 41 | |
| 42 | default: |
| 43 | return 0; |
| 44 | } |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | static void helper_check(unsigned int flags) |
| 49 | { |
| 50 | if (!flags) |
| 51 | xtables_error(PARAMETER_PROBLEM, |
| 52 | "helper match: You must specify `--helper'"); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | helper_print(const void *ip, const struct xt_entry_match *match, int numeric) |
| 57 | { |
| 58 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
| 59 | |
| 60 | printf("helper match %s\"%s\" ", info->invert ? "! " : "", info->name); |
| 61 | } |
| 62 | |
| 63 | static void helper_save(const void *ip, const struct xt_entry_match *match) |
| 64 | { |
| 65 | struct xt_helper_info *info = (struct xt_helper_info *)match->data; |
| 66 | |
| 67 | printf("%s--helper ",info->invert ? "! " : ""); |
| 68 | xtables_save_string(info->name); |
| 69 | } |
| 70 | |
| 71 | static struct xtables_match helper_match = { |
| 72 | .family = NFPROTO_IPV4, |
| 73 | .name = "helper", |
| 74 | .version = XTABLES_VERSION, |
| 75 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
| 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, |
| 82 | }; |
| 83 | |
| 84 | static struct xtables_match helper_match6 = { |
| 85 | .family = NFPROTO_IPV6, |
| 86 | .name = "helper", |
| 87 | .version = XTABLES_VERSION, |
| 88 | .size = XT_ALIGN(sizeof(struct xt_helper_info)), |
| 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, |
| 95 | }; |
| 96 | |
| 97 | void _init(void) |
| 98 | { |
| 99 | xtables_register_match(&helper_match); |
| 100 | xtables_register_match(&helper_match6); |
| 101 | } |