l2_packet_privsep.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant - Layer2 packet handling with privilege separation
00003  * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #include <sys/un.h>
00017 
00018 #include "common.h"
00019 #include "eloop.h"
00020 #include "l2_packet.h"
00021 #include "common/privsep_commands.h"
00022 
00023 
00024 struct l2_packet_data {
00025         int fd; /* UNIX domain socket for privsep access */
00026         void (*rx_callback)(void *ctx, const u8 *src_addr,
00027                             const u8 *buf, size_t len);
00028         void *rx_callback_ctx;
00029         u8 own_addr[ETH_ALEN];
00030         char *own_socket_path;
00031         struct sockaddr_un priv_addr;
00032 };
00033 
00034 
00035 static int wpa_priv_cmd(struct l2_packet_data *l2, int cmd,
00036                         const void *data, size_t data_len)
00037 {
00038         struct msghdr msg;
00039         struct iovec io[2];
00040 
00041         io[0].iov_base = &cmd;
00042         io[0].iov_len = sizeof(cmd);
00043         io[1].iov_base = (u8 *) data;
00044         io[1].iov_len = data_len;
00045 
00046         os_memset(&msg, 0, sizeof(msg));
00047         msg.msg_iov = io;
00048         msg.msg_iovlen = data ? 2 : 1;
00049         msg.msg_name = &l2->priv_addr;
00050         msg.msg_namelen = sizeof(l2->priv_addr);
00051 
00052         if (sendmsg(l2->fd, &msg, 0) < 0) {
00053                 perror("L2: sendmsg(cmd)");
00054                 return -1;
00055         }
00056 
00057         return 0;
00058 }
00059 
00060                              
00061 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
00062 {
00063         os_memcpy(addr, l2->own_addr, ETH_ALEN);
00064         return 0;
00065 }
00066 
00067 
00068 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00069                    const u8 *buf, size_t len)
00070 {
00071         struct msghdr msg;
00072         struct iovec io[4];
00073         int cmd = PRIVSEP_CMD_L2_SEND;
00074 
00075         io[0].iov_base = &cmd;
00076         io[0].iov_len = sizeof(cmd);
00077         io[1].iov_base = &dst_addr;
00078         io[1].iov_len = ETH_ALEN;
00079         io[2].iov_base = &proto;
00080         io[2].iov_len = 2;
00081         io[3].iov_base = (u8 *) buf;
00082         io[3].iov_len = len;
00083 
00084         os_memset(&msg, 0, sizeof(msg));
00085         msg.msg_iov = io;
00086         msg.msg_iovlen = 4;
00087         msg.msg_name = &l2->priv_addr;
00088         msg.msg_namelen = sizeof(l2->priv_addr);
00089 
00090         if (sendmsg(l2->fd, &msg, 0) < 0) {
00091                 perror("L2: sendmsg(packet_send)");
00092                 return -1;
00093         }
00094 
00095         return 0;
00096 }
00097 
00098 
00099 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00100 {
00101         struct l2_packet_data *l2 = eloop_ctx;
00102         u8 buf[2300];
00103         int res;
00104         struct sockaddr_un from;
00105         socklen_t fromlen = sizeof(from);
00106 
00107         os_memset(&from, 0, sizeof(from));
00108         res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
00109                        &fromlen);
00110         if (res < 0) {
00111                 perror("l2_packet_receive - recvfrom");
00112                 return;
00113         }
00114         if (res < ETH_ALEN) {
00115                 wpa_printf(MSG_DEBUG, "L2: Too show packet received");
00116                 return;
00117         }
00118 
00119         if (from.sun_family != AF_UNIX ||
00120             os_strncmp(from.sun_path, l2->priv_addr.sun_path,
00121                        sizeof(from.sun_path)) != 0) {
00122                 wpa_printf(MSG_DEBUG, "L2: Received message from unexpected "
00123                            "source");
00124                 return;
00125         }
00126 
00127         l2->rx_callback(l2->rx_callback_ctx, buf, buf + ETH_ALEN,
00128                         res - ETH_ALEN);
00129 }
00130 
00131 
00132 struct l2_packet_data * l2_packet_init(
00133         const char *ifname, const u8 *own_addr, unsigned short protocol,
00134         void (*rx_callback)(void *ctx, const u8 *src_addr,
00135                             const u8 *buf, size_t len),
00136         void *rx_callback_ctx, int l2_hdr)
00137 {
00138         struct l2_packet_data *l2;
00139         char *own_dir = "/tmp";
00140         char *priv_dir = "/var/run/wpa_priv";
00141         size_t len;
00142         static unsigned int counter = 0;
00143         struct sockaddr_un addr;
00144         fd_set rfds;
00145         struct timeval tv;
00146         int res;
00147         u8 reply[ETH_ALEN + 1];
00148         int reg_cmd[2];
00149 
00150         l2 = os_zalloc(sizeof(struct l2_packet_data));
00151         if (l2 == NULL)
00152                 return NULL;
00153         l2->rx_callback = rx_callback;
00154         l2->rx_callback_ctx = rx_callback_ctx;
00155 
00156         len = os_strlen(own_dir) + 50;
00157         l2->own_socket_path = os_malloc(len);
00158         if (l2->own_socket_path == NULL) {
00159                 os_free(l2);
00160                 return NULL;
00161         }
00162         os_snprintf(l2->own_socket_path, len, "%s/wpa_privsep-l2-%d-%d",
00163                     own_dir, getpid(), counter++);
00164 
00165         l2->priv_addr.sun_family = AF_UNIX;
00166         os_snprintf(l2->priv_addr.sun_path, sizeof(l2->priv_addr.sun_path),
00167                     "%s/%s", priv_dir, ifname);
00168 
00169         l2->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
00170         if (l2->fd < 0) {
00171                 perror("socket(PF_UNIX)");
00172                 os_free(l2->own_socket_path);
00173                 l2->own_socket_path = NULL;
00174                 os_free(l2);
00175                 return NULL;
00176         }
00177 
00178         os_memset(&addr, 0, sizeof(addr));
00179         addr.sun_family = AF_UNIX;
00180         os_strlcpy(addr.sun_path, l2->own_socket_path, sizeof(addr.sun_path));
00181         if (bind(l2->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
00182                 perror("bind(PF_UNIX)");
00183                 goto fail;
00184         }
00185 
00186         reg_cmd[0] = protocol;
00187         reg_cmd[1] = l2_hdr;
00188         if (wpa_priv_cmd(l2, PRIVSEP_CMD_L2_REGISTER, reg_cmd, sizeof(reg_cmd))
00189             < 0) {
00190                 wpa_printf(MSG_ERROR, "L2: Failed to register with wpa_priv");
00191                 goto fail;
00192         }
00193 
00194         FD_ZERO(&rfds);
00195         FD_SET(l2->fd, &rfds);
00196         tv.tv_sec = 5;
00197         tv.tv_usec = 0;
00198         res = select(l2->fd + 1, &rfds, NULL, NULL, &tv);
00199         if (res < 0 && errno != EINTR) {
00200                 perror("select");
00201                 goto fail;
00202         }
00203 
00204         if (FD_ISSET(l2->fd, &rfds)) {
00205                 res = recv(l2->fd, reply, sizeof(reply), 0);
00206                 if (res < 0) {
00207                         perror("recv");
00208                         goto fail;
00209                 }
00210         } else {
00211                 wpa_printf(MSG_DEBUG, "L2: Timeout while waiting for "
00212                            "registration reply");
00213                 goto fail;
00214         }
00215 
00216         if (res != ETH_ALEN) {
00217                 wpa_printf(MSG_DEBUG, "L2: Unexpected registration reply "
00218                            "(len=%d)", res);
00219         }
00220         os_memcpy(l2->own_addr, reply, ETH_ALEN);
00221 
00222         eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
00223 
00224         return l2;
00225 
00226 fail:
00227         close(l2->fd);
00228         l2->fd = -1;
00229         unlink(l2->own_socket_path);
00230         os_free(l2->own_socket_path);
00231         l2->own_socket_path = NULL;
00232         os_free(l2);
00233         return NULL;
00234 }
00235 
00236 
00237 void l2_packet_deinit(struct l2_packet_data *l2)
00238 {
00239         if (l2 == NULL)
00240                 return;
00241 
00242         if (l2->fd >= 0) {
00243                 wpa_priv_cmd(l2, PRIVSEP_CMD_L2_UNREGISTER, NULL, 0);
00244                 eloop_unregister_read_sock(l2->fd);
00245                 close(l2->fd);
00246         }
00247 
00248         if (l2->own_socket_path) {
00249                 unlink(l2->own_socket_path);
00250                 os_free(l2->own_socket_path);
00251         }
00252                 
00253         os_free(l2);
00254 }
00255 
00256 
00257 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
00258 {
00259         /* TODO */
00260         return -1;
00261 }
00262 
00263 
00264 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
00265 {
00266         wpa_priv_cmd(l2, PRIVSEP_CMD_L2_NOTIFY_AUTH_START, NULL, 0);
00267 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:38