Skip to content

Commit

Permalink
net: sockets: mgmt: Add AF_NET_MGMT address family support
Browse files Browse the repository at this point in the history
Allow application to listen network management events using
BSD socket API. Application needs to create the socket using
AF_NET_MGMT address family. At this point we only support
receiving network management events that the network subsystem
is sending.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
  • Loading branch information
jukkar authored and andrewboie committed Jul 1, 2019
1 parent 02b3826 commit c0d6831
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 5 deletions.
12 changes: 7 additions & 5 deletions include/net/net_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ extern "C" {
#define PF_INET6 2 /**< IP protocol family version 6. */
#define PF_PACKET 3 /**< Packet family. */
#define PF_CAN 4 /**< Controller Area Network. */
#define PF_NET_MGMT 5 /**< Network management info. */

/* Address families. */
#define AF_UNSPEC PF_UNSPEC /**< Unspecified address family. */
#define AF_INET PF_INET /**< IP protocol family version 4. */
#define AF_INET6 PF_INET6 /**< IP protocol family version 6. */
#define AF_PACKET PF_PACKET /**< Packet family. */
#define AF_CAN PF_CAN /**< Controller Area Network. */
#define AF_UNSPEC PF_UNSPEC /**< Unspecified address family. */
#define AF_INET PF_INET /**< IP protocol family version 4. */
#define AF_INET6 PF_INET6 /**< IP protocol family version 6. */
#define AF_PACKET PF_PACKET /**< Packet family. */
#define AF_CAN PF_CAN /**< Controller Area Network. */
#define AF_NET_MGMT PF_NET_MGMT /**< Network management info. */

/** Protocol numbers from IANA/BSD */
enum net_ip_protocol {
Expand Down
107 changes: 107 additions & 0 deletions include/net/socket_net_mgmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/** @file
* @brief NET_MGMT socket definitions.
*
* Definitions for NET_MGMT socket support.
*/

/*
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_NET_SOCKET_NET_MGMT_H_
#define ZEPHYR_INCLUDE_NET_SOCKET_NET_MGMT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <zephyr/types.h>
#include <net/net_ip.h>
#include <net/net_if.h>
#include <net/net_mgmt.h>

/**
* @brief Socket NET_MGMT library
* @defgroup socket_net_mgmt Network Core Library
* @ingroup networking
* @{
*/

/* Protocols of the protocol family PF_NET_MGMT */
#define NET_MGMT_EVENT_PROTO 0x01

/* Socket NET_MGMT options */
#define SOL_NET_MGMT_BASE 100
#define SOL_NET_MGMT_RAW (SOL_NET_MGMT_BASE + NET_MGMT_RAW)

/**
* struct sockaddr_nm - The sockaddr structure for NET_MGMT sockets
*
* Similar concepts are used as in Linux AF_NETLINK. The NETLINK name is not
* used in order to avoid confusion between Zephyr and Linux as the
* implementations are different.
*
* The socket domain (address family) is AF_NET_MGMT, and the type of socket
* is either SOCK_RAW or SOCK_DGRAM, because this is a datagram-oriented
* service.
*
* The protocol (protocol type) selects for which feature the socket is used.
*
* When used with bind(), the nm_pid field of the sockaddr_nm can be
* filled with the calling thread' own id. The nm_pid serves here as the local
* address of this net_mgmt socket. The application is responsible for picking
* a unique 32-bit integer to fill in nm_pid.
*/
struct sockaddr_nm {
/** AF_NET_MGMT address family. */
sa_family_t nm_family;

/** Network interface related to this address */
int nm_ifindex;

/** Thread id or similar that is used to separate the different
* sockets. Application can decide how the pid is constructed.
*/
u32_t nm_pid;

/** net_mgmt mask */
u32_t nm_mask;
};


/**
* Each network management message is prefixed with this header.
*/
struct net_mgmt_msghdr {
/** Network management version */
u32_t nm_msg_version;

/** Length of the data */
u32_t nm_msg_len;

/** The actual message data follows */
u8_t nm_msg[];
};

/**
* Version of the message is placed to the header. Currently we have
* following versions.
*
* Network management message versions:
*
* 0x0001 : The net_mgmt event info message follows directly
* after the header.
*/
#define NET_MGMT_SOCKET_VERSION_1 0x0001

/**
* @}
*/

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_NET_SOCKET_NET_MGMT_H_ */
5 changes: 5 additions & 0 deletions subsys/net/lib/sockets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ zephyr_sources_ifdef(CONFIG_NET_SOCKETS_CAN sockets_can.c)
endif()
zephyr_sources_ifdef(CONFIG_NET_SOCKETS_OFFLOAD socket_offload.c)

if(CONFIG_NET_SOCKETS_NET_MGMT)
zephyr_sources(sockets_net_mgmt.c)
zephyr_include_directories(${ZEPHYR_BASE}/subsys/net/ip)
endif()

zephyr_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
18 changes: 18 additions & 0 deletions subsys/net/lib/sockets/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ config NET_SOCKETS_CAN_RECEIVERS
The value tells how many sockets can receive data from same
Socket-CAN interface.

config NET_SOCKETS_NET_MGMT
bool "Enable network mangement socket support [EXPERIMENTAL]"
depends on NET_MGMT_EVENT
select NET_MGMT_EVENT_INFO
help
Select this if you want to use socket API to get network
managements events to your application.

config NET_SOCKETS_NET_MGMT_MAX_LISTENERS
int "Max number of sockets to listen"
default 1
depends on NET_SOCKETS_NET_MGMT
help
This sets the maximum number of net_mgmt sockets that can
be set by the socket interface. So if you have two separate
sockets that are used for listening events, you need to set
this to two.

module = NET_SOCKETS
module-dep = NET_LOG
module-str = Log level for BSD sockets compatible API calls
Expand Down
Loading

0 comments on commit c0d6831

Please sign in to comment.