]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
magically set interface up for connect/join
authorJohannes Berg <johannes@sipsolutions.net>
Sat, 11 Jul 2009 09:03:55 +0000 (11:03 +0200)
committerJohannes Berg <johannes@sipsolutions.net>
Sat, 11 Jul 2009 09:04:18 +0000 (11:04 +0200)
connect.c
ibss.c
iw.c
iw.h
util.c

index 91be62ac16af68556eeadb2cfdb48573a758c955..a4ea0f6dcc1063f6c875d7572b09a50bbe619cbb 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -14,7 +14,7 @@ static int iw_conn(struct nl80211_state *state, struct nl_cb *cb,
 {
        char *end;
        unsigned char bssid[6];
-       int freq;
+       int freq, err;
 
        if (argc < 1)
                return 1;
@@ -43,16 +43,20 @@ static int iw_conn(struct nl80211_state *state, struct nl_cb *cb,
                }
        }
 
-       if (!argc)
-               return 0;
+       if (argc) {
+               if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
+                       return 1;
 
-       if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
-               return 1;
+               argv++;
+               argc--;
 
-       argv++;
-       argc--;
+               err = parse_keys(msg, argv, argc);
+               if (err)
+                       return err;
+       }
+
+       return set_interface_up(state->ifname);
 
-       return parse_keys(msg, argv, argc);
  nla_put_failure:
        return -ENOSPC;
 }
diff --git a/ibss.c b/ibss.c
index 8785e7acaffd68fa0767f00e48f2cb500ae74b53..ee0732868838225992f666b7e874174b0cfb5f42 100644 (file)
--- a/ibss.c
+++ b/ibss.c
@@ -16,6 +16,7 @@ static int join_ibss(struct nl80211_state *state,
 {
        char *end;
        unsigned char abssid[6];
+       int err;
 
        if (argc < 2)
                return 1;
@@ -47,16 +48,20 @@ static int join_ibss(struct nl80211_state *state,
                }
        }
 
-       if (!argc)
-               return 0;
+       if (argc) {
+               if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
+                       return 1;
 
-       if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0)
-               return 1;
+               argv++;
+               argc--;
 
-       argv++;
-       argc--;
+               err = parse_keys(msg, argv, argc);
+               if (err)
+                       return err;
+       }
+
+       return set_interface_up(state->ifname);
 
-       return parse_keys(msg, argv, argc);
  nla_put_failure:
        return -ENOSPC;
 }
diff --git a/iw.c b/iw.c
index accf2721b5daacc84b3394a18b9228990208a59c..b26f86c632cd3d25528a1deef8e44b813f7654e9 100644 (file)
--- a/iw.c
+++ b/iw.c
@@ -253,6 +253,8 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        o_argc = argc;
        o_argv = argv;
 
+       state->ifname = NULL;
+
        switch (idby) {
        case II_PHY_IDX:
                command_idby = CIB_PHY;
@@ -270,6 +272,7 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                break;
        case II_NETDEV:
                command_idby = CIB_NETDEV;
+               state->ifname = *argv;
                devidx = if_nametoindex(*argv);
                if (devidx == 0)
                        devidx = -1;
diff --git a/iw.h b/iw.h
index 651e85c74cf0fa85dd26c436354041995c9daf3a..a33b3535c8db7b2628f0309576db61280f313b50 100644 (file)
--- a/iw.h
+++ b/iw.h
@@ -19,6 +19,8 @@ struct nl80211_state {
        struct nl_sock *nl_sock;
        struct nl_cache *nl_cache;
        struct genl_family *nl80211;
+
+       const char *ifname;
 };
 
 enum command_identify_by {
@@ -115,4 +117,6 @@ char *reg_initiator_to_string(__u8 initiator);
 const char *get_reason_str(uint16_t reason);
 const char *get_status_str(uint16_t status);
 
+int set_interface_up(const char *ifname);
+
 #endif /* __IW_H */
diff --git a/util.c b/util.c
index 6251defcc8580441baa37141574672e9a782ebe0..cca0c14637414d41324ec55bad8df9fabb0ef3fb 100644 (file)
--- a/util.c
+++ b/util.c
@@ -2,6 +2,10 @@
 #include <netlink/attr.h>
 #include <errno.h>
 #include <stdbool.h>
+#include <net/if.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
 #include "iw.h"
 #include "nl80211.h"
 
@@ -223,3 +227,31 @@ int parse_keys(struct nl_msg *msg, char **argv, int argc)
                        "for example: d:2:6162636465 is the same as d:2:abcde\n");
        return 2;
 }
+
+int set_interface_up(const char *ifname)
+{
+       struct ifreq ifr;
+       int fd = socket(PF_INET, SOCK_DGRAM, 0);
+       int err = 0;
+
+       if (fd < 0)
+               return -errno;
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
+               err = -errno;
+               goto out;
+       }
+
+       if (ifr.ifr_flags & IFF_UP)
+               goto out;
+
+       ifr.ifr_flags |= IFF_UP;
+       if (ioctl(fd, SIOCSIFFLAGS, &ifr))
+               err = -errno;
+
+ out:
+       close(fd);
+       return err;
+}