File: nat.c

package info (click to toggle)
lwipv6 1.5a-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 3,552 kB
  • ctags: 4,872
  • sloc: ansic: 36,455; sh: 11,022; makefile: 115
file content (1383 lines) | stat: -rw-r--r-- 36,527 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
/*   This is part of LWIPv6
 *   Developed for the Ale4NET project
 *   Application Level Environment for Networking
 *   
 *   Copyright 2004 Diego Billi - Italy
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */ 
#include "lwip/opt.h"

#if LWIP_USERFILTER && LWIP_NAT

#include "lwip/debug.h"
#include "lwip/sys.h"
#include "lwip/stats.h"
#include "lwip/memp.h" /* MEMP_NAT_RULE */

#include "lwip/inet.h"
#include "lwip/ip.h"
#include "lwip/ip_frag.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include "lwip/icmp.h"

#include "lwip/sockets.h"
#include "lwip/if.h"

#include "lwip/netif.h"
#include "lwip/userfilter.h"

#include "lwip/nat/nat.h"
#include "lwip/nat/nat_tables.h"


#ifndef NAT_DEBUG
#define NAT_DEBUG   DBG_OFF
#endif

/*--------------------------------------------------------------------------*/

uf_verdict_t  nat_defrag  (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif);

uf_verdict_t  nat_track   (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif);
uf_verdict_t  nat_perform (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif);
uf_verdict_t  nat_confirm (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif);

#if 0
struct uf_hook_handler   nat_prerouting_defrag =
{
	.hooknum  = UF_IP_PRE_ROUTING,
	.hook     = nat_defrag,
	.priority = UF_PRI_NAT_PREROUTING_DEFRAG
};
#endif

struct uf_hook_handler   nat_prerouting_track =
{
	.hooknum  = UF_IP_PRE_ROUTING,
	.hook     = nat_track,
	.priority = UF_PRI_NAT_PREROUTING_TRACK
};

struct uf_hook_handler   nat_prerouting_dnat =
{
	.hooknum  = UF_IP_PRE_ROUTING,
	.hook     = nat_perform,
	.priority = UF_PRI_NAT_PREROUTING_DNAT
};

struct uf_hook_handler   nat_input_confirm =
{
	.hooknum  = UF_IP_LOCAL_IN,
	.hook     = nat_confirm,
	.priority = UF_PRI_NAT_INPUT_CONFIRM
};

struct uf_hook_handler   nat_output_track =
{
	.hooknum  = UF_IP_LOCAL_OUT,
	.hook     = nat_track,
	.priority = UF_PRI_NAT_OUTPUT_TRACK
};

struct uf_hook_handler   nat_postrouting_snat =
{
	.hooknum  = UF_IP_POST_ROUTING,
	.hook     = nat_perform,
	.priority = UF_PRI_NAT_POSTROUTING_SNAT
};

struct uf_hook_handler   nat_postrouting_confirm =
{
	.hooknum  = UF_IP_POST_ROUTING,
	.hook     = nat_confirm,
	.priority = UF_PRI_NAT_POSTROUTING_CONFIRM
};

/*--------------------------------------------------------------------------*/
/* Nat PCBS */
/*--------------------------------------------------------------------------*/

/* it seems useless: rd20100730 */
/*sys_sem_t unique_mutex;*/


#define LOCK(sem)         sys_sem_wait_timeout((sem), 0)
#define UNLOCK(sem)       sys_sem_signal((sem))

#define NAT_LOCK(stack)        LOCK(stack->stack_nat->nat_mutex)
#define NAT_UNLOCK(stack)      UNLOCK(stack->stack_nat->nat_mutex)

#define NAT_PCB_REG(stack, pcbs_list, npcb) \
	do { \
		NAT_LOCK(stack); \
		npcb->next = *pcbs_list; \
		*(pcbs_list) = npcb; \
		NAT_UNLOCK(stack); \
	} while(0)

#define NAT_PCB_RMV(stack, pcbs_list, npcb) \
	do { \
		struct nat_pcb *___tmp; \
		NAT_LOCK(stack); \
		if(*(pcbs_list) == npcb) { \
			(*(pcbs_list)) = (*pcbs_list)->next; \
		} else \
			for(___tmp = *pcbs_list; ___tmp != NULL; ___tmp = ___tmp->next) { \
				if((___tmp->next != NULL) && (___tmp->next == npcb)) { \
					___tmp->next = npcb->next; \
					break; \
				} \
			} \
		npcb->next = NULL; \
		NAT_UNLOCK(stack); \
	} while(0)

/*--------------------------------------------------------------------------*/

#define MAX_TRACK_PROTO 256

static struct track_protocol *ip_ct_protos[MAX_TRACK_PROTO];

struct track_protocol * track_proto_find(u8_t protocol)
{
	return ip_ct_protos[protocol];
}

/*--------------------------------------------------------------------------*/

int nat_init(struct stack *stack)
{
	int i;

	/* global mapping, initialized once */
	if (ip_ct_protos[0] == NULL) {
		for (i = 0; i < MAX_TRACK_PROTO; i++)
			ip_ct_protos[i] = &default_track;
		ip_ct_protos[IP_PROTO_TCP]   = &tcp_track;
		ip_ct_protos[IP_PROTO_UDP]   = &udp_track;
		ip_ct_protos[IP_PROTO_ICMP4] = &icmp4_track;
		ip_ct_protos[IP_PROTO_ICMP]  = &icmp6_track;
	}

	/* Register hooks */

	stack->stack_nat = mem_malloc(sizeof(struct stack_nat));
	if (stack->stack_nat == NULL)
		return -ENOMEM;

	stack->stack_nat->nat_mutex    = sys_sem_new(1);
	/*unique_mutex = sys_sem_new(1);*/

	// FIX: remove this and bind ip/port in the stack
	nat_ports_init(stack);

	// Init rules lists
	stack->stack_nat->nat_in_rules  = NULL;
	stack->stack_nat->nat_out_rules = NULL;

	// Init pcbs lists
	stack->stack_nat->nat_active_pcbs    = NULL; 
	stack->stack_nat->nat_tentative_pcbs = NULL;

	/* Set protocol handlers */

	ip_conntrack_protocol_tcp_lockinit(stack);

	//uf_register_hook( & nat_prerouting_defrag);
	uf_register_hook(stack, & nat_prerouting_track);
	uf_register_hook(stack, & nat_prerouting_dnat);
	uf_register_hook(stack, & nat_input_confirm);
	uf_register_hook(stack, & nat_output_track);
	uf_register_hook(stack, & nat_postrouting_snat);
	uf_register_hook(stack, & nat_postrouting_confirm);

	LWIP_DEBUGF(NAT_DEBUG, ("%s: registered NAT hooks!\n", __func__));
	
	return ERR_OK;
}

