-
Notifications
You must be signed in to change notification settings - Fork 141
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
Packet capture prototype #229
base: master
Are you sure you want to change the base?
Changes from 2 commits
42c2e65
dc5c303
6b66de6
86f862c
f2cb80a
192019f
325ea9c
6eab537
e846bec
e4cc266
78edb5e
8d9217d
384f081
34aad53
6c1e7c0
c37c24c
e55d2c1
2a0c827
ebb1489
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ apps = [ | |
'netecho', | ||
'nic', | ||
'nterm', | ||
'pcapctl', | ||
'ofw', | ||
'pci', | ||
'ping', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** @addtogroup pcapctl pcapctl | ||
* @brief Dump network packets | ||
* @ingroup apps | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2023 Nataliia Korop | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* - The name of the author may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** @addtogroup pcapctl | ||
* @{ | ||
*/ | ||
/** @file pcapctl app | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <str.h> | ||
#include <errno.h> | ||
|
||
#include "pcapctl_dump.h" | ||
|
||
#define NAME "pcapctl" | ||
|
||
#define LOGGER(msg, ...) \ | ||
fprintf(stderr, \ | ||
"[PCAP %s:%d]: " msg "\n", \ | ||
__FILE__, __LINE__, \ | ||
##__VA_ARGS__\ | ||
) | ||
|
||
pcapctl_sess_t sess; | ||
|
||
static void start_dumping(const char *name) | ||
{ | ||
pcapctl_dump_start(name, &sess); | ||
} | ||
|
||
static void stop_dumping(void) | ||
{ | ||
pcapctl_dump_stop(&sess); | ||
} | ||
|
||
static void usage(const char *progname) | ||
{ | ||
fprintf(stderr, "Usage:\n"); | ||
fprintf(stderr, " %s start <outfile>: Packets will be written to <outfile>\n", progname); | ||
fprintf(stderr, " %s stop: Dumping stops\n", progname); | ||
|
||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc < 2) { | ||
usage(argv[0]); | ||
return 1; | ||
} else { | ||
errno_t rc = pcapctl_dump_init(&sess); | ||
if (rc != EOK) { | ||
fprintf(stderr, "Error initializing ...\n"); | ||
return 1; | ||
} | ||
if (str_cmp(argv[1], "start") == 0) { | ||
if (argc != 3) { | ||
usage(argv[0]); | ||
return 1; | ||
} | ||
start_dumping(argv[2]); | ||
} else if (str_cmp(argv[1], "stop") == 0) { | ||
stop_dumping(); | ||
fprintf(stdout, "Dumping was stopped\n"); | ||
return EOK; | ||
} else { | ||
usage(argv[0]); | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
/** @} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# | ||
# Copyright (c) 2023 Nataliia Korop | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions | ||
# are met: | ||
# | ||
# - Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# - Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# - The name of the author may not be used to endorse or promote products | ||
# derived from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
deps = ['pcap'] | ||
src = files('main.c') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,8 @@ | |
#include <ops/nic.h> | ||
#include "e1k.h" | ||
|
||
#include "pcapdump_iface.h" | ||
#include "pcap_iface.h" | ||
#define NAME "e1k" | ||
|
||
#define E1000_DEFAULT_INTERRUPT_INTERVAL_USEC 250 | ||
|
@@ -173,6 +175,10 @@ typedef struct { | |
|
||
/** Lock for EEPROM access */ | ||
fibril_mutex_t eeprom_lock; | ||
|
||
/** Interface for dumping packets */ | ||
pcap_iface_t pcapdump; | ||
|
||
} e1000_t; | ||
|
||
/** Global mutex for work with shared irq structure */ | ||
|
@@ -1188,6 +1194,8 @@ static void e1000_receive_frames(nic_t *nic) | |
nic_frame_t *frame = nic_alloc_frame(nic, frame_size); | ||
if (frame != NULL) { | ||
memcpy(frame->data, e1000->rx_frame_virt[next_tail], frame_size); | ||
pcapdump_packet(&e1000->pcapdump, frame->data, frame->size); | ||
|
||
nic_received_frame(nic, frame); | ||
} else { | ||
ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped."); | ||
|
@@ -2202,6 +2210,15 @@ errno_t e1000_dev_add(ddf_dev_t *dev) | |
if (rc != EOK) | ||
goto err_add_to_cat; | ||
|
||
errno_t pcap_rc = pcapdump_init(&e1000->pcapdump); | ||
|
||
if (pcap_rc != EOK) { | ||
printf("Failed creating pcapdump port\n"); | ||
} | ||
rc = ddf_fun_add_to_category(fun, "pcap"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again I would expect this to be handled inside libnic. |
||
if (rc != EOK) | ||
goto err_add_to_cat; | ||
|
||
return EOK; | ||
|
||
err_add_to_cat: | ||
|
@@ -2365,7 +2382,7 @@ static void e1000_send_frame(nic_t *nic, void *data, size_t size) | |
} | ||
|
||
memcpy(e1000->tx_frame_virt[tdt], data, size); | ||
|
||
pcapdump_packet(&e1000->pcapdump, data, size); | ||
tx_descriptor_addr->phys_addr = PTR_TO_U64(e1000->tx_frame_phys[tdt]); | ||
tx_descriptor_addr->length = size; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ libs = [ | |
'math', | ||
'minix', | ||
'nettl', | ||
'pcap', | ||
'ofw', | ||
'pcm', | ||
'pcut', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/** @addtogroup libpcap libpcap | ||
* @ingroup libs | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2023 Nataliia Korop | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* - The name of the author may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** | ||
* @addtogroup libpcap | ||
* @{ | ||
*/ | ||
/** | ||
* @file | ||
* @brief Headers and functions for .pcap file and packets to be dumped | ||
*/ | ||
|
||
#ifndef PCAP_H_ | ||
#define PCAP_H_ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <str_error.h> | ||
#include <time.h> | ||
#include <stdbool.h> | ||
#include <errno.h> | ||
|
||
#define PCAP_MAGIC_MICRO 0xA1B2C3D4 | ||
#define PCAP_MAGIC_NANO 0xA1B23C4D | ||
#define PCAP_MAJOR_VERSION 0x0002 | ||
#define PCAP_MINOR_VERSION 0x0004 | ||
#define PCAP_SNAP_LEN 0x00040000 | ||
|
||
#define PCAP_LINKTYPE_ETHERNET 1 /* IEEE 802.3 Ethernet*/ | ||
|
||
/** Header of the .pcap file | ||
*/ | ||
typedef struct { | ||
uint32_t magic_number; | ||
uint16_t major_v; | ||
uint16_t minor_v; | ||
uint32_t reserved1; | ||
uint32_t reserved2; | ||
uint32_t snaplen; | ||
uint32_t additional; /** The LinkType and additional information field is in the form */ | ||
} __attribute__((packed, aligned(4))) pcap_file_header_t; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute is not needed. Since all the structure fields are naturally aligned, there won't be any padding and so packed is not needed. aligned(4) is not needed either, because 4 already is the alignment of the structure. |
||
|
||
typedef struct pcap_packet_header { | ||
uint32_t seconds_stamp; | ||
uint32_t magic_stamp; | ||
uint32_t captured_length; | ||
uint32_t original_length; | ||
} pcap_packet_header_t; | ||
|
||
typedef struct pcap_writer pcap_writer_t; | ||
|
||
typedef struct { | ||
size_t (*write_u32)(struct pcap_writer *, uint32_t); | ||
size_t (*write_u16)(struct pcap_writer *, uint16_t); | ||
size_t (*write_buffer)(struct pcap_writer *, const void *, size_t); | ||
void (*close)(struct pcap_writer *); | ||
|
||
} pcap_writer_ops_t; | ||
|
||
/** Interface for working with .pcap file | ||
*/ | ||
typedef struct pcap_writer { | ||
void *data; | ||
pcap_writer_ops_t *ops; | ||
} pcap_writer_t; | ||
|
||
errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename); | ||
|
||
extern void pcap_writer_add_header(pcap_writer_t *); | ||
extern void pcap_writer_add_packet( | ||
pcap_writer_t *writer, const void *captured_packet, size_t size); | ||
|
||
extern void pcap_set_time(pcap_packet_header_t *header, bool nano); | ||
|
||
#endif | ||
|
||
/** @} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2023 Nataliia Korop | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* - The name of the author may not be used to endorse or promote products | ||
* derived from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** @addtogroup libpcap | ||
* @{ | ||
*/ | ||
/** @file pcap interface | ||
*/ | ||
|
||
#ifndef PCAP_IFACE_H_ | ||
#define PCAP_IFACE_H_ | ||
|
||
#include <errno.h> | ||
#include "pcap.h" | ||
|
||
typedef struct pcap_iface { | ||
bool to_dump; | ||
errno_t (*init)(const char *); | ||
void (*add_packet)(const void *data, size_t size); | ||
void (*fini)(void); | ||
} pcap_iface_t; | ||
|
||
extern void pcap_close_file(void); | ||
extern errno_t pcap_iface_init(pcap_iface_t *); | ||
extern errno_t pcap_init(const char *); | ||
extern void pcap_add_packet(const void *data, size_t size); | ||
|
||
#endif | ||
/** @} | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is used anywhere.