xref: /freebsd/usr.sbin/ppp/udp.c (revision f5a99677a3e38486bcd1b54cd8010e83bcf77d0c)
16815097bSBrian Somers /*-
26815097bSBrian Somers  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
36815097bSBrian Somers  * All rights reserved.
46815097bSBrian Somers  *
56815097bSBrian Somers  * Redistribution and use in source and binary forms, with or without
66815097bSBrian Somers  * modification, are permitted provided that the following conditions
76815097bSBrian Somers  * are met:
86815097bSBrian Somers  * 1. Redistributions of source code must retain the above copyright
96815097bSBrian Somers  *    notice, this list of conditions and the following disclaimer.
106815097bSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
116815097bSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
126815097bSBrian Somers  *    documentation and/or other materials provided with the distribution.
136815097bSBrian Somers  *
146815097bSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156815097bSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166815097bSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176815097bSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186815097bSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196815097bSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206815097bSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216815097bSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226815097bSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236815097bSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246815097bSBrian Somers  * SUCH DAMAGE.
256815097bSBrian Somers  *
26f5a99677SBrian Somers  *	$Id: udp.c,v 1.2 1999/05/24 16:39:17 brian Exp $
276815097bSBrian Somers  */
286815097bSBrian Somers 
296815097bSBrian Somers #include <sys/types.h>
306815097bSBrian Somers #include <sys/socket.h>
316815097bSBrian Somers #include <netinet/in.h>
326815097bSBrian Somers #include <arpa/inet.h>
336815097bSBrian Somers #include <netdb.h>
346815097bSBrian Somers 
356815097bSBrian Somers #include <errno.h>
366815097bSBrian Somers #include <stdio.h>
376815097bSBrian Somers #include <stdlib.h>
386815097bSBrian Somers #include <string.h>
39f5a99677SBrian Somers #include <sysexits.h>
406815097bSBrian Somers #include <sys/uio.h>
416815097bSBrian Somers #include <termios.h>
426815097bSBrian Somers #include <unistd.h>
436815097bSBrian Somers 
446815097bSBrian Somers #include "layer.h"
456815097bSBrian Somers #include "defs.h"
466815097bSBrian Somers #include "mbuf.h"
476815097bSBrian Somers #include "log.h"
486815097bSBrian Somers #include "sync.h"
496815097bSBrian Somers #include "timer.h"
506815097bSBrian Somers #include "lqr.h"
516815097bSBrian Somers #include "hdlc.h"
526815097bSBrian Somers #include "throughput.h"
536815097bSBrian Somers #include "fsm.h"
546815097bSBrian Somers #include "lcp.h"
556815097bSBrian Somers #include "ccp.h"
566815097bSBrian Somers #include "link.h"
576815097bSBrian Somers #include "async.h"
586815097bSBrian Somers #include "descriptor.h"
596815097bSBrian Somers #include "physical.h"
60f5a99677SBrian Somers #include "main.h"
616815097bSBrian Somers #include "udp.h"
626815097bSBrian Somers 
636815097bSBrian Somers struct udpdevice {
646815097bSBrian Somers   struct device dev;		/* What struct physical knows about */
656815097bSBrian Somers   struct sockaddr_in sock;	/* peer address */
666815097bSBrian Somers   unsigned connected : 1;	/* Have we connect()d ? */
676815097bSBrian Somers };
686815097bSBrian Somers 
696815097bSBrian Somers #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL)
706815097bSBrian Somers 
71f5a99677SBrian Somers int
72f5a99677SBrian Somers udp_DeviceSize(void)
73f5a99677SBrian Somers {
74f5a99677SBrian Somers   return sizeof(struct udpdevice);
75f5a99677SBrian Somers }
76f5a99677SBrian Somers 
776815097bSBrian Somers static ssize_t
786815097bSBrian Somers udp_Sendto(struct physical *p, const void *v, size_t n)
796815097bSBrian Somers {
806815097bSBrian Somers   struct udpdevice *dev = device2udp(p->handler);
816815097bSBrian Somers 
826815097bSBrian Somers   if (dev->connected)
836815097bSBrian Somers     return write(p->fd, v, n);
846815097bSBrian Somers 
856815097bSBrian Somers   return sendto(p->fd, v, n, 0, (struct sockaddr *)&dev->sock,
866815097bSBrian Somers                 sizeof dev->sock);
876815097bSBrian Somers }
886815097bSBrian Somers 
896815097bSBrian Somers static ssize_t
906815097bSBrian Somers udp_Recvfrom(struct physical *p, void *v, size_t n)
916815097bSBrian Somers {
926815097bSBrian Somers   struct udpdevice *dev = device2udp(p->handler);
936815097bSBrian Somers   int sz, ret;
946815097bSBrian Somers 
956815097bSBrian Somers   if (dev->connected)
966815097bSBrian Somers     return read(p->fd, v, n);
976815097bSBrian Somers 
986815097bSBrian Somers   sz = sizeof dev->sock;
996815097bSBrian Somers   ret = recvfrom(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, &sz);
1006815097bSBrian Somers 
1016815097bSBrian Somers   if (*p->name.full == '\0') {
1026815097bSBrian Somers     snprintf(p->name.full, sizeof p->name.full, "%s:%d/udp",
1036815097bSBrian Somers              inet_ntoa(dev->sock.sin_addr), ntohs(dev->sock.sin_port));
1046815097bSBrian Somers     p->name.base = p->name.full;
1056815097bSBrian Somers   }
1066815097bSBrian Somers 
1076815097bSBrian Somers   return ret;
1086815097bSBrian Somers }
1096815097bSBrian Somers 
1106815097bSBrian Somers static void
1116815097bSBrian Somers udp_Free(struct physical *p)
1126815097bSBrian Somers {
1136815097bSBrian Somers   struct udpdevice *dev = device2udp(p->handler);
1146815097bSBrian Somers 
1156815097bSBrian Somers   free(dev);
1166815097bSBrian Somers }
1176815097bSBrian Somers 
1186815097bSBrian Somers static void
119f5a99677SBrian Somers udp_device2iov(struct device *d, struct iovec *iov, int *niov,
1206815097bSBrian Somers                int maxiov, pid_t newpid)
1216815097bSBrian Somers {
122f5a99677SBrian Somers   int sz = physical_MaxDeviceSize();
123f5a99677SBrian Somers 
124f5a99677SBrian Somers   iov[*niov].iov_base = realloc(d, sz);
125f5a99677SBrian Somers   if (iov[*niov].iov_base == NULL) {
126f5a99677SBrian Somers     log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
127f5a99677SBrian Somers     AbortProgram(EX_OSERR);
128f5a99677SBrian Somers   }
129f5a99677SBrian Somers   iov[*niov].iov_len = sz;
1306815097bSBrian Somers   (*niov)++;
1316815097bSBrian Somers }
1326815097bSBrian Somers 
1336815097bSBrian Somers static const struct device baseudpdevice = {
1346815097bSBrian Somers   UDP_DEVICE,
1356815097bSBrian Somers   "udp",
1366815097bSBrian Somers   NULL,
1376815097bSBrian Somers   NULL,
1386815097bSBrian Somers   NULL,
1396815097bSBrian Somers   NULL,
1406815097bSBrian Somers   udp_Free,
1416815097bSBrian Somers   udp_Recvfrom,
1426815097bSBrian Somers   udp_Sendto,
1436815097bSBrian Somers   udp_device2iov,
1446815097bSBrian Somers   NULL,
1456815097bSBrian Somers   NULL
1466815097bSBrian Somers };
1476815097bSBrian Somers 
1486815097bSBrian Somers struct device *
1496815097bSBrian Somers udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
1506815097bSBrian Somers                int maxiov)
1516815097bSBrian Somers {
1526815097bSBrian Somers   if (type == UDP_DEVICE) {
153acbd1f00SBrian Somers     struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base;
1546815097bSBrian Somers 
155f5a99677SBrian Somers     dev = realloc(dev, sizeof *dev);	/* Reduce to the correct size */
156f5a99677SBrian Somers     if (dev == NULL) {
157f5a99677SBrian Somers       log_Printf(LogALERT, "Failed to allocate memory: %d\n",
158f5a99677SBrian Somers                  (int)(sizeof *dev));
159f5a99677SBrian Somers       AbortProgram(EX_OSERR);
160f5a99677SBrian Somers     }
161f5a99677SBrian Somers 
1626815097bSBrian Somers     /* Refresh function pointers etc */
163acbd1f00SBrian Somers     memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
1646815097bSBrian Somers 
165acbd1f00SBrian Somers     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
166acbd1f00SBrian Somers     return &dev->dev;
167acbd1f00SBrian Somers   }
1686815097bSBrian Somers 
169acbd1f00SBrian Somers   return NULL;
1706815097bSBrian Somers }
1716815097bSBrian Somers 
1726815097bSBrian Somers static struct udpdevice *
1736815097bSBrian Somers udp_CreateDevice(struct physical *p, char *host, char *port)
1746815097bSBrian Somers {
1756815097bSBrian Somers   struct udpdevice *dev;
1766815097bSBrian Somers   struct servent *sp;
1776815097bSBrian Somers 
1786815097bSBrian Somers   if ((dev = malloc(sizeof *dev)) == NULL) {
1796815097bSBrian Somers     log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
1806815097bSBrian Somers                p->link.name, strerror(errno));
1816815097bSBrian Somers     return NULL;
1826815097bSBrian Somers   }
1836815097bSBrian Somers 
1846815097bSBrian Somers   dev->sock.sin_family = AF_INET;
1856815097bSBrian Somers   dev->sock.sin_addr.s_addr = inet_addr(host);
1866815097bSBrian Somers   dev->sock.sin_addr = GetIpAddr(host);
1876815097bSBrian Somers   if (dev->sock.sin_addr.s_addr == INADDR_NONE) {
1886815097bSBrian Somers     log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host);
1896815097bSBrian Somers     free(dev);
1906815097bSBrian Somers     return NULL;
1916815097bSBrian Somers   }
1926815097bSBrian Somers   dev->sock.sin_port = htons(atoi(port));
1936815097bSBrian Somers   if (dev->sock.sin_port == 0) {
1946815097bSBrian Somers     sp = getservbyname(port, "udp");
1956815097bSBrian Somers     if (sp)
1966815097bSBrian Somers       dev->sock.sin_port = sp->s_port;
1976815097bSBrian Somers     else {
1986815097bSBrian Somers       log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port);
1996815097bSBrian Somers       free(dev);
2006815097bSBrian Somers       return NULL;
2016815097bSBrian Somers     }
2026815097bSBrian Somers   }
2036815097bSBrian Somers 
2046815097bSBrian Somers   log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name,
2056815097bSBrian Somers              host, port);
2066815097bSBrian Somers 
2076815097bSBrian Somers   p->fd = socket(PF_INET, SOCK_DGRAM, 0);
2086815097bSBrian Somers   if (p->fd >= 0) {
2096815097bSBrian Somers     log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name,
2106815097bSBrian Somers                p->name.full);
2116815097bSBrian Somers     if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) {
2126815097bSBrian Somers       dev->connected = 1;
2136815097bSBrian Somers       return dev;
2146815097bSBrian Somers     } else
2156815097bSBrian Somers       log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
2166815097bSBrian Somers   } else
2176815097bSBrian Somers     log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
2186815097bSBrian Somers 
2196815097bSBrian Somers   close(p->fd);
2206815097bSBrian Somers   p->fd = -1;
2216815097bSBrian Somers   free(dev);
2226815097bSBrian Somers 
2236815097bSBrian Somers   return NULL;
2246815097bSBrian Somers }
2256815097bSBrian Somers 
2266815097bSBrian Somers struct device *
2276815097bSBrian Somers udp_Create(struct physical *p)
2286815097bSBrian Somers {
2296815097bSBrian Somers   char *cp, *host, *port, *svc;
2306815097bSBrian Somers   struct udpdevice *dev;
2316815097bSBrian Somers 
2326815097bSBrian Somers   dev = NULL;
2336815097bSBrian Somers   if (p->fd < 0) {
2346815097bSBrian Somers     if ((cp = strchr(p->name.full, ':')) != NULL) {
2356815097bSBrian Somers       *cp = '\0';
2366815097bSBrian Somers       host = p->name.full;
2376815097bSBrian Somers       port = cp + 1;
2386815097bSBrian Somers       svc = strchr(port, '/');
2396815097bSBrian Somers       if (svc && strcasecmp(svc, "/udp")) {
2406815097bSBrian Somers         *cp = ':';
2416815097bSBrian Somers         return NULL;
2426815097bSBrian Somers       }
2436815097bSBrian Somers       if (svc)
2446815097bSBrian Somers         *svc = '\0';
2456815097bSBrian Somers 
2466815097bSBrian Somers       if (*host && *port)
2476815097bSBrian Somers         dev = udp_CreateDevice(p, host, port);
2486815097bSBrian Somers 
2496815097bSBrian Somers       *cp = ':';
2506815097bSBrian Somers       if (svc)
2516815097bSBrian Somers         *svc = '/';
2526815097bSBrian Somers     }
2536815097bSBrian Somers   } else {
2546815097bSBrian Somers     /* See if we're a connected udp socket */
2556815097bSBrian Somers     int type, sz, err;
2566815097bSBrian Somers 
2576815097bSBrian Somers     sz = sizeof type;
2586815097bSBrian Somers     if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 &&
2596815097bSBrian Somers         sz == sizeof type && type == SOCK_DGRAM) {
2606815097bSBrian Somers       if ((dev = malloc(sizeof *dev)) == NULL) {
2616815097bSBrian Somers         log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
2626815097bSBrian Somers                    p->link.name, strerror(errno));
2636815097bSBrian Somers         return NULL;
2646815097bSBrian Somers       }
2656815097bSBrian Somers 
2666815097bSBrian Somers       /* We can't getpeername().... hence we stay un-connect()ed */
2676815097bSBrian Somers       dev->connected = 0;
2686815097bSBrian Somers 
2696815097bSBrian Somers       log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name);
2706815097bSBrian Somers 
2716815097bSBrian Somers       if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) {
2726815097bSBrian Somers         log_Printf(LogPHASE, "%s:   Changing openmode to PASSIVE\n",
2736815097bSBrian Somers                    p->link.name);
2746815097bSBrian Somers         p->link.lcp.cfg.openmode = OPEN_PASSIVE;
2756815097bSBrian Somers       }
2766815097bSBrian Somers     }
2776815097bSBrian Somers   }
2786815097bSBrian Somers 
2796815097bSBrian Somers   if (dev) {
2806815097bSBrian Somers     memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
281acbd1f00SBrian Somers     physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
2826815097bSBrian Somers     return &dev->dev;
2836815097bSBrian Somers   }
2846815097bSBrian Somers 
2856815097bSBrian Somers   return NULL;
2866815097bSBrian Somers }
287