forked from radvd-project/radvd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netlink.c
216 lines (187 loc) · 5.9 KB
/
netlink.c
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
/*
*
* Authors:
* Lars Fenneberg <[email protected]>
* Reuben Hawkins <[email protected]>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <[email protected]>.
*
*/
#include "netlink.h"
#include "config.h"
#include "log.h"
#include "radvd.h"
#include <asm/types.h>
#include <errno.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
struct iplink_req {
struct nlmsghdr n;
struct ifinfomsg i;
char buf[1024];
};
int netlink_get_device_addr_len(struct Interface *iface)
{
struct iplink_req req = {};
struct iovec iov = {&req, sizeof(req)};
struct sockaddr_nl sa = {};
struct msghdr msg = {(void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0};
int sock, len, addr_len = -1;
unsigned short type;
char answer[32768];
struct rtattr *tb;
/* nl_pid (for linux kernel) and nl_groups (unicast) should be zero */
sa.nl_family = AF_NETLINK;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_GETLINK;
req.i.ifi_index = iface->props.if_index;
sock = netlink_socket();
if (sock == -1)
return -1;
len = sendmsg(sock, &msg, 0);
if (len == -1) {
flog(LOG_ERR, "netlink: sendmsg for addr_len failed: %s", strerror(errno));
goto out;
}
iov.iov_base = answer;
iov.iov_len = sizeof(answer);
len = recvmsg(sock, &msg, 0);
if (len == -1) {
flog(LOG_ERR, "netlink: recvmsg for addr_len failed: %s", strerror(errno));
goto out;
}
if (len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
goto out;
len -= NLMSG_LENGTH(sizeof(struct ifinfomsg));
tb = (struct rtattr *)(answer + NLMSG_LENGTH(sizeof(struct ifinfomsg)));
while (RTA_OK(tb, len)) {
type = tb->rta_type & ~NLA_F_NESTED;
if (type == IFLA_ADDRESS) {
addr_len = RTA_PAYLOAD(tb);
break;
}
tb = RTA_NEXT(tb, len);
}
out:
close(sock);
return addr_len;
}
void process_netlink_msg(int sock, struct Interface *ifaces)
{
char buf[4096];
struct iovec iov = {buf, sizeof(buf)};
struct sockaddr_nl sa;
struct msghdr msg = {(void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0};
int len = recvmsg(sock, &msg, 0);
if (len == -1) {
flog(LOG_ERR, "netlink: recvmsg failed: %s", strerror(errno));
}
for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
char ifnamebuf[IF_NAMESIZE];
/* The end of multipart message. */
if (nh->nlmsg_type == NLMSG_DONE)
return;
if (nh->nlmsg_type == NLMSG_ERROR) {
flog(LOG_ERR, "netlink: unknown error");
abort();
}
/* Continue with parsing payload. */
if (nh->nlmsg_type == RTM_NEWLINK || nh->nlmsg_type == RTM_DELLINK || nh->nlmsg_type == RTM_SETLINK) {
struct ifinfomsg *ifinfo = (struct ifinfomsg *)NLMSG_DATA(nh);
const char *ifname = if_indextoname(ifinfo->ifi_index, ifnamebuf);
struct rtattr *rta = IFLA_RTA(NLMSG_DATA(nh));
int rta_len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
if (rta->rta_type == IFLA_OPERSTATE || rta->rta_type == IFLA_LINKMODE) {
if (ifinfo->ifi_flags & IFF_RUNNING) {
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, flags is running", ifname,
ifinfo->ifi_index);
} else {
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, flags is *NOT* running", ifname,
ifinfo->ifi_index);
}
}
}
/* Reinit the interfaces which need it. */
struct Interface *iface;
switch (nh->nlmsg_type) {
case RTM_NEWLINK:
iface = find_iface_by_name(ifaces, ifname);
break;
default:
iface = find_iface_by_index(ifaces, ifinfo->ifi_index);
break;
}
if (iface) {
touch_iface(iface);
}
} else if (nh->nlmsg_type == RTM_NEWADDR || nh->nlmsg_type == RTM_DELADDR) {
struct ifaddrmsg *ifaddr = (struct ifaddrmsg *)NLMSG_DATA(nh);
const char *ifname = if_indextoname(ifaddr->ifa_index, ifnamebuf);
switch (nh->nlmsg_type) {
case RTM_DELADDR:
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, address deleted", ifname, ifaddr->ifa_index);
break;
case RTM_NEWADDR:
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, new address", ifname, ifaddr->ifa_index);
break;
default:
flog(LOG_ERR, "netlink: unhandled event: %d", nh->nlmsg_type);
break;
}
struct Interface *iface = find_iface_by_index(ifaces, ifaddr->ifa_index);
if (iface) {
struct in6_addr *if_addrs = NULL;
int count = get_iface_addrs(iface->props.name, NULL, &if_addrs);
if (count != iface->props.addrs_count ||
0 != memcmp(if_addrs, iface->props.if_addrs, count * sizeof(struct in6_addr))) {
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, addresses are different", ifname,
ifaddr->ifa_index);
touch_iface(iface);
} else {
dlog(LOG_DEBUG, 3, "netlink: %s, ifindex %d, addresses are the same", ifname,
ifaddr->ifa_index);
}
free(if_addrs);
}
}
}
}
int netlink_socket(void)
{
int sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1) {
flog(LOG_ERR, "Unable to open netlink socket: %s", strerror(errno));
return -1;
}
#if defined SOL_NETLINK && defined NETLINK_NO_ENOBUFS
else if (setsockopt(sock, SOL_NETLINK, NETLINK_NO_ENOBUFS, (int[]){1}, sizeof(int)) < 0) {
flog(LOG_ERR, "Unable to setsockopt NETLINK_NO_ENOBUFS: %s", strerror(errno));
}
#endif
struct sockaddr_nl snl;
memset(&snl, 0, sizeof(snl));
snl.nl_family = AF_NETLINK;
snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV6_IFADDR;
int rc = bind(sock, (struct sockaddr *)&snl, sizeof(snl));
if (rc == -1) {
flog(LOG_ERR, "Unable to bind netlink socket: %s", strerror(errno));
close(sock);
sock = -1;
}
return sock;
}