]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /********************************************************************* |
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 | * | |
96de0e25 | 21 | * Neither Dag Brattli nor University of Tromsø admit liability nor |
1da177e4 LT |
22 | * 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> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
1da177e4 LT |
33 | |
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 | ||
47 | static 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 | */ | |
57 | static 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 | */ | |
93 | void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb) | |
94 | { | |
95 | /* Some common init stuff */ | |
96 | skb->dev = self->netdev; | |
459a98ed | 97 | skb_reset_mac_header(skb); |
c1d2bbe1 | 98 | skb_reset_network_header(skb); |
badff6d0 | 99 | skb_reset_transport_header(skb); |
1da177e4 LT |
100 | skb->protocol = htons(ETH_P_IRDA); |
101 | skb->priority = TC_PRIO_BESTEFFORT; | |
102 | ||
103 | irlap_insert_info(self, skb); | |
104 | ||
41172528 | 105 | if (unlikely(self->mode & IRDA_MODE_MONITOR)) { |
955a9d20 JP |
106 | pr_debug("%s(): %s is in monitor mode\n", __func__, |
107 | self->netdev->name); | |
41172528 SO |
108 | dev_kfree_skb(skb); |
109 | return; | |
110 | } | |
111 | ||
1da177e4 LT |
112 | dev_queue_xmit(skb); |
113 | } | |
114 | ||
115 | /* | |
116 | * Function irlap_send_snrm_cmd (void) | |
117 | * | |
118 | * Transmits a connect SNRM command frame | |
119 | */ | |
120 | void 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 */ | |
1b0fee7d SO |
130 | tx_skb = alloc_skb(sizeof(struct snrm_frame) + |
131 | IRLAP_NEGOCIATION_PARAMS_LEN, | |
132 | GFP_ATOMIC); | |
1da177e4 LT |
133 | if (!tx_skb) |
134 | return; | |
135 | ||
4df864c1 | 136 | frame = skb_put(tx_skb, 2); |
1da177e4 LT |
137 | |
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 | /* | |
cc53ded2 | 148 | * If we are establishing a connection then insert QoS parameters |
1da177e4 LT |
149 | */ |
150 | if (qos) { | |
1b0fee7d | 151 | skb_put(tx_skb, 9); /* 25 left */ |
1da177e4 LT |
152 | 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 | */ | |
172 | static 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)) { | |
955a9d20 JP |
185 | pr_debug("%s(), invalid connection address!\n", |
186 | __func__); | |
1da177e4 LT |
187 | 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) { | |
955a9d20 JP |
196 | pr_debug("%s(), not addressed to us!\n", |
197 | __func__); | |
1da177e4 LT |
198 | 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 | */ | |
213 | void 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 | ||
955a9d20 | 219 | pr_debug("%s() <%ld>\n", __func__, jiffies); |
1da177e4 LT |
220 | |
221 | IRDA_ASSERT(self != NULL, return;); | |
222 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
223 | ||
224 | /* Allocate frame */ | |
1b0fee7d SO |
225 | tx_skb = alloc_skb(sizeof(struct ua_frame) + |
226 | IRLAP_NEGOCIATION_PARAMS_LEN, | |
227 | GFP_ATOMIC); | |
1da177e4 LT |
228 | if (!tx_skb) |
229 | return; | |
230 | ||
4df864c1 | 231 | frame = skb_put(tx_skb, 10); |
1da177e4 LT |
232 | |
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 | */ | |
259 | void irlap_send_dm_frame( struct irlap_cb *self) | |
260 | { | |
261 | struct sk_buff *tx_skb = NULL; | |
1b0fee7d | 262 | struct dm_frame *frame; |
1da177e4 LT |
263 | |
264 | IRDA_ASSERT(self != NULL, return;); | |
265 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
266 | ||
1b0fee7d | 267 | tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC); |
1da177e4 LT |
268 | if (!tx_skb) |
269 | return; | |
270 | ||
4df864c1 | 271 | frame = skb_put(tx_skb, 2); |
1da177e4 LT |
272 | |
273 | if (self->state == LAP_NDM) | |
1b0fee7d | 274 | frame->caddr = CBROADCAST; |
1da177e4 | 275 | else |
1b0fee7d | 276 | frame->caddr = self->caddr; |
1da177e4 | 277 | |
1b0fee7d | 278 | frame->control = DM_RSP | PF_BIT; |
1da177e4 LT |
279 | |
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 | */ | |
289 | void irlap_send_disc_frame(struct irlap_cb *self) | |
290 | { | |
291 | struct sk_buff *tx_skb = NULL; | |
1b0fee7d | 292 | struct disc_frame *frame; |
1da177e4 | 293 | |
1da177e4 LT |
294 | IRDA_ASSERT(self != NULL, return;); |
295 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
296 | ||
1b0fee7d | 297 | tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC); |
1da177e4 LT |
298 | if (!tx_skb) |
299 | return; | |
300 | ||
4df864c1 | 301 | frame = skb_put(tx_skb, 2); |
1da177e4 | 302 | |
1b0fee7d SO |
303 | frame->caddr = self->caddr | CMD_FRAME; |
304 | frame->control = DISC_CMD | PF_BIT; | |
1da177e4 LT |
305 | |
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 | */ | |
315 | void 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 | ||
955a9d20 JP |
323 | pr_debug("%s(), s=%d, S=%d, command=%d\n", __func__, |
324 | s, S, command); | |
1da177e4 LT |
325 | |
326 | IRDA_ASSERT(self != NULL, return;); | |
327 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
328 | IRDA_ASSERT(discovery != NULL, return;); | |
329 | ||
1b0fee7d SO |
330 | tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN, |
331 | GFP_ATOMIC); | |
1da177e4 LT |
332 | 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)); | |
b952f4df | 395 | skb_put_data(tx_skb, discovery->data.info, len); |
1da177e4 LT |
396 | } |
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 | */ | |
406 | static 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 | ||
1da177e4 LT |
415 | IRDA_ASSERT(self != NULL, return;); |
416 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
417 | ||
418 | if (!pskb_may_pull(skb, sizeof(struct xid_frame))) { | |
6c91023d | 419 | net_err_ratelimited("%s: frame too short!\n", __func__); |
1da177e4 LT |
420 | return; |
421 | } | |
6819bc2e | 422 | |
1da177e4 LT |
423 | 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)) { | |
955a9d20 JP |
430 | pr_debug("%s(), frame is not addressed to us!\n", |
431 | __func__); | |
1da177e4 LT |
432 | return; |
433 | } | |
434 | ||
0da974f4 | 435 | if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) { |
6c91023d | 436 | net_warn_ratelimited("%s: kmalloc failed!\n", __func__); |
1da177e4 LT |
437 | return; |
438 | } | |
1da177e4 LT |
439 | |
440 | discovery->data.daddr = info->daddr; | |
441 | discovery->data.saddr = self->saddr; | |
442 | discovery->timestamp = jiffies; | |
443 | ||
955a9d20 JP |
444 | pr_debug("%s(), daddr=%08x\n", __func__, |
445 | discovery->data.daddr); | |
1da177e4 LT |
446 | |
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) { | |
955a9d20 | 452 | pr_debug("EXTENSION\n"); |
1da177e4 LT |
453 | 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 | */ | |
480 | static 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))) { | |
6c91023d | 490 | net_err_ratelimited("%s: frame too short!\n", __func__); |
1da177e4 LT |
491 | return; |
492 | } | |
6819bc2e | 493 | |
1da177e4 LT |
494 | 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)) { | |
955a9d20 JP |
501 | pr_debug("%s(), frame is not addressed to us!\n", |
502 | __func__); | |
1da177e4 LT |
503 | 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... */ | |
6819bc2e | 532 | if((discovery_info == NULL) || |
1da177e4 | 533 | !pskb_may_pull(skb, 3)) { |
6c91023d JP |
534 | net_err_ratelimited("%s: discovery frame too short!\n", |
535 | __func__); | |
1da177e4 LT |
536 | return; |
537 | } | |
538 | ||
539 | /* | |
540 | * We now have some discovery info to deliver! | |
541 | */ | |
ff0102ee | 542 | discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC); |
6c91023d | 543 | if (!discovery) |
1da177e4 | 544 | return; |
1da177e4 LT |
545 | |
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 | */ | |
581 | void irlap_send_rr_frame(struct irlap_cb *self, int command) | |
582 | { | |
583 | struct sk_buff *tx_skb; | |
1b0fee7d | 584 | struct rr_frame *frame; |
1da177e4 | 585 | |
1b0fee7d | 586 | tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC); |
1da177e4 LT |
587 | if (!tx_skb) |
588 | return; | |
589 | ||
4df864c1 | 590 | frame = skb_put(tx_skb, 2); |
1da177e4 | 591 | |
1b0fee7d SO |
592 | frame->caddr = self->caddr; |
593 | frame->caddr |= (command) ? CMD_FRAME : 0; | |
1da177e4 | 594 | |
1b0fee7d | 595 | frame->control = RR | PF_BIT | (self->vr << 5); |
1da177e4 LT |
596 | |
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 | */ | |
606 | void irlap_send_rd_frame(struct irlap_cb *self) | |
607 | { | |
608 | struct sk_buff *tx_skb; | |
1b0fee7d | 609 | struct rd_frame *frame; |
1da177e4 | 610 | |
1b0fee7d | 611 | tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC); |
1da177e4 LT |
612 | if (!tx_skb) |
613 | return; | |
614 | ||
4df864c1 | 615 | frame = skb_put(tx_skb, 2); |
1da177e4 | 616 | |
1b0fee7d | 617 | frame->caddr = self->caddr; |
efd50290 | 618 | frame->control = RD_RSP | PF_BIT; |
1da177e4 LT |
619 | |
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 | */ | |
630 | static 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 | */ | |
649 | static 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 | ||
955a9d20 | 654 | pr_debug("%s(), nr=%d, %ld\n", __func__, info->nr, jiffies); |
1da177e4 LT |
655 | |
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 | ||
662 | static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb, | |
663 | struct irlap_info *info, int command) | |
664 | { | |
1da177e4 LT |
665 | 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 | ||
674 | static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb, | |
675 | struct irlap_info *info, int command) | |
676 | { | |
1da177e4 LT |
677 | 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 | ||
686 | static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb, | |
687 | struct irlap_info *info, int command) | |
688 | { | |
1da177e4 LT |
689 | /* 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 | */ | |
702 | static 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 | */ | |
715 | void 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 { | |
955a9d20 | 746 | pr_debug("%s(), sending unreliable frame\n", __func__); |
1da177e4 LT |
747 | 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 | */ | |
756 | void 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 | ||
c0cfe7fa | 796 | irlap_next_state(self, LAP_NRM_P); |
1da177e4 LT |
797 | irlap_send_i_frame(self, tx_skb, CMD_FRAME); |
798 | } else { | |
955a9d20 | 799 | pr_debug("%s(), sending unreliable frame\n", __func__); |
1da177e4 LT |
800 | |
801 | if (self->ack_required) { | |
802 | irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME); | |
c0cfe7fa | 803 | irlap_next_state(self, LAP_NRM_P); |
1da177e4 LT |
804 | irlap_send_rr_frame(self, CMD_FRAME); |
805 | self->ack_required = FALSE; | |
806 | } else { | |
807 | skb->data[1] |= PF_BIT; | |
c0cfe7fa | 808 | irlap_next_state(self, LAP_NRM_P); |
1da177e4 LT |
809 | 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); | |
955a9d20 JP |
826 | 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); | |
1da177e4 LT |
829 | |
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. | |
25985edc | 840 | * I.e. we are always at the very beginning of or Tx window. |
1da177e4 LT |
841 | * 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 | */ | |
866 | void 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 | */ | |
928 | void 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 | */ | |
971 | void irlap_resend_rejected_frames(struct irlap_cb *self, int command) | |
972 | { | |
973 | struct sk_buff *tx_skb; | |
974 | struct sk_buff *skb; | |
1da177e4 LT |
975 | |
976 | IRDA_ASSERT(self != NULL, return;); | |
977 | IRDA_ASSERT(self->magic == LAP_MAGIC, return;); | |
978 | ||
1da177e4 | 979 | /* Resend unacknowledged frame(s) */ |
528be7ff | 980 | skb_queue_walk(&self->wx_list, skb) { |
1da177e4 LT |
981 | 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) { | |
955a9d20 | 989 | pr_debug("%s(), unable to copy\n", __func__); |
1da177e4 LT |
990 | return; |
991 | } | |
1da177e4 LT |
992 | |
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 | */ | |
528be7ff | 999 | if (skb_queue_is_last(&self->wx_list, skb)) |
1da177e4 LT |
1000 | 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); | |
1da177e4 LT |
1005 | } |
1006 | #if 0 /* Not yet */ | |
1007 | /* | |
1008 | * We can now fill the window with additional data frames | |
1009 | */ | |
b03efcfb | 1010 | while (!skb_queue_empty(&self->txq)) { |
1da177e4 | 1011 | |
955a9d20 | 1012 | pr_debug("%s(), sending additional frames!\n", __func__); |
b03efcfb | 1013 | if (self->window > 0) { |
1da177e4 LT |
1014 | 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) && | |
b03efcfb | 1022 | !skb_queue_empty(&self->txq)) { |
1da177e4 LT |
1023 | 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 | ||
1033 | void 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) { | |
955a9d20 | 1052 | pr_debug("%s(), unable to copy\n", __func__); |
1da177e4 LT |
1053 | return; |
1054 | } | |
1da177e4 LT |
1055 | |
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 | */ | |
1072 | void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb, | |
1073 | __u8 caddr, int command) | |
1074 | { | |
1da177e4 LT |
1075 | 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 | */ | |
1090 | static 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 | */ | |
1109 | static 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 | */ | |
1130 | static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb, | |
1131 | struct irlap_info *info) | |
1132 | { | |
1da177e4 LT |
1133 | 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 | */ | |
1144 | static 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 | ||
1da177e4 LT |
1150 | 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)) { | |
6c91023d | 1156 | net_err_ratelimited("%s: frame too short!\n", __func__); |
1da177e4 LT |
1157 | 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) { | |
955a9d20 | 1172 | pr_debug("Rejected control field is undefined or not implemented\n"); |
1da177e4 LT |
1173 | } |
1174 | if (x) { | |
955a9d20 | 1175 | pr_debug("Rejected control field was invalid because it contained a non permitted I field\n"); |
1da177e4 LT |
1176 | } |
1177 | if (y) { | |
955a9d20 | 1178 | 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"); |
1da177e4 LT |
1179 | } |
1180 | if (z) { | |
955a9d20 | 1181 | pr_debug("Rejected control field control field contained an invalid Nr count\n"); |
1da177e4 LT |
1182 | } |
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 | */ | |
1192 | void 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; | |
1da177e4 | 1197 | |
1b0fee7d | 1198 | tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC); |
1da177e4 LT |
1199 | if (!tx_skb) |
1200 | return; | |
1201 | ||
1202 | /* Broadcast frames must include saddr and daddr fields */ | |
1203 | if (caddr == CBROADCAST) { | |
4df864c1 | 1204 | frame = skb_put(tx_skb, sizeof(struct test_frame)); |
1da177e4 LT |
1205 | |
1206 | /* Insert the swapped addresses */ | |
1207 | frame->saddr = cpu_to_le32(self->saddr); | |
1208 | frame->daddr = cpu_to_le32(daddr); | |
1209 | } else | |
4df864c1 | 1210 | frame = skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER); |
1da177e4 LT |
1211 | |
1212 | frame->caddr = caddr; | |
1213 | frame->control = TEST_RSP | PF_BIT; | |
1214 | ||
1215 | /* Copy info */ | |
b952f4df | 1216 | skb_put_data(tx_skb, cmd->data, cmd->len); |
1da177e4 LT |
1217 | |
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 | */ | |
1229 | static 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 | ||
1da177e4 | 1234 | if (!pskb_may_pull(skb, sizeof(*frame))) { |
6c91023d | 1235 | net_err_ratelimited("%s: frame too short!\n", __func__); |
1da177e4 LT |
1236 | 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)) { | |
955a9d20 JP |
1243 | pr_debug("%s() test frame too short!\n", |
1244 | __func__); | |
1da177e4 LT |
1245 | 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 | */ | |
1281 | int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev, | |
f2ccd8fa | 1282 | struct packet_type *ptype, struct net_device *orig_dev) |
1da177e4 LT |
1283 | { |
1284 | struct irlap_info info; | |
1285 | struct irlap_cb *self; | |
1286 | int command; | |
1287 | __u8 control; | |
e780f1c3 | 1288 | int ret = -1; |
1da177e4 | 1289 | |
721499e8 | 1290 | if (!net_eq(dev_net(dev), &init_net)) |
e730c155 EB |
1291 | goto out; |
1292 | ||
1da177e4 LT |
1293 | /* 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! */ | |
e780f1c3 IJ |
1297 | if (!self || self->magic != LAP_MAGIC) |
1298 | goto err; | |
1da177e4 LT |
1299 | |
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) { | |
6c91023d | 1304 | net_err_ratelimited("%s: can't clone shared skb!\n", __func__); |
e780f1c3 | 1305 | goto err; |
1da177e4 LT |
1306 | } |
1307 | ||
1308 | /* Check if frame is large enough for parsing */ | |
1309 | if (!pskb_may_pull(skb, 2)) { | |
6c91023d | 1310 | net_err_ratelimited("%s: frame too short!\n", __func__); |
e780f1c3 | 1311 | goto err; |
1da177e4 LT |
1312 | } |
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)) { | |
955a9d20 JP |
1324 | pr_debug("%s(), wrong connection address!\n", |
1325 | __func__); | |
1da177e4 LT |
1326 | 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: | |
6c91023d JP |
1359 | net_warn_ratelimited("%s: Unknown S-frame %02x received!\n", |
1360 | __func__, info.control); | |
1da177e4 LT |
1361 | 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: | |
6c91023d JP |
1397 | net_warn_ratelimited("%s: Unknown frame %02x received!\n", |
1398 | __func__, info.control); | |
1da177e4 LT |
1399 | break; |
1400 | } | |
1401 | out: | |
e780f1c3 IJ |
1402 | ret = 0; |
1403 | err: | |
1da177e4 LT |
1404 | /* Always drop our reference on the skb */ |
1405 | dev_kfree_skb(skb); | |
e780f1c3 | 1406 | return ret; |
1da177e4 | 1407 | } |