xref: /netbsd-src/sys/external/bsd/ipf/netinet/ip_rcmd_pxy.c (revision 9e58a5046a26cdbb0df3f6a1c7ed4394fb3dff28)
1*9e58a504Smartin /*	$NetBSD: ip_rcmd_pxy.c,v 1.5 2013/09/14 12:06:19 martin Exp $	*/
2c2aa585cSchristos 
3c2aa585cSchristos /*
413885a66Sdarrenr  * Copyright (C) 2012 by Darren Reed.
5c2aa585cSchristos  *
6c2aa585cSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7c2aa585cSchristos  *
813885a66Sdarrenr  * Id: ip_rcmd_pxy.c,v 1.1.1.2 2012/07/22 13:45:33 darrenr Exp
9c2aa585cSchristos  *
10c2aa585cSchristos  * Simple RCMD transparent proxy for in-kernel use.  For use with the NAT
11c2aa585cSchristos  * code.
12c2aa585cSchristos  */
13c2aa585cSchristos 
140c6adecaSchristos #include <sys/cdefs.h>
15*9e58a504Smartin __KERNEL_RCSID(1, "$NetBSD: ip_rcmd_pxy.c,v 1.5 2013/09/14 12:06:19 martin Exp $");
160c6adecaSchristos 
17c2aa585cSchristos #define	IPF_RCMD_PROXY
18c2aa585cSchristos 
1913885a66Sdarrenr typedef struct rcmdinfo {
2013885a66Sdarrenr 	u_32_t	rcmd_port;	/* Port number seen */
2113885a66Sdarrenr 	u_32_t	rcmd_portseq;	/* Sequence number where port is first seen */
2213885a66Sdarrenr 	ipnat_t	*rcmd_rule;	/* Template rule for back connection */
2313885a66Sdarrenr } rcmdinfo_t;
2413885a66Sdarrenr 
250c6adecaSchristos void ipf_p_rcmd_main_load(void);
260c6adecaSchristos void ipf_p_rcmd_main_unload(void);
27c2aa585cSchristos 
280c6adecaSchristos int ipf_p_rcmd_init(void);
290c6adecaSchristos void ipf_p_rcmd_fini(void);
300c6adecaSchristos void ipf_p_rcmd_del(ipf_main_softc_t *, ap_session_t *);
310c6adecaSchristos int ipf_p_rcmd_new(void *, fr_info_t *, ap_session_t *, nat_t *);
320c6adecaSchristos int ipf_p_rcmd_out(void *, fr_info_t *, ap_session_t *, nat_t *);
330c6adecaSchristos int ipf_p_rcmd_in(void *, fr_info_t *, ap_session_t *, nat_t *);
340c6adecaSchristos u_short ipf_rcmd_atoi(char *);
350c6adecaSchristos int ipf_p_rcmd_portmsg(fr_info_t *, ap_session_t *, nat_t *);
36c2aa585cSchristos 
37c2aa585cSchristos static	frentry_t	rcmdfr;
38c2aa585cSchristos 
39c2aa585cSchristos static	int		rcmd_proxy_init = 0;
40c2aa585cSchristos 
41c2aa585cSchristos 
42c2aa585cSchristos /*
43c2aa585cSchristos  * RCMD application proxy initialization.
44c2aa585cSchristos  */
45c2aa585cSchristos void
ipf_p_rcmd_main_load(void)460c6adecaSchristos ipf_p_rcmd_main_load(void)
47c2aa585cSchristos {
48c2aa585cSchristos 	bzero((char *)&rcmdfr, sizeof(rcmdfr));
49c2aa585cSchristos 	rcmdfr.fr_ref = 1;
50c2aa585cSchristos 	rcmdfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
51c2aa585cSchristos 	MUTEX_INIT(&rcmdfr.fr_lock, "RCMD proxy rule lock");
52c2aa585cSchristos 	rcmd_proxy_init = 1;
53c2aa585cSchristos }
54c2aa585cSchristos 
55c2aa585cSchristos 
56c2aa585cSchristos void
ipf_p_rcmd_main_unload(void)570c6adecaSchristos ipf_p_rcmd_main_unload(void)
58c2aa585cSchristos {
59c2aa585cSchristos 	if (rcmd_proxy_init == 1) {
60c2aa585cSchristos 		MUTEX_DESTROY(&rcmdfr.fr_lock);
61c2aa585cSchristos 		rcmd_proxy_init = 0;
62c2aa585cSchristos 	}
63c2aa585cSchristos }
64c2aa585cSchristos 
65c2aa585cSchristos 
66c2aa585cSchristos /*
67c2aa585cSchristos  * Setup for a new RCMD proxy.
68c2aa585cSchristos  */
69c2aa585cSchristos int
ipf_p_rcmd_new(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)700c6adecaSchristos ipf_p_rcmd_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
71c2aa585cSchristos {
72c2aa585cSchristos 	tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
73c2aa585cSchristos 	rcmdinfo_t *rc;
74c2aa585cSchristos 	ipnat_t *ipn;
75c2aa585cSchristos 
76c2aa585cSchristos 	fin = fin;	/* LINT */
77c2aa585cSchristos 
7813885a66Sdarrenr 	KMALLOC(rc, rcmdinfo_t *);
79c2aa585cSchristos 	if (rc == NULL) {
800c6adecaSchristos 		printf("ipf_p_rcmd_new:KMALLOCS(%zu) failed\n", sizeof(*rc));
81c2aa585cSchristos 		return -1;
82c2aa585cSchristos 	}
83c2aa585cSchristos 	aps->aps_sport = tcp->th_sport;
84c2aa585cSchristos 	aps->aps_dport = tcp->th_dport;
85c2aa585cSchristos 
8613885a66Sdarrenr 	ipn = ipf_proxy_rule_rev(nat);
8713885a66Sdarrenr 	if (ipn == NULL) {
8813885a66Sdarrenr 		KFREE(rc);
8913885a66Sdarrenr 		return -1;
90c2aa585cSchristos 	}
91c2aa585cSchristos 
9213885a66Sdarrenr 	aps->aps_data = rc;
9313885a66Sdarrenr 	aps->aps_psiz = sizeof(*rc);
9413885a66Sdarrenr 	bzero((char *)rc, sizeof(*rc));
95c2aa585cSchristos 
9613885a66Sdarrenr 	rc->rcmd_rule = ipn;
97c2aa585cSchristos 
98c2aa585cSchristos 	return 0;
99c2aa585cSchristos }
100c2aa585cSchristos 
101c2aa585cSchristos 
102c2aa585cSchristos void
ipf_p_rcmd_del(ipf_main_softc_t * softc,ap_session_t * aps)1030c6adecaSchristos ipf_p_rcmd_del(ipf_main_softc_t *softc, ap_session_t *aps)
104c2aa585cSchristos {
105c2aa585cSchristos 	rcmdinfo_t *rci;
106c2aa585cSchristos 
107c2aa585cSchristos 	rci = aps->aps_data;
108c2aa585cSchristos 	if (rci != NULL) {
10913885a66Sdarrenr 		rci->rcmd_rule->in_flags |= IPN_DELETE;
11013885a66Sdarrenr 		ipf_nat_rule_deref(softc, &rci->rcmd_rule);
111c2aa585cSchristos 	}
112c2aa585cSchristos }
113c2aa585cSchristos 
114c2aa585cSchristos 
115c2aa585cSchristos /*
116c2aa585cSchristos  * ipf_rcmd_atoi - implement a simple version of atoi
117c2aa585cSchristos  */
118c2aa585cSchristos u_short
ipf_rcmd_atoi(char * ptr)1190c6adecaSchristos ipf_rcmd_atoi(char *ptr)
120c2aa585cSchristos {
1210c6adecaSchristos 	char *s = ptr, c;
1220c6adecaSchristos 	u_short i = 0;
123c2aa585cSchristos 
124c2aa585cSchristos 	while (((c = *s++) != '\0') && ISDIGIT(c)) {
125c2aa585cSchristos 		i *= 10;
126c2aa585cSchristos 		i += c - '0';
127c2aa585cSchristos 	}
128c2aa585cSchristos 	return i;
129c2aa585cSchristos }
130c2aa585cSchristos 
131c2aa585cSchristos 
132c2aa585cSchristos int
ipf_p_rcmd_portmsg(fr_info_t * fin,ap_session_t * aps,nat_t * nat)1330c6adecaSchristos ipf_p_rcmd_portmsg(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
134c2aa585cSchristos {
135c2aa585cSchristos 	tcphdr_t *tcp, tcph, *tcp2 = &tcph;
136c2aa585cSchristos 	int off, dlen, nflags, direction;
137c2aa585cSchristos 	ipf_main_softc_t *softc;
13813885a66Sdarrenr 	ipf_nat_softc_t *softn;
139c2aa585cSchristos 	char portbuf[8], *s;
140c2aa585cSchristos 	rcmdinfo_t *rc;
141c2aa585cSchristos 	fr_info_t fi;
142c2aa585cSchristos 	u_short sp;
143c2aa585cSchristos 	nat_t *nat2;
144585fa502Spgoyette #ifdef USE_INET6
14513885a66Sdarrenr 	ip6_t *ip6;
146585fa502Spgoyette #endif
14713885a66Sdarrenr 	int tcpsz;
148585fa502Spgoyette 	int slen = 0;
149c2aa585cSchristos 	ip_t *ip;
150c2aa585cSchristos 	mb_t *m;
151c2aa585cSchristos 
152c2aa585cSchristos 	tcp = (tcphdr_t *)fin->fin_dp;
153c2aa585cSchristos 
154c2aa585cSchristos 	m = fin->fin_m;
155c2aa585cSchristos 	ip = fin->fin_ip;
15613885a66Sdarrenr 	tcpsz = TCP_OFF(tcp) << 2;
157585fa502Spgoyette #ifdef USE_INET6
15813885a66Sdarrenr 	ip6 = (ip6_t *)fin->fin_ip;
159585fa502Spgoyette #endif
16013885a66Sdarrenr 	softc = fin->fin_main_soft;
16113885a66Sdarrenr 	softn = softc->ipf_nat_soft;
16213885a66Sdarrenr 	off = (char *)tcp - (char *)ip + tcpsz + fin->fin_ipoff;
163c2aa585cSchristos 
16413885a66Sdarrenr 	dlen = fin->fin_dlen - tcpsz;
165c2aa585cSchristos 	if (dlen <= 0)
166c2aa585cSchristos 		return 0;
167c2aa585cSchristos 
168c2aa585cSchristos 	rc = (rcmdinfo_t *)aps->aps_data;
169c2aa585cSchristos 	if ((rc->rcmd_portseq != 0) &&
170c2aa585cSchristos 	    (tcp->th_seq != rc->rcmd_portseq))
171c2aa585cSchristos 		return 0;
172c2aa585cSchristos 
173c2aa585cSchristos 	bzero(portbuf, sizeof(portbuf));
174c2aa585cSchristos 	COPYDATA(m, off, MIN(sizeof(portbuf), dlen), portbuf);
175c2aa585cSchristos 
176c2aa585cSchristos 	portbuf[sizeof(portbuf) - 1] = '\0';
177c2aa585cSchristos 	s = portbuf;
178c2aa585cSchristos 	sp = ipf_rcmd_atoi(s);
179c2aa585cSchristos 	if (sp == 0) {
180c2aa585cSchristos #ifdef IP_RCMD_PROXY_DEBUG
181c2aa585cSchristos 		printf("ipf_p_rcmd_portmsg:sp == 0 dlen %d [%s]\n",
182c2aa585cSchristos 		       dlen, portbuf);
183c2aa585cSchristos #endif
184c2aa585cSchristos 		return 0;
185c2aa585cSchristos 	}
186c2aa585cSchristos 
187c2aa585cSchristos 	if (rc->rcmd_port != 0 && sp != rc->rcmd_port) {
188c2aa585cSchristos #ifdef IP_RCMD_PROXY_DEBUG
189c2aa585cSchristos 		printf("ipf_p_rcmd_portmsg:sp(%d) != rcmd_port(%d)\n",
190c2aa585cSchristos 		       sp, rc->rcmd_port);
191c2aa585cSchristos #endif
192c2aa585cSchristos 		return 0;
193c2aa585cSchristos 	}
194c2aa585cSchristos 
195c2aa585cSchristos 	rc->rcmd_port = sp;
196c2aa585cSchristos 	rc->rcmd_portseq = tcp->th_seq;
197c2aa585cSchristos 
198c2aa585cSchristos 	/*
199c2aa585cSchristos 	 * Initialise the packet info structure so we can search the NAT
200c2aa585cSchristos 	 * table to see if there already is soemthing present that matches
201c2aa585cSchristos 	 * up with what we want to add.
202c2aa585cSchristos 	 */
203c2aa585cSchristos 	bcopy((char *)fin, (char *)&fi, sizeof(fi));
204c2aa585cSchristos 	fi.fin_flx |= FI_IGNORE;
205c2aa585cSchristos 	fi.fin_data[0] = 0;
206c2aa585cSchristos 	fi.fin_data[1] = sp;
20713885a66Sdarrenr 	fi.fin_src6 = nat->nat_ndst6;
20813885a66Sdarrenr 	fi.fin_dst6 = nat->nat_nsrc6;
209c2aa585cSchristos 
21013885a66Sdarrenr 	if (nat->nat_v[0] == 6) {
21113885a66Sdarrenr #ifdef USE_INET6
21213885a66Sdarrenr 		if (nat->nat_dir == NAT_OUTBOUND) {
21313885a66Sdarrenr 			nat2 = ipf_nat6_outlookup(&fi, NAT_SEARCH|IPN_TCP,
21413885a66Sdarrenr 						  nat->nat_pr[1],
21513885a66Sdarrenr 						  &nat->nat_osrc6.in6,
21613885a66Sdarrenr 						  &nat->nat_odst6.in6);
21713885a66Sdarrenr 		} else {
21813885a66Sdarrenr 			nat2 = ipf_nat6_inlookup(&fi, NAT_SEARCH|IPN_TCP,
21913885a66Sdarrenr 						 nat->nat_pr[0],
22013885a66Sdarrenr 						 &nat->nat_osrc6.in6,
22113885a66Sdarrenr 						 &nat->nat_odst6.in6);
22213885a66Sdarrenr 		}
22313885a66Sdarrenr #else
22413885a66Sdarrenr 		nat2 = (void *)-1;
22513885a66Sdarrenr #endif
22613885a66Sdarrenr 	} else {
227c2aa585cSchristos 		if (nat->nat_dir == NAT_OUTBOUND) {
228c2aa585cSchristos 			nat2 = ipf_nat_outlookup(&fi, NAT_SEARCH|IPN_TCP,
229c2aa585cSchristos 						 nat->nat_pr[1],
23013885a66Sdarrenr 						 nat->nat_osrcip,
23113885a66Sdarrenr 						 nat->nat_odstip);
232c2aa585cSchristos 		} else {
233c2aa585cSchristos 			nat2 = ipf_nat_inlookup(&fi, NAT_SEARCH|IPN_TCP,
234c2aa585cSchristos 						nat->nat_pr[0],
23513885a66Sdarrenr 						nat->nat_osrcip,
23613885a66Sdarrenr 						nat->nat_odstip);
237c2aa585cSchristos 		}
23813885a66Sdarrenr 	}
23913885a66Sdarrenr 	if (nat2 != NULL)
24013885a66Sdarrenr 		return APR_ERR(1);
241c2aa585cSchristos 
242c2aa585cSchristos 	/*
243c2aa585cSchristos 	 * Add skeleton NAT entry for connection which will come
244c2aa585cSchristos 	 * back the other way.
245c2aa585cSchristos 	 */
246c2aa585cSchristos 
24713885a66Sdarrenr 	if (nat->nat_v[0] == 6) {
24813885a66Sdarrenr #ifdef USE_INET6
24913885a66Sdarrenr 		slen = ip6->ip6_plen;
25013885a66Sdarrenr 		ip6->ip6_plen = htons(sizeof(*tcp));
25113885a66Sdarrenr #endif
25213885a66Sdarrenr 	} else {
253c2aa585cSchristos 		slen = ip->ip_len;
254c2aa585cSchristos 		ip->ip_len = htons(fin->fin_hlen + sizeof(*tcp));
25513885a66Sdarrenr 	}
256c2aa585cSchristos 
257c2aa585cSchristos 	/*
258c2aa585cSchristos 	 * Fill out the fake TCP header with a few fields that ipfilter
259c2aa585cSchristos 	 * considers to be important.
260c2aa585cSchristos 	 */
261c2aa585cSchristos 	bzero((char *)tcp2, sizeof(*tcp2));
262c2aa585cSchristos 	tcp2->th_win = htons(8192);
263c2aa585cSchristos 	TCP_OFF_A(tcp2, 5);
264c2aa585cSchristos 	tcp2->th_flags = TH_SYN;
265c2aa585cSchristos 
266c2aa585cSchristos 	fi.fin_dp = (char *)tcp2;
267c2aa585cSchristos 	fi.fin_fr = &rcmdfr;
268c2aa585cSchristos 	fi.fin_dlen = sizeof(*tcp2);
269c2aa585cSchristos 	fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
27013885a66Sdarrenr 	fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
271c2aa585cSchristos 
272c2aa585cSchristos 	if (nat->nat_dir == NAT_OUTBOUND) {
273c2aa585cSchristos 		fi.fin_out = 0;
274c2aa585cSchristos 		direction = NAT_INBOUND;
275c2aa585cSchristos 	} else {
276c2aa585cSchristos 		fi.fin_out = 1;
277c2aa585cSchristos 		direction = NAT_OUTBOUND;
278c2aa585cSchristos 	}
27913885a66Sdarrenr 	nflags = SI_W_SPORT|NAT_SLAVE|IPN_TCP;
280c2aa585cSchristos 
281c2aa585cSchristos 	MUTEX_ENTER(&softn->ipf_nat_new);
28213885a66Sdarrenr 	if (fin->fin_v == 4)
28313885a66Sdarrenr 		nat2 = ipf_nat_add(&fi, rc->rcmd_rule, NULL, nflags,
284c2aa585cSchristos 				   direction);
28513885a66Sdarrenr #ifdef USE_INET6
28613885a66Sdarrenr 	else
28713885a66Sdarrenr 		nat2 = ipf_nat6_add(&fi, rc->rcmd_rule, NULL, nflags,
28813885a66Sdarrenr 				    direction);
28913885a66Sdarrenr #endif
290c2aa585cSchristos 	MUTEX_EXIT(&softn->ipf_nat_new);
291c2aa585cSchristos 
292c2aa585cSchristos 	if (nat2 != NULL) {
293c2aa585cSchristos 		(void) ipf_nat_proto(&fi, nat2, IPN_TCP);
294c2aa585cSchristos 		MUTEX_ENTER(&nat2->nat_lock);
295c2aa585cSchristos 		ipf_nat_update(&fi, nat2);
296c2aa585cSchristos 		MUTEX_EXIT(&nat2->nat_lock);
297c2aa585cSchristos 		fi.fin_ifp = NULL;
298c2aa585cSchristos 		if (nat2->nat_dir == NAT_INBOUND)
29913885a66Sdarrenr 			fi.fin_dst6 = nat->nat_osrc6;
300c2aa585cSchristos 		(void) ipf_state_add(softc, &fi, NULL, SI_W_SPORT);
301c2aa585cSchristos 	}
30213885a66Sdarrenr 	if (nat->nat_v[0] == 6) {
30313885a66Sdarrenr #ifdef USE_INET6
30413885a66Sdarrenr 		ip6->ip6_plen = slen;
30513885a66Sdarrenr #endif
30613885a66Sdarrenr 	} else {
307c2aa585cSchristos 		ip->ip_len = slen;
308c2aa585cSchristos 	}
30913885a66Sdarrenr 	if (nat2 == NULL)
31013885a66Sdarrenr 		return APR_ERR(1);
311c2aa585cSchristos 	return 0;
312c2aa585cSchristos }
313c2aa585cSchristos 
314c2aa585cSchristos 
315c2aa585cSchristos int
ipf_p_rcmd_out(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)3160c6adecaSchristos ipf_p_rcmd_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
317c2aa585cSchristos {
318c2aa585cSchristos 	if (nat->nat_dir == NAT_OUTBOUND)
319c2aa585cSchristos 		return ipf_p_rcmd_portmsg(fin, aps, nat);
320c2aa585cSchristos 	return 0;
321c2aa585cSchristos }
322c2aa585cSchristos 
323c2aa585cSchristos 
324c2aa585cSchristos int
ipf_p_rcmd_in(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)3250c6adecaSchristos ipf_p_rcmd_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
326c2aa585cSchristos {
327c2aa585cSchristos 	if (nat->nat_dir == NAT_INBOUND)
328c2aa585cSchristos 		return ipf_p_rcmd_portmsg(fin, aps, nat);
329c2aa585cSchristos 	return 0;
330c2aa585cSchristos }
331