From patchwork Tue Aug 2 14:37:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Aring X-Patchwork-Id: 9257779 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4D3EA60754 for ; Tue, 2 Aug 2016 14:40:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F60D2860E for ; Tue, 2 Aug 2016 14:40:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3444028610; Tue, 2 Aug 2016 14:40:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1446628611 for ; Tue, 2 Aug 2016 14:40:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756890AbcHBOjY (ORCPT ); Tue, 2 Aug 2016 10:39:24 -0400 Received: from metis.ext.4.pengutronix.de ([92.198.50.35]:40672 "EHLO metis.ext.4.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756860AbcHBOix (ORCPT ); Tue, 2 Aug 2016 10:38:53 -0400 Received: from gallifrey.ext.pengutronix.de ([2001:67c:670:201:5054:ff:fe8d:eefb] helo=omega.localdomain) by metis.ext.pengutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1bUapp-0005Mi-7q; Tue, 02 Aug 2016 16:38:21 +0200 From: Alexander Aring To: linux-wpan@vger.kernel.org Cc: kernel@pengutronix.de, linux-bluetooth@vger.kernel.org, Alexander Aring Subject: [PATCH linux-wpan/radvd for-upstream 2/2] device-linux: get address length via sysfs Date: Tue, 2 Aug 2016 16:37:52 +0200 Message-Id: <20160802143752.12831-3-aar@pengutronix.de> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160802143752.12831-1-aar@pengutronix.de> References: <20160802143752.12831-1-aar@pengutronix.de> X-SA-Exim-Connect-IP: 2001:67c:670:201:5054:ff:fe8d:eefb X-SA-Exim-Mail-From: aar@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-wpan@vger.kernel.org Sender: linux-wpan-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wpan@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch will change the behaviour for 6LoWPAN interface to detect the linklayer address length via sysfs. The usually way to get the address length is via a mapping from ARPHRD device type. This doesn't work for 6LoWPAN interfaces, we need at least some other mechanism. This patch adds the mechanism to read out the sysfs UAPI to get the addr_len attribute of net_device. This will not work if there are two 6LoWPAN link-layer types with the same address length but need different handling in userspace. For that reason I think a linklayer type would be better, this requires a 6lowpan netlink API which doesn't exist right now. So we use this simple way now. Signed-off-by: Alexander Aring --- device-linux.c | 38 ++++++++++++++++++++++++++++++++++++-- pathnames.h | 1 + 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/device-linux.c b/device-linux.c index 7301927..c483952 100644 --- a/device-linux.c +++ b/device-linux.c @@ -29,6 +29,34 @@ static char const *hwstr(unsigned short sa_family); /* + * get interface address length over sysfs + */ +static int get_device_addr_len(struct Interface *iface) +{ + char path[PATH_MAX]; + uint32_t addr_len; + FILE *f; + int ret; + + ret = sprintf(path, SYS_CLASS_NET_ADDRLEN, iface->props.name); + if (ret < 0) + return -1; + + f = fopen(path, "r"); + if (!f) + return -1; + + ret = fscanf(f, "%u", &addr_len); + if (ferror(f)) { + fclose(f); + return -1; + } + + fclose(f); + return addr_len; +} + +/* * this function gets the hardware type and address of an interface, * determines the link layer token length and checks it against * the defined prefixes @@ -84,8 +112,14 @@ int update_device_info(int sock, struct Interface *iface) break; #endif /* ARPHDR_ARCNET */ case ARPHRD_6LOWPAN: - iface->sllao.if_hwaddr_len = 64; - iface->sllao.if_prefix_len = 64; + iface->sllao.if_hwaddr_len = get_device_addr_len(iface); + if (iface->sllao.if_hwaddr_len != -1) { + iface->sllao.if_hwaddr_len *= 8; + iface->sllao.if_prefix_len = 64; + } else { + iface->sllao.if_prefix_len = -1; + } + break; default: iface->sllao.if_hwaddr_len = -1; diff --git a/pathnames.h b/pathnames.h index 580e2b2..152bb7a 100644 --- a/pathnames.h +++ b/pathnames.h @@ -40,6 +40,7 @@ #define PROC_SYS_IP6_BASEREACHTIME "/proc/sys/net/ipv6/neigh/%s/base_reachable_time" #define PROC_SYS_IP6_RETRANSTIMER_MS "/proc/sys/net/ipv6/neigh/%s/retrans_time_ms" #define PROC_SYS_IP6_RETRANSTIMER "/proc/sys/net/ipv6/neigh/%s/retrans_time" +#define SYS_CLASS_NET_ADDRLEN "/sys/class/net/%s/addr_len" #else /* BSD */ #define SYSCTL_IP6_FORWARDING CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_FORWARDING #endif