static void
test_wins_options (void)
{
	GHashTable *options;
	NMIP4Config *ip4_config;
	NMIP4Address *addr;
	struct in_addr tmp;
	const char *expected_wins1 = "63.12.199.5";
	const char *expected_wins2 = "150.4.88.120";

	options = fill_table (generic_options, NULL);
	options = fill_table (wins_options, options);

	ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options);
	ASSERT (ip4_config != NULL,
	        "dhcp-wins", "failed to parse DHCP4 options");

	/* IP4 address */
	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        "dhcp-wins", "unexpected number of IP addresses");
	addr = nm_ip4_config_get_address (ip4_config, 0);

	ASSERT (nm_ip4_config_get_num_wins (ip4_config) == 2,
	        "dhcp-wins", "unexpected number of WINS servers");
	ASSERT (inet_pton (AF_INET, expected_wins1, &tmp) > 0,
	        "dhcp-wins", "couldn't convert expected WINS server address #1");
	ASSERT (nm_ip4_config_get_wins (ip4_config, 0) == tmp.s_addr,
	        "dhcp-wins", "unexpected WINS server #1");
	ASSERT (inet_pton (AF_INET, expected_wins2, &tmp) > 0,
	        "dhcp-wins", "couldn't convert expected WINS server address #1");
	ASSERT (nm_ip4_config_get_wins (ip4_config, 1) == tmp.s_addr,
	        "dhcp-wins", "unexpected WINS server #1");

	g_hash_table_destroy (options);
}
static void
test_ip4_missing_prefix (const char *client, const char *ip, guint32 expected_prefix)
{
	GHashTable *options;
	NMIP4Config *ip4_config;
	NMIP4Address *addr;

	options = fill_table (generic_options, NULL);
	g_hash_table_insert (options, "new_ip_address", string_to_byte_array_gvalue (ip));
	g_hash_table_remove (options, "new_subnet_mask");

	ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
	ASSERT (ip4_config != NULL,
	        "dhcp-ip4-missing-prefix", "failed to parse DHCP4 options");

	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        "dhcp-ip4-missing-prefix", "unexpected number of IP4 addresses (not 1)");

	addr = nm_ip4_config_get_address (ip4_config, 0);
	ASSERT (addr != NULL,
	        "dhcp-ip4-missing-prefix", "missing IP4 address #1");

	ASSERT (nm_ip4_address_get_prefix (addr) == expected_prefix,
	        "dhcp-ip4-missing-prefix", "unexpected IP4 address prefix %d (expected %d)",
	        nm_ip4_address_get_prefix (addr), expected_prefix);

	g_hash_table_destroy (options);
}
static void
test_ip4_prefix_classless (const char *client)
{
	GHashTable *options;
	NMIP4Config *ip4_config;
	NMIP4Address *addr;

	/* Ensure that the missing-subnet-mask handler doesn't mangle classless
	 * subnet masks at all.  The handler should trigger only if the server
	 * doesn't send the subnet mask.
	 */

	options = fill_table (generic_options, NULL);
	g_hash_table_insert (options, "new_ip_address", string_to_byte_array_gvalue ("172.16.54.22"));
	g_hash_table_insert (options, "new_subnet_mask", string_to_byte_array_gvalue ("255.255.252.0"));

	ip4_config = nm_dhcp_manager_test_ip4_options_to_config (client, "eth0", options, "rebind");
	ASSERT (ip4_config != NULL,
	        "dhcp-ip4-prefix-classless", "failed to parse DHCP4 options");

	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        "dhcp-ip4-prefix-classless", "unexpected number of IP4 addresses (not 1)");

	addr = nm_ip4_config_get_address (ip4_config, 0);
	ASSERT (addr != NULL,
	        "dhcp-ip4-prefix-classless", "missing IP4 address #1");

	ASSERT (nm_ip4_address_get_prefix (addr) == 22,
	        "dhcp-ip4-prefix-classless", "unexpected IP4 address prefix %d (expected 22)",
	        nm_ip4_address_get_prefix (addr));

	g_hash_table_destroy (options);
}
Exemplo n.º 4
0
void
nm_ip4_config_hash (NMIP4Config *config, GChecksum *sum, gboolean dns_only)
{
	guint32 i, n;
	const char *s;

	g_return_if_fail (config != NULL);
	g_return_if_fail (sum != NULL);

	if (dns_only == FALSE) {
		for (i = 0; i < nm_ip4_config_get_num_addresses (config); i++) {
			NMIP4Address *a = nm_ip4_config_get_address (config, i);

			hash_u32 (sum, nm_ip4_address_get_address (a));
			hash_u32 (sum, nm_ip4_address_get_prefix (a));
			hash_u32 (sum, nm_ip4_address_get_gateway (a));
		}

		for (i = 0; i < nm_ip4_config_get_num_routes (config); i++) {
			NMIP4Route *r = nm_ip4_config_get_route (config, i);

			hash_u32 (sum, nm_ip4_route_get_dest (r));
			hash_u32 (sum, nm_ip4_route_get_prefix (r));
			hash_u32 (sum, nm_ip4_route_get_next_hop (r));
			hash_u32 (sum, nm_ip4_route_get_metric (r));
		}

		n = nm_ip4_config_get_ptp_address (config);
		if (n)
			hash_u32 (sum, n);

		for (i = 0; i < nm_ip4_config_get_num_nis_servers (config); i++)
			hash_u32 (sum, nm_ip4_config_get_nis_server (config, i));

		s = nm_ip4_config_get_nis_domain (config);
		if (s)
			g_checksum_update (sum, (const guint8 *) s, strlen (s));
	}

	for (i = 0; i < nm_ip4_config_get_num_nameservers (config); i++)
		hash_u32 (sum, nm_ip4_config_get_nameserver (config, i));

	for (i = 0; i < nm_ip4_config_get_num_wins (config); i++)
		hash_u32 (sum, nm_ip4_config_get_wins (config, i));

	for (i = 0; i < nm_ip4_config_get_num_domains (config); i++) {
		s = nm_ip4_config_get_domain (config, i);
		g_checksum_update (sum, (const guint8 *) s, strlen (s));
	}

	for (i = 0; i < nm_ip4_config_get_num_searches (config); i++) {
		s = nm_ip4_config_get_search (config, i);
		g_checksum_update (sum, (const guint8 *) s, strlen (s));
	}
}
static void
test_gateway_in_classless_routes (void)
{
	GHashTable *options;
	NMIP4Config *ip4_config;
	NMIP4Address *addr;
	NMIP4Route *route;
	struct in_addr tmp;
	const char *expected_route1_dest = "192.168.10.0";
	const char *expected_route1_gw = "192.168.1.1";
	const char *expected_gateway = "192.2.3.4";

	options = fill_table (generic_options, NULL);
	options = fill_table (gw_in_classless_routes, options);

	ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options);
	ASSERT (ip4_config != NULL,
	        "dhcp-rfc3442-gateway", "failed to parse DHCP4 options");

	/* IP4 routes */
	ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 1,
	        "dhcp-rfc3442-gateway", "unexpected number of IP routes");

	/* Route #1 */
	route = nm_ip4_config_get_route (ip4_config, 0);
	ASSERT (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0,
	        "dhcp-rfc3442-gateway", "couldn't convert expected route destination #1");
	ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr,
	        "dhcp-rfc3442-gateway", "unexpected route #1 destination");

	ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0,
	        "dhcp-rfc3442-gateway", "couldn't convert expected route next hop #1");
	ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr,
	        "dhcp-rfc3442-gateway", "unexpected route #1 next hop");

	ASSERT (nm_ip4_route_get_prefix (route) == 24,
	        "dhcp-rfc3442-gateway", "unexpected route #1 prefix");
	ASSERT (nm_ip4_route_get_metric (route) == 0,
	        "dhcp-rfc3442-gateway", "unexpected route #1 metric");

	/* Address */
	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        "dhcp-rfc3442-gateway", "unexpected number of IP addresses");
	addr = nm_ip4_config_get_address (ip4_config, 0);
	ASSERT (inet_pton (AF_INET, expected_gateway, &tmp) > 0,
	        "dhcp-rfc3442-gateway", "couldn't convert expected IP gateway");
	ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr,
	        "dhcp-rfc3442-gateway", "unexpected IP gateway");

	g_hash_table_destroy (options);
}
static void
ip4_test_gateway (const char *test,
                  NMIP4Config *ip4_config,
                  const char *expected_gw)
{
	NMIP4Address *addr;
	struct in_addr tmp;

	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        test, "unexpected number of IP addresses");
	addr = nm_ip4_config_get_address (ip4_config, 0);
	ASSERT (inet_pton (AF_INET, expected_gw, &tmp) > 0,
	        test, "couldn't convert expected IP gateway");
	ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr,
	        test, "unexpected IP gateway");
}
Exemplo n.º 7
0
struct rtnl_addr *
nm_ip4_config_to_rtnl_addr (NMIP4Config *config, guint32 i, guint32 flags)
{
    NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config);
    NMIP4Address *config_addr;
    struct rtnl_addr *addr;
    gboolean success = TRUE;

    g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);

    config_addr = nm_ip4_config_get_address (config, i);
    g_return_val_if_fail (config_addr != NULL, NULL);

    if (!(addr = rtnl_addr_alloc()))
        return NULL;

    if (flags & NM_RTNL_ADDR_ADDR)
        success = (ip4_addr_to_rtnl_local (nm_ip4_address_get_address (config_addr), addr) >= 0);

    if (flags & NM_RTNL_ADDR_PTP_ADDR)
        success = (ip4_addr_to_rtnl_peer (priv->ptp_address, addr) >= 0);

    if (flags & NM_RTNL_ADDR_PREFIX)
        rtnl_addr_set_prefixlen (addr, nm_ip4_address_get_prefix (config_addr));

    if (flags & NM_RTNL_ADDR_BROADCAST) {
        guint32 hostmask, network, bcast, netmask;

        netmask = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (config_addr));
        network = ntohl (nm_ip4_address_get_address (config_addr)) & ntohl (netmask);
        hostmask = ~ntohl (netmask);
        bcast = htonl (network | hostmask);

        success = (ip4_addr_to_rtnl_broadcast (bcast, addr) >= 0);
    }

    if (!success) {
        rtnl_addr_put (addr);
        addr = NULL;
    }

    return addr;
}
Exemplo n.º 8
0
void
nm_modem_ip4_pre_commit (NMModem *modem,
                         NMDevice *device,
                         NMIP4Config *config)
{
	NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (modem);

	/* If the modem has an ethernet-type data interface (ie, not PPP and thus
	 * not point-to-point) and IP config has a /32 prefix, then we assume that
	 * ARP will be pointless and we turn it off.
	 */
	if (   priv->ip4_method == NM_MODEM_IP_METHOD_STATIC
	    || priv->ip4_method == NM_MODEM_IP_METHOD_AUTO) {
		const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, 0);

		g_assert (address);
		if (address->plen == 32)
			nm_platform_link_set_noarp (NM_PLATFORM_GET, nm_device_get_ip_ifindex (device));
	}
}
void
nm_modem_ip4_pre_commit (NMModem *modem,
                         NMDevice *device,
                         NMIP4Config *config)
{
	NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (modem);

	/* If the modem has an ethernet-type data interface (ie, not PPP and thus
	 * not point-to-point) and IP config has a /32 prefix, then we assume that
	 * ARP will be pointless and we turn it off.
	 */
	if (   priv->ip_method == MM_MODEM_IP_METHOD_STATIC
	    || priv->ip_method == MM_MODEM_IP_METHOD_DHCP) {
		NMIP4Address *addr = nm_ip4_config_get_address (config, 0);

		g_assert (addr);
		if (nm_ip4_address_get_prefix (addr) == 32)
			nm_system_iface_set_arp (nm_device_get_ip_ifindex (device), FALSE);
	}
}
Exemplo n.º 10
0
static void
update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
{
	char *configured_hostname = NULL;
	NMActRequest *best_req4 = NULL;
	NMActRequest *best_req6 = NULL;
	const char *dhcp_hostname, *p;

	g_return_if_fail (policy != NULL);

	if (policy->lookup) {
		hostname_thread_kill (policy->lookup);
		policy->lookup = NULL;
	}

	/* Hostname precedence order:
	 *
	 * 1) a configured hostname (from system-settings)
	 * 2) automatic hostname from the default device's config (DHCP, VPN, etc)
	 * 3) the original hostname when NM started
	 * 4) reverse-DNS of the best device's IPv4 address
	 *
	 */

	/* Try a persistent hostname first */
	g_object_get (G_OBJECT (policy->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL);
	if (configured_hostname) {
		_set_hostname (policy, TRUE, configured_hostname, "from system configuration");
		g_free (configured_hostname);
		return;
	}

	/* Try automatically determined hostname from the best device's IP config */
	if (!best4)
		best4 = get_best_ip4_device (policy->manager, &best_req4);
	if (!best6)
		best6 = get_best_ip6_device (policy->manager, &best_req6);

	if (!best4 && !best6) {
		/* No best device; fall back to original hostname or if there wasn't
		 * one, 'localhost.localdomain'
		 */
		_set_hostname (policy, TRUE, policy->orig_hostname, "no default device");
		return;
	}

	if (best4) {
		NMDHCP4Config *dhcp4_config;

		/* Grab a hostname out of the device's DHCP4 config */
		dhcp4_config = nm_device_get_dhcp4_config (best4);
		if (dhcp4_config) {
			p = dhcp_hostname = nm_dhcp4_config_get_option (dhcp4_config, "host_name");
			if (dhcp_hostname && strlen (dhcp_hostname)) {
				/* Sanity check; strip leading spaces */
				while (*p) {
					if (!isblank (*p++)) {
						_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv4");
						return;
					}
				}
				nm_log_warn (LOGD_DNS, "DHCPv4-provided hostname '%s' looks invalid; ignoring it",
					         dhcp_hostname);
			}
		}
	} else if (best6) {
		NMDHCP6Config *dhcp6_config;

		/* Grab a hostname out of the device's DHCP4 config */
		dhcp6_config = nm_device_get_dhcp6_config (best6);
		if (dhcp6_config) {
			p = dhcp_hostname = nm_dhcp6_config_get_option (dhcp6_config, "host_name");
			if (dhcp_hostname && strlen (dhcp_hostname)) {
				/* Sanity check; strip leading spaces */
				while (*p) {
					if (!isblank (*p++)) {
						_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv6");
						return;
					}
				}
				nm_log_warn (LOGD_DNS, "DHCPv6-provided hostname '%s' looks invalid; ignoring it",
					         dhcp_hostname);
			}
		}
	}

	/* If no automatically-configured hostname, try using the hostname from
	 * when NM started up.
	 */
	if (policy->orig_hostname) {
		_set_hostname (policy, TRUE, policy->orig_hostname, "from system startup");
		return;
	}

	/* No configured hostname, no automatically determined hostname, and no
	 * bootup hostname. Start reverse DNS of the current IPv4 or IPv6 address.
	 */
	if (best4) {
		NMIP4Config *ip4_config;
		NMIP4Address *addr4;

		ip4_config = nm_device_get_ip4_config (best4);
		if (   !ip4_config
		    || (nm_ip4_config_get_num_nameservers (ip4_config) == 0)
		    || (nm_ip4_config_get_num_addresses (ip4_config) == 0)) {
			/* No valid IP4 config (!!); fall back to localhost.localdomain */
			_set_hostname (policy, TRUE, NULL, "no IPv4 config");
			return;
		}

		addr4 = nm_ip4_config_get_address (ip4_config, 0);
		g_assert (addr4); /* checked for > 1 address above */

		/* Start the hostname lookup thread */
		policy->lookup = hostname4_thread_new (nm_ip4_address_get_address (addr4), lookup_callback, policy);
	} else if (best6) {
		NMIP6Config *ip6_config;
		NMIP6Address *addr6;

		ip6_config = nm_device_get_ip6_config (best6);
		if (   !ip6_config
		    || (nm_ip6_config_get_num_nameservers (ip6_config) == 0)
		    || (nm_ip6_config_get_num_addresses (ip6_config) == 0)) {
			/* No valid IP6 config (!!); fall back to localhost.localdomain */
			_set_hostname (policy, TRUE, NULL, "no IPv6 config");
			return;
		}

		addr6 = nm_ip6_config_get_address (ip6_config, 0);
		g_assert (addr6); /* checked for > 1 address above */

		/* Start the hostname lookup thread */
		policy->lookup = hostname6_thread_new (nm_ip6_address_get_address (addr6), lookup_callback, policy);
	}

	if (!policy->lookup) {
		/* Fall back to 'localhost.localdomain' */
		_set_hostname (policy, TRUE, NULL, "error starting hostname thread");
	}
}
static NMCmdLine *
create_dm_cmd_line (const char *iface,
                    NMIP4Config *ip4_config,
                    const char *pidfile,
                    GError **error)
{
	const char *dm_binary;
	NMCmdLine *cmd;
	GString *s;
	NMIP4Address *tmp;
	struct in_addr addr;
	char buf[INET_ADDRSTRLEN + 1];
	char localaddr[INET_ADDRSTRLEN + 1];

	dm_binary = nm_find_dnsmasq ();
	if (!dm_binary) {
		g_set_error (error, NM_DNSMASQ_MANAGER_ERROR, NM_DNSMASQ_MANAGER_ERROR_NOT_FOUND,
		             "Could not find dnsmasq binary.");
		return NULL;
	}

	/* Find the IP4 address to use */
	tmp = nm_ip4_config_get_address (ip4_config, 0);

	/* Create dnsmasq command line */
	cmd = nm_cmd_line_new ();
	nm_cmd_line_add_string (cmd, dm_binary);

	nm_cmd_line_add_string (cmd, "--no-hosts");
	nm_cmd_line_add_string (cmd, "--keep-in-foreground");
	nm_cmd_line_add_string (cmd, "--bind-interfaces");
	nm_cmd_line_add_string (cmd, "--no-poll");
	nm_cmd_line_add_string (cmd, "--except-interface=lo");

	s = g_string_new ("--listen-address=");
	addr.s_addr = nm_ip4_address_get_address (tmp);
	if (!inet_ntop (AF_INET, &addr, &localaddr[0], INET_ADDRSTRLEN)) {
		nm_warning ("%s: error converting IP4 address 0x%X",
		            __func__, ntohl (addr.s_addr));
		goto error;
	}
	g_string_append (s, localaddr);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	s = g_string_new ("--dhcp-range=");

	/* Add start of address range */
	addr.s_addr = nm_ip4_address_get_address (tmp) + htonl (9);
	if (!inet_ntop (AF_INET, &addr, &buf[0], INET_ADDRSTRLEN)) {
		nm_warning ("%s: error converting IP4 address 0x%X",
		            __func__, ntohl (addr.s_addr));
		goto error;
	}
	g_string_append (s, buf);

	g_string_append_c (s, ',');

	/* Add end of address range */
	addr.s_addr = nm_ip4_address_get_address (tmp) + htonl (99);
	if (!inet_ntop (AF_INET, &addr, &buf[0], INET_ADDRSTRLEN)) {
		nm_warning ("%s: error converting IP4 address 0x%X",
		            __func__, ntohl (addr.s_addr));
		goto error;
	}
	g_string_append (s, buf);

	g_string_append (s, ",60m");
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	s = g_string_new ("--dhcp-option=option:router,");
	g_string_append (s, localaddr);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	nm_cmd_line_add_string (cmd, "--dhcp-lease-max=50");

	s = g_string_new ("--pid-file=");
	g_string_append (s, pidfile);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	return cmd;

error:
	nm_cmd_line_destroy (cmd);
	return NULL;
}
Exemplo n.º 12
0
void
nm_utils_merge_ip4_config (NMIP4Config *ip4_config, NMSettingIP4Config *setting)
{
	int i, j;
	gboolean setting_never_default;

	if (!setting)
		return; /* Defaults are just fine */

	if (nm_setting_ip4_config_get_ignore_auto_dns (setting)) {
		nm_ip4_config_reset_nameservers (ip4_config);
		nm_ip4_config_reset_domains (ip4_config);
		nm_ip4_config_reset_searches (ip4_config);
	}

	if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
		nm_ip4_config_reset_routes (ip4_config);

	for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) {
		guint32 ns;
		gboolean found = FALSE;

		/* Avoid dupes */
		ns = nm_setting_ip4_config_get_dns (setting, i);
		for (j = 0; j < nm_ip4_config_get_num_nameservers (ip4_config); j++) {
			if (nm_ip4_config_get_nameserver (ip4_config, j) == ns) {
				found = TRUE;
				break;
			}
		}

		if (!found)
			nm_ip4_config_add_nameserver (ip4_config, ns);
	}

	/* DNS search domains */
	for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) {
		const char *search = nm_setting_ip4_config_get_dns_search (setting, i);
		gboolean found = FALSE;

		/* Avoid dupes */
		for (j = 0; j < nm_ip4_config_get_num_searches (ip4_config); j++) {
			if (!strcmp (search, nm_ip4_config_get_search (ip4_config, j))) {
				found = TRUE;
				break;
			}
		}

		if (!found)
			nm_ip4_config_add_search (ip4_config, search);
	}

	/* IPv4 addresses */
	for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) {
		NMIP4Address *setting_addr = nm_setting_ip4_config_get_address (setting, i);
		guint32 num;

		num = nm_ip4_config_get_num_addresses (ip4_config);
		for (j = 0; j < num; j++) {
			NMIP4Address *cfg_addr = nm_ip4_config_get_address (ip4_config, j);

			/* Dupe, override with user-specified address */
			if (nm_ip4_address_get_address (cfg_addr) == nm_ip4_address_get_address (setting_addr)) {
				nm_ip4_config_replace_address (ip4_config, j, setting_addr);
				break;
			}
		}

		if (j == num)
			nm_ip4_config_add_address (ip4_config, setting_addr);
	}

	/* IPv4 routes */
	for (i = 0; i < nm_setting_ip4_config_get_num_routes (setting); i++) {
		NMIP4Route *setting_route = nm_setting_ip4_config_get_route (setting, i);
		guint32 num;

		num = nm_ip4_config_get_num_routes (ip4_config);
		for (j = 0; j < num; j++) {
			NMIP4Route *cfg_route = nm_ip4_config_get_route (ip4_config, j);

			/* Dupe, override with user-specified route */
			if (   (nm_ip4_route_get_dest (cfg_route) == nm_ip4_route_get_dest (setting_route))
			    && (nm_ip4_route_get_prefix (cfg_route) == nm_ip4_route_get_prefix (setting_route))
			    && (nm_ip4_route_get_next_hop (cfg_route) == nm_ip4_route_get_next_hop (setting_route))) {
				nm_ip4_config_replace_route (ip4_config, j, setting_route);
				break;
			}
		}

		if (j == num)
			nm_ip4_config_add_route (ip4_config, setting_route);
	}

	setting_never_default = nm_setting_ip4_config_get_never_default (setting);

	if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
		nm_ip4_config_set_never_default (ip4_config, setting_never_default);
	else {
		if (setting_never_default)
			nm_ip4_config_set_never_default (ip4_config, TRUE);
	}
}
Exemplo n.º 13
0
static void
dump_ip4_to_props (NMIP4Config *ip4, GVariantBuilder *builder)
{
	GVariantBuilder int_builder;
	guint n, i;
	const NMPlatformIP4Address *addr;
	const NMPlatformIP4Route *route;
	guint32 array[4];

	/* Addresses */
	g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("aau"));
	n = nm_ip4_config_get_num_addresses (ip4);
	for (i = 0; i < n; i++) {
		addr = nm_ip4_config_get_address (ip4, i);
		array[0] = addr->address;
		array[1] = addr->plen;
		array[2] = (i == 0) ? nm_ip4_config_get_gateway (ip4) : 0;
		g_variant_builder_add (&int_builder, "@au",
		                       g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
		                                                  array, 3, sizeof (guint32)));
	}
	g_variant_builder_add (builder, "{sv}",
	                       "addresses",
	                       g_variant_builder_end (&int_builder));

	/* DNS servers */
	g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("au"));
	n = nm_ip4_config_get_num_nameservers (ip4);
	for (i = 0; i < n; i++)
		g_variant_builder_add (&int_builder, "u", nm_ip4_config_get_nameserver (ip4, i));
	g_variant_builder_add (builder, "{sv}",
	                       "nameservers",
	                       g_variant_builder_end (&int_builder));

	/* Search domains */
	g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("as"));
	n = nm_ip4_config_get_num_domains (ip4);
	for (i = 0; i < n; i++)
		g_variant_builder_add (&int_builder, "s", nm_ip4_config_get_domain (ip4, i));
	g_variant_builder_add (builder, "{sv}",
	                       "domains",
	                       g_variant_builder_end (&int_builder));

	/* WINS servers */
	g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("au"));
	n = nm_ip4_config_get_num_wins (ip4);
	for (i = 0; i < n; i++)
		g_variant_builder_add (&int_builder, "u", nm_ip4_config_get_wins (ip4, i));
	g_variant_builder_add (builder, "{sv}",
	                       "wins-servers",
	                       g_variant_builder_end (&int_builder));

	/* Static routes */
	g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("aau"));
	n = nm_ip4_config_get_num_routes (ip4);
	for (i = 0; i < n; i++) {
		route = nm_ip4_config_get_route (ip4, i);
		array[0] = route->network;
		array[1] = route->plen;
		array[2] = route->gateway;
		array[3] = route->metric;
		g_variant_builder_add (&int_builder, "@au",
		                       g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
		                                                  array, 4, sizeof (guint32)));
	}
	g_variant_builder_add (builder, "{sv}",
	                       "routes",
	                       g_variant_builder_end (&int_builder));
}
static void
test_generic_options (void)
{
	GHashTable *options;
	NMIP4Config *ip4_config;
	NMIP4Address *addr;
	NMIP4Route *route;
	struct in_addr tmp;
	const char *expected_addr = "192.168.1.106";
	const char *expected_gw = "192.168.1.1";
	const char *expected_dns1 = "216.254.95.2";
	const char *expected_dns2 = "216.231.41.2";
	const char *expected_search1 = "foobar.com";
	const char *expected_search2 = "blah.foobar.com";
	const char *expected_route1_dest = "10.1.1.5";
	const char *expected_route1_gw = "10.1.1.1";
	const char *expected_route2_dest = "100.99.88.56";
	const char *expected_route2_gw = "10.1.1.1";

	options = fill_table (generic_options, NULL);
	ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options);
	ASSERT (ip4_config != NULL,
	        "dhcp-generic", "failed to parse DHCP4 options");

	/* IP4 address */
	ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1,
	        "dhcp-generic", "unexpected number of IP addresses");
	addr = nm_ip4_config_get_address (ip4_config, 0);

	ASSERT (inet_pton (AF_INET, expected_addr, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected IP address");
	ASSERT (nm_ip4_address_get_address (addr) == tmp.s_addr,
	        "dhcp-generic", "unexpected IP address");

	ASSERT (nm_ip4_address_get_prefix (addr) == 24,
	        "dhcp-generic", "unexpected IP address prefix length");

	/* Gateway */
	ASSERT (inet_pton (AF_INET, expected_gw, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected IP gateway");
	ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr,
	        "dhcp-generic", "unexpected IP gateway");

	ASSERT (nm_ip4_config_get_ptp_address (ip4_config) == 0,
	        "dhcp-generic", "unexpected PTP address");

	ASSERT (nm_ip4_config_get_num_wins (ip4_config) == 0,
	        "dhcp-generic", "unexpected number of WINS servers");

	ASSERT (nm_ip4_config_get_mtu (ip4_config) == 987,
	        "dhcp-generic", "unexpected MTU");

	/* Domain searches */
	ASSERT (nm_ip4_config_get_num_searches (ip4_config) == 2,
	        "dhcp-generic", "unexpected number of domain searches");
	ASSERT (strcmp (nm_ip4_config_get_search (ip4_config, 0), expected_search1) == 0,
	        "dhcp-generic", "unexpected domain search #1");
	ASSERT (strcmp (nm_ip4_config_get_search (ip4_config, 1), expected_search2) == 0,
	        "dhcp-generic", "unexpected domain search #2");

	/* DNS servers */
	ASSERT (nm_ip4_config_get_num_nameservers (ip4_config) == 2,
	        "dhcp-generic", "unexpected number of domain name servers");
	ASSERT (inet_pton (AF_INET, expected_dns1, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected DNS server address #1");
	ASSERT (nm_ip4_config_get_nameserver (ip4_config, 0) == tmp.s_addr,
	        "dhcp-generic", "unexpected domain name server #1");
	ASSERT (inet_pton (AF_INET, expected_dns2, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected DNS server address #2");
	ASSERT (nm_ip4_config_get_nameserver (ip4_config, 1) == tmp.s_addr,
	        "dhcp-generic", "unexpected domain name server #2");

	/* Routes */
	ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 2,
	        "dhcp-generic", "unexpected number of routes");

	/* Route #1 */
	route = nm_ip4_config_get_route (ip4_config, 0);
	ASSERT (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected route destination #1");
	ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr,
	        "dhcp-generic", "unexpected route #1 destination");

	ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected route next hop #1");
	ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr,
	        "dhcp-generic", "unexpected route #1 next hop");

	ASSERT (nm_ip4_route_get_prefix (route) == 32,
	        "dhcp-generic", "unexpected route #1 prefix");
	ASSERT (nm_ip4_route_get_metric (route) == 0,
	        "dhcp-generic", "unexpected route #1 metric");

	/* Route #2 */
	route = nm_ip4_config_get_route (ip4_config, 1);
	ASSERT (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected route destination #2");
	ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr,
	        "dhcp-generic", "unexpected route #2 destination");

	ASSERT (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0,
	        "dhcp-generic", "couldn't convert expected route next hop #2");
	ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr,
	        "dhcp-generic", "unexpected route #2 next hop");

	ASSERT (nm_ip4_route_get_prefix (route) == 32,
	        "dhcp-generic", "unexpected route #2 prefix");
	ASSERT (nm_ip4_route_get_metric (route) == 0,
	        "dhcp-generic", "unexpected route #2 metric");

	g_hash_table_destroy (options);
}
Exemplo n.º 15
0
static void
update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update)
{
	NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
	NMDevice *best = NULL;
	NMActRequest *best_req = NULL;
	NMDnsManager *dns_mgr;
	GSList *devices = NULL, *iter, *vpns;
	NMIP4Config *ip4_config = NULL;
	NMIP4Address *addr;
	const char *ip_iface = NULL;
	NMConnection *connection = NULL;
	NMSettingConnection *s_con = NULL;
	const char *connection_id;

	best = get_best_ip4_device (policy->manager, &best_req);
	if (!best)
		goto out;
	if (!force_update && (best == policy->default_device4))
		goto out;

	/* If a VPN connection is active, it is preferred */
	vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager);
	for (iter = vpns; iter; iter = g_slist_next (iter)) {
		NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data);
		NMConnection *vpn_connection;
		NMSettingIP4Config *s_ip4;
		gboolean can_default = TRUE;
		NMVPNConnectionState vpn_state;

		/* If it's marked 'never-default', don't make it default */
		vpn_connection = nm_vpn_connection_get_connection (candidate);
		g_assert (vpn_connection);

		/* Check the active IP4 config from the VPN service daemon */
		ip4_config = nm_vpn_connection_get_ip4_config (candidate);
		if (ip4_config && nm_ip4_config_get_never_default (ip4_config))
			can_default = FALSE;

		/* Check the user's preference from the NMConnection */
		s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (vpn_connection, NM_TYPE_SETTING_IP4_CONFIG);
		if (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4))
			can_default = FALSE;

		vpn_state = nm_vpn_connection_get_vpn_state (candidate);
		if (can_default && (vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED)) {
			NMIP4Config *parent_ip4;
			NMDevice *parent;

			ip_iface = nm_vpn_connection_get_ip_iface (candidate);
			connection = nm_vpn_connection_get_connection (candidate);
			addr = nm_ip4_config_get_address (ip4_config, 0);

			parent = nm_vpn_connection_get_parent_device (candidate);
			parent_ip4 = nm_device_get_ip4_config (parent);

			nm_system_replace_default_ip4_route_vpn (ip_iface,
			                                         nm_ip4_address_get_gateway (addr),
			                                         nm_vpn_connection_get_ip4_internal_gateway (candidate),
			                                         nm_ip4_config_get_mss (ip4_config),
			                                         nm_device_get_ip_iface (parent),
			                                         nm_ip4_config_get_mss (parent_ip4));

			dns_type = NM_DNS_IP_CONFIG_TYPE_VPN;
		}
		g_object_unref (candidate);
	}
	g_slist_free (vpns);

	/* The best device gets the default route if a VPN connection didn't */
	if (!ip_iface || !ip4_config) {
		connection = nm_act_request_get_connection (best_req);
		ip_iface = nm_device_get_ip_iface (best);
		ip4_config = nm_device_get_ip4_config (best);
		g_assert (ip4_config);
		addr = nm_ip4_config_get_address (ip4_config, 0);

		nm_system_replace_default_ip4_route (ip_iface, nm_ip4_address_get_gateway (addr), nm_ip4_config_get_mss (ip4_config));

		dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE;
	}

	if (!ip_iface || !ip4_config) {
		nm_log_warn (LOGD_CORE, "couldn't determine IP interface (%p) or IPv4 config (%p)!",
		             ip_iface, ip4_config);
		goto out;
	}

	/* Update the default active connection.  Only mark the new default
	 * active connection after setting default = FALSE on all other connections
	 * first.  The order is important, we don't want two connections marked
	 * default at the same time ever.
	 */
	devices = nm_manager_get_devices (policy->manager);
	for (iter = devices; iter; iter = g_slist_next (iter)) {
		NMDevice *dev = NM_DEVICE (iter->data);
		NMActRequest *req;

		req = nm_device_get_act_request (dev);
		if (req && (req != best_req))
			nm_act_request_set_default (req, FALSE);
	}

	dns_mgr = nm_dns_manager_get (NULL);
	nm_dns_manager_add_ip4_config (dns_mgr, ip_iface, ip4_config, dns_type);
	g_object_unref (dns_mgr);

	/* Now set new default active connection _after_ updating DNS info, so that
	 * if the connection is shared dnsmasq picks up the right stuff.
	 */
	if (best_req)
		nm_act_request_set_default (best_req, TRUE);

	if (connection)
		s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);

	connection_id = s_con ? nm_setting_connection_get_id (s_con) : NULL;
	if (connection_id) {
		nm_log_info (LOGD_CORE, "Policy set '%s' (%s) as default for IPv4 routing and DNS.", connection_id, ip_iface);
	} else {
		nm_log_info (LOGD_CORE, "Policy set (%s) as default for IPv4 routing and DNS.", ip_iface);
	}

