xref: /freebsd/usr.sbin/ppp/tun.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1c39934eaSBrian Somers /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4c39934eaSBrian Somers  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5c39934eaSBrian Somers  * All rights reserved.
6c39934eaSBrian Somers  *
7c39934eaSBrian Somers  * Redistribution and use in source and binary forms, with or without
8c39934eaSBrian Somers  * modification, are permitted provided that the following conditions
9c39934eaSBrian Somers  * are met:
10c39934eaSBrian Somers  * 1. Redistributions of source code must retain the above copyright
11c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer.
12c39934eaSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
13c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
14c39934eaSBrian Somers  *    documentation and/or other materials provided with the distribution.
15c39934eaSBrian Somers  *
16c39934eaSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c39934eaSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c39934eaSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c39934eaSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c39934eaSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c39934eaSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c39934eaSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c39934eaSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c39934eaSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c39934eaSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c39934eaSBrian Somers  * SUCH DAMAGE.
27c7d4711fSBrian Somers  */
28c7d4711fSBrian Somers 
29972a1bcfSBrian Somers #include <sys/param.h>
30c8b9fb53SBrian Somers 
3110a9be1eSBrian Somers #include <sys/socket.h>		/* For IFF_ defines */
32c8b9fb53SBrian Somers #ifndef __FreeBSD__
3310a9be1eSBrian Somers #include <net/if.h>		/* For IFF_ defines */
34ab2de065SBrian Somers #endif
35c8b9fb53SBrian Somers #include <net/route.h>
361ae349f5Scvs2svn #include <netinet/in.h>
3744cae95dSBrian Somers #include <net/if_types.h>
386a6b4bbbSBrian Somers #include <net/if_tun.h>
39eaa4df37SBrian Somers #include <netinet/in_systm.h>
40eaa4df37SBrian Somers #include <netinet/ip.h>
411fa665f5SBrian Somers #include <sys/un.h>
426a6b4bbbSBrian Somers 
43119386a3SBrian Somers #include <errno.h>
446a6b4bbbSBrian Somers #include <string.h>
45eb6e5e05SBrian Somers #if defined(__OpenBSD__) || defined(__NetBSD__)
466a6b4bbbSBrian Somers #include <sys/ioctl.h>
47eb6e5e05SBrian Somers #endif
482a2877b8SBrian Somers #include <stdio.h>
495d9e6103SBrian Somers #include <termios.h>
507884358fSBrian Somers #ifdef __NetBSD__
517884358fSBrian Somers #include <unistd.h>
527884358fSBrian Somers #endif
536a6b4bbbSBrian Somers 
545d9e6103SBrian Somers #include "layer.h"
55b6e82f33SBrian Somers #include "mbuf.h"
56b6e82f33SBrian Somers #include "log.h"
572a2877b8SBrian Somers #include "id.h"
5863258dccSBrian Somers #include "timer.h"
59879ed6faSBrian Somers #include "lqr.h"
606a6b4bbbSBrian Somers #include "hdlc.h"
616a6b4bbbSBrian Somers #include "defs.h"
626d666775SBrian Somers #include "fsm.h"
635828db6dSBrian Somers #include "throughput.h"
645828db6dSBrian Somers #include "iplist.h"
65eaa4df37SBrian Somers #include "slcompress.h"
6630949fd4SBrian Somers #include "ncpaddr.h"
675828db6dSBrian Somers #include "ipcp.h"
685ca5389aSBrian Somers #include "filter.h"
692f786681SBrian Somers #include "descriptor.h"
703b0f8d2eSBrian Somers #include "lcp.h"
713b0f8d2eSBrian Somers #include "ccp.h"
723b0f8d2eSBrian Somers #include "link.h"
733b0f8d2eSBrian Somers #include "mp.h"
74c8b9fb53SBrian Somers #include "iface.h"
75972a1bcfSBrian Somers #ifndef NORADIUS
76972a1bcfSBrian Somers #include "radius.h"
77972a1bcfSBrian Somers #endif
7830949fd4SBrian Somers #include "ipv6cp.h"
7930949fd4SBrian Somers #include "ncp.h"
807a6f8720SBrian Somers #include "bundle.h"
816a6b4bbbSBrian Somers #include "tun.h"
826a6b4bbbSBrian Somers 
836a6b4bbbSBrian Somers void
tun_configure(struct bundle * bundle)8494d7be52SBrian Somers tun_configure(struct bundle *bundle)
856a6b4bbbSBrian Somers {
867884358fSBrian Somers #ifdef __NetBSD__
877884358fSBrian Somers   struct ifreq ifr;
887884358fSBrian Somers   int s;
897884358fSBrian Somers 
9063c6cac9SBrian Somers   s = socket(PF_INET, SOCK_DGRAM, 0);
917884358fSBrian Somers 
927884358fSBrian Somers   if (s < 0) {
937884358fSBrian Somers     log_Printf(LogERROR, "tun_configure: socket(): %s\n", strerror(errno));
947884358fSBrian Somers     return;
957884358fSBrian Somers   }
967884358fSBrian Somers 
977884358fSBrian Somers   sprintf(ifr.ifr_name, "tun%d", bundle->unit);
98c8b9fb53SBrian Somers   ifr.ifr_mtu = bundle->iface->mtu;
997884358fSBrian Somers   if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
1007884358fSBrian Somers       log_Printf(LogERROR, "tun_configure: ioctl(SIOCSIFMTU): %s\n",
1017884358fSBrian Somers              strerror(errno));
1027884358fSBrian Somers 
1037884358fSBrian Somers   close(s);
1047884358fSBrian Somers #else
1056a6b4bbbSBrian Somers   struct tuninfo info;
1066a6b4bbbSBrian Somers 
1078fa6ebe4SBrian Somers   memset(&info, '\0', sizeof info);
10844cae95dSBrian Somers   info.type = IFT_PPP;
109c8b9fb53SBrian Somers   info.mtu = bundle->iface->mtu;
110972a1bcfSBrian Somers 
111ab2de065SBrian Somers   info.baudrate = bundle->bandwidth;
1126a6b4bbbSBrian Somers #ifdef __OpenBSD__
113ebdcbc67SBrian Somers   info.flags = IFF_UP|IFF_POINTOPOINT|IFF_MULTICAST;
1146a6b4bbbSBrian Somers #endif
1152a2877b8SBrian Somers   if (ID0ioctl(bundle->dev.fd, TUNSIFINFO, &info) < 0)
116dd7e2610SBrian Somers     log_Printf(LogERROR, "tun_configure: ioctl(TUNSIFINFO): %s\n",
1176a6b4bbbSBrian Somers 	      strerror(errno));
1187884358fSBrian Somers #endif
1196a6b4bbbSBrian Somers }
120