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
25struct 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
38int 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
45int 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
72static 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
93struct 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_strncpy(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_strncpy(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
152void 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
166int 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
172	s = socket(PF_INET, SOCK_DGRAM, 0);
173	if (s < 0) {
174		perror("socket");
175		return -1;
176	}
177	os_memset(&ifr, 0, sizeof(ifr));
178	os_strncpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
179	if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
180		if (errno != EADDRNOTAVAIL)
181			perror("ioctl[SIOCGIFADDR]");
182		close(s);
183		return -1;
184	}
185	close(s);
186	saddr = (struct sockaddr_in *) &ifr.ifr_addr;
187	if (saddr->sin_family != AF_INET)
188		return -1;
189	os_snprintf(buf, len, "%s", inet_ntoa(saddr->sin_addr));
190	return 0;
191}
192
193
194void l2_packet_notify_auth_start(struct l2_packet_data *l2)
195{
196}
197