diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6262ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +*.swp diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..423cdab --- /dev/null +++ b/LICENCE @@ -0,0 +1,20 @@ +Copyright (c) 2018 Jérémy Farnaud + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..034cbd0 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# bt-tether + +## Description + +**bt-tether** is a small script that enables Bluetooth Tethering on your Jolla Phone. I could not find any software that did that realiably so I tried my hand at it. + +It is **quite experimental** and a little hacky. It’s only been tested on my Jolla Phone in developer mode. I’m putting it here without any warranty, however I’d be glad to receive some feedback and I’ll do my best to improve it. + +**bt-tether** can connect several devices at once to whatever is the current connection of the phone (Mobile or WLAN). It uses the `pand` daemon that should be installed already, along with specially crafted scripts and an adhoc DNS relay. + +## Licence + +This software is distributed under the terms of the MIT Lincence. + diff --git a/files/etc/systemd/system/bt-tether.service b/files/etc/systemd/system/bt-tether.service new file mode 100644 index 0000000..dad20b6 --- /dev/null +++ b/files/etc/systemd/system/bt-tether.service @@ -0,0 +1,11 @@ +[Unit] +Description=Bluetooth Tethering + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/bt-tether +RemainAfterExit=yes + +[Install] +WantedBy=bluetooth.target + diff --git a/files/usr/local/bin/bt-tether b/files/usr/local/bin/bt-tether new file mode 100755 index 0000000..52079f5 --- /dev/null +++ b/files/usr/local/bin/bt-tether @@ -0,0 +1,5 @@ +#!/bin/sh + +mkdir /var/tmp/bt-tether +pand --listen --role NAP --master --devup /usr/local/lib/bt-tether/devup --devdown /usr/local/lib/bt-tether/devdown + diff --git a/files/usr/local/lib/bt-tether/bluetooth-config b/files/usr/local/lib/bt-tether/bluetooth-config new file mode 100755 index 0000000..478bde5 --- /dev/null +++ b/files/usr/local/lib/bt-tether/bluetooth-config @@ -0,0 +1,16 @@ +#!/bin/sh + +if [ "install" = "$1" ]; then + + echo -e "##bt-tether##begin##\nDisablePlugins = network\n##bt-tether##end##" >> /etc/bluetooth/main.conf + +elif [ "uninstall" = "$1" ]; then + + sed -i -e "/##bt-tether##begin##/,/##bt-tether##end##/ d" /etc/bluetooth/main.conf + +else + echo "Usage: $0 install | uninstall" + exit 1 +fi + + diff --git a/files/usr/local/lib/bt-tether/common b/files/usr/local/lib/bt-tether/common new file mode 100644 index 0000000..98e4983 --- /dev/null +++ b/files/usr/local/lib/bt-tether/common @@ -0,0 +1,7 @@ +IF=$1 +SUBNET=10.64.$(( 100 + ${IF#bnep} )) +DNSRELAY_PID=/var/tmp/bt-tether/$IF.dns-relay +UDHCPD_CONF=/var/tmp/bt-tether/$IF.udhcpd +UDHCPD_PID=$UDHCPD_CONF.pid +UDHCPD_LEASE=$UDHCPD_CONF.lease + diff --git a/files/usr/local/lib/bt-tether/devdown b/files/usr/local/lib/bt-tether/devdown new file mode 100755 index 0000000..e47cd1f --- /dev/null +++ b/files/usr/local/lib/bt-tether/devdown @@ -0,0 +1,11 @@ +#!/bin/sh + +. "$( dirname $0 )"/common + +kill -- "$( cat $UDHCPD_PID )" +kill -- "$( cat $DNSRELAY_PID )" + +rm $DNSRELAY_PID $UDHCPD_PID $UDHCPD_LEASE $UDHCPD_CONF + +iptables -t nat -D POSTROUTING -s $SUBNET.0/24 -j MASQUERADE + diff --git a/files/usr/local/lib/bt-tether/devup b/files/usr/local/lib/bt-tether/devup new file mode 100755 index 0000000..63c7f9f --- /dev/null +++ b/files/usr/local/lib/bt-tether/devup @@ -0,0 +1,26 @@ +#!/bin/sh + +. "$( dirname $0 )"/common + +ifconfig $IF $SUBNET.1 netmask 255.255.255.0 broadcast $SUBNET.255 up + +cat >$UDHCPD_CONF < /proc/sys/net/ipv4/ip_forward +iptables -t nat -A POSTROUTING -s $SUBNET.0/24 -j MASQUERADE + +udhcpd $UDHCPD_CONF +/usr/local/lib/bt-tether/dns-relay -f $SUBNET.1 > $DNSRELAY_PID + diff --git a/rpm/bt-tether.spec.gen b/rpm/bt-tether.spec.gen new file mode 100755 index 0000000..0514cf9 --- /dev/null +++ b/rpm/bt-tether.spec.gen @@ -0,0 +1,51 @@ +#!/bin/sh + +VERSION="$1" +RELEASE="${2:-1}" + +cat <<__EOF__ + +Name: bt-tether +Version: ${VERSION} +Release: 1 +Summary: Bluetooth Tethering +Group: Miscellaneous +License: MIT +URL: https://jf.almel.fr +Source: bt-tether-${VERSION}-${RELEASE}.tgz + +%description +This package will enable BlueTooth tethering on SailfishOS. + +%prep +%setup -n bt-tether-${VERSION}-${RELEASE} + +%build +cd src +make + +%install +mkdir -p %{buildroot} +cp -r files/* %{buildroot} +cp src/dns-relay %{buildroot}/usr/local/lib/bt-tether + +%files +/etc +/usr + +%clean + +%post +/usr/local/lib/bt-tether/bluetooth-config install +systemctl enable bt-tether.service +systemctl start bt-tether.service +systemctl restart bluetooth.service + +%preun +/usr/local/lib/bt-tether/bluetooth-config uninstall +systemctl stop bt-tether.service +systemctl disable bt-tether.service +systemctl restart bluetooth.service + +__EOF__ + diff --git a/rpm/prep b/rpm/prep new file mode 100755 index 0000000..c8bf62d --- /dev/null +++ b/rpm/prep @@ -0,0 +1,26 @@ +#!/bin/sh -e + +NAME=bt-tether +VERSION="$1" +RELEASE=${2:-1} +DST="$NAME-$VERSION-$RELEASE" + +if [ -z "$VERSION" ]; then + printf "Usage: $0 version [release]\n" + exit 1 +fi + +cd "$(dirname "$0")"/.. + +mkdir -p target + +rm -rf "target/$DST" "target/$DST.tgz" +mkdir -p "target/$DST" +cp -r files src "target/$DST" +mkdir -p "target/$DST/files/usr/local/share/doc/bt-tether" +cp LICENCE README.md "target/$DST/files/usr/local/share/doc/bt-tether" +tar -C target -zcf "target/$DST.tgz" "$DST" +rm -rf "target/$DST" + +rpm/bt-tether.spec.gen $VERSION $RELEASE > "target/$DST.spec" + diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..5ce85ae --- /dev/null +++ b/src/Makefile @@ -0,0 +1,3 @@ +dns-relay: dns-relay.c + $(CC) -std=c99 -Wall -Wextra -pedantic -o $@ $^ + diff --git a/src/dns-relay.c b/src/dns-relay.c new file mode 100644 index 0000000..119dd72 --- /dev/null +++ b/src/dns-relay.c @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2018 Jérémy Farnaud + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_REQ_SIZE 2048 +#define MAX_SOCKETS 256 +#define REQ_TIMEOUT 10 + +struct ret_info_t { + struct sockaddr_in addr; + time_t timeout; +}; + +struct sockaddr_in relay_addr; +struct sockaddr_in server_addr; + +struct pollfd fds[MAX_SOCKETS]; +struct ret_info_t ret_info[MAX_SOCKETS]; +int nfds = 0; + +void usage(char *name) { + printf("Usage: %s [-f] \n", name); + exit(1); +} + +int fds_add(int fd, short events) { + if (nfds == MAX_SOCKETS) return -1; + + fds[nfds].fd = fd; + fds[nfds].events = events; + fds[nfds].revents = 0; + return nfds++; +} + +int fds_add_with_ri(int fd, short events, struct sockaddr_in addr, time_t timeout) { + int i = fds_add(fd, events); + if (i != -1) { + ret_info[i].addr = addr; + ret_info[i].timeout = timeout; + } + return i; +} + +void fds_remove(int i) { + memmove(&fds[i], &fds[i + 1], sizeof(struct pollfd) * (nfds - i - 1)); + memmove(&ret_info[i], &ret_info[i + 1], sizeof(struct ret_info_t) * (nfds - i - 1)); + --nfds; +} + +int main(int argc, char **argv) { + int ip_arg_n = 1; + int do_fork = 0; + + if (argc > ip_arg_n && strcmp("-f", argv[1]) == 0) { + do_fork = 1; + ++ip_arg_n; + } + + relay_addr.sin_family = AF_INET; + relay_addr.sin_port = htons(53); + + if (argc <= ip_arg_n || !inet_pton(AF_INET, argv[ip_arg_n], &relay_addr.sin_addr)) { + usage(argv[0]); + } + + int relay_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (relay_fd == -1) { + perror("relay socket"); + exit(10); + } + if (bind(relay_fd, (struct sockaddr*)&relay_addr, sizeof(relay_addr))) { + perror("relay bind"); + exit(11); + } + + if (do_fork) { + pid_t pid = fork(); + if (pid != 0) { + printf("%d\n", pid); + exit(0); + } + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } + + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(53); + server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + fds_add(relay_fd, POLLIN); + + for (;;) { + if (poll(fds, nfds, REQ_TIMEOUT * 1000) == -1) { + if (!do_fork) perror("poll"); + exit(201); + } + + time_t now = time(NULL); + + for (int i = 1; i < nfds;) { + if (fds[i].revents == 0 && ret_info[i].timeout > now) { + // We are still waiting for an answer and timeout is still later + ++i; + continue; + } + + if (fds[i].revents == POLLIN) { + char buf[MAX_REQ_SIZE]; + ssize_t len = recv(fds[i].fd, buf, sizeof(buf), MSG_WAITALL); + if (len > 0) { + ssize_t sent_len = sendto( + relay_fd, buf, len, 0, + (struct sockaddr*)&ret_info[i].addr, sizeof(struct sockaddr_in)); + if (sent_len < 0) { + if (!do_fork) perror("relay sendto"); + } + } + } + + // Either we had an answer or the answer has timed out, remove fd from list + close(fds[i].fd); + fds_remove(i); + } + + // Check on relay_fd if we received a request + if ((fds[0].revents & POLLERR) != 0) exit(200); + if ((fds[0].revents & POLLIN) != 0) { + struct sockaddr_in from; + socklen_t from_len = sizeof(from); + char buf[MAX_REQ_SIZE]; + ssize_t len = recvfrom( + relay_fd, buf, sizeof(buf), MSG_WAITALL, (struct sockaddr*)&from, &from_len); + + // We’ll ignore empty packets and packets received when the fds is full + if (len > 0 && nfds < MAX_SOCKETS) { + // Forward request to server + int server_fd = socket(AF_INET, SOCK_DGRAM, 0); + connect(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)); + ssize_t send_len = send(server_fd, buf, len, 0); + if (send_len < 0) { + if (!do_fork) perror("server sendto"); + close(server_fd); + } else { + fds_add_with_ri(server_fd, POLLIN, from, now + REQ_TIMEOUT); + } + } else if (len < 0) { + if (!do_fork) perror("recvfrom"); + } + } + } + + return 0; +} +