Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output: Add linktype name #11994

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3472,6 +3472,10 @@
"properties": {
"linktype": {
"type": "integer"
},
"linktype_name": {
"type": "string",
"description": "the descriptive name of the linktype"
}
},
"additionalProperties": false
Expand Down
71 changes: 71 additions & 0 deletions rust/src/utils/datalink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

// Author: Jeff Lucovsky <[email protected]>

use std::collections::HashMap;
use std::ffi::{CString, CStr};
use std::os::raw::c_char;
use std::ptr;

#[no_mangle]
pub extern "C" fn SCDatalinkInit() -> *mut HashMap<i32, CString> {
let map: HashMap<i32, CString> = HashMap::new();
Box::into_raw(Box::new(map))
}

#[no_mangle]
pub unsafe extern "C" fn SCDatalinkValueNameInsert(
map: *mut HashMap<i32, CString>,
key: i32,
value: *const c_char,
) {
if map.is_null() {
return;
}

if value.is_null() {
return;
}

let map = &mut *map;
let c_str = CStr::from_ptr(value);

let value = CString::new(c_str.to_str().unwrap()).unwrap();
map.insert(key, value);
}

#[no_mangle]
pub unsafe extern "C" fn SCDatalinkValueToName(map: *mut HashMap<i32, CString>, key: i32) -> *const c_char {
if map.is_null() {
return std::ptr::null_mut();
}

let map = &mut *map;
match map.get(&key) {
Some(value) => value.as_ptr(),
None => ptr::null(),
}
}

#[no_mangle]
pub unsafe extern "C" fn SCDatalinkDeInit(map: *mut HashMap<i32, CString>) {
if !map.is_null() {
drop(Box::from_raw(map)); // Automatically dropped at end of scope
}
}

1 change: 1 addition & 0 deletions rust/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
*/

pub mod base64;
pub mod datalink;
40 changes: 1 addition & 39 deletions src/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "util-debug.h"
#include "decode-events.h"
#include "util-exception-policy-types.h"
#include "util-datalink.h"
#ifdef PROFILING
#include "flow-worker.h"
#include "app-layer-protos.h"
Expand Down Expand Up @@ -1205,45 +1206,6 @@ void DecodeUnregisterCounters(void);
#define IPPROTO_SHIM6 140
#endif

/* pcap provides this, but we don't want to depend on libpcap */
#ifndef DLT_EN10MB
#define DLT_EN10MB 1
#endif

#ifndef DLT_C_HDLC
#define DLT_C_HDLC 104
#endif

/* taken from pcap's bpf.h */
#ifndef DLT_RAW
#ifdef __OpenBSD__
#define DLT_RAW 14 /* raw IP */
#else
#define DLT_RAW 12 /* raw IP */
#endif
#endif

#ifndef DLT_NULL
#define DLT_NULL 0
#endif

/** libpcap shows us the way to linktype codes
* \todo we need more & maybe put them in a separate file? */
#define LINKTYPE_NULL DLT_NULL
#define LINKTYPE_ETHERNET DLT_EN10MB
#define LINKTYPE_LINUX_SLL 113
#define LINKTYPE_PPP 9
#define LINKTYPE_RAW DLT_RAW
/* http://www.tcpdump.org/linktypes.html defines DLT_RAW as 101, yet others don't.
* Libpcap on at least OpenBSD returns 101 as datalink type for RAW pcaps though. */
#define LINKTYPE_RAW2 101
#define LINKTYPE_IPV4 228
#define LINKTYPE_IPV6 229
#define LINKTYPE_GRE_OVER_IP 778
#define LINKTYPE_CISCO_HDLC DLT_C_HDLC
#define PPP_OVER_GRE 11
#define VLAN_OVER_GRE 13

/* Packet Flags */

/** Flag to indicate that packet header or contents should not be inspected */
Expand Down
7 changes: 7 additions & 0 deletions src/output-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,15 @@ void EvePacket(const Packet *p, JsonBuilder *js, uint32_t max_length)
return;
}
if (!jb_set_uint(js, "linktype", p->datalink)) {
jb_close(js);
return;
}

