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

Promiscuous mode off fixes #11303

Merged
merged 2 commits into from Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions samples/net/promiscuous_mode/src/main.c
Expand Up @@ -9,6 +9,8 @@

#include <zephyr.h>
#include <errno.h>
#include <stdlib.h>
#include <shell/shell.h>

#include <net/net_core.h>
#include <net/promiscuous.h>
Expand Down Expand Up @@ -137,6 +139,89 @@ static void print_info(struct net_pkt *pkt)
}
}

static int set_promisc_mode(const struct shell *shell,
size_t argc, char *argv[], bool enable)
{
struct net_if *iface;
char *endptr;
int idx, ret;

if (shell_help_requested(shell)) {
shell_help_print(shell, NULL, 0);
return -ENOEXEC;
}

if (argc < 2) {
shell_fprintf(shell, SHELL_ERROR, "Invalid arguments.\n");
return -ENOEXEC;
}

idx = strtol(argv[1], &endptr, 10);

iface = net_if_get_by_index(idx);
if (!iface) {
shell_fprintf(shell, SHELL_ERROR,
"Cannot find network interface for index %d\n",
idx);
return -ENOEXEC;
}

shell_fprintf(shell, SHELL_INFO, "Promiscuous mode %s...\n",
enable ? "ON" : "OFF");

if (enable) {
ret = net_promisc_mode_on(iface);
} else {
ret = net_promisc_mode_off(iface);
}

if (ret < 0) {
if (ret == -EALREADY) {
shell_fprintf(shell, SHELL_INFO,
"Promiscuous mode already %s\n",
enable ? "enabled" : "disabled");
} else {
shell_fprintf(shell, SHELL_ERROR,
"Cannot %s promiscuous mode for "
"interface %p (%d)\n",
enable ? "set" : "unset", iface, ret);
}

return -ENOEXEC;
}

return 0;
}

static int cmd_promisc_on(const struct shell *shell,
size_t argc, char *argv[])
{
return set_promisc_mode(shell, argc, argv, true);
}

static int cmd_promisc_off(const struct shell *shell,
size_t argc, char *argv[])
{
return set_promisc_mode(shell, argc, argv, false);
}

SHELL_CREATE_STATIC_SUBCMD_SET(promisc_commands)
{
SHELL_CMD(on, NULL,
"Turn promiscuous mode on\n"
"promisc on <interface index> "
"Turn on promiscuous mode for the interface\n",
cmd_promisc_on),
SHELL_CMD(off, NULL, "Turn promiscuous mode off\n"
"promisc off <interface index> "
"Turn off promiscuous mode for the interface\n",
cmd_promisc_off),
SHELL_SUBCMD_SET_END
};

SHELL_CMD_REGISTER(promisc, &promisc_commands,
"Promiscuous mode commands", NULL);

void main(void)
{
struct net_pkt *pkt;
Expand Down
25 changes: 21 additions & 4 deletions subsys/net/ip/net_if.c
Expand Up @@ -2956,10 +2956,9 @@ int net_if_down(struct net_if *iface)
return 0;
}

int net_if_set_promisc(struct net_if *iface)
static int promisc_mode_set(struct net_if *iface, bool enable)
{
enum net_l2_flags l2_flags = 0;
int ret;

NET_ASSERT(iface);

Expand All @@ -2973,7 +2972,8 @@ int net_if_set_promisc(struct net_if *iface)

#if defined(CONFIG_NET_L2_ETHERNET)
if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
ret = net_eth_promisc_mode(iface, true);
int ret = net_eth_promisc_mode(iface, enable);

if (ret < 0) {
return ret;
}
Expand All @@ -2982,6 +2982,18 @@ int net_if_set_promisc(struct net_if *iface)
return -ENOTSUP;
#endif

return 0;
}

int net_if_set_promisc(struct net_if *iface)
{
int ret;

ret = promisc_mode_set(iface, true);
if (ret < 0) {
return ret;
}

ret = atomic_test_and_set_bit(iface->if_dev->flags, NET_IF_PROMISC);
if (ret) {
return -EALREADY;
Expand All @@ -2992,7 +3004,12 @@ int net_if_set_promisc(struct net_if *iface)

void net_if_unset_promisc(struct net_if *iface)
{
NET_ASSERT(iface);
int ret;

ret = promisc_mode_set(iface, false);
if (ret < 0) {
return;
}

atomic_clear_bit(iface->if_dev->flags, NET_IF_PROMISC);
}
Expand Down