void nat_free_pcb(struct nat_pcb *pcb);
int nat_shutdown(struct stack *stack)
{
	struct nat_pcb *nat_pcb_tmp; 
	if (stack->stack_nat == NULL)
		return -EINVAL;

	nat_rules_shutdown(stack);
	while (stack->stack_nat->nat_tentative_pcbs != NULL) {
		nat_pcb_tmp = stack->stack_nat->nat_tentative_pcbs;
		NAT_PCB_RMV(stack, &stack->stack_nat->nat_tentative_pcbs, nat_pcb_tmp);
		nat_free_pcb(nat_pcb_tmp);
	}
	while (stack->stack_nat->nat_active_pcbs != NULL) {
		nat_pcb_tmp = stack->stack_nat->nat_active_pcbs;
		NAT_PCB_RMV(stack, &stack->stack_nat->nat_active_pcbs, nat_pcb_tmp);
		nat_free_pcb(nat_pcb_tmp);
	}
	mem_free(stack->stack_nat);
	return ERR_OK;
}

/*--------------------------------------------------------------------------*/

// assuming: unsigned char is 8 bits, long is 32 bits.
//	- chksum points to the chksum in the packet 
//	- optr points to the old data in the packet
//	- nptr points to the new data in the packet
void nat_chksum_adjust(u8_t * chksum, u8_t * optr, s16_t olen, u8_t * nptr, s16_t nlen)
{
	s32_t x, old, new;

	x = chksum[0] * 256 + chksum[1];
	x = ~x & 0xFFFF;
	while (olen) {
		old = optr[0] * 256 + optr[1];
		optr += 2;
		x -= old & 0xffff;
		if (x <= 0) {
			x--;
			x &= 0xffff;
		}
		olen -= 2;
	}
	while (nlen) {
		new = nptr[0] * 256 + nptr[1];
		nptr += 2;
		x += new & 0xffff;
		if (x & 0x10000) {
			x++;
			x &= 0xffff;
		}
		nlen -= 2;
	}
	x = ~x & 0xFFFF;
	chksum[0] = x / 256;
	chksum[1] = x & 0xff;
}

/*--------------------------------------------------------------------------*/
// NAT session descriptors 
/*--------------------------------------------------------------------------*/

static unsigned int pcb_id = 0;

/* Return a new session descriptor. Returns NULL on error */
struct nat_pcb *nat_new_pcb(void)
{
	struct nat_pcb *pcb;

	pcb = memp_malloc(MEMP_NAT_PCB);
	if (pcb != NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: get new! %p\n", __func__, pcb));
		bzero(pcb, sizeof(struct nat_pcb));
		pcb->nat_type = NAT_NONE;
		pcb->id       = pcb_id++;
	}
	return pcb;
}

void nat_free_pcb(struct nat_pcb *pcb)
{
	LWIP_DEBUGF(NAT_DEBUG, ("%s: free %p (id=%d)\n", __func__, pcb, pcb->id));
	memp_free(MEMP_NAT_PCB, pcb);
}

/*--------------------------------------------------------------------------*/

void nat_session_get(struct nat_pcb *pcb);
void nat_session_put(struct nat_pcb *pcb);

void nat_pbuf_init(struct pbuf *p)
{
	p->nat.track = NULL;
	p->nat.dir   = 0;
	p->nat.info  = 0;
}

void nat_pbuf_get(struct pbuf *p)
{
}

void nat_pbuf_clone(struct pbuf *r, struct pbuf *p)
{
	if (p->nat.track == NULL) 
		return;

	nat_session_get(p->nat.track);

	r->nat.track = p->nat.track;
	r->nat.dir   = p->nat.dir;
	r->nat.info  = p->nat.info;
}

void nat_pbuf_put(struct pbuf *p)
{
	if (p->nat.track == NULL)
		return;

	nat_session_put(p->nat.track);

	p->nat.track = NULL;
	p->nat.dir   = 0;
	p->nat.info  = 0;
}

void nat_pbuf_reset(struct pbuf *p)
{
	nat_pbuf_put(p);
}

/*--------------------------------------------------------------------------*/
// Tuples
/*--------------------------------------------------------------------------*/

#ifdef LWIP_DEBUG
/* Print a tuple on output for debug messages */
void dump_tuple (struct ip_tuple *tuple)
{
	LWIP_DEBUGF(NAT_DEBUG, ("TUPLE: ")); 
	ip_addr_debug_print(NAT_DEBUG, &tuple->src.ip); LWIP_DEBUGF(NAT_DEBUG, ("  ")); 
	ip_addr_debug_print(NAT_DEBUG, &tuple->dst.ip); LWIP_DEBUGF(NAT_DEBUG, ("  ")); 

	switch (tuple->src.proto.protonum)
	{
		case IP_PROTO_TCP:
			LWIP_DEBUGF(NAT_DEBUG, ("TCP [%d,%d]", ntohs(tuple->src.proto.upi.tcp.port), 
				ntohs(tuple->dst.proto.upi.tcp.port))); 
			break;
		case IP_PROTO_UDP:
			LWIP_DEBUGF(NAT_DEBUG, ("UDP [%d,%d]", ntohs(tuple->src.proto.upi.udp.port), 
				ntohs(tuple->dst.proto.upi.udp.port))); 
			break;
		case IP_PROTO_ICMP4:
			LWIP_DEBUGF(NAT_DEBUG, ("Icmp4 id=%d type=%d code=%d", 
				ntohs(tuple->src.proto.upi.icmp4.id), 
				tuple->src.proto.upi.icmp4.type, 
				tuple->src.proto.upi.icmp4.code)); 
			break;
		case IP_PROTO_ICMP:
			LWIP_DEBUGF(NAT_DEBUG, ("Icmp6 id=%d (%x) type=%d code=%d", 
				ntohs(tuple->src.proto.upi.icmp6.id), 
				ntohs(tuple->src.proto.upi.icmp6.id), 
				tuple->src.proto.upi.icmp6.type, 
				tuple->src.proto.upi.icmp6.code)); 
			break;
		default:
			LWIP_DEBUGF(NAT_DEBUG, ("%s: strange protocol", __func__ ));
			break;
	}
	LWIP_DEBUGF(NAT_DEBUG, ("\n"));
}
#else
#define dump_tuple(p) {}
#endif

