~ubuntu-branches/ubuntu/vivid/wpasupplicant/vivid

« back to all changes in this revision

Viewing changes to src/l2_packet/l2_packet_linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2008-03-12 20:03:04 UTC
  • mfrom: (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080312200304-4331y9wj46pdd34z
Tags: 0.6.3-1
* New upstream release.
* Drop patches applied upstream:
  - debian/patches/30_wpa_gui_qt4_eventhistoryui_rework.patch
  - debian/patches/31_wpa_gui_qt4_eventhistory_always_scrollbar.patch
  - debian/patches/32_wpa_gui_qt4_eventhistory_scroll_with_events.patch
  - debian/patches/40_dbus_ssid_data.patch
* Tidy up the clean target of debian/rules. Now that the madwifi headers are
  handled differently we no longer need to do any cleanup.
* Fix formatting error in debian/ifupdown/wpa_action.8 to make lintian
  quieter.
* Add patch to fix formatting errors in manpages build from sgml source. Use
  <emphasis> tags to hightlight keywords instead of surrounding them in
  strong quotes.
  - debian/patches/41_manpage_format_fixes.patch
* wpasupplicant binary package no longer suggests pcscd, guessnet, iproute
  or wireless-tools, nor does it recommend dhcp3-client. These are not
  needed.
* Add debian/patches/10_silence_siocsiwauth_icotl_failure.patch to disable
  ioctl failure messages that occur under normal conditions.
* Cherry pick two upstream git commits concerning the dbus interface:
  - debian/patches/11_avoid_dbus_version_namespace.patch
  - debian/patches/12_fix_potential_use_after_free.patch
* Add debian/patches/42_manpage_explain_available_drivers.patch to explain
  that not all of the driver backends are available in the provided
  wpa_supplicant binary, and that the canonical list of supported driver
  backends can be retrieved from the wpa_supplicant -h (help) output.
  (Closes: #466910)
* Add debian/patches/20_wpa_gui_qt4_disable_link_prl.patch to remove
  link_prl CONFIG compile flag added by qmake-qt4 >= 4.3.4-2 to avoid excess
  linking.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * WPA Supplicant - Layer2 packet handling with Linux packet sockets
 
3
 * Copyright (c) 2003-2005, 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
#include <sys/ioctl.h>
 
17
#include <netpacket/packet.h>
 
18
#include <net/if.h>
 
19
 
 
20
#include "common.h"
 
21
#include "eloop.h"
 
22
#include "l2_packet.h"
 
23
 
 
24
 
 
25
struct l2_packet_data {
 
26
        int fd; /* packet socket for EAPOL frames */
 
27
        char ifname[IFNAMSIZ + 1];
 
28
        int ifindex;
 
29
        u8 own_addr[ETH_ALEN];
 
30
        void (*rx_callback)(void *ctx, const u8 *src_addr,
 
31
                            const u8 *buf, size_t len);
 
32
        void *rx_callback_ctx;
 
33
        int l2_hdr; /* whether to include layer 2 (Ethernet) header data
 
34
                     * buffers */
 
35
};
 
36
 
 
37
 
 
38
int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
 
39
{
 
40
        os_memcpy(addr, l2->own_addr, ETH_ALEN);
 
41
        return 0;
 
42
}
 
43
 
 
44
 
 
45
int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
 
46
                   const u8 *buf, size_t len)
 
47
{
 
48
        int ret;
 
49
        if (l2 == NULL)
 
50
                return -1;
 
51
        if (l2->l2_hdr) {
 
52
                ret = send(l2->fd, buf, len, 0);
 
53
                if (ret < 0)
 
54
                        perror("l2_packet_send - send");
 
55
        } else {
 
56
                struct sockaddr_ll ll;
 
57
                os_memset(&ll, 0, sizeof(ll));
 
58
                ll.sll_family = AF_PACKET;
 
59
                ll.sll_ifindex = l2->ifindex;
 
60
                ll.sll_protocol = htons(proto);
 
61
                ll.sll_halen = ETH_ALEN;
 
62
                os_memcpy(ll.sll_addr, dst_addr, ETH_ALEN);
 
63
                ret = sendto(l2->fd, buf, len, 0, (struct sockaddr *) &ll,
 
64
                             sizeof(ll));
 
65
                if (ret < 0)
 
66
                        perror("l2_packet_send - sendto");
 
67
        }
 
68
        return ret;
 
69
}
 
70
 
 
71
 
 
72
static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
 
73
{
 
74
        struct l2_packet_data *l2 = eloop_ctx;
 
75
        u8 buf[2300];
 
76
        int res;
 
77
        struct sockaddr_ll ll;
 
78
        socklen_t fromlen;
 
79
 
 
80
        os_memset(&ll, 0, sizeof(ll));
 
81
        fromlen = sizeof(ll);
 
82
        res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &ll,
 
83
                       &fromlen);
 
84
        if (res < 0) {
 
85
                perror("l2_packet_receive - recvfrom");
 
86
                return;
 
87
        }
 
88
 
 
89
        l2->rx_callback(l2->rx_callback_ctx, ll.sll_addr, buf, res);
 
90
}
 
