* Rearange an mbuf chain so that len bytes are contiguous * and in the data area of an mbuf (so that mtod will work * for a structure of size len). Returns the resulting * mbuf chain on success, frees it and returns null on failure. * If there is room, it will add up to max_protohdr-len extra bytes to the * contiguous region in an attempt to avoid being called next time. */
| 864 | * contiguous region in an attempt to avoid being called next time. |
| 865 | */ |
| 866 | struct mbuf * |
| 867 | m_pullup(struct mbuf *n, int len) |
| 868 | { |
| 869 | struct mbuf *m; |
| 870 | int count; |
| 871 | int space; |
| 872 | |
| 873 | KASSERT((n->m_flags & M_EXTPG) == 0, |
| 874 | ("%s: unmapped mbuf %p", __func__, n)); |
| 875 | |
| 876 | /* |
| 877 | * If first mbuf has no cluster, and has room for len bytes |
| 878 | * without shifting current data, pullup into it, |
| 879 | * otherwise allocate a new mbuf to prepend to the chain. |
| 880 | */ |
| 881 | if ((n->m_flags & M_EXT) == 0 && |
| 882 | n->m_data + len < &n->m_dat[MLEN] && n->m_next) { |
| 883 | if (n->m_len >= len) |
| 884 | return (n); |
| 885 | m = n; |
| 886 | n = n->m_next; |
| 887 | len -= m->m_len; |
| 888 | } else { |
| 889 | if (len > MHLEN) |
| 890 | goto bad; |
| 891 | m = m_get(M_NOWAIT, n->m_type); |
| 892 | if (m == NULL) |
| 893 | goto bad; |
| 894 | if (n->m_flags & M_PKTHDR) |
| 895 | m_move_pkthdr(m, n); |
| 896 | } |
| 897 | space = &m->m_dat[MLEN] - (m->m_data + m->m_len); |
| 898 | do { |
| 899 | count = min(min(max(len, max_protohdr), space), n->m_len); |
| 900 | bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, |
| 901 | (u_int)count); |
| 902 | len -= count; |
| 903 | m->m_len += count; |
| 904 | n->m_len -= count; |
| 905 | space -= count; |
| 906 | if (n->m_len) |
| 907 | n->m_data += count; |
| 908 | else |
| 909 | n = m_free(n); |
| 910 | } while (len > 0 && n); |
| 911 | if (len > 0) { |
| 912 | (void) m_free(m); |
| 913 | goto bad; |
| 914 | } |
| 915 | m->m_next = n; |
| 916 | return (m); |
| 917 | bad: |
| 918 | m_freem(n); |
| 919 | return (NULL); |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Like m_pullup(), except a new mbuf is always allocated, and we allow |
no test coverage detected