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 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: 3 additions & 1 deletion abi/include/abi/ipc/interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ typedef enum {
INTERFACE_WNDMGT_CB =
FOURCC_COMPACT('w', 'm', 'g', 't') | IFACE_EXCHANGE_SERIALIZE | IFACE_MOD_CALLBACK,
INTERFACE_TBARCFG_NOTIFY =
FOURCC_COMPACT('t', 'b', 'c', 'f') | IFACE_EXCHANGE_SERIALIZE
FOURCC_COMPACT('t', 'b', 'c', 'f') | IFACE_EXCHANGE_SERIALIZE,
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 @@ -70,6 +70,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
*/
160 changes: 160 additions & 0 deletions uspace/app/pcapctl/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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 <arg_parse.h>
#include <getopt.h>

#include "pcapctl_dump.h"

#define NAME "pcapctl"
#define DEFAULT_DEV_NUM 0

static errno_t start_dumping(int *dev_number, const char *name)
{
pcapctl_sess_t *sess = NULL;
errno_t rc = pcapctl_dump_open(dev_number, &sess);
if (rc != EOK) {
return 1;
}

pcapctl_dump_start(name, sess);
pcapctl_dump_close(sess);
return EOK;
}

static errno_t stop_dumping(int *dev_number)
{
pcapctl_sess_t *sess = NULL;
errno_t rc = pcapctl_dump_open(dev_number, &sess);
if (rc != EOK) {
return 1;
}
pcapctl_dump_stop(sess);
pcapctl_dump_close(sess);
return EOK;
}

static void list_devs(void)
{
pcapctl_list();
}

/**
* Array of supported commandline options
*/
static const struct option opts[] = {
{ "device", required_argument, 0, 'd' },
{ "list", no_argument, 0, 'l' },
{ "help", no_argument, 0, 'h' },
{ "outfile", required_argument, 0, 'f' },
{ "start", no_argument, 0, 'r' },
{ "stop", no_argument, 0, 't' },
{ 0, 0, 0, 0 }
};

static void usage(void)
{
printf("Usage:\n"
NAME " --list | -l \n"
"\tList of devices\n"
NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n"
"\tPackets dumped from device will be written to <outfile>\n"
NAME " --stop | -t --device= | -d <device>\n"
"\tDumping from <device> stops\n"
NAME " --start | -s --outfile= | -f <outfile>\n"
"\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
NAME " --help | -h\n"
"\tShow this application help.\n");
}

int main(int argc, char *argv[])
{
bool start = false;
bool stop = false;
int dev_number = DEFAULT_DEV_NUM;
const char *output_file_name;
int idx = 0;
int ret = 0;
if (argc == 1) {
usage();
return 0;
}
while (ret != -1) {
ret = getopt_long(argc, argv, "d:lhf:rt", opts, &idx);
switch (ret) {
case 'd':
char *rest;
long result = strtol(optarg, &rest, 10);
dev_number = (int)result;
errno_t rc = pcapctl_is_valid_device(&dev_number);
if (rc != EOK) {
printf("Device with index %d not found\n", dev_number);
return 1;
}
break;
case 'l':
list_devs();
return 0;
case 'h':
usage();
return 0;
case 'f':
output_file_name = optarg;
break;
case 'r':
start = true;
break;
case 't':
stop = true;
break;
}
}

printf("%s: HelenOS Packet Dumping utility: device - %d\n", NAME, dev_number);

if (start) {
// start with dev number and optional..name
start_dumping(&dev_number, output_file_name);
} else if (stop) {
//stop with dev number
stop_dumping(&dev_number);
}
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')
18 changes: 11 additions & 7 deletions uspace/drv/nic/e1k/e1k.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <pci_dev_iface.h>
#include <nic.h>
#include <ops/nic.h>
#include <pcapdump_iface.h>
#include "e1k.h"

#define NAME "e1k"
Expand Down Expand Up @@ -173,6 +174,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 +1190,7 @@ 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);

nic_received_frame(nic, frame);
} else {
ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped.");
Expand Down Expand Up @@ -2197,14 +2200,16 @@ errno_t e1000_dev_add(ddf_dev_t *dev)
if (rc != EOK)
goto err_fun_bind;

rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
if (rc != EOK)
goto err_add_to_cat;

rc = nic_fun_add_to_cats(fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed adding function to categories");
ddf_fun_unbind(fun);
return rc;
}
return EOK;

err_add_to_cat:
ddf_fun_unbind(fun);
// err_add_to_cat:
// ddf_fun_unbind(fun);
err_fun_bind:
err_rx_structure:
e1000_uninitialize_rx_structure(nic);
Expand Down Expand Up @@ -2364,7 +2369,6 @@ static void e1000_send_frame(nic_t *nic, void *data, size_t size)
}

memcpy(e1000->tx_frame_virt[tdt], 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')
6 changes: 4 additions & 2 deletions uspace/drv/nic/ne2k/ne2k.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stdlib.h>
#include <str_error.h>
#include <async.h>
#include <ddf/log.h>
#include "dp8390.h"

#define NAME "ne2k"
Expand Down Expand Up @@ -448,10 +449,10 @@ static errno_t ne2k_dev_add(ddf_dev_t *dev)
return rc;
}

rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
rc = nic_fun_add_to_cats(fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed adding function to categories");
ddf_fun_unbind(fun);
ddf_fun_destroy(fun);
return rc;
}

Expand Down Expand Up @@ -481,6 +482,7 @@ int main(int argc, char *argv[])
nic_driver_init(NAME);
nic_driver_implement(&ne2k_driver_ops, &ne2k_dev_ops, &ne2k_nic_iface);

ddf_log_init(NAME);
return ddf_driver_main(&ne2k_driver);
}

Expand Down
13 changes: 8 additions & 5 deletions uspace/drv/nic/rtl8139/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <pci_dev_iface.h>
#include <stdio.h>
#include <str.h>
#include <pcapdump_iface.h>

#include "defs.h"
#include "driver.h"
Expand Down Expand Up @@ -1304,19 +1305,21 @@ errno_t rtl8139_dev_add(ddf_dev_t *dev)
ddf_msg(LVL_ERROR, "Failed binding device function");
goto err_fun_create;
}
rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);

rc = nic_fun_add_to_cats(fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed adding function to category");
goto err_fun_bind;
ddf_msg(LVL_ERROR, "Failed adding function to categories");
ddf_fun_unbind(fun);
return rc;
}

ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
ddf_dev_get_name(dev));

return EOK;

err_fun_bind:
ddf_fun_unbind(fun);
// err_fun_bind:
// ddf_fun_unbind(fun);
err_fun_create:
ddf_fun_destroy(fun);
err_srv:
Expand Down
11 changes: 6 additions & 5 deletions uspace/drv/nic/rtl8169/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,19 @@ static errno_t rtl8169_dev_add(ddf_dev_t *dev)
goto err_fun_create;
}

rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
rc = nic_fun_add_to_cats(fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed adding function to category");
goto err_fun_bind;
ddf_msg(LVL_ERROR, "Failed adding function to categories");
ddf_fun_unbind(fun);
return rc;
}

ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
ddf_dev_get_name(dev));
return EOK;

err_fun_bind:
ddf_fun_unbind(fun);
// err_fun_bind:
// ddf_fun_unbind(fun);
err_fun_create:
ddf_fun_destroy(fun);
err_srv:
Expand Down
Loading