~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to src/eap_common/eap_common.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * EAP common peer/server definitions
3
 
 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * Alternatively, this software may be distributed under the terms of BSD
10
 
 * license.
11
 
 *
12
 
 * See README and COPYING for more details.
13
 
 */
14
 
 
15
 
#include "includes.h"
16
 
 
17
 
#include "common.h"
18
 
#include "eap_defs.h"
19
 
#include "eap_common.h"
20
 
 
21
 
/**
22
 
 * eap_hdr_validate - Validate EAP header
23
 
 * @vendor: Expected EAP Vendor-Id (0 = IETF)
24
 
 * @eap_type: Expected EAP type number
25
 
 * @msg: EAP frame (starting with EAP header)
26
 
 * @msglen: Length of msg
27
 
 * @plen: Pointer to variable to contain the returned payload length
28
 
 * Returns: Pointer to EAP payload (after type field), or %NULL on failure
29
 
 *
30
 
 * This is a helper function for EAP method implementations. This is usually
31
 
 * called in the beginning of struct eap_method::process() function to verify
32
 
 * that the received EAP request packet has a valid header. This function is
33
 
 * able to process both legacy and expanded EAP headers and in most cases, the
34
 
 * caller can just use the returned payload pointer (into *plen) for processing
35
 
 * the payload regardless of whether the packet used the expanded EAP header or
36
 
 * not.
37
 
 */
38
 
const u8 * eap_hdr_validate(int vendor, EapType eap_type,
39
 
                            const u8 *msg, size_t msglen, size_t *plen)
40
 
{
41
 
        const struct eap_hdr *hdr;
42
 
        const u8 *pos;
43
 
        size_t len;
44
 
 
45
 
        hdr = (const struct eap_hdr *) msg;
46
 
 
47
 
        if (msglen < sizeof(*hdr)) {
48
 
                wpa_printf(MSG_INFO, "EAP: Too short EAP frame");
49
 
                return NULL;
50
 
        }
51
 
 
52
 
        len = be_to_host16(hdr->length);
53
 
        if (len < sizeof(*hdr) + 1 || len > msglen) {
54
 
                wpa_printf(MSG_INFO, "EAP: Invalid EAP length");
55
 
                return NULL;
56
 
        }
57
 
 
58
 
        pos = (const u8 *) (hdr + 1);
59
 
 
60
 
        if (*pos == EAP_TYPE_EXPANDED) {
61
 
                int exp_vendor;
62
 
                u32 exp_type;
63
 
                if (len < sizeof(*hdr) + 8) {
64
 
                        wpa_printf(MSG_INFO, "EAP: Invalid expanded EAP "
65
 
                                   "length");
66
 
                        return NULL;
67
 
                }
68
 
                pos++;
69
 
                exp_vendor = WPA_GET_BE24(pos);
70
 
                pos += 3;
71
 
                exp_type = WPA_GET_BE32(pos);
72
 
                pos += 4;
73
 
                if (exp_vendor != vendor || exp_type != (u32) eap_type) {
74
 
                        wpa_printf(MSG_INFO, "EAP: Invalid expanded frame "
75
 
                                   "type");
76
 
                        return NULL;
77
 
                }
78
 
 
79
 
                *plen = len - sizeof(*hdr) - 8;
80
 
                return pos;
81
 
        } else {
82
 
                if (vendor != EAP_VENDOR_IETF || *pos != eap_type) {
83
 
                        wpa_printf(MSG_INFO, "EAP: Invalid frame type");
84
 
                        return NULL;
85
 
                }
86
 
                *plen = len - sizeof(*hdr) - 1;
87
 
                return pos + 1;
88
 
        }
89
 
}
90
 
 
91
 
 
92
 
/**
93
 
 * eap_msg_alloc - Allocate a buffer for an EAP message
94
 
 * @vendor: Vendor-Id (0 = IETF)
95
 
 * @type: EAP type
96
 
 * @len: Buffer for returning message length
97
 
 * @payload_len: Payload length in bytes (data after Type)
98
 
 * @code: Message Code (EAP_CODE_*)
99
 
 * @identifier: Identifier
100
 
 * @payload: Pointer to payload pointer that will be set to point to the
101
 
 * beginning of the payload or %NULL if payload pointer is not needed
102
 
 * Returns: Pointer to the allocated message buffer or %NULL on error
103
 
 *
104
 
 * This function can be used to allocate a buffer for an EAP message and fill
105
 
 * in the EAP header. This function is automatically using expanded EAP header
106
 
 * if the selected Vendor-Id is not IETF. In other words, most EAP methods do
107
 
 * not need to separately select which header type to use when using this
108
 
 * function to allocate the message buffers.
109
 
 */
110
 
struct eap_hdr * eap_msg_alloc(int vendor, EapType type, size_t *len,
111
 
                               size_t payload_len, u8 code, u8 identifier,
112
 
                               u8 **payload)
113
 
{
114
 
        struct eap_hdr *hdr;
115
 
        u8 *pos;
116
 
 
117
 
        *len = sizeof(struct eap_hdr) + (vendor == EAP_VENDOR_IETF ? 1 : 8) +
118
 
                payload_len;
119
 
        hdr = os_malloc(*len);
120
 
        if (hdr) {
121
 
                hdr->code = code;
122
 
                hdr->identifier = identifier;
123
 
                hdr->length = host_to_be16(*len);
124
 
                pos = (u8 *) (hdr + 1);
125
 
                if (vendor == EAP_VENDOR_IETF) {
126
 
                        *pos++ = type;
127
 
                } else {
128
 
                        *pos++ = EAP_TYPE_EXPANDED;
129
 
                        WPA_PUT_BE24(pos, vendor);
130
 
                        pos += 3;
131
 
                        WPA_PUT_BE32(pos, type);
132
 
                        pos += 4;
133
 
                }
134
 
                if (payload)
135
 
                        *payload = pos;
136
 
        }
137
 
 
138
 
        return hdr;
139
 
}