From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andy Shevchenko Subject: [PATCH v1 2/4] lib/net_utils: Introduce mac_pton_from_user() Date: Tue, 19 Dec 2017 21:14:10 +0200 Message-ID: <20171219191412.14880-2-andriy.shevchenko@linux.intel.com> References: <20171219191412.14880-1-andriy.shevchenko@linux.intel.com> Cc: Andy Shevchenko To: "David S. Miller" , netdev@vger.kernel.org, Larry Finger , Florian Schilhabel , devel@driverdev.osuosl.org, Greg Kroah-Hartman Return-path: Received: from mga01.intel.com ([192.55.52.88]:15902 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750798AbdLSTPR (ORCPT ); Tue, 19 Dec 2017 14:15:17 -0500 In-Reply-To: <20171219191412.14880-1-andriy.shevchenko@linux.intel.com> Sender: netdev-owner@vger.kernel.org List-ID: Some drivers are getting MAC from user space. Make a helper for them. Signed-off-by: Andy Shevchenko --- include/linux/kernel.h | 1 + lib/net_utils.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index ce51455e2adf..e203b313608d 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -587,6 +587,7 @@ extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); extern char *bin2hex(char *dst, const void *src, size_t count); bool mac_pton(const char *s, u8 *mac); +int __must_check mac_pton_from_user(const char __user *s, size_t count, u8 *mac); /* * General tracing related utility functions - trace_printk(), diff --git a/lib/net_utils.c b/lib/net_utils.c index d32c6961fe0f..7be3483aece6 100644 --- a/lib/net_utils.c +++ b/lib/net_utils.c @@ -3,6 +3,7 @@ #include #include #include +#include #define MAC_PTON_MINLEN (3 * ETH_ALEN - 1) @@ -27,3 +28,14 @@ bool mac_pton(const char *s, u8 *mac) return true; } EXPORT_SYMBOL(mac_pton); + +int mac_pton_from_user(const char __user *s, size_t count, u8 *mac) +{ + char buf[MAC_PTON_MINLEN]; + + count = min(count, sizeof(buf)); + if (copy_from_user(buf, s, count)) + return -EFAULT; + return mac_pton(buf, mac) ? 0 : -EINVAL; +} +EXPORT_SYMBOL(mac_pton_from_user); -- 2.15.1