tun.c revision 51447
150276Speter/*-
2184989Srafan * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
350276Speter * All rights reserved.
450276Speter *
550276Speter * Redistribution and use in source and binary forms, with or without
650276Speter * modification, are permitted provided that the following conditions
750276Speter * are met:
850276Speter * 1. Redistributions of source code must retain the above copyright
950276Speter *    notice, this list of conditions and the following disclaimer.
1050276Speter * 2. Redistributions in binary form must reproduce the above copyright
1150276Speter *    notice, this list of conditions and the following disclaimer in the
1250276Speter *    documentation and/or other materials provided with the distribution.
1350276Speter *
1450276Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1550276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1650276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1750276Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1850276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1950276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2050276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2150276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2250276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2350276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2450276Speter * SUCH DAMAGE.
2550276Speter *
2650276Speter * $FreeBSD: head/usr.sbin/ppp/tun.c 51447 1999-09-20 07:18:50Z brian $
2750276Speter */
2850276Speter
2950276Speter#include <sys/param.h>
3050276Speter#ifndef __FreeBSD__
3150276Speter#include <sys/socket.h>		/* For IFF_ defines */
32166124Srafan#include <net/if.h>		/* For IFF_ defines */
3350276Speter#endif
3450276Speter#include <netinet/in.h>
3550276Speter#include <net/if_types.h>
3650276Speter#include <net/if_tun.h>
3750276Speter#include <netinet/in_systm.h>
3850276Speter#include <netinet/ip.h>
3950276Speter#include <sys/un.h>
4050276Speter
4150276Speter#include <errno.h>
4250276Speter#include <string.h>
4350276Speter#if defined(__OpenBSD__) || defined(__NetBSD__)
4450276Speter#include <sys/ioctl.h>
4550276Speter#endif
4650276Speter#include <termios.h>
4750276Speter#ifdef __NetBSD__
4850276Speter#include <stdio.h>
4950276Speter#include <unistd.h>
5050276Speter#endif
5150276Speter
5250276Speter#include "layer.h"
5350276Speter#include "mbuf.h"
5450276Speter#include "log.h"
5550276Speter#include "timer.h"
5650276Speter#include "lqr.h"
5750276Speter#include "hdlc.h"
5850276Speter#include "defs.h"
5950276Speter#include "fsm.h"
6050276Speter#include "throughput.h"
6150276Speter#include "iplist.h"
6250276Speter#include "slcompress.h"
6350276Speter#include "ipcp.h"
6450276Speter#include "filter.h"
6550276Speter#include "descriptor.h"
6650276Speter#include "lcp.h"
6750276Speter#include "ccp.h"
6850276Speter#include "link.h"
6950276Speter#include "mp.h"
7050276Speter#ifndef NORADIUS
7150276Speter#include "radius.h"
7250276Speter#endif
7350276Speter#include "bundle.h"
7450276Speter#include "tun.h"
7550276Speter
76174993Srafanvoid
7750276Spetertun_configure(struct bundle *bundle, int mtu)
7850276Speter{
7950276Speter#ifdef __NetBSD__
8050276Speter  struct ifreq ifr;
8150276Speter  int s;
8250276Speter
8350276Speter  s = socket(AF_INET, SOCK_DGRAM, 0);
8450276Speter
8550276Speter  if (s < 0) {
8650276Speter    log_Printf(LogERROR, "tun_configure: socket(): %s\n", strerror(errno));
8750276Speter    return;
8850276Speter  }
8950276Speter
9050276Speter  sprintf(ifr.ifr_name, "tun%d", bundle->unit);
9150276Speter  ifr.ifr_mtu = mtu;
92184989Srafan  if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
93184989Srafan      log_Printf(LogERROR, "tun_configure: ioctl(SIOCSIFMTU): %s\n",
94184989Srafan             strerror(errno));
95184989Srafan
96184989Srafan  close(s);
97184989Srafan#else
9850276Speter  struct tuninfo info;
9950276Speter
10050276Speter  memset(&info, '\0', sizeof info);
10150276Speter  info.type = IFT_PPP;
10250276Speter  info.mtu = mtu;
10397049Speter
10497049Speter  info.baudrate = bundle->bandwidth;
10550276Speter#ifdef __OpenBSD__
10650276Speter  info.flags = IFF_UP|IFF_POINTOPOINT;
10750276Speter#endif
10866963Speter  if (ioctl(bundle->dev.fd, TUNSIFINFO, &info) < 0)
10950276Speter    log_Printf(LogERROR, "tun_configure: ioctl(TUNSIFINFO): %s\n",
110184989Srafan	      strerror(errno));
11150276Speter#endif
112184989Srafan}
113184989Srafan