+ m_tag_delete(m, p);
+}
+
+/* Find a tag, starting from a given position. */
+struct m_tag *
+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);
+
+ if (t == NULL) {
+ p = SLIST_FIRST(&m->m_pkthdr.tags);
+ } else {
+ VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
+ p = SLIST_NEXT(t, m_tag_link);
+ }
+ while (p != NULL) {
+ VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
+ if (p->m_tag_id == id && p->m_tag_type == type)
+ return (p);
+ p = SLIST_NEXT(p, m_tag_link);
+ }
+ return (NULL);
+}
+
+/* Copy a single tag. */
+struct m_tag *
+m_tag_copy(struct m_tag *t, int how)
+{
+ struct m_tag *p;
+
+ VERIFY(t != NULL);
+
+ p = m_tag_alloc(t->m_tag_id, t->m_tag_type, t->m_tag_len, how);
+ if (p == NULL)
+ return (NULL);
+#if CONFIG_MACF_NET
+ /*
+ * XXXMAC: we should probably pass off the initialization, and
+ * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
+ * special from the mbuf code?
+ */
+ if (t != NULL &&
+ t->m_tag_id == KERNEL_MODULE_TAG_ID &&
+ t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) {
+ if (mac_mbuf_tag_init(p, how) != 0) {
+ m_tag_free(p);
+ return (NULL);
+ }
+ 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);
+}
+
+/*
+ * Copy two tag chains. The destination mbuf (to) loses any attached
+ * tags even if the operation fails. This should not be a problem, as
+ * m_tag_copy_chain() is typically called with a newly-allocated
+ * destination mbuf.
+ */
+int
+m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
+{
+ struct m_tag *p, *t, *tprev = NULL;
+
+ VERIFY(to != NULL && from != NULL);
+
+ m_tag_delete_chain(to, NULL);
+ SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
+ VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
+ t = m_tag_copy(p, how);
+ if (t == NULL) {
+ m_tag_delete_chain(to, NULL);
+ return (0);
+ }
+ if (tprev == NULL) {
+ SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
+ } else {
+ SLIST_INSERT_AFTER(tprev, t, m_tag_link);
+ tprev = t;
+ }
+ }
+ return (1);
+}
+
+/* Initialize tags on an mbuf. */
+void
+m_tag_init(struct mbuf *m)
+{
+ VERIFY(m != NULL);
+
+ SLIST_INIT(&m->m_pkthdr.tags);
+#if PF_PKTHDR
+ bzero(&m->m_pkthdr.pf_mtag, sizeof (m->m_pkthdr.pf_mtag));
+#endif
+}
+
+/* Get first tag in chain. */
+struct m_tag *
+m_tag_first(struct mbuf *m)
+{
+ VERIFY(m != NULL);
+
+ return (SLIST_FIRST(&m->m_pkthdr.tags));
+}
+
+/* Get next tag in chain. */
+struct m_tag *
+m_tag_next(struct mbuf *m, struct m_tag *t)
+{
+#pragma unused(m)
+ VERIFY(t != NULL);
+ VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
+
+ return (SLIST_NEXT(t, m_tag_link));
+}
+
+void
+m_prio_init(struct mbuf *m)
+{
+ if (m->m_flags & M_PKTHDR)
+ m->m_pkthdr.prio = MBUF_TC_BE;