]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
iw: make parse_random_mac_addr() more generally available
authorJohannes Berg <johannes.berg@intel.com>
Mon, 19 Nov 2018 10:22:55 +0000 (11:22 +0100)
committerJohannes Berg <johannes.berg@intel.com>
Fri, 1 Feb 2019 21:57:23 +0000 (22:57 +0100)
The parse_random_mac_addr() function is useful in contexts
other than scanning, so make it more generally available and
make it set the attributes to randomise all when no arguments
are given.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
iw.h
scan.c
util.c

diff --git a/iw.h b/iw.h
index c9184f3f70734562f2ecca877362d09e8c1cf125..69ca3992d419514b583b03aeeff7898534b97140 100644 (file)
--- a/iw.h
+++ b/iw.h
@@ -225,6 +225,8 @@ void iw_hexdump(const char *prefix, const __u8 *data, size_t len);
 
 int get_cf1(const struct chanmode *chanmode, unsigned long freq);
 
+int parse_random_mac_addr(struct nl_msg *msg, char *addrs);
+
 #define SCHED_SCAN_OPTIONS "[interval <in_msecs> | scan_plans [<interval_secs:iterations>*] <interval_secs>] " \
        "[delay <in_secs>] [freqs <freq>+] [matches [ssid <ssid>]+]] [active [ssid <ssid>]+|passive] "  \
        "[randomise[=<addr>/<mask>]]"
diff --git a/scan.c b/scan.c
index ca7dbc6d36c95194de99ce0e74038dd494393b18..b0b7828c3c793d2bbdf0440ac13b2ccc998aa77a 100644 (file)
--- a/scan.c
+++ b/scan.c
@@ -69,35 +69,6 @@ union ieee80211_country_ie_triplet {
        } __attribute__ ((packed)) ext;
 } __attribute__ ((packed));
 
-static int parse_random_mac_addr(struct nl_msg *msg, char *arg)
-{
-       char *a_addr, *a_mask, *sep;
-       unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
-       char *addrs = arg + 9;
-
-       if (*addrs != '=')
-               return 0;
-
-       addrs++;
-       sep = strchr(addrs, '/');
-       a_addr = addrs;
-
-       if (!sep)
-               return 1;
-
-       *sep = 0;
-       a_mask = sep + 1;
-       if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
-               return 1;
-
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-       NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
-
-       return 0;
- nla_put_failure:
-       return -ENOBUFS;
-}
-
 int parse_sched_scan(struct nl_msg *msg, int *argc, char ***argv)
 {
        struct nl_msg *matchset = NULL, *freqs = NULL, *ssids = NULL;
@@ -228,11 +199,9 @@ int parse_sched_scan(struct nl_msg *msg, int *argc, char ***argv)
                        } else if (!strncmp(v[0], "randomise", 9) ||
                                   !strncmp(v[0], "randomize", 9)) {
                                flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
-                               if (c > 0) {
-                                       err = parse_random_mac_addr(msg, v[0]);
-                                       if (err)
-                                               goto nla_put_failure;
-                               }
+                               err = parse_random_mac_addr(msg, v[0] + 9);
+                               if (err)
+                                       goto nla_put_failure;
                        } else {
                                /* this element is not for us, so
                                 * return to continue parsing.
@@ -457,7 +426,7 @@ static int handle_scan(struct nl80211_state *state,
                        } else if (strncmp(argv[i], "randomise", 9) == 0 ||
                                   strncmp(argv[i], "randomize", 9) == 0) {
                                flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
-                               err = parse_random_mac_addr(msg, argv[i]);
+                               err = parse_random_mac_addr(msg, argv[i] + 9);
                                if (err)
                                        goto nla_put_failure;
                                break;
diff --git a/util.c b/util.c
index 119911f524cab644a4ec39bd77258b0ad8ec048b..2fa0b746048d34f6b7665c58cf8df6e00c90f83e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1139,3 +1139,40 @@ int get_cf1(const struct chanmode *chanmode, unsigned long freq)
 
        return cf1;
 }
+
+int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
+{
+       char *a_addr, *a_mask, *sep;
+       unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
+
+       if (!*addrs) {
+               /* randomise all but the multicast bit */
+               NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
+                       "\x00\x00\x00\x00\x00\x00");
+               NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
+                       "\x01\x00\x00\x00\x00\x00");
+               return 0;
+       }
+
+       if (*addrs != '=')
+               return 1;
+
+       addrs++;
+       sep = strchr(addrs, '/');
+       a_addr = addrs;
+
+       if (!sep)
+               return 1;
+
+       *sep = 0;
+       a_mask = sep + 1;
+       if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
+               return 1;
+
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+       NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
+
+       return 0;
+ nla_put_failure:
+       return -ENOBUFS;
+}