libmnl  1.0.3
rtnl-route-dump.c
1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <arpa/inet.h>
7 
8 #include <libmnl/libmnl.h>
9 #include <linux/if.h>
10 #include <linux/if_link.h>
11 #include <linux/rtnetlink.h>
12 
13 static int data_attr_cb2(const struct nlattr *attr, void *data)
14 {
15  /* skip unsupported attribute in user-space */
16  if (mnl_attr_type_valid(attr, RTAX_MAX) < 0)
17  return MNL_CB_OK;
18 
19  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
20  perror("mnl_attr_validate");
21  return MNL_CB_ERROR;
22  }
23  return MNL_CB_OK;
24 }
25 
26 static void attributes_show_ipv4(struct nlattr *tb[])
27 {
28  if (tb[RTA_TABLE]) {
29  printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
30  }
31  if (tb[RTA_DST]) {
32  struct in_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
33  printf("dst=%s ", inet_ntoa(*addr));
34  }
35  if (tb[RTA_SRC]) {
36  struct in_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
37  printf("src=%s ", inet_ntoa(*addr));
38  }
39  if (tb[RTA_OIF]) {
40  printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF]));
41  }
42  if (tb[RTA_FLOW]) {
43  printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW]));
44  }
45  if (tb[RTA_PREFSRC]) {
46  struct in_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
47  printf("prefsrc=%s ", inet_ntoa(*addr));
48  }
49  if (tb[RTA_GATEWAY]) {
50  struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
51  printf("gw=%s ", inet_ntoa(*addr));
52  }
53  if (tb[RTA_METRICS]) {
54  int i;
55  struct nlattr *tbx[RTAX_MAX+1] = {};
56 
57  mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
58 
59  for (i=0; i<RTAX_MAX; i++) {
60  if (tbx[i]) {
61  printf("metrics[%d]=%u ",
62  i, mnl_attr_get_u32(tbx[i]));
63  }
64  }
65  }
66  printf("\n");
67 }
68 
69 static int data_attr_cb(const struct nlattr *attr, void *data)
70 {
71  const struct nlattr **tb = data;
72  int type = mnl_attr_get_type(attr);
73 
74  /* skip unsupported attribute in user-space */
75  if (mnl_attr_type_valid(attr, RTA_MAX) < 0)
76  return MNL_CB_OK;
77 
78  switch(type) {
79  case RTA_TABLE:
80  case RTA_DST:
81  case RTA_SRC:
82  case RTA_OIF:
83  case RTA_FLOW:
84  case RTA_PREFSRC:
85  case RTA_GATEWAY:
86  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
87  perror("mnl_attr_validate");
88  return MNL_CB_ERROR;
89  }
90  break;
91  case RTA_METRICS:
92  if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
93  perror("mnl_attr_validate");
94  return MNL_CB_ERROR;
95  }
96  break;
97  }
98  tb[type] = attr;
99  return MNL_CB_OK;
100 }
101 
102 static int data_cb(const struct nlmsghdr *nlh, void *data)
103 {
104  struct nlattr *tb[RTA_MAX+1] = {};
105  struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
106 
107  /* protocol family = AF_INET | AF_INET6 */
108  printf("family=%u ", rm->rtm_family);
109 
110  /* destination CIDR, eg. 24 or 32 for IPv4 */
111  printf("dst_len=%u ", rm->rtm_dst_len);
112 
113  /* source CIDR */
114  printf("src_len=%u ", rm->rtm_src_len);
115 
116  /* type of service (TOS), eg. 0 */
117  printf("tos=%u ", rm->rtm_tos);
118 
119  /* table id:
120  * RT_TABLE_UNSPEC = 0
121  *
122  * ... user defined values ...
123  *
124  * RT_TABLE_COMPAT = 252
125  * RT_TABLE_DEFAULT = 253
126  * RT_TABLE_MAIN = 254
127  * RT_TABLE_LOCAL = 255
128  * RT_TABLE_MAX = 0xFFFFFFFF
129  *
130  * Synonimous attribute: RTA_TABLE.
131  */
132  printf("table=%u ", rm->rtm_table);
133 
134  /* type:
135  * RTN_UNSPEC = 0
136  * RTN_UNICAST = 1
137  * RTN_LOCAL = 2
138  * RTN_BROADCAST = 3
139  * RTN_ANYCAST = 4
140  * RTN_MULTICAST = 5
141  * RTN_BLACKHOLE = 6
142  * RTN_UNREACHABLE = 7
143  * RTN_PROHIBIT = 8
144  * RTN_THROW = 9
145  * RTN_NAT = 10
146  * RTN_XRESOLVE = 11
147  * __RTN_MAX = 12
148  */
149  printf("type=%u ", rm->rtm_type);
150 
151  /* scope:
152  * RT_SCOPE_UNIVERSE = 0 : everywhere in the universe
153  *
154  * ... user defined values ...
155  *
156  * RT_SCOPE_SITE = 200
157  * RT_SCOPE_LINK = 253 : destination attached to link
158  * RT_SCOPE_HOST = 254 : local address
159  * RT_SCOPE_NOWHERE = 255 : not existing destination
160  */
161  printf("scope=%u ", rm->rtm_scope);
162 
163  /* protocol:
164  * RTPROT_UNSPEC = 0
165  * RTPROT_REDIRECT = 1
166  * RTPROT_KERNEL = 2 : route installed by kernel
167  * RTPROT_BOOT = 3 : route installed during boot
168  * RTPROT_STATIC = 4 : route installed by administrator
169  *
170  * Values >= RTPROT_STATIC are not interpreted by kernel, they are
171  * just user-defined.
172  */
173  printf("proto=%u ", rm->rtm_protocol);
174 
175  /* flags:
176  * RTM_F_NOTIFY = 0x100: notify user of route change
177  * RTM_F_CLONED = 0x200: this route is cloned
178  * RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI
179  * RTM_F_PREFIX = 0x800: Prefix addresses
180  */
181  printf("flags=%x\n", rm->rtm_flags);
182 
183  mnl_attr_parse(nlh, sizeof(*rm), data_attr_cb, tb);
184 
185  switch(rm->rtm_family) {
186  case AF_INET:
187  attributes_show_ipv4(tb);
188  break;
189  }
190 
191  return MNL_CB_OK;
192 }
193 
194 int main(void)
195 {
196  struct mnl_socket *nl;
197  char buf[MNL_SOCKET_BUFFER_SIZE];
198  struct nlmsghdr *nlh;
199  struct rtmsg *rtm;
200  int ret;
201  unsigned int seq, portid;
202 
203  nlh = mnl_nlmsg_put_header(buf);
204  nlh->nlmsg_type = RTM_GETROUTE;
205  nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
206  nlh->nlmsg_seq = seq = time(NULL);
207  rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
208  rtm->rtm_family = AF_INET;
209 
210  nl = mnl_socket_open(NETLINK_ROUTE);
211  if (nl == NULL) {
212  perror("mnl_socket_open");
213  exit(EXIT_FAILURE);
214  }
215 
216  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
217  perror("mnl_socket_bind");
218  exit(EXIT_FAILURE);
219  }
220  portid = mnl_socket_get_portid(nl);
221 
222  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
223  perror("mnl_socket_send");
224  exit(EXIT_FAILURE);
225  }
226 
227  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
228  while (ret > 0) {
229  ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
230  if (ret <= MNL_CB_STOP)
231  break;
232  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
233  }
234  if (ret == -1) {
235  perror("error");
236  exit(EXIT_FAILURE);
237  }
238 
239  mnl_socket_close(nl);
240 
241  return 0;
242 }