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[] = {
56 // Run loop active flag
58 // Number of active tethering connections
59 static unsigned int active;
61 static void siginthandler(int sig) {
62 fprintf(stderr, "SIGINIT received, exiting\n");
66 static int launch(const char *args[]) {
69 if (execv(args[0], (char **const) args) == -1) {
70 fprintf(stderr, "Error launching external process %s: %s\n", args[0], strerror(errno));
73 } else if (pid == -1) {
74 fprintf(stderr, "Can't fork external process %s: %s\n", args[0], strerror(errno));
78 if (waitpid(pid, &status, 0) == -1) {
79 perror("Error waiting for child process completion");
82 if (WIFEXITED(status)) {
83 if (WEXITSTATUS(status) != 0) {
84 fprintf(stderr, "Child process returned error: %d\n", WEXITSTATUS(status));
87 } else if (WIFSIGNALED(status)) {
88 fprintf(stderr, "Child process killed by signal: %d\n", WTERMSIG(status));
95 static void added(Device *device, const char *inetdev) {
96 printf("Got org.kernel.kevent.add on %s\n", device->name);
98 if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
99 fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
103 if (asprintf(&range, "%s-%s,3600", device->startaddress, device->endaddress) == -1) {
104 fprintf(stderr, "Can't construct address range parameter for device %s: %s\n", device->name, strerror(errno));
108 const char *ifconfig[] = { "/sbin/ifconfig", device->name, device->address, "up", NULL };
109 const char *modprobe[] = { "/sbin/modprobe", "ipt_MASQUERADE", NULL };
110 const char *iptables[] = { "/usr/sbin/iptables", "-t", "nat", "-A", "POSTROUTING", "-o", inetdev, "-j", "MASQUERADE", NULL };
111 const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-S", "-p", runfile, "-m", "-b", "-x", "/usr/sbin/dnsmasq", "--", "-k", "-I", "lo", "-i", device->name, "-a", device->address, "-z", "-F", range, NULL };
112 char *forwsysctl = NULL;
113 if (asprintf(&forwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", device->name) == -1) {
114 fprintf(stderr, "Can't construct sysctl path for device %s: %s\n", device->name, strerror(errno));
119 char *inetforwsysctl = NULL;
120 if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
121 fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
129 if (launch(ifconfig) == 0) {
130 if (launch(modprobe) == 0) {
131 if (launch(iptables) == 0) {
132 if (launch(dnsmasq) == 0) {
133 int ffd = open(forwsysctl, O_WRONLY, 0666);
135 fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
137 if (write(ffd, "1", 1) == -1) {
138 fprintf(stderr, "Can't enable forwarding on PAN device %s: %s\n", device->name, strerror(errno));
140 int ifd = open(inetforwsysctl, O_WRONLY, 0666);
142 fprintf(stderr, "Can't enable forwarding on WAN device %s (path %s): %s\n", inetdev, inetforwsysctl, strerror(errno));
144 if (write(ifd, "1", 1) == -1) {
145 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
160 free(inetforwsysctl);
165 static void removed(Device *device, const char *inetdev) {
166 printf("Got org.kernel.kevent.remove on %s\n", device->name);
168 char *runfile = NULL;
169 if (asprintf(&runfile, "/var/run/tethering.%s.pid", device->name) == -1) {
170 fprintf(stderr, "Can't construct PID file name for device %s: %s\n", device->name, strerror(errno));
173 const char *dnsmasq[] = { "/sbin/start-stop-daemon", "-K", "-p", runfile, "-x", "/usr/sbin/dnsmasq", NULL };
174 const char *iptables[] = { "/usr/sbin/iptables", "-t", "nat", "-D", "POSTROUTING", "-o", inetdev, "-j", "MASQUERADE", NULL };
176 // Errors are ignored here: we just disable as much as we can.
182 if (active > 0) active--;
184 char *inetforwsysctl = NULL;
185 if (asprintf(&inetforwsysctl, "/proc/sys/net/ipv4/conf/%s/forwarding", inetdev) == -1) {
186 fprintf(stderr, "Can't construct inet sysctl path for device %s: %s\n", inetdev, strerror(errno));
189 int fd = open(inetforwsysctl, O_WRONLY, 0666);
191 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
193 if (write(fd, "0", 1) == -1) {
194 fprintf(stderr, "Can't enable forwarding on WAN device %s: %s\n", inetdev, strerror(errno));
198 free(inetforwsysctl);
202 int main(int argc, const char *argv[]) {
205 signal(SIGINT, siginthandler);
207 Device *devices = NULL;
210 for (i = 0; DEVICES[i]; i++) {
211 Device *device = device_new(DEVICES[i]);
213 device_set_address(device, ADDRESSES[i]);
214 device_set_startaddress(device, STARTADDRESSES[i]);
215 device_set_endaddress(device, ENDADDRESSES[i]);
216 if (device_validate(device)) {
217 node = device_append(node, device);
222 device_delete(device);
228 fprintf(stderr, "Warning, no devices configured. I will just sit here and wait for nicer weather.\n");
232 dbus_error_init(&err);
233 DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
234 if (dbus_error_is_set(&err)) {
235 fprintf(stderr, "Error %s occured: %s\n", err.name, err.message);
236 dbus_error_free(&err);
242 for (node = devices; node; node = node->next) {
244 if (!asprintf(&filter, "type='signal',interface='org.kernel.kevent',path='/org/kernel/class/net/%s'", node->name)) {
245 fprintf(stderr, "Can't construct filter rule for device %s: %s\n", node->name, strerror(errno));
247 dbus_bus_add_match(conn, filter, NULL);
251 dbus_connection_flush(conn);
254 dbus_connection_read_write(conn, 500);
255 DBusMessage* msg = dbus_connection_pop_message(conn);
257 if (dbus_message_is_signal(msg, "org.kernel.kevent", "add")) {
259 if (!dbus_message_get_path_decomposed(msg, &path)) {
260 fprintf(stderr, "Can't get add message path!\n");
263 for (last = 0; path[last] && last <= 6; last++);
264 if (last == 0 || last > 6) {
265 fprintf(stderr, "Add message has no path or path too long!\n");
267 Device *device = device_search(devices, path[last - 1]);
269 added(device, INETDEV);
272 dbus_free_string_array(path);
274 } else if (dbus_message_is_signal(msg, "org.kernel.kevent", "remove")) {
276 if (!dbus_message_get_path_decomposed(msg, &path)) {
277 fprintf(stderr, "Can't get remove message path!\n");
280 for (last = 0; path[last] && last <= 6; last++);
281 if (last == 0 || last > 6) {
282 fprintf(stderr, "Remove message has no path or path too long!\n");
284 Device *device = device_search(devices, path[last - 1]);
286 removed(device, INETDEV);
289 dbus_free_string_array(path);
292 dbus_message_unref(msg);
296 device_delete_all(devices);
297 dbus_connection_unref(conn);