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
| // gcc user_nl.c -lnl
//
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <asm/types.h>
//#include <linux/genetlink.h>
#include <netlink/socket.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#define MESSAGE_TO_KERNEL "Hello World from user space!!"
struct family_hdr {
int ff;
};
struct data_hdr {
int len;
int kk;
char str[0];
};
/* netlink attributes */
enum {
DOC_EXMPL_A_UNSPEC,
DOC_EXMPL_A_MSG,
DOC_EXMPL_A_MSG2,
__DOC_EXMPL_A_MAX,
};
#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)
//copy from kernel driver genl_ops's cmd
enum {
DOC_EXMPL_C_UNSPEC,
DOC_EXMPL_C_ECHO,
__DOC_EXMPL_C_MAX,
};
#define DOC_EXMPL_C_MAX (__DOC_EXMPL_C_MAX - 1)
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
int *ret = arg;
fprintf(stderr, "error\n");
*ret = err->error;
return NL_STOP;
}
static int finish_handler(struct nl_msg *msg, void *arg)
{
int *ret = arg;
*ret = 0;
fprintf(stderr, "finish\n");
return NL_SKIP;
}
static int ack_handler(struct nl_msg *msg, void *arg)
{
int *ret = arg;
*ret = 0;
fprintf(stderr, "ack\n");
return NL_STOP;
}
static int reply_handler(struct nl_msg *msg, void *arg)
{
struct nlattr *tb[DOC_EXMPL_A_MAX + 1] = {0};
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct family_hdr *fhdr = genlmsg_attrdata(gnlh, 0);
struct data_hdr *hdr;
int rc, i;
int rcvlen;
rc = nla_parse(tb, DOC_EXMPL_A_MAX,
genlmsg_attrdata(gnlh, sizeof(struct family_hdr)),
genlmsg_attrlen(gnlh, sizeof(struct family_hdr)), NULL);
if (rc) {
printf("nla_parse err\n");
return NL_STOP;
}
for (i = 1; i <= DOC_EXMPL_A_MAX; i ++) {
rcvlen = nla_len(tb[i]);
hdr = (struct data_hdr*)nla_data(tb[i]);
printf("rcv: %d %d %d %d rcvlen=%d %s\n", i, fhdr->ff, hdr->len, hdr->kk, rcvlen, hdr->str);
}
return NL_OK;
}
static int test_genlink_nl(void)
{
struct nl_handle *nlsk;
int nlid, err, ret;
struct nl_msg *msg = NULL;
struct nl_cb *cb = NULL;
char cbdata[123], sss[123];
struct family_hdr *fhdr;
struct data_hdr *hdr = (struct data_hdr *)sss;
nlsk = nl_handle_alloc();
if (!nlsk) {
printf("nl_socket_alloc err\n");
return -1;
}
nl_set_buffer_size(nlsk, 8192, 8192);
if (genl_connect(nlsk)) {
printf("genl_connect err\n");
goto free_nlsk;
}
nlid = genl_ctrl_resolve(nlsk, "DOC_EXMPL");
if (nlid < 0) {
printf("genl_ctrl_resolve err\n");
goto free_nlsk;
}
msg = nlmsg_alloc();
if (!msg) {
printf("nlmsg_alloc err\n");
goto free_nlsk;
}
cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb) {
printf("nl_cb_alloc err\n");
goto free_msg;
}
nl_socket_set_cb(nlsk, cb);
fhdr = (struct family_hdr *)genlmsg_put(msg, 0, 0, nlid, sizeof(struct family_hdr), 0, DOC_EXMPL_C_ECHO, 0);
if (!fhdr) {
printf("genlmsg_put err\n");
goto free_cb;
}
fhdr->ff = 111;
hdr->len = 123;
hdr->kk = 456;
strcpy(hdr->str, MESSAGE_TO_KERNEL);
ret = nla_put(msg, DOC_EXMPL_A_MSG, sizeof(struct data_hdr)+strlen(MESSAGE_TO_KERNEL), hdr);
if (ret < 0) {
printf("nla_put err\n");
goto free_cb;
}
hdr->len = 700;
hdr->kk = 800;
ret = nla_put(msg, DOC_EXMPL_A_MSG2, sizeof(struct data_hdr)+strlen(MESSAGE_TO_KERNEL), hdr);
if (ret < 0) {
printf("nla_put err2\n");
goto free_cb;
}
ret = nl_send_auto_complete(nlsk, msg);
if (ret < 0) {
printf("nl_send_auto_complete err\n");
goto free_cb;
}
err = 1;
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reply_handler, &cbdata);
ret = nl_recvmsgs(nlsk, cb);
if (ret) {
printf("nl_recvmsgs err\n");
}
return err;
free_cb:
nl_cb_put(cb);
free_msg:
nlmsg_free(msg);
free_nlsk:
nl_handle_destroy(nlsk);
return -2;
}
int main(int argc, char *argv[])
{
return test_genlink_nl();
}
|