Home | History | Annotate | Line # | Download | only in npf
npf_mbuf.c revision 1.6.8.1
      1  1.6.8.1   yamt /*	$NetBSD: npf_mbuf.c,v 1.6.8.1 2012/04/17 00:08:39 yamt Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*-
      4      1.6  rmind  * Copyright (c) 2009-2011 The NetBSD Foundation, Inc.
      5      1.1  rmind  * All rights reserved.
      6      1.1  rmind  *
      7      1.1  rmind  * This material is based upon work partially supported by The
      8      1.1  rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9      1.1  rmind  *
     10      1.1  rmind  * Redistribution and use in source and binary forms, with or without
     11      1.1  rmind  * modification, are permitted provided that the following conditions
     12      1.1  rmind  * are met:
     13      1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     14      1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     15      1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  rmind  *    documentation and/or other materials provided with the distribution.
     18      1.1  rmind  *
     19      1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  rmind  */
     31      1.1  rmind 
     32      1.1  rmind /*
     33      1.1  rmind  * NPF network buffer management interface.
     34      1.1  rmind  *
     35      1.1  rmind  * Network buffer in NetBSD is mbuf.  Internal mbuf structures are
     36      1.1  rmind  * abstracted within this source.
     37      1.1  rmind  */
     38      1.1  rmind 
     39      1.1  rmind #include <sys/cdefs.h>
     40  1.6.8.1   yamt __KERNEL_RCSID(0, "$NetBSD: npf_mbuf.c,v 1.6.8.1 2012/04/17 00:08:39 yamt Exp $");
     41      1.1  rmind 
     42      1.1  rmind #include <sys/param.h>
     43      1.1  rmind #include <sys/mbuf.h>
     44      1.1  rmind 
     45      1.1  rmind #include "npf_impl.h"
     46      1.1  rmind 
     47      1.1  rmind /*
     48      1.1  rmind  * nbuf_dataptr: return a pointer to data in nbuf.
     49      1.1  rmind  */
     50      1.1  rmind void *
     51      1.1  rmind nbuf_dataptr(nbuf_t *nbuf)
     52      1.1  rmind {
     53      1.1  rmind 	const struct mbuf *m = nbuf;
     54      1.1  rmind 
     55      1.1  rmind 	return mtod(m, void *);
     56      1.1  rmind }
     57      1.1  rmind 
     58      1.1  rmind /*
     59      1.1  rmind  * nbuf_advance: advance in mbuf or chain by specified amount of bytes.
     60      1.1  rmind  *
     61      1.1  rmind  * => Returns new pointer to data in mbuf and NULL if offset gets invalid.
     62      1.1  rmind  * => Sets nbuf to current (after advance) mbuf in the chain.
     63      1.1  rmind  */
     64      1.1  rmind void *
     65      1.1  rmind nbuf_advance(nbuf_t **nbuf, void *n_ptr, u_int n)
     66      1.1  rmind {
     67      1.1  rmind 	struct mbuf *m = *nbuf;
     68      1.1  rmind 	u_int off, wmark;
     69      1.1  rmind 	uint8_t *d;
     70      1.1  rmind 
     71      1.1  rmind 	/* Offset with amount to advance. */
     72      1.1  rmind 	off = (uintptr_t)n_ptr - mtod(m, uintptr_t) + n;
     73      1.1  rmind 	wmark = m->m_len;
     74      1.1  rmind 
     75      1.1  rmind 	/* Find the mbuf according to offset. */
     76      1.1  rmind 	while (__predict_false(wmark <= off)) {
     77      1.1  rmind 		m = m->m_next;
     78      1.1  rmind 		if (__predict_false(m == NULL)) {
     79      1.1  rmind 			/*
     80      1.1  rmind 			 * If out of chain, then offset is
     81      1.1  rmind 			 * higher than packet length.
     82      1.1  rmind 			 */
     83      1.1  rmind 			return NULL;
     84      1.1  rmind 		}
     85      1.1  rmind 		wmark += m->m_len;
     86      1.1  rmind 	}
     87      1.1  rmind 
     88      1.1  rmind 	/* Offset in mbuf data. */
     89      1.1  rmind 	d = mtod(m, uint8_t *);
     90      1.1  rmind 	KASSERT(off >= (wmark - m->m_len));
     91      1.1  rmind 	d += (off - (wmark - m->m_len));
     92      1.1  rmind 
     93      1.1  rmind 	*nbuf = (void *)m;
     94      1.1  rmind 	return d;
     95      1.1  rmind }
     96      1.1  rmind 
     97      1.1  rmind /*
     98      1.1  rmind  * nbuf_rw_datum: read or write a datum of specified length at current
     99      1.1  rmind  * offset in the nbuf chain and copy datum into passed buffer.
    100      1.1  rmind  *
    101      1.1  rmind  * => Datum is allowed to overlap between two or more mbufs.
    102      1.1  rmind  * => Note: all data in nbuf is in network byte order.
    103      1.1  rmind  * => Returns 0 on success, error code on failure.
    104      1.1  rmind  *
    105      1.1  rmind  * Note: this function must be static inline with constant operation
    106      1.1  rmind  * parameter - we expect constant propagation.
    107      1.1  rmind  */
    108      1.1  rmind 
    109      1.1  rmind #define	NBUF_DATA_READ		0
    110      1.1  rmind #define	NBUF_DATA_WRITE		1
    111      1.1  rmind 
    112      1.1  rmind static inline int
    113      1.6  rmind nbuf_rw_datum(const int wr, struct mbuf *m, void *n_ptr, size_t len, void *buf)
    114      1.1  rmind {
    115      1.1  rmind 	uint8_t *d = n_ptr, *b = buf;
    116      1.1  rmind 	u_int off, wmark, end;
    117      1.1  rmind 
    118      1.1  rmind 	/* Current offset in mbuf. */
    119      1.1  rmind 	off = (uintptr_t)n_ptr - mtod(m, uintptr_t);
    120  1.6.8.1   yamt 	KASSERT(off < (u_int)m->m_len);
    121      1.1  rmind 	wmark = m->m_len;
    122      1.1  rmind 
    123      1.1  rmind 	/* Is datum overlapping? */
    124      1.1  rmind 	end = off + len;
    125      1.1  rmind 	while (__predict_false(end > wmark)) {
    126      1.1  rmind 		u_int l;
    127      1.1  rmind 
    128      1.1  rmind 		/* Get the part of current mbuf. */
    129      1.1  rmind 		l = m->m_len - off;
    130      1.1  rmind 		KASSERT(l < len);
    131      1.1  rmind 		len -= l;
    132      1.2  rmind 		if (wr == NBUF_DATA_WRITE) {
    133      1.1  rmind 			while (l--)
    134      1.1  rmind 				*d++ = *b++;
    135      1.1  rmind 		} else {
    136      1.2  rmind 			KASSERT(wr == NBUF_DATA_READ);
    137      1.1  rmind 			while (l--)
    138      1.1  rmind 				*b++ = *d++;
    139      1.1  rmind 		}
    140      1.1  rmind 		KASSERT(len > 0);
    141      1.1  rmind 
    142      1.1  rmind 		/* Take next mbuf and continue. */
    143      1.1  rmind 		m = m->m_next;
    144      1.1  rmind 		if (__predict_false(m == NULL)) {
    145      1.1  rmind 			/*
    146      1.1  rmind 			 * If out of chain, then offset with datum
    147      1.1  rmind 			 * length exceed the packet length.
    148      1.1  rmind 			 */
    149      1.1  rmind 			return EINVAL;
    150      1.1  rmind 		}
    151      1.1  rmind 		wmark += m->m_len;
    152      1.1  rmind 		d = mtod(m, uint8_t *);
    153      1.1  rmind 		off = 0;
    154      1.1  rmind 	}
    155      1.1  rmind 	KASSERT(n_ptr == d || mtod(m, uint8_t *) == d);
    156  1.6.8.1   yamt 	KASSERT(len <= (u_int)m->m_len);
    157      1.1  rmind 
    158      1.1  rmind 	/* Non-overlapping case: fetch the actual data. */
    159      1.2  rmind 	if (wr == NBUF_DATA_WRITE) {
    160      1.1  rmind 		while (len--)
    161      1.1  rmind 			*d++ = *b++;
    162      1.1  rmind 	} else {
    163      1.2  rmind 		KASSERT(wr == NBUF_DATA_READ);
    164      1.1  rmind 		while (len--)
    165      1.1  rmind 			*b++ = *d++;
    166      1.1  rmind 	}
    167      1.1  rmind 	return 0;
    168      1.1  rmind }
    169      1.1  rmind 
    170      1.1  rmind /*
    171      1.1  rmind  * nbuf_{fetch|store}_datum: read/write absraction calls on nbuf_rw_datum().
    172      1.1  rmind  */
    173      1.1  rmind int
    174      1.1  rmind nbuf_fetch_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
    175      1.1  rmind {
    176      1.6  rmind 	struct mbuf *m = nbuf;
    177      1.1  rmind 
    178      1.6  rmind 	return nbuf_rw_datum(NBUF_DATA_READ, m, n_ptr, len, buf);
    179      1.1  rmind }
    180      1.1  rmind 
    181      1.1  rmind int
    182      1.1  rmind nbuf_store_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
    183      1.1  rmind {
    184      1.6  rmind 	struct mbuf *m = nbuf;
    185      1.1  rmind 
    186      1.6  rmind 	KASSERT((m->m_flags & M_PKTHDR) != 0 || !M_READONLY(m));
    187      1.6  rmind 	return nbuf_rw_datum(NBUF_DATA_WRITE, m, n_ptr, len, buf);
    188      1.1  rmind }
    189      1.1  rmind 
    190      1.1  rmind /*
    191      1.3  rmind  * nbuf_advfetch: advance and fetch the datum.
    192      1.3  rmind  */
    193      1.3  rmind int
    194      1.3  rmind nbuf_advfetch(nbuf_t **nbuf, void **n_ptr, u_int n, size_t len, void *buf)
    195      1.3  rmind {
    196      1.4  rmind 	nbuf_t *orig_nbuf = *nbuf;
    197      1.4  rmind 	void *orig_nptr = *n_ptr;
    198      1.4  rmind 	int error;
    199      1.3  rmind 
    200      1.3  rmind 	*n_ptr = nbuf_advance(nbuf, *n_ptr, n);
    201      1.4  rmind 	if (__predict_false(*n_ptr != NULL)) {
    202      1.4  rmind 		error = nbuf_fetch_datum(*nbuf, *n_ptr, len, buf);
    203      1.4  rmind 	} else {
    204      1.4  rmind 		error = EINVAL;
    205      1.4  rmind 	}
    206      1.4  rmind 	if (__predict_false(error)) {
    207      1.4  rmind 		*nbuf = orig_nbuf;
    208      1.4  rmind 		*n_ptr = orig_nptr;
    209      1.3  rmind 	}
    210      1.4  rmind 	return error;
    211      1.3  rmind }
    212      1.3  rmind 
    213      1.3  rmind /*
    214      1.5  rmind  * nbuf_advstore: advance and store the datum.
    215      1.5  rmind  */
    216      1.5  rmind int
    217      1.5  rmind nbuf_advstore(nbuf_t **nbuf, void **n_ptr, u_int n, size_t len, void *buf)
    218      1.5  rmind {
    219      1.5  rmind 	nbuf_t *orig_nbuf = *nbuf;
    220      1.5  rmind 	void *orig_nptr = *n_ptr;
    221      1.5  rmind 	int error;
    222      1.5  rmind 
    223      1.5  rmind 	*n_ptr = nbuf_advance(nbuf, *n_ptr, n);
    224      1.5  rmind 	if (__predict_false(*n_ptr != NULL)) {
    225      1.5  rmind 		error = nbuf_store_datum(*nbuf, *n_ptr, len, buf);
    226      1.5  rmind 	} else {
    227      1.5  rmind 		error = EINVAL;
    228      1.5  rmind 	}
    229      1.5  rmind 	if (__predict_false(error)) {
    230      1.5  rmind 		*nbuf = orig_nbuf;
    231      1.5  rmind 		*n_ptr = orig_nptr;
    232      1.5  rmind 	}
    233      1.5  rmind 	return error;
    234      1.5  rmind }
    235      1.5  rmind 
    236      1.5  rmind /*
    237      1.1  rmind  * nbuf_add_tag: add a tag to specified network buffer.
    238      1.1  rmind  *
    239      1.1  rmind  * => Returns 0 on success, or errno on failure.
    240      1.1  rmind  */
    241      1.1  rmind int
    242      1.1  rmind nbuf_add_tag(nbuf_t *nbuf, uint32_t key, uint32_t val)
    243      1.1  rmind {
    244      1.1  rmind 	struct mbuf *m = nbuf;
    245      1.1  rmind 	struct m_tag *mt;
    246      1.1  rmind 	uint32_t *dat;
    247      1.1  rmind 
    248      1.1  rmind 	mt = m_tag_get(PACKET_TAG_NPF, sizeof(uint32_t), M_NOWAIT);
    249      1.1  rmind 	if (__predict_false(mt == NULL)) {
    250      1.1  rmind 		return ENOMEM;
    251      1.1  rmind 	}
    252      1.1  rmind 	dat = (uint32_t *)(mt + 1);
    253      1.1  rmind 	*dat = val;
    254      1.1  rmind 	m_tag_prepend(m, mt);
    255      1.1  rmind 	return 0;
    256      1.1  rmind }
    257      1.1  rmind 
    258      1.1  rmind /*
    259      1.1  rmind  * nbuf_find_tag: find a tag in specified network buffer.
    260      1.1  rmind  *
    261      1.1  rmind  * => Returns 0 on success, or errno on failure.
    262      1.1  rmind  */
    263      1.1  rmind int
    264      1.1  rmind nbuf_find_tag(nbuf_t *nbuf, uint32_t key, void **data)
    265      1.1  rmind {
    266      1.1  rmind 	struct mbuf *m = nbuf;
    267      1.1  rmind 	struct m_tag *mt;
    268      1.1  rmind 
    269      1.1  rmind 	mt = m_tag_find(m, PACKET_TAG_NPF, NULL);
    270      1.1  rmind 	if (__predict_false(mt == NULL)) {
    271      1.1  rmind 		return EINVAL;
    272      1.1  rmind 	}
    273      1.1  rmind 	*data = (void *)(mt + 1);
    274      1.1  rmind 	return 0;
    275      1.1  rmind }
    276