1/*********************************************************************
2 *
3 * Filename:      irlan_client.c
4 * Version:       0.9
5 * Description:   IrDA LAN Access Protocol (IrLAN) Client
6 * Status:        Experimental.
7 * Author:        Dag Brattli <dagb@cs.uit.no>
8 * Created at:    Sun Aug 31 20:14:37 1997
9 * Modified at:   Tue Dec 14 15:47:02 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/init.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/if_arp.h>
36#include <linux/bitops.h>
37#include <net/arp.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_event.h>
51#include <net/irda/irlan_eth.h>
52#include <net/irda/irlan_provider.h>
53#include <net/irda/irlan_client.h>
54
55#undef CONFIG_IRLAN_GRATUITOUS_ARP
56
57static void irlan_client_ctrl_disconnect_indication(void *instance, void *sap,
58						    LM_REASON reason,
59						    struct sk_buff *);
60static int irlan_client_ctrl_data_indication(void *instance, void *sap,
61					     struct sk_buff *skb);
62static void irlan_client_ctrl_connect_confirm(void *instance, void *sap,
63					      struct qos_info *qos,
64					      __u32 max_sdu_size,
65					      __u8 max_header_size,
66					      struct sk_buff *);
67static void irlan_check_response_param(struct irlan_cb *self, char *param,
68				       char *value, int val_len);
69static void irlan_client_open_ctrl_tsap(struct irlan_cb *self);
70
71static void irlan_client_kick_timer_expired(void *data)
72{
73	struct irlan_cb *self = (struct irlan_cb *) data;
74
75	IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
76
77	IRDA_ASSERT(self != NULL, return;);
78	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
79
80	/*
81	 * If we are in peer mode, the client may not have got the discovery
82	 * indication it needs to make progress. If the client is still in
83	 * IDLE state, we must kick it to, but only if the provider is not IDLE
84	 */
85	if ((self->provider.access_type == ACCESS_PEER) &&
86	    (self->client.state == IRLAN_IDLE) &&
87	    (self->provider.state != IRLAN_IDLE)) {
88		irlan_client_wakeup(self, self->saddr, self->daddr);
89	}
90}
91
92static void irlan_client_start_kick_timer(struct irlan_cb *self, int timeout)
93{
94	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
95
96	irda_start_timer(&self->client.kick_timer, timeout, (void *) self,
97			 irlan_client_kick_timer_expired);
98}
99
100/*
101 * Function irlan_client_wakeup (self, saddr, daddr)
102 *
103 *    Wake up client
104 *
105 */
106void irlan_client_wakeup(struct irlan_cb *self, __u32 saddr, __u32 daddr)
107{
108	IRDA_DEBUG(1, "%s()\n", __FUNCTION__ );
109
110	IRDA_ASSERT(self != NULL, return;);
111	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
112
113	/*
114	 * Check if we are already awake, or if we are a provider in direct
115	 * mode (in that case we must leave the client idle
116	 */
117	if ((self->client.state != IRLAN_IDLE) ||
118	    (self->provider.access_type == ACCESS_DIRECT))
119	{
120			IRDA_DEBUG(0, "%s(), already awake!\n", __FUNCTION__ );
121			return;
122	}
123
124	/* Addresses may have changed! */
125	self->saddr = saddr;
126	self->daddr = daddr;
127
128	if (self->disconnect_reason == LM_USER_REQUEST) {
129			IRDA_DEBUG(0, "%s(), still stopped by user\n", __FUNCTION__ );
130			return;
131	}
132
133	/* Open TSAPs */
134	irlan_client_open_ctrl_tsap(self);
135	irlan_open_data_tsap(self);
136
137	irlan_do_client_event(self, IRLAN_DISCOVERY_INDICATION, NULL);
138
139	/* Start kick timer */
140	irlan_client_start_kick_timer(self, 2*HZ);
141}
142
143/*
144 * Function irlan_discovery_indication (daddr)
145 *
146 *    Remote device with IrLAN server support discovered
147 *
148 */
149void irlan_client_discovery_indication(discinfo_t *discovery,
150				       DISCOVERY_MODE mode,
151				       void *priv)
152{
153	struct irlan_cb *self;
154	__u32 saddr, daddr;
155
156	IRDA_DEBUG(1, "%s()\n", __FUNCTION__ );
157
158	IRDA_ASSERT(discovery != NULL, return;);
159
160	if(mode == DISCOVERY_PASSIVE)
161		return;
162
163	saddr = discovery->saddr;
164	daddr = discovery->daddr;
165
166	/* Find instance */
167	rcu_read_lock();
168	self = irlan_get_any();
169	if (self) {
170		IRDA_ASSERT(self->magic == IRLAN_MAGIC, goto out;);
171
172		IRDA_DEBUG(1, "%s(), Found instance (%08x)!\n", __FUNCTION__ ,
173		      daddr);
174
175		irlan_client_wakeup(self, saddr, daddr);
176	}
177IRDA_ASSERT_LABEL(out:)
178	rcu_read_unlock();
179}
180
181/*
182 * Function irlan_client_data_indication (handle, skb)
183 *
184 *    This function gets the data that is received on the control channel
185 *
186 */
187static int irlan_client_ctrl_data_indication(void *instance, void *sap,
188					     struct sk_buff *skb)
189{
190	struct irlan_cb *self;
191
192	IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
193
194	self = (struct irlan_cb *) instance;
195
196	IRDA_ASSERT(self != NULL, return -1;);
197	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
198	IRDA_ASSERT(skb != NULL, return -1;);
199
200	irlan_do_client_event(self, IRLAN_DATA_INDICATION, skb);
201
202	/* Ready for a new command */
203	IRDA_DEBUG(2, "%s(), clearing tx_busy\n", __FUNCTION__ );
204	self->client.tx_busy = FALSE;
205
206	/* Check if we have some queued commands waiting to be sent */
207	irlan_run_ctrl_tx_queue(self);
208
209	return 0;
210}
211
212static void irlan_client_ctrl_disconnect_indication(void *instance, void *sap,
213						    LM_REASON reason,
214						    struct sk_buff *userdata)
215{
216	struct irlan_cb *self;
217	struct tsap_cb *tsap;
218	struct sk_buff *skb;
219
220	IRDA_DEBUG(4, "%s(), reason=%d\n", __FUNCTION__ , reason);
221
222	self = (struct irlan_cb *) instance;
223	tsap = (struct tsap_cb *) sap;
224
225	IRDA_ASSERT(self != NULL, return;);
226	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
227	IRDA_ASSERT(tsap != NULL, return;);
228	IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
229
230	IRDA_ASSERT(tsap == self->client.tsap_ctrl, return;);
231
232	/* Remove frames queued on the control channel */
233	while ((skb = skb_dequeue(&self->client.txq)) != NULL) {
234		dev_kfree_skb(skb);
235	}
236	self->client.tx_busy = FALSE;
237
238	irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
239}
240
241/*
242 * Function irlan_client_open_tsaps (self)
243 *
244 *    Initialize callbacks and open IrTTP TSAPs
245 *
246 */
247static void irlan_client_open_ctrl_tsap(struct irlan_cb *self)
248{
249	struct tsap_cb *tsap;
250	notify_t notify;
251
252	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
253
254	IRDA_ASSERT(self != NULL, return;);
255	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
256
257	/* Check if already open */
258	if (self->client.tsap_ctrl)
259		return;
260
261	irda_notify_init(&notify);
262
263	/* Set up callbacks */
264	notify.data_indication       = irlan_client_ctrl_data_indication;
265	notify.connect_confirm       = irlan_client_ctrl_connect_confirm;
266	notify.disconnect_indication = irlan_client_ctrl_disconnect_indication;
267	notify.instance = self;
268	strlcpy(notify.name, "IrLAN ctrl (c)", sizeof(notify.name));
269
270	tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
271	if (!tsap) {
272		IRDA_DEBUG(2, "%s(), Got no tsap!\n", __FUNCTION__ );
273		return;
274	}
275	self->client.tsap_ctrl = tsap;
276}
277
278/*
279 * Function irlan_client_connect_confirm (handle, skb)
280 *
281 *    Connection to peer IrLAN laye confirmed
282 *
283 */
284static void irlan_client_ctrl_connect_confirm(void *instance, void *sap,
285					      struct qos_info *qos,
286					      __u32 max_sdu_size,
287					      __u8 max_header_size,
288					      struct sk_buff *skb)
289{
290	struct irlan_cb *self;
291
292	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
293
294	self = (struct irlan_cb *) instance;
295
296	IRDA_ASSERT(self != NULL, return;);
297	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
298
299	self->client.max_sdu_size = max_sdu_size;
300	self->client.max_header_size = max_header_size;
301
302	/* TODO: we could set the MTU depending on the max_sdu_size */
303
304	irlan_do_client_event(self, IRLAN_CONNECT_COMPLETE, NULL);
305}
306
307/*
308 * Function print_ret_code (code)
309 *
310 *    Print return code of request to peer IrLAN layer.
311 *
312 */
313static void print_ret_code(__u8 code)
314{
315	switch(code) {
316	case 0:
317		printk(KERN_INFO "Success\n");
318		break;
319	case 1:
320		IRDA_WARNING("IrLAN: Insufficient resources\n");
321		break;
322	case 2:
323		IRDA_WARNING("IrLAN: Invalid command format\n");
324		break;
325	case 3:
326		IRDA_WARNING("IrLAN: Command not supported\n");
327		break;
328	case 4:
329		IRDA_WARNING("IrLAN: Parameter not supported\n");
330		break;
331	case 5:
332		IRDA_WARNING("IrLAN: Value not supported\n");
333		break;
334	case 6:
335		IRDA_WARNING("IrLAN: Not open\n");
336		break;
337	case 7:
338		IRDA_WARNING("IrLAN: Authentication required\n");
339		break;
340	case 8:
341		IRDA_WARNING("IrLAN: Invalid password\n");
342		break;
343	case 9:
344		IRDA_WARNING("IrLAN: Protocol error\n");
345		break;
346	case 255:
347		IRDA_WARNING("IrLAN: Asynchronous status\n");
348		break;
349	}
350}
351
352/*
353 * Function irlan_client_parse_response (self, skb)
354 *
355 *    Extract all parameters from received buffer, then feed them to
356 *    check_params for parsing
357 */
358void irlan_client_parse_response(struct irlan_cb *self, struct sk_buff *skb)
359{
360	__u8 *frame;
361	__u8 *ptr;
362	int count;
363	int ret;
364	__u16 val_len;
365	int i;
366	char *name;
367	char *value;
368
369	IRDA_ASSERT(skb != NULL, return;);
370
371	IRDA_DEBUG(4, "%s() skb->len=%d\n", __FUNCTION__ , (int) skb->len);
372
373	IRDA_ASSERT(self != NULL, return;);
374	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
375
376	if (!skb) {
377		IRDA_ERROR("%s(), Got NULL skb!\n", __FUNCTION__);
378		return;
379	}
380	frame = skb->data;
381
382	/*
383	 *  Check return code and print it if not success
384	 */
385	if (frame[0]) {
386		print_ret_code(frame[0]);
387		return;
388	}
389
390	name = kmalloc(255, GFP_ATOMIC);
391	if (!name)
392		return;
393	value = kmalloc(1016, GFP_ATOMIC);
394	if (!value) {
395		kfree(name);
396		return;
397	}
398
399	/* How many parameters? */
400	count = frame[1];
401
402	IRDA_DEBUG(4, "%s(), got %d parameters\n", __FUNCTION__ , count);
403
404	ptr = frame+2;
405
406	/* For all parameters */
407	for (i=0; i<count;i++) {
408		ret = irlan_extract_param(ptr, name, value, &val_len);
409		if (ret < 0) {
410			IRDA_DEBUG(2, "%s(), IrLAN, Error!\n", __FUNCTION__ );
411			break;
412		}
413		ptr += ret;
414		irlan_check_response_param(self, name, value, val_len);
415	}
416	/* Cleanup */
417	kfree(name);
418	kfree(value);
419}
420
421/*
422 * Function irlan_check_response_param (self, param, value, val_len)
423 *
424 *     Check which parameter is received and update local variables
425 *
426 */
427static void irlan_check_response_param(struct irlan_cb *self, char *param,
428				       char *value, int val_len)
429{
430	__u16 tmp_cpu; /* Temporary value in host order */
431	__u8 *bytes;
432	int i;
433
434	IRDA_DEBUG(4, "%s(), parm=%s\n", __FUNCTION__ , param);
435
436	IRDA_ASSERT(self != NULL, return;);
437	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
438
439	/* Media type */
440	if (strcmp(param, "MEDIA") == 0) {
441		if (strcmp(value, "802.3") == 0)
442			self->media = MEDIA_802_3;
443		else
444			self->media = MEDIA_802_5;
445		return;
446	}
447	if (strcmp(param, "FILTER_TYPE") == 0) {
448		if (strcmp(value, "DIRECTED") == 0)
449			self->client.filter_type |= IRLAN_DIRECTED;
450		else if (strcmp(value, "FUNCTIONAL") == 0)
451			self->client.filter_type |= IRLAN_FUNCTIONAL;
452		else if (strcmp(value, "GROUP") == 0)
453			self->client.filter_type |= IRLAN_GROUP;
454		else if (strcmp(value, "MAC_FRAME") == 0)
455			self->client.filter_type |= IRLAN_MAC_FRAME;
456		else if (strcmp(value, "MULTICAST") == 0)
457			self->client.filter_type |= IRLAN_MULTICAST;
458		else if (strcmp(value, "BROADCAST") == 0)
459			self->client.filter_type |= IRLAN_BROADCAST;
460		else if (strcmp(value, "IPX_SOCKET") == 0)
461			self->client.filter_type |= IRLAN_IPX_SOCKET;
462
463	}
464	if (strcmp(param, "ACCESS_TYPE") == 0) {
465		if (strcmp(value, "DIRECT") == 0)
466			self->client.access_type = ACCESS_DIRECT;
467		else if (strcmp(value, "PEER") == 0)
468			self->client.access_type = ACCESS_PEER;
469		else if (strcmp(value, "HOSTED") == 0)
470			self->client.access_type = ACCESS_HOSTED;
471		else {
472			IRDA_DEBUG(2, "%s(), unknown access type!\n", __FUNCTION__ );
473		}
474	}
475	/* IRLAN version */
476	if (strcmp(param, "IRLAN_VER") == 0) {
477		IRDA_DEBUG(4, "IrLAN version %d.%d\n", (__u8) value[0],
478		      (__u8) value[1]);
479
480		self->version[0] = value[0];
481		self->version[1] = value[1];
482		return;
483	}
484	/* Which remote TSAP to use for data channel */
485	if (strcmp(param, "DATA_CHAN") == 0) {
486		self->dtsap_sel_data = value[0];
487		IRDA_DEBUG(4, "Data TSAP = %02x\n", self->dtsap_sel_data);
488		return;
489	}
490	if (strcmp(param, "CON_ARB") == 0) {
491		memcpy(&tmp_cpu, value, 2); /* Align value */
492		le16_to_cpus(&tmp_cpu);     /* Convert to host order */
493		self->client.recv_arb_val = tmp_cpu;
494		IRDA_DEBUG(2, "%s(), receive arb val=%d\n", __FUNCTION__ ,
495			   self->client.recv_arb_val);
496	}
497	if (strcmp(param, "MAX_FRAME") == 0) {
498		memcpy(&tmp_cpu, value, 2); /* Align value */
499		le16_to_cpus(&tmp_cpu);     /* Convert to host order */
500		self->client.max_frame = tmp_cpu;
501		IRDA_DEBUG(4, "%s(), max frame=%d\n", __FUNCTION__ ,
502			   self->client.max_frame);
503	}
504
505	/* RECONNECT_KEY, in case the link goes down! */
506	if (strcmp(param, "RECONNECT_KEY") == 0) {
507		IRDA_DEBUG(4, "Got reconnect key: ");
508		/* for (i = 0; i < val_len; i++) */
509/* 			printk("%02x", value[i]); */
510		memcpy(self->client.reconnect_key, value, val_len);
511		self->client.key_len = val_len;
512		IRDA_DEBUG(4, "\n");
513	}
514	/* FILTER_ENTRY, have we got an ethernet address? */
515	if (strcmp(param, "FILTER_ENTRY") == 0) {
516		bytes = value;
517		IRDA_DEBUG(4, "Ethernet address = %02x:%02x:%02x:%02x:%02x:%02x\n",
518		      bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
519		      bytes[5]);
520		for (i = 0; i < 6; i++)
521			self->dev->dev_addr[i] = bytes[i];
522	}
523}
524
525/*
526 * Function irlan_client_get_value_confirm (obj_id, value)
527 *
528 *    Got results from remote LM-IAS
529 *
530 */
531void irlan_client_get_value_confirm(int result, __u16 obj_id,
532				    struct ias_value *value, void *priv)
533{
534	struct irlan_cb *self;
535
536	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
537
538	IRDA_ASSERT(priv != NULL, return;);
539
540	self = (struct irlan_cb *) priv;
541	IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
542
543	/* We probably don't need to make any more queries */
544	iriap_close(self->client.iriap);
545	self->client.iriap = NULL;
546
547	/* Check if request succeeded */
548	if (result != IAS_SUCCESS) {
549		IRDA_DEBUG(2, "%s(), got NULL value!\n", __FUNCTION__ );
550		irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL,
551				      NULL);
552		return;
553	}
554
555	switch (value->type) {
556	case IAS_INTEGER:
557		self->dtsap_sel_ctrl = value->t.integer;
558
559		if (value->t.integer != -1) {
560			irlan_do_client_event(self, IRLAN_IAS_PROVIDER_AVAIL,
561					      NULL);
562			return;
563		}
564		irias_delete_value(value);
565		break;
566	default:
567		IRDA_DEBUG(2, "%s(), unknown type!\n", __FUNCTION__ );
568		break;
569	}
570	irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL, NULL);
571}
572