• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * lib/route/route_utils.c	Routing Utilities
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-2006 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 /**
14  * @ingroup route
15  * @defgroup route_utils Utilities
16  * @brief Routing Utility Functions
17  *
18  *
19  * @par 1) Translating Routing Table Names
20  * @code
21  * // libnl is only aware of the de facto standard routing table names.
22  * // Additional name <-> identifier associations have to be read in via
23  * // a configuration file, f.e. /etc/iproute2/rt_tables
24  * err = rtnl_route_read_table_names("/etc/iproute2/rt_tables");
25  *
26  * // Translating a table name to its idenfier
27  * int table = rtnl_route_str2table("main");
28  *
29  * // ... and the other way around.
30  * char buf[32];
31  * printf("Name: %s\n",
32  *        rtnl_route_table2str(table, buf, sizeof(buf)));
33  * @endcode
34  *
35  *
36  *
37  *
38  * @{
39  */
40 
41 #include <netlink-private/netlink.h>
42 #include <netlink/netlink.h>
43 #include <netlink/utils.h>
44 #include <netlink/route/rtnl.h>
45 #include <netlink/route/route.h>
46 
47 /**
48  * @name Routing Table Identifier Translations
49  * @{
50  */
51 
52 static NL_LIST_HEAD(table_names);
53 
add_routing_table_name(long id,const char * name)54 static int add_routing_table_name(long id, const char *name)
55 {
56 	return __trans_list_add(id, name, &table_names);
57 }
58 
init_routing_table_names(void)59 static void __init init_routing_table_names(void)
60 {
61 	add_routing_table_name(RT_TABLE_UNSPEC, "unspec");
62 	add_routing_table_name(RT_TABLE_COMPAT, "compat");
63 	add_routing_table_name(RT_TABLE_DEFAULT, "default");
64 	add_routing_table_name(RT_TABLE_MAIN, "main");
65 	add_routing_table_name(RT_TABLE_LOCAL, "local");
66 }
67 
release_routing_table_names(void)68 static void __exit release_routing_table_names(void)
69 {
70 	__trans_list_clear(&table_names);
71 }
72 
rtnl_route_read_table_names(const char * path)73 int rtnl_route_read_table_names(const char *path)
74 {
75 	__trans_list_clear(&table_names);
76 
77 	return __nl_read_num_str_file(path, &add_routing_table_name);
78 }
79 
rtnl_route_table2str(int table,char * buf,size_t size)80 char *rtnl_route_table2str(int table, char *buf, size_t size)
81 {
82 	return __list_type2str(table, buf, size, &table_names);
83 }
84 
rtnl_route_str2table(const char * name)85 int rtnl_route_str2table(const char *name)
86 {
87 	return __list_str2type(name, &table_names);
88 }
89 
90 
91 /** @} */
92 
93 /**
94  * @name Routing Protocol Translations
95  * @{
96  */
97 
98 static NL_LIST_HEAD(proto_names);
99 
add_proto_name(long id,const char * name)100 static int add_proto_name(long id, const char *name)
101 {
102 	return __trans_list_add(id, name, &proto_names);
103 }
104 
init_proto_names(void)105 static void __init init_proto_names(void)
106 {
107 	add_proto_name(RTPROT_UNSPEC, "unspec");
108 	add_proto_name(RTPROT_REDIRECT, "redirect");
109 	add_proto_name(RTPROT_KERNEL, "kernel");
110 	add_proto_name(RTPROT_BOOT, "boot");
111 	add_proto_name(RTPROT_STATIC, "static");
112 }
113 
release_proto_names(void)114 static void __exit release_proto_names(void)
115 {
116 	__trans_list_clear(&proto_names);
117 }
118 
rtnl_route_read_protocol_names(const char * path)119 int rtnl_route_read_protocol_names(const char *path)
120 {
121 	__trans_list_clear(&proto_names);
122 
123 	return __nl_read_num_str_file(path, &add_proto_name);
124 }
125 
rtnl_route_proto2str(int proto,char * buf,size_t size)126 char *rtnl_route_proto2str(int proto, char *buf, size_t size)
127 {
128 	return __list_type2str(proto, buf, size, &proto_names);
129 }
130 
rtnl_route_str2proto(const char * name)131 int rtnl_route_str2proto(const char *name)
132 {
133 	return __list_str2type(name, &proto_names);
134 }
135 
136 /** @} */
137 
138 /**
139  * @name Routing Metrices Translations
140  * @{
141  */
142 
143 static const struct trans_tbl route_metrices[] = {
144 	__ADD(RTAX_UNSPEC, unspec),
145 	__ADD(RTAX_LOCK, lock),
146 	__ADD(RTAX_MTU, mtu),
147 	__ADD(RTAX_WINDOW, window),
148 	__ADD(RTAX_RTT, rtt),
149 	__ADD(RTAX_RTTVAR, rttvar),
150 	__ADD(RTAX_SSTHRESH, ssthresh),
151 	__ADD(RTAX_CWND, cwnd),
152 	__ADD(RTAX_ADVMSS, advmss),
153 	__ADD(RTAX_REORDERING, reordering),
154 	__ADD(RTAX_HOPLIMIT, hoplimit),
155 	__ADD(RTAX_INITCWND, initcwnd),
156 	__ADD(RTAX_FEATURES, features),
157 };
158 
rtnl_route_metric2str(int metric,char * buf,size_t size)159 char *rtnl_route_metric2str(int metric, char *buf, size_t size)
160 {
161 	return __type2str(metric, buf, size, route_metrices,
162 			  ARRAY_SIZE(route_metrices));
163 }
164 
rtnl_route_str2metric(const char * name)165 int rtnl_route_str2metric(const char *name)
166 {
167 	return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
168 }
169 
170 /** @} */
171 
172 /** @} */
173