1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <arpa/inet.h>
#include "nsfw_init.h"
#include "nsfw_maintain_api.h"
#include "nsfw_mem_api.h"
#include "nsfw_fd_timer_api.h"
#include "nstack_log.h"
#include "nstack_securec.h"
#include "dump_tool.h"
#include "nstack_dmm_adpt.h"
NSTACK_STATIC u32 g_dump_task_mask = 0;
NSTACK_STATIC dump_task_info g_dump_task[MAX_DUMP_TASK];
static dump_timer_info g_dump_timer;
static nsfw_mem_name g_dump_mem_ring_info =
{ NSFW_SHMEM, NSFW_PROC_MAIN, DUMP_SHMEM_RIGN_NAME };
static nsfw_mem_name g_dump_mem_pool_info =
{ NSFW_SHMEM, NSFW_PROC_MAIN, DUMP_SHMEM_POOL_NAME };
NSTACK_STATIC inline void
dump_task_init (dump_task_info * task)
{
task->task_state = 0;
task->task_id = -1;
task->task_keep_time = 0;
task->task_pool = NULL;
task->task_queue = NULL;
}
NSTACK_STATIC inline void
clear_dump_task ()
{
int i = 0;
for (; i < MAX_DUMP_TASK; i++)
{
dump_task_init (&g_dump_task[i]);
}
}
NSTACK_STATIC i16
get_dump_task (nsfw_tool_dump_msg * req)
{
// base version just support 1 dump task
if (req->task_keep_time > MAX_DUMP_TIME
|| req->task_keep_time < MIN_DUMP_TIME)
{
NSPOL_DUMP_LOGERR ("task keep time invalid] time=%u.",
req->task_keep_time);
return -1;
}
if (1 == g_dump_task[0].task_state)
{
NSPOL_DUMP_LOGERR
("start tcpdump task failed, task still in work state] task id=%d.",
0);
return -1;
}
struct timespec cur_time;
GET_CUR_TIME (&cur_time); /*do not need return value */
g_dump_task[0].task_start_sec = cur_time.tv_sec;
g_dump_task[0].last_hbt_sec = cur_time.tv_sec;
g_dump_task[0].task_state = 1;
g_dump_task[0].task_id = 0;
g_dump_task[0].task_keep_time = req->task_keep_time;
g_dump_task_mask |= (1 << g_dump_task[0].task_id);
NSPOL_DUMP_LOGINF ("start tcpdump task success] task id=%d.", 0);
return 0;
}
NSTACK_STATIC i16
close_dump_task (i16 task_id)
{
if (task_id < 0 || task_id >= MAX_DUMP_TASK)
{
NSPOL_DUMP_LOGERR
("receive invalid task id in close dump task req] task id=%d.",
task_id);
return -1;
}
g_dump_task[task_id].task_state = 0;
g_dump_task_mask &= ~(1 << task_id);
NSPOL_DUMP_LOGINF ("stop tcpdump task success] task id=%d.", 0);
return task_id;
}
NSTACK_STATIC void
stop_expired_task (int idx, u32 now_sec)
{
dump_task_info *ptask = &g_dump_task[idx];
if (0 == ptask->task_state)
{
return;
}
if (now_sec - ptask->task_start_sec > ptask->task_keep_time)
{
NSPOL_DUMP_LOGERR
("stop dump task because task time expired] task id=%d, now_sec=%u, task start time=%u, dump time=%u.",
ptask->task_id, now_sec, ptask->task_start_sec,
ptask->task_keep_time);
close_dump_task (ptask->task_id);
return;
}
if (now_sec - ptask->last_hbt_sec > DUMP_TASK_HBT_TIME_OUT)
{
NSPOL_DUMP_LOGERR
("stop dump task because heart beat time out] task id=%d, now_sec=%u, last hbt time=%u, hbt timeout=%u.",
ptask->task_id, now_sec, ptask->last_hbt_sec,
DUMP_TASK_HBT_TIME_OUT);
close_dump_task (ptask->task_id);
}
return;
}
NSTACK_STATIC inline bool
check_dump_alive (u32 timer_type, void *data)
{
dump_timer_info *ptimer_info = (dump_timer_info *) data;
struct timespec cur_time;
GET_CUR_TIME (&cur_time); /*do not need return value */
int i = 0;
for (; i < MAX_DUMP_TASK; i++)
{
stop_expired_task (i, cur_time.tv_sec);
}
ptimer_info->ptimer =
nsfw_timer_reg_timer (1, ptimer_info, check_dump_alive,
*(struct timespec *) (ptimer_info->interval));
return true;
}
NSTACK_STATIC bool
dump_init_timer (dump_timer_info * ptimer_info)
{
struct timespec *trigger_time =
(struct timespec *) malloc (sizeof (struct timespec));
if (NULL == trigger_time)
{
NSPOL_DUMP_LOGERR ("alloc memory for timer failed.");
return false;
}
trigger_time->tv_sec = DUMP_HBT_CHK_INTERVAL;
trigger_time->tv_nsec = 0;
ptimer_info->interval = trigger_time;
ptimer_info->ptimer =
nsfw_timer_reg_timer (1, ptimer_info, check_dump_alive, *trigger_time);
return true;
}
NSTACK_STATIC inline u32
copy_limit_buf (char *dst_buf, int dst_buf_len, char *src_buf,
u32 src_buf_len, u32 limit_len)
{
if (src_buf_len < limit_len)
{
NSPOL_DUMP_LOGERR ("message too short] len=%d", src_buf_len);
return 0;
}
if (EOK != MEMCPY_S (dst_buf, dst_buf_len, src_buf, limit_len))
{
NSPOL_DUMP_LOGERR ("MEMCPY_S failed");
return 0;
}
return limit_len;
}
NSTACK_STATIC inline u32
get_packet_buf (char *dst_buf, int dst_buf_len, char *src_buf,
u32 src_buf_len, u32 eth_head_len)
{
#define TCP_BUF_LEN (eth_head_len + IP_HEAD_LEN + TCP_HEAD_LEN)
#define UDP_BUF_LEN (eth_head_len + IP_HEAD_LEN + UDP_HEAD_LEN)
#define ICMP_BUF_LEN (eth_head_len + IP_HEAD_LEN + ICMP_HEAD_LEN)
if (NULL == dst_buf || NULL == src_buf)
{
return 0;
}
struct ip_hdr *ip_head = (struct ip_hdr *) (src_buf + eth_head_len);
if (ip_head->_proto == IP_PROTO_TCP)
{
return copy_limit_buf (dst_buf, dst_buf_len, src_buf, src_buf_len,
TCP_BUF_LEN);
}
if (ip_head->_proto == IP_PROTO_UDP)
{
return copy_limit_buf (dst_buf, dst_buf_len, src_buf, src_buf_len,
UDP_BUF_LEN);
}
if (ip_head->_proto == IP_PROTO_ICMP)
{
return copy_limit_buf (dst_buf, dst_buf_len, src_buf, src_buf_len,
ICMP_BUF_LEN);
}
if (EOK != MEMCPY_S (dst_buf, dst_buf_len, src_buf, src_buf_len))
{
NSPOL_DUMP_LOGERR ("MEMCPY_S failed");
return 0;
}
return src_buf_len;
}
NSTACK_STATIC int
pack_msg_inout (void *dump_buf, char *msg_buf, u32 len, u16 direction,
void *para)
{
(void) para;
dump_msg_info *pmsg = (dump_msg_info *) dump_buf;
if (!pmsg)
{
return 0;
}
pmsg->direction = direction;
struct timeval cur_time;
gettimeofday (&cur_time, NULL);
pmsg->dump_sec = cur_time.tv_sec;
pmsg->dump_usec = cur_time.tv_usec;
/* msg content can not be captured */
u32 real_len =
get_packet_buf (pmsg->buf, DUMP_MSG_SIZE, msg_buf, len, ETH_HEAD_LEN);
if (0 == real_len)
{
return 0;
}
pmsg->len = real_len;
return 1;
}
NSTACK_STATIC int
pack_msg_loop (void *dump_buf, char *msg_buf, u32 len, u16 direction,
void *para)
{
dump_msg_info *pmsg = (dump_msg_info *) dump_buf;
if (!pmsg)
{
return 0;
}
pmsg->direction = direction;
struct timeval cur_time;
gettimeofday (&cur_time, NULL);
pmsg->dump_sec = cur_time.tv_sec;
pmsg->dump_usec = cur_time.tv_usec;
eth_head pack_eth_head;
int retVal =
MEMCPY_S (pack_eth_head.dest_mac, MAC_ADDR_LEN, para, MAC_ADDR_LEN);
if (EOK != retVal)
{
NSPOL_DUMP_LOGERR ("MEMCPY_S failed]retVal=%d", retVal);
return 0;
}
retVal = MEMCPY_S (pack_eth_head.src_mac, MAC_ADDR_LEN, para, MAC_ADDR_LEN);
if (EOK != retVal)
{
NSPOL_DUMP_LOGERR ("MEMCPY_S failed]retVal=%d", retVal);
return 0;
}
pack_eth_head.eth_type = htons (PROTOCOL_IP);
retVal =
MEMCPY_S (pmsg->buf, DUMP_MSG_SIZE, &pack_eth_head, sizeof (eth_head));
if (EOK != retVal)
{
NSPOL_DUMP_LOGERR ("MEMCPY_S failed]retVal=%d", retVal);
return 0;
}
u32 buf_len = DUMP_MSG_SIZE - ETH_HEAD_LEN;
/* msg content can not be captured- Begin */
u32 real_len =
get_packet_buf (pmsg->buf + ETH_HEAD_LEN, buf_len, msg_buf, len, 0);
if (0 == real_len)
{
return 0;
}
pmsg->len = real_len + ETH_HEAD_LEN;
return 1;
}
NSTACK_STATIC void
dump_enqueue (int task_idx, void *buf, u32 len, u16 direction,
pack_msg_fun pack_msg, void *para)
{
mring_handle queue = (mring_handle *) g_dump_task[task_idx].task_queue;
mring_handle pool = (mring_handle *) g_dump_task[task_idx].task_pool;
void *msg = NULL;
if (nsfw_mem_ring_dequeue (pool, &msg) <= 0)
{
// such log may be too much if queue is empty
NSPOL_DUMP_LOGDBG ("get msg node from mem pool failed] pool=%p.", pool);
return;
}
if (NULL == msg)
{
NSPOL_DUMP_LOGWAR ("get NULL msg node from mem pool] pool=%p.", pool);
return;
}
if (!pack_msg (msg, buf, len, direction, para))
{
NSPOL_DUMP_LOGWAR ("get dump msg failed");
return;
}
if (nsfw_mem_ring_enqueue (queue, msg) < 0)
{
NSPOL_DUMP_LOGWAR ("dump mem ring enqueue failed] ring=%p.", queue);
}
return;
}
NSTACK_STATIC inline bool
dump_enabled ()
{
return (0 != g_dump_task_mask);
}
void
ntcpdump (void *buf, u32 buf_len, u16 direction)
{
u32 i;
if (!dump_enabled ())
{
return;
}
/* fix Dead-code type Codedex issue here */
for (i = 0; i < MAX_DUMP_TASK; i++)
{
if (g_dump_task[i].task_state)
{
dump_enqueue (i, buf, buf_len, direction, pack_msg_inout, NULL);
}
}
}
void
ntcpdump_loop (void *buf, u32 buf_len, u16 direction, void *eth_addr)
{
u32 i;
if (!dump_enabled ())
{
return;
}
/* fix Dead-code type Codedex issue here */
for (i = 0; i < MAX_DUMP_TASK; i++)
{
if (g_dump_task[i].task_state)
{
dump_enqueue (i, buf, buf_len, direction, pack_msg_loop, eth_addr);
}
}
}
// called by nStackMain
bool
dump_create_pool ()
{
nsfw_mem_sppool pool_info;
if (EOK !=
MEMCPY_S (&pool_info.stname, sizeof (nsfw_mem_name),
&g_dump_mem_pool_info, sizeof (nsfw_mem_name)))
{
NSPOL_DUMP_LOGERR ("create dump mem pool failed, MEMCPY_S failed.");
return false;
}
pool_info.usnum = DUMP_MSG_NUM;
pool_info.useltsize = DUMP_MSG_SIZE + sizeof (dump_msg_info);
pool_info.isocket_id = NSFW_SOCKET_ANY;
pool_info.enmptype = NSFW_MRING_MPSC;
mring_handle pool = nsfw_mem_sp_create (&pool_info);
if (NULL == pool)
{
NSPOL_DUMP_LOGERR ("create dump mem pool failed, pool create failed.");
return false;
}
g_dump_task[0].task_pool = pool;
NSPOL_DUMP_LOGINF ("dump pool create success] pool=%p.", pool);
return true;
}
bool
dump_create_ring ()
{
nsfw_mem_mring ring_info;
if (EOK !=
MEMCPY_S (&ring_info.stname, sizeof (nsfw_mem_name),
&g_dump_mem_ring_info, sizeof (nsfw_mem_name)))
{
NSPOL_DUMP_LOGERR ("create dump mem ring failed, MEMCPY_S failed.");
return false;
}
ring_info.usnum = DUMP_MSG_NUM;
ring_info.isocket_id = NSFW_SOCKET_ANY;
ring_info.enmptype = NSFW_MRING_MPSC;
mring_handle ring = nsfw_mem_ring_create (&ring_info);
if (NULL == ring)
{
NSPOL_DUMP_LOGERR ("create dump mem ring failed, ring create failed.");
return false;
}
g_dump_task[0].task_queue = ring;
NSPOL_DUMP_LOGINF ("dump ring create success] ring=%p.", ring);
return true;
}
NSTACK_STATIC int
on_dump_tool_req (nsfw_mgr_msg * req)
{
i16 task_id = 0;
if (!req)
{
return -1;
}
if (req->src_proc_type != NSFW_PROC_TOOLS)
{
NSPOL_DUMP_LOGDBG
("dump module receive invaild message] module type=%u.",
req->src_proc_type);
return -1;
}
if (req->msg_type != MGR_MSG_TOOL_TCPDUMP_REQ)
{
NSPOL_DUMP_LOGDBG ("dump module receive invaild message] msg type=%u.",
req->msg_type);
return -1;
}
nsfw_tool_dump_msg *dump_msg_req = GET_USER_MSG (nsfw_tool_dump_msg, req);
switch (dump_msg_req->op_type)
{
case START_DUMP_REQ:
task_id = get_dump_task (dump_msg_req);
break;
case STOP_DUMP_REQ:
task_id = close_dump_task (dump_msg_req->task_id);
break;
default:
task_id = -1;
}
nsfw_mgr_msg *rsp = nsfw_mgr_rsp_msg_alloc (req);
if (NULL == rsp)
{
NSPOL_DUMP_LOGDBG ("alloc response for dump request failed.");
return -1;
}
nsfw_tool_dump_msg *dump_msg_rsp = GET_USER_MSG (nsfw_tool_dump_msg, rsp);
dump_msg_rsp->op_type = dump_msg_req->op_type + DUMP_MSG_TYPE_RSP;
dump_msg_rsp->task_id = task_id;
nsfw_mgr_send_msg (rsp);
nsfw_mgr_msg_free (rsp);
return 0;
}
NSTACK_STATIC int
on_dump_hbt_req (nsfw_mgr_msg * req)
{
if (!req)
{
return -1;
}
if (req->src_proc_type != NSFW_PROC_TOOLS)
{
NSPOL_DUMP_LOGDBG
("dump module receive invaild message] module type=%u.",
req->src_proc_type);
return -1;
}
if (req->msg_type != MGR_MSG_TOOL_HEART_BEAT)
{
NSPOL_DUMP_LOGDBG ("dump module receive invaild message] msg type=%u.",
req->msg_type);
return -1;
}
nsfw_tool_hbt *dump_hbt_req = GET_USER_MSG (nsfw_tool_hbt, req);
i16 task_id = dump_hbt_req->task_id;
if (task_id < 0 || task_id >= MAX_DUMP_TASK)
{
NSPOL_DUMP_LOGERR ("dump heart beat with invalid task id] task id=%d.",
task_id);
return -1;
}
if (0 == g_dump_task[task_id].task_state)
{
NSPOL_DUMP_LOGDBG
("dump module receive heart beat but task not enabled] task id=%d.",
task_id);
return 0;
}
struct timespec cur_time;
GET_CUR_TIME (&cur_time); /*no need return value */
// update task alive time
g_dump_task[task_id].last_hbt_seq = dump_hbt_req->seq;
g_dump_task[task_id].last_hbt_sec = cur_time.tv_sec;
return 0;
}
NSTACK_STATIC int dump_tool_init (void *param);
NSTACK_STATIC int
dump_tool_init (void *param)
{
u32 proc_type = (u32) ((long long) param);
NSPOL_DUMP_LOGINF ("dump module init] proc type=%d", proc_type);
switch (proc_type)
{
case NSFW_PROC_MAIN:
nsfw_mgr_reg_msg_fun (MGR_MSG_TOOL_TCPDUMP_REQ, on_dump_tool_req);
nsfw_mgr_reg_msg_fun (MGR_MSG_TOOL_HEART_BEAT, on_dump_hbt_req);
break;
default:
NSPOL_DUMP_LOGERR ("dump init with unknow module] proc type=%d",
proc_type);
return -1;
}
clear_dump_task ();
if (!dump_create_ring ())
{
return -1;
}
if (!dump_create_pool ())
{
return -1;
}
if (!dump_init_timer (&g_dump_timer))
{
return -1;
}
NSPOL_DUMP_LOGINF ("dump module init success.");
return 0;
}
/* *INDENT-OFF* */
NSFW_MODULE_NAME (TCPDUMP_MODULE)
NSFW_MODULE_PRIORITY (10)
NSFW_MODULE_DEPENDS (NSTACK_DMM_MODULE)
NSFW_MODULE_INIT (dump_tool_init)
/* *INDENT-ON* */
|