xref: /freebsd/usr.sbin/bluetooth/btpand/packet.c (revision 8d6f425ddd8021ae2257ba9682f8844254ecdde1)
17718ced0SMaksim Yevmenkin /*	$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
27718ced0SMaksim Yevmenkin 
37718ced0SMaksim Yevmenkin /*-
47718ced0SMaksim Yevmenkin  * Copyright (c) 2008 Iain Hibbert
57718ced0SMaksim Yevmenkin  * All rights reserved.
67718ced0SMaksim Yevmenkin  *
77718ced0SMaksim Yevmenkin  * Redistribution and use in source and binary forms, with or without
87718ced0SMaksim Yevmenkin  * modification, are permitted provided that the following conditions
97718ced0SMaksim Yevmenkin  * are met:
107718ced0SMaksim Yevmenkin  * 1. Redistributions of source code must retain the above copyright
117718ced0SMaksim Yevmenkin  *    notice, this list of conditions and the following disclaimer.
127718ced0SMaksim Yevmenkin  * 2. Redistributions in binary form must reproduce the above copyright
137718ced0SMaksim Yevmenkin  *    notice, this list of conditions and the following disclaimer in the
147718ced0SMaksim Yevmenkin  *    documentation and/or other materials provided with the distribution.
157718ced0SMaksim Yevmenkin  *
167718ced0SMaksim Yevmenkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177718ced0SMaksim Yevmenkin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187718ced0SMaksim Yevmenkin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197718ced0SMaksim Yevmenkin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
207718ced0SMaksim Yevmenkin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
217718ced0SMaksim Yevmenkin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227718ced0SMaksim Yevmenkin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
237718ced0SMaksim Yevmenkin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247718ced0SMaksim Yevmenkin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257718ced0SMaksim Yevmenkin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267718ced0SMaksim Yevmenkin  */
277718ced0SMaksim Yevmenkin 
287718ced0SMaksim Yevmenkin /* $FreeBSD$ */
297718ced0SMaksim Yevmenkin 
307718ced0SMaksim Yevmenkin #include <sys/cdefs.h>
317718ced0SMaksim Yevmenkin __RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
327718ced0SMaksim Yevmenkin 
33*8d6f425dSTakanori Watanabe #define L2CAP_SOCKET_CHECKED
347718ced0SMaksim Yevmenkin #include "btpand.h"
357718ced0SMaksim Yevmenkin 
367718ced0SMaksim Yevmenkin packet_t *
377718ced0SMaksim Yevmenkin packet_alloc(channel_t *chan)
387718ced0SMaksim Yevmenkin {
397718ced0SMaksim Yevmenkin 	packet_t *pkt;
407718ced0SMaksim Yevmenkin 
417718ced0SMaksim Yevmenkin 	pkt = malloc(sizeof(packet_t) + chan->mru);
427718ced0SMaksim Yevmenkin 	if (pkt == NULL) {
437718ced0SMaksim Yevmenkin 		log_err("%s() failed: %m", __func__);
447718ced0SMaksim Yevmenkin 		return NULL;
457718ced0SMaksim Yevmenkin 	}
467718ced0SMaksim Yevmenkin 
477718ced0SMaksim Yevmenkin 	memset(pkt, 0, sizeof(packet_t));
487718ced0SMaksim Yevmenkin 	STAILQ_INIT(&pkt->extlist);
497718ced0SMaksim Yevmenkin 	pkt->ptr = pkt->buf;
507718ced0SMaksim Yevmenkin 
517718ced0SMaksim Yevmenkin 	pkt->chan = chan;
527718ced0SMaksim Yevmenkin 	chan->refcnt++;
537718ced0SMaksim Yevmenkin 
547718ced0SMaksim Yevmenkin 	return pkt;
557718ced0SMaksim Yevmenkin }
567718ced0SMaksim Yevmenkin 
577718ced0SMaksim Yevmenkin void
587718ced0SMaksim Yevmenkin packet_free(packet_t *pkt)
597718ced0SMaksim Yevmenkin {
607718ced0SMaksim Yevmenkin 	exthdr_t *eh;
617718ced0SMaksim Yevmenkin 
627718ced0SMaksim Yevmenkin 	if (pkt->refcnt-- > 0)
637718ced0SMaksim Yevmenkin 		return;
647718ced0SMaksim Yevmenkin 
657718ced0SMaksim Yevmenkin 	while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) {
667718ced0SMaksim Yevmenkin 		STAILQ_REMOVE_HEAD(&pkt->extlist, next);
677718ced0SMaksim Yevmenkin 		free(eh);
687718ced0SMaksim Yevmenkin 	}
697718ced0SMaksim Yevmenkin 
707718ced0SMaksim Yevmenkin 	pkt->chan->refcnt--;
717718ced0SMaksim Yevmenkin 	if (pkt->chan->refcnt == 0)
727718ced0SMaksim Yevmenkin 		channel_free(pkt->chan);
737718ced0SMaksim Yevmenkin 
747718ced0SMaksim Yevmenkin 	free(pkt);
757718ced0SMaksim Yevmenkin }
767718ced0SMaksim Yevmenkin 
777718ced0SMaksim Yevmenkin void
787718ced0SMaksim Yevmenkin packet_adj(packet_t *pkt, size_t size)
797718ced0SMaksim Yevmenkin {
807718ced0SMaksim Yevmenkin 
817718ced0SMaksim Yevmenkin 	assert(pkt->refcnt == 0);
827718ced0SMaksim Yevmenkin 	assert(pkt->len >= size);
837718ced0SMaksim Yevmenkin 
847718ced0SMaksim Yevmenkin 	pkt->ptr += size;
857718ced0SMaksim Yevmenkin 	pkt->len -= size;
867718ced0SMaksim Yevmenkin }
877718ced0SMaksim Yevmenkin 
887718ced0SMaksim Yevmenkin pkthdr_t *
897718ced0SMaksim Yevmenkin pkthdr_alloc(packet_t *pkt)
907718ced0SMaksim Yevmenkin {
917718ced0SMaksim Yevmenkin 	pkthdr_t *ph;
927718ced0SMaksim Yevmenkin 
937718ced0SMaksim Yevmenkin 	ph = malloc(sizeof(pkthdr_t));
947718ced0SMaksim Yevmenkin 	if (ph == NULL) {
957718ced0SMaksim Yevmenkin 		log_err("%s() failed: %m", __func__);
967718ced0SMaksim Yevmenkin 		return NULL;
977718ced0SMaksim Yevmenkin 	}
987718ced0SMaksim Yevmenkin 
997718ced0SMaksim Yevmenkin 	ph->data = pkt;
1007718ced0SMaksim Yevmenkin 	pkt->refcnt++;
1017718ced0SMaksim Yevmenkin 
1027718ced0SMaksim Yevmenkin 	return ph;
1037718ced0SMaksim Yevmenkin }
1047718ced0SMaksim Yevmenkin 
1057718ced0SMaksim Yevmenkin void
1067718ced0SMaksim Yevmenkin pkthdr_free(pkthdr_t *ph)
1077718ced0SMaksim Yevmenkin {
1087718ced0SMaksim Yevmenkin 
1097718ced0SMaksim Yevmenkin 	packet_free(ph->data);
1107718ced0SMaksim Yevmenkin 	free(ph);
1117718ced0SMaksim Yevmenkin }
112