Home | History | Annotate | Download | only in extensions
      1 #include <stdio.h>
      2 #include <xtables.h>
      3 #include <linux/netfilter/xt_CLASSIFY.h>
      4 #include <linux/pkt_sched.h>
      5 
      6 enum {
      7 	O_SET_CLASS = 0,
      8 };
      9 
     10 static void
     11 CLASSIFY_help(void)
     12 {
     13 	printf(
     14 "CLASSIFY target options:\n"
     15 "--set-class MAJOR:MINOR    Set skb->priority value (always hexadecimal!)\n");
     16 }
     17 
     18 static const struct xt_option_entry CLASSIFY_opts[] = {
     19 	{.name = "set-class", .id = O_SET_CLASS, .type = XTTYPE_STRING,
     20 	 .flags = XTOPT_MAND},
     21 	XTOPT_TABLEEND,
     22 };
     23 
     24 static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
     25 {
     26 	unsigned int i, j;
     27 
     28 	if (sscanf(s, "%x:%x", &i, &j) != 2)
     29 		return 1;
     30 
     31 	*p = TC_H_MAKE(i<<16, j);
     32 	return 0;
     33 }
     34 
     35 static void CLASSIFY_parse(struct xt_option_call *cb)
     36 {
     37 	struct xt_classify_target_info *clinfo = cb->data;
     38 
     39 	xtables_option_parse(cb);
     40 	if (CLASSIFY_string_to_priority(cb->arg, &clinfo->priority))
     41 		xtables_error(PARAMETER_PROBLEM,
     42 			   "Bad class value \"%s\"", cb->arg);
     43 }
     44 
     45 static void
     46 CLASSIFY_print_class(unsigned int priority, int numeric)
     47 {
     48 	printf(" %x:%x", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
     49 }
     50 
     51 static void
     52 CLASSIFY_print(const void *ip,
     53       const struct xt_entry_target *target,
     54       int numeric)
     55 {
     56 	const struct xt_classify_target_info *clinfo =
     57 		(const struct xt_classify_target_info *)target->data;
     58 	printf(" CLASSIFY set");
     59 	CLASSIFY_print_class(clinfo->priority, numeric);
     60 }
     61 
     62 static void
     63 CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
     64 {
     65 	const struct xt_classify_target_info *clinfo =
     66 		(const struct xt_classify_target_info *)target->data;
     67 
     68 	printf(" --set-class %.4x:%.4x",
     69 	       TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
     70 }
     71 
     72 static struct xtables_target classify_target = {
     73 	.family		= NFPROTO_UNSPEC,
     74 	.name		= "CLASSIFY",
     75 	.version	= XTABLES_VERSION,
     76 	.size		= XT_ALIGN(sizeof(struct xt_classify_target_info)),
     77 	.userspacesize	= XT_ALIGN(sizeof(struct xt_classify_target_info)),
     78 	.help		= CLASSIFY_help,
     79 	.print		= CLASSIFY_print,
     80 	.save		= CLASSIFY_save,
     81 	.x6_parse	= CLASSIFY_parse,
     82 	.x6_options	= CLASSIFY_opts,
     83 };
     84 
     85 void _init(void)
     86 {
     87 	xtables_register_target(&classify_target);
     88 }
     89