dbus: Add D-Bus methods to flush the BSS cache
[mech_eap.git] / wpa_supplicant / bss.c
1 /*
2  * BSS table
3  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "drivers/driver.h"
21 #include "wpa_supplicant_i.h"
22 #include "config.h"
23 #include "notify.h"
24 #include "scan.h"
25 #include "bss.h"
26
27
28 /**
29  * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
30  */
31 #define WPA_BSS_EXPIRATION_PERIOD 10
32
33 /**
34  * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired
35  *
36  * This value control the time in seconds after which a BSS entry gets removed
37  * if it has not been updated or is not in use.
38  */
39 #define WPA_BSS_EXPIRATION_AGE 180
40
41 /**
42  * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans
43  *
44  * If the BSS entry has not been seen in this many scans, it will be removed.
45  * Value 1 means that the entry is removed after the first scan without the
46  * BSSID being seen. Larger values can be used to avoid BSS entries
47  * disappearing if they are not visible in every scan (e.g., low signal quality
48  * or interference).
49  */
50 #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
51
52 #define WPA_BSS_FREQ_CHANGED_FLAG       BIT(0)
53 #define WPA_BSS_SIGNAL_CHANGED_FLAG     BIT(1)
54 #define WPA_BSS_PRIVACY_CHANGED_FLAG    BIT(2)
55 #define WPA_BSS_MODE_CHANGED_FLAG       BIT(3)
56 #define WPA_BSS_WPAIE_CHANGED_FLAG      BIT(4)
57 #define WPA_BSS_RSNIE_CHANGED_FLAG      BIT(5)
58 #define WPA_BSS_WPS_CHANGED_FLAG        BIT(6)
59 #define WPA_BSS_RATES_CHANGED_FLAG      BIT(7)
60 #define WPA_BSS_IES_CHANGED_FLAG        BIT(8)
61
62
63 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
64 {
65         dl_list_del(&bss->list);
66         dl_list_del(&bss->list_id);
67         wpa_s->num_bss--;
68         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
69                 " SSID '%s'", bss->id, MAC2STR(bss->bssid),
70                 wpa_ssid_txt(bss->ssid, bss->ssid_len));
71         wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
72         os_free(bss);
73 }
74
75
76 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
77                              const u8 *ssid, size_t ssid_len)
78 {
79         struct wpa_bss *bss;
80         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
81                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
82                     bss->ssid_len == ssid_len &&
83                     os_memcmp(bss->ssid, ssid, ssid_len) == 0)
84                         return bss;
85         }
86         return NULL;
87 }
88
89
90 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
91 {
92         os_time_t usec;
93
94         dst->flags = src->flags;
95         os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
96         dst->freq = src->freq;
97         dst->beacon_int = src->beacon_int;
98         dst->caps = src->caps;
99         dst->qual = src->qual;
100         dst->noise = src->noise;
101         dst->level = src->level;
102         dst->tsf = src->tsf;
103
104         os_get_time(&dst->last_update);
105         dst->last_update.sec -= src->age / 1000;
106         usec = (src->age % 1000) * 1000;
107         if (dst->last_update.usec < usec) {
108                 dst->last_update.sec--;
109                 dst->last_update.usec += 1000000;
110         }
111         dst->last_update.usec -= usec;
112 }
113
114
115 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
116                         const u8 *ssid, size_t ssid_len,
117                         struct wpa_scan_res *res)
118 {
119         struct wpa_bss *bss;
120
121         bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
122         if (bss == NULL)
123                 return;
124         bss->id = wpa_s->bss_next_id++;
125         bss->last_update_idx = wpa_s->bss_update_idx;
126         wpa_bss_copy_res(bss, res);
127         os_memcpy(bss->ssid, ssid, ssid_len);
128         bss->ssid_len = ssid_len;
129         bss->ie_len = res->ie_len;
130         bss->beacon_ie_len = res->beacon_ie_len;
131         os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
132
133         dl_list_add_tail(&wpa_s->bss, &bss->list);
134         dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
135         wpa_s->num_bss++;
136         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
137                 " SSID '%s'",
138                 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
139         wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
140         if (wpa_s->num_bss > wpa_s->conf->bss_max_count) {
141                 /* Remove the oldest entry */
142                 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
143                                                     struct wpa_bss, list));
144         }
145 }
146
147
148 static int are_ies_equal(const struct wpa_bss *old,
149                          const struct wpa_scan_res *new, u32 ie)
150 {
151         const u8 *old_ie, *new_ie;
152         struct wpabuf *old_ie_buff = NULL;
153         struct wpabuf *new_ie_buff = NULL;
154         int new_ie_len, old_ie_len, ret, is_multi;
155
156         switch (ie) {
157         case WPA_IE_VENDOR_TYPE:
158                 old_ie = wpa_bss_get_vendor_ie(old, ie);
159                 new_ie = wpa_scan_get_vendor_ie(new, ie);
160                 is_multi = 0;
161                 break;
162         case WPS_IE_VENDOR_TYPE:
163                 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
164                 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
165                 is_multi = 1;
166                 break;
167         case WLAN_EID_RSN:
168         case WLAN_EID_SUPP_RATES:
169         case WLAN_EID_EXT_SUPP_RATES:
170                 old_ie = wpa_bss_get_ie(old, ie);
171                 new_ie = wpa_scan_get_ie(new, ie);
172                 is_multi = 0;
173                 break;
174         default:
175                 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
176                 return 0;
177         }
178
179         if (is_multi) {
180                 /* in case of multiple IEs stored in buffer */
181                 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
182                 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
183                 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
184                 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
185         } else {
186                 /* in case of single IE */
187                 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
188                 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
189         }
190
191         ret = (old_ie_len == new_ie_len &&
192                os_memcmp(old_ie, new_ie, old_ie_len) == 0);
193
194         wpabuf_free(old_ie_buff);
195         wpabuf_free(new_ie_buff);
196
197         return ret;
198 }
199
200
201 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
202                                const struct wpa_scan_res *new)
203 {
204         u32 changes = 0;
205         int caps_diff = old->caps ^ new->caps;
206
207         if (old->freq != new->freq)
208                 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
209
210         if (old->level != new->level)
211                 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
212
213         if (caps_diff & IEEE80211_CAP_PRIVACY)
214                 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
215
216         if (caps_diff & IEEE80211_CAP_IBSS)
217                 changes |= WPA_BSS_MODE_CHANGED_FLAG;
218
219         if (old->ie_len == new->ie_len &&
220             os_memcmp(old + 1, new + 1, old->ie_len) == 0)
221                 return changes;
222         changes |= WPA_BSS_IES_CHANGED_FLAG;
223
224         if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
225                 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
226
227         if (!are_ies_equal(old, new, WLAN_EID_RSN))
228                 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
229
230         if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
231                 changes |= WPA_BSS_WPS_CHANGED_FLAG;
232
233         if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
234             !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
235                 changes |= WPA_BSS_RATES_CHANGED_FLAG;
236
237         return changes;
238 }
239
240
241 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
242                                const struct wpa_bss *bss)
243 {
244         if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
245                 wpas_notify_bss_freq_changed(wpa_s, bss->id);
246
247         if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
248                 wpas_notify_bss_signal_changed(wpa_s, bss->id);
249
250         if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
251                 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
252
253         if (changes & WPA_BSS_MODE_CHANGED_FLAG)
254                 wpas_notify_bss_mode_changed(wpa_s, bss->id);
255
256         if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
257                 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
258
259         if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
260                 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
261
262         if (changes & WPA_BSS_WPS_CHANGED_FLAG)
263                 wpas_notify_bss_wps_changed(wpa_s, bss->id);
264
265         if (changes & WPA_BSS_IES_CHANGED_FLAG)
266                 wpas_notify_bss_ies_changed(wpa_s, bss->id);
267
268         if (changes & WPA_BSS_RATES_CHANGED_FLAG)
269                 wpas_notify_bss_rates_changed(wpa_s, bss->id);
270 }
271
272
273 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
274                            struct wpa_scan_res *res)
275 {
276         u32 changes;
277
278         changes = wpa_bss_compare_res(bss, res);
279         bss->scan_miss_count = 0;
280         bss->last_update_idx = wpa_s->bss_update_idx;
281         wpa_bss_copy_res(bss, res);
282         /* Move the entry to the end of the list */
283         dl_list_del(&bss->list);
284         if (bss->ie_len + bss->beacon_ie_len >=
285             res->ie_len + res->beacon_ie_len) {
286                 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
287                 bss->ie_len = res->ie_len;
288                 bss->beacon_ie_len = res->beacon_ie_len;
289         } else {
290                 struct wpa_bss *nbss;
291                 struct dl_list *prev = bss->list_id.prev;
292                 dl_list_del(&bss->list_id);
293                 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
294                                   res->beacon_ie_len);
295                 if (nbss) {
296                         bss = nbss;
297                         os_memcpy(bss + 1, res + 1,
298                                   res->ie_len + res->beacon_ie_len);
299                         bss->ie_len = res->ie_len;
300                         bss->beacon_ie_len = res->beacon_ie_len;
301                 }
302                 dl_list_add(prev, &bss->list_id);
303         }
304         dl_list_add_tail(&wpa_s->bss, &bss->list);
305
306         notify_bss_changes(wpa_s, changes, bss);
307 }
308
309
310 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
311 {
312         return bss == wpa_s->current_bss ||
313                 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
314                 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
315 }
316
317
318 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
319 {
320         wpa_s->bss_update_idx++;
321         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
322                 wpa_s->bss_update_idx);
323 }
324
325
326 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
327                              struct wpa_scan_res *res)
328 {
329         const u8 *ssid, *p2p;
330         struct wpa_bss *bss;
331
332         ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
333         if (ssid == NULL) {
334                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
335                         MACSTR, MAC2STR(res->bssid));
336                 return;
337         }
338         if (ssid[1] > 32) {
339                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
340                         MACSTR, MAC2STR(res->bssid));
341                 return;
342         }
343
344         p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
345         if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
346             os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
347                 return; /* Skip P2P listen discovery results here */
348
349         /* TODO: add option for ignoring BSSes we are not interested in
350          * (to save memory) */
351         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
352         if (bss == NULL)
353                 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
354         else
355                 wpa_bss_update(wpa_s, bss, res);
356 }
357
358
359 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
360                                     const struct scan_info *info)
361 {
362         int found;
363         size_t i;
364
365         if (info == NULL)
366                 return 1;
367
368         if (info->num_freqs) {
369                 found = 0;
370                 for (i = 0; i < info->num_freqs; i++) {
371                         if (bss->freq == info->freqs[i]) {
372                                 found = 1;
373                                 break;
374                         }
375                 }
376                 if (!found)
377                         return 0;
378         }
379
380         if (info->num_ssids) {
381                 found = 0;
382                 for (i = 0; i < info->num_ssids; i++) {
383                         const struct wpa_driver_scan_ssid *s = &info->ssids[i];
384                         if ((s->ssid == NULL || s->ssid_len == 0) ||
385                             (s->ssid_len == bss->ssid_len &&
386                              os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
387                              0)) {
388                                 found = 1;
389                                 break;
390                         }
391                 }
392                 if (!found)
393                         return 0;
394         }
395
396         return 1;
397 }
398
399
400 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
401                         int new_scan)
402 {
403         struct wpa_bss *bss, *n;
404
405         if (!new_scan)
406                 return; /* do not expire entries without new scan */
407
408         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
409                 if (wpa_bss_in_use(wpa_s, bss))
410                         continue;
411                 if (!wpa_bss_included_in_scan(bss, info))
412                         continue; /* expire only BSSes that were scanned */
413                 if (bss->last_update_idx < wpa_s->bss_update_idx)
414                         bss->scan_miss_count++;
415                 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
416                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
417                                 "no match in scan", bss->id);
418                         wpa_bss_remove(wpa_s, bss);
419                 }
420         }
421 }
422
423
424 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
425 {
426         struct wpa_bss *bss, *n;
427         struct os_time t;
428
429         if (dl_list_empty(&wpa_s->bss))
430                 return;
431
432         os_get_time(&t);
433         t.sec -= age;
434
435         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
436                 if (wpa_bss_in_use(wpa_s, bss))
437                         continue;
438
439                 if (os_time_before(&bss->last_update, &t)) {
440                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
441                                 "age", bss->id);
442                         wpa_bss_remove(wpa_s, bss);
443                 } else
444                         break;
445         }
446 }
447
448
449 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
450 {
451         struct wpa_supplicant *wpa_s = eloop_ctx;
452
453         wpa_bss_flush_by_age(wpa_s, WPA_BSS_EXPIRATION_AGE);
454         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
455                                wpa_bss_timeout, wpa_s, NULL);
456 }
457
458
459 int wpa_bss_init(struct wpa_supplicant *wpa_s)
460 {
461         dl_list_init(&wpa_s->bss);
462         dl_list_init(&wpa_s->bss_id);
463         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
464                                wpa_bss_timeout, wpa_s, NULL);
465         return 0;
466 }
467
468
469 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
470 {
471         struct wpa_bss *bss, *n;
472
473         if (wpa_s->bss.next == NULL)
474                 return; /* BSS table not yet initialized */
475
476         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
477                 if (wpa_bss_in_use(wpa_s, bss))
478                         continue;
479                 wpa_bss_remove(wpa_s, bss);
480         }
481 }
482
483
484 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
485 {
486         eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
487         wpa_bss_flush(wpa_s);
488 }
489
490
491 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
492                                    const u8 *bssid)
493 {
494         struct wpa_bss *bss;
495         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
496                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
497                         return bss;
498         }
499         return NULL;
500 }
501
502
503 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
504 {
505         struct wpa_bss *bss;
506         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
507                 if (bss->id == id)
508                         return bss;
509         }
510         return NULL;
511 }
512
513
514 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
515 {
516         const u8 *end, *pos;
517
518         pos = (const u8 *) (bss + 1);
519         end = pos + bss->ie_len;
520
521         while (pos + 1 < end) {
522                 if (pos + 2 + pos[1] > end)
523                         break;
524                 if (pos[0] == ie)
525                         return pos;
526                 pos += 2 + pos[1];
527         }
528
529         return NULL;
530 }
531
532
533 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
534 {
535         const u8 *end, *pos;
536
537         pos = (const u8 *) (bss + 1);
538         end = pos + bss->ie_len;
539
540         while (pos + 1 < end) {
541                 if (pos + 2 + pos[1] > end)
542                         break;
543                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
544                     vendor_type == WPA_GET_BE32(&pos[2]))
545                         return pos;
546                 pos += 2 + pos[1];
547         }
548
549         return NULL;
550 }
551
552
553 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
554                                             u32 vendor_type)
555 {
556         struct wpabuf *buf;
557         const u8 *end, *pos;
558
559         buf = wpabuf_alloc(bss->ie_len);
560         if (buf == NULL)
561                 return NULL;
562
563         pos = (const u8 *) (bss + 1);
564         end = pos + bss->ie_len;
565
566         while (pos + 1 < end) {
567                 if (pos + 2 + pos[1] > end)
568                         break;
569                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
570                     vendor_type == WPA_GET_BE32(&pos[2]))
571                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
572                 pos += 2 + pos[1];
573         }
574
575         if (wpabuf_len(buf) == 0) {
576                 wpabuf_free(buf);
577                 buf = NULL;
578         }
579
580         return buf;
581 }
582
583
584 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
585 {
586         int rate = 0;
587         const u8 *ie;
588         int i;
589
590         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
591         for (i = 0; ie && i < ie[1]; i++) {
592                 if ((ie[i + 2] & 0x7f) > rate)
593                         rate = ie[i + 2] & 0x7f;
594         }
595
596         ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
597         for (i = 0; ie && i < ie[1]; i++) {
598                 if ((ie[i + 2] & 0x7f) > rate)
599                         rate = ie[i + 2] & 0x7f;
600         }
601
602         return rate;
603 }
604
605
606 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
607 {
608         const u8 *ie, *ie2;
609         int i, j;
610         unsigned int len;
611         u8 *r;
612
613         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
614         ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
615
616         len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
617
618         r = os_malloc(len);
619         if (!r)
620                 return -1;
621
622         for (i = 0; ie && i < ie[1]; i++)
623                 r[i] = ie[i + 2] & 0x7f;
624
625         for (j = 0; ie2 && j < ie2[1]; j++)
626                 r[i + j] = ie2[j + 2] & 0x7f;
627
628         *rates = r;
629         return len;
630 }