/*--------------------------------------------------------------------------*/

int get_masquerade_ip(struct ip_addr *ip, u8_t ipv, struct netif *netif)
{
	struct ip_addr_list *addr;

	addr = ip_addr_list_masquarade_addr(netif->addrs, ipv);
	if (addr != NULL) {
		ip_addr_set(ip, &addr->ipaddr);
		return 1;
	}

	return -1;
}

/* Create a unique tuple for NAT */
int tuple_create_nat_inverse(struct ip_tuple *reply, struct ip_tuple *tuple, 
	struct netif *iface, nat_type_t type, struct manip_range *nat_manip) 
{
	int r;
	struct track_protocol *proto;

	/* Set IP */

	reply->ipv = tuple->ipv;

	if (type == NAT_MASQUERADE) {
		if (get_masquerade_ip(&reply->dst.ip, reply->ipv, iface) < 0)
			return -1;
	}
	else
	if (type == NAT_SNAT) {
		ip_addr_set (&reply->dst.ip, &nat_manip->ipmin);
	}
	else
	if (type == NAT_DNAT) {
		ip_addr_set (&reply->src.ip, &nat_manip->ipmin);
	}
	else {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: BUG\n", __func__ ));
		return -1;
	}

	/* Set PROTO */

	reply->src.proto.protonum = tuple->src.proto.protonum;

	proto = track_proto_find( tuple->src.proto.protonum );
	if (proto == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: BUG 2 \n", __func__ ));
		return -1;
	}
	
	r = proto->nat_tuple_inverse(iface->stack, reply, tuple, type, nat_manip);

	return r;
}

/*--------------------------------------------------------------------------*/

/* Returns 1 if t2 and t1 are equal. */
int nat_tuple_cmp(struct ip_tuple *t1, struct ip_tuple *t2)
{
	int r;

	/* FIX: too simple */
	r = memcmp(t1, t2, sizeof(struct ip_tuple));

	if ( r == 0) return 1;
	else         return 0;
}

int tuple_inverse(struct ip_tuple *reply, struct ip_tuple *tuple) 
{
	struct track_protocol *proto;

	bzero(reply, sizeof(struct ip_tuple));

	reply->ipv = tuple->ipv;
	ip_addr_set (&reply->src.ip, &tuple->dst.ip);
	ip_addr_set (&reply->dst.ip, &tuple->src.ip);

	reply->src.proto.protonum = tuple->src.proto.protonum;
	proto = track_proto_find( tuple->src.proto.protonum );

	return proto->inverse(reply, tuple);
}

int tuple_create(struct ip_tuple *tuple, char  *iphdr, struct track_protocol *proto) 
{
	struct ip_hdr *ip6hdr;
	struct ip4_hdr *ip4hdr;
	u32_t  iphdrlen;

	bzero(tuple, sizeof(struct ip_tuple));

	ip6hdr = (struct ip_hdr *) iphdr;
	ip4hdr = (struct ip4_hdr *) iphdr;
	tuple->ipv = IPH_V(ip6hdr);
	if (tuple->ipv == 6) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: ipv6 \n", __func__ ));
		ip_addr_set (&tuple->src.ip , &(ip6hdr->src)  );
		ip_addr_set (&tuple->dst.ip , &(ip6hdr->dest) );

		tuple->src.proto.protonum = IPH_NEXTHDR(ip6hdr); 

		iphdrlen = IP_HLEN;
	}
	else if (tuple->ipv == 4) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: ipv4 \n", __func__ ));
		IP64_CONV (&tuple->src.ip , &(ip4hdr->src)  );
		IP64_CONV (&tuple->dst.ip , &(ip4hdr->dest) );

		tuple->src.proto.protonum = IPH4_PROTO(ip4hdr);

		iphdrlen = IPH4_HL(ip4hdr) * 4;
	} else {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: IP version wrong \n", __func__ ));
		return -1; /* error */
	}

	return proto->tuple(tuple, iphdr + iphdrlen);
}

/*--------------------------------------------------------------------------*/
// NAT timer functions */
/*--------------------------------------------------------------------------*/

struct nat_pcb * nat_create_session(nat_type_t nat_type, 
	struct nat_pcb *pcb, struct netif *iface, struct manip_range *manip)
{
	LWIP_DEBUGF(NAT_DEBUG, ("\tnat=%s iface=%d \n",STR_NATNAME(nat_type), iface->id ));

	pcb->nat_type = nat_type;

	if (pcb->nat_type == NAT_MASQUERADE) {
		pcb->status   |= TS_MASQUERADE;
		pcb->nat_type  = NAT_SNAT;
		pcb->iface    = iface;
	}

	/* Setup inverse natted tuple 
	   NOTE: pcb->tuple[CONN_DIR_ORIGINAL ]is already set
	*/
	if (tuple_create_nat_inverse(&pcb->tuple[CONN_DIR_REPLY], 
		&pcb->tuple[CONN_DIR_ORIGINAL], iface, nat_type, manip ) < 0)
		return NULL;

	LWIP_DEBUGF(NAT_DEBUG, ("\tinverse="));	dump_tuple (&pcb->tuple[CONN_DIR_REPLY]);
	LWIP_DEBUGF(NAT_DEBUG, ("\n"));

