Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | /* * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <stdbool.h> #include <fcntl.h> #include <logging/log.h> LOG_MODULE_REGISTER(net_sock_packet, CONFIG_NET_SOCKETS_LOG_LEVEL); #include <kernel.h> #include <drivers/entropy.h> #include <sys/util.h> #include <net/net_context.h> #include <net/net_pkt.h> #include <net/socket.h> #include <net/ethernet.h> #include <syscall_handler.h> #include <sys/fdtable.h> #include "sockets_internal.h" extern const struct socket_op_vtable sock_fd_op_vtable; static const struct socket_op_vtable packet_sock_fd_op_vtable; static inline int k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout) { struct k_poll_event events[] = { K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, fifo), }; return k_poll(events, ARRAY_SIZE(events), timeout); } static int zpacket_socket(int family, int type, int proto) { struct net_context *ctx; int fd; int ret; fd = z_reserve_fd(); if (fd < 0) { return -1; } ret = net_context_get(family, type, proto, &ctx); if (ret < 0) { z_free_fd(fd); errno = -ret; return -1; } /* Initialize user_data, all other calls will preserve it */ ctx->user_data = NULL; /* recv_q and accept_q are in union */ k_fifo_init(&ctx->recv_q); #ifdef CONFIG_USERSPACE /* Set net context object as initialized and grant access to the * calling thread (and only the calling thread) */ z_object_recycle(ctx); #endif z_finalize_fd(fd, ctx, (const struct fd_op_vtable *)&packet_sock_fd_op_vtable); return fd; } static void zpacket_received_cb(struct net_context *ctx, struct net_pkt *pkt, union net_ip_header *ip_hdr, union net_proto_header *proto_hdr, int status, void *user_data) { NET_DBG("ctx=%p, pkt=%p, st=%d, user_data=%p", ctx, pkt, status, user_data); /* if pkt is NULL, EOF */ if (!pkt) { struct net_pkt *last_pkt = k_fifo_peek_tail(&ctx->recv_q); if (!last_pkt) { /* If there're no packets in the queue, recv() may * be blocked waiting on it to become non-empty, * so cancel that wait. */ sock_set_eof(ctx); k_fifo_cancel_wait(&ctx->recv_q); NET_DBG("Marked socket %p as peer-closed", ctx); } else { net_pkt_set_eof(last_pkt, true); NET_DBG("Set EOF flag on pkt %p", ctx); } return; } /* Normal packet */ net_pkt_set_eof(pkt, false); k_fifo_put(&ctx->recv_q, pkt); } static int zpacket_bind_ctx(struct net_context *ctx, const struct sockaddr *addr, socklen_t addrlen) { int ret = 0; ret = net_context_bind(ctx, addr, addrlen); if (ret < 0) { errno = -ret; return -1; } /* For packet socket, we expect to receive packets after call * to bind(). */ ret = net_context_recv(ctx, zpacket_received_cb, K_NO_WAIT, ctx->user_data); if (ret < 0) { errno = -ret; return -1; } return 0; } ssize_t zpacket_sendto_ctx(struct net_context *ctx, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { s32_t timeout = K_FOREVER; int status; if (!dest_addr) { errno = EDESTADDRREQ; return -1; } if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) { timeout = K_NO_WAIT; } /* Register the callback before sending in order to receive the response * from the peer. */ status = net_context_recv(ctx, zpacket_received_cb, K_NO_WAIT, ctx->user_data); if (status < 0) { errno = -status; return -1; } status = net_context_sendto(ctx, buf, len, dest_addr, addrlen, NULL, timeout, ctx->user_data); if (status < 0) { errno = -status; return -1; } return status; } ssize_t zpacket_recvfrom_ctx(struct net_context *ctx, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { size_t recv_len = 0; s32_t timeout = K_FOREVER; struct net_pkt *pkt; if ((flags & ZSOCK_MSG_DONTWAIT) || sock_is_nonblock(ctx)) { timeout = K_NO_WAIT; } if (flags & ZSOCK_MSG_PEEK) { int res; res = k_fifo_wait_non_empty(&ctx->recv_q, timeout); /* EAGAIN when timeout expired, EINTR when cancelled */ if (res && res != -EAGAIN && res != -EINTR) { errno = -res; return -1; } pkt = k_fifo_peek_head(&ctx->recv_q); } else { pkt = k_fifo_get(&ctx->recv_q, timeout); } if (!pkt) { errno = EAGAIN; return -1; } /* We do not handle any headers here, * just pass the whole packet to caller. */ recv_len = net_pkt_get_len(pkt); if (recv_len > max_len) { recv_len = max_len; } if (net_pkt_read(pkt, buf, recv_len)) { errno = ENOBUFS; return -1; } if (!(flags & ZSOCK_MSG_PEEK)) { net_pkt_unref(pkt); } else { net_pkt_cursor_init(pkt); } return recv_len; } int zpacket_getsockopt_ctx(struct net_context *ctx, int level, int optname, void *optval, socklen_t *optlen) { if (!optval || !optlen) { errno = EINVAL; return -1; } return sock_fd_op_vtable.getsockopt(ctx, level, optname, optval, optlen); } int zpacket_setsockopt_ctx(struct net_context *ctx, int level, int optname, const void *optval, socklen_t optlen) { return sock_fd_op_vtable.setsockopt(ctx, level, optname, optval, optlen); } static ssize_t packet_sock_read_vmeth(void *obj, void *buffer, size_t count) { return zpacket_recvfrom_ctx(obj, buffer, count, 0, NULL, 0); } static ssize_t packet_sock_write_vmeth(void *obj, const void *buffer, size_t count) { return zpacket_sendto_ctx(obj, buffer, count, 0, NULL, 0); } static int packet_sock_ioctl_vmeth(void *obj, unsigned int request, va_list args) { return sock_fd_op_vtable.fd_vtable.ioctl(obj, request, args); } /* * TODO: A packet socket can be bound to a network device using SO_BINDTODEVICE. */ static int packet_sock_bind_vmeth(void *obj, const struct sockaddr *addr, socklen_t addrlen) { return zpacket_bind_ctx(obj, addr, addrlen); } /* The connect() function is no longer necessary. */ static int packet_sock_connect_vmeth(void *obj, const struct sockaddr *addr, socklen_t addrlen) { return -EOPNOTSUPP; } /* * The listen() and accept() functions are without any functionality, * since the client-Server-Semantic is no longer present. * When we use packet sockets we are sending unconnected packets. */ static int packet_sock_listen_vmeth(void *obj, int backlog) { return -EOPNOTSUPP; } static int packet_sock_accept_vmeth(void *obj, struct sockaddr *addr, socklen_t *addrlen) { return -EOPNOTSUPP; } static ssize_t packet_sock_sendto_vmeth(void *obj, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { return zpacket_sendto_ctx(obj, buf, len, flags, dest_addr, addrlen); } static ssize_t packet_sock_recvfrom_vmeth(void *obj, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { return zpacket_recvfrom_ctx(obj, buf, max_len, flags, src_addr, addrlen); } static int packet_sock_getsockopt_vmeth(void *obj, int level, int optname, void *optval, socklen_t *optlen) { return zpacket_getsockopt_ctx(obj, level, optname, optval, optlen); } static int packet_sock_setsockopt_vmeth(void *obj, int level, int optname, const void *optval, socklen_t optlen) { return zpacket_setsockopt_ctx(obj, level, optname, optval, optlen); } static const struct socket_op_vtable packet_sock_fd_op_vtable = { .fd_vtable = { .read = packet_sock_read_vmeth, .write = packet_sock_write_vmeth, .ioctl = packet_sock_ioctl_vmeth, }, .bind = packet_sock_bind_vmeth, .connect = packet_sock_connect_vmeth, .listen = packet_sock_listen_vmeth, .accept = packet_sock_accept_vmeth, .sendto = packet_sock_sendto_vmeth, .recvfrom = packet_sock_recvfrom_vmeth, .getsockopt = packet_sock_getsockopt_vmeth, .setsockopt = packet_sock_setsockopt_vmeth, }; static bool packet_is_supported(int family, int type, int proto) { if (type != SOCK_RAW || proto != ETH_P_ALL) { return false; } return true; } NET_SOCKET_REGISTER(af_packet, AF_PACKET, packet_is_supported, zpacket_socket); |