dma: don't block on lock/opening the mbox file, backoff instead
[dragonfly.git] / usr.sbin / ppp / atm.c
blob638d79d8045395c49c5dd6c34bcab84ca1918bde
1 /*-
2 * Copyright (c) 2000 Jakob Stoklund Olesen <stoklund@taxidriver.dk>
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/usr.sbin/ppp/atm.c,v 1.9.2.1 2002/09/01 02:12:22 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/atm.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <netnatm/natm.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <sys/uio.h>
41 #include <termios.h>
42 #include <unistd.h>
44 #include "layer.h"
45 #include "defs.h"
46 #include "mbuf.h"
47 #include "log.h"
48 #include "timer.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "throughput.h"
52 #include "fsm.h"
53 #include "lcp.h"
54 #include "ccp.h"
55 #include "link.h"
56 #include "async.h"
57 #include "descriptor.h"
58 #include "physical.h"
59 #include "main.h"
60 #include "atm.h"
62 /* String identifying PPPoA */
63 #define PPPOA "PPPoA"
64 #define PPPOA_LEN (sizeof(PPPOA) - 1)
66 struct atmdevice {
67 struct device dev; /* What struct physical knows about */
70 #define device2atm(d) ((d)->type == ATM_DEVICE ? (struct atmdevice *)d : NULL)
72 int
73 atm_DeviceSize(void)
75 return sizeof(struct atmdevice);
78 static ssize_t
79 atm_Sendto(struct physical *p, const void *v, size_t n)
81 ssize_t ret = write(p->fd, v, n);
82 if (ret < 0) {
83 log_Printf(LogDEBUG, "atm_Sendto(%ld): %s\n", (long)n, strerror(errno));
84 return ret;
86 return ret;
89 static ssize_t
90 atm_Recvfrom(struct physical *p, void *v, size_t n)
92 ssize_t ret = read(p->fd, (char*)v, n);
93 if (ret < 0) {
94 log_Printf(LogDEBUG, "atm_Recvfrom(%ld): %s\n", (long)n, strerror(errno));
95 return ret;
97 return ret;
100 static void
101 atm_Free(struct physical *p)
103 struct atmdevice *dev = device2atm(p->handler);
105 free(dev);
108 static void
109 atm_device2iov(struct device *d, struct iovec *iov, int *niov,
110 int maxiov, int *auxfd, int *nauxfd)
112 int sz = physical_MaxDeviceSize();
114 iov[*niov].iov_base = realloc(d, sz);
115 if (iov[*niov].iov_base == NULL) {
116 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
117 AbortProgram(EX_OSERR);
119 iov[*niov].iov_len = sz;
120 (*niov)++;
123 static const struct device baseatmdevice = {
124 ATM_DEVICE,
125 "atm",
127 { CD_NOTREQUIRED, 0 },
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 atm_Free,
136 atm_Recvfrom,
137 atm_Sendto,
138 atm_device2iov,
139 NULL,
140 NULL,
141 NULL
144 struct device *
145 atm_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
146 int maxiov, int *auxfd, int *nauxfd)
148 if (type == ATM_DEVICE) {
149 struct atmdevice *dev = (struct atmdevice *)iov[(*niov)++].iov_base;
151 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
152 if (dev == NULL) {
153 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
154 (int)(sizeof *dev));
155 AbortProgram(EX_OSERR);
158 /* Refresh function pointers etc */
159 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
161 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
162 return &dev->dev;
165 return NULL;
168 static struct atmdevice *
169 atm_CreateDevice(struct physical *p, const char *iface, unsigned vpi,
170 unsigned vci)
172 struct atmdevice *dev;
173 struct sockaddr_natm sock;
175 if ((dev = calloc(1, sizeof *dev)) == NULL) {
176 log_Printf(LogWARN, "%s: Cannot allocate an atm device: %s\n",
177 p->link.name, strerror(errno));
178 return NULL;
181 sock.snatm_len = sizeof sock;
182 sock.snatm_family = AF_NATM;
183 strncpy(sock.snatm_if, iface, IFNAMSIZ);
184 sock.snatm_vpi = vpi;
185 sock.snatm_vci = vci;
187 log_Printf(LogPHASE, "%s: Connecting to %s:%u.%u\n", p->link.name,
188 iface, vpi, vci);
190 p->fd = socket(PF_NATM, SOCK_DGRAM, PROTO_NATMAAL5);
191 if (p->fd >= 0) {
192 log_Printf(LogDEBUG, "%s: Opened atm socket %s\n", p->link.name,
193 p->name.full);
194 if (connect(p->fd, (struct sockaddr *)&sock, sizeof sock) == 0)
195 return dev;
196 else
197 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
198 } else
199 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
201 close(p->fd);
202 p->fd = -1;
203 free(dev);
205 return NULL;
208 struct device *
209 atm_Create(struct physical *p)
211 struct atmdevice *dev;
213 dev = NULL;
214 if (p->fd < 0 && !strncasecmp(p->name.full, PPPOA, PPPOA_LEN)
215 && p->name.full[PPPOA_LEN] == ':') {
216 char iface[25];
217 unsigned vci, vpi;
219 if (sscanf(p->name.full + PPPOA_LEN + 1, "%25[A-Za-z0-9]:%u.%u", iface,
220 &vpi, &vci) != 3) {
221 log_Printf(LogWARN, "Malformed ATM device name \'%s\', "
222 "PPPoA:if:vpi.vci expected\n", p->name.full);
223 return NULL;
226 dev = atm_CreateDevice(p, iface, vpi, vci);
229 if (dev) {
230 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
231 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
232 if (p->cfg.cd.necessity != CD_DEFAULT)
233 log_Printf(LogWARN, "Carrier settings ignored\n");
234 return &dev->dev;
237 return NULL;