Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add CLASSIFY target support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <getopt.h> |
| 6 | |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 7 | #include <xtables.h> |
| 8 | #include <linux/netfilter/x_tables.h> |
| 9 | #include <linux/netfilter/xt_CLASSIFY.h> |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/pkt_sched.h> |
| 12 | |
| 13 | /* Function which prints out usage message. */ |
| 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( |
| 18 | "CLASSIFY target v%s options:\n" |
| 19 | " --set-class [MAJOR:MINOR] Set skb->priority value\n" |
| 20 | "\n", |
| 21 | IPTABLES_VERSION); |
| 22 | } |
| 23 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 24 | static const struct option CLASSIFY_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { "set-class", 1, NULL, '1' }, |
| 26 | { } |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 29 | static int CLASSIFY_string_to_priority(const char *s, unsigned int *p) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 30 | { |
| 31 | unsigned int i, j; |
| 32 | |
| 33 | if (sscanf(s, "%x:%x", &i, &j) != 2) |
| 34 | return 1; |
| 35 | |
| 36 | *p = TC_H_MAKE(i<<16, j); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | /* Function which parses command options; returns true if it |
| 41 | ate an option */ |
| 42 | static int |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 43 | CLASSIFY_parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 44 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 45 | struct xt_entry_target **target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 46 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 47 | struct xt_classify_target_info *clinfo |
| 48 | = (struct xt_classify_target_info *)(*target)->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 49 | |
| 50 | switch (c) { |
| 51 | case '1': |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 52 | if (CLASSIFY_string_to_priority(optarg, &clinfo->priority)) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 53 | exit_error(PARAMETER_PROBLEM, |
| 54 | "Bad class value `%s'", optarg); |
| 55 | if (*flags) |
| 56 | exit_error(PARAMETER_PROBLEM, |
| 57 | "CLASSIFY: Can't specify --set-class twice"); |
| 58 | *flags = 1; |
| 59 | break; |
| 60 | |
| 61 | default: |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 69 | CLASSIFY_final_check(unsigned int flags) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 70 | { |
| 71 | if (!flags) |
| 72 | exit_error(PARAMETER_PROBLEM, |
| 73 | "CLASSIFY: Parameter --set-class is required"); |
| 74 | } |
| 75 | |
| 76 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 77 | CLASSIFY_print_class(unsigned int priority, int numeric) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 78 | { |
| 79 | printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority)); |
| 80 | } |
| 81 | |
| 82 | /* Prints out the targinfo. */ |
| 83 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 84 | CLASSIFY_print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 85 | const struct xt_entry_target *target, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 86 | int numeric) |
| 87 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 88 | const struct xt_classify_target_info *clinfo = |
| 89 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 90 | printf("CLASSIFY set "); |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 91 | CLASSIFY_print_class(clinfo->priority, numeric); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 95 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 96 | CLASSIFY_save(const void *ip, const struct xt_entry_target *target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 97 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 98 | const struct xt_classify_target_info *clinfo = |
| 99 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 100 | |
| 101 | printf("--set-class %.4x:%.4x ", |
| 102 | TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority)); |
| 103 | } |
| 104 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 105 | static struct xtables_target classify_target = { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 106 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 107 | .name = "CLASSIFY", |
| 108 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 109 | .size = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 110 | .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 111 | .help = CLASSIFY_help, |
| 112 | .parse = CLASSIFY_parse, |
| 113 | .final_check = CLASSIFY_final_check, |
| 114 | .print = CLASSIFY_print, |
| 115 | .save = CLASSIFY_save, |
| 116 | .extra_opts = CLASSIFY_opts, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 119 | static struct xtables_target classify_target6 = { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame] | 120 | .family = AF_INET6, |
| 121 | .name = "CLASSIFY", |
| 122 | .version = IPTABLES_VERSION, |
| 123 | .size = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 124 | .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 125 | .help = CLASSIFY_help, |
| 126 | .parse = CLASSIFY_parse, |
| 127 | .final_check = CLASSIFY_final_check, |
| 128 | .print = CLASSIFY_print, |
| 129 | .save = CLASSIFY_save, |
| 130 | .extra_opts = CLASSIFY_opts, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | void _init(void) |
| 134 | { |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame^] | 135 | xtables_register_target(&classify_target); |
| 136 | xtables_register_target(&classify_target6); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 137 | } |