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 /* $FreeBSD$ */ 317718ced0SMaksim Yevmenkin 327718ced0SMaksim Yevmenkin #include <sys/cdefs.h> 337718ced0SMaksim Yevmenkin __RCSID("$NetBSD: packet.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 347718ced0SMaksim Yevmenkin 358d6f425dSTakanori Watanabe #define L2CAP_SOCKET_CHECKED 367718ced0SMaksim Yevmenkin #include "btpand.h" 377718ced0SMaksim Yevmenkin 387718ced0SMaksim Yevmenkin packet_t * 397718ced0SMaksim Yevmenkin packet_alloc(channel_t *chan) 407718ced0SMaksim Yevmenkin { 417718ced0SMaksim Yevmenkin packet_t *pkt; 427718ced0SMaksim Yevmenkin 437718ced0SMaksim Yevmenkin pkt = malloc(sizeof(packet_t) + chan->mru); 447718ced0SMaksim Yevmenkin if (pkt == NULL) { 457718ced0SMaksim Yevmenkin log_err("%s() failed: %m", __func__); 467718ced0SMaksim Yevmenkin return NULL; 477718ced0SMaksim Yevmenkin } 487718ced0SMaksim Yevmenkin 497718ced0SMaksim Yevmenkin memset(pkt, 0, sizeof(packet_t)); 507718ced0SMaksim Yevmenkin STAILQ_INIT(&pkt->extlist); 517718ced0SMaksim Yevmenkin pkt->ptr = pkt->buf; 527718ced0SMaksim Yevmenkin 537718ced0SMaksim Yevmenkin pkt->chan = chan; 547718ced0SMaksim Yevmenkin chan->refcnt++; 557718ced0SMaksim Yevmenkin 567718ced0SMaksim Yevmenkin return pkt; 577718ced0SMaksim Yevmenkin } 587718ced0SMaksim Yevmenkin 597718ced0SMaksim Yevmenkin void 607718ced0SMaksim Yevmenkin packet_free(packet_t *pkt) 617718ced0SMaksim Yevmenkin { 627718ced0SMaksim Yevmenkin exthdr_t *eh; 637718ced0SMaksim Yevmenkin 647718ced0SMaksim Yevmenkin if (pkt->refcnt-- > 0) 657718ced0SMaksim Yevmenkin return; 667718ced0SMaksim Yevmenkin 677718ced0SMaksim Yevmenkin while ((eh = STAILQ_FIRST(&pkt->extlist)) != NULL) { 687718ced0SMaksim Yevmenkin STAILQ_REMOVE_HEAD(&pkt->extlist, next); 697718ced0SMaksim Yevmenkin free(eh); 707718ced0SMaksim Yevmenkin } 717718ced0SMaksim Yevmenkin 727718ced0SMaksim Yevmenkin pkt->chan->refcnt--; 737718ced0SMaksim Yevmenkin if (pkt->chan->refcnt == 0) 747718ced0SMaksim Yevmenkin channel_free(pkt->chan); 757718ced0SMaksim Yevmenkin 767718ced0SMaksim Yevmenkin free(pkt); 777718ced0SMaksim Yevmenkin } 787718ced0SMaksim Yevmenkin 797718ced0SMaksim Yevmenkin void 807718ced0SMaksim Yevmenkin packet_adj(packet_t *pkt, size_t size) 817718ced0SMaksim Yevmenkin { 827718ced0SMaksim Yevmenkin 837718ced0SMaksim Yevmenkin assert(pkt->refcnt == 0); 847718ced0SMaksim Yevmenkin assert(pkt->len >= size); 857718ced0SMaksim Yevmenkin 867718ced0SMaksim Yevmenkin pkt->ptr += size; 877718ced0SMaksim Yevmenkin pkt->len -= size; 887718ced0SMaksim Yevmenkin } 897718ced0SMaksim Yevmenkin 907718ced0SMaksim Yevmenkin pkthdr_t * 917718ced0SMaksim Yevmenkin pkthdr_alloc(packet_t *pkt) 927718ced0SMaksim Yevmenkin { 937718ced0SMaksim Yevmenkin pkthdr_t *ph; 947718ced0SMaksim Yevmenkin 957718ced0SMaksim Yevmenkin ph = malloc(sizeof(pkthdr_t)); 967718ced0SMaksim Yevmenkin if (ph == NULL) { 977718ced0SMaksim Yevmenkin log_err("%s() failed: %m", __func__); 987718ced0SMaksim Yevmenkin return NULL; 997718ced0SMaksim Yevmenkin } 1007718ced0SMaksim Yevmenkin 1017718ced0SMaksim Yevmenkin ph->data = pkt; 1027718ced0SMaksim Yevmenkin pkt->refcnt++; 1037718ced0SMaksim Yevmenkin 1047718ced0SMaksim Yevmenkin return ph; 1057718ced0SMaksim Yevmenkin } 1067718ced0SMaksim Yevmenkin 1077718ced0SMaksim Yevmenkin void 1087718ced0SMaksim Yevmenkin pkthdr_free(pkthdr_t *ph) 1097718ced0SMaksim Yevmenkin { 1107718ced0SMaksim Yevmenkin 1117718ced0SMaksim Yevmenkin packet_free(ph->data); 1127718ced0SMaksim Yevmenkin free(ph); 1137718ced0SMaksim Yevmenkin } 114