FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
nat_syslog.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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/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 
77  syslog_msg_sd_init (&syslog_msg, NAPMAP_SDID);
78  syslog_msg_add_sd_param (&syslog_msg, SSUBIX_SDPARAM_NAME, "%d", ssubix);
79  if (sv6enc)
80  {
81  syslog_msg_add_sd_param (&syslog_msg, SV6ENC_SDPARAM_NAME, "%U",
82  format_ip6_address, sv6enc);
83  }
84  else
85  {
86  fib = fib_table_get (sfibix, FIB_PROTOCOL_IP4);
87  syslog_msg_add_sd_param (&syslog_msg, SVLAN_SDPARAM_NAME, "%d",
88  fib->ft_table_id);
89  }
91  syslog_msg_add_sd_param (&syslog_msg, ISADDR_SDPARAM_NAME, "%U",
92  format_ip4_address, isaddr);
93  syslog_msg_add_sd_param (&syslog_msg, ISPORT_SDPARAM_NAME, "%d",
94  clib_net_to_host_u16 (isport));
96  syslog_msg_add_sd_param (&syslog_msg, XSADDR_SDPARAM_NAME, "%U",
97  format_ip4_address, xsaddr);
98  syslog_msg_add_sd_param (&syslog_msg, XSPORT_SDPARAM_NAME, "%d",
99  clib_net_to_host_u16 (xsport));
100  syslog_msg_add_sd_param (&syslog_msg, PROTO_SDPARAM_NAME, "%d",
101  nat_proto_to_ip_proto (proto));
102 
103  syslog_msg_send (&syslog_msg);
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 
165  syslog_msg_sd_init (&syslog_msg, NSESS_SDID);
166  syslog_msg_add_sd_param (&syslog_msg, SSUBIX_SDPARAM_NAME, "%d", ssubix);
167  syslog_msg_add_sd_param (&syslog_msg, SVLAN_SDPARAM_NAME, "%d",
168  fib->ft_table_id);
170  syslog_msg_add_sd_param (&syslog_msg, ISADDR_SDPARAM_NAME, "%U",
171  format_ip4_address, isaddr);
172  syslog_msg_add_sd_param (&syslog_msg, ISPORT_SDPARAM_NAME, "%d",
173  clib_net_to_host_u16 (isport));
175  syslog_msg_add_sd_param (&syslog_msg, XSADDR_SDPARAM_NAME, "%U",
176  format_ip4_address, xsaddr);
177  syslog_msg_add_sd_param (&syslog_msg, XSPORT_SDPARAM_NAME, "%d",
178  clib_net_to_host_u16 (xsport));
179  syslog_msg_add_sd_param (&syslog_msg, PROTO_SDPARAM_NAME, "%d",
180  nat_proto_to_ip_proto (proto));
181  syslog_msg_add_sd_param (&syslog_msg, XDADDR_SDPARAM_NAME, "%U",
182  format_ip4_address, xdaddr);
183  syslog_msg_add_sd_param (&syslog_msg, XDPORT_SDPARAM_NAME, "%d",
184  clib_net_to_host_u16 (xdport));
185  if (is_twicenat)
186  {
187  syslog_msg_add_sd_param (&syslog_msg, IDADDR_SDPARAM_NAME, "%U",
188  format_ip4_address, idaddr);
189  syslog_msg_add_sd_param (&syslog_msg, IDPORT_SDPARAM_NAME, "%d",
190  clib_net_to_host_u16 (idport));
191  }
192 
193  syslog_msg_send (&syslog_msg);
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 
240  syslog_msg_sd_init (&syslog_msg, NSESS_SDID);
241  syslog_msg_add_sd_param (&syslog_msg, SVLAN_SDPARAM_NAME, "%d",
242  fib->ft_table_id);
244  syslog_msg_add_sd_param (&syslog_msg, ISADDR_SDPARAM_NAME, "%U",
245  format_ip6_address, isaddr);
246  syslog_msg_add_sd_param (&syslog_msg, ISPORT_SDPARAM_NAME, "%d",
247  clib_net_to_host_u16 (isport));
249  syslog_msg_add_sd_param (&syslog_msg, XSADDR_SDPARAM_NAME, "%U",
250  format_ip4_address, xsaddr);
251  syslog_msg_add_sd_param (&syslog_msg, XSPORT_SDPARAM_NAME, "%d",
252  clib_net_to_host_u16 (xsport));
253  syslog_msg_add_sd_param (&syslog_msg, PROTO_SDPARAM_NAME, "%d", proto);
254  syslog_msg_add_sd_param (&syslog_msg, XDADDR_SDPARAM_NAME, "%U",
255  format_ip4_address, xdaddr);
256  syslog_msg_add_sd_param (&syslog_msg, XDPORT_SDPARAM_NAME, "%d",
257  clib_net_to_host_u16 (xdport));
258 
259  syslog_msg_send (&syslog_msg);
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  */
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
#define XSADDR_SDPARAM_NAME
Definition: nat_syslog.c:49
#define NAPMAP_SDID
Definition: nat_syslog.c:39
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
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
#define SDEL_MSGID
Definition: nat_syslog.c:34
nat_protocol_t
Definition: lib.h:41
#define NSESS_SDID
Definition: nat_syslog.c:38
unsigned char u8
Definition: types.h:56
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
#define SSUBIX_SDPARAM_NAME
Definition: nat_syslog.c:41
format_function_t format_ip4_address
Definition: format.h:73
#define APMADD_APMDEL_SEVERITY
Definition: nat_syslog.c:31
#define SADD_MSGID
Definition: nat_syslog.c:33
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
#define IDPORT_SDPARAM_NAME
Definition: nat_syslog.c:47
unsigned int u32
Definition: types.h:88
#define IATYP_IPV6
Definition: nat_syslog.c:57
#define NAT_FACILITY
Definition: nat_syslog.c:26
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
#define XSPORT_SDPARAM_NAME
Definition: nat_syslog.c:50
int syslog_msg_send(syslog_msg_t *syslog_msg)
Send syslog message.
Definition: syslog.c:159
vl_api_ip_proto_t proto
Definition: acl_types.api:50
#define SVLAN_SDPARAM_NAME
Definition: nat_syslog.c:42
#define XDPORT_SDPARAM_NAME
Definition: nat_syslog.c:52
unsigned short u16
Definition: types.h:57
#define XDADDR_SDPARAM_NAME
Definition: nat_syslog.c:51
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
#define IATYP_IPV4
Definition: nat_syslog.c:56
void syslog_msg_sd_init(syslog_msg_t *syslog_msg, char *sd_id)
Initialize structured data element.
Definition: syslog.c:101
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
#define SV6ENC_SDPARAM_NAME
Definition: nat_syslog.c:54
RFC5424 syslog protocol declarations.
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
format_function_t format_ip6_address
Definition: format.h:91
#define XATYP_SDPARAM_NAME
Definition: nat_syslog.c:48
#define APMDEL_MSGID
Definition: nat_syslog.c:36
#define SADD_SDEL_SEVERITY
Definition: nat_syslog.c:30
#define ISADDR_SDPARAM_NAME
Definition: nat_syslog.c:44
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
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
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
#define IDADDR_SDPARAM_NAME
Definition: nat_syslog.c:46
#define NAT_APPNAME
Definition: nat_syslog.c:28
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
syslog message
Definition: syslog.h:96
static_always_inline u8 nat_proto_to_ip_proto(nat_protocol_t nat_proto)
Definition: inlines.h:35
NAT syslog logging.
#define PROTO_SDPARAM_NAME
Definition: nat_syslog.c:53
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
#define APMADD_MSGID
Definition: nat_syslog.c:35
static u8 * syslog_msg
Definition: main.c:95
#define ISPORT_SDPARAM_NAME
Definition: nat_syslog.c:45
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
static int syslog_severity_filter_block(syslog_severity_t s)
Severity filter test.
Definition: syslog.h:210
static int syslog_is_enabled(void)
Check if syslog logging is enabled.
Definition: syslog.h:197
#define IATYP_SDPARAM_NAME
Definition: nat_syslog.c:43
A protocol Independent FIB table.
Definition: fib_table.h:71