1/*-
2 * lan.c
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
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: lan.c,v 1.5 2004/01/13 01:54:39 max Exp $
31 */
32
33#include <arpa/inet.h>
34#include <sys/queue.h>
35#define L2CAP_SOCKET_CHECKED
36#include <bluetooth.h>
37#include <sdp.h>
38#include <stdio.h>
39#include <string.h>
40#include "profile.h"
41#include "provider.h"
42
43static int32_t
44lan_profile_create_service_class_id_list(
45		uint8_t *buf, uint8_t const * const eob,
46		uint8_t const *data, uint32_t datalen)
47{
48	static uint16_t	service_classes[] = {
49		SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP
50	};
51
52	return (common_profile_create_service_class_id_list(
53			buf, eob,
54			(uint8_t const *) service_classes,
55			sizeof(service_classes)));
56}
57
58static int32_t
59lan_profile_create_bluetooth_profile_descriptor_list(
60		uint8_t *buf, uint8_t const * const eob,
61		uint8_t const *data, uint32_t datalen)
62{
63	static uint16_t	profile_descriptor_list[] = {
64		SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
65		0x0100
66	};
67
68	return (common_profile_create_bluetooth_profile_descriptor_list(
69			buf, eob,
70			(uint8_t const *) profile_descriptor_list,
71			sizeof(profile_descriptor_list)));
72}
73
74static int32_t
75lan_profile_create_service_name(
76		uint8_t *buf, uint8_t const * const eob,
77		uint8_t const *data, uint32_t datalen)
78{
79	static char	service_name[] = "LAN Access using PPP";
80
81	return (common_profile_create_string8(
82			buf, eob,
83			(uint8_t const *) service_name, strlen(service_name)));
84}
85
86static int32_t
87lan_profile_create_protocol_descriptor_list(
88		uint8_t *buf, uint8_t const * const eob,
89		uint8_t const *data, uint32_t datalen)
90{
91	provider_p		provider = (provider_p) data;
92	sdp_lan_profile_p	lan = (sdp_lan_profile_p) provider->data;
93
94	return (rfcomm_profile_create_protocol_descriptor_list(
95			buf, eob,
96			(uint8_t const *) &lan->server_channel, 1));
97}
98
99static int32_t
100lan_profile_create_service_availability(
101		uint8_t *buf, uint8_t const * const eob,
102		uint8_t const *data, uint32_t datalen)
103{
104	provider_p		provider = (provider_p) data;
105	sdp_lan_profile_p	lan = (sdp_lan_profile_p) provider->data;
106
107	return (common_profile_create_service_availability(buf, eob,
108			&lan->load_factor, 1));
109}
110
111static int32_t
112lan_profile_create_ip_subnet(
113		uint8_t *buf, uint8_t const * const eob,
114		uint8_t const *data, uint32_t datalen)
115{
116	provider_p		provider = (provider_p) data;
117	sdp_lan_profile_p	lan = (sdp_lan_profile_p) provider->data;
118	char			net[32];
119	int32_t			len;
120
121	len = snprintf(net, sizeof(net), "%s/%d",
122			inet_ntoa(* (struct in_addr *) &lan->ip_subnet),
123			lan->ip_subnet_radius);
124
125	if (len < 0 || buf + 2 + len > eob)
126		return (-1);
127
128	SDP_PUT8(SDP_DATA_STR8, buf);
129	SDP_PUT8(len, buf);
130	memcpy(buf, net, len);
131
132	return (2 + len);
133}
134
135static int32_t
136lan_profile_data_valid(uint8_t const *data, uint32_t datalen)
137{
138	sdp_lan_profile_p	lan = (sdp_lan_profile_p) data;
139
140	if (lan->server_channel < 1 ||
141	    lan->server_channel > 30 ||
142	    lan->ip_subnet_radius > 32)
143		return (0);
144
145	return (1);
146}
147
148static attr_t	lan_profile_attrs[] = {
149	{ SDP_ATTR_SERVICE_RECORD_HANDLE,
150	  common_profile_create_service_record_handle },
151	{ SDP_ATTR_SERVICE_CLASS_ID_LIST,
152	  lan_profile_create_service_class_id_list },
153	{ SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
154	  lan_profile_create_bluetooth_profile_descriptor_list },
155	{ SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
156	  common_profile_create_language_base_attribute_id_list },
157	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET,
158	  lan_profile_create_service_name },
159	{ SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
160	  lan_profile_create_protocol_descriptor_list },
161	{ SDP_ATTR_SERVICE_AVAILABILITY,
162	  lan_profile_create_service_availability },
163	{ SDP_ATTR_IP_SUBNET,
164	  lan_profile_create_ip_subnet },
165	{ 0, NULL } /* end entry */
166};
167
168profile_t	lan_profile_descriptor = {
169	SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
170	sizeof(sdp_lan_profile_t),
171	lan_profile_data_valid,
172	(attr_t const * const) &lan_profile_attrs
173};
174
175