6 * Copyright (c) 2001-2008 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
48 #define RFCOMM_PPPD "rfcomm_pppd"
50 int rfcomm_channel_lookup (bdaddr_t const *local,
51 bdaddr_t const *remote,
52 int service, int *channel, int *error);
54 static void exec_ppp (int s, char *unit, char *label);
55 static void sighandler (int s);
56 static void usage (void);
62 main(int argc, char *argv[])
64 struct sockaddr_rfcomm sock_addr;
65 char *label = NULL, *unit = NULL, *ep = NULL;
67 int s, channel, detach, server, service,
71 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
79 /* Parse command line arguments */
80 while ((s = getopt(argc, argv, "a:cC:dDhl:sSu:")) != -1) {
82 case 'a': /* BDADDR */
83 if (!bt_aton(optarg, &addr)) {
84 struct hostent *he = NULL;
86 if ((he = bt_gethostbyname(optarg)) == NULL)
87 errx(1, "%s: %s", optarg, hstrerror(h_errno));
89 memcpy(&addr, he->h_addr, sizeof(addr));
93 case 'c': /* client */
97 case 'C': /* RFCOMM channel */
98 channel = strtoul(optarg, &ep, 10);
101 switch (tolower(optarg[0])) {
102 case 'd': /* DialUp Networking */
103 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
106 case 'l': /* LAN Access Using PPP */
107 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
113 case 'd': /* do not detach */
117 case 'D': /* Register DUN service as well as LAN service */
121 case 'l': /* PPP label */
125 case 's': /* server */
129 case 'S': /* Register SP service as well as LAN service */
133 case 'u': /* PPP -unit option */
134 strtoul(optarg, &ep, 10);
149 /* Check if we got everything we wanted */
151 errx(1, "Must specify PPP label");
154 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
155 errx(1, "Must specify server BD_ADDR");
157 /* Check channel, if was not set then obtain it via SDP */
158 if (channel == 0 && service != 0)
159 if (rfcomm_channel_lookup(NULL, &addr, service,
161 errc(1, s, "Could not obtain RFCOMM channel");
164 if (channel <= 0 || channel > 30)
165 errx(1, "Invalid RFCOMM channel number %d", channel);
167 openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
169 if (detach && daemon(0, 0) < 0) {
170 syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
171 strerror(errno), errno);
175 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
177 syslog(LOG_ERR, "Could not create socket. %s (%d)",
178 strerror(errno), errno);
185 sdp_lan_profile_t lan;
187 /* Install signal handler */
188 memset(&sa, 0, sizeof(sa));
189 sa.sa_handler = sighandler;
191 if (sigaction(SIGTERM, &sa, NULL) < 0) {
192 syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
193 strerror(errno), errno);
197 if (sigaction(SIGHUP, &sa, NULL) < 0) {
198 syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
199 strerror(errno), errno);
203 if (sigaction(SIGINT, &sa, NULL) < 0) {
204 syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
205 strerror(errno), errno);
209 sa.sa_handler = SIG_IGN;
210 sa.sa_flags = SA_NOCLDWAIT;
212 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
213 syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)",
214 strerror(errno), errno);
218 /* bind socket and listen for incoming connections */
219 sock_addr.rfcomm_len = sizeof(sock_addr);
220 sock_addr.rfcomm_family = AF_BLUETOOTH;
221 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
222 sizeof(sock_addr.rfcomm_bdaddr));
223 sock_addr.rfcomm_channel = channel;
225 if (bind(s, (struct sockaddr *) &sock_addr,
226 sizeof(sock_addr)) < 0) {
227 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
228 strerror(errno), errno);
232 if (listen(s, 10) < 0) {
233 syslog(LOG_ERR, "Could not listen on socket. %s (%d)",
234 strerror(errno), errno);
238 ss = sdp_open_local(NULL);
240 syslog(LOG_ERR, "Unable to create local SDP session");
244 if (sdp_error(ss) != 0) {
245 syslog(LOG_ERR, "Unable to open local SDP session. " \
246 "%s (%d)", strerror(sdp_error(ss)),
251 memset(&lan, 0, sizeof(lan));
252 lan.server_channel = channel;
254 if (sdp_register_service(ss,
255 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
256 &addr, (void *) &lan, sizeof(lan), NULL) != 0) {
257 syslog(LOG_ERR, "Unable to register LAN service with " \
258 "local SDP daemon. %s (%d)",
259 strerror(sdp_error(ss)), sdp_error(ss));
264 * Register DUN (Dial-Up Networking) service on the same
265 * RFCOMM channel if requested. There is really no good reason
266 * to not to support this. AT-command exchange can be faked
267 * with chat script in ppp.conf
271 sdp_dun_profile_t dun;
273 memset(&dun, 0, sizeof(dun));
274 dun.server_channel = channel;
276 if (sdp_register_service(ss,
277 SDP_SERVICE_CLASS_DIALUP_NETWORKING,
278 &addr, (void *) &dun, sizeof(dun),
280 syslog(LOG_ERR, "Unable to register DUN " \
281 "service with local SDP daemon. " \
282 "%s (%d)", strerror(sdp_error(ss)),
289 * Register SP (Serial Port) service on the same RFCOMM channel
290 * if requested. It appears that some cell phones are using so
291 * called "callback mechanism". In this scenario user is trying
292 * to connect his cell phone to the Internet, and, user's host
293 * computer is acting as the gateway server. It seems that it
294 * is not possible to tell the phone to just connect and start
295 * using the LAN service. Instead the user's host computer must
296 * "jump start" the phone by connecting to the phone's SP
297 * service. What happens next is the phone kills the existing
298 * connection and opens another connection back to the user's
299 * host computer. The phone really wants to use LAN service,
300 * but for whatever reason it looks for SP service on the
301 * user's host computer. This brain damaged behavior was
302 * reported for Nokia 6600 and Sony/Ericsson P900. Both phones
303 * are Symbian-based phones. Perhaps this is a Symbian problem?
309 memset(&sp, 0, sizeof(sp));
310 sp.server_channel = channel;
312 if (sdp_register_service(ss,
313 SDP_SERVICE_CLASS_SERIAL_PORT,
314 &addr, (void *) &sp, sizeof(sp),
316 syslog(LOG_ERR, "Unable to register SP " \
317 "service with local SDP daemon. " \
318 "%s (%d)", strerror(sdp_error(ss)),
324 for (done = 0; !done; ) {
325 socklen_t len = sizeof(sock_addr);
326 int s1 = accept(s, (struct sockaddr *) &sock_addr, &len);
329 syslog(LOG_ERR, "Could not accept connection " \
330 "on socket. %s (%d)", strerror(errno),
336 if (pid == (pid_t) -1) {
337 syslog(LOG_ERR, "Could not fork(). %s (%d)",
338 strerror(errno), errno);
346 /* Reset signal handler */
347 memset(&sa, 0, sizeof(sa));
348 sa.sa_handler = SIG_DFL;
350 sigaction(SIGTERM, &sa, NULL);
351 sigaction(SIGHUP, &sa, NULL);
352 sigaction(SIGINT, &sa, NULL);
353 sigaction(SIGCHLD, &sa, NULL);
359 * XXX Make sure user does not shoot himself
360 * in the foot. Do not pass unit option to the
361 * PPP when operating in the server mode.
364 exec_ppp(s1, NULL, label);
369 sock_addr.rfcomm_len = sizeof(sock_addr);
370 sock_addr.rfcomm_family = AF_BLUETOOTH;
371 memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY,
372 sizeof(sock_addr.rfcomm_bdaddr));
373 sock_addr.rfcomm_channel = 0;
375 if (bind(s, (struct sockaddr *) &sock_addr,
376 sizeof(sock_addr)) < 0) {
377 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
378 strerror(errno), errno);
382 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
383 sizeof(sock_addr.rfcomm_bdaddr));
384 sock_addr.rfcomm_channel = channel;
386 if (connect(s, (struct sockaddr *) &sock_addr,
387 sizeof(sock_addr)) < 0) {
388 syslog(LOG_ERR, "Could not connect socket. %s (%d)",
389 strerror(errno), errno);
393 exec_ppp(s, unit, label);
400 * Redirects stdin/stdout to s, stderr to /dev/null and exec
401 * 'ppp -direct -quiet [-unit N] label'. Never returns.
405 exec_ppp(int s, char *unit, char *label)
407 char ppp[] = "/usr/sbin/ppp";
408 char *ppp_args[] = { ppp, "-direct", "-quiet",
409 NULL, NULL, NULL, NULL };
413 syslog(LOG_ERR, "Could not dup(0). %s (%d)",
414 strerror(errno), errno);
420 syslog(LOG_ERR, "Could not dup(1). %s (%d)",
421 strerror(errno), errno);
426 open("/dev/null", O_RDWR);
429 ppp_args[3] = "-unit";
435 if (execv(ppp, ppp_args) < 0) {
436 syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \
437 "%s (%d)", ppp, (unit != NULL)? " -unit " : "",
438 (unit != NULL)? unit : "", label,
439 strerror(errno), errno);
451 /* Display usage and exit */
456 "Usage: %s options\n" \
457 "Where options are:\n" \
458 "\t-a address Address to listen on or connect to (required for client)\n" \
459 "\t-c Act as a clinet (default)\n" \
460 "\t-C channel RFCOMM channel to listen on or connect to (required)\n" \
461 "\t-d Run in foreground\n" \
462 "\t-D Register Dial-Up Networking service (server mode only)\n" \
463 "\t-l label Use PPP label (required)\n" \
464 "\t-s Act as a server\n" \
465 "\t-S Register Serial Port service (server mode only)\n" \
466 "\t-u N Tell PPP to operate on /dev/tunN (client mode only)\n" \
467 "\t-h Display this message\n", RFCOMM_PPPD);