We have moved to a new Sailfish OS Forum. Please start new discussions there.
6

Network sharing from PC to Phone [Answered]

asked 2014-10-15 13:27:31 +0200

Luca gravatar image

updated 2020-04-19 12:57:56 +0200

Sebix gravatar image

Hi all, at the moment I have Sailfish OS on my Nexus 4 (I think I'll buy a Jolla later). I usually connect the phone with usb cable to the PC (Slackware Linux) and use the PC internet connection to allow the phone going to internet.

This is what I do after connecting the phone (USB cable):

On the PC:

su
ifconfig usb0 192.168.2.12
sh /etc/rc.d/rc.ip_forward start
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.2.15 -j MASQUERADE

On the phone after connecting via SSH:

devel-su
route add default gw 192.168.2.12
echo "nameserver 8.8.8.8" > /etc/resolv.conf

This way my phone has an internet connection without connecting to mobile network.

The problem is that some app doesn't go to internet without mobile connection. For example browser works fine and I can access website using PC connection while mail client app always require me to connect to mobile network even if already connected via usb.

What so you think about? Is there a possible solution?

Thanks

edit retag flag offensive close delete

Comments

There is a How To here on TJC to get the internet connection of your Pc through the USB cable but I am not sure if you will suffer the problem thst you are only able to use the internet partly...

Alex ( 2014-10-15 15:07:49 +0200 )edit

What is your /etc/rc.d/rc.ip_forward?

Sebix ( 2016-08-04 22:01:08 +0200 )edit

1 Answer

Sort by » oldest newest most voted
11

answered 2014-10-16 09:54:04 +0200

rainisto gravatar image

updated 2015-05-28 12:09:32 +0200

Edit on latest 1.1.6+ images there has been changes in connman so you dont need to hack connmanctl anymore, just use normal route commands.


Unfortunately some applications check with connman about the network status. So you need to connect your phone to for example non-working open wlan accesspoint, and just use usb connection as default route.

Following is only for advanced users. Don't blame me if you mess up your networking. Remember to take backups of your connman config so you can recover back to normal.

More complicated answer if you really want to do it. Is first remove blacklisted usb and rndis interfaces from /etc/connman/main.conf (and append gadget to PreferredTechnologies line if you want)

And after reboot install connman-tools package, and use "connmanctl services" to get list of your usb gadget line and then use

/usr/lib/connman/tools/connmanctl config gadget_[identifierFromServicescommand]_usb --ipv4 manual 192.168.2.15 255.255.255.0 192.168.2.14 --nameservers 8.8.8.8
/usr/lib/connman/tools/connmanctl connect gadget_[identifierFromServicescommand]_usb

After that connmanctl state should be "ready" and email client should not be asking network connect queries anymore.

Here is example usbnet.sh script that I use on my phone (start with "usbnet.sh start" vs "usbnet.sh stop"):

#!/bin/sh
#### in /etc/networking/interfaces-file I have the following lines in Linux PC:
#allow-hotplug usb0
#auto usb0
#iface usb0 inet static
#        address 192.168.2.14
#        netmask 255.255.255.0
#        up iptables -A POSTROUTING -t nat -s 192.168.2.15/32 -j MASQUERADE
#        up echo 1 > /proc/sys/net/ipv4/ip_forward
#        down iptables -D POSTROUTING -t nat -s 192.168.2.15/32 -j MASQUERADE
#        down echo 0 > /proc/sys/net/ipv4/ip_forward

if [ -f /usr/lib/connman/tools/connmanctl ]; then
  ctl=/usr/lib/connman/tools/connmanctl
else
  echo "please install connman-tools first"
  exit 1
fi
check=`grep usb /etc/connman/main.conf`
if [ "$check" == "" ]; then
  echo /etc/connman/main.conf seems ok.
else
  echo please fix /etc/connman/main.conf by for example running following command:
  echo "# sudo sed -i 's/usb,//g' /etc/connman/main.conf" 
  echo "and reboot machine"
  exit 1
fi

if [ "$*" == "start" ]; then
  $ctl enable gadget > /dev/null 2>&1 
  gadget=`$ctl services|grep gadget|cut -b 26-|tail -1`
  if [ "$gadget" == "" ]; then
    echo "did not find any connected gadget"
  else
    echo "lets rock and roll. Disconnect cellular and wifi connections..."
    $ctl disable wifi > /dev/null 2>&1
    $ctl disable cellular > /dev/null 2>&1
    echo "lets try figure out pc side ip..."
    pc_ip=`netstat -a -n|grep 15:22|cut -d: -f8|tail -1`
    if [ "$pc_ip" == "" ]; then 
      pc_ip=192.168.2.14
      echo "reverting to default 192.168.2.14"
    else
      echo "found it: $pc_ip"
    fi
    $ctl config $gadget --ipv4 manual 192.168.2.15 255.255.255.0 $pc_ip --nameservers 8.8.8.8
    $ctl connect $gadget > /dev/null 2>&1
  fi
else
  echo "stopping usbnet.sh"
  $ctl enable wifi > /dev/null 2>&1
  $ctl enable cellular > /dev/null 2>&1 
fi
edit flag offensive delete publish link more

Comments

Hi rainisto, thanks for the detailed answer. The reason why I like Jolla is the possibility to do what you'd like to do...

This evening I'll try your suggestion and give some feedback.

Luca ( 2014-10-16 16:39:58 +0200 )edit

It doesn't works. After "connect" command the ssh session freeze and I can't navigate. With "ifconfig" in the phone terminal app I see that all network interfaces go down. The only one listed is "lo".

I must unplug and plug again USB to be able to ssh to the phone.

Luca ( 2014-10-16 17:17:29 +0200 )edit

This is the error I get after "connect":

Method "Connect" with signature "" on interface "net.connman.Service" doesn't exist

Luca ( 2014-10-16 17:30:23 +0200 )edit

and you did notice that In my example I used .14 as gw and in your exaple you should use .12? Edited the answer with a sript that should automaticly dig the ssh ip from active connection.

rainisto ( 2014-10-16 17:34:32 +0200 )edit

Yes, I used .12 . I get the same problem. It seems usb0 interface in the phone go down when executed "connect" command. How do you connect to phone before executing connman commands?

Luca ( 2014-10-16 18:04:20 +0200 )edit
Login/Signup to Answer

Question tools

Follow
9 followers

Stats

Asked: 2014-10-15 13:27:31 +0200

Seen: 2,853 times

Last updated: Apr 19 '20