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 |
| 15 | help(void) |
| 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 | |
Jan Engelhardt | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame] | 24 | static const struct option opts[] = { |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 25 | { "set-class", 1, 0, '1' }, |
| 26 | { 0 } |
| 27 | }; |
| 28 | |
| 29 | /* Initialize the target. */ |
| 30 | static void |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 31 | init(struct xt_entry_target *t, unsigned int *nfcache) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
Patrick McHardy | 2739cb8 | 2005-11-18 18:00:25 +0000 | [diff] [blame] | 35 | int string_to_priority(const char *s, unsigned int *p) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 36 | { |
| 37 | unsigned int i, j; |
| 38 | |
| 39 | if (sscanf(s, "%x:%x", &i, &j) != 2) |
| 40 | return 1; |
| 41 | |
| 42 | *p = TC_H_MAKE(i<<16, j); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* Function which parses command options; returns true if it |
| 47 | ate an option */ |
| 48 | static int |
| 49 | parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 50 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 51 | struct xt_entry_target **target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 52 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 53 | struct xt_classify_target_info *clinfo |
| 54 | = (struct xt_classify_target_info *)(*target)->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 55 | |
| 56 | switch (c) { |
| 57 | case '1': |
| 58 | if (string_to_priority(optarg, &clinfo->priority)) |
| 59 | exit_error(PARAMETER_PROBLEM, |
| 60 | "Bad class value `%s'", optarg); |
| 61 | if (*flags) |
| 62 | exit_error(PARAMETER_PROBLEM, |
| 63 | "CLASSIFY: Can't specify --set-class twice"); |
| 64 | *flags = 1; |
| 65 | break; |
| 66 | |
| 67 | default: |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | final_check(unsigned int flags) |
| 76 | { |
| 77 | if (!flags) |
| 78 | exit_error(PARAMETER_PROBLEM, |
| 79 | "CLASSIFY: Parameter --set-class is required"); |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | print_class(unsigned int priority, int numeric) |
| 84 | { |
| 85 | printf("%x:%x ", TC_H_MAJ(priority)>>16, TC_H_MIN(priority)); |
| 86 | } |
| 87 | |
| 88 | /* Prints out the targinfo. */ |
| 89 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 90 | print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 91 | const struct xt_entry_target *target, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 92 | int numeric) |
| 93 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 94 | const struct xt_classify_target_info *clinfo = |
| 95 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 96 | printf("CLASSIFY set "); |
| 97 | print_class(clinfo->priority, numeric); |
| 98 | } |
| 99 | |
| 100 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 101 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 102 | save(const void *ip, const struct xt_entry_target *target) |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 103 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 104 | const struct xt_classify_target_info *clinfo = |
| 105 | (const struct xt_classify_target_info *)target->data; |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 106 | |
| 107 | printf("--set-class %.4x:%.4x ", |
| 108 | TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority)); |
| 109 | } |
| 110 | |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 111 | static struct xtables_target classify = { |
| 112 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 113 | .name = "CLASSIFY", |
| 114 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 115 | .size = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 116 | .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 117 | .help = &help, |
| 118 | .init = &init, |
| 119 | .parse = &parse, |
| 120 | .final_check = &final_check, |
| 121 | .print = &print, |
| 122 | .save = &save, |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 123 | .extra_opts = opts, |
| 124 | }; |
| 125 | |
| 126 | static struct xtables_target classify6 = { |
| 127 | .family = AF_INET6, |
| 128 | .name = "CLASSIFY", |
| 129 | .version = IPTABLES_VERSION, |
| 130 | .size = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 131 | .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)), |
| 132 | .help = &help, |
| 133 | .init = &init, |
| 134 | .parse = &parse, |
| 135 | .final_check = &final_check, |
| 136 | .print = &print, |
| 137 | .save = &save, |
| 138 | .extra_opts = opts, |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | void _init(void) |
| 142 | { |
Yasuyuki KOZAKAI | e4cc20b | 2007-08-04 08:23:13 +0000 | [diff] [blame^] | 143 | xtables_register_target(&classify); |
| 144 | xtables_register_target(&classify6); |
Joszef Kadlecsik | 6ab626b | 2003-04-11 10:14:10 +0000 | [diff] [blame] | 145 | } |