ip_proxy.c revision 53642
153642Sguido/* 253642Sguido * Copyright (C) 1997-1998 by Darren Reed. 353642Sguido * 453642Sguido * Redistribution and use in source and binary forms are permitted 553642Sguido * provided that this notice is preserved and due credit is given 653642Sguido * to the original author and the contributors. 753642Sguido */ 853642Sguido#if !defined(lint) 953642Sguido/*static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.2.2.1 1999/09/19 12:18:19 darrenr Exp $";*/ 1053642Sguidostatic const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/ip_proxy.c 53642 1999-11-23 21:44:59Z guido $"; 1153642Sguido#endif 1253642Sguido 1353642Sguido#if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL) 1453642Sguido# define _KERNEL 1553642Sguido#endif 1653642Sguido 1753642Sguido#include <sys/errno.h> 1853642Sguido#include <sys/types.h> 1953642Sguido#include <sys/param.h> 2053642Sguido#include <sys/time.h> 2153642Sguido#include <sys/file.h> 2253642Sguido#if !defined(__FreeBSD_version) 2353642Sguido# include <sys/ioctl.h> 2453642Sguido#endif 2553642Sguido#include <sys/fcntl.h> 2653642Sguido#include <sys/uio.h> 2753642Sguido#if !defined(_KERNEL) && !defined(KERNEL) 2853642Sguido# include <stdio.h> 2953642Sguido# include <string.h> 3053642Sguido# include <stdlib.h> 3153642Sguido#endif 3253642Sguido#ifndef linux 3353642Sguido# include <sys/protosw.h> 3453642Sguido#endif 3553642Sguido#include <sys/socket.h> 3653642Sguido#if defined(_KERNEL) 3753642Sguido# if !defined(linux) 3853642Sguido# include <sys/systm.h> 3953642Sguido# else 4053642Sguido# include <linux/string.h> 4153642Sguido# endif 4253642Sguido#endif 4353642Sguido#if !defined(__SVR4) && !defined(__svr4__) 4453642Sguido# ifndef linux 4553642Sguido# include <sys/mbuf.h> 4653642Sguido# endif 4753642Sguido#else 4853642Sguido# include <sys/byteorder.h> 4953642Sguido# ifdef _KERNEL 5053642Sguido# include <sys/dditypes.h> 5153642Sguido# endif 5253642Sguido# include <sys/stream.h> 5353642Sguido# include <sys/kmem.h> 5453642Sguido#endif 5553642Sguido#if __FreeBSD__ > 2 5653642Sguido# include <sys/queue.h> 5753642Sguido#endif 5853642Sguido#include <net/if.h> 5953642Sguido#ifdef sun 6053642Sguido# include <net/af.h> 6153642Sguido#endif 6253642Sguido#include <net/route.h> 6353642Sguido#include <netinet/in.h> 6453642Sguido#include <netinet/in_systm.h> 6553642Sguido#include <netinet/ip.h> 6653642Sguido#ifndef linux 6753642Sguido# include <netinet/ip_var.h> 6853642Sguido#endif 6953642Sguido#include <netinet/tcp.h> 7053642Sguido#include <netinet/udp.h> 7153642Sguido#include <netinet/ip_icmp.h> 7253642Sguido#include "netinet/ip_compat.h" 7353642Sguido#include <netinet/tcpip.h> 7453642Sguido#include "netinet/ip_fil.h" 7553642Sguido#include "netinet/ip_proxy.h" 7653642Sguido#include "netinet/ip_nat.h" 7753642Sguido#include "netinet/ip_state.h" 7853642Sguido#if (__FreeBSD_version >= 300000) 7953642Sguido# include <sys/malloc.h> 8053642Sguido#endif 8153642Sguido 8253642Sguido 8353642Sguido#ifndef MIN 8453642Sguido#define MIN(a,b) (((a)<(b))?(a):(b)) 8553642Sguido#endif 8653642Sguido 8753642Sguidostatic ap_session_t *appr_new_session __P((aproxy_t *, ip_t *, 8853642Sguido fr_info_t *, nat_t *)); 8953642Sguidostatic int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int )); 9053642Sguido 9153642Sguido 9253642Sguido#define AP_SESS_SIZE 53 9353642Sguido 9453642Sguido#if defined(_KERNEL) && !defined(linux) 9553642Sguido#include "netinet/ip_ftp_pxy.c" 9653642Sguido#include "netinet/ip_rcmd_pxy.c" 9753642Sguido#include "netinet/ip_raudio_pxy.c" 9853642Sguido#endif 9953642Sguido 10053642Sguidoap_session_t *ap_sess_tab[AP_SESS_SIZE]; 10153642Sguidoap_session_t *ap_sess_list = NULL; 10253642Sguidoaproxy_t ap_proxies[] = { 10353642Sguido#ifdef IPF_FTP_PROXY 10453642Sguido { "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL, 10553642Sguido ippr_ftp_in, ippr_ftp_out }, 10653642Sguido#endif 10753642Sguido#ifdef IPF_RCMD_PROXY 10853642Sguido { "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, ippr_rcmd_new, 10953642Sguido NULL, ippr_rcmd_out }, 11053642Sguido#endif 11153642Sguido#ifdef IPF_RAUDIO_PROXY 11253642Sguido { "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, 11353642Sguido ippr_raudio_new, ippr_raudio_in, ippr_raudio_out }, 11453642Sguido#endif 11553642Sguido { "", '\0', 0, 0, NULL, NULL } 11653642Sguido}; 11753642Sguido 11853642Sguido 11953642Sguidoint appr_ok(ip, tcp, nat) 12053642Sguidoip_t *ip; 12153642Sguidotcphdr_t *tcp; 12253642Sguidoipnat_t *nat; 12353642Sguido{ 12453642Sguido aproxy_t *apr = nat->in_apr; 12553642Sguido u_short dport = nat->in_dport; 12653642Sguido 12753642Sguido if (!apr || (apr->apr_flags & APR_DELETE) || 12853642Sguido (ip->ip_p != apr->apr_p)) 12953642Sguido return 0; 13053642Sguido if ((tcp && (tcp->th_dport != dport)) || (!tcp && dport)) 13153642Sguido return 0; 13253642Sguido return 1; 13353642Sguido} 13453642Sguido 13553642Sguido 13653642Sguido/* 13753642Sguido * Allocate a new application proxy structure and fill it in with the 13853642Sguido * relevant details. call the init function once complete, prior to 13953642Sguido * returning. 14053642Sguido */ 14153642Sguidostatic ap_session_t *appr_new_session(apr, ip, fin, nat) 14253642Sguidoaproxy_t *apr; 14353642Sguidoip_t *ip; 14453642Sguidofr_info_t *fin; 14553642Sguidonat_t *nat; 14653642Sguido{ 14753642Sguido register ap_session_t *aps; 14853642Sguido 14953642Sguido if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p)) 15053642Sguido return NULL; 15153642Sguido 15253642Sguido KMALLOC(aps, ap_session_t *); 15353642Sguido if (!aps) 15453642Sguido return NULL; 15553642Sguido bzero((char *)aps, sizeof(*aps)); 15653642Sguido aps->aps_next = ap_sess_list; 15753642Sguido aps->aps_p = ip->ip_p; 15853642Sguido aps->aps_data = NULL; 15953642Sguido aps->aps_apr = apr; 16053642Sguido aps->aps_psiz = 0; 16153642Sguido ap_sess_list = aps; 16253642Sguido aps->aps_nat = nat; 16353642Sguido nat->nat_aps = aps; 16453642Sguido if (apr->apr_new != NULL) 16553642Sguido (void) (*apr->apr_new)(fin, ip, aps, nat); 16653642Sguido return aps; 16753642Sguido} 16853642Sguido 16953642Sguido 17053642Sguido/* 17153642Sguido * check to see if a packet should be passed through an active proxy routine 17253642Sguido * if one has been setup for it. 17353642Sguido */ 17453642Sguidoint appr_check(ip, fin, nat) 17553642Sguidoip_t *ip; 17653642Sguidofr_info_t *fin; 17753642Sguidonat_t *nat; 17853642Sguido{ 17953642Sguido ap_session_t *aps; 18053642Sguido aproxy_t *apr; 18153642Sguido tcphdr_t *tcp = NULL; 18253642Sguido u_32_t sum; 18353642Sguido int err; 18453642Sguido 18553642Sguido if (nat->nat_aps == NULL) 18653642Sguido nat->nat_aps = appr_new_session(nat->nat_ptr->in_apr, ip, 18753642Sguido fin, nat); 18853642Sguido aps = nat->nat_aps; 18953642Sguido if ((aps != NULL) && (aps->aps_p == ip->ip_p)) { 19053642Sguido if (ip->ip_p == IPPROTO_TCP) { 19153642Sguido tcp = (tcphdr_t *)fin->fin_dp; 19253642Sguido /* 19353642Sguido * verify that the checksum is correct. If not, then 19453642Sguido * don't do anything with this packet. 19553642Sguido */ 19653642Sguido#if SOLARIS && defined(_KERNEL) 19753642Sguido sum = fr_tcpsum(fin->fin_qfm, ip, tcp); 19853642Sguido#else 19953642Sguido sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp); 20053642Sguido#endif 20153642Sguido if (sum != tcp->th_sum) { 20253642Sguido frstats[fin->fin_out].fr_tcpbad++; 20353642Sguido return -1; 20453642Sguido } 20553642Sguido } 20653642Sguido 20753642Sguido apr = aps->aps_apr; 20853642Sguido err = 0; 20953642Sguido if (fin->fin_out != 0) { 21053642Sguido if (apr->apr_outpkt != NULL) 21153642Sguido err = (*apr->apr_outpkt)(fin, ip, aps, nat); 21253642Sguido } else { 21353642Sguido if (apr->apr_inpkt != NULL) 21453642Sguido err = (*apr->apr_inpkt)(fin, ip, aps, nat); 21553642Sguido } 21653642Sguido 21753642Sguido if (tcp != NULL) { 21853642Sguido err = appr_fixseqack(fin, ip, aps, err); 21953642Sguido#if SOLARIS && defined(_KERNEL) 22053642Sguido tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp); 22153642Sguido#else 22253642Sguido tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp); 22353642Sguido#endif 22453642Sguido } 22553642Sguido aps->aps_bytes += ip->ip_len; 22653642Sguido aps->aps_pkts++; 22753642Sguido return 2; 22853642Sguido } 22953642Sguido return -1; 23053642Sguido} 23153642Sguido 23253642Sguido 23353642Sguidoaproxy_t *appr_match(pr, name) 23453642Sguidou_int pr; 23553642Sguidochar *name; 23653642Sguido{ 23753642Sguido aproxy_t *ap; 23853642Sguido 23953642Sguido for (ap = ap_proxies; ap->apr_p; ap++) 24053642Sguido if ((ap->apr_p == pr) && 24153642Sguido !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) { 24253642Sguido ap->apr_ref++; 24353642Sguido return ap; 24453642Sguido } 24553642Sguido return NULL; 24653642Sguido} 24753642Sguido 24853642Sguido 24953642Sguidovoid appr_free(ap) 25053642Sguidoaproxy_t *ap; 25153642Sguido{ 25253642Sguido ap->apr_ref--; 25353642Sguido} 25453642Sguido 25553642Sguido 25653642Sguidovoid aps_free(aps) 25753642Sguidoap_session_t *aps; 25853642Sguido{ 25953642Sguido ap_session_t *a, **ap; 26053642Sguido 26153642Sguido if (!aps) 26253642Sguido return; 26353642Sguido 26453642Sguido for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next) 26553642Sguido if (a == aps) { 26653642Sguido *ap = a->aps_next; 26753642Sguido break; 26853642Sguido } 26953642Sguido 27053642Sguido if (a) { 27153642Sguido if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) 27253642Sguido KFREES(aps->aps_data, aps->aps_psiz); 27353642Sguido KFREE(aps); 27453642Sguido } 27553642Sguido} 27653642Sguido 27753642Sguido 27853642Sguidostatic int appr_fixseqack(fin, ip, aps, inc) 27953642Sguidofr_info_t *fin; 28053642Sguidoip_t *ip; 28153642Sguidoap_session_t *aps; 28253642Sguidoint inc; 28353642Sguido{ 28453642Sguido int sel, ch = 0, out, nlen; 28553642Sguido u_32_t seq1, seq2; 28653642Sguido tcphdr_t *tcp; 28753642Sguido 28853642Sguido tcp = (tcphdr_t *)fin->fin_dp; 28953642Sguido out = fin->fin_out; 29053642Sguido nlen = ip->ip_len; 29153642Sguido nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2); 29253642Sguido 29353642Sguido if (out != 0) { 29453642Sguido seq1 = (u_32_t)ntohl(tcp->th_seq); 29553642Sguido sel = aps->aps_sel[out]; 29653642Sguido 29753642Sguido /* switch to other set ? */ 29853642Sguido if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) && 29953642Sguido (seq1 > aps->aps_seqmin[!sel])) 30053642Sguido sel = aps->aps_sel[out] = !sel; 30153642Sguido 30253642Sguido if (aps->aps_seqoff[sel]) { 30353642Sguido seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel]; 30453642Sguido if (seq1 > seq2) { 30553642Sguido seq2 = aps->aps_seqoff[sel]; 30653642Sguido seq1 += seq2; 30753642Sguido tcp->th_seq = htonl(seq1); 30853642Sguido ch = 1; 30953642Sguido } 31053642Sguido } 31153642Sguido 31253642Sguido if (inc && (seq1 > aps->aps_seqmin[!sel])) { 31353642Sguido aps->aps_seqmin[!sel] = seq1 + nlen - 1; 31453642Sguido aps->aps_seqoff[!sel] = aps->aps_seqoff[sel] + inc; 31553642Sguido } 31653642Sguido 31753642Sguido /***/ 31853642Sguido 31953642Sguido seq1 = ntohl(tcp->th_ack); 32053642Sguido sel = aps->aps_sel[1 - out]; 32153642Sguido 32253642Sguido /* switch to other set ? */ 32353642Sguido if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) && 32453642Sguido (seq1 > aps->aps_ackmin[!sel])) 32553642Sguido sel = aps->aps_sel[1 - out] = !sel; 32653642Sguido 32753642Sguido if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) { 32853642Sguido seq2 = aps->aps_ackoff[sel]; 32953642Sguido tcp->th_ack = htonl(seq1 - seq2); 33053642Sguido ch = 1; 33153642Sguido } 33253642Sguido } else { 33353642Sguido seq1 = ntohl(tcp->th_seq); 33453642Sguido sel = aps->aps_sel[out]; 33553642Sguido 33653642Sguido /* switch to other set ? */ 33753642Sguido if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) && 33853642Sguido (seq1 > aps->aps_ackmin[!sel])) 33953642Sguido sel = aps->aps_sel[out] = !sel; 34053642Sguido 34153642Sguido if (aps->aps_ackoff[sel]) { 34253642Sguido seq2 = aps->aps_ackmin[sel] - 34353642Sguido aps->aps_ackoff[sel]; 34453642Sguido if (seq1 > seq2) { 34553642Sguido seq2 = aps->aps_ackoff[sel]; 34653642Sguido seq1 += seq2; 34753642Sguido tcp->th_seq = htonl(seq1); 34853642Sguido ch = 1; 34953642Sguido } 35053642Sguido } 35153642Sguido 35253642Sguido if (inc && (seq1 > aps->aps_ackmin[!sel])) { 35353642Sguido aps->aps_ackmin[!sel] = seq1 + nlen - 1; 35453642Sguido aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc; 35553642Sguido } 35653642Sguido 35753642Sguido /***/ 35853642Sguido 35953642Sguido seq1 = ntohl(tcp->th_ack); 36053642Sguido sel = aps->aps_sel[1 - out]; 36153642Sguido 36253642Sguido /* switch to other set ? */ 36353642Sguido if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) && 36453642Sguido (seq1 > aps->aps_seqmin[!sel])) 36553642Sguido sel = aps->aps_sel[1 - out] = !sel; 36653642Sguido 36753642Sguido if (aps->aps_seqoff[sel] && (seq1 > aps->aps_seqmin[sel])) { 36853642Sguido seq2 = aps->aps_seqoff[sel]; 36953642Sguido tcp->th_ack = htonl(seq1 - seq2); 37053642Sguido ch = 1; 37153642Sguido } 37253642Sguido } 37353642Sguido return ch ? 2 : 0; 37453642Sguido} 37553642Sguido 37653642Sguido 37753642Sguidoint appr_init() 37853642Sguido{ 37953642Sguido aproxy_t *ap; 38053642Sguido int err = 0; 38153642Sguido 38253642Sguido for (ap = ap_proxies; ap->apr_p; ap++) { 38353642Sguido err = (*ap->apr_init)(); 38453642Sguido if (err != 0) 38553642Sguido break; 38653642Sguido } 38753642Sguido return err; 38853642Sguido} 389