Ejemplo n.º 1
0
static void
impl_ppp_manager_set_ip6_config (NMPPPManager *manager,
                                 GDBusMethodInvocation *context,
                                 GVariant *config_dict)
{
	NMPPPManagerPrivate *priv = NM_PPP_MANAGER_GET_PRIVATE (manager);
	NMIP6Config *config;
	NMPlatformIP6Address addr;
	struct in6_addr a;
	NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT;

	_LOGI ("(IPv6 Config Get) reply received.");

	remove_timeout_handler (manager);

	config = nm_ip6_config_new (nm_platform_link_get_ifindex (NM_PLATFORM_GET, priv->ip_iface));

	memset (&addr, 0, sizeof (addr));
	addr.plen = 64;

	if (iid_value_to_ll6_addr (config_dict, NM_PPP_IP6_CONFIG_PEER_IID, &a, NULL)) {
		nm_ip6_config_set_gateway (config, &a);
		addr.peer_address = a;
	}

	if (iid_value_to_ll6_addr (config_dict, NM_PPP_IP6_CONFIG_OUR_IID, &addr.address, &iid)) {
		nm_ip6_config_add_address (config, &addr);

		if (set_ip_config_common (manager, config_dict, NM_PPP_IP6_CONFIG_INTERFACE, NULL)) {
			/* Push the IPv6 config and interface identifier up to the device */
			g_signal_emit (manager, signals[IP6_CONFIG], 0, priv->ip_iface, &iid, config);
		}
	} else
		_LOGE ("invalid IPv6 address received!");

	g_object_unref (config);
	g_dbus_method_invocation_return_value (context, NULL);
}
Ejemplo n.º 2
0
static void
rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_data)
{
    static NMIP6Config *rdisc_config = NULL;
    NMIP6Config *existing;
    static int system_support = -1;
    guint32 ifa_flags = 0x00;
    int i;

    if (system_support == -1) {
        /*
         * Check, whether kernel is recent enough, to help user space handling RA.
         * If it's not supported, we have no ipv6-privacy and must add autoconf
         * addresses as /128.
         * The reason for the /128 is to prevent the kernel
         * from adding a prefix route for this address.
         **/
        system_support = nm_platform_check_support_kernel_extended_ifa_flags (NM_PLATFORM_GET);
    }

    if (system_support)
        ifa_flags = IFA_F_NOPREFIXROUTE;
    if (global_opt.tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR
            || global_opt.tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR)
    {
        /* without system_support, this flag will be ignored. Still set it, doesn't seem to do any harm. */
        ifa_flags |= IFA_F_MANAGETEMPADDR;
    }

    existing = nm_ip6_config_capture (ifindex, FALSE, global_opt.tempaddr);
    if (rdisc_config)
        nm_ip6_config_subtract (existing, rdisc_config);
    else
        rdisc_config = nm_ip6_config_new (ifindex);

    if (changed & NM_RDISC_CONFIG_GATEWAYS) {
        /* Use the first gateway as ordered in router discovery cache. */
        if (rdisc->gateways->len) {
            NMRDiscGateway *gateway = &g_array_index (rdisc->gateways, NMRDiscGateway, 0);

            nm_ip6_config_set_gateway (rdisc_config, &gateway->address);
        } else
            nm_ip6_config_set_gateway (rdisc_config, NULL);
    }

    if (changed & NM_RDISC_CONFIG_ADDRESSES) {
        /* Rebuild address list from router discovery cache. */
        nm_ip6_config_reset_addresses (rdisc_config);

        /* rdisc->addresses contains at most max_addresses entries.
         * This is different from what the kernel does, which
         * also counts static and temporary addresses when checking
         * max_addresses.
         **/
        for (i = 0; i < rdisc->addresses->len; i++) {
            NMRDiscAddress *discovered_address = &g_array_index (rdisc->addresses, NMRDiscAddress, i);
            NMPlatformIP6Address address;

            memset (&address, 0, sizeof (address));
            address.address = discovered_address->address;
            address.plen = system_support ? 64 : 128;
            address.timestamp = discovered_address->timestamp;
            address.lifetime = discovered_address->lifetime;
            address.preferred = discovered_address->preferred;
            if (address.preferred > address.lifetime)
                address.preferred = address.lifetime;
            address.source = NM_IP_CONFIG_SOURCE_RDISC;
            address.n_ifa_flags = ifa_flags;

            nm_ip6_config_add_address (rdisc_config, &address);
        }
    }

    if (changed & NM_RDISC_CONFIG_ROUTES) {
        /* Rebuild route list from router discovery cache. */
        nm_ip6_config_reset_routes (rdisc_config);

        for (i = 0; i < rdisc->routes->len; i++) {
            NMRDiscRoute *discovered_route = &g_array_index (rdisc->routes, NMRDiscRoute, i);
            NMPlatformIP6Route route;

            /* Only accept non-default routes.  The router has no idea what the
             * local configuration or user preferences are, so sending routes
             * with a prefix length of 0 is quite rude and thus ignored.
             */
            if (   discovered_route->plen > 0
                    && discovered_route->plen <= 128) {
                memset (&route, 0, sizeof (route));
                route.network = discovered_route->network;
                route.plen = discovered_route->plen;
                route.gateway = discovered_route->gateway;
                route.source = NM_IP_CONFIG_SOURCE_RDISC;
                route.metric = global_opt.priority_v6;

                nm_ip6_config_add_route (rdisc_config, &route);
            }
        }
    }

    if (changed & NM_RDISC_CONFIG_DHCP_LEVEL) {
        /* Unsupported until systemd DHCPv6 is ready */
    }

    if (changed & NM_RDISC_CONFIG_HOP_LIMIT)
        nm_platform_sysctl_set_ip6_hop_limit_safe (NM_PLATFORM_GET, global_opt.ifname, rdisc->hop_limit);

    if (changed & NM_RDISC_CONFIG_MTU) {
        char val[16];

        g_snprintf (val, sizeof (val), "%d", rdisc->mtu);
        nm_platform_sysctl_set (NM_PLATFORM_GET, nm_utils_ip6_property_path (global_opt.ifname, "mtu"), val);
    }

    nm_ip6_config_merge (existing, rdisc_config, NM_IP_CONFIG_MERGE_DEFAULT);
    if (!nm_ip6_config_commit (existing, ifindex, TRUE))
        nm_log_warn (LOGD_IP6, "(%s): failed to apply IPv6 config", global_opt.ifname);
}
NMIP6Config *
nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex)
{
	NMIP6ManagerPrivate *priv;
	NMIP6Device *device;
	NMIP6Config *config;
	struct rtnl_addr *rtnladdr;
	struct nl_addr *nladdr;
	struct in6_addr *addr;
	NMIP6Address *ip6addr;
	struct rtnl_route *rtnlroute;
	struct nl_addr *nldest, *nlgateway;
	const struct in6_addr *dest, *gateway;
	uint32_t metric;
	NMIP6Route *ip6route;
	int i;

	g_return_val_if_fail (NM_IS_IP6_MANAGER (manager), NULL);
	g_return_val_if_fail (ifindex > 0, NULL);

	priv = NM_IP6_MANAGER_GET_PRIVATE (manager);

	device = (NMIP6Device *) g_hash_table_lookup (priv->devices,
	                                              GINT_TO_POINTER (ifindex));
	if (!device) {
		nm_log_warn (LOGD_IP6, "(%d): addrconf not started.", ifindex);
		return NULL;
	}

	config = nm_ip6_config_new ();
	if (!config) {
		nm_log_err (LOGD_IP6, "(%s): out of memory creating IP6 config object.",
		            device->iface);
		return NULL;
	}

	/* Make sure we refill the route and address caches, otherwise we won't get
	 * up-to-date information here since the netlink route/addr change messages
	 * may be lagging a bit.
	 */
	nl_cache_refill (priv->nlh, priv->route_cache);
	nl_cache_refill (priv->nlh, priv->addr_cache);

	/* Add routes */
	for (rtnlroute = FIRST_ROUTE (priv->route_cache); rtnlroute; rtnlroute = NEXT_ROUTE (rtnlroute)) {
		/* Make sure it's an IPv6 route for this device */
		if (rtnl_route_get_oif (rtnlroute) != device->ifindex)
			continue;
		if (rtnl_route_get_family (rtnlroute) != AF_INET6)
			continue;

		/* And ignore cache/cloned routes as they aren't part of the interface's
		 * permanent routing configuration.
		 */
		if (rtnl_route_get_flags (rtnlroute) & RTM_F_CLONED)
			continue;

		nldest = rtnl_route_get_dst (rtnlroute);
		if (!nldest || nl_addr_get_family (nldest) != AF_INET6)
			continue;
		dest = nl_addr_get_binary_addr (nldest);

		nlgateway = rtnl_route_get_gateway (rtnlroute);
		if (!nlgateway || nl_addr_get_family (nlgateway) != AF_INET6)
			continue;
		gateway = nl_addr_get_binary_addr (nlgateway);

		if (rtnl_route_get_dst_len (rtnlroute) == 0) {
			/* Default gateway route; cache the router's address for later */
			if (!nm_ip6_config_get_gateway (config))
				nm_ip6_config_set_gateway (config, gateway);
			continue;
		}

		/* Also ignore link-local routes where the destination and gateway are
		 * the same, which apparently get added by the kernel but return -EINVAL
		 * when we try to add them via netlink.
		 */
		if (gateway && IN6_ARE_ADDR_EQUAL (dest, gateway))
			continue;

		ip6route = nm_ip6_route_new ();
		nm_ip6_route_set_dest (ip6route, dest);
		nm_ip6_route_set_prefix (ip6route, rtnl_route_get_dst_len (rtnlroute));
		nm_ip6_route_set_next_hop (ip6route, gateway);
		rtnl_route_get_metric(rtnlroute, 1, &metric);
		if (metric != UINT_MAX)
			nm_ip6_route_set_metric (ip6route, metric);
		nm_ip6_config_take_route (config, ip6route);
	}

	/* Add addresses */
	for (rtnladdr = FIRST_ADDR (priv->addr_cache); rtnladdr; rtnladdr = NEXT_ADDR (rtnladdr)) {
		if (rtnl_addr_get_ifindex (rtnladdr) != device->ifindex)
			continue;

		nladdr = rtnl_addr_get_local (rtnladdr);
		if (!nladdr || nl_addr_get_family (nladdr) != AF_INET6)
			continue;

		addr = nl_addr_get_binary_addr (nladdr);
		ip6addr = nm_ip6_address_new ();
		nm_ip6_address_set_prefix (ip6addr, rtnl_addr_get_prefixlen (rtnladdr));
		nm_ip6_address_set_address (ip6addr, addr);
		nm_ip6_config_take_address (config, ip6addr);
		gateway = nm_ip6_config_get_gateway (config);
		if (gateway)
			nm_ip6_address_set_gateway (ip6addr, gateway);
	}

	/* Add DNS servers */
	if (device->rdnss_servers) {
		NMIP6RDNSS *rdnss = (NMIP6RDNSS *)(device->rdnss_servers->data);

		for (i = 0; i < device->rdnss_servers->len; i++)
			nm_ip6_config_add_nameserver (config, &rdnss[i].addr);
	}

	/* Add DNS domains */
	if (device->dnssl_domains) {
		NMIP6DNSSL *dnssl = (NMIP6DNSSL *)(device->dnssl_domains->data);

		for (i = 0; i < device->dnssl_domains->len; i++)
			nm_ip6_config_add_domain (config, dnssl[i].domain);
	}

	return config;
}