1107120Sjulian/* 2107120Sjulian * ng_l2cap_cmds.h 3139823Simp */ 4139823Simp 5139823Simp/*- 6107120Sjulian * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com> 7107120Sjulian * All rights reserved. 8107120Sjulian * 9107120Sjulian * Redistribution and use in source and binary forms, with or without 10107120Sjulian * modification, are permitted provided that the following conditions 11107120Sjulian * are met: 12107120Sjulian * 1. Redistributions of source code must retain the above copyright 13107120Sjulian * notice, this list of conditions and the following disclaimer. 14107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright 15107120Sjulian * notice, this list of conditions and the following disclaimer in the 16107120Sjulian * documentation and/or other materials provided with the distribution. 17107120Sjulian * 18107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28107120Sjulian * SUCH DAMAGE. 29107120Sjulian * 30114878Sjulian * $Id: ng_l2cap_cmds.h,v 1.4 2003/04/01 18:15:26 max Exp $ 31107120Sjulian * $FreeBSD$ 32107120Sjulian */ 33107120Sjulian 34107120Sjulian#ifndef _NETGRAPH_L2CAP_CMDS_H_ 35107120Sjulian#define _NETGRAPH_L2CAP_CMDS_H_ 36107120Sjulian 37107120Sjulian/****************************************************************************** 38107120Sjulian ****************************************************************************** 39107120Sjulian ** L2CAP to L2CAP signaling command macros 40107120Sjulian ****************************************************************************** 41107120Sjulian ******************************************************************************/ 42107120Sjulian 43107120Sjulian/* 44107120Sjulian * Note: All L2CAP implementations are required to support minimal signaling 45107120Sjulian * MTU of 48 bytes. In order to simplify things we will send one command 46107120Sjulian * per one L2CAP packet. Given evrything above we can assume that one 47107120Sjulian * signaling packet will fit into single mbuf. 48107120Sjulian */ 49107120Sjulian 50107120Sjulian/* L2CAP_CommandRej */ 51107120Sjulian#define _ng_l2cap_cmd_rej(_m, _ident, _reason, _mtu, _scid, _dcid) \ 52107120Sjuliando { \ 53107120Sjulian struct _cmd_rej { \ 54107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 55107120Sjulian ng_l2cap_cmd_rej_cp param; \ 56107120Sjulian ng_l2cap_cmd_rej_data_t data; \ 57107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 58107120Sjulian \ 59111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 60107120Sjulian if ((_m) == NULL) \ 61107120Sjulian break; \ 62107120Sjulian \ 63107120Sjulian c = mtod((_m), struct _cmd_rej *); \ 64107120Sjulian c->hdr.code = NG_L2CAP_CMD_REJ; \ 65107120Sjulian c->hdr.ident = (_ident); \ 66107120Sjulian c->hdr.length = sizeof(c->param); \ 67107120Sjulian \ 68107120Sjulian c->param.reason = htole16((_reason)); \ 69107120Sjulian \ 70107120Sjulian if ((_reason) == NG_L2CAP_REJ_MTU_EXCEEDED) { \ 71107120Sjulian c->data.mtu.mtu = htole16((_mtu)); \ 72107120Sjulian c->hdr.length += sizeof(c->data.mtu); \ 73107120Sjulian } else if ((_reason) == NG_L2CAP_REJ_INVALID_CID) { \ 74107120Sjulian c->data.cid.scid = htole16((_scid)); \ 75107120Sjulian c->data.cid.dcid = htole16((_dcid)); \ 76107120Sjulian c->hdr.length += sizeof(c->data.cid); \ 77107120Sjulian } \ 78107120Sjulian \ 79107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) + \ 80107120Sjulian c->hdr.length; \ 81107120Sjulian \ 82107120Sjulian c->hdr.length = htole16(c->hdr.length); \ 83107120Sjulian} while (0) 84107120Sjulian 85107120Sjulian/* L2CAP_ConnectReq */ 86107120Sjulian#define _ng_l2cap_con_req(_m, _ident, _psm, _scid) \ 87107120Sjuliando { \ 88107120Sjulian struct _con_req { \ 89107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 90107120Sjulian ng_l2cap_con_req_cp param; \ 91107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 92107120Sjulian \ 93111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 94107120Sjulian if ((_m) == NULL) \ 95107120Sjulian break; \ 96107120Sjulian \ 97107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 98107120Sjulian \ 99107120Sjulian c = mtod((_m), struct _con_req *); \ 100107120Sjulian c->hdr.code = NG_L2CAP_CON_REQ; \ 101107120Sjulian c->hdr.ident = (_ident); \ 102107120Sjulian c->hdr.length = htole16(sizeof(c->param)); \ 103107120Sjulian \ 104107120Sjulian c->param.psm = htole16((_psm)); \ 105107120Sjulian c->param.scid = htole16((_scid)); \ 106107120Sjulian} while (0) 107107120Sjulian 108107120Sjulian/* L2CAP_ConnectRsp */ 109107120Sjulian#define _ng_l2cap_con_rsp(_m, _ident, _dcid, _scid, _result, _status) \ 110107120Sjuliando { \ 111107120Sjulian struct _con_rsp { \ 112107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 113107120Sjulian ng_l2cap_con_rsp_cp param; \ 114107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 115107120Sjulian \ 116111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 117107120Sjulian if ((_m) == NULL) \ 118107120Sjulian break; \ 119107120Sjulian \ 120107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 121107120Sjulian \ 122107120Sjulian c = mtod((_m), struct _con_rsp *); \ 123107120Sjulian c->hdr.code = NG_L2CAP_CON_RSP; \ 124107120Sjulian c->hdr.ident = (_ident); \ 125107120Sjulian c->hdr.length = htole16(sizeof(c->param)); \ 126107120Sjulian \ 127107120Sjulian c->param.dcid = htole16((_dcid)); \ 128107120Sjulian c->param.scid = htole16((_scid)); \ 129107120Sjulian c->param.result = htole16((_result)); \ 130107120Sjulian c->param.status = htole16((_status)); \ 131107120Sjulian} while (0) 132107120Sjulian 133107120Sjulian/* L2CAP_ConfigReq */ 134107120Sjulian#define _ng_l2cap_cfg_req(_m, _ident, _dcid, _flags, _data) \ 135107120Sjuliando { \ 136107120Sjulian struct _cfg_req { \ 137107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 138107120Sjulian ng_l2cap_cfg_req_cp param; \ 139107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 140107120Sjulian \ 141111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 142107120Sjulian if ((_m) == NULL) { \ 143107120Sjulian NG_FREE_M((_data)); \ 144107120Sjulian break; \ 145107120Sjulian } \ 146107120Sjulian \ 147107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 148107120Sjulian \ 149107120Sjulian c = mtod((_m), struct _cfg_req *); \ 150107120Sjulian c->hdr.code = NG_L2CAP_CFG_REQ; \ 151107120Sjulian c->hdr.ident = (_ident); \ 152107120Sjulian c->hdr.length = sizeof(c->param); \ 153107120Sjulian \ 154107120Sjulian c->param.dcid = htole16((_dcid)); \ 155107120Sjulian c->param.flags = htole16((_flags)); \ 156107120Sjulian if ((_data) != NULL) { \ 157114878Sjulian int l = (_data)->m_pkthdr.len; \ 158114878Sjulian \ 159107120Sjulian m_cat((_m), (_data)); \ 160114878Sjulian c->hdr.length += l; \ 161114878Sjulian (_m)->m_pkthdr.len += l; \ 162107120Sjulian } \ 163107120Sjulian \ 164107120Sjulian c->hdr.length = htole16(c->hdr.length); \ 165107120Sjulian} while (0) 166107120Sjulian 167107120Sjulian/* L2CAP_ConfigRsp */ 168107120Sjulian#define _ng_l2cap_cfg_rsp(_m, _ident, _scid, _flags, _result, _data) \ 169107120Sjuliando { \ 170107120Sjulian struct _cfg_rsp { \ 171107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 172107120Sjulian ng_l2cap_cfg_rsp_cp param; \ 173107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 174107120Sjulian \ 175111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 176107120Sjulian if ((_m) == NULL) { \ 177107120Sjulian NG_FREE_M((_data)); \ 178107120Sjulian break; \ 179107120Sjulian } \ 180107120Sjulian \ 181107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 182107120Sjulian \ 183107120Sjulian c = mtod((_m), struct _cfg_rsp *); \ 184107120Sjulian c->hdr.code = NG_L2CAP_CFG_RSP; \ 185107120Sjulian c->hdr.ident = (_ident); \ 186107120Sjulian c->hdr.length = sizeof(c->param); \ 187107120Sjulian \ 188107120Sjulian c->param.scid = htole16((_scid)); \ 189107120Sjulian c->param.flags = htole16((_flags)); \ 190107120Sjulian c->param.result = htole16((_result)); \ 191107120Sjulian if ((_data) != NULL) { \ 192114878Sjulian int l = (_data)->m_pkthdr.len; \ 193114878Sjulian \ 194107120Sjulian m_cat((_m), (_data)); \ 195114878Sjulian c->hdr.length += l; \ 196114878Sjulian (_m)->m_pkthdr.len += l; \ 197107120Sjulian } \ 198107120Sjulian \ 199107120Sjulian c->hdr.length = htole16(c->hdr.length); \ 200107120Sjulian} while (0) 201107120Sjulian 202107120Sjulian/* Build configuration options */ 203107120Sjulian#define _ng_l2cap_build_cfg_options(_m, _mtu, _flush_timo, _flow) \ 204107120Sjuliando { \ 205107120Sjulian u_int8_t *p = NULL; \ 206107120Sjulian \ 207111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 208107120Sjulian if ((_m) == NULL) \ 209107120Sjulian break; \ 210107120Sjulian \ 211107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = 0; \ 212107120Sjulian p = mtod((_m), u_int8_t *); \ 213107120Sjulian \ 214107120Sjulian if ((_mtu) != NULL) { \ 215107120Sjulian struct _cfg_opt_mtu { \ 216107120Sjulian ng_l2cap_cfg_opt_t hdr; \ 217107120Sjulian u_int16_t val; \ 218107120Sjulian } __attribute__ ((packed)) *o = NULL; \ 219107120Sjulian \ 220107120Sjulian o = (struct _cfg_opt_mtu *) p; \ 221107120Sjulian o->hdr.type = NG_L2CAP_OPT_MTU; \ 222107120Sjulian o->hdr.length = sizeof(o->val); \ 223107120Sjulian o->val = htole16(*(u_int16_t *)(_mtu)); \ 224107120Sjulian \ 225107120Sjulian (_m)->m_pkthdr.len += sizeof(*o); \ 226107120Sjulian p += sizeof(*o); \ 227107120Sjulian } \ 228107120Sjulian \ 229107120Sjulian if ((_flush_timo) != NULL) { \ 230107120Sjulian struct _cfg_opt_flush { \ 231107120Sjulian ng_l2cap_cfg_opt_t hdr; \ 232107120Sjulian u_int16_t val; \ 233107120Sjulian } __attribute__ ((packed)) *o = NULL; \ 234107120Sjulian \ 235107120Sjulian o = (struct _cfg_opt_flush *) p; \ 236107120Sjulian o->hdr.type = NG_L2CAP_OPT_FLUSH_TIMO; \ 237107120Sjulian o->hdr.length = sizeof(o->val); \ 238107120Sjulian o->val = htole16(*(u_int16_t *)(_flush_timo)); \ 239107120Sjulian \ 240107120Sjulian (_m)->m_pkthdr.len += sizeof(*o); \ 241107120Sjulian p += sizeof(*o); \ 242107120Sjulian } \ 243107120Sjulian \ 244107120Sjulian if ((_flow) != NULL) { \ 245107120Sjulian struct _cfg_opt_flow { \ 246107120Sjulian ng_l2cap_cfg_opt_t hdr; \ 247107120Sjulian ng_l2cap_flow_t val; \ 248107120Sjulian } __attribute__ ((packed)) *o = NULL; \ 249107120Sjulian \ 250107120Sjulian o = (struct _cfg_opt_flow *) p; \ 251107120Sjulian o->hdr.type = NG_L2CAP_OPT_QOS; \ 252107120Sjulian o->hdr.length = sizeof(o->val); \ 253107120Sjulian o->val.flags = ((ng_l2cap_flow_p)(_flow))->flags; \ 254107120Sjulian o->val.service_type = ((ng_l2cap_flow_p) \ 255107120Sjulian (_flow))->service_type; \ 256107120Sjulian o->val.token_rate = \ 257107120Sjulian htole32(((ng_l2cap_flow_p)(_flow))->token_rate);\ 258107120Sjulian o->val.token_bucket_size = \ 259107120Sjulian htole32(((ng_l2cap_flow_p) \ 260107120Sjulian (_flow))->token_bucket_size); \ 261107120Sjulian o->val.peak_bandwidth = \ 262107120Sjulian htole32(((ng_l2cap_flow_p) \ 263107120Sjulian (_flow))->peak_bandwidth); \ 264107120Sjulian o->val.latency = htole32(((ng_l2cap_flow_p) \ 265107120Sjulian (_flow))->latency); \ 266107120Sjulian o->val.delay_variation = \ 267107120Sjulian htole32(((ng_l2cap_flow_p) \ 268107120Sjulian (_flow))->delay_variation); \ 269107120Sjulian \ 270107120Sjulian (_m)->m_pkthdr.len += sizeof(*o); \ 271107120Sjulian } \ 272107120Sjulian \ 273107120Sjulian (_m)->m_len = (_m)->m_pkthdr.len; \ 274107120Sjulian} while (0) 275107120Sjulian 276107120Sjulian/* L2CAP_DisconnectReq */ 277107120Sjulian#define _ng_l2cap_discon_req(_m, _ident, _dcid, _scid) \ 278107120Sjuliando { \ 279107120Sjulian struct _discon_req { \ 280107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 281107120Sjulian ng_l2cap_discon_req_cp param; \ 282107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 283107120Sjulian \ 284111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 285107120Sjulian if ((_m) == NULL) \ 286107120Sjulian break; \ 287107120Sjulian \ 288107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 289107120Sjulian \ 290107120Sjulian c = mtod((_m), struct _discon_req *); \ 291107120Sjulian c->hdr.code = NG_L2CAP_DISCON_REQ; \ 292107120Sjulian c->hdr.ident = (_ident); \ 293107120Sjulian c->hdr.length = htole16(sizeof(c->param)); \ 294107120Sjulian \ 295107120Sjulian c->param.dcid = htole16((_dcid)); \ 296107120Sjulian c->param.scid = htole16((_scid)); \ 297107120Sjulian} while (0) 298107120Sjulian 299107120Sjulian/* L2CA_DisconnectRsp */ 300107120Sjulian#define _ng_l2cap_discon_rsp(_m, _ident, _dcid, _scid) \ 301107120Sjuliando { \ 302107120Sjulian struct _discon_rsp { \ 303107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 304107120Sjulian ng_l2cap_discon_rsp_cp param; \ 305107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 306107120Sjulian \ 307111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 308107120Sjulian if ((_m) == NULL) \ 309107120Sjulian break; \ 310107120Sjulian \ 311107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 312107120Sjulian \ 313107120Sjulian c = mtod((_m), struct _discon_rsp *); \ 314107120Sjulian c->hdr.code = NG_L2CAP_DISCON_RSP; \ 315107120Sjulian c->hdr.ident = (_ident); \ 316107120Sjulian c->hdr.length = htole16(sizeof(c->param)); \ 317107120Sjulian \ 318107120Sjulian c->param.dcid = htole16((_dcid)); \ 319107120Sjulian c->param.scid = htole16((_scid)); \ 320107120Sjulian} while (0) 321107120Sjulian 322107120Sjulian/* L2CAP_EchoReq */ 323107120Sjulian#define _ng_l2cap_echo_req(_m, _ident, _data, _size) \ 324107120Sjuliando { \ 325107120Sjulian ng_l2cap_cmd_hdr_t *c = NULL; \ 326107120Sjulian \ 327111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 328107120Sjulian if ((_m) == NULL) \ 329107120Sjulian break; \ 330107120Sjulian \ 331107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 332107120Sjulian \ 333107120Sjulian c = mtod((_m), ng_l2cap_cmd_hdr_t *); \ 334107120Sjulian c->code = NG_L2CAP_ECHO_REQ; \ 335107120Sjulian c->ident = (_ident); \ 336107120Sjulian c->length = 0; \ 337107120Sjulian \ 338107120Sjulian if ((_data) != NULL) { \ 339107120Sjulian m_copyback((_m), sizeof(*c), (_size), (_data)); \ 340107120Sjulian c->length += (_size); \ 341107120Sjulian } \ 342107120Sjulian \ 343107120Sjulian c->length = htole16(c->length); \ 344107120Sjulian} while (0) 345107120Sjulian 346107120Sjulian/* L2CAP_InfoReq */ 347107120Sjulian#define _ng_l2cap_info_req(_m, _ident, _type) \ 348107120Sjuliando { \ 349107120Sjulian struct _info_req { \ 350107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 351107120Sjulian ng_l2cap_info_req_cp param; \ 352107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 353107120Sjulian \ 354111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 355107120Sjulian if ((_m) == NULL) \ 356107120Sjulian break; \ 357107120Sjulian \ 358107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \ 359107120Sjulian \ 360107120Sjulian c = mtod((_m), struct _info_req *); \ 361107120Sjulian c->hdr.code = NG_L2CAP_INFO_REQ; \ 362107120Sjulian c->hdr.ident = (_ident); \ 363107120Sjulian c->hdr.length = htole16(sizeof(c->param)); \ 364107120Sjulian \ 365107120Sjulian c->param.type = htole16((_type)); \ 366107120Sjulian} while (0) 367107120Sjulian 368107120Sjulian/* L2CAP_InfoRsp */ 369107120Sjulian#define _ng_l2cap_info_rsp(_m, _ident, _type, _result, _mtu) \ 370107120Sjuliando { \ 371107120Sjulian struct _info_rsp { \ 372107120Sjulian ng_l2cap_cmd_hdr_t hdr; \ 373107120Sjulian ng_l2cap_info_rsp_cp param; \ 374107120Sjulian ng_l2cap_info_rsp_data_t data; \ 375107120Sjulian } __attribute__ ((packed)) *c = NULL; \ 376107120Sjulian \ 377111119Simp MGETHDR((_m), M_DONTWAIT, MT_DATA); \ 378107120Sjulian if ((_m) == NULL) \ 379107120Sjulian break; \ 380107120Sjulian \ 381107120Sjulian c = mtod((_m), struct _info_rsp *); \ 382210783Semax c->hdr.code = NG_L2CAP_INFO_RSP; \ 383107120Sjulian c->hdr.ident = (_ident); \ 384107120Sjulian c->hdr.length = sizeof(c->param); \ 385107120Sjulian \ 386107120Sjulian c->param.type = htole16((_type)); \ 387107120Sjulian c->param.result = htole16((_result)); \ 388107120Sjulian \ 389107120Sjulian if ((_result) == NG_L2CAP_SUCCESS) { \ 390107120Sjulian switch ((_type)) { \ 391107120Sjulian case NG_L2CAP_CONNLESS_MTU: \ 392107120Sjulian c->data.mtu.mtu = htole16((_mtu)); \ 393107120Sjulian c->hdr.length += sizeof((c->data.mtu.mtu)); \ 394107120Sjulian break; \ 395107120Sjulian } \ 396107120Sjulian } \ 397107120Sjulian \ 398107120Sjulian (_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) + \ 399107120Sjulian c->hdr.length; \ 400107120Sjulian \ 401107120Sjulian c->hdr.length = htole16(c->hdr.length); \ 402107120Sjulian} while (0) 403107120Sjulian 404107120Sjulianvoid ng_l2cap_con_wakeup (ng_l2cap_con_p); 405107120Sjulianvoid ng_l2cap_con_fail (ng_l2cap_con_p, u_int16_t); 406107120Sjulianvoid ng_l2cap_process_command_timeout (node_p, hook_p, void *, int); 407107120Sjulian 408107120Sjulian#endif /* ndef _NETGRAPH_L2CAP_CMDS_H_ */ 409107120Sjulian 410