• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/lib/cls.c     	CLI Classifier Helpers
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 /**
14  * @ingroup cli
15  * @defgroup cli_cls Classifiers
16  * @{
17  */
18 
19 #include <netlink/cli/utils.h>
20 #include <netlink/cli/cls.h>
21 #include <netlink/route/cls/ematch.h>
22 
nl_cli_cls_alloc(void)23 struct rtnl_cls *nl_cli_cls_alloc(void)
24 {
25 	struct rtnl_cls *cls;
26 
27 	if (!(cls = rtnl_cls_alloc()))
28 		nl_cli_fatal(ENOMEM, "Unable to allocate classifier object");
29 
30 	return cls;
31 }
32 
nl_cli_cls_alloc_cache(struct nl_sock * sock,int ifindex,uint32_t parent)33 struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex,
34 					uint32_t parent)
35 {
36 	struct nl_cache *cache;
37 	int err;
38 
39 	if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0)
40 		nl_cli_fatal(err, "Unable to allocate classifier cache: %s",
41 			     nl_geterror(err));
42 
43 	return cache;
44 }
45 
nl_cli_cls_parse_proto(struct rtnl_cls * cls,char * arg)46 void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg)
47 {
48 	int proto;
49 
50 	if ((proto = nl_str2ether_proto(arg)) < 0)
51 		nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg);
52 
53 	rtnl_cls_set_protocol(cls, proto);
54 }
55 
nl_cli_cls_parse_ematch(struct rtnl_cls * cls,char * arg)56 struct rtnl_ematch_tree *nl_cli_cls_parse_ematch(struct rtnl_cls *cls, char *arg)
57 {
58 	struct rtnl_ematch_tree *tree;
59 	char *errstr = NULL;
60 	int err;
61 
62 	if ((err = rtnl_ematch_parse_expr(arg, &errstr, &tree)) < 0)
63 		nl_cli_fatal(err, "Unable to parse ematch expression: %s",
64 				  errstr);
65 
66 	if (errstr)
67 		free(errstr);
68 
69 	return tree;
70 }
71 
72 /** @} */
73