atm.c revision 65862
1/*- 2 * Copyright (c) 2000 Jakob Stoklund Olesen <stoklund@taxidriver.dk> 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: head/usr.sbin/ppp/atm.c 65862 2000-09-14 22:02:54Z brian $ 27 */ 28 29#include <sys/types.h> 30#include <sys/socket.h> 31#include <net/if.h> 32#include <netnatm/natm.h> 33 34#include <errno.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <sysexits.h> 39#include <sys/uio.h> 40#include <termios.h> 41#include <unistd.h> 42 43#include "layer.h" 44#include "defs.h" 45#include "mbuf.h" 46#include "log.h" 47#include "timer.h" 48#include "lqr.h" 49#include "hdlc.h" 50#include "throughput.h" 51#include "fsm.h" 52#include "lcp.h" 53#include "ccp.h" 54#include "link.h" 55#include "async.h" 56#include "descriptor.h" 57#include "physical.h" 58#include "main.h" 59#include "atm.h" 60 61/* String identifying PPPoA */ 62#define PPPOA "PPPoA" 63#define PPPOA_LEN (sizeof(PPPOA)-1) 64 65struct atmdevice { 66 struct device dev; /* What struct physical knows about */ 67}; 68 69#define device2atm(d) ((d)->type == ATM_DEVICE ? (struct atmdevice *)d : NULL) 70 71int 72atm_DeviceSize(void) 73{ 74 return sizeof(struct atmdevice); 75} 76 77static ssize_t 78atm_Sendto(struct physical *p, const void *v, size_t n) 79{ 80 ssize_t ret = write(p->fd, v, n); 81 if (ret<0) { 82 log_Printf(LogDEBUG, "atm_Sendto(%d): %s\n", n, strerror(errno)); 83 return ret; 84 } 85 return ret; 86} 87 88static ssize_t 89atm_Recvfrom(struct physical *p, void *v, size_t n) 90{ 91 ssize_t ret = read(p->fd, (char*)v, n); 92 if (ret<0) { 93 log_Printf(LogDEBUG, "atm_Recvfrom(%d): %s\n", n, strerror(errno)); 94 return ret; 95 } 96 return ret; 97} 98 99static void 100atm_Free(struct physical *p) 101{ 102 struct atmdevice *dev = device2atm(p->handler); 103 104 free(dev); 105} 106 107static void 108atm_device2iov(struct device *d, struct iovec *iov, int *niov, 109 int maxiov, int *auxfd, int *nauxfd) 110{ 111 int sz = physical_MaxDeviceSize(); 112 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); 117 } 118 iov[*niov].iov_len = sz; 119 (*niov)++; 120} 121 122static const struct device baseatmdevice = { 123 ATM_DEVICE, 124 "atm", 125 { CD_NOTREQUIRED, 0 }, 126 NULL, 127 NULL, 128 NULL, 129 NULL, 130 NULL, 131 NULL, 132 atm_Free, 133 atm_Recvfrom, 134 atm_Sendto, 135 atm_device2iov, 136 NULL, 137 NULL 138}; 139 140struct device * 141atm_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 142 int maxiov, int *auxfd, int *nauxfd) 143{ 144 if (type == ATM_DEVICE) { 145 struct atmdevice *dev = (struct atmdevice *)iov[(*niov)++].iov_base; 146 147 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 148 if (dev == NULL) { 149 log_Printf(LogALERT, "Failed to allocate memory: %d\n", 150 (int)(sizeof *dev)); 151 AbortProgram(EX_OSERR); 152 } 153 154 /* Refresh function pointers etc */ 155 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev); 156 157 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 158 return &dev->dev; 159 } 160 161 return NULL; 162} 163 164static struct atmdevice * 165atm_CreateDevice(struct physical *p, const char *iface, unsigned vpi, unsigned vci) 166{ 167 struct atmdevice *dev; 168 struct sockaddr_natm sock; 169 170 if ((dev = calloc (1, sizeof *dev)) == NULL) { 171 log_Printf(LogWARN, "%s: Cannot allocate an atm device: %s\n", 172 p->link.name, strerror(errno)); 173 return NULL; 174 } 175 176 sock.snatm_len = sizeof sock; 177 sock.snatm_family = AF_NATM; 178 strncpy (sock.snatm_if, iface, IFNAMSIZ); 179 sock.snatm_vpi = vpi; 180 sock.snatm_vci = vci; 181 182 log_Printf(LogPHASE, "%s: Connecting to %s:%u.%u\n", p->link.name, 183 iface, vpi, vci); 184 185 p->fd = socket(PF_NATM, SOCK_DGRAM, PROTO_NATMAAL5); 186 if (p->fd >= 0) { 187 log_Printf(LogDEBUG, "%s: Opened atm socket %s\n", p->link.name, 188 p->name.full); 189 if (connect(p->fd, (struct sockaddr *)&sock, sizeof sock) == 0) 190 return dev; 191 else 192 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno)); 193 } else 194 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno)); 195 196 close(p->fd); 197 p->fd = -1; 198 free(dev); 199 200 return NULL; 201} 202 203struct device * 204atm_Create(struct physical *p) 205{ 206 struct atmdevice *dev; 207 208 dev = NULL; 209 if (p->fd < 0 && !strncasecmp (p->name.full, PPPOA, PPPOA_LEN) 210 && p->name.full[PPPOA_LEN] == ':') { 211 char iface[25]; 212 unsigned vci, vpi; 213 214 if (sscanf (p->name.full + PPPOA_LEN + 1, "%25[A-Za-z0-9]:%u.%u", iface, 215 &vpi, &vci) != 3) { 216 log_Printf (LogWARN, "Malformed ATM device name \'%s\', PPPoA:if:vpi.vci expected\n", p->name.full); 217 return NULL; 218 } 219 220 dev = atm_CreateDevice(p, iface, vpi, vci); 221 } 222 223 if (dev) { 224 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev); 225 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF); 226 if (p->cfg.cd.necessity != CD_DEFAULT) 227 log_Printf(LogWARN, "Carrier settings ignored\n"); 228 return &dev->dev; 229 } 230 231 return NULL; 232} 233