6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2001-2008 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
50 #define RFCOMM_PPPD "rfcomm_pppd"
52 int rfcomm_channel_lookup (bdaddr_t const *local,
53 bdaddr_t const *remote,
54 int service, int *channel, int *error);
56 static void exec_ppp (int s, char *unit, char *label);
57 static void sighandler (int s);
58 static void usage (void);
64 main(int argc, char *argv[])
66 struct sockaddr_rfcomm sock_addr;
67 char *label = NULL, *unit = NULL, *ep = NULL;
69 int s, channel, detach, server, service,
73 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
81 /* Parse command line arguments */
82 while ((s = getopt(argc, argv, "a:cC:dDhl:sSu:")) != -1) {
84 case 'a': /* BDADDR */
85 if (!bt_aton(optarg, &addr)) {
86 struct hostent *he = NULL;
88 if ((he = bt_gethostbyname(optarg)) == NULL)
89 errx(1, "%s: %s", optarg, hstrerror(h_errno));
91 memcpy(&addr, he->h_addr, sizeof(addr));
95 case 'c': /* client */
99 case 'C': /* RFCOMM channel */
100 channel = strtoul(optarg, &ep, 10);
103 switch (tolower(optarg[0])) {
104 case 'd': /* DialUp Networking */
105 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
108 case 'l': /* LAN Access Using PPP */
109 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
115 case 'd': /* do not detach */
119 case 'D': /* Register DUN service as well as LAN service */
123 case 'l': /* PPP label */
127 case 's': /* server */
131 case 'S': /* Register SP service as well as LAN service */
135 case 'u': /* PPP -unit option */
136 strtoul(optarg, &ep, 10);
151 /* Check if we got everything we wanted */
153 errx(1, "Must specify PPP label");
156 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
157 errx(1, "Must specify server BD_ADDR");
159 /* Check channel, if was not set then obtain it via SDP */
160 if (channel == 0 && service != 0)
161 if (rfcomm_channel_lookup(NULL, &addr, service,
163 errc(1, s, "Could not obtain RFCOMM channel");
166 if (channel <= 0 || channel > 30)
167 errx(1, "Invalid RFCOMM channel number %d", channel);
169 openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
171 if (detach && daemon(0, 0) < 0) {
172 syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
173 strerror(errno), errno);
177 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
179 syslog(LOG_ERR, "Could not create socket. %s (%d)",
180 strerror(errno), errno);
187 sdp_lan_profile_t lan;
189 /* Install signal handler */
190 memset(&sa, 0, sizeof(sa));
191 sa.sa_handler = sighandler;
193 if (sigaction(SIGTERM, &sa, NULL) < 0) {
194 syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
195 strerror(errno), errno);
199 if (sigaction(SIGHUP, &sa, NULL) < 0) {
200 syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
201 strerror(errno), errno);
205 if (sigaction(SIGINT, &sa, NULL) < 0) {
206 syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
207 strerror(errno), errno);
211 sa.sa_handler = SIG_IGN;
212 sa.sa_flags = SA_NOCLDWAIT;
214 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
215 syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)",
216 strerror(errno), errno);
220 /* bind socket and listen for incoming connections */
221 sock_addr.rfcomm_len = sizeof(sock_addr);
222 sock_addr.rfcomm_family = AF_BLUETOOTH;
223 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
224 sizeof(sock_addr.rfcomm_bdaddr));
225 sock_addr.rfcomm_channel = channel;
227 if (bind(s, (struct sockaddr *) &sock_addr,
228 sizeof(sock_addr)) < 0) {
229 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
230 strerror(errno), errno);
234 if (listen(s, 10) < 0) {
235 syslog(LOG_ERR, "Could not listen on socket. %s (%d)",
236 strerror(errno), errno);
240 ss = sdp_open_local(NULL);
242 syslog(LOG_ERR, "Unable to create local SDP session");
246 if (sdp_error(ss) != 0) {
247 syslog(LOG_ERR, "Unable to open local SDP session. " \
248 "%s (%d)", strerror(sdp_error(ss)),
253 memset(&lan, 0, sizeof(lan));
254 lan.server_channel = channel;
256 if (sdp_register_service(ss,
257 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
258 &addr, (void *) &lan, sizeof(lan), NULL) != 0) {
259 syslog(LOG_ERR, "Unable to register LAN service with " \
260 "local SDP daemon. %s (%d)",
261 strerror(sdp_error(ss)), sdp_error(ss));
266 * Register DUN (Dial-Up Networking) service on the same
267 * RFCOMM channel if requested. There is really no good reason
268 * to not to support this. AT-command exchange can be faked
269 * with chat script in ppp.conf
273 sdp_dun_profile_t dun;
275 memset(&dun, 0, sizeof(dun));
276 dun.server_channel = channel;
278 if (sdp_register_service(ss,
279 SDP_SERVICE_CLASS_DIALUP_NETWORKING,
280 &addr, (void *) &dun, sizeof(dun),
282 syslog(LOG_ERR, "Unable to register DUN " \
283 "service with local SDP daemon. " \
284 "%s (%d)", strerror(sdp_error(ss)),
291 * Register SP (Serial Port) service on the same RFCOMM channel
292 * if requested. It appears that some cell phones are using so
293 * called "callback mechanism". In this scenario user is trying
294 * to connect his cell phone to the Internet, and, user's host
295 * computer is acting as the gateway server. It seems that it
296 * is not possible to tell the phone to just connect and start
297 * using the LAN service. Instead the user's host computer must
298 * "jump start" the phone by connecting to the phone's SP
299 * service. What happens next is the phone kills the existing
300 * connection and opens another connection back to the user's
301 * host computer. The phone really wants to use LAN service,
302 * but for whatever reason it looks for SP service on the
303 * user's host computer. This brain damaged behavior was
304 * reported for Nokia 6600 and Sony/Ericsson P900. Both phones
305 * are Symbian-based phones. Perhaps this is a Symbian problem?
311 memset(&sp, 0, sizeof(sp));
312 sp.server_channel = channel;
314 if (sdp_register_service(ss,
315 SDP_SERVICE_CLASS_SERIAL_PORT,
316 &addr, (void *) &sp, sizeof(sp),
318 syslog(LOG_ERR, "Unable to register SP " \
319 "service with local SDP daemon. " \
320 "%s (%d)", strerror(sdp_error(ss)),
326 for (done = 0; !done; ) {
327 socklen_t len = sizeof(sock_addr);
328 int s1 = accept(s, (struct sockaddr *) &sock_addr, &len);
331 syslog(LOG_ERR, "Could not accept connection " \
332 "on socket. %s (%d)", strerror(errno),
338 if (pid == (pid_t) -1) {
339 syslog(LOG_ERR, "Could not fork(). %s (%d)",
340 strerror(errno), errno);
348 /* Reset signal handler */
349 memset(&sa, 0, sizeof(sa));
350 sa.sa_handler = SIG_DFL;
352 sigaction(SIGTERM, &sa, NULL);
353 sigaction(SIGHUP, &sa, NULL);
354 sigaction(SIGINT, &sa, NULL);
355 sigaction(SIGCHLD, &sa, NULL);
361 * XXX Make sure user does not shoot himself
362 * in the foot. Do not pass unit option to the
363 * PPP when operating in the server mode.
366 exec_ppp(s1, NULL, label);
371 sock_addr.rfcomm_len = sizeof(sock_addr);
372 sock_addr.rfcomm_family = AF_BLUETOOTH;
373 memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY,
374 sizeof(sock_addr.rfcomm_bdaddr));
375 sock_addr.rfcomm_channel = 0;
377 if (bind(s, (struct sockaddr *) &sock_addr,
378 sizeof(sock_addr)) < 0) {
379 syslog(LOG_ERR, "Could not bind socket. %s (%d)",
380 strerror(errno), errno);
384 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
385 sizeof(sock_addr.rfcomm_bdaddr));
386 sock_addr.rfcomm_channel = channel;
388 if (connect(s, (struct sockaddr *) &sock_addr,
389 sizeof(sock_addr)) < 0) {
390 syslog(LOG_ERR, "Could not connect socket. %s (%d)",
391 strerror(errno), errno);
395 exec_ppp(s, unit, label);
402 * Redirects stdin/stdout to s, stderr to /dev/null and exec
403 * 'ppp -direct -quiet [-unit N] label'. Never returns.
407 exec_ppp(int s, char *unit, char *label)
409 char ppp[] = "/usr/sbin/ppp";
410 char *ppp_args[] = { ppp, "-direct", "-quiet",
411 NULL, NULL, NULL, NULL };
415 syslog(LOG_ERR, "Could not dup(0). %s (%d)",
416 strerror(errno), errno);
422 syslog(LOG_ERR, "Could not dup(1). %s (%d)",
423 strerror(errno), errno);
428 open("/dev/null", O_RDWR);
431 ppp_args[3] = "-unit";
437 if (execv(ppp, ppp_args) < 0) {
438 syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \
439 "%s (%d)", ppp, (unit != NULL)? " -unit " : "",
440 (unit != NULL)? unit : "", label,
441 strerror(errno), errno);
453 /* Display usage and exit */
458 "Usage: %s options\n" \
459 "Where options are:\n" \
460 "\t-a address Address to listen on or connect to (required for client)\n" \
461 "\t-c Act as a clinet (default)\n" \
462 "\t-C channel RFCOMM channel to listen on or connect to (required)\n" \
463 "\t-d Run in foreground\n" \
464 "\t-D Register Dial-Up Networking service (server mode only)\n" \
465 "\t-l label Use PPP label (required)\n" \
466 "\t-s Act as a server\n" \
467 "\t-S Register Serial Port service (server mode only)\n" \
468 "\t-u N Tell PPP to operate on /dev/tunN (client mode only)\n" \
469 "\t-h Display this message\n", RFCOMM_PPPD);