fix_options.c revision 56977
144743Smarkm /*
244743Smarkm  * Routine to disable IP-level socket options. This code was taken from 4.4BSD
344743Smarkm  * rlogind and kernel source, but all mistakes in it are my fault.
444743Smarkm  *
544743Smarkm  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
656977Sshin  *
756977Sshin  * $FreeBSD: head/contrib/tcp_wrappers/fix_options.c 56977 2000-02-03 10:27:03Z shin $
844743Smarkm  */
944743Smarkm
1044743Smarkm#ifndef lint
1144743Smarkmstatic char sccsid[] = "@(#) fix_options.c 1.6 97/04/08 02:29:19";
1244743Smarkm#endif
1344743Smarkm
1444743Smarkm#include <sys/types.h>
1544743Smarkm#include <sys/param.h>
1656977Sshin#ifdef INET6
1756977Sshin#include <sys/socket.h>
1856977Sshin#endif
1944743Smarkm#include <netinet/in.h>
2044743Smarkm#include <netinet/in_systm.h>
2144743Smarkm#include <netinet/ip.h>
2244743Smarkm#include <netdb.h>
2344743Smarkm#include <stdio.h>
2444743Smarkm#include <syslog.h>
2544743Smarkm
2644743Smarkm#ifndef IPOPT_OPTVAL
2744743Smarkm#define IPOPT_OPTVAL	0
2844743Smarkm#define IPOPT_OLEN	1
2944743Smarkm#endif
3044743Smarkm
3144743Smarkm#include "tcpd.h"
3244743Smarkm
3344743Smarkm#define BUFFER_SIZE	512		/* Was: BUFSIZ */
3444743Smarkm
3544743Smarkm/* fix_options - get rid of IP-level socket options */
3644743Smarkm
3744743Smarkmfix_options(request)
3844743Smarkmstruct request_info *request;
3944743Smarkm{
4044743Smarkm#ifdef IP_OPTIONS
4144743Smarkm    unsigned char optbuf[BUFFER_SIZE / 3], *cp;
4244743Smarkm    char    lbuf[BUFFER_SIZE], *lp;
4344743Smarkm    int     optsize = sizeof(optbuf), ipproto;
4444743Smarkm    struct protoent *ip;
4544743Smarkm    int     fd = request->fd;
4644743Smarkm    unsigned int opt;
4744743Smarkm    int     optlen;
4844743Smarkm    struct in_addr dummy;
4956977Sshin#ifdef INET6
5056977Sshin    struct sockaddr_storage ss;
5156977Sshin    int sslen;
5244743Smarkm
5356977Sshin    /*
5456977Sshin     * check if this is AF_INET socket
5556977Sshin     * XXX IPv6 support?
5656977Sshin     */
5756977Sshin    sslen = sizeof(ss);
5856977Sshin    if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
5956977Sshin	syslog(LOG_ERR, "getpeername: %m");
6056977Sshin	clean_exit(request);
6156977Sshin    }
6256977Sshin    if (ss.ss_family != AF_INET)
6356977Sshin	return;
6456977Sshin#endif
6556977Sshin
6644743Smarkm    if ((ip = getprotobyname("ip")) != 0)
6744743Smarkm	ipproto = ip->p_proto;
6844743Smarkm    else
6944743Smarkm	ipproto = IPPROTO_IP;
7044743Smarkm
7144743Smarkm    if (getsockopt(fd, ipproto, IP_OPTIONS, (char *) optbuf, &optsize) == 0
7244743Smarkm	&& optsize != 0) {
7344743Smarkm
7444743Smarkm	/*
7544743Smarkm	 * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
7644743Smarkm	 * address to the result IP options list when source routing options
7744743Smarkm	 * are present (see <netinet/ip_var.h>), but produces no output for
7844743Smarkm	 * other IP options. Solaris 2.x getsockopt() does produce output for
7944743Smarkm	 * non-routing IP options, and uses the same format as BSD even when
8044743Smarkm	 * the space for the destination address is unused. The code below
8144743Smarkm	 * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
8244743Smarkm	 * may occasionally miss source routing options on incompatible
8344743Smarkm	 * systems such as Linux. Their choice.
8444743Smarkm	 *
8544743Smarkm	 * Look for source routing options. Drop the connection when one is
8644743Smarkm	 * found. Just wiping the IP options is insufficient: we would still
8744743Smarkm	 * help the attacker by providing a real TCP sequence number, and the
8844743Smarkm	 * attacker would still be able to send packets (blind spoofing). I
8944743Smarkm	 * discussed this attack with Niels Provos, half a year before the
9044743Smarkm	 * attack was described in open mailing lists.
9144743Smarkm	 *
9244743Smarkm	 * It would be cleaner to just return a yes/no reply and let the caller
9344743Smarkm	 * decide how to deal with it. Resident servers should not terminate.
9444743Smarkm	 * However I am not prepared to make changes to internal interfaces
9544743Smarkm	 * on short notice.
9644743Smarkm	 */
9744743Smarkm#define ADDR_LEN sizeof(dummy.s_addr)
9844743Smarkm
9944743Smarkm	for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
10044743Smarkm	    opt = cp[IPOPT_OPTVAL];
10144743Smarkm	    if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
10244743Smarkm		syslog(LOG_WARNING,
10344743Smarkm		   "refused connect from %s with IP source routing options",
10444743Smarkm		       eval_client(request));
10544743Smarkm		shutdown(fd, 2);
10644743Smarkm		return;
10744743Smarkm	    }
10844743Smarkm	    if (opt == IPOPT_EOL)
10944743Smarkm		break;
11044743Smarkm	    if (opt == IPOPT_NOP) {
11144743Smarkm		optlen = 1;
11244743Smarkm	    } else {
11344743Smarkm		optlen = cp[IPOPT_OLEN];
11444743Smarkm		if (optlen <= 0)		/* Do not loop! */
11544743Smarkm		    break;
11644743Smarkm	    }
11744743Smarkm	}
11844743Smarkm	lp = lbuf;
11944743Smarkm	for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
12044743Smarkm	    sprintf(lp, " %2.2x", *cp);
12144743Smarkm	syslog(LOG_NOTICE,
12244743Smarkm	       "connect from %s with IP options (ignored):%s",
12344743Smarkm	       eval_client(request), lbuf);
12444743Smarkm	if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
12544743Smarkm	    syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
12644743Smarkm	    shutdown(fd, 2);
12744743Smarkm	}
12844743Smarkm    }
12944743Smarkm#endif
13044743Smarkm}
131