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 $
25 #include <sys/param.h>
27 #if defined(__FreeBSD__)
29 #include "opt_inet6.h"
34 # include "opt_inet.h"
35 # include "opt_inet6.h"
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #if defined(__FreeBSD__)
47 #include <sys/random.h>
49 #include <sys/malloc.h>
52 #if defined (__OpenBSD__)
59 #include <net/if_var.h>
60 #include <net/netisr.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <net/slcompress.h>
68 #if defined (__NetBSD__) || defined (__OpenBSD__)
69 #include <machine/cpu.h> /* XXX for softnet */
72 #include <machine/stdarg.h>
74 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
80 #if defined (__FreeBSD__) || defined (__OpenBSD__)
81 # include <netinet/if_ether.h>
83 # include <net/ethertypes.h>
86 #include <net/if_sppp.h>
91 #define FR_UI 0x03 /* Unnumbered Information */
92 #define FR_IP 0xCC /* IP protocol identifier */
93 #define FR_PADDING 0x00 /* NLPID padding */
94 #define FR_SIGNALING 0x08 /* Q.933/T1.617 signaling identifier */
95 #define FR_SNAP 0x80 /* NLPID snap */
100 #define FR_DE 0x02 /* discard eligibility */
101 #define FR_FECN 0x04 /* forward notification */
102 #define FR_BECN 0x08 /* backward notification */
105 * Signaling message types.
107 #define FR_MSG_ENQUIRY 0x75 /* status enquiry */
108 #define FR_MSG_STATUS 0x7d /* status */
110 #define FR_ENQUIRY_SIZE 14
113 * Message field types.
115 #define FR_FLD_RTYPE 0x01 /* report type */
116 #define FR_FLD_VERIFY 0x03 /* link verification */
117 #define FR_FLD_PVC 0x07 /* PVC status */
118 #define FR_FLD_LSHIFT5 0x95 /* locking shift 5 */
123 #define FR_RTYPE_FULL 0 /* full status */
124 #define FR_RTYPE_SHORT 1 /* link verification only */
125 #define FR_RTYPE_SINGLE 2 /* single PVC status */
127 /* PVC status field. */
128 #define FR_DLCI_DELETE 0x04 /* PVC is deleted */
129 #define FR_DLCI_ACTIVE 0x02 /* PVC is operational */
130 #define FR_DLCI_NEW 0x08 /* PVC is new */
133 unsigned short htype; /* hardware type = ARPHRD_FRELAY */
134 unsigned short ptype; /* protocol type = ETHERTYPE_IP */
135 unsigned char halen; /* hardware address length = 2 */
136 unsigned char palen; /* protocol address length = 4 */
137 unsigned short op; /* ARP/RARP/InARP request/reply */
138 unsigned short hsource; /* hardware source address */
139 unsigned short psource1; /* protocol source */
140 unsigned short psource2;
141 unsigned short htarget; /* hardware target address */
142 unsigned short ptarget1; /* protocol target */
143 unsigned short ptarget2;
146 #if defined(__FreeBSD__) && __FreeBSD_version < 501113
147 #define SPP_FMT "%s%d: "
148 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
150 #define SPP_FMT "%s: "
151 #define SPP_ARGS(ifp) (ifp)->if_xname
154 /* almost every function needs these */
156 struct ifnet *ifp = SP2IFP(sp); \
157 int debug = ifp->if_flags & IFF_DEBUG
159 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req, u_short addr);
160 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len);
162 void sppp_fr_input (struct sppp *sp, struct mbuf *m)
165 u_char *h = mtod (m, u_char*);
167 int dlci, hlen, proto;
169 /* Get the DLCI number. */
170 if (m->m_pkthdr.len < 10) {
174 dlci = (h[0] << 2 & 0x3f0) | (h[1] >> 4 & 0x0f);
176 /* Process signaling packets. */
178 sppp_fr_signal (sp, h, m->m_pkthdr.len);
183 if (dlci != sp->fr_dlci) {
185 printf (SPP_FMT "Received packet from invalid DLCI %d\n",
186 SPP_ARGS(ifp), dlci);
190 /* Process the packet. */
191 if (ntohs (*(short*) (h+2)) == ETHERTYPE_IP) {
192 /* Prehistoric IP framing? */
198 printf (SPP_FMT "Invalid frame relay header flag 0x%02x\n",
199 SPP_ARGS(ifp), h[2]);
205 printf (SPP_FMT "Unsupported NLPID 0x%02x\n",
206 SPP_ARGS(ifp), h[3]);
210 if (h[4] != FR_SNAP) {
212 printf (SPP_FMT "Bad NLPID 0x%02x\n",
213 SPP_ARGS(ifp), h[4]);
216 if (h[5] || h[6] || h[7]) {
218 printf (SPP_FMT "Bad OID 0x%02x-0x%02x-0x%02x\n",
223 proto = ntohs (*(short*) (h+8));
224 if (proto == ETHERTYPE_ARP) {
225 /* Process the ARP request. */
226 if (m->m_pkthdr.len != 10 + sizeof (struct arp_req)) {
228 printf (SPP_FMT "Bad ARP request size = %d bytes\n",
233 sppp_fr_arp (sp, (struct arp_req*) (h + 10),
242 proto = ETHERTYPE_IP;
247 /* Remove frame relay header. */
252 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
253 drop: if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
254 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
264 if (! (ifp->if_flags & IFF_UP))
267 M_SETFIB(m, ifp->if_fib);
270 if (netisr_queue(isr, m)) { /* (0) on success. */
272 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
278 * Add the frame relay header to the packet.
279 * For IP the header length is 4 bytes,
280 * for all other protocols - 10 bytes (RFC 1490).
282 struct mbuf *sppp_fr_header (struct sppp *sp, struct mbuf *m,
289 /* Prepend the space for Frame Relay header. */
290 hlen = (family == AF_INET) ? 4 : 10;
291 M_PREPEND (m, hlen, M_NOWAIT);
294 h = mtod (m, u_char*);
296 /* Fill the header. */
297 h[0] = sp->fr_dlci >> 2 & 0xfc;
298 h[1] = sp->fr_dlci << 4 | 1;
304 printf (SPP_FMT "Cannot handle address family %d\n",
305 SPP_ARGS(ifp), family);
310 #if 0 /* Crashes on fragmented packets */
312 * Set the discard eligibility bit, if:
313 * 1) no fragmentation
314 * 2) length > 400 bytes
315 * 3a) the protocol is UDP or
316 * 3b) TCP data (no control bits)
319 struct ip *ip = (struct ip*) (h + hlen);
320 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
322 if (! (ip->ip_off & ~IP_DF) && ip->ip_len > 400 &&
323 (ip->ip_p == IPPROTO_UDP ||
324 ip->ip_p == IPPROTO_TCP && ! tcp->th_flags))
342 *(short*) (h+8) = htons(type);
347 * Send periodical frame relay link verification messages via DLCI 0.
348 * Called every 10 seconds (default value of T391 timer is 10 sec).
349 * Every 6-th message is a full status request
350 * (default value of N391 counter is 6).
352 void sppp_fr_keepalive (struct sppp *sp)
355 unsigned char *h, *p;
358 MGETHDR (m, M_NOWAIT, MT_DATA);
361 m->m_pkthdr.rcvif = 0;
363 h = mtod (m, u_char*);
365 *p++ = 0; /* DLCI = 0 */
368 *p++ = FR_SIGNALING; /* NLPID = UNI call control */
370 *p++ = 0; /* call reference length = 0 */
371 *p++ = FR_MSG_ENQUIRY; /* message type = status enquiry */
373 *p++ = FR_FLD_LSHIFT5; /* locking shift 5 */
375 *p++ = FR_FLD_RTYPE; /* report type field */
376 *p++ = 1; /* report type length = 1 */
377 if (sp->pp_seq[IDX_LCP] % 6)
378 *p++ = FR_RTYPE_SHORT; /* link verification only */
380 *p++ = FR_RTYPE_FULL; /* full status needed */
382 if (sp->pp_seq[IDX_LCP] >= 255)
383 sp->pp_seq[IDX_LCP] = 0;
384 *p++ = FR_FLD_VERIFY; /* link verification type field */
385 *p++ = 2; /* link verification field length = 2 */
386 *p++ = ++sp->pp_seq[IDX_LCP]; /* our sequence number */
387 *p++ = sp->pp_rseq[IDX_LCP]; /* last received sequence number */
389 m->m_pkthdr.len = m->m_len = p - h;
391 printf (SPP_FMT "send lmi packet, seq=%d, rseq=%d\n",
392 SPP_ARGS(ifp), (u_char) sp->pp_seq[IDX_LCP],
393 (u_char) sp->pp_rseq[IDX_LCP]);
395 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
396 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
400 * Process the frame relay Inverse ARP request.
402 static void sppp_fr_arp (struct sppp *sp, struct arp_req *req,
403 u_short his_hardware_address)
407 struct arp_req *reply;
409 u_short my_hardware_address;
410 u_long his_ip_address, my_ip_address;
412 if ((ntohs (req->htype) != ARPHRD_FRELAY ||
413 ntohs (req->htype) != 16) || /* for BayNetworks routers */
414 ntohs (req->ptype) != ETHERTYPE_IP) {
416 printf (SPP_FMT "Invalid ARP hardware/protocol type = 0x%x/0x%x\n",
418 ntohs (req->htype), ntohs (req->ptype));
421 if (req->halen != 2 || req->palen != 4) {
423 printf (SPP_FMT "Invalid ARP hardware/protocol address length = %d/%d\n",
425 req->halen, req->palen);
428 switch (ntohs (req->op)) {
431 printf (SPP_FMT "Invalid ARP op = 0x%x\n",
432 SPP_ARGS(ifp), ntohs (req->op));
439 case ARPOP_INVREQUEST:
440 my_hardware_address = ntohs (req->htarget);
441 his_ip_address = ntohs (req->psource1) << 16 |
442 ntohs (req->psource2);
443 my_ip_address = ntohs (req->ptarget1) << 16 |
444 ntohs (req->ptarget2);
448 printf (SPP_FMT "got ARP request, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
449 SPP_ARGS(ifp), ntohs (req->hsource),
450 (unsigned char) (his_ip_address >> 24),
451 (unsigned char) (his_ip_address >> 16),
452 (unsigned char) (his_ip_address >> 8),
453 (unsigned char) his_ip_address,
455 (unsigned char) (my_ip_address >> 24),
456 (unsigned char) (my_ip_address >> 16),
457 (unsigned char) (my_ip_address >> 8),
458 (unsigned char) my_ip_address);
460 sppp_get_ip_addrs (sp, &my_ip_address, 0, 0);
462 return; /* nothing to reply */
465 printf (SPP_FMT "send ARP reply, source=0x%04x/%d.%d.%d.%d, target=0x%04x/%d.%d.%d.%d\n",
466 SPP_ARGS(ifp), my_hardware_address,
467 (unsigned char) (my_ip_address >> 24),
468 (unsigned char) (my_ip_address >> 16),
469 (unsigned char) (my_ip_address >> 8),
470 (unsigned char) my_ip_address,
471 his_hardware_address,
472 (unsigned char) (his_ip_address >> 24),
473 (unsigned char) (his_ip_address >> 16),
474 (unsigned char) (his_ip_address >> 8),
475 (unsigned char) his_ip_address);
477 /* Send the Inverse ARP reply. */
478 MGETHDR (m, M_NOWAIT, MT_DATA);
481 m->m_pkthdr.len = m->m_len = 10 + sizeof (*reply);
482 m->m_pkthdr.rcvif = 0;
484 h = mtod (m, u_char*);
485 reply = (struct arp_req*) (h + 10);
487 h[0] = his_hardware_address >> 8;
488 h[1] = his_hardware_address;
495 *(short*) (h+8) = htons (ETHERTYPE_ARP);
497 reply->htype = htons (ARPHRD_FRELAY);
498 reply->ptype = htons (ETHERTYPE_IP);
501 reply->op = htons (ARPOP_INVREPLY);
502 reply->hsource = htons (my_hardware_address);
503 reply->psource1 = htonl (my_ip_address);
504 reply->psource2 = htonl (my_ip_address) >> 16;
505 reply->htarget = htons (his_hardware_address);
506 reply->ptarget1 = htonl (his_ip_address);
507 reply->ptarget2 = htonl (his_ip_address) >> 16;
509 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
510 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
514 * Process the input signaling packet (DLCI 0).
515 * The implemented protocol is ANSI T1.617 Annex D.
517 static void sppp_fr_signal (struct sppp *sp, unsigned char *h, int len)
523 if (h[2] != FR_UI || h[3] != FR_SIGNALING || h[4] != 0) {
525 printf (SPP_FMT "Invalid signaling header\n",
528 printf ("%02x", *h++);
530 printf ("-%02x", *h++);
535 if (h[5] == FR_MSG_ENQUIRY) {
536 if (len == FR_ENQUIRY_SIZE &&
537 h[12] == (u_char) sp->pp_seq[IDX_LCP]) {
538 sp->pp_seq[IDX_LCP] = random();
539 printf (SPP_FMT "loopback detected\n",
544 if (h[5] != FR_MSG_STATUS) {
546 printf (SPP_FMT "Unknown signaling message: 0x%02x\n",
547 SPP_ARGS(ifp), h[5]);
551 /* Parse message fields. */
552 for (p=h+6; p<h+len; ) {
556 printf (SPP_FMT "Unknown signaling field 0x%x\n",
566 printf (SPP_FMT "Invalid signaling verify field length %d\n",
567 SPP_ARGS(ifp), p[1]);
570 sp->pp_rseq[IDX_LCP] = p[2];
572 printf (SPP_FMT "got lmi reply rseq=%d, seq=%d",
573 SPP_ARGS(ifp), p[2], p[3]);
574 if (p[3] != (u_char) sp->pp_seq[IDX_LCP])
575 printf (" (really %d)",
576 (u_char) sp->pp_seq[IDX_LCP]);
583 printf (SPP_FMT "Invalid PVC status length %d\n",
584 SPP_ARGS(ifp), p[1]);
587 dlci = (p[2] << 4 & 0x3f0) | (p[3] >> 3 & 0x0f);
590 if (sp->fr_status != p[4])
591 printf (SPP_FMT "DLCI %d %s%s\n",
593 p[4] & FR_DLCI_DELETE ? "deleted" :
594 p[4] & FR_DLCI_ACTIVE ? "active" : "passive",
595 p[4] & FR_DLCI_NEW ? ", new" : "");
596 sp->fr_status = p[4];
601 else if (p < h+len+1 && p[1])
605 printf (SPP_FMT "Invalid signaling field 0x%x\n",