2 * WPA Supplicant - Layer2 packet handling with privilege separation
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "l2_packet.h"
15 #include "common/privsep_commands.h"
18 struct l2_packet_data {
19 int fd; /* UNIX domain socket for privsep access */
20 void (*rx_callback)(void *ctx, const u8 *src_addr,
21 const u8 *buf, size_t len);
22 void *rx_callback_ctx;
23 u8 own_addr[ETH_ALEN];
24 char *own_socket_path;
25 struct sockaddr_un priv_addr;
29 static int wpa_priv_cmd(struct l2_packet_data *l2, int cmd,
30 const void *data, size_t data_len)
35 io[0].iov_base = &cmd;
36 io[0].iov_len = sizeof(cmd);
37 io[1].iov_base = (u8 *) data;
38 io[1].iov_len = data_len;
40 os_memset(&msg, 0, sizeof(msg));
42 msg.msg_iovlen = data ? 2 : 1;
43 msg.msg_name = &l2->priv_addr;
44 msg.msg_namelen = sizeof(l2->priv_addr);
46 if (sendmsg(l2->fd, &msg, 0) < 0) {
47 perror("L2: sendmsg(cmd)");
55 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
57 os_memcpy(addr, l2->own_addr, ETH_ALEN);
62 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
63 const u8 *buf, size_t len)
67 int cmd = PRIVSEP_CMD_L2_SEND;
69 io[0].iov_base = &cmd;
70 io[0].iov_len = sizeof(cmd);
71 io[1].iov_base = &dst_addr;
72 io[1].iov_len = ETH_ALEN;
73 io[2].iov_base = &proto;
75 io[3].iov_base = (u8 *) buf;
78 os_memset(&msg, 0, sizeof(msg));
81 msg.msg_name = &l2->priv_addr;
82 msg.msg_namelen = sizeof(l2->priv_addr);
84 if (sendmsg(l2->fd, &msg, 0) < 0) {
85 perror("L2: sendmsg(packet_send)");
93 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
95 struct l2_packet_data *l2 = eloop_ctx;
98 struct sockaddr_un from;
99 socklen_t fromlen = sizeof(from);
101 os_memset(&from, 0, sizeof(from));
102 res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
105 perror("l2_packet_receive - recvfrom");
108 if (res < ETH_ALEN) {
109 wpa_printf(MSG_DEBUG, "L2: Too show packet received");
113 if (from.sun_family != AF_UNIX ||
114 os_strncmp(from.sun_path, l2->priv_addr.sun_path,
115 sizeof(from.sun_path)) != 0) {
116 wpa_printf(MSG_DEBUG, "L2: Received message from unexpected "
121 l2->rx_callback(l2->rx_callback_ctx, buf, buf + ETH_ALEN,
126 struct l2_packet_data * l2_packet_init(
127 const char *ifname, const u8 *own_addr, unsigned short protocol,
128 void (*rx_callback)(void *ctx, const u8 *src_addr,
129 const u8 *buf, size_t len),
130 void *rx_callback_ctx, int l2_hdr)
132 struct l2_packet_data *l2;
133 char *own_dir = "/tmp";
134 char *priv_dir = "/var/run/wpa_priv";
136 static unsigned int counter = 0;
137 struct sockaddr_un addr;
141 u8 reply[ETH_ALEN + 1];
144 l2 = os_zalloc(sizeof(struct l2_packet_data));
147 l2->rx_callback = rx_callback;
148 l2->rx_callback_ctx = rx_callback_ctx;
150 len = os_strlen(own_dir) + 50;
151 l2->own_socket_path = os_malloc(len);
152 if (l2->own_socket_path == NULL) {
156 os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d",
157 own_dir, getpid(), counter++);
159 l2->priv_addr.sun_family = AF_UNIX;
160 os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path),
161 "%s/%s", priv_dir, ifname);
163 l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
165 perror("socket(PF_UNIX)");
166 os_free(l2->own_socket_path);
167 l2->own_socket_path = NULL;
172 os_memset(&addr, 0, sizeof(addr));
173 addr.sun_family = AF_UNIX;
174 os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path));
175 if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
176 perror("l2-pkt-privsep: bind(PF_UNIX)");
180 reg_cmd[0] = protocol;
182 if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd))
184 wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv");
189 FD_SET(l2->fd, &rfds);
192 res = select(l2->fd + 1, &rfds, NULL, NULL, &tv);
193 if (res < 0 && errno != EINTR) {
198 if (FD_ISSET(l2->fd, &rfds)) {
199 res = recv(l2->fd, reply, sizeof(reply), 0);
205 wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for "
206 "registration reply");
210 if (res != ETH_ALEN) {
211 wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply "
214 os_memcpy(l2->own_addr, reply, ETH_ALEN);
216 eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
223 unlink(l2->own_socket_path);
224 os_free(l2->own_socket_path);
225 l2->own_socket_path = NULL;
231 void l2_packet_deinit(struct l2_packet_data *l2)
237 wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0);
238 eloop_unregister_read_sock(l2->fd);
242 if (l2->own_socket_path) {
243 unlink(l2->own_socket_path);
244 os_free(l2->own_socket_path);
251 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
258 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
260 wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0);