2 * Copyright (C) 2012 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ip_tftp_pxy.c,v 1.1.2.9 2012/07/22 08:04:23 darren_r Exp $
11 typedef struct ipf_tftp_softc_s {
12 int ipf_p_tftp_readonly;
13 ipftuneable_t *ipf_p_tftp_tune;
16 int ipf_p_tftp_backchannel __P((fr_info_t *, ap_session_t *, nat_t *));
17 int ipf_p_tftp_client __P((ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
19 int ipf_p_tftp_in __P((void *, fr_info_t *, ap_session_t *, nat_t *));
20 void ipf_p_tftp_main_load __P((void));
21 void ipf_p_tftp_main_unload __P((void));
22 int ipf_p_tftp_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
23 void ipf_p_tftp_del __P((ipf_main_softc_t *, ap_session_t *));
24 int ipf_p_tftp_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
25 int ipf_p_tftp_server __P((ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
27 void *ipf_p_tftp_soft_create __P((ipf_main_softc_t *));
28 void ipf_p_tftp_soft_destroy __P((ipf_main_softc_t *, void *));
30 static frentry_t tftpfr;
31 static int tftp_proxy_init = 0;
33 typedef enum tftp_cmd_e {
41 typedef struct tftpinfo {
42 tftp_cmd_t ti_lastcmd;
50 static ipftuneable_t ipf_tftp_tuneables[] = {
51 { { (void *)offsetof(ipf_tftp_softc_t, ipf_p_tftp_readonly) },
52 "tftp_read_only", 0, 1,
53 stsizeof(ipf_tftp_softc_t, ipf_p_tftp_readonly),
55 { { NULL }, NULL, 0, 0, 0, 0, NULL, NULL }
60 * TFTP application proxy initialization.
63 ipf_p_tftp_main_load()
66 bzero((char *)&tftpfr, sizeof(tftpfr));
68 tftpfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
69 MUTEX_INIT(&tftpfr.fr_lock, "TFTP proxy rule lock");
75 ipf_p_tftp_main_unload()
78 if (tftp_proxy_init == 1) {
79 MUTEX_DESTROY(&tftpfr.fr_lock);
86 ipf_p_tftp_soft_create(softc)
87 ipf_main_softc_t *softc;
89 ipf_tftp_softc_t *softt;
91 KMALLOC(softt, ipf_tftp_softc_t *);
95 bzero((char *)softt, sizeof(*softt));
97 softt->ipf_p_tftp_tune = ipf_tune_array_copy(softt,
98 sizeof(ipf_tftp_tuneables),
100 if (softt->ipf_p_tftp_tune == NULL) {
101 ipf_p_tftp_soft_destroy(softc, softt);
104 if (ipf_tune_array_link(softc, softt->ipf_p_tftp_tune) == -1) {
105 ipf_p_tftp_soft_destroy(softc, softt);
109 softt->ipf_p_tftp_readonly = 1;
116 ipf_p_tftp_soft_destroy(softc, arg)
117 ipf_main_softc_t *softc;
120 ipf_tftp_softc_t *softt = arg;
122 if (softt->ipf_p_tftp_tune != NULL) {
123 ipf_tune_array_unlink(softc, softt->ipf_p_tftp_tune);
124 KFREES(softt->ipf_p_tftp_tune, sizeof(ipf_tftp_tuneables));
125 softt->ipf_p_tftp_tune = NULL;
133 ipf_p_tftp_out(arg, fin, aps, nat)
139 ipf_tftp_softc_t *softt = arg;
141 fin->fin_flx |= FI_NOWILD;
142 if (nat->nat_dir == NAT_OUTBOUND)
143 return ipf_p_tftp_client(softt, fin, aps, nat);
144 return ipf_p_tftp_server(softt, fin, aps, nat);
149 ipf_p_tftp_in(arg, fin, aps, nat)
155 ipf_tftp_softc_t *softt = arg;
157 fin->fin_flx |= FI_NOWILD;
158 if (nat->nat_dir == NAT_INBOUND)
159 return ipf_p_tftp_client(softt, fin, aps, nat);
160 return ipf_p_tftp_server(softt, fin, aps, nat);
165 ipf_p_tftp_new(arg, fin, aps, nat)
177 fin = fin; /* LINT */
182 KMALLOC(ti, tftpinfo_t *);
185 KMALLOCS(ipn, ipnat_t *, size);
192 aps->aps_psiz = sizeof(*ti);
193 bzero((char *)ti, sizeof(*ti));
194 bzero((char *)ipn, size);
197 udp = (udphdr_t *)fin->fin_dp;
198 aps->aps_sport = udp->uh_sport;
199 aps->aps_dport = udp->uh_dport;
206 ipn->in_pr[0] = IPPROTO_UDP;
207 ipn->in_pr[1] = IPPROTO_UDP;
208 ipn->in_ifps[0] = nat->nat_ifps[0];
209 ipn->in_ifps[1] = nat->nat_ifps[1];
210 ipn->in_v[0] = nat->nat_ptr->in_v[1];
211 ipn->in_v[1] = nat->nat_ptr->in_v[0];
212 ipn->in_flags = IPN_UDP|IPN_FIXEDDPORT|IPN_PROXYRULE;
214 ipn->in_nsrcip6 = nat->nat_odst6;
215 ipn->in_osrcip6 = nat->nat_ndst6;
217 if ((np->in_redir & NAT_REDIRECT) != 0) {
218 ipn->in_redir = NAT_MAP;
219 if (ipn->in_v[0] == 4) {
220 ipn->in_snip = ntohl(nat->nat_odstaddr);
221 ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
224 ipn->in_snip6 = nat->nat_odst6;
225 ipn->in_dnip6 = nat->nat_nsrc6;
228 ipn->in_ndstip6 = nat->nat_nsrc6;
229 ipn->in_odstip6 = nat->nat_osrc6;
231 ipn->in_redir = NAT_REDIRECT;
232 if (ipn->in_v[0] == 4) {
233 ipn->in_snip = ntohl(nat->nat_odstaddr);
234 ipn->in_dnip = ntohl(nat->nat_osrcaddr);
237 ipn->in_snip6 = nat->nat_odst6;
238 ipn->in_dnip6 = nat->nat_osrc6;
241 ipn->in_ndstip6 = nat->nat_osrc6;
242 ipn->in_odstip6 = nat->nat_nsrc6;
244 ipn->in_odport = htons(fin->fin_sport);
245 ipn->in_ndport = htons(fin->fin_sport);
247 IP6_SETONES(&ipn->in_osrcmsk6);
248 IP6_SETONES(&ipn->in_nsrcmsk6);
249 IP6_SETONES(&ipn->in_odstmsk6);
250 IP6_SETONES(&ipn->in_ndstmsk6);
251 MUTEX_INIT(&ipn->in_lock, "tftp proxy NAT rule");
253 ipn->in_namelen = np->in_namelen;
254 bcopy(np->in_names, ipn->in_ifnames, ipn->in_namelen);
255 ipn->in_ifnames[0] = np->in_ifnames[0];
256 ipn->in_ifnames[1] = np->in_ifnames[1];
265 ipf_p_tftp_del(softc, aps)
266 ipf_main_softc_t *softc;
271 tftp = aps->aps_data;
273 tftp->ti_rule->in_flags |= IPN_DELETE;
274 ipf_nat_rule_deref(softc, &tftp->ti_rule);
280 * Setup for a new TFTP proxy.
283 ipf_p_tftp_backchannel(fin, aps, nat)
288 ipf_main_softc_t *softc = fin->fin_main_soft;
290 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
293 i6addr_t swip6, sw2ip6;
296 struct in_addr swip, sw2ip;
300 u_short slen = 0; /* silence gcc */
308 * Add skeleton NAT entry for connection which will come back the
311 bcopy((char *)fin, (char *)&fi, sizeof(fi));
312 fi.fin_flx |= FI_IGNORE;
315 bzero((char *)&udp, sizeof(udp));
316 udp.uh_sport = 0; /* XXX - don't specify remote port */
317 udp.uh_dport = ti->ti_rule->in_ndport;
318 udp.uh_ulen = htons(sizeof(udp));
322 fi.fin_dp = (char *)&udp;
324 fi.fin_dport = ntohs(ti->ti_rule->in_ndport);
325 fi.fin_dlen = sizeof(udp);
326 fi.fin_plen = fi.fin_hlen + sizeof(udp);
327 fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
328 nflags = NAT_SLAVE|IPN_UDP|SI_W_SPORT;
330 ip6 = (ip6_t *)fin->fin_ip;
336 fi.fin_src6 = nat->nat_ndst6;
337 fi.fin_dst6 = nat->nat_nsrc6;
338 if (nat->nat_v[0] == 4) {
340 ip->ip_len = htons(fin->fin_hlen + sizeof(udp));
343 ip->ip_src = nat->nat_ndstip;
344 ip->ip_dst = nat->nat_nsrcip;
347 slen = ip6->ip6_plen;
348 ip6->ip6_plen = htons(sizeof(udp));
349 swip6.in6 = ip6->ip6_src;
350 sw2ip6.in6 = ip6->ip6_dst;
351 ip6->ip6_src = nat->nat_ndst6.in6;
352 ip6->ip6_dst = nat->nat_nsrc6.in6;
356 if (nat->nat_dir == NAT_INBOUND) {
363 nflags |= NAT_NOTRULEPORT;
365 MUTEX_ENTER(&softn->ipf_nat_new);
367 if (nat->nat_v[0] == 6)
368 nat2 = ipf_nat6_add(&fi, ti->ti_rule, NULL, nflags, dir);
371 nat2 = ipf_nat_add(&fi, ti->ti_rule, NULL, nflags, dir);
372 MUTEX_EXIT(&softn->ipf_nat_new);
374 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
375 ipf_nat_update(&fi, nat2);
377 if (ti->ti_rule->in_redir == NAT_MAP) {
378 fi.fin_src6 = nat->nat_ndst6;
379 fi.fin_dst6 = nat->nat_nsrc6;
380 if (nat->nat_v[0] == 4) {
381 ip->ip_src = nat->nat_ndstip;
382 ip->ip_dst = nat->nat_nsrcip;
385 ip6->ip6_src = nat->nat_ndst6.in6;
386 ip6->ip6_dst = nat->nat_nsrc6.in6;
390 fi.fin_src6 = nat->nat_odst6;
391 fi.fin_dst6 = nat->nat_osrc6;
392 if (fin->fin_v == 4) {
393 ip->ip_src = nat->nat_odstip;
394 ip->ip_dst = nat->nat_osrcip;
397 ip6->ip6_src = nat->nat_odst6.in6;
398 ip6->ip6_dst = nat->nat_osrc6.in6;
402 if (ipf_state_add(softc, &fi, NULL, SI_W_SPORT) != 0) {
403 ipf_nat_setpending(softc, nat2);
406 if (nat->nat_v[0] == 4) {
412 ip6->ip6_plen = slen;
413 ip6->ip6_src = swip6.in6;
414 ip6->ip6_dst = sw2ip6.in6;
422 ipf_p_tftp_client(softt, fin, aps, nat)
423 ipf_tftp_softc_t *softt;
434 if (fin->fin_dlen < 4)
439 msg += sizeof(udphdr_t);
440 opcode = (msg[0] << 8) | msg[1];
441 DT3(tftp_cmd, fr_info_t *, fin, int, opcode, nat_t *, nat);
445 case TFTP_CMD_WRITE :
446 if (softt->ipf_p_tftp_readonly != 0)
450 len = fin->fin_dlen - sizeof(*udp) - 2;
451 if (len > sizeof(ti->ti_filename) - 1)
452 len = sizeof(ti->ti_filename) - 1;
454 for (t = (u_char *)ti->ti_filename; (len > 0); len--, s++) {
459 ipf_p_tftp_backchannel(fin, aps, nat);
466 ti->ti_lastcmd = opcode;
472 ipf_p_tftp_server(softt, fin, aps, nat)
473 ipf_tftp_softc_t *softt;
483 if (fin->fin_dlen < 4)
488 msg += sizeof(udphdr_t);
489 arg = (msg[2] << 8) | msg[3];
490 opcode = (msg[0] << 8) | msg[1];
495 ti->ti_lastblk = arg;
498 case TFTP_CMD_ERROR :
499 ti->ti_lasterror = arg;
506 ti->ti_lastcmd = opcode;