forked from ibm-s390-linux/smc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnetlink.c
304 lines (269 loc) · 6.55 KB
/
libnetlink.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
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
/*
* SMC Tools - Shared Memory Communication Tools
*
* Copyright IBM Corp. 2020
*
* Author(s): Ursula Braun <[email protected]>
* Guvenc Gulce <[email protected]>
*
* Userspace program for SMC Information display
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <netlink/socket.h>
#include <netlink/msg.h>
#include <netlink/genl/ctrl.h>
#include "smctools_common.h"
#include "libnetlink.h"
#define MAGIC_SEQ 123456
int smc_id = 0;
struct nl_sock *sk;
/* Operations on sock_diag netlink socket */
int rtnl_open(struct rtnl_handle *rth)
{
socklen_t addr_len;
int rcvbuf = 1024 * 1024;
int sndbuf = 32768;
rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC,
NETLINK_SOCK_DIAG);
if (rth->fd < 0) {
perror("Error: Cannot open netlink socket");
return EXIT_FAILURE;
}
if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
sizeof(sndbuf)) < 0) {
perror("Error: SO_SNDBUF");
return EXIT_FAILURE;
}
if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
sizeof(rcvbuf)) < 0) {
perror("Error: SO_RCVBUF");
return EXIT_FAILURE;
}
memset(&rth->local, 0, sizeof(rth->local));
rth->local.nl_family = AF_NETLINK;
rth->local.nl_groups = 0;
if (bind(rth->fd, (struct sockaddr*)&rth->local,
sizeof(rth->local)) < 0) {
perror("Error: Cannot bind netlink socket");
return EXIT_FAILURE;
}
addr_len = sizeof(rth->local);
if (getsockname(rth->fd, (struct sockaddr*)&rth->local,
&addr_len) < 0) {
perror("Error: getsockname");
return EXIT_FAILURE;
}
if (addr_len != sizeof(rth->local)) {
fprintf(stderr, "Error: Wrong address length %d\n", addr_len);
return EXIT_FAILURE;
}
if (rth->local.nl_family != AF_NETLINK) {
fprintf(stderr, "Error: Wrong address family %d\n",
rth->local.nl_family);
return EXIT_FAILURE;
}
rth->seq = time(NULL);
return 0;
}
void rtnl_close(struct rtnl_handle *rth)
{
if (rth->fd >= 0) {
close(rth->fd);
rth->fd = -1;
}
}
int rtnl_dump(struct rtnl_handle *rth, void (*handler)(struct nlmsghdr *nlh))
{
int msglen, found_done = 0;
struct sockaddr_nl nladdr;
struct iovec iov;
struct msghdr msg = {
.msg_name = &nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = &iov,
.msg_iovlen = 1,
};
char buf[32768];
struct nlmsghdr *h = (struct nlmsghdr *)buf;
memset(buf, 0, sizeof(buf));
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
again:
msglen = recvmsg(rth->fd, &msg, 0);
if (msglen < 0) {
if (errno == EINTR || errno == EAGAIN)
goto again;
fprintf(stderr, "Error: Netlink receive error %s (%d)\n",
strerror(errno), errno);
return EXIT_FAILURE;
}
if (msglen == 0) {
fprintf(stderr, "Error: Unexpected EOF on netlink\n");
return EXIT_FAILURE;
}
while(NLMSG_OK(h, msglen)) {
if (h->nlmsg_flags & NLM_F_DUMP_INTR)
fprintf(stderr, "Error: Dump interrupted\n");
if (h->nlmsg_type == NLMSG_DONE) {
found_done = 1;
break;
}
if (h->nlmsg_type == NLMSG_ERROR) {
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
fprintf(stderr, "Error: Incomplete message\n");
} else {
perror("RTNETLINK answers");
}
return EXIT_FAILURE;
}
(*handler)(h);
h = NLMSG_NEXT(h, msglen);
}
if (msg.msg_flags & MSG_TRUNC) {
fprintf(stderr, "Error: Message truncated\n");
goto again;
}
if (!found_done) {
h = (struct nlmsghdr *)buf;
goto again;
}
return EXIT_SUCCESS;
}
void parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta,
int len)
{
unsigned short type;
memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
while (RTA_OK(rta, len)) {
type = rta->rta_type;
if ((type <= max) && (!tb[type]))
tb[type] = rta;
rta = RTA_NEXT(rta,len);
}
if (len)
fprintf(stderr, "Error: Deficit %d, rta_len=%d\n", len, rta->rta_len);
}
int sockdiag_send(int fd, unsigned char cmd)
{
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
DIAG_REQUEST(req, struct smc_diag_req r, MAGIC_SEQ);
struct msghdr msg;
struct iovec iov[1];
int iovlen = 1;
memset(&req.r, 0, sizeof(req.r));
req.r.diag_family = PF_SMC;
iov[0] = (struct iovec) {
.iov_base = &req,
.iov_len = sizeof(req)
};
msg = (struct msghdr) {
.msg_name = (void *)&nladdr,
.msg_namelen = sizeof(nladdr),
.msg_iov = iov,
.msg_iovlen = iovlen,
};
req.r.diag_ext = cmd;
if (sendmsg(fd, &msg, 0) < 0) {
close(fd);
return EXIT_FAILURE;
}
return 0;
}
/* Operations on generic netlink sockets */
int gen_nl_open(char *pname)
{
int rc = EXIT_FAILURE;
/* Allocate a netlink socket and connect to it */
sk = nl_socket_alloc();
if (!sk) {
nl_perror(NLE_NOMEM, "Error");
return rc;
}
rc = genl_connect(sk);
if (rc) {
nl_perror(rc, "Error");
rc = EXIT_FAILURE;
goto err1;
}
smc_id = genl_ctrl_resolve(sk, SMC_GENL_FAMILY_NAME);
if (smc_id < 0) {
rc = EXIT_FAILURE;
if (smc_id == -NLE_OBJ_NOTFOUND)
fprintf(stderr, "Error: SMC module not loaded\n");
else
nl_perror(smc_id, "Error");
goto err2;
}
return EXIT_SUCCESS;
err2:
nl_close(sk);
err1:
nl_socket_free(sk);
return rc;
}
int gen_nl_handle(int cmd, int nlmsg_flags,
int (*cb_handler)(struct nl_msg *msg, void *arg), void *arg)
{
struct nl_msg *msg;
int rc;
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, cb_handler, arg);
/* Allocate a netlink message and set header information. */
msg = nlmsg_alloc();
if (!msg) {
nl_perror(NLE_NOMEM, "Error");
goto errout;
}
if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, smc_id, 0, nlmsg_flags,
cmd, SMC_GENL_FAMILY_VERSION)) {
nl_perror(NLE_NOMEM, "Error");
goto errout;
}
/* Send message */
rc = nl_send_auto(sk, msg);
if (rc < 0) {
nl_perror(rc, "Error");
goto errout;
}
/* Receive reply message, returns number of cb invocations. */
rc = nl_recvmsgs_default(sk);
if (rc < 0) {
if (rc == -NLE_OPNOTSUPP) {
fprintf(stderr, "Error: Operation not supported by kernel\n");
} else {
nl_perror(rc, "Error");
}
goto errout;
}
nlmsg_free(msg);
return EXIT_SUCCESS;
errout:
nlmsg_free(msg);
return EXIT_FAILURE;
}
int gen_nl_handle_dump(int cmd, int (*cb_handler)(struct nl_msg *msg, void *arg), void *arg)
{
return gen_nl_handle(cmd, NLM_F_DUMP, cb_handler, arg);
}
void gen_nl_close()
{
if (sk) {
nl_close(sk);
nl_socket_free(sk);
sk = NULL;
}
}