1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2007 - 2013 Red Hat, Inc.
20 * Copyright 2007 - 2008 Novell, Inc.
23 #include "nm-default.h"
27 #include "nm-setting-gsm.h"
29 #include "nm-setting-private.h"
30 #include "nm-core-enum-types.h"
33 * SECTION:nm-setting-gsm
34 * @short_description: Describes GSM/3GPP-based mobile broadband properties
36 * The #NMSettingGsm object is a #NMSetting subclass that describes
37 * properties that allow connections to 3GPP-based mobile broadband
38 * networks, including those using GPRS/EDGE and UMTS/HSPA technology.
41 G_DEFINE_TYPE_WITH_CODE (NMSettingGsm, nm_setting_gsm, NM_TYPE_SETTING,
42 _nm_register_setting (GSM, 1))
43 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_GSM)
45 #define NM_SETTING_GSM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_GSM, NMSettingGsmPrivate))
48 char *number; /* For dialing, duh */
51 NMSettingSecretFlags password_flags;
53 /* Restrict connection to certain devices or SIMs */
56 char *sim_operator_id;
58 char *apn; /* NULL for dynamic */
59 char *network_id; /* for manual registration or NULL for automatic */
62 NMSettingSecretFlags pin_flags;
65 } NMSettingGsmPrivate;
88 * Creates a new #NMSettingGsm object with default values.
90 * Returns: the new empty #NMSettingGsm object
93 nm_setting_gsm_new (void)
95 return (NMSetting *) g_object_new (NM_TYPE_SETTING_GSM, NULL);
99 * nm_setting_gsm_get_number:
100 * @setting: the #NMSettingGsm
102 * Returns: the #NMSettingGsm:number property of the setting
105 nm_setting_gsm_get_number (NMSettingGsm *setting)
107 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
109 return NM_SETTING_GSM_GET_PRIVATE (setting)->number;
113 * nm_setting_gsm_get_username:
114 * @setting: the #NMSettingGsm
116 * Returns: the #NMSettingGsm:username property of the setting
119 nm_setting_gsm_get_username (NMSettingGsm *setting)
121 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
123 return NM_SETTING_GSM_GET_PRIVATE (setting)->username;
127 * nm_setting_gsm_get_password:
128 * @setting: the #NMSettingGsm
130 * Returns: the #NMSettingGsm:password property of the setting
133 nm_setting_gsm_get_password (NMSettingGsm *setting)
135 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
137 return NM_SETTING_GSM_GET_PRIVATE (setting)->password;
141 * nm_setting_gsm_get_password_flags:
142 * @setting: the #NMSettingGsm
144 * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingGsm:password
147 nm_setting_gsm_get_password_flags (NMSettingGsm *setting)
149 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NM_SETTING_SECRET_FLAG_NONE);
151 return NM_SETTING_GSM_GET_PRIVATE (setting)->password_flags;
155 * nm_setting_gsm_get_apn:
156 * @setting: the #NMSettingGsm
158 * Returns: the #NMSettingGsm:apn property of the setting
161 nm_setting_gsm_get_apn (NMSettingGsm *setting)
163 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
165 return NM_SETTING_GSM_GET_PRIVATE (setting)->apn;
169 * nm_setting_gsm_get_network_id:
170 * @setting: the #NMSettingGsm
172 * Returns: the #NMSettingGsm:network-id property of the setting
175 nm_setting_gsm_get_network_id (NMSettingGsm *setting)
177 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
179 return NM_SETTING_GSM_GET_PRIVATE (setting)->network_id;
183 * nm_setting_gsm_get_pin:
184 * @setting: the #NMSettingGsm
186 * Returns: the #NMSettingGsm:pin property of the setting
189 nm_setting_gsm_get_pin (NMSettingGsm *setting)
191 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
193 return NM_SETTING_GSM_GET_PRIVATE (setting)->pin;
197 * nm_setting_gsm_get_pin_flags:
198 * @setting: the #NMSettingGsm
200 * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingGsm:pin
203 nm_setting_gsm_get_pin_flags (NMSettingGsm *setting)
205 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NM_SETTING_SECRET_FLAG_NONE);
207 return NM_SETTING_GSM_GET_PRIVATE (setting)->pin_flags;
211 * nm_setting_gsm_get_home_only:
212 * @setting: the #NMSettingGsm
214 * Returns: the #NMSettingGsm:home-only property of the setting
217 nm_setting_gsm_get_home_only (NMSettingGsm *setting)
219 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), FALSE);
221 return NM_SETTING_GSM_GET_PRIVATE (setting)->home_only;
225 * nm_setting_gsm_get_device_id:
226 * @setting: the #NMSettingGsm
228 * Returns: the #NMSettingGsm:device-id property of the setting
233 nm_setting_gsm_get_device_id (NMSettingGsm *setting)
235 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
237 return NM_SETTING_GSM_GET_PRIVATE (setting)->device_id;
241 * nm_setting_gsm_get_sim_id:
242 * @setting: the #NMSettingGsm
244 * Returns: the #NMSettingGsm:sim-id property of the setting
249 nm_setting_gsm_get_sim_id (NMSettingGsm *setting)
251 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
253 return NM_SETTING_GSM_GET_PRIVATE (setting)->sim_id;
257 * nm_setting_gsm_get_sim_operator_id:
258 * @setting: the #NMSettingGsm
260 * Returns: the #NMSettingGsm:sim-operator-id property of the setting
265 nm_setting_gsm_get_sim_operator_id (NMSettingGsm *setting)
267 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
269 return NM_SETTING_GSM_GET_PRIVATE (setting)->sim_operator_id;
273 verify (NMSetting *setting, NMConnection *connection, GError **error)
275 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (setting);
277 if (priv->number && !priv->number[0]) {
278 g_set_error_literal (error,
280 NM_CONNECTION_ERROR_INVALID_PROPERTY,
281 _("property is empty"));
282 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NUMBER);
287 guint32 apn_len = strlen (priv->apn);
290 if (apn_len < 1 || apn_len > 64) {
293 NM_CONNECTION_ERROR_INVALID_PROPERTY,
294 _("property value '%s' is empty or too long (>64)"),
296 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_APN);
300 /* APNs roughly follow the same rules as DNS domain names. Allowed
301 * characters are a-z, 0-9, . and -. GSM 03.03 Section 9.1 states:
303 * The syntax of the APN shall follow the Name Syntax defined in
304 * RFC 2181 [14] and RFC 1035 [15]. The APN consists of one or
305 * more labels. Each label is coded as one octet length field
306 * followed by that number of octets coded as 8 bit ASCII characters.
307 * Following RFC 1035 [15] the labels should consist only of the
308 * alphabetic characters (A-Z and a-z), digits (0-9) and the
309 * dash (-). The case of alphabetic characters is not significant.
311 * A dot (.) is commonly used to separate parts of the APN, and
312 * apparently the underscore (_) is used as well. RFC 2181 indicates
313 * that no restrictions of any kind are placed on DNS labels, and thus
314 * it would appear that none are placed on APNs either, but many modems
315 * and networks will fail to accept APNs that include odd characters
316 * like space ( ) and such.
318 for (i = 0; i < apn_len; i++) {
319 if ( !g_ascii_isalnum (priv->apn[i])
320 && (priv->apn[i] != '.')
321 && (priv->apn[i] != '_')
322 && (priv->apn[i] != '-')) {
325 NM_CONNECTION_ERROR_INVALID_PROPERTY,
326 _("'%s' contains invalid char(s) (use [A-Za-z._-])"),
328 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_APN);
334 if (priv->username && !strlen (priv->username)) {
335 g_set_error_literal (error,
337 NM_CONNECTION_ERROR_INVALID_PROPERTY,
338 _("property is empty"));
339 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_USERNAME);
343 if (priv->network_id) {
344 guint32 nid_len = strlen (priv->network_id);
347 /* Accept both 5 and 6 digit MCC/MNC codes */
348 if ((nid_len < 5) || (nid_len > 6)) {
351 NM_CONNECTION_ERROR_INVALID_PROPERTY,
352 _("'%s' length is invalid (should be 5 or 6 digits)"),
354 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NETWORK_ID);
358 for (i = 0; i < nid_len; i++) {
359 if (!g_ascii_isdigit (priv->network_id[i])) {
362 NM_CONNECTION_ERROR_INVALID_PROPERTY,
363 _("'%s' is not a number"),
365 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NETWORK_ID);
371 if (priv->device_id && !priv->device_id[0]) {
372 g_set_error_literal (error,
374 NM_CONNECTION_ERROR_INVALID_PROPERTY,
375 _("property is empty"));
376 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_DEVICE_ID);
380 if (priv->sim_id && !priv->sim_id[0]) {
381 g_set_error_literal (error,
383 NM_CONNECTION_ERROR_INVALID_PROPERTY,
384 _("property is empty"));
385 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_SIM_ID);
389 if (priv->sim_operator_id) {
390 size_t len = strlen (priv->sim_operator_id);
391 const char *p = priv->sim_operator_id;
393 if (len == 0 || (len != 5 && len != 6)) {
394 g_set_error_literal (error,
396 NM_CONNECTION_ERROR_INVALID_PROPERTY,
397 _("property is empty or wrong size"));
398 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_SIM_OPERATOR_ID);
403 if (!g_ascii_isdigit (*p++)) {
404 g_set_error_literal (error,
406 NM_CONNECTION_ERROR_INVALID_PROPERTY,
407 _("property must contain only digits"));
408 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_SIM_OPERATOR_ID);
418 verify_secrets (NMSetting *setting, NMConnection *connection, GError **error)
420 return _nm_setting_verify_secret_string (NM_SETTING_GSM_GET_PRIVATE (setting)->password,
421 NM_SETTING_GSM_SETTING_NAME,
422 NM_SETTING_GSM_PASSWORD,
427 need_secrets (NMSetting *setting)
429 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (setting);
430 GPtrArray *secrets = NULL;
432 if (priv->password && *priv->password)
435 if (priv->username) {
436 if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
437 secrets = g_ptr_array_sized_new (1);
438 g_ptr_array_add (secrets, NM_SETTING_GSM_PASSWORD);
446 nm_setting_gsm_init (NMSettingGsm *setting)
451 finalize (GObject *object)
453 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (object);
455 g_free (priv->number);
456 g_free (priv->username);
457 g_free (priv->password);
459 g_free (priv->network_id);
461 g_free (priv->device_id);
462 g_free (priv->sim_id);
463 g_free (priv->sim_operator_id);
465 G_OBJECT_CLASS (nm_setting_gsm_parent_class)->finalize (object);
469 set_property (GObject *object, guint prop_id,
470 const GValue *value, GParamSpec *pspec)
472 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (object);
477 g_free (priv->number);
478 priv->number = g_value_dup_string (value);
481 g_free (priv->username);
482 priv->username = g_value_dup_string (value);
485 g_free (priv->password);
486 priv->password = g_value_dup_string (value);
488 case PROP_PASSWORD_FLAGS:
489 priv->password_flags = g_value_get_flags (value);
494 tmp = g_value_dup_string (value);
496 priv->apn = g_strstrip (tmp);
498 case PROP_NETWORK_ID:
499 g_free (priv->network_id);
500 priv->network_id = NULL;
501 tmp = g_value_dup_string (value);
503 priv->network_id = g_strstrip (tmp);
507 priv->pin = g_value_dup_string (value);
510 priv->pin_flags = g_value_get_flags (value);
513 priv->home_only = g_value_get_boolean (value);
516 g_free (priv->device_id);
517 priv->device_id = g_value_dup_string (value);
520 g_free (priv->sim_id);
521 priv->sim_id = g_value_dup_string (value);
523 case PROP_SIM_OPERATOR_ID:
524 g_free (priv->sim_operator_id);
525 priv->sim_operator_id = g_value_dup_string (value);
528 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534 get_property (GObject *object, guint prop_id,
535 GValue *value, GParamSpec *pspec)
537 NMSettingGsm *setting = NM_SETTING_GSM (object);
541 g_value_set_string (value, nm_setting_gsm_get_number (setting));
544 g_value_set_string (value, nm_setting_gsm_get_username (setting));
547 g_value_set_string (value, nm_setting_gsm_get_password (setting));
549 case PROP_PASSWORD_FLAGS:
550 g_value_set_flags (value, nm_setting_gsm_get_password_flags (setting));
553 g_value_set_string (value, nm_setting_gsm_get_apn (setting));
555 case PROP_NETWORK_ID:
556 g_value_set_string (value, nm_setting_gsm_get_network_id (setting));
559 g_value_set_string (value, nm_setting_gsm_get_pin (setting));
562 g_value_set_flags (value, nm_setting_gsm_get_pin_flags (setting));
565 g_value_set_boolean (value, nm_setting_gsm_get_home_only (setting));
568 g_value_set_string (value, nm_setting_gsm_get_device_id (setting));
571 g_value_set_string (value, nm_setting_gsm_get_sim_id (setting));
573 case PROP_SIM_OPERATOR_ID:
574 g_value_set_string (value, nm_setting_gsm_get_sim_operator_id (setting));
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583 nm_setting_gsm_class_init (NMSettingGsmClass *setting_class)
585 GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
586 NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
588 g_type_class_add_private (setting_class, sizeof (NMSettingGsmPrivate));
590 /* virtual methods */
591 object_class->set_property = set_property;
592 object_class->get_property = get_property;
593 object_class->finalize = finalize;
594 parent_class->verify = verify;
595 parent_class->verify_secrets = verify_secrets;
596 parent_class->need_secrets = need_secrets;
601 * NMSettingGsm:number:
603 * Number to dial when establishing a PPP data session with the GSM-based
604 * mobile broadband network. Many modems do not require PPP for connections
605 * to the mobile network and thus this property should be left blank, which
606 * allows NetworkManager to select the appropriate settings automatically.
608 g_object_class_install_property
609 (object_class, PROP_NUMBER,
610 g_param_spec_string (NM_SETTING_GSM_NUMBER, "", "",
613 G_PARAM_STATIC_STRINGS));
616 * NMSettingGsm:username:
618 * The username used to authenticate with the network, if required. Many
619 * providers do not require a username, or accept any username. But if a
620 * username is required, it is specified here.
622 g_object_class_install_property
623 (object_class, PROP_USERNAME,
624 g_param_spec_string (NM_SETTING_GSM_USERNAME, "", "",
627 G_PARAM_STATIC_STRINGS));
630 * NMSettingGsm:password:
632 * The password used to authenticate with the network, if required. Many
633 * providers do not require a password, or accept any password. But if a
634 * password is required, it is specified here.
636 g_object_class_install_property
637 (object_class, PROP_PASSWORD,
638 g_param_spec_string (NM_SETTING_GSM_PASSWORD, "", "",
641 NM_SETTING_PARAM_SECRET |
642 G_PARAM_STATIC_STRINGS));
645 * NMSettingGsm:password-flags:
647 * Flags indicating how to handle the #NMSettingGsm:password property.
649 g_object_class_install_property
650 (object_class, PROP_PASSWORD_FLAGS,
651 g_param_spec_flags (NM_SETTING_GSM_PASSWORD_FLAGS, "", "",
652 NM_TYPE_SETTING_SECRET_FLAGS,
653 NM_SETTING_SECRET_FLAG_NONE,
655 G_PARAM_STATIC_STRINGS));
660 * The GPRS Access Point Name specifying the APN used when establishing a
661 * data session with the GSM-based network. The APN often determines how
662 * the user will be billed for their network usage and whether the user has
663 * access to the Internet or just a provider-specific walled-garden, so it
664 * is important to use the correct APN for the user's mobile broadband plan.
665 * The APN may only be composed of the characters a-z, 0-9, ., and - per GSM
666 * 03.60 Section 14.9.
668 g_object_class_install_property
669 (object_class, PROP_APN,
670 g_param_spec_string (NM_SETTING_GSM_APN, "", "",
673 G_PARAM_STATIC_STRINGS));
676 * NMSettingGsm:network-id:
678 * The Network ID (GSM LAI format, ie MCC-MNC) to force specific network
679 * registration. If the Network ID is specified, NetworkManager will
680 * attempt to force the device to register only on the specified network.
681 * This can be used to ensure that the device does not roam when direct
682 * roaming control of the device is not otherwise possible.
684 g_object_class_install_property
685 (object_class, PROP_NETWORK_ID,
686 g_param_spec_string (NM_SETTING_GSM_NETWORK_ID, "", "",
689 G_PARAM_STATIC_STRINGS));
694 * If the SIM is locked with a PIN it must be unlocked before any other
695 * operations are requested. Specify the PIN here to allow operation of the
698 g_object_class_install_property
699 (object_class, PROP_PIN,
700 g_param_spec_string (NM_SETTING_GSM_PIN, "", "",
703 NM_SETTING_PARAM_SECRET |
704 G_PARAM_STATIC_STRINGS));
707 * NMSettingGsm:pin-flags:
709 * Flags indicating how to handle the #NMSettingGsm:pin property.
711 g_object_class_install_property
712 (object_class, PROP_PIN_FLAGS,
713 g_param_spec_flags (NM_SETTING_GSM_PIN_FLAGS, "", "",
714 NM_TYPE_SETTING_SECRET_FLAGS,
715 NM_SETTING_SECRET_FLAG_NONE,
717 G_PARAM_STATIC_STRINGS));
720 * NMSettingGsm:home-only:
722 * When %TRUE, only connections to the home network will be allowed.
723 * Connections to roaming networks will not be made.
725 g_object_class_install_property
726 (object_class, PROP_HOME_ONLY,
727 g_param_spec_boolean (NM_SETTING_GSM_HOME_ONLY, "", "",
730 G_PARAM_STATIC_STRINGS));
733 * NMSettingGsm:device-id:
735 * The device unique identifier (as given by the WWAN management service)
736 * which this connection applies to. If given, the connection will only
737 * apply to the specified device.
741 g_object_class_install_property
742 (object_class, PROP_DEVICE_ID,
743 g_param_spec_string (NM_SETTING_GSM_DEVICE_ID, "", "",
746 G_PARAM_STATIC_STRINGS));
749 * NMSettingGsm:sim-id:
751 * The SIM card unique identifier (as given by the WWAN management service)
752 * which this connection applies to. If given, the connection will apply
753 * to any device also allowed by #NMSettingGsm:device-id which contains a
754 * SIM card matching the given identifier.
758 g_object_class_install_property
759 (object_class, PROP_SIM_ID,
760 g_param_spec_string (NM_SETTING_GSM_SIM_ID, "", "",
763 G_PARAM_STATIC_STRINGS));
766 * NMSettingGsm:sim-operator-id:
768 * A MCC/MNC string like "310260" or "21601" identifying the specific
769 * mobile network operator which this connection applies to. If given,
770 * the connection will apply to any device also allowed by
771 * #NMSettingGsm:device-id and #NMSettingGsm:sim-id which contains a SIM
772 * card provisioined by the given operator.
776 g_object_class_install_property
777 (object_class, PROP_SIM_OPERATOR_ID,
778 g_param_spec_string (NM_SETTING_GSM_SIM_OPERATOR_ID, "", "",
781 G_PARAM_STATIC_STRINGS));
783 /* Ignore incoming deprecated properties */
784 _nm_setting_class_add_dbus_only_property (parent_class, "allowed-bands",
785 G_VARIANT_TYPE_UINT32,
787 _nm_setting_class_add_dbus_only_property (parent_class, "network-type",
788 G_VARIANT_TYPE_INT32,