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