summaryrefslogtreecommitdiff
path: root/libnm-core/nm-setting-wifi-p2p.c
blob: bbcc80edb147da179c4c33bf6b0fafbc73a6c819 (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
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
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright 2019 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-setting-wifi-p2p.h"

#include <net/ethernet.h>

#include "nm-utils.h"
#include "nm-libnm-core-intern/nm-common-macros.h"
#include "nm-utils-private.h"
#include "nm-setting-private.h"

/**
 * SECTION:nm-setting-wifi-p2p
 * @short_description: Describes connection properties for 802.11 Wi-Fi P2P networks
 *
 * The #NMSettingWifiP2P object is a #NMSetting subclass that describes properties
 * necessary for connection to 802.11 Wi-Fi P2P networks (aka Wi-Fi Direct).
 **/

/**
 * NMSettingWifiP2P:
 *
 * Wi-Fi P2P Settings
 *
 * Since: 1.16
 */

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

NM_GOBJECT_PROPERTIES_DEFINE_BASE (
	PROP_PEER,
	PROP_WPS_METHOD,
	PROP_WFD_IES,
);

typedef struct {
	char *peer_mac_address;
	GBytes *wfd_ies;

	NMSettingWirelessSecurityWpsMethod wps_method;
} NMSettingWifiP2PPrivate;

struct _NMSettingWifiP2P {
	NMSetting parent;
	NMSettingWifiP2PPrivate _priv;
};

struct _NMSettingWifiP2PClass {
	NMSettingClass parent;
};

G_DEFINE_TYPE (NMSettingWifiP2P, nm_setting_wifi_p2p, NM_TYPE_SETTING)

#define NM_SETTING_WIFI_P2P_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMSettingWifiP2P, NM_IS_SETTING_WIFI_P2P, NMSetting)

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

/**
 * nm_setting_wifi_p2p_get_peer:
 * @setting: the #NMSettingWifiP2P
 *
 * Returns: the #NMSettingWifiP2P:peer property of the setting
 *
 * Since: 1.16
 **/
const char *
nm_setting_wifi_p2p_get_peer (NMSettingWifiP2P *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIFI_P2P (setting), NULL);

	return NM_SETTING_WIFI_P2P_GET_PRIVATE (setting)->peer_mac_address;
}

/**
 * nm_setting_wifi_p2p_get_wps_method:
 * @setting: the #NMSettingWifiP2P
 *
 * Returns: the #NMSettingWifiP2P:wps-method property of the setting
 *
 * Since: 1.16
 **/
NMSettingWirelessSecurityWpsMethod
nm_setting_wifi_p2p_get_wps_method (NMSettingWifiP2P *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIFI_P2P (setting), NM_SETTING_WIRELESS_SECURITY_WPS_METHOD_DEFAULT);

	return NM_SETTING_WIFI_P2P_GET_PRIVATE (setting)->wps_method;
}

/**
 * nm_setting_wifi_p2p_get_wfd_ies:
 * @setting: the #NMSettingWiFiP2P
 *
 * Returns: (transfer none): the #NMSettingWiFiP2P:wfd-ies property of the setting
 *
 * Since: 1.16
 **/
GBytes *
nm_setting_wifi_p2p_get_wfd_ies (NMSettingWifiP2P *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIFI_P2P (setting), NULL);

	return NM_SETTING_WIFI_P2P_GET_PRIVATE (setting)->wfd_ies;
}

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

