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