act.c 13 KB

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