summaryrefslogtreecommitdiffstats
path: root/src/iwkey.c
blob: b4b44c5e976d91a84a368e80b12025a69d9b5f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* $Id$ */

/*
 * This file is part of waproamd.
 *
 * waproamd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * waproamd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with waproamd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "iwkey.h"
#include "wireless.h"
#include "util.h"

static int n_used_keys = 0;

int wep_key_add(struct interface *i, uint8_t w[WEP_KEY_LEN]) {
    struct iwreq req;
    assert(i);
    
    if (n_used_keys >= n_max_keys) {
        fprintf(stderr, "Too many keys added!\n");
        return -1;
    }

    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);

    req.u.encoding.pointer = w;
    req.u.encoding.length = WEP_KEY_LEN;
    req.u.encoding.flags = key_map[n_used_keys++]+1;
    
    if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
        fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
        return -1;
    }

    return 0;    
}

int wep_key_finish(struct interface *i) {
    struct iwreq req;
    assert(i);

    if (n_used_keys) {
        uint8_t tmp[WEP_KEY_LEN];
        int j;
        
        memset(&req, 0, sizeof(req));
        strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
        
        req.u.encoding.pointer = tmp;
        req.u.encoding.length = WEP_KEY_LEN;
        req.u.encoding.flags = (key_map[n_used_keys-1]+1);
        
        if (ioctl(i->fd, SIOCGIWENCODE, &req) < 0) {
            fprintf(stderr, "ioctl(SIOCGIWENCODE): %s\n", strerror(errno));
            return -1;
        }
        
        for (j = n_used_keys; j < n_max_keys; j++) {
            memset(&req, 0, sizeof(req));
            strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
            
            req.u.encoding.pointer = tmp;
            req.u.encoding.length = WEP_KEY_LEN;
            req.u.encoding.flags = (key_map[j]+1);
            
            if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
                fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
                return -1;
            }
        }
    }
        
    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_ifrn.ifrn_name, i->name, IFNAMSIZ);
    
    req.u.encoding.pointer = NULL;
    req.u.encoding.length = 0;
    req.u.encoding.flags = IW_ENCODE_RESTRICTED;

    if (ioctl(i->fd, SIOCSIWENCODE, &req) < 0) {
        fprintf(stderr, "ioctl(SIOCSIWENCODE): %s\n", strerror(errno));
        return -1;
    }

    n_used_keys = 0;
    
    return 0;
}