Home | History | Annotate | Line # | Download | only in netinet
ip_tftp_pxy.c revision 1.2
      1 /*	$NetBSD: ip_tftp_pxy.c,v 1.2 2012/03/23 20:39:50 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2010 by Darren Reed.
      5  *
      6  * See the IPFILTER.LICENCE file for details on licencing.
      7  *
      8  * Id: ip_tftp_pxy.c,v 2.1.2.2 2012/01/26 05:29:13 darrenr Exp
      9  */
     10 
     11 #define IPF_TFTP_PROXY
     12 
     13 void ipf_p_tftp_main_load(void);
     14 void ipf_p_tftp_main_unload(void);
     15 int ipf_p_tftp_new(void *, fr_info_t *, ap_session_t *, nat_t *);
     16 int ipf_p_tftp_out(void *, fr_info_t *, ap_session_t *, nat_t *);
     17 int ipf_p_tftp_in(void *, fr_info_t *, ap_session_t *, nat_t *);
     18 int ipf_p_tftp_client(fr_info_t *, ap_session_t *, nat_t *);
     19 int ipf_p_tftp_server(fr_info_t *, ap_session_t *, nat_t *);
     20 int ipf_p_tftp_backchannel(fr_info_t *, ap_session_t *, nat_t *);
     21 
     22 static	frentry_t	tftpfr;
     23 
     24 int	tftp_proxy_init = 0;
     25 
     26 typedef struct tftpinfo {
     27 	nat_t	*ti_datanat;
     28 	ipstate_t	*ti_datastate;
     29 	int	ti_lastcmd;
     30 	int	ti_nextblk;
     31 	int	ti_lastblk;
     32 	int	ti_lasterror;
     33 	char	ti_filename[80];
     34 } tftpinfo_t;
     35 
     36 #define	TFTP_CMD_READ	1
     37 #define	TFTP_CMD_WRITE	2
     38 #define	TFTP_CMD_DATA	3
     39 #define	TFTP_CMD_ACK	4
     40 #define	TFTP_CMD_ERROR	5
     41 
     42 
     43 /*
     44  * TFTP application proxy initialization.
     45  */
     46 void
     47 ipf_p_tftp_main_load(void)
     48 {
     49 
     50 	bzero((char *)&tftpfr, sizeof(tftpfr));
     51 	tftpfr.fr_ref = 1;
     52 	tftpfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
     53 	MUTEX_INIT(&tftpfr.fr_lock, "TFTP proxy rule lock");
     54 	tftp_proxy_init = 1;
     55 }
     56 
     57 
     58 void
     59 ipf_p_tftp_main_unload(void)
     60 {
     61 
     62 	if (tftp_proxy_init == 1) {
     63 		MUTEX_DESTROY(&tftpfr.fr_lock);
     64 		tftp_proxy_init = 0;
     65 	}
     66 }
     67 
     68 
     69 int
     70 ipf_p_tftp_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
     71 {
     72 
     73 	if (nat->nat_dir == NAT_OUTBOUND)
     74 		return ipf_p_tftp_client(fin, aps, nat);
     75 	return ipf_p_tftp_server(fin, aps, nat);
     76 }
     77 
     78 
     79 int
     80 ipf_p_tftp_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
     81 {
     82 
     83 	if (nat->nat_dir == NAT_INBOUND)
     84 		return ipf_p_tftp_client(fin, aps, nat);
     85 	return ipf_p_tftp_server(fin, aps, nat);
     86 }
     87 
     88 
     89 int
     90 ipf_p_tftp_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
     91 {
     92 	udphdr_t *udp;
     93 	tftpinfo_t *ti;
     94 
     95 	KMALLOC(ti, tftpinfo_t *);
     96 	if (ti == NULL)
     97 		return -1;
     98 
     99 	aps->aps_data = ti;
    100 	aps->aps_psiz = sizeof(*ti);
    101 	ti->ti_lastcmd = 0;
    102 
    103 	nat = nat;	/* LINT */
    104 	fin = fin;	/* LINT */
    105 
    106 	udp = (udphdr_t *)fin->fin_dp;
    107 	aps->aps_sport = udp->uh_sport;
    108 	aps->aps_dport = udp->uh_dport;
    109 	return 0;
    110 }
    111 
    112 
    113 /*
    114  * Setup for a new TFTP proxy.
    115  */
    116 int
    117 ipf_p_tftp_backchannel(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    118 {
    119 	ipf_main_softc_t *softc = fin->fin_main_soft;
    120 #ifdef USE_MUTEXES
    121 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
    122 #endif
    123 	struct in_addr swip,swip2;
    124 	tftpinfo_t *ti;
    125 	udphdr_t *udp;
    126 	fr_info_t fi;
    127 	nat_t *nat2;
    128 
    129 	ti = aps->aps_data;
    130 	udp = (udphdr_t *)fin->fin_dp;
    131 	/*
    132 	 * Add skeleton NAT entry for connection which will come back the
    133 	 * other way.
    134 	 */
    135 	bcopy((char *)fin, (char *)&fi, sizeof(fi));
    136 	fi.fin_flx |= FI_IGNORE;
    137 	fi.fin_data[1] = 0;
    138 	if (nat->nat_dir == NAT_OUTBOUND)
    139 		nat2 = ipf_nat_outlookup(&fi, NAT_SEARCH|IPN_UDP,
    140 					 nat->nat_pr[0], nat->nat_osrcip,
    141 					 nat->nat_odstip);
    142 	else
    143 		nat2 = ipf_nat_inlookup(&fi, NAT_SEARCH|IPN_UDP,
    144 					nat->nat_pr[0], nat->nat_nsrcip,
    145 					nat->nat_odstip);
    146 	if (nat2 == NULL) {
    147 		u_short slen;
    148 		int nflags;
    149 		ip_t *ip;
    150 
    151 		ip = fin->fin_ip;
    152 		slen = ip->ip_len;
    153 		ip->ip_len = htons(fin->fin_hlen + sizeof(*udp));
    154 		bzero((char *)udp, sizeof(*udp));
    155 		udp->uh_sport = htons(fi.fin_data[0]);
    156 		udp->uh_dport = 0; /* XXX - don't specify remote port */
    157 		udp->uh_ulen = 0;
    158 		udp->uh_sum = 0;
    159 		fi.fin_dp = (char *)udp;
    160 		fi.fin_fr = &tftpfr;
    161 		fi.fin_dlen = sizeof(*udp);
    162 		fi.fin_plen = fi.fin_hlen + sizeof(*udp);
    163 		fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
    164 		nflags = NAT_SLAVE|IPN_UDP|SI_W_DPORT;
    165 
    166 		swip = ip->ip_src;
    167 		swip2 = ip->ip_dst;
    168 		fi.fin_fi.fi_saddr = nat->nat_osrcaddr;
    169 		ip->ip_src = nat->nat_osrcip;
    170 		fi.fin_fi.fi_daddr = nat->nat_odstaddr;
    171 		ip->ip_dst = nat->nat_odstip;
    172 
    173 		if (nat->nat_dir == NAT_INBOUND)
    174 			nflags |= NAT_NOTRULEPORT;
    175 
    176 		MUTEX_ENTER(&softn->ipf_nat_new);
    177 		nat2 = ipf_nat_add(&fi, nat->nat_ptr, &ti->ti_datanat,
    178 				   nflags, nat->nat_dir);
    179 		MUTEX_EXIT(&softn->ipf_nat_new);
    180 		if (nat2 != NULL) {
    181 			(void) ipf_nat_proto(&fi, nat2, IPN_UDP);
    182 			ipf_nat_update(&fi, nat2);
    183 			fi.fin_ifp = NULL;
    184 			if (ipf_state_add(softc, &fi, &ti->ti_datastate,
    185 					  SI_W_DPORT) != 0) {
    186 				ipf_nat_setpending(softc, nat2);
    187 			}
    188 		}
    189 		ip->ip_len = slen;
    190 		ip->ip_src = swip;
    191 		ip->ip_dst = swip2;
    192 		return 0;
    193 	}
    194 	return -1;
    195 }
    196 
    197 
    198 int
    199 ipf_p_tftp_client(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    200 {
    201 	u_char *msg, *s, *t;
    202 	tftpinfo_t *ti;
    203 	u_short opcode;
    204 	udphdr_t *udp;
    205 	int len;
    206 
    207 	if (fin->fin_dlen < 4)
    208 		return 0;
    209 
    210 	ti = aps->aps_data;
    211 	msg = fin->fin_dp;
    212 	msg += sizeof(udphdr_t);
    213 	opcode = (msg[0] << 8) | msg[1];
    214 
    215 	switch (opcode)
    216 	{
    217 	case TFTP_CMD_READ :
    218 	case TFTP_CMD_WRITE :
    219 		if (fin->fin_out != 0)
    220 			return -1;
    221 		len = fin->fin_dlen - sizeof(*udp) - 2;
    222 		if (len > sizeof(ti->ti_filename) - 1)
    223 			len = sizeof(ti->ti_filename) - 1;
    224 		s = msg + 2;
    225 		for (t = (u_char *)ti->ti_filename; (len > 0); len--, s++) {
    226 			*t++ = *s;
    227 			if (*s == '\0')
    228 				break;
    229 		}
    230 		break;
    231 	default :
    232 		return -1;
    233 	}
    234 
    235 	ti = aps->aps_data;
    236 	ti->ti_lastcmd = opcode;
    237 	return 0;
    238 }
    239 
    240 
    241 int
    242 ipf_p_tftp_server(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
    243 {
    244 	tftpinfo_t *ti;
    245 	u_short opcode;
    246 	u_short arg;
    247 	u_char *msg;
    248 
    249 	if (fin->fin_dlen < 4)
    250 		return 0;
    251 
    252 	ti = aps->aps_data;
    253 	msg = fin->fin_dp;
    254 	msg += sizeof(udphdr_t);
    255 	arg = (msg[2] << 8) | msg[3];
    256 	opcode = (msg[0] << 8) | msg[1];
    257 
    258 	switch (opcode)
    259 	{
    260 	case TFTP_CMD_ACK :
    261 		/* This proxy should not see any ACKS for DATA blocks */
    262 		if (fin->fin_out != 1)
    263 			return -1;
    264 		if ((arg == 0) &&
    265 		    (ti->ti_lastcmd == TFTP_CMD_READ ||
    266 		     ti->ti_lastcmd == TFTP_CMD_WRITE))
    267 			ipf_p_tftp_backchannel(fin, aps, nat);
    268 		ti->ti_lastblk = arg;
    269 		break;
    270 	case TFTP_CMD_ERROR :
    271 		if (fin->fin_out != 1)
    272 			return -1;
    273 		ti->ti_lasterror = arg;
    274 		break;
    275 	default :
    276 		return -1;
    277 	}
    278 
    279 	ti->ti_lastcmd = opcode;
    280 	return 0;
    281 }
    282