Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables to add NFMARK 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 <ip6tables.h> |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 9 | /* For 64bit kernel / 32bit userspace */ |
| 10 | #include "../include/linux/netfilter_ipv6/ip6t_mark.h" |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 11 | |
| 12 | /* Function which prints out usage message. */ |
| 13 | static void |
| 14 | help(void) |
| 15 | { |
| 16 | printf( |
| 17 | "MARK match v%s options:\n" |
| 18 | "[!] --mark value[/mask] Match nfmark value with optional mask\n" |
| 19 | "\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 20 | IPTABLES_VERSION); |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static struct option opts[] = { |
| 24 | { "mark", 1, 0, '1' }, |
| 25 | {0} |
| 26 | }; |
| 27 | |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 28 | /* Function which parses command options; returns true if it |
| 29 | ate an option */ |
| 30 | static int |
| 31 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 32 | const struct ip6t_entry *entry, |
| 33 | unsigned int *nfcache, |
| 34 | struct ip6t_entry_match **match) |
| 35 | { |
| 36 | struct ip6t_mark_info *markinfo = (struct ip6t_mark_info *)(*match)->data; |
| 37 | |
| 38 | switch (c) { |
| 39 | char *end; |
| 40 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 41 | check_inverse(optarg, &invert, &optind, 0); |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 42 | markinfo->mark = strtoul(optarg, &end, 0); |
| 43 | if (*end == '/') { |
| 44 | markinfo->mask = strtoul(end+1, &end, 0); |
| 45 | } else |
| 46 | markinfo->mask = 0xffffffff; |
| 47 | if (*end != '\0' || end == optarg) |
| 48 | exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg); |
| 49 | if (invert) |
| 50 | markinfo->invert = 1; |
| 51 | *flags = 1; |
| 52 | break; |
| 53 | |
| 54 | default: |
| 55 | return 0; |
| 56 | } |
| 57 | return 1; |
| 58 | } |
| 59 | |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 60 | static void |
| 61 | print_mark(unsigned long mark, unsigned long mask, int numeric) |
| 62 | { |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 63 | if(mask != 0xffffffff) |
| 64 | printf("0x%lx/0x%lx ", mark, mask); |
| 65 | else |
| 66 | printf("0x%lx ", mark); |
| 67 | } |
| 68 | |
| 69 | /* Final check; must have specified --mark. */ |
| 70 | static void |
| 71 | final_check(unsigned int flags) |
| 72 | { |
| 73 | if (!flags) |
| 74 | exit_error(PARAMETER_PROBLEM, |
| 75 | "MARK match: You must specify `--mark'"); |
| 76 | } |
| 77 | |
| 78 | /* Prints out the matchinfo. */ |
| 79 | static void |
| 80 | print(const struct ip6t_ip6 *ip, |
| 81 | const struct ip6t_entry_match *match, |
| 82 | int numeric) |
| 83 | { |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 84 | struct ip6t_mark_info *info = (struct ip6t_mark_info *)match->data; |
| 85 | |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 86 | printf("MARK match "); |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 87 | |
| 88 | if (info->invert) |
| 89 | printf("!"); |
| 90 | |
| 91 | print_mark(info->mark, info->mask, numeric); |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* Saves the union ip6t_matchinfo in parsable form to stdout. */ |
| 95 | static void |
| 96 | save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match) |
| 97 | { |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 98 | struct ip6t_mark_info *info = (struct ip6t_mark_info *)match->data; |
| 99 | |
| 100 | if (info->invert) |
| 101 | printf("! "); |
| 102 | |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 103 | printf("--mark "); |
Martin Josefsson | c5617bf | 2004-05-26 21:56:26 +0000 | [diff] [blame] | 104 | print_mark(info->mark, info->mask, 0); |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 107 | static struct ip6tables_match mark = { |
| 108 | .name = "mark", |
| 109 | .version = IPTABLES_VERSION, |
| 110 | .size = IP6T_ALIGN(sizeof(struct ip6t_mark_info)), |
| 111 | .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_mark_info)), |
| 112 | .help = &help, |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 113 | .parse = &parse, |
| 114 | .final_check = &final_check, |
| 115 | .print = &print, |
| 116 | .save = &save, |
| 117 | .extra_opts = opts, |
Harald Welte | 7559e07 | 2000-11-16 11:49:38 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | void _init(void) |
| 121 | { |
| 122 | register_match6(&mark); |
| 123 | } |