Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 Hop Limit matching module |
| 3 | * Maciej Soltysiak <solt@dns.toxicfilms.tv> |
| 4 | * Based on HW's ttl match |
| 5 | * This program is released under the terms of GNU GPL |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 6 | * Cleanups by Stephane Ouellette <ouellettes@videotron.ca> |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <getopt.h> |
| 13 | #include <ip6tables.h> |
| 14 | |
| 15 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 16 | #include <linux/netfilter_ipv6/ip6t_hl.h> |
| 17 | |
| 18 | static void help(void) |
| 19 | { |
| 20 | printf( |
| 21 | "HL match v%s options:\n" |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 22 | " --hl-eq [!] value Match hop limit value\n" |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 23 | " --hl-lt value Match HL < value\n" |
| 24 | " --hl-gt value Match HL > value\n" |
| 25 | , IPTABLES_VERSION); |
| 26 | } |
| 27 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 28 | static int parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame] | 29 | const void *entry, unsigned int *nfcache, |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 30 | struct xt_entry_match **match) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 31 | { |
| 32 | struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data; |
| 33 | u_int8_t value; |
| 34 | |
| 35 | check_inverse(optarg, &invert, &optind, 0); |
| 36 | value = atoi(argv[optind-1]); |
| 37 | |
| 38 | if (*flags) |
| 39 | exit_error(PARAMETER_PROBLEM, |
| 40 | "Can't specify HL option twice"); |
| 41 | |
| 42 | if (!optarg) |
| 43 | exit_error(PARAMETER_PROBLEM, |
| 44 | "hl: You must specify a value"); |
| 45 | switch (c) { |
| 46 | case '2': |
| 47 | if (invert) |
| 48 | info->mode = IP6T_HL_NE; |
| 49 | else |
| 50 | info->mode = IP6T_HL_EQ; |
| 51 | |
| 52 | /* is 0 allowed? */ |
| 53 | info->hop_limit = value; |
| 54 | *flags = 1; |
| 55 | |
| 56 | break; |
| 57 | case '3': |
| 58 | if (invert) |
| 59 | exit_error(PARAMETER_PROBLEM, |
| 60 | "hl: unexpected `!'"); |
| 61 | |
| 62 | info->mode = IP6T_HL_LT; |
| 63 | info->hop_limit = value; |
| 64 | *flags = 1; |
| 65 | |
| 66 | break; |
| 67 | case '4': |
| 68 | if (invert) |
| 69 | exit_error(PARAMETER_PROBLEM, |
| 70 | "hl: unexpected `!'"); |
| 71 | |
| 72 | info->mode = IP6T_HL_GT; |
| 73 | info->hop_limit = value; |
| 74 | *flags = 1; |
| 75 | |
| 76 | break; |
| 77 | default: |
| 78 | return 0; |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | static void final_check(unsigned int flags) |
| 85 | { |
| 86 | if (!flags) |
| 87 | exit_error(PARAMETER_PROBLEM, |
| 88 | "HL match: You must specify one of " |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 89 | "`--hl-eq', `--hl-lt', `--hl-gt'"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame] | 92 | static void print(const void *ip, |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 93 | const struct xt_entry_match *match, |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 94 | int numeric) |
| 95 | { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 96 | static const char *op[] = { |
| 97 | [IP6T_HL_EQ] = "==", |
| 98 | [IP6T_HL_NE] = "!=", |
| 99 | [IP6T_HL_LT] = "<", |
| 100 | [IP6T_HL_GT] = ">" }; |
| 101 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 102 | const struct ip6t_hl_info *info = |
| 103 | (struct ip6t_hl_info *) match->data; |
| 104 | |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 105 | printf("HL match HL %s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame] | 108 | static void save(const void *ip, |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 109 | const struct xt_entry_match *match) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 110 | { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 111 | static const char *op[] = { |
| 112 | [IP6T_HL_EQ] = "eq", |
| 113 | [IP6T_HL_NE] = "eq !", |
| 114 | [IP6T_HL_LT] = "lt", |
| 115 | [IP6T_HL_GT] = "gt" }; |
| 116 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 117 | const struct ip6t_hl_info *info = |
| 118 | (struct ip6t_hl_info *) match->data; |
| 119 | |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 120 | printf("--hl-%s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct option opts[] = { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 124 | { .name = "hl", .has_arg = 1, .flag = 0, .val = '2' }, |
| 125 | { .name = "hl-eq", .has_arg = 1, .flag = 0, .val = '2' }, |
| 126 | { .name = "hl-lt", .has_arg = 1, .flag = 0, .val = '3' }, |
| 127 | { .name = "hl-gt", .has_arg = 1, .flag = 0, .val = '4' }, |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 128 | { 0 } |
| 129 | }; |
| 130 | |
| 131 | static |
| 132 | struct ip6tables_match hl = { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 133 | .name = "hl", |
| 134 | .version = IPTABLES_VERSION, |
| 135 | .size = IP6T_ALIGN(sizeof(struct ip6t_hl_info)), |
| 136 | .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_hl_info)), |
| 137 | .help = &help, |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 138 | .parse = &parse, |
| 139 | .final_check = &final_check, |
| 140 | .print = &print, |
| 141 | .save = &save, |
| 142 | .extra_opts = opts |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | |
| 146 | void _init(void) |
| 147 | { |
| 148 | register_match6(&hl); |
| 149 | } |