1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
6 *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
9 *  This program is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#include <stdio.h>
30#include <errno.h>
31#include <ctype.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <stdlib.h>
35#include <syslog.h>
36#include <dirent.h>
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/wait.h>
41#include <sys/param.h>
42#include <sys/ioctl.h>
43#include <sys/socket.h>
44
45#include <netinet/in.h>
46
47#include <bluetooth/bluetooth.h>
48#include <bluetooth/rfcomm.h>
49
50#include "dund.h"
51#include "lib.h"
52
53#define PROC_BASE  "/proc"
54
55static int for_each_port(int (*func)(struct rfcomm_dev_info *, unsigned long), unsigned long arg)
56{
57	struct rfcomm_dev_list_req *dl;
58	struct rfcomm_dev_info *di;
59	long r = 0;
60	int  sk, i;
61
62	sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
63	if (sk < 0 ) {
64		perror("Can't open RFCOMM control socket");
65		exit(1);
66	}
67
68	dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
69	if (!dl) {
70		perror("Can't allocate request memory");
71		close(sk);
72		exit(1);
73	}
74
75	dl->dev_num = RFCOMM_MAX_DEV;
76	di = dl->dev_info;
77
78	if (ioctl(sk, RFCOMMGETDEVLIST, (void *) dl) < 0) {
79		perror("Can't get device list");
80		exit(1);
81	}
82
83	for (i = 0; i < dl->dev_num; i++) {
84		r = func(di + i, arg);
85		if (r) break;
86	}
87
88	close(sk);
89	free(dl);
90	return r;
91}
92
93static int uses_rfcomm(char *path, char *dev)
94{
95	struct dirent *de;
96	DIR   *dir;
97
98	dir = opendir(path);
99	if (!dir)
100		return 0;
101
102	if (chdir(path) < 0)
103		return 0;
104
105	while ((de = readdir(dir)) != NULL) {
106		char link[PATH_MAX + 1];
107		int  len = readlink(de->d_name, link, sizeof(link));
108		if (len > 0) {
109			link[len] = 0;
110			if (strstr(link, dev)) {
111				closedir(dir);
112				return 1;
113			}
114		}
115	}
116
117	closedir(dir);
118
119	return 0;
120}
121
122static int find_pppd(int id, pid_t *pid)
123{
124	struct dirent *de;
125	char  path[PATH_MAX + 1];
126	char  dev[10];
127	int   empty = 1;
128	DIR   *dir;
129
130	dir = opendir(PROC_BASE);
131	if (!dir) {
132		perror(PROC_BASE);
133		return -1;
134	}
135
136	sprintf(dev, "rfcomm%d", id);
137
138	*pid = 0;
139	while ((de = readdir(dir)) != NULL) {
140		empty = 0;
141		if (isdigit(de->d_name[0])) {
142			sprintf(path, "%s/%s/fd", PROC_BASE, de->d_name);
143			if (uses_rfcomm(path, dev)) {
144				*pid = atoi(de->d_name);
145				break;
146			}
147		}
148	}
149	closedir(dir);
150
151	if (empty)
152		fprintf(stderr, "%s is empty (not mounted ?)\n", PROC_BASE);
153
154	return *pid != 0;
155}
156
157static int dun_exec(char *tty, char *prog, char **args)
158{
159	int pid = fork();
160	int fd;
161
162	switch (pid) {
163	case -1:
164		return -1;
165
166	case 0:
167		break;
168
169	default:
170		return pid;
171	}
172
173	setsid();
174
175	/* Close all FDs */
176	for (fd = 3; fd < 20; fd++)
177		close(fd);
178
179	execvp(prog, args);
180
181	syslog(LOG_ERR, "Error while executing %s", prog);
182
183	exit(1);
184}
185
186static int dun_create_tty(int sk, char *tty, int size)
187{
188	struct sockaddr_rc sa;
189	struct stat st;
190	socklen_t alen;
191	int id, try = 30;
192
193	struct rfcomm_dev_req req = {
194		flags:   (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
195		dev_id:  -1
196	};
197
198	alen = sizeof(sa);
199	if (getpeername(sk, (struct sockaddr *) &sa, &alen) < 0)
200		return -1;
201	bacpy(&req.dst, &sa.rc_bdaddr);
202
203	alen = sizeof(sa);
204	if (getsockname(sk, (struct sockaddr *) &sa, &alen) < 0)
205		return -1;
206	bacpy(&req.src, &sa.rc_bdaddr);
207	req.channel = sa.rc_channel;
208
209	id = ioctl(sk, RFCOMMCREATEDEV, &req);
210	if (id < 0)
211		return id;
212
213	snprintf(tty, size, "/dev/rfcomm%d", id);
214	while (stat(tty, &st) < 0) {
215		snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id);
216		if (stat(tty, &st) < 0) {
217			snprintf(tty, size, "/dev/rfcomm%d", id);
218			if (try--) {
219				usleep(100 * 1000);
220				continue;
221			}
222
223			memset(&req, 0, sizeof(req));
224			req.dev_id = id;
225			ioctl(sk, RFCOMMRELEASEDEV, &req);
226
227			return -1;
228		}
229	}
230
231	return id;
232}
233
234int dun_init(void)
235{
236	return 0;
237}
238
239int dun_cleanup(void)
240{
241	return 0;
242}
243
244static int show_conn(struct rfcomm_dev_info *di, unsigned long arg)
245{
246	pid_t pid;
247
248	if (di->state == BT_CONNECTED &&
249		(di->flags & (1<<RFCOMM_REUSE_DLC)) &&
250		(di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
251		(di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
252
253		if (find_pppd(di->id, &pid)) {
254			char dst[18];
255			ba2str(&di->dst, dst);
256
257			printf("rfcomm%d: %s channel %d pppd pid %d\n",
258					di->id, dst, di->channel, pid);
259		}
260	}
261	return 0;
262}
263
264static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg)
265{
266	bdaddr_t *dst = (bdaddr_t *) arg;
267	pid_t pid;
268
269	if (di->state == BT_CONNECTED &&
270		(di->flags & (1<<RFCOMM_REUSE_DLC)) &&
271		(di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
272		(di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
273
274		if (dst && bacmp(&di->dst, dst))
275			return 0;
276
277		if (find_pppd(di->id, &pid)) {
278			if (kill(pid, SIGINT) < 0)
279				perror("Kill");
280
281			if (!dst)
282				return 0;
283			return 1;
284		}
285	}
286	return 0;
287}
288
289int dun_show_connections(void)
290{
291	for_each_port(show_conn, 0);
292	return 0;
293}
294
295int dun_kill_connection(uint8_t *dst)
296{
297	for_each_port(kill_conn, (unsigned long) dst);
298	return 0;
299}
300
301int dun_kill_all_connections(void)
302{
303	for_each_port(kill_conn, 0);
304	return 0;
305}
306
307int dun_open_connection(int sk, char *pppd, char **args, int wait)
308{
309	char tty[100];
310	int  pid;
311
312	if (dun_create_tty(sk, tty, sizeof(tty) - 1) < 0) {
313		syslog(LOG_ERR, "RFCOMM TTY creation failed. %s(%d)", strerror(errno), errno);
314		return -1;
315	}
316
317	args[0] = "pppd";
318	args[1] = tty;
319	args[2] = "nodetach";
320
321	pid = dun_exec(tty, pppd, args);
322	if (pid < 0) {
323		syslog(LOG_ERR, "Exec failed. %s(%d)", strerror(errno), errno);
324		return -1;
325	}
326
327	if (wait) {
328		int status;
329		waitpid(pid, &status, 0);
330		/* FIXME: Check for waitpid errors */
331	}
332
333	return 0;
334}
335