	return pcb;
}

void nat_session_get(struct nat_pcb *pcb)
{
	pcb->refcount++;
}

void nat_session_put(struct nat_pcb *pcb)
{
	struct track_protocol *proto;
	struct stack *stack = pcb->stack;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: pcb id=%d ref=%d\n", __func__, pcb->id, (int)pcb->refcount));
	pcb->refcount--;
	if (pcb->refcount == 0)  {
		LWIP_DEBUGF(NAT_DEBUG, ("\tid=%d ref now = 0 -> FREE\n", pcb->id));

		/* The tracking not confirmed this connection, remove it */
		if (!(pcb->status & TS_CONFIRMED)) {
			NAT_PCB_RMV(stack, &stack->stack_nat->nat_tentative_pcbs, pcb);
		} 

		// Need to unbind() used ports for SNAT
		if (pcb->nat_type != NAT_NONE) {

			proto = track_proto_find( pcb->tuple[CONN_DIR_ORIGINAL].src.proto.protonum );
			if (proto != NULL) {
				proto->nat_free(pcb);
			}
			else
				LWIP_DEBUGF(NAT_DEBUG, ("%s: BUG 2 \n", __func__ ));
		}

		nat_free_pcb(pcb);
	}
}

/*--------------------------------------------------------------------------*/
// Tracking
/*--------------------------------------------------------------------------*/

void close_timeout(void *arg)
{
	struct nat_pcb *pcb = (struct nat_pcb *) arg;
	struct stack *stack = pcb->stack;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: session p=%p id=%d expired\n", __func__, pcb, pcb->id));
	LWIP_DEBUGF(NAT_DEBUG, ("\t")); dump_tuple(&pcb->tuple[CONN_DIR_ORIGINAL]); 

	NAT_PCB_RMV(stack, &stack->stack_nat->nat_active_pcbs, pcb);

	nat_session_put(pcb);
}

void conn_refresh_timer(u32_t timeout, struct nat_pcb *pcb)
{
	pcb->timeout = timeout;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: refresh on pcb id=%d\n", __func__, pcb->id));
	if (!conn_remove_timer(pcb)) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: No timer\n", __func__));
		return ;
	}
	
	sys_timeout(timeout, (sys_timeout_handler) close_timeout, pcb);
}

int conn_remove_timer(struct nat_pcb *pcb)
{
	return sys_untimeout_and_check((sys_timeout_handler)close_timeout, pcb);
}

void conn_force_timeout(struct nat_pcb *pcb)
{
	close_timeout(pcb);
}

int new_track(struct stack *stack, struct nat_pcb **newpcb, uf_hook_t hook, 
	struct pbuf **q, 
	struct ip_tuple * tuple, conn_dir_t *direction, struct track_protocol *proto)
{
	struct ip_hdr *ip6hdr;
	struct ip4_hdr *ip4hdr;
	u16_t iphdrlen;
	struct nat_pcb *pcb;
	struct pbuf *p=*q;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: start\n", __func__));

	pcb = nat_new_pcb();
	if (pcb == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: NAT PCB memory full.\n", __func__));
		return -1;
	}

	/* 
	 * Save ORIGINAL and REPLY connection tuple. 
	 */
	memcpy(&pcb->tuple[CONN_DIR_ORIGINAL], tuple, sizeof (struct ip_tuple));

	if (tuple_inverse(&pcb->tuple[CONN_DIR_REPLY], 
		&pcb->tuple[CONN_DIR_ORIGINAL]) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: Unable to get inverse tuple\n", __func__));
        	return -1;
	}

	LWIP_DEBUGF(NAT_DEBUG, ("%s: New track. id=%d\n", __func__, pcb->id));
	LWIP_DEBUGF(NAT_DEBUG, ("\tORIGINAL: ")); dump_tuple ( &pcb->tuple[CONN_DIR_ORIGINAL] ); 
	LWIP_DEBUGF(NAT_DEBUG, ("\tREPLY   : ")); dump_tuple ( &pcb->tuple[CONN_DIR_REPLY] ); 

	/* Init per-protocol informations */
	ip4hdr = (struct ip4_hdr *) p->payload;
	ip6hdr = (struct ip_hdr *) p->payload;
	if (tuple->ipv == 6)      
		iphdrlen = IP_HLEN;
	else if (tuple->ipv == 4) 
		iphdrlen = IPH4_HL(ip4hdr) * 4;
	else
		return -1;

	if (proto->new(stack, pcb, p, p->payload, iphdrlen) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: Unable to create new valid tracking.\n", __func__));

		nat_free_pcb(pcb);
		return -1;
	}

	*direction = CONN_DIR_ORIGINAL;
	*newpcb    = pcb;

	pcb->stack = stack;
	pcb->refcount = 1;

	pcb->next = NULL;
	/* Register this connection as a Tentative */
	NAT_PCB_REG(stack, &stack->stack_nat->nat_tentative_pcbs, pcb);

	return 1;
}

struct nat_pcb * conn_find_track(struct stack *stack, conn_dir_t *direction, struct ip_tuple * tuple )
{
	struct nat_pcb *pcb = NULL;

	/* Search in the table */
	NAT_LOCK(stack);
	for(pcb = stack->stack_nat->nat_active_pcbs; pcb != NULL; pcb = pcb->next)  {

		LWIP_DEBUGF(NAT_DEBUG, ("\tpcb=%p\tORIGINAL: ", pcb)); 
		dump_tuple(&pcb->tuple[CONN_DIR_ORIGINAL]);
		LWIP_DEBUGF(NAT_DEBUG, ("\t\t\tREPLY   : ")); 
		dump_tuple(&pcb->tuple[CONN_DIR_REPLY]);
		LWIP_DEBUGF(NAT_DEBUG, ("\n"));

		if (nat_tuple_cmp(&pcb->tuple[CONN_DIR_ORIGINAL], tuple)) {
			*direction = CONN_DIR_ORIGINAL;
			break;
		}
		if (nat_tuple_cmp(&pcb->tuple[CONN_DIR_REPLY]   , tuple)) {
			*direction = CONN_DIR_REPLY;
			break;
		}
	}

