2 * Copyright (c) 2000 Jakob Stoklund Olesen <stoklund@taxidriver.dk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netnatm/natm.h>
50 #include "throughput.h"
56 #include "descriptor.h"
61 /* String identifying PPPoA */
63 #define PPPOA_LEN (sizeof(PPPOA) - 1)
66 struct device dev; /* What struct physical knows about */
69 #define device2atm(d) ((d)->type == ATM_DEVICE ? (struct atmdevice *)d : NULL)
74 return sizeof(struct atmdevice);
78 atm_Sendto(struct physical *p, const void *v, size_t n)
80 ssize_t ret = write(p->fd, v, n);
82 log_Printf(LogDEBUG, "atm_Sendto(%ld): %s\n", (long)n, strerror(errno));
89 atm_Recvfrom(struct physical *p, void *v, size_t n)
91 ssize_t ret = read(p->fd, (char*)v, n);
93 log_Printf(LogDEBUG, "atm_Recvfrom(%ld): %s\n", (long)n, strerror(errno));
100 atm_Free(struct physical *p)
102 struct atmdevice *dev = device2atm(p->handler);
108 atm_device2iov(struct device *d, struct iovec *iov, int *niov,
109 int maxiov, int *auxfd, int *nauxfd)
111 int sz = physical_MaxDeviceSize();
113 iov[*niov].iov_base = realloc(d, sz);
114 if (iov[*niov].iov_base == NULL) {
115 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
116 AbortProgram(EX_OSERR);
118 iov[*niov].iov_len = sz;
122 static const struct device baseatmdevice = {
126 { CD_NOTREQUIRED, 0 },
142 atm_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
143 int maxiov, int *auxfd, int *nauxfd)
145 if (type == ATM_DEVICE) {
146 struct atmdevice *dev = (struct atmdevice *)iov[(*niov)++].iov_base;
148 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
150 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
152 AbortProgram(EX_OSERR);
155 /* Refresh function pointers etc */
156 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
158 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
165 static struct atmdevice *
166 atm_CreateDevice(struct physical *p, const char *iface, unsigned vpi,
169 struct atmdevice *dev;
170 struct sockaddr_natm sock;
172 if ((dev = calloc(1, sizeof *dev)) == NULL) {
173 log_Printf(LogWARN, "%s: Cannot allocate an atm device: %s\n",
174 p->link.name, strerror(errno));
178 sock.snatm_len = sizeof sock;
179 sock.snatm_family = AF_NATM;
180 strncpy(sock.snatm_if, iface, IFNAMSIZ);
181 sock.snatm_vpi = vpi;
182 sock.snatm_vci = vci;
184 log_Printf(LogPHASE, "%s: Connecting to %s:%u.%u\n", p->link.name,
187 p->fd = socket(PF_NATM, SOCK_DGRAM, PROTO_NATMAAL5);
189 log_Printf(LogDEBUG, "%s: Opened atm socket %s\n", p->link.name,
191 if (connect(p->fd, (struct sockaddr *)&sock, sizeof sock) == 0)
194 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
196 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
206 atm_Create(struct physical *p)
208 struct atmdevice *dev;
211 if (p->fd < 0 && !strncasecmp(p->name.full, PPPOA, PPPOA_LEN)
212 && p->name.full[PPPOA_LEN] == ':') {
216 if (sscanf(p->name.full + PPPOA_LEN + 1, "%25[A-Za-z0-9]:%u.%u", iface,
218 log_Printf(LogWARN, "Malformed ATM device name \'%s\', "
219 "PPPoA:if:vpi.vci expected\n", p->name.full);
223 dev = atm_CreateDevice(p, iface, vpi, vci);
227 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
228 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
229 if (p->cfg.cd.necessity != CD_DEFAULT)
230 log_Printf(LogWARN, "Carrier settings ignored\n");