1 /*
2  * src/cls/basic.c	Basic Classifier
3  *
4  *	This library is free software; you can redistribute it and/or
5  *	modify it under the terms of the GNU General Public License as
6  *	published by the Free Software Foundation version 2 of the License.
7  *
8  * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch>
9  */
10 
11 #include "utils.h"
12 #include <netlink/route/cls/basic.h>
13 #include <netlink/route/cls/ematch.h>
14 
print_usage(void)15 static void print_usage(void)
16 {
17 	printf(
18 "Usage: ... basic [OPTIONS]...\n"
19 "\n"
20 "Options\n"
21 " -h, --help                Show this help.\n"
22 " -e, --ematch=MATCH        Extended match (See --ematch help).\n"
23 " -c, --classid=HANDLE      Target class to classify matching packets to.\n"
24 	);
25 	exit(0);
26 }
27 
basic_parse_argv(struct rtnl_cls * cls,int argc,char ** argv)28 static void basic_parse_argv(struct rtnl_cls *cls, int argc, char **argv)
29 {
30 	uint32_t classid;
31 
32 	for (;;) {
33 		int c, optidx = 0, err;
34 		static struct option long_opts[] = {
35 			{ "help", 0, 0, 'h' },
36 			{ "ematch", 1, 0, 'e' },
37 			{ "classid", 1, 0, 'c' },
38 			{ 0, 0, 0, 0 }
39 		};
40 
41 		c = getopt_long(argc, argv, "he:c:", long_opts, &optidx);
42 		if (c == -1)
43 			break;
44 
45 		switch (c) {
46 		case '?':
47 			exit(NLE_INVAL);
48 
49 		case 'h':
50 			print_usage();
51 
52 		case 'e':
53 #if 0
54 			if ((err = parse_ematch_syntax(optarg, &tree)) < 0)
55 				fatal(err, "Error while parsing ematch: %s",
56 				      nl_geterror(err));
57 
58 			if ((err = rtnl_basic_set_ematch(cls, tree)) < 0)
59 				fatal(err, "Unable to set ematch: %s",
60 					nl_geterror(err));
61 #endif
62 			break;
63 
64 		case 'c':
65 			if ((err = rtnl_tc_str2handle(optarg, &classid)) < 0)
66 				fatal(err, "Invalid classid \"%s\": %s",
67 				      optarg, nl_geterror(err));
68 
69 			if ((err = rtnl_basic_set_classid(cls, classid)) < 0)
70 				fatal(err, "Unable to set classid: %s",
71 				      nl_geterror(err));
72 			break;
73 		}
74  	}
75 }
76 
77 static struct cls_module basic_module = {
78 	.name		= "basic",
79 	.parse_argv	= basic_parse_argv,
80 };
81 
basic_init(void)82 static void __attribute__ ((constructor)) basic_init(void)
83 {
84 	register_cls_module(&basic_module);
85 }
86 
basic_exit(void)87 static void __attribute__ ((destructor)) basic_exit(void)
88 {
89 	unregister_cls_module(&basic_module);
90 }
91