blob: b6edbc1a6977d63661d792c9b6db0c730aad7308 [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
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"
Harald Welte80fe35d2002-05-29 13:08:15 +000024, IPTABLES_VERSION);
Harald Welte703828f2000-10-04 15:27:07 +000025}
26
Harald Welte703828f2000-10-04 15:27:07 +000027static int parse(int c, char **argv, int invert, unsigned int *flags,
Peter Rileyea146a92007-09-02 13:09:07 +000028 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000029 struct xt_entry_match **match)
Harald Welte703828f2000-10-04 15:27:07 +000030{
31 struct ipt_ttl_info *info = (struct ipt_ttl_info *) (*match)->data;
Harald Welteefa8fc22005-07-19 22:03:49 +000032 unsigned int value;
Harald Welte703828f2000-10-04 15:27:07 +000033
Harald Welteb77f1da2002-03-14 11:35:58 +000034 check_inverse(optarg, &invert, &optind, 0);
Harald Welte1441c422000-11-13 12:32:50 +000035
Harald Welte703828f2000-10-04 15:27:07 +000036 switch (c) {
Harald Welte1441c422000-11-13 12:32:50 +000037 case '2':
Patrick McHardy6656e132005-11-18 17:59:56 +000038 if (string_to_number(optarg, 0, 255, &value) == -1)
39 exit_error(PARAMETER_PROBLEM,
40 "ttl: Expected value between 0 and 255");
41
Harald Welte1441c422000-11-13 12:32:50 +000042 if (invert)
43 info->mode = IPT_TTL_NE;
44 else
45 info->mode = IPT_TTL_EQ;
Harald Welte703828f2000-10-04 15:27:07 +000046
47 /* is 0 allowed? */
48 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000049 break;
50 case '3':
Patrick McHardy6656e132005-11-18 17:59:56 +000051 if (string_to_number(optarg, 0, 255, &value) == -1)
52 exit_error(PARAMETER_PROBLEM,
53 "ttl: Expected value between 0 and 255");
54
Harald Welte1441c422000-11-13 12:32:50 +000055 if (invert)
56 exit_error(PARAMETER_PROBLEM,
57 "ttl: unexpected `!'");
58
59 info->mode = IPT_TTL_LT;
60 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000061 break;
62 case '4':
Patrick McHardy6656e132005-11-18 17:59:56 +000063 if (string_to_number(optarg, 0, 255, &value) == -1)
64 exit_error(PARAMETER_PROBLEM,
65 "ttl: Expected value between 0 and 255");
66
Harald Welte703828f2000-10-04 15:27:07 +000067 if (invert)
Harald Welte1441c422000-11-13 12:32:50 +000068 exit_error(PARAMETER_PROBLEM,
69 "ttl: unexpected `!'");
70
71 info->mode = IPT_TTL_GT;
72 info->ttl = value;
Harald Welte703828f2000-10-04 15:27:07 +000073 break;
74 default:
75 return 0;
76
77 }
78
Phil Oester7defa342004-08-23 20:45:17 +000079 if (*flags)
80 exit_error(PARAMETER_PROBLEM,
81 "Can't specify TTL option twice");
82 *flags = 1;
83
Harald Welte703828f2000-10-04 15:27:07 +000084 return 1;
85}
86
87static void final_check(unsigned int flags)
88{
89 if (!flags)
90 exit_error(PARAMETER_PROBLEM,
Harald Welte1441c422000-11-13 12:32:50 +000091 "TTL match: You must specify one of "
92 "`--ttl-eq', `--ttl-lt', `--ttl-gt");
Harald Welte703828f2000-10-04 15:27:07 +000093}
94
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000095static void print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000096 const struct xt_entry_match *match,
Harald Welte703828f2000-10-04 15:27:07 +000097 int numeric)
98{
99 const struct ipt_ttl_info *info =
100 (struct ipt_ttl_info *) match->data;
101
102 printf("TTL match ");
Harald Welte1441c422000-11-13 12:32:50 +0000103 switch (info->mode) {
104 case IPT_TTL_EQ:
105 printf("TTL == ");
106 break;
107 case IPT_TTL_NE:
108 printf("TTL != ");
109 break;
110 case IPT_TTL_LT:
111 printf("TTL < ");
112 break;
113 case IPT_TTL_GT:
114 printf("TTL > ");
115 break;
116 }
Harald Welte703828f2000-10-04 15:27:07 +0000117 printf("%u ", info->ttl);
118}
119
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000120static void save(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000121 const struct xt_entry_match *match)
Harald Welte703828f2000-10-04 15:27:07 +0000122{
123 const struct ipt_ttl_info *info =
124 (struct ipt_ttl_info *) match->data;
125
Harald Welte1441c422000-11-13 12:32:50 +0000126 switch (info->mode) {
127 case IPT_TTL_EQ:
128 printf("--ttl-eq ");
129 break;
130 case IPT_TTL_NE:
131 printf("! --ttl-eq ");
132 break;
133 case IPT_TTL_LT:
134 printf("--ttl-lt ");
135 break;
136 case IPT_TTL_GT:
137 printf("--ttl-gt ");
138 break;
139 default:
140 /* error */
141 break;
142 }
Harald Welte703828f2000-10-04 15:27:07 +0000143 printf("%u ", info->ttl);
144}
145
Jan Engelhardt661f1122007-07-30 14:46:51 +0000146static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000147 { "ttl", 1, NULL, '2' },
148 { "ttl-eq", 1, NULL, '2'},
149 { "ttl-lt", 1, NULL, '3'},
150 { "ttl-gt", 1, NULL, '4'},
151 { }
Harald Welte703828f2000-10-04 15:27:07 +0000152};
153
Pablo Neira8caee8b2004-12-28 13:11:59 +0000154static struct iptables_match ttl = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000155 .name = "ttl",
156 .version = IPTABLES_VERSION,
157 .size = IPT_ALIGN(sizeof(struct ipt_ttl_info)),
158 .userspacesize = IPT_ALIGN(sizeof(struct ipt_ttl_info)),
159 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000160 .parse = &parse,
161 .final_check = &final_check,
162 .print = &print,
163 .save = &save,
164 .extra_opts = opts
Harald Welte703828f2000-10-04 15:27:07 +0000165};
166
167
168void _init(void)
169{
170 register_match(&ttl);
171}