]>
Commit | Line | Data |
---|---|---|
99dde7aa JB |
1 | #include <errno.h> |
2 | ||
3 | #include <netlink/genl/genl.h> | |
4 | #include <netlink/genl/family.h> | |
5 | #include <netlink/genl/ctrl.h> | |
6 | #include <netlink/msg.h> | |
7 | #include <netlink/attr.h> | |
8 | ||
9 | #include "nl80211.h" | |
10 | #include "iw.h" | |
11 | ||
7fabd346 JB |
12 | static int iw_conn(struct nl80211_state *state, struct nl_cb *cb, |
13 | struct nl_msg *msg, int argc, char **argv) | |
99dde7aa JB |
14 | { |
15 | char *end; | |
51e9bd80 | 16 | unsigned char bssid[6]; |
ded1f078 | 17 | int freq, err; |
99dde7aa JB |
18 | |
19 | if (argc < 1) | |
20 | return 1; | |
21 | ||
22 | /* SSID */ | |
23 | NLA_PUT(msg, NL80211_ATTR_SSID, strlen(argv[0]), argv[0]); | |
24 | argv++; | |
25 | argc--; | |
26 | ||
27 | /* freq */ | |
28 | if (argc) { | |
29 | freq = strtoul(argv[0], &end, 10); | |
30 | if (*end == '\0') { | |
31 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
32 | argv++; | |
33 | argc--; | |
34 | } | |
35 | } | |
36 | ||
37 | /* bssid */ | |
38 | if (argc) { | |
51e9bd80 JB |
39 | if (mac_addr_a2n(bssid, argv[0]) == 0) { |
40 | NLA_PUT(msg, NL80211_ATTR_MAC, 6, bssid); | |
41 | argv++; | |
42 | argc--; | |
43 | } | |
99dde7aa JB |
44 | } |
45 | ||
ded1f078 JB |
46 | if (argc) { |
47 | if (strcmp(*argv, "key") != 0 && strcmp(*argv, "keys") != 0) | |
48 | return 1; | |
51e9bd80 | 49 | |
ded1f078 JB |
50 | argv++; |
51 | argc--; | |
99dde7aa | 52 | |
ded1f078 JB |
53 | err = parse_keys(msg, argv, argc); |
54 | if (err) | |
55 | return err; | |
56 | } | |
57 | ||
58 | return set_interface_up(state->ifname); | |
51e9bd80 | 59 | |
99dde7aa JB |
60 | nla_put_failure: |
61 | return -ENOSPC; | |
62 | } | |
63 | ||
64 | static int disconnect(struct nl80211_state *state, | |
65 | struct nl_cb *cb, | |
66 | struct nl_msg *msg, | |
67 | int argc, char **argv) | |
68 | { | |
69 | return 0; | |
70 | } | |
71 | TOPLEVEL(disconnect, NULL, | |
72 | NL80211_CMD_DISCONNECT, 0, CIB_NETDEV, disconnect, | |
73 | "Disconnect from the current network."); | |
7fabd346 JB |
74 | HIDDEN(conn, establish, "", NL80211_CMD_CONNECT, 0, CIB_NETDEV, iw_conn); |
75 | ||
76 | static int iw_connect(struct nl80211_state *state, struct nl_cb *cb, | |
77 | struct nl_msg *msg, int argc, char **argv) | |
78 | { | |
79 | char **conn_argv, *dev = argv[0]; | |
80 | static const __u32 cmds[] = { | |
81 | NL80211_CMD_CONNECT, | |
82 | }; | |
83 | struct print_event_args printargs = { }; | |
84 | int conn_argc, err; | |
85 | bool wait = false; | |
86 | int i; | |
87 | ||
88 | /* strip "wlan0 connect" */ | |
89 | argc -= 2; | |
90 | argv += 2; | |
91 | ||
92 | /* check -w */ | |
93 | if (argc && strcmp(argv[0], "-w") == 0) { | |
94 | wait = true; | |
95 | argc--; | |
96 | argv++; | |
97 | } | |
98 | ||
99 | conn_argc = 3 + argc; | |
100 | conn_argv = calloc(conn_argc, sizeof(*conn_argv)); | |
101 | if (!conn_argv) | |
102 | return -ENOMEM; | |
103 | conn_argv[0] = dev; | |
104 | conn_argv[1] = "conn"; | |
105 | conn_argv[2] = "establish"; | |
106 | for (i = 0; i < argc; i++) | |
107 | conn_argv[i + 3] = argv[i]; | |
108 | err = handle_cmd(state, II_NETDEV, conn_argc, conn_argv); | |
109 | free(conn_argv); | |
110 | if (err) | |
111 | return err; | |
112 | ||
113 | if (!wait) | |
114 | return 0; | |
115 | ||
116 | /* | |
117 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
118 | * | |
119 | * This code has a bug, which requires creating a separate | |
120 | * nl80211 socket to fix: | |
121 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
122 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
123 | * before (!) we listen to it, because we only start listening | |
124 | * after we send our scan request. | |
125 | * | |
126 | * Doing it the other way around has a race condition as well, | |
127 | * if you first open the events socket you may get a notification | |
128 | * for a previous scan. | |
129 | * | |
130 | * The only proper way to fix this would be to listen to events | |
131 | * before sending the command, and for the kernel to send the | |
132 | * connect request along with the event, so that you can match | |
133 | * up whether the connect _you_ requested was finished or aborted. | |
134 | * | |
135 | * Alas, the kernel doesn't do that (yet). | |
136 | */ | |
137 | ||
138 | __listen_events(state, ARRAY_SIZE(cmds), cmds, &printargs); | |
139 | return 0; | |
140 | } | |
141 | TOPLEVEL(connect, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]", | |
142 | 0, 0, CIB_NETDEV, iw_connect, | |
143 | "Join the network with the given SSID (and frequency, BSSID).\n" | |
144 | "With -w, wait for the connect to finish or fail."); |