static void update_ipconfig(struct connman_service *service, struct connman_ipconfig *ipconfig) { if (!service || service != __connman_service_get_default()) return; if (ipconfig != __connman_service_get_ip6config(service)) return; if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) { if (default_interface) { int ifindex; ifindex = connman_inet_ifindex(default_interface); __connman_dhcpv6_stop_pd(ifindex); g_free(default_interface); default_interface = NULL; } DBG("No IPv6 support for interface index %d", __connman_ipconfig_get_index(ipconfig)); return; } /* * Did we had PD activated already? If not, then start it. */ if (!default_interface) { DBG("IPv6 ipconfig %p changed for interface index %d", ipconfig, __connman_ipconfig_get_index(ipconfig)); setup_prefix_delegation(service); } }
static void add_nat_rules(struct connman_session *session) { struct connman_ipconfig *ipconfig; const char *addr; char *ifname; int index, id, err; if (!session->fw) return; DBG(""); ipconfig = __connman_service_get_ip4config(session->service); index = __connman_ipconfig_get_index(ipconfig); ifname = connman_inet_ifname(index); addr = __connman_ipconfig_get_local(ipconfig); id = __connman_firewall_add_rule(session->fw, "nat", "POSTROUTING", "-o %s -j SNAT --to-source %s", ifname, addr); g_free(ifname); if (id < 0) { DBG("failed to add SNAT rule"); return; } err = __connman_firewall_enable_rule(session->fw, id); if (err < 0) { DBG("could not enable SNAT rule"); __connman_firewall_remove_rule(session->fw, id); return; } session->snat_id = id; }
static void add_default_route(struct connman_session *session) { struct connman_ipconfig *ipconfig; int err; struct in_addr addr = { INADDR_ANY }; if (!session->service) return; ipconfig = __connman_service_get_ip4config(session->service); session->index = __connman_ipconfig_get_index(ipconfig); session->gateway = g_strdup(__connman_ipconfig_get_gateway(ipconfig)); if (!session->gateway) session->gateway = g_strdup(inet_ntoa(addr)); session->prefixlen = __connman_ipconfig_get_prefixlen(ipconfig); DBG("index %d routing table %d default gateway %s/%u", session->index, session->mark, session->gateway, session->prefixlen); err = __connman_inet_add_default_to_table(session->mark, session->index, session->gateway); if (err < 0) DBG("session %p %s", session, strerror(-err)); err = __connman_inet_add_subnet_to_table(session->mark, session->index, session->gateway, session->prefixlen); if (err < 0) DBG("session add subnet route %p %s", session, strerror(-err)); }
static int ipv4ll_start_client(struct connman_dhcp *dhcp) { GDHCPClient *ipv4ll_client; GDHCPClientError error; const char *hostname; int index; int err; if (dhcp->ipv4ll_client) return -EALREADY; index = __connman_ipconfig_get_index(dhcp->ipconfig); ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error); if (error != G_DHCP_CLIENT_ERROR_NONE) return -EINVAL; if (getenv("CONNMAN_DHCP_DEBUG")) { dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d", index); g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug, dhcp->ipv4ll_debug_prefix); } g_dhcp_client_set_id(ipv4ll_client); if (dhcp->network) { hostname = connman_utsname_get_hostname(); if (hostname) g_dhcp_client_set_send(ipv4ll_client, G_DHCP_HOST_NAME, hostname); } g_dhcp_client_register_event(ipv4ll_client, G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp); g_dhcp_client_register_event(ipv4ll_client, G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE, ipv4ll_available_cb, dhcp); dhcp->ipv4ll_client = ipv4ll_client; err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL); if (err < 0) { ipv4ll_stop_client(dhcp); return err; } ipv4ll_running = true; return 0; }
static void add_default_route(struct connman_session *session) { struct connman_ipconfig *ipconfig; int err; if (!session->service) return; ipconfig = __connman_service_get_ip4config(session->service); session->index = __connman_ipconfig_get_index(ipconfig); session->gateway = g_strdup(__connman_ipconfig_get_gateway(ipconfig)); DBG("index %d routing table %d default gateway %s", session->index, session->mark, session->gateway); err = __connman_inet_add_default_to_table(session->mark, session->index, session->gateway); if (err < 0) DBG("session %p %s", session, strerror(-err)); }
static void add_nat_rules(struct connman_session *session) { struct connman_ipconfig *ipconfig; struct fw_snat *fw_snat; const char *addr; int index, err; char *ifname; if (!session->service) return; ipconfig = __connman_service_get_ip4config(session->service); index = __connman_ipconfig_get_index(ipconfig); ifname = connman_inet_ifname(index); addr = __connman_ipconfig_get_local(ipconfig); if (!addr) return; g_free(session->addr); session->addr = g_strdup(addr); session->snat_enabled = true; fw_snat = fw_snat_lookup(index, session->addr); if (fw_snat) { fw_snat_ref(session, fw_snat); return; } err = fw_snat_create(session, index, ifname, addr); if (err < 0) { DBG("failed to add SNAT rule"); session->snat_enabled = false; } g_free(ifname); }
static int dhcp_initialize(struct connman_dhcp *dhcp) { GDHCPClient *dhcp_client; GDHCPClientError error; int index; DBG("dhcp %p", dhcp); index = __connman_ipconfig_get_index(dhcp->ipconfig); dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error); if (error != G_DHCP_CLIENT_ERROR_NONE) return -EINVAL; if (getenv("CONNMAN_DHCP_DEBUG")) { dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d", index); g_dhcp_client_set_debug(dhcp_client, dhcp_debug, dhcp->dhcp_debug_prefix); } g_dhcp_client_set_id(dhcp_client); if (dhcp->network) { struct connman_service *service; const char *hostname; service = connman_service_lookup_from_network(dhcp->network); hostname = __connman_service_get_hostname(service); if (!hostname) hostname = connman_utsname_get_hostname(); if (hostname) g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname); g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME); g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER); g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME); g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER); g_dhcp_client_set_request(dhcp_client, 252); g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU); } g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET); g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER); g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE, lease_available_cb, dhcp); g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp); g_dhcp_client_register_event(dhcp_client, G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp); dhcp->dhcp_client = dhcp_client; return 0; }
static bool apply_lease_available_on_network(GDHCPClient *dhcp_client, struct connman_dhcp *dhcp) { char **nameservers, **timeservers, *pac = NULL; struct connman_service *service; GList *list, *option = NULL; int ns_entries; int i; if (!dhcp->network) return true; service = connman_service_lookup_from_network(dhcp->network); if (!service) { connman_error("Can not lookup service"); return false; } option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU); if (option && option->data) { int mtu, index, err; mtu = atoi(option->data); if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) { index = __connman_ipconfig_get_index(dhcp->ipconfig); err = connman_inet_set_mtu(index, mtu); DBG("MTU %d index %d err %d", mtu, index, err); } } option = g_dhcp_client_get_option(dhcp_client, 252); if (option) pac = g_strdup(option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER); ns_entries = g_list_length(option); nameservers = g_try_new0(char *, ns_entries + 1); if (nameservers) { for (i = 0, list = option;list; list = list->next, i++) nameservers[i] = g_strdup(list->data); nameservers[ns_entries] = NULL; } option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME); if (option) __connman_service_set_domainname(service, option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME); if (option) __connman_service_set_hostname(service, option->data); option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER); ns_entries = g_list_length(option); timeservers = g_try_new0(char *, ns_entries + 1); if (timeservers) { for (i = 0, list = option; list; list = list->next, i++) timeservers[i] = g_strdup(list->data); timeservers[ns_entries] = NULL; } if (!compare_string_arrays(nameservers, dhcp->nameservers)) { if (dhcp->nameservers) { for (i = 0; dhcp->nameservers[i]; i++) { __connman_service_nameserver_remove(service, dhcp->nameservers[i], false); } g_strfreev(dhcp->nameservers); } dhcp->nameservers = nameservers; for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) { __connman_service_nameserver_append(service, dhcp->nameservers[i], false); } } else { g_strfreev(nameservers); } if (!compare_string_arrays(timeservers, dhcp->timeservers)) { if (dhcp->timeservers) { for (i = 0; dhcp->timeservers[i]; i++) { __connman_service_timeserver_remove(service, dhcp->timeservers[i]); } g_strfreev(dhcp->timeservers); } dhcp->timeservers = timeservers; for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) { __connman_service_timeserver_append(service, dhcp->timeservers[i]); } } else { g_strfreev(timeservers); } if (g_strcmp0(pac, dhcp->pac) != 0) { g_free(dhcp->pac); dhcp->pac = pac; __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, dhcp->pac); } if (connman_setting_get_bool("Enable6to4")) __connman_6to4_probe(service); return true; }
static void set_vpn_routes(struct gateway_data *new_gateway, struct connman_service *service, const char *gateway, enum connman_ipconfig_type type, const char *peer, struct gateway_data *active_gateway) { struct gateway_config *config; struct connman_ipconfig *ipconfig; char *dest; DBG("new %p service %p gw %s type %d peer %s active %p", new_gateway, service, gateway, type, peer, active_gateway); if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { ipconfig = __connman_service_get_ip4config(service); config = new_gateway->ipv4_gateway; } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) { ipconfig = __connman_service_get_ip6config(service); config = new_gateway->ipv6_gateway; } else return; if (config) { int index = __connman_ipconfig_get_index(ipconfig); struct get_gateway_params *params; config->vpn = true; if (peer) config->vpn_ip = g_strdup(peer); else if (gateway) config->vpn_ip = g_strdup(gateway); params = g_try_malloc(sizeof(struct get_gateway_params)); if (!params) return; params->vpn_index = index; params->vpn_gateway = g_strdup(gateway); /* * Find the gateway that is serving the VPN link */ __connman_inet_get_route(gateway, get_gateway_cb, params); } if (!active_gateway) return; if (type == CONNMAN_IPCONFIG_TYPE_IPV4) { /* * Special route to VPN server via gateway. This * is needed so that we can access hosts behind * the VPN. The route might already exist depending * on network topology. */ if (!active_gateway->ipv4_gateway) return; DBG("active gw %s", active_gateway->ipv4_gateway->gateway); if (g_strcmp0(active_gateway->ipv4_gateway->gateway, "0.0.0.0") != 0) dest = active_gateway->ipv4_gateway->gateway; else dest = NULL; connman_inet_add_host_route(active_gateway->index, gateway, dest); } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) { if (!active_gateway->ipv6_gateway) return; DBG("active gw %s", active_gateway->ipv6_gateway->gateway); if (g_strcmp0(active_gateway->ipv6_gateway->gateway, "::") != 0) dest = active_gateway->ipv6_gateway->gateway; else dest = NULL; connman_inet_add_ipv6_host_route(active_gateway->index, gateway, dest); } }