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__) && __FreeBSD__ >= 3
30 #include "opt_inet6.h"
36 # include "opt_inet.h"
37 # include "opt_inet6.h"
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
49 #include <sys/random.h>
51 #include <sys/malloc.h>
54 #if defined (__OpenBSD__)
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>
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
92 #include <net/if_sppp.h>
97 #define FR_UI 0x03 /* Unnumbered Information */
98 #define FR_IP 0xCC /* IP protocol identifier */
99 #define FR_PADDING 0x00 /* NLPID padding */
100 #define FR_SIGNALING 0x08 /* Q.933/T1.617 signaling identifier */
101 #define FR_SNAP 0x80 /* NLPID snap */
106 #define FR_DE 0x02 /* discard eligibility */
107 #define FR_FECN 0x04 /* forward notification */
108 #define FR_BECN 0x08 /* backward notification */
111 * Signaling message types.
113 #define FR_MSG_ENQUIRY 0x75 /* status enquiry */
114 #define FR_MSG_STATUS 0x7d /* status */
116 #define FR_ENQUIRY_SIZE 14
119 * Message field types.
121 #define FR_FLD_RTYPE 0x01 /* report type */
122 #define FR_FLD_VERIFY 0x03 /* link verification */
123 #define FR_FLD_PVC 0x07 /* PVC status */
124 #define FR_FLD_LSHIFT5 0x95 /* locking shift 5 */
129 #define FR_RTYPE_FULL 0 /* full status */
130 #define FR_RTYPE_SHORT 1 /* link verification only */
131 #define FR_RTYPE_SINGLE 2 /* single PVC status */
133 /* PVC status field. */
134 #define FR_DLCI_DELETE 0x04 /* PVC is deleted */
135 #define FR_DLCI_ACTIVE 0x02 /* PVC is operational */
136 #define FR_DLCI_NEW 0x08 /* PVC is new */
139 unsigned short htype; /* hardware type = ARPHRD_FRELAY */
140 unsigned short ptype; /* protocol type = ETHERTYPE_IP */
141 unsigned char halen; /* hardware address length = 2 */
142 unsigned char palen; /* protocol address length = 4 */
143 unsigned short op; /* ARP/RARP/InARP request/reply */
144 unsigned short hsource; /* hardware source address */
145 unsigned short psource1; /* protocol source */
146 unsigned short psource2;
147 unsigned short htarget; /* hardware target address */
148 unsigned short ptarget1; /* protocol target */
149 unsigned short ptarget2;
152 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && __FreeBSD_version < 501113
153 #define SPP_FMT "%s%d: "
154 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
156 #define SPP_FMT "%s: "
157 #define SPP_ARGS(ifp) (ifp)->if_xname
160 /* almost every function needs these */
162 struct ifnet *ifp = SP2IFP(sp); \
163 int debug = ifp->if_flags & IFF_DEBUG
165 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req, u_short addr);
166 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len);
168 void sppp_fr_input (struct sppp *sp, struct mbuf *m)
171 u_char *h = mtod (m, u_char*);
173 int dlci, hlen, proto;
175 /* Get the DLCI number. */
176 if (m->m_pkthdr.len < 10) {
180 dlci = (h[0] << 2 & 0x3f0) | (h[1] >> 4 & 0x0f);
182 /* Process signaling packets. */
184 sppp_fr_signal (sp, h, m->m_pkthdr.len);
189 if (dlci != sp->fr_dlci) {
191 printf (SPP_FMT "Received packet from invalid DLCI %d\n",
192 SPP_ARGS(ifp), dlci);
196 /* Process the packet. */
197 if (ntohs (*(short*) (h+2)) == ETHERTYPE_IP) {
198 /* Prehistoric IP framing? */
204 printf (SPP_FMT "Invalid frame relay header flag 0x%02x\n",
205 SPP_ARGS(ifp), h[2]);
211 printf (SPP_FMT "Unsupported NLPID 0x%02x\n",
212 SPP_ARGS(ifp), h[3]);
216 if (h[4] != FR_SNAP) {
218 printf (SPP_FMT "Bad NLPID 0x%02x\n",
219 SPP_ARGS(ifp), h[4]);
222 if (h[5] || h[6] || h[7]) {
224 printf (SPP_FMT "Bad OID 0x%02x-0x%02x-0x%02x\n",
229 proto = ntohs (*(short*) (h+8));
230 if (proto == ETHERTYPE_ARP) {
231 /* Process the ARP request. */
232 if (m->m_pkthdr.len != 10 + sizeof (struct arp_req)) {
234 printf (SPP_FMT "Bad ARP request size = %d bytes\n",
239 sppp_fr_arp (sp, (struct arp_req*) (h + 10),
248 proto = ETHERTYPE_IP;
253 /* Remove frame relay header. */
259 drop: ++ifp->if_ierrors;
280 if (! (ifp->if_flags & IFF_UP))
284 if (netisr_queue(isr, m)) { /* (0) on success. */
286 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
292 * Add the frame relay header to the packet.
293 * For IP the header length is 4 bytes,
294 * for all other protocols - 10 bytes (RFC 1490).
296 struct mbuf *sppp_fr_header (struct sppp *sp, struct mbuf *m,
303 /* Prepend the space for Frame Relay header. */
304 hlen = (family == AF_INET) ? 4 : 10;
305 M_PREPEND (m, hlen, M_DONTWAIT);
308 h = mtod (m, u_char*);
310 /* Fill the header. */
311 h[0] = sp->fr_dlci >> 2 & 0xfc;
312 h[1] = sp->fr_dlci << 4 | 1;
318 printf (SPP_FMT "Cannot handle address family %d\n",
319 SPP_ARGS(ifp), family);
324 #if 0 /* Crashes on fragmented packets */
326 * Set the discard eligibility bit, if:
327 * 1) no fragmentation
328 * 2) length > 400 bytes
329 * 3a) the protocol is UDP or
330 * 3b) TCP data (no control bits)
333 struct ip *ip = (struct ip*) (h + hlen);
334 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
336 if (! (ip->ip_off & ~IP_DF) && ip->ip_len > 400 &&
337 (ip->ip_p == IPPROTO_UDP ||
338 ip->ip_p == IPPROTO_TCP && ! tcp->th_flags))
347 type = ETHERTYPE_IPX;
366 *(short*) (h+8) = htons(type);
371 * Send periodical frame relay link verification messages via DLCI 0.
372 * Called every 10 seconds (default value of T391 timer is 10 sec).
373 * Every 6-th message is a full status request
374 * (default value of N391 counter is 6).
376 void sppp_fr_keepalive (struct sppp *sp)
379 unsigned char *h, *p;
382 MGETHDR (m, M_DONTWAIT, MT_DATA);
385 m->m_pkthdr.rcvif = 0;
387 h = mtod (m, u_char*);
389 *p++ = 0; /* DLCI = 0 */
392 *p++ = FR_SIGNALING; /* NLPID = UNI call control */
394 *p++ = 0; /* call reference length = 0 */
395 *p++ = FR_MSG_ENQUIRY; /* message type = status enquiry */
397 *p++ = FR_FLD_LSHIFT5; /* locking shift 5 */
399 *p++ = FR_FLD_RTYPE; /* report type field */
400 *p++ = 1; /* report type length = 1 */
401 if (sp->pp_seq[IDX_LCP] % 6)
402 *p++ = FR_RTYPE_SHORT; /* link verification only */
404 *p++ = FR_RTYPE_FULL; /* full status needed */
406 if (sp->pp_seq[IDX_LCP] >= 255)
407 sp->pp_seq[IDX_LCP] = 0;
408 *p++ = FR_FLD_VERIFY; /* link verification type field */
409 *p++ = 2; /* link verification field length = 2 */
410 *p++ = ++sp->pp_seq[IDX_LCP]; /* our sequence number */
411 *p++ = sp->pp_rseq[IDX_LCP]; /* last received sequence number */
413 m->m_pkthdr.len = m->m_len = p - h;
415 printf (SPP_FMT "send lmi packet, seq=%d, rseq=%d\n",
416 SPP_ARGS(ifp), (u_char) sp->pp_seq[IDX_LCP],
417 (u_char) sp->pp_rseq[IDX_LCP]);
419 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
424 * Process the frame relay Inverse ARP request.
426 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req,
427 u_short his_hardware_address)
431 struct arp_req *reply;
433 u_short my_hardware_address;
434 u_long his_ip_address, my_ip_address;
436 if ((ntohs (req->htype) != ARPHRD_FRELAY ||
437 ntohs (req->htype) != 16) || /* for BayNetworks routers */
438 ntohs (req->ptype) != ETHERTYPE_IP) {
440 printf (SPP_FMT "Invalid ARP hardware/protocol type = 0x%x/0x%x\n",
442 ntohs (req->htype), ntohs (req->ptype));
445 if (req->halen != 2 || req->palen != 4) {
447 printf (SPP_FMT "Invalid ARP hardware/protocol address length = %d/%d\n",
449 req->halen, req->palen);
452 switch (ntohs (req->op)) {
455 printf (SPP_FMT "Invalid ARP op = 0x%x\n",
456 SPP_ARGS(ifp), ntohs (req->op));
463 case ARPOP_INVREQUEST:
464 my_hardware_address = ntohs (req->htarget);
465 his_ip_address = ntohs (req->psource1) << 16 |
466 ntohs (req->psource2);
467 my_ip_address = ntohs (req->ptarget1) << 16 |
468 ntohs (req->ptarget2);
472 printf (SPP_FMT "got ARP request, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
473 SPP_ARGS(ifp), ntohs (req->hsource),
474 (unsigned char) (his_ip_address >> 24),
475 (unsigned char) (his_ip_address >> 16),
476 (unsigned char) (his_ip_address >> 8),
477 (unsigned char) his_ip_address,
479 (unsigned char) (my_ip_address >> 24),
480 (unsigned char) (my_ip_address >> 16),
481 (unsigned char) (my_ip_address >> 8),
482 (unsigned char) my_ip_address);
484 sppp_get_ip_addrs (sp, &my_ip_address, 0, 0);
486 return; /* nothing to reply */
489 printf (SPP_FMT "send ARP reply, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
490 SPP_ARGS(ifp), my_hardware_address,
491 (unsigned char) (my_ip_address >> 24),
492 (unsigned char) (my_ip_address >> 16),
493 (unsigned char) (my_ip_address >> 8),
494 (unsigned char) my_ip_address,
495 his_hardware_address,
496 (unsigned char) (his_ip_address >> 24),
497 (unsigned char) (his_ip_address >> 16),
498 (unsigned char) (his_ip_address >> 8),
499 (unsigned char) his_ip_address);
501 /* Send the Inverse ARP reply. */
502 MGETHDR (m, M_DONTWAIT, MT_DATA);
505 m->m_pkthdr.len = m->m_len = 10 + sizeof (*reply);
506 m->m_pkthdr.rcvif = 0;
508 h = mtod (m, u_char*);
509 reply = (struct arp_req*) (h + 10);
511 h[0] = his_hardware_address >> 8;
512 h[1] = his_hardware_address;
519 *(short*) (h+8) = htons (ETHERTYPE_ARP);
521 reply->htype = htons (ARPHRD_FRELAY);
522 reply->ptype = htons (ETHERTYPE_IP);
525 reply->op = htons (ARPOP_INVREPLY);
526 reply->hsource = htons (my_hardware_address);
527 reply->psource1 = htonl (my_ip_address);
528 reply->psource2 = htonl (my_ip_address) >> 16;
529 reply->htarget = htons (his_hardware_address);
530 reply->ptarget1 = htonl (his_ip_address);
531 reply->ptarget2 = htonl (his_ip_address) >> 16;
533 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
538 * Process the input signaling packet (DLCI 0).
539 * The implemented protocol is ANSI T1.617 Annex D.
541 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len)
547 if (h[2] != FR_UI || h[3] != FR_SIGNALING || h[4] != 0) {
549 printf (SPP_FMT "Invalid signaling header\n",
552 printf ("%02x", *h++);
554 printf ("-%02x", *h++);
559 if (h[5] == FR_MSG_ENQUIRY) {
560 if (len == FR_ENQUIRY_SIZE &&
561 h[12] == (u_char) sp->pp_seq[IDX_LCP]) {
562 sp->pp_seq[IDX_LCP] = random();
563 printf (SPP_FMT "loopback detected\n",
568 if (h[5] != FR_MSG_STATUS) {
570 printf (SPP_FMT "Unknown signaling message: 0x%02x\n",
571 SPP_ARGS(ifp), h[5]);
575 /* Parse message fields. */
576 for (p=h+6; p<h+len; ) {
580 printf (SPP_FMT "Unknown signaling field 0x%x\n",
590 printf (SPP_FMT "Invalid signaling verify field length %d\n",
591 SPP_ARGS(ifp), p[1]);
594 sp->pp_rseq[IDX_LCP] = p[2];
596 printf (SPP_FMT "got lmi reply rseq=%d, seq=%d",
597 SPP_ARGS(ifp), p[2], p[3]);
598 if (p[3] != (u_char) sp->pp_seq[IDX_LCP])
599 printf (" (really %d)",
600 (u_char) sp->pp_seq[IDX_LCP]);
607 printf (SPP_FMT "Invalid PVC status length %d\n",
608 SPP_ARGS(ifp), p[1]);
611 dlci = (p[2] << 4 & 0x3f0) | (p[3] >> 3 & 0x0f);
614 if (sp->fr_status != p[4])
615 printf (SPP_FMT "DLCI %d %s%s\n",
617 p[4] & FR_DLCI_DELETE ? "deleted" :
618 p[4] & FR_DLCI_ACTIVE ? "active" : "passive",
619 p[4] & FR_DLCI_NEW ? ", new" : "");
620 sp->fr_status = p[4];
625 else if (p < h+len+1 && p[1])
629 printf (SPP_FMT "Invalid signaling field 0x%x\n",