ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 9892
Committed: Sat Jan 16 18:20:59 2021 UTC (5 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 16013 byte(s)
Log Message:
- s_bsd.c:add_connection(): fixed stupidly broken 'ipv6 loopback address irc-sanitizer' logic

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 9857 * Copyright (c) 1997-2021 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file s_bsd.c
23     * \brief Network functions.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28     #include <netinet/in_systm.h>
29     #include <netinet/ip.h>
30     #include <netinet/tcp.h>
31 michael 1011 #include "list.h"
32 adx 30 #include "fdlist.h"
33     #include "s_bsd.h"
34     #include "client.h"
35     #include "dbuf.h"
36     #include "event.h"
37     #include "irc_string.h"
38     #include "ircd.h"
39     #include "listener.h"
40     #include "numeric.h"
41     #include "packet.h"
42 michael 3322 #include "res.h"
43 adx 30 #include "restart.h"
44 michael 1309 #include "conf.h"
45     #include "log.h"
46 michael 3347 #include "server.h"
47 adx 30 #include "send.h"
48     #include "memory.h"
49 michael 3347 #include "user.h"
50 adx 30
51 michael 3274
52 michael 5887 static const char *const comm_err_str[] =
53     {
54     [COMM_OK] = "Comm OK",
55     [COMM_ERR_BIND] = "Error during bind()",
56     [COMM_ERR_TIMEOUT] = "connect timeout",
57     [COMM_ERR_CONNECT] = "Error during connect()",
58     [COMM_ERROR] = "Comm Error"
59     };
60 adx 30
61 michael 1013 static void comm_connect_callback(fde_t *, int);
62 michael 4461 static void comm_connect_timeout(fde_t *, void *);
63     static void comm_connect_tryconnect(fde_t *, void *);
64 adx 30
65    
66 michael 8414 /* comm_get_sockerr - get the error value from the socket or the current errno
67 adx 30 *
68     * Get the *real* error from the socket (well try to anyway..).
69     * This may only work when SO_DEBUG is enabled but its worth the
70     * gamble anyway.
71     */
72     int
73 michael 8826 comm_get_sockerr(fde_t *F)
74 adx 30 {
75     int errtmp = errno;
76     #ifdef SO_ERROR
77     int err = 0;
78     socklen_t len = sizeof(err);
79    
80 michael 8826 assert(F);
81     assert(F->flags.open == true);
82    
83     if (getsockopt(F->fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
84 adx 30 {
85     if (err)
86     errtmp = err;
87     }
88 michael 8557
89 adx 30 errno = errtmp;
90     #endif
91     return errtmp;
92     }
93    
94     /*
95 michael 2916 * report_error - report an error from an errno.
96 adx 30 * Record error to log and also send a copy to all *LOCAL* opers online.
97     *
98     * text is a *format* string for outputing error. It must
99     * contain only two '%s', the first will be replaced
100 michael 9250 * by the sockhost from the client, and the latter will
101 adx 30 * be taken from sys_errlist[errno].
102     *
103 michael 9250 * client if not NULL, is the *LOCAL* client associated with
104 adx 30 * the error.
105     *
106     * Cannot use perror() within daemon. stderr is closed in
107     * ircd and cannot be used. And, worse yet, it might have
108     * been reassigned to a normal connection...
109 michael 2916 *
110 adx 30 * Actually stderr is still there IFF ircd was run with -s --Rodder
111     */
112     void
113 michael 8431 report_error(int level, const char *text, const char *who, int error)
114 adx 30 {
115     who = (who) ? who : "";
116    
117 michael 1618 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
118     text, who, strerror(error));
119 michael 1247 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
120 adx 30 }
121    
122     /*
123     * setup_socket()
124     *
125     * Set the socket non-blocking, and other wonderful bits.
126     */
127 michael 2632 static void
128     setup_socket(int fd)
129 adx 30 {
130     int opt = 1;
131    
132 michael 967 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
133 adx 30
134     #ifdef IPTOS_LOWDELAY
135     opt = IPTOS_LOWDELAY;
136 michael 967 setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
137 adx 30 #endif
138    
139 michael 9054 #ifdef TCP_QUICKACK
140     opt = 1;
141     setsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &opt, sizeof(opt));
142     #endif
143    
144 adx 30 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
145     }
146    
147     /*
148     * ssl_handshake - let OpenSSL initialize the protocol. Register for
149     * read/write events if necessary.
150     */
151     static void
152 michael 8339 ssl_handshake(fde_t *F, void *data)
153 adx 30 {
154 michael 9250 struct Client *client = data;
155 adx 30
156 michael 9250 assert(client);
157     assert(client->connection);
158     assert(client->connection->fd);
159     assert(client->connection->fd == F);
160 michael 8339
161 michael 9157 tls_handshake_status_t ret = tls_handshake(&F->tls, TLS_ROLE_SERVER, NULL);
162 michael 7105 if (ret != TLS_HANDSHAKE_DONE)
163 michael 2463 {
164 michael 9250 if ((event_base->time.sec_monotonic - client->connection->created_monotonic) > TLS_HANDSHAKE_TIMEOUT)
165 michael 2725 {
166 michael 9250 exit_client(client, "Timeout during TLS handshake");
167 michael 2725 return;
168     }
169    
170 michael 7105 switch (ret)
171 michael 2463 {
172 michael 7105 case TLS_HANDSHAKE_WANT_WRITE:
173 michael 9250 comm_setselect(F, COMM_SELECT_WRITE, ssl_handshake, client, TLS_HANDSHAKE_TIMEOUT);
174 michael 2463 return;
175 michael 7105 case TLS_HANDSHAKE_WANT_READ:
176 michael 9250 comm_setselect(F, COMM_SELECT_READ, ssl_handshake, client, TLS_HANDSHAKE_TIMEOUT);
177 michael 2463 return;
178     default:
179 michael 9250 exit_client(client, "Error during TLS handshake");
180 michael 2916 return;
181 michael 2463 }
182     }
183    
184 michael 8339 comm_settimeout(F, 0, NULL, NULL);
185 michael 2733
186 michael 9408 if (tls_verify_certificate(&F->tls, ConfigServerInfo.message_digest_algorithm, &client->tls_certfp) == false)
187 michael 9213 ilog(LOG_TYPE_IRCD, "Client %s gave bad TLS client certificate",
188 michael 9250 client_get_name(client, MASK_IP));
189 michael 2228
190 michael 9250 auth_start(client);
191 adx 30 }
192    
193     /*
194 michael 2916 * add_connection - creates a client which has just connected to us on
195 adx 30 * the given fd. The sockhost field is initialized with the ip# of the host.
196     * An unique id is calculated now, in case it is needed for auth.
197     * The client is sent to the auth module for verification, and not put in
198     * any client list yet.
199     */
200     void
201 michael 549 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
202 adx 30 {
203 michael 9250 struct Client *client = client_make(NULL);
204 michael 549
205 michael 9590 client->connection->fd = fd_open(fd, true, listener_has_flag(listener, LISTENER_TLS) ?
206     "Incoming TLS connection" : "Incoming connection");
207 adx 30
208 michael 1123 /*
209 adx 30 * copy address to 'sockhost' as a string, copy it to host too
210     * so we have something valid to put into error messages...
211     */
212 michael 9250 client->ip = *irn;
213 adx 30
214 michael 9250 getnameinfo((const struct sockaddr *)&client->ip,
215     client->ip.ss_len, client->sockhost,
216     sizeof(client->sockhost), NULL, 0, NI_NUMERICHOST);
217 michael 1072
218 michael 9892 if (client->sockhost[0] == ':' &&
219     client->sockhost[1] == ':')
220 adx 30 {
221 michael 9892 memmove(client->sockhost + 1, client->sockhost, sizeof(client->sockhost) - 1);
222 michael 9250 client->sockhost[0] = '0';
223 michael 549 }
224 adx 30
225 michael 9250 strlcpy(client->host, client->sockhost, sizeof(client->host));
226 michael 8265
227 michael 9250 client->connection->listener = listener;
228 adx 30 ++listener->ref_count;
229    
230 michael 9590 if (listener_has_flag(listener, LISTENER_TLS))
231 adx 30 {
232 michael 9250 if (tls_new(&client->connection->fd->tls, fd, TLS_ROLE_SERVER) == false)
233 adx 30 {
234 michael 9250 SetDead(client);
235     exit_client(client, "TLS context initialization failed");
236 adx 30 return;
237     }
238    
239 michael 9250 AddFlag(client, FLAGS_TLS);
240     ssl_handshake(client->connection->fd, client);
241 adx 30 }
242     else
243 michael 9250 auth_start(client);
244 adx 30 }
245    
246     /*
247     * stolen from squid - its a neat (but overused! :) routine which we
248     * can use to see whether we can ignore this errno or not. It is
249     * generally useful for non-blocking network IO related errnos.
250     * -- adrian
251     */
252 michael 8660 bool
253 michael 8414 comm_ignore_errno(int ierrno)
254 adx 30 {
255     switch (ierrno)
256     {
257     case EINPROGRESS:
258     case EWOULDBLOCK:
259     #if EAGAIN != EWOULDBLOCK
260     case EAGAIN:
261     #endif
262     case EALREADY:
263     case EINTR:
264     #ifdef ERESTART
265     case ERESTART:
266     #endif
267 michael 8660 return true;
268 adx 30 default:
269 michael 8660 return false;
270 adx 30 }
271     }
272    
273     /*
274     * comm_settimeout() - set the socket timeout
275     *
276     * Set the timeout for the fd
277     */
278     void
279 michael 8339 comm_settimeout(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
280 adx 30 {
281 michael 8717 assert(F);
282 michael 8658 assert(F->flags.open == true);
283 adx 30
284 michael 9438 F->timeout = timeout ? event_base->time.sec_monotonic + timeout : 0;
285 michael 8339 F->timeout_handler = callback;
286     F->timeout_data = cbdata;
287 adx 30 }
288    
289     /*
290     * comm_setflush() - set a flush function
291     *
292     * A flush function is simply a function called if found during
293     * comm_timeouts(). Its basically a second timeout, except in this case
294     * I'm too lazy to implement multiple timeout functions! :-)
295     * its kinda nice to have it separate, since this is designed for
296     * flush functions, and when comm_close() is implemented correctly
297     * with close functions, we _actually_ don't call comm_close() here ..
298     * -- originally Adrian's notes
299 michael 2916 * comm_close() is replaced with fd_close() in fdlist.c
300 adx 30 */
301     void
302 michael 8339 comm_setflush(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata)
303 adx 30 {
304 michael 8717 assert(F);
305 michael 8658 assert(F->flags.open == true);
306 adx 30
307 michael 9438 F->flush_timeout = timeout ? event_base->time.sec_monotonic + timeout : 0;
308 michael 8339 F->flush_handler = callback;
309     F->flush_data = cbdata;
310 adx 30 }
311    
312     /*
313     * comm_checktimeouts() - check the socket timeouts
314     *
315     * All this routine does is call the given callback/cbdata, without closing
316     * down the file descriptor. When close handlers have been implemented,
317     * this will happen.
318     */
319     void
320 michael 4439 comm_checktimeouts(void *unused)
321 adx 30 {
322 michael 8339 for (int fd = 0; fd <= highest_fd; ++fd)
323 michael 7431 {
324 michael 8339 fde_t *F = &fd_table[fd];
325 adx 30
326 michael 8658 if (F->flags.open == false)
327 michael 8339 continue;
328 adx 30
329 michael 8339 /* check flush functions */
330 michael 9440 if (F->flush_timeout && F->flush_timeout < event_base->time.sec_monotonic)
331 michael 8339 {
332 michael 9440 void (*hdl)(fde_t *, void *) = F->flush_handler;
333     void *data = F->flush_data;
334 michael 8349
335 michael 8339 comm_setflush(F, 0, NULL, NULL);
336     hdl(F, data);
337 adx 30 }
338 michael 8339
339     /* check timeouts */
340 michael 9440 if (F->timeout && F->timeout < event_base->time.sec_monotonic)
341 michael 8339 {
342     /* Call timeout handler */
343 michael 9440 void (*hdl)(fde_t *, void *) = F->timeout_handler;
344     void *data = F->timeout_data;
345 michael 8349
346 michael 8339 comm_settimeout(F, 0, NULL, NULL);
347     hdl(F, data);
348     }
349 michael 7431 }
350 adx 30 }
351    
352     /*
353     * void comm_connect_tcp(int fd, const char *host, unsigned short port,
354     * struct sockaddr *clocal, int socklen,
355     * CNCB *callback, void *data, int aftype, int timeout)
356     * Input: An fd to connect with, a host and port to connect to,
357     * a local sockaddr to connect from + length(or NULL to use the
358     * default), a callback, the data to pass into the callback, the
359     * address family.
360     * Output: None.
361     * Side-effects: A non-blocking connection to the host is started, and
362     * if necessary, set up for selection. The callback given
363     * may be called now, or it may be called later.
364     */
365     void
366 michael 8872 comm_connect_tcp(fde_t *F, const struct irc_ssaddr *caddr, unsigned short port, const struct irc_ssaddr *baddr,
367     void (*callback)(fde_t *, int, void *), void *data, uintmax_t timeout)
368 adx 30 {
369 michael 8872 assert(callback);
370 adx 30
371 michael 9043 F->connect.hostaddr = *caddr;
372 michael 8872 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
373     ((struct sockaddr_in *)&F->connect.hostaddr)->sin_port = htons(port);
374 michael 8339 F->connect.callback = callback;
375     F->connect.data = data;
376 adx 30
377     /* Note that we're using a passed sockaddr here. This is because
378     * generally you'll be bind()ing to a sockaddr grabbed from
379     * getsockname(), so this makes things easier.
380     * XXX If NULL is passed as local, we should later on bind() to the
381     * virtual host IP, for completeness.
382     * -- adrian
383     */
384 michael 8872 if (baddr && bind(F->fd, (const struct sockaddr *)baddr, baddr->ss_len) < 0)
385 michael 2916 {
386 adx 30 /* Failure, call the callback with COMM_ERR_BIND */
387 michael 8339 comm_connect_callback(F, COMM_ERR_BIND);
388 michael 6948 return; /* ... and quit */
389 adx 30 }
390    
391 michael 8941 comm_settimeout(F, timeout, comm_connect_timeout, NULL);
392 michael 8872 comm_connect_tryconnect(F, NULL);
393 adx 30 }
394    
395     /*
396     * comm_connect_callback() - call the callback, and continue with life
397     */
398     static void
399 michael 8339 comm_connect_callback(fde_t *F, int status)
400 adx 30 {
401 michael 4464 void (*hdl)(fde_t *, int, void *);
402 adx 30
403     /* This check is gross..but probably necessary */
404 michael 8339 if (F->connect.callback == NULL)
405 adx 30 return;
406    
407     /* Clear the connect flag + handler */
408 michael 8339 hdl = F->connect.callback;
409     F->connect.callback = NULL;
410 adx 30
411     /* Clear the timeout handler */
412 michael 8339 comm_settimeout(F, 0, NULL, NULL);
413 adx 30
414     /* Call the handler */
415 michael 8339 hdl(F, status, F->connect.data);
416 adx 30 }
417    
418     /*
419     * comm_connect_timeout() - this gets called when the socket connection
420     * times out. This *only* can be called once connect() is initially
421     * called ..
422     */
423     static void
424 michael 8339 comm_connect_timeout(fde_t *F, void *unused)
425 adx 30 {
426     /* error! */
427 michael 8339 comm_connect_callback(F, COMM_ERR_TIMEOUT);
428 adx 30 }
429    
430 michael 8339 /* static void comm_connect_tryconnect(fde_t *fd, void *unused)
431 adx 30 * Input: The fd, the handler data(unused).
432     * Output: None.
433     * Side-effects: Try and connect with pending connect data for the FD. If
434     * we succeed or get a fatal error, call the callback.
435     * Otherwise, it is still blocking or something, so register
436     * to select for a write event on this FD.
437     */
438     static void
439 michael 8339 comm_connect_tryconnect(fde_t *F, void *unused)
440 adx 30 {
441     /* This check is needed or re-entrant s_bsd_* like sigio break it. */
442 michael 8339 if (F->connect.callback == NULL)
443 adx 30 return;
444    
445     /* Try the connect() */
446 michael 8339 int retval = connect(F->fd, (struct sockaddr *)&F->connect.hostaddr, F->connect.hostaddr.ss_len);
447 adx 30
448     /* Error? */
449     if (retval < 0)
450     {
451     /*
452     * If we get EISCONN, then we've already connect()ed the socket,
453     * which is a good thing.
454     * -- adrian
455     */
456     if (errno == EISCONN)
457 michael 8339 comm_connect_callback(F, COMM_OK);
458 michael 8414 else if (comm_ignore_errno(errno))
459 adx 30 /* Ignore error? Reschedule */
460 michael 8339 comm_setselect(F, COMM_SELECT_WRITE, comm_connect_tryconnect, NULL, 0);
461 adx 30 else
462     /* Error? Fail with COMM_ERR_CONNECT */
463 michael 8339 comm_connect_callback(F, COMM_ERR_CONNECT);
464 adx 30 return;
465     }
466    
467     /* If we get here, we've suceeded, so call with COMM_OK */
468 michael 8339 comm_connect_callback(F, COMM_OK);
469 adx 30 }
470    
471     /*
472     * comm_errorstr() - return an error string for the given error condition
473     */
474     const char *
475     comm_errstr(int error)
476     {
477     if (error < 0 || error >= COMM_ERR_MAX)
478     return "Invalid error number!";
479     return comm_err_str[error];
480     }
481    
482     /*
483     * comm_open() - open a socket
484     *
485     * This is a highly highly cut down version of squid's comm_open() which
486     * for the most part emulates socket(), *EXCEPT* it fails if we're about
487     * to run out of file descriptors.
488     */
489     int
490 michael 8339 comm_socket(int family, int sock_type, int proto)
491 adx 30 {
492     /* First, make sure we aren't going to run out of file descriptors */
493     if (number_fd >= hard_fdlimit)
494     {
495     errno = ENFILE;
496     return -1;
497     }
498    
499     /*
500     * Next, we try to open the socket. We *should* drop the reserved FD
501     * limit if/when we get an error, but we can deal with that later.
502     * XXX !!! -- adrian
503     */
504 michael 7431 int fd = socket(family, sock_type, proto);
505 adx 30 if (fd < 0)
506     return -1; /* errno will be passed through, yay.. */
507    
508 michael 2632 setup_socket(fd);
509 adx 30
510 michael 8339 return fd;
511 adx 30 }
512    
513     /*
514     * comm_accept() - accept an incoming connection
515     *
516     * This is a simple wrapper for accept() which enforces FD limits like
517     * comm_open() does. Returned fd must be either closed or tagged with
518     * fd_open (this function no longer does it).
519     */
520     int
521 michael 8826 comm_accept(fde_t *F, struct irc_ssaddr *addr)
522 adx 30 {
523     socklen_t addrlen = sizeof(struct irc_ssaddr);
524    
525     if (number_fd >= hard_fdlimit)
526     {
527     errno = ENFILE;
528     return -1;
529     }
530    
531 michael 8431 memset(addr, 0, sizeof(*addr));
532 michael 4407
533 adx 30 /*
534     * Next, do the accept(). if we get an error, we should drop the
535     * reserved fd limit, but we can deal with that when comm_open()
536     * also does it. XXX -- adrian
537     */
538 michael 8826 int fd = accept(F->fd, (struct sockaddr *)addr, &addrlen);
539     if (fd < 0)
540 adx 30 return -1;
541    
542 michael 4407 remove_ipv6_mapping(addr);
543 adx 30
544 michael 8826 setup_socket(fd);
545 adx 30
546     /* .. and return */
547 michael 8826 return fd;
548 adx 30 }
549    
550 michael 2916 /*
551 adx 30 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
552 michael 1122 * OSes with IPv6 mapping listening on both
553 adx 30 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
554 michael 2916 *
555 adx 30 */
556     void
557     remove_ipv6_mapping(struct irc_ssaddr *addr)
558     {
559     if (addr->ss.ss_family == AF_INET6)
560     {
561 michael 1122 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
562     {
563 michael 2916 struct sockaddr_in6 v6;
564 michael 1122 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
565 adx 30
566 michael 1122 memcpy(&v6, addr, sizeof(v6));
567     memset(v4, 0, sizeof(struct sockaddr_in));
568     memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
569    
570 adx 30 addr->ss.ss_family = AF_INET;
571     addr->ss_len = sizeof(struct sockaddr_in);
572     }
573 michael 2916 else
574 adx 30 addr->ss_len = sizeof(struct sockaddr_in6);
575     }
576     else
577     addr->ss_len = sizeof(struct sockaddr_in);
578 michael 2916 }

Properties

Name Value
svn:eol-style native
svn:keywords Id