1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* NetworkManager -- Network link manager
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program 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
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
* Copyright (C) 2004 - 2011 Red Hat, Inc.
19
* Copyright (C) 2005 - 2008 Novell, Inc.
30
#include "NetworkManagerUtils.h"
32
#include "nm-logging.h"
33
#include "nm-device.h"
34
#include "nm-device-wifi.h"
35
#include "nm-device-ethernet.h"
36
#include "nm-dbus-manager.h"
37
#include "nm-dispatcher-action.h"
38
#include "nm-dbus-glib-types.h"
39
#include "nm-setting-connection.h"
40
#include "nm-setting-ip4-config.h"
41
#include "nm-setting-ip6-config.h"
42
#include "nm-setting-wireless.h"
43
#include "nm-setting-wireless-security.h"
44
#include "nm-manager-auth.h"
46
#include <netlink/addr.h>
47
#include <netinet/in.h>
50
* nm_ethernet_address_is_valid
52
* Compares an Ethernet address against known invalid addresses.
56
nm_ethernet_address_is_valid (const struct ether_addr *test_addr)
58
guint8 invalid_addr1[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
59
guint8 invalid_addr2[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
60
guint8 invalid_addr3[ETH_ALEN] = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44};
61
guint8 invalid_addr4[ETH_ALEN] = {0x00, 0x30, 0xb4, 0x00, 0x00, 0x00}; /* prism54 dummy MAC */
63
g_return_val_if_fail (test_addr != NULL, FALSE);
65
/* Compare the AP address the card has with invalid ethernet MAC addresses. */
66
if (!memcmp (test_addr->ether_addr_octet, &invalid_addr1, ETH_ALEN))
69
if (!memcmp (test_addr->ether_addr_octet, &invalid_addr2, ETH_ALEN))
72
if (!memcmp (test_addr->ether_addr_octet, &invalid_addr3, ETH_ALEN))
75
if (!memcmp (test_addr->ether_addr_octet, &invalid_addr4, ETH_ALEN))
78
if (test_addr->ether_addr_octet[0] & 1) /* Multicast addresses */
86
nm_spawn_process (const char *args)
93
g_return_val_if_fail (args != NULL, -1);
95
if (!g_shell_parse_argv (args, &num_args, &argv, &error)) {
96
nm_log_warn (LOGD_CORE, "could not parse arguments for '%s': %s", args, error->message);
101
if (!g_spawn_sync ("/", argv, NULL, 0, NULL, NULL, NULL, NULL, &status, &error)) {
102
nm_log_warn (LOGD_CORE, "could not spawn process '%s': %s", args, error->message);
103
g_error_free (error);
111
* nm_utils_ip4_netmask_to_prefix
113
* Figure out the network prefix from a netmask. Netmask
114
* MUST be in network byte order.
118
nm_utils_ip4_netmask_to_prefix (guint32 netmask)
123
p = (guchar *) &netmask;
124
end = p + sizeof (guint32);
126
while ((*p == 0xFF) && p < end) {
144
* nm_utils_ip4_prefix_to_netmask
146
* Figure out the netmask from a prefix.
150
nm_utils_ip4_prefix_to_netmask (guint32 prefix)
152
guint32 msk = 0x80000000;
161
return (guint32) htonl (netmask);
165
nm_ether_ntop (const struct ether_addr *mac)
167
/* we like leading zeros and all-caps, instead
168
* of what glibc's ether_ntop() gives us
170
return g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
171
mac->ether_addr_octet[0], mac->ether_addr_octet[1],
172
mac->ether_addr_octet[2], mac->ether_addr_octet[3],
173
mac->ether_addr_octet[4], mac->ether_addr_octet[5]);
177
nm_utils_merge_ip4_config (NMIP4Config *ip4_config, NMSettingIP4Config *setting)
182
return; /* Defaults are just fine */
184
if (nm_setting_ip4_config_get_ignore_auto_dns (setting)) {
185
nm_ip4_config_reset_nameservers (ip4_config);
186
nm_ip4_config_reset_domains (ip4_config);
187
nm_ip4_config_reset_searches (ip4_config);
190
if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
191
nm_ip4_config_reset_routes (ip4_config);
193
for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) {
195
gboolean found = FALSE;
198
ns = nm_setting_ip4_config_get_dns (setting, i);
199
for (j = 0; j < nm_ip4_config_get_num_nameservers (ip4_config); j++) {
200
if (nm_ip4_config_get_nameserver (ip4_config, j) == ns) {
207
nm_ip4_config_add_nameserver (ip4_config, ns);
210
/* DNS search domains */
211
for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) {
212
const char *search = nm_setting_ip4_config_get_dns_search (setting, i);
213
gboolean found = FALSE;
216
for (j = 0; j < nm_ip4_config_get_num_searches (ip4_config); j++) {
217
if (!strcmp (search, nm_ip4_config_get_search (ip4_config, j))) {
224
nm_ip4_config_add_search (ip4_config, search);
228
for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) {
229
NMIP4Address *setting_addr = nm_setting_ip4_config_get_address (setting, i);
232
num = nm_ip4_config_get_num_addresses (ip4_config);
233
for (j = 0; j < num; j++) {
234
NMIP4Address *cfg_addr = nm_ip4_config_get_address (ip4_config, j);
236
/* Dupe, override with user-specified address */
237
if (nm_ip4_address_get_address (cfg_addr) == nm_ip4_address_get_address (setting_addr)) {
238
nm_ip4_config_replace_address (ip4_config, j, setting_addr);
244
nm_ip4_config_add_address (ip4_config, setting_addr);
248
for (i = 0; i < nm_setting_ip4_config_get_num_routes (setting); i++) {
249
NMIP4Route *setting_route = nm_setting_ip4_config_get_route (setting, i);
252
num = nm_ip4_config_get_num_routes (ip4_config);
253
for (j = 0; j < num; j++) {
254
NMIP4Route *cfg_route = nm_ip4_config_get_route (ip4_config, j);
256
/* Dupe, override with user-specified route */
257
if ( (nm_ip4_route_get_dest (cfg_route) == nm_ip4_route_get_dest (setting_route))
258
&& (nm_ip4_route_get_prefix (cfg_route) == nm_ip4_route_get_prefix (setting_route))
259
&& (nm_ip4_route_get_next_hop (cfg_route) == nm_ip4_route_get_next_hop (setting_route))) {
260
nm_ip4_config_replace_route (ip4_config, j, setting_route);
266
nm_ip4_config_add_route (ip4_config, setting_route);
269
if (nm_setting_ip4_config_get_never_default (setting))
270
nm_ip4_config_set_never_default (ip4_config, TRUE);
273
static inline gboolean
274
ip6_addresses_equal (const struct in6_addr *a, const struct in6_addr *b)
276
return memcmp (a, b, sizeof (struct in6_addr)) == 0;
279
/* This is exactly identical to nm_utils_merge_ip4_config, with s/4/6/,
280
* except that we can't compare addresses with ==.
283
nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting)
288
return; /* Defaults are just fine */
290
if (nm_setting_ip6_config_get_ignore_auto_dns (setting)) {
291
nm_ip6_config_reset_nameservers (ip6_config);
292
nm_ip6_config_reset_domains (ip6_config);
293
nm_ip6_config_reset_searches (ip6_config);
296
if (nm_setting_ip6_config_get_ignore_auto_routes (setting))
297
nm_ip6_config_reset_routes (ip6_config);
299
for (i = 0; i < nm_setting_ip6_config_get_num_dns (setting); i++) {
300
const struct in6_addr *ns;
301
gboolean found = FALSE;
304
ns = nm_setting_ip6_config_get_dns (setting, i);
305
for (j = 0; j < nm_ip6_config_get_num_nameservers (ip6_config); j++) {
306
if (ip6_addresses_equal (nm_ip6_config_get_nameserver (ip6_config, j), ns)) {
313
nm_ip6_config_add_nameserver (ip6_config, ns);
316
/* DNS search domains */
317
for (i = 0; i < nm_setting_ip6_config_get_num_dns_searches (setting); i++) {
318
const char *search = nm_setting_ip6_config_get_dns_search (setting, i);
319
gboolean found = FALSE;
322
for (j = 0; j < nm_ip6_config_get_num_searches (ip6_config); j++) {
323
if (!strcmp (search, nm_ip6_config_get_search (ip6_config, j))) {
330
nm_ip6_config_add_search (ip6_config, search);
334
for (i = 0; i < nm_setting_ip6_config_get_num_addresses (setting); i++) {
335
NMIP6Address *setting_addr = nm_setting_ip6_config_get_address (setting, i);
338
num = nm_ip6_config_get_num_addresses (ip6_config);
339
for (j = 0; j < num; j++) {
340
NMIP6Address *cfg_addr = nm_ip6_config_get_address (ip6_config, j);
342
/* Dupe, override with user-specified address */
343
if (ip6_addresses_equal (nm_ip6_address_get_address (cfg_addr), nm_ip6_address_get_address (setting_addr))) {
344
nm_ip6_config_replace_address (ip6_config, j, setting_addr);
350
nm_ip6_config_add_address (ip6_config, setting_addr);
354
for (i = 0; i < nm_setting_ip6_config_get_num_routes (setting); i++) {
355
NMIP6Route *setting_route = nm_setting_ip6_config_get_route (setting, i);
358
num = nm_ip6_config_get_num_routes (ip6_config);
359
for (j = 0; j < num; j++) {
360
NMIP6Route *cfg_route = nm_ip6_config_get_route (ip6_config, j);
362
/* Dupe, override with user-specified route */
363
if ( ip6_addresses_equal (nm_ip6_route_get_dest (cfg_route), nm_ip6_route_get_dest (setting_route))
364
&& (nm_ip6_route_get_prefix (cfg_route) == nm_ip6_route_get_prefix (setting_route))
365
&& ip6_addresses_equal (nm_ip6_route_get_next_hop (cfg_route), nm_ip6_route_get_next_hop (setting_route))) {
366
nm_ip6_config_replace_route (ip6_config, j, setting_route);
372
nm_ip6_config_add_route (ip6_config, setting_route);
375
if (nm_setting_ip6_config_get_never_default (setting))
376
nm_ip6_config_set_never_default (ip6_config, TRUE);
380
dump_object_to_props (GObject *object, GHashTable *hash)
385
pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &len);
386
for (i = 0; i < len; i++) {
387
value_hash_add_object_property (hash,
391
pspecs[i]->value_type);
397
dump_dhcp4_to_props (NMDHCP4Config *config, GHashTable *hash)
399
GSList *options, *iter;
401
options = nm_dhcp4_config_list_options (config);
402
for (iter = options; iter; iter = g_slist_next (iter)) {
403
const char *option = (const char *) iter->data;
406
val = nm_dhcp4_config_get_option (config, option);
407
value_hash_add_str (hash, option, val);
409
g_slist_free (options);
413
dump_dhcp6_to_props (NMDHCP6Config *config, GHashTable *hash)
415
GSList *options, *iter;
417
options = nm_dhcp6_config_list_options (config);
418
for (iter = options; iter; iter = g_slist_next (iter)) {
419
const char *option = (const char *) iter->data;
422
val = nm_dhcp6_config_get_option (config, option);
423
value_hash_add_str (hash, option, val);
425
g_slist_free (options);
429
fill_device_props (NMDevice *device,
430
GHashTable *dev_hash,
431
GHashTable *ip4_hash,
432
GHashTable *ip6_hash,
433
GHashTable *dhcp4_hash,
434
GHashTable *dhcp6_hash)
436
NMIP4Config *ip4_config;
437
NMIP6Config *ip6_config;
438
NMDHCP4Config *dhcp4_config;
439
NMDHCP6Config *dhcp6_config;
441
/* If the action is for a VPN, send the VPN's IP interface instead of the device's */
442
value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_IP_INTERFACE, nm_device_get_ip_iface (device));
443
value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_INTERFACE, nm_device_get_iface (device));
444
value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_TYPE, nm_device_get_device_type (device));
445
value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_STATE, nm_device_get_state (device));
446
value_hash_add_object_path (dev_hash, NMD_DEVICE_PROPS_PATH, nm_device_get_path (device));
448
ip4_config = nm_device_get_ip4_config (device);
450
dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
452
ip6_config = nm_device_get_ip6_config (device);
454
dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
456
dhcp4_config = nm_device_get_dhcp4_config (device);
458
dump_dhcp4_to_props (dhcp4_config, dhcp4_hash);
460
dhcp6_config = nm_device_get_dhcp6_config (device);
462
dump_dhcp6_to_props (dhcp6_config, dhcp6_hash);
466
fill_vpn_props (NMIP4Config *ip4_config,
467
NMIP6Config *ip6_config,
468
GHashTable *ip4_hash,
469
GHashTable *ip6_hash)
472
dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
474
dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
478
dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
480
dbus_g_proxy_end_call (proxy, call, NULL, G_TYPE_INVALID);
481
g_object_unref (proxy);
485
nm_utils_call_dispatcher (const char *action,
486
NMConnection *connection,
488
const char *vpn_iface,
489
NMIP4Config *vpn_ip4_config,
490
NMIP6Config *vpn_ip6_config)
492
NMDBusManager *dbus_mgr;
494
DBusGConnection *g_connection;
495
GHashTable *connection_hash;
496
GHashTable *connection_props;
497
GHashTable *device_props;
498
GHashTable *device_ip4_props;
499
GHashTable *device_ip6_props;
500
GHashTable *device_dhcp4_props;
501
GHashTable *device_dhcp6_props;
502
GHashTable *vpn_ip4_props;
503
GHashTable *vpn_ip6_props;
505
g_return_if_fail (action != NULL);
507
/* All actions except 'hostname' require a device */
508
if (strcmp (action, "hostname") != 0)
509
g_return_if_fail (NM_IS_DEVICE (device));
510
/* VPN actions require at least an IPv4 config (for now) */
511
if (strcmp (action, "vpn-up") == 0)
512
g_return_if_fail (vpn_ip4_config != NULL);
514
dbus_mgr = nm_dbus_manager_get ();
515
g_connection = nm_dbus_manager_get_connection (dbus_mgr);
516
proxy = dbus_g_proxy_new_for_name (g_connection,
517
NM_DISPATCHER_DBUS_SERVICE,
518
NM_DISPATCHER_DBUS_PATH,
519
NM_DISPATCHER_DBUS_IFACE);
521
nm_log_err (LOGD_CORE, "could not get dispatcher proxy!");
522
g_object_unref (dbus_mgr);
527
connection_hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_NO_SECRETS);
529
connection_props = value_hash_create ();
532
value_hash_add_object_path (connection_props,
533
NMD_CONNECTION_PROPS_PATH,
534
nm_connection_get_path (connection));
536
connection_hash = value_hash_create ();
537
connection_props = value_hash_create ();
540
device_props = value_hash_create ();
541
device_ip4_props = value_hash_create ();
542
device_ip6_props = value_hash_create ();
543
device_dhcp4_props = value_hash_create ();
544
device_dhcp6_props = value_hash_create ();
545
vpn_ip4_props = value_hash_create ();
546
vpn_ip6_props = value_hash_create ();
548
/* hostname actions only send the hostname */
549
if (strcmp (action, "hostname") != 0) {
550
fill_device_props (device,
557
fill_vpn_props (vpn_ip4_config, NULL, vpn_ip4_props, vpn_ip6_props);
560
/* Do a non-blocking call, but wait for the reply, because dbus-glib
561
* sometimes needs time to complete internal housekeeping. If we use
562
* dbus_g_proxy_call_no_reply(), that housekeeping (specifically the
563
* GetNameOwner response) doesn't complete and we run into an assert
564
* on unreffing the proxy.
566
dbus_g_proxy_begin_call_with_timeout (proxy, "Action",
568
dbus_mgr, /* automatically unref the dbus mgr when call is done */
571
G_TYPE_STRING, action,
572
DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash,
573
DBUS_TYPE_G_MAP_OF_VARIANT, connection_props,
574
DBUS_TYPE_G_MAP_OF_VARIANT, device_props,
575
DBUS_TYPE_G_MAP_OF_VARIANT, device_ip4_props,
576
DBUS_TYPE_G_MAP_OF_VARIANT, device_ip6_props,
577
DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp4_props,
578
DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp6_props,
579
G_TYPE_STRING, vpn_iface ? vpn_iface : "",
580
DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip4_props,
581
DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip6_props,
583
g_hash_table_destroy (connection_hash);
584
g_hash_table_destroy (connection_props);
585
g_hash_table_destroy (device_props);
586
g_hash_table_destroy (device_ip4_props);
587
g_hash_table_destroy (device_ip6_props);
588
g_hash_table_destroy (device_dhcp4_props);
589
g_hash_table_destroy (device_dhcp6_props);
590
g_hash_table_destroy (vpn_ip4_props);
591
g_hash_table_destroy (vpn_ip6_props);
595
nm_match_spec_hwaddr (const GSList *specs, const char *hwaddr)
598
char *hwaddr_match, *p;
600
g_return_val_if_fail (hwaddr != NULL, FALSE);
602
p = hwaddr_match = g_strdup_printf ("mac:%s", hwaddr);
605
*p = g_ascii_tolower (*p);
609
for (iter = specs; iter; iter = g_slist_next (iter)) {
610
if (!strcmp ((const char *) iter->data, hwaddr_match)) {
611
g_free (hwaddr_match);
616
g_free (hwaddr_match);
623
parse_subchannels (const char *subchannels, guint32 *a, guint32 *b, guint32 *c)
625
long unsigned int tmp;
626
char buf[BUFSIZE + 1];
627
const char *p = subchannels;
629
char *pa = NULL, *pb = NULL, *pc = NULL;
631
g_return_val_if_fail (subchannels != NULL, FALSE);
632
g_return_val_if_fail (a != NULL, FALSE);
633
g_return_val_if_fail (*a == 0, FALSE);
634
g_return_val_if_fail (b != NULL, FALSE);
635
g_return_val_if_fail (*b == 0, FALSE);
636
g_return_val_if_fail (c != NULL, FALSE);
637
g_return_val_if_fail (*c == 0, FALSE);
640
if (!isxdigit (subchannels[0]))
643
/* Get the first channel */
644
while (*p && (*p != ',')) {
645
if (!isxdigit (*p) && (*p != '.'))
646
return FALSE; /* Invalid chars */
648
return FALSE; /* Too long to be a subchannel */
653
/* and grab each of its elements, there should be 3 */
655
pb = strchr (buf, '.');
657
pc = strchr (pb + 1, '.');
658
if (!pa || !pb || !pc)
661
/* Split the string */
666
tmp = strtoul (pa, NULL, 16);
672
tmp = strtoul (pb, NULL, 16);
678
tmp = strtoul (pc, NULL, 16);
686
#define SUBCHAN_TAG "s390-subchannels:"
689
nm_match_spec_s390_subchannels (const GSList *specs, const char *subchannels)
692
guint32 a = 0, b = 0, c = 0;
693
guint32 spec_a = 0, spec_b = 0, spec_c = 0;
695
g_return_val_if_fail (subchannels != NULL, FALSE);
697
if (!parse_subchannels (subchannels, &a, &b, &c))
700
for (iter = specs; iter; iter = g_slist_next (iter)) {
701
const char *spec = iter->data;
703
if (!strncmp (spec, SUBCHAN_TAG, strlen (SUBCHAN_TAG))) {
704
spec += strlen (SUBCHAN_TAG);
705
if (parse_subchannels (spec, &spec_a, &spec_b, &spec_c)) {
706
if (a == spec_a && b == spec_b && c == spec_c)
716
nm_utils_get_shared_wifi_permission (NMConnection *connection)
718
NMSettingWireless *s_wifi;
719
NMSettingWirelessSecurity *s_wsec;
720
NMSettingIP4Config *s_ip4;
721
const char *method = NULL;
723
s_ip4 = nm_connection_get_setting_ip4_config (connection);
725
method = nm_setting_ip4_config_get_method (s_ip4);
727
if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0)
728
return NULL; /* Not shared */
730
s_wifi = nm_connection_get_setting_wireless (connection);
732
s_wsec = nm_connection_get_setting_wireless_security (connection);
733
if (nm_setting_wireless_get_security (s_wifi) || s_wsec)
734
return NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED;
736
return NM_AUTH_PERMISSION_WIFI_SHARE_OPEN;
742
/*********************************/
745
nm_gvalue_destroy (gpointer data)
747
GValue *value = (GValue *) data;
749
g_value_unset (value);
750
g_slice_free (GValue, value);
754
value_hash_create (void)
756
return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, nm_gvalue_destroy);
760
value_hash_add (GHashTable *hash,
764
g_hash_table_insert (hash, g_strdup (key), value);
768
value_hash_add_str (GHashTable *hash,
774
value = g_slice_new0 (GValue);
775
g_value_init (value, G_TYPE_STRING);
776
g_value_set_string (value, str);
778
value_hash_add (hash, key, value);
782
value_hash_add_object_path (GHashTable *hash,
788
value = g_slice_new0 (GValue);
789
g_value_init (value, DBUS_TYPE_G_OBJECT_PATH);
790
g_value_set_boxed (value, op);
792
value_hash_add (hash, key, value);
796
value_hash_add_uint (GHashTable *hash,
802
value = g_slice_new0 (GValue);
803
g_value_init (value, G_TYPE_UINT);
804
g_value_set_uint (value, val);
806
value_hash_add (hash, key, value);
810
value_hash_add_bool (GHashTable *hash,
816
value = g_slice_new0 (GValue);
817
g_value_init (value, G_TYPE_BOOLEAN);
818
g_value_set_boolean (value, val);
820
value_hash_add (hash, key, value);
824
value_hash_add_object_property (GHashTable *hash,
832
value = g_slice_new0 (GValue);
833
g_value_init (value, val_type);
834
g_object_get_property (object, prop, value);
835
value_hash_add (hash, key, value);
839
nm_utils_do_sysctl (const char *path, const char *value)
841
int fd, len, nwrote, total;
843
fd = open (path, O_WRONLY | O_TRUNC);
847
len = strlen (value);
850
nwrote = write (fd, value + total, len - total);
858
} while (total < len);
865
nm_utils_get_proc_sys_net_value (const char *path,
869
GError *error = NULL;
870
char *contents = NULL;
871
gboolean success = FALSE;
874
if (!g_file_get_contents (path, &contents, NULL, &error)) {
875
nm_log_dbg (LOGD_DEVICE, "(%s): error reading %s: (%d) %s",
877
error ? error->code : -1,
878
error && error->message ? error->message : "(unknown)");
879
g_clear_error (&error);
882
tmp = strtol (contents, NULL, 10);
883
if ((errno == 0) && (tmp == 0 || tmp == 1)) {
884
*out_value = (guint32) tmp;
894
get_new_connection_name (const GSList *existing,
896
const char *preferred)
898
GSList *names = NULL;
902
gboolean preferred_found = FALSE;
904
for (iter = existing; iter; iter = g_slist_next (iter)) {
905
NMConnection *candidate = NM_CONNECTION (iter->data);
908
id = nm_connection_get_id (candidate);
910
names = g_slist_append (names, (gpointer) id);
912
if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
913
preferred_found = TRUE;
916
/* Return the preferred name if it was unique */
917
if (preferred && !preferred_found) {
918
g_slist_free (names);
919
return g_strdup (preferred);
922
/* Otherwise find the next available unique connection name using the given
923
* connection name template.
925
while (!cname && (i++ < 10000)) {
927
gboolean found = FALSE;
929
temp = g_strdup_printf (format, i);
930
for (iter = names; iter; iter = g_slist_next (iter)) {
931
if (!strcmp (iter->data, temp)) {
942
g_slist_free (names);
947
nm_utils_complete_generic (NMConnection *connection,
949
const GSList *existing,
951
const char *preferred,
952
gboolean default_enable_ipv6)
954
NMSettingConnection *s_con;
955
NMSettingIP4Config *s_ip4;
956
NMSettingIP6Config *s_ip6;
960
s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
962
s_con = (NMSettingConnection *) nm_setting_connection_new ();
963
nm_connection_add_setting (connection, NM_SETTING (s_con));
965
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL);
967
if (!nm_setting_connection_get_uuid (s_con)) {
968
uuid = nm_utils_uuid_generate ();
969
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL);
973
/* Add a connection ID if absent */
974
if (!nm_setting_connection_get_id (s_con)) {
975
id = get_new_connection_name (existing, format, preferred);
976
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
980
/* Add an 'auto' IPv4 connection if present */
981
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
983
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
984
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
986
method = nm_setting_ip4_config_get_method (s_ip4);
988
g_object_set (G_OBJECT (s_ip4),
989
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
993
/* Add an 'auto' IPv6 setting if allowed and not preset */
994
s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
995
if (!s_ip6 && default_enable_ipv6) {
996
s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
997
nm_connection_add_setting (connection, NM_SETTING (s_ip6));
999
if (s_ip6 && !nm_setting_ip6_config_get_method (s_ip6)) {
1000
g_object_set (G_OBJECT (s_ip6),
1001
NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
1002
NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,