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