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