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 * 265d9e6103SBrian Somers * $Id: tun.c,v 1.13 1999/04/26 08:54:34 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> 425d9e6103SBrian Somers #include <termios.h> 437884358fSBrian Somers #ifdef __NetBSD__ 447884358fSBrian Somers #include <stdio.h> 457884358fSBrian Somers #include <unistd.h> 467884358fSBrian Somers #endif 476a6b4bbbSBrian Somers 485d9e6103SBrian Somers #include "layer.h" 49b6e82f33SBrian Somers #include "mbuf.h" 50b6e82f33SBrian Somers #include "log.h" 5163258dccSBrian Somers #include "timer.h" 52879ed6faSBrian Somers #include "lqr.h" 536a6b4bbbSBrian Somers #include "hdlc.h" 546a6b4bbbSBrian Somers #include "defs.h" 556d666775SBrian Somers #include "fsm.h" 565828db6dSBrian Somers #include "throughput.h" 575828db6dSBrian Somers #include "iplist.h" 58eaa4df37SBrian Somers #include "slcompress.h" 595828db6dSBrian Somers #include "ipcp.h" 605ca5389aSBrian Somers #include "filter.h" 612f786681SBrian Somers #include "descriptor.h" 623b0f8d2eSBrian Somers #include "lcp.h" 633b0f8d2eSBrian Somers #include "ccp.h" 643b0f8d2eSBrian Somers #include "link.h" 653b0f8d2eSBrian Somers #include "mp.h" 66972a1bcfSBrian Somers #ifndef NORADIUS 67972a1bcfSBrian Somers #include "radius.h" 68972a1bcfSBrian Somers #endif 697a6f8720SBrian Somers #include "bundle.h" 706a6b4bbbSBrian Somers #include "tun.h" 716a6b4bbbSBrian Somers 726a6b4bbbSBrian Somers void 73faefde08SBrian Somers tun_configure(struct bundle *bundle, int mtu) 746a6b4bbbSBrian Somers { 757884358fSBrian Somers #ifdef __NetBSD__ 767884358fSBrian Somers struct ifreq ifr; 777884358fSBrian Somers int s; 787884358fSBrian Somers 797884358fSBrian Somers s = socket(AF_INET, SOCK_DGRAM, 0); 807884358fSBrian Somers 817884358fSBrian Somers if (s < 0) { 827884358fSBrian Somers log_Printf(LogERROR, "tun_configure: socket(): %s\n", strerror(errno)); 837884358fSBrian Somers return; 847884358fSBrian Somers } 857884358fSBrian Somers 867884358fSBrian Somers sprintf(ifr.ifr_name, "tun%d", bundle->unit); 877884358fSBrian Somers ifr.ifr_mtu = mtu; 887884358fSBrian Somers if (ioctl(s, SIOCSIFMTU, &ifr) < 0) 897884358fSBrian Somers log_Printf(LogERROR, "tun_configure: ioctl(SIOCSIFMTU): %s\n", 907884358fSBrian Somers strerror(errno)); 917884358fSBrian Somers 927884358fSBrian Somers close(s); 937884358fSBrian Somers #else 946a6b4bbbSBrian Somers struct tuninfo info; 956a6b4bbbSBrian Somers 968fa6ebe4SBrian Somers memset(&info, '\0', sizeof info); 9744cae95dSBrian Somers info.type = IFT_PPP; 98972a1bcfSBrian Somers #ifndef NORADIUS 99972a1bcfSBrian Somers if (bundle->radius.valid && bundle->radius.mtu && bundle->radius.mtu < mtu) { 100972a1bcfSBrian Somers log_Printf(LogLCP, "Reducing MTU to radius value %lu\n", 101972a1bcfSBrian Somers bundle->radius.mtu); 102972a1bcfSBrian Somers info.mtu = bundle->radius.mtu; 103972a1bcfSBrian Somers } else 104972a1bcfSBrian Somers #endif 1056a6b4bbbSBrian Somers info.mtu = mtu; 106972a1bcfSBrian Somers 1078fa6ebe4SBrian Somers info.baudrate = bundle->ifSpeed; 1086a6b4bbbSBrian Somers #ifdef __OpenBSD__ 1096a6b4bbbSBrian Somers info.flags = IFF_UP|IFF_POINTOPOINT; 1106a6b4bbbSBrian Somers #endif 111faefde08SBrian Somers if (ioctl(bundle->dev.fd, TUNSIFINFO, &info) < 0) 112dd7e2610SBrian Somers log_Printf(LogERROR, "tun_configure: ioctl(TUNSIFINFO): %s\n", 1136a6b4bbbSBrian Somers strerror(errno)); 1147884358fSBrian Somers #endif 1156a6b4bbbSBrian Somers } 116