1 /*********************************************************************
2  *
3  * Filename:      discovery.c
4  * Version:       0.1
5  * Description:   Routines for handling discoveries at the IrLMP layer
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Tue Apr  6 15:33:50 1999
9  * Modified at:   Sat Oct  9 17:11:31 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Modified at:   Fri May 28  3:11 CST 1999
12  * Modified by:   Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
13  *
14  *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
15  *
16  *     This program is free software; you can redistribute it and/or
17  *     modify it under the terms of the GNU General Public License as
18  *     published by the Free Software Foundation; either version 2 of
19  *     the License, or (at your option) any later version.
20  *
21  *     This program is distributed in the hope that it will be useful,
22  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  *     GNU General Public License for more details.
25  *
26  *     You should have received a copy of the GNU General Public License
27  *     along with this program; if not, write to the Free Software
28  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  *     MA 02111-1307 USA
30  *
31  ********************************************************************/
32 
33 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 
39 #include <net/irda/irda.h>
40 #include <net/irda/irlmp.h>
41 
42 #include <net/irda/discovery.h>
43 
44 #include <asm/unaligned.h>
45 
46 /*
47  * Function irlmp_add_discovery (cachelog, discovery)
48  *
49  *    Add a new discovery to the cachelog, and remove any old discoveries
50  *    from the same device
51  *
52  * Note : we try to preserve the time this device was *first* discovered
53  * (as opposed to the time of last discovery used for cleanup). This is
54  * used by clients waiting for discovery events to tell if the device
55  * discovered is "new" or just the same old one. They can't rely there
56  * on a binary flag (new/old), because not all discovery events are
57  * propagated to them, and they might not always listen, so they would
58  * miss some new devices popping up...
59  * Jean II
60  */
irlmp_add_discovery(hashbin_t * cachelog,discovery_t * new)61 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
62 {
63 	discovery_t *discovery, *node;
64 	unsigned long flags;
65 
66 	/* Set time of first discovery if node is new (see below) */
67 	new->firststamp = new->timestamp;
68 
69 	spin_lock_irqsave(&cachelog->hb_spinlock, flags);
70 
71 	/*
72 	 * Remove all discoveries of devices that has previously been
73 	 * discovered on the same link with the same name (info), or the
74 	 * same daddr. We do this since some devices (mostly PDAs) change
75 	 * their device address between every discovery.
76 	 */
77 	discovery = (discovery_t *) hashbin_get_first(cachelog);
78 	while (discovery != NULL ) {
79 		node = discovery;
80 
81 		/* Be sure to stay one item ahead */
82 		discovery = (discovery_t *) hashbin_get_next(cachelog);
83 
84 		if ((node->data.saddr == new->data.saddr) &&
85 		    ((node->data.daddr == new->data.daddr) ||
86 		     (strcmp(node->data.info, new->data.info) == 0)))
87 		{
88 			/* This discovery is a previous discovery
89 			 * from the same device, so just remove it
90 			 */
91 			hashbin_remove_this(cachelog, (irda_queue_t *) node);
92 			/* Check if hints bits are unchanged */
93 			if (get_unaligned((__u16 *)node->data.hints) == get_unaligned((__u16 *)new->data.hints))
94 				/* Set time of first discovery for this node */
95 				new->firststamp = node->firststamp;
96 			kfree(node);
97 		}
98 	}
99 
100 	/* Insert the new and updated version */
101 	hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
102 
103 	spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
104 }
105 
106 /*
107  * Function irlmp_add_discovery_log (cachelog, log)
108  *
109  *    Merge a disovery log into the cachelog.
110  *
111  */
irlmp_add_discovery_log(hashbin_t * cachelog,hashbin_t * log)112 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
113 {
114 	discovery_t *discovery;
115 
116 	IRDA_DEBUG(4, "%s()\n", __func__);
117 
118 	/*
119 	 *  If log is missing this means that IrLAP was unable to perform the
120 	 *  discovery, so restart discovery again with just the half timeout
121 	 *  of the normal one.
122 	 */
123 	/* Well... It means that there was nobody out there - Jean II */
124 	if (log == NULL) {
125 		/* irlmp_start_discovery_timer(irlmp, 150); */
126 		return;
127 	}
128 
129 	/*
130 	 * Locking : we are the only owner of this discovery log, so
131 	 * no need to lock it.
132 	 * We just need to lock the global log in irlmp_add_discovery().
133 	 */
134 	discovery = (discovery_t *) hashbin_remove_first(log);
135 	while (discovery != NULL) {
136 		irlmp_add_discovery(cachelog, discovery);
137 
138 		discovery = (discovery_t *) hashbin_remove_first(log);
139 	}
140 
141 	/* Delete the now empty log */
142 	hashbin_delete(log, (FREE_FUNC) kfree);
143 }
144 
145 /*
146  * Function irlmp_expire_discoveries (log, saddr, force)
147  *
148  *    Go through all discoveries and expire all that has stayed too long
149  *
150  * Note : this assume that IrLAP won't change its saddr, which
151  * currently is a valid assumption...
152  */
irlmp_expire_discoveries(hashbin_t * log,__u32 saddr,int force)153 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
154 {
155 	discovery_t *		discovery;
156 	discovery_t *		curr;
157 	unsigned long		flags;
158 	discinfo_t *		buffer = NULL;
159 	int			n;		/* Size of the full log */
160 	int			i = 0;		/* How many we expired */
161 
162 	IRDA_ASSERT(log != NULL, return;);
163 	IRDA_DEBUG(4, "%s()\n", __func__);
164 
165 	spin_lock_irqsave(&log->hb_spinlock, flags);
166 
167 	discovery = (discovery_t *) hashbin_get_first(log);
168 	while (discovery != NULL) {
169 		/* Be sure to be one item ahead */
170 		curr = discovery;
171 		discovery = (discovery_t *) hashbin_get_next(log);
172 
173 		/* Test if it's time to expire this discovery */
174 		if ((curr->data.saddr == saddr) &&
175 		    (force ||
176 		     ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
177 		{
178 			/* Create buffer as needed.
179 			 * As this function get called a lot and most time
180 			 * we don't have anything to put in the log (we are
181 			 * quite picky), we can save a lot of overhead
182 			 * by not calling kmalloc. Jean II */
183 			if(buffer == NULL) {
184 				/* Create the client specific buffer */
185 				n = HASHBIN_GET_SIZE(log);
186 				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
187 				if (buffer == NULL) {
188 					spin_unlock_irqrestore(&log->hb_spinlock, flags);
189 					return;
190 				}
191 
192 			}
193 
194 			/* Copy discovery information */
195 			memcpy(&(buffer[i]), &(curr->data),
196 			       sizeof(discinfo_t));
197 			i++;
198 
199 			/* Remove it from the log */
200 			curr = hashbin_remove_this(log, (irda_queue_t *) curr);
201 			kfree(curr);
202 		}
203 	}
204 
205 	/* Drop the spinlock before calling the higher layers, as
206 	 * we can't guarantee they won't call us back and create a
207 	 * deadlock. We will work on our own private data, so we
208 	 * don't care to be interrupted. - Jean II */
209 	spin_unlock_irqrestore(&log->hb_spinlock, flags);
210 
211 	if(buffer == NULL)
212 		return;
213 
214 	/* Tell IrLMP and registered clients about it */
215 	irlmp_discovery_expiry(buffer, i);
216 
217 	/* Free up our buffer */
218 	kfree(buffer);
219 }
220 
221 #if 0
222 /*
223  * Function irlmp_dump_discoveries (log)
224  *
225  *    Print out all discoveries in log
226  *
227  */
228 void irlmp_dump_discoveries(hashbin_t *log)
229 {
230 	discovery_t *discovery;
231 
232 	IRDA_ASSERT(log != NULL, return;);
233 
234 	discovery = (discovery_t *) hashbin_get_first(log);
235 	while (discovery != NULL) {
236 		IRDA_DEBUG(0, "Discovery:\n");
237 		IRDA_DEBUG(0, "  daddr=%08x\n", discovery->data.daddr);
238 		IRDA_DEBUG(0, "  saddr=%08x\n", discovery->data.saddr);
239 		IRDA_DEBUG(0, "  nickname=%s\n", discovery->data.info);
240 
241 		discovery = (discovery_t *) hashbin_get_next(log);
242 	}
243 }
244 #endif
245 
246 /*
247  * Function irlmp_copy_discoveries (log, pn, mask)
248  *
249  *    Copy all discoveries in a buffer
250  *
251  * This function implement a safe way for lmp clients to access the
252  * discovery log. The basic problem is that we don't want the log
253  * to change (add/remove) while the client is reading it. If the
254  * lmp client manipulate directly the hashbin, he is sure to get
255  * into troubles...
256  * The idea is that we copy all the current discovery log in a buffer
257  * which is specific to the client and pass this copy to him. As we
258  * do this operation with the spinlock grabbed, we are safe...
259  * Note : we don't want those clients to grab the spinlock, because
260  * we have no control on how long they will hold it...
261  * Note : we choose to copy the log in "struct irda_device_info" to
262  * save space...
263  * Note : the client must kfree himself() the log...
264  * Jean II
265  */
irlmp_copy_discoveries(hashbin_t * log,int * pn,__u16 mask,int old_entries)266 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
267 						__u16 mask, int old_entries)
268 {
269 	discovery_t *		discovery;
270 	unsigned long		flags;
271 	discinfo_t *		buffer = NULL;
272 	int			j_timeout = (sysctl_discovery_timeout * HZ);
273 	int			n;		/* Size of the full log */
274 	int			i = 0;		/* How many we picked */
275 
276 	IRDA_ASSERT(pn != NULL, return NULL;);
277 	IRDA_ASSERT(log != NULL, return NULL;);
278 
279 	/* Save spin lock */
280 	spin_lock_irqsave(&log->hb_spinlock, flags);
281 
282 	discovery = (discovery_t *) hashbin_get_first(log);
283 	while (discovery != NULL) {
284 		/* Mask out the ones we don't want :
285 		 * We want to match the discovery mask, and to get only
286 		 * the most recent one (unless we want old ones) */
287 		if ((get_unaligned((__u16 *)discovery->data.hints) & mask) &&
288 		    ((old_entries) ||
289 		     ((jiffies - discovery->firststamp) < j_timeout))) {
290 			/* Create buffer as needed.
291 			 * As this function get called a lot and most time
292 			 * we don't have anything to put in the log (we are
293 			 * quite picky), we can save a lot of overhead
294 			 * by not calling kmalloc. Jean II */
295 			if(buffer == NULL) {
296 				/* Create the client specific buffer */
297 				n = HASHBIN_GET_SIZE(log);
298 				buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
299 				if (buffer == NULL) {
300 					spin_unlock_irqrestore(&log->hb_spinlock, flags);
301 					return NULL;
302 				}
303 
304 			}
305 
306 			/* Copy discovery information */
307 			memcpy(&(buffer[i]), &(discovery->data),
308 			       sizeof(discinfo_t));
309 			i++;
310 		}
311 		discovery = (discovery_t *) hashbin_get_next(log);
312 	}
313 
314 	spin_unlock_irqrestore(&log->hb_spinlock, flags);
315 
316 	/* Get the actual number of device in the buffer and return */
317 	*pn = i;
318 	return buffer;
319 }
320 
321 #ifdef CONFIG_PROC_FS
discovery_seq_idx(loff_t pos)322 static inline discovery_t *discovery_seq_idx(loff_t pos)
323 
324 {
325 	discovery_t *discovery;
326 
327 	for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
328 	     discovery != NULL;
329 	     discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
330 		if (pos-- == 0)
331 			break;
332 	}
333 
334 	return discovery;
335 }
336 
discovery_seq_start(struct seq_file * seq,loff_t * pos)337 static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
338 {
339 	spin_lock_irq(&irlmp->cachelog->hb_spinlock);
340 	return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
341 }
342 
discovery_seq_next(struct seq_file * seq,void * v,loff_t * pos)343 static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
344 {
345 	++*pos;
346 	return (v == SEQ_START_TOKEN)
347 		? (void *) hashbin_get_first(irlmp->cachelog)
348 		: (void *) hashbin_get_next(irlmp->cachelog);
349 }
350 
discovery_seq_stop(struct seq_file * seq,void * v)351 static void discovery_seq_stop(struct seq_file *seq, void *v)
352 {
353 	spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
354 }
355 
discovery_seq_show(struct seq_file * seq,void * v)356 static int discovery_seq_show(struct seq_file *seq, void *v)
357 {
358 	if (v == SEQ_START_TOKEN)
359 		seq_puts(seq, "IrLMP: Discovery log:\n\n");
360 	else {
361 		const discovery_t *discovery = v;
362 
363 		seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
364 			   discovery->data.info,
365 			   discovery->data.hints[0],
366 			   discovery->data.hints[1]);
367 #if 0
368 		if ( discovery->data.hints[0] & HINT_PNP)
369 			seq_puts(seq, "PnP Compatible ");
370 		if ( discovery->data.hints[0] & HINT_PDA)
371 			seq_puts(seq, "PDA/Palmtop ");
372 		if ( discovery->data.hints[0] & HINT_COMPUTER)
373 			seq_puts(seq, "Computer ");
374 		if ( discovery->data.hints[0] & HINT_PRINTER)
375 			seq_puts(seq, "Printer ");
376 		if ( discovery->data.hints[0] & HINT_MODEM)
377 			seq_puts(seq, "Modem ");
378 		if ( discovery->data.hints[0] & HINT_FAX)
379 			seq_puts(seq, "Fax ");
380 		if ( discovery->data.hints[0] & HINT_LAN)
381 			seq_puts(seq, "LAN Access ");
382 
383 		if ( discovery->data.hints[1] & HINT_TELEPHONY)
384 			seq_puts(seq, "Telephony ");
385 		if ( discovery->data.hints[1] & HINT_FILE_SERVER)
386 			seq_puts(seq, "File Server ");
387 		if ( discovery->data.hints[1] & HINT_COMM)
388 			seq_puts(seq, "IrCOMM ");
389 		if ( discovery->data.hints[1] & HINT_OBEX)
390 			seq_puts(seq, "IrOBEX ");
391 #endif
392 		seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
393 			       discovery->data.saddr,
394 			       discovery->data.daddr);
395 
396 		seq_putc(seq, '\n');
397 	}
398 	return 0;
399 }
400 
401 static const struct seq_operations discovery_seq_ops = {
402 	.start  = discovery_seq_start,
403 	.next   = discovery_seq_next,
404 	.stop   = discovery_seq_stop,
405 	.show   = discovery_seq_show,
406 };
407 
discovery_seq_open(struct inode * inode,struct file * file)408 static int discovery_seq_open(struct inode *inode, struct file *file)
409 {
410 	IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
411 
412 	return seq_open(file, &discovery_seq_ops);
413 }
414 
415 const struct file_operations discovery_seq_fops = {
416 	.owner		= THIS_MODULE,
417 	.open           = discovery_seq_open,
418 	.read           = seq_read,
419 	.llseek         = seq_lseek,
420 	.release	= seq_release,
421 };
422 #endif
423