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