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