	if (pcb != NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("\tFOUND pcb id=%d\n", pcb->id)); 
		pcb->refcount++;
	}

	NAT_UNLOCK(stack);

	return pcb;
}

/*
 * Try to track the packet. If this packet doesn't belong to any existing connection
 * a new one will be created. On errors return -1.
 */
int conn_track(struct stack *stack, conn_dir_t *direction, uf_hook_t hook, 
	struct pbuf **q, struct netif *inif, struct netif *outif, struct track_protocol *proto)
{
	struct pbuf *p = * q;
	struct nat_pcb *tmppcb = NULL;
	struct ip_tuple tuple;  

	LWIP_DEBUGF(NAT_DEBUG, ("%s: start\n", __func__ ));

	/* Get the tuple of the packet */
///	if (tuple_create(&tuple, p, proto) < 0) {
	if (tuple_create(&tuple, p->payload, proto) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: unable to create tuple!\n", __func__ ));
		return -1;
	}

	dump_tuple (&tuple); 

	/* Find tracking informations */	
	tmppcb = conn_find_track(stack, direction, &tuple);
	if (tmppcb == NULL) {
		if (new_track(stack, &tmppcb, hook, q, &tuple, direction, proto) < 0) {
			LWIP_DEBUGF(NAT_DEBUG, ("%s: unable to create new track \n", __func__ ));
			return -1;
		}
		LWIP_DEBUGF(NAT_DEBUG, ("%s: NEW track %p id=%d!!\n", __func__, tmppcb, tmppcb->id));
	}

	p->nat.track = tmppcb;
	p->nat.dir   = *direction;

	/* It exists; we have (non-exclusive) reference. */
	if (*direction == CONN_DIR_REPLY) {
		p->nat.info = CT_ESTABLISHED + CT_IS_REPLY;
		LWIP_DEBUGF(NAT_DEBUG, ("%s: CT_ESTABLISHED + CT_IS_REPLY\n", __func__));
	} else {
		/* Once we've had two way comms, always ESTABLISHED. */
		if (p->nat.track->status & TS_SEEN_REPLY) {
			LWIP_DEBUGF(NAT_DEBUG, ("%s: CT_ESTABLISHED\n", __func__));
			p->nat.info = CT_ESTABLISHED;
		} else {
			LWIP_DEBUGF(NAT_DEBUG, ("%s: CT_NEW\n", __func__));
			p->nat.info = CT_NEW;
		}
	}

	return 1;
}


/* Returns 1 if 'p' is a valid packet for tracking and NAT.
   Some ICMP6 packet (NS,ND, RA,RD) don't need tracking or NAT */
int conn_need_track(struct pbuf *p)
{
	struct ip_hdr *ip6hdr;
	struct ip4_hdr *ip4hdr;
	u32_t  iphdrlen;
	struct ip_addr src,dst;

	ip6hdr = (struct ip_hdr *) p->payload;
	ip4hdr = (struct ip4_hdr *) p->payload;
	if (IPH_V(ip6hdr) == 6) {
		iphdrlen = IP_HLEN;
		if (ip_addr_ismulticast(&ip6hdr->dest)) 
			return 0;
		if (ip_addr_ismulticast(&ip6hdr->src)) 
			return 0;

		if (ip_addr_islinkscope(&ip6hdr->dest))
			return 0;
		if (ip_addr_islinkscope(&ip6hdr->src))
			return 0;
	}
	else if (IPH_V(ip6hdr) == 4) {
		iphdrlen = IPH4_HL(ip4hdr) * 4;

		IP64_CONV (&src , &(ip4hdr->src)  );
		IP64_CONV (&dst , &(ip4hdr->dest) );

		if (ip_addr_is_v4multicast(&dst)) 
			return 0;
	}

	return 1;
}

/*--------------------------------------------------------------------------*/
/* HOOKS */
/*--------------------------------------------------------------------------*/

#if 0
uf_verdict_t  nat_defrag  (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif)
{
	struct pbuf *p = *q;
	struct ip4_hdr *ip4hdr;
	struct ip_hdr  *ip6hdr;
	u8_t proto;
	uf_verdict_t ret = UF_ACCEPT;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: start\n", __func__));

	/* ASSERT! This is the first hook, no tracking info yet! */
	if (p->nat.track != NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s:  pcb not NULL!!!!!\n", __func__));
	}

	nat_pbuf_reset(p);


	/* Find transport protocol handler */
	ip6hdr = (struct ip_hdr *) p->payload;
	ip4hdr = (struct ip4_hdr *) p->payload;

	if (IPH_V(ip6hdr) == 4) {
	
		if ((IPH4_OFFSET(ip4hdr) & htons(IP_OFFMASK | IP_MF)) != 0) 
		{
#ifdef IPv4_FRAGMENTATION 
			LWIP_DEBUGF(NAT_DEBUG, ("%s: Packet is a fragment (id=0x%04x tot_len=%u len=%u MF=%u offset=%u)\n", __func__, 
				ntohs(IPH4_ID(ip4hdr)), p->tot_len, ntohs(IPH4_LEN(ip4hdr)), !!(IPH4_OFFSET(ip4hdr) & htons(IP_MF)), (ntohs(IPH4_OFFSET(ip4hdr)) & IP_OFFMASK)*8));

			p = ip4_reass(p);
#else
			ret = UF_DROP;
#endif
		} 
	}
	else 
	if (IPH_V(ip6hdr) == 6) {

		proto = IPH_NEXTHDR(ip6hdr);

		if (proto == IP6_NEXTHDR_FRAGMENT) {
#ifdef IPv6_FRAGMENTATION 
		        LWIP_DEBUGF(NAT_DEBUG, ("%s: Fragment Header\n", __func__));

		        struct ip6_fraghdr *fhdr = (struct ip6_fraghdr *) (p->payload + IP_HLEN);

		        p = ip6_reass(p, fhdr, NULL); 
#else
			ret = UF_DROP;
#endif
		}
	}
	else
		return UF_DROP;

	if (ret == UF_DROP) {
		LWIP_DEBUGF(NAT_DEBUG | 2, ("%s: IP packet dropped since it was fragmented\n",  __func__));
		IP_STATS_INC(ip.opterr);
		IP_STATS_INC(ip.drop);
		return UF_DROP;
	}

	if (p == NULL) {
		LWIP_DEBUGF(NAT_DEBUG,("%s: packet cached\n", __func__));
		return UF_STOLEN;
	}

	*q = p;

	return UF_ACCEPT;
}
#endif


