123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /* vim: set expandtab ts=4 sw=4: */
- /*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "interface/Interface.h"
- #include "interface/InterfaceWrapper.h"
- #include "interface/tuntap/TUNInterface.h"
- #include "util/AddrTools.h"
- #include "util/Identity.h"
- #include "util/events/Pipe.h"
- #include "wire/Ethernet.h"
- #include "wire/Error.h"
- #include <errno.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <net/if.h>
- #include <ctype.h>
- #include <net/if_tun.h>
- #include <sys/stropts.h>
- #include <sys/sockio.h>
- #include <fcntl.h>
- #include <net/route.h>
- struct TUNInterface_Illumos_pvt
- {
- struct Interface generic;
- struct Pipe* const pipe;
- Identity
- };
- /**
- * Illumos has no concept of packet info, it only supports IPv4 and IPv6
- * through TUN devices and it detects it by reading the version byte.
- */
- static uint16_t ethertypeForPacketType(uint8_t highByte)
- {
- return ((highByte >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
- }
- static uint8_t receiveMessage(struct Message* message, struct Interface* iface)
- {
- struct TUNInterface_Illumos_pvt* ctx =
- Identity_check((struct TUNInterface_Illumos_pvt*)iface->receiverContext);
- if (message->length < 4) {
- return Error_NONE;
- }
- Message_shift(message, 4);
- ((uint16_t*) message->bytes)[0] = 0;
- ((uint16_t*) message->bytes)[1] = ethertypeForPacketType(message->bytes[4]);
- return Interface_receiveMessage(&ctx->generic, message);
- }
- static uint8_t sendMessage(struct Message* message, struct Interface* iface)
- {
- struct TUNInterface_Illumos_pvt* ctx = Identity_check((struct TUNInterface_Illumos_pvt*)iface);
- Message_shift(message, -4);
- uint16_t ethertype = ((uint16_t*) message->bytes)[-1];
- if (ethertype != Ethernet_TYPE_IP6 && ethertype != Ethernet_TYPE_IP4) {
- Assert_always(!"Unsupported ethertype");
- }
- return Interface_sendMessage(&ctx->pipe->iface, message);
- }
- struct Interface* TUNInterface_new(const char* interfaceName,
- char assignedInterfaceName[TUNInterface_IFNAMSIZ],
- struct EventBase* base,
- struct Log* logger,
- struct Except* eh,
- struct Allocator* alloc)
- {
- // Extract the number eg: 0 from tun0
- int ppa = 0;
- if (interfaceName) {
- for (uint32_t i = 0; i < strlen(interfaceName); i++) {
- if (isdigit(interfaceName[i])) {
- ppa = atoi(interfaceName);
- }
- }
- }
- // Open the descriptor
- int tunFd = open("/dev/tun", O_RDWR);
- // Either the name is specified and we use TUNSETPPA,
- // or it's not specified and we just want a TUNNEWPPA
- if (ppa) {
- ppa = ioctl(tunFd, TUNSETPPA, ppa);
- } else {
- ppa = ioctl(tunFd, TUNNEWPPA, -1);
- }
- int ipFd = open("/dev/ip6", O_RDWR, 0);
- int tunFd2 = open("/dev/tun", O_RDWR, 0);
- if (tunFd < 0 || ipFd < 0 || ppa < 0 || tunFd2 < 0) {
- int err = errno;
- close(tunFd);
- close(ipFd);
- close(tunFd2);
- char* error = NULL;
- if (tunFd < 0) {
- error = "open(\"/dev/tun\")";
- } else if (ipFd < 0) {
- error = "open(\"/dev/ip6\")";
- } else if (ppa < 0) {
- error = "ioctl(TUNNEWPPA)";
- } else if (tunFd2 < 0) {
- error = "open(\"/dev/tun\") (opening for plumbing interface)";
- }
- Except_raise(eh, TUNInterface_new_INTERNAL, error, strerror(err));
- }
- struct lifreq ifr = {
- .lifr_ppa = ppa,
- .lifr_flags = IFF_IPV6
- };
- // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
- // so we'll skip the pretty names and call everything tunX
- int maxNameSize = (LIFNAMSIZ < TUNInterface_IFNAMSIZ) ? LIFNAMSIZ : TUNInterface_IFNAMSIZ;
- snprintf(assignedInterfaceName, maxNameSize, "tun%d", ppa);
- snprintf(ifr.lifr_name, maxNameSize, "tun%d", ppa);
- char* error = NULL;
- if (ioctl(tunFd, I_SRDOPT, RMSGD) < 0) {
- error = "putting tun into message-discard mode";
- } else if (ioctl(tunFd2, I_PUSH, "ip") < 0) {
- // add the ip module
- error = "ioctl(I_PUSH)";
- } else if (ioctl(tunFd2, SIOCSLIFNAME, &ifr) < 0) {
- // set the name of the interface and specify it as ipv6
- error = "ioctl(SIOCSLIFNAME)";
- } else if (ioctl(ipFd, I_LINK, tunFd2) < 0) {
- // link the device to the ipv6 router
- error = "ioctl(I_LINK)";
- }
- if (error) {
- int err = errno;
- close(ipFd);
- close(tunFd2);
- close(tunFd);
- Except_raise(eh, TUNInterface_new_INTERNAL, "%s [%s]", error, strerror(err));
- }
- close(ipFd);
- struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
- struct TUNInterface_Illumos_pvt* ctx =
- Allocator_clone(alloc, (&(struct TUNInterface_Illumos_pvt) {
- .pipe = p
- }));
- Identity_set(ctx);
- InterfaceWrapper_wrap(&p->iface, sendMessage, receiveMessage, &ctx->generic);
- return &ctx->generic;
- }
|