3 (c) 2010 Gregor Riepl <onitake@gmail.com>
5 Tethering utility for Maemo
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
31 #include <dbus/dbus.h>
34 static const char *INETDEV = "gprs0";
35 static const char *DEVICES[] = {
40 static const char *ADDRESSES[] = {
45 static const char *STARTADDRESSES[] = {
50 static const char *ENDADDRESSES[] = {
55 // Wait x seconds for process termination
56 static const unsigned int TERMINATE_WAIT = 5;
57 // Poll the main loop after x milliseconds
58 static const unsigned int POLL_MAINLOOP = 500;
60 // Run loop active flag
62 // Number of active tethering connections
63 static unsigned int active;
65 static void siginthandler(int sig) {
67 fprintf(stderr, "SIGINIT received, exiting\n");
70 fprintf(stderr, "Another SIGINIT received, aborting\n");
71 if (kill(getpid(), SIGKILL) == -1) {
72 fprintf(stderr, "PANIC! Error killing self: %s\n", strerror(errno));
77 static int launch(const char *args[]) {
80 if (execv(args[0], (char **const) args) == -1) {
81 fprintf(stderr, "Error launching external process %s: %s\n", args[0], strerror(errno));
84 } else if (pid == -1) {
85 fprintf(stderr, "Can't fork external process %s: %s\n", args[0], strerror(errno));
89 if (waitpid(pid, &status, WNOHANG) == -1) {
90 fprintf(stderr, "Error waiting for child process completion: %s\n", strerror(errno));
94 for (i = 0; i < TERMINATE_WAIT && !WIFEXITED(status); i++) {
95 if (WIFEXITED(status)) {
96 if (WEXITSTATUS(status) != 0) {
97 fprintf(stderr, "Child process returned error: %d\n", WEXITSTATUS(status));
100 } else if (WIFSIGNALED(status)) {
101 fprintf(stderr, "Child process killed by signal: %d\n", WTERMSIG(status));
105 if (waitpid(pid, &status, WNOHANG) == -1) {
106 fprintf(stderr, "Error waiting for child process completion: %s\n", strerror(errno));
110 if (i >= TERMINATE_WAIT) {
111 fprintf(stderr, "Child process %s still running after %u seconds, ignoring.\n", args[0], TERMINATE_WAIT);
118 static void added(Device *device, const char *inetdev) {
119 printf("Got org.kernel.kevent.add on %s\n", device->name);
120 char *runfile = NULL;
121 if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
122 fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
126 if (asprintf(&range, "%s,%s,3600", device->startaddress, device->endaddress) == -1) {
127 fprintf(stderr, "Can't construct address range parameter for device %s: %s\n", device->name, strerror(errno));
131 const char *ifconfig[] = { "/sbin/ifconfig", device->name, device->address, "up", NULL };
132 const char *modprobe[] = { "/sbin/modprobe", "ipt_MASQUERADE", NULL };
133 const char *iptables[] = { "/usr/sbin/iptables", "-t", "nat", "-A", "POSTROUTING", "-o", inetdev, "-j", "MASQUERADE", NULL };
134 const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-S", "-p", runfile, "-b", "-x", "/usr/sbin/dnsmasq", "--", "-x", runfile, "-k", "-I", "lo", "-i", device->name, "-a", device->address, "-z", "-F", range, NULL };
135 char *forwsysctl = NULL;
136 if (asprintf(&forwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", device->name) == -1) {
137 fprintf(stderr, "Can't construct sysctl path for device %s: %s\n", device->name, strerror(errno));
142 char *inetforwsysctl = NULL;
143 if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
144 fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
152 if (launch(ifconfig) == 0) {
153 if (launch(modprobe) == 0) {
154 if (launch(iptables) == 0) {
155 if (launch(dnsmasq) == 0) {
156 int ffd = open(forwsysctl, O_WRONLY, 0666);
158 fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
160 if (write(ffd, "1", 1) == -1) {
161 fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
163 int ifd = open(inetforwsysctl, O_WRONLY, 0666);
165 fprintf(stderr, "Can't enable forwarding on WAN device %s (path %s): %s\n", inetdev, inetforwsysctl, strerror(errno));
167 if (write(ifd, "1", 1) == -1) {
168 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
183 free(inetforwsysctl);
188 static void removed(Device *device, const char *inetdev) {
189 printf("Got org.kernel.kevent.remove on %s\n", device->name);
191 char *runfile = NULL;
192 if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
193 fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
196 const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-K", "-p", runfile, "-x", "/usr/sbin/dnsmasq", NULL };
197 const char *iptables[] = { "/usr/sbin/iptables", "-t", "nat", "-D", "POSTROUTING", "-o", inetdev, "-j", "MASQUERADE", NULL };
199 // Errors are ignored here: we just disable as much as we can.
201 if (unlink(runfile) == -1) {
202 fprintf(stderr, "Error removing PID file for device %s: %s\n", device->name, strerror(errno));
208 if (active > 0) active--;
210 char *inetforwsysctl = NULL;
211 if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
212 fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
215 int fd = open(inetforwsysctl, O_WRONLY, 0666);
217 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
219 if (write(fd, "0", 1) == -1) {
220 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
224 free(inetforwsysctl);
228 int main(int argc, const char *argv[]) {
231 Device *devices = NULL;
234 for (i = 0; DEVICES[i]; i++) {
235 Device *device = device_new(DEVICES[i]);
237 device_set_address(device, ADDRESSES[i]);
238 device_set_startaddress(device, STARTADDRESSES[i]);
239 device_set_endaddress(device, ENDADDRESSES[i]);
240 if (device_validate(device)) {
241 node = device_append(node, device);
246 device_delete(device);
252 fprintf(stderr, "Warning, no devices configured. I will just sit here and wait for nicer weather.\n");
256 dbus_error_init(&err);
257 DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
258 if (dbus_error_is_set(&err)) {
259 fprintf(stderr, "Error %s occured: %s\n", err.name, err.message);
260 dbus_error_free(&err);
266 for (node = devices; node; node = node->next) {
268 if (!asprintf(&filter, "type='signal',interface='org.kernel.kevent',path='/org/kernel/class/net/%s'", node->name)) {
269 fprintf(stderr, "Can't construct filter rule for device %s: %s\n", node->name, strerror(errno));
271 dbus_bus_add_match(conn, filter, NULL);
275 dbus_connection_flush(conn);
278 signal(SIGINT, siginthandler);
280 dbus_connection_read_write(conn, POLL_MAINLOOP);
281 DBusMessage* msg = dbus_connection_pop_message(conn);
283 if (dbus_message_is_signal(msg, "org.kernel.kevent", "add")) {
285 if (!dbus_message_get_path_decomposed(msg, &path)) {
286 fprintf(stderr, "Can't get add message path!\n");
289 for (last = 0; path[last] && last <= 6; last++);
290 if (last == 0 || last > 6) {
291 fprintf(stderr, "Add message has no path or path too long!\n");
293 Device *device = device_search(devices, path[last - 1]);
295 added(device, INETDEV);
298 dbus_free_string_array(path);
300 } else if (dbus_message_is_signal(msg, "org.kernel.kevent", "remove")) {
302 if (!dbus_message_get_path_decomposed(msg, &path)) {
303 fprintf(stderr, "Can't get remove message path!\n");
306 for (last = 0; path[last] && last <= 6; last++);
307 if (last == 0 || last > 6) {
308 fprintf(stderr, "Remove message has no path or path too long!\n");
310 Device *device = device_search(devices, path[last - 1]);
312 removed(device, INETDEV);
315 dbus_free_string_array(path);
318 dbus_message_unref(msg);
322 device_delete_all(devices);
323 dbus_connection_unref(conn);