• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @defgroup rtnl Routing Library (libnl-route)
14  * @{
15  */
16 
17 #include <netlink-private/netlink.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. Due to a bug in
38  * older versions, this returned the number of bytes sent. So for
39  * compatibility, treat positive return values as success too.
40  */
nl_rtgen_request(struct nl_sock * sk,int type,int family,int flags)41 int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
42 {
43 	int err;
44 	struct rtgenmsg gmsg = {
45 		.rtgen_family = family,
46 	};
47 
48 	err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));
49 
50 	return err >= 0 ? 0 : err;
51 }
52 
53 /** @} */
54 
55 /**
56  * @name Routing Type Translations
57  * @{
58  */
59 
60 static const struct trans_tbl rtntypes[] = {
61 	__ADD(RTN_UNSPEC,unspec)
62 	__ADD(RTN_UNICAST,unicast)
63 	__ADD(RTN_LOCAL,local)
64 	__ADD(RTN_BROADCAST,broadcast)
65 	__ADD(RTN_ANYCAST,anycast)
66 	__ADD(RTN_MULTICAST,multicast)
67 	__ADD(RTN_BLACKHOLE,blackhole)
68 	__ADD(RTN_UNREACHABLE,unreachable)
69 	__ADD(RTN_PROHIBIT,prohibit)
70 	__ADD(RTN_THROW,throw)
71 	__ADD(RTN_NAT,nat)
72 	__ADD(RTN_XRESOLVE,xresolve)
73 };
74 
nl_rtntype2str(int type,char * buf,size_t size)75 char *nl_rtntype2str(int type, char *buf, size_t size)
76 {
77 	return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
78 }
79 
nl_str2rtntype(const char * name)80 int nl_str2rtntype(const char *name)
81 {
82 	return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
83 }
84 
85 /** @} */
86 
87 /**
88  * @name Scope Translations
89  * @{
90  */
91 
92 static const struct trans_tbl scopes[] = {
93 	__ADD(255,nowhere)
94 	__ADD(254,host)
95 	__ADD(253,link)
96 	__ADD(200,site)
97 	__ADD(0,global)
98 };
99 
rtnl_scope2str(int scope,char * buf,size_t size)100 char *rtnl_scope2str(int scope, char *buf, size_t size)
101 {
102 	return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
103 }
104 
rtnl_str2scope(const char * name)105 int rtnl_str2scope(const char *name)
106 {
107 	return __str2type(name, scopes, ARRAY_SIZE(scopes));
108 }
109 
110 /** @} */
111 
112 /**
113  * @name Realms Translations
114  * @{
115  */
116 
rtnl_realms2str(uint32_t realms,char * buf,size_t len)117 char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
118 {
119 	int from = RTNL_REALM_FROM(realms);
120 	int to = RTNL_REALM_TO(realms);
121 
122 	snprintf(buf, len, "%d/%d", from, to);
123 
124 	return buf;
125 }
126 
127 /** @} */
128 
129 /** @} */
130