2017-07-12 23:07:32 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/pkt/pkt_sockif.c
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2017-07-14 18:57:38 +02:00
|
|
|
#include <stdbool.h>
|
2017-07-14 14:02:49 +02:00
|
|
|
#include <string.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2017-07-13 01:36:05 +02:00
|
|
|
#include <debug.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2017-07-14 14:02:49 +02:00
|
|
|
#include <netpacket/packet.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include <socket/socket.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
#include "pkt/pkt.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int pkt_setup(FAR struct socket *psock);
|
2017-07-13 21:36:18 +02:00
|
|
|
static sockcaps_t pkt_sockcaps(FAR struct socket *psock);
|
|
|
|
static void pkt_addref(FAR struct socket *psock);
|
|
|
|
static int pkt_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
static int pkt_close(FAR struct socket *psock);
|
2017-07-12 23:07:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_pkt_sockif =
|
|
|
|
{
|
2017-07-14 17:04:19 +02:00
|
|
|
pkt_setup, /* si_setup */
|
|
|
|
pkt_sockcaps, /* si_sockcaps */
|
|
|
|
pkt_addref, /* si_addref */
|
|
|
|
pkt_bind, /* si_bind */
|
2023-03-06 05:10:27 +01:00
|
|
|
NULL, /* si_getsockname */
|
2023-03-06 06:02:11 +01:00
|
|
|
NULL, /* si_getpeername */
|
2023-03-06 06:08:24 +01:00
|
|
|
NULL, /* si_listen */
|
2023-03-06 06:11:38 +01:00
|
|
|
NULL, /* si_connect */
|
2023-03-06 06:16:26 +01:00
|
|
|
NULL, /* si_accept */
|
2023-03-06 06:24:00 +01:00
|
|
|
NULL, /* si_poll */
|
2020-12-01 07:55:16 +01:00
|
|
|
pkt_sendmsg, /* si_sendmsg */
|
|
|
|
pkt_recvmsg, /* si_recvmsg */
|
2017-07-14 17:04:19 +02:00
|
|
|
pkt_close /* si_close */
|
2017-07-12 23:07:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_sockif_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate and attach a raw packet connection structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pkt_sockif_alloc(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Allocate the packet socket connection structure and save in the new
|
|
|
|
* socket instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct pkt_conn_s *conn = pkt_alloc();
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to reserve a connection structure */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the reference count on the connection structure. This reference
|
|
|
|
* count will be incremented only if the socket is dup'ed
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
conn->crefs = 1;
|
|
|
|
|
|
|
|
/* Save the pre-allocated connection in the socket structure */
|
|
|
|
|
|
|
|
psock->s_conn = conn;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called for socket() to verify that the provided socket type and
|
|
|
|
* protocol are usable by this address family. Perform any family-
|
|
|
|
* specific socket fields.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-12 23:07:32 +02:00
|
|
|
* psock A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-14 14:55:45 +02:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value is
|
2017-07-12 23:07:32 +02:00
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int pkt_setup(FAR struct socket *psock)
|
2017-07-12 23:07:32 +02:00
|
|
|
{
|
|
|
|
/* Allocate the appropriate connection structure. This reserves the
|
2021-01-15 10:01:00 +01:00
|
|
|
* connection structure, it is unallocated at this point. It will not
|
|
|
|
* actually be initialized until the socket is connected.
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2023-01-18 06:42:42 +01:00
|
|
|
* SOCK_RAW and SOCK_CTRL are supported.
|
2017-07-12 23:07:32 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-18 06:42:42 +01:00
|
|
|
if (psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL)
|
2017-07-12 23:07:32 +02:00
|
|
|
{
|
|
|
|
return pkt_sockif_alloc(psock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:36:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_sockcaps
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the bit encoded capabilities of this socket.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 21:36:18 +02:00
|
|
|
* psock - Socket structure of the socket whose capabilities are being
|
|
|
|
* queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2019-11-03 20:59:42 +01:00
|
|
|
* The set of socket capabilities is returned.
|
2017-07-13 21:36:18 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sockcaps_t pkt_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
2024-08-14 09:24:13 +02:00
|
|
|
return SOCKCAP_NONBLOCKING;
|
2017-07-13 21:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_addref
|
|
|
|
*
|
|
|
|
* Description:
|
2020-02-22 19:31:14 +01:00
|
|
|
* Increment the reference count on the underlying connection structure.
|
2017-07-13 21:36:18 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 21:36:18 +02:00
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pkt_addref(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct pkt_conn_s *conn;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL);
|
2017-07-13 21:36:18 +02:00
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
DEBUGASSERT(conn->crefs > 0 && conn->crefs < 255);
|
|
|
|
conn->crefs++;
|
|
|
|
}
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* pkt_bind() gives the socket 'psock' the local address 'addr'. 'addr'
|
|
|
|
* is 'addrlen' bytes long. Traditionally, this is called "assigning a
|
|
|
|
* name to a socket." When a socket is created with socket(), it exists
|
|
|
|
* in a name space (address family) but has no name assigned.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 17:27:56 +02:00
|
|
|
* psock Socket structure of the socket to bind
|
|
|
|
* addr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; A negated errno value is returned on failure. See
|
|
|
|
* bind() for a list a appropriate error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-13 00:17:37 +01:00
|
|
|
static int pkt_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen)
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
|
|
|
int ifindex;
|
|
|
|
|
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
2017-07-14 14:02:49 +02:00
|
|
|
if (addr->sa_family != AF_PACKET || addrlen < sizeof(struct sockaddr_ll))
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
nerr("ERROR: Invalid address length: %d < %zu\n",
|
2017-07-14 14:02:49 +02:00
|
|
|
addrlen, sizeof(struct sockaddr_ll));
|
2017-07-13 17:27:56 +02:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:01:00 +01:00
|
|
|
/* Bind a raw socket to a network device. */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2023-01-18 06:42:42 +01:00
|
|
|
if (psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL)
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct pkt_conn_s *conn = psock->s_conn;
|
2018-06-25 20:08:10 +02:00
|
|
|
FAR struct net_driver_s *dev;
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2021-01-15 10:01:00 +01:00
|
|
|
/* Look at the addr and identify the network interface */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2018-10-30 01:00:30 +01:00
|
|
|
ifindex = ((FAR struct sockaddr_ll *)addr)->sll_ifindex;
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2024-02-19 04:46:04 +01:00
|
|
|
/* Check if we have that interface */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
dev = netdev_findbyindex(ifindex);
|
|
|
|
if (dev == NULL)
|
|
|
|
{
|
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:46:04 +01:00
|
|
|
/* Put ifindex into connection */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
|
|
|
conn->ifindex = ifindex;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pkt_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on a raw packet socket instance
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 20:15:15 +02:00
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pkt_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Perform some pre-close operations for the raw packet address type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
|
|
|
case SOCK_RAW:
|
2023-01-18 06:42:42 +01:00
|
|
|
case SOCK_CTRL:
|
2017-07-13 20:15:15 +02:00
|
|
|
{
|
|
|
|
FAR struct pkt_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->crefs <= 1)
|
|
|
|
{
|
2023-02-07 08:06:03 +01:00
|
|
|
/* Yes... free any read-ahead data */
|
|
|
|
|
|
|
|
iob_free_queue(&conn->readahead);
|
|
|
|
|
|
|
|
/* Then free the connection structure */
|
2017-07-13 20:15:15 +02:00
|
|
|
|
|
|
|
conn->crefs = 0; /* No more references on the connection */
|
|
|
|
pkt_free(psock->s_conn); /* Free network resources */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->crefs--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_PKT */
|