blob: 99c89ea778369a4147f46c2bdbf458255804d988 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add TOS matching support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7
8#include <iptables.h>
9#include <linux/netfilter_ipv4/ipt_tos.h>
10
11/* TOS names and values. */
12struct TOS_value
13{
14 unsigned char TOS;
15 const char *name;
16} TOS_values[] = {
17 { IPTOS_LOWDELAY, "Minimize-Delay" },
18 { IPTOS_THROUGHPUT, "Maximize-Throughput" },
19 { IPTOS_RELIABILITY, "Maximize-Reliability" },
20 { IPTOS_MINCOST, "Minimize-Cost" },
21 { IPTOS_NORMALSVC, "Normal-Service" },
22};
23
24/* Function which prints out usage message. */
25static void
26help(void)
27{
28 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +000029
Marc Bouchere6869a82000-03-20 06:03:29 +000030 printf(
31"TOS match v%s options:\n"
32"[!] --tos value Match Type of Service field from one of the\n"
33" following numeric or descriptive values:\n",
34NETFILTER_VERSION);
35
36 for (i = 0; i < sizeof(TOS_values)/sizeof(struct TOS_value);i++)
37 printf(" %s %u (0x%02x)\n",
38 TOS_values[i].name,
39 TOS_values[i].TOS,
40 TOS_values[i].TOS);
41 fputc('\n', stdout);
42}
43
44static struct option opts[] = {
45 { "tos", 1, 0, '1' },
46 {0}
47};
48
49/* Initialize the match. */
50static void
51init(struct ipt_entry_match *m, unsigned int *nfcache)
52{
53 *nfcache |= NFC_IP_TOS;
54}
55
56static void
57parse_tos(const unsigned char *s, struct ipt_tos_info *info)
58{
59 unsigned int i;
60 int tos = string_to_number(s, 0, 255);
61
62 if (tos != -1) {
63 if (tos == IPTOS_LOWDELAY
64 || tos == IPTOS_THROUGHPUT
65 || tos == IPTOS_RELIABILITY
66 || tos == IPTOS_MINCOST
67 || tos == IPTOS_NORMALSVC) {
68 info->tos = (u_int8_t )tos;
69 return;
70 }
71 } else {
72 for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++)
73 if (strcasecmp(s,TOS_values[i].name) == 0) {
74 info->tos = TOS_values[i].TOS;
75 return;
76 }
77 }
78 exit_error(PARAMETER_PROBLEM, "Bad TOS value `%s'", s);
79}
80
81/* Function which parses command options; returns true if it
82 ate an option */
83static int
84parse(int c, char **argv, int invert, unsigned int *flags,
85 const struct ipt_entry *entry,
86 unsigned int *nfcache,
87 struct ipt_entry_match **match)
88{
89 struct ipt_tos_info *tosinfo = (struct ipt_tos_info *)(*match)->data;
90
91 switch (c) {
92 case '1':
93 if (check_inverse(optarg, &invert))
94 optind++;
95 parse_tos(argv[optind-1], tosinfo);
96 if (invert)
97 tosinfo->invert = 1;
98 *flags = 1;
99 break;
100
101 default:
102 return 0;
103 }
104 return 1;
105}
106
107static void
108print_tos(u_int8_t tos, int invert, int numeric)
109{
110 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000111
Marc Bouchere6869a82000-03-20 06:03:29 +0000112 if (invert)
113 fputc('!', stdout);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000114
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 if (!numeric) {
116 for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++)
117 if (TOS_values[i].TOS == tos) {
118 printf("%s ", TOS_values[i].name);
119 return;
120 }
121 }
122 printf("0x%02x ", tos);
123}
124
125/* Final check; must have specified --tos. */
126static void
127final_check(unsigned int flags)
128{
129 if (!flags)
130 exit_error(PARAMETER_PROBLEM,
131 "TOS match: You must specify `--tos'");
132}
133
134/* Prints out the matchinfo. */
135static void
136print(const struct ipt_ip *ip,
137 const struct ipt_entry_match *match,
138 int numeric)
139{
140 printf("TOS match ");
141 print_tos(((struct ipt_tos_info *)match->data)->tos,
142 ((struct ipt_tos_info *)match->data)->invert, numeric);
143}
144
145/* Saves the union ipt_matchinfo in parsable form to stdout. */
146static void
147save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
148{
149 printf("--tos ");
150 print_tos(((struct ipt_tos_info *)match->data)->tos,
151 ((struct ipt_tos_info *)match->data)->invert, 0);
152}
153
154struct iptables_match tos
155= { NULL,
156 "tos",
157 NETFILTER_VERSION,
158 sizeof(struct ipt_tos_info),
Rusty Russelledf14cf2000-04-19 11:26:44 +0000159 sizeof(struct ipt_tos_info),
Marc Bouchere6869a82000-03-20 06:03:29 +0000160 &help,
161 &init,
162 &parse,
163 &final_check,
164 &print,
165 &save,
166 opts
167};
168
169void _init(void)
170{
171 register_match(&tos);
172}