Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables for the TTL target |
| 2 | * (C) 2000 by Harald Welte <laforge@gnumonks.org> |
| 3 | * |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 4 | * $Id$ |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is distributed under the terms of GNU GPL |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <getopt.h> |
| 12 | #include <iptables.h> |
| 13 | |
| 14 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 15 | #include <linux/netfilter_ipv4/ipt_TTL.h> |
| 16 | |
| 17 | #define IPT_TTL_USED 1 |
| 18 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 19 | static void TTL_help(void) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 20 | { |
| 21 | printf( |
| 22 | "TTL target v%s options\n" |
Nicolas Bouliane | 37fd00d | 2004-07-27 21:46:21 +0000 | [diff] [blame] | 23 | " --ttl-set value Set TTL to <value 0-255>\n" |
| 24 | " --ttl-dec value Decrement TTL by <value 1-255>\n" |
| 25 | " --ttl-inc value Increment TTL by <value 1-255>\n" |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 26 | , IPTABLES_VERSION); |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 29 | static int TTL_parse(int c, char **argv, int invert, unsigned int *flags, |
| 30 | const void *entry, struct xt_entry_target **target) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 31 | { |
| 32 | struct ipt_TTL_info *info = (struct ipt_TTL_info *) (*target)->data; |
Nicolas Bouliane | 37fd00d | 2004-07-27 21:46:21 +0000 | [diff] [blame] | 33 | unsigned int value; |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 34 | |
| 35 | if (*flags & IPT_TTL_USED) { |
| 36 | exit_error(PARAMETER_PROBLEM, |
| 37 | "Can't specify TTL option twice"); |
| 38 | } |
| 39 | |
| 40 | if (!optarg) |
| 41 | exit_error(PARAMETER_PROBLEM, |
| 42 | "TTL: You must specify a value"); |
| 43 | |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 44 | if (check_inverse(optarg, &invert, NULL, 0)) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 45 | exit_error(PARAMETER_PROBLEM, |
| 46 | "TTL: unexpected `!'"); |
| 47 | |
Nicolas Bouliane | 37fd00d | 2004-07-27 21:46:21 +0000 | [diff] [blame] | 48 | if (string_to_number(optarg, 0, 255, &value) == -1) |
| 49 | exit_error(PARAMETER_PROBLEM, |
| 50 | "TTL: Expected value between 0 and 255"); |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 51 | |
| 52 | switch (c) { |
| 53 | |
| 54 | case '1': |
| 55 | info->mode = IPT_TTL_SET; |
| 56 | break; |
| 57 | |
| 58 | case '2': |
| 59 | if (value == 0) { |
| 60 | exit_error(PARAMETER_PROBLEM, |
| 61 | "TTL: decreasing by 0?"); |
| 62 | } |
| 63 | |
| 64 | info->mode = IPT_TTL_DEC; |
| 65 | break; |
| 66 | |
| 67 | case '3': |
| 68 | if (value == 0) { |
| 69 | exit_error(PARAMETER_PROBLEM, |
| 70 | "TTL: increasing by 0?"); |
| 71 | } |
| 72 | |
| 73 | info->mode = IPT_TTL_INC; |
| 74 | break; |
| 75 | |
| 76 | default: |
| 77 | return 0; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | info->ttl = value; |
| 82 | *flags |= IPT_TTL_USED; |
| 83 | |
| 84 | return 1; |
| 85 | } |
| 86 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 87 | static void TTL_check(unsigned int flags) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 88 | { |
| 89 | if (!(flags & IPT_TTL_USED)) |
| 90 | exit_error(PARAMETER_PROBLEM, |
| 91 | "TTL: You must specify an action"); |
| 92 | } |
| 93 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 94 | static void TTL_save(const void *ip, const struct xt_entry_target *target) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 95 | { |
| 96 | const struct ipt_TTL_info *info = |
| 97 | (struct ipt_TTL_info *) target->data; |
| 98 | |
| 99 | switch (info->mode) { |
| 100 | case IPT_TTL_SET: |
| 101 | printf("--ttl-set "); |
| 102 | break; |
| 103 | case IPT_TTL_DEC: |
| 104 | printf("--ttl-dec "); |
| 105 | break; |
| 106 | |
| 107 | case IPT_TTL_INC: |
| 108 | printf("--ttl-inc "); |
| 109 | break; |
| 110 | } |
| 111 | printf("%u ", info->ttl); |
| 112 | } |
| 113 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 114 | static void TTL_print(const void *ip, const struct xt_entry_target *target, |
| 115 | int numeric) |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 116 | { |
| 117 | const struct ipt_TTL_info *info = |
| 118 | (struct ipt_TTL_info *) target->data; |
| 119 | |
| 120 | printf("TTL "); |
| 121 | switch (info->mode) { |
| 122 | case IPT_TTL_SET: |
| 123 | printf("set to "); |
| 124 | break; |
| 125 | case IPT_TTL_DEC: |
| 126 | printf("decrement by "); |
| 127 | break; |
| 128 | case IPT_TTL_INC: |
| 129 | printf("increment by "); |
| 130 | break; |
| 131 | } |
| 132 | printf("%u ", info->ttl); |
| 133 | } |
| 134 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 135 | static const struct option TTL_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 136 | { "ttl-set", 1, NULL, '1' }, |
| 137 | { "ttl-dec", 1, NULL, '2' }, |
| 138 | { "ttl-inc", 1, NULL, '3' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 139 | { .name = NULL } |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 142 | static struct iptables_target ttl_target = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 143 | .next = NULL, |
| 144 | .name = "TTL", |
| 145 | .version = IPTABLES_VERSION, |
| 146 | .size = IPT_ALIGN(sizeof(struct ipt_TTL_info)), |
| 147 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_TTL_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 148 | .help = TTL_help, |
| 149 | .parse = TTL_parse, |
| 150 | .final_check = TTL_check, |
| 151 | .print = TTL_print, |
| 152 | .save = TTL_save, |
| 153 | .extra_opts = TTL_opts, |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | void _init(void) |
| 157 | { |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 158 | register_target(&ttl_target); |
Harald Welte | 703828f | 2000-10-04 15:27:07 +0000 | [diff] [blame] | 159 | } |