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 6 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')
16 changes: 15 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,7 @@ typedef struct {

/** Lock for EEPROM access */
fibril_mutex_t eeprom_lock;

} e1000_t;

/** Global mutex for work with shared irq structure */
Expand Down Expand Up @@ -1188,6 +1191,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(nic_get_pcap_iface(nic), frame->data, frame->size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call the packet capture hooks here? Any reason not to do it inside libnic? Then it would work for all the drivers.


nic_received_frame(nic, frame);
} else {
ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped.");
Expand Down Expand Up @@ -2202,6 +2207,15 @@ errno_t e1000_dev_add(ddf_dev_t *dev)
if (rc != EOK)
goto err_add_to_cat;

errno_t pcap_rc = pcapdump_init(nic_get_pcap_iface(nic));

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 +2379,7 @@ static void e1000_send_frame(nic_t *nic, void *data, size_t size)
}

memcpy(e1000->tx_frame_virt[tdt], data, size);

pcapdump_packet(nic_get_pcap_iface(nic), 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')
15 changes: 13 additions & 2 deletions uspace/drv/nic/virtio-net/virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
#include <nic.h>

#include <virtio-pci.h>

#include "pcapdump_iface.h"
#include "pcap_iface.h"
#define NAME "virtio-net"

#define VIRTIO_NET_NUM_QUEUES 3
Expand Down Expand Up @@ -91,6 +92,7 @@ static void virtio_net_irq_handler(ipc_call_t *icall, ddf_dev_t *dev)
nic_frame_t *frame = nic_alloc_frame(nic, len - sizeof(*hdr));
if (frame) {
memcpy(frame->data, &hdr[1], len - sizeof(*hdr));
pcapdump_packet(nic_get_pcap_iface(nic), frame->data, frame->size);
nic_received_frame(nic, frame);
} else {
ddf_msg(LVL_WARN,
Expand Down Expand Up @@ -350,7 +352,7 @@ static void virtio_net_send(nic_t *nic, void *data, size_t size)

/* Copy packet data into the buffer just past the header */
memcpy(&hdr[1], data, size);

pcapdump_packet(nic_get_pcap_iface(nic), data, size);
/*
* Set the descriptor, put it into the virtqueue and notify the device
*/
Expand Down Expand Up @@ -430,6 +432,15 @@ static errno_t virtio_net_dev_add(ddf_dev_t *dev)
ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
ddf_dev_get_name(dev));

errno_t pcap_rc = pcapdump_init(nic_get_pcap_iface(nic));

if (pcap_rc != EOK) {
printf("Failed creating pcapdump port\n");
}
rc = ddf_fun_add_to_category(fun, "pcap");
if (rc != EOK)
goto unbind;

return EOK;

unbind:
Expand Down
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
5 changes: 5 additions & 0 deletions uspace/lib/nic/include/nic.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#include <device/hw_res_parsed.h>
#include <ops/nic.h>

#include "pcap_iface.h"

#define DEVICE_CATEGORY_NIC "nic"

struct nic;
Expand Down Expand Up @@ -277,6 +279,9 @@ extern uint64_t nic_query_mcast_hash(nic_t *);
extern void nic_sw_period_start(nic_t *);
extern void nic_sw_period_stop(nic_t *);

/* pcapdump interface */
extern pcap_iface_t *nic_get_pcap_iface(nic_t *);

#endif // __NIC_H__

/** @}
Expand Down
5 changes: 5 additions & 0 deletions uspace/lib/nic/include/nic_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "nic.h"
#include "nic_rx_control.h"
#include "nic_wol_virtues.h"
#include "pcap/include/pcapdump_iface.h"

struct sw_poll_info {
fid_t fibril;
Expand Down Expand Up @@ -194,6 +195,10 @@ struct nic {
* The implementation is optional.
*/
poll_request_handler on_poll_request;

/** Interface for dumping packets */
pcap_iface_t pcapdump;

/** Data specific for particular driver */
void *specific;
};
Expand Down
2 changes: 1 addition & 1 deletion uspace/lib/nic/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

deps = [ 'drv' ]
deps = [ 'drv' , 'pcap' ]
c_args = [ '-DLIBNIC_INTERNAL', ]
src = files(
'src/nic_driver.c',
Expand Down
5 changes: 5 additions & 0 deletions uspace/lib/nic/src/nic_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,10 @@ void nic_sw_period_stop(nic_t *nic_data)
nic_data->sw_poll_info.running = 0;
}

pcap_iface_t *nic_get_pcap_iface(nic_t *nic_data)
{
return &nic_data->pcapdump;
}

/** @}
*/
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
*/
Loading