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