+ udpstat.udps_snd_swcsum++;
+ udpstat.udps_snd_swcsum_bytes += len;
+}
+
+#if INET6
+void
+udp_in6_cksum_stats(u_int32_t len)
+{
+ udpstat.udps_rcv6_swcsum++;
+ udpstat.udps_rcv6_swcsum_bytes += len;
+}
+
+void
+udp_out6_cksum_stats(u_int32_t len)
+{
+ udpstat.udps_snd6_swcsum++;
+ udpstat.udps_snd6_swcsum_bytes += len;
+}
+#endif /* INET6 */
+
+/*
+ * Checksum extended UDP header and data.
+ */
+static int
+udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
+{
+ struct ifnet *ifp = m->m_pkthdr.rcvif;
+ struct ip *ip = mtod(m, struct ip *);
+ struct ipovly *ipov = (struct ipovly *)ip;
+
+ if (uh->uh_sum == 0) {
+ udpstat.udps_nosum++;
+ return (0);
+ }
+
+ if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
+ (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
+ (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
+ if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
+ uh->uh_sum = m->m_pkthdr.csum_rx_val;
+ } else {
+ uint16_t sum = m->m_pkthdr.csum_rx_val;
+ uint16_t start = m->m_pkthdr.csum_rx_start;
+
+ /*
+ * Perform 1's complement adjustment of octets
+ * that got included/excluded in the hardware-
+ * calculated checksum value. Ignore cases
+ * where the value includes or excludes the
+ * IP header span, as the sum for those octets
+ * would already be 0xffff and thus no-op.
+ */
+ if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
+ start != 0 && (off - start) != off) {
+#if BYTE_ORDER != BIG_ENDIAN
+ if (start < off) {
+ HTONS(ip->ip_len);
+ HTONS(ip->ip_off);
+ }
+#endif /* BYTE_ORDER != BIG_ENDIAN */
+ /* callee folds in sum */
+ sum = m_adj_sum16(m, start, off, sum);
+#if BYTE_ORDER != BIG_ENDIAN
+ if (start < off) {
+ NTOHS(ip->ip_off);
+ NTOHS(ip->ip_len);
+ }
+#endif /* BYTE_ORDER != BIG_ENDIAN */
+ }
+
+ /* callee folds in sum */
+ uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
+ ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP));
+ }
+ uh->uh_sum ^= 0xffff;
+ } else {
+ uint16_t ip_sum;
+ char b[9];
+
+ bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1));
+ bzero(ipov->ih_x1, sizeof (ipov->ih_x1));
+ ip_sum = ipov->ih_len;
+ ipov->ih_len = uh->uh_ulen;
+ uh->uh_sum = in_cksum(m, ulen + sizeof (struct ip));
+ bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1));
+ ipov->ih_len = ip_sum;
+
+ udp_in_cksum_stats(ulen);
+ }
+
+ if (uh->uh_sum != 0) {
+ udpstat.udps_badsum++;
+ IF_UDP_STATINC(ifp, badchksum);
+ return (-1);
+ }
+
+ return (0);
+}
+
+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;
+ 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_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;