Change in libosmocore[master]: gprs_ns2: fr: setup the device to correct FR/LMI settings

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

lynxis lazus gerrit-no-reply at lists.osmocom.org
Thu Dec 17 04:04:10 UTC 2020


lynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/21752 )


Change subject: gprs_ns2: fr: setup the device to correct FR/LMI settings
......................................................................

gprs_ns2: fr: setup the device to correct FR/LMI settings

A hdlc can be used in different modes. Also a FR device can be used
with lmi and certain settings as without it.
ns2 will use FR with no lmi in the kernel.

Change-Id: I04786d2b864860b08c2e1afdb199470f4b80cc3b
---
M src/gb/gprs_ns2_fr.c
1 file changed, 110 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/52/21752/1

diff --git a/src/gb/gprs_ns2_fr.c b/src/gb/gprs_ns2_fr.c
index 6aa687c..b5c096f 100644
--- a/src/gb/gprs_ns2_fr.c
+++ b/src/gb/gprs_ns2_fr.c
@@ -36,12 +36,14 @@
 #include <netinet/ip.h>
 #include <netinet/ip6.h>
 #include <arpa/inet.h>
-#include <net/if.h>
+#include <linux/if.h>
 
 #include <sys/ioctl.h>
 #include <netpacket/packet.h>
 #include <linux/if_ether.h>
 #include <linux/hdlc.h>
+#include <linux/hdlc/ioctl.h>
+#include <linux/sockios.h>
 
 #include <osmocom/gprs/frame_relay.h>
 #include <osmocom/core/byteswap.h>
@@ -84,7 +86,7 @@
 };
 
 struct priv_bind {
-	char netif[IF_NAMESIZE];
+	char netif[IFNAMSIZ];
 	struct osmo_fr_link *link;
 	struct osmo_wqueue wqueue;
 	int ifindex;
@@ -434,6 +436,102 @@
 }
 #endif /* LIBMNL */
 
+int set_ifupdown(const char *netif, bool up)
+{
+	int sock, rc;
+	struct ifreq req;
+
+	sock = socket(AF_INET, SOCK_DGRAM, 0);
+	if (sock < 0)
+		return sock;
+
+	memset(&req, 0, sizeof req);
+	strncpy(req.ifr_name, netif, IFNAMSIZ);
+
+	if (up)
+		req.ifr_flags |= IFF_UP;
+
+	rc = ioctl(sock, SIOCSIFFLAGS, &req);
+	close(sock);
+	return rc;
+}
+
+int setup_device(const char *netif)
+{
+	int sock, rc;
+	char buffer[128];
+	fr_proto *fr = (void*)buffer;
+	struct ifreq req;
+
+	sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+	if (sock < 0) {
+		LOGP(DLNS, LOGL_ERROR, "%s: Unable to create socket: %s\n",
+		     netif, strerror(errno));
+		return sock;
+	}
+
+	memset(&req, 0, sizeof(struct ifreq));
+	memset(&buffer, 0, sizeof(buffer));
+	strncpy(req.ifr_name, netif, IFNAMSIZ);
+	req.ifr_settings.ifs_ifsu.sync = (void*)buffer;
+	req.ifr_settings.size = sizeof(buffer);
+	req.ifr_settings.type = IF_GET_PROTO;
+
+	rc = ioctl(sock, SIOCWANDEV, &req);
+	if (rc < 0) {
+		LOGP(DLNS, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n",
+		     netif, strerror(errno));
+		goto err;
+	}
+
+	/* check if the device is good */
+	if (req.ifr_settings.type != IF_PROTO_FR && fr->lmi != LMI_NONE) {
+		LOGP(DLNS, LOGL_INFO, "%s: has correct frame relay mode and lmi\n", netif);
+		goto ifup;
+	}
+
+	/* modify the device to match */
+	rc = set_ifupdown(netif, false);
+	if (rc) {
+		LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
+		      netif, strerror(errno));
+		goto err;
+	}
+
+	memset(&req, 0, sizeof(struct ifreq));
+	memset(fr, 0, sizeof(fr_proto));
+	strncpy(req.ifr_name, netif, IFNAMSIZ);
+	req.ifr_settings.type = IF_PROTO_FR;
+	req.ifr_settings.size = sizeof(fr_proto);
+	req.ifr_settings.ifs_ifsu.fr = fr;
+	fr->lmi = LMI_NONE;
+	/* polling verification timer*/
+	fr->t391 = 10;
+	/* link integrity verification polling timer */
+	fr->t392 = 15;
+	/* full status polling counter*/
+	fr->n391 = 6;
+	/* error threshold */
+	fr->n392 = 3;
+	/* monitored events count */
+	fr->n393 = 4;
+
+	rc = ioctl(sock, SIOCWANDEV, &req);
+	if (rc) {
+		LOGP(DLNS, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n",
+		      netif, strerror(errno));
+		goto err;
+	}
+
+ifup:
+	rc = set_ifupdown(netif, true);
+	if (rc)
+		LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
+		      netif, strerror(errno));
+err:
+	close(sock);
+	return rc;
+}
 
 /*! Create a new bind for NS over FR.
  *  \param[in] nsi NS instance in which to create the bind
@@ -482,7 +580,7 @@
 		goto err_name;
 	}
 
-	if (strlen(netif) > IF_NAMESIZE) {
+	if (strlen(netif) > IFNAMSIZ) {
 		rc = -EINVAL;
 		goto err_priv;
 	}
@@ -508,6 +606,14 @@
 		goto err_fr;
 	}
 
+	/* set protocol frame relay and lmi */
+	rc = setup_device(priv->netif);
+	if(rc < 0) {
+		LOGP(DLNS, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif);
+		goto err_fr;
+	}
+
+
 	osmo_wqueue_init(&priv->wqueue, 10);
 	priv->wqueue.write_cb = handle_netif_write;
 	priv->wqueue.read_cb = handle_netif_read;
@@ -600,7 +706,7 @@
 			continue;
 
 		_netif = gprs_ns2_fr_bind_netif(bind);
-		if (!strncmp(_netif, netif, IF_NAMESIZE))
+		if (!strncmp(_netif, netif, IFNAMSIZ))
 			return bind;
 	}
 

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/21752
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I04786d2b864860b08c2e1afdb199470f4b80cc3b
Gerrit-Change-Number: 21752
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201217/446dd465/attachment.htm>


More information about the gerrit-log mailing list