Merge commit 'garage/master'
[wpasupplicant] / wpa_supplicant / ibss_rsn.c
index e459081..b156684 100644 (file)
@@ -31,6 +31,13 @@ static void ibss_rsn_free(struct ibss_rsn_peer *peer)
 }
 
 
+static void supp_set_state(void *ctx, wpa_states state)
+{
+       struct ibss_rsn_peer *peer = ctx;
+       peer->supp_state = state;
+}
+
+
 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
                           size_t len)
 {
@@ -79,9 +86,16 @@ static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
 
 static int supp_get_beacon_ie(void *ctx)
 {
+       struct ibss_rsn_peer *peer = ctx;
+
        wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
-       /* TODO */
-       return -1;
+       /* TODO: get correct RSN IE */
+       return wpa_sm_set_ap_rsn_ie(peer->supp,
+                                   (u8 *) "\x30\x14\x01\x00"
+                                   "\x00\x0f\xac\x04"
+                                   "\x01\x00\x00\x0f\xac\x04"
+                                   "\x01\x00\x00\x0f\xac\x02"
+                                   "\x00\x00", 22);
 }
 
 
@@ -90,12 +104,33 @@ static int supp_set_key(void *ctx, wpa_alg alg,
                        const u8 *seq, size_t seq_len,
                        const u8 *key, size_t key_len)
 {
+       struct ibss_rsn_peer *peer = ctx;
+
+       if (key_idx == 0) {
+               /*
+                * In IBSS RSN, the pairwise key from the 4-way handshake
+                * initiated by the peer with highest MAC address is used.
+                */
+               if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
+                             ETH_ALEN) > 0)
+                       return 0;
+       }
+
        wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
                   "set_tx=%d)",
                   __func__, alg, MAC2STR(addr), key_idx, set_tx);
        wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
-       wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
-       return 0;
+       wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
+
+       return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
+                              set_tx, seq, seq_len, key, key_len);
+}
+
+
+static void * supp_get_network_ctx(void *ctx)
+{
+       struct ibss_rsn_peer *peer = ctx;
+       return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
 }
 
 
@@ -123,10 +158,13 @@ int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
                return -1;
 
        ctx->ctx = peer;
+       ctx->msg_ctx = peer->ibss_rsn->wpa_s;
+       ctx->set_state = supp_set_state;
        ctx->ether_send = supp_ether_send;
        ctx->get_beacon_ie = supp_get_beacon_ie;
        ctx->alloc_eapol = supp_alloc_eapol;
        ctx->set_key = supp_set_key;
+       ctx->get_network_ctx = supp_get_network_ctx;
        ctx->mlme_setprotection = supp_mlme_setprotection;
        ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
        peer->supp = wpa_sm_init(ctx);
@@ -143,7 +181,6 @@ int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
        wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
        wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
 
-#if 0 /* TODO? */
        peer->supp_ie_len = sizeof(peer->supp_ie);
        if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
                                            &peer->supp_ie_len) < 0) {
@@ -151,7 +188,6 @@ int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
                           " failed");
                return -1;
        }
-#endif
 
        wpa_sm_notify_assoc(peer->supp, peer->addr);
 
@@ -199,6 +235,42 @@ static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
 }
 
 
