blob: 81c096f9c46f6a8840e3613965e94ff8104c840d [file] [log] [blame]
Jan Engelhardt0720c122008-01-20 13:22:42 +00001/*
2 * Shared library add-on to iptables to add tos match support
3 *
4 * Copyright © CC Computer Consultants GmbH, 2007
5 * Contact: Jan Engelhardt <jengelh@computergmbh.de>
6 */
7#include <getopt.h>
8#include <netdb.h>
9#include <stdbool.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#include <xtables.h>
15#include <linux/netfilter/xt_dscp.h>
Jan Engelhardt0720c122008-01-20 13:22:42 +000016#include "tos_values.c"
17
Jan Engelhardt350661a2010-01-31 22:42:52 +010018struct ipt_tos_info {
Jan Engelhardt7ac40522011-01-07 12:34:04 +010019 uint8_t tos;
20 uint8_t invert;
Jan Engelhardt350661a2010-01-31 22:42:52 +010021};
22
Jan Engelhardt0720c122008-01-20 13:22:42 +000023enum {
Jan Engelhardtd8f59192011-04-29 02:12:56 +020024 O_TOS = 1 << 0,
Jan Engelhardt0720c122008-01-20 13:22:42 +000025};
26
Jan Engelhardtd8f59192011-04-29 02:12:56 +020027static const struct xt_option_entry tos_mt_opts_v0[] = {
28 {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK,
Jan Engelhardt06312da2011-05-01 19:58:56 +020029 .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0xFF},
Jan Engelhardtd8f59192011-04-29 02:12:56 +020030 XTOPT_TABLEEND,
31};
32
33static const struct xt_option_entry tos_mt_opts[] = {
34 {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK,
Jan Engelhardt06312da2011-05-01 19:58:56 +020035 .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0x3F},
Jan Engelhardtd8f59192011-04-29 02:12:56 +020036 XTOPT_TABLEEND,
Jan Engelhardt0720c122008-01-20 13:22:42 +000037};
38
39static void tos_mt_help(void)
40{
41 const struct tos_symbol_info *symbol;
42
43 printf(
44"tos match options:\n"
45"[!] --tos value[/mask] Match Type of Service/Priority field value\n"
46"[!] --tos symbol Match TOS field (IPv4 only) by symbol\n"
47" Accepted symbolic names for value are:\n");
48
49 for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
50 printf(" (0x%02x) %2u %s\n",
51 symbol->value, symbol->value, symbol->name);
52
53 printf("\n");
54}
55
Jan Engelhardtd8f59192011-04-29 02:12:56 +020056static void tos_mt_parse_v0(struct xt_option_call *cb)
Jan Engelhardt0720c122008-01-20 13:22:42 +000057{
Jan Engelhardtd8f59192011-04-29 02:12:56 +020058 struct ipt_tos_info *info = cb->data;
Jan Engelhardt0720c122008-01-20 13:22:42 +000059
Jan Engelhardtd8f59192011-04-29 02:12:56 +020060 xtables_option_parse(cb);
61 if (cb->val.tos_mask != 0xFF)
62 xtables_error(PARAMETER_PROBLEM, "tos: Your kernel is "
63 "too old to support anything besides /0xFF "
64 "as a mask.");
65 info->tos = cb->val.tos_value;
66 if (cb->invert)
67 info->invert = true;
Jan Engelhardt0720c122008-01-20 13:22:42 +000068}
69
Jan Engelhardtd8f59192011-04-29 02:12:56 +020070static void tos_mt_parse(struct xt_option_call *cb)
Jan Engelhardt0720c122008-01-20 13:22:42 +000071{
Jan Engelhardtd8f59192011-04-29 02:12:56 +020072 struct xt_tos_match_info *info = cb->data;
Jan Engelhardt0720c122008-01-20 13:22:42 +000073
Jan Engelhardtd8f59192011-04-29 02:12:56 +020074 xtables_option_parse(cb);
75 info->tos_value = cb->val.tos_value;
76 info->tos_mask = cb->val.tos_mask;
77 if (cb->invert)
78 info->invert = true;
Jan Engelhardt0720c122008-01-20 13:22:42 +000079}
80
81static void tos_mt_print_v0(const void *ip, const struct xt_entry_match *match,
82 int numeric)
83{
84 const struct ipt_tos_info *info = (const void *)match->data;
85
Jan Engelhardt73866352010-12-18 02:04:59 +010086 printf(" tos match ");
Jan Engelhardt0720c122008-01-20 13:22:42 +000087 if (info->invert)
88 printf("!");
89 if (numeric || !tos_try_print_symbolic("", info->tos, 0x3F))
Jan Engelhardt73866352010-12-18 02:04:59 +010090 printf("0x%02x", info->tos);
Jan Engelhardt0720c122008-01-20 13:22:42 +000091}
92
93static void tos_mt_print(const void *ip, const struct xt_entry_match *match,
94 int numeric)
95{
96 const struct xt_tos_match_info *info = (const void *)match->data;
97
Jan Engelhardt73866352010-12-18 02:04:59 +010098 printf(" tos match");
Jan Engelhardt0720c122008-01-20 13:22:42 +000099 if (info->invert)
100 printf("!");
101 if (numeric ||
102 !tos_try_print_symbolic("", info->tos_value, info->tos_mask))
Jan Engelhardt73866352010-12-18 02:04:59 +0100103 printf("0x%02x/0x%02x", info->tos_value, info->tos_mask);
Jan Engelhardt0720c122008-01-20 13:22:42 +0000104}
105
106static void tos_mt_save_v0(const void *ip, const struct xt_entry_match *match)
107{
108 const struct ipt_tos_info *info = (const void *)match->data;
109
110 if (info->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100111 printf(" !");
112 printf(" --tos 0x%02x", info->tos);
Jan Engelhardt0720c122008-01-20 13:22:42 +0000113}
114
115static void tos_mt_save(const void *ip, const struct xt_entry_match *match)
116{
117 const struct xt_tos_match_info *info = (const void *)match->data;
118
119 if (info->invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100120 printf(" !");
121 printf(" --tos 0x%02x/0x%02x", info->tos_value, info->tos_mask);
Jan Engelhardt0720c122008-01-20 13:22:42 +0000122}
123
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200124static struct xtables_match tos_mt_reg[] = {
125 {
126 .version = XTABLES_VERSION,
127 .name = "tos",
128 .family = NFPROTO_IPV4,
129 .revision = 0,
130 .size = XT_ALIGN(sizeof(struct ipt_tos_info)),
131 .userspacesize = XT_ALIGN(sizeof(struct ipt_tos_info)),
132 .help = tos_mt_help,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200133 .print = tos_mt_print_v0,
134 .save = tos_mt_save_v0,
Jan Engelhardtd8f59192011-04-29 02:12:56 +0200135 .x6_parse = tos_mt_parse_v0,
136 .x6_options = tos_mt_opts_v0,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200137 },
138 {
139 .version = XTABLES_VERSION,
140 .name = "tos",
141 .family = NFPROTO_UNSPEC,
142 .revision = 1,
143 .size = XT_ALIGN(sizeof(struct xt_tos_match_info)),
144 .userspacesize = XT_ALIGN(sizeof(struct xt_tos_match_info)),
145 .help = tos_mt_help,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200146 .print = tos_mt_print,
147 .save = tos_mt_save,
Jan Engelhardtd8f59192011-04-29 02:12:56 +0200148 .x6_parse = tos_mt_parse,
149 .x6_options = tos_mt_opts,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200150 },
Jan Engelhardt0720c122008-01-20 13:22:42 +0000151};
152
153void _init(void)
154{
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200155 xtables_register_matches(tos_mt_reg, ARRAY_SIZE(tos_mt_reg));
Jan Engelhardt0720c122008-01-20 13:22:42 +0000156}