blob: b8af74ab8b689254312fa87e699ff3f7f5c6402a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090014 *
15 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090017 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * the License, or (at your option) any later version.
22 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020023 * Neither Dag Brattli nor University of Tromsø admit liability nor
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090024 * provide warranty for any of this software. This material is
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090056static void irlan_provider_connect_indication(void *instance, void *sap,
57 struct qos_info *qos,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 __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 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090068static int irlan_provider_data_indication(void *instance, void *sap,
69 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct irlan_cb *self;
72 __u8 code;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090073
Harvey Harrison0dc47872008-03-05 20:47:47 -080074 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 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");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090087 irlan_do_provider_event(self, IRLAN_GET_INFO_CMD, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
89
90 case CMD_GET_MEDIA_CHAR:
91 IRDA_DEBUG(4, "Got GET_MEDIA_CHAR command!\n");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090092 irlan_do_provider_event(self, IRLAN_GET_MEDIA_CMD, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 case CMD_OPEN_DATA_CHANNEL:
95 IRDA_DEBUG(4, "Got OPEN_DATA_CHANNEL command!\n");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090096 irlan_do_provider_event(self, IRLAN_OPEN_DATA_CMD, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 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:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800103 IRDA_DEBUG(2, "%s(), Got RECONNECT_DATA_CHAN command\n", __func__ );
104 IRDA_DEBUG(2, "%s(), NOT IMPLEMENTED\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
106 case CMD_CLOSE_DATA_CHAN:
107 IRDA_DEBUG(2, "Got CLOSE_DATA_CHAN command!\n");
Harvey Harrison0dc47872008-03-05 20:47:47 -0800108 IRDA_DEBUG(2, "%s(), NOT IMPLEMENTED\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800111 IRDA_DEBUG(2, "%s(), Unknown command!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 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 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900123static void irlan_provider_connect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct qos_info *qos,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900125 __u32 max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __u8 max_header_size,
127 struct sk_buff *skb)
128{
129 struct irlan_cb *self;
130 struct tsap_cb *tsap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Harvey Harrison0dc47872008-03-05 20:47:47 -0800132 IRDA_DEBUG(0, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 self = (struct irlan_cb *) instance;
135 tsap = (struct tsap_cb *) sap;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 IRDA_ASSERT(self != NULL, return;);
138 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 IRDA_ASSERT(tsap == self->provider.tsap_ctrl,return;);
141 IRDA_ASSERT(self->provider.state == IRLAN_IDLE, return;);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 self->provider.max_sdu_size = max_sdu_size;
144 self->provider.max_header_size = max_header_size;
145
146 irlan_do_provider_event(self, IRLAN_CONNECT_INDICATION, NULL);
147
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * If we are in peer mode, the client may not have got the discovery
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900150 * indication it needs to make progress. If the client is still in
151 * IDLE state, we must kick it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900153 if ((self->provider.access_type == ACCESS_PEER) &&
154 (self->client.state == IRLAN_IDLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 {
156 irlan_client_wakeup(self, self->saddr, self->daddr);
157 }
158}
159
160/*
161 * Function irlan_provider_connect_response (handle)
162 *
163 * Accept incoming connection
164 *
165 */
166void irlan_provider_connect_response(struct irlan_cb *self,
167 struct tsap_cb *tsap)
168{
169 IRDA_ASSERT(self != NULL, return;);
170 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
171
172 /* Just accept */
173 irttp_connect_response(tsap, IRLAN_MTU, NULL);
174}
175
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900176static void irlan_provider_disconnect_indication(void *instance, void *sap,
177 LM_REASON reason,
178 struct sk_buff *userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct irlan_cb *self;
181 struct tsap_cb *tsap;
182
Harvey Harrison0dc47872008-03-05 20:47:47 -0800183 IRDA_DEBUG(4, "%s(), reason=%d\n", __func__ , reason);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 self = (struct irlan_cb *) instance;
186 tsap = (struct tsap_cb *) sap;
187
188 IRDA_ASSERT(self != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900189 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 IRDA_ASSERT(tsap != NULL, return;);
191 IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 IRDA_ASSERT(tsap == self->provider.tsap_ctrl, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
196}
197
198/*
199 * Function irlan_parse_open_data_cmd (self, skb)
200 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900201 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
203 */
204int irlan_parse_open_data_cmd(struct irlan_cb *self, struct sk_buff *skb)
205{
206 int ret;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ret = irlan_provider_parse_command(self, CMD_OPEN_DATA_CHANNEL, skb);
209
210 /* Open data channel */
211 irlan_open_data_tsap(self);
212
213 return ret;
214}
215
216/*
217 * Function parse_command (skb)
218 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900219 * Extract all parameters from received buffer, then feed them to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * check_params for parsing
221 *
222 */
223int irlan_provider_parse_command(struct irlan_cb *self, int cmd,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900224 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 __u8 *frame;
227 __u8 *ptr;
228 int count;
229 __u16 val_len;
230 int i;
231 char *name;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900232 char *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int ret = RSP_SUCCESS;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 IRDA_ASSERT(skb != NULL, return -RSP_PROTOCOL_ERROR;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900236
Harvey Harrison0dc47872008-03-05 20:47:47 -0800237 IRDA_DEBUG(4, "%s(), skb->len=%d\n", __func__ , (int)skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 IRDA_ASSERT(self != NULL, return -RSP_PROTOCOL_ERROR;);
240 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -RSP_PROTOCOL_ERROR;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!skb)
243 return -RSP_PROTOCOL_ERROR;
244
245 frame = skb->data;
246
247 name = kmalloc(255, GFP_ATOMIC);
248 if (!name)
249 return -RSP_INSUFFICIENT_RESOURCES;
250 value = kmalloc(1016, GFP_ATOMIC);
251 if (!value) {
252 kfree(name);
253 return -RSP_INSUFFICIENT_RESOURCES;
254 }
255
256 /* How many parameters? */
257 count = frame[1];
258
259 IRDA_DEBUG(4, "Got %d parameters\n", count);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ptr = frame+2;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* For all parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900264 for (i=0; i<count;i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ret = irlan_extract_param(ptr, name, value, &val_len);
266 if (ret < 0) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800267 IRDA_DEBUG(2, "%s(), IrLAN, Error!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269 }
270 ptr+=ret;
271 ret = RSP_SUCCESS;
272 irlan_check_command_param(self, name, value);
273 }
274 /* Cleanup */
275 kfree(name);
276 kfree(value);
277
278 return ret;
279}
280
281/*
282 * Function irlan_provider_send_reply (self, info)
283 *
284 * Send reply to query to peer IrLAN layer
285 *
286 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900287void irlan_provider_send_reply(struct irlan_cb *self, int command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int ret_code)
289{
290 struct sk_buff *skb;
291
Harvey Harrison0dc47872008-03-05 20:47:47 -0800292 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 IRDA_ASSERT(self != NULL, return;);
295 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
296
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700297 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
298 /* Bigger param length comes from CMD_GET_MEDIA_CHAR */
299 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
300 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "BORADCAST") +
301 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "MULTICAST") +
302 IRLAN_STRING_PARAMETER_LEN("ACCESS_TYPE", "HOSTED"),
303 GFP_ATOMIC);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!skb)
306 return;
307
308 /* Reserve space for TTP, LMP, and LAP header */
309 skb_reserve(skb, self->provider.max_header_size);
310 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (command) {
313 case CMD_GET_PROVIDER_INFO:
314 skb->data[0] = 0x00; /* Success */
315 skb->data[1] = 0x02; /* 2 parameters */
316 switch (self->media) {
317 case MEDIA_802_3:
318 irlan_insert_string_param(skb, "MEDIA", "802.3");
319 break;
320 case MEDIA_802_5:
321 irlan_insert_string_param(skb, "MEDIA", "802.5");
322 break;
323 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800324 IRDA_DEBUG(2, "%s(), unknown media type!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 }
327 irlan_insert_short_param(skb, "IRLAN_VER", 0x0101);
328 break;
329
330 case CMD_GET_MEDIA_CHAR:
331 skb->data[0] = 0x00; /* Success */
332 skb->data[1] = 0x05; /* 5 parameters */
333 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
334 irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
335 irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
336
337 switch (self->provider.access_type) {
338 case ACCESS_DIRECT:
339 irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
340 break;
341 case ACCESS_PEER:
342 irlan_insert_string_param(skb, "ACCESS_TYPE", "PEER");
343 break;
344 case ACCESS_HOSTED:
345 irlan_insert_string_param(skb, "ACCESS_TYPE", "HOSTED");
346 break;
347 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800348 IRDA_DEBUG(2, "%s(), Unknown access type\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 }
351 irlan_insert_short_param(skb, "MAX_FRAME", 0x05ee);
352 break;
353 case CMD_OPEN_DATA_CHANNEL:
354 skb->data[0] = 0x00; /* Success */
355 if (self->provider.send_arb_val) {
356 skb->data[1] = 0x03; /* 3 parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900357 irlan_insert_short_param(skb, "CON_ARB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 self->provider.send_arb_val);
359 } else
360 skb->data[1] = 0x02; /* 2 parameters */
361 irlan_insert_byte_param(skb, "DATA_CHAN", self->stsap_sel_data);
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700362 irlan_insert_string_param(skb, "RECONNECT_KEY", "LINUX RULES!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 case CMD_FILTER_OPERATION:
365 irlan_filter_request(self, skb);
366 break;
367 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800368 IRDA_DEBUG(2, "%s(), Unknown command!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370 }
371
372 irttp_data_request(self->provider.tsap_ctrl, skb);
373}
374
375/*
376 * Function irlan_provider_register(void)
377 *
378 * Register provider support so we can accept incoming connections.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900379 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
381int irlan_provider_open_ctrl_tsap(struct irlan_cb *self)
382{
383 struct tsap_cb *tsap;
384 notify_t notify;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900385
Harvey Harrison0dc47872008-03-05 20:47:47 -0800386 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 IRDA_ASSERT(self != NULL, return -1;);
389 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
390
391 /* Check if already open */
392 if (self->provider.tsap_ctrl)
393 return -1;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * First register well known control TSAP
397 */
398 irda_notify_init(&notify);
399 notify.data_indication = irlan_provider_data_indication;
400 notify.connect_indication = irlan_provider_connect_indication;
401 notify.disconnect_indication = irlan_provider_disconnect_indication;
402 notify.instance = self;
403 strlcpy(notify.name, "IrLAN ctrl (p)", sizeof(notify.name));
404
405 tsap = irttp_open_tsap(LSAP_ANY, 1, &notify);
406 if (!tsap) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800407 IRDA_DEBUG(2, "%s(), Got no tsap!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -1;
409 }
410 self->provider.tsap_ctrl = tsap;
411
412 /* Register with LM-IAS */
413 irlan_ias_register(self, tsap->stsap_sel);
414
415 return 0;
416}
417