+static int auth_set_key(void *ctx, int vlan_id, const char *_alg,
+                       const u8 *addr, int idx, u8 *key,
+                       size_t key_len)
+{
+       struct ibss_rsn *ibss_rsn = ctx;
+       u8 seq[6];
+       wpa_alg alg;
+
+       os_memset(seq, 0, sizeof(seq));
+       if (os_strcmp(_alg, "none") == 0)
+               alg = WPA_ALG_NONE;
+       else if (os_strcmp(_alg, "WEP") == 0)
+               alg = WPA_ALG_WEP;
+       else if (os_strcmp(_alg, "TKIP") == 0)
+               alg = WPA_ALG_TKIP;
+       else if (os_strcmp(_alg, "CCMP") == 0)
+               alg = WPA_ALG_CCMP;
+       else if (os_strcmp(_alg, "IGTK") == 0)
+               alg = WPA_ALG_IGTK;
+       else
+               return -1;
+
+       if (idx == 0) {
+               /*
+                * In IBSS RSN, the pairwise key from the 4-way handshake
+                * initiated by the peer with highest MAC address is used.
+                */
+               if (os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0)
+                       return 0;
+       }
+
+       return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
+                              1, seq, 6, key, key_len);
+}
+
+
 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
                                    const u8 *own_addr)
 {
@@ -220,6 +292,7 @@ static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
        cb.logger = auth_logger;
        cb.send_eapol = auth_send_eapol;
        cb.get_psk = auth_get_psk;
+       cb.set_key = auth_set_key;
 
        ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
        if (ibss_rsn->auth_group == NULL) {
@@ -242,10 +315,11 @@ static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
 
        /* TODO: get peer RSN IE with Probe Request */
        if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
-                               (u8 *) "\x30\x12\x01\x00"
+                               (u8 *) "\x30\x14\x01\x00"
                                "\x00\x0f\xac\x04"
                                "\x01\x00\x00\x0f\xac\x04"
-                               "\x01\x00\x00\x0f\xac\x02", 20, NULL, 0) !=
+                               "\x01\x00\x00\x0f\xac\x02"
+                               "\x00\x00", 22, NULL, 0) !=
            WPA_IE_OK) {
                wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
                return -1;
@@ -327,3 +401,106 @@ void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
        os_free(ibss_rsn);
 
 }
+
+
+static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
+{
+       const struct ieee802_1x_hdr *hdr;
+       const struct wpa_eapol_key *key;
+       u16 key_info;
+       size_t plen;
+
+       /* TODO: Support other EAPOL packets than just EAPOL-Key */
+
+       if (len < sizeof(*hdr) + sizeof(*key))
+               return -1;
+
+       hdr = (const struct ieee802_1x_hdr *) buf;
+       key = (const struct wpa_eapol_key *) (hdr + 1);
+       plen = be_to_host16(hdr->length);
+
+       if (hdr->version < EAPOL_VERSION) {
+               /* TODO: backwards compatibility */
+       }
+       if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
+               wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
+                       "not a Key frame", hdr->type);
+               return -1;
+       }
+       if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
+               wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
+                          "invalid (frame size %lu)",
+                          (unsigned long) plen, (unsigned long) len);
+               return -1;
+       }
+
+       if (key->type != EAPOL_KEY_TYPE_RSN) {
+               wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
+                          "discarded", key->type);
+               return -1;
+       }
+
+       key_info = WPA_GET_BE16(key->key_info);
+
+       return !!(key_info & WPA_KEY_INFO_ACK);
+}
+
+
+static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
+                                    struct ibss_rsn_peer *peer,
+                                    const u8 *buf, size_t len)
+{
+       int supp;
+       u8 *tmp;
+
+       supp = ibss_rsn_eapol_dst_supp(buf, len);
+       if (supp < 0)
+               return -1;
+
+       tmp = os_malloc(len);
+       if (tmp == NULL)
+               return -1;
+       os_memcpy(tmp, buf, len);
+       if (supp) {
+               wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
+               wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
+       } else {
+               wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
+               wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
+       }
+       os_free(tmp);
+
+       return 1;
+}
+
+
+int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
+                     const u8 *buf, size_t len)
+{
+       struct ibss_rsn_peer *peer;
+
+       for (peer = ibss_rsn->peers; peer; peer = peer->next) {
+               if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
+                       return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
+                                                        buf, len);
+       }
+
+       if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
+               /*
+                * Create new IBSS peer based on an EAPOL message from the peer
+                * Authenticator.
+                */
+               if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
+                       return -1;
+               return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
+                                                buf, len);
+       }
+
+       return 0;
+}
+
+
+void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
+{
+       os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
+}