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> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 13 | #include <xtables.h> |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 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( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | "hl match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [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" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 24 | " --hl-gt value Match HL > value\n"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 27 | static int hl_parse(int c, char **argv, int invert, unsigned int *flags, |
| 28 | const void *entry, struct xt_entry_match **match) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 29 | { |
| 30 | struct ip6t_hl_info *info = (struct ip6t_hl_info *) (*match)->data; |
| 31 | u_int8_t value; |
| 32 | |
| 33 | check_inverse(optarg, &invert, &optind, 0); |
| 34 | value = atoi(argv[optind-1]); |
| 35 | |
| 36 | if (*flags) |
| 37 | exit_error(PARAMETER_PROBLEM, |
| 38 | "Can't specify HL option twice"); |
| 39 | |
| 40 | if (!optarg) |
| 41 | exit_error(PARAMETER_PROBLEM, |
| 42 | "hl: You must specify a value"); |
| 43 | switch (c) { |
| 44 | case '2': |
| 45 | if (invert) |
| 46 | info->mode = IP6T_HL_NE; |
| 47 | else |
| 48 | info->mode = IP6T_HL_EQ; |
| 49 | |
| 50 | /* is 0 allowed? */ |
| 51 | info->hop_limit = value; |
| 52 | *flags = 1; |
| 53 | |
| 54 | break; |
| 55 | case '3': |
| 56 | if (invert) |
| 57 | exit_error(PARAMETER_PROBLEM, |
| 58 | "hl: unexpected `!'"); |
| 59 | |
| 60 | info->mode = IP6T_HL_LT; |
| 61 | info->hop_limit = value; |
| 62 | *flags = 1; |
| 63 | |
| 64 | break; |
| 65 | case '4': |
| 66 | if (invert) |
| 67 | exit_error(PARAMETER_PROBLEM, |
| 68 | "hl: unexpected `!'"); |
| 69 | |
| 70 | info->mode = IP6T_HL_GT; |
| 71 | info->hop_limit = value; |
| 72 | *flags = 1; |
| 73 | |
| 74 | break; |
| 75 | default: |
| 76 | return 0; |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return 1; |
| 80 | } |
| 81 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 82 | static void hl_check(unsigned int flags) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 83 | { |
| 84 | if (!flags) |
| 85 | exit_error(PARAMETER_PROBLEM, |
| 86 | "HL match: You must specify one of " |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 87 | "`--hl-eq', `--hl-lt', `--hl-gt'"); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 90 | static void hl_print(const void *ip, const struct xt_entry_match *match, |
| 91 | int numeric) |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 92 | { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 93 | static const char *op[] = { |
| 94 | [IP6T_HL_EQ] = "==", |
| 95 | [IP6T_HL_NE] = "!=", |
| 96 | [IP6T_HL_LT] = "<", |
| 97 | [IP6T_HL_GT] = ">" }; |
| 98 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 99 | const struct ip6t_hl_info *info = |
| 100 | (struct ip6t_hl_info *) match->data; |
| 101 | |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 102 | printf("HL match HL %s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 105 | 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] | 106 | { |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame^] | 107 | static const char *const op[] = { |
| 108 | [IP6T_HL_EQ] = "--hl-eq", |
| 109 | [IP6T_HL_NE] = "! --hl-eq", |
| 110 | [IP6T_HL_LT] = "--hl-lt", |
| 111 | [IP6T_HL_GT] = "--hl-gt" }; |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 112 | |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 113 | const struct ip6t_hl_info *info = |
| 114 | (struct ip6t_hl_info *) match->data; |
| 115 | |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame^] | 116 | printf("%s %u ", op[info->mode], info->hop_limit); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 119 | static const struct option hl_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 120 | { .name = "hl", .has_arg = 1, .val = '2' }, |
| 121 | { .name = "hl-eq", .has_arg = 1, .val = '2' }, |
| 122 | { .name = "hl-lt", .has_arg = 1, .val = '3' }, |
| 123 | { .name = "hl-gt", .has_arg = 1, .val = '4' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 124 | { .name = NULL } |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 127 | static struct xtables_match hl_mt6_reg = { |
Stephane Ouellette | 46525cd | 2003-05-07 20:08:36 +0000 | [diff] [blame] | 128 | .name = "hl", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 129 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 130 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 131 | .size = XT_ALIGN(sizeof(struct ip6t_hl_info)), |
| 132 | .userspacesize = XT_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 | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 144 | xtables_register_match(&hl_mt6_reg); |
Maciej Soltysiak | 60358d7 | 2003-01-08 09:14:20 +0000 | [diff] [blame] | 145 | } |