profile.c revision 177059
1/* 2 * profile.c 3 */ 4 5/*- 6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: profile.c,v 1.6 2004/01/13 19:31:54 max Exp $ 31 * $FreeBSD: head/usr.sbin/bluetooth/sdpd/profile.c 177059 2008-03-11 16:51:07Z emax $ 32 */ 33 34#include <sys/queue.h> 35#include <bluetooth.h> 36#include <sdp.h> 37#include <string.h> 38#include "profile.h" 39#include "provider.h" 40 41/* 42 * Lookup profile descriptor 43 */ 44 45profile_p 46profile_get_descriptor(uint16_t uuid) 47{ 48 extern profile_t dun_profile_descriptor; 49 extern profile_t ftrn_profile_descriptor; 50 extern profile_t irmc_profile_descriptor; 51 extern profile_t irmc_command_profile_descriptor; 52 extern profile_t lan_profile_descriptor; 53 extern profile_t opush_profile_descriptor; 54 extern profile_t sp_profile_descriptor; 55 extern profile_t nap_profile_descriptor; 56 extern profile_t gn_profile_descriptor; 57 extern profile_t panu_profile_descriptor; 58 59 static const profile_p profiles[] = { 60 &dun_profile_descriptor, 61 &ftrn_profile_descriptor, 62 &irmc_profile_descriptor, 63 &irmc_command_profile_descriptor, 64 &lan_profile_descriptor, 65 &opush_profile_descriptor, 66 &sp_profile_descriptor, 67 &nap_profile_descriptor, 68 &gn_profile_descriptor, 69 &panu_profile_descriptor 70 }; 71 72 int32_t i; 73 74 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++) 75 if (profiles[i]->uuid == uuid) 76 return (profiles[i]); 77 78 return (NULL); 79} 80 81/* 82 * Look attribute in the profile descripror 83 */ 84 85profile_attr_create_p 86profile_get_attr(const profile_p profile, uint16_t attr) 87{ 88 attr_p ad = (attr_p) profile->attrs; 89 90 for (; ad->create != NULL; ad ++) 91 if (ad->attr == attr) 92 return (ad->create); 93 94 return (NULL); 95} 96 97/* 98 * uint32 value32 - 5 bytes 99 */ 100 101int32_t 102common_profile_create_service_record_handle( 103 uint8_t *buf, uint8_t const * const eob, 104 uint8_t const *data, uint32_t datalen) 105{ 106 if (buf + 5 > eob) 107 return (-1); 108 109 SDP_PUT8(SDP_DATA_UINT32, buf); 110 SDP_PUT32(((provider_p) data)->handle, buf); 111 112 return (5); 113} 114 115/* 116 * seq8 len8 - 2 bytes 117 * uuid16 value16 - 3 bytes 118 * [ uuid16 value ] 119 */ 120 121int32_t 122common_profile_create_service_class_id_list( 123 uint8_t *buf, uint8_t const * const eob, 124 uint8_t const *data, uint32_t datalen) 125{ 126 int32_t len = 3 * (datalen >>= 1); 127 128 if (len <= 0 || len > 0xff || buf + 2 + len > eob) 129 return (-1); 130 131 SDP_PUT8(SDP_DATA_SEQ8, buf); 132 SDP_PUT8(len, buf); 133 134 for (; datalen > 0; datalen --) { 135 SDP_PUT8(SDP_DATA_UUID16, buf); 136 SDP_PUT16(*((uint16_t const *)data), buf); 137 data += sizeof(uint16_t); 138 } 139 140 return (2 + len); 141} 142 143/* 144 * seq8 len8 - 2 bytes 145 * seq 8 len8 - 2 bytes 146 * uuid16 value16 - 3 bytes 147 * uint16 value16 - 3 bytes 148 * [ seq 8 len8 149 * uuid16 value16 150 * uint16 value16 ] 151 */ 152 153int32_t 154common_profile_create_bluetooth_profile_descriptor_list( 155 uint8_t *buf, uint8_t const * const eob, 156 uint8_t const *data, uint32_t datalen) 157{ 158 int32_t len = 8 * (datalen >>= 2); 159 160 if (len <= 0 || len > 0xff || buf + 2 + len > eob) 161 return (-1); 162 163 SDP_PUT8(SDP_DATA_SEQ8, buf); 164 SDP_PUT8(len, buf); 165 166 for (; datalen > 0; datalen --) { 167 SDP_PUT8(SDP_DATA_SEQ8, buf); 168 SDP_PUT8(6, buf); 169 SDP_PUT8(SDP_DATA_UUID16, buf); 170 SDP_PUT16(*((uint16_t const *)data), buf); 171 data += sizeof(uint16_t); 172 SDP_PUT8(SDP_DATA_UINT16, buf); 173 SDP_PUT16(*((uint16_t const *)data), buf); 174 data += sizeof(uint16_t); 175 } 176 177 return (2 + len); 178} 179 180/* 181 * seq8 len8 - 2 bytes 182 * uint16 value16 - 3 bytes 183 * uint16 value16 - 3 bytes 184 * uint16 value16 - 3 bytes 185 */ 186 187int32_t 188common_profile_create_language_base_attribute_id_list( 189 uint8_t *buf, uint8_t const * const eob, 190 uint8_t const *data, uint32_t datalen) 191{ 192 if (buf + 11 > eob) 193 return (-1); 194 195 SDP_PUT8(SDP_DATA_SEQ8, buf); 196 SDP_PUT8(9, buf); 197 198 /* 199 * Language code per ISO 639:1988. Use "en". 200 */ 201 202 SDP_PUT8(SDP_DATA_UINT16, buf); 203 SDP_PUT16(((0x65 << 8) | 0x6e), buf); 204 205 /* 206 * Encoding. Recommended is UTF-8. ISO639 UTF-8 MIBenum is 106 207 * (http://www.iana.org/assignments/character-sets) 208 */ 209 210 SDP_PUT8(SDP_DATA_UINT16, buf); 211 SDP_PUT16(106, buf); 212 213 /* 214 * Offset (Primary Language Base is 0x100) 215 */ 216 217 SDP_PUT8(SDP_DATA_UINT16, buf); 218 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf); 219 220 return (11); 221} 222 223/* 224 * Common provider name is "FreeBSD" 225 */ 226 227int32_t 228common_profile_create_service_provider_name( 229 uint8_t *buf, uint8_t const * const eob, 230 uint8_t const *data, uint32_t datalen) 231{ 232 char provider_name[] = "FreeBSD"; 233 234 return (common_profile_create_string8(buf, eob, 235 (uint8_t const *) provider_name, 236 strlen(provider_name))); 237} 238 239/* 240 * str8 len8 string 241 */ 242 243int32_t 244common_profile_create_string8( 245 uint8_t *buf, uint8_t const * const eob, 246 uint8_t const *data, uint32_t datalen) 247{ 248 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob) 249 return (-1); 250 251 SDP_PUT8(SDP_DATA_STR8, buf); 252 SDP_PUT8(datalen, buf); 253 memcpy(buf, data, datalen); 254 255 return (2 + datalen); 256} 257 258/* 259 * seq8 len8 - 2 bytes 260 * seq8 len8 - 2 bytes 261 * uuid16 value16 - 3 bytes 262 * seq8 len8 - 2 bytes 263 * uuid16 value16 - 3 bytes 264 * uint8 value8 - 2 bytes 265 */ 266 267int32_t 268rfcomm_profile_create_protocol_descriptor_list( 269 uint8_t *buf, uint8_t const * const eob, 270 uint8_t const *data, uint32_t datalen) 271{ 272 if (datalen != 1 || buf + 14 > eob) 273 return (-1); 274 275 SDP_PUT8(SDP_DATA_SEQ8, buf); 276 SDP_PUT8(12, buf); 277 278 SDP_PUT8(SDP_DATA_SEQ8, buf); 279 SDP_PUT8(3, buf); 280 SDP_PUT8(SDP_DATA_UUID16, buf); 281 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 282 283 SDP_PUT8(SDP_DATA_SEQ8, buf); 284 SDP_PUT8(5, buf); 285 SDP_PUT8(SDP_DATA_UUID16, buf); 286 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf); 287 SDP_PUT8(SDP_DATA_UINT8, buf); 288 SDP_PUT8(*data, buf); 289 290 return (14); 291} 292 293/* 294 * seq8 len8 - 2 bytes 295 * seq8 len8 - 2 bytes 296 * uuid16 value16 - 3 bytes 297 * seq8 len8 - 2 bytes 298 * uuid16 value16 - 3 bytes 299 * uint8 value8 - 2 bytes 300 * seq8 len8 - 2 bytes 301 * uuid16 value16 - 3 bytes 302 */ 303 304int32_t 305obex_profile_create_protocol_descriptor_list( 306 uint8_t *buf, uint8_t const * const eob, 307 uint8_t const *data, uint32_t datalen) 308{ 309 if (datalen != 1 || buf + 19 > eob) 310 return (-1); 311 312 SDP_PUT8(SDP_DATA_SEQ8, buf); 313 SDP_PUT8(17, buf); 314 315 SDP_PUT8(SDP_DATA_SEQ8, buf); 316 SDP_PUT8(3, buf); 317 SDP_PUT8(SDP_DATA_UUID16, buf); 318 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 319 320 SDP_PUT8(SDP_DATA_SEQ8, buf); 321 SDP_PUT8(5, buf); 322 SDP_PUT8(SDP_DATA_UUID16, buf); 323 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf); 324 SDP_PUT8(SDP_DATA_UINT8, buf); 325 SDP_PUT8(*data, buf); 326 327 SDP_PUT8(SDP_DATA_SEQ8, buf); 328 SDP_PUT8(3, buf); 329 SDP_PUT8(SDP_DATA_UUID16, buf); 330 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf); 331 332 return (19); 333} 334 335/* 336 * seq8 len8 337 * uint8 value8 - bytes 338 * [ uint8 value 8 ] 339 */ 340 341int32_t 342obex_profile_create_supported_formats_list( 343 uint8_t *buf, uint8_t const * const eob, 344 uint8_t const *data, uint32_t datalen) 345{ 346 int32_t len = 2 * datalen; 347 348 if (len <= 0 || len > 0xff || buf + 2 + len > eob) 349 return (-1); 350 351 SDP_PUT8(SDP_DATA_SEQ8, buf); 352 SDP_PUT8(len, buf); 353 354 for (; datalen > 0; datalen --) { 355 SDP_PUT8(SDP_DATA_UINT8, buf); 356 SDP_PUT8(*data++, buf); 357 } 358 359 return (2 + len); 360} 361 362/* 363 * do not check anything 364 */ 365 366int32_t 367common_profile_always_valid(uint8_t const *data, uint32_t datalen) 368{ 369 return (1); 370} 371 372/* 373 * verify server channel number (the first byte in the data) 374 */ 375 376int32_t 377common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen) 378{ 379 if (data[0] < 1 || data[0] > 30) 380 return (0); 381 382 return (1); 383} 384 385/* 386 * verify server channel number and supported_formats_size 387 * sdp_opush_profile and sdp_irmc_profile 388 */ 389 390int32_t 391obex_profile_data_valid(uint8_t const *data, uint32_t datalen) 392{ 393 sdp_opush_profile_p opush = (sdp_opush_profile_p) data; 394 395 if (opush->server_channel < 1 || 396 opush->server_channel > 30 || 397 opush->supported_formats_size == 0 || 398 opush->supported_formats_size > sizeof(opush->supported_formats)) 399 return (0); 400 401 return (1); 402} 403 404/* 405 * BNEP protocol descriptor 406 */ 407 408int32_t 409bnep_profile_create_protocol_descriptor_list( 410 uint8_t *buf, uint8_t const * const eob, 411 uint8_t const *data, uint32_t datalen) 412{ 413 /* supported protocol types */ 414 uint16_t ptype[] = { 415 0x0800, /* IPv4 */ 416 0x0806, /* ARP */ 417#ifdef INET6 418 0x86dd, /* IPv6 */ 419#endif 420 }; 421 422 uint16_t i, psm = 15, version = 0x0100, 423 nptypes = sizeof(ptype)/sizeof(ptype[0]), 424 nptypes_size = nptypes * 3; 425 426 if (18 + nptypes_size > 255 || buf + 20 + nptypes_size > eob) 427 return (-1); 428 429 SDP_PUT8(SDP_DATA_SEQ8, buf); 430 SDP_PUT8(18 + nptypes_size, buf); 431 432 SDP_PUT8(SDP_DATA_SEQ8, buf); 433 SDP_PUT8(6, buf); 434 SDP_PUT8(SDP_DATA_UUID16, buf); 435 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 436 SDP_PUT8(SDP_DATA_UINT16, buf); 437 SDP_PUT16(psm, buf); 438 439 SDP_PUT8(SDP_DATA_SEQ8, buf); 440 SDP_PUT8(8 + nptypes_size, buf); 441 SDP_PUT8(SDP_DATA_UUID16, buf); 442 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf); 443 SDP_PUT8(SDP_DATA_UINT16, buf); 444 SDP_PUT16(version, buf); 445 SDP_PUT8(SDP_DATA_SEQ8, buf); 446 SDP_PUT8(nptypes_size, buf); 447 for (i = 0; i < nptypes; i ++) { 448 SDP_PUT8(SDP_DATA_UINT16, buf); 449 SDP_PUT16(ptype[i], buf); 450 } 451 452 return (20 + nptypes_size); 453} 454 455/* 456 * BNEP security description 457 */ 458 459int32_t 460bnep_profile_create_security_description( 461 uint8_t *buf, uint8_t const * const eob, 462 uint8_t const *data, uint32_t datalen) 463{ 464 uint16_t security_descr; 465 466 if (datalen != 2 || buf + 3 > eob) 467 return (-1); 468 469 memcpy(&security_descr, data, sizeof(security_descr)); 470 471 SDP_PUT8(SDP_DATA_UINT16, buf); 472 SDP_PUT16(security_descr, buf); 473 474 return (3); 475} 476 477