Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add CLASSIFY target support. */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 2 | #include <stdbool.h> |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 8 | #include <xtables.h> |
| 9 | #include <linux/netfilter/x_tables.h> |
| 10 | #include <linux/netfilter/xt_CLASSIFY.h> |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/pkt_sched.h> |
| 13 | |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 14 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 15 | CLASSIFY_help(void) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 16 | { |
| 17 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 18 | "CLASSIFY target options:\n" |
Jan Engelhardt | c9ccba5 | 2009-04-04 18:43:15 +0200 | [diff] [blame] | 19 | "--set-class MAJOR:MINOR Set skb->priority value (always hexadecimal!)\n"); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 20 | } |
| 21 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 22 | static const struct option CLASSIFY_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 23 | {.name = "set-class", .has_arg = true, .val = '1'}, |
| 24 | XT_GETOPT_TABLEEND, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 27 | static int CLASSIFY_string_to_priority(const char *s, unsigned int *p) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 28 | { |
| 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 Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 38 | static int |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 39 | CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 40 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 41 | struct xt_entry_target **target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 42 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 43 | struct xt_classify_target_info *clinfo |
| 44 | = (struct xt_classify_target_info *)(*target)->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 45 | |
| 46 | switch (c) { |
| 47 | case '1': |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 48 | if (CLASSIFY_string_to_priority(optarg, &clinfo->priority)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 49 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 50 | "Bad class value `%s'", optarg); |
| 51 | if (*flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 52 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 53 | "CLASSIFY: Can't specify --set-class twice"); |
| 54 | *flags = 1; |
| 55 | break; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 62 | CLASSIFY_final_check(unsigned int flags) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 63 | { |
| 64 | if (!flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 65 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 66 | "CLASSIFY: Parameter --set-class is required"); |
| 67 | } |
| 68 | |
| 69 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 70 | CLASSIFY_print_class(unsigned int priority, int numeric) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 71 | { |
| 72 | printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority)); |
| 73 | } |
| 74 | |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 75 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 76 | CLASSIFY_print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 77 | const struct xt_entry_target *target, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 78 | int numeric) |
| 79 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 80 | const struct xt_classify_target_info *clinfo = |
| 81 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 82 | printf("CLASSIFY set "); |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 83 | CLASSIFY_print_class(clinfo->priority, numeric); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 86 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 87 | CLASSIFY_save(const void *ip, const struct xt_entry_target *target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 88 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 89 | const struct xt_classify_target_info *clinfo = |
| 90 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 91 | |
| 92 | printf("--set-class %.4x:%.4x ", |
| 93 | TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority)); |
| 94 | } |
| 95 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 96 | static struct xtables_target classify_target = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 97 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 98 | .name = "CLASSIFY", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 99 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 100 | .size = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 101 | .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 102 | .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 Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | void _init(void) |
| 111 | { |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 112 | xtables_register_target(&classify_target); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 113 | } |