libnl 3.5.0

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