2 * Synchronous Frame Relay link level subroutines.
3 * ANSI T1.617-compaible link management signaling
4 * implemented for Frame Relay mode.
5 * Cisco-type Frame Relay framing added, thanks Alex Tutubalin.
6 * Only one DLCI per channel for now.
8 * Copyright (C) 1994-2000 Cronyx Engineering.
9 * Author: Serge Vakulenko, <vak@cronyx.ru>
11 * Copyright (C) 1999-2004 Cronyx Engineering.
12 * Author: Kurakin Roman, <rik@cronyx.ru>
14 * This software is distributed with NO WARRANTIES, not even the implied
15 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 * Authors grant any other persons or organisations a permission to use,
18 * modify and redistribute this software in source and binary forms,
19 * as long as this message is kept with the software, all derivative
20 * works or modified versions.
22 * $Cronyx Id: if_spppfr.c,v 1.1.2.10 2004/06/29 09:02:30 rik Exp $
26 #include <sys/param.h>
28 #if defined(__FreeBSD__)
30 #include "opt_inet6.h"
35 # include "opt_inet.h"
36 # include "opt_inet6.h"
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/sockio.h>
45 #include <sys/socket.h>
46 #include <sys/syslog.h>
47 #if defined(__FreeBSD__)
48 #include <sys/random.h>
50 #include <sys/malloc.h>
53 #if defined (__OpenBSD__)
60 #include <net/if_var.h>
61 #include <net/netisr.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <net/slcompress.h>
69 #if defined (__NetBSD__) || defined (__OpenBSD__)
70 #include <machine/cpu.h> /* XXX for softnet */
73 #include <machine/stdarg.h>
75 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #include <netinet/tcp.h>
81 #if defined (__FreeBSD__) || defined (__OpenBSD__)
82 # include <netinet/if_ether.h>
84 # include <net/ethertypes.h>
87 #include <net/if_sppp.h>
92 #define FR_UI 0x03 /* Unnumbered Information */
93 #define FR_IP 0xCC /* IP protocol identifier */
94 #define FR_PADDING 0x00 /* NLPID padding */
95 #define FR_SIGNALING 0x08 /* Q.933/T1.617 signaling identifier */
96 #define FR_SNAP 0x80 /* NLPID snap */
101 #define FR_DE 0x02 /* discard eligibility */
102 #define FR_FECN 0x04 /* forward notification */
103 #define FR_BECN 0x08 /* backward notification */
106 * Signaling message types.
108 #define FR_MSG_ENQUIRY 0x75 /* status enquiry */
109 #define FR_MSG_STATUS 0x7d /* status */
111 #define FR_ENQUIRY_SIZE 14
114 * Message field types.
116 #define FR_FLD_RTYPE 0x01 /* report type */
117 #define FR_FLD_VERIFY 0x03 /* link verification */
118 #define FR_FLD_PVC 0x07 /* PVC status */
119 #define FR_FLD_LSHIFT5 0x95 /* locking shift 5 */
124 #define FR_RTYPE_FULL 0 /* full status */
125 #define FR_RTYPE_SHORT 1 /* link verification only */
126 #define FR_RTYPE_SINGLE 2 /* single PVC status */
128 /* PVC status field. */
129 #define FR_DLCI_DELETE 0x04 /* PVC is deleted */
130 #define FR_DLCI_ACTIVE 0x02 /* PVC is operational */
131 #define FR_DLCI_NEW 0x08 /* PVC is new */
134 unsigned short htype; /* hardware type = ARPHRD_FRELAY */
135 unsigned short ptype; /* protocol type = ETHERTYPE_IP */
136 unsigned char halen; /* hardware address length = 2 */
137 unsigned char palen; /* protocol address length = 4 */
138 unsigned short op; /* ARP/RARP/InARP request/reply */
139 unsigned short hsource; /* hardware source address */
140 unsigned short psource1; /* protocol source */
141 unsigned short psource2;
142 unsigned short htarget; /* hardware target address */
143 unsigned short ptarget1; /* protocol target */
144 unsigned short ptarget2;
147 #if defined(__FreeBSD__) && __FreeBSD_version < 501113
148 #define SPP_FMT "%s%d: "
149 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
151 #define SPP_FMT "%s: "
152 #define SPP_ARGS(ifp) (ifp)->if_xname
155 /* almost every function needs these */
157 struct ifnet *ifp = SP2IFP(sp); \
158 int debug = ifp->if_flags & IFF_DEBUG
160 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req, u_short addr);
161 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len);
163 void sppp_fr_input (struct sppp *sp, struct mbuf *m)
166 u_char *h = mtod (m, u_char*);
168 int dlci, hlen, proto;
170 /* Get the DLCI number. */
171 if (m->m_pkthdr.len < 10) {
175 dlci = (h[0] << 2 & 0x3f0) | (h[1] >> 4 & 0x0f);
177 /* Process signaling packets. */
179 sppp_fr_signal (sp, h, m->m_pkthdr.len);
184 if (dlci != sp->fr_dlci) {
186 printf (SPP_FMT "Received packet from invalid DLCI %d\n",
187 SPP_ARGS(ifp), dlci);
191 /* Process the packet. */
192 if (ntohs (*(short*) (h+2)) == ETHERTYPE_IP) {
193 /* Prehistoric IP framing? */
199 printf (SPP_FMT "Invalid frame relay header flag 0x%02x\n",
200 SPP_ARGS(ifp), h[2]);
206 printf (SPP_FMT "Unsupported NLPID 0x%02x\n",
207 SPP_ARGS(ifp), h[3]);
211 if (h[4] != FR_SNAP) {
213 printf (SPP_FMT "Bad NLPID 0x%02x\n",
214 SPP_ARGS(ifp), h[4]);
217 if (h[5] || h[6] || h[7]) {
219 printf (SPP_FMT "Bad OID 0x%02x-0x%02x-0x%02x\n",
224 proto = ntohs (*(short*) (h+8));
225 if (proto == ETHERTYPE_ARP) {
226 /* Process the ARP request. */
227 if (m->m_pkthdr.len != 10 + sizeof (struct arp_req)) {
229 printf (SPP_FMT "Bad ARP request size = %d bytes\n",
234 sppp_fr_arp (sp, (struct arp_req*) (h + 10),
243 proto = ETHERTYPE_IP;
248 /* Remove frame relay header. */
253 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
254 drop: if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
255 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
265 if (! (ifp->if_flags & IFF_UP))
268 M_SETFIB(m, ifp->if_fib);
271 if (netisr_queue(isr, m)) { /* (0) on success. */
273 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
279 * Add the frame relay header to the packet.
280 * For IP the header length is 4 bytes,
281 * for all other protocols - 10 bytes (RFC 1490).
283 struct mbuf *sppp_fr_header (struct sppp *sp, struct mbuf *m,
290 /* Prepend the space for Frame Relay header. */
291 hlen = (family == AF_INET) ? 4 : 10;
292 M_PREPEND (m, hlen, M_NOWAIT);
295 h = mtod (m, u_char*);
297 /* Fill the header. */
298 h[0] = sp->fr_dlci >> 2 & 0xfc;
299 h[1] = sp->fr_dlci << 4 | 1;
305 printf (SPP_FMT "Cannot handle address family %d\n",
306 SPP_ARGS(ifp), family);
311 #if 0 /* Crashes on fragmented packets */
313 * Set the discard eligibility bit, if:
314 * 1) no fragmentation
315 * 2) length > 400 bytes
316 * 3a) the protocol is UDP or
317 * 3b) TCP data (no control bits)
320 struct ip *ip = (struct ip*) (h + hlen);
321 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
323 if (! (ip->ip_off & ~IP_DF) && ip->ip_len > 400 &&
324 (ip->ip_p == IPPROTO_UDP ||
325 ip->ip_p == IPPROTO_TCP && ! tcp->th_flags))
343 *(short*) (h+8) = htons(type);
348 * Send periodical frame relay link verification messages via DLCI 0.
349 * Called every 10 seconds (default value of T391 timer is 10 sec).
350 * Every 6-th message is a full status request
351 * (default value of N391 counter is 6).
353 void sppp_fr_keepalive (struct sppp *sp)
356 unsigned char *h, *p;
359 MGETHDR (m, M_NOWAIT, MT_DATA);
362 m->m_pkthdr.rcvif = 0;
364 h = mtod (m, u_char*);
366 *p++ = 0; /* DLCI = 0 */
369 *p++ = FR_SIGNALING; /* NLPID = UNI call control */
371 *p++ = 0; /* call reference length = 0 */
372 *p++ = FR_MSG_ENQUIRY; /* message type = status enquiry */
374 *p++ = FR_FLD_LSHIFT5; /* locking shift 5 */
376 *p++ = FR_FLD_RTYPE; /* report type field */
377 *p++ = 1; /* report type length = 1 */
378 if (sp->pp_seq[IDX_LCP] % 6)
379 *p++ = FR_RTYPE_SHORT; /* link verification only */
381 *p++ = FR_RTYPE_FULL; /* full status needed */
383 if (sp->pp_seq[IDX_LCP] >= 255)
384 sp->pp_seq[IDX_LCP] = 0;
385 *p++ = FR_FLD_VERIFY; /* link verification type field */
386 *p++ = 2; /* link verification field length = 2 */
387 *p++ = ++sp->pp_seq[IDX_LCP]; /* our sequence number */
388 *p++ = sp->pp_rseq[IDX_LCP]; /* last received sequence number */
390 m->m_pkthdr.len = m->m_len = p - h;
392 printf (SPP_FMT "send lmi packet, seq=%d, rseq=%d\n",
393 SPP_ARGS(ifp), (u_char) sp->pp_seq[IDX_LCP],
394 (u_char) sp->pp_rseq[IDX_LCP]);
396 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
397 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
401 * Process the frame relay Inverse ARP request.
403 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req,
404 u_short his_hardware_address)
408 struct arp_req *reply;
410 u_short my_hardware_address;
411 u_long his_ip_address, my_ip_address;
413 if ((ntohs (req->htype) != ARPHRD_FRELAY ||
414 ntohs (req->htype) != 16) || /* for BayNetworks routers */
415 ntohs (req->ptype) != ETHERTYPE_IP) {
417 printf (SPP_FMT "Invalid ARP hardware/protocol type = 0x%x/0x%x\n",
419 ntohs (req->htype), ntohs (req->ptype));
422 if (req->halen != 2 || req->palen != 4) {
424 printf (SPP_FMT "Invalid ARP hardware/protocol address length = %d/%d\n",
426 req->halen, req->palen);
429 switch (ntohs (req->op)) {
432 printf (SPP_FMT "Invalid ARP op = 0x%x\n",
433 SPP_ARGS(ifp), ntohs (req->op));
440 case ARPOP_INVREQUEST:
441 my_hardware_address = ntohs (req->htarget);
442 his_ip_address = ntohs (req->psource1) << 16 |
443 ntohs (req->psource2);
444 my_ip_address = ntohs (req->ptarget1) << 16 |
445 ntohs (req->ptarget2);
449 printf (SPP_FMT "got ARP request, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
450 SPP_ARGS(ifp), ntohs (req->hsource),
451 (unsigned char) (his_ip_address >> 24),
452 (unsigned char) (his_ip_address >> 16),
453 (unsigned char) (his_ip_address >> 8),
454 (unsigned char) his_ip_address,
456 (unsigned char) (my_ip_address >> 24),
457 (unsigned char) (my_ip_address >> 16),
458 (unsigned char) (my_ip_address >> 8),
459 (unsigned char) my_ip_address);
461 sppp_get_ip_addrs (sp, &my_ip_address, 0, 0);
463 return; /* nothing to reply */
466 printf (SPP_FMT "send ARP reply, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
467 SPP_ARGS(ifp), my_hardware_address,
468 (unsigned char) (my_ip_address >> 24),
469 (unsigned char) (my_ip_address >> 16),
470 (unsigned char) (my_ip_address >> 8),
471 (unsigned char) my_ip_address,
472 his_hardware_address,
473 (unsigned char) (his_ip_address >> 24),
474 (unsigned char) (his_ip_address >> 16),
475 (unsigned char) (his_ip_address >> 8),
476 (unsigned char) his_ip_address);
478 /* Send the Inverse ARP reply. */
479 MGETHDR (m, M_NOWAIT, MT_DATA);
482 m->m_pkthdr.len = m->m_len = 10 + sizeof (*reply);
483 m->m_pkthdr.rcvif = 0;
485 h = mtod (m, u_char*);
486 reply = (struct arp_req*) (h + 10);
488 h[0] = his_hardware_address >> 8;
489 h[1] = his_hardware_address;
496 *(short*) (h+8) = htons (ETHERTYPE_ARP);
498 reply->htype = htons (ARPHRD_FRELAY);
499 reply->ptype = htons (ETHERTYPE_IP);
502 reply->op = htons (ARPOP_INVREPLY);
503 reply->hsource = htons (my_hardware_address);
504 reply->psource1 = htonl (my_ip_address);
505 reply->psource2 = htonl (my_ip_address) >> 16;
506 reply->htarget = htons (his_hardware_address);
507 reply->ptarget1 = htonl (his_ip_address);
508 reply->ptarget2 = htonl (his_ip_address) >> 16;
510 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
511 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
515 * Process the input signaling packet (DLCI 0).
516 * The implemented protocol is ANSI T1.617 Annex D.
518 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len)
524 if (h[2] != FR_UI || h[3] != FR_SIGNALING || h[4] != 0) {
526 printf (SPP_FMT "Invalid signaling header\n",
529 printf ("%02x", *h++);
531 printf ("-%02x", *h++);
536 if (h[5] == FR_MSG_ENQUIRY) {
537 if (len == FR_ENQUIRY_SIZE &&
538 h[12] == (u_char) sp->pp_seq[IDX_LCP]) {
539 sp->pp_seq[IDX_LCP] = random();
540 printf (SPP_FMT "loopback detected\n",
545 if (h[5] != FR_MSG_STATUS) {
547 printf (SPP_FMT "Unknown signaling message: 0x%02x\n",
548 SPP_ARGS(ifp), h[5]);
552 /* Parse message fields. */
553 for (p=h+6; p<h+len; ) {
557 printf (SPP_FMT "Unknown signaling field 0x%x\n",
567 printf (SPP_FMT "Invalid signaling verify field length %d\n",
568 SPP_ARGS(ifp), p[1]);
571 sp->pp_rseq[IDX_LCP] = p[2];
573 printf (SPP_FMT "got lmi reply rseq=%d, seq=%d",
574 SPP_ARGS(ifp), p[2], p[3]);
575 if (p[3] != (u_char) sp->pp_seq[IDX_LCP])
576 printf (" (really %d)",
577 (u_char) sp->pp_seq[IDX_LCP]);
584 printf (SPP_FMT "Invalid PVC status length %d\n",
585 SPP_ARGS(ifp), p[1]);
588 dlci = (p[2] << 4 & 0x3f0) | (p[3] >> 3 & 0x0f);
591 if (sp->fr_status != p[4])
592 printf (SPP_FMT "DLCI %d %s%s\n",
594 p[4] & FR_DLCI_DELETE ? "deleted" :
595 p[4] & FR_DLCI_ACTIVE ? "active" : "passive",
596 p[4] & FR_DLCI_NEW ? ", new" : "");
597 sp->fr_status = p[4];
602 else if (p < h+len+1 && p[1])
606 printf (SPP_FMT "Invalid signaling field 0x%x\n",