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 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 18 | static void hl_help(void) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 28 | static int hl_parse(int c, char **argv, int invert, unsigned int *flags, |
| 29 | const void *entry, struct xt_entry_match **match) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 30 | { |
| 31 | struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data; |
| 32 | u_int8_t value; |
| 33 | |
| 34 | check_inverse(optarg, &invert, &optind, 0); |
| 35 | value = atoi(argv[optind-1]); |
| 36 | |
| 37 | if (*flags) |
| 38 | exit_error(PARAMETER_PROBLEM, |
| 39 | "Can't specify HL option twice"); |
| 40 | |
| 41 | if (!optarg) |
| 42 | exit_error(PARAMETER_PROBLEM, |
| 43 | "hl: You must specify a value"); |
| 44 | switch (c) { |
| 45 | case '2': |
| 46 | if (invert) |
| 47 | info->mode = IP6T_HL_NE; |
| 48 | else |
| 49 | info->mode = IP6T_HL_EQ; |
| 50 | |
| 51 | /* is 0 allowed? */ |
| 52 | info->hop_limit = value; |
| 53 | *flags = 1; |
| 54 | |
| 55 | break; |
| 56 | case '3': |
| 57 | if (invert) |
| 58 | exit_error(PARAMETER_PROBLEM, |
| 59 | "hl: unexpected `!'"); |
| 60 | |
| 61 | info->mode = IP6T_HL_LT; |
| 62 | info->hop_limit = value; |
| 63 | *flags = 1; |
| 64 | |
| 65 | break; |
| 66 | case '4': |
| 67 | if (invert) |
| 68 | exit_error(PARAMETER_PROBLEM, |
| 69 | "hl: unexpected `!'"); |
| 70 | |
| 71 | info->mode = IP6T_HL_GT; |
| 72 | info->hop_limit = value; |
| 73 | *flags = 1; |
| 74 | |
| 75 | break; |
| 76 | default: |
| 77 | return 0; |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 83 | static void hl_check(unsigned int flags) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 84 | { |
| 85 | if (!flags) |
| 86 | exit_error(PARAMETER_PROBLEM, |
| 87 | "HL match: You must specify one of " |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 88 | "`--hl-eq', `--hl-lt', `--hl-gt'"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 91 | static void hl_print(const void *ip, const struct xt_entry_match *match, |
| 92 | int numeric) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 93 | { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 94 | static const char *op[] = { |
| 95 | [IP6T_HL_EQ] = "==", |
| 96 | [IP6T_HL_NE] = "!=", |
| 97 | [IP6T_HL_LT] = "<", |
| 98 | [IP6T_HL_GT] = ">" }; |
| 99 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 100 | const struct ip6t_hl_info *info = |
| 101 | (struct ip6t_hl_info *) match->data; |
| 102 | |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 103 | printf("HL match HL %s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 106 | static void hl_save(const void *ip, const struct xt_entry_match *match) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 107 | { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 108 | static const char *op[] = { |
| 109 | [IP6T_HL_EQ] = "eq", |
| 110 | [IP6T_HL_NE] = "eq !", |
| 111 | [IP6T_HL_LT] = "lt", |
| 112 | [IP6T_HL_GT] = "gt" }; |
| 113 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 114 | const struct ip6t_hl_info *info = |
| 115 | (struct ip6t_hl_info *) match->data; |
| 116 | |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 117 | printf("--hl-%s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 120 | static const struct option hl_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 121 | { .name = "hl", .has_arg = 1, .val = '2' }, |
| 122 | { .name = "hl-eq", .has_arg = 1, .val = '2' }, |
| 123 | { .name = "hl-lt", .has_arg = 1, .val = '3' }, |
| 124 | { .name = "hl-gt", .has_arg = 1, .val = '4' }, |
| 125 | { } |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 128 | static struct ip6tables_match hl_match6 = { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 129 | .name = "hl", |
| 130 | .version = IPTABLES_VERSION, |
| 131 | .size = IP6T_ALIGN(sizeof(struct ip6t_hl_info)), |
| 132 | .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_hl_info)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 133 | .help = hl_help, |
| 134 | .parse = hl_parse, |
| 135 | .final_check = hl_check, |
| 136 | .print = hl_print, |
| 137 | .save = hl_save, |
| 138 | .extra_opts = hl_opts, |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | |
| 142 | void _init(void) |
| 143 | { |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 144 | register_match6(&hl_match6); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 145 | } |