libnl  3.6.0
mirred.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
4  */
5 
6 /**
7  * @ingroup act
8  * @defgroup act_mirred Mirror and Redirect
9  *
10  * @{
11  */
12 
13 #include <netlink-private/netlink.h>
14 #include <netlink-private/tc.h>
15 #include <netlink/netlink.h>
16 #include <netlink/attr.h>
17 #include <netlink/utils.h>
18 #include <netlink-private/route/tc-api.h>
19 #include <netlink/route/act/mirred.h>
20 
21 static struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
22  [TCA_MIRRED_PARMS] = { .minlen = sizeof(struct tc_mirred) },
23 };
24 
25 static int mirred_msg_parser(struct rtnl_tc *tc, void *data)
26 {
27  struct rtnl_mirred *u = data;
28  struct nlattr *tb[TCA_MIRRED_MAX + 1];
29  int err;
30 
31  err = tca_parse(tb, TCA_MIRRED_MAX, tc, mirred_policy);
32  if (err < 0)
33  return err;
34 
35  if (!tb[TCA_MIRRED_PARMS])
36  return -NLE_MISSING_ATTR;
37 
38  nla_memcpy(&u->m_parm, tb[TCA_MIRRED_PARMS], sizeof(u->m_parm));
39  return 0;
40 }
41 
42 static void mirred_free_data(struct rtnl_tc *tc, void *data)
43 {
44 }
45 
46 static void mirred_dump_line(struct rtnl_tc *tc, void *data,
47  struct nl_dump_params *p)
48 {
49  struct rtnl_mirred *u = data;
50  if (!u)
51  return;
52 
53  nl_dump(p, " index %u", u->m_parm.ifindex);
54 
55  if (u->m_parm.eaction == TCA_EGRESS_MIRROR)
56  nl_dump(p, " egress mirror");
57  else if (u->m_parm.eaction == TCA_EGRESS_REDIR)
58  nl_dump(p, " egress redirect");
59 
60  switch(u->m_parm.action) {
61  case TC_ACT_UNSPEC:
62  nl_dump(p, " unspecified");
63  break;
64  case TC_ACT_PIPE:
65  nl_dump(p, " pipe");
66  break;
67  case TC_ACT_STOLEN:
68  nl_dump(p, " stolen");
69  break;
70  case TC_ACT_SHOT:
71  nl_dump(p, " shot");
72  break;
73  case TC_ACT_QUEUED:
74  nl_dump(p, " queued");
75  break;
76  case TC_ACT_REPEAT:
77  nl_dump(p, " repeat");
78  break;
79  }
80 }
81 
82 static void mirred_dump_details(struct rtnl_tc *tc, void *data,
83  struct nl_dump_params *p)
84 {
85 }
86 
87 static void mirred_dump_stats(struct rtnl_tc *tc, void *data,
88  struct nl_dump_params *p)
89 {
90  struct rtnl_mirred *u = data;
91 
92  if (!u)
93  return;
94  /* TODO */
95 }
96 
97 
98 static int mirred_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
99 {
100  struct rtnl_mirred *u = data;
101 
102  if (!u)
103  return 0;
104 
105  NLA_PUT(msg, TCA_MIRRED_PARMS, sizeof(u->m_parm), &u->m_parm);
106  return 0;
107 
108 nla_put_failure:
109  return -NLE_NOMEM;
110 }
111 
112 /**
113  * @name Attribute Modifications
114  * @{
115  */
116 
117 int rtnl_mirred_set_action(struct rtnl_act *act, int action)
118 {
119  struct rtnl_mirred *u;
120 
121  if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
122  return -NLE_NOMEM;
123 
124  if (action > TCA_INGRESS_MIRROR || action < TCA_EGRESS_REDIR)
125  return -NLE_INVAL;
126 
127  switch (action) {
128  case TCA_EGRESS_MIRROR:
129  case TCA_EGRESS_REDIR:
130  u->m_parm.eaction = action;
131  break;
132  case TCA_INGRESS_REDIR:
133  case TCA_INGRESS_MIRROR:
134  default:
135  return NLE_OPNOTSUPP;
136  }
137  return 0;
138 }
139 
140 int rtnl_mirred_get_action(struct rtnl_act *act)
141 {
142  struct rtnl_mirred *u;
143 
144  if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
145  return -NLE_NOMEM;
146  return u->m_parm.eaction;
147 }
148 
149 int rtnl_mirred_set_ifindex(struct rtnl_act *act, uint32_t ifindex)
150 {
151  struct rtnl_mirred *u;
152 
153  if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
154  return -NLE_NOMEM;
155 
156  u->m_parm.ifindex = ifindex;
157  return 0;
158 }
159 
160 uint32_t rtnl_mirred_get_ifindex(struct rtnl_act *act)
161 {
162  struct rtnl_mirred *u;
163 
164  if ((u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
165  return u->m_parm.ifindex;
166  return 0;
167 }
168 
169 int rtnl_mirred_set_policy(struct rtnl_act *act, int policy)
170 {
171  struct rtnl_mirred *u;
172 
173  if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
174  return -NLE_NOMEM;
175 
176  if (policy > TC_ACT_REPEAT || policy < TC_ACT_OK)
177  return -NLE_INVAL;
178 
179  switch (u->m_parm.eaction) {
180  case TCA_EGRESS_MIRROR:
181  case TCA_EGRESS_REDIR:
182  u->m_parm.action = policy;
183  break;
184  case TCA_INGRESS_REDIR:
185  case TCA_INGRESS_MIRROR:
186  default:
187  return NLE_OPNOTSUPP;
188  }
189  return 0;
190 }
191 
192 int rtnl_mirred_get_policy(struct rtnl_act *act)
193 {
194  struct rtnl_mirred *u;
195 
196  if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
197  return -NLE_NOMEM;
198  return u->m_parm.action;
199 }
200 
201 /** @} */
202 
203 static struct rtnl_tc_ops mirred_ops = {
204  .to_kind = "mirred",
205  .to_type = RTNL_TC_TYPE_ACT,
206  .to_size = sizeof(struct rtnl_mirred),
207  .to_msg_parser = mirred_msg_parser,
208  .to_free_data = mirred_free_data,
209  .to_clone = NULL,
210  .to_msg_fill = mirred_msg_fill,
211  .to_dump = {
212  [NL_DUMP_LINE] = mirred_dump_line,
213  [NL_DUMP_DETAILS] = mirred_dump_details,
214  [NL_DUMP_STATS] = mirred_dump_stats,
215  },
216 };
217 
218 static void __init mirred_init(void)
219 {
220  rtnl_tc_register(&mirred_ops);
221 }
222 
223 static void __exit mirred_exit(void)
224 {
225  rtnl_tc_unregister(&mirred_ops);
226 }
227 
228 /** @} */
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:159
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:346
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
Definition: tc.c:1079
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:50
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
Definition: tc.c:1018
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
Definition: tc.c:1052
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:955
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
Attribute validation policy.
Definition: attr.h:63
uint16_t minlen
Minimal length of payload required.
Definition: attr.h:68