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