xref: /aosp_15_r20/external/libnl/src/lib/cls.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2010-2011 Thomas Graf <[email protected]>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_cls Classifiers
9  * @{
10  */
11 
12 #include "nl-default.h"
13 
14 #include <netlink/cli/utils.h>
15 #include <netlink/cli/cls.h>
16 #include <netlink/route/cls/ematch.h>
17 
nl_cli_cls_alloc(void)18 struct rtnl_cls *nl_cli_cls_alloc(void)
19 {
20 	struct rtnl_cls *cls;
21 
22 	if (!(cls = rtnl_cls_alloc()))
23 		nl_cli_fatal(ENOMEM, "Unable to allocate classifier object");
24 
25 	return cls;
26 }
27 
nl_cli_cls_alloc_cache(struct nl_sock * sock,int ifindex,uint32_t parent)28 struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex,
29 					uint32_t parent)
30 {
31 	struct nl_cache *cache;
32 	int err;
33 
34 	if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0)
35 		nl_cli_fatal(err, "Unable to allocate classifier cache: %s",
36 			     nl_geterror(err));
37 
38 	return cache;
39 }
40 
nl_cli_cls_parse_proto(struct rtnl_cls * cls,char * arg)41 void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg)
42 {
43 	int proto;
44 
45 	if ((proto = nl_str2ether_proto(arg)) < 0)
46 		nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg);
47 
48 	rtnl_cls_set_protocol(cls, proto);
49 }
50 
nl_cli_cls_parse_ematch(struct rtnl_cls * cls,char * arg)51 struct rtnl_ematch_tree *nl_cli_cls_parse_ematch(struct rtnl_cls *cls, char *arg)
52 {
53 	struct rtnl_ematch_tree *tree;
54 	char *errstr = NULL;
55 	int err;
56 
57 	if ((err = rtnl_ematch_parse_expr(arg, &errstr, &tree)) < 0)
58 		nl_cli_fatal(err, "Unable to parse ematch expression: %s",
59 				  errstr);
60 
61 	if (errstr)
62 		free(errstr);
63 
64 	return tree;
65 }
66 
67 /** @} */
68