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"
32 * SECTION:nm-setting-gsm
33 * @short_description: Describes GSM/3GPP-based mobile broadband properties
34 * @include: nm-setting-gsm.h
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.
42 * nm_setting_gsm_error_quark:
44 * Registers an error quark for #NMSettingGsm if necessary.
46 * Returns: the error quark used for #NMSettingGsm errors.
49 nm_setting_gsm_error_quark (void)
53 if (G_UNLIKELY (!quark))
54 quark = g_quark_from_static_string ("nm-setting-gsm-error-quark");
59 G_DEFINE_TYPE_WITH_CODE (NMSettingGsm, nm_setting_gsm, NM_TYPE_SETTING,
60 _nm_register_setting (NM_SETTING_GSM_SETTING_NAME,
63 NM_SETTING_GSM_ERROR))
64 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_GSM)
66 #define NM_SETTING_GSM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_GSM, NMSettingGsmPrivate))
69 char *number; /* For dialing, duh */
72 NMSettingSecretFlags password_flags;
74 char *apn; /* NULL for dynamic */
75 char *network_id; /* for manual registration or NULL for automatic */
76 int network_type; /* One of the NM_SETTING_GSM_NETWORK_TYPE_* */
77 guint32 allowed_bands; /* A bitfield of NM_SETTING_GSM_BAND_* */
80 NMSettingSecretFlags pin_flags;
83 } NMSettingGsmPrivate;
103 * nm_setting_gsm_new:
105 * Creates a new #NMSettingGsm object with default values.
107 * Returns: the new empty #NMSettingGsm object
110 nm_setting_gsm_new (void)
112 return (NMSetting *) g_object_new (NM_TYPE_SETTING_GSM, NULL);
116 * nm_setting_gsm_get_number:
117 * @setting: the #NMSettingGsm
119 * Returns: the #NMSettingGsm:number property of the setting
122 nm_setting_gsm_get_number (NMSettingGsm *setting)
124 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
126 return NM_SETTING_GSM_GET_PRIVATE (setting)->number;
130 * nm_setting_gsm_get_username:
131 * @setting: the #NMSettingGsm
133 * Returns: the #NMSettingGsm:username property of the setting
136 nm_setting_gsm_get_username (NMSettingGsm *setting)
138 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
140 return NM_SETTING_GSM_GET_PRIVATE (setting)->username;
144 * nm_setting_gsm_get_password:
145 * @setting: the #NMSettingGsm
147 * Returns: the #NMSettingGsm:password property of the setting
150 nm_setting_gsm_get_password (NMSettingGsm *setting)
152 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
154 return NM_SETTING_GSM_GET_PRIVATE (setting)->password;
158 * nm_setting_gsm_get_password_flags:
159 * @setting: the #NMSettingGsm
161 * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingGsm:password
164 nm_setting_gsm_get_password_flags (NMSettingGsm *setting)
166 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NM_SETTING_SECRET_FLAG_NONE);
168 return NM_SETTING_GSM_GET_PRIVATE (setting)->password_flags;
172 * nm_setting_gsm_get_apn:
173 * @setting: the #NMSettingGsm
175 * Returns: the #NMSettingGsm:apn property of the setting
178 nm_setting_gsm_get_apn (NMSettingGsm *setting)
180 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
182 return NM_SETTING_GSM_GET_PRIVATE (setting)->apn;
186 * nm_setting_gsm_get_network_id:
187 * @setting: the #NMSettingGsm
189 * Returns: the #NMSettingGsm:network-id property of the setting
192 nm_setting_gsm_get_network_id (NMSettingGsm *setting)
194 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
196 return NM_SETTING_GSM_GET_PRIVATE (setting)->network_id;
200 * nm_setting_gsm_get_network_type:
201 * @setting: the #NMSettingGsm
203 * Returns: the #NMSettingGsm:network-type property of the setting
205 * Deprecated: 0.9.10: No longer used. Network type setting should be done talking to ModemManager directly.
208 nm_setting_gsm_get_network_type (NMSettingGsm *setting)
210 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), -1);
212 return NM_SETTING_GSM_GET_PRIVATE (setting)->network_type;
216 * nm_setting_gsm_get_allowed_bands:
217 * @setting: the #NMSettingGsm
219 * Returns: the #NMSettingGsm:allowed-bands property of the setting
221 * Deprecated: 0.9.10: No longer used. Bands setting should be done talking to ModemManager directly.
224 nm_setting_gsm_get_allowed_bands (NMSettingGsm *setting)
226 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NM_SETTING_GSM_BAND_UNKNOWN);
228 return NM_SETTING_GSM_GET_PRIVATE (setting)->allowed_bands;
232 * nm_setting_gsm_get_pin:
233 * @setting: the #NMSettingGsm
235 * Returns: the #NMSettingGsm:pin property of the setting
238 nm_setting_gsm_get_pin (NMSettingGsm *setting)
240 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NULL);
242 return NM_SETTING_GSM_GET_PRIVATE (setting)->pin;
246 * nm_setting_gsm_get_pin_flags:
247 * @setting: the #NMSettingGsm
249 * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingGsm:pin
252 nm_setting_gsm_get_pin_flags (NMSettingGsm *setting)
254 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), NM_SETTING_SECRET_FLAG_NONE);
256 return NM_SETTING_GSM_GET_PRIVATE (setting)->pin_flags;
260 * nm_setting_gsm_get_home_only:
261 * @setting: the #NMSettingGsm
263 * Returns: the #NMSettingGsm:home-only property of the setting
266 nm_setting_gsm_get_home_only (NMSettingGsm *setting)
268 g_return_val_if_fail (NM_IS_SETTING_GSM (setting), FALSE);
270 return NM_SETTING_GSM_GET_PRIVATE (setting)->home_only;
274 verify (NMSetting *setting, GSList *all_settings, GError **error)
276 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (setting);
278 if (priv->number && !priv->number[0]) {
279 g_set_error_literal (error,
280 NM_SETTING_GSM_ERROR,
281 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
282 _("property is empty"));
283 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NUMBER);
288 guint32 apn_len = strlen (priv->apn);
291 if (apn_len < 1 || apn_len > 64) {
293 NM_SETTING_GSM_ERROR,
294 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
295 _("property value '%s' is empty or too long (>64)"),
297 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_APN);
301 /* APNs roughly follow the same rules as DNS domain names. Allowed
302 * characters are a-z, 0-9, . and -. GSM 03.03 Section 9.1 states:
304 * The syntax of the APN shall follow the Name Syntax defined in
305 * RFC 2181 [14] and RFC 1035 [15]. The APN consists of one or
306 * more labels. Each label is coded as one octet length field
307 * followed by that number of octets coded as 8 bit ASCII characters.
308 * Following RFC 1035 [15] the labels should consist only of the
309 * alphabetic characters (A-Z and a-z), digits (0-9) and the
310 * dash (-). The case of alphabetic characters is not significant.
312 * A dot (.) is commonly used to separate parts of the APN, and
313 * apparently the underscore (_) is used as well. RFC 2181 indicates
314 * that no restrictions of any kind are placed on DNS labels, and thus
315 * it would appear that none are placed on APNs either, but many modems
316 * and networks will fail to accept APNs that include odd characters
317 * like space ( ) and such.
319 for (i = 0; i < apn_len; i++) {
320 if ( !g_ascii_isalnum (priv->apn[i])
321 && (priv->apn[i] != '.')
322 && (priv->apn[i] != '_')
323 && (priv->apn[i] != '-')) {
325 NM_SETTING_GSM_ERROR,
326 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
327 _("'%s' contains invalid char(s) (use [A-Za-z._-])"),
329 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_APN);
335 if (priv->username && !strlen (priv->username)) {
336 g_set_error_literal (error,
337 NM_SETTING_GSM_ERROR,
338 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
339 _("property is empty"));
340 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_USERNAME);
344 if (priv->network_id) {
345 guint32 nid_len = strlen (priv->network_id);
348 /* Accept both 5 and 6 digit MCC/MNC codes */
349 if ((nid_len < 5) || (nid_len > 6)) {
351 NM_SETTING_GSM_ERROR,
352 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
353 _("'%s' length is invalid (should be 5 or 6 digits)"),
355 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NETWORK_ID);
359 for (i = 0; i < nid_len; i++) {
360 if (!g_ascii_isdigit (priv->network_id[i])) {
362 NM_SETTING_GSM_ERROR,
363 NM_SETTING_GSM_ERROR_INVALID_PROPERTY,
364 _("'%s' is not a number"),
366 g_prefix_error (error, "%s.%s: ", NM_SETTING_GSM_SETTING_NAME, NM_SETTING_GSM_NETWORK_ID);
376 need_secrets (NMSetting *setting)
378 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (setting);
379 GPtrArray *secrets = NULL;
381 if (priv->password && *priv->password)
384 if (priv->username) {
385 if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
386 secrets = g_ptr_array_sized_new (1);
387 g_ptr_array_add (secrets, NM_SETTING_GSM_PASSWORD);
395 nm_setting_gsm_init (NMSettingGsm *setting)
400 finalize (GObject *object)
402 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (object);
404 g_free (priv->number);
405 g_free (priv->username);
406 g_free (priv->password);
408 g_free (priv->network_id);
411 G_OBJECT_CLASS (nm_setting_gsm_parent_class)->finalize (object);
415 set_property (GObject *object, guint prop_id,
416 const GValue *value, GParamSpec *pspec)
418 NMSettingGsmPrivate *priv = NM_SETTING_GSM_GET_PRIVATE (object);
423 g_free (priv->number);
424 priv->number = g_value_dup_string (value);
427 g_free (priv->username);
428 priv->username = g_value_dup_string (value);
431 g_free (priv->password);
432 priv->password = g_value_dup_string (value);
434 case PROP_PASSWORD_FLAGS:
435 priv->password_flags = g_value_get_uint (value);
440 tmp = g_value_dup_string (value);
442 priv->apn = g_strstrip (tmp);
444 case PROP_NETWORK_ID:
445 g_free (priv->network_id);
446 priv->network_id = NULL;
447 tmp = g_value_dup_string (value);
449 priv->network_id = g_strstrip (tmp);
451 case PROP_NETWORK_TYPE:
452 priv->network_type = g_value_get_int (value);
454 case PROP_ALLOWED_BANDS:
455 priv->allowed_bands = g_value_get_uint (value);
459 priv->pin = g_value_dup_string (value);
462 priv->pin_flags = g_value_get_uint (value);
465 priv->home_only = g_value_get_boolean (value);
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
474 get_property (GObject *object, guint prop_id,
475 GValue *value, GParamSpec *pspec)
477 NMSettingGsm *setting = NM_SETTING_GSM (object);
481 g_value_set_string (value, nm_setting_gsm_get_number (setting));
484 g_value_set_string (value, nm_setting_gsm_get_username (setting));
487 g_value_set_string (value, nm_setting_gsm_get_password (setting));
489 case PROP_PASSWORD_FLAGS:
490 g_value_set_uint (value, nm_setting_gsm_get_password_flags (setting));
493 g_value_set_string (value, nm_setting_gsm_get_apn (setting));
495 case PROP_NETWORK_ID:
496 g_value_set_string (value, nm_setting_gsm_get_network_id (setting));
498 case PROP_NETWORK_TYPE:
499 g_value_set_int (value, NM_SETTING_GSM_GET_PRIVATE (setting)->network_type);
501 case PROP_ALLOWED_BANDS:
502 g_value_set_uint (value, NM_SETTING_GSM_GET_PRIVATE (setting)->allowed_bands);
505 g_value_set_string (value, nm_setting_gsm_get_pin (setting));
508 g_value_set_uint (value, nm_setting_gsm_get_pin_flags (setting));
511 g_value_set_boolean (value, nm_setting_gsm_get_home_only (setting));
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
520 nm_setting_gsm_class_init (NMSettingGsmClass *setting_class)
522 GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
523 NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
525 g_type_class_add_private (setting_class, sizeof (NMSettingGsmPrivate));
527 /* virtual methods */
528 object_class->set_property = set_property;
529 object_class->get_property = get_property;
530 object_class->finalize = finalize;
531 parent_class->verify = verify;
532 parent_class->need_secrets = need_secrets;
537 * NMSettingGsm:number:
539 * Number to dial when establishing a PPP data session with the GSM-based
540 * mobile broadband network. Many modems do not require PPP for connections
541 * to the mobile network and thus this property should be left blank, which
542 * allows NetworkManager to select the appropriate settings automatically.
544 g_object_class_install_property
545 (object_class, PROP_NUMBER,
546 g_param_spec_string (NM_SETTING_GSM_NUMBER, "", "",
549 G_PARAM_STATIC_STRINGS));
552 * NMSettingGsm:username:
554 * The username used to authenticate with the network, if required. Many
555 * providers do not require a username, or accept any username. But if a
556 * username is required, it is specified here.
558 g_object_class_install_property
559 (object_class, PROP_USERNAME,
560 g_param_spec_string (NM_SETTING_GSM_USERNAME, "", "",
563 G_PARAM_STATIC_STRINGS));
566 * NMSettingGsm:password:
568 * The password used to authenticate with the network, if required. Many
569 * providers do not require a password, or accept any password. But if a
570 * password is required, it is specified here.
572 g_object_class_install_property
573 (object_class, PROP_PASSWORD,
574 g_param_spec_string (NM_SETTING_GSM_PASSWORD, "", "",
577 NM_SETTING_PARAM_SECRET |
578 G_PARAM_STATIC_STRINGS));
581 * NMSettingGsm:password-flags:
583 * Flags indicating how to handle the #NMSettingGsm:password property.
585 g_object_class_install_property
586 (object_class, PROP_PASSWORD_FLAGS,
587 g_param_spec_uint (NM_SETTING_GSM_PASSWORD_FLAGS, "", "",
588 NM_SETTING_SECRET_FLAG_NONE,
589 NM_SETTING_SECRET_FLAGS_ALL,
590 NM_SETTING_SECRET_FLAG_NONE,
592 G_PARAM_STATIC_STRINGS));
597 * The GPRS Access Point Name specifying the APN used when establishing a
598 * data session with the GSM-based network. The APN often determines how
599 * the user will be billed for their network usage and whether the user has
600 * access to the Internet or just a provider-specific walled-garden, so it
601 * is important to use the correct APN for the user's mobile broadband plan.
602 * The APN may only be composed of the characters a-z, 0-9, ., and - per GSM
603 * 03.60 Section 14.9.
605 g_object_class_install_property
606 (object_class, PROP_APN,
607 g_param_spec_string (NM_SETTING_GSM_APN, "", "",
610 G_PARAM_STATIC_STRINGS));
613 * NMSettingGsm:network-id:
615 * The Network ID (GSM LAI format, ie MCC-MNC) to force specific network
616 * registration. If the Network ID is specified, NetworkManager will
617 * attempt to force the device to register only on the specified network.
618 * This can be used to ensure that the device does not roam when direct
619 * roaming control of the device is not otherwise possible.
621 g_object_class_install_property
622 (object_class, PROP_NETWORK_ID,
623 g_param_spec_string (NM_SETTING_GSM_NETWORK_ID, "", "",
626 G_PARAM_STATIC_STRINGS));
629 * NMSettingGsm:network-type:
631 * Network preference to force the device to only use specific network
632 * technologies. The permitted values are %NM_SETTING_GSM_NETWORK_TYPE_ANY,
633 * %NM_SETTING_GSM_NETWORK_TYPE_UMTS_HSPA,
634 * %NM_SETTING_GSM_NETWORK_TYPE_GPRS_EDGE,
635 * %NM_SETTING_GSM_NETWORK_TYPE_PREFER_UMTS_HSPA,
636 * %NM_SETTING_GSM_NETWORK_TYPE_PREFER_GPRS_EDGE,
637 * %NM_SETTING_GSM_NETWORK_TYPE_PREFER_4G, and
638 * %NM_SETTING_GSM_NETWORK_TYPE_4G. Note that not all devices allow network
639 * preference control.
641 * Deprecated: 0.9.10: No longer used. Network type setting should be done
642 * by talking to ModemManager directly.
644 g_object_class_install_property
645 (object_class, PROP_NETWORK_TYPE,
646 g_param_spec_int (NM_SETTING_GSM_NETWORK_TYPE, "", "",
647 NM_SETTING_GSM_NETWORK_TYPE_ANY,
648 NM_SETTING_GSM_NETWORK_TYPE_4G,
649 NM_SETTING_GSM_NETWORK_TYPE_ANY,
652 G_PARAM_STATIC_STRINGS));
655 * NMSettingGsm:allowed-bands:
657 * Bitfield of allowed frequency bands. Note that not all devices allow
658 * frequency band control. Permitted values are those specified by
659 * #NMSettingGsmNetworkBand.
661 * Deprecated: 0.9.10: No longer used. Band setting should be done by
662 * talking to ModemManager directly.
664 g_object_class_install_property
665 (object_class, PROP_ALLOWED_BANDS,
666 g_param_spec_uint (NM_SETTING_GSM_ALLOWED_BANDS, "", "",
667 NM_SETTING_GSM_BAND_UNKNOWN,
668 NM_SETTING_GSM_BANDS_MAX,
669 NM_SETTING_GSM_BAND_ANY,
672 G_PARAM_STATIC_STRINGS));
677 * If the SIM is locked with a PIN it must be unlocked before any other
678 * operations are requested. Specify the PIN here to allow operation of the
681 g_object_class_install_property
682 (object_class, PROP_PIN,
683 g_param_spec_string (NM_SETTING_GSM_PIN, "", "",
686 NM_SETTING_PARAM_SECRET |
687 G_PARAM_STATIC_STRINGS));
690 * NMSettingGsm:pin-flags:
692 * Flags indicating how to handle the #NMSettingGsm:pin property.
694 g_object_class_install_property
695 (object_class, PROP_PIN_FLAGS,
696 g_param_spec_uint (NM_SETTING_GSM_PIN_FLAGS, "", "",
697 NM_SETTING_SECRET_FLAG_NONE,
698 NM_SETTING_SECRET_FLAGS_ALL,
699 NM_SETTING_SECRET_FLAG_NONE,
701 G_PARAM_STATIC_STRINGS));
704 * NMSettingGsm:home-only:
706 * When %TRUE, only connections to the home network will be allowed.
707 * Connections to roaming networks will not be made.
709 g_object_class_install_property
710 (object_class, PROP_HOME_ONLY,
711 g_param_spec_boolean (NM_SETTING_GSM_HOME_ONLY, "", "",
714 G_PARAM_STATIC_STRINGS));