| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Shared library add-on to iptables for the TTL target | 
|  | 2 | * (C) 2000 by Harald Welte <laforge@gnumonks.org> | 
|  | 3 | * | 
|  | 4 | * $Id$ | 
|  | 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 <xtables.h> | 
|  | 13 |  | 
|  | 14 | #include <linux/netfilter_ipv4/ipt_TTL.h> | 
|  | 15 |  | 
|  | 16 | #define IPT_TTL_USED	1 | 
|  | 17 |  | 
|  | 18 | static void TTL_help(void) | 
|  | 19 | { | 
|  | 20 | printf( | 
|  | 21 | "TTL target options\n" | 
|  | 22 | "  --ttl-set value		Set TTL to <value 0-255>\n" | 
|  | 23 | "  --ttl-dec value		Decrement TTL by <value 1-255>\n" | 
|  | 24 | "  --ttl-inc value		Increment TTL by <value 1-255>\n"); | 
|  | 25 | } | 
|  | 26 |  | 
|  | 27 | static int TTL_parse(int c, char **argv, int invert, unsigned int *flags, | 
|  | 28 | const void *entry, struct xt_entry_target **target) | 
|  | 29 | { | 
|  | 30 | struct ipt_TTL_info *info = (struct ipt_TTL_info *) (*target)->data; | 
|  | 31 | unsigned int value; | 
|  | 32 |  | 
|  | 33 | if (*flags & IPT_TTL_USED) { | 
|  | 34 | xtables_error(PARAMETER_PROBLEM, | 
|  | 35 | "Can't specify TTL option twice"); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | if (!optarg) | 
|  | 39 | xtables_error(PARAMETER_PROBLEM, | 
|  | 40 | "TTL: You must specify a value"); | 
|  | 41 |  | 
|  | 42 | if (xtables_check_inverse(optarg, &invert, NULL, 0)) | 
|  | 43 | xtables_error(PARAMETER_PROBLEM, | 
|  | 44 | "TTL: unexpected `!'"); | 
|  | 45 |  | 
|  | 46 | if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX)) | 
|  | 47 | xtables_error(PARAMETER_PROBLEM, | 
|  | 48 | "TTL: Expected value between 0 and 255"); | 
|  | 49 |  | 
|  | 50 | switch (c) { | 
|  | 51 |  | 
|  | 52 | case '1': | 
|  | 53 | info->mode = IPT_TTL_SET; | 
|  | 54 | break; | 
|  | 55 |  | 
|  | 56 | case '2': | 
|  | 57 | if (value == 0) { | 
|  | 58 | xtables_error(PARAMETER_PROBLEM, | 
|  | 59 | "TTL: decreasing by 0?"); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | info->mode = IPT_TTL_DEC; | 
|  | 63 | break; | 
|  | 64 |  | 
|  | 65 | case '3': | 
|  | 66 | if (value == 0) { | 
|  | 67 | xtables_error(PARAMETER_PROBLEM, | 
|  | 68 | "TTL: increasing by 0?"); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | info->mode = IPT_TTL_INC; | 
|  | 72 | break; | 
|  | 73 |  | 
|  | 74 | default: | 
|  | 75 | return 0; | 
|  | 76 |  | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | info->ttl = value; | 
|  | 80 | *flags |= IPT_TTL_USED; | 
|  | 81 |  | 
|  | 82 | return 1; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | static void TTL_check(unsigned int flags) | 
|  | 86 | { | 
|  | 87 | if (!(flags & IPT_TTL_USED)) | 
|  | 88 | xtables_error(PARAMETER_PROBLEM, | 
|  | 89 | "TTL: You must specify an action"); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | static void TTL_save(const void *ip, const struct xt_entry_target *target) | 
|  | 93 | { | 
|  | 94 | const struct ipt_TTL_info *info = | 
|  | 95 | (struct ipt_TTL_info *) target->data; | 
|  | 96 |  | 
|  | 97 | switch (info->mode) { | 
|  | 98 | case IPT_TTL_SET: | 
|  | 99 | printf("--ttl-set "); | 
|  | 100 | break; | 
|  | 101 | case IPT_TTL_DEC: | 
|  | 102 | printf("--ttl-dec "); | 
|  | 103 | break; | 
|  | 104 |  | 
|  | 105 | case IPT_TTL_INC: | 
|  | 106 | printf("--ttl-inc "); | 
|  | 107 | break; | 
|  | 108 | } | 
|  | 109 | printf("%u ", info->ttl); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static void TTL_print(const void *ip, const struct xt_entry_target *target, | 
|  | 113 | int numeric) | 
|  | 114 | { | 
|  | 115 | const struct ipt_TTL_info *info = | 
|  | 116 | (struct ipt_TTL_info *) target->data; | 
|  | 117 |  | 
|  | 118 | printf("TTL "); | 
|  | 119 | switch (info->mode) { | 
|  | 120 | case IPT_TTL_SET: | 
|  | 121 | printf("set to "); | 
|  | 122 | break; | 
|  | 123 | case IPT_TTL_DEC: | 
|  | 124 | printf("decrement by "); | 
|  | 125 | break; | 
|  | 126 | case IPT_TTL_INC: | 
|  | 127 | printf("increment by "); | 
|  | 128 | break; | 
|  | 129 | } | 
|  | 130 | printf("%u ", info->ttl); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | static const struct option TTL_opts[] = { | 
|  | 134 | { "ttl-set", 1, NULL, '1' }, | 
|  | 135 | { "ttl-dec", 1, NULL, '2' }, | 
|  | 136 | { "ttl-inc", 1, NULL, '3' }, | 
|  | 137 | { .name = NULL } | 
|  | 138 | }; | 
|  | 139 |  | 
|  | 140 | static struct xtables_target ttl_tg_reg = { | 
|  | 141 | .name		= "TTL", | 
|  | 142 | .version	= XTABLES_VERSION, | 
|  | 143 | .family		= NFPROTO_IPV4, | 
|  | 144 | .size		= XT_ALIGN(sizeof(struct ipt_TTL_info)), | 
|  | 145 | .userspacesize	= XT_ALIGN(sizeof(struct ipt_TTL_info)), | 
|  | 146 | .help		= TTL_help, | 
|  | 147 | .parse		= TTL_parse, | 
|  | 148 | .final_check	= TTL_check, | 
|  | 149 | .print		= TTL_print, | 
|  | 150 | .save		= TTL_save, | 
|  | 151 | .extra_opts	= TTL_opts, | 
|  | 152 | }; | 
|  | 153 |  | 
|  | 154 | void _init(void) | 
|  | 155 | { | 
|  | 156 | xtables_register_target(&ttl_tg_reg); | 
|  | 157 | } |