1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2012 Thomas Graf <[email protected]>
4 */
5
6 /**
7 * @defgroup rtnl Routing Library (libnl-route)
8 * @{
9 */
10
11 #include "nl-default.h"
12
13 #include <netlink/netlink.h>
14 #include <netlink/utils.h>
15 #include <netlink/route/rtnl.h>
16
17 #include "nl-priv-dynamic-core/nl-core.h"
18
19 /**
20 * @name Sending
21 * @{
22 */
23
24 /**
25 * Send routing netlink request message
26 * @arg sk Netlink socket.
27 * @arg type Netlink message type.
28 * @arg family Address family.
29 * @arg flags Additional netlink message flags.
30 *
31 * Fills out a routing netlink request message and sends it out
32 * using nl_send_simple().
33 *
34 * @return 0 on success or a negative error code. Due to a bug in older
35 * version of the library, this function returned the number of bytes sent.
36 * Treat any non-negative number as success.
37 */
nl_rtgen_request(struct nl_sock * sk,int type,int family,int flags)38 int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
39 {
40 int err;
41 struct rtgenmsg gmsg = {
42 .rtgen_family = family,
43 };
44
45 err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));
46
47 return err >= 0 ? 0 : err;
48 }
49
50 /** @} */
51
52 /**
53 * @name Routing Type Translations
54 * @{
55 */
56
57 static const struct trans_tbl rtntypes[] = {
58 __ADD(RTN_UNSPEC,unspec),
59 __ADD(RTN_UNICAST,unicast),
60 __ADD(RTN_LOCAL,local),
61 __ADD(RTN_BROADCAST,broadcast),
62 __ADD(RTN_ANYCAST,anycast),
63 __ADD(RTN_MULTICAST,multicast),
64 __ADD(RTN_BLACKHOLE,blackhole),
65 __ADD(RTN_UNREACHABLE,unreachable),
66 __ADD(RTN_PROHIBIT,prohibit),
67 __ADD(RTN_THROW,throw),
68 __ADD(RTN_NAT,nat),
69 __ADD(RTN_XRESOLVE,xresolve),
70 };
71
nl_rtntype2str(int type,char * buf,size_t size)72 char *nl_rtntype2str(int type, char *buf, size_t size)
73 {
74 return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
75 }
76
nl_str2rtntype(const char * name)77 int nl_str2rtntype(const char *name)
78 {
79 return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
80 }
81
82 /** @} */
83
84 /**
85 * @name Scope Translations
86 * @{
87 */
88
89 static const struct trans_tbl scopes[] = {
90 __ADD(255,nowhere),
91 __ADD(254,host),
92 __ADD(253,link),
93 __ADD(200,site),
94 __ADD(0,global),
95 };
96
rtnl_scope2str(int scope,char * buf,size_t size)97 char *rtnl_scope2str(int scope, char *buf, size_t size)
98 {
99 return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
100 }
101
rtnl_str2scope(const char * name)102 int rtnl_str2scope(const char *name)
103 {
104 return __str2type(name, scopes, ARRAY_SIZE(scopes));
105 }
106
107 /** @} */
108
109 /**
110 * @name Realms Translations
111 * @{
112 */
113
rtnl_realms2str(uint32_t realms,char * buf,size_t len)114 char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
115 {
116 int from = RTNL_REALM_FROM(realms);
117 int to = RTNL_REALM_TO(realms);
118
119 snprintf(buf, len, "%d/%d", from, to);
120
121 return buf;
122 }
123
124 /** @} */
125
126 /** @} */
127