]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bluetooth/rfcomm_pppd/rfcomm_pppd.c
This commit was generated by cvs2svn to compensate for changes in r178479,
[FreeBSD/FreeBSD.git] / usr.sbin / bluetooth / rfcomm_pppd / rfcomm_pppd.c
1 /*
2  * rfcomm_pppd.c
3  */
4
5 /*-
6  * Copyright (c) 2001-2008 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $
31  * $FreeBSD$
32  */
33
34 #include <bluetooth.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sdp.h>
40 #include <signal.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47
48 #define RFCOMM_PPPD     "rfcomm_pppd"
49
50 int             rfcomm_channel_lookup   (bdaddr_t const *local,
51                                          bdaddr_t const *remote,
52                                          int service, int *channel, int *error);
53
54 static void     exec_ppp        (int s, char *unit, char *label);
55 static void     sighandler      (int s);
56 static void     usage           (void);
57
58 static int      done;
59
60 /* Main */
61 int
62 main(int argc, char *argv[])
63 {
64         struct sockaddr_rfcomm   sock_addr;
65         char                    *label = NULL, *unit = NULL, *ep = NULL;
66         bdaddr_t                 addr;
67         int                      s, channel, detach, server, service,
68                                  regdun, regsp;
69         pid_t                    pid;
70
71         memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
72         channel = 0;
73         detach = 1;
74         server = 0;
75         service = 0;
76         regdun = 0;
77         regsp = 0;
78
79         /* Parse command line arguments */
80         while ((s = getopt(argc, argv, "a:cC:dDhl:sSu:")) != -1) {
81                 switch (s) {
82                 case 'a': /* BDADDR */
83                         if (!bt_aton(optarg, &addr)) {
84                                 struct hostent  *he = NULL;
85
86                                 if ((he = bt_gethostbyname(optarg)) == NULL)
87                                         errx(1, "%s: %s", optarg, hstrerror(h_errno));
88
89                                 memcpy(&addr, he->h_addr, sizeof(addr));
90                         }
91                         break;
92
93                 case 'c': /* client */
94                         server = 0;
95                         break;
96
97                 case 'C': /* RFCOMM channel */
98                         channel = strtoul(optarg, &ep, 10);
99                         if (*ep != '\0') {
100                                 channel = 0;
101                                 switch (tolower(optarg[0])) {
102                                 case 'd': /* DialUp Networking */
103                                         service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
104                                         break;
105
106                                 case 'l': /* LAN Access Using PPP */
107                                         service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
108                                         break;
109                                 }
110                         }
111                         break;
112
113                 case 'd': /* do not detach */
114                         detach = 0;
115                         break;
116
117                 case 'D': /* Register DUN service as well as LAN service */
118                         regdun = 1;
119                         break;
120
121                 case 'l': /* PPP label */
122                         label = optarg;
123                         break;
124
125                 case 's': /* server */
126                         server = 1;
127                         break;
128
129                 case 'S': /* Register SP service as well as LAN service */
130                         regsp = 1;
131                         break;
132
133                 case 'u': /* PPP -unit option */
134                         strtoul(optarg, &ep, 10);
135                         if (*ep != '\0')
136                                 usage();
137                                 /* NOT REACHED */
138
139                         unit = optarg;
140                         break;
141
142                 case 'h':
143                 default:
144                         usage();
145                         /* NOT REACHED */
146                 }
147         }
148
149         /* Check if we got everything we wanted */
150         if (label == NULL)
151                 errx(1, "Must specify PPP label");
152
153         if (!server) {
154                 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
155                         errx(1, "Must specify server BD_ADDR");
156
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,
160                                                         &channel, &s) != 0)
161                                 errc(1, s, "Could not obtain RFCOMM channel");
162         }
163
164         if (channel <= 0 || channel > 30)
165                 errx(1, "Invalid RFCOMM channel number %d", channel);
166
167         openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
168
169         if (detach) {
170                 pid = fork();
171                 if (pid == (pid_t) -1) {
172                         syslog(LOG_ERR, "Could not fork(). %s (%d)",
173                                 strerror(errno), errno);
174                         exit(1);
175                 }
176
177                 if (pid != 0)
178                         exit(0);
179
180                 if (daemon(0, 0) < 0) {
181                         syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
182                                 strerror(errno), errno);
183                         exit(1);
184                 }
185         }
186
187         s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
188         if (s < 0) {
189                 syslog(LOG_ERR, "Could not create socket. %s (%d)",
190                         strerror(errno), errno);
191                 exit(1);
192         }
193
194         if (server) {
195                 struct sigaction         sa;
196                 void                    *ss = NULL;
197                 sdp_lan_profile_t        lan;
198
199                 /* Install signal handler */
200                 memset(&sa, 0, sizeof(sa));
201                 sa.sa_handler = sighandler;
202
203                 if (sigaction(SIGTERM, &sa, NULL) < 0) {
204                         syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
205                                 strerror(errno), errno);
206                         exit(1);
207                 }
208
209                 if (sigaction(SIGHUP, &sa, NULL) < 0) {
210                         syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
211                                 strerror(errno), errno);
212                         exit(1);
213                 }
214
215                 if (sigaction(SIGINT, &sa, NULL) < 0) {
216                         syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
217                                 strerror(errno), errno);
218                         exit(1);
219                 }
220
221                 sa.sa_handler = SIG_IGN;
222                 sa.sa_flags = SA_NOCLDWAIT;
223
224                 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
225                         syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)",
226                                 strerror(errno), errno);
227                         exit(1);
228                 }
229
230                 /* bind socket and listen for incoming connections */
231                 sock_addr.rfcomm_len = sizeof(sock_addr);
232                 sock_addr.rfcomm_family = AF_BLUETOOTH;
233                 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
234                         sizeof(sock_addr.rfcomm_bdaddr));
235                 sock_addr.rfcomm_channel = channel;
236
237                 if (bind(s, (struct sockaddr *) &sock_addr,
238                                 sizeof(sock_addr)) < 0) {
239                         syslog(LOG_ERR, "Could not bind socket. %s (%d)",
240                                 strerror(errno), errno);
241                         exit(1);
242                 }
243
244                 if (listen(s, 10) < 0) {
245                         syslog(LOG_ERR, "Could not listen on socket. %s (%d)",
246                                 strerror(errno), errno);
247                         exit(1);
248                 }
249
250                 ss = sdp_open_local(NULL);
251                 if (ss == NULL) {
252                         syslog(LOG_ERR, "Unable to create local SDP session");
253                         exit(1);
254                 }
255
256                 if (sdp_error(ss) != 0) {
257                         syslog(LOG_ERR, "Unable to open local SDP session. " \
258                                 "%s (%d)", strerror(sdp_error(ss)),
259                                 sdp_error(ss));
260                         exit(1);
261                 }
262
263                 memset(&lan, 0, sizeof(lan));
264                 lan.server_channel = channel;
265
266                 if (sdp_register_service(ss,
267                                 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
268                                 &addr, (void *) &lan, sizeof(lan), NULL) != 0) {
269                         syslog(LOG_ERR, "Unable to register LAN service with " \
270                                 "local SDP daemon. %s (%d)",
271                                 strerror(sdp_error(ss)), sdp_error(ss));
272                         exit(1);
273                 }
274
275                 /*
276                  * Register DUN (Dial-Up Networking) service on the same
277                  * RFCOMM channel if requested. There is really no good reason
278                  * to not to support this. AT-command exchange can be faked
279                  * with chat script in ppp.conf
280                  */
281
282                 if (regdun) {
283                         sdp_dun_profile_t       dun;
284
285                         memset(&dun, 0, sizeof(dun));
286                         dun.server_channel = channel;
287
288                         if (sdp_register_service(ss,
289                                         SDP_SERVICE_CLASS_DIALUP_NETWORKING,
290                                         &addr, (void *) &dun, sizeof(dun),
291                                         NULL) != 0) {
292                                 syslog(LOG_ERR, "Unable to register DUN " \
293                                         "service with local SDP daemon. " \
294                                         "%s (%d)", strerror(sdp_error(ss)),
295                                         sdp_error(ss));
296                                 exit(1);
297                         }
298                 }
299
300                 /*
301                  * Register SP (Serial Port) service on the same RFCOMM channel
302                  * if requested. It appears that some cell phones are using so
303                  * called "callback mechanism". In this scenario user is trying
304                  * to connect his cell phone to the Internet, and, user's host
305                  * computer is acting as the gateway server. It seems that it
306                  * is not possible to tell the phone to just connect and start
307                  * using the LAN service. Instead the user's host computer must
308                  * "jump start" the phone by connecting to the phone's SP
309                  * service. What happens next is the phone kills the existing
310                  * connection and opens another connection back to the user's
311                  * host computer. The phone really wants to use LAN service,
312                  * but for whatever reason it looks for SP service on the
313                  * user's host computer. This brain damaged behavior was
314                  * reported for Nokia 6600 and Sony/Ericsson P900. Both phones
315                  * are Symbian-based phones. Perhaps this is a Symbian problem?
316                  */
317
318                 if (regsp) {
319                         sdp_sp_profile_t        sp;
320
321                         memset(&sp, 0, sizeof(sp));
322                         sp.server_channel = channel;
323
324                         if (sdp_register_service(ss,
325                                         SDP_SERVICE_CLASS_SERIAL_PORT,
326                                         &addr, (void *) &sp, sizeof(sp),
327                                         NULL) != 0) {
328                                 syslog(LOG_ERR, "Unable to register SP " \
329                                         "service with local SDP daemon. " \
330                                         "%s (%d)", strerror(sdp_error(ss)),
331                                         sdp_error(ss));
332                                 exit(1);
333                         }
334                 }
335                 
336                 for (done = 0; !done; ) {
337                         socklen_t       len = sizeof(sock_addr);
338                         int             s1 = accept(s, (struct sockaddr *) &sock_addr, &len);
339
340                         if (s1 < 0) {
341                                 syslog(LOG_ERR, "Could not accept connection " \
342                                         "on socket. %s (%d)", strerror(errno),
343                                         errno);
344                                 exit(1);
345                         }
346                                 
347                         pid = fork();
348                         if (pid == (pid_t) -1) {
349                                 syslog(LOG_ERR, "Could not fork(). %s (%d)",
350                                         strerror(errno), errno);
351                                 exit(1);
352                         }
353
354                         if (pid == 0) {
355                                 sdp_close(ss);
356                                 close(s);
357
358                                 /* Reset signal handler */
359                                 memset(&sa, 0, sizeof(sa));
360                                 sa.sa_handler = SIG_DFL;
361
362                                 sigaction(SIGTERM, &sa, NULL);
363                                 sigaction(SIGHUP, &sa, NULL);
364                                 sigaction(SIGINT, &sa, NULL);
365                                 sigaction(SIGCHLD, &sa, NULL);
366
367                                 /* Become daemon */
368                                 daemon(0, 0);
369
370                                 /*
371                                  * XXX Make sure user does not shoot himself
372                                  * in the foot. Do not pass unit option to the
373                                  * PPP when operating in the server mode.
374                                  */
375
376                                 exec_ppp(s1, NULL, label);
377                         } else
378                                 close(s1);
379                 }
380         } else {
381                 sock_addr.rfcomm_len = sizeof(sock_addr);
382                 sock_addr.rfcomm_family = AF_BLUETOOTH;
383                 memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY,
384                         sizeof(sock_addr.rfcomm_bdaddr));
385                 sock_addr.rfcomm_channel = 0;
386
387                 if (bind(s, (struct sockaddr *) &sock_addr,
388                                 sizeof(sock_addr)) < 0) {
389                         syslog(LOG_ERR, "Could not bind socket. %s (%d)",
390                                 strerror(errno), errno);
391                         exit(1);
392                 }
393
394                 memcpy(&sock_addr.rfcomm_bdaddr, &addr,
395                         sizeof(sock_addr.rfcomm_bdaddr));
396                 sock_addr.rfcomm_channel = channel;
397
398                 if (connect(s, (struct sockaddr *) &sock_addr,
399                                 sizeof(sock_addr)) < 0) {
400                         syslog(LOG_ERR, "Could not connect socket. %s (%d)",
401                                 strerror(errno), errno);
402                         exit(1);
403                 }
404
405                 exec_ppp(s, unit, label);
406         }
407
408         exit(0);
409 } /* main */
410
411 /* 
412  * Redirects stdin/stdout to s, stderr to /dev/null and exec
413  * 'ppp -direct -quiet [-unit N] label'. Never returns.
414  */
415
416 static void
417 exec_ppp(int s, char *unit, char *label)
418 {
419         char     ppp[] = "/usr/sbin/ppp";
420         char    *ppp_args[] = { ppp,  "-direct", "-quiet",
421                                 NULL, NULL,      NULL,     NULL };
422
423         close(0);
424         if (dup(s) < 0) {
425                 syslog(LOG_ERR, "Could not dup(0). %s (%d)",
426                         strerror(errno), errno);
427                 exit(1);
428         }
429
430         close(1);
431         if (dup(s) < 0) {
432                 syslog(LOG_ERR, "Could not dup(1). %s (%d)",
433                         strerror(errno), errno);
434                 exit(1);
435         }
436
437         close(2);
438         open("/dev/null", O_RDWR);
439
440         if (unit != NULL) {
441                 ppp_args[3] = "-unit";
442                 ppp_args[4] = unit;
443                 ppp_args[5] = label;
444         } else
445                 ppp_args[3] = label;
446
447         if (execv(ppp, ppp_args) < 0) {
448                 syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \
449                         "%s (%d)", ppp, (unit != NULL)? " -unit " : "",
450                         (unit != NULL)? unit : "", label,
451                         strerror(errno), errno);
452                 exit(1);
453         }
454 } /* run_ppp */
455
456 /* Signal handler */
457 static void
458 sighandler(int s)
459 {
460         done = 1;
461 } /* sighandler */
462
463 /* Display usage and exit */
464 static void
465 usage(void)
466 {
467         fprintf(stdout,
468 "Usage: %s options\n" \
469 "Where options are:\n" \
470 "\t-a address   Address to listen on or connect to (required for client)\n" \
471 "\t-c           Act as a clinet (default)\n" \
472 "\t-C channel   RFCOMM channel to listen on or connect to (required)\n" \
473 "\t-d           Run in foreground\n" \
474 "\t-D           Register Dial-Up Networking service (server mode only)\n" \
475 "\t-l label     Use PPP label (required)\n" \
476 "\t-s           Act as a server\n" \
477 "\t-S           Register Serial Port service (server mode only)\n" \
478 "\t-u N         Tell PPP to operate on /dev/tunN (client mode only)\n" \
479 "\t-h           Display this message\n", RFCOMM_PPPD);
480
481         exit(255);
482 } /* usage */
483