xref: /freebsd/usr.sbin/ppp/tcp.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
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 <sys/stat.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 "tcp.h"
60 
61 static int
62 tcp_OpenConnection(const char *name, char *host, char *port)
63 {
64   struct sockaddr_in dest;
65   int sock;
66   struct servent *sp;
67 
68   dest.sin_family = AF_INET;
69   dest.sin_addr = GetIpAddr(host);
70   if (dest.sin_addr.s_addr == INADDR_NONE) {
71     log_Printf(LogWARN, "%s: %s: unknown host\n", name, host);
72     return -2;
73   }
74   dest.sin_port = htons(atoi(port));
75   if (dest.sin_port == 0) {
76     sp = getservbyname(port, "tcp");
77     if (sp)
78       dest.sin_port = sp->s_port;
79     else {
80       log_Printf(LogWARN, "%s: %s: unknown service\n", name, port);
81       return -2;
82     }
83   }
84   log_Printf(LogPHASE, "%s: Connecting to %s:%s/tcp\n", name, host, port);
85 
86   sock = socket(PF_INET, SOCK_STREAM, 0);
87   if (sock < 0)
88     return -2;
89 
90   if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) {
91     log_Printf(LogWARN, "%s: connect: %s\n", name, strerror(errno));
92     close(sock);
93     return -2;
94   }
95 
96   return sock;
97 }
98 
99 static struct device tcpdevice = {
100   TCP_DEVICE,
101   "tcp",
102   0,
103   { CD_NOTREQUIRED, 0 },
104   NULL,
105   NULL,
106   NULL,
107   NULL,
108   NULL,
109   NULL,
110   NULL,
111   NULL,
112   NULL,
113   NULL,
114   NULL,
115   NULL,
116   NULL,
117   NULL
118 };
119 
120 struct device *
121 tcp_iov2device(int type, struct physical *p, struct iovec *iov,
122                int *niov, int maxiov __unused, int *auxfd __unused,
123 	       int *nauxfd __unused)
124 {
125   if (type == TCP_DEVICE) {
126     free(iov[(*niov)++].iov_base);
127     physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
128     return &tcpdevice;
129   }
130 
131   return NULL;
132 }
133 
134 struct device *
135 tcp_Create(struct physical *p)
136 {
137   char *cp, *host, *port, *svc;
138 
139   if (p->fd < 0) {
140     if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
141       *cp = '\0';
142       host = p->name.full;
143       port = cp + 1;
144       svc = strchr(port, '/');
145       if (svc && strcasecmp(svc, "/tcp")) {
146         *cp = ':';
147         return 0;
148       }
149       if (svc) {
150         p->fd--;     /* We own the device but maybe can't use it - change fd */
151         *svc = '\0';
152       }
153       if (*host && *port) {
154         p->fd = tcp_OpenConnection(p->link.name, host, port);
155         *cp = ':';
156         if (svc)
157           *svc = '/';
158         if (p->fd >= 0)
159           log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name,
160                      p->name.full);
161       } else {
162         if (svc)
163           *svc = '/';
164         *cp = ':';
165       }
166     }
167   }
168 
169   if (p->fd >= 0) {
170     /* See if we're a tcp socket */
171     struct stat st;
172 
173     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
174       int type, sz;
175 
176       sz = sizeof type;
177       if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
178         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
179         close(p->fd);
180         p->fd = -1;
181         return NULL;
182       }
183 
184       if (sz == sizeof type && type == SOCK_STREAM) {
185         struct sockaddr_in sock;
186         struct sockaddr *sockp = (struct sockaddr *)&sock;
187 
188         if (*p->name.full == '\0') {
189           sz = sizeof sock;
190           if (getpeername(p->fd, sockp, &sz) != 0 ||
191               sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) {
192             log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n",
193                        p->link.name);
194             return NULL;
195           }
196 
197           log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name);
198 
199           snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp",
200                    inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
201           p->name.base = p->name.full;
202         }
203         physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
204         if (p->cfg.cd.necessity != CD_DEFAULT)
205           log_Printf(LogWARN, "Carrier settings ignored\n");
206         return &tcpdevice;
207       }
208     }
209   }
210 
211   return NULL;
212 }
213