mxs: enable uboot-mxs-duckbill build for duckbill profile
[openwrt.git] / package / network / services / hostapd / madwifi / net80211 / ieee80211_crypto.h
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $Id: ieee80211_crypto.h 3068 2007-12-21 17:46:02Z mentor $
33  */
34 #ifndef _NET80211_IEEE80211_CRYPTO_H_
35 #define _NET80211_IEEE80211_CRYPTO_H_
36
37 /*
38  * 802.11 protocol crypto-related definitions.
39  */
40 #define IEEE80211_KEYBUF_SIZE   16
41 #define IEEE80211_MICBUF_SIZE   (8 + 8)         /* space for both TX & RX keys */
42 #define IEEE80211_TID_SIZE      17              /* total number of TIDs */
43
44 /*
45  * Old WEP-style key.  Deprecated.
46  */
47 struct ieee80211_wepkey {
48         u_int wk_len;                           /* key length in bytes */
49         u_int8_t wk_key[IEEE80211_KEYBUF_SIZE];
50 };
51
52 struct ieee80211_cipher;
53
54 /*
55  * Crypto key state.  There is sufficient room for all supported
56  * ciphers (see below).  The underlying ciphers are handled
57  * separately through loadable cipher modules that register with
58  * the generic crypto support.  A key has a reference to an instance
59  * of the cipher; any per-key state is hung off wk_private by the
60  * cipher when it is attached.  Ciphers are automatically called
61  * to detach and cleanup any such state when the key is deleted.
62  *
63  * The generic crypto support handles encap/decap of cipher-related
64  * frame contents for both hardware- and software-based implementations.
65  * A key requiring software crypto support is automatically flagged and
66  * the cipher is expected to honor this and do the necessary work.
67  * Ciphers such as TKIP may also support mixed hardware/software
68  * encrypt/decrypt and MIC processing.
69  */
70
71 typedef u_int16_t ieee80211_keyix_t;
72
73 /* XXX pack better? */
74 /* XXX 48-bit rsc/tsc */
75 struct ieee80211_key {
76         u_int8_t wk_keylen;             /* key length in bytes */
77         u_int8_t wk_flags;
78 #define IEEE80211_KEY_XMIT      0x01    /* key used for xmit */
79 #define IEEE80211_KEY_RECV      0x02    /* key used for recv */
80 #define IEEE80211_KEY_GROUP     0x04    /* key used for WPA group operation */
81 #define IEEE80211_KEY_SWCRYPT   0x10    /* host-based encrypt/decrypt */
82 #define IEEE80211_KEY_SWMIC     0x20    /* host-based enmic/demic */
83         ieee80211_keyix_t wk_keyix;     /* key index */
84         u_int8_t wk_key[IEEE80211_KEYBUF_SIZE+IEEE80211_MICBUF_SIZE];
85 #define wk_txmic        wk_key+IEEE80211_KEYBUF_SIZE+0  /* XXX can't () right */
86 #define wk_rxmic        wk_key+IEEE80211_KEYBUF_SIZE+8  /* XXX can't () right */
87         u_int64_t wk_keyrsc[IEEE80211_TID_SIZE];        /* key receive sequence counter */
88         u_int64_t wk_keytsc;            /* key transmit sequence counter */
89         const struct ieee80211_cipher *wk_cipher;
90         void *wk_private;               /* private cipher state */
91 };
92 #define IEEE80211_KEY_COMMON            /* common flags passed in by apps */\
93         (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV | IEEE80211_KEY_GROUP)
94
95 /*
96  * NB: these values are ordered carefully; there are lots of
97  * of implications in any reordering.  In particular beware
98  * that 4 is not used to avoid conflicting with IEEE80211_F_PRIVACY.
99  */
100 #define IEEE80211_CIPHER_WEP            0
101 #define IEEE80211_CIPHER_TKIP           1
102 #define IEEE80211_CIPHER_AES_OCB        2
103 #define IEEE80211_CIPHER_AES_CCM        3
104 #define IEEE80211_CIPHER_CKIP           5
105 #define IEEE80211_CIPHER_NONE           6       /* pseudo value */
106
107 #define IEEE80211_CIPHER_MAX            (IEEE80211_CIPHER_NONE + 1)
108
109 #define IEEE80211_KEYIX_NONE    ((ieee80211_keyix_t)-1)
110
111 #if defined(__KERNEL__) || defined(_KERNEL)
112
113 struct ieee80211com;
114 struct ieee80211vap;
115 struct ieee80211_node;
116 struct sk_buff;
117
118 void ieee80211_crypto_attach(struct ieee80211com *);
119 void ieee80211_crypto_detach(struct ieee80211com *);
120 void ieee80211_crypto_vattach(struct ieee80211vap *);
121 void ieee80211_crypto_vdetach(struct ieee80211vap *);
122 int ieee80211_crypto_newkey(struct ieee80211vap *, int, int,
123         struct ieee80211_key *);
124 int ieee80211_crypto_delkey(struct ieee80211vap *, struct ieee80211_key *,
125         struct ieee80211_node *);
126 int ieee80211_crypto_setkey(struct ieee80211vap *, struct ieee80211_key *,
127         const u_int8_t macaddr[IEEE80211_ADDR_LEN], struct ieee80211_node *);
128 void ieee80211_crypto_delglobalkeys(struct ieee80211vap *);
129
130 /*
131  * Template for a supported cipher.  Ciphers register with the
132  * crypto code and are typically loaded as separate modules
133  * (the null cipher is always present).
134  * XXX may need refcnts
135  */
136 struct ieee80211_cipher {
137         const char *ic_name;            /* printable name */
138         u_int ic_cipher;                /* IEEE80211_CIPHER_* */
139         u_int ic_header;                /* size of privacy header (bytes) */
140         u_int ic_trailer;               /* size of privacy trailer (bytes) */
141         u_int ic_miclen;                /* size of mic trailer (bytes) */
142         void *(*ic_attach)(struct ieee80211vap *, struct ieee80211_key *);
143         void (*ic_detach)(struct ieee80211_key *);
144         int (*ic_setkey)(struct ieee80211_key *);
145         int (*ic_encap)(struct ieee80211_key *, struct sk_buff *, u_int8_t);
146         int (*ic_decap)(struct ieee80211_key *, struct sk_buff *, int);
147         int (*ic_enmic)(struct ieee80211_key *, struct sk_buff *, int);
148         int (*ic_demic)(struct ieee80211_key *, struct sk_buff *, int, int);
149 };
150 extern const struct ieee80211_cipher ieee80211_cipher_none;
151
152 void ieee80211_crypto_register(const struct ieee80211_cipher *);
153 void ieee80211_crypto_unregister(const struct ieee80211_cipher *);
154 int ieee80211_crypto_available(struct ieee80211vap*, u_int);
155
156 struct ieee80211_key *ieee80211_crypto_encap(struct ieee80211_node *,
157         struct sk_buff *);
158 struct ieee80211_key *ieee80211_crypto_decap(struct ieee80211_node *,
159         struct sk_buff *, int);
160
161 /*
162  * Check and remove any MIC.
163  */
164 static __inline int
165 ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
166         struct sk_buff *skb, int hdrlen, int force)
167 {
168         const struct ieee80211_cipher *cip = k->wk_cipher;
169         return (cip->ic_miclen > 0 ? cip->ic_demic(k, skb, hdrlen, force) : 1);
170 }
171
172 /*
173  * Add any MIC.
174  */
175 static __inline int
176 ieee80211_crypto_enmic(struct ieee80211vap *vap, struct ieee80211_key *k,
177         struct sk_buff *skb, int force)
178 {
179         const struct ieee80211_cipher *cip = k->wk_cipher;
180         return (cip->ic_miclen > 0 ? cip->ic_enmic(k, skb, force) : 1);
181 }
182
183 /* 
184  * Reset key state to an unused state.  The crypto
185  * key allocation mechanism ensures other state (e.g.
186  * key data) is properly setup before a key is used.
187  */
188 static __inline void
189 ieee80211_crypto_resetkey(struct ieee80211vap *vap, struct ieee80211_key *k,
190         ieee80211_keyix_t ix)
191 {
192         k->wk_cipher = &ieee80211_cipher_none;;
193         k->wk_private = k->wk_cipher->ic_attach(vap, k);
194         k->wk_keyix = ix;
195         k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
196 }
197
198 /*
199  * Crypto-related notification methods.
200  */
201 void ieee80211_notify_replay_failure(struct ieee80211vap *,
202         const struct ieee80211_frame *, const struct ieee80211_key *,
203         u_int64_t rsc);
204 void ieee80211_notify_michael_failure(struct ieee80211vap *,
205         const struct ieee80211_frame *, ieee80211_keyix_t keyix);
206 #endif /* defined(__KERNEL__) || defined(_KERNEL) */
207 #endif /* _NET80211_IEEE80211_CRYPTO_H_ */