l2_packet_privsep.c

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

Generated on Sat Nov 21 23:16:52 2009 for hostapd by  doxygen 1.6.1