+void
+udp_fill_keepalive_offload_frames(ifnet_t ifp,
+ struct ifnet_keepalive_offload_frame *frames_array,
+ u_int32_t frames_array_count, size_t frame_data_offset,
+ u_int32_t *used_frames_count)
+{
+ struct inpcb *inp;
+ inp_gen_t gencnt;
+ u_int32_t frame_index = *used_frames_count;
+
+ if (ifp == NULL || frames_array == NULL ||
+ frames_array_count == 0 ||
+ frame_index >= frames_array_count ||
+ frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE)
+ return;
+
+ lck_rw_lock_shared(udbinfo.ipi_lock);
+ gencnt = udbinfo.ipi_gencnt;
+ LIST_FOREACH(inp, udbinfo.ipi_listhead, inp_list) {
+ struct socket *so;
+ u_int8_t *data;
+ struct ifnet_keepalive_offload_frame *frame;
+ struct mbuf *m = NULL;
+
+ if (frame_index >= frames_array_count)
+ break;
+
+ if (inp->inp_gencnt > gencnt ||
+ inp->inp_state == INPCB_STATE_DEAD)
+ continue;
+
+ if ((so = inp->inp_socket) == NULL ||
+ (so->so_state & SS_DEFUNCT))
+ continue;
+ /*
+ * check for keepalive offload flag without socket
+ * lock to avoid a deadlock
+ */
+ if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) {
+ continue;
+ }
+
+ udp_lock(so, 1, 0);
+ if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if ((inp->inp_vflag & INP_IPV4) &&
+ (inp->inp_laddr.s_addr == INADDR_ANY ||
+ inp->inp_faddr.s_addr == INADDR_ANY)) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if ((inp->inp_vflag & INP_IPV6) &&
+ (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ||
+ IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if (inp->inp_lport == 0 || inp->inp_fport == 0) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if (inp->inp_last_outifp == NULL ||
+ inp->inp_last_outifp->if_index != ifp->if_index) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if ((inp->inp_vflag & INP_IPV4)) {
+ if ((frame_data_offset + sizeof(struct udpiphdr) +
+ inp->inp_keepalive_datalen) >
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if ((sizeof(struct udpiphdr) +
+ inp->inp_keepalive_datalen) > _MHLEN) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ } else {
+ if ((frame_data_offset + sizeof(struct ip6_hdr) +
+ sizeof(struct udphdr) +
+ inp->inp_keepalive_datalen) >
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ if ((sizeof(struct ip6_hdr) + sizeof(struct udphdr) +
+ inp->inp_keepalive_datalen) > _MHLEN) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ }
+ MGETHDR(m, M_WAIT, MT_HEADER);
+ if (m == NULL) {
+ udp_unlock(so, 1, 0);
+ continue;
+ }
+ /*
+ * This inp has all the information that is needed to
+ * generate an offload frame.
+ */
+ if (inp->inp_vflag & INP_IPV4) {
+ struct ip *ip;
+ struct udphdr *udp;
+
+ frame = &frames_array[frame_index];
+ frame->length = frame_data_offset +
+ sizeof(struct udpiphdr) +
+ inp->inp_keepalive_datalen;
+ frame->ether_type =
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4;
+ frame->interval = inp->inp_keepalive_interval;
+ switch (inp->inp_keepalive_type) {
+ case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
+ frame->type =
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
+ break;
+ default:
+ break;
+ }
+ data = mtod(m, u_int8_t *);
+ bzero(data, sizeof(struct udpiphdr));
+ ip = (__typeof__(ip))(void *)data;
+ udp = (__typeof__(udp))(void *) (data +
+ sizeof(struct ip));
+ m->m_len = sizeof(struct udpiphdr);
+ data = data + sizeof(struct udpiphdr);
+ if (inp->inp_keepalive_datalen > 0 &&
+ inp->inp_keepalive_data != NULL) {
+ bcopy(inp->inp_keepalive_data, data,
+ inp->inp_keepalive_datalen);
+ m->m_len += inp->inp_keepalive_datalen;
+ }
+ m->m_pkthdr.len = m->m_len;
+
+ ip->ip_v = IPVERSION;
+ ip->ip_hl = (sizeof(struct ip) >> 2);
+ ip->ip_p = IPPROTO_UDP;
+ ip->ip_len = htons(sizeof(struct udpiphdr) +
+ (u_short)inp->inp_keepalive_datalen);
+ ip->ip_ttl = inp->inp_ip_ttl;
+ ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);
+ ip->ip_src = inp->inp_laddr;
+ ip->ip_dst = inp->inp_faddr;
+ ip->ip_sum = in_cksum_hdr_opt(ip);
+
+ udp->uh_sport = inp->inp_lport;
+ udp->uh_dport = inp->inp_fport;
+ udp->uh_ulen = htons(sizeof(struct udphdr) +
+ (u_short)inp->inp_keepalive_datalen);
+
+ if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
+ udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
+ ip->ip_dst.s_addr,
+ htons(sizeof(struct udphdr) +
+ (u_short)inp->inp_keepalive_datalen +
+ IPPROTO_UDP));
+ m->m_pkthdr.csum_flags = CSUM_UDP;
+ m->m_pkthdr.csum_data = offsetof(struct udphdr,
+ uh_sum);
+ }
+ m->m_pkthdr.pkt_proto = IPPROTO_UDP;
+ in_delayed_cksum(m);
+ bcopy(m->m_data, frame->data + frame_data_offset,
+ m->m_len);
+ } else {
+ struct ip6_hdr *ip6;
+ struct udphdr *udp6;
+
+ VERIFY(inp->inp_vflag & INP_IPV6);
+ frame = &frames_array[frame_index];
+ frame->length = frame_data_offset +
+ sizeof(struct ip6_hdr) +
+ sizeof(struct udphdr) +
+ inp->inp_keepalive_datalen;
+ frame->ether_type =
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6;
+ frame->interval = inp->inp_keepalive_interval;
+ switch (inp->inp_keepalive_type) {
+ case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
+ frame->type =
+ IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
+ break;
+ default:
+ break;
+ }
+ data = mtod(m, u_int8_t *);
+ bzero(data, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
+ ip6 = (__typeof__(ip6))(void *)data;
+ udp6 = (__typeof__(udp6))(void *)(data +
+ sizeof(struct ip6_hdr));
+ m->m_len = sizeof(struct ip6_hdr) +
+ sizeof(struct udphdr);
+ data = data + (sizeof(struct ip6_hdr) +
+ sizeof(struct udphdr));
+ if (inp->inp_keepalive_datalen > 0 &&
+ inp->inp_keepalive_data != NULL) {
+ bcopy(inp->inp_keepalive_data, data,
+ inp->inp_keepalive_datalen);
+ m->m_len += inp->inp_keepalive_datalen;
+ }
+ m->m_pkthdr.len = m->m_len;
+ ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
+ ip6->ip6_flow = ip6->ip6_flow & ~IPV6_FLOW_ECN_MASK;
+ ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
+ ip6->ip6_vfc |= IPV6_VERSION;
+ ip6->ip6_nxt = IPPROTO_UDP;
+ ip6->ip6_hlim = ip6_defhlim;
+ ip6->ip6_plen = htons(sizeof(struct udphdr) +
+ (u_short)inp->inp_keepalive_datalen);
+ ip6->ip6_src = inp->in6p_laddr;
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
+ ip6->ip6_src.s6_addr16[1] = 0;
+
+ ip6->ip6_dst = inp->in6p_faddr;
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
+ ip6->ip6_dst.s6_addr16[1] = 0;
+
+ udp6->uh_sport = inp->in6p_lport;
+ udp6->uh_dport = inp->in6p_fport;
+ udp6->uh_ulen = htons(sizeof(struct udphdr) +
+ (u_short)inp->inp_keepalive_datalen);
+ if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
+ udp6->uh_sum = in6_pseudo(&ip6->ip6_src,
+ &ip6->ip6_dst,
+ htonl(sizeof(struct udphdr) +
+ (u_short)inp->inp_keepalive_datalen +
+ IPPROTO_UDP));
+ m->m_pkthdr.csum_flags = CSUM_UDPIPV6;
+ m->m_pkthdr.csum_data = offsetof(struct udphdr,
+ uh_sum);
+ }
+ m->m_pkthdr.pkt_proto = IPPROTO_UDP;
+ in6_delayed_cksum(m);
+ bcopy(m->m_data, frame->data + frame_data_offset,
+ m->m_len);
+ }
+ if (m != NULL) {
+ m_freem(m);
+ m = NULL;
+ }
+ frame_index++;
+ udp_unlock(so, 1, 0);
+ }
+ lck_rw_done(udbinfo.ipi_lock);
+ *used_frames_count = frame_index;
+}