blob: debda3de4726a8e4a6dc571143b5cdc24306a1a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: irlap_frame.c
4 * Version: 1.0
5 * Description: Build and transmit IrLAP frames
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 10:27:26 1997
9 * Modified at: Wed Jan 5 08:59:04 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 *
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021 * Neither Dag Brattli nor University of Tromsø admit liability nor
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
24 *
25 ********************************************************************/
26
27#include <linux/skbuff.h>
28#include <linux/if.h>
29#include <linux/if_ether.h>
30#include <linux/netdevice.h>
31#include <linux/irda.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <net/pkt_sched.h>
35#include <net/sock.h>
36
37#include <asm/byteorder.h>
38
39#include <net/irda/irda.h>
40#include <net/irda/irda_device.h>
41#include <net/irda/irlap.h>
42#include <net/irda/wrapper.h>
43#include <net/irda/timer.h>
44#include <net/irda/irlap_frame.h>
45#include <net/irda/qos.h>
46
47static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
48 int command);
49
50/*
51 * Function irlap_insert_info (self, skb)
52 *
53 * Insert minimum turnaround time and speed information into the skb. We
54 * need to do this since it's per packet relevant information. Safe to
55 * have this function inlined since it's only called from one place
56 */
57static inline void irlap_insert_info(struct irlap_cb *self,
58 struct sk_buff *skb)
59{
60 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
61
62 /*
63 * Insert MTT (min. turn time) and speed into skb, so that the
64 * device driver knows which settings to use
65 */
66 cb->magic = LAP_MAGIC;
67 cb->mtt = self->mtt_required;
68 cb->next_speed = self->speed;
69
70 /* Reset */
71 self->mtt_required = 0;
72
73 /*
74 * Delay equals negotiated BOFs count, plus the number of BOFs to
75 * force the negotiated minimum turnaround time
76 */
77 cb->xbofs = self->bofs_count;
78 cb->next_xbofs = self->next_bofs;
79 cb->xbofs_delay = self->xbofs_delay;
80
81 /* Reset XBOF's delay (used only for getting min turn time) */
82 self->xbofs_delay = 0;
83 /* Put the correct xbofs value for the next packet */
84 self->bofs_count = self->next_bofs;
85}
86
87/*
88 * Function irlap_queue_xmit (self, skb)
89 *
90 * A little wrapper for dev_queue_xmit, so we can insert some common
91 * code into it.
92 */
93void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
94{
95 /* Some common init stuff */
96 skb->dev = self->netdev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070097 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -070098 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -030099 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 skb->protocol = htons(ETH_P_IRDA);
101 skb->priority = TC_PRIO_BESTEFFORT;
102
103 irlap_insert_info(self, skb);
104
Samuel Ortiz41172522007-07-02 22:55:31 -0700105 if (unlikely(self->mode & IRDA_MODE_MONITOR)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800106 pr_debug("%s(): %s is in monitor mode\n", __func__,
107 self->netdev->name);
Samuel Ortiz41172522007-07-02 22:55:31 -0700108 dev_kfree_skb(skb);
109 return;
110 }
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_queue_xmit(skb);
113}
114
115/*
116 * Function irlap_send_snrm_cmd (void)
117 *
118 * Transmits a connect SNRM command frame
119 */
120void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
121{
122 struct sk_buff *tx_skb;
123 struct snrm_frame *frame;
124 int ret;
125
126 IRDA_ASSERT(self != NULL, return;);
127 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
128
129 /* Allocate frame */
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700130 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
131 IRLAP_NEGOCIATION_PARAMS_LEN,
132 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!tx_skb)
134 return;
135
Johannes Berg4df864c2017-06-16 14:29:21 +0200136 frame = skb_put(tx_skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* Insert connection address field */
139 if (qos)
140 frame->caddr = CMD_FRAME | CBROADCAST;
141 else
142 frame->caddr = CMD_FRAME | self->caddr;
143
144 /* Insert control field */
145 frame->control = SNRM_CMD | PF_BIT;
146
147 /*
Joe Perchescc53ded2007-12-20 14:00:51 -0800148 * If we are establishing a connection then insert QoS parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
150 if (qos) {
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700151 skb_put(tx_skb, 9); /* 25 left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 frame->saddr = cpu_to_le32(self->saddr);
153 frame->daddr = cpu_to_le32(self->daddr);
154
155 frame->ncaddr = self->caddr;
156
157 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
158 if (ret < 0) {
159 dev_kfree_skb(tx_skb);
160 return;
161 }
162 }
163 irlap_queue_xmit(self, tx_skb);
164}
165
166/*
167 * Function irlap_recv_snrm_cmd (skb, info)
168 *
169 * Received SNRM (Set Normal Response Mode) command frame
170 *
171 */
172static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
173 struct irlap_info *info)
174{
175 struct snrm_frame *frame;
176
177 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
178 frame = (struct snrm_frame *) skb->data;
179
180 /* Copy the new connection address ignoring the C/R bit */
181 info->caddr = frame->ncaddr & 0xFE;
182
183 /* Check if the new connection address is valid */
184 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800185 pr_debug("%s(), invalid connection address!\n",
186 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return;
188 }
189
190 /* Copy peer device address */
191 info->daddr = le32_to_cpu(frame->saddr);
192 info->saddr = le32_to_cpu(frame->daddr);
193
194 /* Only accept if addressed directly to us */
195 if (info->saddr != self->saddr) {
Joe Perches955a9d202014-11-11 14:44:57 -0800196 pr_debug("%s(), not addressed to us!\n",
197 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return;
199 }
200 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
201 } else {
202 /* Signal that this SNRM frame does not contain and I-field */
203 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
204 }
205}
206
207/*
208 * Function irlap_send_ua_response_frame (qos)
209 *
210 * Send UA (Unnumbered Acknowledgement) frame
211 *
212 */
213void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
214{
215 struct sk_buff *tx_skb;
216 struct ua_frame *frame;
217 int ret;
218
Joe Perches955a9d202014-11-11 14:44:57 -0800219 pr_debug("%s() <%ld>\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 IRDA_ASSERT(self != NULL, return;);
222 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
223
224 /* Allocate frame */
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700225 tx_skb = alloc_skb(sizeof(struct ua_frame) +
226 IRLAP_NEGOCIATION_PARAMS_LEN,
227 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!tx_skb)
229 return;
230
Johannes Berg4df864c2017-06-16 14:29:21 +0200231 frame = skb_put(tx_skb, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* Build UA response */
234 frame->caddr = self->caddr;
235 frame->control = UA_RSP | PF_BIT;
236
237 frame->saddr = cpu_to_le32(self->saddr);
238 frame->daddr = cpu_to_le32(self->daddr);
239
240 /* Should we send QoS negotiation parameters? */
241 if (qos) {
242 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
243 if (ret < 0) {
244 dev_kfree_skb(tx_skb);
245 return;
246 }
247 }
248
249 irlap_queue_xmit(self, tx_skb);
250}
251
252
253/*
254 * Function irlap_send_dm_frame (void)
255 *
256 * Send disconnected mode (DM) frame
257 *
258 */
259void irlap_send_dm_frame( struct irlap_cb *self)
260{
261 struct sk_buff *tx_skb = NULL;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700262 struct dm_frame *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 IRDA_ASSERT(self != NULL, return;);
265 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
266
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700267 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!tx_skb)
269 return;
270
Johannes Berg4df864c2017-06-16 14:29:21 +0200271 frame = skb_put(tx_skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (self->state == LAP_NDM)
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700274 frame->caddr = CBROADCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 else
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700276 frame->caddr = self->caddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700278 frame->control = DM_RSP | PF_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 irlap_queue_xmit(self, tx_skb);
281}
282
283/*
284 * Function irlap_send_disc_frame (void)
285 *
286 * Send disconnect (DISC) frame
287 *
288 */
289void irlap_send_disc_frame(struct irlap_cb *self)
290{
291 struct sk_buff *tx_skb = NULL;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700292 struct disc_frame *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 IRDA_ASSERT(self != NULL, return;);
295 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
296
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700297 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!tx_skb)
299 return;
300
Johannes Berg4df864c2017-06-16 14:29:21 +0200301 frame = skb_put(tx_skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700303 frame->caddr = self->caddr | CMD_FRAME;
304 frame->control = DISC_CMD | PF_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 irlap_queue_xmit(self, tx_skb);
307}
308
309/*
310 * Function irlap_send_discovery_xid_frame (S, s, command)
311 *
312 * Build and transmit a XID (eXchange station IDentifier) discovery
313 * frame.
314 */
315void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
316 __u8 command, discovery_t *discovery)
317{
318 struct sk_buff *tx_skb = NULL;
319 struct xid_frame *frame;
320 __u32 bcast = BROADCAST;
321 __u8 *info;
322
Joe Perches955a9d202014-11-11 14:44:57 -0800323 pr_debug("%s(), s=%d, S=%d, command=%d\n", __func__,
324 s, S, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 IRDA_ASSERT(self != NULL, return;);
327 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
328 IRDA_ASSERT(discovery != NULL, return;);
329
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700330 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
331 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!tx_skb)
333 return;
334
335 skb_put(tx_skb, 14);
336 frame = (struct xid_frame *) tx_skb->data;
337
338 if (command) {
339 frame->caddr = CBROADCAST | CMD_FRAME;
340 frame->control = XID_CMD | PF_BIT;
341 } else {
342 frame->caddr = CBROADCAST;
343 frame->control = XID_RSP | PF_BIT;
344 }
345 frame->ident = XID_FORMAT;
346
347 frame->saddr = cpu_to_le32(self->saddr);
348
349 if (command)
350 frame->daddr = cpu_to_le32(bcast);
351 else
352 frame->daddr = cpu_to_le32(discovery->data.daddr);
353
354 switch (S) {
355 case 1:
356 frame->flags = 0x00;
357 break;
358 case 6:
359 frame->flags = 0x01;
360 break;
361 case 8:
362 frame->flags = 0x02;
363 break;
364 case 16:
365 frame->flags = 0x03;
366 break;
367 default:
368 frame->flags = 0x02;
369 break;
370 }
371
372 frame->slotnr = s;
373 frame->version = 0x00;
374
375 /*
376 * Provide info for final slot only in commands, and for all
377 * responses. Send the second byte of the hint only if the
378 * EXTENSION bit is set in the first byte.
379 */
380 if (!command || (frame->slotnr == 0xff)) {
381 int len;
382
383 if (discovery->data.hints[0] & HINT_EXTENSION) {
384 info = skb_put(tx_skb, 2);
385 info[0] = discovery->data.hints[0];
386 info[1] = discovery->data.hints[1];
387 } else {
388 info = skb_put(tx_skb, 1);
389 info[0] = discovery->data.hints[0];
390 }
391 info = skb_put(tx_skb, 1);
392 info[0] = discovery->data.charset;
393
394 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
yuan linyub952f4d2017-06-18 22:52:04 +0800395 skb_put_data(tx_skb, discovery->data.info, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 irlap_queue_xmit(self, tx_skb);
398}
399
400/*
401 * Function irlap_recv_discovery_xid_rsp (skb, info)
402 *
403 * Received a XID discovery response
404 *
405 */
406static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
407 struct sk_buff *skb,
408 struct irlap_info *info)
409{
410 struct xid_frame *xid;
411 discovery_t *discovery = NULL;
412 __u8 *discovery_info;
413 char *text;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 IRDA_ASSERT(self != NULL, return;);
416 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
417
418 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
Joe Perches6c910232014-11-11 13:37:30 -0800419 net_err_ratelimited("%s: frame too short!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 xid = (struct xid_frame *) skb->data;
424
425 info->daddr = le32_to_cpu(xid->saddr);
426 info->saddr = le32_to_cpu(xid->daddr);
427
428 /* Make sure frame is addressed to us */
429 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800430 pr_debug("%s(), frame is not addressed to us!\n",
431 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return;
433 }
434
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700435 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
Joe Perches6c910232014-11-11 13:37:30 -0800436 net_warn_ratelimited("%s: kmalloc failed!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 discovery->data.daddr = info->daddr;
441 discovery->data.saddr = self->saddr;
442 discovery->timestamp = jiffies;
443
Joe Perches955a9d202014-11-11 14:44:57 -0800444 pr_debug("%s(), daddr=%08x\n", __func__,
445 discovery->data.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
448
449 /* Get info returned from peer */
450 discovery->data.hints[0] = discovery_info[0];
451 if (discovery_info[0] & HINT_EXTENSION) {
Joe Perches955a9d202014-11-11 14:44:57 -0800452 pr_debug("EXTENSION\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 discovery->data.hints[1] = discovery_info[1];
454 discovery->data.charset = discovery_info[2];
455 text = (char *) &discovery_info[3];
456 } else {
457 discovery->data.hints[1] = 0;
458 discovery->data.charset = discovery_info[1];
459 text = (char *) &discovery_info[2];
460 }
461 /*
462 * Terminate info string, should be safe since this is where the
463 * FCS bytes resides.
464 */
465 skb->data[skb->len] = '\0';
466 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
467 discovery->name_len = strlen(discovery->data.info);
468
469 info->discovery = discovery;
470
471 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
472}
473
474/*
475 * Function irlap_recv_discovery_xid_cmd (skb, info)
476 *
477 * Received a XID discovery command
478 *
479 */
480static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
481 struct sk_buff *skb,
482 struct irlap_info *info)
483{
484 struct xid_frame *xid;
485 discovery_t *discovery = NULL;
486 __u8 *discovery_info;
487 char *text;
488
489 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
Joe Perches6c910232014-11-11 13:37:30 -0800490 net_err_ratelimited("%s: frame too short!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return;
492 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 xid = (struct xid_frame *) skb->data;
495
496 info->daddr = le32_to_cpu(xid->saddr);
497 info->saddr = le32_to_cpu(xid->daddr);
498
499 /* Make sure frame is addressed to us */
500 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800501 pr_debug("%s(), frame is not addressed to us!\n",
502 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return;
504 }
505
506 switch (xid->flags & 0x03) {
507 case 0x00:
508 info->S = 1;
509 break;
510 case 0x01:
511 info->S = 6;
512 break;
513 case 0x02:
514 info->S = 8;
515 break;
516 case 0x03:
517 info->S = 16;
518 break;
519 default:
520 /* Error!! */
521 return;
522 }
523 info->s = xid->slotnr;
524
525 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
526
527 /*
528 * Check if last frame
529 */
530 if (info->s == 0xff) {
531 /* Check if things are sane at this point... */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900532 if((discovery_info == NULL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 !pskb_may_pull(skb, 3)) {
Joe Perches6c910232014-11-11 13:37:30 -0800534 net_err_ratelimited("%s: discovery frame too short!\n",
535 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return;
537 }
538
539 /*
540 * We now have some discovery info to deliver!
541 */
Chen Gangff0102e2013-05-16 23:13:04 +0000542 discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC);
Joe Perches6c910232014-11-11 13:37:30 -0800543 if (!discovery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 discovery->data.daddr = info->daddr;
547 discovery->data.saddr = self->saddr;
548 discovery->timestamp = jiffies;
549
550 discovery->data.hints[0] = discovery_info[0];
551 if (discovery_info[0] & HINT_EXTENSION) {
552 discovery->data.hints[1] = discovery_info[1];
553 discovery->data.charset = discovery_info[2];
554 text = (char *) &discovery_info[3];
555 } else {
556 discovery->data.hints[1] = 0;
557 discovery->data.charset = discovery_info[1];
558 text = (char *) &discovery_info[2];
559 }
560 /*
561 * Terminate string, should be safe since this is where the
562 * FCS bytes resides.
563 */
564 skb->data[skb->len] = '\0';
565 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
566 discovery->name_len = strlen(discovery->data.info);
567
568 info->discovery = discovery;
569 } else
570 info->discovery = NULL;
571
572 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
573}
574
575/*
576 * Function irlap_send_rr_frame (self, command)
577 *
578 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
579 * only possible to send RR frames with the poll bit set.
580 */
581void irlap_send_rr_frame(struct irlap_cb *self, int command)
582{
583 struct sk_buff *tx_skb;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700584 struct rr_frame *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700586 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!tx_skb)
588 return;
589
Johannes Berg4df864c2017-06-16 14:29:21 +0200590 frame = skb_put(tx_skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700592 frame->caddr = self->caddr;
593 frame->caddr |= (command) ? CMD_FRAME : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700595 frame->control = RR | PF_BIT | (self->vr << 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 irlap_queue_xmit(self, tx_skb);
598}
599
600/*
601 * Function irlap_send_rd_frame (self)
602 *
603 * Request disconnect. Used by a secondary station to request the
604 * disconnection of the link.
605 */
606void irlap_send_rd_frame(struct irlap_cb *self)
607{
608 struct sk_buff *tx_skb;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700609 struct rd_frame *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700611 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!tx_skb)
613 return;
614
Johannes Berg4df864c2017-06-16 14:29:21 +0200615 frame = skb_put(tx_skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700617 frame->caddr = self->caddr;
Maks Naumovefd50292014-08-12 11:28:41 -0700618 frame->control = RD_RSP | PF_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 irlap_queue_xmit(self, tx_skb);
621}
622
623/*
624 * Function irlap_recv_rr_frame (skb, info)
625 *
626 * Received RR (Receive Ready) frame from peer station, no harm in
627 * making it inline since its called only from one single place
628 * (irlap_driver_rcv).
629 */
630static inline void irlap_recv_rr_frame(struct irlap_cb *self,
631 struct sk_buff *skb,
632 struct irlap_info *info, int command)
633{
634 info->nr = skb->data[1] >> 5;
635
636 /* Check if this is a command or a response frame */
637 if (command)
638 irlap_do_event(self, RECV_RR_CMD, skb, info);
639 else
640 irlap_do_event(self, RECV_RR_RSP, skb, info);
641}
642
643/*
644 * Function irlap_recv_rnr_frame (self, skb, info)
645 *
646 * Received RNR (Receive Not Ready) frame from peer station
647 *
648 */
649static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
650 struct irlap_info *info, int command)
651{
652 info->nr = skb->data[1] >> 5;
653
Joe Perches955a9d202014-11-11 14:44:57 -0800654 pr_debug("%s(), nr=%d, %ld\n", __func__, info->nr, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (command)
657 irlap_do_event(self, RECV_RNR_CMD, skb, info);
658 else
659 irlap_do_event(self, RECV_RNR_RSP, skb, info);
660}
661
662static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
663 struct irlap_info *info, int command)
664{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 info->nr = skb->data[1] >> 5;
666
667 /* Check if this is a command or a response frame */
668 if (command)
669 irlap_do_event(self, RECV_REJ_CMD, skb, info);
670 else
671 irlap_do_event(self, RECV_REJ_RSP, skb, info);
672}
673
674static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
675 struct irlap_info *info, int command)
676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 info->nr = skb->data[1] >> 5;
678
679 /* Check if this is a command or a response frame */
680 if (command)
681 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
682 else
683 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
684}
685
686static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
687 struct irlap_info *info, int command)
688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Check if this is a command or a response frame */
690 if (command)
691 irlap_do_event(self, RECV_DISC_CMD, skb, info);
692 else
693 irlap_do_event(self, RECV_RD_RSP, skb, info);
694}
695
696/*
697 * Function irlap_recv_ua_frame (skb, frame)
698 *
699 * Received UA (Unnumbered Acknowledgement) frame
700 *
701 */
702static inline void irlap_recv_ua_frame(struct irlap_cb *self,
703 struct sk_buff *skb,
704 struct irlap_info *info)
705{
706 irlap_do_event(self, RECV_UA_RSP, skb, info);
707}
708
709/*
710 * Function irlap_send_data_primary(self, skb)
711 *
712 * Send I-frames as the primary station but without the poll bit set
713 *
714 */
715void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
716{
717 struct sk_buff *tx_skb;
718
719 if (skb->data[1] == I_FRAME) {
720
721 /*
722 * Insert frame sequence number (Vs) in control field before
723 * inserting into transmit window queue.
724 */
725 skb->data[1] = I_FRAME | (self->vs << 1);
726
727 /*
728 * Insert frame in store, in case of retransmissions
729 * Increase skb reference count, see irlap_do_event()
730 */
731 skb_get(skb);
732 skb_queue_tail(&self->wx_list, skb);
733
734 /* Copy buffer */
735 tx_skb = skb_clone(skb, GFP_ATOMIC);
736 if (tx_skb == NULL) {
737 return;
738 }
739
740 self->vs = (self->vs + 1) % 8;
741 self->ack_required = FALSE;
742 self->window -= 1;
743
744 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
745 } else {
Joe Perches955a9d202014-11-11 14:44:57 -0800746 pr_debug("%s(), sending unreliable frame\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
748 self->window -= 1;
749 }
750}
751/*
752 * Function irlap_send_data_primary_poll (self, skb)
753 *
754 * Send I(nformation) frame as primary with poll bit set
755 */
756void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
757{
758 struct sk_buff *tx_skb;
759 int transmission_time;
760
761 /* Stop P timer */
762 del_timer(&self->poll_timer);
763
764 /* Is this reliable or unreliable data? */
765 if (skb->data[1] == I_FRAME) {
766
767 /*
768 * Insert frame sequence number (Vs) in control field before
769 * inserting into transmit window queue.
770 */
771 skb->data[1] = I_FRAME | (self->vs << 1);
772
773 /*
774 * Insert frame in store, in case of retransmissions
775 * Increase skb reference count, see irlap_do_event()
776 */
777 skb_get(skb);
778 skb_queue_tail(&self->wx_list, skb);
779
780 /* Copy buffer */
781 tx_skb = skb_clone(skb, GFP_ATOMIC);
782 if (tx_skb == NULL) {
783 return;
784 }
785
786 /*
787 * Set poll bit if necessary. We do this to the copied
788 * skb, since retransmitted need to set or clear the poll
789 * bit depending on when they are sent.
790 */
791 tx_skb->data[1] |= PF_BIT;
792
793 self->vs = (self->vs + 1) % 8;
794 self->ack_required = FALSE;
795
G. Liakhovetskic0cfe7f2007-06-08 19:15:17 -0700796 irlap_next_state(self, LAP_NRM_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
798 } else {
Joe Perches955a9d202014-11-11 14:44:57 -0800799 pr_debug("%s(), sending unreliable frame\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (self->ack_required) {
802 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
G. Liakhovetskic0cfe7f2007-06-08 19:15:17 -0700803 irlap_next_state(self, LAP_NRM_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 irlap_send_rr_frame(self, CMD_FRAME);
805 self->ack_required = FALSE;
806 } else {
807 skb->data[1] |= PF_BIT;
G. Liakhovetskic0cfe7f2007-06-08 19:15:17 -0700808 irlap_next_state(self, LAP_NRM_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
810 }
811 }
812
813 /* How much time we took for transmission of all frames.
814 * We don't know, so let assume we used the full window. Jean II */
815 transmission_time = self->final_timeout;
816
817 /* Reset parameter so that we can fill next window */
818 self->window = self->window_size;
819
820#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
821 /* Remove what we have not used. Just do a prorata of the
822 * bytes left in window to window capacity.
823 * See max_line_capacities[][] in qos.c for details. Jean II */
824 transmission_time -= (self->final_timeout * self->bytes_left
825 / self->line_capacity);
Joe Perches955a9d202014-11-11 14:44:57 -0800826 pr_debug("%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n",
827 __func__, self->final_timeout, self->bytes_left,
828 self->line_capacity, transmission_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* We are allowed to transmit a maximum number of bytes again. */
831 self->bytes_left = self->line_capacity;
832#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
833
834 /*
835 * The network layer has a intermediate buffer between IrLAP
836 * and the IrDA driver which can contain 8 frames. So, even
837 * though IrLAP is currently sending the *last* frame of the
838 * tx-window, the driver most likely has only just started
839 * sending the *first* frame of the same tx-window.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300840 * I.e. we are always at the very beginning of or Tx window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * Now, we are supposed to set the final timer from the end
842 * of our tx-window to let the other peer reply. So, we need
843 * to add extra time to compensate for the fact that we
844 * are really at the start of tx-window, otherwise the final timer
845 * might expire before he can answer...
846 * Jean II
847 */
848 irlap_start_final_timer(self, self->final_timeout + transmission_time);
849
850 /*
851 * The clever amongst you might ask why we do this adjustement
852 * only here, and not in all the other cases in irlap_event.c.
853 * In all those other case, we only send a very short management
854 * frame (few bytes), so the adjustement would be lost in the
855 * noise...
856 * The exception of course is irlap_resend_rejected_frame().
857 * Jean II */
858}
859
860/*
861 * Function irlap_send_data_secondary_final (self, skb)
862 *
863 * Send I(nformation) frame as secondary with final bit set
864 *
865 */
866void irlap_send_data_secondary_final(struct irlap_cb *self,
867 struct sk_buff *skb)
868{
869 struct sk_buff *tx_skb = NULL;
870
871 IRDA_ASSERT(self != NULL, return;);
872 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
873 IRDA_ASSERT(skb != NULL, return;);
874
875 /* Is this reliable or unreliable data? */
876 if (skb->data[1] == I_FRAME) {
877
878 /*
879 * Insert frame sequence number (Vs) in control field before
880 * inserting into transmit window queue.
881 */
882 skb->data[1] = I_FRAME | (self->vs << 1);
883
884 /*
885 * Insert frame in store, in case of retransmissions
886 * Increase skb reference count, see irlap_do_event()
887 */
888 skb_get(skb);
889 skb_queue_tail(&self->wx_list, skb);
890
891 tx_skb = skb_clone(skb, GFP_ATOMIC);
892 if (tx_skb == NULL) {
893 return;
894 }
895
896 tx_skb->data[1] |= PF_BIT;
897
898 self->vs = (self->vs + 1) % 8;
899 self->ack_required = FALSE;
900
901 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
902 } else {
903 if (self->ack_required) {
904 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
905 irlap_send_rr_frame(self, RSP_FRAME);
906 self->ack_required = FALSE;
907 } else {
908 skb->data[1] |= PF_BIT;
909 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
910 }
911 }
912
913 self->window = self->window_size;
914#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
915 /* We are allowed to transmit a maximum number of bytes again. */
916 self->bytes_left = self->line_capacity;
917#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
918
919 irlap_start_wd_timer(self, self->wd_timeout);
920}
921
922/*
923 * Function irlap_send_data_secondary (self, skb)
924 *
925 * Send I(nformation) frame as secondary without final bit set
926 *
927 */
928void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
929{
930 struct sk_buff *tx_skb = NULL;
931
932 /* Is this reliable or unreliable data? */
933 if (skb->data[1] == I_FRAME) {
934
935 /*
936 * Insert frame sequence number (Vs) in control field before
937 * inserting into transmit window queue.
938 */
939 skb->data[1] = I_FRAME | (self->vs << 1);
940
941 /*
942 * Insert frame in store, in case of retransmissions
943 * Increase skb reference count, see irlap_do_event()
944 */
945 skb_get(skb);
946 skb_queue_tail(&self->wx_list, skb);
947
948 tx_skb = skb_clone(skb, GFP_ATOMIC);
949 if (tx_skb == NULL) {
950 return;
951 }
952
953 self->vs = (self->vs + 1) % 8;
954 self->ack_required = FALSE;
955 self->window -= 1;
956
957 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
958 } else {
959 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
960 self->window -= 1;
961 }
962}
963
964/*
965 * Function irlap_resend_rejected_frames (nr)
966 *
967 * Resend frames which has not been acknowledged. Should be safe to
968 * traverse the list without locking it since this function will only be
969 * called from interrupt context (BH)
970 */
971void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
972{
973 struct sk_buff *tx_skb;
974 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 IRDA_ASSERT(self != NULL, return;);
977 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Resend unacknowledged frame(s) */
David S. Miller528be7f2009-05-28 23:26:33 -0700980 skb_queue_walk(&self->wx_list, skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 irlap_wait_min_turn_around(self, &self->qos_tx);
982
983 /* We copy the skb to be retransmitted since we will have to
984 * modify it. Cloning will confuse packet sniffers
985 */
986 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
987 tx_skb = skb_copy(skb, GFP_ATOMIC);
988 if (!tx_skb) {
Joe Perches955a9d202014-11-11 14:44:57 -0800989 pr_debug("%s(), unable to copy\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* Clear old Nr field + poll bit */
994 tx_skb->data[1] &= 0x0f;
995
996 /*
997 * Set poll bit on the last frame retransmitted
998 */
David S. Miller528be7f2009-05-28 23:26:33 -0700999 if (skb_queue_is_last(&self->wx_list, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1001 else
1002 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1003
1004 irlap_send_i_frame(self, tx_skb, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006#if 0 /* Not yet */
1007 /*
1008 * We can now fill the window with additional data frames
1009 */
David S. Millerb03efcf2005-07-08 14:57:23 -07001010 while (!skb_queue_empty(&self->txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Joe Perches955a9d202014-11-11 14:44:57 -08001012 pr_debug("%s(), sending additional frames!\n", __func__);
David S. Millerb03efcf2005-07-08 14:57:23 -07001013 if (self->window > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 skb = skb_dequeue( &self->txq);
1015 IRDA_ASSERT(skb != NULL, return;);
1016
1017 /*
1018 * If send window > 1 then send frame with pf
1019 * bit cleared
1020 */
1021 if ((self->window > 1) &&
David S. Millerb03efcf2005-07-08 14:57:23 -07001022 !skb_queue_empty(&self->txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 irlap_send_data_primary(self, skb);
1024 } else {
1025 irlap_send_data_primary_poll(self, skb);
1026 }
1027 kfree_skb(skb);
1028 }
1029 }
1030#endif
1031}
1032
1033void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1034{
1035 struct sk_buff *tx_skb;
1036 struct sk_buff *skb;
1037
1038 IRDA_ASSERT(self != NULL, return;);
1039 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1040
1041 /* Resend unacknowledged frame(s) */
1042 skb = skb_peek(&self->wx_list);
1043 if (skb != NULL) {
1044 irlap_wait_min_turn_around(self, &self->qos_tx);
1045
1046 /* We copy the skb to be retransmitted since we will have to
1047 * modify it. Cloning will confuse packet sniffers
1048 */
1049 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1050 tx_skb = skb_copy(skb, GFP_ATOMIC);
1051 if (!tx_skb) {
Joe Perches955a9d202014-11-11 14:44:57 -08001052 pr_debug("%s(), unable to copy\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /* Clear old Nr field + poll bit */
1057 tx_skb->data[1] &= 0x0f;
1058
1059 /* Set poll/final bit */
1060 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1061
1062 irlap_send_i_frame(self, tx_skb, command);
1063 }
1064}
1065
1066/*
1067 * Function irlap_send_ui_frame (self, skb, command)
1068 *
1069 * Contruct and transmit an Unnumbered Information (UI) frame
1070 *
1071 */
1072void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1073 __u8 caddr, int command)
1074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 IRDA_ASSERT(self != NULL, return;);
1076 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1077 IRDA_ASSERT(skb != NULL, return;);
1078
1079 /* Insert connection address */
1080 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1081
1082 irlap_queue_xmit(self, skb);
1083}
1084
1085/*
1086 * Function irlap_send_i_frame (skb)
1087 *
1088 * Contruct and transmit Information (I) frame
1089 */
1090static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1091 int command)
1092{
1093 /* Insert connection address */
1094 skb->data[0] = self->caddr;
1095 skb->data[0] |= (command) ? CMD_FRAME : 0;
1096
1097 /* Insert next to receive (Vr) */
1098 skb->data[1] |= (self->vr << 5); /* insert nr */
1099
1100 irlap_queue_xmit(self, skb);
1101}
1102
1103/*
1104 * Function irlap_recv_i_frame (skb, frame)
1105 *
1106 * Receive and parse an I (Information) frame, no harm in making it inline
1107 * since it's called only from one single place (irlap_driver_rcv).
1108 */
1109static inline void irlap_recv_i_frame(struct irlap_cb *self,
1110 struct sk_buff *skb,
1111 struct irlap_info *info, int command)
1112{
1113 info->nr = skb->data[1] >> 5; /* Next to receive */
1114 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1115 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1116
1117 /* Check if this is a command or a response frame */
1118 if (command)
1119 irlap_do_event(self, RECV_I_CMD, skb, info);
1120 else
1121 irlap_do_event(self, RECV_I_RSP, skb, info);
1122}
1123
1124/*
1125 * Function irlap_recv_ui_frame (self, skb, info)
1126 *
1127 * Receive and parse an Unnumbered Information (UI) frame
1128 *
1129 */
1130static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1131 struct irlap_info *info)
1132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1134
1135 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1136}
1137
1138/*
1139 * Function irlap_recv_frmr_frame (skb, frame)
1140 *
1141 * Received Frame Reject response.
1142 *
1143 */
1144static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1145 struct irlap_info *info)
1146{
1147 __u8 *frame;
1148 int w, x, y, z;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 IRDA_ASSERT(self != NULL, return;);
1151 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1152 IRDA_ASSERT(skb != NULL, return;);
1153 IRDA_ASSERT(info != NULL, return;);
1154
1155 if (!pskb_may_pull(skb, 4)) {
Joe Perches6c910232014-11-11 13:37:30 -08001156 net_err_ratelimited("%s: frame too short!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return;
1158 }
1159
1160 frame = skb->data;
1161
1162 info->nr = frame[2] >> 5; /* Next to receive */
1163 info->pf = frame[2] & PF_BIT; /* Final bit */
1164 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1165
1166 w = frame[3] & 0x01;
1167 x = frame[3] & 0x02;
1168 y = frame[3] & 0x04;
1169 z = frame[3] & 0x08;
1170
1171 if (w) {
Joe Perches955a9d202014-11-11 14:44:57 -08001172 pr_debug("Rejected control field is undefined or not implemented\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 if (x) {
Joe Perches955a9d202014-11-11 14:44:57 -08001175 pr_debug("Rejected control field was invalid because it contained a non permitted I field\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 if (y) {
Joe Perches955a9d202014-11-11 14:44:57 -08001178 pr_debug("Received I field exceeded the maximum negotiated for the existing connection or exceeded the maximum this station supports if no connection exists\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 if (z) {
Joe Perches955a9d202014-11-11 14:44:57 -08001181 pr_debug("Rejected control field control field contained an invalid Nr count\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1184}
1185
1186/*
1187 * Function irlap_send_test_frame (self, daddr)
1188 *
1189 * Send a test frame response
1190 *
1191 */
1192void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1193 struct sk_buff *cmd)
1194{
1195 struct sk_buff *tx_skb;
1196 struct test_frame *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Samuel Ortiz1b0fee72006-09-27 20:06:44 -07001198 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!tx_skb)
1200 return;
1201
1202 /* Broadcast frames must include saddr and daddr fields */
1203 if (caddr == CBROADCAST) {
Johannes Berg4df864c2017-06-16 14:29:21 +02001204 frame = skb_put(tx_skb, sizeof(struct test_frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* Insert the swapped addresses */
1207 frame->saddr = cpu_to_le32(self->saddr);
1208 frame->daddr = cpu_to_le32(daddr);
1209 } else
Johannes Berg4df864c2017-06-16 14:29:21 +02001210 frame = skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 frame->caddr = caddr;
1213 frame->control = TEST_RSP | PF_BIT;
1214
1215 /* Copy info */
yuan linyub952f4d2017-06-18 22:52:04 +08001216 skb_put_data(tx_skb, cmd->data, cmd->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /* Return to sender */
1219 irlap_wait_min_turn_around(self, &self->qos_tx);
1220 irlap_queue_xmit(self, tx_skb);
1221}
1222
1223/*
1224 * Function irlap_recv_test_frame (self, skb)
1225 *
1226 * Receive a test frame
1227 *
1228 */
1229static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1230 struct irlap_info *info, int command)
1231{
1232 struct test_frame *frame;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!pskb_may_pull(skb, sizeof(*frame))) {
Joe Perches6c910232014-11-11 13:37:30 -08001235 net_err_ratelimited("%s: frame too short!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
1237 }
1238 frame = (struct test_frame *) skb->data;
1239
1240 /* Broadcast frames must carry saddr and daddr fields */
1241 if (info->caddr == CBROADCAST) {
1242 if (skb->len < sizeof(struct test_frame)) {
Joe Perches955a9d202014-11-11 14:44:57 -08001243 pr_debug("%s() test frame too short!\n",
1244 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 }
1247
1248 /* Read and swap addresses */
1249 info->daddr = le32_to_cpu(frame->saddr);
1250 info->saddr = le32_to_cpu(frame->daddr);
1251
1252 /* Make sure frame is addressed to us */
1253 if ((info->saddr != self->saddr) &&
1254 (info->saddr != BROADCAST)) {
1255 return;
1256 }
1257 }
1258
1259 if (command)
1260 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1261 else
1262 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1263}
1264
1265/*
1266 * Function irlap_driver_rcv (skb, netdev, ptype)
1267 *
1268 * Called when a frame is received. Dispatches the right receive function
1269 * for processing of the frame.
1270 *
1271 * Note on skb management :
1272 * After calling the higher layers of the IrDA stack, we always
1273 * kfree() the skb, which drop the reference count (and potentially
1274 * destroy it).
1275 * If a higher layer of the stack want to keep the skb around (to put
1276 * in a queue or pass it to the higher layer), it will need to use
1277 * skb_get() to keep a reference on it. This is usually done at the
1278 * LMP level in irlmp.c.
1279 * Jean II
1280 */
1281int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001282 struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct irlap_info info;
1285 struct irlap_cb *self;
1286 int command;
1287 __u8 control;
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001288 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001290 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001291 goto out;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* FIXME: should we get our own field? */
1294 self = (struct irlap_cb *) dev->atalk_ptr;
1295
1296 /* If the net device is down, then IrLAP is gone! */
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001297 if (!self || self->magic != LAP_MAGIC)
1298 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /* We are no longer an "old" protocol, so we need to handle
1301 * share and non linear skbs. This should never happen, so
1302 * we don't need to be clever about it. Jean II */
1303 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
Joe Perches6c910232014-11-11 13:37:30 -08001304 net_err_ratelimited("%s: can't clone shared skb!\n", __func__);
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001305 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /* Check if frame is large enough for parsing */
1309 if (!pskb_may_pull(skb, 2)) {
Joe Perches6c910232014-11-11 13:37:30 -08001310 net_err_ratelimited("%s: frame too short!\n", __func__);
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001311 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
1314 command = skb->data[0] & CMD_FRAME;
1315 info.caddr = skb->data[0] & CBROADCAST;
1316
1317 info.pf = skb->data[1] & PF_BIT;
1318 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1319
1320 control = info.control;
1321
1322 /* First we check if this frame has a valid connection address */
1323 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
Joe Perches955a9d202014-11-11 14:44:57 -08001324 pr_debug("%s(), wrong connection address!\n",
1325 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 goto out;
1327 }
1328 /*
1329 * Optimize for the common case and check if the frame is an
1330 * I(nformation) frame. Only I-frames have bit 0 set to 0
1331 */
1332 if (~control & 0x01) {
1333 irlap_recv_i_frame(self, skb, &info, command);
1334 goto out;
1335 }
1336 /*
1337 * We now check is the frame is an S(upervisory) frame. Only
1338 * S-frames have bit 0 set to 1 and bit 1 set to 0
1339 */
1340 if (~control & 0x02) {
1341 /*
1342 * Received S(upervisory) frame, check which frame type it is
1343 * only the first nibble is of interest
1344 */
1345 switch (control & 0x0f) {
1346 case RR:
1347 irlap_recv_rr_frame(self, skb, &info, command);
1348 break;
1349 case RNR:
1350 irlap_recv_rnr_frame(self, skb, &info, command);
1351 break;
1352 case REJ:
1353 irlap_recv_rej_frame(self, skb, &info, command);
1354 break;
1355 case SREJ:
1356 irlap_recv_srej_frame(self, skb, &info, command);
1357 break;
1358 default:
Joe Perches6c910232014-11-11 13:37:30 -08001359 net_warn_ratelimited("%s: Unknown S-frame %02x received!\n",
1360 __func__, info.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362 }
1363 goto out;
1364 }
1365 /*
1366 * This must be a C(ontrol) frame
1367 */
1368 switch (control) {
1369 case XID_RSP:
1370 irlap_recv_discovery_xid_rsp(self, skb, &info);
1371 break;
1372 case XID_CMD:
1373 irlap_recv_discovery_xid_cmd(self, skb, &info);
1374 break;
1375 case SNRM_CMD:
1376 irlap_recv_snrm_cmd(self, skb, &info);
1377 break;
1378 case DM_RSP:
1379 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1380 break;
1381 case DISC_CMD: /* And RD_RSP since they have the same value */
1382 irlap_recv_disc_frame(self, skb, &info, command);
1383 break;
1384 case TEST_CMD:
1385 irlap_recv_test_frame(self, skb, &info, command);
1386 break;
1387 case UA_RSP:
1388 irlap_recv_ua_frame(self, skb, &info);
1389 break;
1390 case FRMR_RSP:
1391 irlap_recv_frmr_frame(self, skb, &info);
1392 break;
1393 case UI_FRAME:
1394 irlap_recv_ui_frame(self, skb, &info);
1395 break;
1396 default:
Joe Perches6c910232014-11-11 13:37:30 -08001397 net_warn_ratelimited("%s: Unknown frame %02x received!\n",
1398 __func__, info.control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
1400 }
1401out:
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001402 ret = 0;
1403err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Always drop our reference on the skb */
1405 dev_kfree_skb(skb);
Ilpo Järvinene780f1c2008-12-14 23:18:30 -08001406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}