+ VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
+ while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) {
+ VERIFY(q->m_tag_cookie == M_TAG_VALID_PATTERN);
+ m_tag_delete(m, q);
+ }
+ 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->m_flags & M_PKTHDR);
+
+ 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
+ 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->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) {
+ 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 dynamic and static tags on an mbuf. */
+void
+m_tag_init(struct mbuf *m, int all)
+{
+ VERIFY(m->m_flags & M_PKTHDR);
+
+ SLIST_INIT(&m->m_pkthdr.tags);
+ /*
+ * 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));