udp.c revision 53733
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: head/usr.sbin/ppp/udp.c 53733 1999-11-26 22:44:33Z brian $
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
62struct 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
70int
71udp_DeviceSize(void)
72{
73  return sizeof(struct udpdevice);
74}
75
76static ssize_t
77udp_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
88static ssize_t
89udp_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
109static void
110udp_Free(struct physical *p)
111{
112  struct udpdevice *dev = device2udp(p->handler);
113
114  free(dev);
115}
116
117static void
118udp_device2iov(struct device *d, struct iovec *iov, int *niov,
119               int maxiov, int *auxfd, int *nauxfd)
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
132static const struct device baseudpdevice = {
133  UDP_DEVICE,
134  "udp",
135  { CD_NOTREQUIRED, 0 },
136  NULL,
137  NULL,
138  NULL,
139  NULL,
140  NULL,
141  NULL,
142  udp_Free,
143  udp_Recvfrom,
144  udp_Sendto,
145  udp_device2iov,
146  NULL,
147  NULL
148};
149
150struct device *
151udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
152               int maxiov, int *auxfd, int *nauxfd)
153{
154  if (type == UDP_DEVICE) {
155    struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base;
156
157    dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
158    if (dev == NULL) {
159      log_Printf(LogALERT, "Failed to allocate memory: %d\n",
160                 (int)(sizeof *dev));
161      AbortProgram(EX_OSERR);
162    }
163
164    /* Refresh function pointers etc */
165    memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
166
167    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
168    return &dev->dev;
169  }
170
171  return NULL;
172}
173
174static struct udpdevice *
175udp_CreateDevice(struct physical *p, char *host, char *port)
176{
177  struct udpdevice *dev;
178  struct servent *sp;
179
180  if ((dev = malloc(sizeof *dev)) == NULL) {
181    log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
182               p->link.name, strerror(errno));
183    return NULL;
184  }
185
186  dev->sock.sin_family = AF_INET;
187  dev->sock.sin_addr.s_addr = inet_addr(host);
188  dev->sock.sin_addr = GetIpAddr(host);
189  if (dev->sock.sin_addr.s_addr == INADDR_NONE) {
190    log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host);
191    free(dev);
192    return NULL;
193  }
194  dev->sock.sin_port = htons(atoi(port));
195  if (dev->sock.sin_port == 0) {
196    sp = getservbyname(port, "udp");
197    if (sp)
198      dev->sock.sin_port = sp->s_port;
199    else {
200      log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port);
201      free(dev);
202      return NULL;
203    }
204  }
205
206  log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name,
207             host, port);
208
209  p->fd = socket(PF_INET, SOCK_DGRAM, 0);
210  if (p->fd >= 0) {
211    log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name,
212               p->name.full);
213    if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) {
214      dev->connected = 1;
215      return dev;
216    } else
217      log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
218  } else
219    log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
220
221  close(p->fd);
222  p->fd = -1;
223  free(dev);
224
225  return NULL;
226}
227
228struct device *
229udp_Create(struct physical *p)
230{
231  char *cp, *host, *port, *svc;
232  struct udpdevice *dev;
233
234  dev = NULL;
235  if (p->fd < 0) {
236    if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
237      *cp = '\0';
238      host = p->name.full;
239      port = cp + 1;
240      svc = strchr(port, '/');
241      if (svc && strcasecmp(svc, "/udp")) {
242        *cp = ':';
243        return NULL;
244      }
245      if (svc) {
246        p->fd--;     /* We own the device but maybe can't use it - change fd */
247        *svc = '\0';
248      }
249
250      if (*host && *port)
251        dev = udp_CreateDevice(p, host, port);
252
253      *cp = ':';
254      if (svc)
255        *svc = '/';
256    }
257  } else {
258    /* See if we're a connected udp socket */
259    int type, sz, err;
260
261    sz = sizeof type;
262    if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 &&
263        sz == sizeof type && type == SOCK_DGRAM) {
264      if ((dev = malloc(sizeof *dev)) == NULL) {
265        log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
266                   p->link.name, strerror(errno));
267        return NULL;
268      }
269
270      /* We can't getpeername().... hence we stay un-connect()ed */
271      dev->connected = 0;
272
273      log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name);
274
275      if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) {
276        log_Printf(LogPHASE, "%s:   Changing openmode to PASSIVE\n",
277                   p->link.name);
278        p->link.lcp.cfg.openmode = OPEN_PASSIVE;
279      }
280    }
281  }
282
283  if (dev) {
284    memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
285    physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
286    if (p->cfg.cd.necessity != CD_DEFAULT)
287      log_Printf(LogWARN, "Carrier settings ignored\n");
288    return &dev->dev;
289  }
290
291  return NULL;
292}
293