libnl  3.2.24-rc1
act.c
1 /*
2  * lib/route/act.c Action
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) 2013 Cong Wang <xiyou.wangcong@gmail.com>
10  */
11 
12 /**
13  * @ingroup tc
14  * @defgroup act Action
15  * @{
16  */
17 
18 #include <netlink-private/netlink.h>
19 #include <netlink-private/tc.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink-private/route/tc-api.h>
23 #include <netlink/route/link.h>
24 
25 
26 static struct nl_object_ops act_obj_ops;
27 static struct nl_cache_ops rtnl_act_ops;
28 
29 int rtnl_act_append(struct rtnl_act **head, struct rtnl_act *new)
30 {
31  struct rtnl_act *p_act;
32  int count = 1;
33 
34  if (*head == NULL) {
35  *head = new;
36  return 0;
37  }
38 
39  p_act = *head;
40  while (p_act->a_next) {
41  ++count;
42  p_act = p_act->a_next;
43  }
44 
45  if (count > TCA_ACT_MAX_PRIO)
46  return -NLE_RANGE;
47 
48  p_act->a_next = new;
49  return 0;
50 }
51 
52 int rtnl_act_remove(struct rtnl_act **head, struct rtnl_act *act)
53 {
54  struct rtnl_act *a, **ap;
55 
56  for (ap = head; (a = *ap) != NULL; ap = &a->a_next)
57  if (a == act)
58  break;
59  if (a) {
60  *ap = a->a_next;
61  a->a_next = NULL;
62  return 0;
63  }
64 
65  return -NLE_OBJ_NOTFOUND;
66 }
67 
68 static int rtnl_act_fill_one(struct nl_msg *msg, struct rtnl_act *act, int order)
69 {
70  struct rtnl_tc *tc = TC_CAST(act);
71  struct rtnl_tc_ops *ops;
72  struct nlattr *nest;
73  int err = -NLE_NOMEM;
74 
75  nest = nla_nest_start(msg, order);
76  if (!nest)
77  goto nla_put_failure;
78 
79  if (tc->ce_mask & TCA_ATTR_KIND)
80  NLA_PUT_STRING(msg, TCA_ACT_KIND, tc->tc_kind);
81 
82  ops = rtnl_tc_get_ops(tc);
83  if (ops && (ops->to_msg_fill || ops->to_msg_fill_raw)) {
84  struct nlattr *opts;
85  void *data = rtnl_tc_data(tc);
86 
87  if (ops->to_msg_fill) {
88  if (!(opts = nla_nest_start(msg, TCA_ACT_OPTIONS)))
89  goto nla_put_failure;
90 
91  if ((err = ops->to_msg_fill(tc, data, msg)) < 0)
92  goto nla_put_failure;
93 
94  nla_nest_end(msg, opts);
95  } else if ((err = ops->to_msg_fill_raw(tc, data, msg)) < 0)
96  goto nla_put_failure;
97  }
98  nla_nest_end(msg, nest);
99  return 0;
100 
101 nla_put_failure:
102  return err;
103 }
104 
105 int rtnl_act_fill(struct nl_msg *msg, int attrtype, struct rtnl_act *act)
106 {
107  struct rtnl_act *p_act = act;
108  struct nlattr *nest;
109  int err, order = 0;
110 
111  nest = nla_nest_start(msg, attrtype);
112  if (!nest)
113  return -NLE_MSGSIZE;
114 
115  while (p_act) {
116  err = rtnl_act_fill_one(msg, p_act, ++order);
117  if (err)
118  return err;
119  p_act = p_act->a_next;
120  }
121 
122  nla_nest_end(msg, nest);
123  return 0;
124 }
125 
126 static int rtnl_act_msg_build(struct rtnl_act *act, int type, int flags,
127  struct nl_msg **result)
128 {
129  struct nl_msg *msg;
130  struct tcamsg tcahdr = {
131  .tca_family = AF_UNSPEC,
132  };
133  int err = -NLE_MSGSIZE;
134 
135  msg = nlmsg_alloc_simple(type, flags);
136  if (!msg)
137  return -NLE_NOMEM;
138 
139  if (nlmsg_append(msg, &tcahdr, sizeof(tcahdr), NLMSG_ALIGNTO) < 0)
140  goto nla_put_failure;
141 
142  err = rtnl_act_fill(msg, TCA_ACT_TAB, act);
143  if (err < 0)
144  goto nla_put_failure;
145 
146  *result = msg;
147  return 0;
148 
149 nla_put_failure:
150  nlmsg_free(msg);
151  return err;
152 }
153 
154 static int act_build(struct rtnl_act *act, int type, int flags,
155  struct nl_msg **result)
156 {
157  int err;
158 
159  err = rtnl_act_msg_build(act, type, flags, result);
160  if (err < 0)
161  return err;
162  return 0;
163 }
164 
165 /**
166  * @name Allocation/Freeing
167  * @{
168  */
169 
170 struct rtnl_act *rtnl_act_alloc(void)
171 {
172  struct rtnl_tc *tc;
173 
174  tc = TC_CAST(nl_object_alloc(&act_obj_ops));
175  if (tc)
176  tc->tc_type = RTNL_TC_TYPE_ACT;
177 
178  return (struct rtnl_act *) tc;
179 }
180 
181 void rtnl_act_put(struct rtnl_act *act)
182 {
183  nl_object_put((struct nl_object *) act);
184 }
185 
186 /** @} */
187 
188 /**
189  * @name Addition/Modification/Deletion
190  * @{
191  */
192 
193 /**
194  * Build a netlink message requesting the addition of an action
195  * @arg act Action to add
196  * @arg flags Additional netlink message flags
197  * @arg result Pointer to store resulting netlink message
198  *
199  * The behaviour of this function is identical to rtnl_act_add() with
200  * the exception that it will not send the message but return it int the
201  * provided return pointer instead.
202  *
203  * @see rtnl_act_add()
204  *
205  * @return 0 on success or a negative error code.
206  */
207 int rtnl_act_build_add_request(struct rtnl_act *act, int flags,
208  struct nl_msg **result)
209 {
210  return act_build(act, RTM_NEWACTION, flags, result);
211 }
212 
213 /**
214  * Add/Update action
215  * @arg sk Netlink socket
216  * @arg act Action to add/update
217  * @arg flags Additional netlink message flags
218  *
219  * Builds a \c RTM_NEWACTION netlink message requesting the addition
220  * of a new action and sends the message to the kernel. The
221  * configuration of the action is derived from the attributes of
222  * the specified traffic class.
223  *
224  * The following flags may be specified:
225  * - \c NLM_F_CREATE: Create action if it does not exist,
226  * otherwise -NLE_OBJ_NOTFOUND is returned.
227  * - \c NLM_F_EXCL: Return -NLE_EXISTS if an action with
228  * matching handle exists already.
229  *
230  * Existing actions with matching handles will be updated, unless
231  * the flag \c NLM_F_EXCL is specified. If no matching action
232  * exists, it will be created if the flag \c NLM_F_CREATE is set,
233  * otherwise the error -NLE_OBJ_NOTFOUND is returned.
234  *
235  * If the parent qdisc does not support classes, the error
236  * \c NLE_OPNOTSUPP is returned.
237  *
238  * After sending, the function will wait for the ACK or an eventual
239  * error message to be received and will therefore block until the
240  * operation has been completed.
241  *
242  * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
243  * this function to return immediately after sending. In this case,
244  * it is the responsibility of the caller to handle any error
245  * messages returned.
246  *
247  * @return 0 on success or a negative error code.
248  */
249 int rtnl_act_add(struct nl_sock *sk, struct rtnl_act *act, int flags)
250 {
251  struct nl_msg *msg;
252  int err;
253 
254  if ((err = rtnl_act_build_add_request(act, flags, &msg)) < 0)
255  return err;
256 
257  return nl_send_sync(sk, msg);
258 }
259 
260 /**
261  * Build a netlink message to change action attributes
262  * @arg act Action to change
263  * @arg flags additional netlink message flags
264  * @arg result Pointer to store resulting message.
265  *
266  * Builds a new netlink message requesting a change of a neigh
267  * attributes. The netlink message header isn't fully equipped with
268  * all relevant fields and must thus be sent out via nl_send_auto_complete()
269  * or supplemented as needed.
270  *
271  * @return 0 on success or a negative error code.
272  */
273 int rtnl_act_build_change_request(struct rtnl_act *act, int flags,
274  struct nl_msg **result)
275 {
276  return act_build(act, RTM_NEWACTION, NLM_F_REPLACE | flags, result);
277 }
278 
279 /**
280  * Change an action
281  * @arg sk Netlink socket.
282  * @arg act action to change
283  * @arg flags additional netlink message flags
284  *
285  * Builds a netlink message by calling rtnl_act_build_change_request(),
286  * sends the request to the kernel and waits for the next ACK to be
287  * received and thus blocks until the request has been processed.
288  *
289  * @return 0 on sucess or a negative error if an error occured.
290  */
291 int rtnl_act_change(struct nl_sock *sk, struct rtnl_act *act, int flags)
292 {
293  struct nl_msg *msg;
294  int err;
295 
296  if ((err = rtnl_act_build_change_request(act, flags, &msg)) < 0)
297  return err;
298 
299  return nl_send_sync(sk, msg);
300 }
301 
302 /**
303  * Build netlink message requesting the deletion of an action
304  * @arg act Action to delete
305  * @arg flags Additional netlink message flags
306  * @arg result Pointer to store resulting netlink message
307  *
308  * The behaviour of this function is identical to rtnl_act_delete() with
309  * the exception that it will not send the message but return it in the
310  * provided return pointer instead.
311  *
312  * @see rtnl_act_delete()
313  *
314  * @return 0 on success or a negative error code.
315  */
316 int rtnl_act_build_delete_request(struct rtnl_act *act, int flags,
317  struct nl_msg **result)
318 {
319  return act_build(act, RTM_DELACTION, flags, result);
320 }
321 
322 /**
323  * Delete action
324  * @arg sk Netlink socket
325  * @arg act Action to delete
326  * @arg flags Additional netlink message flags
327  *
328  * Builds a \c RTM_DELACTION netlink message requesting the deletion
329  * of an action and sends the message to the kernel.
330  *
331  * The message is constructed out of the following attributes:
332  * - \c ifindex (required)
333  * - \c prio (required)
334  * - \c protocol (required)
335  * - \c handle (required)
336  * - \c parent (optional, if not specified parent equals root-qdisc)
337  * - \c kind (optional, must match if provided)
338  *
339  * All other action attributes including all class type specific
340  * attributes are ignored.
341  *
342  * After sending, the function will wait for the ACK or an eventual
343  * error message to be received and will therefore block until the
344  * operation has been completed.
345  *
346  * @note Disabling auto-ack (nl_socket_disable_auto_ack()) will cause
347  * this function to return immediately after sending. In this case,
348  * it is the responsibility of the caller to handle any error
349  * messages returned.
350  *
351  * @return 0 on success or a negative error code.
352  */
353 int rtnl_act_delete(struct nl_sock *sk, struct rtnl_act *act, int flags)
354 {
355  struct nl_msg *msg;
356  int err;
357 
358  if ((err = rtnl_act_build_delete_request(act, flags, &msg)) < 0)
359  return err;
360 
361  return nl_send_sync(sk, msg);
362 }
363 
364 /** @} */
365 
366 static void act_dump_line(struct rtnl_tc *tc, struct nl_dump_params *p)
367 {
368 }
369 
370 void rtnl_act_put_all(struct rtnl_act **head)
371 {
372  struct rtnl_act *curr, *next;
373 
374  curr = *head;
375  while (curr) {
376  next = curr->a_next;
377  rtnl_act_put(curr);
378  curr = next;
379  }
380  *head = NULL;
381 }
382 
383 int rtnl_act_parse(struct rtnl_act **head, struct nlattr *tb)
384 {
385  struct rtnl_tc_ops *ops;
386  struct nlattr *tb2[TCA_ACT_MAX + 1];
387  struct nlattr *nla[TCA_ACT_MAX_PRIO + 1];
388  char kind[TCKINDSIZ];
389  int err, i;
390 
391  err = nla_parse(nla, TCA_ACT_MAX_PRIO, nla_data(tb),
392  NLMSG_ALIGN(nla_len(tb)), NULL);
393  if (err < 0)
394  return err;
395 
396  for (i = 1; i <= TCA_ACT_MAX_PRIO && nla[i]; i++) {
397  struct rtnl_act *act;
398  struct rtnl_tc *tc;
399 
400  act = rtnl_act_alloc();
401  if (!act) {
402  err = -NLE_NOMEM;
403  goto err_free;
404  }
405  tc = TC_CAST(act);
406  err = nla_parse(tb2, TCA_ACT_MAX, nla_data(nla[i]),
407  nla_len(nla[i]), NULL);
408  if (err < 0)
409  goto err_free;
410 
411  if (tb2[TCA_ACT_KIND] == NULL) {
412  err = -NLE_MISSING_ATTR;
413  goto err_free;
414  }
415 
416  nla_strlcpy(kind, tb2[TCA_ACT_KIND], sizeof(kind));
417  rtnl_tc_set_kind(tc, kind);
418 
419  if (tb2[TCA_ACT_OPTIONS]) {
420  tc->tc_opts = nl_data_alloc_attr(tb2[TCA_ACT_OPTIONS]);
421  if (!tc->tc_opts) {
422  err = -NLE_NOMEM;
423  goto err_free;
424  }
425  tc->ce_mask |= TCA_ATTR_OPTS;
426  }
427 
428  ops = rtnl_tc_get_ops(tc);
429  if (ops && ops->to_msg_parser) {
430  void *data = rtnl_tc_data(tc);
431 
432  if (!data) {
433  err = -NLE_NOMEM;
434  goto err_free;
435  }
436 
437  err = ops->to_msg_parser(tc, data);
438  if (err < 0)
439  goto err_free;
440  }
441  err = rtnl_act_append(head, act);
442  if (err < 0)
443  goto err_free;
444  }
445  return 0;
446 
447 err_free:
448  rtnl_act_put_all(head);
449 
450  return err;
451 }
452 
453 static int rtnl_act_msg_parse(struct nlmsghdr *n, struct rtnl_act **act)
454 {
455  struct rtnl_tc *tc = TC_CAST(act);
456  struct nl_cache *link_cache;
457  struct nlattr *tb[TCAA_MAX + 1];
458  struct tcamsg *tm;
459  int err;
460 
461  tc->ce_msgtype = n->nlmsg_type;
462 
463  err = nlmsg_parse(n, sizeof(*tm), tb, TCAA_MAX, NULL);
464  if (err < 0)
465  return err;
466 
467  tm = nlmsg_data(n);
468  tc->tc_family = tm->tca_family;
469 
470  if (tb[TCA_ACT_TAB] == NULL)
471  return -NLE_MISSING_ATTR;
472 
473  err = rtnl_act_parse(act, tb[TCA_ACT_TAB]);
474  if (err < 0)
475  return err;
476 
477  if ((link_cache = __nl_cache_mngt_require("route/link"))) {
478  struct rtnl_link *link;
479 
480  if ((link = rtnl_link_get(link_cache, tc->tc_ifindex))) {
481  rtnl_tc_set_link(tc, link);
482 
483  /* rtnl_tc_set_link incs refcnt */
484  rtnl_link_put(link);
485  }
486  }
487 
488  return 0;
489 }
490 static int act_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
491  struct nlmsghdr *nlh, struct nl_parser_param *pp)
492 {
493  struct rtnl_act *act, *p_act;
494  int err;
495 
496  if (!(act = rtnl_act_alloc()))
497  return -NLE_NOMEM;
498 
499  if ((err = rtnl_act_msg_parse(nlh, &act)) < 0)
500  goto errout;
501 
502  p_act = act;
503  while(p_act) {
504  err = pp->pp_cb(OBJ_CAST(act), pp);
505  if (err)
506  break;
507  p_act = p_act->a_next;
508  }
509 errout:
510  rtnl_act_put(act);
511 
512  return err;
513 }
514 
515 static int act_request_update(struct nl_cache *cache, struct nl_sock *sk)
516 {
517  struct tcamsg tcahdr = {
518  .tca_family = AF_UNSPEC,
519  };
520 
521  return nl_send_simple(sk, RTM_GETACTION, NLM_F_DUMP, &tcahdr,
522  sizeof(tcahdr));
523 }
524 
525 static struct rtnl_tc_type_ops act_ops = {
526  .tt_type = RTNL_TC_TYPE_ACT,
527  .tt_dump_prefix = "act",
528  .tt_dump = {
529  [NL_DUMP_LINE] = act_dump_line,
530  },
531 };
532 
533 static struct nl_cache_ops rtnl_act_ops = {
534  .co_name = "route/act",
535  .co_hdrsize = sizeof(struct tcmsg),
536  .co_msgtypes = {
537  { RTM_NEWACTION, NL_ACT_NEW, "new" },
538  { RTM_DELACTION, NL_ACT_DEL, "del" },
539  { RTM_GETACTION, NL_ACT_GET, "get" },
540  END_OF_MSGTYPES_LIST,
541  },
542  .co_protocol = NETLINK_ROUTE,
543  .co_request_update = act_request_update,
544  .co_msg_parser = act_msg_parser,
545  .co_obj_ops = &act_obj_ops,
546 };
547 
548 static struct nl_object_ops act_obj_ops = {
549  .oo_name = "route/act",
550  .oo_size = sizeof(struct rtnl_act),
551  .oo_free_data = rtnl_tc_free_data,
552  .oo_clone = rtnl_tc_clone,
553  .oo_dump = {
554  [NL_DUMP_LINE] = rtnl_tc_dump_line,
555  [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
556  [NL_DUMP_STATS] = rtnl_tc_dump_stats,
557  },
558  .oo_compare = rtnl_tc_compare,
559  .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
560 };
561 
562 static void __init act_init(void)
563 {
564  rtnl_tc_type_register(&act_ops);
565  nl_cache_mngt_register(&rtnl_act_ops);
566 }
567 
568 static void __exit act_exit(void)
569 {
570  nl_cache_mngt_unregister(&rtnl_act_ops);
571  rtnl_tc_type_unregister(&act_ops);
572 }
573 
574 /** @} */