blob: 4ef976433b1d7adec20e6494c4f122b9ca1a2938 [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 Welteb77f1da2002-03-14 11:35:58 +00004 * $Id: libipt_ttl.c,v 1.4 2002/02/25 11:25:41 laforge Exp $
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
17static void help(void)
18{
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"
24, NETFILTER_VERSION);
Harald Welte703828f2000-10-04 15:27:07 +000025}
26
27static void init(struct ipt_entry_match *m, unsigned int *nfcache)
28{
29 /* caching not yet implemented */
Harald Welte1441c422000-11-13 12:32:50 +000030 *nfcache |= NFC_UNKNOWN;
Harald Welte703828f2000-10-04 15:27:07 +000031}
32
33static int parse(int c, char **argv, int invert, unsigned int *flags,
34 const struct ipt_entry *entry, unsigned int *nfcache,
35 struct ipt_entry_match **match)
36{
37 struct ipt_ttl_info *info = (struct ipt_ttl_info *) (*match)->data;
38 u_int8_t value;
39
Harald Welteb77f1da2002-03-14 11:35:58 +000040 check_inverse(optarg, &invert, &optind, 0);
Harald Welte1441c422000-11-13 12:32:50 +000041 value = atoi(argv[optind-1]);
42
43 if (*flags)
44 exit_error(PARAMETER_PROBLEM,
45 "Can't specify TTL option twice");
46
47 if (!optarg)
48 exit_error(PARAMETER_PROBLEM,
49 "ttl: You must specify a value");
Harald Welte703828f2000-10-04 15:27:07 +000050 switch (c) {
Harald Welte1441c422000-11-13 12:32:50 +000051 case '2':
52 if (invert)
53 info->mode = IPT_TTL_NE;
54 else
55 info->mode = IPT_TTL_EQ;
Harald Welte703828f2000-10-04 15:27:07 +000056
57 /* is 0 allowed? */
58 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000059 *flags = 1;
60
61 break;
62 case '3':
63 if (invert)
64 exit_error(PARAMETER_PROBLEM,
65 "ttl: unexpected `!'");
66
67 info->mode = IPT_TTL_LT;
68 info->ttl = value;
69 *flags = 1;
70
71 break;
72 case '4':
Harald Welte703828f2000-10-04 15:27:07 +000073 if (invert)
Harald Welte1441c422000-11-13 12:32:50 +000074 exit_error(PARAMETER_PROBLEM,
75 "ttl: unexpected `!'");
76
77 info->mode = IPT_TTL_GT;
78 info->ttl = value;
79 *flags = 1;
80
Harald Welte703828f2000-10-04 15:27:07 +000081 break;
82 default:
83 return 0;
84
85 }
86
87 return 1;
88}
89
90static void final_check(unsigned int flags)
91{
92 if (!flags)
93 exit_error(PARAMETER_PROBLEM,
Harald Welte1441c422000-11-13 12:32:50 +000094 "TTL match: You must specify one of "
95 "`--ttl-eq', `--ttl-lt', `--ttl-gt");
Harald Welte703828f2000-10-04 15:27:07 +000096}
97
98static void print(const struct ipt_ip *ip,
99 const struct ipt_entry_match *match,
100 int numeric)
101{
102 const struct ipt_ttl_info *info =
103 (struct ipt_ttl_info *) match->data;
104
105 printf("TTL match ");
Harald Welte1441c422000-11-13 12:32:50 +0000106 switch (info->mode) {
107 case IPT_TTL_EQ:
108 printf("TTL == ");
109 break;
110 case IPT_TTL_NE:
111 printf("TTL != ");
112 break;
113 case IPT_TTL_LT:
114 printf("TTL < ");
115 break;
116 case IPT_TTL_GT:
117 printf("TTL > ");
118 break;
119 }
Harald Welte703828f2000-10-04 15:27:07 +0000120 printf("%u ", info->ttl);
121}
122
123static void save(const struct ipt_ip *ip,
124 const struct ipt_entry_match *match)
125{
126 const struct ipt_ttl_info *info =
127 (struct ipt_ttl_info *) match->data;
128
Harald Welte1441c422000-11-13 12:32:50 +0000129 switch (info->mode) {
130 case IPT_TTL_EQ:
131 printf("--ttl-eq ");
132 break;
133 case IPT_TTL_NE:
134 printf("! --ttl-eq ");
135 break;
136 case IPT_TTL_LT:
137 printf("--ttl-lt ");
138 break;
139 case IPT_TTL_GT:
140 printf("--ttl-gt ");
141 break;
142 default:
143 /* error */
144 break;
145 }
Harald Welte703828f2000-10-04 15:27:07 +0000146 printf("%u ", info->ttl);
147}
148
149static struct option opts[] = {
Harald Welte1441c422000-11-13 12:32:50 +0000150 { "ttl", 1, 0, '2' },
151 { "ttl-eq", 1, 0, '2'},
152 { "ttl-lt", 1, 0, '3'},
153 { "ttl-gt", 1, 0, '4'},
Harald Welte703828f2000-10-04 15:27:07 +0000154 { 0 }
155};
156
Harald Welte3efb6ea2001-08-06 18:50:21 +0000157static
Harald Welte703828f2000-10-04 15:27:07 +0000158struct iptables_match ttl = {
159 NULL,
160 "ttl",
161 NETFILTER_VERSION,
162 IPT_ALIGN(sizeof(struct ipt_ttl_info)),
163 IPT_ALIGN(sizeof(struct ipt_ttl_info)),
164 &help,
165 &init,
166 &parse,
167 &final_check,
168 &print,
169 &save,
170 opts
171};
172
173
174void _init(void)
175{
176 register_match(&ttl);
177}