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$ 27 */ 28 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <netdb.h> 34 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> 43 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 "udp.h" 61 62 63 #define UDP_CONNECTED 1 64 #define UDP_UNCONNECTED 2 65 #define UDP_MAYBEUNCONNECTED 3 66 67 struct udpdevice { 68 struct device dev; /* What struct physical knows about */ 69 struct sockaddr_in sock; /* peer address */ 70 unsigned connected : 2; /* Have we connect()d ? */ 71 }; 72 73 #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL) 74 75 int 76 udp_DeviceSize(void) 77 { 78 return sizeof(struct udpdevice); 79 } 80 81 static ssize_t 82 udp_Sendto(struct physical *p, const void *v, size_t n) 83 { 84 struct udpdevice *dev = device2udp(p->handler); 85 int ret; 86 87 switch (dev->connected) { 88 case UDP_CONNECTED: 89 ret = write(p->fd, v, n); 90 break; 91 92 case UDP_UNCONNECTED: 93 default: 94 ret = sendto(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, 95 sizeof dev->sock); 96 break; 97 } 98 if (dev->connected == UDP_MAYBEUNCONNECTED) { 99 if (ret == -1 && errno == EISCONN) { 100 dev->connected = UDP_CONNECTED; 101 ret = write(p->fd, v, n); 102 } else 103 dev->connected = UDP_UNCONNECTED; 104 } 105 106 return ret; 107 } 108 109 static ssize_t 110 udp_Recvfrom(struct physical *p, void *v, size_t n) 111 { 112 struct udpdevice *dev = device2udp(p->handler); 113 int sz, ret; 114 115 if (dev->connected == UDP_CONNECTED) 116 return read(p->fd, v, n); 117 118 sz = sizeof dev->sock; 119 ret = recvfrom(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, &sz); 120 121 if (*p->name.full == '\0') { 122 snprintf(p->name.full, sizeof p->name.full, "%s:%d/udp", 123 inet_ntoa(dev->sock.sin_addr), ntohs(dev->sock.sin_port)); 124 p->name.base = p->name.full; 125 } 126 127 return ret; 128 } 129 130 static void 131 udp_Free(struct physical *p) 132 { 133 struct udpdevice *dev = device2udp(p->handler); 134 135 free(dev); 136 } 137 138 static void 139 udp_device2iov(struct device *d, struct iovec *iov, int *niov, 140 int maxiov, int *auxfd, int *nauxfd) 141 { 142 int sz = physical_MaxDeviceSize(); 143 144 iov[*niov].iov_base = realloc(d, sz); 145 if (iov[*niov].iov_base == NULL) { 146 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); 147 AbortProgram(EX_OSERR); 148 } 149 iov[*niov].iov_len = sz; 150 (*niov)++; 151 } 152 153 static const struct device baseudpdevice = { 154 UDP_DEVICE, 155 "udp", 156 { CD_NOTREQUIRED, 0 }, 157 NULL, 158 NULL, 159 NULL, 160 NULL, 161 NULL, 162 NULL, 163 udp_Free, 164 udp_Recvfrom, 165 udp_Sendto, 166 udp_device2iov, 167 NULL, 168 NULL 169 }; 170 171 struct device * 172 udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 173 int maxiov, int *auxfd, int *nauxfd) 174 { 175 if (type == UDP_DEVICE) { 176 struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base; 177 178 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 179 if (dev == NULL) { 180 log_Printf(LogALERT, "Failed to allocate memory: %d\n", 181 (int)(sizeof *dev)); 182 AbortProgram(EX_OSERR); 183 } 184 185 /* Refresh function pointers etc */ 186 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev); 187 188 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC); 189 return &dev->dev; 190 } 191 192 return NULL; 193 } 194 195 static struct udpdevice * 196 udp_CreateDevice(struct physical *p, char *host, char *port) 197 { 198 struct udpdevice *dev; 199 struct servent *sp; 200 201 if ((dev = malloc(sizeof *dev)) == NULL) { 202 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n", 203 p->link.name, strerror(errno)); 204 return NULL; 205 } 206 207 dev->sock.sin_family = AF_INET; 208 dev->sock.sin_addr.s_addr = inet_addr(host); 209 dev->sock.sin_addr = GetIpAddr(host); 210 if (dev->sock.sin_addr.s_addr == INADDR_NONE) { 211 log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host); 212 free(dev); 213 return NULL; 214 } 215 dev->sock.sin_port = htons(atoi(port)); 216 if (dev->sock.sin_port == 0) { 217 sp = getservbyname(port, "udp"); 218 if (sp) 219 dev->sock.sin_port = sp->s_port; 220 else { 221 log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port); 222 free(dev); 223 return NULL; 224 } 225 } 226 227 log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name, 228 host, port); 229 230 p->fd = socket(PF_INET, SOCK_DGRAM, 0); 231 if (p->fd >= 0) { 232 log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name, 233 p->name.full); 234 if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) { 235 dev->connected = UDP_CONNECTED; 236 return dev; 237 } else 238 log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno)); 239 } else 240 log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno)); 241 242 close(p->fd); 243 p->fd = -1; 244 free(dev); 245 246 return NULL; 247 } 248 249 struct device * 250 udp_Create(struct physical *p) 251 { 252 char *cp, *host, *port, *svc; 253 struct udpdevice *dev; 254 255 dev = NULL; 256 if (p->fd < 0) { 257 if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) { 258 *cp = '\0'; 259 host = p->name.full; 260 port = cp + 1; 261 svc = strchr(port, '/'); 262 if (svc && strcasecmp(svc, "/udp")) { 263 *cp = ':'; 264 return NULL; 265 } 266 if (svc) { 267 p->fd--; /* We own the device but maybe can't use it - change fd */ 268 *svc = '\0'; 269 } 270 271 if (*host && *port) 272 dev = udp_CreateDevice(p, host, port); 273 274 *cp = ':'; 275 if (svc) 276 *svc = '/'; 277 } 278 } else { 279 /* See if we're a connected udp socket */ 280 int type, sz, err; 281 282 sz = sizeof type; 283 if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 && 284 sz == sizeof type && type == SOCK_DGRAM) { 285 if ((dev = malloc(sizeof *dev)) == NULL) { 286 log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n", 287 p->link.name, strerror(errno)); 288 return NULL; 289 } 290 291 /* We can't getpeername().... */ 292 dev->connected = UDP_MAYBEUNCONNECTED; 293 294 log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name); 295 296 if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) { 297 log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n", 298 p->link.name); 299 p->link.lcp.cfg.openmode = OPEN_PASSIVE; 300 } 301 } 302 } 303 304 if (dev) { 305 memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev); 306 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC); 307 if (p->cfg.cd.necessity != CD_DEFAULT) 308 log_Printf(LogWARN, "Carrier settings ignored\n"); 309 return &dev->dev; 310 } 311 312 return NULL; 313 } 314