static gboolean
verify (NMSetting *setting, NMConnection *connection, GError **error)
{
	NMSettingWifiP2PPrivate *priv = NM_SETTING_WIFI_P2P_GET_PRIVATE (setting);

	if (!priv->peer_mac_address) {
		g_set_error_literal (error,
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_MISSING_PROPERTY,
		                     _("property is missing"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIFI_P2P_SETTING_NAME, NM_SETTING_WIFI_P2P_PEER);
		return FALSE;
	}

	if (!nm_utils_hwaddr_valid (priv->peer_mac_address, ETH_ALEN)) {
		g_set_error_literal (error,
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIFI_P2P_SETTING_NAME, NM_SETTING_WIFI_P2P_PEER);
		return FALSE;
	}

	if (!_nm_utils_wps_method_validate (priv->wps_method,
	                                    NM_SETTING_WIFI_P2P_SETTING_NAME,
	                                    NM_SETTING_WIFI_P2P_WPS_METHOD,
	                                    TRUE,
	                                    error))
		return FALSE;

	return TRUE;
}

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

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	NMSettingWifiP2P *setting = NM_SETTING_WIFI_P2P (object);

	switch (prop_id) {
	case PROP_PEER:
		g_value_set_string (value, nm_setting_wifi_p2p_get_peer (setting));
		break;
	case PROP_WPS_METHOD:
		g_value_set_uint (value, nm_setting_wifi_p2p_get_wps_method (setting));
		break;
	case PROP_WFD_IES:
		g_value_set_boxed (value, nm_setting_wifi_p2p_get_wfd_ies (setting));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMSettingWifiP2PPrivate *priv = NM_SETTING_WIFI_P2P_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_PEER:
		g_free (priv->peer_mac_address);
		priv->peer_mac_address = _nm_utils_hwaddr_canonical_or_invalid (g_value_get_string (value),
		                                                                ETH_ALEN);
		break;
	case PROP_WPS_METHOD:
		priv->wps_method = g_value_get_uint (value);
		break;
	case PROP_WFD_IES:
		g_clear_pointer (&priv->wfd_ies, g_bytes_unref);
		priv->wfd_ies = g_value_dup_boxed (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

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

static void
nm_setting_wifi_p2p_init (NMSettingWifiP2P *setting)
{
}

/**
 * nm_setting_wifi_p2p_new:
 *
 * Creates a new #NMSettingWifiP2P object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingWifiP2P object
 *
 * Since: 1.16
 **/
NMSetting *
nm_setting_wifi_p2p_new (void)
{
	return g_object_new (NM_TYPE_SETTING_WIFI_P2P, NULL);
}

static void
finalize (GObject *object)
{
	NMSettingWifiP2PPrivate *priv = NM_SETTING_WIFI_P2P_GET_PRIVATE (object);

	g_free (priv->peer_mac_address);
	g_bytes_unref (priv->wfd_ies);

	G_OBJECT_CLASS (nm_setting_wifi_p2p_parent_class)->finalize (object);
}

static void
nm_setting_wifi_p2p_class_init (NMSettingWifiP2PClass *setting_wifi_p2p_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (setting_wifi_p2p_class);
	NMSettingClass *setting_class = NM_SETTING_CLASS (setting_wifi_p2p_class);

	object_class->get_property = get_property;
	object_class->set_property = set_property;
	object_class->finalize     = finalize;

	setting_class->verify      = verify;

	/**
	 * NMSettingWifiP2P:peer:
	 *
	 * The P2P device that should be connected to. Currently this is the only
	 * way to create or join a group.
	 *
	 * Since: 1.16
	 */
	/* ---keyfile---
	 * property: peer
	 * format: usual hex-digits-and-colons notation
	 * description: MAC address in traditional hex-digits-and-colons notation
	 *   (e.g. 00:22:68:12:79:A2), or semicolon separated list of 6 bytes (obsolete)
	 *   (e.g. 0;34;104;18;121;162).
	 * ---end---
	 */
	obj_properties[PROP_PEER] =
	    g_param_spec_string (NM_SETTING_WIFI_P2P_PEER, "", "",
	                         NULL,
	                         G_PARAM_READWRITE |
	                         G_PARAM_STATIC_STRINGS);

	/**
	 * NMSettingWifiP2P:wps-method:
	 *
	 * Flags indicating which mode of WPS is to be used.
	 *
	 * There's little point in changing the default setting as NetworkManager will
	 * automatically determine the best method to use.
	 *
	 * Since: 1.16
	 */
	obj_properties[PROP_WPS_METHOD] =
	    g_param_spec_uint (NM_SETTING_WIFI_P2P_WPS_METHOD, "", "",
	                       0,
	                       G_MAXUINT32,
	                       NM_SETTING_WIRELESS_SECURITY_WPS_METHOD_DEFAULT,
	                       G_PARAM_READWRITE |
	                       NM_SETTING_PARAM_FUZZY_IGNORE |
	                       G_PARAM_STATIC_STRINGS);

	/**
	 * NMSettingWifiP2P:wfd-ies:
	 *
	 * The Wi-Fi Display (WFD) Information Elements (IEs) to set.
	 *
	 * Wi-Fi Display requires a protocol specific information element to be
	 * set in certain Wi-Fi frames. These can be specified here for the
	 * purpose of establishing a connection.
	 * This setting is only useful when implementing a Wi-Fi Display client.
	 *
	 * Since: 1.16
	 */
	obj_properties[PROP_WFD_IES] =
	    g_param_spec_boxed (NM_SETTING_WIFI_P2P_WFD_IES, "", "",
	                        G_TYPE_BYTES,
	                        G_PARAM_READWRITE |
	                        NM_SETTING_PARAM_FUZZY_IGNORE |
	                        G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);

	_nm_setting_class_commit (setting_class, NM_META_SETTING_TYPE_WIFI_P2P);
}