[DECNET]: Use correct config option for routing by fwmark in compare_keys()
[hh.org.git] / net / irda / discovery.c
blob3fefc822c1c087c2cebd4baadb646ef771442784
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>
14 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
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.
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.
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
31 ********************************************************************/
33 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/seq_file.h>
37 #include <net/irda/irda.h>
38 #include <net/irda/irlmp.h>
40 #include <net/irda/discovery.h>
43 * Function irlmp_add_discovery (cachelog, discovery)
45 * Add a new discovery to the cachelog, and remove any old discoveries
46 * from the same device
48 * Note : we try to preserve the time this device was *first* discovered
49 * (as opposed to the time of last discovery used for cleanup). This is
50 * used by clients waiting for discovery events to tell if the device
51 * discovered is "new" or just the same old one. They can't rely there
52 * on a binary flag (new/old), because not all discovery events are
53 * propagated to them, and they might not always listen, so they would
54 * miss some new devices popping up...
55 * Jean II
57 void irlmp_add_discovery(hashbin_t *cachelog, discovery_t *new)
59 discovery_t *discovery, *node;
60 unsigned long flags;
62 /* Set time of first discovery if node is new (see below) */
63 new->firststamp = new->timestamp;
65 spin_lock_irqsave(&cachelog->hb_spinlock, flags);
67 /*
68 * Remove all discoveries of devices that has previously been
69 * discovered on the same link with the same name (info), or the
70 * same daddr. We do this since some devices (mostly PDAs) change
71 * their device address between every discovery.
73 discovery = (discovery_t *) hashbin_get_first(cachelog);
74 while (discovery != NULL ) {
75 node = discovery;
77 /* Be sure to stay one item ahead */
78 discovery = (discovery_t *) hashbin_get_next(cachelog);
80 if ((node->data.saddr == new->data.saddr) &&
81 ((node->data.daddr == new->data.daddr) ||
82 (strcmp(node->data.info, new->data.info) == 0)))
84 /* This discovery is a previous discovery
85 * from the same device, so just remove it
87 hashbin_remove_this(cachelog, (irda_queue_t *) node);
88 /* Check if hints bits are unchanged */
89 if(u16ho(node->data.hints) == u16ho(new->data.hints))
90 /* Set time of first discovery for this node */
91 new->firststamp = node->firststamp;
92 kfree(node);
96 /* Insert the new and updated version */
97 hashbin_insert(cachelog, (irda_queue_t *) new, new->data.daddr, NULL);
99 spin_unlock_irqrestore(&cachelog->hb_spinlock, flags);
103 * Function irlmp_add_discovery_log (cachelog, log)
105 * Merge a disovery log into the cachelog.
108 void irlmp_add_discovery_log(hashbin_t *cachelog, hashbin_t *log)
110 discovery_t *discovery;
112 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
115 * If log is missing this means that IrLAP was unable to perform the
116 * discovery, so restart discovery again with just the half timeout
117 * of the normal one.
119 /* Well... It means that there was nobody out there - Jean II */
120 if (log == NULL) {
121 /* irlmp_start_discovery_timer(irlmp, 150); */
122 return;
126 * Locking : we are the only owner of this discovery log, so
127 * no need to lock it.
128 * We just need to lock the global log in irlmp_add_discovery().
130 discovery = (discovery_t *) hashbin_remove_first(log);
131 while (discovery != NULL) {
132 irlmp_add_discovery(cachelog, discovery);
134 discovery = (discovery_t *) hashbin_remove_first(log);
137 /* Delete the now empty log */
138 hashbin_delete(log, (FREE_FUNC) kfree);
142 * Function irlmp_expire_discoveries (log, saddr, force)
144 * Go through all discoveries and expire all that has stayed too long
146 * Note : this assume that IrLAP won't change its saddr, which
147 * currently is a valid assumption...
149 void irlmp_expire_discoveries(hashbin_t *log, __u32 saddr, int force)
151 discovery_t * discovery;
152 discovery_t * curr;
153 unsigned long flags;
154 discinfo_t * buffer = NULL;
155 int n; /* Size of the full log */
156 int i = 0; /* How many we expired */
158 IRDA_ASSERT(log != NULL, return;);
159 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
161 spin_lock_irqsave(&log->hb_spinlock, flags);
163 discovery = (discovery_t *) hashbin_get_first(log);
164 while (discovery != NULL) {
165 /* Be sure to be one item ahead */
166 curr = discovery;
167 discovery = (discovery_t *) hashbin_get_next(log);
169 /* Test if it's time to expire this discovery */
170 if ((curr->data.saddr == saddr) &&
171 (force ||
172 ((jiffies - curr->timestamp) > DISCOVERY_EXPIRE_TIMEOUT)))
174 /* Create buffer as needed.
175 * As this function get called a lot and most time
176 * we don't have anything to put in the log (we are
177 * quite picky), we can save a lot of overhead
178 * by not calling kmalloc. Jean II */
179 if(buffer == NULL) {
180 /* Create the client specific buffer */
181 n = HASHBIN_GET_SIZE(log);
182 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
183 if (buffer == NULL) {
184 spin_unlock_irqrestore(&log->hb_spinlock, flags);
185 return;
190 /* Copy discovery information */
191 memcpy(&(buffer[i]), &(curr->data),
192 sizeof(discinfo_t));
193 i++;
195 /* Remove it from the log */
196 curr = hashbin_remove_this(log, (irda_queue_t *) curr);
197 kfree(curr);
201 /* Drop the spinlock before calling the higher layers, as
202 * we can't guarantee they won't call us back and create a
203 * deadlock. We will work on our own private data, so we
204 * don't care to be interupted. - Jean II */
205 spin_unlock_irqrestore(&log->hb_spinlock, flags);
207 if(buffer == NULL)
208 return;
210 /* Tell IrLMP and registered clients about it */
211 irlmp_discovery_expiry(buffer, i);
213 /* Free up our buffer */
214 kfree(buffer);
217 #if 0
219 * Function irlmp_dump_discoveries (log)
221 * Print out all discoveries in log
224 void irlmp_dump_discoveries(hashbin_t *log)
226 discovery_t *discovery;
228 IRDA_ASSERT(log != NULL, return;);
230 discovery = (discovery_t *) hashbin_get_first(log);
231 while (discovery != NULL) {
232 IRDA_DEBUG(0, "Discovery:\n");
233 IRDA_DEBUG(0, " daddr=%08x\n", discovery->data.daddr);
234 IRDA_DEBUG(0, " saddr=%08x\n", discovery->data.saddr);
235 IRDA_DEBUG(0, " nickname=%s\n", discovery->data.info);
237 discovery = (discovery_t *) hashbin_get_next(log);
240 #endif
243 * Function irlmp_copy_discoveries (log, pn, mask)
245 * Copy all discoveries in a buffer
247 * This function implement a safe way for lmp clients to access the
248 * discovery log. The basic problem is that we don't want the log
249 * to change (add/remove) while the client is reading it. If the
250 * lmp client manipulate directly the hashbin, he is sure to get
251 * into troubles...
252 * The idea is that we copy all the current discovery log in a buffer
253 * which is specific to the client and pass this copy to him. As we
254 * do this operation with the spinlock grabbed, we are safe...
255 * Note : we don't want those clients to grab the spinlock, because
256 * we have no control on how long they will hold it...
257 * Note : we choose to copy the log in "struct irda_device_info" to
258 * save space...
259 * Note : the client must kfree himself() the log...
260 * Jean II
262 struct irda_device_info *irlmp_copy_discoveries(hashbin_t *log, int *pn,
263 __u16 mask, int old_entries)
265 discovery_t * discovery;
266 unsigned long flags;
267 discinfo_t * buffer = NULL;
268 int j_timeout = (sysctl_discovery_timeout * HZ);
269 int n; /* Size of the full log */
270 int i = 0; /* How many we picked */
272 IRDA_ASSERT(pn != NULL, return NULL;);
273 IRDA_ASSERT(log != NULL, return NULL;);
275 /* Save spin lock */
276 spin_lock_irqsave(&log->hb_spinlock, flags);
278 discovery = (discovery_t *) hashbin_get_first(log);
279 while (discovery != NULL) {
280 /* Mask out the ones we don't want :
281 * We want to match the discovery mask, and to get only
282 * the most recent one (unless we want old ones) */
283 if ((u16ho(discovery->data.hints) & mask) &&
284 ((old_entries) ||
285 ((jiffies - discovery->firststamp) < j_timeout)) ) {
286 /* Create buffer as needed.
287 * As this function get called a lot and most time
288 * we don't have anything to put in the log (we are
289 * quite picky), we can save a lot of overhead
290 * by not calling kmalloc. Jean II */
291 if(buffer == NULL) {
292 /* Create the client specific buffer */
293 n = HASHBIN_GET_SIZE(log);
294 buffer = kmalloc(n * sizeof(struct irda_device_info), GFP_ATOMIC);
295 if (buffer == NULL) {
296 spin_unlock_irqrestore(&log->hb_spinlock, flags);
297 return NULL;
302 /* Copy discovery information */
303 memcpy(&(buffer[i]), &(discovery->data),
304 sizeof(discinfo_t));
305 i++;
307 discovery = (discovery_t *) hashbin_get_next(log);
310 spin_unlock_irqrestore(&log->hb_spinlock, flags);
312 /* Get the actual number of device in the buffer and return */
313 *pn = i;
314 return(buffer);
317 #ifdef CONFIG_PROC_FS
318 static inline discovery_t *discovery_seq_idx(loff_t pos)
321 discovery_t *discovery;
323 for (discovery = (discovery_t *) hashbin_get_first(irlmp->cachelog);
324 discovery != NULL;
325 discovery = (discovery_t *) hashbin_get_next(irlmp->cachelog)) {
326 if (pos-- == 0)
327 break;
330 return discovery;
333 static void *discovery_seq_start(struct seq_file *seq, loff_t *pos)
335 spin_lock_irq(&irlmp->cachelog->hb_spinlock);
336 return *pos ? discovery_seq_idx(*pos - 1) : SEQ_START_TOKEN;
339 static void *discovery_seq_next(struct seq_file *seq, void *v, loff_t *pos)
341 ++*pos;
342 return (v == SEQ_START_TOKEN)
343 ? (void *) hashbin_get_first(irlmp->cachelog)
344 : (void *) hashbin_get_next(irlmp->cachelog);
347 static void discovery_seq_stop(struct seq_file *seq, void *v)
349 spin_unlock_irq(&irlmp->cachelog->hb_spinlock);
352 static int discovery_seq_show(struct seq_file *seq, void *v)
354 if (v == SEQ_START_TOKEN)
355 seq_puts(seq, "IrLMP: Discovery log:\n\n");
356 else {
357 const discovery_t *discovery = v;
359 seq_printf(seq, "nickname: %s, hint: 0x%02x%02x",
360 discovery->data.info,
361 discovery->data.hints[0],
362 discovery->data.hints[1]);
363 #if 0
364 if ( discovery->data.hints[0] & HINT_PNP)
365 seq_puts(seq, "PnP Compatible ");
366 if ( discovery->data.hints[0] & HINT_PDA)
367 seq_puts(seq, "PDA/Palmtop ");
368 if ( discovery->data.hints[0] & HINT_COMPUTER)
369 seq_puts(seq, "Computer ");
370 if ( discovery->data.hints[0] & HINT_PRINTER)
371 seq_puts(seq, "Printer ");
372 if ( discovery->data.hints[0] & HINT_MODEM)
373 seq_puts(seq, "Modem ");
374 if ( discovery->data.hints[0] & HINT_FAX)
375 seq_puts(seq, "Fax ");
376 if ( discovery->data.hints[0] & HINT_LAN)
377 seq_puts(seq, "LAN Access ");
379 if ( discovery->data.hints[1] & HINT_TELEPHONY)
380 seq_puts(seq, "Telephony ");
381 if ( discovery->data.hints[1] & HINT_FILE_SERVER)
382 seq_puts(seq, "File Server ");
383 if ( discovery->data.hints[1] & HINT_COMM)
384 seq_puts(seq, "IrCOMM ");
385 if ( discovery->data.hints[1] & HINT_OBEX)
386 seq_puts(seq, "IrOBEX ");
387 #endif
388 seq_printf(seq,", saddr: 0x%08x, daddr: 0x%08x\n\n",
389 discovery->data.saddr,
390 discovery->data.daddr);
392 seq_putc(seq, '\n');
394 return 0;
397 static struct seq_operations discovery_seq_ops = {
398 .start = discovery_seq_start,
399 .next = discovery_seq_next,
400 .stop = discovery_seq_stop,
401 .show = discovery_seq_show,
404 static int discovery_seq_open(struct inode *inode, struct file *file)
406 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
408 return seq_open(file, &discovery_seq_ops);
411 struct file_operations discovery_seq_fops = {
412 .owner = THIS_MODULE,
413 .open = discovery_seq_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = seq_release,
418 #endif