| 1 |
/* |
| 2 |
* WPA Supplicant - Layer2 packet handling with privilege separation |
| 3 |
* Copyright (c) 2007, Jouni Malinen <[email protected]> |
| 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/un.h> |
| 11 |
|
| 12 |
#include "common.h" |
| 13 |
#include "eloop.h" |
| 14 |
#include "l2_packet.h" |
| 15 |
#include "common/privsep_commands.h" |
| 16 |
|
| 17 |
|
| 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; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
static int wpa_priv_cmd(struct l2_packet_data *l2, int cmd, |
| 30 |
const void *data, size_t data_len) |
| 31 |
{ |
| 32 |
struct msghdr msg; |
| 33 |
struct iovec io[2]; |
| 34 |
|
| 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; |
| 39 |
|
| 40 |
os_memset(&msg, 0, sizeof(msg)); |
| 41 |
msg.msg_iov = io; |
| 42 |
msg.msg_iovlen = data ? 2 : 1; |
| 43 |
msg.msg_name = &l2->priv_addr; |
| 44 |
msg.msg_namelen = sizeof(l2->priv_addr); |
| 45 |
|
| 46 |
if (sendmsg(l2->fd, &msg, 0) < 0) { |
| 47 |
wpa_printf(MSG_ERROR, "L2: sendmsg(cmd): %s", strerror(errno)); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr) |
| 56 |
{ |
| 57 |
os_memcpy(addr, l2->own_addr, ETH_ALEN); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto, |
| 63 |
const u8 *buf, size_t len) |
| 64 |
{ |
| 65 |
struct msghdr msg; |
| 66 |
struct iovec io[4]; |
| 67 |
int cmd = PRIVSEP_CMD_L2_SEND; |
| 68 |
|
| 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; |
| 74 |
io[2].iov_len = 2; |
| 75 |
io[3].iov_base = (u8 *) buf; |
| 76 |
io[3].iov_len = len; |
| 77 |
|
| 78 |
os_memset(&msg, 0, sizeof(msg)); |
| 79 |
msg.msg_iov = io; |
| 80 |
msg.msg_iovlen = 4; |
| 81 |
msg.msg_name = &l2->priv_addr; |
| 82 |
msg.msg_namelen = sizeof(l2->priv_addr); |
| 83 |
|
| 84 |
if (sendmsg(l2->fd, &msg, 0) < 0) { |
| 85 |
wpa_printf(MSG_ERROR, "L2: sendmsg(packet_send): %s", |
| 86 |
strerror(errno)); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx) |
| 95 |
{ |
| 96 |
struct l2_packet_data *l2 = eloop_ctx; |
| 97 |
u8 buf[2300]; |
| 98 |
int res; |
| 99 |
struct sockaddr_un from; |
| 100 |
socklen_t fromlen = sizeof(from); |
| 101 |
|
| 102 |
os_memset(&from, 0, sizeof(from)); |
| 103 |
res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from, |
| 104 |
&fromlen); |
| 105 |
if (res < 0) { |
| 106 |
wpa_printf(MSG_ERROR, "l2_packet_receive - recvfrom: %s", |
| 107 |
strerror(errno)); |
| 108 |
return; |
| 109 |
} |
| 110 |
if (res < ETH_ALEN) { |
| 111 |
wpa_printf(MSG_DEBUG, "L2: Too show packet received"); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
if (from.sun_family != AF_UNIX || |
| 116 |
os_strncmp(from.sun_path, l2->priv_addr.sun_path, |
| 117 |
sizeof(from.sun_path)) != 0) { |
| 118 |
wpa_printf(MSG_DEBUG, "L2: Received message from unexpected " |
| 119 |
"source"); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
l2->rx_callback(l2->rx_callback_ctx, buf, buf + ETH_ALEN, |
| 124 |
res - ETH_ALEN); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
struct l2_packet_data * l2_packet_init( |
| 129 |
const char *ifname, const u8 *own_addr, unsigned short protocol, |
| 130 |
void (*rx_callback)(void *ctx, const u8 *src_addr, |
| 131 |
const u8 *buf, size_t len), |
| 132 |
void *rx_callback_ctx, int l2_hdr) |
| 133 |
{ |
| 134 |
struct l2_packet_data *l2; |
| 135 |
char *own_dir = "/tmp"; |
| 136 |
char *priv_dir = "/var/run/wpa_priv"; |
| 137 |
size_t len; |
| 138 |
static unsigned int counter = 0; |
| 139 |
struct sockaddr_un addr; |
| 140 |
fd_set rfds; |
| 141 |
struct timeval tv; |
| 142 |
int res; |
| 143 |
u8 reply[ETH_ALEN + 1]; |
| 144 |
int reg_cmd[2]; |
| 145 |
|
| 146 |
l2 = os_zalloc(sizeof(struct l2_packet_data)); |
| 147 |
if (l2 == NULL) |
| 148 |
return NULL; |
| 149 |
l2->rx_callback = rx_callback; |
| 150 |
l2->rx_callback_ctx = rx_callback_ctx; |
| 151 |
|
| 152 |
len = os_strlen(own_dir) + 50; |
| 153 |
l2->own_socket_path = os_malloc(len); |
| 154 |
if (l2->own_socket_path == NULL) { |
| 155 |
os_free(l2); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d", |
| 159 |
own_dir, getpid(), counter++); |
| 160 |
|
| 161 |
l2->priv_addr.sun_family = AF_UNIX; |
| 162 |
os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path), |
| 163 |
"%s/%s", priv_dir, ifname); |
| 164 |
|
| 165 |
l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 166 |
if (l2->fd < 0) { |
| 167 |
wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
| 168 |
os_free(l2->own_socket_path); |
| 169 |
l2->own_socket_path = NULL; |
| 170 |
os_free(l2); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
os_memset(&addr, 0, sizeof(addr)); |
| 175 |
addr.sun_family = AF_UNIX; |
| 176 |
os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path)); |
| 177 |
if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 178 |
wpa_printf(MSG_ERROR, "l2-pkt-privsep: bind(PF_UNIX): %s", |
| 179 |
strerror(errno)); |
| 180 |
goto fail; |
| 181 |
} |
| 182 |
|
| 183 |
reg_cmd[0] = protocol; |
| 184 |
reg_cmd[1] = l2_hdr; |
| 185 |
if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd)) |
| 186 |
< 0) { |
| 187 |
wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv"); |
| 188 |
goto fail; |
| 189 |
} |
| 190 |
|
| 191 |
FD_ZERO(&rfds); |
| 192 |
FD_SET(l2->fd, &rfds); |
| 193 |
tv.tv_sec = 5; |
| 194 |
tv.tv_usec = 0; |
| 195 |
res = select(l2->fd + 1, &rfds, NULL, NULL, &tv); |
| 196 |
if (res < 0 && errno != EINTR) { |
| 197 |
wpa_printf(MSG_ERROR, "select: %s", strerror(errno)); |
| 198 |
goto fail; |
| 199 |
} |
| 200 |
|
| 201 |
if (FD_ISSET(l2->fd, &rfds)) { |
| 202 |
res = recv(l2->fd, reply, sizeof(reply), 0); |
| 203 |
if (res < 0) { |
| 204 |
wpa_printf(MSG_ERROR, "recv: %s", strerror(errno)); |
| 205 |
goto fail; |
| 206 |
} |
| 207 |
} else { |
| 208 |
wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for " |
| 209 |
"registration reply"); |
| 210 |
goto fail; |
| 211 |
} |
| 212 |
|
| 213 |
if (res != ETH_ALEN) { |
| 214 |
wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply " |
| 215 |
"(len=%d)", res); |
| 216 |
} |
| 217 |
os_memcpy(l2->own_addr, reply, ETH_ALEN); |
| 218 |
|
| 219 |
if (rx_callback) |
| 220 |
eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL); |
| 221 |
|
| 222 |
return l2; |
| 223 |
|
| 224 |
fail: |
| 225 |
close(l2->fd); |
| 226 |
l2->fd = -1; |
| 227 |
unlink(l2->own_socket_path); |
| 228 |
os_free(l2->own_socket_path); |
| 229 |
l2->own_socket_path = NULL; |
| 230 |
os_free(l2); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
struct l2_packet_data * l2_packet_init_bridge( |
| 236 |
const char *br_ifname, const char *ifname, const u8 *own_addr, |
| 237 |
unsigned short protocol, |
| 238 |
void (*rx_callback)(void *ctx, const u8 *src_addr, |
| 239 |
const u8 *buf, size_t len), |
| 240 |
void *rx_callback_ctx, int l2_hdr) |
| 241 |
{ |
| 242 |
return l2_packet_init(br_ifname, own_addr, protocol, rx_callback, |
| 243 |
rx_callback_ctx, l2_hdr); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
void l2_packet_deinit(struct l2_packet_data *l2) |
| 248 |
{ |
| 249 |
if (l2 == NULL) |
| 250 |
return; |
| 251 |
|
| 252 |
if (l2->fd >= 0) { |
| 253 |
wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0); |
| 254 |
eloop_unregister_read_sock(l2->fd); |
| 255 |
close(l2->fd); |
| 256 |
} |
| 257 |
|
| 258 |
if (l2->own_socket_path) { |
| 259 |
unlink(l2->own_socket_path); |
| 260 |
os_free(l2->own_socket_path); |
| 261 |
} |
| 262 |
|
| 263 |
os_free(l2); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len) |
| 268 |
{ |
| 269 |
/* TODO */ |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
void l2_packet_notify_auth_start(struct l2_packet_data *l2) |
| 275 |
{ |
| 276 |
wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
int l2_packet_set_packet_filter(struct l2_packet_data *l2, |
| 281 |
enum l2_packet_filter_type type) |
| 282 |
{ |
| 283 |
return -1; |
| 284 |
} |