17a5d273aae657cb53bc50535e1e55c444d520ca
[dragonfly.git] / usr.sbin / ppp / i4b.c
1 /*-
2  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/ppp/i4b.c,v 1.8.2.3 2002/09/01 02:12:27 brian Exp $
27  * $DragonFly: src/usr.sbin/ppp/i4b.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
28  */
29
30 #include <sys/param.h>
31
32 #include <sys/un.h>
33 #if defined(__OpenBSD__) || defined(__NetBSD__)
34 #include <sys/ioctl.h>
35 #endif
36 #include <sys/stat.h>
37
38 #include <errno.h>
39 #include <fcntl.h>
40 #ifdef __FreeBSD__
41 #include <machine/i4b_ioctl.h>
42 #include <machine/i4b_rbch_ioctl.h>
43 #else
44 #ifdef __NetBSD__
45 #include <netisdn/i4b_ioctl.h>
46 #include <netisdn/i4b_rbch_ioctl.h>
47 #else
48 #include <i4b/i4b_ioctl.h>
49 #include <i4b/i4b_rbch_ioctl.h>
50 #endif
51 #endif
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sysexits.h>
56 #include <sys/uio.h>
57 #include <termios.h>
58 #include <unistd.h>
59
60 #include "layer.h"
61 #include "defs.h"
62 #include "mbuf.h"
63 #include "log.h"
64 #include "timer.h"
65 #include "lqr.h"
66 #include "hdlc.h"
67 #include "throughput.h"
68 #include "fsm.h"
69 #include "lcp.h"
70 #include "ccp.h"
71 #include "link.h"
72 #include "async.h"
73 #include "descriptor.h"
74 #include "physical.h"
75 #include "mp.h"
76 #include "chat.h"
77 #include "auth.h"
78 #include "chap.h"
79 #include "cbcp.h"
80 #include "datalink.h"
81 #include "main.h"
82 #include "i4b.h"
83
84 #define Online(dev)     ((dev)->mbits & TIOCM_CD)
85
86 struct i4bdevice {
87   struct device dev;            /* What struct physical knows about */
88   struct pppTimer Timer;        /* CD checks */
89   int mbits;                    /* Current DCD status */
90   int carrier_seconds;          /* seconds before CD is *required* */
91 };
92
93 #define device2i4b(d) ((d)->type == I4B_DEVICE ? (struct i4bdevice *)d : NULL)
94
95 int
96 i4b_DeviceSize(void)
97 {
98   return sizeof(struct i4bdevice);
99 }
100
101 /*
102  * i4b_Timeout() watches the DCD signal and mentions it if it's status
103  * changes.
104  */
105 static void
106 i4b_Timeout(void *data)
107 {
108   struct physical *p = data;
109   struct i4bdevice *dev = device2i4b(p->handler);
110   int ombits, change;
111
112   timer_Stop(&dev->Timer);
113   dev->Timer.load = SECTICKS;           /* Once a second please */
114   timer_Start(&dev->Timer);
115   ombits = dev->mbits;
116
117   if (p->fd >= 0) {
118     if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
119       log_Printf(LogPHASE, "%s: ioctl error (%s)!\n", p->link.name,
120                  strerror(errno));
121       datalink_Down(p->dl, CLOSE_NORMAL);
122       timer_Stop(&dev->Timer);
123       return;
124     }
125   } else
126     dev->mbits = 0;
127
128   if (ombits == -1) {
129     /* First time looking for carrier */
130     if (Online(dev))
131       log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
132     else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
133       log_Printf(LogPHASE, "%s: %s: No carrier"
134                  " (increase ``set cd'' from %d ?)\n",
135                  p->link.name, p->name.full, dev->dev.cd.delay);
136       timer_Stop(&dev->Timer);
137       /* i4b_AwaitCarrier() will notice */
138     } else {
139       /* Keep waiting */
140       log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
141                  p->link.name, p->name.full, dev->carrier_seconds,
142                  dev->dev.cd.delay);
143       dev->mbits = -1;
144     }
145   } else {
146     change = ombits ^ dev->mbits;
147     if (change & TIOCM_CD) {
148       if (dev->mbits & TIOCM_CD)
149         log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
150       else {
151         log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
152         log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
153         datalink_Down(p->dl, CLOSE_NORMAL);
154         timer_Stop(&dev->Timer);
155       }
156     } else
157       log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
158                  Online(dev) ? "on" : "off");
159   }
160 }
161
162 static void
163 i4b_StartTimer(struct physical *p)
164 {
165   struct i4bdevice *dev = device2i4b(p->handler);
166
167   timer_Stop(&dev->Timer);
168   dev->Timer.load = SECTICKS;
169   dev->Timer.func = i4b_Timeout;
170   dev->Timer.name = "i4b CD";
171   dev->Timer.arg = p;
172   log_Printf(LogDEBUG, "%s: Using i4b_Timeout [%p]\n",
173              p->link.name, i4b_Timeout);
174   timer_Start(&dev->Timer);
175 }
176
177 static int
178 i4b_AwaitCarrier(struct physical *p)
179 {
180   struct i4bdevice *dev = device2i4b(p->handler);
181
182   if (dev->mbits == -1) {
183     if (dev->Timer.state == TIMER_STOPPED) {
184       dev->carrier_seconds = 0;
185       i4b_StartTimer(p);
186     }
187     return CARRIER_PENDING;                     /* Not yet ! */
188   }
189
190   return Online(dev) ? CARRIER_OK : CARRIER_LOST;
191 }
192
193 static int
194 i4b_Raw(struct physical *p)
195 {
196   int oldflag;
197
198   log_Printf(LogDEBUG, "%s: Entering i4b_Raw\n", p->link.name);
199
200   oldflag = fcntl(p->fd, F_GETFL, 0);
201   if (oldflag < 0)
202     return 0;
203   fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
204
205   return 1;
206 }
207
208 static void
209 i4b_Offline(struct physical *p)
210 {
211   struct i4bdevice *dev = device2i4b(p->handler);
212
213   if (p->fd >= 0) {
214     timer_Stop(&dev->Timer);
215     if (Online(dev)) {
216       int dummy;
217
218       dummy = 1;
219       ioctl(p->fd, TIOCCDTR, &dummy);
220     }
221   }
222 }
223
224 static void
225 i4b_Cooked(struct physical *p)
226 {
227   int oldflag;
228
229   i4b_Offline(p);       /* In case of emergency close()s */
230
231   if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
232     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
233 }
234
235 static void
236 i4b_StopTimer(struct physical *p)
237 {
238   struct i4bdevice *dev = device2i4b(p->handler);
239
240   timer_Stop(&dev->Timer);
241 }
242
243 static void
244 i4b_Free(struct physical *p)
245 {
246   struct i4bdevice *dev = device2i4b(p->handler);
247
248   i4b_Offline(p);       /* In case of emergency close()s */
249   free(dev);
250 }
251
252 static int
253 i4b_Speed(struct physical *p)
254 {
255   struct termios ios;
256   int ret;
257
258   if (tcgetattr(p->fd, &ios) == -1 ||
259       (ret = SpeedToInt(cfgetispeed(&ios))) == 0)
260     ret = 64000;
261
262   return ret;
263 }
264
265 static const char *
266 i4b_OpenInfo(struct physical *p)
267 {
268   struct i4bdevice *dev = device2i4b(p->handler);
269   static char buf[26];
270
271   if (Online(dev))
272     snprintf(buf, sizeof buf, "carrier took %ds", dev->carrier_seconds);
273   else
274     *buf = '\0';
275
276   return buf;
277 }
278
279 static int
280 i4b_Slot(struct physical *p)
281 {
282   struct stat st;
283
284   if (fstat(p->fd, &st) == 0)
285     return minor(st.st_rdev);
286
287   return -1;
288 }
289
290 static void
291 i4b_device2iov(struct device *d, struct iovec *iov, int *niov,
292                int maxiov, int *auxfd, int *nauxfd)
293 {
294   struct i4bdevice *dev = device2i4b(d);
295   int sz = physical_MaxDeviceSize();
296
297   iov[*niov].iov_base = realloc(d, sz);
298   if (iov[*niov].iov_base == NULL) {
299     log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
300     AbortProgram(EX_OSERR);
301   }
302   iov[*niov].iov_len = sz;
303   (*niov)++;
304
305   if (dev->Timer.state != TIMER_STOPPED) {
306     timer_Stop(&dev->Timer);
307     dev->Timer.state = TIMER_RUNNING;
308   }
309 }
310
311 static struct device basei4bdevice = {
312   I4B_DEVICE,
313   "i4b",
314   0,
315   { CD_REQUIRED, DEF_I4BCDDELAY },
316   i4b_AwaitCarrier,
317   NULL,
318   i4b_Raw,
319   i4b_Offline,
320   i4b_Cooked,
321   NULL,
322   i4b_StopTimer,
323   i4b_Free,
324   NULL,
325   NULL,
326   i4b_device2iov,
327   i4b_Speed,
328   i4b_OpenInfo,
329   i4b_Slot
330 };
331
332 struct device *
333 i4b_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
334                int maxiov, int *auxfd, int *nauxfd)
335 {
336   if (type == I4B_DEVICE) {
337     struct i4bdevice *dev = (struct i4bdevice *)iov[(*niov)++].iov_base;
338
339     dev = realloc(dev, sizeof *dev);    /* Reduce to the correct size */
340     if (dev == NULL) {
341       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
342                  (int)(sizeof *dev));
343       AbortProgram(EX_OSERR);
344     }
345
346     /* Refresh function pointers etc */
347     memcpy(&dev->dev, &basei4bdevice, sizeof dev->dev);
348
349     physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
350     if (dev->Timer.state != TIMER_STOPPED) {
351       dev->Timer.state = TIMER_STOPPED;
352       p->handler = &dev->dev;           /* For the benefit of StartTimer */
353       i4b_StartTimer(p);
354     }
355     return &dev->dev;
356   }
357
358   return NULL;
359 }
360
361 struct device *
362 i4b_Create(struct physical *p)
363 {
364   struct i4bdevice *dev;
365   int oldflag, dial;
366   msg_vr_req_t req;
367   telno_t number;
368
369   if (p->fd < 0 || ioctl(p->fd, I4B_RBCH_VR_REQ, &req))
370     /* Don't want this */
371     return NULL;
372
373   /*
374    * We don't bother validating the version.... all versions of i4b that
375    * support I4B_RBCH_VR_REQ are fair game :-)
376    */
377
378   if (*p->name.full == '\0') {
379     physical_SetDevice(p, ttyname(p->fd));
380     log_Printf(LogDEBUG, "%s: Input is an i4b version %d.%d.%d isdn "
381                "device (%s)\n", p->link.name, req.version, req.release,
382                req.step, p->name.full);
383     dial = 0;
384   } else {
385     log_Printf(LogDEBUG, "%s: Opened %s (i4b version %d.%d.%d)\n",
386                p->link.name, p->name.full, req.version, req.release, req.step);
387     dial = 1;
388   }
389
390   /* We're gonna return an i4bdevice (unless something goes horribly wrong) */
391
392   if ((dev = malloc(sizeof *dev)) == NULL) {
393     /* Complete failure - parent doesn't continue trying to ``create'' */
394     close(p->fd);
395     p->fd = -1;
396     return NULL;
397   }
398
399   memcpy(&dev->dev, &basei4bdevice, sizeof dev->dev);
400   memset(&dev->Timer, '\0', sizeof dev->Timer);
401   dev->mbits = -1;
402
403   switch (p->cfg.cd.necessity) {
404     case CD_VARIABLE:
405       dev->dev.cd.delay = p->cfg.cd.delay;
406       break;
407     case CD_REQUIRED:
408       dev->dev.cd = p->cfg.cd;
409       break;
410     case CD_NOTREQUIRED:
411       log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
412                  p->link.name, dev->dev.cd.delay);
413     case CD_DEFAULT:
414       break;
415   }
416
417   oldflag = fcntl(p->fd, F_GETFL, 0);
418   if (oldflag < 0) {
419     /* Complete failure - parent doesn't continue trying to ``create'' */
420
421     log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
422                p->link.name, strerror(errno));
423     i4b_Cooked(p);
424     close(p->fd);
425     p->fd = -1;
426     free(dev);
427     return NULL;
428   } else
429     fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
430
431   if (dial) {
432     strncpy(number, datalink_ChoosePhoneNumber(p->dl), sizeof number - 1);
433     number[sizeof number - 1] = '\0';
434     if (number[0] == '\0')
435       dial = 0;
436   }
437   if (dial && ioctl(p->fd, I4B_RBCH_DIALOUT, number) == -1) {
438     /* Complete failure - parent doesn't continue trying to ``create'' */
439
440     log_Printf(LogWARN, "%s: ioctl(I4B_RBCH_DIALOUT): %s\n",
441                p->link.name, strerror(errno));
442     i4b_Cooked(p);
443     close(p->fd);
444     p->fd = -1;
445     free(dev);
446     return NULL;
447   }
448
449   physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
450
451   return &dev->dev;
452 }