1e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher/* 2e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * libipq.c 3e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 4e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * IPQ userspace library. 5e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 6e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * Please note that this library is still developmental, and there may 7e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * be some API changes. 8e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 9ef798b9b7e005209deb457d1ffccab3d2bcdba96James Morris * Author: James Morris <jmorris@intercode.com.au> 10ef798b9b7e005209deb457d1ffccab3d2bcdba96James Morris * 11ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris * 07-11-2001 Modified by Fernando Anton to add support for IPv6. 12ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris * 13ef798b9b7e005209deb457d1ffccab3d2bcdba96James Morris * Copyright (c) 2000-2001 Netfilter Core Team 14ef798b9b7e005209deb457d1ffccab3d2bcdba96James Morris * 15e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * This program is free software; you can redistribute it and/or modify 16e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * it under the terms of the GNU General Public License as published by 17e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * the Free Software Foundation; either version 2 of the License, or 18e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * (at your option) any later version. 19e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 20e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * This program is distributed in the hope that it will be useful, 21e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * but WITHOUT ANY WARRANTY; without even the implied warranty of 22e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * GNU General Public License for more details. 24e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 25e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher */ 26e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 27e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher#include <stdlib.h> 28e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher#include <stdio.h> 29e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher#include <string.h> 30e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher#include <unistd.h> 31dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris#include <sys/time.h> 32dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris#include <sys/types.h> 337e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 34e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher#include <libipq/libipq.h> 35e37c2d0b2cca00f4dfa77b52df065aad304da59bJan Engelhardt#include <netinet/in.h> 36e37c2d0b2cca00f4dfa77b52df065aad304da59bJan Engelhardt#include <linux/netfilter.h> 37e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 38e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher/**************************************************************************** 39e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 40e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * Private interface 41e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 42e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ****************************************************************************/ 43e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 44e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherenum { 45e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_NONE = 0, 46e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_IMPL, 47e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_HANDLE, 48e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_SOCKET, 49e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_BIND, 50e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_BUFFER, 51e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_RECV, 52e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_NLEOF, 53e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_ADDRLEN, 54e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_STRUNC, 55e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_RTRUNC, 56e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_NLRECV, 57e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_SEND, 58e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher IPQ_ERR_SUPP, 59dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris IPQ_ERR_RECVBUF, 60ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris IPQ_ERR_TIMEOUT, 61ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris IPQ_ERR_PROTOCOL 62e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher}; 63ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris#define IPQ_MAXERR IPQ_ERR_PROTOCOL 64e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 65e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstruct ipq_errmap_t { 66e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher int errcode; 67e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher char *message; 68e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} ipq_errmap[] = { 69e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_NONE, "Unknown error" }, 70e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_IMPL, "Implementation error" }, 71e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_HANDLE, "Unable to create netlink handle" }, 72e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_SOCKET, "Unable to create netlink socket" }, 73e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_BIND, "Unable to bind netlink socket" }, 74e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_BUFFER, "Unable to allocate buffer" }, 75e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_RECV, "Failed to receive netlink message" }, 76e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_NLEOF, "Received EOF on netlink socket" }, 77e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_ADDRLEN, "Invalid peer address length" }, 78e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_STRUNC, "Sent message truncated" }, 79e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_RTRUNC, "Received message truncated" }, 80e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_NLRECV, "Received error from netlink" }, 81e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_SEND, "Failed to send netlink message" }, 82e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher { IPQ_ERR_SUPP, "Operation not supported" }, 83dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris { IPQ_ERR_RECVBUF, "Receive buffer size invalid" }, 84ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris { IPQ_ERR_TIMEOUT, "Timeout"}, 85ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris { IPQ_ERR_PROTOCOL, "Invalid protocol specified" } 86e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher}; 87e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 88e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic int ipq_errno = IPQ_ERR_NONE; 89e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 90e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_sendto(const struct ipq_handle *h, 91e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher const void *msg, size_t len); 927e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 93e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h, 94dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris unsigned char *buf, size_t len, 95dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris int timeout); 967e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 97e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h, 98e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher const struct msghdr *msg, 99e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned int flags); 1007e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 101e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic char *ipq_strerror(int errcode); 102e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 103e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_sendto(const struct ipq_handle *h, 104e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher const void *msg, size_t len) 105e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 106e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher int status = sendto(h->fd, msg, len, 0, 107e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher (struct sockaddr *)&h->peer, sizeof(h->peer)); 108e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (status < 0) 109e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_SEND; 1107e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell return status; 111e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 112e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 113e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_sendmsg(const struct ipq_handle *h, 114e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher const struct msghdr *msg, 115e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned int flags) 116e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 117e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher int status = sendmsg(h->fd, msg, flags); 118e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (status < 0) 119e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_SEND; 1207e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell return status; 121e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 122e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 123e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic ssize_t ipq_netlink_recvfrom(const struct ipq_handle *h, 124dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris unsigned char *buf, size_t len, 125dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris int timeout) 126e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 127efa8fc2123a2a9fc229ab471edd2b2688ce1da3aHarald Welte unsigned int addrlen; 128efa8fc2123a2a9fc229ab471edd2b2688ce1da3aHarald Welte int status; 129e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct nlmsghdr *nlh; 1307e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 131e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (len < sizeof(struct nlmsgerr)) { 132e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_RECVBUF; 133e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return -1; 134e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 135e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher addrlen = sizeof(h->peer); 136dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris 137dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris if (timeout != 0) { 138dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris int ret; 139dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris struct timeval tv; 140dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris fd_set read_fds; 141dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris 142dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris if (timeout < 0) { 143dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris /* non-block non-timeout */ 144dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris tv.tv_sec = 0; 145dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris tv.tv_usec = 0; 146dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } else { 147dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris tv.tv_sec = timeout / 1000000; 148dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris tv.tv_usec = timeout % 1000000; 149dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } 150dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris 151dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris FD_ZERO(&read_fds); 152dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris FD_SET(h->fd, &read_fds); 153dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris ret = select(h->fd+1, &read_fds, NULL, NULL, &tv); 154dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris if (ret < 0) { 155dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris if (errno == EINTR) { 156dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris return 0; 157dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } else { 158dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris ipq_errno = IPQ_ERR_RECV; 159dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris return -1; 160dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } 161dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } 162dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris if (!FD_ISSET(h->fd, &read_fds)) { 163dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris ipq_errno = IPQ_ERR_TIMEOUT; 164dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris return 0; 165dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } 166dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris } 167e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher status = recvfrom(h->fd, buf, len, 0, 168e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher (struct sockaddr *)&h->peer, &addrlen); 169e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (status < 0) { 170e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_RECV; 171e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return status; 172e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 173e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (addrlen != sizeof(h->peer)) { 174e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_RECV; 175e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return -1; 176e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 177e5105970e87c0c877a6c54c775f83d30f8d7b57cHarald Welte if (h->peer.nl_pid != 0) { 178e5105970e87c0c877a6c54c775f83d30f8d7b57cHarald Welte ipq_errno = IPQ_ERR_RECV; 179e5105970e87c0c877a6c54c775f83d30f8d7b57cHarald Welte return -1; 180e5105970e87c0c877a6c54c775f83d30f8d7b57cHarald Welte } 181e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (status == 0) { 182e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_NLEOF; 183e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return -1; 184e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 185e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nlh = (struct nlmsghdr *)buf; 186e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) { 187e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_RTRUNC; 188e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return -1; 189e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 190e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return status; 191e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 192e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 193e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherstatic char *ipq_strerror(int errcode) 194e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 195e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (errcode < 0 || errcode > IPQ_MAXERR) 196e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher errcode = IPQ_ERR_IMPL; 197e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return ipq_errmap[errcode].message; 198e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 199e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 200e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher/**************************************************************************** 201e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 202e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * Public interface 203e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * 204e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ****************************************************************************/ 205e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 2067e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell/* 207e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * Create and initialise an ipq handle. 208e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher */ 2097ac405297ec38449b30e3b05fd6bf2082fd3d803Jan Engelhardtstruct ipq_handle *ipq_create_handle(uint32_t flags, uint32_t protocol) 210e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 211e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher int status; 212e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct ipq_handle *h; 2137e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 214e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h = (struct ipq_handle *)malloc(sizeof(struct ipq_handle)); 215e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (h == NULL) { 216e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_HANDLE; 217e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return NULL; 218e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 219ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris 220e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(h, 0, sizeof(struct ipq_handle)); 221ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris 22203d99486d8283552705b58dc55b6085dffc38792Jan Engelhardt if (protocol == NFPROTO_IPV4) 223ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_FIREWALL); 22403d99486d8283552705b58dc55b6085dffc38792Jan Engelhardt else if (protocol == NFPROTO_IPV6) 225ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IP6_FW); 226ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris else { 227ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris ipq_errno = IPQ_ERR_PROTOCOL; 228ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris free(h); 229ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris return NULL; 230ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris } 231ffe96c5a701396fd666228034ff694ffdcd1ad10James Morris 232e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (h->fd == -1) { 233e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_SOCKET; 234e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher free(h); 235e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return NULL; 236e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 237e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(&h->local, 0, sizeof(struct sockaddr_nl)); 238e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->local.nl_family = AF_NETLINK; 239e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->local.nl_pid = getpid(); 240e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->local.nl_groups = 0; 241e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local)); 242e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (status == -1) { 243e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_errno = IPQ_ERR_BIND; 244e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher close(h->fd); 245e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher free(h); 246e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return NULL; 247e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 248e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(&h->peer, 0, sizeof(struct sockaddr_nl)); 249e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->peer.nl_family = AF_NETLINK; 250e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->peer.nl_pid = 0; 251e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher h->peer.nl_groups = 0; 252e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return h; 253e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 254e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 255e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher/* 2567e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell * No error condition is checked here at this stage, but it may happen 257e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher * if/when reliable messaging is implemented. 258e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher */ 259e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_destroy_handle(struct ipq_handle *h) 260e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 261e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (h) { 262e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher close(h->fd); 263e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher free(h); 264e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 265e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return 0; 266e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 267e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 268e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_set_mode(const struct ipq_handle *h, 2697ac405297ec38449b30e3b05fd6bf2082fd3d803Jan Engelhardt uint8_t mode, size_t range) 270e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 271e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct { 272e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct nlmsghdr nlh; 273e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_peer_msg_t pm; 274e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } req; 2757e53bf9c2a697abdb6f1385557338423a86612a3Rusty Russell 276e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(&req, 0, sizeof(req)); 277e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req)); 278e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.nlh.nlmsg_flags = NLM_F_REQUEST; 279e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.nlh.nlmsg_type = IPQM_MODE; 280e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.nlh.nlmsg_pid = h->local.nl_pid; 281e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.pm.msg.mode.value = mode; 282e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher req.pm.msg.mode.range = range; 283e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return ipq_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); 284e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 285e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 286dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris/* 287dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris * timeout is in microseconds (1 second is 1000000 (1 million) microseconds) 288dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris * 289dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris */ 290e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherssize_t ipq_read(const struct ipq_handle *h, 291e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned char *buf, size_t len, int timeout) 292e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 293dbd8a263593180b81e448e46adb7f7ce6b51fd67James Morris return ipq_netlink_recvfrom(h, buf, len, timeout); 294e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 295e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 296e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_message_type(const unsigned char *buf) 297e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 298e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return ((struct nlmsghdr*)buf)->nlmsg_type; 299e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 300e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 301e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_get_msgerr(const unsigned char *buf) 302e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 303e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct nlmsghdr *h = (struct nlmsghdr *)buf; 304e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); 305e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return -err->error; 306e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 307e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 308e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucheripq_packet_msg_t *ipq_get_packet(const unsigned char *buf) 309e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 310e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return NLMSG_DATA((struct nlmsghdr *)(buf)); 311e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 312e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 313e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_set_verdict(const struct ipq_handle *h, 314803f33c0e9cb458cf7989f3826e9baba8da19e2fRusty Russell ipq_id_t id, 315e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned int verdict, 316e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher size_t data_len, 317e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned char *buf) 318e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 319e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher unsigned char nvecs; 320e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher size_t tlen; 321e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct nlmsghdr nlh; 322e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher ipq_peer_msg_t pm; 323e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct iovec iov[3]; 324e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher struct msghdr msg; 325e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 326e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(&nlh, 0, sizeof(nlh)); 327e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nlh.nlmsg_flags = NLM_F_REQUEST; 328e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nlh.nlmsg_type = IPQM_VERDICT; 329e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nlh.nlmsg_pid = h->local.nl_pid; 330e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher memset(&pm, 0, sizeof(pm)); 331e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher pm.msg.verdict.value = verdict; 332e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher pm.msg.verdict.id = id; 333e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher pm.msg.verdict.data_len = data_len; 334e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[0].iov_base = &nlh; 335e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[0].iov_len = sizeof(nlh); 336e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[1].iov_base = ± 337e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[1].iov_len = sizeof(pm); 338e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher tlen = sizeof(nlh) + sizeof(pm); 339e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nvecs = 2; 340e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (data_len && buf) { 341e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[2].iov_base = buf; 342e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher iov[2].iov_len = data_len; 343e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher tlen += data_len; 344e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nvecs++; 345e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher } 346e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_name = (void *)&h->peer; 347e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_namelen = sizeof(h->peer); 348e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_iov = iov; 349e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_iovlen = nvecs; 350e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_control = NULL; 351e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_controllen = 0; 352e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher msg.msg_flags = 0; 353e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher nlh.nlmsg_len = tlen; 354e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return ipq_netlink_sendmsg(h, &msg, 0); 355e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 356e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 357e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher/* Not implemented yet */ 358e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucherint ipq_ctl(const struct ipq_handle *h, int request, ...) 359e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 360e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher return 1; 361e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 362e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher 363b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morrischar *ipq_errstr(void) 364b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morris{ 365b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morris return ipq_strerror(ipq_errno); 366b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morris} 367b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morris 368e6869a8f59d779ff4d5a0984c86d80db7078496Marc Bouchervoid ipq_perror(const char *s) 369e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher{ 370e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (s) 371e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher fputs(s, stderr); 372e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher else 373e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher fputs("ERROR", stderr); 374e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (ipq_errno) 375b1e0b99aff57351419c24c618ccb00ae0fb142f9James Morris fprintf(stderr, ": %s", ipq_errstr()); 376e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher if (errno) 377e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher fprintf(stderr, ": %s", strerror(errno)); 378e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher fputc('\n', stderr); 379e6869a8f59d779ff4d5a0984c86d80db7078496Marc Boucher} 380