1 /**
2  * Copyright (c) 2023-2024 Marcin Niestroj
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __DRIVERS_NET_NSOS_H__
8 #define __DRIVERS_NET_NSOS_H__
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 /* Protocol families. */
14 #define NSOS_MID_PF_UNSPEC       0          /**< Unspecified protocol family.  */
15 #define NSOS_MID_PF_INET         1          /**< IP protocol family version 4. */
16 #define NSOS_MID_PF_INET6        2          /**< IP protocol family version 6. */
17 #define NSOS_MID_PF_UNIX         6          /**< Unix protocol.                */
18 #define NSOS_MID_PF_PACKET       3          /**< Packet family.                */
19 
20 /* Address families. */
21 #define NSOS_MID_AF_UNSPEC      NSOS_MID_PF_UNSPEC   /**< Unspecified address family.   */
22 #define NSOS_MID_AF_INET        NSOS_MID_PF_INET     /**< IP protocol family version 4. */
23 #define NSOS_MID_AF_INET6       NSOS_MID_PF_INET6    /**< IP protocol family version 6. */
24 #define NSOS_MID_AF_UNIX        NSOS_MID_PF_UNIX     /**< Unix protocol.                */
25 #define NSOS_MID_AF_PACKET      NSOS_MID_PF_PACKET   /**< Packet family.                */
26 
27 /** Protocol numbers from IANA/BSD */
28 enum nsos_mid_net_ip_protocol {
29 	NSOS_MID_IPPROTO_IP = 0,            /**< IP protocol (pseudo-val for setsockopt() */
30 	NSOS_MID_IPPROTO_ICMP = 1,          /**< ICMP protocol   */
31 	NSOS_MID_IPPROTO_IGMP = 2,          /**< IGMP protocol   */
32 	NSOS_MID_IPPROTO_ETH_P_ALL = 3,     /**< Every packet. from linux if_ether.h  */
33 	NSOS_MID_IPPROTO_IPIP = 4,          /**< IPIP tunnels    */
34 	NSOS_MID_IPPROTO_TCP = 6,           /**< TCP protocol    */
35 	NSOS_MID_IPPROTO_UDP = 17,          /**< UDP protocol    */
36 	NSOS_MID_IPPROTO_IPV6 = 41,         /**< IPv6 protocol   */
37 	NSOS_MID_IPPROTO_ICMPV6 = 58,       /**< ICMPv6 protocol */
38 	NSOS_MID_IPPROTO_RAW = 255,         /**< RAW IP packets  */
39 };
40 
41 /** Socket type */
42 enum nsos_mid_net_sock_type {
43 	NSOS_MID_SOCK_STREAM = 1,           /**< Stream socket type   */
44 	NSOS_MID_SOCK_DGRAM,                /**< Datagram socket type */
45 	NSOS_MID_SOCK_RAW                   /**< RAW socket type      */
46 };
47 
48 #define NSOS_MID_MSG_PEEK 0x02
49 #define NSOS_MID_MSG_TRUNC 0x20
50 #define NSOS_MID_MSG_DONTWAIT 0x40
51 #define NSOS_MID_MSG_WAITALL 0x100
52 
53 struct nsos_mid_sockaddr {
54 	uint16_t sa_family;      /* Address family */
55 	char     sa_data[];      /* Socket address */
56 };
57 
58 struct nsos_mid_sockaddr_in {
59 	uint16_t sin_family;     /* AF_INET */
60 	uint16_t sin_port;       /* Port number */
61 	uint32_t sin_addr;       /* IPv4 address */
62 };
63 
64 struct nsos_mid_sockaddr_in6 {
65 	uint16_t sin6_family;    /* AF_INET6 */
66 	uint16_t sin6_port;      /* Port number */
67 	uint8_t  sin6_addr[16];
68 	uint32_t sin6_scope_id;  /* Set of interfaces for a scope */
69 };
70 
71 #define UNIX_PATH_MAX 108
72 struct nsos_mid_sockaddr_un {
73 	sa_family_t sun_family;              /* AF_UNIX */
74 	char        sun_path[UNIX_PATH_MAX]; /* pathname */
75 };
76 
77 struct nsos_mid_sockaddr_ll {
78 	sa_family_t sll_family;   /**< Always AF_PACKET                   */
79 	uint16_t    sll_protocol; /**< Physical-layer protocol            */
80 	int         sll_ifindex;  /**< Interface number                   */
81 	uint16_t    sll_hatype;   /**< ARP hardware type                  */
82 	uint8_t     sll_pkttype;  /**< Packet type                        */
83 	uint8_t     sll_halen;    /**< Length of address                  */
84 	uint8_t     sll_addr[8];  /**< Physical-layer address, big endian */
85 };
86 
87 struct nsos_mid_sockaddr_storage {
88 	union {
89 		struct nsos_mid_sockaddr_in sockaddr_in;
90 		struct nsos_mid_sockaddr_in6 sockaddr_in6;
91 		struct nsos_mid_sockaddr_un sockaddr_un;
92 		struct nsos_mid_sockaddr_ll sockaddr_ll;
93 	};
94 };
95 
96 struct nsos_mid_pollfd {
97 	int fd;
98 	short events;
99 	short revents;
100 
101 	void (*cb)(struct nsos_mid_pollfd *pollfd_mid);
102 };
103 
104 struct nsos_mid_addrinfo {
105 	int                       ai_flags;
106 	int                       ai_family;
107 	int                       ai_socktype;
108 	int                       ai_protocol;
109 	size_t                    ai_addrlen;
110 	struct nsos_mid_sockaddr *ai_addr;
111 	char                     *ai_canonname;
112 	struct nsos_mid_addrinfo *ai_next;
113 };
114 
115 struct nsos_mid_iovec {
116 	void  *iov_base;
117 	size_t iov_len;
118 };
119 
120 struct nsos_mid_msghdr {
121 	void                  *msg_name;       /* optional socket address, big endian */
122 	size_t                 msg_namelen;    /* size of socket address */
123 	struct nsos_mid_iovec *msg_iov;        /* scatter/gather array */
124 	size_t                 msg_iovlen;     /* number of elements in msg_iov */
125 	void                  *msg_control;    /* ancillary data */
126 	size_t                 msg_controllen; /* ancillary data buffer len */
127 	int                    msg_flags;      /* flags on received message */
128 };
129 
nsos_socket_flag_convert(int * flags_a,int flag_a,int * flags_b,int flag_b)130 static inline void nsos_socket_flag_convert(int *flags_a, int flag_a,
131 					    int *flags_b, int flag_b)
132 {
133 	if ((*flags_a) & flag_a) {
134 		*flags_a &= ~flag_a;
135 		*flags_b |= flag_b;
136 	}
137 }
138 
139 int nsos_adapt_get_errno(void);
140 
141 int nsos_adapt_socket(int family, int type, int proto);
142 
143 int nsos_adapt_bind(int fd, const struct nsos_mid_sockaddr *addr, size_t addrlen);
144 int nsos_adapt_connect(int fd, const struct nsos_mid_sockaddr *addr, size_t addrlen);
145 int nsos_adapt_listen(int fd, int backlog);
146 int nsos_adapt_accept(int fd, struct nsos_mid_sockaddr *addr, size_t *addrlen);
147 int nsos_adapt_sendto(int fd, const void *buf, size_t len, int flags,
148 		      const struct nsos_mid_sockaddr *addr, size_t addrlen);
149 int nsos_adapt_sendmsg(int fd, const struct nsos_mid_msghdr *msg_mid, int flags);
150 int nsos_adapt_recvfrom(int fd, void *buf, size_t len, int flags,
151 			struct nsos_mid_sockaddr *addr, size_t *addrlen);
152 int nsos_adapt_getsockopt(int fd, int level, int optname,
153 			  void *optval, size_t *optlen);
154 int nsos_adapt_setsockopt(int fd, int level, int optname,
155 			  const void *optval, size_t optlen);
156 
157 void nsos_adapt_poll_add(struct nsos_mid_pollfd *pollfd);
158 void nsos_adapt_poll_remove(struct nsos_mid_pollfd *pollfd);
159 void nsos_adapt_poll_update(struct nsos_mid_pollfd *pollfd);
160 
161 int nsos_adapt_fcntl_getfl(int fd);
162 int nsos_adapt_fcntl_setfl(int fd, int flags);
163 
164 int nsos_adapt_fionread(int fd, int *avail);
165 
166 int nsos_adapt_dup(int oldfd);
167 
168 int nsos_adapt_getaddrinfo(const char *node, const char *service,
169 			   const struct nsos_mid_addrinfo *hints,
170 			   struct nsos_mid_addrinfo **res,
171 			   int *system_errno);
172 void nsos_adapt_freeaddrinfo(struct nsos_mid_addrinfo *res);
173 
174 #endif /* __DRIVERS_NET_NSOS_H__ */
175