51 #define UDHCP_CONFIG_PATH "/run/usb-moded/udhcpd.conf"
52 #define UDHCP_CONFIG_DIR "/run/usb-moded"
53 #define UDHCP_CONFIG_LINK "/etc/udhcpd.conf"
81 static void ipforward_data_set_dns1 (
ipforward_data_t *
self,
const char *dns);
82 static void ipforward_data_set_dns2 (
ipforward_data_t *
self,
const char *dns);
83 static void ipforward_data_set_nat_interface(
ipforward_data_t *
self,
const char *interface);
90 static gchar *ofono_get_default_modem (
void);
91 static gchar *ofono_get_modem_status (
const char *modem);
92 static bool ofono_get_roaming_status(
void);
100 static bool connman_technology_set_tethering (DBusConnection *con,
const char *technology,
bool on, DBusError *err);
101 static gchar *connman_manager_get_service_path (DBusConnection *con,
const char *type);
102 static bool connman_service_get_connection_data(DBusConnection *con,
const char *service,
ipforward_data_t *ipforward);
104 bool connman_set_tethering (
const char *technology,
bool on);
119 static bool network_interface_exists (
char *interface);
120 static char *network_get_interface (
const modedata_t *data);
122 static void network_cleanup_ip_forwarding(
void);
123 static int network_check_udhcpd_symlink (
void);
134 static const char default_interface[] =
"usb0";
141 ipforward_data_create(
void)
143 LOG_REGISTER_CONTEXT;
149 self->nat_interface = 0;
157 LOG_REGISTER_CONTEXT;
161 ipforward_data_clear(
self);
169 LOG_REGISTER_CONTEXT;
171 ipforward_data_set_dns1(
self, 0);
172 ipforward_data_set_dns2(
self, 0);
173 ipforward_data_set_nat_interface(
self, 0);
179 LOG_REGISTER_CONTEXT;
182 self->dns1 = dns ? g_strdup(dns) : 0;
188 LOG_REGISTER_CONTEXT;
191 self->dns2 = dns ? g_strdup(dns) : 0;
195 ipforward_data_set_nat_interface(
ipforward_data_t *
self,
const char *interface)
197 LOG_REGISTER_CONTEXT;
199 g_free(self->nat_interface),
200 self->nat_interface =
interface ? g_strdup(interface) : 0;
217 ofono_get_default_modem(void)
220 DBusConnection *con = 0;
221 DBusError err = DBUS_ERROR_INIT;
222 DBusMessage *rsp = 0;
224 if( !(con = umdbus_get_connection()) )
227 rsp = umdbus_blocking_call(con,
238 DBusMessageIter body;
239 if( umdbus_parser_init(&body, rsp) ) {
240 DBusMessageIter iter_array;
241 if( umdbus_parser_get_array(&body, &iter_array) ) {
242 DBusMessageIter astruct;
243 if( umdbus_parser_get_struct(&iter_array, &astruct) ) {
244 const char *
object = 0;
245 if( umdbus_parser_get_object(&astruct, &
object) ) {
246 modem = g_strdup(
object);
254 dbus_message_unref(rsp);
257 dbus_connection_unref(con);
259 dbus_error_free(&err);
261 log_warning(
"default modem = %s", modem ?:
"n/a");
275 ofono_get_modem_status(
const char *modem)
278 DBusConnection *con = 0;
279 DBusError err = DBUS_ERROR_INIT;
280 DBusMessage *rsp = 0;
282 if( !(con = umdbus_get_connection()) )
285 rsp = umdbus_blocking_call(con,
288 "org.ofono.NetworkRegistration",
295 DBusMessageIter body;
296 if( umdbus_parser_init(&body, rsp) ) {
297 DBusMessageIter iter_array;
298 if( umdbus_parser_get_array(&body, &iter_array) ) {
299 DBusMessageIter entry;
300 while( umdbus_parser_get_entry(&iter_array, &entry) ) {
302 if( !umdbus_parser_get_string(&entry, &key) )
304 if( strcmp(key,
"Status") )
307 if( !umdbus_parser_get_variant(&entry, &var) )
310 if( !umdbus_parser_get_string(&var, &val) )
312 status = g_strdup(val);
320 dbus_message_unref(rsp);
323 dbus_connection_unref(con);
325 dbus_error_free(&err);
327 log_warning(
"modem status = %s", status ?:
"n/a");
337 ofono_get_roaming_status(
void)
339 LOG_REGISTER_CONTEXT;
341 bool roaming =
false;
345 if( !(modem = ofono_get_default_modem()) )
348 if( !(status = ofono_get_modem_status(modem)) )
351 if( !strcmp(status,
"roaming") )
358 log_warning(
"modem roaming = %d", roaming);
369 # define CONNMAN_SERVICE "net.connman"
370 # define CONNMAN_TECH_INTERFACE "net.connman.Technology"
371 # define CONNMAN_ERROR_ALREADY_ENABLED "net.connman.Error.AlreadyEnabled"
372 # define CONNMAN_ERROR_ALREADY_DISABLED "net.connman.Error.AlreadyDisabled"
388 connman_technology_set_tethering(DBusConnection *con,
const char *technology,
bool on,
391 LOG_REGISTER_CONTEXT;
394 DBusMessage *rsp = 0;
395 const char *key =
"Tethering";
396 dbus_bool_t val = on;
398 rsp = umdbus_blocking_call(con,
401 CONNMAN_TECH_INTERFACE,
404 DBUS_TYPE_STRING, &key,
406 DBUS_TYPE_BOOLEAN, &val,
411 if( !g_strcmp0(err->name, CONNMAN_ERROR_ALREADY_ENABLED) )
415 if( !g_strcmp0(err->name, CONNMAN_ERROR_ALREADY_DISABLED) )
418 log_err(
"%s.%s method call failed: %s: %s",
419 CONNMAN_TECH_INTERFACE,
"SetProperty",
420 err->name, err->message);
425 log_debug(
"%s tethering %s", technology, on ?
"on" :
"off");
426 dbus_error_free(err);
431 dbus_message_unref(rsp);
449 connman_manager_get_service_path(DBusConnection *con,
const char *type)
451 LOG_REGISTER_CONTEXT;
454 DBusError err = DBUS_ERROR_INIT;
455 DBusMessage *rsp = 0;
457 rsp = umdbus_blocking_call(con,
460 "net.connman.Manager",
468 DBusMessageIter body;
469 if( umdbus_parser_init(&body, rsp) ) {
471 DBusMessageIter array_of_structs;
472 if( umdbus_parser_get_array(&body, &array_of_structs) ) {
474 DBusMessageIter astruct;
475 while( umdbus_parser_get_struct(&array_of_structs, &astruct) ) {
477 const char *
object = 0;
478 if( !umdbus_parser_get_object(&astruct, &
object) )
480 DBusMessageIter array_of_entries;
481 if( !umdbus_parser_get_array(&astruct, &array_of_entries) )
484 DBusMessageIter entry;
485 while( umdbus_parser_get_entry(&array_of_entries, &entry) ) {
488 if( !umdbus_parser_get_string(&entry, &key) )
490 if( strcmp(key,
"Type") )
493 if( !umdbus_parser_get_variant(&entry, &var) )
495 const char *value = 0;
496 if( !umdbus_parser_get_string(&var, &value) )
498 if( strcmp(value, type) )
500 service = g_strdup(
object);
509 dbus_message_unref(rsp);
511 dbus_error_free(&err);
513 log_warning(
"%s service = %s", type, service ?:
"n/a");
527 connman_service_get_connection_data(DBusConnection *con,
531 LOG_REGISTER_CONTEXT;
534 DBusMessage *rsp = NULL;
535 DBusError err = DBUS_ERROR_INIT;
537 log_debug(
"Filling in dns data");
539 rsp = umdbus_blocking_call(con,
542 "net.connman.Service",
549 const char *dns1 = 0;
550 const char *dns2 = 0;
551 const char *state = 0;
552 const char *
interface = 0;
554 DBusMessageIter body;
555 if( umdbus_parser_init(&body, rsp) ) {
557 DBusMessageIter array_of_entries;
558 if( umdbus_parser_get_array(&body, &array_of_entries) ) {
560 DBusMessageIter entry;
561 while( umdbus_parser_get_entry(&array_of_entries, &entry) ) {
564 if( !umdbus_parser_get_string(&entry, &key) )
567 if( !umdbus_parser_get_variant(&entry, &var) )
570 if( !strcmp(key,
"Nameservers"))
572 DBusMessageIter array_of_strings;
573 if( umdbus_parser_get_array(&var, &array_of_strings) ) {
575 if( !umdbus_parser_at_end(&array_of_strings) )
576 umdbus_parser_get_string(&array_of_strings, &dns1);
577 if( !umdbus_parser_at_end(&array_of_strings) )
578 umdbus_parser_get_string(&array_of_strings, &dns2);
581 else if( !strcmp(key,
"State"))
583 umdbus_parser_get_string(&var, &state);
585 else if( !strcmp(key,
"Ethernet"))
587 DBusMessageIter array_of_en_entries;
588 if( umdbus_parser_get_array(&var, &array_of_en_entries) ) {
589 DBusMessageIter en_entry;
590 while( umdbus_parser_get_entry(&array_of_en_entries, &en_entry) ) {
591 const char *en_key = 0;
592 if( !umdbus_parser_get_string(&en_entry, &en_key) )
594 if( strcmp(en_key,
"Interface") )
596 DBusMessageIter en_var;
597 if( umdbus_parser_get_variant(&en_entry, &en_var) )
598 umdbus_parser_get_string(&en_var, &interface);
606 bool connected = (!g_strcmp0(state,
"ready") ||
607 !g_strcmp0(state,
"online"));
609 log_debug(
"state = %s", state ?:
"n/a");
610 log_debug(
"connected = %s", connected ?
"true" :
"false");
611 log_debug(
"interface = %s", interface ?:
"n/a");
612 log_debug(
"dns1 = %s", dns1 ?:
"n/a");
613 log_debug(
"dns2 = %s", dns2 ?:
"n/a");
615 if( !dns1 || !interface || !connected )
618 ipforward_data_set_dns1(ipforward, dns1);
619 ipforward_data_set_dns2(ipforward, dns2 ?: dns1);
620 ipforward_data_set_nat_interface(ipforward, interface);
626 dbus_message_unref(rsp);
628 dbus_error_free(&err);
646 LOG_REGISTER_CONTEXT;
649 DBusConnection *con = 0;
653 if( !(con = umdbus_get_connection()) )
657 if( !(cellular = connman_manager_get_service_path(con,
"cellular")) )
658 log_warning(
"no sellular service");
659 else if( connman_service_get_connection_data(con, cellular, ipforward) )
663 if( !(wifi = connman_manager_get_service_path(con,
"wifi")) )
664 log_warning(
"no wifi service");
665 else if( connman_service_get_connection_data(con, wifi, ipforward) )
676 log_warning(
"no connection data");
678 log_debug(
"got connection data");
684 dbus_connection_unref(con);
697 connman_set_tethering(
const char *technology,
bool on)
699 LOG_REGISTER_CONTEXT;
702 DBusError err = DBUS_ERROR_INIT;
703 DBusConnection *con = 0;
705 if( !(con = umdbus_get_connection()) )
708 res = connman_technology_set_tethering(con, technology, on, &err);
711 dbus_error_free(&err);
714 dbus_connection_unref(con);
716 log_debug(
"set tethering(%s) = %s -> %s", technology,
717 on ?
"on" :
"off", res ?
"ack" :
"nak");
735 LOG_REGISTER_CONTEXT;
737 static const char path[] =
"/etc/resolv.conf";
744 if( !(file = fopen(path,
"r")) ) {
745 log_warning(
"%s: can't open for reading: %m", path);
750 while( count < 2 && getline(&buff, &size, file) >= 0 ) {
752 if( strchr(
"#\n", *buff) )
755 gchar **tokens = g_strsplit(buff,
" ", 3);
756 if( !tokens || !tokens[0] || !tokens[1] ) {
759 else if( !g_strcmp0(tokens[0],
"nameserver") ) {
761 g_strstrip(tokens[1]);
763 ipforward_data_set_dns1(ipforward, tokens[1]);
765 ipforward_data_set_dns2(ipforward, tokens[1]);
771 log_warning(
"%s: no nameserver lines found", path);
779 ipforward_data_set_dns2(ipforward, ipforward->
dns1);
802 network_interface_exists(
char *interface)
804 LOG_REGISTER_CONTEXT;
811 snprintf(path,
sizeof path,
"/sys/class/net/%s", interface);
812 ack = (access(path, F_OK) == 0);
827 LOG_REGISTER_CONTEXT;
832 char *setting = config_get_network_setting(NETWORK_INTERFACE_KEY);
834 if( network_interface_exists(setting) )
837 interface = setting, setting = 0;
842 interface = strdup(default_interface);
843 if( !network_interface_exists(interface) )
845 log_warning(
"Neither configured %s nor fallback %s interface exists."
846 " Check your config!",
848 interface ?:
"NULL");
849 free(interface),
interface = 0;
853 log_debug(
"interface = %s", interface ?:
"NULL");
869 LOG_REGISTER_CONTEXT;
873 char *nat_interface = 0;
877 if( !(interface = network_get_interface(data)) )
880 nat_interface = config_get_network_setting(NETWORK_NAT_INTERFACE_KEY);
881 if( !nat_interface ) {
883 log_debug(
"No nat interface available!");
889 write_to_file(
"/proc/sys/net/ipv4/ip_forward",
"1");
891 snprintf(command,
sizeof command,
"/sbin/iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", nat_interface);
892 common_system(command);
894 snprintf(command,
sizeof command,
"/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
895 common_system(command);
897 snprintf(command,
sizeof command,
"/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
898 common_system(command);
900 log_debug(
"ipforwarding success!");
913 network_cleanup_ip_forwarding(
void)
915 LOG_REGISTER_CONTEXT;
917 write_to_file(
"/proc/sys/net/ipv4/ip_forward",
"0");
919 common_system(
"/sbin/iptables -F FORWARD");
927 network_check_udhcpd_symlink(
void)
929 LOG_REGISTER_CONTEXT;
932 char dest[
sizeof UDHCP_CONFIG_PATH + 1];
933 ssize_t rc = readlink(UDHCP_CONFIG_LINK, dest,
sizeof dest - 1);
936 if( errno != ENOENT )
937 log_err(
"%s: can't read symlink: %m", UDHCP_CONFIG_LINK);
939 else if( (
size_t)rc <
sizeof dest ) {
941 if( strcmp(dest, UDHCP_CONFIG_PATH) )
942 log_warning(
"%s: symlink is invalid", UDHCP_CONFIG_LINK);
959 LOG_REGISTER_CONTEXT;
969 if( !(interface = network_get_interface(data)) ) {
970 log_err(
"no network interface");
975 if( !(ip = config_get_network_setting(NETWORK_IP_KEY)) ) {
976 log_err(
"no network address");
981 if( sscanf(ip,
"%*d.%*d.%*d%n.%*d", &len) == EOF || ip[len] !=
'.') {
982 log_err(
"malformed network address: %s", ip);
986 if( !(netmask = config_get_network_setting(NETWORK_NETMASK_KEY)) ) {
987 log_err(
"no network address mask");
992 if( mkdir(UDHCP_CONFIG_DIR, 0775) == -1 && errno != EEXIST ) {
993 log_warning(
"%s: can't create directory: %m", UDHCP_CONFIG_DIR);
997 if( !(conffile = fopen(UDHCP_CONFIG_PATH,
"w")) ) {
998 log_err(
"%s: can't open for writing: %m", UDHCP_CONFIG_PATH);
1002 fprintf(conffile,
"start\t%.*s.1\n", len, ip);
1003 fprintf(conffile,
"end\t%.*s.15\n", len, ip);
1004 fprintf(conffile,
"interface\t%s\n", interface);
1005 fprintf(conffile,
"option\tsubnet\t%s\n", netmask);
1006 fprintf(conffile,
"option\tlease\t3600\n");
1007 fprintf(conffile,
"max_leases\t15\n");
1009 if(ipforward != NULL)
1011 if( !ipforward->
dns1 || !ipforward->
dns2 )
1012 log_debug(
"No dns info!");
1014 fprintf(conffile,
"opt\tdns\t%s %s\n", ipforward->
dns1, ipforward->
dns2);
1015 fprintf(conffile,
"opt\trouter\t%s\n", ip);
1018 fclose(conffile), conffile = 0;
1021 if( network_check_udhcpd_symlink() != 0 ) {
1022 if( unlink(UDHCP_CONFIG_LINK) == -1 && errno != ENOENT )
1023 log_warning(
"%s: can't remove invalid config: %m", UDHCP_CONFIG_LINK);
1025 if( symlink(UDHCP_CONFIG_PATH, UDHCP_CONFIG_LINK) == -1 ) {
1026 log_err(
"%s: can't create symlink to %s: %m",
1027 UDHCP_CONFIG_LINK, UDHCP_CONFIG_PATH);
1030 log_debug(
"%s: symlink to %s created",
1031 UDHCP_CONFIG_LINK, UDHCP_CONFIG_PATH);
1061 LOG_REGISTER_CONTEXT;
1071 if( ofono_get_roaming_status() ) {
1073 if(config_is_roaming_not_allowed())
1078 ipforward = ipforward_data_create();
1081 if( !connman_get_connection_data(ipforward) )
1083 log_debug(
"data connection not available from connman!");
1088 if( !legacy_get_connection_data(ipforward) ) {
1089 log_debug(
"data connection not available in resolv.conf!");
1096 ret = network_write_udhcpd_config(data, ipforward);
1098 if( ret == 0 && data->
nat )
1099 ret = network_setup_ip_forwarding(data, ipforward);
1102 ipforward_data_delete(ipforward);
1116 LOG_REGISTER_CONTEXT;
1121 gchar *
interface = 0;
1128 if( !(interface = network_get_interface(data)) ) {
1129 log_err(
"no network interface");
1133 if( !(address = config_get_network_setting(NETWORK_IP_KEY)) ) {
1134 log_err(
"no network address");
1138 if( !(netmask = config_get_network_setting(NETWORK_NETMASK_KEY)) ) {
1139 log_err(
"no network address mask");
1143 if( !(gateway = config_get_network_setting(NETWORK_GATEWAY_KEY)) ) {
1145 log_warning(
"no network gateway");
1148 if( !strcmp(address,
"dhcp") )
1150 snprintf(command,
sizeof command,
"dhclient -d %s", interface);
1151 if( common_system(command) != 0 ) {
1152 snprintf(command,
sizeof command,
"udhcpc -i %s", interface);
1153 if( common_system(command) != 0 )
1159 snprintf(command,
sizeof command,
"ifconfig %s %s netmask %s", interface, address, netmask);
1160 if( common_system(command) != 0 )
1167 snprintf(command,
sizeof command,
"route add default gw %s", gateway);
1168 if( common_system(command) != 0 )
1175 log_debug(
"iface=%s addr=%s mask=%s gw=%s -> %s",
1180 ret ?
"failure" :
"success");
1196 LOG_REGISTER_CONTEXT;
1198 gchar *
interface = network_get_interface(data);
1202 log_debug(
"iface=%s nat=%d", interface ?:
"n/a", data->
nat);
1205 snprintf(command,
sizeof command,
"ifconfig %s down", interface);
1206 common_system(command);
1211 network_cleanup_ip_forwarding();
1223 LOG_REGISTER_CONTEXT;