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