xref: /freebsd/usr.sbin/ppp/tcp.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29  */
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 #include <termios.h>
44 #include <unistd.h>
45 
46 #include "layer.h"
47 #include "defs.h"
48 #include "mbuf.h"
49 #include "log.h"
50 #include "timer.h"
51 #include "lqr.h"
52 #include "hdlc.h"
53 #include "throughput.h"
54 #include "fsm.h"
55 #include "lcp.h"
56 #include "ccp.h"
57 #include "link.h"
58 #include "async.h"
59 #include "descriptor.h"
60 #include "physical.h"
61 #include "tcp.h"
62 
63 static int
64 tcp_OpenConnection(const char *name, char *host, char *port)
65 {
66   struct sockaddr_in dest;
67   int sock;
68   struct servent *sp;
69 
70   dest.sin_family = AF_INET;
71   dest.sin_addr = GetIpAddr(host);
72   if (dest.sin_addr.s_addr == INADDR_NONE) {
73     log_Printf(LogWARN, "%s: %s: unknown host\n", name, host);
74     return -2;
75   }
76   dest.sin_port = htons(atoi(port));
77   if (dest.sin_port == 0) {
78     sp = getservbyname(port, "tcp");
79     if (sp)
80       dest.sin_port = sp->s_port;
81     else {
82       log_Printf(LogWARN, "%s: %s: unknown service\n", name, port);
83       return -2;
84     }
85   }
86   log_Printf(LogPHASE, "%s: Connecting to %s:%s/tcp\n", name, host, port);
87 
88   sock = socket(PF_INET, SOCK_STREAM, 0);
89   if (sock < 0)
90     return -2;
91 
92   if (connect(sock, (struct sockaddr *)&dest, sizeof dest) < 0) {
93     log_Printf(LogWARN, "%s: connect: %s\n", name, strerror(errno));
94     close(sock);
95     return -2;
96   }
97 
98   return sock;
99 }
100 
101 static struct device tcpdevice = {
102   TCP_DEVICE,
103   "tcp",
104   0,
105   { CD_NOTREQUIRED, 0 },
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   NULL,
119   NULL
120 };
121 
122 struct device *
123 tcp_iov2device(int type, struct physical *p, struct iovec *iov,
124                int *niov, int maxiov __unused, int *auxfd __unused,
125 	       int *nauxfd __unused)
126 {
127   if (type == TCP_DEVICE) {
128     free(iov[(*niov)++].iov_base);
129     physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
130     return &tcpdevice;
131   }
132 
133   return NULL;
134 }
135 
136 struct device *
137 tcp_Create(struct physical *p)
138 {
139   char *cp, *host, *port, *svc;
140 
141   if (p->fd < 0) {
142     if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
143       *cp = '\0';
144       host = p->name.full;
145       port = cp + 1;
146       svc = strchr(port, '/');
147       if (svc && strcasecmp(svc, "/tcp")) {
148         *cp = ':';
149         return 0;
150       }
151       if (svc) {
152         p->fd--;     /* We own the device but maybe can't use it - change fd */
153         *svc = '\0';
154       }
155       if (*host && *port) {
156         p->fd = tcp_OpenConnection(p->link.name, host, port);
157         *cp = ':';
158         if (svc)
159           *svc = '/';
160         if (p->fd >= 0)
161           log_Printf(LogDEBUG, "%s: Opened tcp socket %s\n", p->link.name,
162                      p->name.full);
163       } else {
164         if (svc)
165           *svc = '/';
166         *cp = ':';
167       }
168     }
169   }
170 
171   if (p->fd >= 0) {
172     /* See if we're a tcp socket */
173     struct stat st;
174 
175     if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
176       int type, sz;
177 
178       sz = sizeof type;
179       if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
180         log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
181         close(p->fd);
182         p->fd = -1;
183         return NULL;
184       }
185 
186       if (sz == sizeof type && type == SOCK_STREAM) {
187         struct sockaddr_in sock;
188         struct sockaddr *sockp = (struct sockaddr *)&sock;
189 
190         if (*p->name.full == '\0') {
191           sz = sizeof sock;
192           if (getpeername(p->fd, sockp, &sz) != 0 ||
193               sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) {
194             log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n",
195                        p->link.name);
196             return NULL;
197           }
198 
199           log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name);
200 
201           snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp",
202                    inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
203           p->name.base = p->name.full;
204         }
205         physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC);
206         if (p->cfg.cd.necessity != CD_DEFAULT)
207           log_Printf(LogWARN, "Carrier settings ignored\n");
208         return &tcpdevice;
209       }
210     }
211   }
212 
213   return NULL;
214 }
215