91
 
 
92
 
 
93
struct l2_packet_data * l2_packet_init(
 
94
        const char *ifname, const u8 *own_addr, unsigned short protocol,
 
95
        void (*rx_callback)(void *ctx, const u8 *src_addr,
 
96
                            const u8 *buf, size_t len),
 
97
        void *rx_callback_ctx, int l2_hdr)
 
98
{
 
99
        struct l2_packet_data *l2;
 
100
        struct ifreq ifr;
 
101
        struct sockaddr_ll ll;
 
102
 
 
103
        l2 = os_zalloc(sizeof(struct l2_packet_data));
 
104
        if (l2 == NULL)
 
105
                return NULL;
 
106
        os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
 
107
        l2->rx_callback = rx_callback;
 
108
        l2->rx_callback_ctx = rx_callback_ctx;
 
109
        l2->l2_hdr = l2_hdr;
 
110
 
 
111
        l2->fd = socket(PF_PACKET, l2_hdr ? SOCK_RAW : SOCK_DGRAM,
 
112
                        htons(protocol));
 
113
        if (l2->fd < 0) {
 
114
                perror("socket(PF_PACKET)");
 
115
                os_free(l2);
 
116
                return NULL;
 
117
        }
 
118
        os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
 
119
        if (ioctl(l2->fd, SIOCGIFINDEX, &ifr) < 0) {
 
120
                perror("ioctl[SIOCGIFINDEX]");
 
121
                close(l2->fd);
 
122
                os_free(l2);
 
123
                return NULL;
 
124
        }
 
125
        l2->ifindex = ifr.ifr_ifindex;
 
126
 
 
127
        os_memset(&ll, 0, sizeof(ll));
 
128
        ll.sll_family = PF_PACKET;
 
129
        ll.sll_ifindex = ifr.ifr_ifindex;
 
130
        ll.sll_protocol = htons(protocol);
 
131
        if (bind(l2->fd, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
 
132
                perror("bind[PF_PACKET]");
 
133
                close(l2->fd);
 
134
                os_free(l2);
 
135
                return NULL;
 
136
        }
 
137
 
 
138
        if (ioctl(l2->fd, SIOCGIFHWADDR, &ifr) < 0) {
 
139
                perror("ioctl[SIOCGIFHWADDR]");
 
140
                close(l2->fd);
 
141
                os_free(l2);
 
142
                return NULL;
 
143
        }
 
144
        os_memcpy(l2->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
 
145
 
 
146
        eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
 
147
 
 
148
        return l2;
 
149
}
 
150
 
 
151
 
 
152
void l2_packet_deinit(struct l2_packet_data *l2)
 
153
{
 
154
        if (l2 == NULL)
 
155
                return;
 
156
 
 
157
        if (l2->fd >= 0) {
 
158
                eloop_unregister_read_sock(l2->fd);
 
159
                close(l2->fd);
 
160
        }
 
161
                
 
162
        os_free(l2);
 
163
}
 
164
 
 
165
 
 
166
int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
 
167
{
 
168
        int s;
 
169
        struct ifreq ifr;
 
170
        struct sockaddr_in *saddr;
 
171
        size_t res;
 
172
 
 
173
        s = socket(PF_INET, SOCK_DGRAM, 0);
 
174
        if (s < 0) {
 
175
                perror("socket");
 
176
                return -1;
 
177
        }
 
178
        os_memset(&ifr, 0, sizeof(ifr));
 
179
        os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
 
180
        if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
 
181
                if (errno != EADDRNOTAVAIL)
 
182
                        perror("ioctl[SIOCGIFADDR]");
 
183
                close(s);
 
184
                return -1;
 
185
        }
 
186
        close(s);
 
187
        saddr = (struct sockaddr_in *) &ifr.ifr_addr;
 
188
        if (saddr->sin_family != AF_INET)
 
189
                return -1;
 
190
        res = os_strlcpy(buf, inet_ntoa(saddr->sin_addr), len);
 
191
        if (res >= len)
 
192
                return -1;
 
193
        return 0;
 
194
}
 
195
 
 
196
 
 
197
void l2_packet_notify_auth_start(struct l2_packet_data *l2)
 
198
{
 
199
}