X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/6d2010ae8f7a6078e10b361c6962983bab233e0f..bb59bff194111743b33cc36712410b5656329d3c:/bsd/kern/uipc_mbuf2.c diff --git a/bsd/kern/uipc_mbuf2.c b/bsd/kern/uipc_mbuf2.c index 386238460..2d6f23f08 100644 --- a/bsd/kern/uipc_mbuf2.c +++ b/bsd/kern/uipc_mbuf2.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2010 Apple Inc. All rights reserved. + * Copyright (c) 2000-2013 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * @@ -106,8 +106,9 @@ #include #include #include -#if INET6 #include +#include +#if INET6 #include #include #endif /* INET6 */ @@ -399,13 +400,15 @@ m_tag_create(u_int32_t id, u_int16_t type, int len, int wait, struct mbuf *buf) VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN); struct mbuf *m = m_dtom(p); - struct m_taghdr *hdr = (struct m_taghdr *)m->m_data; + struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data; + VERIFY(IS_P2ALIGNED(hdr + 1, sizeof (u_int64_t))); VERIFY(m->m_flags & M_TAGHDR && !(m->m_flags & M_EXT)); /* The mbuf can store this m_tag */ if (M_TAG_ALIGN(len) <= MLEN - m->m_len) { - t = (struct m_tag *)(m->m_data + m->m_len); + t = (struct m_tag *)(void *)(m->m_data + m->m_len); + VERIFY(IS_P2ALIGNED(t, sizeof (u_int64_t))); hdr->refcnt++; m->m_len += M_TAG_ALIGN(len); VERIFY(m->m_len <= MLEN); @@ -445,14 +448,16 @@ m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait) m->m_flags |= M_TAGHDR; - hdr = (struct m_taghdr *)m->m_data; + hdr = (struct m_taghdr *)(void *)m->m_data; + VERIFY(IS_P2ALIGNED(hdr + 1, sizeof (u_int64_t))); hdr->refcnt = 1; m->m_len += sizeof (struct m_taghdr); - t = (struct m_tag *)(m->m_data + m->m_len); + t = (struct m_tag *)(void *)(m->m_data + m->m_len); + VERIFY(IS_P2ALIGNED(t, sizeof (u_int64_t))); m->m_len += M_TAG_ALIGN(len); VERIFY(m->m_len <= MLEN); } else if (len + sizeof (struct m_tag) <= MCLBYTES) { - t = (struct m_tag *)m_mclalloc(wait); + t = (struct m_tag *)(void *)m_mclalloc(wait); } else { t = NULL; } @@ -460,6 +465,7 @@ m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait) if (t == NULL) return (NULL); + VERIFY(IS_P2ALIGNED(t, sizeof (u_int64_t))); t->m_tag_cookie = M_TAG_VALID_PATTERN; t->m_tag_type = type; t->m_tag_len = len; @@ -480,19 +486,17 @@ m_tag_free(struct m_tag *t) t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) mac_mbuf_tag_destroy(t); #endif -#if INET6 - if (t != NULL && - t->m_tag_id == KERNEL_MODULE_TAG_ID && - t->m_tag_type == KERNEL_TAG_TYPE_INET6 && - t->m_tag_len == sizeof (struct ip6aux)) - ip6_destroyaux((struct ip6aux *)(t + 1)); -#endif /* INET6 */ if (t == NULL) return; + + VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN); + if (M_TAG_ALIGN(t->m_tag_len) + sizeof (struct m_taghdr) <= MLEN) { struct mbuf * m = m_dtom(t); VERIFY(m->m_flags & M_TAGHDR); - struct m_taghdr *hdr = (struct m_taghdr *)m->m_data; + struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data; + + VERIFY(IS_P2ALIGNED(hdr + 1, sizeof (u_int64_t))); /* No other tags in this mbuf */ if(--hdr->refcnt == 0) { @@ -525,8 +529,8 @@ m_tag_prepend(struct mbuf *m, struct m_tag *t) void m_tag_unlink(struct mbuf *m, struct m_tag *t) { - VERIFY(m != NULL && t != NULL); - VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN); + VERIFY(m->m_flags & M_PKTHDR); + VERIFY(t != NULL && t->m_tag_cookie == M_TAG_VALID_PATTERN); SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); } @@ -535,8 +539,6 @@ m_tag_unlink(struct mbuf *m, struct m_tag *t) void m_tag_delete(struct mbuf *m, struct m_tag *t) { - VERIFY(m != NULL && t != NULL); - m_tag_unlink(m, t); m_tag_free(t); } @@ -547,7 +549,7 @@ m_tag_delete_chain(struct mbuf *m, struct m_tag *t) { struct m_tag *p, *q; - VERIFY(m != NULL); + VERIFY(m->m_flags & M_PKTHDR); if (t != NULL) { p = t; @@ -571,7 +573,7 @@ m_tag_locate(struct mbuf *m, u_int32_t id, u_int16_t type, struct m_tag *t) { struct m_tag *p; - VERIFY(m != NULL); + VERIFY(m->m_flags & M_PKTHDR); if (t == NULL) { p = SLIST_FIRST(&m->m_pkthdr.tags); @@ -615,14 +617,6 @@ m_tag_copy(struct m_tag *t, int how) mac_mbuf_tag_copy(t, p); } else #endif -#if INET6 - if (t != NULL && - t->m_tag_id == KERNEL_MODULE_TAG_ID && - t->m_tag_type == KERNEL_TAG_TYPE_INET6 && - t->m_tag_len == sizeof (struct ip6aux)) { - ip6_copyaux((struct ip6aux *)(t + 1), (struct ip6aux *)(p + 1)); - } else -#endif /* INET6 */ bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */ return (p); } @@ -638,7 +632,7 @@ m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how) { struct m_tag *p, *t, *tprev = NULL; - VERIFY(to != NULL && from != NULL); + VERIFY((to->m_flags & M_PKTHDR) && (from->m_flags & M_PKTHDR)); m_tag_delete_chain(to, NULL); SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { @@ -658,23 +652,29 @@ m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how) return (1); } -/* Initialize tags on an mbuf. */ +/* Initialize dynamic and static tags on an mbuf. */ void -m_tag_init(struct mbuf *m) +m_tag_init(struct mbuf *m, int all) { - VERIFY(m != NULL); + VERIFY(m->m_flags & M_PKTHDR); SLIST_INIT(&m->m_pkthdr.tags); -#if PF_PKTHDR - bzero(&m->m_pkthdr.pf_mtag, sizeof (m->m_pkthdr.pf_mtag)); -#endif + /* + * If the caller wants to preserve static mbuf tags + * (e.g. m_dup_pkthdr), don't zero them out. + */ + if (all) { + bzero(&m->m_pkthdr.pf_mtag, sizeof (m->m_pkthdr.pf_mtag)); + bzero(&m->m_pkthdr.proto_mtag, sizeof (m->m_pkthdr.proto_mtag)); + bzero(&m->m_pkthdr.necp_mtag, sizeof (m->m_pkthdr.necp_mtag)); + } } /* Get first tag in chain. */ struct m_tag * m_tag_first(struct mbuf *m) { - VERIFY(m != NULL); + VERIFY(m->m_flags & M_PKTHDR); return (SLIST_FIRST(&m->m_pkthdr.tags)); } @@ -690,9 +690,178 @@ m_tag_next(struct mbuf *m, struct m_tag *t) return (SLIST_NEXT(t, m_tag_link)); } -void -m_prio_init(struct mbuf *m) +int +m_set_traffic_class(struct mbuf *m, mbuf_traffic_class_t tc) +{ + u_int32_t val = MBUF_TC2SCVAL(tc); /* just the val portion */ + + return (m_set_service_class(m, m_service_class_from_val(val))); +} + +mbuf_traffic_class_t +m_get_traffic_class(struct mbuf *m) { - if (m->m_flags & M_PKTHDR) - m->m_pkthdr.prio = MBUF_TC_BE; + return (MBUF_SC2TC(m_get_service_class(m))); +} + +int +m_set_service_class(struct mbuf *m, mbuf_svc_class_t sc) +{ + int error = 0; + + VERIFY(m->m_flags & M_PKTHDR); + + if (MBUF_VALID_SC(sc)) + m->m_pkthdr.pkt_svc = sc; + else + error = EINVAL; + + return (error); +} + +mbuf_svc_class_t +m_get_service_class(struct mbuf *m) +{ + mbuf_svc_class_t sc; + + VERIFY(m->m_flags & M_PKTHDR); + + if (MBUF_VALID_SC(m->m_pkthdr.pkt_svc)) + sc = m->m_pkthdr.pkt_svc; + else + sc = MBUF_SC_BE; + + return (sc); +} + +mbuf_svc_class_t +m_service_class_from_idx(u_int32_t i) +{ + mbuf_svc_class_t sc = MBUF_SC_BE; + + switch (i) { + case SCIDX_BK_SYS: + return (MBUF_SC_BK_SYS); + + case SCIDX_BK: + return (MBUF_SC_BK); + + case SCIDX_BE: + return (MBUF_SC_BE); + + case SCIDX_RD: + return (MBUF_SC_RD); + + case SCIDX_OAM: + return (MBUF_SC_OAM); + + case SCIDX_AV: + return (MBUF_SC_AV); + + case SCIDX_RV: + return (MBUF_SC_RV); + + case SCIDX_VI: + return (MBUF_SC_VI); + + case SCIDX_VO: + return (MBUF_SC_VO); + + case SCIDX_CTL: + return (MBUF_SC_CTL); + + default: + break; + } + + VERIFY(0); + /* NOTREACHED */ + return (sc); +} + +mbuf_svc_class_t +m_service_class_from_val(u_int32_t v) +{ + mbuf_svc_class_t sc = MBUF_SC_BE; + + switch (v) { + case SCVAL_BK_SYS: + return (MBUF_SC_BK_SYS); + + case SCVAL_BK: + return (MBUF_SC_BK); + + case SCVAL_BE: + return (MBUF_SC_BE); + + case SCVAL_RD: + return (MBUF_SC_RD); + + case SCVAL_OAM: + return (MBUF_SC_OAM); + + case SCVAL_AV: + return (MBUF_SC_AV); + + case SCVAL_RV: + return (MBUF_SC_RV); + + case SCVAL_VI: + return (MBUF_SC_VI); + + case SCVAL_VO: + return (MBUF_SC_VO); + + case SCVAL_CTL: + return (MBUF_SC_CTL); + + default: + break; + } + + VERIFY(0); + /* NOTREACHED */ + return (sc); +} + +uint16_t +m_adj_sum16(struct mbuf *m, uint32_t start, uint32_t ulpoff, uint32_t sum) +{ + int len = (ulpoff - start); + + if (len > 0) { + uint32_t adj = m_sum16(m, start, len); + if (adj >= sum) + sum = ~(adj - sum) & 0xffff; + else + sum -= adj; + } else if (len < 0) { + sum += m_sum16(m, ulpoff, -len); + } + + ADDCARRY(sum); + + return (sum); +} + +extern int cpu_in_cksum(struct mbuf *m, int len, int off, uint32_t initial_sum); + +uint16_t +m_sum16(struct mbuf *m, uint32_t off, uint32_t len) +{ + int mlen; + + /* + * Sanity check + * + * Use m_length2() instead of m_length(), as we cannot rely on + * the caller setting m_pkthdr.len correctly, if the mbuf is + * a M_PKTHDR one. + */ + if ((mlen = m_length2(m, NULL)) < (off + len)) { + panic("%s: mbuf len (%d) < off+len (%d+%d)\n", __func__, + mlen, off, len); + } + + return (~cpu_in_cksum(m, len, off, 0) & 0xffff); }