1c39934eaSBrian Somers /*- 2c39934eaSBrian Somers * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3c39934eaSBrian Somers * All rights reserved. 4c39934eaSBrian Somers * 5c39934eaSBrian Somers * Redistribution and use in source and binary forms, with or without 6c39934eaSBrian Somers * modification, are permitted provided that the following conditions 7c39934eaSBrian Somers * are met: 8c39934eaSBrian Somers * 1. Redistributions of source code must retain the above copyright 9c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer. 10c39934eaSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 11c39934eaSBrian Somers * notice, this list of conditions and the following disclaimer in the 12c39934eaSBrian Somers * documentation and/or other materials provided with the distribution. 13c39934eaSBrian Somers * 14c39934eaSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15c39934eaSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c39934eaSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c39934eaSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18c39934eaSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c39934eaSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c39934eaSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c39934eaSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c39934eaSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c39934eaSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c39934eaSBrian Somers * SUCH DAMAGE. 25c39934eaSBrian Somers * 267884358fSBrian Somers * $Id: tun.c,v 1.12 1999/04/26 08:54:25 brian Exp $ 27c7d4711fSBrian Somers */ 28c7d4711fSBrian Somers 29972a1bcfSBrian Somers #include <sys/param.h> 3010a9be1eSBrian Somers #include <sys/socket.h> /* For IFF_ defines */ 3110a9be1eSBrian Somers #include <net/if.h> /* For IFF_ defines */ 321ae349f5Scvs2svn #include <netinet/in.h> 3344cae95dSBrian Somers #include <net/if_types.h> 346a6b4bbbSBrian Somers #include <net/if_tun.h> 35eaa4df37SBrian Somers #include <netinet/in_systm.h> 36eaa4df37SBrian Somers #include <netinet/ip.h> 371fa665f5SBrian Somers #include <sys/un.h> 386a6b4bbbSBrian Somers 39119386a3SBrian Somers #include <errno.h> 406a6b4bbbSBrian Somers #include <string.h> 416a6b4bbbSBrian Somers #include <sys/ioctl.h> 427884358fSBrian Somers #ifdef __NetBSD__ 437884358fSBrian Somers #include <stdio.h> 447884358fSBrian Somers #include <unistd.h> 457884358fSBrian Somers #endif 466a6b4bbbSBrian Somers 47b6e82f33SBrian Somers #include "mbuf.h" 48b6e82f33SBrian Somers #include "log.h" 4963258dccSBrian Somers #include "timer.h" 50879ed6faSBrian Somers #include "lqr.h" 516a6b4bbbSBrian Somers #include "hdlc.h" 526a6b4bbbSBrian Somers #include "defs.h" 536d666775SBrian Somers #include "fsm.h" 545828db6dSBrian Somers #include "throughput.h" 555828db6dSBrian Somers #include "iplist.h" 56eaa4df37SBrian Somers #include "slcompress.h" 575828db6dSBrian Somers #include "ipcp.h" 585ca5389aSBrian Somers #include "filter.h" 592f786681SBrian Somers #include "descriptor.h" 603b0f8d2eSBrian Somers #include "lcp.h" 613b0f8d2eSBrian Somers #include "ccp.h" 623b0f8d2eSBrian Somers #include "link.h" 633b0f8d2eSBrian Somers #include "mp.h" 64972a1bcfSBrian Somers #ifndef NORADIUS 65972a1bcfSBrian Somers #include "radius.h" 66972a1bcfSBrian Somers #endif 677a6f8720SBrian Somers #include "bundle.h" 686a6b4bbbSBrian Somers #include "tun.h" 696a6b4bbbSBrian Somers 706a6b4bbbSBrian Somers void 71faefde08SBrian Somers tun_configure(struct bundle *bundle, int mtu) 726a6b4bbbSBrian Somers { 737884358fSBrian Somers #ifdef __NetBSD__ 747884358fSBrian Somers struct ifreq ifr; 757884358fSBrian Somers int s; 767884358fSBrian Somers 777884358fSBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 787884358fSBrian Somers 797884358fSBrian Somers if (s < 0) { 807884358fSBrian Somers log_Printf(LogERROR, "tun_configure: socket(): %s\n", strerror(errno)); 817884358fSBrian Somers return; 827884358fSBrian Somers } 837884358fSBrian Somers 847884358fSBrian Somers sprintf(ifr.ifr_name, "tun%d", bundle->unit); 857884358fSBrian Somers ifr.ifr_mtu = mtu; 867884358fSBrian Somers if (ioctl(s, SIOCSIFMTU, &ifr) < 0) 877884358fSBrian Somers log_Printf(LogERROR, "tun_configure: ioctl(SIOCSIFMTU): %s\n", 887884358fSBrian Somers strerror(errno)); 897884358fSBrian Somers 907884358fSBrian Somers close(s); 917884358fSBrian Somers #else 926a6b4bbbSBrian Somers struct tuninfo info; 936a6b4bbbSBrian Somers 948fa6ebe4SBrian Somers memset(&info, '\0', sizeof info); 9544cae95dSBrian Somers info.type = IFT_PPP; 96972a1bcfSBrian Somers #ifndef NORADIUS 97972a1bcfSBrian Somers if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) { 98972a1bcfSBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 99972a1bcfSBrian Somers bundle->radius.mtu); 100972a1bcfSBrian Somers info.mtu = bundle->radius.mtu; 101972a1bcfSBrian Somers } else 102972a1bcfSBrian Somers #endif 1036a6b4bbbSBrian Somers info.mtu = mtu; 104972a1bcfSBrian Somers 1058fa6ebe4SBrian Somers info.baudrate = bundle->ifSpeed; 1066a6b4bbbSBrian Somers #ifdef __OpenBSD__ 1076a6b4bbbSBrian Somers info.flags = IFF_UP|IFF_POINTOPOINT; 1086a6b4bbbSBrian Somers #endif 109faefde08SBrian Somers if (ioctl(bundle->dev.fd, TUNSIFINFO, &info) < 0) 110dd7e2610SBrian Somers log_Printf(LogERROR, "tun_configure: ioctl(TUNSIFINFO): %s\n", 1116a6b4bbbSBrian Somers strerror(errno)); 1127884358fSBrian Somers #endif 1136a6b4bbbSBrian Somers } 114