1 /*
2 * Copyright (C) 2012 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 *
6 * $Id: ip_tftp_pxy.c,v 1.1.2.9 2012/07/22 08:04:23 darren_r Exp $
7 */
8
9 #define IPF_TFTP_PROXY
10
11 typedef struct ipf_tftp_softc_s {
12 int ipf_p_tftp_readonly;
13 ipftuneable_t *ipf_p_tftp_tune;
14 } ipf_tftp_softc_t;
15
16 int ipf_p_tftp_backchannel(fr_info_t *, ap_session_t *, nat_t *);
17 int ipf_p_tftp_client(ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
18 nat_t *);
19 int ipf_p_tftp_in(void *, fr_info_t *, ap_session_t *, nat_t *);
20 void ipf_p_tftp_main_load(void);
21 void ipf_p_tftp_main_unload(void);
22 int ipf_p_tftp_new(void *, fr_info_t *, ap_session_t *, nat_t *);
23 void ipf_p_tftp_del(ipf_main_softc_t *, ap_session_t *);
24 int ipf_p_tftp_out(void *, fr_info_t *, ap_session_t *, nat_t *);
25 int ipf_p_tftp_server(ipf_tftp_softc_t *, fr_info_t *, ap_session_t *,
26 nat_t *);
27 void *ipf_p_tftp_soft_create(ipf_main_softc_t *);
28 void ipf_p_tftp_soft_destroy(ipf_main_softc_t *, void *);
29
30 static frentry_t tftpfr;
31 static int tftp_proxy_init = 0;
32
33 typedef enum tftp_cmd_e {
34 TFTP_CMD_READ = 1,
35 TFTP_CMD_WRITE = 2,
36 TFTP_CMD_DATA = 3,
37 TFTP_CMD_ACK = 4,
38 TFTP_CMD_ERROR = 5
39 } tftp_cmd_t;
40
41 typedef struct tftpinfo {
42 tftp_cmd_t ti_lastcmd;
43 int ti_nextblk;
44 int ti_lastblk;
45 int ti_lasterror;
46 char ti_filename[80];
47 ipnat_t *ti_rule;
48 } tftpinfo_t;
49
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),
54 0, NULL, NULL },
55 { { NULL }, NULL, 0, 0, 0, 0, NULL, NULL }
56 };
57
58
59 /*
60 * TFTP application proxy initialization.
61 */
62 void
ipf_p_tftp_main_load(void)63 ipf_p_tftp_main_load(void)
64 {
65
66 bzero((char *)&tftpfr, sizeof(tftpfr));
67 tftpfr.fr_ref = 1;
68 tftpfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
69 MUTEX_INIT(&tftpfr.fr_lock, "TFTP proxy rule lock");
70 tftp_proxy_init = 1;
71 }
72
73
74 void
ipf_p_tftp_main_unload(void)75 ipf_p_tftp_main_unload(void)
76 {
77
78 if (tftp_proxy_init == 1) {
79 MUTEX_DESTROY(&tftpfr.fr_lock);
80 tftp_proxy_init = 0;
81 }
82 }
83
84
85 void *
ipf_p_tftp_soft_create(ipf_main_softc_t * softc)86 ipf_p_tftp_soft_create(ipf_main_softc_t *softc)
87 {
88 ipf_tftp_softc_t *softt;
89
90 KMALLOC(softt, ipf_tftp_softc_t *);
91 if (softt == NULL)
92 return (NULL);
93
94 bzero((char *)softt, sizeof(*softt));
95
96 softt->ipf_p_tftp_tune = ipf_tune_array_copy(softt,
97 sizeof(ipf_tftp_tuneables),
98 ipf_tftp_tuneables);
99 if (softt->ipf_p_tftp_tune == NULL) {
100 ipf_p_tftp_soft_destroy(softc, softt);
101 return (NULL);
102 }
103 if (ipf_tune_array_link(softc, softt->ipf_p_tftp_tune) == -1) {
104 ipf_p_tftp_soft_destroy(softc, softt);
105 return (NULL);
106 }
107
108 softt->ipf_p_tftp_readonly = 1;
109
110 return (softt);
111 }
112
113
114 void
ipf_p_tftp_soft_destroy(ipf_main_softc_t * softc,void * arg)115 ipf_p_tftp_soft_destroy(ipf_main_softc_t *softc, void *arg)
116 {
117 ipf_tftp_softc_t *softt = arg;
118
119 if (softt->ipf_p_tftp_tune != NULL) {
120 ipf_tune_array_unlink(softc, softt->ipf_p_tftp_tune);
121 KFREES(softt->ipf_p_tftp_tune, sizeof(ipf_tftp_tuneables));
122 softt->ipf_p_tftp_tune = NULL;
123 }
124
125 KFREE(softt);
126 }
127
128
129 int
ipf_p_tftp_out(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)130 ipf_p_tftp_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
131 {
132 ipf_tftp_softc_t *softt = arg;
133
134 fin->fin_flx |= FI_NOWILD;
135 if (nat->nat_dir == NAT_OUTBOUND)
136 return (ipf_p_tftp_client(softt, fin, aps, nat));
137 return (ipf_p_tftp_server(softt, fin, aps, nat));
138 }
139
140
141 int
ipf_p_tftp_in(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)142 ipf_p_tftp_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
143 {
144 ipf_tftp_softc_t *softt = arg;
145
146 fin->fin_flx |= FI_NOWILD;
147 if (nat->nat_dir == NAT_INBOUND)
148 return (ipf_p_tftp_client(softt, fin, aps, nat));
149 return (ipf_p_tftp_server(softt, fin, aps, nat));
150 }
151
152
153 int
ipf_p_tftp_new(void * arg,fr_info_t * fin __unused,ap_session_t * aps,nat_t * nat)154 ipf_p_tftp_new(void *arg, fr_info_t *fin __unused, ap_session_t *aps, nat_t *nat)
155 {
156 udphdr_t *udp;
157 tftpinfo_t *ti;
158 ipnat_t *ipn;
159 ipnat_t *np;
160 int size;
161
162 np = nat->nat_ptr;
163 size = np->in_size;
164
165 KMALLOC(ti, tftpinfo_t *);
166 if (ti == NULL)
167 return (-1);
168 KMALLOCS(ipn, ipnat_t *, size);
169 if (ipn == NULL) {
170 KFREE(ti);
171 return (-1);
172 }
173
174 aps->aps_data = ti;
175 aps->aps_psiz = sizeof(*ti);
176 bzero((char *)ti, sizeof(*ti));
177 bzero((char *)ipn, size);
178 ti->ti_rule = ipn;
179
180 udp = (udphdr_t *)fin->fin_dp;
181 aps->aps_sport = udp->uh_sport;
182 aps->aps_dport = udp->uh_dport;
183
184 ipn->in_size = size;
185 ipn->in_apr = NULL;
186 ipn->in_use = 1;
187 ipn->in_hits = 1;
188 ipn->in_ippip = 1;
189 ipn->in_pr[0] = IPPROTO_UDP;
190 ipn->in_pr[1] = IPPROTO_UDP;
191 ipn->in_ifps[0] = nat->nat_ifps[0];
192 ipn->in_ifps[1] = nat->nat_ifps[1];
193 ipn->in_v[0] = nat->nat_ptr->in_v[1];
194 ipn->in_v[1] = nat->nat_ptr->in_v[0];
195 ipn->in_flags = IPN_UDP|IPN_FIXEDDPORT|IPN_PROXYRULE;
196
197 ipn->in_nsrcip6 = nat->nat_odst6;
198 ipn->in_osrcip6 = nat->nat_ndst6;
199
200 if ((np->in_redir & NAT_REDIRECT) != 0) {
201 ipn->in_redir = NAT_MAP;
202 if (ipn->in_v[0] == 4) {
203 ipn->in_snip = ntohl(nat->nat_odstaddr);
204 ipn->in_dnip = ntohl(nat->nat_nsrcaddr);
205 } else {
206 #ifdef USE_INET6
207 ipn->in_snip6 = nat->nat_odst6;
208 ipn->in_dnip6 = nat->nat_nsrc6;
209 #endif
210 }
211 ipn->in_ndstip6 = nat->nat_nsrc6;
212 ipn->in_odstip6 = nat->nat_osrc6;
213 } else {
214 ipn->in_redir = NAT_REDIRECT;
215 if (ipn->in_v[0] == 4) {
216 ipn->in_snip = ntohl(nat->nat_odstaddr);
217 ipn->in_dnip = ntohl(nat->nat_osrcaddr);
218 } else {
219 #ifdef USE_INET6
220 ipn->in_snip6 = nat->nat_odst6;
221 ipn->in_dnip6 = nat->nat_osrc6;
222 #endif
223 }
224 ipn->in_ndstip6 = nat->nat_osrc6;
225 ipn->in_odstip6 = nat->nat_nsrc6;
226 }
227 ipn->in_odport = htons(fin->fin_sport);
228 ipn->in_ndport = htons(fin->fin_sport);
229
230 IP6_SETONES(&ipn->in_osrcmsk6);
231 IP6_SETONES(&ipn->in_nsrcmsk6);
232 IP6_SETONES(&ipn->in_odstmsk6);
233 IP6_SETONES(&ipn->in_ndstmsk6);
234 MUTEX_INIT(&ipn->in_lock, "tftp proxy NAT rule");
235
236 ipn->in_namelen = np->in_namelen;
237 bcopy(np->in_names, ipn->in_ifnames, ipn->in_namelen);
238 ipn->in_ifnames[0] = np->in_ifnames[0];
239 ipn->in_ifnames[1] = np->in_ifnames[1];
240
241 ti->ti_lastcmd = 0;
242
243 return (0);
244 }
245
246
247 void
ipf_p_tftp_del(ipf_main_softc_t * softc,ap_session_t * aps)248 ipf_p_tftp_del(ipf_main_softc_t *softc, ap_session_t *aps)
249 {
250 tftpinfo_t *tftp;
251
252 tftp = aps->aps_data;
253 if (tftp != NULL) {
254 tftp->ti_rule->in_flags |= IPN_DELETE;
255 ipf_nat_rule_deref(softc, &tftp->ti_rule);
256 }
257 }
258
259
260 /*
261 * Setup for a new TFTP proxy.
262 */
263 int
ipf_p_tftp_backchannel(fr_info_t * fin,ap_session_t * aps,nat_t * nat)264 ipf_p_tftp_backchannel(fr_info_t *fin, ap_session_t *aps, nat_t *nat)
265 {
266 ipf_main_softc_t *softc = fin->fin_main_soft;
267 #ifdef USE_MUTEXES
268 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
269 #endif
270 #ifdef USE_INET6
271 i6addr_t swip6, sw2ip6;
272 ip6_t *ip6;
273 #endif
274 struct in_addr swip, sw2ip;
275 tftpinfo_t *ti;
276 udphdr_t udp;
277 fr_info_t fi;
278 u_short slen = 0; /* silence gcc */
279 nat_t *nat2;
280 int nflags;
281 ip_t *ip;
282 int dir;
283
284 ti = aps->aps_data;
285 /*
286 * Add skeleton NAT entry for connection which will come back the
287 * other way.
288 */
289 bcopy((char *)fin, (char *)&fi, sizeof(fi));
290 fi.fin_flx |= FI_IGNORE;
291 fi.fin_data[1] = 0;
292
293 bzero((char *)&udp, sizeof(udp));
294 udp.uh_sport = 0; /* XXX - don't specify remote port */
295 udp.uh_dport = ti->ti_rule->in_ndport;
296 udp.uh_ulen = htons(sizeof(udp));
297 udp.uh_sum = 0;
298
299 fi.fin_fr = &tftpfr;
300 fi.fin_dp = (char *)&udp;
301 fi.fin_sport = 0;
302 fi.fin_dport = ntohs(ti->ti_rule->in_ndport);
303 fi.fin_dlen = sizeof(udp);
304 fi.fin_plen = fi.fin_hlen + sizeof(udp);
305 fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
306 nflags = NAT_SLAVE|IPN_UDP|SI_W_SPORT;
307 #ifdef USE_INET6
308 ip6 = (ip6_t *)fin->fin_ip;
309 #endif
310 ip = fin->fin_ip;
311 sw2ip.s_addr = 0;
312 swip.s_addr = 0;
313
314 fi.fin_src6 = nat->nat_ndst6;
315 fi.fin_dst6 = nat->nat_nsrc6;
316 if (nat->nat_v[0] == 4) {
317 slen = ip->ip_len;
318 ip->ip_len = htons(fin->fin_hlen + sizeof(udp));
319 swip = ip->ip_src;
320 sw2ip = ip->ip_dst;
321 ip->ip_src = nat->nat_ndstip;
322 ip->ip_dst = nat->nat_nsrcip;
323 } else {
324 #ifdef USE_INET6
325 slen = ip6->ip6_plen;
326 ip6->ip6_plen = htons(sizeof(udp));
327 swip6.in6 = ip6->ip6_src;
328 sw2ip6.in6 = ip6->ip6_dst;
329 ip6->ip6_src = nat->nat_ndst6.in6;
330 ip6->ip6_dst = nat->nat_nsrc6.in6;
331 #endif
332 }
333
334 if (nat->nat_dir == NAT_INBOUND) {
335 dir = NAT_OUTBOUND;
336 fi.fin_out = 1;
337 } else {
338 dir = NAT_INBOUND;
339 fi.fin_out = 0;
340 }
341 nflags |= NAT_NOTRULEPORT;
342
343 MUTEX_ENTER(&softn->ipf_nat_new);
344 #ifdef USE_INET6
345 if (nat->nat_v[0] == 6)
346 nat2 = ipf_nat6_add(&fi, ti->ti_rule, NULL, nflags, dir);
347 else
348 #endif
349 nat2 = ipf_nat_add(&fi, ti->ti_rule, NULL, nflags, dir);
350 MUTEX_EXIT(&softn->ipf_nat_new);
351 if (nat2 != NULL) {
352 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
353 ipf_nat_update(&fi, nat2);
354 fi.fin_ifp = NULL;
355 if (ti->ti_rule->in_redir == NAT_MAP) {
356 fi.fin_src6 = nat->nat_ndst6;
357 fi.fin_dst6 = nat->nat_nsrc6;
358 if (nat->nat_v[0] == 4) {
359 ip->ip_src = nat->nat_ndstip;
360 ip->ip_dst = nat->nat_nsrcip;
361 } else {
362 #ifdef USE_INET6
363 ip6->ip6_src = nat->nat_ndst6.in6;
364 ip6->ip6_dst = nat->nat_nsrc6.in6;
365 #endif
366 }
367 } else {
368 fi.fin_src6 = nat->nat_odst6;
369 fi.fin_dst6 = nat->nat_osrc6;
370 if (fin->fin_v == 4) {
371 ip->ip_src = nat->nat_odstip;
372 ip->ip_dst = nat->nat_osrcip;
373 } else {
374 #ifdef USE_INET6
375 ip6->ip6_src = nat->nat_odst6.in6;
376 ip6->ip6_dst = nat->nat_osrc6.in6;
377 #endif
378 }
379 }
380 if (ipf_state_add(softc, &fi, NULL, SI_W_SPORT) != 0) {
381 ipf_nat_setpending(softc, nat2);
382 }
383 }
384 if (nat->nat_v[0] == 4) {
385 ip->ip_len = slen;
386 ip->ip_src = swip;
387 ip->ip_dst = sw2ip;
388 } else {
389 #ifdef USE_INET6
390 ip6->ip6_plen = slen;
391 ip6->ip6_src = swip6.in6;
392 ip6->ip6_dst = sw2ip6.in6;
393 #endif
394 }
395 return (0);
396 }
397
398
399 int
ipf_p_tftp_client(ipf_tftp_softc_t * softt,fr_info_t * fin,ap_session_t * aps,nat_t * nat)400 ipf_p_tftp_client(ipf_tftp_softc_t *softt, fr_info_t *fin, ap_session_t *aps,
401 nat_t *nat)
402 {
403 u_char *msg, *s, *t;
404 tftpinfo_t *ti;
405 u_short opcode;
406 udphdr_t *udp;
407 int len;
408
409 if (fin->fin_dlen < 4)
410 return (0);
411
412 ti = aps->aps_data;
413 msg = fin->fin_dp;
414 msg += sizeof(udphdr_t);
415 opcode = (msg[0] << 8) | msg[1];
416 DT3(tftp_cmd, fr_info_t *, fin, int, opcode, nat_t *, nat);
417
418 switch (opcode)
419 {
420 case TFTP_CMD_WRITE :
421 if (softt->ipf_p_tftp_readonly != 0)
422 break;
423 /* FALLTHROUGH */
424 case TFTP_CMD_READ :
425 len = fin->fin_dlen - sizeof(*udp) - 2;
426 if (len > sizeof(ti->ti_filename) - 1)
427 len = sizeof(ti->ti_filename) - 1;
428 s = msg + 2;
429 for (t = (u_char *)ti->ti_filename; (len > 0); len--, s++) {
430 *t++ = *s;
431 if (*s == '\0')
432 break;
433 }
434 ipf_p_tftp_backchannel(fin, aps, nat);
435 break;
436 default :
437 return (-1);
438 }
439
440 ti = aps->aps_data;
441 ti->ti_lastcmd = opcode;
442 return (0);
443 }
444
445
446 int
ipf_p_tftp_server(ipf_tftp_softc_t * softt,fr_info_t * fin,ap_session_t * aps,nat_t * nat)447 ipf_p_tftp_server(ipf_tftp_softc_t *softt, fr_info_t *fin, ap_session_t *aps,
448 nat_t *nat)
449 {
450 tftpinfo_t *ti;
451 u_short opcode;
452 u_short arg;
453 u_char *msg;
454
455 if (fin->fin_dlen < 4)
456 return (0);
457
458 ti = aps->aps_data;
459 msg = fin->fin_dp;
460 msg += sizeof(udphdr_t);
461 arg = (msg[2] << 8) | msg[3];
462 opcode = (msg[0] << 8) | msg[1];
463
464 switch (opcode)
465 {
466 case TFTP_CMD_ACK :
467 ti->ti_lastblk = arg;
468 break;
469
470 case TFTP_CMD_ERROR :
471 ti->ti_lasterror = arg;
472 break;
473
474 default :
475 return (-1);
476 }
477
478 ti->ti_lastcmd = opcode;
479 return (0);
480 }
481