From: Maximilian Eschenbacher <maximilian@eschenbacher.email>
To: linux-kernel@vger.kernel.org
Cc: valentina.manea.m@gmail.com, shuah.kh@samsung.com,
gregkh@linuxfoundation.org,
Dominik Paulus <dominik.paulus@fau.de>,
Maximilian Eschenbacher <maximilian@eschenbacher.email>,
Fjodor Schelichow <fjodor.schelichow@hotmail.com>,
Johannes Stadlinger <johannes.stadlinger@fau.de>,
Tobias Polzer <tobias.polzer@fau.de>
Subject: [PATCH 04/18] usbip: Add CIDR matching helper functions
Date: Tue, 16 Sep 2014 23:38:41 +0000 [thread overview]
Message-ID: <1410910735-27929-5-git-send-email-maximilian@eschenbacher.email> (raw)
In-Reply-To: <1410910735-27929-1-git-send-email-maximilian@eschenbacher.email>
From: Dominik Paulus <dominik.paulus@fau.de>
This patch adds a few utility functions to match IP addresses against
CIDR masks.
Signed-off-by: Maximilian Eschenbacher <maximilian@eschenbacher.email>
Signed-off-by: Fjodor Schelichow <fjodor.schelichow@hotmail.com>
Signed-off-by: Johannes Stadlinger <johannes.stadlinger@fau.de>
Signed-off-by: Dominik Paulus <dominik.paulus@fau.de>
Signed-off-by: Tobias Polzer <tobias.polzer@fau.de>
---
tools/usb/usbip/src/utils.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
tools/usb/usbip/src/utils.h | 15 ++++++++
2 files changed, 103 insertions(+)
diff --git a/tools/usb/usbip/src/utils.c b/tools/usb/usbip/src/utils.c
index 2b3d6d2..8dae7aa 100644
--- a/tools/usb/usbip/src/utils.c
+++ b/tools/usb/usbip/src/utils.c
@@ -50,3 +50,91 @@ int modify_match_busid(char *busid, int add)
return 0;
}
+
+/*
+ * Parses a string of form "ip/prefix" into a subnet mask to dest.
+ * Returns -1 on error, 0 on success
+ */
+int parse_cidr(const char *src, struct subnet *dest)
+{
+ char *ip, *prefix, *saveptr;
+ char *endptr;
+ struct in6_addr ip6;
+ struct in_addr ip4;
+ int bits;
+ long int tmp;
+ char buf[128]; /* For strtok */
+
+ strncpy(buf, src, sizeof(buf));
+ buf[sizeof(buf)-1] = 0;
+
+ ip = strtok_r(buf, "/", &saveptr);
+ prefix = strtok_r(NULL, "/", &saveptr);
+ if (strtok_r(NULL, "/", &saveptr) || !ip ||
+ strlen(src) > sizeof(buf) - 1)
+ return -1;
+
+ if (inet_pton(AF_INET6, ip, &ip6) == 1) {
+ dest->ai_family = AF_INET6;
+ bits = 128;
+ dest->address.ip6 = ip6;
+ } else if (inet_pton(AF_INET, ip, &ip4) == 1) {
+ dest->ai_family = AF_INET;
+ bits = 32;
+ dest->address.ip4 = ip4;
+ } else {
+ return -1;
+ }
+
+ /*
+ * We also accept single IPs without an explicitely
+ * specified prefix
+ */
+ if (prefix) {
+ tmp = strtol(prefix, &endptr, 10);
+ if (tmp < 0 || tmp > bits || *endptr != '\0')
+ return -1;
+ dest->prefix = tmp;
+ } else {
+ dest->prefix = bits;
+ }
+
+ return 0;
+}
+
+/*
+ * Checks if addr is in range. Expects addr to be a struct in6_addr* if
+ * ai_family == AF_INET6, else struct in_addr*.
+ * Returns 1 if in range, 0 otherwise.
+ */
+int in_range(struct sockaddr_storage *addr, struct subnet range)
+{
+ if (addr->ss_family != range.ai_family)
+ return 0;
+ if (addr->ss_family == AF_INET6) {
+ int i;
+ struct sockaddr_in6 *in6addr = (struct sockaddr_in6 *) addr;
+ unsigned char *ip = in6addr->sin6_addr.s6_addr;
+
+ for (i = 0; i < range.prefix; ++i) {
+ int idx = i/8, mask = 1 << (7 - i%8);
+
+ if ((ip[idx] & mask) != (range.address.ip6.s6_addr[idx]
+ & mask))
+ return 0;
+ }
+ } else {
+ int i;
+ struct sockaddr_in *inaddr = (struct sockaddr_in *) addr;
+ uint32_t ip = ntohl(inaddr->sin_addr.s_addr);
+ uint32_t comp = ntohl(range.address.ip4.s_addr);
+
+ for (i = 0; i < range.prefix; ++i) {
+ int mask = 1 << (31-i);
+
+ if ((ip & mask) != (comp & mask))
+ return 0;
+ }
+ }
+ return 1;
+}
diff --git a/tools/usb/usbip/src/utils.h b/tools/usb/usbip/src/utils.h
index 5916fd3..a3704ef 100644
--- a/tools/usb/usbip/src/utils.h
+++ b/tools/usb/usbip/src/utils.h
@@ -19,7 +19,22 @@
#ifndef __UTILS_H
#define __UTILS_H
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netinet/ip.h>
+
+struct subnet {
+ int ai_family;
+ int prefix;
+ union {
+ struct in6_addr ip6;
+ struct in_addr ip4;
+ } address;
+};
+
int modify_match_busid(char *busid, int add);
+int parse_cidr(const char *src, struct subnet *dest);
+int in_range(struct sockaddr_storage *addr, struct subnet range);
#endif /* __UTILS_H */
--
2.1.0
next prev parent reply other threads:[~2014-09-16 22:07 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-16 23:38 [PATCH 00/18] usbip: Crypto and ACLs Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 01/18] usbip: sysfs_utils: add read_sysfs_attribute Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 02/18] usbip: Add support for client authentication Maximilian Eschenbacher
2014-09-21 0:42 ` Max Vozeler
2014-09-21 12:43 ` Dominik Paulus
2014-10-03 14:16 ` Valentina Manea
2014-09-16 23:38 ` [PATCH 03/18] usbip: Add kernel support for client ACLs Maximilian Eschenbacher
2014-09-21 0:44 ` Max Vozeler
2014-09-21 12:42 ` Dominik Paulus
2014-09-16 23:38 ` Maximilian Eschenbacher [this message]
2014-09-16 23:38 ` [PATCH 05/18] usbip: Add ACL support to usbip bind Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 06/18] usbip: Add support for ACLs in usbipd Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 07/18] usbip: Add proper error reporting Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 08/18] usbip: Handle usbip being started as user Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 09/18] usbip: Improve debug output Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 10/18] usbip: Separate protocol/program version Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 11/18] usbip: TLS for all userspace communication Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 12/18] usbip: Exchange session keys in userspace Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 13/18] usbip: Pass session keys to the kernel Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 14/18] usbip: Wrap kernel_sendmsg()/recvmsg() Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 15/18] usbip: Add encryption support to kernel Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 16/18] usbip: Update documentation Maximilian Eschenbacher
2014-09-16 23:38 ` [PATCH 17/18] usbip: Increment version number to 1.2.1 Maximilian Eschenbacher
2014-09-17 17:51 ` Denys Vlasenko
2014-09-18 15:31 ` Fjodor Schelichow
2014-09-16 23:38 ` [PATCH 18/18] usbip: list.h include stddef.h for offsetof Maximilian Eschenbacher
2014-09-26 11:56 ` [PATCH 00/18] usbip: Crypto and ACLs Valentina Manea
2014-11-07 17:49 ` Greg KH
2014-11-09 21:10 ` Valentina Manea
2014-11-09 23:33 ` Greg KH
2014-11-11 17:52 ` Maximilian Eschenbacher
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1410910735-27929-5-git-send-email-maximilian@eschenbacher.email \
--to=maximilian@eschenbacher.email \
--cc=dominik.paulus@fau.de \
--cc=fjodor.schelichow@hotmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=johannes.stadlinger@fau.de \
--cc=linux-kernel@vger.kernel.org \
--cc=shuah.kh@samsung.com \
--cc=tobias.polzer@fau.de \
--cc=valentina.manea.m@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).