summaryrefslogtreecommitdiff
path: root/src/mesh-netlink.c
blob: 22d69254fd2836ebd1ff94cff5274ca2490f4df0 (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
/*
 *
 *  Connection Manager
 *
 *
 *  Copyright (C) 2017 Samsung Electronics Co., Ltd.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 *  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "connman.h"
#include <connman/mesh-netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <netlink/netlink.h>

static int seq_check_cb(struct nl_msg *msg, void *arg)
{
	DBG("");

	return NL_OK;
}

static int finish_cb(struct nl_msg *msg, void *arg)
{
	int *ret = arg;

	DBG("");

	*ret = 0;

	return NL_SKIP;
}

static int ack_cb(struct nl_msg *msg, void *arg)
{
	int *ret = arg;

	DBG("");

	*ret = 0;

	return NL_STOP;
}

static int valid_cb(struct nl_msg *msg, void *arg)
{
	DBG("");

	return NL_SKIP;
}

static int error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
	int *ret = arg;

	*ret = err->error;

	DBG("error %d", *ret);

	return NL_STOP;
}

int __connman_mesh_netlink_set_gate_announce(mesh_nl80211_global *global,
					int mesh_if_index, bool gate_announce, int hwmp_rootmode)
{
	struct nl_msg *msg;
	struct nlattr *container;
	struct nl_cb *cb;
	int err = -1;

	msg = nlmsg_alloc();
	if (!msg)
		return -1;

	cb = nl_cb_clone(global->cb);
	if (!cb)
		goto out;

	genlmsg_put(msg, 0, 0, global->id, 0, 0, NL80211_CMD_SET_MESH_CONFIG, 0);
	nla_put_u32(msg, NL80211_ATTR_IFINDEX, mesh_if_index);

	container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);

	nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE, hwmp_rootmode);

	nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS, gate_announce);

	nla_nest_end(msg, container);

	err = nl_send_auto_complete(global->nl_socket, msg);
	if (err < 0) {
		DBG("Failed to send msg");
		goto out;
	}

	err = 1;

	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_cb, &err);
	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_cb, &err);
	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_cb, &err);
	nl_cb_err(cb, NL_CB_CUSTOM, error_cb, &err);

	while (err > 0) {
		int res = nl_recvmsgs(global->nl_socket, cb);
		if (res < 0)
			DBG("nl_recvmsgs failed: %d", res);
	}

out:
	nl_cb_put(cb);
	nlmsg_free(msg);
	return err;
}

mesh_nl80211_global *__connman_mesh_nl80211_global_init(void)
{
	mesh_nl80211_global *global;

	DBG("");

	global = g_malloc0(sizeof(mesh_nl80211_global));

	global->nl_socket = nl_socket_alloc();
	if (!global->nl_socket) {
		DBG("Failed to allocate netlink socket");
		g_free(global);
		return NULL;
	}

	if (genl_connect(global->nl_socket)) {
		DBG("Failed to connect to generic netlink");
		nl_socket_free(global->nl_socket);
		g_free(global);
		return NULL;
	}

	nl_socket_set_buffer_size(global->nl_socket, 8192, 8192);

	global->id = genl_ctrl_resolve(global->nl_socket, "nl80211");
	if (global->id < 0) {
		DBG("nl80211 generic netlink not found");
		nl_socket_free(global->nl_socket);
		g_free(global);
		return NULL;
	}

	global->cb = nl_cb_alloc(NL_CB_DEFAULT);
	if (!global->cb) {
		DBG("Failed to allocate netwlink callbacks");
		nl_socket_free(global->nl_socket);
		g_free(global);
		return NULL;
	}

	nl_cb_set(global->cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, seq_check_cb, NULL);

	return global;
}

void __connman_mesh_nl80211_global_deinit(mesh_nl80211_global *global)
{
	DBG("");

	nl_cb_put(global->cb);
	nl_socket_free(global->nl_socket);
	g_free(global);
}