uf_verdict_t nat_track (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif)
{
	struct pbuf *p = *q;
	struct pbuf *new_p;
	struct ip4_hdr *ip4hdr;
	struct ip_hdr  *ip6hdr;

	struct track_protocol *proto;

	conn_dir_t direction;
	uf_verdict_t verdict;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: pbuf %d/%d\n", __func__, p->len, p->tot_len));

	/* ASSERT! This is the first hook, no tracking info yet! */
	if (p->nat.track != NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s:  pcb not NULL!!!!!\n", __func__));
	}

	nat_pbuf_reset(p);

	/*
	 *  We need a not-memory-fragmented packet for NAT manipulation.
	 */
	new_p = pbuf_make_writable(p);
	if (new_p == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s:  unable to realloc pbuf!!!!!\n", __func__));
		return UF_ACCEPT;
	}

	LWIP_DEBUGF(NAT_DEBUG, ("%s: p now writable (old=%p new=%p)\n", __func__, p, new_p));

	*q = p = new_p;

	/* Find transport protocol handler */
	ip6hdr = (struct ip_hdr *) p->payload;
	ip4hdr = (struct ip4_hdr *) p->payload;
	if (IPH_V(ip6hdr) == 6) 
		// FIX: handle IPv6 extension headers ?
		proto = track_proto_find( IPH_NEXTHDR(ip6hdr) );
	else if (IPH_V(ip6hdr) == 4) 
		proto = track_proto_find(  IPH4_PROTO(ip4hdr) );
	else
		return UF_DROP;

	LWIP_DEBUGF(NAT_DEBUG, ("%s: proto = %d\n", __func__, IPH4_PROTO(ip4hdr) ));

	if (proto->error != NULL) {
		if (proto->error(stack, &verdict, p) < 0) {
			LWIP_DEBUGF(NAT_DEBUG, ("%s: proto error() -> %s\n", __func__, STR_VERDICT(verdict) ));
			return verdict;				
		}
	}

	if (!conn_need_track(p)) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: doesn't need track\n", __func__ ));
		return UF_ACCEPT;				
	}

	/* Find connection, if none is found a new will be created */	
	if (conn_track(stack, &direction, hooknum, q, inif, outif, proto ) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: tracking failed (not new valid connection)! DROP\n", __func__ ));
		return UF_DROP;
	}

	/* Update tracking informations */
	if (proto->handle(stack, &verdict, p, direction) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: drop this packet!!\n", __func__));
		/* Invalid: inverse of the return code tells
		 * the netfilter core what to do*/
		nat_session_put(p->nat.track);
		p->nat.track = NULL;
		return verdict;
	}

	if (direction == CONN_DIR_REPLY)
		p->nat.track->status |= TS_SEEN_REPLY;


	LWIP_DEBUGF(NAT_DEBUG, ("%s: pcb %p, dir=%s -> %s!!\n", __func__, 
		p->nat.track, STR_DIRECTIONNAME(direction), STR_VERDICT(verdict)));

	return verdict;
}

uf_verdict_t  nat_confirm (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif)
{
	struct nat_pcb *pcb = NULL;
	struct pbuf *p = *q;

	pcb = p->nat.track;
	if (pcb == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: no track for this packet!!\n", __func__));
		return UF_ACCEPT;
	}

	if (! (pcb->status & TS_CONFIRMED)) {

		/// FIX: controllare che non ci sia gia' un'altra connessione valida
		/// con le stesse tuple. Se e' presente vuol dire che:
		///  a) Il NAT ha modificato una connessione usando una tupla gia' occupata
		///     Una connessione proveniente dal LOCALOUT e' uguale ad una modificata in
		///     SNAT.

		pcb->status |= TS_CONFIRMED;

		pcb->refcount++;
		sys_timeout(pcb->timeout, (sys_timeout_handler) close_timeout, pcb);

		NAT_PCB_RMV(stack, &stack->stack_nat->nat_tentative_pcbs, pcb);
		NAT_PCB_REG(stack, &stack->stack_nat->nat_active_pcbs, pcb);

		LWIP_DEBUGF(NAT_DEBUG, ("%s: confirming track %p id=%d!!\n", __func__, pcb, pcb->id));
	}

	return UF_ACCEPT;
}

/* Return -1 on error */
int  nat_check_rule(struct nat_pcb *pcb, uf_hook_t hooknum, struct netif *inif,struct netif *outif)
{
	struct nat_rule * list, *rule;
	struct netif    * netif = NULL;
	struct manip_range *nat_manip;
	nat_type_t      type;
	struct stack *stack = pcb->stack;

	if (hooknum == UF_IP_PRE_ROUTING) {
		list  = stack->stack_nat->nat_in_rules;
		netif = inif;
	} 
	else if (hooknum == UF_IP_POST_ROUTING) {
		list  = stack->stack_nat->nat_out_rules;
		netif = outif;
	}
	else {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: wrong hooknum!\n",__func__));
		return -1;
	}

	/* Search for rules */
	for(rule = list; rule != NULL; rule = rule->next) {
		if (nat_match_rule(&rule->matches, netif, &pcb->tuple[CONN_DIR_ORIGINAL])) 
			break; 
	}

	if (rule == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: no rule found\n",__func__));
		return 1;
	}
	else {
		type = rule->type;
		nat_manip = &rule->manip;
	}

	/* We try only SNAT or DNAT, not both */
	pcb->status |= TS_NAT_TRY_MASK;

	if (nat_create_session(type, pcb, netif, nat_manip) == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: unable to create session with rule\n",__func__));
		return -1;
	}

	return 1;
}

