libipt_ttl.c revision 5d9678ad3eabc34ac40dfe055d7f6a8e44445a5a
1/* Shared library add-on to iptables to add TTL matching support
2 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3 *
4 * $Id$
5 *
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 <xtables.h>
13
14#include <linux/netfilter_ipv4/ip_tables.h>
15#include <linux/netfilter_ipv4/ipt_ttl.h>
16
17static void ttl_help(void)
18{
19	printf(
20"ttl match options:\n"
21"  --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");
24}
25
26static int ttl_parse(int c, char **argv, int invert, unsigned int *flags,
27                     const void *entry, struct xt_entry_match **match)
28{
29	struct ipt_ttl_info *info = (struct ipt_ttl_info *) (*match)->data;
30	unsigned int value;
31
32	check_inverse(optarg, &invert, &optind, 0);
33
34	switch (c) {
35		case '2':
36			if (string_to_number(optarg, 0, 255, &value) == -1)
37				exit_error(PARAMETER_PROBLEM,
38				           "ttl: Expected value between 0 and 255");
39
40			if (invert)
41				info->mode = IPT_TTL_NE;
42			else
43				info->mode = IPT_TTL_EQ;
44
45			/* is 0 allowed? */
46			info->ttl = value;
47			break;
48		case '3':
49			if (string_to_number(optarg, 0, 255, &value) == -1)
50				exit_error(PARAMETER_PROBLEM,
51				           "ttl: Expected value between 0 and 255");
52
53			if (invert)
54				exit_error(PARAMETER_PROBLEM,
55						"ttl: unexpected `!'");
56
57			info->mode = IPT_TTL_LT;
58			info->ttl = value;
59			break;
60		case '4':
61			if (string_to_number(optarg, 0, 255, &value) == -1)
62				exit_error(PARAMETER_PROBLEM,
63				           "ttl: Expected value between 0 and 255");
64
65			if (invert)
66				exit_error(PARAMETER_PROBLEM,
67						"ttl: unexpected `!'");
68
69			info->mode = IPT_TTL_GT;
70			info->ttl = value;
71			break;
72		default:
73			return 0;
74
75	}
76
77	if (*flags)
78		exit_error(PARAMETER_PROBLEM,
79				"Can't specify TTL option twice");
80	*flags = 1;
81
82	return 1;
83}
84
85static void ttl_check(unsigned int flags)
86{
87	if (!flags)
88		exit_error(PARAMETER_PROBLEM,
89			"TTL match: You must specify one of "
90			"`--ttl-eq', `--ttl-lt', `--ttl-gt");
91}
92
93static void ttl_print(const void *ip, const struct xt_entry_match *match,
94                      int numeric)
95{
96	const struct ipt_ttl_info *info =
97		(struct ipt_ttl_info *) match->data;
98
99	printf("TTL match ");
100	switch (info->mode) {
101		case IPT_TTL_EQ:
102			printf("TTL == ");
103			break;
104		case IPT_TTL_NE:
105			printf("TTL != ");
106			break;
107		case IPT_TTL_LT:
108			printf("TTL < ");
109			break;
110		case IPT_TTL_GT:
111			printf("TTL > ");
112			break;
113	}
114	printf("%u ", info->ttl);
115}
116
117static void ttl_save(const void *ip, const struct xt_entry_match *match)
118{
119	const struct ipt_ttl_info *info =
120		(struct ipt_ttl_info *) match->data;
121
122	switch (info->mode) {
123		case IPT_TTL_EQ:
124			printf("--ttl-eq ");
125			break;
126		case IPT_TTL_NE:
127			printf("! --ttl-eq ");
128			break;
129		case IPT_TTL_LT:
130			printf("--ttl-lt ");
131			break;
132		case IPT_TTL_GT:
133			printf("--ttl-gt ");
134			break;
135		default:
136			/* error */
137			break;
138	}
139	printf("%u ", info->ttl);
140}
141
142static const struct option ttl_opts[] = {
143	{ "ttl", 1, NULL, '2' },
144	{ "ttl-eq", 1, NULL, '2'},
145	{ "ttl-lt", 1, NULL, '3'},
146	{ "ttl-gt", 1, NULL, '4'},
147	{ .name = NULL }
148};
149
150static struct xtables_match ttl_mt_reg = {
151	.name		= "ttl",
152	.version	= XTABLES_VERSION,
153	.family		= NFPROTO_IPV4,
154	.size		= XT_ALIGN(sizeof(struct ipt_ttl_info)),
155	.userspacesize	= XT_ALIGN(sizeof(struct ipt_ttl_info)),
156	.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,
162};
163
164
165void _init(void)
166{
167	xtables_register_match(&ttl_mt_reg);
168}
169