12aef6930SMark Murray /*
22aef6930SMark Murray * Routine to disable IP-level socket options. This code was taken from 4.4BSD
32aef6930SMark Murray * rlogind and kernel source, but all mistakes in it are my fault.
42aef6930SMark Murray *
52aef6930SMark Murray * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
68053080cSYoshinobu Inoue *
78053080cSYoshinobu Inoue * $FreeBSD$
82aef6930SMark Murray */
92aef6930SMark Murray
102aef6930SMark Murray #ifndef lint
112aef6930SMark Murray static char sccsid[] = "@(#) fix_options.c 1.6 97/04/08 02:29:19";
122aef6930SMark Murray #endif
132aef6930SMark Murray
142aef6930SMark Murray #include <sys/types.h>
152aef6930SMark Murray #include <sys/param.h>
168053080cSYoshinobu Inoue #ifdef INET6
178053080cSYoshinobu Inoue #include <sys/socket.h>
188053080cSYoshinobu Inoue #endif
192aef6930SMark Murray #include <netinet/in.h>
202aef6930SMark Murray #include <netinet/in_systm.h>
212aef6930SMark Murray #include <netinet/ip.h>
222aef6930SMark Murray #include <netdb.h>
232aef6930SMark Murray #include <stdio.h>
242aef6930SMark Murray #include <syslog.h>
252aef6930SMark Murray
262aef6930SMark Murray #ifndef IPOPT_OPTVAL
272aef6930SMark Murray #define IPOPT_OPTVAL 0
282aef6930SMark Murray #define IPOPT_OLEN 1
292aef6930SMark Murray #endif
302aef6930SMark Murray
312aef6930SMark Murray #include "tcpd.h"
322aef6930SMark Murray
332aef6930SMark Murray #define BUFFER_SIZE 512 /* Was: BUFSIZ */
342aef6930SMark Murray
352aef6930SMark Murray /* fix_options - get rid of IP-level socket options */
362aef6930SMark Murray
37247cd152SRui Paulo void
fix_options(struct request_info * request)38*14f102eaSEd Maste fix_options(struct request_info *request)
392aef6930SMark Murray {
402aef6930SMark Murray #ifdef IP_OPTIONS
412aef6930SMark Murray unsigned char optbuf[BUFFER_SIZE / 3], *cp;
422aef6930SMark Murray char lbuf[BUFFER_SIZE], *lp;
432aef6930SMark Murray int optsize = sizeof(optbuf), ipproto;
442aef6930SMark Murray struct protoent *ip;
452aef6930SMark Murray int fd = request->fd;
462aef6930SMark Murray unsigned int opt;
472aef6930SMark Murray int optlen;
482aef6930SMark Murray struct in_addr dummy;
498053080cSYoshinobu Inoue #ifdef INET6
508053080cSYoshinobu Inoue struct sockaddr_storage ss;
518053080cSYoshinobu Inoue int sslen;
528053080cSYoshinobu Inoue
538053080cSYoshinobu Inoue /*
548053080cSYoshinobu Inoue * check if this is AF_INET socket
558053080cSYoshinobu Inoue * XXX IPv6 support?
568053080cSYoshinobu Inoue */
578053080cSYoshinobu Inoue sslen = sizeof(ss);
588053080cSYoshinobu Inoue if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
598053080cSYoshinobu Inoue syslog(LOG_ERR, "getpeername: %m");
608053080cSYoshinobu Inoue clean_exit(request);
618053080cSYoshinobu Inoue }
628053080cSYoshinobu Inoue if (ss.ss_family != AF_INET)
638053080cSYoshinobu Inoue return;
648053080cSYoshinobu Inoue #endif
652aef6930SMark Murray
662aef6930SMark Murray if ((ip = getprotobyname("ip")) != 0)
672aef6930SMark Murray ipproto = ip->p_proto;
682aef6930SMark Murray else
692aef6930SMark Murray ipproto = IPPROTO_IP;
702aef6930SMark Murray
712aef6930SMark Murray if (getsockopt(fd, ipproto, IP_OPTIONS, (char *) optbuf, &optsize) == 0
722aef6930SMark Murray && optsize != 0) {
732aef6930SMark Murray
742aef6930SMark Murray /*
752aef6930SMark Murray * Horror! 4.[34] BSD getsockopt() prepends the first-hop destination
762aef6930SMark Murray * address to the result IP options list when source routing options
772aef6930SMark Murray * are present (see <netinet/ip_var.h>), but produces no output for
782aef6930SMark Murray * other IP options. Solaris 2.x getsockopt() does produce output for
792aef6930SMark Murray * non-routing IP options, and uses the same format as BSD even when
802aef6930SMark Murray * the space for the destination address is unused. The code below
812aef6930SMark Murray * does the right thing with 4.[34]BSD derivatives and Solaris 2, but
822aef6930SMark Murray * may occasionally miss source routing options on incompatible
832aef6930SMark Murray * systems such as Linux. Their choice.
842aef6930SMark Murray *
852aef6930SMark Murray * Look for source routing options. Drop the connection when one is
862aef6930SMark Murray * found. Just wiping the IP options is insufficient: we would still
872aef6930SMark Murray * help the attacker by providing a real TCP sequence number, and the
882aef6930SMark Murray * attacker would still be able to send packets (blind spoofing). I
892aef6930SMark Murray * discussed this attack with Niels Provos, half a year before the
902aef6930SMark Murray * attack was described in open mailing lists.
912aef6930SMark Murray *
922aef6930SMark Murray * It would be cleaner to just return a yes/no reply and let the caller
932aef6930SMark Murray * decide how to deal with it. Resident servers should not terminate.
942aef6930SMark Murray * However I am not prepared to make changes to internal interfaces
952aef6930SMark Murray * on short notice.
962aef6930SMark Murray */
972aef6930SMark Murray #define ADDR_LEN sizeof(dummy.s_addr)
982aef6930SMark Murray
992aef6930SMark Murray for (cp = optbuf + ADDR_LEN; cp < optbuf + optsize; cp += optlen) {
1002aef6930SMark Murray opt = cp[IPOPT_OPTVAL];
1012aef6930SMark Murray if (opt == IPOPT_LSRR || opt == IPOPT_SSRR) {
1022aef6930SMark Murray syslog(LOG_WARNING,
1032aef6930SMark Murray "refused connect from %s with IP source routing options",
1042aef6930SMark Murray eval_client(request));
1052aef6930SMark Murray shutdown(fd, 2);
1062aef6930SMark Murray return;
1072aef6930SMark Murray }
1082aef6930SMark Murray if (opt == IPOPT_EOL)
1092aef6930SMark Murray break;
1102aef6930SMark Murray if (opt == IPOPT_NOP) {
1112aef6930SMark Murray optlen = 1;
1122aef6930SMark Murray } else {
1132aef6930SMark Murray optlen = cp[IPOPT_OLEN];
1142aef6930SMark Murray if (optlen <= 0) /* Do not loop! */
1152aef6930SMark Murray break;
1162aef6930SMark Murray }
1172aef6930SMark Murray }
1182aef6930SMark Murray lp = lbuf;
1192aef6930SMark Murray for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
1202aef6930SMark Murray sprintf(lp, " %2.2x", *cp);
1212aef6930SMark Murray syslog(LOG_NOTICE,
1222aef6930SMark Murray "connect from %s with IP options (ignored):%s",
1232aef6930SMark Murray eval_client(request), lbuf);
1242aef6930SMark Murray if (setsockopt(fd, ipproto, IP_OPTIONS, (char *) 0, optsize) != 0) {
1252aef6930SMark Murray syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
1262aef6930SMark Murray shutdown(fd, 2);
1272aef6930SMark Murray }
1282aef6930SMark Murray }
1292aef6930SMark Murray #endif
1302aef6930SMark Murray }
131