profile.c revision 225736
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: stable/9/usr.sbin/bluetooth/sdpd/profile.c 177358 2008-03-18 18:21:39Z 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 * Service Availability 260 */ 261 262int32_t 263common_profile_create_service_availability( 264 uint8_t *buf, uint8_t const * const eob, 265 uint8_t const *data, uint32_t datalen) 266{ 267 if (datalen != 1 || buf + 2 > eob) 268 return (-1); 269 270 SDP_PUT8(SDP_DATA_UINT8, buf); 271 SDP_PUT8(data[0], buf); 272 273 return (2); 274} 275 276/* 277 * seq8 len8 - 2 bytes 278 * seq8 len8 - 2 bytes 279 * uuid16 value16 - 3 bytes 280 * seq8 len8 - 2 bytes 281 * uuid16 value16 - 3 bytes 282 * uint8 value8 - 2 bytes 283 */ 284 285int32_t 286rfcomm_profile_create_protocol_descriptor_list( 287 uint8_t *buf, uint8_t const * const eob, 288 uint8_t const *data, uint32_t datalen) 289{ 290 if (datalen != 1 || buf + 14 > eob) 291 return (-1); 292 293 SDP_PUT8(SDP_DATA_SEQ8, buf); 294 SDP_PUT8(12, buf); 295 296 SDP_PUT8(SDP_DATA_SEQ8, buf); 297 SDP_PUT8(3, buf); 298 SDP_PUT8(SDP_DATA_UUID16, buf); 299 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 300 301 SDP_PUT8(SDP_DATA_SEQ8, buf); 302 SDP_PUT8(5, buf); 303 SDP_PUT8(SDP_DATA_UUID16, buf); 304 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf); 305 SDP_PUT8(SDP_DATA_UINT8, buf); 306 SDP_PUT8(*data, buf); 307 308 return (14); 309} 310 311/* 312 * seq8 len8 - 2 bytes 313 * seq8 len8 - 2 bytes 314 * uuid16 value16 - 3 bytes 315 * seq8 len8 - 2 bytes 316 * uuid16 value16 - 3 bytes 317 * uint8 value8 - 2 bytes 318 * seq8 len8 - 2 bytes 319 * uuid16 value16 - 3 bytes 320 */ 321 322int32_t 323obex_profile_create_protocol_descriptor_list( 324 uint8_t *buf, uint8_t const * const eob, 325 uint8_t const *data, uint32_t datalen) 326{ 327 if (datalen != 1 || buf + 19 > eob) 328 return (-1); 329 330 SDP_PUT8(SDP_DATA_SEQ8, buf); 331 SDP_PUT8(17, buf); 332 333 SDP_PUT8(SDP_DATA_SEQ8, buf); 334 SDP_PUT8(3, buf); 335 SDP_PUT8(SDP_DATA_UUID16, buf); 336 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 337 338 SDP_PUT8(SDP_DATA_SEQ8, buf); 339 SDP_PUT8(5, buf); 340 SDP_PUT8(SDP_DATA_UUID16, buf); 341 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf); 342 SDP_PUT8(SDP_DATA_UINT8, buf); 343 SDP_PUT8(*data, buf); 344 345 SDP_PUT8(SDP_DATA_SEQ8, buf); 346 SDP_PUT8(3, buf); 347 SDP_PUT8(SDP_DATA_UUID16, buf); 348 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf); 349 350 return (19); 351} 352 353/* 354 * seq8 len8 355 * uint8 value8 - bytes 356 * [ uint8 value 8 ] 357 */ 358 359int32_t 360obex_profile_create_supported_formats_list( 361 uint8_t *buf, uint8_t const * const eob, 362 uint8_t const *data, uint32_t datalen) 363{ 364 int32_t len = 2 * datalen; 365 366 if (len <= 0 || len > 0xff || buf + 2 + len > eob) 367 return (-1); 368 369 SDP_PUT8(SDP_DATA_SEQ8, buf); 370 SDP_PUT8(len, buf); 371 372 for (; datalen > 0; datalen --) { 373 SDP_PUT8(SDP_DATA_UINT8, buf); 374 SDP_PUT8(*data++, buf); 375 } 376 377 return (2 + len); 378} 379 380/* 381 * do not check anything 382 */ 383 384int32_t 385common_profile_always_valid(uint8_t const *data, uint32_t datalen) 386{ 387 return (1); 388} 389 390/* 391 * verify server channel number (the first byte in the data) 392 */ 393 394int32_t 395common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen) 396{ 397 if (data[0] < 1 || data[0] > 30) 398 return (0); 399 400 return (1); 401} 402 403/* 404 * verify server channel number and supported_formats_size 405 * sdp_opush_profile and sdp_irmc_profile 406 */ 407 408int32_t 409obex_profile_data_valid(uint8_t const *data, uint32_t datalen) 410{ 411 sdp_opush_profile_p opush = (sdp_opush_profile_p) data; 412 413 if (opush->server_channel < 1 || 414 opush->server_channel > 30 || 415 opush->supported_formats_size == 0 || 416 opush->supported_formats_size > sizeof(opush->supported_formats)) 417 return (0); 418 419 return (1); 420} 421 422/* 423 * BNEP protocol descriptor 424 */ 425 426int32_t 427bnep_profile_create_protocol_descriptor_list( 428 uint8_t *buf, uint8_t const * const eob, 429 uint8_t const *data, uint32_t datalen) 430{ 431 /* supported protocol types */ 432 uint16_t ptype[] = { 433 0x0800, /* IPv4 */ 434 0x0806, /* ARP */ 435#ifdef INET6 436 0x86dd, /* IPv6 */ 437#endif 438 }; 439 440 uint16_t i, psm, version = 0x0100, 441 nptypes = sizeof(ptype)/sizeof(ptype[0]), 442 nptypes_size = nptypes * 3; 443 444 if (datalen != 2 || 18 + nptypes_size > 255 || 445 buf + 20 + nptypes_size > eob) 446 return (-1); 447 448 memcpy(&psm, data, sizeof(psm)); 449 450 SDP_PUT8(SDP_DATA_SEQ8, buf); 451 SDP_PUT8(18 + nptypes_size, buf); 452 453 SDP_PUT8(SDP_DATA_SEQ8, buf); 454 SDP_PUT8(6, buf); 455 SDP_PUT8(SDP_DATA_UUID16, buf); 456 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 457 SDP_PUT8(SDP_DATA_UINT16, buf); 458 SDP_PUT16(psm, buf); 459 460 SDP_PUT8(SDP_DATA_SEQ8, buf); 461 SDP_PUT8(8 + nptypes_size, buf); 462 SDP_PUT8(SDP_DATA_UUID16, buf); 463 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf); 464 SDP_PUT8(SDP_DATA_UINT16, buf); 465 SDP_PUT16(version, buf); 466 SDP_PUT8(SDP_DATA_SEQ8, buf); 467 SDP_PUT8(nptypes_size, buf); 468 for (i = 0; i < nptypes; i ++) { 469 SDP_PUT8(SDP_DATA_UINT16, buf); 470 SDP_PUT16(ptype[i], buf); 471 } 472 473 return (20 + nptypes_size); 474} 475 476/* 477 * BNEP security description 478 */ 479 480int32_t 481bnep_profile_create_security_description( 482 uint8_t *buf, uint8_t const * const eob, 483 uint8_t const *data, uint32_t datalen) 484{ 485 uint16_t security_descr; 486 487 if (datalen != 2 || buf + 3 > eob) 488 return (-1); 489 490 memcpy(&security_descr, data, sizeof(security_descr)); 491 492 SDP_PUT8(SDP_DATA_UINT16, buf); 493 SDP_PUT16(security_descr, buf); 494 495 return (3); 496} 497 498