NAT64: documentation
[vpp.git] / src / plugins / snat / nat64.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT64 implementation
18  */
19
20 #include <snat/nat64.h>
21 #include <snat/nat64_db.h>
22 #include <vnet/fib/ip4_fib.h>
23
24
25 nat64_main_t nat64_main;
26
27 /* *INDENT-OFF* */
28
29 /* Hook up input features */
30 VNET_FEATURE_INIT (nat64_in2out, static) = {
31   .arc_name = "ip6-unicast",
32   .node_name = "nat64-in2out",
33   .runs_before = VNET_FEATURES ("ip6-lookup"),
34 };
35 VNET_FEATURE_INIT (nat64_out2in, static) = {
36   .arc_name = "ip4-unicast",
37   .node_name = "nat64-out2in",
38   .runs_before = VNET_FEATURES ("ip4-lookup"),
39 };
40
41 static u8 well_known_prefix[] = {
42   0x00, 0x64, 0xff, 0x9b,
43   0x00, 0x00, 0x00, 0x00,
44   0x00, 0x00, 0x00, 0x00,
45   0x00, 0x00, 0x00, 0x00
46 };
47
48 /* *INDENT-ON* */
49
50 clib_error_t *
51 nat64_init (vlib_main_t * vm)
52 {
53   nat64_main_t *nm = &nat64_main;
54   clib_error_t *error = 0;
55   vlib_thread_main_t *tm = vlib_get_thread_main ();
56
57   nm->is_disabled = 0;
58
59   if (tm->n_vlib_mains > 1)
60     {
61       nm->is_disabled = 1;
62       goto error;
63     }
64
65   if (nat64_db_init (&nm->db))
66     {
67       error = clib_error_return (0, "NAT64 DB init failed");
68       goto error;
69     }
70
71   /* set session timeouts to default values */
72   nm->udp_timeout = SNAT_UDP_TIMEOUT;
73   nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
74   nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
75   nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
76   nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
77
78 error:
79   return error;
80 }
81
82 int
83 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
84 {
85   nat64_main_t *nm = &nat64_main;
86   snat_address_t *a = 0;
87   snat_interface_t *interface;
88   int i;
89
90   /* Check if address already exists */
91   for (i = 0; i < vec_len (nm->addr_pool); i++)
92     {
93       if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
94         {
95           a = nm->addr_pool + i;
96           break;
97         }
98     }
99
100   if (is_add)
101     {
102       if (a)
103         return VNET_API_ERROR_VALUE_EXIST;
104
105       vec_add2 (nm->addr_pool, a, 1);
106       a->addr = *addr;
107       a->fib_index = 0;
108       if (vrf_id != ~0)
109         a->fib_index =
110           fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
111 #define _(N, i, n, s) \
112       clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
113       foreach_snat_protocol
114 #undef _
115     }
116   else
117     {
118       if (!a)
119         return VNET_API_ERROR_NO_SUCH_ENTRY;
120
121 #define _(N, id, n, s) \
122       clib_bitmap_free (a->busy_##n##_port_bitmap);
123       foreach_snat_protocol
124 #undef _
125         vec_del1 (nm->addr_pool, i);
126     }
127
128   /* Add/del external address to FIB */
129   /* *INDENT-OFF* */
130   pool_foreach (interface, nm->interfaces,
131   ({
132     if (interface->is_inside)
133       continue;
134
135     snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
136     break;
137   }));
138   /* *INDENT-ON* */
139
140   return 0;
141 }
142
143 void
144 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
145 {
146   nat64_main_t *nm = &nat64_main;
147   snat_address_t *a = 0;
148
149   /* *INDENT-OFF* */
150   vec_foreach (a, nm->addr_pool)
151     {
152       if (fn (a, ctx))
153         break;
154     };
155   /* *INDENT-ON* */
156 }
157
158 int
159 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
160 {
161   nat64_main_t *nm = &nat64_main;
162   snat_interface_t *interface = 0, *i;
163   snat_address_t *ap;
164   const char *feature_name, *arc_name;
165
166   /* Check if address already exists */
167   /* *INDENT-OFF* */
168   pool_foreach (i, nm->interfaces,
169   ({
170     if (i->sw_if_index == sw_if_index)
171       {
172         interface = i;
173         break;
174       }
175   }));
176   /* *INDENT-ON* */
177
178   if (is_add)
179     {
180       if (interface)
181         return VNET_API_ERROR_VALUE_EXIST;
182
183       pool_get (nm->interfaces, interface);
184       interface->sw_if_index = sw_if_index;
185       interface->is_inside = is_inside;
186
187     }
188   else
189     {
190       if (!interface)
191         return VNET_API_ERROR_NO_SUCH_ENTRY;
192
193       pool_put (nm->interfaces, interface);
194     }
195
196   if (!is_inside)
197     {
198       /* *INDENT-OFF* */
199       vec_foreach (ap, nm->addr_pool)
200         snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
201       /* *INDENT-ON* */
202     }
203
204   arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
205   feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
206
207   return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
208                                       is_add, 0, 0);
209 }
210
211 void
212 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
213 {
214   nat64_main_t *nm = &nat64_main;
215   snat_interface_t *i = 0;
216
217   /* *INDENT-OFF* */
218   pool_foreach (i, nm->interfaces,
219   ({
220     if (fn (i, ctx))
221       break;
222   }));
223   /* *INDENT-ON* */
224 }
225
226 int
227 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
228                                ip4_address_t * addr, u16 * port)
229 {
230   nat64_main_t *nm = &nat64_main;
231   snat_main_t *sm = &snat_main;
232   int i;
233   snat_address_t *a, *ga = 0;
234   u32 portnum;
235
236   for (i = 0; i < vec_len (nm->addr_pool); i++)
237     {
238       a = nm->addr_pool + i;
239       switch (proto)
240         {
241 #define _(N, j, n, s) \
242         case SNAT_PROTOCOL_##N: \
243           if (a->busy_##n##_ports < (65535-1024)) \
244             { \
245               if (a->fib_index == fib_index) \
246                 { \
247                   while (1) \
248                     { \
249                       portnum = random_u32 (&sm->random_seed); \
250                       portnum &= 0xFFFF; \
251                       if (portnum < 1024) \
252                         continue; \
253                       if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
254                                                     portnum)) \
255                         continue; \
256                       clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
257                                                 portnum, 1); \
258                       a->busy_##n##_ports++; \
259                       *port = portnum; \
260                       addr->as_u32 = a->addr.as_u32; \
261                       return 0; \
262                     } \
263                  } \
264                else if (a->fib_index == 0) \
265                  ga = a; \
266             } \
267           break;
268           foreach_snat_protocol
269 #undef _
270         default:
271           clib_warning ("unknown protocol");
272           return 1;
273         }
274     }
275
276   if (ga)
277     {
278       switch (proto)
279         {
280 #define _(N, j, n, s) \
281         case SNAT_PROTOCOL_##N: \
282           while (1) \
283             { \
284               portnum = random_u32 (&sm->random_seed); \
285               portnum &= 0xFFFF; \
286               if (portnum < 1024) \
287                 continue; \
288               if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
289                                             portnum)) \
290                 continue; \
291               clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
292                                         portnum, 1); \
293               a->busy_##n##_ports++; \
294               *port = portnum; \
295               addr->as_u32 = a->addr.as_u32; \
296               return 0; \
297             }
298           break;
299           foreach_snat_protocol
300 #undef _
301         default:
302           clib_warning ("unknown protocol");
303           return 1;
304         }
305     }
306
307   /* Totally out of translations to use... */
308   //TODO: IPFix
309   return 1;
310 }
311
312 void
313 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
314                               snat_protocol_t proto)
315 {
316   nat64_main_t *nm = &nat64_main;
317   int i;
318   snat_address_t *a;
319
320   for (i = 0; i < vec_len (nm->addr_pool); i++)
321     {
322       a = nm->addr_pool + i;
323       if (addr->as_u32 != a->addr.as_u32)
324         continue;
325       switch (proto)
326         {
327 #define _(N, j, n, s) \
328         case SNAT_PROTOCOL_##N: \
329           ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
330                   port) == 1); \
331           clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
332           a->busy_##n##_ports--; \
333           break;
334           foreach_snat_protocol
335 #undef _
336         default:
337           clib_warning ("unknown protocol");
338           return;
339         }
340       break;
341     }
342 }
343
344 int
345 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
346                                 ip4_address_t * out_addr, u16 in_port,
347                                 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
348 {
349   nat64_main_t *nm = &nat64_main;
350   nat64_db_bib_entry_t *bibe;
351   u32 fib_index =
352     fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
353   snat_protocol_t p = ip_proto_to_snat_proto (proto);
354   ip46_address_t addr;
355   int i;
356   snat_address_t *a;
357
358   addr.as_u64[0] = in_addr->as_u64[0];
359   addr.as_u64[1] = in_addr->as_u64[1];
360   bibe =
361     nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
362                              p, fib_index, 1);
363
364   if (is_add)
365     {
366       if (bibe)
367         return VNET_API_ERROR_VALUE_EXIST;
368
369       for (i = 0; i < vec_len (nm->addr_pool); i++)
370         {
371           a = nm->addr_pool + i;
372           if (out_addr->as_u32 != a->addr.as_u32)
373             continue;
374           switch (p)
375             {
376 #define _(N, j, n, s) \
377             case SNAT_PROTOCOL_##N: \
378               if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
379                                             out_port)) \
380                 return VNET_API_ERROR_INVALID_VALUE; \
381               clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
382                                         out_port, 1); \
383               if (out_port > 1024) \
384                 a->busy_##n##_ports++; \
385               break;
386               foreach_snat_protocol
387 #undef _
388             default:
389               clib_warning ("unknown protocol");
390               return VNET_API_ERROR_INVALID_VALUE_2;
391             }
392           break;
393         }
394       bibe =
395         nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
396                                    clib_host_to_net_u16 (in_port),
397                                    clib_host_to_net_u16 (out_port), fib_index,
398                                    p, 1);
399       if (!bibe)
400         return VNET_API_ERROR_UNSPECIFIED;
401     }
402   else
403     {
404       if (!bibe)
405         return VNET_API_ERROR_NO_SUCH_ENTRY;
406
407       nat64_free_out_addr_and_port (out_addr, out_port, p);
408       nat64_db_bib_entry_free (&nm->db, bibe);
409     }
410
411   return 0;
412 }
413
414 int
415 nat64_set_udp_timeout (u32 timeout)
416 {
417   nat64_main_t *nm = &nat64_main;
418
419   if (timeout == 0)
420     nm->udp_timeout = SNAT_UDP_TIMEOUT;
421   else if (timeout < SNAT_UDP_TIMEOUT_MIN)
422     return VNET_API_ERROR_INVALID_VALUE;
423   else
424     nm->udp_timeout = timeout;
425
426   return 0;
427 }
428
429 u32
430 nat64_get_udp_timeout (void)
431 {
432   nat64_main_t *nm = &nat64_main;
433
434   return nm->udp_timeout;
435 }
436
437 int
438 nat64_set_icmp_timeout (u32 timeout)
439 {
440   nat64_main_t *nm = &nat64_main;
441
442   if (timeout == 0)
443     nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
444   else
445     nm->icmp_timeout = timeout;
446
447   return 0;
448 }
449
450 u32
451 nat64_get_icmp_timeout (void)
452 {
453   nat64_main_t *nm = &nat64_main;
454
455   return nm->icmp_timeout;
456 }
457
458 int
459 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
460 {
461   nat64_main_t *nm = &nat64_main;
462
463   if (trans == 0)
464     nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
465   else
466     nm->tcp_trans_timeout = trans;
467
468   if (est == 0)
469     nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
470   else
471     nm->tcp_est_timeout = est;
472
473   if (incoming_syn == 0)
474     nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
475   else
476     nm->tcp_incoming_syn_timeout = incoming_syn;
477
478   return 0;
479 }
480
481 u32
482 nat64_get_tcp_trans_timeout (void)
483 {
484   nat64_main_t *nm = &nat64_main;
485
486   return nm->tcp_trans_timeout;
487 }
488
489 u32
490 nat64_get_tcp_est_timeout (void)
491 {
492   nat64_main_t *nm = &nat64_main;
493
494   return nm->tcp_est_timeout;
495 }
496
497 u32
498 nat64_get_tcp_incoming_syn_timeout (void)
499 {
500   nat64_main_t *nm = &nat64_main;
501
502   return nm->tcp_incoming_syn_timeout;
503 }
504
505 void
506 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
507 {
508   nat64_main_t *nm = &nat64_main;
509   u32 now = (u32) vlib_time_now (vm);
510
511   switch (ste->proto)
512     {
513     case SNAT_PROTOCOL_ICMP:
514       ste->expire = now + nm->icmp_timeout;
515       return;
516     case SNAT_PROTOCOL_TCP:
517       {
518         switch (ste->tcp_state)
519           {
520           case NAT64_TCP_STATE_V4_INIT:
521           case NAT64_TCP_STATE_V6_INIT:
522           case NAT64_TCP_STATE_V4_FIN_RCV:
523           case NAT64_TCP_STATE_V6_FIN_RCV:
524           case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
525           case NAT64_TCP_STATE_TRANS:
526             ste->expire = now + nm->tcp_trans_timeout;
527             return;
528           case NAT64_TCP_STATE_ESTABLISHED:
529             ste->expire = now + nm->tcp_est_timeout;
530             return;
531           default:
532             return;
533           }
534       }
535     case SNAT_PROTOCOL_UDP:
536       ste->expire = now + nm->udp_timeout;
537       return;
538     default:
539       return;
540     }
541 }
542
543 void
544 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
545                              u8 is_ip6)
546 {
547   switch (ste->tcp_state)
548     {
549     case NAT64_TCP_STATE_CLOSED:
550       {
551         if (tcp->flags & TCP_FLAG_SYN)
552           {
553             if (is_ip6)
554               ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
555             else
556               ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
557           }
558         return;
559       }
560     case NAT64_TCP_STATE_V4_INIT:
561       {
562         if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
563           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
564         return;
565       }
566     case NAT64_TCP_STATE_V6_INIT:
567       {
568         if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
569           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
570         return;
571       }
572     case NAT64_TCP_STATE_ESTABLISHED:
573       {
574         if (tcp->flags & TCP_FLAG_FIN)
575           {
576             if (is_ip6)
577               ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
578             else
579               ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
580           }
581         else if (tcp->flags & TCP_FLAG_RST)
582           {
583             ste->tcp_state = NAT64_TCP_STATE_TRANS;
584           }
585         return;
586       }
587     case NAT64_TCP_STATE_V4_FIN_RCV:
588       {
589         if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
590           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
591         return;
592       }
593     case NAT64_TCP_STATE_V6_FIN_RCV:
594       {
595         if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
596           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
597         return;
598       }
599     case NAT64_TCP_STATE_TRANS:
600       {
601         if (!(tcp->flags & TCP_FLAG_RST))
602           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
603         return;
604       }
605     default:
606       return;
607     }
608 }
609
610 int
611 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
612 {
613   nat64_main_t *nm = &nat64_main;
614   nat64_prefix_t *p = 0;
615   int i;
616
617   /* Verify prefix length */
618   if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
619       && plen != 96)
620     return VNET_API_ERROR_INVALID_VALUE;
621
622   /* Check if tenant already have prefix */
623   for (i = 0; i < vec_len (nm->pref64); i++)
624     {
625       if (nm->pref64[i].vrf_id == vrf_id)
626         {
627           p = nm->pref64 + i;
628           break;
629         }
630     }
631
632   if (is_add)
633     {
634       if (!p)
635         {
636           vec_add2 (nm->pref64, p, 1);
637           p->fib_index =
638             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
639           p->vrf_id = vrf_id;
640         }
641
642       p->prefix.as_u64[0] = prefix->as_u64[0];
643       p->prefix.as_u64[1] = prefix->as_u64[1];
644       p->plen = plen;
645     }
646   else
647     {
648       if (!p)
649         return VNET_API_ERROR_NO_SUCH_ENTRY;
650
651       vec_del1 (nm->pref64, i);
652     }
653
654   return 0;
655 }
656
657 void
658 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
659 {
660   nat64_main_t *nm = &nat64_main;
661   nat64_prefix_t *p = 0;
662
663   /* *INDENT-OFF* */
664   vec_foreach (p, nm->pref64)
665     {
666       if (fn (p, ctx))
667         break;
668     };
669   /* *INDENT-ON* */
670 }
671
672 void
673 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
674 {
675   nat64_main_t *nm = &nat64_main;
676   nat64_prefix_t *p, *gp = 0, *prefix = 0;
677
678   /* *INDENT-OFF* */
679   vec_foreach (p, nm->pref64)
680     {
681       if (p->fib_index == fib_index)
682         {
683           prefix = p;
684           break;
685         }
686
687       if (p->fib_index == 0)
688         gp = p;
689     };
690   /* *INDENT-ON* */
691
692   if (!prefix)
693     prefix = gp;
694
695   if (prefix)
696     {
697       memset (ip6, 0, 16);
698       memcpy (ip6, &p->prefix, p->plen);
699       switch (p->plen)
700         {
701         case 32:
702           ip6->as_u32[1] = ip4->as_u32;
703           break;
704         case 40:
705           ip6->as_u8[5] = ip4->as_u8[0];
706           ip6->as_u8[6] = ip4->as_u8[1];
707           ip6->as_u8[7] = ip4->as_u8[2];
708           ip6->as_u8[9] = ip4->as_u8[3];
709           break;
710         case 48:
711           ip6->as_u8[6] = ip4->as_u8[0];
712           ip6->as_u8[7] = ip4->as_u8[1];
713           ip6->as_u8[9] = ip4->as_u8[2];
714           ip6->as_u8[10] = ip4->as_u8[3];
715           break;
716         case 56:
717           ip6->as_u8[7] = ip4->as_u8[0];
718           ip6->as_u8[9] = ip4->as_u8[1];
719           ip6->as_u8[10] = ip4->as_u8[2];
720           ip6->as_u8[11] = ip4->as_u8[3];
721           break;
722         case 64:
723           ip6->as_u8[9] = ip4->as_u8[0];
724           ip6->as_u8[10] = ip4->as_u8[1];
725           ip6->as_u8[11] = ip4->as_u8[2];
726           ip6->as_u8[12] = ip4->as_u8[3];
727           break;
728         case 96:
729           ip6->as_u32[3] = ip4->as_u32;
730           break;
731         default:
732           clib_warning ("invalid prefix length");
733           break;
734         }
735     }
736   else
737     {
738       memcpy (ip6, well_known_prefix, 16);
739       ip6->as_u32[3] = ip4->as_u32;
740     }
741 }
742
743 void
744 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
745 {
746   nat64_main_t *nm = &nat64_main;
747   nat64_prefix_t *p, *gp = 0;
748   u8 plen = 0;
749
750   /* *INDENT-OFF* */
751   vec_foreach (p, nm->pref64)
752     {
753       if (p->fib_index == fib_index)
754         {
755           plen = p->plen;
756           break;
757         }
758
759       if (p->vrf_id == 0)
760         gp = p;
761     };
762   /* *INDENT-ON* */
763
764   if (!plen)
765     {
766       if (gp)
767         plen = gp->plen;
768       else
769         plen = 96;
770     }
771
772   switch (plen)
773     {
774     case 32:
775       ip4->as_u32 = ip6->as_u32[1];
776       break;
777     case 40:
778       ip4->as_u8[0] = ip6->as_u8[5];
779       ip4->as_u8[1] = ip6->as_u8[6];
780       ip4->as_u8[2] = ip6->as_u8[7];
781       ip4->as_u8[3] = ip6->as_u8[9];
782       break;
783     case 48:
784       ip4->as_u8[0] = ip6->as_u8[6];
785       ip4->as_u8[1] = ip6->as_u8[7];
786       ip4->as_u8[2] = ip6->as_u8[9];
787       ip4->as_u8[3] = ip6->as_u8[10];
788       break;
789     case 56:
790       ip4->as_u8[0] = ip6->as_u8[7];
791       ip4->as_u8[1] = ip6->as_u8[9];
792       ip4->as_u8[2] = ip6->as_u8[10];
793       ip4->as_u8[3] = ip6->as_u8[11];
794       break;
795     case 64:
796       ip4->as_u8[0] = ip6->as_u8[9];
797       ip4->as_u8[1] = ip6->as_u8[10];
798       ip4->as_u8[2] = ip6->as_u8[11];
799       ip4->as_u8[3] = ip6->as_u8[12];
800       break;
801     case 96:
802       ip4->as_u32 = ip6->as_u32[3];
803       break;
804     default:
805       clib_warning ("invalid prefix length");
806       break;
807     }
808
809   clib_warning ("%U %U plen %u", format_ip6_address, ip6, format_ip4_address,
810                 ip4, plen);
811 }
812
813 /**
814  * @brief The 'nat64-expire-walk' process's main loop.
815  *
816  * Check expire time for NAT64 sessions.
817  */
818 static uword
819 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
820                       vlib_frame_t * f)
821 {
822   nat64_main_t *nm = &nat64_main;
823
824   while (!nm->is_disabled)
825     {
826       vlib_process_wait_for_event_or_clock (vm, 10.0);
827       vlib_process_get_events (vm, NULL);
828       u32 now = (u32) vlib_time_now (vm);
829
830       nad64_db_st_free_expired (&nm->db, now);
831     }
832
833   return 0;
834 }
835
836 static vlib_node_registration_t nat64_expire_walk_node;
837
838 /* *INDENT-OFF* */
839 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
840     .function = nat64_expire_walk_fn,
841     .type = VLIB_NODE_TYPE_PROCESS,
842     .name = "nat64-expire-walk",
843 };
844 /* *INDENT-ON* */
845
846 /*
847  * fd.io coding-style-patch-verification: ON
848  *
849  * Local Variables:
850  * eval: (c-set-style "gnu")
851  * End:
852  */