Blame view
net/rose/rose_subr.c
12.1 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c Linux-2.6.12-rc2 |
2 |
/* |
1da177e4c Linux-2.6.12-rc2 |
3 4 5 6 7 8 9 10 |
* * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) */ #include <linux/errno.h> #include <linux/types.h> #include <linux/socket.h> #include <linux/in.h> #include <linux/kernel.h> |
1da177e4c Linux-2.6.12-rc2 |
11 12 13 14 |
#include <linux/timer.h> #include <linux/string.h> #include <linux/sockios.h> #include <linux/net.h> |
5a0e3ad6a include cleanup: ... |
15 |
#include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
16 17 18 19 20 |
#include <net/ax25.h> #include <linux/inet.h> #include <linux/netdevice.h> #include <linux/skbuff.h> #include <net/sock.h> |
c752f0739 [TCP]: Move the t... |
21 |
#include <net/tcp_states.h> |
1da177e4c Linux-2.6.12-rc2 |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#include <linux/fcntl.h> #include <linux/mm.h> #include <linux/interrupt.h> #include <net/rose.h> static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose); /* * This routine purges all of the queues of frames. */ void rose_clear_queues(struct sock *sk) { skb_queue_purge(&sk->sk_write_queue); skb_queue_purge(&rose_sk(sk)->ack_queue); } /* * This routine purges the input queue of those frames that have been * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the * SDL diagram. */ void rose_frames_acked(struct sock *sk, unsigned short nr) { struct sk_buff *skb; struct rose_sock *rose = rose_sk(sk); /* * Remove all the ack-ed frames from the ack queue. */ if (rose->va != nr) { while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) { skb = skb_dequeue(&rose->ack_queue); kfree_skb(skb); rose->va = (rose->va + 1) % ROSE_MODULUS; } } } void rose_requeue_frames(struct sock *sk) { struct sk_buff *skb, *skb_prev = NULL; /* * Requeue all the un-ack-ed frames on the output queue to be picked * up by rose_kick. This arrangement handles the possibility of an * empty output queue. */ while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) { if (skb_prev == NULL) skb_queue_head(&sk->sk_write_queue, skb); else |
8728b834b [NET]: Kill skb->... |
73 |
skb_append(skb_prev, skb, &sk->sk_write_queue); |
1da177e4c Linux-2.6.12-rc2 |
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
skb_prev = skb; } } /* * Validate that the value of nr is between va and vs. Return true or * false for testing. */ int rose_validate_nr(struct sock *sk, unsigned short nr) { struct rose_sock *rose = rose_sk(sk); unsigned short vc = rose->va; while (vc != rose->vs) { if (nr == vc) return 1; vc = (vc + 1) % ROSE_MODULUS; } return nr == rose->vs; } /* * This routine is called when the packet layer internally generates a * control frame. */ void rose_write_internal(struct sock *sk, int frametype) { struct rose_sock *rose = rose_sk(sk); struct sk_buff *skb; unsigned char *dptr; unsigned char lci1, lci2; |
e5dcc0c32 net: rose: fix a ... |
105 106 107 |
int maxfaclen = 0; int len, faclen; int reserve; |
1da177e4c Linux-2.6.12-rc2 |
108 |
|
e5dcc0c32 net: rose: fix a ... |
109 110 |
reserve = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1; len = ROSE_MIN_LEN; |
1da177e4c Linux-2.6.12-rc2 |
111 112 113 114 |
switch (frametype) { case ROSE_CALL_REQUEST: len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN; |
e5dcc0c32 net: rose: fix a ... |
115 |
maxfaclen = 256; |
1da177e4c Linux-2.6.12-rc2 |
116 117 118 119 120 121 122 |
break; case ROSE_CALL_ACCEPTED: case ROSE_CLEAR_REQUEST: case ROSE_RESET_REQUEST: len += 2; break; } |
e5dcc0c32 net: rose: fix a ... |
123 124 |
skb = alloc_skb(reserve + len + maxfaclen, GFP_ATOMIC); if (!skb) |
1da177e4c Linux-2.6.12-rc2 |
125 126 127 128 129 |
return; /* * Space for AX.25 header and PID. */ |
e5dcc0c32 net: rose: fix a ... |
130 |
skb_reserve(skb, reserve); |
1da177e4c Linux-2.6.12-rc2 |
131 |
|
e5dcc0c32 net: rose: fix a ... |
132 |
dptr = skb_put(skb, len); |
1da177e4c Linux-2.6.12-rc2 |
133 134 135 136 137 138 139 140 141 |
lci1 = (rose->lci >> 8) & 0x0F; lci2 = (rose->lci >> 0) & 0xFF; switch (frametype) { case ROSE_CALL_REQUEST: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr++ = frametype; |
e0bccd315 rose: Add length ... |
142 |
*dptr++ = ROSE_CALL_REQ_ADDR_LEN_VAL; |
1da177e4c Linux-2.6.12-rc2 |
143 144 145 146 |
memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN); dptr += ROSE_ADDR_LEN; memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN); dptr += ROSE_ADDR_LEN; |
e5dcc0c32 net: rose: fix a ... |
147 148 |
faclen = rose_create_facilities(dptr, rose); skb_put(skb, faclen); |
1da177e4c Linux-2.6.12-rc2 |
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
dptr += faclen; break; case ROSE_CALL_ACCEPTED: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr++ = frametype; *dptr++ = 0x00; /* Address length */ *dptr++ = 0; /* Facilities length */ break; case ROSE_CLEAR_REQUEST: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr++ = frametype; *dptr++ = rose->cause; *dptr++ = rose->diagnostic; break; case ROSE_RESET_REQUEST: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr++ = frametype; *dptr++ = ROSE_DTE_ORIGINATED; *dptr++ = 0; break; case ROSE_RR: case ROSE_RNR: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr = frametype; *dptr++ |= (rose->vr << 5) & 0xE0; break; case ROSE_CLEAR_CONFIRMATION: case ROSE_RESET_CONFIRMATION: *dptr++ = ROSE_GFI | lci1; *dptr++ = lci2; *dptr++ = frametype; break; default: printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X ", frametype); kfree_skb(skb); return; } rose_transmit_link(skb, rose->neighbour); } int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m) { unsigned char *frame; frame = skb->data; *ns = *nr = *q = *d = *m = 0; switch (frame[2]) { case ROSE_CALL_REQUEST: case ROSE_CALL_ACCEPTED: case ROSE_CLEAR_REQUEST: case ROSE_CLEAR_CONFIRMATION: case ROSE_RESET_REQUEST: case ROSE_RESET_CONFIRMATION: return frame[2]; default: break; } if ((frame[2] & 0x1F) == ROSE_RR || (frame[2] & 0x1F) == ROSE_RNR) { *nr = (frame[2] >> 5) & 0x07; return frame[2] & 0x1F; } if ((frame[2] & 0x01) == ROSE_DATA) { *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT; *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT; *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT; *nr = (frame[2] >> 5) & 0x07; *ns = (frame[2] >> 1) & 0x07; return ROSE_DATA; } return ROSE_ILLEGAL; } static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len) { unsigned char *pt; unsigned char l, lg, n = 0; int fac_national_digis_received = 0; do { switch (*p & 0xC0) { case 0x00: |
e0bccd315 rose: Add length ... |
248 249 |
if (len < 2) return -1; |
1da177e4c Linux-2.6.12-rc2 |
250 251 252 253 254 255 |
p += 2; n += 2; len -= 2; break; case 0x40: |
e0bccd315 rose: Add length ... |
256 257 |
if (len < 3) return -1; |
1da177e4c Linux-2.6.12-rc2 |
258 259 260 261 262 263 264 265 |
if (*p == FAC_NATIONAL_RAND) facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF); p += 3; n += 3; len -= 3; break; case 0x80: |
e0bccd315 rose: Add length ... |
266 267 |
if (len < 4) return -1; |
1da177e4c Linux-2.6.12-rc2 |
268 269 270 271 272 273 |
p += 4; n += 4; len -= 4; break; case 0xC0: |
e0bccd315 rose: Add length ... |
274 275 |
if (len < 2) return -1; |
1da177e4c Linux-2.6.12-rc2 |
276 |
l = p[1]; |
e0bccd315 rose: Add length ... |
277 278 |
if (len < 2 + l) return -1; |
1da177e4c Linux-2.6.12-rc2 |
279 280 |
if (*p == FAC_NATIONAL_DEST_DIGI) { if (!fac_national_digis_received) { |
e0bccd315 rose: Add length ... |
281 282 |
if (l < AX25_ADDR_LEN) return -1; |
1da177e4c Linux-2.6.12-rc2 |
283 284 285 286 287 288 |
memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN); facilities->source_ndigis = 1; } } else if (*p == FAC_NATIONAL_SRC_DIGI) { if (!fac_national_digis_received) { |
e0bccd315 rose: Add length ... |
289 290 |
if (l < AX25_ADDR_LEN) return -1; |
1da177e4c Linux-2.6.12-rc2 |
291 292 293 294 295 |
memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN); facilities->dest_ndigis = 1; } } else if (*p == FAC_NATIONAL_FAIL_CALL) { |
e0bccd315 rose: Add length ... |
296 297 |
if (l < AX25_ADDR_LEN) return -1; |
1da177e4c Linux-2.6.12-rc2 |
298 299 300 |
memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN); } else if (*p == FAC_NATIONAL_FAIL_ADD) { |
e0bccd315 rose: Add length ... |
301 302 |
if (l < 1 + ROSE_ADDR_LEN) return -1; |
1da177e4c Linux-2.6.12-rc2 |
303 304 305 |
memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN); } else if (*p == FAC_NATIONAL_DIGIS) { |
e0bccd315 rose: Add length ... |
306 307 |
if (l % AX25_ADDR_LEN) return -1; |
1da177e4c Linux-2.6.12-rc2 |
308 309 310 311 |
fac_national_digis_received = 1; facilities->source_ndigis = 0; facilities->dest_ndigis = 0; for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) { |
be20250c1 ROSE: prevent hea... |
312 313 314 |
if (pt[6] & AX25_HBIT) { if (facilities->dest_ndigis >= ROSE_MAX_DIGIS) return -1; |
1da177e4c Linux-2.6.12-rc2 |
315 |
memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN); |
be20250c1 ROSE: prevent hea... |
316 317 318 |
} else { if (facilities->source_ndigis >= ROSE_MAX_DIGIS) return -1; |
1da177e4c Linux-2.6.12-rc2 |
319 |
memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN); |
be20250c1 ROSE: prevent hea... |
320 |
} |
1da177e4c Linux-2.6.12-rc2 |
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
} } p += l + 2; n += l + 2; len -= l + 2; break; } } while (*p != 0x00 && len > 0); return n; } static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len) { unsigned char l, n = 0; char callsign[11]; do { switch (*p & 0xC0) { case 0x00: |
e0bccd315 rose: Add length ... |
341 342 |
if (len < 2) return -1; |
1da177e4c Linux-2.6.12-rc2 |
343 344 345 346 347 348 |
p += 2; n += 2; len -= 2; break; case 0x40: |
e0bccd315 rose: Add length ... |
349 350 |
if (len < 3) return -1; |
1da177e4c Linux-2.6.12-rc2 |
351 352 353 354 355 356 |
p += 3; n += 3; len -= 3; break; case 0x80: |
e0bccd315 rose: Add length ... |
357 358 |
if (len < 4) return -1; |
1da177e4c Linux-2.6.12-rc2 |
359 360 361 362 363 364 |
p += 4; n += 4; len -= 4; break; case 0xC0: |
e0bccd315 rose: Add length ... |
365 366 |
if (len < 2) return -1; |
1da177e4c Linux-2.6.12-rc2 |
367 |
l = p[1]; |
be20250c1 ROSE: prevent hea... |
368 369 370 371 |
/* Prevent overflows*/ if (l < 10 || l > 20) return -1; |
1da177e4c Linux-2.6.12-rc2 |
372 373 374 375 |
if (*p == FAC_CCITT_DEST_NSAP) { memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN); memcpy(callsign, p + 12, l - 10); callsign[l - 10] = '\0'; |
baed16a7f [AX.25]: Make asc... |
376 |
asc2ax(&facilities->source_call, callsign); |
1da177e4c Linux-2.6.12-rc2 |
377 378 379 380 381 |
} if (*p == FAC_CCITT_SRC_NSAP) { memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN); memcpy(callsign, p + 12, l - 10); callsign[l - 10] = '\0'; |
baed16a7f [AX.25]: Make asc... |
382 |
asc2ax(&facilities->dest_call, callsign); |
1da177e4c Linux-2.6.12-rc2 |
383 384 385 386 387 388 389 390 391 392 |
} p += l + 2; n += l + 2; len -= l + 2; break; } } while (*p != 0x00 && len > 0); return n; } |
e0bccd315 rose: Add length ... |
393 |
int rose_parse_facilities(unsigned char *p, unsigned packet_len, |
1da177e4c Linux-2.6.12-rc2 |
394 395 396 397 398 |
struct rose_facilities_struct *facilities) { int facilities_len, len; facilities_len = *p++; |
95c961747 net: cleanup unsi... |
399 |
if (facilities_len == 0 || (unsigned int)facilities_len > packet_len) |
1da177e4c Linux-2.6.12-rc2 |
400 |
return 0; |
e0bccd315 rose: Add length ... |
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
while (facilities_len >= 3 && *p == 0x00) { facilities_len--; p++; switch (*p) { case FAC_NATIONAL: /* National */ len = rose_parse_national(p + 1, facilities, facilities_len - 1); break; case FAC_CCITT: /* CCITT */ len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1); break; default: printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X ", *p); len = 1; break; } if (len < 0) return 0; if (WARN_ON(len >= facilities_len)) return 0; facilities_len -= len + 1; p += len + 1; |
1da177e4c Linux-2.6.12-rc2 |
427 |
} |
e0bccd315 rose: Add length ... |
428 |
return facilities_len == 0; |
1da177e4c Linux-2.6.12-rc2 |
429 430 431 432 433 434 |
} static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose) { unsigned char *p = buffer + 1; char *callsign; |
f75268cd6 [AX25]: Make ax2a... |
435 |
char buf[11]; |
1da177e4c Linux-2.6.12-rc2 |
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
int len, nb; /* National Facilities */ if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) { *p++ = 0x00; *p++ = FAC_NATIONAL; if (rose->rand != 0) { *p++ = FAC_NATIONAL_RAND; *p++ = (rose->rand >> 8) & 0xFF; *p++ = (rose->rand >> 0) & 0xFF; } /* Sent before older facilities */ if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) { int maxdigi = 0; *p++ = FAC_NATIONAL_DIGIS; *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis); for (nb = 0 ; nb < rose->source_ndigis ; nb++) { if (++maxdigi >= ROSE_MAX_DIGIS) break; memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN); p[6] |= AX25_HBIT; p += AX25_ADDR_LEN; } for (nb = 0 ; nb < rose->dest_ndigis ; nb++) { if (++maxdigi >= ROSE_MAX_DIGIS) break; memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN); p[6] &= ~AX25_HBIT; p += AX25_ADDR_LEN; } } /* For compatibility */ if (rose->source_ndigis > 0) { *p++ = FAC_NATIONAL_SRC_DIGI; *p++ = AX25_ADDR_LEN; memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN); p += AX25_ADDR_LEN; } /* For compatibility */ if (rose->dest_ndigis > 0) { *p++ = FAC_NATIONAL_DEST_DIGI; *p++ = AX25_ADDR_LEN; memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN); p += AX25_ADDR_LEN; } } *p++ = 0x00; *p++ = FAC_CCITT; *p++ = FAC_CCITT_DEST_NSAP; |
f75268cd6 [AX25]: Make ax2a... |
491 |
callsign = ax2asc(buf, &rose->dest_call); |
1da177e4c Linux-2.6.12-rc2 |
492 493 494 495 496 497 498 499 500 501 502 503 504 |
*p++ = strlen(callsign) + 10; *p++ = (strlen(callsign) + 9) * 2; /* ??? */ *p++ = 0x47; *p++ = 0x00; *p++ = 0x11; *p++ = ROSE_ADDR_LEN * 2; memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN); p += ROSE_ADDR_LEN; memcpy(p, callsign, strlen(callsign)); p += strlen(callsign); *p++ = FAC_CCITT_SRC_NSAP; |
f75268cd6 [AX25]: Make ax2a... |
505 |
callsign = ax2asc(buf, &rose->source_call); |
1da177e4c Linux-2.6.12-rc2 |
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
*p++ = strlen(callsign) + 10; *p++ = (strlen(callsign) + 9) * 2; /* ??? */ *p++ = 0x47; *p++ = 0x00; *p++ = 0x11; *p++ = ROSE_ADDR_LEN * 2; memcpy(p, &rose->source_addr, ROSE_ADDR_LEN); p += ROSE_ADDR_LEN; memcpy(p, callsign, strlen(callsign)); p += strlen(callsign); len = p - buffer; buffer[0] = len - 1; return len; } void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic) { struct rose_sock *rose = rose_sk(sk); rose_stop_timer(sk); rose_stop_idletimer(sk); rose_clear_queues(sk); rose->lci = 0; rose->state = ROSE_STATE_0; if (cause != -1) rose->cause = cause; if (diagnostic != -1) rose->diagnostic = diagnostic; sk->sk_state = TCP_CLOSE; sk->sk_err = reason; sk->sk_shutdown |= SEND_SHUTDOWN; if (!sock_flag(sk, SOCK_DEAD)) { sk->sk_state_change(sk); sock_set_flag(sk, SOCK_DEAD); } } |