Compare commits

...

6 Commits

Author SHA1 Message Date
Harald Welte 5255769504 Initial support for trunkdev
The 'trunkdev' is a virtual DAHDI device driver that exposes the
multiplexed E1 frames on a /dev/dahdi/trunkdev device.  This is useful
for all kinds of simulated setup, as well as in  the context of
using osmo-e1d for OCTOI (Osmocom TDMoIP).

In order for the dahdi_trunkdev utility to be built, you will need
to have installed dahdi header files of dahdi-linux with trunkdev
support, currently located at
https://gitea.osmocom.org/retronetworking/dahdi-linux/src/branch/laforge/trunkdev

Change-Id: Ie05100c80fd978e7ac6f5a028e0ef6c828e0bcc3
2023-07-29 17:49:17 +02:00
Harald Welte e9a4598c1f dahdi_pcap: Allow caller to specify network / user mode
A LAPD dissector (such as the one in wireshark) needs to know if
the local side is the user or the network in order to properly
decode/display the trace.  This is encoded in the sll_addr field
whose first octet indicates if the local (capturing) node serves
as the network or user side of ISDN.

Change-Id: Ief575bc4118fe5f20ef4b374d29eca442b04dabb
2022-02-21 21:20:59 +01:00
Harald Welte de9bd096c9 dahdi_pcap: Fix the use of the pkttype field
* pkttype is big-endian, so '3' is not valid, but htons(3)
* let's not use magic numbers but the actual #define PACKET_*
* differentiate PACKET_HOST (rx) from PACKET_OUTGOING (tx),
  which makes wireshark properly show rx/tx packets, allows coloring
  or filtering based on direction, etc.

Change-Id: I8c66a011a0fa0c5275bf3d5cf6b42064f02e28c0
2022-02-21 21:20:59 +01:00
Harald Welte 89b6df7593 dahdi_pcap: Fix LAPD pseudo header hardware address length
the 'halen' field is the length of the hardware address (8 bytes),
and not the length of the payload.

