libnl: /data/src/libnl/lib/route/rule.c Source File

libnl 3.12.0

rule.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup rtnl
8 * @defgroup rule Routing Rules
9 * @brief
10 * @{
11 */
12
13#include "nl-default.h"
14
15#include <linux/fib_rules.h>
16
17#include <netlink/netlink.h>
18#include <netlink/utils.h>
19#include <netlink/route/rtnl.h>
20#include <netlink/route/rule.h>
21
22#include "nl-route.h"
23#include "nl-priv-dynamic-core/nl-core.h"
24#include "nl-priv-dynamic-core/cache-api.h"
25
26/** @cond SKIP */
27struct rtnl_rule {
28 NLHDR_COMMON
29 uint8_t r_family;
30 uint8_t r_action;
31 uint8_t r_dsfield; /* ipv4 only */
32 uint8_t r_l3mdev;
33 uint8_t r_protocol; /* protocol that installed rule */
34 uint8_t r_ip_proto; /* IP/IPv6 protocol */
35 uint32_t r_table;
36 uint32_t r_flags;
37 uint32_t r_prio;
38 uint32_t r_mark;
39 uint32_t r_mask;
40 uint32_t r_goto;
41 uint32_t r_flow; /* ipv4 only */
42 struct nl_addr *r_src;
43 struct nl_addr *r_dst;
44 char r_iifname[IFNAMSIZ];
45 char r_oifname[IFNAMSIZ];
46
47 struct fib_rule_port_range r_sport;
48 struct fib_rule_port_range r_dport;
49};
50
51#define RULE_ATTR_FAMILY 0x000001
52#define RULE_ATTR_TABLE 0x000002
53#define RULE_ATTR_ACTION 0x000004
54#define RULE_ATTR_FLAGS 0x000008
55#define RULE_ATTR_IIFNAME 0x000010
56#define RULE_ATTR_OIFNAME 0x000020
57#define RULE_ATTR_PRIO 0x000040
58#define RULE_ATTR_MARK 0x000080
59#define RULE_ATTR_MASK 0x000100
60#define RULE_ATTR_GOTO 0x000200
61#define RULE_ATTR_SRC 0x000400
62#define RULE_ATTR_DST 0x000800
63#define RULE_ATTR_DSFIELD 0x001000
64#define RULE_ATTR_FLOW 0x002000
65#define RULE_ATTR_L3MDEV 0x004000
66#define RULE_ATTR_PROTOCOL 0x008000
67#define RULE_ATTR_IP_PROTO 0x010000
68#define RULE_ATTR_SPORT 0x020000
69#define RULE_ATTR_DPORT 0x040000
70
71static struct nl_cache_ops rtnl_rule_ops;
72static struct nl_object_ops rule_obj_ops;
73/** @endcond */
74
75static void rule_free_data(struct nl_object *c)
76{
77 struct rtnl_rule *rule = nl_object_priv(c);
78
79 if (!rule)
80 return;
81
82 nl_addr_put(rule->r_src);
83 nl_addr_put(rule->r_dst);
84}
85
86static int rule_clone(struct nl_object *_dst, struct nl_object *_src)
87{
88 struct rtnl_rule *dst = nl_object_priv(_dst);
89 struct rtnl_rule *src = nl_object_priv(_src);
90
91 dst->r_src = NULL;
92 dst->r_dst = NULL;
93
94 if (src->r_src)
95 if (!(dst->r_src = nl_addr_clone(src->r_src)))
96 return -NLE_NOMEM;
97
98 if (src->r_dst)
99 if (!(dst->r_dst = nl_addr_clone(src->r_dst)))
100 return -NLE_NOMEM;
101
102 return 0;
103}
104
105static struct nla_policy rule_policy[FRA_MAX+1] = {
106 [FRA_TABLE] = { .type = NLA_U32 },
107 [FRA_IIFNAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ },
108 [FRA_OIFNAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ },
109 [FRA_PRIORITY] = { .type = NLA_U32 },
110 [FRA_FWMARK] = { .type = NLA_U32 },
111 [FRA_FWMASK] = { .type = NLA_U32 },
112 [FRA_GOTO] = { .type = NLA_U32 },
113 [FRA_FLOW] = { .type = NLA_U32 },
114 [FRA_L3MDEV] = { .type = NLA_U8 },
115 [FRA_PROTOCOL] = { .type = NLA_U8 },
116 [FRA_IP_PROTO] = { .type = NLA_U8 },
117 [FRA_SPORT_RANGE] = { .minlen = sizeof(struct fib_rule_port_range),
118 .maxlen = sizeof(struct fib_rule_port_range) },
119 [FRA_DPORT_RANGE] = { .minlen = sizeof(struct fib_rule_port_range),
120 .maxlen = sizeof(struct fib_rule_port_range) },
121};
122
123static int rule_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
124 struct nlmsghdr *n, struct nl_parser_param *pp)
125{
126 struct rtnl_rule *rule;
127 struct fib_rule_hdr *frh;
128 struct nlattr *tb[FRA_MAX+1];
129 int err = 1, family;
130
131 rule = rtnl_rule_alloc();
132 if (!rule) {
133 err = -NLE_NOMEM;
134 goto errout;
135 }
136
137 rule->ce_msgtype = n->nlmsg_type;
138 frh = nlmsg_data(n);
139
140 err = nlmsg_parse(n, sizeof(*frh), tb, FRA_MAX, rule_policy);
141 if (err < 0)
142 goto errout;
143
144 rule->r_family = family = frh->family;
145 rule->r_table = frh->table;
146 rule->r_action = frh->action;
147 rule->r_flags = frh->flags;
148
149 rule->ce_mask = (RULE_ATTR_FAMILY | RULE_ATTR_ACTION | RULE_ATTR_FLAGS);
150 if (rule->r_table)
151 rule->ce_mask |= RULE_ATTR_TABLE;
152
153 /* ipv4 only */
154 if (frh->tos) {
155 rule->r_dsfield = frh->tos;
156 rule->ce_mask |= RULE_ATTR_DSFIELD;
157 }
158
159 if (tb[FRA_TABLE]) {
160 rule->r_table = nla_get_u32(tb[FRA_TABLE]);
161 if (rule->r_table)
162 rule->ce_mask |= RULE_ATTR_TABLE;
163 }
164
165 if (tb[FRA_IIFNAME]) {
166 nla_strlcpy(rule->r_iifname, tb[FRA_IIFNAME], IFNAMSIZ);
167 rule->ce_mask |= RULE_ATTR_IIFNAME;
168 }
169
170 if (tb[FRA_OIFNAME]) {
171 nla_strlcpy(rule->r_oifname, tb[FRA_OIFNAME], IFNAMSIZ);
172 rule->ce_mask |= RULE_ATTR_OIFNAME;
173 }
174
175 if (tb[FRA_PRIORITY]) {
176 rule->r_prio = nla_get_u32(tb[FRA_PRIORITY]);
177 rule->ce_mask |= RULE_ATTR_PRIO;
178 }
179
180 if (tb[FRA_FWMARK]) {
181 rule->r_mark = nla_get_u32(tb[FRA_FWMARK]);
182 rule->ce_mask |= RULE_ATTR_MARK;
183 }
184
185 if (tb[FRA_FWMASK]) {
186 rule->r_mask = nla_get_u32(tb[FRA_FWMASK]);
187 rule->ce_mask |= RULE_ATTR_MASK;
188 }
189
190 if (tb[FRA_GOTO]) {
191 rule->r_goto = nla_get_u32(tb[FRA_GOTO]);
192 rule->ce_mask |= RULE_ATTR_GOTO;
193 }
194
195 if (tb[FRA_SRC]) {
196 if (!(rule->r_src = nl_addr_alloc_attr(tb[FRA_SRC], family)))
197 goto errout_enomem;
198
199 nl_addr_set_prefixlen(rule->r_src, frh->src_len);
200 rule->ce_mask |= RULE_ATTR_SRC;
201 }
202
203 if (tb[FRA_DST]) {
204 if (!(rule->r_dst = nl_addr_alloc_attr(tb[FRA_DST], family)))
205 goto errout_enomem;
206 nl_addr_set_prefixlen(rule->r_dst, frh->dst_len);
207 rule->ce_mask |= RULE_ATTR_DST;
208 }
209
210 /* ipv4 only */
211 if (tb[FRA_FLOW]) {
212 rule->r_flow = nla_get_u32(tb[FRA_FLOW]);
213 rule->ce_mask |= RULE_ATTR_FLOW;
214 }
215
216 if (tb[FRA_L3MDEV]) {
217 rule->r_l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
218 rule->ce_mask |= RULE_ATTR_L3MDEV;
219 }
220
221 if (tb[FRA_PROTOCOL]) {
222 rule->r_protocol = nla_get_u8(tb[FRA_PROTOCOL]);
223 rule->ce_mask |= RULE_ATTR_PROTOCOL;
224 }
225
226 if (tb[FRA_IP_PROTO]) {
227 rule->r_ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
228 rule->ce_mask |= RULE_ATTR_IP_PROTO;
229 }
230
231 if (tb[FRA_SPORT_RANGE]) {
232 struct fib_rule_port_range *pr;
233
234 pr = nla_data(tb[FRA_SPORT_RANGE]);
235 rule->r_sport = *pr;
236 rule->ce_mask |= RULE_ATTR_SPORT;
237 }
238
239 if (tb[FRA_DPORT_RANGE]) {
240 struct fib_rule_port_range *pr;
241
242 pr = nla_data(tb[FRA_DPORT_RANGE]);
243 rule->r_dport = *pr;
244 rule->ce_mask |= RULE_ATTR_DPORT;
245 }
246
247 err = pp->pp_cb((struct nl_object *) rule, pp);
248errout:
249 rtnl_rule_put(rule);
250 return err;
251
252errout_enomem:
253 err = -NLE_NOMEM;
254 goto errout;
255}
256
257static int rule_request_update(struct nl_cache *c, struct nl_sock *h)
258{
259 return nl_rtgen_request(h, RTM_GETRULE, AF_UNSPEC, NLM_F_DUMP);
260}
261
262static void rule_dump_line(struct nl_object *o, struct nl_dump_params *p)
263{
264 struct rtnl_rule *r = (struct rtnl_rule *) o;
265 char buf[128];
266
267 nl_dump_line(p, "%8d ", (r->ce_mask & RULE_ATTR_PRIO) ? r->r_prio : 0);
268 nl_dump(p, "%s ", nl_af2str(r->r_family, buf, sizeof(buf)));
269
270 if (r->ce_mask & RULE_ATTR_SRC)
271 nl_dump(p, "from %s ",
272 nl_addr2str(r->r_src, buf, sizeof(buf)));
273
274 if (r->ce_mask & RULE_ATTR_DST)
275 nl_dump(p, "to %s ",
276 nl_addr2str(r->r_dst, buf, sizeof(buf)));
277
278 if (r->ce_mask & RULE_ATTR_DSFIELD)
279 nl_dump(p, "tos %u ", r->r_dsfield);
280
281 if (r->ce_mask & (RULE_ATTR_MARK | RULE_ATTR_MASK))
282 nl_dump(p, "mark %#x/%#x", r->r_mark, r->r_mask);
283
284 if (r->ce_mask & RULE_ATTR_IIFNAME)
285 nl_dump(p, "iif %s ", r->r_iifname);
286
287 if (r->ce_mask & RULE_ATTR_OIFNAME)
288 nl_dump(p, "oif %s ", r->r_oifname);
289
290 if (r->ce_mask & RULE_ATTR_TABLE)
291 nl_dump(p, "lookup %s ",
292 rtnl_route_table2str(r->r_table, buf, sizeof(buf)));
293
294 if (r->ce_mask & RULE_ATTR_L3MDEV)
295 nl_dump(p, "lookup [l3mdev-table] ");
296
297 if (r->ce_mask & RULE_ATTR_IP_PROTO)
298 nl_dump(p, "ipproto %s ",
299 nl_ip_proto2str(r->r_ip_proto, buf, sizeof(buf)));
300
301 if (r->ce_mask & RULE_ATTR_SPORT) {
302 if (r->r_sport.start == r->r_sport.end)
303 nl_dump(p, "sport %u ", r->r_sport.start);
304 else
305 nl_dump(p, "sport %u-%u ",
306 r->r_sport.start, r->r_sport.end);
307 }
308
309 if (r->ce_mask & RULE_ATTR_DPORT) {
310 if (r->r_dport.start == r->r_dport.end)
311 nl_dump(p, "dport %u ", r->r_dport.start);
312 else
313 nl_dump(p, "dport %u-%u ",
314 r->r_dport.start, r->r_dport.end);
315 }
316
317 if (r->ce_mask & RULE_ATTR_PROTOCOL)
318 nl_dump(p, "protocol %s ",
319 rtnl_route_proto2str(r->r_protocol, buf, sizeof(buf)));
320
321 if (r->ce_mask & RULE_ATTR_FLOW)
322 nl_dump(p, "flow %s ",
323 rtnl_realms2str(r->r_flow, buf, sizeof(buf)));
324
325 if (r->ce_mask & RULE_ATTR_GOTO)
326 nl_dump(p, "goto %u ", r->r_goto);
327
328 if (r->ce_mask & RULE_ATTR_ACTION)
329 nl_dump(p, "action %s",
330 nl_rtntype2str(r->r_action, buf, sizeof(buf)));
331
332 nl_dump(p, "\n");
333}
334
335static void rule_dump_details(struct nl_object *obj, struct nl_dump_params *p)
336{
337 rule_dump_line(obj, p);
338}
339
340static void rule_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
341{
342 rule_dump_details(obj, p);
343}
344
345static uint64_t rule_compare(struct nl_object *_a, struct nl_object *_b,
346 uint64_t attrs, int flags)
347{
348 struct rtnl_rule *a = (struct rtnl_rule *) _a;
349 struct rtnl_rule *b = (struct rtnl_rule *) _b;
350 uint64_t diff = 0;
351
352#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
353 diff |= _DIFF(RULE_ATTR_FAMILY, a->r_family != b->r_family);
354 diff |= _DIFF(RULE_ATTR_TABLE, a->r_table != b->r_table);
355 diff |= _DIFF(RULE_ATTR_ACTION, a->r_action != b->r_action);
356 diff |= _DIFF(RULE_ATTR_IIFNAME, strcmp(a->r_iifname, b->r_iifname));
357 diff |= _DIFF(RULE_ATTR_OIFNAME, strcmp(a->r_oifname, b->r_oifname));
358 diff |= _DIFF(RULE_ATTR_PRIO, a->r_prio != b->r_prio);
359 diff |= _DIFF(RULE_ATTR_MARK, a->r_mark != b->r_mark);
360 diff |= _DIFF(RULE_ATTR_MASK, a->r_mask != b->r_mask);
361 diff |= _DIFF(RULE_ATTR_GOTO, a->r_goto != b->r_goto);
362 diff |= _DIFF(RULE_ATTR_SRC, nl_addr_cmp(a->r_src, b->r_src));
363 diff |= _DIFF(RULE_ATTR_DST, nl_addr_cmp(a->r_dst, b->r_dst));
364 diff |= _DIFF(RULE_ATTR_DSFIELD, a->r_dsfield != b->r_dsfield);
365 diff |= _DIFF(RULE_ATTR_FLOW, a->r_flow != b->r_flow);
366#undef _DIFF
367
368 return diff;
369}
370
371static const struct trans_tbl rule_attrs[] = {
372 __ADD(RULE_ATTR_FAMILY, family),
373 __ADD(RULE_ATTR_TABLE, table),
374 __ADD(RULE_ATTR_ACTION, action),
375 __ADD(RULE_ATTR_IIFNAME, iifname),
376 __ADD(RULE_ATTR_OIFNAME, oifname),
377 __ADD(RULE_ATTR_PRIO, prio),
378 __ADD(RULE_ATTR_MARK, mark),
379 __ADD(RULE_ATTR_MASK, mask),
380 __ADD(RULE_ATTR_GOTO, goto),
381 __ADD(RULE_ATTR_SRC, src),
382 __ADD(RULE_ATTR_DST, dst),
383 __ADD(RULE_ATTR_DSFIELD, dsfield),
384 __ADD(RULE_ATTR_FLOW, flow),
385};
386
387static char *rule_attrs2str(int attrs, char *buf, size_t len)
388{
389 return __flags2str(attrs, buf, len, rule_attrs,
390 ARRAY_SIZE(rule_attrs));
391}
392
393/**
394 * @name Allocation/Freeing
395 * @{
396 */
397
398struct rtnl_rule *rtnl_rule_alloc(void)
399{
400 return (struct rtnl_rule *) nl_object_alloc(&rule_obj_ops);
401}
402
403void rtnl_rule_put(struct rtnl_rule *rule)
404{
405 nl_object_put((struct nl_object *) rule);
406}
407
408/** @} */
409
410/**
411 * @name Cache Management
412 * @{
413 */
414
415/**
416 * Build a rule cache including all rules currently configured in the kernel.
417 * @arg sock Netlink socket.
418 * @arg family Address family or AF_UNSPEC.
419 * @arg result Pointer to store resulting cache.
420 *
421 * Allocates a new rule cache, initializes it properly and updates it
422 * to include all rules currently configured in the kernel.
423 *
424 * @return 0 on success or a negative error code.
425 */
426 int rtnl_rule_alloc_cache(struct nl_sock *sock, int family,
427 struct nl_cache **result)
428{
429 struct nl_cache * cache;
430 int err;
431
432 if (!(cache = nl_cache_alloc(&rtnl_rule_ops)))
433 return -NLE_NOMEM;
434
435 cache->c_iarg1 = family;
436
437 if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
438 free(cache);
439 return err;
440 }
441
442 *result = cache;
443 return 0;
444}
445
446/** @} */
447
448/**
449 * @name Rule Addition
450 * @{
451 */
452
453static int build_rule_msg(struct rtnl_rule *tmpl, int cmd, int flags,
454 struct nl_msg **result)
455{
456 struct nl_msg *msg;
457 struct fib_rule_hdr frh = {
458 .family = tmpl->r_family,
459 .table = tmpl->r_table,
460 .action = tmpl->r_action,
461 .flags = tmpl->r_flags,
462 .tos = tmpl->r_dsfield,
463 };
464
465 if (!(tmpl->ce_mask & RULE_ATTR_FAMILY))
466 return -NLE_MISSING_ATTR;
467
468 msg = nlmsg_alloc_simple(cmd, flags);
469 if (!msg)
470 return -NLE_NOMEM;
471
472 if (tmpl->ce_mask & RULE_ATTR_SRC)
473 frh.src_len = nl_addr_get_prefixlen(tmpl->r_src);
474
475 if (tmpl->ce_mask & RULE_ATTR_DST)
476 frh.dst_len = nl_addr_get_prefixlen(tmpl->r_dst);
477
478 if (nlmsg_append(msg, &frh, sizeof(frh), NLMSG_ALIGNTO) < 0)
479 goto nla_put_failure;
480
481 /* Additional table attribute replacing the 8bit in the header, was
482 * required to allow more than 256 tables. */
483 NLA_PUT_U32(msg, FRA_TABLE, tmpl->r_table);
484
485 if (tmpl->ce_mask & RULE_ATTR_SRC)
486 NLA_PUT_ADDR(msg, FRA_SRC, tmpl->r_src);
487
488 if (tmpl->ce_mask & RULE_ATTR_DST)
489 NLA_PUT_ADDR(msg, FRA_DST, tmpl->r_dst);
490
491 if (tmpl->ce_mask & RULE_ATTR_IIFNAME)
492 NLA_PUT_STRING(msg, FRA_IIFNAME, tmpl->r_iifname);
493
494 if (tmpl->ce_mask & RULE_ATTR_OIFNAME)
495 NLA_PUT_STRING(msg, FRA_OIFNAME, tmpl->r_oifname);
496
497 if (tmpl->ce_mask & RULE_ATTR_PRIO)
498 NLA_PUT_U32(msg, FRA_PRIORITY, tmpl->r_prio);
499
500 if (tmpl->ce_mask & RULE_ATTR_MARK)
501 NLA_PUT_U32(msg, FRA_FWMARK, tmpl->r_mark);
502
503 if (tmpl->ce_mask & RULE_ATTR_MASK)
504 NLA_PUT_U32(msg, FRA_FWMASK, tmpl->r_mask);
505
506 if (tmpl->ce_mask & RULE_ATTR_GOTO)
507 NLA_PUT_U32(msg, FRA_GOTO, tmpl->r_goto);
508
509 if (tmpl->ce_mask & RULE_ATTR_FLOW)
510 NLA_PUT_U32(msg, FRA_FLOW, tmpl->r_flow);
511
512 if (tmpl->ce_mask & RULE_ATTR_L3MDEV)
513 NLA_PUT_U8(msg, FRA_L3MDEV, tmpl->r_l3mdev);
514
515 if (tmpl->ce_mask & RULE_ATTR_IP_PROTO)
516 NLA_PUT_U8(msg, FRA_IP_PROTO, tmpl->r_ip_proto);
517
518 if (tmpl->ce_mask & RULE_ATTR_SPORT)
519 NLA_PUT(msg, FRA_SPORT_RANGE, sizeof(tmpl->r_sport),
520 &tmpl->r_sport);
521
522 if (tmpl->ce_mask & RULE_ATTR_DPORT)
523 NLA_PUT(msg, FRA_DPORT_RANGE, sizeof(tmpl->r_dport),
524 &tmpl->r_dport);
525
526 if (tmpl->ce_mask & RULE_ATTR_PROTOCOL)
527 NLA_PUT_U8(msg, FRA_PROTOCOL, tmpl->r_protocol);
528
529 *result = msg;
530 return 0;
531
532nla_put_failure:
533 nlmsg_free(msg);
534 return -NLE_MSGSIZE;
535}
536
537/**
538 * Build netlink request message to add a new rule
539 * @arg tmpl template with data of new rule
540 * @arg flags additional netlink message flags
541 * @arg result Result pointer
542 *
543 * Builds a new netlink message requesting a addition of a new
544 * rule. The netlink message header isn't fully equipped with
545 * all relevant fields and must thus be sent out via nl_send_auto_complete()
546 * or supplemented as needed. \a tmpl must contain the attributes of the new
547 * address set via \c rtnl_rule_set_* functions.
548 *
549 * @return 0 on success or a negative error code.
550 */
551 int rtnl_rule_build_add_request(struct rtnl_rule *tmpl, int flags,
552 struct nl_msg **result)
553{
554 return build_rule_msg(tmpl, RTM_NEWRULE, NLM_F_CREATE | flags,
555 result);
556}
557
558/**
559 * Add a new rule
560 * @arg sk Netlink socket.
561 * @arg tmpl template with requested changes
562 * @arg flags additional netlink message flags
563 *
564 * Builds a netlink message by calling rtnl_rule_build_add_request(),
565 * sends the request to the kernel and waits for the next ACK to be
566 * received and thus blocks until the request has been fullfilled.
567 *
568 * @return 0 on success or a negative error if an error occured.
569 */
570 int rtnl_rule_add(struct nl_sock *sk, struct rtnl_rule *tmpl, int flags)
571{
572 struct nl_msg *msg;
573 int err;
574
575 if ((err = rtnl_rule_build_add_request(tmpl, flags, &msg)) < 0)
576 return err;
577
578 err = nl_send_auto_complete(sk, msg);
579 nlmsg_free(msg);
580 if (err < 0)
581 return err;
582
583 return wait_for_ack(sk);
584}
585
586/** @} */
587
588/**
589 * @name Rule Deletion
590 * @{
591 */
592
593/**
594 * Build a netlink request message to delete a rule
595 * @arg rule rule to delete
596 * @arg flags additional netlink message flags
597 * @arg result Result pointer
598 *
599 * Builds a new netlink message requesting a deletion of a rule.
600 * The netlink message header isn't fully equipped with all relevant
601 * fields and must thus be sent out via nl_send_auto_complete()
602 * or supplemented as needed. \a rule must point to an existing
603 * address.
604 *
605 * @return 0 on success or a negative error code.
606 */
607 int rtnl_rule_build_delete_request(struct rtnl_rule *rule, int flags,
608 struct nl_msg **result)
609{
610 return build_rule_msg(rule, RTM_DELRULE, flags, result);
611}
612
613/**
614 * Delete a rule
615 * @arg sk Netlink socket.
616 * @arg rule rule to delete
617 * @arg flags additional netlink message flags
618 *
619 * Builds a netlink message by calling rtnl_rule_build_delete_request(),
620 * sends the request to the kernel and waits for the next ACK to be
621 * received and thus blocks until the request has been fullfilled.
622 *
623 * @return 0 on success or a negative error if an error occured.
624 */
625 int rtnl_rule_delete(struct nl_sock *sk, struct rtnl_rule *rule, int flags)
626{
627 struct nl_msg *msg;
628 int err;
629
630 if ((err = rtnl_rule_build_delete_request(rule, flags, &msg)) < 0)
631 return err;
632
633 err = nl_send_auto_complete(sk, msg);
634 nlmsg_free(msg);
635 if (err < 0)
636 return err;
637
638 return wait_for_ack(sk);
639}
640
641/** @} */
642
643/**
644 * @name Attribute Modification
645 * @{
646 */
647
648void rtnl_rule_set_family(struct rtnl_rule *rule, int family)
649{
650 rule->r_family = family;
651 rule->ce_mask |= RULE_ATTR_FAMILY;
652}
653
654int rtnl_rule_get_family(struct rtnl_rule *rule)
655{
656 if (rule->ce_mask & RULE_ATTR_FAMILY)
657 return rule->r_family;
658 else
659 return AF_UNSPEC;
660}
661
662void rtnl_rule_set_prio(struct rtnl_rule *rule, uint32_t prio)
663{
664 rule->r_prio = prio;
665 rule->ce_mask |= RULE_ATTR_PRIO;
666}
667
668uint32_t rtnl_rule_get_prio(struct rtnl_rule *rule)
669{
670 return rule->r_prio;
671}
672
673void rtnl_rule_set_mark(struct rtnl_rule *rule, uint32_t mark)
674{
675 rule->r_mark = mark;
676 rule->ce_mask |= RULE_ATTR_MARK;
677}
678
679uint32_t rtnl_rule_get_mark(struct rtnl_rule *rule)
680{
681 return rule->r_mark;
682}
683
684void rtnl_rule_set_mask(struct rtnl_rule *rule, uint32_t mask)
685{
686 rule->r_mask = mask;
687 rule->ce_mask |= RULE_ATTR_MASK;
688}
689
690uint32_t rtnl_rule_get_mask(struct rtnl_rule *rule)
691{
692 return rule->r_mask;
693}
694
695void rtnl_rule_set_table(struct rtnl_rule *rule, uint32_t table)
696{
697 rule->r_table = table;
698 rule->ce_mask |= RULE_ATTR_TABLE;
699}
700
701uint32_t rtnl_rule_get_table(struct rtnl_rule *rule)
702{
703 return rule->r_table;
704}
705
706void rtnl_rule_set_dsfield(struct rtnl_rule *rule, uint8_t dsfield)
707{
708 rule->r_dsfield = dsfield;
709 rule->ce_mask |= RULE_ATTR_DSFIELD;
710}
711
712uint8_t rtnl_rule_get_dsfield(struct rtnl_rule *rule)
713{
714 return rule->r_dsfield;
715}
716
717static inline int __assign_addr(struct rtnl_rule *rule, struct nl_addr **pos,
718 struct nl_addr *new, int flag)
719{
720 if (rule->ce_mask & RULE_ATTR_FAMILY) {
721 if (new->a_family != rule->r_family)
722 return -NLE_AF_MISMATCH;
723 } else
724 rule->r_family = new->a_family;
725
726 if (*pos)
727 nl_addr_put(*pos);
728
729 nl_addr_get(new);
730 *pos = new;
731
732 rule->ce_mask |= (flag | RULE_ATTR_FAMILY);
733
734 return 0;
735}
736
737int rtnl_rule_set_src(struct rtnl_rule *rule, struct nl_addr *src)
738{
739 return __assign_addr(rule, &rule->r_src, src, RULE_ATTR_SRC);
740}
741
742struct nl_addr *rtnl_rule_get_src(struct rtnl_rule *rule)
743{
744 return rule->r_src;
745}
746
747int rtnl_rule_set_dst(struct rtnl_rule *rule, struct nl_addr *dst)
748{
749 return __assign_addr(rule, &rule->r_dst, dst, RULE_ATTR_DST);
750}
751
752struct nl_addr *rtnl_rule_get_dst(struct rtnl_rule *rule)
753{
754 return rule->r_dst;
755}
756
757int rtnl_rule_set_iif(struct rtnl_rule *rule, const char *dev)
758{
759 if (strlen(dev) > IFNAMSIZ-1)
760 return -NLE_RANGE;
761
762 strcpy(rule->r_iifname, dev);
763 rule->ce_mask |= RULE_ATTR_IIFNAME;
764 return 0;
765}
766
767char *rtnl_rule_get_iif(struct rtnl_rule *rule)
768{
769 if (rule->ce_mask & RULE_ATTR_IIFNAME)
770 return rule->r_iifname;
771 else
772 return NULL;
773}
774
775int rtnl_rule_set_oif(struct rtnl_rule *rule, const char *dev)
776{
777 if (strlen(dev) > IFNAMSIZ-1)
778 return -NLE_RANGE;
779
780 strcpy(rule->r_oifname, dev);
781 rule->ce_mask |= RULE_ATTR_OIFNAME;
782 return 0;
783}
784
785char *rtnl_rule_get_oif(struct rtnl_rule *rule)
786{
787 if (rule->ce_mask & RULE_ATTR_OIFNAME)
788 return rule->r_oifname;
789 else
790 return NULL;
791}
792
793void rtnl_rule_set_action(struct rtnl_rule *rule, uint8_t action)
794{
795 rule->r_action = action;
796 rule->ce_mask |= RULE_ATTR_ACTION;
797}
798
799uint8_t rtnl_rule_get_action(struct rtnl_rule *rule)
800{
801 return rule->r_action;
802}
803
804/**
805 * Set l3mdev value of the rule (FRA_L3MDEV)
806 * @arg rule rule
807 * @arg value value to set
808 *
809 * Set the l3mdev value to value. Currently supported values
810 * are only 1 (set it) and -1 (unset it). All other values
811 * are reserved.
812 */
813 void rtnl_rule_set_l3mdev(struct rtnl_rule *rule, int value)
814{
815 if (value >= 0) {
816 rule->r_l3mdev = (uint8_t) value;
817 rule->ce_mask |= RULE_ATTR_L3MDEV;
818 } else {
819 rule->r_l3mdev = 0;
820 rule->ce_mask &= ~((uint32_t) RULE_ATTR_L3MDEV);
821 }
822}
823
824/**
825 * Get l3mdev value of the rule (FRA_L3MDEV)
826 * @arg rule rule
827 *
828 * @return a negative error code, including -NLE_MISSING_ATTR
829 * if the property is unset. Otherwise returns a non-negative
830 * value. As FRA_L3MDEV is a boolean, the only expected
831 * value at the moment is 1.
832 */
833 int rtnl_rule_get_l3mdev(struct rtnl_rule *rule)
834{
835 if (!rule)
836 return -NLE_INVAL;
837 if (!(rule->ce_mask & RULE_ATTR_L3MDEV))
838 return -NLE_MISSING_ATTR;
839 return rule->r_l3mdev;
840}
841
842int rtnl_rule_set_protocol(struct rtnl_rule *rule, uint8_t protocol)
843{
844 if (protocol) {
845 rule->r_protocol = protocol;
846 rule->ce_mask |= RULE_ATTR_PROTOCOL;
847 } else {
848 rule->r_protocol = 0;
849 rule->ce_mask &= ~((uint32_t) RULE_ATTR_PROTOCOL);
850 }
851 return 0;
852}
853
854int rtnl_rule_get_protocol(struct rtnl_rule *rule, uint8_t *protocol)
855{
856 if (!(rule->ce_mask & RULE_ATTR_PROTOCOL))
857 return -NLE_INVAL;
858
859 *protocol = rule->r_protocol;
860 return 0;
861}
862
863int rtnl_rule_set_ipproto(struct rtnl_rule *rule, uint8_t ip_proto)
864{
865 if (ip_proto) {
866 rule->r_ip_proto = ip_proto;
867 rule->ce_mask |= RULE_ATTR_IP_PROTO;
868 } else {
869 rule->r_ip_proto = 0;
870 rule->ce_mask &= ~((uint32_t) RULE_ATTR_IP_PROTO);
871 }
872 return 0;
873}
874
875int rtnl_rule_get_ipproto(struct rtnl_rule *rule, uint8_t *ip_proto)
876{
877 if (!(rule->ce_mask & RULE_ATTR_IP_PROTO))
878 return -NLE_INVAL;
879
880 *ip_proto = rule->r_ip_proto;
881 return 0;
882}
883
884static int __rtnl_rule_set_port(struct fib_rule_port_range *prange,
885 uint16_t start, uint16_t end,
886 uint64_t attr, uint64_t *mask)
887{
888 if ((start && end < start) || (end && !start))
889 return -NLE_INVAL;
890
891 if (start) {
892 prange->start = start;
893 prange->end = end;
894 *mask |= attr;
895 } else {
896 prange->start = 0;
897 prange->end = 0;
898 *mask &= ~attr;
899
900 }
901 return 0;
902}
903
904int rtnl_rule_set_sport(struct rtnl_rule *rule, uint16_t sport)
905{
906 return __rtnl_rule_set_port(&rule->r_sport, sport, sport,
907 RULE_ATTR_SPORT, &rule->ce_mask);
908}
909
910int rtnl_rule_set_sport_range(struct rtnl_rule *rule, uint16_t start,
911 uint16_t end)
912{
913 return __rtnl_rule_set_port(&rule->r_sport, start, end,
914 RULE_ATTR_SPORT, &rule->ce_mask);
915}
916
917int rtnl_rule_get_sport(struct rtnl_rule *rule, uint16_t *start, uint16_t *end)
918{
919 if (!(rule->ce_mask & RULE_ATTR_SPORT))
920 return -NLE_INVAL;
921
922 *start = rule->r_sport.start;
923 *end = rule->r_sport.end;
924 return 0;
925}
926
927int rtnl_rule_set_dport(struct rtnl_rule *rule, uint16_t dport)
928{
929 return __rtnl_rule_set_port(&rule->r_dport, dport, dport,
930 RULE_ATTR_DPORT, &rule->ce_mask);
931}
932
933int rtnl_rule_set_dport_range(struct rtnl_rule *rule, uint16_t start,
934 uint16_t end)
935{
936 return __rtnl_rule_set_port(&rule->r_dport, start, end,
937 RULE_ATTR_DPORT, &rule->ce_mask);
938}
939
940int rtnl_rule_get_dport(struct rtnl_rule *rule, uint16_t *start, uint16_t *end)
941{
942 if (!(rule->ce_mask & RULE_ATTR_DPORT))
943 return -NLE_INVAL;
944
945 *start = rule->r_dport.start;
946 *end = rule->r_dport.end;
947 return 0;
948}
949
950void rtnl_rule_set_realms(struct rtnl_rule *rule, uint32_t realms)
951{
952 rule->r_flow = realms;
953 rule->ce_mask |= RULE_ATTR_FLOW;
954}
955
956uint32_t rtnl_rule_get_realms(struct rtnl_rule *rule)
957{
958 return rule->r_flow;
959}
960
961void rtnl_rule_set_goto(struct rtnl_rule *rule, uint32_t ref)
962{
963 rule->r_goto = ref;
964 rule->ce_mask |= RULE_ATTR_GOTO;
965}
966
967uint32_t rtnl_rule_get_goto(struct rtnl_rule *rule)
968{
969 return rule->r_goto;
970}
971
972/** @} */
973
974static struct nl_object_ops rule_obj_ops = {
975 .oo_name = "route/rule",
976 .oo_size = sizeof(struct rtnl_rule),
977 .oo_free_data = rule_free_data,
978 .oo_clone = rule_clone,
979 .oo_dump = {
980 [NL_DUMP_LINE] = rule_dump_line,
981 [NL_DUMP_DETAILS] = rule_dump_details,
982 [NL_DUMP_STATS] = rule_dump_stats,
983 },
984 .oo_compare = rule_compare,
985 .oo_attrs2str = rule_attrs2str,
986 .oo_id_attrs = ~0,
987};
988
989static struct nl_af_group rule_groups[] = {
990 { AF_INET, RTNLGRP_IPV4_RULE },
991 { AF_INET6, RTNLGRP_IPV6_RULE },
992 { END_OF_GROUP_LIST },
993};
994
995static struct nl_cache_ops rtnl_rule_ops = {
996 .co_name = "route/rule",
997 .co_hdrsize = sizeof(struct fib_rule_hdr),
998 .co_msgtypes = {
999 { RTM_NEWRULE, NL_ACT_NEW, "new" },
1000 { RTM_DELRULE, NL_ACT_DEL, "del" },
1001 { RTM_GETRULE, NL_ACT_GET, "get" },
1002 END_OF_MSGTYPES_LIST,
1003 },
1004 .co_protocol = NETLINK_ROUTE,
1005 .co_request_update = rule_request_update,
1006 .co_msg_parser = rule_msg_parser,
1007 .co_obj_ops = &rule_obj_ops,
1008 .co_groups = rule_groups,
1009};
1010
1011static void _nl_init rule_init(void)
1012{
1013 nl_cache_mngt_register(&rtnl_rule_ops);
1014}
1015
1016static void _nl_exit rule_exit(void)
1017{
1018 nl_cache_mngt_unregister(&rtnl_rule_ops);
1019}
1020
1021/** @} */
nl_addr_set_prefixlen
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
Definition addr.c:967
nl_addr_get
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition addr.c:525
nl_addr_alloc_attr
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
Definition addr.c:261
nl_addr_cmp
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
Definition addr.c:587
nl_addr_clone
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition addr.c:495
nl_addr2str
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition addr.c:1001
nl_addr_get_prefixlen
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
Definition addr.c:978
nl_addr_put
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition addr.c:541
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition attr.c:714
NLA_PUT_U8
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Definition attr.h:201
NLA_PUT_ADDR
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
Definition attr.h:290
nla_data
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition attr.c:119
NLA_PUT
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition attr.h:166
NLA_PUT_U32
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition attr.h:237
nla_get_u8
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
Definition attr.c:614
nla_strlcpy
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition attr.c:383
NLA_PUT_STRING
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
Definition attr.h:264
NLA_STRING
@ NLA_STRING
NUL terminated character string.
Definition attr.h:39
NLA_U8
@ NLA_U8
8 bit integer
Definition attr.h:35
NLA_U32
@ NLA_U32
32 bit integer
Definition attr.h:37
nl_cache_mngt_unregister
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition cache_mngt.c:287
nl_cache_mngt_register
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition cache_mngt.c:252
nl_cache_refill
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition cache.c:1043
nl_cache_alloc
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
Definition cache.c:185
nlmsg_alloc_simple
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:352
nlmsg_data
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition msg.c:108
nlmsg_free
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:572
nlmsg_parse
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
Definition msg.c:219
nlmsg_append
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:456
nl_object_put
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
nl_object_alloc
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
nl_rtgen_request
int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
Send routing netlink request message.
Definition rtnl.c:38
rtnl_rule_delete
int rtnl_rule_delete(struct nl_sock *sk, struct rtnl_rule *rule, int flags)
Delete a rule.
Definition rule.c:625
rtnl_rule_build_delete_request
int rtnl_rule_build_delete_request(struct rtnl_rule *rule, int flags, struct nl_msg **result)
Build a netlink request message to delete a rule.
Definition rule.c:607
rtnl_rule_alloc_cache
int rtnl_rule_alloc_cache(struct nl_sock *sock, int family, struct nl_cache **result)
Build a rule cache including all rules currently configured in the kernel.
Definition rule.c:426
rtnl_rule_get_l3mdev
int rtnl_rule_get_l3mdev(struct rtnl_rule *rule)
Get l3mdev value of the rule (FRA_L3MDEV)
Definition rule.c:833
rtnl_rule_set_l3mdev
void rtnl_rule_set_l3mdev(struct rtnl_rule *rule, int value)
Set l3mdev value of the rule (FRA_L3MDEV)
Definition rule.c:813
rtnl_rule_build_add_request
int rtnl_rule_build_add_request(struct rtnl_rule *tmpl, int flags, struct nl_msg **result)
Build netlink request message to add a new rule.
Definition rule.c:551
rtnl_rule_add
int rtnl_rule_add(struct nl_sock *sk, struct rtnl_rule *tmpl, int flags)
Add a new rule.
Definition rule.c:570
nl_send_auto_complete
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1250
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_STATS
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
nla_policy
Attribute validation policy.
Definition attr.h:66