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