Change-Id: I72e009f40c8e9e295bc1e1f4a0951cd1236cdf43
2022-02-21 21:20:59 +01:00
Harald Welte 68395096f0 Fix compilation with gcc-10.2.0
/bin/sh ../libtool  --tag=CC   --mode=link gcc -I. -I./xtalk/include  -Wall  -g -O2 -lusb-1.0  -o astribank_tool astribank_tool-astribank_tool.o libastribank.la -lpthread -lm
libtool: link: gcc -I. -I./xtalk/include -Wall -g -O2 -o astribank_tool astribank_tool-astribank_tool.o  ./.libs/libastribank.a -lusb-1.0 -lpthread -lm
/usr/bin/ld: ./.libs/libastribank.a(libxtalk_la-xtalk_sync.o):/space/home/laforge/projects/git/dahdi-tools/xpp/xtalk/xtalk_sync.c:63: multiple definition of `members'; ./.libs/libastribank.a(libastribank_la-mpptalk.o):/space/home/laforge/projects/git/dahdi-tools/xpp/mpptalk.c:240: first defined here

Change-Id: I1d6f46874f74e1dfc70cabf7c4a8b8cdae9af4fa
2022-02-21 21:20:59 +01:00
Harald Welte 3b90fdbba1 add contrib/jenkins.sh for jenkins.osmocom.org build verification
Change-Id: I47701be92476ba9adfdf9783747c6c511cec1903
2022-02-21 21:20:59 +01:00
9 changed files with 508 additions and 13 deletions

View File

@ -17,7 +17,7 @@ CFLAGS += \
#-Wformat=2
endif
SUBDIRS = xpp doc hotplug
SUBDIRS = xpp doc hotplug trunkdev
if PPPD
SUBDIRS += ppp

View File

@ -182,6 +182,7 @@ AST_EXT_LIB_SETUP([PCAP], [pcap], [pcap])
AST_C_DEFINE_CHECK([DAHDI], [DAHDI_CODE], [dahdi/user.h])
DAHDI23_DIR="${DAHDI_DIR}"
AST_C_DEFINE_CHECK([DAHDI23], [DAHDI_CONFIG_NTTE], [dahdi/user.h])
AST_C_DEFINE_CHECK([TRUNKDEV], [DAHDI_TRUNKDEV_CREATE], [dahdi/user.h])
AST_EXT_LIB_CHECK([NEWT], [newt], [newtBell], [newt.h])
AST_EXT_LIB_CHECK([USB], [usb], [usb_init], [usb.h])
AST_EXT_LIB_CHECK([PCAP], [pcap], [pcap_compile], [pcap.h])
@ -193,6 +194,7 @@ CPPFLAGS="$saved_CPPFLAGS"
AM_CONDITIONAL([PBX_NEWT], [test "$PBX_NEWT" = "1"])
AM_CONDITIONAL([PBX_PCAP], [test "$PBX_PCAP" = "1" -a "$DAHDI_TXMIRROR" = "1"])
AM_CONDITIONAL([PBX_TRUNKDEV], [test "$PBX_TRUNKDEV" = "1"])
AC_CHECK_FUNCS([semtimedop])
AC_CHECK_FUNCS([alarm bzero gettimeofday memset pow regcomp select socket strcasecmp strchr strdup strerror strrchr strstr strtol strtoul])
@ -390,6 +392,7 @@ AC_CONFIG_FILES([
xpp/xtalk/Makefile
xpp/oct612x/Makefile
xpp/perl_modules/Makefile
trunkdev/Makefile
])
AC_OUTPUT

33
contrib/jenkins.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/sh
# jenkins build helper script for dahdi-tools. This is how we build on jenkins.osmocom.org
if ! [ -x "$(command -v osmo-build-dep.sh)" ]; then
echo "Error: We need to have scripts/osmo-deps.sh from http://git.osmocom.org/osmo-ci/ in PATH !"
exit 2
fi
set -ex
base="$PWD"
deps="$base/deps"
inst="$deps/install"
export deps inst
osmo-clean-workspace.sh
mkdir "$deps" || true
cd "$deps"
if [ -d dahdi-linux ]; then
(cd dahdi-linux && git fetch && git checkout -f -B master origin/master)
else
git clone https://git.osmocom.org/dahdi-linux
fi
cd $base
autoreconf -fi
./configure --with-dahdi="$deps/dahdi-linux"
$MAKE $PARALLEL_MAKE
osmo-clean-workspace.sh

View File

@ -41,6 +41,7 @@
#include <sys/ioctl.h>
#include <stdlib.h>
#include <getopt.h>
#include <linux/if_packet.h>
#define BLOCK_SIZE 512
#define MAX_CHAN 16
@ -100,7 +101,7 @@ int make_mirror(long type, int chan)
return fd;
}
int log_packet(struct chan_fds * fd, char is_read, pcap_dumper_t * dump)
int log_packet(struct chan_fds * fd, char is_read, int we_are_network, pcap_dumper_t * dump)
{
unsigned char buf[BLOCK_SIZE * 4];
int res = 0;
@ -161,10 +162,10 @@ int log_packet(struct chan_fds * fd, char is_read, pcap_dumper_t * dump)
hdr.caplen = res+sizeof(struct lapd_sll_hdr)-2;
hdr.len = res+sizeof(struct lapd_sll_hdr)-2;
lapd->sll_pkttype = 3;
lapd->sll_pkttype = htons(is_read ? PACKET_HOST : PACKET_OUTGOING);
lapd->sll_hatype = 0;
lapd->sll_halen = res;
// lapd->sll_addr = ???
lapd->sll_halen = htons(8);
lapd->sll_addr[0] = we_are_network;
lapd->sll_protocol[0] = 0x00;
lapd->sll_protocol[1] = 0x30;
@ -197,10 +198,11 @@ void usage()
printf("Usage: dahdi_pcap [OPTIONS]\n");
printf("Capture packets from DAHDI channels to pcap file\n\n");
printf("Options:\n");
printf(" -p, --proto=[mtp2|lapd] The protocol to capture, default mtp2\n");
printf(" -c, --chan=<channels> Comma separated list of channels to capture from, max %d. Mandatory\n", MAX_CHAN);
printf(" -f, --file=<filename> The pcap file to capture to. Mandatory\n");
printf(" -h, --help Display this text\n");
printf(" -p, --proto=[mtp2|lapd] The protocol to capture, default mtp2\n");
printf(" -c, --chan=<channels> Comma separated list of channels to capture from, max %d. Mandatory\n", MAX_CHAN);
printf(" -r, --role=[network|user] Is the local side the network or user side in ISDN?\n");
printf(" -f, --file=<filename> The pcap file to capture to. Mandatory\n");
printf(" -h, --help Display this text\n");
}
int main(int argc, char **argv)
@ -210,6 +212,7 @@ int main(int argc, char **argv)
int num_chans = 0;
int max_fd = 0;
int proto = DLT_MTP2_WITH_PHDR;
int we_are_network = 0;
int i;
int packetcount;
@ -220,12 +223,13 @@ int main(int argc, char **argv)
static struct option long_options[] = {
{"proto", required_argument, 0, 'p'},
{"chan", required_argument, 0, 'c'},
{"role", required_argument, 0, 'r'},
{"file", required_argument, 0, 'f'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "p:c:f:?",
c = getopt_long(argc, argv, "p:c:r:f:?",
long_options, &option_index);
if (c == -1)
break;
@ -268,6 +272,16 @@ int main(int argc, char **argv)
}
max_fd++;
break;
case 'r':
if (!strcasecmp("network", optarg))
we_are_network = 1;
else if (!strcasecmp("user", optarg))
we_are_network = 0;
else {
fprintf(stderr, "Role must be user or network!\n");
exit(1);
}
break;
case 'f':
// File to capture to
filename=optarg;
@ -317,11 +331,11 @@ int main(int argc, char **argv)
{
if(FD_ISSET(chans[i].rfd, &rd_set))
{
packetcount += log_packet(&chans[i], 1, dump);
packetcount += log_packet(&chans[i], 1, we_are_network, dump);
}
if(FD_ISSET(chans[i].tfd, &rd_set))
{
packetcount += log_packet(&chans[i], 0, dump);
packetcount += log_packet(&chans[i], 0, we_are_network, dump);
}
}
printf("Packets captured: %d\r", packetcount);

14
trunkdev/Makefile.am Normal file
View File

@ -0,0 +1,14 @@
CFLAGS += -g -Wall -O2 $(DAHDI_INCLUDE)
if PBX_TRUNKDEV
sbin_PROGRAMS = \
dahdi_trunkdev \
$(NULL)
dahdi_trunkdev_SOURCES = \
trunkdev-tool.c \
dahdi-sysfs.c \
$(NULL)
endif

88
trunkdev/dahdi-sysfs.c Normal file
View File

@ -0,0 +1,88 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include "dahdi-sysfs.h"
/* read a sysfs attribute from a file, stripping any trailing newline */
char *sysfs_read_attr(const char *path)
{
FILE *f;
char *buf;
f = fopen(path, "r");
if (!f)
return NULL;
buf = malloc(256);
if (!buf)
goto out_close;
if (!fgets(buf, 256, f))
goto out_free;
if (strlen(buf) && buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
fclose(f);
printf("%s: %s\n", path, buf);
return buf;
out_free:
free(buf);
out_close:
fclose(f);
return NULL;
}
static char *sysfs_read_span_attr(unsigned int spanno, const char *name)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), "/sys/bus/dahdi_spans/devices/span-%u/%s", spanno, name);
return sysfs_read_attr(path);
}
static int sysfs_read_span_attr_int(unsigned int spanno, const char *name)
{
char *attr;
int attr_int;
attr = sysfs_read_span_attr(spanno, name);
if (!attr)
return -1;
attr_int = atoi(attr);
free(attr);
return attr_int;
}
/* return number of channels in span */
int dahdi_span_get_basechan(unsigned int spanno)
{
return sysfs_read_span_attr_int(spanno, "basechan");
}
/* return number of channels in span */
int dahdi_span_get_channels(unsigned int spanno)
{
return sysfs_read_span_attr_int(spanno, "channels");
}
char *dahdi_span_get_name(unsigned int spanno)
{
return sysfs_read_span_attr(spanno, "name");
}
char *dahdi_span_get_desc(unsigned int spanno)
{
return sysfs_read_span_attr(spanno, "desc");
}
char *dahdi_span_get_spantype(unsigned int spanno)
{
return sysfs_read_span_attr(spanno, "spantype");
}

9
trunkdev/dahdi-sysfs.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
char *sysfs_read_attr(const char *path);
int dahdi_span_get_basechan(unsigned int spanno);
int dahdi_span_get_channels(unsigned int spanno);
char *dahdi_span_get_name(unsigned int spanno);
char *dahdi_span_get_desc(unsigned int spanno);
char *dahdi_span_get_spantype(unsigned int spanno);

334
trunkdev/trunkdev-tool.c Normal file
View File

@ -0,0 +1,334 @@
/*
* trunkdev configuration program for DAHDI Telephony Interface
*
* Copyright (C) 2022 Harald Welte <laforge@osmocom.org>
* Copyright (C) 2001-2008 Digium, Inc.
*
* All rights reserved.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <dahdi/user.h>
#include "dahdi-sysfs.h"
static int g_fd = -1;
/* wrapper around DAHDI_TRUNKDEV_CREATE */
static int trunkdev_create(int fd, const char *name)
{
struct dahdi_trunkdev_create td_c = { 0 };
int rc;
strncpy(td_c.name, name, sizeof(td_c.name));
td_c.name[sizeof(td_c.name)-1] = 0;
rc = ioctl(fd, DAHDI_TRUNKDEV_CREATE, &td_c);
if (rc)
fprintf(stderr, "Error creating trunkdev '%s': %s\n", name, strerror(errno));
return rc;
}
/* wrapper around DAHDI_TRUNKDEV_DELETE */
static int trunkdev_delete(int fd, const char *name)
{
struct dahdi_trunkdev_delete td_d = { 0 };
int rc;
strncpy(td_d.name, name, sizeof(td_d.name));
td_d.name[sizeof(td_d.name)-1] = 0;
rc = ioctl(fd, DAHDI_TRUNKDEV_DELETE, &td_d);
if (rc)
fprintf(stderr, "Error deleting trunkdev '%s': %s\n", name, strerror(errno));
return rc;
}
/* wrapper around DAHDI_TRUNKDEV_SPECIFY */
static int trunkdev_specify(int fd, const char *name)
{
struct dahdi_trunkdev_open td_o = { 0 };
int rc;
strncpy(td_o.name, name, sizeof(td_o.name));
td_o.name[sizeof(td_o.name)-1] = 0;
rc = ioctl(fd, DAHDI_TRUNKDEV_OPEN, &td_o);
if (rc < 0) {
fprintf(stderr, "Error opening trunkdev '%s': %s\n", name, strerror(errno));
return rc;
}
return td_o.spanno;
}
/* wrapper around DAHDI_SPANCONFIG (assuming E1 with CRC4) */
static int spanconfig(int fd, int spanno)
{
struct dahdi_lineconfig dlc = {
.span = spanno,
.lbo = 0,
.lineconfig = DAHDI_CONFIG_CRC4,
.sync = 0,
};
return ioctl(fd, DAHDI_SPANCONFIG, &dlc);
}
/* wrapper around DAHDI_CHANCONFIG (assuming E1/ALAW/CLEAR; unless dchan -> HDLCFCS) */
static int chanconfig(int fd, int channo, bool dchan)
{
struct dahdi_chanconfig dcc = {
.chan = channo,
.sigtype = DAHDI_SIG_CLEAR,
.deflaw = DAHDI_LAW_ALAW,
.master = 0,
.idlebits = 0,
};
if (dchan)
dcc.sigtype = DAHDI_SIG_HDLCFCS;
return ioctl(fd, DAHDI_CHANCONFIG, &dcc);
}
static int chanconfig_hdlc_master(int fd, int channo, const char *netdev_name)
{
struct dahdi_chanconfig dcc = {
.chan = channo,
.sigtype = DAHDI_SIG_HDLCNET,
.deflaw = DAHDI_LAW_ALAW,
.master = 0,
.idlebits = 0,
};
strncpy(dcc.netdev_name, netdev_name, sizeof(dcc.netdev_name));
dcc.netdev_name[sizeof(dcc.netdev_name)-1] = '\0';
return ioctl(fd, DAHDI_CHANCONFIG, &dcc);
}
static int chanconfig_hdlc_slave(int fd, int channo, int master)
{
struct dahdi_chanconfig dcc = {
.chan = channo,
.sigtype = DAHDI_SIG_SLAVE,
.deflaw = DAHDI_LAW_ALAW,
.master = master,
.idlebits = 0,
};
return ioctl(fd, DAHDI_CHANCONFIG, &dcc);
}
/* high-level wrapper to configure an E1 span + all its channels */
static int config(int spanno, const char *hdlc_netdev)
{
struct dahdi_spaninfo dsi = {
.spanno = spanno,
};
int fd, rc;
fd = open("/dev/dahdi/ctl", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Unable to open /dev/dahd/ctl: %s\n", strerror(errno));
return fd;
}
rc = ioctl(fd, DAHDI_SPANSTAT, &dsi);
if (rc < 0) {
fprintf(stderr, "Error during spanstat: %s\n", strerror(errno));
close(fd);
return rc;
}
printf("Span %u (%s: %s)\n", dsi.spanno, dsi.name, dsi.desc);
printf(" numchans=%u, totalchans=%u\n", dsi.numchans, dsi.totalchans);
rc = spanconfig(fd, spanno);
if (rc < 0) {
fprintf(stderr, "Error during spanconfig: %s\n", strerror(errno));
close(fd);
return rc;
}
/* resolve basechan + num_chan via sysfs */
int basechan = dahdi_span_get_basechan(spanno);
int num_chan = dahdi_span_get_channels(spanno);
if (basechan < 0 || num_chan < 0) {
fprintf(stderr, "Error getting basechan=%d or channels=%d for spanno=%d: %s\n",
basechan, num_chan, spanno, strerror(errno));
close(fd);
return -1;
}
for (unsigned int i = basechan; i < basechan+num_chan; i++) {
if (!hdlc_netdev) {
/* ISDN: we assume a standard ISDN configuration with TS16 == D-channel */
rc = chanconfig(fd, i, i == basechan + 15);
} else {
/* HDLC: we assume TS 1-31 as superchannel */
if (i == basechan)
rc = chanconfig_hdlc_master(fd, i, hdlc_netdev);
else
rc = chanconfig_hdlc_slave(fd, i, basechan);
}
if (rc < 0) {
fprintf(stderr, "Error during spanconfig: %s\n", strerror(errno));
close(fd);
return rc;
}
}
close(fd);
return 0;
}
static void print_help(void)
{
printf("\n");
printf( "dahdi_trunkdev create NAME\n"
" Create a new dahdi trunkdev named NAME\n"
"dahdi_trunkdev delete NAME\n"
" Delete an existing dahdi trunkdev named NAME\n"
"dahdi_trunkdev config NAME\n"
" Configure span + all channels of trunkdev named NAME for ISDN\n"
"dahdi_trunkdev config-hdlc NAME\n"
" Configure span + all channels of trunkdev named NAME for HDLC\n"
"dahdi_trunkdev read NAME\n"
" Continuously read + discard frames from trunkdev named NAME\n"
"dahdi_trunkdev rdwr NAME\n"
" Continuously read + loop back (write) frames of trunkdev named NAME\n");
}
int main(int argc, char **argv)
{
int rc;
printf("DAHDI trunkdev tool (C) 2022 by Harald Welte <laforge@osmocom.org>\n\n");
if (argc < 2) {
fprintf(stderr, "First argument must specify the command\n");
print_help();
exit(2);
}
if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help")) {
print_help();
exit(0);
}
g_fd = open("/dev/dahdi/trunkdev", O_RDWR);
if (g_fd < 0) {
fprintf(stderr, "Unable to open trunkdev: %s\n", strerror(errno));
exit(1);
}
if (!strcmp(argv[1], "create")) {
/* dahdi_trunkdev create foo */
if (argc < 3) {
fprintf(stderr, "Second argument must specify the name\n");
exit(2);
}
rc = trunkdev_create(g_fd, argv[2]);
if (rc < 0) {
fprintf(stderr, "Unable to create trunkdev: %s\n", strerror(errno));
exit(1);
}
printf("DAHDI trunkdev %s created; span number %d\n", argv[2], rc);
} else if (!strcmp(argv[1], "delete")) {
/* dahdi_trunkdev create foo */
if (argc < 3) {
fprintf(stderr, "Second argument must specify the name\n");
exit(2);
}
rc = trunkdev_delete(g_fd, argv[2]);
if (rc < 0) {
fprintf(stderr, "Unable to create trunkdev: %s\n", strerror(errno));
exit(1);
}
printf("DAHDI trunkdev %s deleted\n", argv[2]);
} else if (!strcmp(argv[1], "config")) {
/* dahdi_trunkdev config foo */
if (argc < 3) {
fprintf(stderr, "Second argument must specify the name\n");
exit(2);
}
rc = trunkdev_specify(g_fd, argv[2]);
if (rc < 0) {
fprintf(stderr, "Unable to specify trunkdev: %s\n", strerror(errno));
exit(2);
}
rc = config(rc, NULL);
if (rc < 0)
exit(2);
printf("DAHDI trunkdev %s configured\n", argv[2]);
} else if (!strcmp(argv[1], "config-hdlc")) {
/* dahdi_trunkdev config-hdlc foo hdlc-foo */
if (argc < 3) {
fprintf(stderr, "Second argument must specify the name\n");
exit(2);
}
if (argc < 4) {
fprintf(stderr, "Third argument must specify the net-device name\n");
exit(2);
}
rc = trunkdev_specify(g_fd, argv[2]);
if (rc < 0) {
fprintf(stderr, "Unable to specify trunkdev: %s\n", strerror(errno));
exit(2);
}
rc = config(rc, argv[3]);
if (rc < 0)
exit(2);
printf("DAHDI trunkdev %s configured as netdev %s\n", argv[2], argv[3]);
} else if (!strcmp(argv[1], "read") || !strcmp(argv[1], "rdwr")) {
/* dahdi_trunkdev (read|rdwr) foo */
if (argc < 3) {
fprintf(stderr, "Second argument must specify the name\n");
exit(2);
}
rc = trunkdev_specify(g_fd, argv[2]);
if (rc < 0) {
fprintf(stderr, "Unable to specify trunkdev: %s\n", strerror(errno));
exit(1);
}
if (!strcmp(argv[1], "read"))
printf("Reading + Disccarding from DAHDI trunkdev %s\n", argv[2]);
else
printf("Reading + Writing (looping back) DAHDI trunkdev %s\n", argv[2]);
while (1) {
uint8_t buf[32*8];
rc = read(g_fd, buf, sizeof(buf));
if (rc <= 0) {
fprintf(stderr, "Error %d reading: %s\n", rc, strerror(errno));
exit(1);
}
fputc('.', stdout);
if (!strcmp(argv[1], "rdwr")) {
rc = write(g_fd, buf, rc);
if (rc <= 0) {
fprintf(stderr, "Error %d writing: %s\n", rc, strerror(errno));
exit(1);
}
}
}
} else
exit(23);
exit(0);
}

View File

@ -60,7 +60,7 @@ union XTALK_PDATA(XTALK) {
MEMBER(XTALK, ACK);
MEMBER(XTALK, PROTO_GET);
MEMBER(XTALK, PROTO_GET_REPLY);
} PACKED members;
} PACKED _members;
const struct xtalk_protocol xtalk_sync_proto = {
.name = "XTALK-SYNC",