13b9b51feSCy Schubert /*
23b9b51feSCy Schubert  * Copyright (C) 2012 by Darren Reed.
33b9b51feSCy Schubert  *
43b9b51feSCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
53b9b51feSCy Schubert  *
63b9b51feSCy Schubert  * $Id: ip_tftp_pxy.c,v 1.1.2.9 2012/07/22 08:04:23 darren_r Exp $
73b9b51feSCy Schubert  */
83b9b51feSCy Schubert 
93b9b51feSCy Schubert #define IPF_TFTP_PROXY
103b9b51feSCy Schubert 
113b9b51feSCy Schubert typedef struct ipf_tftp_softc_s {
123b9b51feSCy Schubert 	int     	ipf_p_tftp_readonly;
133b9b51feSCy Schubert 	ipftuneable_t	*ipf_p_tftp_tune;
143b9b51feSCy Schubert } ipf_tftp_softc_t;
153b9b51feSCy Schubert 
163b9b51feSCy Schubert int ipf_p_tftp_backchannel(fr_info_t *, ap_session_t *, nat_t *);
173b9b51feSCy Schubert int ipf_p_tftp_client(ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
183b9b51feSCy Schubert 			   nat_t *);
193b9b51feSCy Schubert int ipf_p_tftp_in(void *, fr_info_t *, ap_session_t *, nat_t *);
203b9b51feSCy Schubert void ipf_p_tftp_main_load(void);
213b9b51feSCy Schubert void ipf_p_tftp_main_unload(void);
223b9b51feSCy Schubert int ipf_p_tftp_new(void *, fr_info_t *, ap_session_t *, nat_t *);
233b9b51feSCy Schubert void ipf_p_tftp_del(ipf_main_softc_t *, ap_session_t *);
243b9b51feSCy Schubert int ipf_p_tftp_out(void *, fr_info_t *, ap_session_t *, nat_t *);
253b9b51feSCy Schubert int ipf_p_tftp_server(ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
263b9b51feSCy Schubert 			   nat_t *);
273b9b51feSCy Schubert void *ipf_p_tftp_soft_create(ipf_main_softc_t *);
283b9b51feSCy Schubert void ipf_p_tftp_soft_destroy(ipf_main_softc_t *, void *);
293b9b51feSCy Schubert 
303b9b51feSCy Schubert static	frentry_t	tftpfr;
313b9b51feSCy Schubert static	int		tftp_proxy_init = 0;
323b9b51feSCy Schubert 
333b9b51feSCy Schubert typedef enum tftp_cmd_e {
343b9b51feSCy Schubert 	TFTP_CMD_READ = 1,
353b9b51feSCy Schubert 	TFTP_CMD_WRITE = 2,
363b9b51feSCy Schubert 	TFTP_CMD_DATA = 3,
373b9b51feSCy Schubert 	TFTP_CMD_ACK = 4,
383b9b51feSCy Schubert 	TFTP_CMD_ERROR = 5
393b9b51feSCy Schubert } tftp_cmd_t;
403b9b51feSCy Schubert 
413b9b51feSCy Schubert typedef struct tftpinfo {
423b9b51feSCy Schubert 	tftp_cmd_t	ti_lastcmd;
433b9b51feSCy Schubert 	int		ti_nextblk;
443b9b51feSCy Schubert 	int		ti_lastblk;
453b9b51feSCy Schubert 	int		ti_lasterror;
463b9b51feSCy Schubert 	char		ti_filename[80];
473b9b51feSCy Schubert 	ipnat_t		*ti_rule;
483b9b51feSCy Schubert } tftpinfo_t;
493b9b51feSCy Schubert 
503b9b51feSCy Schubert static  ipftuneable_t   ipf_tftp_tuneables[] = {
513b9b51feSCy Schubert 	{ { (void *)offsetof(ipf_tftp_softc_t, ipf_p_tftp_readonly) },
523b9b51feSCy Schubert 		"tftp_read_only",	0,	1,
533b9b51feSCy Schubert 		stsizeof(ipf_tftp_softc_t, ipf_p_tftp_readonly),
543b9b51feSCy Schubert 		0, NULL, NULL },
553b9b51feSCy Schubert 	{ { NULL }, NULL, 0, 0, 0, 0, NULL, NULL }
563b9b51feSCy Schubert };
573b9b51feSCy Schubert 
583b9b51feSCy Schubert 
593b9b51feSCy Schubert /*
603b9b51feSCy Schubert  * TFTP application proxy initialization.
613b9b51feSCy Schubert  */
623b9b51feSCy Schubert void
ipf_p_tftp_main_load(void)63064a5a95SCy Schubert ipf_p_tftp_main_load(void)
643b9b51feSCy Schubert {
653b9b51feSCy Schubert 
663b9b51feSCy Schubert 	bzero((char *)&tftpfr, sizeof(tftpfr));
673b9b51feSCy Schubert 	tftpfr.fr_ref = 1;
683b9b51feSCy Schubert 	tftpfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
693b9b51feSCy Schubert 	MUTEX_INIT(&tftpfr.fr_lock, "TFTP proxy rule lock");
703b9b51feSCy Schubert 	tftp_proxy_init = 1;
713b9b51feSCy Schubert }
723b9b51feSCy Schubert 
733b9b51feSCy Schubert 
743b9b51feSCy Schubert void
ipf_p_tftp_main_unload(void)75064a5a95SCy Schubert ipf_p_tftp_main_unload(void)
763b9b51feSCy Schubert {
773b9b51feSCy Schubert 
783b9b51feSCy Schubert 	if (tftp_proxy_init == 1) {
793b9b51feSCy Schubert 		MUTEX_DESTROY(&tftpfr.fr_lock);
803b9b51feSCy Schubert 		tftp_proxy_init = 0;
813b9b51feSCy Schubert 	}
823b9b51feSCy Schubert }
833b9b51feSCy Schubert 
843b9b51feSCy Schubert 
853b9b51feSCy Schubert void *
ipf_p_tftp_soft_create(ipf_main_softc_t * softc)86064a5a95SCy Schubert ipf_p_tftp_soft_create(ipf_main_softc_t *softc)
873b9b51feSCy Schubert {
883b9b51feSCy Schubert 	ipf_tftp_softc_t *softt;
893b9b51feSCy Schubert 
903b9b51feSCy Schubert 	KMALLOC(softt, ipf_tftp_softc_t *);
913b9b51feSCy Schubert 	if (softt == NULL)
928c82b374SCy Schubert 		return (NULL);
933b9b51feSCy Schubert 
943b9b51feSCy Schubert 	bzero((char *)softt, sizeof(*softt));
953b9b51feSCy Schubert 
963b9b51feSCy Schubert 	softt->ipf_p_tftp_tune = ipf_tune_array_copy(softt,
973b9b51feSCy Schubert 						     sizeof(ipf_tftp_tuneables),
983b9b51feSCy Schubert 						     ipf_tftp_tuneables);
993b9b51feSCy Schubert 	if (softt->ipf_p_tftp_tune == NULL) {
1003b9b51feSCy Schubert 		ipf_p_tftp_soft_destroy(softc, softt);
1018c82b374SCy Schubert 		return (NULL);
1023b9b51feSCy Schubert 	}
1033b9b51feSCy Schubert 	if (ipf_tune_array_link(softc, softt->ipf_p_tftp_tune) == -1) {
1043b9b51feSCy Schubert 		ipf_p_tftp_soft_destroy(softc, softt);
1058c82b374SCy Schubert 		return (NULL);
1063b9b51feSCy Schubert 	}
1073b9b51feSCy Schubert 
1083b9b51feSCy Schubert 	softt->ipf_p_tftp_readonly = 1;
1093b9b51feSCy Schubert 
1108c82b374SCy Schubert 	return (softt);
1113b9b51feSCy Schubert }
1123b9b51feSCy Schubert 
1133b9b51feSCy Schubert 
1143b9b51feSCy Schubert void
ipf_p_tftp_soft_destroy(ipf_main_softc_t * softc,void * arg)115064a5a95SCy Schubert ipf_p_tftp_soft_destroy(ipf_main_softc_t *softc, void *arg)
1163b9b51feSCy Schubert {
1173b9b51feSCy Schubert 	ipf_tftp_softc_t *softt = arg;
1183b9b51feSCy Schubert 
1193b9b51feSCy Schubert 	if (softt->ipf_p_tftp_tune != NULL) {
1203b9b51feSCy Schubert 		ipf_tune_array_unlink(softc, softt->ipf_p_tftp_tune);
1213b9b51feSCy Schubert 		KFREES(softt->ipf_p_tftp_tune, sizeof(ipf_tftp_tuneables));
1223b9b51feSCy Schubert 		softt->ipf_p_tftp_tune = NULL;
1233b9b51feSCy Schubert 	}
1243b9b51feSCy Schubert 
1253b9b51feSCy Schubert 	KFREE(softt);
1263b9b51feSCy Schubert }
1273b9b51feSCy Schubert 
1283b9b51feSCy Schubert 
1293b9b51feSCy Schubert int
ipf_p_tftp_out(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)130064a5a95SCy Schubert ipf_p_tftp_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
1313b9b51feSCy Schubert {
1323b9b51feSCy Schubert 	ipf_tftp_softc_t *softt = arg;
1333b9b51feSCy Schubert 
1343b9b51feSCy Schubert 	fin->fin_flx |= FI_NOWILD;
1353b9b51feSCy Schubert 	if (nat->nat_dir == NAT_OUTBOUND)
1368c82b374SCy Schubert 		return (ipf_p_tftp_client(softt, fin, aps, nat));
1378c82b374SCy Schubert 	return (ipf_p_tftp_server(softt, fin, aps, nat));
1383b9b51feSCy Schubert }
1393b9b51feSCy Schubert 
1403b9b51feSCy Schubert 
1413b9b51feSCy Schubert int
ipf_p_tftp_in(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)142064a5a95SCy Schubert ipf_p_tftp_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
1433b9b51feSCy Schubert {
1443b9b51feSCy Schubert 	ipf_tftp_softc_t *softt = arg;
1453b9b51feSCy Schubert 
1463b9b51feSCy Schubert 	fin->fin_flx |= FI_NOWILD;
1473b9b51feSCy Schubert 	if (nat->nat_dir == NAT_INBOUND)
1488c82b374SCy Schubert 		return (ipf_p_tftp_client(softt, fin, aps, nat));
1498c82b374SCy Schubert 	return (ipf_p_tftp_server(softt, fin, aps, nat));
1503b9b51feSCy Schubert }
1513b9b51feSCy Schubert 
1523b9b51feSCy Schubert 
1533b9b51feSCy Schubert int
ipf_p_tftp_new(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)154064a5a95SCy Schubert ipf_p_tftp_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
1553b9b51feSCy Schubert {
1563b9b51feSCy Schubert 	udphdr_t *udp;
1573b9b51feSCy Schubert 	tftpinfo_t *ti;
1583b9b51feSCy Schubert 	ipnat_t *ipn;
1593b9b51feSCy Schubert 	ipnat_t *np;
1603b9b51feSCy Schubert 	int size;
1613b9b51feSCy Schubert 
1623b9b51feSCy Schubert 	fin = fin;	/* LINT */
1633b9b51feSCy Schubert 
1643b9b51feSCy Schubert 	np = nat->nat_ptr;
1653b9b51feSCy Schubert 	size = np->in_size;
1663b9b51feSCy Schubert 
1673b9b51feSCy Schubert 	KMALLOC(ti, tftpinfo_t *);
1683b9b51feSCy Schubert 	if (ti == NULL)
1698c82b374SCy Schubert 		return (-1);
1703b9b51feSCy Schubert 	KMALLOCS(ipn, ipnat_t *, size);
1713b9b51feSCy Schubert 	if (ipn == NULL) {
1723b9b51feSCy Schubert 		KFREE(ti);
1738c82b374SCy Schubert 		return (-1);
1743b9b51feSCy Schubert 	}
1753b9b51feSCy Schubert 
1763b9b51feSCy Schubert 	aps->aps_data = ti;
1773b9b51feSCy Schubert 	aps->aps_psiz = sizeof(*ti);
1783b9b51feSCy Schubert 	bzero((char *)ti, sizeof(*ti));
1793b9b51feSCy Schubert 	bzero((char *)ipn, size);
1803b9b51feSCy Schubert 	ti->ti_rule = ipn;
1813b9b51feSCy Schubert 
1823b9b51feSCy Schubert 	udp = (udphdr_t *)fin->fin_dp;
1833b9b51feSCy Schubert 	aps->aps_sport = udp->uh_sport;
1843b9b51feSCy Schubert 	aps->aps_dport = udp->uh_dport;
1853b9b51feSCy Schubert 
1863b9b51feSCy Schubert 	ipn->in_size = size;
1873b9b51feSCy Schubert 	ipn->in_apr = NULL;
1883b9b51feSCy Schubert 	ipn->in_use = 1;
1893b9b51feSCy Schubert 	ipn->in_hits = 1;
1903b9b51feSCy Schubert 	ipn->in_ippip = 1;
1913b9b51feSCy Schubert 	ipn->in_pr[0] = IPPROTO_UDP;
1923b9b51feSCy Schubert 	ipn->in_pr[1] = IPPROTO_UDP;
1933b9b51feSCy Schubert 	ipn->in_ifps[0] = nat->nat_ifps[0];
1943b9b51feSCy Schubert 	ipn->in_ifps[1] = nat->nat_ifps[1];
1953b9b51feSCy Schubert 	ipn->in_v[0] = nat->nat_ptr->in_v[1];
1963b9b51feSCy Schubert 	ipn->in_v[1] = nat->nat_ptr->in_v[0];
1973b9b51feSCy Schubert 	ipn->in_flags = IPN_UDP|IPN_FIXEDDPORT|IPN_PROXYRULE;
1983b9b51feSCy Schubert 
1993b9b51feSCy Schubert 	ipn->in_nsrcip6 = nat->nat_odst6;
2003b9b51feSCy Schubert 	ipn->in_osrcip6 = nat->nat_ndst6;
2013b9b51feSCy Schubert 
2023b9b51feSCy Schubert 	if ((np->in_redir & NAT_REDIRECT) != 0) {
2033b9b51feSCy Schubert 		ipn->in_redir = NAT_MAP;
2043b9b51feSCy Schubert 		if (ipn->in_v[0] == 4) {
2053b9b51feSCy Schubert 			ipn->in_snip = ntohl(nat->nat_odstaddr);
2063b9b51feSCy Schubert 			ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
2073b9b51feSCy Schubert 		} else {
2083b9b51feSCy Schubert #ifdef USE_INET6
2093b9b51feSCy Schubert 			ipn->in_snip6 = nat->nat_odst6;
2103b9b51feSCy Schubert 			ipn->in_dnip6 = nat->nat_nsrc6;
2113b9b51feSCy Schubert #endif
2123b9b51feSCy Schubert 		}
2133b9b51feSCy Schubert 		ipn->in_ndstip6 = nat->nat_nsrc6;
2143b9b51feSCy Schubert 		ipn->in_odstip6 = nat->nat_osrc6;
2153b9b51feSCy Schubert 	} else {
2163b9b51feSCy Schubert 		ipn->in_redir = NAT_REDIRECT;
2173b9b51feSCy Schubert 		if (ipn->in_v[0] == 4) {
2183b9b51feSCy Schubert 			ipn->in_snip = ntohl(nat->nat_odstaddr);
2193b9b51feSCy Schubert 			ipn->in_dnip = ntohl(nat->nat_osrcaddr);
2203b9b51feSCy Schubert 		} else {
2213b9b51feSCy Schubert #ifdef USE_INET6
2223b9b51feSCy Schubert 			ipn->in_snip6 = nat->nat_odst6;
2233b9b51feSCy Schubert 			ipn->in_dnip6 = nat->nat_osrc6;
2243b9b51feSCy Schubert #endif
2253b9b51feSCy Schubert 		}
2263b9b51feSCy Schubert 		ipn->in_ndstip6 = nat->nat_osrc6;
2273b9b51feSCy Schubert 		ipn->in_odstip6 = nat->nat_nsrc6;
2283b9b51feSCy Schubert 	}
2293b9b51feSCy Schubert 	ipn->in_odport = htons(fin->fin_sport);
2303b9b51feSCy Schubert 	ipn->in_ndport = htons(fin->fin_sport);
2313b9b51feSCy Schubert 
2323b9b51feSCy Schubert 	IP6_SETONES(&ipn->in_osrcmsk6);
2333b9b51feSCy Schubert 	IP6_SETONES(&ipn->in_nsrcmsk6);
2343b9b51feSCy Schubert 	IP6_SETONES(&ipn->in_odstmsk6);
2353b9b51feSCy Schubert 	IP6_SETONES(&ipn->in_ndstmsk6);
2363b9b51feSCy Schubert 	MUTEX_INIT(&ipn->in_lock, "tftp proxy NAT rule");
2373b9b51feSCy Schubert 
2383b9b51feSCy Schubert 	ipn->in_namelen = np->in_namelen;
2393b9b51feSCy Schubert 	bcopy(np->in_names, ipn->in_ifnames, ipn->in_namelen);
2403b9b51feSCy Schubert 	ipn->in_ifnames[0] = np->in_ifnames[0];
2413b9b51feSCy Schubert 	ipn->in_ifnames[1] = np->in_ifnames[1];
2423b9b51feSCy Schubert 
2433b9b51feSCy Schubert 	ti->ti_lastcmd = 0;
2443b9b51feSCy Schubert 
2458c82b374SCy Schubert 	return (0);
2463b9b51feSCy Schubert }
2473b9b51feSCy Schubert 
2483b9b51feSCy Schubert 
2493b9b51feSCy Schubert void
ipf_p_tftp_del(ipf_main_softc_t * softc,ap_session_t * aps)250064a5a95SCy Schubert ipf_p_tftp_del(ipf_main_softc_t *softc, ap_session_t *aps)
2513b9b51feSCy Schubert {
2523b9b51feSCy Schubert 	tftpinfo_t *tftp;
2533b9b51feSCy Schubert 
2543b9b51feSCy Schubert 	tftp = aps->aps_data;
2553b9b51feSCy Schubert 	if (tftp != NULL) {
2563b9b51feSCy Schubert 		tftp->ti_rule->in_flags |= IPN_DELETE;
2573b9b51feSCy Schubert 		ipf_nat_rule_deref(softc, &tftp->ti_rule);
2583b9b51feSCy Schubert 	}
2593b9b51feSCy Schubert }
2603b9b51feSCy Schubert 
2613b9b51feSCy Schubert 
2623b9b51feSCy Schubert /*
2633b9b51feSCy Schubert  * Setup for a new TFTP proxy.
2643b9b51feSCy Schubert  */
2653b9b51feSCy Schubert int
ipf_p_tftp_backchannel(fr_info_t * fin,ap_session_t * aps,nat_t * nat)266064a5a95SCy Schubert ipf_p_tftp_backchannel(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
2673b9b51feSCy Schubert {
2683b9b51feSCy Schubert 	ipf_main_softc_t *softc = fin->fin_main_soft;
2693b9b51feSCy Schubert #ifdef USE_MUTEXES
2703b9b51feSCy Schubert 	ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2713b9b51feSCy Schubert #endif
2723b9b51feSCy Schubert #ifdef USE_INET6
2733b9b51feSCy Schubert 	i6addr_t swip6, sw2ip6;
2743b9b51feSCy Schubert 	ip6_t *ip6;
2753b9b51feSCy Schubert #endif
2763b9b51feSCy Schubert 	struct in_addr swip, sw2ip;
2773b9b51feSCy Schubert 	tftpinfo_t *ti;
2783b9b51feSCy Schubert 	udphdr_t udp;
2793b9b51feSCy Schubert 	fr_info_t fi;
2803b9b51feSCy Schubert 	u_short slen = 0; /* silence gcc */
2813b9b51feSCy Schubert 	nat_t *nat2;
2823b9b51feSCy Schubert 	int nflags;
2833b9b51feSCy Schubert 	ip_t *ip;
2843b9b51feSCy Schubert 	int dir;
2853b9b51feSCy Schubert 
2863b9b51feSCy Schubert 	ti = aps->aps_data;
2873b9b51feSCy Schubert 	/*
2883b9b51feSCy Schubert 	 * Add skeleton NAT entry for connection which will come back the
2893b9b51feSCy Schubert 	 * other way.
2903b9b51feSCy Schubert 	 */
2913b9b51feSCy Schubert 	bcopy((char *)fin, (char *)&fi, sizeof(fi));
2923b9b51feSCy Schubert 	fi.fin_flx |= FI_IGNORE;
2933b9b51feSCy Schubert 	fi.fin_data[1] = 0;
2943b9b51feSCy Schubert 
2953b9b51feSCy Schubert 	bzero((char *)&udp, sizeof(udp));
2963b9b51feSCy Schubert 	udp.uh_sport = 0;	/* XXX - don't specify remote port */
2973b9b51feSCy Schubert 	udp.uh_dport = ti->ti_rule->in_ndport;
2983b9b51feSCy Schubert 	udp.uh_ulen = htons(sizeof(udp));
2993b9b51feSCy Schubert 	udp.uh_sum = 0;
3003b9b51feSCy Schubert 
3013b9b51feSCy Schubert 	fi.fin_fr = &tftpfr;
3023b9b51feSCy Schubert 	fi.fin_dp = (char *)&udp;
3033b9b51feSCy Schubert 	fi.fin_sport = 0;
3043b9b51feSCy Schubert 	fi.fin_dport = ntohs(ti->ti_rule->in_ndport);
3053b9b51feSCy Schubert 	fi.fin_dlen = sizeof(udp);
3063b9b51feSCy Schubert 	fi.fin_plen = fi.fin_hlen + sizeof(udp);
3073b9b51feSCy Schubert 	fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
3083b9b51feSCy Schubert 	nflags = NAT_SLAVE|IPN_UDP|SI_W_SPORT;
3093b9b51feSCy Schubert #ifdef USE_INET6
3103b9b51feSCy Schubert 	ip6 = (ip6_t *)fin->fin_ip;
3113b9b51feSCy Schubert #endif
3123b9b51feSCy Schubert 	ip = fin->fin_ip;
3133b9b51feSCy Schubert 	sw2ip.s_addr = 0;
3143b9b51feSCy Schubert 	swip.s_addr = 0;
3153b9b51feSCy Schubert 
3163b9b51feSCy Schubert 	fi.fin_src6 = nat->nat_ndst6;
3173b9b51feSCy Schubert 	fi.fin_dst6 = nat->nat_nsrc6;
3183b9b51feSCy Schubert 	if (nat->nat_v[0] == 4) {
3193b9b51feSCy Schubert 		slen = ip->ip_len;
3203b9b51feSCy Schubert 		ip->ip_len = htons(fin->fin_hlen + sizeof(udp));
3213b9b51feSCy Schubert 		swip = ip->ip_src;
3223b9b51feSCy Schubert 		sw2ip = ip->ip_dst;
3233b9b51feSCy Schubert 		ip->ip_src = nat->nat_ndstip;
3243b9b51feSCy Schubert 		ip->ip_dst = nat->nat_nsrcip;
3253b9b51feSCy Schubert 	} else {
3263b9b51feSCy Schubert #ifdef USE_INET6
3273b9b51feSCy Schubert 		slen = ip6->ip6_plen;
3283b9b51feSCy Schubert 		ip6->ip6_plen = htons(sizeof(udp));
3293b9b51feSCy Schubert 		swip6.in6 = ip6->ip6_src;
3303b9b51feSCy Schubert 		sw2ip6.in6 = ip6->ip6_dst;
3313b9b51feSCy Schubert 		ip6->ip6_src = nat->nat_ndst6.in6;
3323b9b51feSCy Schubert 		ip6->ip6_dst = nat->nat_nsrc6.in6;
3333b9b51feSCy Schubert #endif
3343b9b51feSCy Schubert 	}
3353b9b51feSCy Schubert 
3363b9b51feSCy Schubert 	if (nat->nat_dir == NAT_INBOUND) {
3373b9b51feSCy Schubert 		dir = NAT_OUTBOUND;
3383b9b51feSCy Schubert 		fi.fin_out = 1;
3393b9b51feSCy Schubert 	} else {
3403b9b51feSCy Schubert 		dir = NAT_INBOUND;
3413b9b51feSCy Schubert 		fi.fin_out = 0;
3423b9b51feSCy Schubert 	}
3433b9b51feSCy Schubert 	nflags |= NAT_NOTRULEPORT;
3443b9b51feSCy Schubert 
3453b9b51feSCy Schubert 	MUTEX_ENTER(&softn->ipf_nat_new);
3463b9b51feSCy Schubert #ifdef USE_INET6
3473b9b51feSCy Schubert 	if (nat->nat_v[0] == 6)
3483b9b51feSCy Schubert 		nat2 = ipf_nat6_add(&fi, ti->ti_rule, NULL, nflags, dir);
3493b9b51feSCy Schubert 	else
3503b9b51feSCy Schubert #endif
3513b9b51feSCy Schubert 		nat2 = ipf_nat_add(&fi, ti->ti_rule, NULL, nflags, dir);
3523b9b51feSCy Schubert 	MUTEX_EXIT(&softn->ipf_nat_new);
3533b9b51feSCy Schubert 	if (nat2 != NULL) {
3543b9b51feSCy Schubert 		(void) ipf_nat_proto(&fi, nat2, IPN_UDP);
3553b9b51feSCy Schubert 		ipf_nat_update(&fi, nat2);
3563b9b51feSCy Schubert 		fi.fin_ifp = NULL;
3573b9b51feSCy Schubert 		if (ti->ti_rule->in_redir == NAT_MAP) {
3583b9b51feSCy Schubert 			fi.fin_src6 = nat->nat_ndst6;
3593b9b51feSCy Schubert 			fi.fin_dst6 = nat->nat_nsrc6;
3603b9b51feSCy Schubert 			if (nat->nat_v[0] == 4) {
3613b9b51feSCy Schubert 				ip->ip_src = nat->nat_ndstip;
3623b9b51feSCy Schubert 				ip->ip_dst = nat->nat_nsrcip;
3633b9b51feSCy Schubert 			} else {
3643b9b51feSCy Schubert #ifdef USE_INET6
3653b9b51feSCy Schubert 				ip6->ip6_src = nat->nat_ndst6.in6;
3663b9b51feSCy Schubert 				ip6->ip6_dst = nat->nat_nsrc6.in6;
3673b9b51feSCy Schubert #endif
3683b9b51feSCy Schubert 			}
3693b9b51feSCy Schubert 		} else {
3703b9b51feSCy Schubert 			fi.fin_src6 = nat->nat_odst6;
3713b9b51feSCy Schubert 			fi.fin_dst6 = nat->nat_osrc6;
3723b9b51feSCy Schubert 			if (fin->fin_v == 4) {
3733b9b51feSCy Schubert 				ip->ip_src = nat->nat_odstip;
3743b9b51feSCy Schubert 				ip->ip_dst = nat->nat_osrcip;
3753b9b51feSCy Schubert 			} else {
3763b9b51feSCy Schubert #ifdef USE_INET6
3773b9b51feSCy Schubert 				ip6->ip6_src = nat->nat_odst6.in6;
3783b9b51feSCy Schubert 				ip6->ip6_dst = nat->nat_osrc6.in6;
3793b9b51feSCy Schubert #endif
3803b9b51feSCy Schubert 			}
3813b9b51feSCy Schubert 		}
3823b9b51feSCy Schubert 		if (ipf_state_add(softc, &fi, NULL, SI_W_SPORT) != 0) {
3833b9b51feSCy Schubert 			ipf_nat_setpending(softc, nat2);
3843b9b51feSCy Schubert 		}
3853b9b51feSCy Schubert 	}
3863b9b51feSCy Schubert 	if (nat->nat_v[0] == 4) {
3873b9b51feSCy Schubert 		ip->ip_len = slen;
3883b9b51feSCy Schubert 		ip->ip_src = swip;
3893b9b51feSCy Schubert 		ip->ip_dst = sw2ip;
3903b9b51feSCy Schubert 	} else {
3913b9b51feSCy Schubert #ifdef USE_INET6
3923b9b51feSCy Schubert 		ip6->ip6_plen = slen;
3933b9b51feSCy Schubert 		ip6->ip6_src = swip6.in6;
3943b9b51feSCy Schubert 		ip6->ip6_dst = sw2ip6.in6;
3953b9b51feSCy Schubert #endif
3963b9b51feSCy Schubert 	}
3978c82b374SCy Schubert 	return (0);
3983b9b51feSCy Schubert }
3993b9b51feSCy Schubert 
4003b9b51feSCy Schubert 
4013b9b51feSCy Schubert int
ipf_p_tftp_client(ipf_tftp_softc_t * softt,fr_info_t * fin,ap_session_t * aps,nat_t * nat)402064a5a95SCy Schubert ipf_p_tftp_client(ipf_tftp_softc_t *softt, fr_info_t *fin, ap_session_t *aps,
403064a5a95SCy Schubert 	nat_t *nat)
4043b9b51feSCy Schubert {
4053b9b51feSCy Schubert 	u_char *msg, *s, *t;
4063b9b51feSCy Schubert 	tftpinfo_t *ti;
4073b9b51feSCy Schubert 	u_short opcode;
4083b9b51feSCy Schubert 	udphdr_t *udp;
4093b9b51feSCy Schubert 	int len;
4103b9b51feSCy Schubert 
4113b9b51feSCy Schubert 	if (fin->fin_dlen < 4)
4128c82b374SCy Schubert 		return (0);
4133b9b51feSCy Schubert 
4143b9b51feSCy Schubert 	ti = aps->aps_data;
4153b9b51feSCy Schubert 	msg = fin->fin_dp;
4163b9b51feSCy Schubert 	msg += sizeof(udphdr_t);
4173b9b51feSCy Schubert 	opcode = (msg[0] << 8) | msg[1];
4183b9b51feSCy Schubert 	DT3(tftp_cmd, fr_info_t *, fin, int, opcode, nat_t *, nat);
4193b9b51feSCy Schubert 
4203b9b51feSCy Schubert 	switch (opcode)
4213b9b51feSCy Schubert 	{
4223b9b51feSCy Schubert 	case TFTP_CMD_WRITE :
4233b9b51feSCy Schubert 		if (softt->ipf_p_tftp_readonly != 0)
4243b9b51feSCy Schubert 			break;
4253b9b51feSCy Schubert 		/* FALLTHROUGH */
4263b9b51feSCy Schubert 	case TFTP_CMD_READ :
4273b9b51feSCy Schubert 		len = fin->fin_dlen - sizeof(*udp) - 2;
4283b9b51feSCy Schubert 		if (len > sizeof(ti->ti_filename) - 1)
4293b9b51feSCy Schubert 			len = sizeof(ti->ti_filename) - 1;
4303b9b51feSCy Schubert 		s = msg + 2;
4313b9b51feSCy Schubert 		for (t = (u_char *)ti->ti_filename; (len > 0); len--, s++) {
4323b9b51feSCy Schubert 			*t++ = *s;
4333b9b51feSCy Schubert 			if (*s == '\0')
4343b9b51feSCy Schubert 				break;
4353b9b51feSCy Schubert 		}
4363b9b51feSCy Schubert 		ipf_p_tftp_backchannel(fin, aps, nat);
4373b9b51feSCy Schubert 		break;
4383b9b51feSCy Schubert 	default :
4398c82b374SCy Schubert 		return (-1);
4403b9b51feSCy Schubert 	}
4413b9b51feSCy Schubert 
4423b9b51feSCy Schubert 	ti = aps->aps_data;
4433b9b51feSCy Schubert 	ti->ti_lastcmd = opcode;
4448c82b374SCy Schubert 	return (0);
4453b9b51feSCy Schubert }
4463b9b51feSCy Schubert 
4473b9b51feSCy Schubert 
4483b9b51feSCy Schubert int
ipf_p_tftp_server(ipf_tftp_softc_t * softt,fr_info_t * fin,ap_session_t * aps,nat_t * nat)449064a5a95SCy Schubert ipf_p_tftp_server(ipf_tftp_softc_t *softt, fr_info_t *fin, ap_session_t *aps,
450064a5a95SCy Schubert 	nat_t *nat)
4513b9b51feSCy Schubert {
4523b9b51feSCy Schubert 	tftpinfo_t *ti;
4533b9b51feSCy Schubert 	u_short opcode;
4543b9b51feSCy Schubert 	u_short arg;
4553b9b51feSCy Schubert 	u_char *msg;
4563b9b51feSCy Schubert 
4573b9b51feSCy Schubert 	if (fin->fin_dlen < 4)
4588c82b374SCy Schubert 		return (0);
4593b9b51feSCy Schubert 
4603b9b51feSCy Schubert 	ti = aps->aps_data;
4613b9b51feSCy Schubert 	msg = fin->fin_dp;
4623b9b51feSCy Schubert 	msg += sizeof(udphdr_t);
4633b9b51feSCy Schubert 	arg = (msg[2] << 8) | msg[3];
4643b9b51feSCy Schubert 	opcode = (msg[0] << 8) | msg[1];
4653b9b51feSCy Schubert 
4663b9b51feSCy Schubert 	switch (opcode)
4673b9b51feSCy Schubert 	{
4683b9b51feSCy Schubert 	case TFTP_CMD_ACK :
4693b9b51feSCy Schubert 		ti->ti_lastblk = arg;
4703b9b51feSCy Schubert 		break;
4713b9b51feSCy Schubert 
4723b9b51feSCy Schubert 	case TFTP_CMD_ERROR :
4733b9b51feSCy Schubert 		ti->ti_lasterror = arg;
4743b9b51feSCy Schubert 		break;
4753b9b51feSCy Schubert 
4763b9b51feSCy Schubert 	default :
4778c82b374SCy Schubert 		return (-1);
4783b9b51feSCy Schubert 	}
4793b9b51feSCy Schubert 
4803b9b51feSCy Schubert 	ti->ti_lastcmd = opcode;
4818c82b374SCy Schubert 	return (0);
4823b9b51feSCy Schubert }
483