summaryrefslogtreecommitdiff
path: root/src/platform/wifi/nm-wifi-utils.c
blob: 25d71c6a884c541ed2a1db2359d8cc20c3f711df (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
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2005 - 2018 Red Hat, Inc.
 * Copyright (C) 2006 - 2008 Novell, Inc.
 */

#include "nm-default.h"

#include "nm-wifi-utils.h"

#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#include "nm-wifi-utils-private.h"
#include "nm-wifi-utils-nl80211.h"
#if HAVE_WEXT
#include "nm-wifi-utils-wext.h"
#endif
#include "nm-core-utils.h"

#include "platform/nm-platform-utils.h"

G_DEFINE_ABSTRACT_TYPE (NMWifiUtils, nm_wifi_utils, G_TYPE_OBJECT)

/*****************************************************************************/

static void
nm_wifi_utils_init (NMWifiUtils *self)
{
}

static void
nm_wifi_utils_class_init (NMWifiUtilsClass *klass)
{
}

NMWifiUtils *
nm_wifi_utils_new (int ifindex, struct nl_sock *genl, gboolean check_scan)
{
	NMWifiUtils *ret;

	g_return_val_if_fail (ifindex > 0, NULL);

	ret = nm_wifi_utils_nl80211_new (ifindex, genl);

#if HAVE_WEXT
	if (ret == NULL)
		ret = nm_wifi_utils_wext_new (ifindex, check_scan);
#endif

	return ret;
}

NMDeviceWifiCapabilities
nm_wifi_utils_get_caps (NMWifiUtils *data)
{
	g_return_val_if_fail (data != NULL, NM_WIFI_DEVICE_CAP_NONE);

	return data->caps;
}

NM80211Mode
nm_wifi_utils_get_mode (NMWifiUtils *data)
{
	g_return_val_if_fail (data != NULL, NM_802_11_MODE_UNKNOWN);
	return NM_WIFI_UTILS_GET_CLASS (data)->get_mode (data);
}

gboolean
nm_wifi_utils_set_mode (NMWifiUtils *data, const NM80211Mode mode)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (   (mode == NM_802_11_MODE_INFRA)
	                      || (mode == NM_802_11_MODE_AP)
	                      || (mode == NM_802_11_MODE_ADHOC), FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);

	/* nl80211 probably doesn't need this */
	return klass->set_mode ? klass->set_mode (data, mode) : TRUE;
}

gboolean
nm_wifi_utils_set_powersave (NMWifiUtils *data, guint32 powersave)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	return klass->set_powersave ? klass->set_powersave (data, powersave) : TRUE;
}

NMSettingWirelessWakeOnWLan
nm_wifi_utils_get_wake_on_wlan (NMWifiUtils *data)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);

	return klass->get_wake_on_wlan ? klass->get_wake_on_wlan (data) : NM_SETTING_WIRELESS_WAKE_ON_WLAN_IGNORE;
}

gboolean
nm_wifi_utils_set_wake_on_wlan (NMWifiUtils *data, NMSettingWirelessWakeOnWLan wowl)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	return klass->set_wake_on_wlan ? klass->set_wake_on_wlan (data, wowl) : FALSE;
}

guint32
nm_wifi_utils_get_freq (NMWifiUtils *data)
{
	g_return_val_if_fail (data != NULL, 0);
	return NM_WIFI_UTILS_GET_CLASS (data)->get_freq (data);
}

guint32
nm_wifi_utils_find_freq (NMWifiUtils *data, const guint32 *freqs)
{
	g_return_val_if_fail (data != NULL, 0);
	g_return_val_if_fail (freqs != NULL, 0);
	return NM_WIFI_UTILS_GET_CLASS (data)->find_freq (data, freqs);
}

gboolean
nm_wifi_utils_get_bssid (NMWifiUtils *data, guint8 *out_bssid)
{
	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (out_bssid != NULL, FALSE);

	memset (out_bssid, 0, ETH_ALEN);
	return NM_WIFI_UTILS_GET_CLASS (data)->get_bssid (data, out_bssid);
}

guint32
nm_wifi_utils_get_rate (NMWifiUtils *data)
{
	g_return_val_if_fail (data != NULL, 0);
	return NM_WIFI_UTILS_GET_CLASS (data)->get_rate (data);
}

int
nm_wifi_utils_get_qual (NMWifiUtils *data)
{
	g_return_val_if_fail (data != NULL, 0);
	return NM_WIFI_UTILS_GET_CLASS (data)->get_qual (data);
}

gboolean
nm_wifi_utils_is_wifi (int dirfd, const char *ifname)
{
	g_return_val_if_fail (dirfd >= 0, FALSE);

	if (faccessat (dirfd, "phy80211", F_OK, 0) == 0)
		return TRUE;
#if HAVE_WEXT
	if (nm_wifi_utils_wext_is_wifi (ifname))
		return TRUE;
#endif
	return FALSE;
}

/* OLPC Mesh-only functions */

guint32
nm_wifi_utils_get_mesh_channel (NMWifiUtils *data)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	g_return_val_if_fail (klass->get_mesh_channel != NULL, FALSE);

	return klass->get_mesh_channel (data);
}

gboolean
nm_wifi_utils_set_mesh_channel (NMWifiUtils *data, guint32 channel)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (channel <= 13, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	g_return_val_if_fail (klass->set_mesh_channel != NULL, FALSE);

	return klass->set_mesh_channel (data, channel);
}

gboolean
nm_wifi_utils_set_mesh_ssid (NMWifiUtils *data, const guint8 *ssid, gsize len)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	g_return_val_if_fail (klass->set_mesh_ssid != NULL, FALSE);

	return klass->set_mesh_ssid (data, ssid, len);
}

gboolean
nm_wifi_utils_indicate_addressing_running (NMWifiUtils *data, gboolean running)
{
	NMWifiUtilsClass *klass;

	g_return_val_if_fail (data != NULL, FALSE);

	klass = NM_WIFI_UTILS_GET_CLASS (data);
	return klass->indicate_addressing_running ? klass->indicate_addressing_running (data, running) : FALSE;
}