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