1187938Semax/* $NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */ 2187938Semax 3187938Semax/*- 4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5330449Seadler * 6187938Semax * Copyright (c) 2008 Iain Hibbert 7187938Semax * All rights reserved. 8187938Semax * 9187938Semax * Redistribution and use in source and binary forms, with or without 10187938Semax * modification, are permitted provided that the following conditions 11187938Semax * are met: 12187938Semax * 1. Redistributions of source code must retain the above copyright 13187938Semax * notice, this list of conditions and the following disclaimer. 14187938Semax * 2. Redistributions in binary form must reproduce the above copyright 15187938Semax * notice, this list of conditions and the following disclaimer in the 16187938Semax * documentation and/or other materials provided with the distribution. 17187938Semax * 18187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28187938Semax */ 29187938Semax 30187938Semax/* $FreeBSD: stable/11/usr.sbin/bluetooth/btpand/packet.c 330449 2018-03-05 07:26:05Z eadler $ */ 31187938Semax 32187938Semax#include <sys/cdefs.h> 33187938Semax__RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 34187938Semax 35281210Stakawata#define L2CAP_SOCKET_CHECKED 36187938Semax#include "btpand.h" 37187938Semax 38187938Semaxpacket_t * 39187938Semaxpacket_alloc(channel_t *chan) 40187938Semax{ 41187938Semax packet_t *pkt; 42187938Semax 43187938Semax pkt = malloc(sizeof(packet_t) + chan->mru); 44187938Semax if (pkt == NULL) { 45187938Semax log_err("%s() failed: %m", __func__); 46187938Semax return NULL; 47187938Semax } 48187938Semax 49187938Semax memset(pkt, 0, sizeof(packet_t)); 50187938Semax STAILQ_INIT(&pkt->extlist); 51187938Semax pkt->ptr = pkt->buf; 52187938Semax 53187938Semax pkt->chan = chan; 54187938Semax chan->refcnt++; 55187938Semax 56187938Semax return pkt; 57187938Semax} 58187938Semax 59187938Semaxvoid 60187938Semaxpacket_free(packet_t *pkt) 61187938Semax{ 62187938Semax exthdr_t *eh; 63187938Semax 64187938Semax if (pkt->refcnt-- > 0) 65187938Semax return; 66187938Semax 67187938Semax while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) { 68187938Semax STAILQ_REMOVE_HEAD(&pkt->extlist, next); 69187938Semax free(eh); 70187938Semax } 71187938Semax 72187938Semax pkt->chan->refcnt--; 73187938Semax if (pkt->chan->refcnt == 0) 74187938Semax channel_free(pkt->chan); 75187938Semax 76187938Semax free(pkt); 77187938Semax} 78187938Semax 79187938Semaxvoid 80187938Semaxpacket_adj(packet_t *pkt, size_t size) 81187938Semax{ 82187938Semax 83187938Semax assert(pkt->refcnt == 0); 84187938Semax assert(pkt->len >= size); 85187938Semax 86187938Semax pkt->ptr += size; 87187938Semax pkt->len -= size; 88187938Semax} 89187938Semax 90187938Semaxpkthdr_t * 91187938Semaxpkthdr_alloc(packet_t *pkt) 92187938Semax{ 93187938Semax pkthdr_t *ph; 94187938Semax 95187938Semax ph = malloc(sizeof(pkthdr_t)); 96187938Semax if (ph == NULL) { 97187938Semax log_err("%s() failed: %m", __func__); 98187938Semax return NULL; 99187938Semax } 100187938Semax 101187938Semax ph->data = pkt; 102187938Semax pkt->refcnt++; 103187938Semax 104187938Semax return ph; 105187938Semax} 106187938Semax 107187938Semaxvoid 108187938Semaxpkthdr_free(pkthdr_t *ph) 109187938Semax{ 110187938Semax 111187938Semax packet_free(ph->data); 112187938Semax free(ph); 113187938Semax} 114