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