xref: /aosp_15_r20/external/ethtool/libmnl/examples/rtnl/rtnl-neigh-dump.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <time.h>
7 #include <arpa/inet.h>
8 
9 #include <libmnl/libmnl.h>
10 #include <linux/if.h>
11 #include <linux/if_link.h>
12 #include <linux/rtnetlink.h>
13 
data_attr_cb(const struct nlattr * attr,void * data)14 static int data_attr_cb(const struct nlattr *attr, void *data)
15 {
16 	const struct nlattr **tb = data;
17 	int type = mnl_attr_get_type(attr);
18 
19 	/* skip unsupported attribute in user-space */
20 	if (mnl_attr_type_valid(attr, NDA_MAX) < 0)
21 		return MNL_CB_OK;
22 
23 	switch(type) {
24 	case NDA_DST:
25 	case NDA_LLADDR:
26 		if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) {
27 			perror("mnl_attr_validate");
28 			return MNL_CB_ERROR;
29 		}
30 		break;
31 	}
32 	tb[type] = attr;
33 	return MNL_CB_OK;
34 }
35 
data_cb(const struct nlmsghdr * nlh,void * data)36 static int data_cb(const struct nlmsghdr *nlh, void *data)
37 {
38 	struct nlattr *tb[NDA_MAX + 1] = {};
39 	struct ndmsg *ndm = mnl_nlmsg_get_payload(nlh);
40 
41 	printf("index=%d family=%d ", ndm->ndm_ifindex, ndm->ndm_family);
42 
43 	mnl_attr_parse(nlh, sizeof(*ndm), data_attr_cb, tb);
44 	printf("dst=");
45 	if (tb[NDA_DST]) {
46 		void *addr = mnl_attr_get_payload(tb[NDA_DST]);
47 		char out[INET6_ADDRSTRLEN];
48 
49 		if (inet_ntop(ndm->ndm_family, addr, out, sizeof(out)))
50 			printf("%s ", out);
51 	}
52 
53 	mnl_attr_parse(nlh, sizeof(*ndm), data_attr_cb, tb);
54 	printf("lladdr=");
55 	if (tb[NDA_LLADDR]) {
56 		void *addr = mnl_attr_get_payload(tb[NDA_LLADDR]);
57 		unsigned char lladdr[6] = {0};
58 
59 		if (memcpy(&lladdr, addr, 6))
60 			printf("%02x:%02x:%02x:%02x:%02x:%02x ",
61 			       lladdr[0], lladdr[1], lladdr[2],
62 			       lladdr[3], lladdr[4], lladdr[5]);
63 	}
64 
65 	printf("state=");
66 	switch(ndm->ndm_state) {
67 	case NUD_INCOMPLETE:
68 		printf("incomplete ");
69 		break;
70 	case NUD_REACHABLE:
71 		printf("reachable ");
72 		break;
73 	case NUD_STALE:
74 		printf("stale ");
75 		break;
76 	case NUD_DELAY:
77 		printf("delay ");
78 		break;
79 	case NUD_PROBE:
80 		printf("probe ");
81 		break;
82 	case NUD_FAILED:
83 		printf("failed ");
84 		break;
85 	case NUD_NOARP:
86 		printf("noarp ");
87 		break;
88 	case NUD_PERMANENT:
89 		printf("permanent ");
90 		break;
91 	default:
92 		printf("%d ", ndm->ndm_state);
93 		break;
94 	}
95 
96 	printf("\n");
97 	return MNL_CB_OK;
98 }
99 
main(int argc,char * argv[])100 int main(int argc, char *argv[])
101 {
102 	char buf[MNL_SOCKET_DUMP_SIZE];
103 	unsigned int seq, portid;
104 	struct mnl_socket *nl;
105 	struct nlmsghdr *nlh;
106 	struct ndmsg *nd;
107 	int ret;
108 
109 	if (argc != 2) {
110 		fprintf(stderr, "Usage: %s <inet|inet6>\n", argv[0]);
111 		exit(EXIT_FAILURE);
112 	}
113 
114 	nlh = mnl_nlmsg_put_header(buf);
115 	nlh->nlmsg_type	= RTM_GETNEIGH;
116 	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
117 	nlh->nlmsg_seq = seq = time(NULL);
118 
119 	nd = mnl_nlmsg_put_extra_header(nlh, sizeof(struct ndmsg));
120 	if (strcmp(argv[1], "inet") == 0)
121 		nd->ndm_family = AF_INET;
122 	else if (strcmp(argv[1], "inet6") == 0)
123 		nd->ndm_family = AF_INET6;
124 
125 	nl = mnl_socket_open(NETLINK_ROUTE);
126 	if (nl == NULL) {
127 		perror("mnl_socket_open");
128 		exit(EXIT_FAILURE);
129 	}
130 
131 	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
132 		perror("mnl_socket_bind");
133 		exit(EXIT_FAILURE);
134 	}
135 	portid = mnl_socket_get_portid(nl);
136 
137 	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
138 		perror("mnl_socket_sendto");
139 		exit(EXIT_FAILURE);
140 	}
141 
142 	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
143 	while (ret > 0) {
144 		ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
145 		if (ret <= MNL_CB_STOP)
146 			break;
147 		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
148 	}
149 
150 	if (ret == -1) {
151 		perror("error");
152 		exit(EXIT_FAILURE);
153 	}
154 
155 	mnl_socket_close(nl);
156 
157 	return 0;
158 }
159