Lines Matching full:context

2  * @brief Network context API
84 * from applications which are usually run in task context.
88 bool net_context_is_reuseaddr_set(struct net_context *context) in net_context_is_reuseaddr_set() argument
91 return context->options.reuseaddr; in net_context_is_reuseaddr_set()
97 bool net_context_is_reuseport_set(struct net_context *context) in net_context_is_reuseport_set() argument
100 return context->options.reuseport; in net_context_is_reuseport_set()
106 bool net_context_is_v6only_set(struct net_context *context) in net_context_is_v6only_set() argument
109 if (context == NULL) { in net_context_is_v6only_set()
113 return context->options.ipv6_v6only; in net_context_is_v6only_set()
115 ARG_UNUSED(context); in net_context_is_v6only_set()
121 bool net_context_is_recv_pktinfo_set(struct net_context *context) in net_context_is_recv_pktinfo_set() argument
124 return context->options.recv_pktinfo; in net_context_is_recv_pktinfo_set()
126 ARG_UNUSED(context); in net_context_is_recv_pktinfo_set()
132 bool net_context_is_timestamping_set(struct net_context *context) in net_context_is_timestamping_set() argument
135 return (bool)(context->options.timestamping > 0); in net_context_is_timestamping_set()
137 ARG_UNUSED(context); in net_context_is_timestamping_set()
144 static inline bool is_in_tcp_listen_state(struct net_context *context) in is_in_tcp_listen_state() argument
147 if (net_context_get_type(context) == SOCK_STREAM && in is_in_tcp_listen_state()
148 net_context_get_state(context) == NET_CONTEXT_LISTENING) { in is_in_tcp_listen_state()
158 static inline bool is_in_tcp_time_wait_state(struct net_context *context) in is_in_tcp_time_wait_state() argument
161 if (net_context_get_type(context) == SOCK_STREAM) { in is_in_tcp_time_wait_state()
162 const struct tcp *tcp_conn = context->tcp; in is_in_tcp_time_wait_state()
175 static int check_used_port(struct net_context *context, in check_used_port() argument
191 if (context != NULL && context == &contexts[i]) { in check_used_port()
220 /* When both context have the REUSEPORT set, both in check_used_port()
230 /* In case of REUSEADDR, only one context may be in check_used_port()
232 * Furthermore, in case the existing context is in in check_used_port()
249 /* When both context have the REUSEPORT set, both in check_used_port()
255 /* With REUSEADDR, the existing context must be in check_used_port()
282 /* When both context have the REUSEPORT set, both in check_used_port()
292 /* In case of REUSEADDR, only one context may be in check_used_port()
294 * Furthermore, in case the existing context is in in check_used_port()
311 /* When both context have the REUSEPORT set, both in check_used_port()
317 /* With REUSEADDR, the existing context must be in check_used_port()
335 (context->options.port_range >> 16), in check_used_port()
338 (context->options.port_range & 0xffff), in check_used_port()
354 static uint16_t find_available_port(struct net_context *context, in find_available_port() argument
365 (context->options.port_range >> 16), in find_available_port()
368 (context->options.port_range & 0xffff), in find_available_port()
389 } while (count > 0 && check_used_port(context, in find_available_port()
391 net_context_get_proto(context), in find_available_port()
419 uint16_t proto, struct net_context **context) in net_context_check() argument
455 NET_DBG("Context type and protocol mismatch," in net_context_check()
462 NET_DBG("Context type and protocol mismatch," in net_context_check()
470 NET_DBG("Unknown context type."); in net_context_check()
507 if (!context) { in net_context_check()
508 NET_DBG("Invalid context"); in net_context_check()
517 struct net_context **context) in net_context_get() argument
522 ret = net_context_check(family, type, proto, context); in net_context_get()
610 *context = &contexts[i]; in net_context_get()
626 ret = net_offload_get(net_if_get_default(), family, type, proto, context); in net_context_get()
628 (*context)->flags &= ~NET_CONTEXT_IN_USE; in net_context_get()
629 *context = NULL; in net_context_get()
633 net_context_set_iface(*context, net_if_get_default()); in net_context_get()
639 int net_context_ref(struct net_context *context) in net_context_ref() argument
641 int old_rc = atomic_inc(&context->refcount); in net_context_ref()
646 int net_context_unref(struct net_context *context) in net_context_unref() argument
648 int old_rc = atomic_dec(&context->refcount); in net_context_unref()
654 k_mutex_lock(&context->lock, K_FOREVER); in net_context_unref()
656 if (context->conn_handler) { in net_context_unref()
660 net_conn_unregister(context->conn_handler); in net_context_unref()
663 context->conn_handler = NULL; in net_context_unref()
666 net_context_set_state(context, NET_CONTEXT_UNCONNECTED); in net_context_unref()
668 context->flags &= ~NET_CONTEXT_IN_USE; in net_context_unref()
670 NET_DBG("Context %p released", context); in net_context_unref()
672 k_mutex_unlock(&context->lock); in net_context_unref()
677 int net_context_put(struct net_context *context) in net_context_put() argument
681 NET_ASSERT(context); in net_context_put()
683 if (!PART_OF_ARRAY(contexts, context)) { in net_context_put()
687 k_mutex_lock(&context->lock, K_FOREVER); in net_context_put()
690 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_put()
691 context->flags &= ~NET_CONTEXT_IN_USE; in net_context_put()
692 ret = net_offload_put(net_context_get_iface(context), context); in net_context_put()
696 context->connect_cb = NULL; in net_context_put()
697 context->recv_cb = NULL; in net_context_put()
698 context->send_cb = NULL; in net_context_put()
701 net_tcp_put(context); in net_context_put()
704 net_context_unref(context); in net_context_put()
707 k_mutex_unlock(&context->lock); in net_context_put()
713 static int bind_default(struct net_context *context) in bind_default() argument
715 sa_family_t family = net_context_get_family(context); in bind_default()
720 if (net_sin6_ptr(&context->local)->sin6_addr) { in bind_default()
728 find_available_port(context, in bind_default()
731 return net_context_bind(context, (struct sockaddr *)&addr6, in bind_default()
738 if (net_sin_ptr(&context->local)->sin_addr) { in bind_default()
745 find_available_port(context, in bind_default()
748 return net_context_bind(context, (struct sockaddr *)&addr4, in bind_default()
755 if (net_sll_ptr(&context->local)->sll_addr) { in bind_default()
763 return net_context_bind(context, (struct sockaddr *)&ll_addr, in bind_default()
770 if (context->iface >= 0) { in bind_default()
783 context->iface = can_addr.can_ifindex; in bind_default()
791 return net_context_bind(context, (struct sockaddr *)&can_addr, in bind_default()
798 static int recheck_port(struct net_context *context, in recheck_port() argument
806 ret = check_used_port(context, iface, in recheck_port()
810 net_context_is_reuseaddr_set(context), in recheck_port()
811 net_context_is_reuseport_set(context), in recheck_port()
819 re_port = find_available_port(context, addr); in recheck_port()
826 net_sin_ptr(&context->local)->sin_port = re_port; in recheck_port()
835 net_sin_ptr(&context->local)->sin_port = net_sin(addr)->sin_port; in recheck_port()
841 int net_context_bind(struct net_context *context, const struct sockaddr *addr, in net_context_bind() argument
847 NET_ASSERT(PART_OF_ARRAY(contexts, context)); in net_context_bind()
855 if (context->conn_handler) { in net_context_bind()
868 if (net_context_is_bound_to_iface(context)) { in net_context_bind()
869 iface = net_context_get_iface(context); in net_context_bind()
876 net_context_get_type(context) == SOCK_DGRAM) { in net_context_bind()
878 (context->options.ipv6_mcast_ifindex > 0), in net_context_bind()
882 context->options.ipv6_mcast_ifindex))); in net_context_bind()
897 &net_sin6(&context->remote)->sin6_addr); in net_context_bind()
921 k_mutex_lock(&context->lock, K_FOREVER); in net_context_bind()
923 net_context_set_iface(context, iface); in net_context_bind()
925 net_sin6_ptr(&context->local)->sin6_family = AF_INET6; in net_context_bind()
926 net_sin6_ptr(&context->local)->sin6_addr = ptr; in net_context_bind()
929 k_mutex_unlock(&context->lock); in net_context_bind()
930 return net_offload_bind(iface, context, addr, addrlen); in net_context_bind()
935 ret = recheck_port(context, iface, context->proto, in net_context_bind()
942 net_sin6_ptr(&context->local)->sin6_port; in net_context_bind()
945 ret = recheck_port(context, iface, context->proto, in net_context_bind()
953 NET_DBG("Context %p binding to %s [%s]:%d iface %d (%p)", in net_context_bind()
954 context, in net_context_bind()
956 net_context_get_proto(context)), in net_context_bind()
962 k_mutex_unlock(&context->lock); in net_context_bind()
977 if (net_context_is_bound_to_iface(context)) { in net_context_bind()
978 iface = net_context_get_iface(context); in net_context_bind()
985 net_context_get_type(context) == SOCK_DGRAM) { in net_context_bind()
987 (context->options.ipv4_mcast_ifindex > 0), in net_context_bind()
991 context->options.ipv4_mcast_ifindex))); in net_context_bind()
1006 &net_sin(&context->remote)->sin_addr); in net_context_bind()
1028 k_mutex_lock(&context->lock, K_FOREVER); in net_context_bind()
1030 net_context_set_iface(context, iface); in net_context_bind()
1032 net_sin_ptr(&context->local)->sin_family = AF_INET; in net_context_bind()
1033 net_sin_ptr(&context->local)->sin_addr = ptr; in net_context_bind()
1036 k_mutex_unlock(&context->lock); in net_context_bind()
1037 return net_offload_bind(iface, context, addr, addrlen); in net_context_bind()
1042 ret = recheck_port(context, iface, context->proto, in net_context_bind()
1049 net_sin_ptr(&context->local)->sin_port; in net_context_bind()
1052 ret = recheck_port(context, iface, context->proto, in net_context_bind()
1060 NET_DBG("Context %p binding to %s %s:%d iface %d (%p)", in net_context_bind()
1061 context, in net_context_bind()
1063 net_context_get_proto(context)), in net_context_bind()
1069 k_mutex_unlock(&context->lock); in net_context_bind()
1096 net_context_set_iface(context, iface); in net_context_bind()
1099 context, in net_context_bind()
1104 k_mutex_lock(&context->lock, K_FOREVER); in net_context_bind()
1106 net_context_set_iface(context, iface); in net_context_bind()
1108 net_sll_ptr(&context->local)->sll_family = AF_PACKET; in net_context_bind()
1109 net_sll_ptr(&context->local)->sll_ifindex = in net_context_bind()
1111 net_sll_ptr(&context->local)->sll_protocol = in net_context_bind()
1115 net_sll_ptr(&context->local)->sll_addr = in net_context_bind()
1117 net_sll_ptr(&context->local)->sll_halen = in net_context_bind()
1121 NET_DBG("Context %p bind to type 0x%04x iface[%d] %p addr %s", in net_context_bind()
1122 context, htons(net_context_get_proto(context)), in net_context_bind()
1125 net_sll_ptr(&context->local)->sll_addr, in net_context_bind()
1126 net_sll_ptr(&context->local)->sll_halen)); in net_context_bind()
1128 k_mutex_unlock(&context->lock); in net_context_bind()
1154 net_context_set_iface(context, iface); in net_context_bind()
1157 context, in net_context_bind()
1162 k_mutex_lock(&context->lock, K_FOREVER); in net_context_bind()
1164 net_context_set_iface(context, iface); in net_context_bind()
1165 net_context_set_family(context, AF_CAN); in net_context_bind()
1167 net_can_ptr(&context->local)->can_family = AF_CAN; in net_context_bind()
1168 net_can_ptr(&context->local)->can_ifindex = in net_context_bind()
1171 NET_DBG("Context %p binding to %d iface[%d] %p", in net_context_bind()
1172 context, net_context_get_proto(context), in net_context_bind()
1175 k_mutex_unlock(&context->lock); in net_context_bind()
1200 int net_context_listen(struct net_context *context, int backlog) in net_context_listen() argument
1204 NET_ASSERT(PART_OF_ARRAY(contexts, context)); in net_context_listen()
1206 if (!net_context_is_used(context)) { in net_context_listen()
1211 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_listen()
1212 return net_offload_listen(net_context_get_iface(context), in net_context_listen()
1213 context, backlog); in net_context_listen()
1216 k_mutex_lock(&context->lock, K_FOREVER); in net_context_listen()
1218 if (net_tcp_listen(context) >= 0) { in net_context_listen()
1219 k_mutex_unlock(&context->lock); in net_context_listen()
1223 k_mutex_unlock(&context->lock); in net_context_listen()
1229 int net_context_create_ipv4_new(struct net_context *context, in net_context_create_ipv4_new() argument
1236 struct sockaddr_in_ptr *)&context->local)->sin_addr); in net_context_create_ipv4_new()
1238 src = ((struct sockaddr_in_ptr *)&context->local)->sin_addr; in net_context_create_ipv4_new()
1253 net_pkt_set_ip_dscp(pkt, net_ipv4_get_dscp(context->options.dscp_ecn)); in net_context_create_ipv4_new()
1254 net_pkt_set_ip_ecn(pkt, net_ipv4_get_ecn(context->options.dscp_ecn)); in net_context_create_ipv4_new()
1258 net_ipv4_get_dscp(context->options.dscp_ecn))); in net_context_create_ipv4_new()
1283 int net_context_create_ipv6_new(struct net_context *context, in net_context_create_ipv6_new() argument
1290 struct sockaddr_in6_ptr *)&context->local)->sin6_addr); in net_context_create_ipv6_new()
1292 src = ((struct sockaddr_in6_ptr *)&context->local)->sin6_addr; in net_context_create_ipv6_new()
1298 context->options.addr_preferences); in net_context_create_ipv6_new()
1302 net_pkt_set_ip_dscp(pkt, net_ipv6_get_dscp(context->options.dscp_ecn)); in net_context_create_ipv6_new()
1303 net_pkt_set_ip_ecn(pkt, net_ipv6_get_ecn(context->options.dscp_ecn)); in net_context_create_ipv6_new()
1307 net_ipv6_get_dscp(context->options.dscp_ecn))); in net_context_create_ipv6_new()
1315 int net_context_connect(struct net_context *context, in net_context_connect() argument
1328 NET_ASSERT(PART_OF_ARRAY(contexts, context)); in net_context_connect()
1330 k_mutex_lock(&context->lock, K_FOREVER); in net_context_connect()
1332 if (net_context_get_state(context) == NET_CONTEXT_CONNECTING) { in net_context_connect()
1337 if (!net_context_is_used(context)) { in net_context_connect()
1342 if (addr->sa_family != net_context_get_family(context)) { in net_context_connect()
1343 NET_ASSERT(addr->sa_family == net_context_get_family(context), in net_context_connect()
1346 net_context_get_family(context)); in net_context_connect()
1357 if (net_context_get_state(context) == NET_CONTEXT_LISTENING) { in net_context_connect()
1363 net_context_get_family(context) == AF_INET6) { in net_context_connect()
1365 &context->remote; in net_context_connect()
1372 if (net_context_get_proto(context) == IPPROTO_TCP && in net_context_connect()
1385 context->flags |= NET_CONTEXT_REMOTE_ADDR_SET; in net_context_connect()
1387 context->flags &= ~NET_CONTEXT_REMOTE_ADDR_SET; in net_context_connect()
1398 ret = bind_default(context); in net_context_connect()
1403 net_sin6_ptr(&context->local)->sin6_family = AF_INET6; in net_context_connect()
1406 net_sin6((struct sockaddr *)&context->local)->sin6_port; in net_context_connect()
1408 if (net_sin6_ptr(&context->local)->sin6_addr) { in net_context_connect()
1410 net_sin6_ptr(&context->local)->sin6_addr); in net_context_connect()
1415 net_context_get_family(context) == AF_INET) { in net_context_connect()
1417 &context->remote; in net_context_connect()
1424 if (net_context_get_proto(context) == IPPROTO_TCP && in net_context_connect()
1426 net_ipv4_is_addr_bcast(net_context_get_iface(context), in net_context_connect()
1439 context->flags |= NET_CONTEXT_REMOTE_ADDR_SET; in net_context_connect()
1441 context->flags &= ~NET_CONTEXT_REMOTE_ADDR_SET; in net_context_connect()
1446 ret = bind_default(context); in net_context_connect()
1451 net_sin_ptr(&context->local)->sin_family = AF_INET; in net_context_connect()
1454 net_sin((struct sockaddr *)&context->local)->sin_port; in net_context_connect()
1456 if (net_sin_ptr(&context->local)->sin_addr) { in net_context_connect()
1458 net_sin_ptr(&context->local)->sin_addr); in net_context_connect()
1468 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_connect()
1470 net_context_get_iface(context), in net_context_connect()
1471 context, in net_context_connect()
1481 net_context_get_type(context) == SOCK_DGRAM) { in net_context_connect()
1483 cb(context, 0, user_data); in net_context_connect()
1488 net_context_get_type(context) == SOCK_STREAM) { in net_context_connect()
1491 ret = net_tcp_connect(context, addr, laddr, rport, lport, in net_context_connect()
1498 k_mutex_unlock(&context->lock); in net_context_connect()
1503 int net_context_accept(struct net_context *context, in net_context_accept() argument
1510 NET_ASSERT(PART_OF_ARRAY(contexts, context)); in net_context_accept()
1512 if (!net_context_is_used(context)) { in net_context_accept()
1516 k_mutex_lock(&context->lock, K_FOREVER); in net_context_accept()
1519 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_accept()
1521 net_context_get_iface(context), in net_context_accept()
1522 context, in net_context_accept()
1529 if ((net_context_get_state(context) != NET_CONTEXT_LISTENING) && in net_context_accept()
1530 (net_context_get_type(context) != SOCK_STREAM)) { in net_context_accept()
1532 net_context_get_state(context), in net_context_accept()
1533 net_context_get_type(context)); in net_context_accept()
1538 if (net_context_get_proto(context) == IPPROTO_TCP) { in net_context_accept()
1539 ret = net_tcp_accept(context, cb, user_data); in net_context_accept()
1544 k_mutex_unlock(&context->lock); in net_context_accept()
1598 static int get_context_priority(struct net_context *context, in get_context_priority() argument
1602 return get_uint8_option(context->options.priority, in get_context_priority()
1605 ARG_UNUSED(context); in get_context_priority()
1613 static int get_context_proxy(struct net_context *context, in get_context_proxy() argument
1623 if (*len < context->options.proxy.addrlen) { in get_context_proxy()
1627 *len = MIN(context->options.proxy.addrlen, *len); in get_context_proxy()
1629 memcpy(addr, &context->options.proxy.addr, *len); in get_context_proxy()
1633 ARG_UNUSED(context); in get_context_proxy()
1641 static int get_context_txtime(struct net_context *context, in get_context_txtime() argument
1645 return get_bool_option(context->options.txtime, in get_context_txtime()
1648 ARG_UNUSED(context); in get_context_txtime()
1656 static int get_context_rcvtimeo(struct net_context *context, in get_context_rcvtimeo() argument
1660 *((k_timeout_t *)value) = context->options.rcvtimeo; in get_context_rcvtimeo()
1668 ARG_UNUSED(context); in get_context_rcvtimeo()
1676 static int get_context_sndtimeo(struct net_context *context, in get_context_sndtimeo() argument
1680 *((k_timeout_t *)value) = context->options.sndtimeo; in get_context_sndtimeo()
1688 ARG_UNUSED(context); in get_context_sndtimeo()
1696 static int get_context_rcvbuf(struct net_context *context, in get_context_rcvbuf() argument
1700 return get_uint16_option(context->options.rcvbuf, in get_context_rcvbuf()
1703 ARG_UNUSED(context); in get_context_rcvbuf()
1711 static int get_context_sndbuf(struct net_context *context, in get_context_sndbuf() argument
1715 return get_uint16_option(context->options.sndbuf, in get_context_sndbuf()
1718 ARG_UNUSED(context); in get_context_sndbuf()
1726 static int get_context_dscp_ecn(struct net_context *context, in get_context_dscp_ecn() argument
1730 return get_uint8_option(context->options.dscp_ecn, in get_context_dscp_ecn()
1733 ARG_UNUSED(context); in get_context_dscp_ecn()
1741 static int get_context_ttl(struct net_context *context, in get_context_ttl() argument
1745 *((int *)value) = context->ipv4_ttl; in get_context_ttl()
1753 ARG_UNUSED(context); in get_context_ttl()
1761 static int get_context_mcast_ttl(struct net_context *context, in get_context_mcast_ttl() argument
1765 *((int *)value) = context->ipv4_mcast_ttl; in get_context_mcast_ttl()
1773 ARG_UNUSED(context); in get_context_mcast_ttl()
1781 static int get_context_mcast_hop_limit(struct net_context *context, in get_context_mcast_hop_limit() argument
1785 *((int *)value) = context->ipv6_mcast_hop_limit; in get_context_mcast_hop_limit()
1793 ARG_UNUSED(context); in get_context_mcast_hop_limit()
1801 static int get_context_unicast_hop_limit(struct net_context *context, in get_context_unicast_hop_limit() argument
1805 *((int *)value) = context->ipv6_hop_limit; in get_context_unicast_hop_limit()
1813 ARG_UNUSED(context); in get_context_unicast_hop_limit()
1821 static int get_context_reuseaddr(struct net_context *context, in get_context_reuseaddr() argument
1825 return get_bool_option(context->options.reuseaddr, in get_context_reuseaddr()
1828 ARG_UNUSED(context); in get_context_reuseaddr()
1836 static int get_context_reuseport(struct net_context *context, in get_context_reuseport() argument
1840 return get_bool_option(context->options.reuseport, in get_context_reuseport()
1843 ARG_UNUSED(context); in get_context_reuseport()
1851 static int get_context_ipv6_v6only(struct net_context *context, in get_context_ipv6_v6only() argument
1855 return get_bool_option(context->options.ipv6_v6only, in get_context_ipv6_v6only()
1858 ARG_UNUSED(context); in get_context_ipv6_v6only()
1866 static int get_context_recv_pktinfo(struct net_context *context, in get_context_recv_pktinfo() argument
1870 return get_bool_option(context->options.recv_pktinfo, in get_context_recv_pktinfo()
1873 ARG_UNUSED(context); in get_context_recv_pktinfo()
1881 static int get_context_addr_preferences(struct net_context *context, in get_context_addr_preferences() argument
1885 return get_uint16_option(context->options.addr_preferences, in get_context_addr_preferences()
1888 ARG_UNUSED(context); in get_context_addr_preferences()
1896 static int get_context_timestamping(struct net_context *context, in get_context_timestamping() argument
1900 *((uint8_t *)value) = context->options.timestamping; in get_context_timestamping()
1908 ARG_UNUSED(context); in get_context_timestamping()
1916 static int get_context_mtu(struct net_context *context, in get_context_mtu() argument
1919 sa_family_t family = net_context_get_family(context); in get_context_mtu()
1924 mtu = net_pmtu_get_mtu(&context->remote); in get_context_mtu()
1930 if (net_context_is_bound_to_iface(context)) { in get_context_mtu()
1931 iface = net_context_get_iface(context); in get_context_mtu()
1937 &net_sin6(&context->remote)->sin6_addr); in get_context_mtu()
1940 &net_sin(&context->remote)->sin_addr); in get_context_mtu()
1958 static int get_context_mcast_ifindex(struct net_context *context, in get_context_mcast_ifindex() argument
1962 sa_family_t family = net_context_get_family(context); in get_context_mcast_ifindex()
1969 if (context->options.ipv6_mcast_ifindex == 0) { in get_context_mcast_ifindex()
1973 if (net_context_is_bound_to_iface(context)) { in get_context_mcast_ifindex()
1974 iface = net_context_get_iface(context); in get_context_mcast_ifindex()
1996 *((int *)value) = context->options.ipv6_mcast_ifindex; in get_context_mcast_ifindex()
2008 ARG_UNUSED(context); in get_context_mcast_ifindex()
2016 static int get_context_local_port_range(struct net_context *context, in get_context_local_port_range() argument
2024 *((uint32_t *)value) = context->options.port_range; in get_context_local_port_range()
2028 ARG_UNUSED(context); in get_context_local_port_range()
2068 static int context_setup_udp_packet(struct net_context *context, in context_setup_udp_packet() argument
2085 ret = net_context_create_ipv6_new(context, pkt, in context_setup_udp_packet()
2092 ret = net_context_create_ipv4_new(context, pkt, in context_setup_udp_packet()
2100 ret = bind_default(context); in context_setup_udp_packet()
2107 &context->local)->sin_port, in context_setup_udp_packet()
2119 if (context->options.timestamping & SOF_TIMESTAMPING_TX_HARDWARE) { in context_setup_udp_packet()
2123 if (context->options.timestamping & SOF_TIMESTAMPING_RX_HARDWARE) { in context_setup_udp_packet()
2131 static void context_finalize_packet(struct net_context *context, in context_finalize_packet() argument
2142 net_ipv6_finalize(pkt, net_context_get_proto(context)); in context_finalize_packet()
2144 net_ipv4_finalize(pkt, net_context_get_proto(context)); in context_finalize_packet()
2148 static struct net_pkt *context_alloc_pkt(struct net_context *context, in context_alloc_pkt() argument
2155 if (context->tx_slab) { in context_alloc_pkt()
2156 pkt = net_pkt_alloc_from_slab(context->tx_slab(), timeout); in context_alloc_pkt()
2161 net_pkt_set_iface(pkt, net_context_get_iface(context)); in context_alloc_pkt()
2163 net_pkt_set_context(pkt, context); in context_alloc_pkt()
2166 net_context_get_proto(context), in context_alloc_pkt()
2176 pkt = net_pkt_alloc_with_buffer(net_context_get_iface(context), len, in context_alloc_pkt()
2178 net_context_get_proto(context), in context_alloc_pkt()
2181 net_pkt_set_context(pkt, context); in context_alloc_pkt()
2202 static int context_sendto(struct net_context *context, in context_sendto() argument
2219 NET_ASSERT(PART_OF_ARRAY(contexts, context)); in context_sendto()
2221 if (!net_context_is_used(context)) { in context_sendto()
2240 net_context_get_family(context) == AF_INET6 && in context_sendto()
2244 family = net_context_get_family(context); in context_sendto()
2256 addr6 = net_sin6(&context->remote); in context_sendto()
2274 net_context_get_type(context) == SOCK_DGRAM) { in context_sendto()
2277 (context->options.ipv6_mcast_ifindex > 0), (false))) { in context_sendto()
2280 context->options.ipv6_mcast_ifindex))); in context_sendto()
2293 &net_sin6(&context->remote)->sin6_addr) && in context_sendto()
2294 !net_context_is_bound_to_iface(context)) { in context_sendto()
2296 net_context_set_iface(context, iface); in context_sendto()
2309 addr4 = net_sin(&context->remote); in context_sendto()
2340 net_context_get_type(context) == SOCK_DGRAM) { in context_sendto()
2343 (context->options.ipv4_mcast_ifindex > 0), (false))) { in context_sendto()
2346 context->options.ipv4_mcast_ifindex))); in context_sendto()
2358 if (net_sin(&context->remote)->sin_addr.s_addr == 0U && in context_sendto()
2359 !net_context_is_bound_to_iface(context)) { in context_sendto()
2361 net_context_set_iface(context, iface); in context_sendto()
2374 (&context->remote); in context_sendto()
2387 iface = net_context_get_iface(context); in context_sendto()
2401 if (net_context_get_type(context) == SOCK_DGRAM) { in context_sendto()
2402 context->flags |= NET_CONTEXT_REMOTE_ADDR_SET; in context_sendto()
2409 if ((void *)&context->remote != (void *)ll_addr) { in context_sendto()
2410 memcpy((struct sockaddr_ll *)&context->remote, in context_sendto()
2424 (&context->remote); in context_sendto()
2440 net_can_ptr(&context->local)->can_ifindex; in context_sendto()
2466 iface = net_context_get_iface(context); in context_sendto()
2471 context->send_cb = cb; in context_sendto()
2472 context->user_data = user_data; in context_sendto()
2475 net_context_get_proto(context) == IPPROTO_TCP && in context_sendto()
2476 !net_if_is_ip_offloaded(net_context_get_iface(context))) { in context_sendto()
2480 pkt = context_alloc_pkt(context, family, len, PKT_WAIT_TIME); in context_sendto()
2487 pkt, net_context_get_proto(context)); in context_sendto()
2489 if (net_context_get_type(context) == SOCK_DGRAM) { in context_sendto()
2501 get_context_priority(context, &priority, NULL); in context_sendto()
2512 get_context_txtime(context, &is_txtime, NULL); in context_sendto()
2521 net_if_is_ip_offloaded(net_context_get_iface(context))) { in context_sendto()
2530 ret = net_offload_sendto(net_context_get_iface(context), in context_sendto()
2534 ret = net_offload_send(net_context_get_iface(context), in context_sendto()
2538 net_context_get_proto(context) == IPPROTO_UDP) { in context_sendto()
2539 ret = context_setup_udp_packet(context, family, pkt, buf, len, msghdr, in context_sendto()
2545 context_finalize_packet(context, family, pkt); in context_sendto()
2549 net_context_get_proto(context) == IPPROTO_TCP) { in context_sendto()
2551 ret = net_tcp_queue(context, buf, len, msghdr); in context_sendto()
2558 ret = net_tcp_send_data(context, cb, user_data); in context_sendto()
2567 if (net_context_get_proto(context) == IPPROTO_RAW) { in context_sendto()
2589 net_context_get_proto(context) == CAN_RAW) { in context_sendto()
2600 net_context_get_proto(context)); in context_sendto()
2617 int net_context_send(struct net_context *context, in net_context_send() argument
2627 k_mutex_lock(&context->lock, K_FOREVER); in net_context_send()
2629 if (!(context->flags & NET_CONTEXT_REMOTE_ADDR_SET) || in net_context_send()
2630 !net_sin(&context->remote)->sin_port) { in net_context_send()
2636 net_context_get_family(context) == AF_INET6) { in net_context_send()
2639 net_context_get_family(context) == AF_INET) { in net_context_send()
2642 net_context_get_family(context) == AF_PACKET) { in net_context_send()
2646 net_context_get_family(context) == AF_CAN) { in net_context_send()
2652 ret = context_sendto(context, buf, len, &context->remote, in net_context_send()
2655 k_mutex_unlock(&context->lock); in net_context_send()
2660 int net_context_sendmsg(struct net_context *context, in net_context_sendmsg() argument
2669 k_mutex_lock(&context->lock, K_FOREVER); in net_context_sendmsg()
2671 ret = context_sendto(context, msghdr, 0, NULL, 0, in net_context_sendmsg()
2674 k_mutex_unlock(&context->lock); in net_context_sendmsg()
2679 int net_context_sendto(struct net_context *context, in net_context_sendto() argument
2690 k_mutex_lock(&context->lock, K_FOREVER); in net_context_sendto()
2692 ret = context_sendto(context, buf, len, dst_addr, addrlen, in net_context_sendto()
2695 k_mutex_unlock(&context->lock); in net_context_sendto()
2706 struct net_context *context = find_context(conn); in net_context_packet_received() local
2709 NET_ASSERT(context); in net_context_packet_received()
2712 k_mutex_lock(&context->lock, K_FOREVER); in net_context_packet_received()
2714 net_context_set_iface(context, net_pkt_iface(pkt)); in net_context_packet_received()
2715 net_pkt_set_context(pkt, context); in net_context_packet_received()
2720 if (!context->recv_cb) { in net_context_packet_received()
2724 if (net_context_get_proto(context) == IPPROTO_TCP) { in net_context_packet_received()
2730 k_sem_give(&context->recv_data_wait); in net_context_packet_received()
2733 k_mutex_unlock(&context->lock); in net_context_packet_received()
2735 context->recv_cb(context, pkt, ip_hdr, proto_hdr, 0, user_data); in net_context_packet_received()
2742 k_mutex_unlock(&context->lock); in net_context_packet_received()
2748 static int recv_udp(struct net_context *context, in recv_udp() argument
2754 .sa_family = net_context_get_family(context), in recv_udp()
2762 /* If the context already has a connection handler, it means it's in recv_udp()
2769 * set for the net context passed by recv_udp(). in recv_udp()
2771 if (context->conn_handler) { in recv_udp()
2772 context->recv_cb = cb; in recv_udp()
2773 ret = net_conn_update(context->conn_handler, in recv_udp()
2776 context->flags & NET_CONTEXT_REMOTE_ADDR_SET ? in recv_udp()
2777 &context->remote : NULL, in recv_udp()
2778 ntohs(net_sin(&context->remote)->sin_port)); in recv_udp()
2782 ret = bind_default(context); in recv_udp()
2788 net_context_get_family(context) == AF_INET6) { in recv_udp()
2789 if (net_sin6_ptr(&context->local)->sin6_addr) { in recv_udp()
2791 net_sin6_ptr(&context->local)->sin6_addr); in recv_udp()
2797 net_sin6((struct sockaddr *)&context->local)->sin6_port; in recv_udp()
2798 lport = net_sin6((struct sockaddr *)&context->local)->sin6_port; in recv_udp()
2800 net_context_get_family(context) == AF_INET) { in recv_udp()
2801 if (net_sin_ptr(&context->local)->sin_addr) { in recv_udp()
2803 net_sin_ptr(&context->local)->sin_addr); in recv_udp()
2808 lport = net_sin((struct sockaddr *)&context->local)->sin_port; in recv_udp()
2811 context->recv_cb = cb; in recv_udp()
2813 ret = net_conn_register(net_context_get_proto(context), in recv_udp()
2814 net_context_get_family(context), in recv_udp()
2815 context->flags & NET_CONTEXT_REMOTE_ADDR_SET ? in recv_udp()
2816 &context->remote : NULL, in recv_udp()
2818 ntohs(net_sin(&context->remote)->sin_port), in recv_udp()
2820 context, in recv_udp()
2823 &context->conn_handler); in recv_udp()
2838 struct net_context *context = find_context(conn); in net_context_raw_packet_received() local
2840 NET_ASSERT(context); in net_context_raw_packet_received()
2847 if (!context->recv_cb) { in net_context_raw_packet_received()
2851 net_context_set_iface(context, net_pkt_iface(pkt)); in net_context_raw_packet_received()
2852 net_pkt_set_context(pkt, context); in net_context_raw_packet_received()
2854 context->recv_cb(context, pkt, ip_hdr, proto_hdr, 0, user_data); in net_context_raw_packet_received()
2857 k_sem_give(&context->recv_data_wait); in net_context_raw_packet_received()
2863 static int recv_raw(struct net_context *context, in recv_raw() argument
2873 /* If the context already has a connection handler, it means it's in recv_raw()
2880 * set for the net context passed by recv_raw(). in recv_raw()
2882 if (context->conn_handler) { in recv_raw()
2883 context->recv_cb = cb; in recv_raw()
2884 ret = net_conn_update(context->conn_handler, in recv_raw()
2891 ret = bind_default(context); in recv_raw()
2896 context->recv_cb = cb; in recv_raw()
2898 ret = net_conn_register(net_context_get_proto(context), in recv_raw()
2899 net_context_get_family(context), in recv_raw()
2901 context, in recv_raw()
2904 &context->conn_handler); in recv_raw()
2909 int net_context_recv(struct net_context *context, in net_context_recv() argument
2915 NET_ASSERT(context); in net_context_recv()
2917 if (!net_context_is_used(context)) { in net_context_recv()
2921 k_mutex_lock(&context->lock, K_FOREVER); in net_context_recv()
2924 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_recv()
2926 net_context_get_iface(context), in net_context_recv()
2927 context, cb, timeout, user_data); in net_context_recv()
2932 net_context_get_proto(context) == IPPROTO_UDP) { in net_context_recv()
2933 ret = recv_udp(context, cb, timeout, user_data); in net_context_recv()
2935 net_context_get_proto(context) == IPPROTO_TCP) { in net_context_recv()
2936 ret = net_tcp_recv(context, cb, user_data); in net_context_recv()
2939 net_context_get_family(context) == AF_PACKET) { in net_context_recv()
2944 net_sll_ptr(&context->local)->sll_ifindex; in net_context_recv()
2946 net_sll_ptr(&context->local)->sll_protocol; in net_context_recv()
2948 net_sll_ptr(&context->local)->sll_halen; in net_context_recv()
2951 net_sll_ptr(&context->local)->sll_addr, in net_context_recv()
2954 ret = recv_raw(context, cb, timeout, in net_context_recv()
2957 net_context_get_family(context) == AF_CAN) { in net_context_recv()
2962 ret = recv_raw(context, cb, timeout, in net_context_recv()
2987 k_sem_reset(&context->recv_data_wait); in net_context_recv()
2989 k_mutex_unlock(&context->lock); in net_context_recv()
2991 if (k_sem_take(&context->recv_data_wait, timeout) == -EAGAIN) { in net_context_recv()
2995 k_mutex_lock(&context->lock, K_FOREVER); in net_context_recv()
3000 k_mutex_unlock(&context->lock); in net_context_recv()
3005 int net_context_update_recv_wnd(struct net_context *context, in net_context_update_recv_wnd() argument
3011 net_if_is_ip_offloaded(net_context_get_iface(context))) { in net_context_update_recv_wnd()
3015 k_mutex_lock(&context->lock, K_FOREVER); in net_context_update_recv_wnd()
3017 ret = net_tcp_update_recv_wnd(context, delta); in net_context_update_recv_wnd()
3019 k_mutex_unlock(&context->lock); in net_context_update_recv_wnd()
3078 static int set_context_priority(struct net_context *context, in set_context_priority() argument
3082 return set_uint8_option(&context->options.priority, value, len); in set_context_priority()
3084 ARG_UNUSED(context); in set_context_priority()
3092 static int set_context_txtime(struct net_context *context, in set_context_txtime() argument
3096 return set_bool_option(&context->options.txtime, value, len); in set_context_txtime()
3098 ARG_UNUSED(context); in set_context_txtime()
3106 static int set_context_proxy(struct net_context *context, in set_context_proxy() argument
3116 if (addr->sa_family != net_context_get_family(context)) { in set_context_proxy()
3120 context->options.proxy.addrlen = len; in set_context_proxy()
3121 memcpy(&context->options.proxy.addr, addr, len); in set_context_proxy()
3125 ARG_UNUSED(context); in set_context_proxy()
3133 static int set_context_rcvtimeo(struct net_context *context, in set_context_rcvtimeo() argument
3141 context->options.rcvtimeo = *((k_timeout_t *)value); in set_context_rcvtimeo()
3145 ARG_UNUSED(context); in set_context_rcvtimeo()
3153 static int set_context_sndtimeo(struct net_context *context, in set_context_sndtimeo() argument
3161 context->options.sndtimeo = *((k_timeout_t *)value); in set_context_sndtimeo()
3165 ARG_UNUSED(context); in set_context_sndtimeo()
3173 static int set_context_rcvbuf(struct net_context *context, in set_context_rcvbuf() argument
3177 return set_uint16_option(&context->options.rcvbuf, value, len); in set_context_rcvbuf()
3179 ARG_UNUSED(context); in set_context_rcvbuf()
3187 static int set_context_sndbuf(struct net_context *context, in set_context_sndbuf() argument
3191 return set_uint16_option(&context->options.sndbuf, value, len); in set_context_sndbuf()
3193 ARG_UNUSED(context); in set_context_sndbuf()
3201 static int set_context_dscp_ecn(struct net_context *context, in set_context_dscp_ecn() argument
3205 return set_uint8_option(&context->options.dscp_ecn, value, len); in set_context_dscp_ecn()
3207 ARG_UNUSED(context); in set_context_dscp_ecn()
3215 static int set_context_ttl(struct net_context *context, in set_context_ttl() argument
3221 len = sizeof(context->ipv4_ttl); in set_context_ttl()
3223 return set_uint8_option(&context->ipv4_ttl, &ttl, len); in set_context_ttl()
3225 ARG_UNUSED(context); in set_context_ttl()
3233 static int set_context_mcast_ttl(struct net_context *context, in set_context_mcast_ttl() argument
3239 len = sizeof(context->ipv4_mcast_ttl); in set_context_mcast_ttl()
3241 return set_uint8_option(&context->ipv4_mcast_ttl, &mcast_ttl, len); in set_context_mcast_ttl()
3243 ARG_UNUSED(context); in set_context_mcast_ttl()
3251 static int set_context_mcast_hop_limit(struct net_context *context, in set_context_mcast_hop_limit() argument
3265 if (net_if_get_by_index(context->iface) == NULL) { in set_context_mcast_hop_limit()
3269 net_if_get_by_index(context->iface)); in set_context_mcast_hop_limit()
3275 context->ipv6_mcast_hop_limit = mcast_hop_limit; in set_context_mcast_hop_limit()
3279 ARG_UNUSED(context); in set_context_mcast_hop_limit()
3287 static int set_context_unicast_hop_limit(struct net_context *context, in set_context_unicast_hop_limit() argument
3293 len = sizeof(context->ipv6_hop_limit); in set_context_unicast_hop_limit()
3295 return set_uint8_option(&context->ipv6_hop_limit, in set_context_unicast_hop_limit()
3298 ARG_UNUSED(context); in set_context_unicast_hop_limit()
3306 static int set_context_reuseaddr(struct net_context *context, in set_context_reuseaddr() argument
3310 return set_bool_option(&context->options.reuseaddr, value, len); in set_context_reuseaddr()
3312 ARG_UNUSED(context); in set_context_reuseaddr()
3320 static int set_context_reuseport(struct net_context *context, in set_context_reuseport() argument
3324 return set_bool_option(&context->options.reuseport, value, len); in set_context_reuseport()
3326 ARG_UNUSED(context); in set_context_reuseport()
3334 static int set_context_ipv6_mtu(struct net_context *context, in set_context_ipv6_mtu() argument
3350 ret = net_pmtu_update_mtu(&context->remote, mtu); in set_context_ipv6_mtu()
3358 if (net_context_is_bound_to_iface(context)) { in set_context_ipv6_mtu()
3359 iface = net_context_get_iface(context); in set_context_ipv6_mtu()
3361 sa_family_t family = net_context_get_family(context); in set_context_ipv6_mtu()
3365 &net_sin6(&context->remote)->sin6_addr); in set_context_ipv6_mtu()
3375 ARG_UNUSED(context); in set_context_ipv6_mtu()
3383 static int set_context_ipv6_v6only(struct net_context *context, in set_context_ipv6_v6only() argument
3387 return set_bool_option(&context->options.ipv6_v6only, value, len); in set_context_ipv6_v6only()
3389 ARG_UNUSED(context); in set_context_ipv6_v6only()
3397 static int set_context_recv_pktinfo(struct net_context *context, in set_context_recv_pktinfo() argument
3401 return set_bool_option(&context->options.recv_pktinfo, value, len); in set_context_recv_pktinfo()
3403 ARG_UNUSED(context); in set_context_recv_pktinfo()
3411 static int set_context_addr_preferences(struct net_context *context, in set_context_addr_preferences() argument
3415 return set_uint16_option(&context->options.addr_preferences, in set_context_addr_preferences()
3418 ARG_UNUSED(context); in set_context_addr_preferences()
3426 static int set_context_timestamping(struct net_context *context, in set_context_timestamping() argument
3432 return set_uint8_option(&context->options.timestamping, in set_context_timestamping()
3435 ARG_UNUSED(context); in set_context_timestamping()
3443 static int set_context_mcast_ifindex(struct net_context *context, in set_context_mcast_ifindex() argument
3447 sa_family_t family = net_context_get_family(context); in set_context_mcast_ifindex()
3459 type = net_context_get_type(context); in set_context_mcast_ifindex()
3466 context->options.ipv6_mcast_ifindex = 0; in set_context_mcast_ifindex()
3489 context->options.ipv6_mcast_ifindex = mcast_ifindex; in set_context_mcast_ifindex()
3496 ARG_UNUSED(context); in set_context_mcast_ifindex()
3504 static int set_context_local_port_range(struct net_context *context, in set_context_local_port_range() argument
3531 context->options.port_range = port_range; in set_context_local_port_range()
3535 ARG_UNUSED(context); in set_context_local_port_range()
3543 int net_context_set_option(struct net_context *context, in net_context_set_option() argument
3549 NET_ASSERT(context); in net_context_set_option()
3551 if (!PART_OF_ARRAY(contexts, context)) { in net_context_set_option()
3555 k_mutex_lock(&context->lock, K_FOREVER); in net_context_set_option()
3559 ret = set_context_priority(context, value, len); in net_context_set_option()
3562 ret = set_context_txtime(context, value, len); in net_context_set_option()
3565 ret = set_context_proxy(context, value, len); in net_context_set_option()
3568 ret = set_context_rcvtimeo(context, value, len); in net_context_set_option()
3571 ret = set_context_sndtimeo(context, value, len); in net_context_set_option()
3574 ret = set_context_rcvbuf(context, value, len); in net_context_set_option()
3577 ret = set_context_sndbuf(context, value, len); in net_context_set_option()
3580 ret = set_context_dscp_ecn(context, value, len); in net_context_set_option()
3583 ret = set_context_ttl(context, value, len); in net_context_set_option()
3586 ret = set_context_mcast_ttl(context, value, len); in net_context_set_option()
3589 ret = set_context_mcast_hop_limit(context, value, len); in net_context_set_option()
3592 ret = set_context_unicast_hop_limit(context, value, len); in net_context_set_option()
3595 ret = set_context_reuseaddr(context, value, len); in net_context_set_option()
3598 ret = set_context_reuseport(context, value, len); in net_context_set_option()
3601 ret = set_context_ipv6_v6only(context, value, len); in net_context_set_option()
3604 ret = set_context_recv_pktinfo(context, value, len); in net_context_set_option()
3607 ret = set_context_addr_preferences(context, value, len); in net_context_set_option()
3610 ret = set_context_timestamping(context, value, len); in net_context_set_option()
3615 net_context_get_family(context) == AF_INET) { in net_context_set_option()
3618 net_context_get_family(context) == AF_INET6) { in net_context_set_option()
3619 ret = set_context_ipv6_mtu(context, value, len); in net_context_set_option()
3624 ret = set_context_mcast_ifindex(context, value, len); in net_context_set_option()
3627 ret = set_context_local_port_range(context, value, len); in net_context_set_option()
3631 k_mutex_unlock(&context->lock); in net_context_set_option()
3636 int net_context_get_option(struct net_context *context, in net_context_get_option() argument
3642 NET_ASSERT(context); in net_context_get_option()
3644 if (!PART_OF_ARRAY(contexts, context)) { in net_context_get_option()
3648 k_mutex_lock(&context->lock, K_FOREVER); in net_context_get_option()
3652 ret = get_context_priority(context, value, len); in net_context_get_option()
3655 ret = get_context_txtime(context, value, len); in net_context_get_option()
3658 ret = get_context_proxy(context, value, len); in net_context_get_option()
3661 ret = get_context_rcvtimeo(context, value, len); in net_context_get_option()
3664 ret = get_context_sndtimeo(context, value, len); in net_context_get_option()
3667 ret = get_context_rcvbuf(context, value, len); in net_context_get_option()
3670 ret = get_context_sndbuf(context, value, len); in net_context_get_option()
3673 ret = get_context_dscp_ecn(context, value, len); in net_context_get_option()
3676 ret = get_context_ttl(context, value, len); in net_context_get_option()
3679 ret = get_context_mcast_ttl(context, value, len); in net_context_get_option()
3682 ret = get_context_mcast_hop_limit(context, value, len); in net_context_get_option()
3685 ret = get_context_unicast_hop_limit(context, value, len); in net_context_get_option()
3688 ret = get_context_reuseaddr(context, value, len); in net_context_get_option()
3691 ret = get_context_reuseport(context, value, len); in net_context_get_option()
3694 ret = get_context_ipv6_v6only(context, value, len); in net_context_get_option()
3697 ret = get_context_recv_pktinfo(context, value, len); in net_context_get_option()
3700 ret = get_context_addr_preferences(context, value, len); in net_context_get_option()
3703 ret = get_context_timestamping(context, value, len); in net_context_get_option()
3706 ret = get_context_mtu(context, value, len); in net_context_get_option()
3709 ret = get_context_mcast_ifindex(context, value, len); in net_context_get_option()
3712 ret = get_context_local_port_range(context, value, len); in net_context_get_option()
3716 k_mutex_unlock(&context->lock); in net_context_get_option()
3787 const char *net_context_state(struct net_context *context) in net_context_state() argument
3789 switch (net_context_get_state(context)) { in net_context_state()