blob: 6370cb6748616722babef7285c00fd32740169c7 [file] [log] [blame]
Harald Welte703828f2000-10-04 15:27:07 +00001/* Shared library add-on to iptables to add TTL matching support
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 *
Harald Welte703828f2000-10-04 15:27:07 +00004 * This program is released under the terms of GNU GPL */
Harald Welte703828f2000-10-04 15:27:07 +00005#include <stdio.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01006#include <xtables.h>
Harald Welte703828f2000-10-04 15:27:07 +00007#include <linux/netfilter_ipv4/ipt_ttl.h>
8
Jan Engelhardtdba08392011-02-18 03:20:56 +01009enum {
10 O_TTL_EQ = 0,
11 O_TTL_LT,
12 O_TTL_GT,
13 F_TTL_EQ = 1 << O_TTL_EQ,
14 F_TTL_LT = 1 << O_TTL_LT,
15 F_TTL_GT = 1 << O_TTL_GT,
16 F_ANY = F_TTL_EQ | F_TTL_LT | F_TTL_GT,
17};
18
Jan Engelhardt59d16402007-10-04 16:28:39 +000019static void ttl_help(void)
Harald Welte703828f2000-10-04 15:27:07 +000020{
21 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022"ttl match options:\n"
Harald Welte1441c422000-11-13 12:32:50 +000023" --ttl-eq value Match time to live value\n"
24" --ttl-lt value Match TTL < value\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025" --ttl-gt value Match TTL > value\n");
Harald Welte703828f2000-10-04 15:27:07 +000026}
27
Jan Engelhardtdba08392011-02-18 03:20:56 +010028static void ttl_parse(struct xt_option_call *cb)
Harald Welte703828f2000-10-04 15:27:07 +000029{
Jan Engelhardtdba08392011-02-18 03:20:56 +010030 struct ipt_ttl_info *info = cb->data;
Harald Welte703828f2000-10-04 15:27:07 +000031
Jan Engelhardtdba08392011-02-18 03:20:56 +010032 xtables_option_parse(cb);
33 switch (cb->entry->id) {
34 case O_TTL_EQ:
35 info->mode = cb->invert ? IPT_TTL_NE : IPT_TTL_EQ;
36 break;
37 case O_TTL_LT:
38 info->mode = IPT_TTL_LT;
39 break;
40 case O_TTL_GT:
41 info->mode = IPT_TTL_GT;
42 break;
Harald Welte703828f2000-10-04 15:27:07 +000043 }
Harald Welte703828f2000-10-04 15:27:07 +000044}
45
Jan Engelhardtdba08392011-02-18 03:20:56 +010046static void ttl_check(struct xt_fcheck_call *cb)
Harald Welte703828f2000-10-04 15:27:07 +000047{
Jan Engelhardtdba08392011-02-18 03:20:56 +010048 if (!(cb->xflags & F_ANY))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010049 xtables_error(PARAMETER_PROBLEM,
Harald Welte1441c422000-11-13 12:32:50 +000050 "TTL match: You must specify one of "
51 "`--ttl-eq', `--ttl-lt', `--ttl-gt");
Harald Welte703828f2000-10-04 15:27:07 +000052}
53
Jan Engelhardt59d16402007-10-04 16:28:39 +000054static void ttl_print(const void *ip, const struct xt_entry_match *match,
55 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +000056{
57 const struct ipt_ttl_info *info =
58 (struct ipt_ttl_info *) match->data;
59
Jan Engelhardt73866352010-12-18 02:04:59 +010060 printf(" TTL match ");
Harald Welte1441c422000-11-13 12:32:50 +000061 switch (info->mode) {
62 case IPT_TTL_EQ:
Jan Engelhardt73866352010-12-18 02:04:59 +010063 printf("TTL ==");
Harald Welte1441c422000-11-13 12:32:50 +000064 break;
65 case IPT_TTL_NE:
Jan Engelhardt73866352010-12-18 02:04:59 +010066 printf("TTL !=");
Harald Welte1441c422000-11-13 12:32:50 +000067 break;
68 case IPT_TTL_LT:
Jan Engelhardt73866352010-12-18 02:04:59 +010069 printf("TTL <");
Harald Welte1441c422000-11-13 12:32:50 +000070 break;
71 case IPT_TTL_GT:
Jan Engelhardt73866352010-12-18 02:04:59 +010072 printf("TTL >");
Harald Welte1441c422000-11-13 12:32:50 +000073 break;
74 }
Jan Engelhardt73866352010-12-18 02:04:59 +010075 printf(" %u", info->ttl);
Harald Welte703828f2000-10-04 15:27:07 +000076}
77
Jan Engelhardt59d16402007-10-04 16:28:39 +000078static void ttl_save(const void *ip, const struct xt_entry_match *match)
Harald Welte703828f2000-10-04 15:27:07 +000079{
80 const struct ipt_ttl_info *info =
81 (struct ipt_ttl_info *) match->data;
82
Harald Welte1441c422000-11-13 12:32:50 +000083 switch (info->mode) {
84 case IPT_TTL_EQ:
Jan Engelhardt73866352010-12-18 02:04:59 +010085 printf(" --ttl-eq");
Harald Welte1441c422000-11-13 12:32:50 +000086 break;
87 case IPT_TTL_NE:
Jan Engelhardt73866352010-12-18 02:04:59 +010088 printf(" ! --ttl-eq");
Harald Welte1441c422000-11-13 12:32:50 +000089 break;
90 case IPT_TTL_LT:
Jan Engelhardt73866352010-12-18 02:04:59 +010091 printf(" --ttl-lt");
Harald Welte1441c422000-11-13 12:32:50 +000092 break;
93 case IPT_TTL_GT:
Jan Engelhardt73866352010-12-18 02:04:59 +010094 printf(" --ttl-gt");
Harald Welte1441c422000-11-13 12:32:50 +000095 break;
96 default:
97 /* error */
98 break;
99 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100100 printf(" %u", info->ttl);
Harald Welte703828f2000-10-04 15:27:07 +0000101}
102
Jan Engelhardtdba08392011-02-18 03:20:56 +0100103#define s struct ipt_ttl_info
104static const struct xt_option_entry ttl_opts[] = {
105 {.name = "ttl-lt", .id = O_TTL_LT, .excl = F_ANY, .type = XTTYPE_UINT8,
106 .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
107 {.name = "ttl-gt", .id = O_TTL_GT, .excl = F_ANY, .type = XTTYPE_UINT8,
108 .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
109 {.name = "ttl-eq", .id = O_TTL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
110 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, ttl)},
111 {.name = "ttl", .id = O_TTL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
112 .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
113 XTOPT_TABLEEND,
Harald Welte703828f2000-10-04 15:27:07 +0000114};
Jan Engelhardtdba08392011-02-18 03:20:56 +0100115#undef s
Harald Welte703828f2000-10-04 15:27:07 +0000116
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200117static struct xtables_match ttl_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000118 .name = "ttl",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200119 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100120 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200121 .size = XT_ALIGN(sizeof(struct ipt_ttl_info)),
122 .userspacesize = XT_ALIGN(sizeof(struct ipt_ttl_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000123 .help = ttl_help,
Jan Engelhardt59d16402007-10-04 16:28:39 +0000124 .print = ttl_print,
125 .save = ttl_save,
Jan Engelhardtdba08392011-02-18 03:20:56 +0100126 .x6_parse = ttl_parse,
127 .x6_fcheck = ttl_check,
128 .x6_options = ttl_opts,
Harald Welte703828f2000-10-04 15:27:07 +0000129};
130
131
132void _init(void)
133{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200134 xtables_register_match(&ttl_mt_reg);
Harald Welte703828f2000-10-04 15:27:07 +0000135}