1 /*********************************************************************
2  *
3  * Filename:      irlan_provider.c
4  * Version:       0.9
5  * Description:   IrDA LAN Access Protocol Implementation
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Aug 31 20:14:37 1997
9  * Modified at:   Sat Oct 30 12:52:10 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Sources:       skeleton.c by Donald Becker <becker@CESDIS.gsfc.nasa.gov>
12  *                slip.c by Laurence Culhane,   <loz@holmes.demon.co.uk>
13  *                          Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
14  *
15  *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
16  *     All Rights Reserved.
17  *
18  *     This program is free software; you can redistribute it and/or
19  *     modify it under the terms of the GNU General Public License as
20  *     published by the Free Software Foundation; either version 2 of
21  *     the License, or (at your option) any later version.
22  *
23  *     Neither Dag Brattli nor University of Tromsø admit liability nor
24  *     provide warranty for any of this software. This material is
25  *     provided "AS-IS" and at no charge.
26  *
27  ********************************************************************/
28 
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/init.h>
35 #include <linux/random.h>
36 #include <linux/bitops.h>
37 #include <linux/slab.h>
38 
39 #include <asm/system.h>
40 #include <asm/byteorder.h>
41 
42 #include <net/irda/irda.h>
43 #include <net/irda/irttp.h>
44 #include <net/irda/irlmp.h>
45 #include <net/irda/irias_object.h>
46 #include <net/irda/iriap.h>
47 #include <net/irda/timer.h>
48 
49 #include <net/irda/irlan_common.h>
50 #include <net/irda/irlan_eth.h>
51 #include <net/irda/irlan_event.h>
52 #include <net/irda/irlan_provider.h>
53 #include <net/irda/irlan_filter.h>
54 #include <net/irda/irlan_client.h>
55 
56 static void irlan_provider_connect_indication(void *instance, void *sap,
57 					      struct qos_info *qos,
58 					      __u32 max_sdu_size,
59 					      __u8 max_header_size,
60 					      struct sk_buff *skb);
61 
62 /*
63  * Function irlan_provider_control_data_indication (handle, skb)
64  *
65  *    This function gets the data that is received on the control channel
66  *
67  */
irlan_provider_data_indication(void * instance,void * sap,struct sk_buff * skb)68 static int irlan_provider_data_indication(void *instance, void *sap,
69 					  struct sk_buff *skb)
70 {
71 	struct irlan_cb *self;
72 	__u8 code;
73 
74 	IRDA_DEBUG(4, "%s()\n", __func__ );
75 
76 	self = (struct irlan_cb *) instance;
77 
78 	IRDA_ASSERT(self != NULL, return -1;);
79 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
80 
81 	IRDA_ASSERT(skb != NULL, return -1;);
82 
83 	code = skb->data[0];
84 	switch(code) {
85 	case CMD_GET_PROVIDER_INFO:
86 		IRDA_DEBUG(4, "Got GET_PROVIDER_INFO command!\n");
87 		irlan_do_provider_event(self, IRLAN_GET_INFO_CMD, skb);
88 		break;
89 
90 	case CMD_GET_MEDIA_CHAR:
91 		IRDA_DEBUG(4, "Got GET_MEDIA_CHAR command!\n");
92 		irlan_do_provider_event(self, IRLAN_GET_MEDIA_CMD, skb);
93 		break;
94 	case CMD_OPEN_DATA_CHANNEL:
95 		IRDA_DEBUG(4, "Got OPEN_DATA_CHANNEL command!\n");
96 		irlan_do_provider_event(self, IRLAN_OPEN_DATA_CMD, skb);
97 		break;
98 	case CMD_FILTER_OPERATION:
99 		IRDA_DEBUG(4, "Got FILTER_OPERATION command!\n");
100 		irlan_do_provider_event(self, IRLAN_FILTER_CONFIG_CMD, skb);
101 		break;
102 	case CMD_RECONNECT_DATA_CHAN:
103 		IRDA_DEBUG(2, "%s(), Got RECONNECT_DATA_CHAN command\n", __func__ );
104 		IRDA_DEBUG(2, "%s(), NOT IMPLEMENTED\n", __func__ );
105 		break;
106 	case CMD_CLOSE_DATA_CHAN:
107 		IRDA_DEBUG(2, "Got CLOSE_DATA_CHAN command!\n");
108 		IRDA_DEBUG(2, "%s(), NOT IMPLEMENTED\n", __func__ );
109 		break;
110 	default:
111 		IRDA_DEBUG(2, "%s(), Unknown command!\n", __func__ );
112 		break;
113 	}
114 	return 0;
115 }
116 
117 /*
118  * Function irlan_provider_connect_indication (handle, skb, priv)
119  *
120  *    Got connection from peer IrLAN client
121  *
122  */
irlan_provider_connect_indication(void * instance,void * sap,struct qos_info * qos,__u32 max_sdu_size,__u8 max_header_size,struct sk_buff * skb)123 static void irlan_provider_connect_indication(void *instance, void *sap,
124 					      struct qos_info *qos,
125 					      __u32 max_sdu_size,
126 					      __u8 max_header_size,
127 					      struct sk_buff *skb)
128 {
129 	struct irlan_cb *self;
130 	struct tsap_cb *tsap;
131 	__u32 saddr, daddr;
132 
133 	IRDA_DEBUG(0, "%s()\n", __func__ );
134 
135 	self = (struct irlan_cb *) instance;
136 	tsap = (struct tsap_cb *) sap;
137 
138 	IRDA_ASSERT(self != NULL, return;);
139 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
140 
141 	IRDA_ASSERT(tsap == self->provider.tsap_ctrl,return;);
142 	IRDA_ASSERT(self->provider.state == IRLAN_IDLE, return;);
143 
144 	daddr = irttp_get_daddr(tsap);
145 	saddr = irttp_get_saddr(tsap);
146 	self->provider.max_sdu_size = max_sdu_size;
147 	self->provider.max_header_size = max_header_size;
148 
149 	irlan_do_provider_event(self, IRLAN_CONNECT_INDICATION, NULL);
150 
151 	/*
152 	 * If we are in peer mode, the client may not have got the discovery
153 	 * indication it needs to make progress. If the client is still in
154 	 * IDLE state, we must kick it.
155 	 */
156 	if ((self->provider.access_type == ACCESS_PEER) &&
157 	    (self->client.state == IRLAN_IDLE))
158 	{
159 		irlan_client_wakeup(self, self->saddr, self->daddr);
160 	}
161 }
162 
163 /*
164  * Function irlan_provider_connect_response (handle)
165  *
166  *    Accept incoming connection
167  *
168  */
irlan_provider_connect_response(struct irlan_cb * self,struct tsap_cb * tsap)169 void irlan_provider_connect_response(struct irlan_cb *self,
170 				     struct tsap_cb *tsap)
171 {
172 	IRDA_ASSERT(self != NULL, return;);
173 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
174 
175 	/* Just accept */
176 	irttp_connect_response(tsap, IRLAN_MTU, NULL);
177 }
178 
irlan_provider_disconnect_indication(void * instance,void * sap,LM_REASON reason,struct sk_buff * userdata)179 static void irlan_provider_disconnect_indication(void *instance, void *sap,
180 						 LM_REASON reason,
181 						 struct sk_buff *userdata)
182 {
183 	struct irlan_cb *self;
184 	struct tsap_cb *tsap;
185 
186 	IRDA_DEBUG(4, "%s(), reason=%d\n", __func__ , reason);
187 
188 	self = (struct irlan_cb *) instance;
189 	tsap = (struct tsap_cb *) sap;
190 
191 	IRDA_ASSERT(self != NULL, return;);
192 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
193 	IRDA_ASSERT(tsap != NULL, return;);
194 	IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
195 
196 	IRDA_ASSERT(tsap == self->provider.tsap_ctrl, return;);
197 
198 	irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
199 }
200 
201 /*
202  * Function irlan_parse_open_data_cmd (self, skb)
203  *
204  *
205  *
206  */
irlan_parse_open_data_cmd(struct irlan_cb * self,struct sk_buff * skb)207 int irlan_parse_open_data_cmd(struct irlan_cb *self, struct sk_buff *skb)
208 {
209 	int ret;
210 
211 	ret = irlan_provider_parse_command(self, CMD_OPEN_DATA_CHANNEL, skb);
212 
213 	/* Open data channel */
214 	irlan_open_data_tsap(self);
215 
216 	return ret;
217 }
218 
219 /*
220  * Function parse_command (skb)
221  *
222  *    Extract all parameters from received buffer, then feed them to
223  *    check_params for parsing
224  *
225  */
irlan_provider_parse_command(struct irlan_cb * self,int cmd,struct sk_buff * skb)226 int irlan_provider_parse_command(struct irlan_cb *self, int cmd,
227 				 struct sk_buff *skb)
228 {
229 	__u8 *frame;
230 	__u8 *ptr;
231 	int count;
232 	__u16 val_len;
233 	int i;
234 	char *name;
235 	char *value;
236 	int ret = RSP_SUCCESS;
237 
238 	IRDA_ASSERT(skb != NULL, return -RSP_PROTOCOL_ERROR;);
239 
240 	IRDA_DEBUG(4, "%s(), skb->len=%d\n", __func__ , (int)skb->len);
241 
242 	IRDA_ASSERT(self != NULL, return -RSP_PROTOCOL_ERROR;);
243 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -RSP_PROTOCOL_ERROR;);
244 
245 	if (!skb)
246 		return -RSP_PROTOCOL_ERROR;
247 
248 	frame = skb->data;
249 
250 	name = kmalloc(255, GFP_ATOMIC);
251 	if (!name)
252 		return -RSP_INSUFFICIENT_RESOURCES;
253 	value = kmalloc(1016, GFP_ATOMIC);
254 	if (!value) {
255 		kfree(name);
256 		return -RSP_INSUFFICIENT_RESOURCES;
257 	}
258 
259 	/* How many parameters? */
260 	count = frame[1];
261 
262 	IRDA_DEBUG(4, "Got %d parameters\n", count);
263 
264 	ptr = frame+2;
265 
266 	/* For all parameters */
267 	for (i=0; i<count;i++) {
268 		ret = irlan_extract_param(ptr, name, value, &val_len);
269 		if (ret < 0) {
270 			IRDA_DEBUG(2, "%s(), IrLAN, Error!\n", __func__ );
271 			break;
272 		}
273 		ptr+=ret;
274 		ret = RSP_SUCCESS;
275 		irlan_check_command_param(self, name, value);
276 	}
277 	/* Cleanup */
278 	kfree(name);
279 	kfree(value);
280 
281 	return ret;
282 }
283 
284 /*
285  * Function irlan_provider_send_reply (self, info)
286  *
287  *    Send reply to query to peer IrLAN layer
288  *
289  */
irlan_provider_send_reply(struct irlan_cb * self,int command,int ret_code)290 void irlan_provider_send_reply(struct irlan_cb *self, int command,
291 			       int ret_code)
292 {
293 	struct sk_buff *skb;
294 
295 	IRDA_DEBUG(4, "%s()\n", __func__ );
296 
297 	IRDA_ASSERT(self != NULL, return;);
298 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
299 
300 	skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
301 			/* Bigger param length comes from CMD_GET_MEDIA_CHAR */
302 			IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
303 			IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "BORADCAST") +
304 			IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "MULTICAST") +
305 			IRLAN_STRING_PARAMETER_LEN("ACCESS_TYPE", "HOSTED"),
306 			GFP_ATOMIC);
307 
308 	if (!skb)
309 		return;
310 
311 	/* Reserve space for TTP, LMP, and LAP header */
312 	skb_reserve(skb, self->provider.max_header_size);
313 	skb_put(skb, 2);
314 
315 	switch (command) {
316 	case CMD_GET_PROVIDER_INFO:
317 		skb->data[0] = 0x00; /* Success */
318 		skb->data[1] = 0x02; /* 2 parameters */
319 		switch (self->media) {
320 		case MEDIA_802_3:
321 			irlan_insert_string_param(skb, "MEDIA", "802.3");
322 			break;
323 		case MEDIA_802_5:
324 			irlan_insert_string_param(skb, "MEDIA", "802.5");
325 			break;
326 		default:
327 			IRDA_DEBUG(2, "%s(), unknown media type!\n", __func__ );
328 			break;
329 		}
330 		irlan_insert_short_param(skb, "IRLAN_VER", 0x0101);
331 		break;
332 
333 	case CMD_GET_MEDIA_CHAR:
334 		skb->data[0] = 0x00; /* Success */
335 		skb->data[1] = 0x05; /* 5 parameters */
336 		irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
337 		irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
338 		irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
339 
340 		switch (self->provider.access_type) {
341 		case ACCESS_DIRECT:
342 			irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
343 			break;
344 		case ACCESS_PEER:
345 			irlan_insert_string_param(skb, "ACCESS_TYPE", "PEER");
346 			break;
347 		case ACCESS_HOSTED:
348 			irlan_insert_string_param(skb, "ACCESS_TYPE", "HOSTED");
349 			break;
350 		default:
351 			IRDA_DEBUG(2, "%s(), Unknown access type\n", __func__ );
352 			break;
353 		}
354 		irlan_insert_short_param(skb, "MAX_FRAME", 0x05ee);
355 		break;
356 	case CMD_OPEN_DATA_CHANNEL:
357 		skb->data[0] = 0x00; /* Success */
358 		if (self->provider.send_arb_val) {
359 			skb->data[1] = 0x03; /* 3 parameters */
360 			irlan_insert_short_param(skb, "CON_ARB",
361 						 self->provider.send_arb_val);
362 		} else
363 			skb->data[1] = 0x02; /* 2 parameters */
364 		irlan_insert_byte_param(skb, "DATA_CHAN", self->stsap_sel_data);
365 		irlan_insert_string_param(skb, "RECONNECT_KEY", "LINUX RULES!");
366 		break;
367 	case CMD_FILTER_OPERATION:
368 		irlan_filter_request(self, skb);
369 		break;
370 	default:
371 		IRDA_DEBUG(2, "%s(), Unknown command!\n", __func__ );
372 		break;
373 	}
374 
375 	irttp_data_request(self->provider.tsap_ctrl, skb);
376 }
377 
378 /*
379  * Function irlan_provider_register(void)
380  *
381  *    Register provider support so we can accept incoming connections.
382  *
383  */
irlan_provider_open_ctrl_tsap(struct irlan_cb * self)384 int irlan_provider_open_ctrl_tsap(struct irlan_cb *self)
385 {
386 	struct tsap_cb *tsap;
387 	notify_t notify;
388 
389 	IRDA_DEBUG(4, "%s()\n", __func__ );
390 
391 	IRDA_ASSERT(self != NULL, return -1;);
392 	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
393 
394 	/* Check if already open */
395 	if (self->provider.tsap_ctrl)
396 		return -1;
397 
398 	/*
399 	 *  First register well known control TSAP
400 	 */
401 	irda_notify_init(&notify);
402 	notify.data_indication       = irlan_provider_data_indication;
403 	notify.connect_indication    = irlan_provider_connect_indication;
404 	notify.disconnect_indication = irlan_provider_disconnect_indication;
405 	notify.instance = self;
406 	strlcpy(notify.name, "IrLAN ctrl (p)", sizeof(notify.name));
407 
408 	tsap = irttp_open_tsap(LSAP_ANY, 1, &notify);
409 	if (!tsap) {
410 		IRDA_DEBUG(2, "%s(), Got no tsap!\n", __func__ );
411 		return -1;
412 	}
413 	self->provider.tsap_ctrl = tsap;
414 
415 	/* Register with LM-IAS */
416 	irlan_ias_register(self, tsap->stsap_sel);
417 
418 	return 0;
419 }
420 
421