]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/uwb/wlp/wss-lc.c
Merge git://git.infradead.org/mtd-2.6
[linux-2.6-omap-h63xx.git] / drivers / uwb / wlp / wss-lc.c
1 /*
2  * WiMedia Logical Link Control Protocol (WLP)
3  *
4  * Copyright (C) 2007 Intel Corporation
5  * Reinette Chatre <reinette.chatre@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  *
21  *
22  * Implementation of the WLP association protocol.
23  *
24  * FIXME: Docs
25  *
26  * A UWB network interface will configure a WSS through wlp_wss_setup() after
27  * the interface has been assigned a MAC address, typically after
28  * "ifconfig" has been called. When the interface goes down it should call
29  * wlp_wss_remove().
30  *
31  * When the WSS is ready for use the user interacts via sysfs to create,
32  * discover, and activate WSS.
33  *
34  * wlp_wss_enroll_activate()
35  *
36  * wlp_wss_create_activate()
37  *      wlp_wss_set_wssid_hash()
38  *              wlp_wss_comp_wssid_hash()
39  *      wlp_wss_sel_bcast_addr()
40  *      wlp_wss_sysfs_add()
41  *
42  * Called when no more references to WSS exist:
43  *      wlp_wss_release()
44  *              wlp_wss_reset()
45  */
46
47 #include <linux/etherdevice.h> /* for is_valid_ether_addr */
48 #include <linux/skbuff.h>
49 #include <linux/wlp.h>
50 #define D_LOCAL 5
51 #include <linux/uwb/debug.h>
52 #include "wlp-internal.h"
53
54
55 size_t wlp_wss_key_print(char *buf, size_t bufsize, u8 *key)
56 {
57         size_t result;
58
59         result = scnprintf(buf, bufsize,
60                           "%02x %02x %02x %02x %02x %02x "
61                           "%02x %02x %02x %02x %02x %02x "
62                           "%02x %02x %02x %02x",
63                           key[0], key[1], key[2], key[3],
64                           key[4], key[5], key[6], key[7],
65                           key[8], key[9], key[10], key[11],
66                           key[12], key[13], key[14], key[15]);
67         return result;
68 }
69
70 /**
71  * Compute WSSID hash
72  * WLP Draft 0.99 [7.2.1]
73  *
74  * The WSSID hash for a WSSID is the result of an octet-wise exclusive-OR
75  * of all octets in the WSSID.
76  */
77 static
78 u8 wlp_wss_comp_wssid_hash(struct wlp_uuid *wssid)
79 {
80         return wssid->data[0]  ^ wssid->data[1]  ^ wssid->data[2]
81                ^ wssid->data[3]  ^ wssid->data[4]  ^ wssid->data[5]
82                ^ wssid->data[6]  ^ wssid->data[7]  ^ wssid->data[8]
83                ^ wssid->data[9]  ^ wssid->data[10] ^ wssid->data[11]
84                ^ wssid->data[12] ^ wssid->data[13] ^ wssid->data[14]
85                ^ wssid->data[15];
86 }
87
88 /**
89  * Select a multicast EUI-48 for the WSS broadcast address.
90  * WLP Draft 0.99 [7.2.1]
91  *
92  * Selected based on the WiMedia Alliance OUI, 00-13-88, within the WLP
93  * range, [01-13-88-00-01-00, 01-13-88-00-01-FF] inclusive.
94  *
95  * This address is currently hardcoded.
96  * FIXME?
97  */
98 static
99 struct uwb_mac_addr wlp_wss_sel_bcast_addr(struct wlp_wss *wss)
100 {
101         struct uwb_mac_addr bcast = {
102                 .data = { 0x01, 0x13, 0x88, 0x00, 0x01, 0x00 }
103         };
104         return bcast;
105 }
106
107 /**
108  * Clear the contents of the WSS structure - all except kobj, mutex, virtual
109  *
110  * We do not want to reinitialize - the internal kobj should not change as
111  * it still points to the parent received during setup. The mutex should
112  * remain also. We thus just reset values individually.
113  * The virutal address assigned to WSS will remain the same for the
114  * lifetime of the WSS. We only reset the fields that can change during its
115  * lifetime.
116  */
117 void wlp_wss_reset(struct wlp_wss *wss)
118 {
119         struct wlp *wlp = container_of(wss, struct wlp, wss);
120         struct device *dev = &wlp->rc->uwb_dev.dev;
121         d_fnstart(5, dev, "wss (%p) \n", wss);
122         memset(&wss->wssid, 0, sizeof(wss->wssid));
123         wss->hash = 0;
124         memset(&wss->name[0], 0, sizeof(wss->name));
125         memset(&wss->bcast, 0, sizeof(wss->bcast));
126         wss->secure_status = WLP_WSS_UNSECURE;
127         memset(&wss->master_key[0], 0, sizeof(wss->master_key));
128         wss->tag = 0;
129         wss->state = WLP_WSS_STATE_NONE;
130         d_fnend(5, dev, "wss (%p) \n", wss);
131 }
132
133 /**
134  * Create sysfs infrastructure for WSS
135  *
136  * The WSS is configured to have the interface as parent (see wlp_wss_setup())
137  * a new sysfs directory that includes wssid as its name is created in the
138  * interface's sysfs directory. The group of files interacting with WSS are
139  * created also.
140  */
141 static
142 int wlp_wss_sysfs_add(struct wlp_wss *wss, char *wssid_str)
143 {
144         struct wlp *wlp = container_of(wss, struct wlp, wss);
145         struct device *dev = &wlp->rc->uwb_dev.dev;
146         int result;
147
148         d_fnstart(5, dev, "wss (%p), wssid: %s\n", wss, wssid_str);
149         result = kobject_set_name(&wss->kobj, "wss-%s", wssid_str);
150         if (result < 0)
151                 return result;
152         wss->kobj.ktype = &wss_ktype;
153         result = kobject_init_and_add(&wss->kobj,
154                         &wss_ktype, wss->kobj.parent, "wlp");
155         if (result < 0) {
156                 dev_err(dev, "WLP: Cannot register WSS kobject.\n");
157                 goto error_kobject_register;
158         }
159         result = sysfs_create_group(&wss->kobj, &wss_attr_group);
160         if (result < 0) {
161                 dev_err(dev, "WLP: Cannot register WSS attributes: %d\n",
162                         result);
163                 goto error_sysfs_create_group;
164         }
165         d_fnend(5, dev, "Completed. result = %d \n", result);
166         return 0;
167 error_sysfs_create_group:
168
169         kobject_put(&wss->kobj); /* will free name if needed */
170         return result;
171 error_kobject_register:
172         kfree(wss->kobj.name);
173         wss->kobj.name = NULL;
174         wss->kobj.ktype = NULL;
175         return result;
176 }
177
178
179 /**
180  * Release WSS
181  *
182  * No more references exist to this WSS. We should undo everything that was
183  * done in wlp_wss_create_activate() except removing the group. The group
184  * is not removed because an object can be unregistered before the group is
185  * created. We also undo any additional operations on the WSS after this
186  * (addition of members).
187  *
188  * If memory was allocated for the kobject's name then it will
189  * be freed by the kobject system during this time.
190  *
191  * The EDA cache is removed and reinitilized when the WSS is removed. We
192  * thus loose knowledge of members of this WSS at that time and need not do
193  * it here.
194  */
195 void wlp_wss_release(struct kobject *kobj)
196 {
197         struct wlp_wss *wss = container_of(kobj, struct wlp_wss, kobj);
198
199         wlp_wss_reset(wss);
200 }
201
202 /**
203  * Enroll into a WSS using provided neighbor as registrar
204  *
205  * First search the neighborhood information to learn which neighbor is
206  * referred to, next proceed with enrollment.
207  *
208  * &wss->mutex is held
209  */
210 static
211 int wlp_wss_enroll_target(struct wlp_wss *wss, struct wlp_uuid *wssid,
212                           struct uwb_dev_addr *dest)
213 {
214         struct wlp *wlp = container_of(wss, struct wlp, wss);
215         struct device *dev = &wlp->rc->uwb_dev.dev;
216         struct wlp_neighbor_e *neighbor;
217         char buf[WLP_WSS_UUID_STRSIZE];
218         int result = -ENXIO;
219         struct uwb_dev_addr *dev_addr;
220
221         wlp_wss_uuid_print(buf, sizeof(buf), wssid);
222         d_fnstart(5, dev, "wss %p, wssid %s, registrar %02x:%02x \n",
223                   wss, buf, dest->data[1], dest->data[0]);
224         mutex_lock(&wlp->nbmutex);
225         list_for_each_entry(neighbor, &wlp->neighbors, node) {
226                 dev_addr = &neighbor->uwb_dev->dev_addr;
227                 if (!memcmp(dest, dev_addr, sizeof(*dest))) {
228                         d_printf(5, dev, "Neighbor %02x:%02x is valid, "
229                                  "enrolling. \n",
230                                  dev_addr->data[1], dev_addr->data[0]);
231                         result = wlp_enroll_neighbor(wlp, neighbor, wss,
232                                                      wssid);
233                         break;
234                 }
235         }
236         if (result == -ENXIO)
237                 dev_err(dev, "WLP: Cannot find neighbor %02x:%02x. \n",
238                         dest->data[1], dest->data[0]);
239         mutex_unlock(&wlp->nbmutex);
240         d_fnend(5, dev, "wss %p, wssid %s, registrar %02x:%02x, result %d \n",
241                   wss, buf, dest->data[1], dest->data[0], result);
242         return result;
243 }
244
245 /**
246  * Enroll into a WSS previously discovered
247  *
248  * User provides WSSID of WSS, search for neighbor that has this WSS
249  * activated and attempt to enroll.
250  *
251  * &wss->mutex is held
252  */
253 static
254 int wlp_wss_enroll_discovered(struct wlp_wss *wss, struct wlp_uuid *wssid)
255 {
256         struct wlp *wlp = container_of(wss, struct wlp, wss);
257         struct device *dev = &wlp->rc->uwb_dev.dev;
258         struct wlp_neighbor_e *neighbor;
259         struct wlp_wssid_e *wssid_e;
260         char buf[WLP_WSS_UUID_STRSIZE];
261         int result = -ENXIO;
262
263         wlp_wss_uuid_print(buf, sizeof(buf), wssid);
264         d_fnstart(5, dev, "wss %p, wssid %s \n", wss, buf);
265         mutex_lock(&wlp->nbmutex);
266         list_for_each_entry(neighbor, &wlp->neighbors, node) {
267                 list_for_each_entry(wssid_e, &neighbor->wssid, node) {
268                         if (!memcmp(wssid, &wssid_e->wssid, sizeof(*wssid))) {
269                                 d_printf(5, dev, "Found WSSID %s in neighbor "
270                                          "%02x:%02x cache. \n", buf,
271                                          neighbor->uwb_dev->dev_addr.data[1],
272                                          neighbor->uwb_dev->dev_addr.data[0]);
273                                 result = wlp_enroll_neighbor(wlp, neighbor,
274                                                              wss, wssid);
275                                 if (result == 0) /* enrollment success */
276                                         goto out;
277                                 break;
278                         }
279                 }
280         }
281 out:
282         if (result == -ENXIO)
283                 dev_err(dev, "WLP: Cannot find WSSID %s in cache. \n", buf);
284         mutex_unlock(&wlp->nbmutex);
285         d_fnend(5, dev, "wss %p, wssid %s, result %d \n", wss, buf, result);
286         return result;
287 }
288
289 /**
290  * Enroll into WSS with provided WSSID, registrar may be provided
291  *
292  * @wss: out WSS that will be enrolled
293  * @wssid: wssid of neighboring WSS that we want to enroll in
294  * @devaddr: registrar can be specified, will be broadcast (ff:ff) if any
295  *           neighbor can be used as registrar.
296  *
297  * &wss->mutex is held
298  */
299 static
300 int wlp_wss_enroll(struct wlp_wss *wss, struct wlp_uuid *wssid,
301                    struct uwb_dev_addr *devaddr)
302 {
303         int result;
304         struct wlp *wlp = container_of(wss, struct wlp, wss);
305         struct device *dev = &wlp->rc->uwb_dev.dev;
306         char buf[WLP_WSS_UUID_STRSIZE];
307         struct uwb_dev_addr bcast = {.data = {0xff, 0xff} };
308
309         wlp_wss_uuid_print(buf, sizeof(buf), wssid);
310         if (wss->state != WLP_WSS_STATE_NONE) {
311                 dev_err(dev, "WLP: Already enrolled in WSS %s.\n", buf);
312                 result = -EEXIST;
313                 goto error;
314         }
315         if (!memcmp(&bcast, devaddr, sizeof(bcast))) {
316                 d_printf(5, dev, "Request to enroll in discovered WSS "
317                          "with WSSID %s \n", buf);
318                 result = wlp_wss_enroll_discovered(wss, wssid);
319         } else {
320                 d_printf(5, dev, "Request to enroll in WSSID %s with "
321                          "registrar %02x:%02x\n", buf, devaddr->data[1],
322                          devaddr->data[0]);
323                 result = wlp_wss_enroll_target(wss, wssid, devaddr);
324         }
325         if (result < 0) {
326                 dev_err(dev, "WLP: Unable to enroll into WSS %s, result %d \n",
327                         buf, result);
328                 goto error;
329         }
330         d_printf(2, dev, "Successfully enrolled into WSS %s \n", buf);
331         result = wlp_wss_sysfs_add(wss, buf);
332         if (result < 0) {
333                 dev_err(dev, "WLP: Unable to set up sysfs for WSS kobject.\n");
334                 wlp_wss_reset(wss);
335         }
336 error:
337         return result;
338
339 }
340
341 /**
342  * Activate given WSS
343  *
344  * Prior to activation a WSS must be enrolled. To activate a WSS a device
345  * includes the WSS hash in the WLP IE in its beacon in each superframe.
346  * WLP 0.99 [7.2.5].
347  *
348  * The WSS tag is also computed at this time. We only support one activated
349  * WSS so we can use the hash as a tag - there will never be a conflict.
350  *
351  * We currently only support one activated WSS so only one WSS hash is
352  * included in the WLP IE.
353  */
354 static
355 int wlp_wss_activate(struct wlp_wss *wss)
356 {
357         struct wlp *wlp = container_of(wss, struct wlp, wss);
358         struct device *dev = &wlp->rc->uwb_dev.dev;
359         struct uwb_rc *uwb_rc = wlp->rc;
360         int result;
361         struct {
362                 struct wlp_ie wlp_ie;
363                 u8 hash; /* only include one hash */
364         } ie_data;
365
366         d_fnstart(5, dev, "Activating WSS %p. \n", wss);
367         BUG_ON(wss->state != WLP_WSS_STATE_ENROLLED);
368         wss->hash = wlp_wss_comp_wssid_hash(&wss->wssid);
369         wss->tag = wss->hash;
370         memset(&ie_data, 0, sizeof(ie_data));
371         ie_data.wlp_ie.hdr.element_id = UWB_IE_WLP;
372         ie_data.wlp_ie.hdr.length = sizeof(ie_data) - sizeof(struct uwb_ie_hdr);
373         wlp_ie_set_hash_length(&ie_data.wlp_ie, sizeof(ie_data.hash));
374         ie_data.hash = wss->hash;
375         result = uwb_rc_ie_add(uwb_rc, &ie_data.wlp_ie.hdr,
376                                sizeof(ie_data));
377         if (result < 0) {
378                 dev_err(dev, "WLP: Unable to add WLP IE to beacon. "
379                         "result = %d.\n", result);
380                 goto error_wlp_ie;
381         }
382         wss->state = WLP_WSS_STATE_ACTIVE;
383         result = 0;
384 error_wlp_ie:
385         d_fnend(5, dev, "Activating WSS %p, result = %d \n", wss, result);
386         return result;
387 }
388
389 /**
390  * Enroll in and activate WSS identified by provided WSSID
391  *
392  * The neighborhood cache should contain a list of all neighbors and the
393  * WSS they have activated. Based on that cache we search which neighbor we
394  * can perform the association process with. The user also has option to
395  * specify which neighbor it prefers as registrar.
396  * Successful enrollment is followed by activation.
397  * Successful activation will create the sysfs directory containing
398  * specific information regarding this WSS.
399  */
400 int wlp_wss_enroll_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
401                             struct uwb_dev_addr *devaddr)
402 {
403         struct wlp *wlp = container_of(wss, struct wlp, wss);
404         struct device *dev = &wlp->rc->uwb_dev.dev;
405         int result = 0;
406         char buf[WLP_WSS_UUID_STRSIZE];
407
408         d_fnstart(5, dev, "Enrollment and activation requested. \n");
409         mutex_lock(&wss->mutex);
410         result = wlp_wss_enroll(wss, wssid, devaddr);
411         if (result < 0) {
412                 wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
413                 dev_err(dev, "WLP: Enrollment into WSS %s failed.\n", buf);
414                 goto error_enroll;
415         }
416         result = wlp_wss_activate(wss);
417         if (result < 0) {
418                 dev_err(dev, "WLP: Unable to activate WSS. Undoing enrollment "
419                         "result = %d \n", result);
420                 /* Undo enrollment */
421                 wlp_wss_reset(wss);
422                 goto error_activate;
423         }
424 error_activate:
425 error_enroll:
426         mutex_unlock(&wss->mutex);
427         d_fnend(5, dev, "Completed. result = %d \n", result);
428         return result;
429 }
430
431 /**
432  * Create, enroll, and activate a new WSS
433  *
434  * @wssid: new wssid provided by user
435  * @name:  WSS name requested by used.
436  * @sec_status: security status requested by user
437  *
438  * A user requested the creation of a new WSS. All operations are done
439  * locally. The new WSS will be stored locally, the hash will be included
440  * in the WLP IE, and the sysfs infrastructure for this WSS will be
441  * created.
442  */
443 int wlp_wss_create_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
444                             char *name, unsigned sec_status, unsigned accept)
445 {
446         struct wlp *wlp = container_of(wss, struct wlp, wss);
447         struct device *dev = &wlp->rc->uwb_dev.dev;
448         int result = 0;
449         char buf[WLP_WSS_UUID_STRSIZE];
450         d_fnstart(5, dev, "Request to create new WSS.\n");
451         result = wlp_wss_uuid_print(buf, sizeof(buf), wssid);
452         d_printf(5, dev, "Request to create WSS: WSSID=%s, name=%s, "
453                  "sec_status=%u, accepting enrollment=%u \n",
454                  buf, name, sec_status, accept);
455         if (!mutex_trylock(&wss->mutex)) {
456                 dev_err(dev, "WLP: WLP association session in progress.\n");
457                 return -EBUSY;
458         }
459         if (wss->state != WLP_WSS_STATE_NONE) {
460                 dev_err(dev, "WLP: WSS already exists. Not creating new.\n");
461                 result = -EEXIST;
462                 goto out;
463         }
464         if (wss->kobj.parent == NULL) {
465                 dev_err(dev, "WLP: WSS parent not ready. Is network interface "
466                        "up?\n");
467                 result = -ENXIO;
468                 goto out;
469         }
470         if (sec_status == WLP_WSS_SECURE) {
471                 dev_err(dev, "WLP: FIXME Creation of secure WSS not "
472                         "supported yet.\n");
473                 result = -EINVAL;
474                 goto out;
475         }
476         wss->wssid = *wssid;
477         memcpy(wss->name, name, sizeof(wss->name));
478         wss->bcast = wlp_wss_sel_bcast_addr(wss);
479         wss->secure_status = sec_status;
480         wss->accept_enroll = accept;
481         /*wss->virtual_addr is initialized in call to wlp_wss_setup*/
482         /* sysfs infrastructure */
483         result = wlp_wss_sysfs_add(wss, buf);
484         if (result < 0) {
485                 dev_err(dev, "Cannot set up sysfs for WSS kobject.\n");
486                 wlp_wss_reset(wss);
487                 goto out;
488         } else
489                 result = 0;
490         wss->state = WLP_WSS_STATE_ENROLLED;
491         result = wlp_wss_activate(wss);
492         if (result < 0) {
493                 dev_err(dev, "WLP: Unable to activate WSS. Undoing "
494                         "enrollment\n");
495                 wlp_wss_reset(wss);
496                 goto out;
497         }
498         result = 0;
499 out:
500         mutex_unlock(&wss->mutex);
501         d_fnend(5, dev, "Completed. result = %d \n", result);
502         return result;
503 }
504
505 /**
506  * Determine if neighbor has WSS activated
507  *
508  * @returns: 1 if neighbor has WSS activated, zero otherwise
509  *
510  * This can be done in two ways:
511  * - send a C1 frame, parse C2/F0 response
512  * - examine the WLP IE sent by the neighbor
513  *
514  * The WLP IE is not fully supported in hardware so we use the C1/C2 frame
515  * exchange to determine if a WSS is activated. Using the WLP IE should be
516  * faster and should be used when it becomes possible.
517  */
518 int wlp_wss_is_active(struct wlp *wlp, struct wlp_wss *wss,
519                       struct uwb_dev_addr *dev_addr)
520 {
521         int result = 0;
522         struct device *dev = &wlp->rc->uwb_dev.dev;
523         char buf[WLP_WSS_UUID_STRSIZE];
524         DECLARE_COMPLETION_ONSTACK(completion);
525         struct wlp_session session;
526         struct sk_buff  *skb;
527         struct wlp_frame_assoc *resp;
528         struct wlp_uuid wssid;
529
530         wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
531         d_fnstart(5, dev, "wlp %p, wss %p (wssid %s), neighbor %02x:%02x \n",
532                   wlp, wss, buf, dev_addr->data[1], dev_addr->data[0]);
533         mutex_lock(&wlp->mutex);
534         /* Send C1 association frame */
535         result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C1);
536         if (result < 0) {
537                 dev_err(dev, "Unable to send C1 frame to neighbor "
538                         "%02x:%02x (%d)\n", dev_addr->data[1],
539                         dev_addr->data[0], result);
540                 result = 0;
541                 goto out;
542         }
543         /* Create session, wait for response */
544         session.exp_message = WLP_ASSOC_C2;
545         session.cb = wlp_session_cb;
546         session.cb_priv = &completion;
547         session.neighbor_addr = *dev_addr;
548         BUG_ON(wlp->session != NULL);
549         wlp->session = &session;
550         /* Wait for C2/F0 frame */
551         result = wait_for_completion_interruptible_timeout(&completion,
552                                                    WLP_PER_MSG_TIMEOUT * HZ);
553         if (result == 0) {
554                 dev_err(dev, "Timeout while sending C1 to neighbor "
555                              "%02x:%02x.\n", dev_addr->data[1],
556                              dev_addr->data[0]);
557                 goto out;
558         }
559         if (result < 0) {
560                 dev_err(dev, "Unable to send C1 to neighbor %02x:%02x.\n",
561                         dev_addr->data[1], dev_addr->data[0]);
562                 result = 0;
563                 goto out;
564         }
565         /* Parse message in session->data: it will be either C2 or F0 */
566         skb = session.data;
567         resp = (void *) skb->data;
568         d_printf(5, dev, "Received response to C1 frame. \n");
569         d_dump(5, dev, skb->data, skb->len > 72 ? 72 : skb->len);
570         if (resp->type == WLP_ASSOC_F0) {
571                 result = wlp_parse_f0(wlp, skb);
572                 if (result < 0)
573                         dev_err(dev, "WLP:  unable to parse incoming F0 "
574                                 "frame from neighbor %02x:%02x.\n",
575                                 dev_addr->data[1], dev_addr->data[0]);
576                 result = 0;
577                 goto error_resp_parse;
578         }
579         /* WLP version and message type fields have already been parsed */
580         result = wlp_get_wssid(wlp, (void *)resp + sizeof(*resp), &wssid,
581                                skb->len - sizeof(*resp));
582         if (result < 0) {
583                 dev_err(dev, "WLP: unable to obtain WSSID from C2 frame.\n");
584                 result = 0;
585                 goto error_resp_parse;
586         }
587         if (!memcmp(&wssid, &wss->wssid, sizeof(wssid))) {
588                 d_printf(5, dev, "WSSID in C2 frame matches local "
589                          "active WSS.\n");
590                 result = 1;
591         } else {
592                 dev_err(dev, "WLP: Received a C2 frame without matching "
593                         "WSSID.\n");
594                 result = 0;
595         }
596 error_resp_parse:
597         kfree_skb(skb);
598 out:
599         wlp->session = NULL;
600         mutex_unlock(&wlp->mutex);
601         d_fnend(5, dev, "wlp %p, wss %p (wssid %s), neighbor %02x:%02x \n",
602                   wlp, wss, buf, dev_addr->data[1], dev_addr->data[0]);
603         return result;
604 }
605
606 /**
607  * Activate connection with neighbor by updating EDA cache
608  *
609  * @wss:       local WSS to which neighbor wants to connect
610  * @dev_addr:  neighbor's address
611  * @wssid:     neighbor's WSSID - must be same as our WSS's WSSID
612  * @tag:       neighbor's WSS tag used to identify frames transmitted by it
613  * @virt_addr: neighbor's virtual EUI-48
614  */
615 static
616 int wlp_wss_activate_connection(struct wlp *wlp, struct wlp_wss *wss,
617                                 struct uwb_dev_addr *dev_addr,
618                                 struct wlp_uuid *wssid, u8 *tag,
619                                 struct uwb_mac_addr *virt_addr)
620 {
621         struct device *dev = &wlp->rc->uwb_dev.dev;
622         int result = 0;
623         char buf[WLP_WSS_UUID_STRSIZE];
624         wlp_wss_uuid_print(buf, sizeof(buf), wssid);
625         d_fnstart(5, dev, "wlp %p, wss %p, wssid %s, tag %u, virtual "
626                   "%02x:%02x:%02x:%02x:%02x:%02x \n", wlp, wss, buf, *tag,
627                   virt_addr->data[0], virt_addr->data[1], virt_addr->data[2],
628                   virt_addr->data[3], virt_addr->data[4], virt_addr->data[5]);
629
630         if (!memcmp(wssid, &wss->wssid, sizeof(*wssid))) {
631                 d_printf(5, dev, "WSSID from neighbor frame matches local "
632                          "active WSS.\n");
633                 /* Update EDA cache */
634                 result = wlp_eda_update_node(&wlp->eda, dev_addr, wss,
635                                              (void *) virt_addr->data, *tag,
636                                              WLP_WSS_CONNECTED);
637                 if (result < 0)
638                         dev_err(dev, "WLP: Unable to update EDA cache "
639                                 "with new connected neighbor information.\n");
640         } else {
641                 dev_err(dev, "WLP: Neighbor does not have matching "
642                         "WSSID.\n");
643                 result = -EINVAL;
644         }
645
646         d_fnend(5, dev, "wlp %p, wss %p, wssid %s, tag %u, virtual "
647                   "%02x:%02x:%02x:%02x:%02x:%02x, result = %d \n",
648                   wlp, wss, buf, *tag,
649                   virt_addr->data[0], virt_addr->data[1], virt_addr->data[2],
650                   virt_addr->data[3], virt_addr->data[4], virt_addr->data[5],
651                   result);
652
653         return result;
654 }
655
656 /**
657  * Connect to WSS neighbor
658  *
659  * Use C3/C4 exchange to determine if neighbor has WSS activated and
660  * retrieve the WSS tag and virtual EUI-48 of the neighbor.
661  */
662 static
663 int wlp_wss_connect_neighbor(struct wlp *wlp, struct wlp_wss *wss,
664                              struct uwb_dev_addr *dev_addr)
665 {
666         int result;
667         struct device *dev = &wlp->rc->uwb_dev.dev;
668         char buf[WLP_WSS_UUID_STRSIZE];
669         struct wlp_uuid wssid;
670         u8 tag;
671         struct uwb_mac_addr virt_addr;
672         DECLARE_COMPLETION_ONSTACK(completion);
673         struct wlp_session session;
674         struct wlp_frame_assoc *resp;
675         struct sk_buff *skb;
676
677         wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
678         d_fnstart(5, dev, "wlp %p, wss %p (wssid %s), neighbor %02x:%02x \n",
679                   wlp, wss, buf, dev_addr->data[1], dev_addr->data[0]);
680         mutex_lock(&wlp->mutex);
681         /* Send C3 association frame */
682         result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C3);
683         if (result < 0) {
684                 dev_err(dev, "Unable to send C3 frame to neighbor "
685                         "%02x:%02x (%d)\n", dev_addr->data[1],
686                         dev_addr->data[0], result);
687                 goto out;
688         }
689         /* Create session, wait for response */
690         session.exp_message = WLP_ASSOC_C4;
691         session.cb = wlp_session_cb;
692         session.cb_priv = &completion;
693         session.neighbor_addr = *dev_addr;
694         BUG_ON(wlp->session != NULL);
695         wlp->session = &session;
696         /* Wait for C4/F0 frame */
697         result = wait_for_completion_interruptible_timeout(&completion,
698                                                    WLP_PER_MSG_TIMEOUT * HZ);
699         if (result == 0) {
700                 dev_err(dev, "Timeout while sending C3 to neighbor "
701                              "%02x:%02x.\n", dev_addr->data[1],
702                              dev_addr->data[0]);
703                 result = -ETIMEDOUT;
704                 goto out;
705         }
706         if (result < 0) {
707                 dev_err(dev, "Unable to send C3 to neighbor %02x:%02x.\n",
708                         dev_addr->data[1], dev_addr->data[0]);
709                 goto out;
710         }
711         /* Parse message in session->data: it will be either C4 or F0 */
712         skb = session.data;
713         resp = (void *) skb->data;
714         d_printf(5, dev, "Received response to C3 frame. \n");
715         d_dump(5, dev, skb->data, skb->len > 72 ? 72 : skb->len);
716         if (resp->type == WLP_ASSOC_F0) {
717                 result = wlp_parse_f0(wlp, skb);
718                 if (result < 0)
719                         dev_err(dev, "WLP: unable to parse incoming F0 "
720                                 "frame from neighbor %02x:%02x.\n",
721                                 dev_addr->data[1], dev_addr->data[0]);
722                 result = -EINVAL;
723                 goto error_resp_parse;
724         }
725         result = wlp_parse_c3c4_frame(wlp, skb, &wssid, &tag, &virt_addr);
726         if (result < 0) {
727                 dev_err(dev, "WLP: Unable to parse C4 frame from neighbor.\n");
728                 goto error_resp_parse;
729         }
730         result = wlp_wss_activate_connection(wlp, wss, dev_addr, &wssid, &tag,
731                                              &virt_addr);
732         if (result < 0) {
733                 dev_err(dev, "WLP: Unable to activate connection to "
734                         "neighbor %02x:%02x.\n", dev_addr->data[1],
735                         dev_addr->data[0]);
736                 goto error_resp_parse;
737         }
738 error_resp_parse:
739         kfree_skb(skb);
740 out:
741         /* Record that we unsuccessfully tried to connect to this neighbor */
742         if (result < 0)
743                 wlp_eda_update_node_state(&wlp->eda, dev_addr,
744                                           WLP_WSS_CONNECT_FAILED);
745         wlp->session = NULL;
746         mutex_unlock(&wlp->mutex);
747         d_fnend(5, dev, "wlp %p, wss %p (wssid %s), neighbor %02x:%02x \n",
748                   wlp, wss, buf, dev_addr->data[1], dev_addr->data[0]);
749         return result;
750 }
751
752 /**
753  * Connect to neighbor with common WSS, send pending frame
754  *
755  * This function is scheduled when a frame is destined to a neighbor with
756  * which we do not have a connection. A copy of the EDA cache entry is
757  * provided - not the actual cache entry (because it is protected by a
758  * spinlock).
759  *
760  * First determine if neighbor has the same WSS activated, connect if it
761  * does. The C3/C4 exchange is dual purpose to determine if neighbor has
762  * WSS activated and proceed with the connection.
763  *
764  * The frame that triggered the connection setup is sent after connection
765  * setup.
766  *
767  * network queue is stopped - we need to restart when done
768  *
769  */
770 static
771 void wlp_wss_connect_send(struct work_struct *ws)
772 {
773         struct wlp_assoc_conn_ctx *conn_ctx = container_of(ws,
774                                                   struct wlp_assoc_conn_ctx,
775                                                   ws);
776         struct wlp *wlp = conn_ctx->wlp;
777         struct sk_buff *skb = conn_ctx->skb;
778         struct wlp_eda_node *eda_entry = &conn_ctx->eda_entry;
779         struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
780         struct wlp_wss *wss = &wlp->wss;
781         int result;
782         struct device *dev = &wlp->rc->uwb_dev.dev;
783         char buf[WLP_WSS_UUID_STRSIZE];
784
785         mutex_lock(&wss->mutex);
786         wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
787         d_fnstart(5, dev, "wlp %p, wss %p (wssid %s), neighbor %02x:%02x \n",
788                   wlp, wss, buf, dev_addr->data[1], dev_addr->data[0]);
789         if (wss->state < WLP_WSS_STATE_ACTIVE) {
790                 if (printk_ratelimit())
791                         dev_err(dev, "WLP: Attempting to connect with "
792                                 "WSS that is not active or connected.\n");
793                 dev_kfree_skb(skb);
794                 goto out;
795         }
796         /* Establish connection - send C3 rcv C4 */
797         result = wlp_wss_connect_neighbor(wlp, wss, dev_addr);
798         if (result < 0) {
799                 if (printk_ratelimit())
800                         dev_err(dev, "WLP: Unable to establish connection "
801                                 "with neighbor %02x:%02x.\n",
802                                 dev_addr->data[1], dev_addr->data[0]);
803                 dev_kfree_skb(skb);
804                 goto out;
805         }
806         /* EDA entry changed, update the local copy being used */
807         result = wlp_copy_eda_node(&wlp->eda, dev_addr, eda_entry);
808         if (result < 0) {
809                 if (printk_ratelimit())
810                         dev_err(dev, "WLP: Cannot find EDA entry for "
811                                 "neighbor %02x:%02x \n",
812                                 dev_addr->data[1], dev_addr->data[0]);
813         }
814         result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
815         if (result < 0) {
816                 if (printk_ratelimit())
817                         dev_err(dev, "WLP: Unable to prepare frame header for "
818                                 "transmission (neighbor %02x:%02x). \n",
819                                 dev_addr->data[1], dev_addr->data[0]);
820                 dev_kfree_skb(skb);
821                 goto out;
822         }
823         BUG_ON(wlp->xmit_frame == NULL);
824         result = wlp->xmit_frame(wlp, skb, dev_addr);
825         if (result < 0) {
826                 if (printk_ratelimit())
827                         dev_err(dev, "WLP: Unable to transmit frame: %d\n",
828                                 result);
829                 if (result == -ENXIO)
830                         dev_err(dev, "WLP: Is network interface up? \n");
831                 /* We could try again ... */
832                 dev_kfree_skb(skb);/*we need to free if tx fails */
833         }
834 out:
835         kfree(conn_ctx);
836         BUG_ON(wlp->start_queue == NULL);
837         wlp->start_queue(wlp);
838         mutex_unlock(&wss->mutex);
839         d_fnend(5, dev, "wlp %p, wss %p (wssid %s)\n", wlp, wss, buf);
840 }
841
842 /**
843  * Add WLP header to outgoing skb
844  *
845  * @eda_entry: pointer to neighbor's entry in the EDA cache
846  * @_skb:      skb containing data destined to the neighbor
847  */
848 int wlp_wss_prep_hdr(struct wlp *wlp, struct wlp_eda_node *eda_entry,
849                      void *_skb)
850 {
851         struct device *dev = &wlp->rc->uwb_dev.dev;
852         int result = 0;
853         unsigned char *eth_addr = eda_entry->eth_addr;
854         struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
855         struct sk_buff *skb = _skb;
856         struct wlp_frame_std_abbrv_hdr *std_hdr;
857
858         d_fnstart(6, dev, "wlp %p \n", wlp);
859         if (eda_entry->state == WLP_WSS_CONNECTED) {
860                 /* Add WLP header */
861                 BUG_ON(skb_headroom(skb) < sizeof(*std_hdr));
862                 std_hdr = (void *) __skb_push(skb, sizeof(*std_hdr));
863                 std_hdr->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
864                 std_hdr->hdr.type = WLP_FRAME_STANDARD;
865                 std_hdr->tag = eda_entry->wss->tag;
866         } else {
867                 if (printk_ratelimit())
868                         dev_err(dev, "WLP: Destination neighbor (Ethernet: "
869                                 "%02x:%02x:%02x:%02x:%02x:%02x, Dev: "
870                                 "%02x:%02x) is not connected. \n", eth_addr[0],
871                                 eth_addr[1], eth_addr[2], eth_addr[3],
872                                 eth_addr[4], eth_addr[5], dev_addr->data[1],
873                                 dev_addr->data[0]);
874                 result = -EINVAL;
875         }
876         d_fnend(6, dev, "wlp %p \n", wlp);
877         return result;
878 }
879
880
881 /**
882  * Prepare skb for neighbor: connect if not already and prep WLP header
883  *
884  * This function is called in interrupt context, but it needs to sleep. We
885  * temporarily stop the net queue to establish the WLP connection.
886  * Setup of the WLP connection and restart of queue is scheduled
887  * on the default work queue.
888  *
889  * run with eda->lock held (spinlock)
890  */
891 int wlp_wss_connect_prep(struct wlp *wlp, struct wlp_eda_node *eda_entry,
892                          void *_skb)
893 {
894         int result = 0;
895         struct device *dev = &wlp->rc->uwb_dev.dev;
896         struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
897         unsigned char *eth_addr = eda_entry->eth_addr;
898         struct sk_buff *skb = _skb;
899         struct wlp_assoc_conn_ctx *conn_ctx;
900
901         d_fnstart(5, dev, "wlp %p\n", wlp);
902         d_printf(5, dev, "To neighbor %02x:%02x with eth "
903                   "%02x:%02x:%02x:%02x:%02x:%02x\n", dev_addr->data[1],
904                   dev_addr->data[0], eth_addr[0], eth_addr[1], eth_addr[2],
905                   eth_addr[3], eth_addr[4], eth_addr[5]);
906         if (eda_entry->state == WLP_WSS_UNCONNECTED) {
907                 /* We don't want any more packets while we set up connection */
908                 BUG_ON(wlp->stop_queue == NULL);
909                 wlp->stop_queue(wlp);
910                 conn_ctx = kmalloc(sizeof(*conn_ctx), GFP_ATOMIC);
911                 if (conn_ctx == NULL) {
912                         if (printk_ratelimit())
913                                 dev_err(dev, "WLP: Unable to allocate memory "
914                                         "for connection handling.\n");
915                         result = -ENOMEM;
916                         goto out;
917                 }
918                 conn_ctx->wlp = wlp;
919                 conn_ctx->skb = skb;
920                 conn_ctx->eda_entry = *eda_entry;
921                 INIT_WORK(&conn_ctx->ws, wlp_wss_connect_send);
922                 schedule_work(&conn_ctx->ws);
923                 result = 1;
924         } else if (eda_entry->state == WLP_WSS_CONNECT_FAILED) {
925                 /* Previous connection attempts failed, don't retry - see
926                  * conditions for connection in WLP 0.99 [7.6.2] */
927                 if (printk_ratelimit())
928                         dev_err(dev, "Could not connect to neighbor "
929                          "previously. Not retrying. \n");
930                 result = -ENONET;
931                 goto out;
932         } else { /* eda_entry->state == WLP_WSS_CONNECTED */
933                 d_printf(5, dev, "Neighbor is connected, preparing frame.\n");
934                 result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
935         }
936 out:
937         d_fnend(5, dev, "wlp %p, result = %d \n", wlp, result);
938         return result;
939 }
940
941 /**
942  * Emulate broadcast: copy skb, send copy to neighbor (connect if not already)
943  *
944  * We need to copy skbs in the case where we emulate broadcast through
945  * unicast. We copy instead of clone because we are modifying the data of
946  * the frame after copying ... clones share data so we cannot emulate
947  * broadcast using clones.
948  *
949  * run with eda->lock held (spinlock)
950  */
951 int wlp_wss_send_copy(struct wlp *wlp, struct wlp_eda_node *eda_entry,
952                       void *_skb)
953 {
954         int result = -ENOMEM;
955         struct device *dev = &wlp->rc->uwb_dev.dev;
956         struct sk_buff *skb = _skb;
957         struct sk_buff *copy;
958         struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
959
960         d_fnstart(5, dev, "to neighbor %02x:%02x, skb (%p) \n",
961                   dev_addr->data[1], dev_addr->data[0], skb);
962         copy = skb_copy(skb, GFP_ATOMIC);
963         if (copy == NULL) {
964                 if (printk_ratelimit())
965                         dev_err(dev, "WLP: Unable to copy skb for "
966                                 "transmission.\n");
967                 goto out;
968         }
969         result = wlp_wss_connect_prep(wlp, eda_entry, copy);
970         if (result < 0) {
971                 if (printk_ratelimit())
972                         dev_err(dev, "WLP: Unable to connect/send skb "
973                                 "to neighbor.\n");
974                 dev_kfree_skb_irq(copy);
975                 goto out;
976         } else if (result == 1)
977                 /* Frame will be transmitted separately */
978                 goto out;
979         BUG_ON(wlp->xmit_frame == NULL);
980         result = wlp->xmit_frame(wlp, copy, dev_addr);
981         if (result < 0) {
982                 if (printk_ratelimit())
983                         dev_err(dev, "WLP: Unable to transmit frame: %d\n",
984                                 result);
985                 if ((result == -ENXIO) && printk_ratelimit())
986                         dev_err(dev, "WLP: Is network interface up? \n");
987                 /* We could try again ... */
988                 dev_kfree_skb_irq(copy);/*we need to free if tx fails */
989         }
990 out:
991         d_fnend(5, dev, "to neighbor %02x:%02x \n", dev_addr->data[1],
992                   dev_addr->data[0]);
993         return result;
994 }
995
996
997 /**
998  * Setup WSS
999  *
1000  * Should be called by network driver after the interface has been given a
1001  * MAC address.
1002  */
1003 int wlp_wss_setup(struct net_device *net_dev, struct wlp_wss *wss)
1004 {
1005         struct wlp *wlp = container_of(wss, struct wlp, wss);
1006         struct device *dev = &wlp->rc->uwb_dev.dev;
1007         int result = 0;
1008         d_fnstart(5, dev, "wss (%p) \n", wss);
1009         mutex_lock(&wss->mutex);
1010         wss->kobj.parent = &net_dev->dev.kobj;
1011         if (!is_valid_ether_addr(net_dev->dev_addr)) {
1012                 dev_err(dev, "WLP: Invalid MAC address. Cannot use for"
1013                        "virtual.\n");
1014                 result = -EINVAL;
1015                 goto out;
1016         }
1017         memcpy(wss->virtual_addr.data, net_dev->dev_addr,
1018                sizeof(wss->virtual_addr.data));
1019 out:
1020         mutex_unlock(&wss->mutex);
1021         d_fnend(5, dev, "wss (%p) \n", wss);
1022         return result;
1023 }
1024 EXPORT_SYMBOL_GPL(wlp_wss_setup);
1025
1026 /**
1027  * Remove WSS
1028  *
1029  * Called by client that configured WSS through wlp_wss_setup(). This
1030  * function is called when client no longer needs WSS, eg. client shuts
1031  * down.
1032  *
1033  * We remove the WLP IE from the beacon before initiating local cleanup.
1034  */
1035 void wlp_wss_remove(struct wlp_wss *wss)
1036 {
1037         struct wlp *wlp = container_of(wss, struct wlp, wss);
1038         struct device *dev = &wlp->rc->uwb_dev.dev;
1039         d_fnstart(5, dev, "wss (%p) \n", wss);
1040         mutex_lock(&wss->mutex);
1041         if (wss->state == WLP_WSS_STATE_ACTIVE)
1042                 uwb_rc_ie_rm(wlp->rc, UWB_IE_WLP);
1043         if (wss->state != WLP_WSS_STATE_NONE) {
1044                 sysfs_remove_group(&wss->kobj, &wss_attr_group);
1045                 kobject_put(&wss->kobj);
1046         }
1047         wss->kobj.parent = NULL;
1048         memset(&wss->virtual_addr, 0, sizeof(wss->virtual_addr));
1049         /* Cleanup EDA cache */
1050         wlp_eda_release(&wlp->eda);
1051         wlp_eda_init(&wlp->eda);
1052         mutex_unlock(&wss->mutex);
1053         d_fnend(5, dev, "wss (%p) \n", wss);
1054 }
1055 EXPORT_SYMBOL_GPL(wlp_wss_remove);