summaryrefslogtreecommitdiffstats
path: root/src/trans-libnl.c
blob: c623375203c1b90dbc1e32c16726af2e44bd4431 (plain)
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

#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/tsnif.h>
#include <asm/errno.h>

#include "intf.h"
#include "trans.h"
#include "debug.h"

#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif

TSNIF_POLICY(gnl_policy);

static int seq_cb(struct nl_msg *msg, void *arg)
{
	/* FIXME Our sequence numbers of DATA packets are zero,
	 * so let disable the seq check completely */
	return NL_OK;
}

static int process_cb(struct trans_handle *h, struct trans_msg *m,
		      struct nlmsghdr *nlh)
{
	struct genlmsghdr *ghdr = nlmsg_data(nlh);
	struct nlattr *attrs[TSNIF_ATTR_MAX + 1];

	genlmsg_parse(nlh, 0, attrs, TSNIF_ATTR_MAX, gnl_policy);

	/* parse the message and get all the attributes we can */
	if (attrs[TSNIF_ATTR_IDX])
		m->idx = nla_get_u32(attrs[TSNIF_ATTR_IDX]);

        if (attrs[TSNIF_ATTR_TYPE])
                m->type = nla_get_u32(attrs[TSNIF_ATTR_TYPE]);

	if (attrs[TSNIF_ATTR_GROUP])
		m->group = nla_get_u32(attrs[TSNIF_ATTR_GROUP]);

	if (attrs[TSNIF_ATTR_DATA]) {
		m->data.ptr = nla_data(attrs[TSNIF_ATTR_DATA]);
		m->data.len = nla_len(attrs[TSNIF_ATTR_DATA]);
	}

	if (attrs[TSNIF_ATTR_TIME]) {
		struct timespec *time;

		time = (struct timespec*) nla_data(attrs[TSNIF_ATTR_TIME]);
		m->data.time = *(time);
	}

	if (attrs[TSNIF_ATTR_WS]) {
		struct winsize *ws;

		ws = (struct winsize*) nla_data(attrs[TSNIF_ATTR_WS]);
		m->data.ws = *(ws);
	}

	if (attrs[TSNIF_ATTR_FLAGS])
		m->data.flags = nla_get_u32(attrs[TSNIF_ATTR_FLAGS]);

	m->cmd = ghdr->cmd;

	TSNIF_DEBUG(TRANS, "got cmd %d, type %d, idx %d, "
		    "ack %d, err %d\n",
		    m->cmd, m->type, m->idx,
		    m->ack, m->err);

	return h->cb(h, m) ? NL_STOP : NL_OK;
}

static int err_cb(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
{
	struct trans_msg m;

	memset(&m, 0x0, sizeof(m));

	if (nlerr->error == TSNIF_OK)
		m.ack = 1;
	else {
		m.err = 1;
		m.error = nlerr->error;
	}

	TSNIF_DEBUG(TRANS, "got error cb - ack %d, err %d, error %d\n",
		m.ack, m.err, m.error);

	return process_cb(arg, &m, &nlerr->msg);
}

static int parse_cb(struct nl_msg *msg, void *arg)
{
	struct trans_msg m;

	TSNIF_DEBUG(TRANS, "got parse cb\n");

	memset(&m, 0x0, sizeof(m));

	return process_cb(arg, &m, nlmsg_hdr(msg));
}

static int set_no_enobufs(struct trans_handle *h)
{
	int val = 1;

	return setsockopt(trans_fd(h),
		SOL_NETLINK, NETLINK_NO_ENOBUFS,
		&val, sizeof(val));
}

int trans_init(struct trans_handle *h, trans_cb_t cb)
{
	struct nl_handle *sock;
	int family;
	int err;

	memset(h, 0x0, sizeof(*h));

	sock = nl_handle_alloc();
	if (!sock)
		return nl_get_errno();

	err = genl_connect(sock);
	if (err)
		return err;

	TSNIF_DEBUG(TRANS, "connected\n");

	family = genl_ctrl_resolve(sock, "tsnif");
	if (family < 0)
		return family;

	TSNIF_DEBUG(TRANS, "tsnif module found\n");


	nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, parse_cb, h);
	nl_socket_modify_cb(sock, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, seq_cb, h);
	nl_cb_err(nl_socket_get_cb(sock), NL_CB_CUSTOM, err_cb, h);

	h->sock   = sock;
	h->family = family;
	h->cb     = cb;

	err = set_no_enobufs(h);
	if (err)
		TSNIF_DEBUG(TRANS, "failed to set ENOBUFS, moving on...\n");

	return 0;
}

int trans_close(struct trans_handle *h)
{
	nl_handle_destroy(h->sock);
	return 0;
}

int trans_process(struct trans_handle *h)
{
	int err;

	err = nl_recvmsgs_default(h->sock);
	if (err)
		TSNIF_DEBUG(TRANS, "got error: %s\n", nl_geterror());

	return err;
}

int trans_send(struct trans_handle *h, struct trans_msg *m)
{
	struct nl_msg *msg;
	int err;

	TSNIF_DEBUG(TRANS, "sending cmd = %d\n", m->cmd);

	if ((m->cmd != TSNIF_CMD_ATTACH) &&
	    (m->cmd != TSNIF_CMD_DETACH) &&
	    (m->cmd != TSNIF_CMD_TTY_LIST) &&
	    (m->cmd != TSNIF_CMD_MGROUP))
		return -EINVAL;

	msg = nlmsg_alloc();
	genlmsg_put(msg,
		NL_AUTO_PID, NL_AUTO_SEQ,
		h->family, 0, NLM_F_ECHO,
                m->cmd, TSNIF_VERSION);

	if ((m->cmd == TSNIF_CMD_ATTACH) ||
	    (m->cmd == TSNIF_CMD_DETACH)) {
		nla_put_u32(msg, TSNIF_ATTR_TYPE, m->type);
		nla_put_u32(msg, TSNIF_ATTR_IDX, m->idx);
	}

	err = nl_send_auto_complete(h->sock, msg);
	nlmsg_free(msg);

	TSNIF_DEBUG(TRANS, "sent result %d\n", err);
	return err > 0 ? 0 : err;
}

int trans_group(struct trans_handle *h, int group)
{
	return nl_socket_add_membership(h->sock, group);
}

int trans_fd(struct trans_handle *h)
{
	return nl_socket_get_fd(h->sock);
}