blob: 0c91cb5450f472a67c05c9ccba5244335928f54a [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add TOS target support. */
2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include <getopt.h>
6
7#include <iptables.h>
8#include <linux/netfilter_ipv4/ip_tables.h>
9#include <linux/netfilter_ipv4/ipt_TOS.h>
10
11struct tosinfo {
12 struct ipt_entry_target t;
13 struct ipt_tos_target_info tos;
14};
15
16/* TOS names and values. */
17struct TOS_value
18{
19 unsigned char TOS;
20 const char *name;
21} TOS_values[] = {
22 { IPTOS_LOWDELAY, "Minimize-Delay" },
23 { IPTOS_THROUGHPUT, "Maximize-Throughput" },
24 { IPTOS_RELIABILITY, "Maximize-Reliability" },
25 { IPTOS_MINCOST, "Minimize-Cost" },
26 { IPTOS_NORMALSVC, "Normal-Service" },
27};
28
29/* Function which prints out usage message. */
30static void
31help(void)
32{
33 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +000034
Marc Bouchere6869a82000-03-20 06:03:29 +000035 printf(
36"TOS target v%s options:\n"
37" --set-tos value Set Type of Service field to one of the\n"
38" following numeric or descriptive values:\n",
39NETFILTER_VERSION);
40
41 for (i = 0; i < sizeof(TOS_values)/sizeof(struct TOS_value);i++)
42 printf(" %s %u (0x%02x)\n",
43 TOS_values[i].name,
44 TOS_values[i].TOS,
45 TOS_values[i].TOS);
46 fputc('\n', stdout);
47}
48
49static struct option opts[] = {
50 { "set-tos", 1, 0, '1' },
51 { 0 }
52};
53
54/* Initialize the target. */
55static void
56init(struct ipt_entry_target *t, unsigned int *nfcache)
57{
58}
59
60static void
61parse_tos(const unsigned char *s, struct ipt_tos_target_info *info)
62{
63 unsigned int i;
64 int tos = string_to_number(s, 0, 255);
65
66 if (tos != -1) {
67 if (tos == IPTOS_LOWDELAY
68 || tos == IPTOS_THROUGHPUT
69 || tos == IPTOS_RELIABILITY
70 || tos == IPTOS_MINCOST
71 || tos == IPTOS_NORMALSVC) {
72 info->tos = (u_int8_t )tos;
73 return;
74 }
75 } else {
76 for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++)
77 if (strcasecmp(s,TOS_values[i].name) == 0) {
78 info->tos = TOS_values[i].TOS;
79 return;
80 }
81 }
82 exit_error(PARAMETER_PROBLEM, "Bad TOS value `%s'", s);
83}
84
85/* Function which parses command options; returns true if it
86 ate an option */
87static int
88parse(int c, char **argv, int invert, unsigned int *flags,
89 const struct ipt_entry *entry,
90 struct ipt_entry_target **target)
91{
92 struct ipt_tos_target_info *tosinfo
93 = (struct ipt_tos_target_info *)(*target)->data;
94
95 switch (c) {
96 case '1':
97 if (*flags)
98 exit_error(PARAMETER_PROBLEM,
99 "TOS target: Cant specify --set-tos twice");
100 parse_tos(optarg, tosinfo);
101 *flags = 1;
102 break;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000103
Marc Bouchere6869a82000-03-20 06:03:29 +0000104 default:
105 return 0;
106 }
107
108 return 1;
109}
110
111static void
112final_check(unsigned int flags)
113{
114 if (!flags)
115 exit_error(PARAMETER_PROBLEM,
116 "TOS target: Parameter --set-tos is required");
117}
118
119static void
120print_tos(u_int8_t tos, int numeric)
121{
122 unsigned int i;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000123
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 if (!numeric) {
125 for (i = 0; i<sizeof(TOS_values)/sizeof(struct TOS_value); i++)
126 if (TOS_values[i].TOS == tos) {
127 printf("%s ", TOS_values[i].name);
128 return;
129 }
130 }
131 printf("0x%02x ", tos);
132}
133
134/* Prints out the targinfo. */
135static void
136print(const struct ipt_ip *ip,
137 const struct ipt_entry_target *target,
138 int numeric)
139{
140 const struct ipt_tos_target_info *tosinfo =
141 (const struct ipt_tos_target_info *)target->data;
142 printf("TOS set ");
Rusty Russell7e53bf92000-03-20 07:03:28 +0000143 print_tos(tosinfo->tos, numeric);
Marc Bouchere6869a82000-03-20 06:03:29 +0000144}
145
146/* Saves the union ipt_targinfo in parsable form to stdout. */
147static void
148save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
149{
150 const struct ipt_tos_target_info *tosinfo =
151 (const struct ipt_tos_target_info *)target->data;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000152
153 printf("--set-tos 0x%02x ", tosinfo->tos);
Marc Bouchere6869a82000-03-20 06:03:29 +0000154}
155
156struct iptables_target tos
157= { NULL,
158 "TOS",
159 NETFILTER_VERSION,
Rusty Russell73f72f52000-07-03 10:17:57 +0000160 IPT_ALIGN(sizeof(struct ipt_tos_target_info)),
161 IPT_ALIGN(sizeof(struct ipt_tos_target_info)),
Marc Bouchere6869a82000-03-20 06:03:29 +0000162 &help,
163 &init,
164 &parse,
165 &final_check,
166 &print,
167 &save,
168 opts
169};
170
171void _init(void)
172{
173 register_target(&tos);
174}