1 /* Shared library add-on to iptables to add TOS target support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ipt_TOS.h>
12 struct ipt_entry_target t;
13 struct ipt_tos_target_info tos;
16 /* TOS names and values. */
23 { IPTOS_LOWDELAY, "Minimize-Delay" },
24 { IPTOS_THROUGHPUT, "Maximize-Throughput" },
25 { IPTOS_RELIABILITY, "Maximize-Reliability" },
26 { IPTOS_MINCOST, "Minimize-Cost" },
27 { IPTOS_NORMALSVC, "Normal-Service" },
30 /* Function which prints out usage message. */
37 "TOS target v%s options:\n"
38 " --set-tos value Set Type of Service field to one of the\n"
39 " following numeric or descriptive values:\n",
42 for (i = 0; i < sizeof(TOS_values)/sizeof(struct TOS_value);i++)
43 printf(" %s %u (0x%02x)\n",
50 static struct option opts[] = {
51 { "set-tos", 1, 0, '1' },
55 /* Initialize the target. */
57 init(struct ipt_entry_target *t, unsigned int *nfcache)
62 parse_tos(const unsigned char *s, struct ipt_tos_target_info *info)
66 if (string_to_number(s, 0, 255, &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;
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;
82 exit_error(PARAMETER_PROBLEM, "Bad TOS value `%s'", s);
85 /* Function which parses command options; returns true if it
88 parse(int c, char **argv, int invert, unsigned int *flags,
89 const struct ipt_entry *entry,
90 struct ipt_entry_target **target)
92 struct ipt_tos_target_info *tosinfo
93 = (struct ipt_tos_target_info *)(*target)->data;
98 exit_error(PARAMETER_PROBLEM,
99 "TOS target: Cant specify --set-tos twice");
100 parse_tos(optarg, tosinfo);
112 final_check(unsigned int flags)
115 exit_error(PARAMETER_PROBLEM,
116 "TOS target: Parameter --set-tos is required");
120 print_tos(u_int8_t tos, int 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);
131 printf("0x%02x ", tos);
134 /* Prints out the targinfo. */
136 print(const struct ipt_ip *ip,
137 const struct ipt_entry_target *target,
140 const struct ipt_tos_target_info *tosinfo =
141 (const struct ipt_tos_target_info *)target->data;
143 print_tos(tosinfo->tos, numeric);
146 /* Saves the union ipt_targinfo in parsable form to stdout. */
148 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
150 const struct ipt_tos_target_info *tosinfo =
151 (const struct ipt_tos_target_info *)target->data;
153 printf("--set-tos 0x%02x ", tosinfo->tos);
156 static struct iptables_target tos = {
159 .version = IPTABLES_VERSION,
160 .size = IPT_ALIGN(sizeof(struct ipt_tos_target_info)),
161 .userspacesize = IPT_ALIGN(sizeof(struct ipt_tos_target_info)),
165 .final_check = &final_check,
173 register_target(&tos);