blob: d7820f1f4ef979527a75076fc377dc63f1b3672f [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 *
Rusty Russell70f758d2004-12-22 04:26:53 +00004 * $Id$
Harald Welte703828f2000-10-04 15:27:07 +00005 *
6 * This program is released under the terms of GNU GPL */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.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
Jan Engelhardt59d16402007-10-04 16:28:39 +000017static void ttl_help(void)
Harald Welte703828f2000-10-04 15:27:07 +000018{
19 printf(
20"TTL match v%s options:\n"
Harald Welte1441c422000-11-13 12:32:50 +000021" --ttl-eq value Match time to live value\n"
22" --ttl-lt value Match TTL < value\n"
23" --ttl-gt value Match TTL > value\n"
Harald Welte80fe35d2002-05-29 13:08:15 +000024, IPTABLES_VERSION);
Harald Welte703828f2000-10-04 15:27:07 +000025}
26
Jan Engelhardt59d16402007-10-04 16:28:39 +000027static int ttl_parse(int c, char **argv, int invert, unsigned int *flags,
28 const void *entry, struct xt_entry_match **match)
Harald Welte703828f2000-10-04 15:27:07 +000029{
30 struct ipt_ttl_info *info = (struct ipt_ttl_info *) (*match)->data;
Harald Welteefa8fc22005-07-19 22:03:49 +000031 unsigned int value;
Harald Welte703828f2000-10-04 15:27:07 +000032
Harald Welteb77f1da2002-03-14 11:35:58 +000033 check_inverse(optarg, &invert, &optind, 0);
Harald Welte1441c422000-11-13 12:32:50 +000034
Harald Welte703828f2000-10-04 15:27:07 +000035 switch (c) {
Harald Welte1441c422000-11-13 12:32:50 +000036 case '2':
Patrick McHardy6656e132005-11-18 17:59:56 +000037 if (string_to_number(optarg, 0, 255, &value) == -1)
38 exit_error(PARAMETER_PROBLEM,
39 "ttl: Expected value between 0 and 255");
40
Harald Welte1441c422000-11-13 12:32:50 +000041 if (invert)
42 info->mode = IPT_TTL_NE;
43 else
44 info->mode = IPT_TTL_EQ;
Harald Welte703828f2000-10-04 15:27:07 +000045
46 /* is 0 allowed? */
47 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000048 break;
49 case '3':
Patrick McHardy6656e132005-11-18 17:59:56 +000050 if (string_to_number(optarg, 0, 255, &value) == -1)
51 exit_error(PARAMETER_PROBLEM,
52 "ttl: Expected value between 0 and 255");
53
Harald Welte1441c422000-11-13 12:32:50 +000054 if (invert)
55 exit_error(PARAMETER_PROBLEM,
56 "ttl: unexpected `!'");
57
58 info->mode = IPT_TTL_LT;
59 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000060 break;
61 case '4':
Patrick McHardy6656e132005-11-18 17:59:56 +000062 if (string_to_number(optarg, 0, 255, &value) == -1)
63 exit_error(PARAMETER_PROBLEM,
64 "ttl: Expected value between 0 and 255");
65
Harald Welte703828f2000-10-04 15:27:07 +000066 if (invert)
Harald Welte1441c422000-11-13 12:32:50 +000067 exit_error(PARAMETER_PROBLEM,
68 "ttl: unexpected `!'");
69
70 info->mode = IPT_TTL_GT;
71 info->ttl = value;
Harald Welte703828f2000-10-04 15:27:07 +000072 break;
73 default:
74 return 0;
75
76 }
77
Phil Oester7defa342004-08-23 20:45:17 +000078 if (*flags)
79 exit_error(PARAMETER_PROBLEM,
80 "Can't specify TTL option twice");
81 *flags = 1;
82
Harald Welte703828f2000-10-04 15:27:07 +000083 return 1;
84}
85
Jan Engelhardt59d16402007-10-04 16:28:39 +000086static void ttl_check(unsigned int flags)
Harald Welte703828f2000-10-04 15:27:07 +000087{
88 if (!flags)
89 exit_error(PARAMETER_PROBLEM,
Harald Welte1441c422000-11-13 12:32:50 +000090 "TTL match: You must specify one of "
91 "`--ttl-eq', `--ttl-lt', `--ttl-gt");
Harald Welte703828f2000-10-04 15:27:07 +000092}
93
Jan Engelhardt59d16402007-10-04 16:28:39 +000094static void ttl_print(const void *ip, const struct xt_entry_match *match,
95 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +000096{
97 const struct ipt_ttl_info *info =
98 (struct ipt_ttl_info *) match->data;
99
100 printf("TTL match ");
Harald Welte1441c422000-11-13 12:32:50 +0000101 switch (info->mode) {
102 case IPT_TTL_EQ:
103 printf("TTL == ");
104 break;
105 case IPT_TTL_NE:
106 printf("TTL != ");
107 break;
108 case IPT_TTL_LT:
109 printf("TTL < ");
110 break;
111 case IPT_TTL_GT:
112 printf("TTL > ");
113 break;
114 }
Harald Welte703828f2000-10-04 15:27:07 +0000115 printf("%u ", info->ttl);
116}
117
Jan Engelhardt59d16402007-10-04 16:28:39 +0000118static void ttl_save(const void *ip, const struct xt_entry_match *match)
Harald Welte703828f2000-10-04 15:27:07 +0000119{
120 const struct ipt_ttl_info *info =
121 (struct ipt_ttl_info *) match->data;
122
Harald Welte1441c422000-11-13 12:32:50 +0000123 switch (info->mode) {
124 case IPT_TTL_EQ:
125 printf("--ttl-eq ");
126 break;
127 case IPT_TTL_NE:
128 printf("! --ttl-eq ");
129 break;
130 case IPT_TTL_LT:
131 printf("--ttl-lt ");
132 break;
133 case IPT_TTL_GT:
134 printf("--ttl-gt ");
135 break;
136 default:
137 /* error */
138 break;
139 }
Harald Welte703828f2000-10-04 15:27:07 +0000140 printf("%u ", info->ttl);
141}
142
Jan Engelhardt59d16402007-10-04 16:28:39 +0000143static const struct option ttl_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000144 { "ttl", 1, NULL, '2' },
145 { "ttl-eq", 1, NULL, '2'},
146 { "ttl-lt", 1, NULL, '3'},
147 { "ttl-gt", 1, NULL, '4'},
Max Kellermann9ee386a2008-01-29 13:48:05 +0000148 { .name = NULL }
Harald Welte703828f2000-10-04 15:27:07 +0000149};
150
Jan Engelhardt59d16402007-10-04 16:28:39 +0000151static struct iptables_match ttl_match = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000152 .name = "ttl",
153 .version = IPTABLES_VERSION,
154 .size = IPT_ALIGN(sizeof(struct ipt_ttl_info)),
155 .userspacesize = IPT_ALIGN(sizeof(struct ipt_ttl_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000156 .help = ttl_help,
157 .parse = ttl_parse,
158 .final_check = ttl_check,
159 .print = ttl_print,
160 .save = ttl_save,
161 .extra_opts = ttl_opts,
Harald Welte703828f2000-10-04 15:27:07 +0000162};
163
164
165void _init(void)
166{
Jan Engelhardt59d16402007-10-04 16:28:39 +0000167 register_match(&ttl_match);
Harald Welte703828f2000-10-04 15:27:07 +0000168}