libnl  3.2.11
basic.c
1 /*
2  * lib/route/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 Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2008-2011 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup cls
14  * @defgroup cls_basic Basic Classifier
15  *
16  * @par Introduction
17  * The basic classifier is the simplest form of a classifier. It does
18  * not have any special classification capabilities, instead it can be
19  * used to classify exclusively based on extended matches or to
20  * create a "catch-all" filter.
21  *
22  * @{
23  */
24 
25 #include <netlink-local.h>
26 #include <netlink-tc.h>
27 #include <netlink/netlink.h>
28 #include <netlink/route/tc-api.h>
29 #include <netlink/route/classifier.h>
30 #include <netlink/route/cls/basic.h>
31 #include <netlink/route/cls/ematch.h>
32 
33 struct rtnl_basic
34 {
35  uint32_t b_target;
36  struct rtnl_ematch_tree * b_ematch;
37  int b_mask;
38 };
39 
40 /** @cond SKIP */
41 #define BASIC_ATTR_TARGET 0x001
42 #define BASIC_ATTR_EMATCH 0x002
43 /** @endcond */
44 
45 static struct nla_policy basic_policy[TCA_BASIC_MAX+1] = {
46  [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
47  [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
48 };
49 
50 static int basic_clone(void *_dst, void *_src)
51 {
52  return -NLE_OPNOTSUPP;
53 }
54 
55 static void basic_free_data(struct rtnl_tc *tc, void *data)
56 {
57  struct rtnl_basic *b = data;
58 
59  if (!b)
60  return;
61 
62  rtnl_ematch_tree_free(b->b_ematch);
63 }
64 
65 static int basic_msg_parser(struct rtnl_tc *tc, void *data)
66 {
67  struct nlattr *tb[TCA_BASIC_MAX + 1];
68  struct rtnl_basic *b = data;
69  int err;
70 
71  err = tca_parse(tb, TCA_BASIC_MAX, tc, basic_policy);
72  if (err < 0)
73  return err;
74 
75  if (tb[TCA_BASIC_CLASSID]) {
76  b->b_target = nla_get_u32(tb[TCA_BASIC_CLASSID]);
77  b->b_mask |= BASIC_ATTR_TARGET;
78  }
79 
80  if (tb[TCA_BASIC_EMATCHES]) {
81  if ((err = rtnl_ematch_parse_attr(tb[TCA_BASIC_EMATCHES],
82  &b->b_ematch)) < 0)
83  return err;
84 
85  if (b->b_ematch)
86  b->b_mask |= BASIC_ATTR_EMATCH;
87  }
88 
89  return 0;
90 }
91 
92 static void basic_dump_line(struct rtnl_tc *tc, void *data,
93  struct nl_dump_params *p)
94 {
95  struct rtnl_basic *b = data;
96  char buf[32];
97 
98  if (!b)
99  return;
100 
101  if (b->b_mask & BASIC_ATTR_EMATCH)
102  nl_dump(p, " ematch");
103  else
104  nl_dump(p, " match-all");
105 
106  if (b->b_mask & BASIC_ATTR_TARGET)
107  nl_dump(p, " target %s",
108  rtnl_tc_handle2str(b->b_target, buf, sizeof(buf)));
109 }
110 
111 static void basic_dump_details(struct rtnl_tc *tc, void *data,
112  struct nl_dump_params *p)
113 {
114  struct rtnl_basic *b = data;
115 
116  if (!b)
117  return;
118 
119  if (b->b_mask & BASIC_ATTR_EMATCH) {
120  nl_dump_line(p, " ematch ");
121  rtnl_ematch_tree_dump(b->b_ematch, p);
122  } else
123  nl_dump(p, "no options.\n");
124 }
125 
126 static int basic_msg_fill(struct rtnl_tc *tc, void *data,
127  struct nl_msg *msg)
128 {
129  struct rtnl_basic *b = data;
130 
131  if (!b)
132  return 0;
133 
134  if (!(b->b_mask & BASIC_ATTR_TARGET))
135  return -NLE_MISSING_ATTR;
136 
137  NLA_PUT_U32(msg, TCA_BASIC_CLASSID, b->b_target);
138 
139  if (b->b_mask & BASIC_ATTR_EMATCH &&
140  rtnl_ematch_fill_attr(msg, TCA_BASIC_EMATCHES, b->b_ematch) < 0)
141  goto nla_put_failure;
142 
143  return 0;
144 
145 nla_put_failure:
146  return -NLE_NOMEM;
147 }
148 
149 /**
150  * @name Attribute Modifications
151  * @{
152  */
153 
154 void rtnl_basic_set_target(struct rtnl_cls *cls, uint32_t target)
155 {
156  struct rtnl_basic *b;
157 
158  if (!(b = rtnl_tc_data(TC_CAST(cls))))
159  return;
160 
161  b->b_target = target;
162  b->b_mask |= BASIC_ATTR_TARGET;
163 }
164 
165 uint32_t rtnl_basic_get_target(struct rtnl_cls *cls)
166 {
167  struct rtnl_basic *b;
168 
169  if (!(b = rtnl_tc_data(TC_CAST(cls))))
170  return 0;
171 
172  return b->b_target;
173 }
174 
175 void rtnl_basic_set_ematch(struct rtnl_cls *cls, struct rtnl_ematch_tree *tree)
176 {
177  struct rtnl_basic *b;
178 
179  if (!(b = rtnl_tc_data(TC_CAST(cls))))
180  return;
181 
182  if (b->b_ematch) {
183  rtnl_ematch_tree_free(b->b_ematch);
184  b->b_mask &= ~BASIC_ATTR_EMATCH;
185  }
186 
187  b->b_ematch = tree;
188 
189  if (tree)
190  b->b_mask |= BASIC_ATTR_EMATCH;
191 }
192 
193 struct rtnl_ematch_tree *rtnl_basic_get_ematch(struct rtnl_cls *cls)
194 {
195  struct rtnl_basic *b;
196 
197  if (!(b = rtnl_tc_data(TC_CAST(cls))))
198  return NULL;
199 
200  return b->b_ematch;
201 }
202 
203 /** @} */
204 
205 static struct rtnl_tc_ops basic_ops = {
206  .to_kind = "basic",
207  .to_type = RTNL_TC_TYPE_CLS,
208  .to_size = sizeof(struct rtnl_basic),
209  .to_msg_parser = basic_msg_parser,
210  .to_clone = basic_clone,
211  .to_free_data = basic_free_data,
212  .to_msg_fill = basic_msg_fill,
213  .to_dump = {
214  [NL_DUMP_LINE] = basic_dump_line,
215  [NL_DUMP_DETAILS] = basic_dump_details,
216  },
217 };
218 
219 static void __init basic_init(void)
220 {
221  rtnl_tc_register(&basic_ops);
222 }
223 
224 static void __exit basic_exit(void)
225 {
226  rtnl_tc_unregister(&basic_ops);
227 }
228 
229 /** @} */