FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
nat_syslog.c
Go to the documentation of this file.
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  * @file
17  * @brief NAT syslog logging
18  */
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/syslog/syslog.h>
22 
23 #include <nat/lib/nat_syslog.h>
24 #include <nat/lib/inlines.h>
25 
26 #define NAT_FACILITY SYSLOG_FACILITY_LOCAL0
27 
28 #define NAT_APPNAME "NAT"
29 
30 #define SADD_SDEL_SEVERITY SYSLOG_SEVERITY_INFORMATIONAL
31 #define APMADD_APMDEL_SEVERITY SYSLOG_SEVERITY_INFORMATIONAL
32 
33 #define SADD_MSGID "SADD"
34 #define SDEL_MSGID "SDEL"
35 #define APMADD_MSGID "APMADD"
36 #define APMDEL_MSGID "APMDEL"
37 
38 #define NSESS_SDID "nsess"
39 #define NAPMAP_SDID "napmap"
40 
41 #define SSUBIX_SDPARAM_NAME "SSUBIX"
42 #define SVLAN_SDPARAM_NAME "SVLAN"
43 #define IATYP_SDPARAM_NAME "IATYP"
44 #define ISADDR_SDPARAM_NAME "ISADDR"
45 #define ISPORT_SDPARAM_NAME "ISPORT"
46 #define IDADDR_SDPARAM_NAME "IDADDR"
47 #define IDPORT_SDPARAM_NAME "IDPORT"
48 #define XATYP_SDPARAM_NAME "XATYP"
49 #define XSADDR_SDPARAM_NAME "XSADDR"
50 #define XSPORT_SDPARAM_NAME "XSPORT"
51 #define XDADDR_SDPARAM_NAME "XDADDR"
52 #define XDPORT_SDPARAM_NAME "XDPORT"
53 #define PROTO_SDPARAM_NAME "PROTO"
54 #define SV6ENC_SDPARAM_NAME "SV6ENC"
55 
56 #define IATYP_IPV4 "IPv4"
57 #define IATYP_IPV6 "IPv6"
58 
59 static inline void
60 nat_syslog_nat44_apmap (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
61  u16 isport, ip4_address_t * xsaddr, u16 xsport,
62  nat_protocol_t proto, u8 is_add,
63  ip6_address_t * sv6enc)
64 {
66  fib_table_t *fib;
67 
68  if (!syslog_is_enabled ())
69  return;
70 
72  return;
73 
76 
79  if (sv6enc)
80  {
82  format_ip6_address, sv6enc);
83  }
84  else
85  {
86  fib = fib_table_get (sfibix, FIB_PROTOCOL_IP4);
88  fib->ft_table_id);
89  }
92  format_ip4_address, isaddr);
94  clib_net_to_host_u16 (isport));
97  format_ip4_address, xsaddr);
99  clib_net_to_host_u16 (xsport));
102 
104 }
105 
106 void
107 nat_syslog_nat44_apmadd (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
108  u16 isport, ip4_address_t * xsaddr, u16 xsport,
110 {
111  nat_syslog_nat44_apmap (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
112  proto, 1, 0);
113 }
114 
115 void
116 nat_syslog_nat44_apmdel (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
117  u16 isport, ip4_address_t * xsaddr, u16 xsport,
119 {
120  nat_syslog_nat44_apmap (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
121  proto, 0, 0);
122 }
123 
124 void
125 nat_syslog_dslite_apmadd (u32 ssubix, ip6_address_t * sv6enc,
126  ip4_address_t * isaddr, u16 isport,
127  ip4_address_t * xsaddr, u16 xsport,
129 {
130  nat_syslog_nat44_apmap (ssubix, 0, isaddr, isport, xsaddr, xsport,
131  proto, 1, sv6enc);
132 }
133 
134 void
135 nat_syslog_dslite_apmdel (u32 ssubix, ip6_address_t * sv6enc,
136  ip4_address_t * isaddr, u16 isport,
137  ip4_address_t * xsaddr, u16 xsport,
139 {
140  nat_syslog_nat44_apmap (ssubix, 0, isaddr, isport, xsaddr, xsport,
141  proto, 0, sv6enc);
142 }
143 
144 static inline void
145 nat_syslog_nat44_sess (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
146  u16 isport, ip4_address_t * xsaddr, u16 xsport,
147  ip4_address_t * idaddr, u16 idport,
148  ip4_address_t * xdaddr, u16 xdport,
149  nat_protocol_t proto, u8 is_add, u8 is_twicenat)
150 {
152  fib_table_t *fib;
153 
154  if (!syslog_is_enabled ())
155  return;
156 
158  return;
159 
160  fib = fib_table_get (sfibix, FIB_PROTOCOL_IP4);
161 
163  is_add ? SADD_MSGID : SDEL_MSGID);
164 
168  fib->ft_table_id);
171  format_ip4_address, isaddr);
173  clib_net_to_host_u16 (isport));
176  format_ip4_address, xsaddr);
178  clib_net_to_host_u16 (xsport));
182  format_ip4_address, xdaddr);
184  clib_net_to_host_u16 (xdport));
185  if (is_twicenat)
186  {
188  format_ip4_address, idaddr);
190  clib_net_to_host_u16 (idport));
191  }
192 
194 }
195 
196 void
197 nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
198  u16 isport, ip4_address_t * idaddr, u16 idport,
199  ip4_address_t * xsaddr, u16 xsport,
200  ip4_address_t * xdaddr, u16 xdport,
201  nat_protocol_t proto, u8 is_twicenat)
202 {
203  nat_syslog_nat44_sess (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
204  idaddr, idport, xdaddr, xdport, proto, 1,
205  is_twicenat);
206 }
207 
208 void
209 nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t * isaddr,
210  u16 isport, ip4_address_t * idaddr, u16 idport,
211  ip4_address_t * xsaddr, u16 xsport,
212  ip4_address_t * xdaddr, u16 xdport,
213  nat_protocol_t proto, u8 is_twicenat)
214 {
215  nat_syslog_nat44_sess (ssubix, sfibix, isaddr, isport, xsaddr, xsport,
216  idaddr, idport, xdaddr, xdport, proto, 0,
217  is_twicenat);
218 }
219 
220 static inline void
221 nat_syslog_nat64_sess (u32 sfibix, ip6_address_t * isaddr, u16 isport,
222  ip4_address_t * xsaddr, u16 xsport,
223  ip4_address_t * xdaddr, u16 xdport,
224  nat_protocol_t proto, u8 is_add)
225 {
227  fib_table_t *fib;
228 
229  if (!syslog_is_enabled ())
230  return;
231 
233  return;
234 
235  fib = fib_table_get (sfibix, FIB_PROTOCOL_IP6);
236 
238  is_add ? SADD_MSGID : SDEL_MSGID);
239 
242  fib->ft_table_id);
245  format_ip6_address, isaddr);
247  clib_net_to_host_u16 (isport));
250  format_ip4_address, xsaddr);
252  clib_net_to_host_u16 (xsport));
255  format_ip4_address, xdaddr);
257  clib_net_to_host_u16 (xdport));
258 
260 }
261 
262 void
263 nat_syslog_nat64_sadd (u32 sfibix, ip6_address_t * isaddr, u16 isport,
264  ip4_address_t * xsaddr, u16 xsport,
265  ip4_address_t * xdaddr, u16 xdport,
267 {
268  nat_syslog_nat64_sess (sfibix, isaddr, isport, xsaddr, xsport, xdaddr,
269  xdport, proto, 1);
270 }
271 
272 void
273 nat_syslog_nat64_sdel (u32 sfibix, ip6_address_t * isaddr, u16 isport,
274  ip4_address_t * xsaddr, u16 xsport,
275  ip4_address_t * xdaddr, u16 xdport,
277 {
278  nat_syslog_nat64_sess (sfibix, isaddr, isport, xsaddr, xsport, xdaddr,
279  xdport, proto, 0);
280 }
281 
282 /*
283  * fd.io coding-style-patch-verification: ON
284  *
285  * Local Variables:
286  * eval: (c-set-style "gnu")
287  * End:
288  */
nat_syslog_nat44_sess
static void nat_syslog_nat44_sess(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto, u8 is_add, u8 is_twicenat)
Definition: nat_syslog.c:145
NAT_APPNAME
#define NAT_APPNAME
Definition: nat_syslog.c:28
format_ip4_address
format_function_t format_ip4_address
Definition: format.h:73
SSUBIX_SDPARAM_NAME
#define SSUBIX_SDPARAM_NAME
Definition: nat_syslog.c:41
ISADDR_SDPARAM_NAME
#define ISADDR_SDPARAM_NAME
Definition: nat_syslog.c:44
nat_syslog_nat64_sadd
void nat_syslog_nat64_sadd(u32 sfibix, ip6_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto)
Definition: nat_syslog.c:263
syslog_msg
static u8 * syslog_msg
Definition: main.c:95
nat_protocol_t
nat_protocol_t
Definition: lib.h:63
IDADDR_SDPARAM_NAME
#define IDADDR_SDPARAM_NAME
Definition: nat_syslog.c:46
fib_table.h
u16
unsigned short u16
Definition: types.h:57
nat_syslog_dslite_apmadd
void nat_syslog_dslite_apmadd(u32 ssubix, ip6_address_t *sv6enc, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, nat_protocol_t proto)
Definition: nat_syslog.c:125
SADD_SDEL_SEVERITY
#define SADD_SDEL_SEVERITY
Definition: nat_syslog.c:30
ISPORT_SDPARAM_NAME
#define ISPORT_SDPARAM_NAME
Definition: nat_syslog.c:45
syslog_msg_init
void syslog_msg_init(syslog_msg_t *syslog_msg, syslog_facility_t facility, syslog_severity_t severity, char *app_name, char *msgid)
Initialize syslog message header.
Definition: syslog.c:143
nat_syslog_nat64_sess
static void nat_syslog_nat64_sess(u32 sfibix, ip6_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto, u8 is_add)
Definition: nat_syslog.c:221
fib_table_t_
A protocol Independent FIB table.
Definition: fib_table.h:71
nat_syslog_nat44_sadd
void nat_syslog_nat44_sadd(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto, u8 is_twicenat)
Definition: nat_syslog.c:197
nat_syslog_nat44_apmadd
void nat_syslog_nat44_apmadd(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, nat_protocol_t proto)
Definition: nat_syslog.c:107
fib_table_t_::ft_table_id
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
IATYP_SDPARAM_NAME
#define IATYP_SDPARAM_NAME
Definition: nat_syslog.c:43
nat_proto_to_ip_proto
static_always_inline u8 nat_proto_to_ip_proto(nat_protocol_t nat_proto)
Definition: inlines.h:37
syslog_severity_filter_block
static int syslog_severity_filter_block(syslog_severity_t s)
Severity filter test.
Definition: syslog.h:210
nat_syslog_dslite_apmdel
void nat_syslog_dslite_apmdel(u32 ssubix, ip6_address_t *sv6enc, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, nat_protocol_t proto)
Definition: nat_syslog.c:135
nat_syslog_nat64_sdel
void nat_syslog_nat64_sdel(u32 sfibix, ip6_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto)
Definition: nat_syslog.c:273
ip4_address_t
Definition: ip4_packet.h:50
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
inlines.h
syslog.h
PROTO_SDPARAM_NAME
#define PROTO_SDPARAM_NAME
Definition: nat_syslog.c:53
NAPMAP_SDID
#define NAPMAP_SDID
Definition: nat_syslog.c:39
APMDEL_MSGID
#define APMDEL_MSGID
Definition: nat_syslog.c:36
IATYP_IPV4
#define IATYP_IPV4
Definition: nat_syslog.c:56
SDEL_MSGID
#define SDEL_MSGID
Definition: nat_syslog.c:34
fib_table_get
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:29
IATYP_IPV6
#define IATYP_IPV6
Definition: nat_syslog.c:57
ip.h
u32
unsigned int u32
Definition: types.h:88
syslog_msg_add_sd_param
void syslog_msg_add_sd_param(syslog_msg_t *syslog_msg, char *name, char *fmt,...)
Add structured data elemnt parameter name-value pair RFC5424 6.3.3.
Definition: syslog.c:111
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
SVLAN_SDPARAM_NAME
#define SVLAN_SDPARAM_NAME
Definition: nat_syslog.c:42
NSESS_SDID
#define NSESS_SDID
Definition: nat_syslog.c:38
syslog_is_enabled
static int syslog_is_enabled(void)
Check if syslog logging is enabled.
Definition: syslog.h:197
syslog_msg_t
syslog message
Definition: syslog.h:96
nat_syslog_nat44_apmdel
void nat_syslog_nat44_apmdel(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, nat_protocol_t proto)
Definition: nat_syslog.c:116
XSPORT_SDPARAM_NAME
#define XSPORT_SDPARAM_NAME
Definition: nat_syslog.c:50
u8
unsigned char u8
Definition: types.h:56
APMADD_MSGID
#define APMADD_MSGID
Definition: nat_syslog.c:35
format_ip6_address
format_function_t format_ip6_address
Definition: format.h:91
syslog_msg_sd_init
void syslog_msg_sd_init(syslog_msg_t *syslog_msg, char *sd_id)
Initialize structured data element.
Definition: syslog.c:101
XDPORT_SDPARAM_NAME
#define XDPORT_SDPARAM_NAME
Definition: nat_syslog.c:52
XDADDR_SDPARAM_NAME
#define XDADDR_SDPARAM_NAME
Definition: nat_syslog.c:51
nat_syslog_nat44_sdel
void nat_syslog_nat44_sdel(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, nat_protocol_t proto, u8 is_twicenat)
Definition: nat_syslog.c:209
XSADDR_SDPARAM_NAME
#define XSADDR_SDPARAM_NAME
Definition: nat_syslog.c:49
syslog_msg_send
int syslog_msg_send(syslog_msg_t *syslog_msg)
Send syslog message.
Definition: syslog.c:159
nat_syslog_nat44_apmap
static void nat_syslog_nat44_apmap(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, nat_protocol_t proto, u8 is_add, ip6_address_t *sv6enc)
Definition: nat_syslog.c:60
SADD_MSGID
#define SADD_MSGID
Definition: nat_syslog.c:33
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
APMADD_APMDEL_SEVERITY
#define APMADD_APMDEL_SEVERITY
Definition: nat_syslog.c:31
SV6ENC_SDPARAM_NAME
#define SV6ENC_SDPARAM_NAME
Definition: nat_syslog.c:54
NAT_FACILITY
#define NAT_FACILITY
Definition: nat_syslog.c:26
IDPORT_SDPARAM_NAME
#define IDPORT_SDPARAM_NAME
Definition: nat_syslog.c:47
XATYP_SDPARAM_NAME
#define XATYP_SDPARAM_NAME
Definition: nat_syslog.c:48
nat_syslog.h
NAT syslog logging.