d76de8bd2cdec1ee20be0fa28445e85adfc48f9d
[vpp.git] / src / plugins / cnat / cnat_src_policy.h
1 /*
2  * Copyright (c) 2020 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 #ifndef __CNAT_SRC_POLICY_H__
17 #define __CNAT_SRC_POLICY_H__
18
19 // #include <vnet/udp/udp.h>
20 #include <cnat/cnat_types.h>
21 #include <cnat/cnat_session.h>
22 #include <cnat/cnat_translation.h>
23
24 typedef enum
25 {
26   CNAT_SPORT_PROTO_TCP,
27   CNAT_SPORT_PROTO_UDP,
28   CNAT_SPORT_PROTO_ICMP,
29   CNAT_SPORT_PROTO_ICMP6,
30   CNAT_N_SPORT_PROTO
31 } cnat_sport_proto_t;
32
33 typedef enum cnat_source_policy_errors_
34 {
35   CNAT_SOURCE_ERROR_EXHAUSTED_PORTS,
36   CNAT_SOURCE_ERROR_USE_DEFAULT,
37   CNAT_SOURCE_N_ERRORS,
38 } cnat_source_policy_errors_t;
39
40 typedef struct cnat_src_port_allocator_
41 {
42   /* Source ports bitmap for snat */
43   clib_bitmap_t *bmap;
44
45   /* Lock for src_ports access */
46   clib_spinlock_t lock;
47 } cnat_src_port_allocator_t;
48
49 /* function to use to compute source (IP, port) for a new session to a vip */
50 typedef cnat_source_policy_errors_t (*cnat_vip_source_policy_t)
51   (vlib_main_t * vm, vlib_buffer_t * b, cnat_session_t * session,
52    u32 * rsession_flags, const cnat_translation_t * ct,
53    cnat_node_ctx_t * ctx);
54
55 typedef struct cnat_src_policy_main_
56 {
57   cnat_vip_source_policy_t vip_policy;
58   cnat_vip_source_policy_t default_policy;
59
60   /* Per proto source ports allocator for snat */
61   cnat_src_port_allocator_t *src_ports;
62 } cnat_src_policy_main_t;
63
64 extern cnat_src_policy_main_t cnat_src_policy_main;
65
66 void cnat_register_vip_src_policy (cnat_vip_source_policy_t fp);
67 int cnat_allocate_port (u16 * port, ip_protocol_t iproto);
68 void cnat_free_port (u16 port, ip_protocol_t iproto);
69
70 /*
71  * fd.io coding-style-patch-verification: ON
72  *
73  * Local Variables:
74  * eval: (c-set-style "gnu")
75  * End:
76  */
77
78 #endif