blob: 2920ee8d150fd57502510d9d2a0a81877b98ddcb [file] [log] [blame]
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +00001/* Shared library add-on to iptables to add CLASSIFY target support. */
Jan Engelhardt32b8e612010-07-23 21:16:14 +02002#include <stdbool.h>
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +00003#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +00008#include <xtables.h>
9#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter/xt_CLASSIFY.h>
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000011#include <linux/types.h>
12#include <linux/pkt_sched.h>
13
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000014static void
László Attila Tóth72118882007-10-08 05:12:42 +000015CLASSIFY_help(void)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000016{
17 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020018"CLASSIFY target options:\n"
Jan Engelhardtc9ccba52009-04-04 18:43:15 +020019"--set-class MAJOR:MINOR Set skb->priority value (always hexadecimal!)\n");
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000020}
21
László Attila Tóth72118882007-10-08 05:12:42 +000022static const struct option CLASSIFY_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +020023 {.name = "set-class", .has_arg = true, .val = '1'},
24 XT_GETOPT_TABLEEND,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000025};
26
László Attila Tóth72118882007-10-08 05:12:42 +000027static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000028{
29 unsigned int i, j;
30
31 if (sscanf(s, "%x:%x", &i, &j) != 2)
32 return 1;
33
34 *p = TC_H_MAKE(i<<16, j);
35 return 0;
36}
37
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000038static int
László Attila Tóth72118882007-10-08 05:12:42 +000039CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000040 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000041 struct xt_entry_target **target)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000042{
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +000043 struct xt_classify_target_info *clinfo
44 = (struct xt_classify_target_info *)(*target)->data;
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000045
46 switch (c) {
47 case '1':
László Attila Tóth72118882007-10-08 05:12:42 +000048 if (CLASSIFY_string_to_priority(optarg, &clinfo->priority))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010049 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000050 "Bad class value `%s'", optarg);
51 if (*flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010052 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000053 "CLASSIFY: Can't specify --set-class twice");
54 *flags = 1;
55 break;
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000056 }
57
58 return 1;
59}
60
61static void
László Attila Tóth72118882007-10-08 05:12:42 +000062CLASSIFY_final_check(unsigned int flags)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000063{
64 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010065 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000066 "CLASSIFY: Parameter --set-class is required");
67}
68
69static void
László Attila Tóth72118882007-10-08 05:12:42 +000070CLASSIFY_print_class(unsigned int priority, int numeric)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000071{
72 printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
73}
74
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000075static void
László Attila Tóth72118882007-10-08 05:12:42 +000076CLASSIFY_print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000077 const struct xt_entry_target *target,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000078 int numeric)
79{
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +000080 const struct xt_classify_target_info *clinfo =
81 (const struct xt_classify_target_info *)target->data;
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000082 printf("CLASSIFY set ");
László Attila Tóth72118882007-10-08 05:12:42 +000083 CLASSIFY_print_class(clinfo->priority, numeric);
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000084}
85
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000086static void
László Attila Tóth72118882007-10-08 05:12:42 +000087CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000088{
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +000089 const struct xt_classify_target_info *clinfo =
90 (const struct xt_classify_target_info *)target->data;
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +000091
92 printf("--set-class %.4x:%.4x ",
93 TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
94}
95
László Attila Tóth72118882007-10-08 05:12:42 +000096static struct xtables_target classify_target = {
Jan Engelhardt42979362009-06-01 11:56:23 +020097 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +000098 .name = "CLASSIFY",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020099 .version = XTABLES_VERSION,
Yasuyuki KOZAKAIe4cc20b2007-08-04 08:23:13 +0000100 .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
101 .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
László Attila Tóth72118882007-10-08 05:12:42 +0000102 .help = CLASSIFY_help,
103 .parse = CLASSIFY_parse,
104 .final_check = CLASSIFY_final_check,
105 .print = CLASSIFY_print,
106 .save = CLASSIFY_save,
107 .extra_opts = CLASSIFY_opts,
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +0000108};
109
110void _init(void)
111{
László Attila Tóth72118882007-10-08 05:12:42 +0000112 xtables_register_target(&classify_target);
Joszef Kadlecsik6ab626b2003-04-11 10:14:10 +0000113}