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