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 * $OpenBSD: atm.c,v 1.10 2003/10/20 03:15:37 deraadt Exp $
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 __RCSID("$MirOS: src/usr.sbin/ppp/ppp/atm.c,v 1.2 2005/03/13 19:17:13 tg Exp $");
62
63 /* String identifying PPPoA */
64 #define PPPOA "PPPoA"
65 #define PPPOA_LEN (sizeof(PPPOA) - 1)
66
67 struct atmdevice {
68 struct device dev; /* What struct physical knows about */
69 };
70
71 #define device2atm(d) ((d)->type == ATM_DEVICE ? (struct atmdevice *)d : NULL)
72
73 int
atm_DeviceSize(void)74 atm_DeviceSize(void)
75 {
76 return sizeof(struct atmdevice);
77 }
78
79 static ssize_t
atm_Sendto(struct physical * p,const void * v,size_t n)80 atm_Sendto(struct physical *p, const void *v, size_t n)
81 {
82 ssize_t ret = write(p->fd, v, n);
83 if (ret < 0) {
84 log_Printf(LogDEBUG, "atm_Sendto(%ld): %s\n", (long)n, strerror(errno));
85 return ret;
86 }
87 return ret;
88 }
89
90 static ssize_t
atm_Recvfrom(struct physical * p,void * v,size_t n)91 atm_Recvfrom(struct physical *p, void *v, size_t n)
92 {
93 ssize_t ret = read(p->fd, (char*)v, n);
94 if (ret < 0) {
95 log_Printf(LogDEBUG, "atm_Recvfrom(%ld): %s\n", (long)n, strerror(errno));
96 return ret;
97 }
98 return ret;
99 }
100
101 static void
atm_Free(struct physical * p)102 atm_Free(struct physical *p)
103 {
104 struct atmdevice *dev = device2atm(p->handler);
105
106 free(dev);
107 }
108
109 static void
atm_device2iov(struct device * d,struct iovec * iov,int * niov,int maxiov,int * auxfd,int * nauxfd)110 atm_device2iov(struct device *d, struct iovec *iov, int *niov,
111 int maxiov, int *auxfd, int *nauxfd)
112 {
113 int sz = physical_MaxDeviceSize();
114
115 iov[*niov].iov_base = realloc(d, sz);
116 if (iov[*niov].iov_base == NULL) {
117 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
118 AbortProgram(EX_OSERR);
119 }
120 iov[*niov].iov_len = sz;
121 (*niov)++;
122 }
123
124 static const struct device baseatmdevice = {
125 ATM_DEVICE,
126 "atm",
127 0,
128 { CD_NOTREQUIRED, 0 },
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 atm_Free,
137 atm_Recvfrom,
138 atm_Sendto,
139 atm_device2iov,
140 NULL,
141 NULL,
142 NULL
143 };
144
145 struct device *
atm_iov2device(int type,struct physical * p,struct iovec * iov,int * niov,int maxiov,int * auxfd,int * nauxfd)146 atm_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
147 int maxiov, int *auxfd, int *nauxfd)
148 {
149 if (type == ATM_DEVICE) {
150 struct atmdevice *dev = (struct atmdevice *)iov[(*niov)++].iov_base;
151 struct atmdevice *newdev;
152
153 newdev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
154 if (newdev == NULL) {
155 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
156 (int)(sizeof *dev));
157 AbortProgram(EX_OSERR);
158 }
159 dev = newdev;
160
161 /* Refresh function pointers etc */
162 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
163
164 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
165 return &dev->dev;
166 }
167
168 return NULL;
169 }
170
171 static struct atmdevice *
atm_CreateDevice(struct physical * p,const char * iface,unsigned vpi,unsigned vci)172 atm_CreateDevice(struct physical *p, const char *iface, unsigned vpi,
173 unsigned vci)
174 {
175 struct atmdevice *dev;
176 struct sockaddr_natm sock;
177
178 if ((dev = calloc(1, sizeof *dev)) == NULL) {
179 log_Printf(LogWARN, "%s: Cannot allocate an atm device: %s\n",
180 p->link.name, strerror(errno));
181 return NULL;
182 }
183
184 sock.snatm_len = sizeof sock;
185 sock.snatm_family = AF_NATM;
186 strncpy(sock.snatm_if, iface, IFNAMSIZ);
187 sock.snatm_vpi = vpi;
188 sock.snatm_vci = vci;
189
190 log_Printf(LogPHASE, "%s: Connecting to %s:%u.%u\n", p->link.name,
191 iface, vpi, vci);
192
193 p->fd = socket(PF_NATM, SOCK_DGRAM, PROTO_NATMAAL5);
194 if (p->fd >= 0) {
195 log_Printf(LogDEBUG, "%s: Opened atm socket %s\n", p->link.name,
196 p->name.full);
197 if (connect(p->fd, (struct sockaddr *)&sock, sizeof sock) == 0)
198 return dev;
199 else
200 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
201 } else
202 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
203
204 close(p->fd);
205 p->fd = -1;
206 free(dev);
207
208 return NULL;
209 }
210
211 struct device *
atm_Create(struct physical * p)212 atm_Create(struct physical *p)
213 {
214 struct atmdevice *dev;
215
216 dev = NULL;
217 if (p->fd < 0 && !strncasecmp(p->name.full, PPPOA, PPPOA_LEN)
218 && p->name.full[PPPOA_LEN] == ':') {
219 char iface[26];
220 unsigned vci, vpi;
221
222 if (sscanf(p->name.full + PPPOA_LEN + 1, "%25[A-Za-z0-9]:%u.%u", iface,
223 &vpi, &vci) != 3) {
224 log_Printf(LogWARN, "Malformed ATM device name \'%s\', "
225 "PPPoA:if:vpi.vci expected\n", p->name.full);
226 return NULL;
227 }
228
229 dev = atm_CreateDevice(p, iface, vpi, vci);
230 }
231
232 if (dev) {
233 memcpy(&dev->dev, &baseatmdevice, sizeof dev->dev);
234 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNCNOACF);
235 if (p->cfg.cd.necessity != CD_DEFAULT)
236 log_Printf(LogWARN, "Carrier settings ignored\n");
237 return &dev->dev;
238 }
239
240 return NULL;
241 }
242