1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
title: Netlink socket show ip
keywords: kernel,linux,netlink,socket,ip
# Netlink show network interface ip's
This example based on reading netlink socket and using kernel builtin netlink protocols.
As base is used netlink example.
__Makefile__
```Makefile
make:
gcc netlink_show_ip.c -o netlink_show_ip
```
__netlink_show_ip.c__
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#define PAYLOAD_SIZE 1024 /* maximum payload size*/
#define NETLINK_PROTO 23
int main(int argc, char **argv) {
struct sockaddr_nl src_addr;
struct nlmsghdr *nlh;
struct msghdr msg;
struct iovec iov;
int sock_fd;
int ret;
sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock_fd < 0)
{
printf("socket creation for NETLINK failed...\n");
return -1;
}
close(sock_fd);
return 0;
}
```
## Add socket configuration
Here we configure netlink socket and send message to request ip addresses from NETLINK_ROUTE
```c
...
int main(int argc, char **argv) {
...
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
//bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));//???
// assemble the message according to the netlink protocol
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE));
memset(nlh, 0, NLMSG_SPACE(PAYLOAD_SIZE));
nlh->nlmsg_len = NLMSG_SPACE(PAYLOAD_SIZE);
nlh->nlmsg_type = RTM_GETADDR;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
struct ifaddrmsg *ifa;
ifa = (struct ifaddrmsg*)NLMSG_DATA(nlh);
ifa->ifa_family = AF_INET; // we only get ipv4 address here
// prepare struct msghdr for sending.
memset(&iov, 0, sizeof(iov));
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void *)&src_addr;
msg.msg_namelen = sizeof(src_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
// send netlink message to kernel.
ret = sendmsg(sock_fd, &msg, 0);
if (ret < 0) {
printf("Can't send message to netlink socket\n");
}
...
}
```
## Receive and process netlink replies
```c
#define NLMSG_NEXT(nlh,len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
(struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len <= (len))
```
Message types
```c
#define NLMSG_NOOP 0x1 /* Nothing. */
#define NLMSG_ERROR 0x2 /* Error */
#define NLMSG_DONE 0x3 /* End of a dump */
#define NLMSG_OVERRUN 0x4 /* Data lost */
```
Definition of message rtnetlink types
```c
RTM_NEWADDR = 20,
#define RTM_NEWADDR RTM_NEWADDR
```
List of type of RTA types
```c
enum {
IFA_UNSPEC,
IFA_ADDRESS,
IFA_LOCAL,
IFA_LABEL,
IFA_BROADCAST,
IFA_ANYCAST,
IFA_CACHEINFO,
IFA_MULTICAST,
IFA_FLAGS,
IFA_RT_PRIORITY, /* u32, priority/metric for prefix route */
IFA_TARGET_NETNSID,
IFA_PROTO, /* u8, address protocol */
__IFA_MAX,
};
```
Receiving netlink message from a
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
...
...
int main(){
...
do {
ret = recvmsg(sock_fd, &msg_recv, 0);
if (ret < 0) {
printf("Can't receive reply message\n");
break;
}
if (ret==0) {
printf("received 0 byte reply\n");
break;
}
ret_len = ret;
nlh_recv = (struct nlmsghdr*)recv_buf;
while (NLMSG_OK(nlh_recv, (uint32_t)ret_len) && (nlh_recv->nlmsg_type != NLMSG_DONE))
{
printf("received message type %d\n", nlh_recv->nlmsg_type);
if (nlh_recv->nlmsg_type == NLMSG_ERROR) {
printf("Message error\n");
break;
}
if (nlh_recv->nlmsg_type == RTM_NEWADDR) {
...
}
nlh_recv = NLMSG_NEXT(nlh_recv, ret);
}
} while ((nl_msg_type != NLMSG_DONE) && (nl_msg_type != NLMSG_ERROR));
...
}
```
After message type is detected parse the RTNETLINK message
```c
int main() {
...
ifa_recv = (struct ifaddrmsg*)NLMSG_DATA(nlh_recv);
int ifa_len = IFA_PAYLOAD(nlh_recv);
rta = IFA_RTA(ifa_recv);
printf("interface %s\n", if_indextoname(ifa_recv->ifa_index, ifname));
printf("prefix length %d\n", ifa_recv->ifa_prefixlen);
while (RTA_OK(rta, ret_len)) {
rta = RTA_NEXT(rta, ifa_len);
char ip[INET6_ADDRSTRLEN];
//parse the message
if (rta->rta_type == IFA_ADDRESS) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_ADDRESS %s\n", ip);
}
if (rta->rta_type == IFA_LOCAL) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_LOCAL %s\n", ip);
}
if (rta->rta_type == IFA_BROADCAST) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_BROADCAST %s\n", ip);
}
}
...
}
```
## Build
```sh
make
```
## Run
```sh
./netlink_show_ip
```
Output looks like
```sh
received message type 20
interface lo
prefix length 8
IFA_LOCAL 127.0.0.1
received message type 20
interface enp0
prefix length 22
IFA_LOCAL 192.168.1.149
IFA_BROADCAST 192.168.1.255
```
## Final result
__netlink_show_ip.c__
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define PAYLOAD_SIZE 4096 /* maximum payload size*/
char recv_buf[4096];
int main(int argc, char **argv) {
struct sockaddr_nl src_addr;
struct nlmsghdr *nlh;
struct nlmsghdr *nlh_recv;
struct msghdr msg;
struct msghdr msg_recv;
struct iovec iov;
struct iovec iov_recv;
struct ifaddrmsg *ifa;
struct ifaddrmsg *ifa_recv;
struct rtattr *rta = NULL;
int sock_fd;
int ret;
int ret_len;
uint32_t nl_msg_type;
char ifname[IF_NAMESIZE];
sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock_fd < 0)
{
printf("socket creation for NETLINK failed...\n");
return -1;
}
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));//???
// assemble the message according to the netlink protocol
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE));
memset(nlh, 0, NLMSG_SPACE(PAYLOAD_SIZE));
nlh->nlmsg_len = NLMSG_SPACE(PAYLOAD_SIZE);
nlh->nlmsg_type = RTM_GETADDR;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
ifa = (struct ifaddrmsg*)NLMSG_DATA(nlh);
ifa->ifa_family = AF_INET; // we only get ipv4 address here
// prepare struct msghdr for sending.
memset(&iov, 0, sizeof(iov));
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
//struct msghdr msg = { src_addr, sizeof(*src_addr), &iov, 1, NULL, 0, 0 };
memset(&msg, 0, sizeof(msg));
msg.msg_name = (void *)&src_addr;
msg.msg_namelen = sizeof(src_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
// send netlink message to kernel.
ret = sendmsg(sock_fd, &msg, 0);
if (ret < 0) {
printf("Can't send message to netlink socket\n");
}
//prepare to receive reply
memset(&iov_recv, 0, sizeof(iov_recv));
iov_recv.iov_base = recv_buf;
iov_recv.iov_len = PAYLOAD_SIZE;
memset(&msg_recv, 0, sizeof(msg_recv));
msg_recv.msg_name = (void *)&src_addr;
msg_recv.msg_namelen = sizeof(src_addr);
msg_recv.msg_iov = &iov_recv;
msg_recv.msg_iovlen = 1;
do {
ret = recvmsg(sock_fd, &msg_recv, 0);
if (ret < 0) {
printf("Can't receive reply message\n");
break;
}
if (ret==0) {
printf("received 0 byte reply\n");
break;
}
ret_len = ret;
nlh_recv = (struct nlmsghdr*)recv_buf;
while (NLMSG_OK(nlh_recv, (uint32_t)ret_len) && (nlh_recv->nlmsg_type != NLMSG_DONE))
{
printf("received message type %d\n", nlh_recv->nlmsg_type);
if (nlh_recv->nlmsg_type == NLMSG_ERROR) {
printf("Message error\n");
break;
}
if (nlh_recv->nlmsg_type == RTM_NEWADDR) {
ifa_recv = (struct ifaddrmsg*)NLMSG_DATA(nlh_recv);
int ifa_len = IFA_PAYLOAD(nlh_recv);
rta = IFA_RTA(ifa_recv);
printf("interface %s\n", if_indextoname(ifa_recv->ifa_index, ifname));
printf("prefix length %d\n", ifa_recv->ifa_prefixlen);
while (RTA_OK(rta, ret_len)) {
rta = RTA_NEXT(rta, ifa_len);
char ip[INET6_ADDRSTRLEN];
//parse the message
if (rta->rta_type == IFA_ADDRESS) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_ADDRESS %s\n", ip);
}
if (rta->rta_type == IFA_LOCAL) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_LOCAL %s\n", ip);
}
if (rta->rta_type == IFA_BROADCAST) {
inet_ntop(ifa_recv->ifa_family, RTA_DATA(rta), ip, INET6_ADDRSTRLEN);
printf("IFA_BROADCAST %s\n", ip);
}
}
}
nlh_recv = NLMSG_NEXT(nlh_recv, ret);
}
} while ((nl_msg_type != NLMSG_DONE) && (nl_msg_type != NLMSG_ERROR));
close(sock_fd);
return 0;
}
```
## Links
1. [/notes/kernel/netlink_socket.md](/notes/kernel/netlink_socket.md)
1. https://github.com/d0u9/examples/blob/master/C/netlink/ip_show.c
2. https://gist.github.com/luohao-brian/7535fa346f37450137e5db45cd793ace
3. https://github.com/mwarning/netlink-examples/tree/master
4. https://elixir.bootlin.com/linux/v6.6.20/source/include/uapi/linux/rtnetlink.h#L42
5. https://elixir.bootlin.com/linux/v6.6.20/source/include/uapi/linux/netlink.h#L100
|