void nat_modify_ip(nat_manip_t nat, char *p, struct ip_tuple *inverse, u8_t manip_innerproto)
{
	struct ip_hdr  *iphdr;
	struct ip4_hdr *ip4hdr;
	u16_t iphdrlen;
	struct track_protocol *proto;

	struct ip4_addr tmp_ip4;

	u32_t          old_ip4_addr;
	struct ip_addr old_ip6_addr;

	u8_t  *iphdr_old_changed_buf;
	u8_t  *iphdr_new_changed_buf;
	u32_t  iphdr_changed_buflen;

	/* Modify IP header */

	iphdr = (struct ip_hdr *) p;
	ip4hdr = (struct ip4_hdr *) p;

	if (inverse->ipv == 4) {
		/* Need to convert tuple's IP used for manipulations 
		   from 128 bit to 32 bit */
		if (nat == MANIP_DST) {
			// copy old address
			old_ip4_addr = ip4hdr->dest.addr;
			ip64_addr_set( &tmp_ip4, &inverse->src.ip);
			ip4_addr_set((struct ip4_addr *) &(ip4hdr->dest), &tmp_ip4);
			
			// Adjust IP checksum. Only IPv4 has checksum
			nat_chksum_adjust((u8_t *) & IPH4_CHKSUM(ip4hdr), 
				(u8_t *) & old_ip4_addr, 4, (u8_t *) & ip4hdr->dest.addr, 4);
	
			// remember IP header changes
			iphdr_new_changed_buf = (u8_t *) &ip4hdr->dest.addr;
			iphdr_old_changed_buf = (u8_t *) &old_ip4_addr;
			iphdr_changed_buflen  = 4;
		}
		if (nat == MANIP_SRC) {
			old_ip4_addr = ip4hdr->src.addr;
			ip64_addr_set( &tmp_ip4, &inverse->dst.ip);
			ip4_addr_set((struct ip4_addr *) &(ip4hdr->src), &tmp_ip4);
	
			// Adjust IP checksum. Only IPv4 has checksum
			nat_chksum_adjust((u8_t *) & IPH4_CHKSUM(ip4hdr), 
				(u8_t *) & old_ip4_addr, 4, (u8_t *) & ip4hdr->src.addr, 4);
		
			// remember IP header changes
			iphdr_new_changed_buf = (u8_t *) &ip4hdr->src.addr;
			iphdr_old_changed_buf = (u8_t *) &old_ip4_addr;
			iphdr_changed_buflen = 4;
		}
	} else 
	if (inverse->ipv == 6) {
		if (nat == MANIP_DST) {
			ip_addr_set( &old_ip6_addr, &iphdr->dest);
			ip_addr_set( &iphdr->dest, &inverse->src.ip);

			iphdr_new_changed_buf = (u8_t *) &iphdr->dest;
		}
		if (nat == MANIP_SRC) {
			ip_addr_set( &old_ip6_addr, &iphdr->src);
			ip_addr_set( &iphdr->src, &inverse->dst.ip);

			iphdr_new_changed_buf = (u8_t *) &iphdr->src;
		}

		iphdr_old_changed_buf = (u8_t *) &old_ip6_addr;
		iphdr_changed_buflen = 16;
	}

	/* Modify transport header */

	if (manip_innerproto) {

		proto = track_proto_find(inverse->src.proto.protonum);
	
		if (inverse->ipv == 6)      iphdrlen = IP_HLEN;
		else if (inverse->ipv == 4) iphdrlen = IPH4_HL(ip4hdr) * 4;
	
		proto->manip(nat, p, iphdrlen, inverse, 
			iphdr_new_changed_buf, 
			iphdr_old_changed_buf, 
			iphdr_changed_buflen);
	}
}

int icmp_reply_translation(struct pbuf *p, nat_manip_t nat_here);

uf_verdict_t  nat_perform   (struct stack *stack, uf_hook_t hooknum, struct pbuf **q, struct netif *inif, struct netif *outif)
{
	struct pbuf    *p = * q;
	struct ip_hdr  *ip6hdr;
	struct ip4_hdr *ip4hdr;
	u16_t proto;

	struct nat_pcb   *pcb;
	conn_dir_t       direction;
	struct ip_tuple  *tuple_inverse;

	nat_manip_t      nat_here; /* NAT to do in this hook */
	nat_manip_t      nat_todo; /* NAT to do on the connection */


	LWIP_DEBUGF(NAT_DEBUG, ("%s: start %s!\n", __func__, STR_HOOKNAME(hooknum)));

	if (p->nat.track == NULL) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: no track for this packet!!\n", __func__));
		return UF_ACCEPT;
	}


	pcb = p->nat.track;
	direction = p->nat.dir;

        /* 
	 * Check what to do...
	 */

	nat_here = HOOK2MANIP(hooknum);
	
	ip6hdr = (struct ip_hdr *) p->payload;
	ip4hdr = (struct ip4_hdr *) p->payload;
	if (IPH_V(ip6hdr) == 6)       proto = IPH_NEXTHDR(ip6hdr) ;
	else if (IPH_V(ip6hdr) == 4)  proto = IPH4_PROTO(ip4hdr) ;
	else
		return UF_DROP;

	switch (p->nat.info) {
		case CT_RELATED:
		case CT_RELATED + CT_IS_REPLY:
			LWIP_DEBUGF(NAT_DEBUG, ("%s: is RELATED || RELATED+REPLY \n", __func__ ));
			if (proto == IP_PROTO_ICMP || proto == IP_PROTO_ICMP4) {
				if (icmp_reply_translation(p, nat_here) <= 0) 
					return UF_DROP;
				else
					return UF_ACCEPT;
			}
			/* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
		case CT_NEW:

			LWIP_DEBUGF(NAT_DEBUG, ("%s: is NEW \n", __func__ ));

			/* If no NAT has been done on this connection and one of SNAT or DNAT
			   rules haven't been checked yet, try them */
			if (pcb->nat_type == NAT_NONE)
			if ((pcb->status & TS_NAT_TRY_MASK) != TS_NAT_TRY_MASK) {
	
				LWIP_DEBUGF(NAT_DEBUG, ("%s: Check for rules...\n", __func__ ));

				if (nat_check_rule(pcb, hooknum, inif, outif ) < 0) {
					LWIP_DEBUGF(NAT_DEBUG, ("%s: error, DROP\n", __func__ ));
					return UF_DROP;
				}
			}

			break;
	
		default:
			LWIP_DEBUGF(NAT_DEBUG, ("%s: is ESTABLISHED or other... \n", __func__ ));
			/* ESTABLISHED: do NAT */
			break;

	}