const char *dl_name = DatalinkValueToName(p->datalink);
// Intentionally ignore the return value from jb_set_string and proceed
// so the jb object is closed
jb_set_string(js, "linktype_name", dl_name == NULL ? "n/a" : dl_name);

jb_close(js);
}

Expand Down
2 changes: 2 additions & 0 deletions src/suricata.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ void GlobalsDestroy(void)
TmqhCleanup();
TmModuleRunDeInit();
ParseSizeDeinit();
DatalinkTableDeinit();

#ifdef HAVE_DPDK
DPDKCleanupEAL();
Expand Down Expand Up @@ -2876,6 +2877,7 @@ int InitGlobal(void)

/* Initialize the configuration module. */
ConfInit();
DatalinkTableInit();

VarNameStoreInit();

Expand Down
29 changes: 29 additions & 0 deletions src/util-datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "suricata-common.h"
#include "util-datalink.h"
#include "rust.h"
#include "decode.h"

int g_datalink_value = LINKTYPE_NULL;
Expand All @@ -42,3 +43,31 @@ bool DatalinkHasMultipleValues(void)
{
return g_datalink_is_multiple == 1;
}

static void *datalink_value_map;

void DatalinkTableInit(void)
{
datalink_value_map = SCDatalinkInit();
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_NULL, "NULL");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_ETHERNET, "EN10MB");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_LINUX_SLL, "LINUX_SLL");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_PPP, "PPP");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_RAW, "RAW");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_RAW2, "RAW2");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_GRE_OVER_IP, "GRE_RAW");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_NULL, "NULL");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_CISCO_HDLC, "C_HDLC");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_IPV4, "IPv4");
SCDatalinkValueNameInsert(datalink_value_map, LINKTYPE_IPV6, "IPv6");
}

void DatalinkTableDeinit(void)
{
SCDatalinkDeInit(datalink_value_map);
}

const char *DatalinkValueToName(int datalink_value)
{
return SCDatalinkValueToName(datalink_value_map, datalink_value);
}
42 changes: 42 additions & 0 deletions src/util-datalink.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,50 @@
#ifndef SURICATA_UTIL_DATALINK_H
#define SURICATA_UTIL_DATALINK_H

#include "util-debug.h"

/* pcap provides this, but we don't want to depend on libpcap */
#ifndef DLT_EN10MB
#define DLT_EN10MB 1
#endif

#ifndef DLT_C_HDLC
#define DLT_C_HDLC 104
#endif

/* taken from pcap's bpf.h */
#ifndef DLT_RAW
#ifdef __OpenBSD__
#define DLT_RAW 14 /* raw IP */
#else
#define DLT_RAW 12 /* raw IP */
#endif
#endif

#ifndef DLT_NULL
#define DLT_NULL 0
#endif

/** libpcap shows us the way to linktype codes
* \todo we need more & maybe put them in a separate file? */
#define LINKTYPE_NULL DLT_NULL
#define LINKTYPE_ETHERNET DLT_EN10MB
#define LINKTYPE_LINUX_SLL 113
#define LINKTYPE_PPP 9
#define LINKTYPE_RAW DLT_RAW
/* http://www.tcpdump.org/linktypes.html defines DLT_RAW as 101, yet others don't.
* Libpcap on at least OpenBSD returns 101 as datalink type for RAW pcaps though. */
#define LINKTYPE_RAW2 101
#define LINKTYPE_IPV4 228
#define LINKTYPE_IPV6 229
#define LINKTYPE_GRE_OVER_IP 778
#define LINKTYPE_CISCO_HDLC DLT_C_HDLC

void DatalinkSetGlobalType(int datalink);
int DatalinkGetGlobalType(void);
bool DatalinkHasMultipleValues(void);
void DatalinkTableInit(void);
void DatalinkTableDeinit(void);
const char *DatalinkValueToName(int datalink_value);

#endif /* SURICATA_UTIL_DATALINK_H */
Loading