blob: 1fa7bd31fbb704ce0acb63b14b5da26b4c9bd760 [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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010012#include <xtables.h>
Harald Welte703828f2000-10-04 15:27:07 +000013
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(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"ttl match 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"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023" --ttl-gt value Match TTL > value\n");
Harald Welte703828f2000-10-04 15:27:07 +000024}
25
Jan Engelhardt59d16402007-10-04 16:28:39 +000026static int ttl_parse(int c, char **argv, int invert, unsigned int *flags,
27 const void *entry, struct xt_entry_match **match)
Harald Welte703828f2000-10-04 15:27:07 +000028{
29 struct ipt_ttl_info *info = (struct ipt_ttl_info *) (*match)->data;
Harald Welteefa8fc22005-07-19 22:03:49 +000030 unsigned int value;
Harald Welte703828f2000-10-04 15:27:07 +000031
Harald Welteb77f1da2002-03-14 11:35:58 +000032 check_inverse(optarg, &invert, &optind, 0);
Harald Welte1441c422000-11-13 12:32:50 +000033
Harald Welte703828f2000-10-04 15:27:07 +000034 switch (c) {
Harald Welte1441c422000-11-13 12:32:50 +000035 case '2':
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010036 if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX))
Patrick McHardy6656e132005-11-18 17:59:56 +000037 exit_error(PARAMETER_PROBLEM,
38 "ttl: Expected value between 0 and 255");
39
Harald Welte1441c422000-11-13 12:32:50 +000040 if (invert)
41 info->mode = IPT_TTL_NE;
42 else
43 info->mode = IPT_TTL_EQ;
Harald Welte703828f2000-10-04 15:27:07 +000044
45 /* is 0 allowed? */
46 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000047 break;
48 case '3':
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010049 if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX))
Patrick McHardy6656e132005-11-18 17:59:56 +000050 exit_error(PARAMETER_PROBLEM,
51 "ttl: Expected value between 0 and 255");
52
Harald Welte1441c422000-11-13 12:32:50 +000053 if (invert)
54 exit_error(PARAMETER_PROBLEM,
55 "ttl: unexpected `!'");
56
57 info->mode = IPT_TTL_LT;
58 info->ttl = value;
Harald Welte1441c422000-11-13 12:32:50 +000059 break;
60 case '4':
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010061 if (!xtables_strtoui(optarg, NULL, &value, 0, UINT8_MAX))
Patrick McHardy6656e132005-11-18 17:59:56 +000062 exit_error(PARAMETER_PROBLEM,
63 "ttl: Expected value between 0 and 255");
64
Harald Welte703828f2000-10-04 15:27:07 +000065 if (invert)
Harald Welte1441c422000-11-13 12:32:50 +000066 exit_error(PARAMETER_PROBLEM,
67 "ttl: unexpected `!'");
68
69 info->mode = IPT_TTL_GT;
70 info->ttl = value;
Harald Welte703828f2000-10-04 15:27:07 +000071 break;
72 default:
73 return 0;
74
75 }
76
Phil Oester7defa342004-08-23 20:45:17 +000077 if (*flags)
78 exit_error(PARAMETER_PROBLEM,
79 "Can't specify TTL option twice");
80 *flags = 1;
81
Harald Welte703828f2000-10-04 15:27:07 +000082 return 1;
83}
84
Jan Engelhardt59d16402007-10-04 16:28:39 +000085static void ttl_check(unsigned int flags)
Harald Welte703828f2000-10-04 15:27:07 +000086{
87 if (!flags)
88 exit_error(PARAMETER_PROBLEM,
Harald Welte1441c422000-11-13 12:32:50 +000089 "TTL match: You must specify one of "
90 "`--ttl-eq', `--ttl-lt', `--ttl-gt");
Harald Welte703828f2000-10-04 15:27:07 +000091}
92
Jan Engelhardt59d16402007-10-04 16:28:39 +000093static void ttl_print(const void *ip, const struct xt_entry_match *match,
94 int numeric)
Harald Welte703828f2000-10-04 15:27:07 +000095{
96 const struct ipt_ttl_info *info =
97 (struct ipt_ttl_info *) match->data;
98
99 printf("TTL match ");
Harald Welte1441c422000-11-13 12:32:50 +0000100 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 }
Harald Welte703828f2000-10-04 15:27:07 +0000114 printf("%u ", info->ttl);
115}
116
Jan Engelhardt59d16402007-10-04 16:28:39 +0000117static void ttl_save(const void *ip, const struct xt_entry_match *match)
Harald Welte703828f2000-10-04 15:27:07 +0000118{
119 const struct ipt_ttl_info *info =
120 (struct ipt_ttl_info *) match->data;
121
Harald Welte1441c422000-11-13 12:32:50 +0000122 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 }
Harald Welte703828f2000-10-04 15:27:07 +0000139 printf("%u ", info->ttl);
140}
141
Jan Engelhardt59d16402007-10-04 16:28:39 +0000142static const struct option ttl_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000143 { "ttl", 1, NULL, '2' },
144 { "ttl-eq", 1, NULL, '2'},
145 { "ttl-lt", 1, NULL, '3'},
146 { "ttl-gt", 1, NULL, '4'},
Max Kellermann9ee386a2008-01-29 13:48:05 +0000147 { .name = NULL }
Harald Welte703828f2000-10-04 15:27:07 +0000148};
149
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200150static struct xtables_match ttl_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000151 .name = "ttl",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200152 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100153 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200154 .size = XT_ALIGN(sizeof(struct ipt_ttl_info)),
155 .userspacesize = XT_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 Engelhardt8b7c64d2008-04-15 11:48:25 +0200167 xtables_register_match(&ttl_mt_reg);
Harald Welte703828f2000-10-04 15:27:07 +0000168}