#if 0
	/* If no NAT has been done on this connection and one of SNAT or DNAT
	   rules haven't been checked yet, try them */
	if (pcb->nat_type == NAT_NONE)
	if ((pcb->status & TS_NAT_TRY_MASK) != TS_NAT_TRY_MASK) {

		LWIP_DEBUGF(NAT_DEBUG, ("%s: Check for rules...\n", __func__ ));

		if (nat_check_rule(pcb, hooknum, inif, outif ) < 0) {
			LWIP_DEBUGF(NAT_DEBUG, ("%s: error, DROP\n", __func__ ));
			return UF_DROP;
		}
	}
#endif

	/* No NAT? No party */
	if (pcb->nat_type == NAT_NONE) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: no NAT to do.\n", __func__ ));
		return UF_ACCEPT;
	}

	/* How tuples will be manipulated by Hooks
         *
	 * NAT  | DIRECTION | --> PREROUTING    ----->   POSTROUTING -->
	 *------|-----------|------------------------------------------
	 *      | ORIGINAL  |   [x,y] -> x,N (D)            - 
	 * DNAT |           |
	 *      | REPLY     |         -                [N,x] -> y,x (S)
	 *------|-----------|------------------------------------------
	 *      | ORIGINAL  |         -                [x,y] -> N,y (S)
	 * SNAT |           |
	 *      | REPLY     |   [y,N] -> y,x (D)            -
	 *------|-----------|------------------------------------------
	 *
	 *    x,y  tuple (IP.src,IP.dst,proto.src,proto.dst)
	 *   [x,y] connection's tuple saved in tracking data
	 *    (D)  new destination = source      in the inverse tuple
	 *    (S)  new source      = destination in the inverse tuple
	 */


        /* We use the tuple in the other direction for NAT. See above */
	if (direction == CONN_DIR_ORIGINAL) 
		tuple_inverse = &pcb->tuple[CONN_DIR_REPLY];
	else
		tuple_inverse = &pcb->tuple[CONN_DIR_ORIGINAL];


	/* Manipulation's type depends on hook and packet direction */

	nat_todo = NAT2MANIP(pcb->nat_type);

	if (direction == CONN_DIR_REPLY) {
        	nat_todo = ! nat_todo;
	}

	/* Manipulation on this packet is needed in this hook? */
	if (nat_todo == nat_here) {

		nat_modify_ip(nat_todo, p->payload, tuple_inverse, 1);

		LWIP_DEBUGF(NAT_DEBUG, ("%s: NATed packet !\n", __func__));
		ip_debug_print(NAT_DEBUG, p);
		LWIP_DEBUGF(NAT_DEBUG, ("%s: END.\n", __func__));
	}
	else {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: no NAT to do in this hook\n", __func__ ));

	}

	return UF_ACCEPT;
}


int icmp_reply_translation(struct pbuf *p, nat_manip_t nat_here)
{
	struct {
		struct icmp_echo_hdr icmp;
		union {
			struct ip4_hdr ip4hdr;
			struct ip_hdr  ip6hdr;
		} uip;
	} *inside;


	conn_dir_t       direction;
	struct ip4_hdr *ip4hdr;
	struct ip_hdr *ip6hdr;
	int hdrlen;

	nat_manip_t      nat_todo; /* NAT to do on the connection */

	struct ip_tuple *inverse;
	struct ip_tuple  inner_inverse;

	struct nat_pcb *pcb = p->nat.track;

	direction = p->nat.dir;

	ip6hdr = (struct ip_hdr *)  p->payload;
	ip4hdr = (struct ip4_hdr *) p->payload;
	if (IPH4_V(ip4hdr) == 4)  hdrlen = IPH4_HL(ip4hdr) * 4;
	else                      hdrlen = IP_HLEN;

	inside = (void *)(p->payload + hdrlen);


	if (p->nat.info == CT_RELATED || p->nat.info == CT_RELATED + CT_IS_REPLY) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: Strange, not RELATED or RELATED+REPLY\n", __func__));
		return -1;
	}

	/* 
	 *  Get tuples for translations 
	 */	
        /* We use the tuple in the other direction for NAT. See above */
	if (direction == CONN_DIR_ORIGINAL) 
		inverse = &pcb->tuple[CONN_DIR_REPLY];
	else
		inverse = &pcb->tuple[CONN_DIR_ORIGINAL];

	if (tuple_inverse( &inner_inverse, inverse ) < 0) {
		LWIP_DEBUGF(NAT_DEBUG, ("%s: Strange, can't get tuple inverse!\n", __func__));
		return -1;
	}


	/*
	 *
	 */
	nat_todo = NAT2MANIP(pcb->nat_type);

	if (direction == CONN_DIR_REPLY) {
        	nat_todo = ! nat_todo;
	}

	if (nat_todo != nat_here)
		return 1;

	/*
	 * Change Inner packet
	 */
	nat_modify_ip(!nat_todo, (char *) &inside->uip.ip4hdr, &inner_inverse, 1);


	LWIP_DEBUGF(NAT_DEBUG, ("%s: NATed INNER PACKET !\n", __func__));
	ip_debug_print(NAT_DEBUG, p);

	/// CHECKSUM?

	/*
	 * Change Packet hearde
	 */
	nat_modify_ip(nat_todo, p->payload, inverse, 0);

	LWIP_DEBUGF(NAT_DEBUG, ("%s: NATed PACKET !\n", __func__));
	ip_debug_print(NAT_DEBUG, p);


	LWIP_DEBUGF(NAT_DEBUG, ("%s: END !\n", __func__));

	return 1;
}

#endif /* LWIP_NAT */