out:
	policy->default_device4 = best;
}
Exemplo n.º 16
0
static NMDevice *
get_best_ip4_device (NMManager *manager, NMActRequest **out_req)
{
	GSList *devices, *iter;
	NMDevice *best = NULL;
	int best_prio = G_MAXINT;

	g_return_val_if_fail (manager != NULL, NULL);
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
	g_return_val_if_fail (out_req != NULL, NULL);
	g_return_val_if_fail (*out_req == NULL, NULL);

	devices = nm_manager_get_devices (manager);
	for (iter = devices; iter; iter = g_slist_next (iter)) {
		NMDevice *dev = NM_DEVICE (iter->data);
		NMActRequest *req;
		NMConnection *connection;
		NMIP4Config *ip4_config;
		NMSettingIP4Config *s_ip4;
		int prio;
		guint i;
		gboolean can_default = FALSE;
		const char *method = NULL;

		if (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED)
			continue;

		ip4_config = nm_device_get_ip4_config (dev);
		if (!ip4_config)
			continue;

		req = nm_device_get_act_request (dev);
		g_assert (req);
		connection = nm_act_request_get_connection (req);
		g_assert (connection);

		/* Never set the default route through an IPv4LL-addressed device */
		s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
		if (s_ip4)
			method = nm_setting_ip4_config_get_method (s_ip4);

		if (s_ip4 && !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL))
			continue;

		/* Make sure at least one of this device's IP addresses has a gateway */
		for (i = 0; i < nm_ip4_config_get_num_addresses (ip4_config); i++) {
			NMIP4Address *addr;

			addr = nm_ip4_config_get_address (ip4_config, i);
			if (nm_ip4_address_get_gateway (addr)) {
				can_default = TRUE;
				break;
			}
		}

		if (!can_default && !NM_IS_DEVICE_MODEM (dev))
			continue;

		/* 'never-default' devices can't ever be the default */
		if (   (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4))
		    || nm_ip4_config_get_never_default (ip4_config))
			continue;

		prio = nm_device_get_priority (dev);
		if (prio > 0 && prio < best_prio) {
			best = dev;
			best_prio = prio;
			*out_req = req;
		}
	}

	return best;
}
Exemplo n.º 17
0
static NMCmdLine *
create_dm_cmd_line (const char *iface,
                    NMIP4Config *ip4_config,
                    const char *pidfile,
                    GError **error)
{
	NMCmdLine *cmd;
	GString *s;
	const NMPlatformIP4Address *tmp;
	char first[INET_ADDRSTRLEN];
	char last[INET_ADDRSTRLEN];
	char localaddr[INET_ADDRSTRLEN];
	char *error_desc = NULL;
	const char *dm_binary;

	dm_binary = nm_utils_find_helper ("dnsmasq", DNSMASQ_PATH, error);
	if (!dm_binary)
		return NULL;

	/* Create dnsmasq command line */
	cmd = nm_cmd_line_new ();
	nm_cmd_line_add_string (cmd, dm_binary);

	if (getenv ("NM_DNSMASQ_DEBUG")) {
		nm_cmd_line_add_string (cmd, "--log-dhcp");
		nm_cmd_line_add_string (cmd, "--log-queries");
	}

	/* dnsmasq may read from it's default config file location, which if that
	 * location is a valid config file, it will combine with the options here
	 * and cause undesirable side-effects.  Like sending bogus IP addresses
	 * as the gateway or whatever.  So tell dnsmasq not to use any config file
	 * at all.
	 */
	nm_cmd_line_add_string (cmd, "--conf-file");

	nm_cmd_line_add_string (cmd, "--no-hosts");
	nm_cmd_line_add_string (cmd, "--keep-in-foreground");
	nm_cmd_line_add_string (cmd, "--bind-interfaces");
	nm_cmd_line_add_string (cmd, "--except-interface=lo");
	nm_cmd_line_add_string (cmd, "--clear-on-reload");

	/* Use strict order since in the case of VPN connections, the VPN's
	 * nameservers will be first in resolv.conf, and those need to be tried
	 * first by dnsmasq to successfully resolve names from the VPN.
	 */
	nm_cmd_line_add_string (cmd, "--strict-order");

	/* Find the IP4 address to use */
	tmp = nm_ip4_config_get_address (ip4_config, 0);

	s = g_string_new ("--listen-address=");
	nm_utils_inet4_ntop (tmp->address, localaddr);
	g_string_append (s, localaddr);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	if (!nm_dnsmasq_utils_get_range (tmp, first, last, &error_desc)) {
		g_set_error_literal (error,
		                     NM_MANAGER_ERROR,
		                     NM_MANAGER_ERROR_FAILED,
		                     error_desc);
		nm_log_warn (LOGD_SHARING, "Failed to find DHCP address ranges: %s", error_desc);
		g_free (error_desc);
		nm_cmd_line_destroy (cmd);
		return NULL;
	}

	s = g_string_new ("--dhcp-range=");
	g_string_append_printf (s, "%s,%s,60m", first, last);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	s = g_string_new ("--dhcp-option=option:router,");
	g_string_append (s, localaddr);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	nm_cmd_line_add_string (cmd, "--dhcp-lease-max=50");

	s = g_string_new ("--pid-file=");
	g_string_append (s, pidfile);
	nm_cmd_line_add_string (cmd, s->str);
	g_string_free (s, TRUE);

	return cmd;
}