blob: 0f81280bd6e65b468e17119752a14dd839dc3504 [file] [log] [blame]
Harald Welte703828f2000-10-04 15:27:07 +00001/* Shared library add-on to iptables for the TTL target
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 *
Harald Welte703828f2000-10-04 15:27:07 +00004 * This program is distributed under the terms of GNU GPL
5 */
6#include <stdio.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01007#include <xtables.h>
Harald Welte703828f2000-10-04 15:27:07 +00008#include <linux/netfilter_ipv4/ipt_TTL.h>
9
Jan Engelhardtfa728c82011-02-13 03:31:54 +010010enum {
11 O_TTL_SET = 0,
12 O_TTL_INC,
13 O_TTL_DEC,
14 F_TTL_SET = 1 << O_TTL_SET,
15 F_TTL_INC = 1 << O_TTL_INC,
16 F_TTL_DEC = 1 << O_TTL_DEC,
17 F_ANY = F_TTL_SET | F_TTL_INC | F_TTL_DEC,
18};
19
20#define s struct ipt_TTL_info
21static const struct xt_option_entry TTL_opts[] = {
22 {.name = "ttl-set", .type = XTTYPE_UINT8, .id = O_TTL_SET,
23 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
24 {.name = "ttl-dec", .type = XTTYPE_UINT8, .id = O_TTL_DEC,
25 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
26 .min = 1},
27 {.name = "ttl-inc", .type = XTTYPE_UINT8, .id = O_TTL_INC,
28 .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
29 .min = 1},
30 XTOPT_TABLEEND,
31};
32#undef s
Harald Welte703828f2000-10-04 15:27:07 +000033
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000034static void TTL_help(void)
Harald Welte703828f2000-10-04 15:27:07 +000035{
36 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020037"TTL target options\n"
Nicolas Bouliane37fd00d2004-07-27 21:46:21 +000038" --ttl-set value Set TTL to <value 0-255>\n"
39" --ttl-dec value Decrement TTL by <value 1-255>\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020040" --ttl-inc value Increment TTL by <value 1-255>\n");
Harald Welte703828f2000-10-04 15:27:07 +000041}
42
Jan Engelhardtfa728c82011-02-13 03:31:54 +010043static void TTL_parse(struct xt_option_call *cb)
Harald Welte703828f2000-10-04 15:27:07 +000044{
Jan Engelhardtfa728c82011-02-13 03:31:54 +010045 struct ipt_TTL_info *info = cb->data;
Harald Welte703828f2000-10-04 15:27:07 +000046
Jan Engelhardtfa728c82011-02-13 03:31:54 +010047 xtables_option_parse(cb);
48 switch (cb->entry->id) {
49 case O_TTL_SET:
50 info->mode = IPT_TTL_SET;
51 break;
52 case O_TTL_DEC:
53 info->mode = IPT_TTL_DEC;
54 break;
55 case O_TTL_INC:
56 info->mode = IPT_TTL_INC;
57 break;
Harald Welte703828f2000-10-04 15:27:07 +000058 }
Harald Welte703828f2000-10-04 15:27:07 +000059}
60
Jan Engelhardtfa728c82011-02-13 03:31:54 +010061static void TTL_check(struct xt_fcheck_call *cb)
Harald Welte703828f2000-10-04 15:27:07 +000062{
Jan Engelhardtfa728c82011-02-13 03:31:54 +010063 if (!(cb->xflags & F_ANY))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010064 xtables_error(PARAMETER_PROBLEM,
Harald Welte703828f2000-10-04 15:27:07 +000065 "TTL: You must specify an action");
66}
67
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000068static void TTL_save(const void *ip, const struct xt_entry_target *target)
Harald Welte703828f2000-10-04 15:27:07 +000069{
70 const struct ipt_TTL_info *info =
71 (struct ipt_TTL_info *) target->data;
72
73 switch (info->mode) {
74 case IPT_TTL_SET:
Jan Engelhardt73866352010-12-18 02:04:59 +010075 printf(" --ttl-set");
Harald Welte703828f2000-10-04 15:27:07 +000076 break;
77 case IPT_TTL_DEC:
Jan Engelhardt73866352010-12-18 02:04:59 +010078 printf(" --ttl-dec");
Harald Welte703828f2000-10-04 15:27:07 +000079 break;
80
81 case IPT_TTL_INC:
Jan Engelhardt73866352010-12-18 02:04:59 +010082 printf(" --ttl-inc");
Harald Welte703828f2000-10-04 15:27:07 +000083 break;
84 }
Jan Engelhardt73866352010-12-18 02:04:59 +010085 printf(" %u", info->ttl);
Harald Welte703828f2000-10-04 15:27:07 +000086}
87
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000088static void TTL_print(const void *ip, const struct xt_entry_target *target,
89 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +000090{
91 const struct ipt_TTL_info *info =
92 (struct ipt_TTL_info *) target->data;
93
Jan Engelhardt73866352010-12-18 02:04:59 +010094 printf(" TTL ");
Harald Welte703828f2000-10-04 15:27:07 +000095 switch (info->mode) {
96 case IPT_TTL_SET:
Jan Engelhardt73866352010-12-18 02:04:59 +010097 printf("set to");
Harald Welte703828f2000-10-04 15:27:07 +000098 break;
99 case IPT_TTL_DEC:
Jan Engelhardt73866352010-12-18 02:04:59 +0100100 printf("decrement by");
Harald Welte703828f2000-10-04 15:27:07 +0000101 break;
102 case IPT_TTL_INC:
Jan Engelhardt73866352010-12-18 02:04:59 +0100103 printf("increment by");
Harald Welte703828f2000-10-04 15:27:07 +0000104 break;
105 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100106 printf(" %u", info->ttl);
Harald Welte703828f2000-10-04 15:27:07 +0000107}
108
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200109static struct xtables_target ttl_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000110 .name = "TTL",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200111 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100112 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200113 .size = XT_ALIGN(sizeof(struct ipt_TTL_info)),
114 .userspacesize = XT_ALIGN(sizeof(struct ipt_TTL_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000115 .help = TTL_help,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000116 .print = TTL_print,
117 .save = TTL_save,
Jan Engelhardtfa728c82011-02-13 03:31:54 +0100118 .x6_parse = TTL_parse,
119 .x6_fcheck = TTL_check,
120 .x6_options = TTL_opts,
Harald Welte703828f2000-10-04 15:27:07 +0000121};
122
123void _init(void)
124{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200125 xtables_register_target(&ttl_tg_reg);
Harald Welte703828f2000-10-04 15:27:07 +0000126}