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$
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
37210386Srpaulovoid
3844743Smarkmfix_options(request)
3944743Smarkmstruct request_info *request;
4044743Smarkm{
4144743Smarkm#ifdef IP_OPTIONS
4244743Smarkm    unsigned char optbuf[BUFFER_SIZE / 3], *cp;
4344743Smarkm    char    lbuf[BUFFER_SIZE], *lp;
4444743Smarkm    int     optsize = sizeof(optbuf), ipproto;
4544743Smarkm    struct protoent *ip;
4644743Smarkm    int     fd = request->fd;
4744743Smarkm    unsigned int opt;
4844743Smarkm    int     optlen;
4944743Smarkm    struct in_addr dummy;
5056977Sshin#ifdef INET6
5156977Sshin    struct sockaddr_storage ss;
5256977Sshin    int sslen;
5344743Smarkm
5456977Sshin    /*
5556977Sshin     * check if this is AF_INET socket
5656977Sshin     * XXX IPv6 support?
5756977Sshin     */
5856977Sshin    sslen = sizeof(ss);
5956977Sshin    if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
6056977Sshin	syslog(LOG_ERR, "getpeername: %m");
6156977Sshin	clean_exit(request);
6256977Sshin    }
6356977Sshin    if (ss.ss_family != AF_INET)
6456977Sshin	return;
6556977Sshin#endif
6656977Sshin
6744743Smarkm    if ((ip = getprotobyname("ip")) != 0)
6844743Smarkm	ipproto = ip->p_proto;
6944743Smarkm    else
7044743Smarkm	ipproto = IPPROTO_IP;
7144743Smarkm
7244743Smarkm    if (getsockopt(fd, ipproto, IP_OPTIONS, (char *) optbuf, &optsize) == 0
7344743Smarkm	&& optsize != 0) {
7444743Smarkm
7544743Smarkm	/*
7644743Smarkm	 * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
7744743Smarkm	 * address to the result IP options list when source routing options
7844743Smarkm	 * are present (see <netinet/ip_var.h>), but produces no output for
7944743Smarkm	 * other IP options. Solaris 2.x getsockopt() does produce output for
8044743Smarkm	 * non-routing IP options, and uses the same format as BSD even when
8144743Smarkm	 * the space for the destination address is unused. The code below
8244743Smarkm	 * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
8344743Smarkm	 * may occasionally miss source routing options on incompatible
8444743Smarkm	 * systems such as Linux. Their choice.
8544743Smarkm	 *
8644743Smarkm	 * Look for source routing options. Drop the connection when one is
8744743Smarkm	 * found. Just wiping the IP options is insufficient: we would still
8844743Smarkm	 * help the attacker by providing a real TCP sequence number, and the
8944743Smarkm	 * attacker would still be able to send packets (blind spoofing). I
9044743Smarkm	 * discussed this attack with Niels Provos, half a year before the
9144743Smarkm	 * attack was described in open mailing lists.
9244743Smarkm	 *
9344743Smarkm	 * It would be cleaner to just return a yes/no reply and let the caller
9444743Smarkm	 * decide how to deal with it. Resident servers should not terminate.
9544743Smarkm	 * However I am not prepared to make changes to internal interfaces
9644743Smarkm	 * on short notice.
9744743Smarkm	 */
9844743Smarkm#define ADDR_LEN sizeof(dummy.s_addr)
9944743Smarkm
10044743Smarkm	for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
10144743Smarkm	    opt = cp[IPOPT_OPTVAL];
10244743Smarkm	    if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
10344743Smarkm		syslog(LOG_WARNING,
10444743Smarkm		   "refused connect from %s with IP source routing options",
10544743Smarkm		       eval_client(request));
10644743Smarkm		shutdown(fd, 2);
10744743Smarkm		return;
10844743Smarkm	    }
10944743Smarkm	    if (opt == IPOPT_EOL)
11044743Smarkm		break;
11144743Smarkm	    if (opt == IPOPT_NOP) {
11244743Smarkm		optlen = 1;
11344743Smarkm	    } else {
11444743Smarkm		optlen = cp[IPOPT_OLEN];
11544743Smarkm		if (optlen <= 0)		/* Do not loop! */
11644743Smarkm		    break;
11744743Smarkm	    }
11844743Smarkm	}
11944743Smarkm	lp = lbuf;
12044743Smarkm	for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
12144743Smarkm	    sprintf(lp, " %2.2x", *cp);
12244743Smarkm	syslog(LOG_NOTICE,
12344743Smarkm	       "connect from %s with IP options (ignored):%s",
12444743Smarkm	       eval_client(request), lbuf);
12544743Smarkm	if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
12644743Smarkm	    syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
12744743Smarkm	    shutdown(fd, 2);
12844743Smarkm	}
12944743Smarkm    }
13044743Smarkm#endif
13144743Smarkm}
132