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

Packet capture prototype #229

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions abi/include/abi/ipc/interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ typedef enum {
FOURCC_COMPACT('w', 'm', 'g', 't') | IFACE_EXCHANGE_SERIALIZE,
INTERFACE_WNDMGT_CB =
FOURCC_COMPACT('w', 'm', 'g', 't') | IFACE_EXCHANGE_SERIALIZE | IFACE_MOD_CALLBACK,
INTERFACE_PCAP_CONTROL =
FOURCC_COMPACT('p', 'c', 't', 'l') | IFACE_EXCHANGE_SERIALIZE,
} iface_t;

#endif
Expand Down
1 change: 1 addition & 0 deletions uspace/app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ apps = [
'netecho',
'nic',
'nterm',
'pcapctl',
'ofw',
'pci',
'ping',
Expand Down
4 changes: 4 additions & 0 deletions uspace/app/pcapctl/doc/doxygroups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @addtogroup pcapctl pcapctl
* @brief Dump network packets
* @ingroup apps
*/
100 changes: 100 additions & 0 deletions uspace/app/pcapctl/main.c
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, ...) \
Copy link
Contributor

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.

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;
}

/** @}
*/
29 changes: 29 additions & 0 deletions uspace/app/pcapctl/meson.build
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')
19 changes: 18 additions & 1 deletion uspace/drv/nic/e1k/e1k.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion uspace/drv/nic/e1k/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

deps = [ 'nic' ]
deps = [ 'nic' , 'pcap' ]
src = files('e1k.c')
2 changes: 1 addition & 1 deletion uspace/lib/c/include/ipc/services.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef enum {
#define SERVICE_NAME_TCP "net/tcp"
#define SERVICE_NAME_VBD "vbd"
#define SERVICE_NAME_VOLSRV "volsrv"

#define SERVICE_NAME_DUMPPCAP "dumppcap"
#endif

/** @}
Expand Down
1 change: 1 addition & 0 deletions uspace/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ libs = [
'math',
'minix',
'nettl',
'pcap',
'ofw',
'pcm',
'pcut',
Expand Down
3 changes: 3 additions & 0 deletions uspace/lib/pcap/doc/doxygoups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @addtogroup libpcap libpcap
* @ingroup libs
*/
103 changes: 103 additions & 0 deletions uspace/lib/pcap/include/pcap.h
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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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

/** @}
*/
55 changes: 55 additions & 0 deletions uspace/lib/pcap/include/pcap_iface.h
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
/** @}
*/
Loading