1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
|
/* $OpenBSD: if_tun.c,v 1.227 2020/10/04 06:59:16 anton Exp $ */
/* $NetBSD: if_tun.c,v 1.24 1996/05/07 02:40:48 thorpej Exp $ */
/*
* Copyright (c) 1988, Julian Onions <Julian.Onions@nexor.co.uk>
* Nottingham University 1987.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This driver takes packets off the IP i/f and hands them up to a
* user process to have its wicked way with. This driver has its
* roots in a similar driver written by Phil Cockcroft (formerly) at
* UCL. This driver is based much more on read/write/select mode of
* operation though.
*/
/* #define TUN_DEBUG 9 */
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/sigio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/selinfo.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <sys/device.h>
#include <sys/vnode.h>
#include <sys/signalvar.h>
#include <sys/poll.h>
#include <sys/conf.h>
#include <sys/smr.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/rtable.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include "bpfilter.h"
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#ifdef MPLS
#include <netmpls/mpls.h>
#endif /* MPLS */
#include <net/if_tun.h>
struct tun_softc {
struct arpcom sc_ac; /* ethernet common data */
#define sc_if sc_ac.ac_if
struct selinfo sc_rsel; /* read select */
struct selinfo sc_wsel; /* write select (not used) */
SMR_LIST_ENTRY(tun_softc)
sc_entry; /* all tunnel interfaces */
int sc_unit;
struct sigio_ref sc_sigio; /* async I/O registration */
unsigned int sc_flags; /* misc flags */
#define TUN_DEAD (1 << 16)
dev_t sc_dev;
struct refcnt sc_refs;
unsigned int sc_reading;
};
#ifdef TUN_DEBUG
int tundebug = TUN_DEBUG;
#define TUNDEBUG(a) (tundebug? printf a : 0)
#else
#define TUNDEBUG(a) /* (tundebug? printf a : 0) */
#endif
/* Only these IFF flags are changeable by TUNSIFINFO */
#define TUN_IFF_FLAGS (IFF_UP|IFF_POINTOPOINT|IFF_MULTICAST|IFF_BROADCAST)
void tunattach(int);
int tun_dev_open(dev_t, const struct if_clone *, int, struct proc *);
int tun_dev_close(dev_t, struct proc *);
int tun_dev_ioctl(dev_t, u_long, void *);
int tun_dev_read(dev_t, struct uio *, int);
int tun_dev_write(dev_t, struct uio *, int, int);
int tun_dev_poll(dev_t, int, struct proc *);
int tun_dev_kqfilter(dev_t, struct knote *);
int tun_ioctl(struct ifnet *, u_long, caddr_t);
void tun_input(struct ifnet *, struct mbuf *);
int tun_output(struct ifnet *, struct mbuf *, struct sockaddr *,
struct rtentry *);
int tun_enqueue(struct ifnet *, struct mbuf *);
int tun_clone_create(struct if_clone *, int);
int tap_clone_create(struct if_clone *, int);
int tun_create(struct if_clone *, int, int);
int tun_clone_destroy(struct ifnet *);
void tun_wakeup(struct tun_softc *);
int tun_init(struct tun_softc *);
void tun_start(struct ifnet *);
int filt_tunread(struct knote *, long);
int filt_tunwrite(struct knote *, long);
void filt_tunrdetach(struct knote *);
void filt_tunwdetach(struct knote *);
void tun_link_state(struct tun_softc *, int);
const struct filterops tunread_filtops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = filt_tunrdetach,
.f_event = filt_tunread,
};
const struct filterops tunwrite_filtops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = filt_tunwdetach,
.f_event = filt_tunwrite,
};
SMR_LIST_HEAD(tun_list, tun_softc);
struct if_clone tun_cloner =
IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
struct if_clone tap_cloner =
IF_CLONE_INITIALIZER("tap", tap_clone_create, tun_clone_destroy);
void
tunattach(int n)
{
if_clone_attach(&tun_cloner);
if_clone_attach(&tap_cloner);
}
int
tun_clone_create(struct if_clone *ifc, int unit)
{
return (tun_create(ifc, unit, 0));
}
int
tap_clone_create(struct if_clone *ifc, int unit)
{
return (tun_create(ifc, unit, TUN_LAYER2));
}
struct tun_list tun_devs_list = SMR_LIST_HEAD_INITIALIZER(tun_list);
struct tun_softc *
tun_name_lookup(const char *name)
{
struct tun_softc *sc;
KERNEL_ASSERT_LOCKED();
SMR_LIST_FOREACH_LOCKED(sc, &tun_devs_list, sc_entry) {
if (strcmp(sc->sc_if.if_xname, name) == 0)
return (sc);
}
return (NULL);
}
int
tun_insert(struct tun_softc *sc)
{
int error = 0;
/* check for a race */
if (tun_name_lookup(sc->sc_if.if_xname) != NULL)
error = EEXIST;
else {
/* tun_name_lookup checks for the right lock already */
SMR_LIST_INSERT_HEAD_LOCKED(&tun_devs_list, sc, sc_entry);
}
return (error);
}
int
tun_create(struct if_clone *ifc, int unit, int flags)
{
struct tun_softc *sc;
struct ifnet *ifp;
if (unit > minor(~0U))
return (ENXIO);
KERNEL_ASSERT_LOCKED();
sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
ifp = &sc->sc_if;
snprintf(ifp->if_xname, sizeof(ifp->if_xname),
"%s%d", ifc->ifc_name, unit);
ifp->if_softc = sc;
/* this is enough state for tun_dev_open to work with */
if (tun_insert(sc) != 0)
goto exists;
/* build the interface */
ifp->if_ioctl = tun_ioctl;
ifp->if_enqueue = tun_enqueue;
ifp->if_start = tun_start;
ifp->if_hardmtu = TUNMRU;
ifp->if_link_state = LINK_STATE_DOWN;
if_counters_alloc(ifp);
if ((flags & TUN_LAYER2) == 0) {
ifp->if_input = tun_input;
ifp->if_output = tun_output;
ifp->if_mtu = ETHERMTU;
ifp->if_flags = (IFF_POINTOPOINT|IFF_MULTICAST);
ifp->if_type = IFT_TUNNEL;
ifp->if_hdrlen = sizeof(u_int32_t);
ifp->if_rtrequest = p2p_rtrequest;
if_attach(ifp);
if_alloc_sadl(ifp);
#if NBPFILTER > 0
bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
#endif
} else {
sc->sc_flags |= TUN_LAYER2;
ether_fakeaddr(ifp);
ifp->if_flags =
(IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
if_attach(ifp);
ether_ifattach(ifp);
}
sigio_init(&sc->sc_sigio);
refcnt_init(&sc->sc_refs);
/* tell tun_dev_open we're initialised */
sc->sc_flags |= TUN_INITED|TUN_STAYUP;
wakeup(sc);
return (0);
exists:
free(sc, M_DEVBUF, sizeof(*sc));
return (EEXIST);
}
int
tun_clone_destroy(struct ifnet *ifp)
{
struct tun_softc *sc = ifp->if_softc;
dev_t dev;
int s;
KERNEL_ASSERT_LOCKED();
if (ISSET(sc->sc_flags, TUN_DEAD))
return (ENXIO);
SET(sc->sc_flags, TUN_DEAD);
/* kick userland off the device */
dev = sc->sc_dev;
if (dev) {
struct vnode *vp;
if (vfinddev(dev, VCHR, &vp))
VOP_REVOKE(vp, REVOKEALL);
KASSERT(sc->sc_dev == 0);
}
/* prevent userland from getting to the device again */
SMR_LIST_REMOVE_LOCKED(sc, sc_entry);
smr_barrier();
/* help read() give up */
if (sc->sc_reading)
wakeup(&ifp->if_snd);
/* wait for device entrypoints to finish */
refcnt_finalize(&sc->sc_refs, "tundtor");
s = splhigh();
klist_invalidate(&sc->sc_rsel.si_note);
klist_invalidate(&sc->sc_wsel.si_note);
splx(s);
if (ISSET(sc->sc_flags, TUN_LAYER2))
ether_ifdetach(ifp);
if_detach(ifp);
sigio_free(&sc->sc_sigio);
free(sc, M_DEVBUF, sizeof *sc);
return (0);
}
static struct tun_softc *
tun_get(dev_t dev)
{
struct tun_softc *sc;
smr_read_enter();
SMR_LIST_FOREACH(sc, &tun_devs_list, sc_entry) {
if (sc->sc_dev == dev) {
refcnt_take(&sc->sc_refs);
break;
}
}
smr_read_leave();
return (sc);
}
static inline void
tun_put(struct tun_softc *sc)
{
refcnt_rele_wake(&sc->sc_refs);
}
int
tunopen(dev_t dev, int flag, int mode, struct proc *p)
{
return (tun_dev_open(dev, &tun_cloner, mode, p));
}
int
tapopen(dev_t dev, int flag, int mode, struct proc *p)
{
return (tun_dev_open(dev, &tap_cloner, mode, p));
}
int
tun_dev_open(dev_t dev, const struct if_clone *ifc, int mode, struct proc *p)
{
struct tun_softc *sc;
struct ifnet *ifp;
int error;
u_short stayup = 0;
char name[IFNAMSIZ];
unsigned int rdomain;
snprintf(name, sizeof(name), "%s%u", ifc->ifc_name, minor(dev));
rdomain = rtable_l2(p->p_p->ps_rtableid);
/* let's find or make an interface to work with */
while ((ifp = ifunit(name)) == NULL) {
error = if_clone_create(name, rdomain);
switch (error) {
case 0: /* it's probably ours */
stayup = TUN_STAYUP;
/* FALLTHROUGH */
case EEXIST: /* we may have lost a race with someone else */
break;
default:
return (error);
}
}
sc = ifp->if_softc;
/* wait for it to be fully constructed before we use it */
while (!ISSET(sc->sc_flags, TUN_INITED)) {
error = tsleep_nsec(sc, PCATCH, "tuninit", INFSLP);
if (error != 0) {
/* XXX if_clone_destroy if stayup? */
return (error);
}
}
if (sc->sc_dev != 0) {
/* aww, we lost */
return (EBUSY);
}
/* it's ours now */
sc->sc_dev = dev;
CLR(sc->sc_flags, stayup);
/* automatically mark the interface running on open */
SET(ifp->if_flags, IFF_UP | IFF_RUNNING);
tun_link_state(sc, LINK_STATE_FULL_DUPLEX);
return (0);
}
/*
* tunclose - close the device; if closing the real device, flush pending
* output and unless STAYUP bring down and destroy the interface.
*/
int
tunclose(dev_t dev, int flag, int mode, struct proc *p)
{
return (tun_dev_close(dev, p));
}
int
tapclose(dev_t dev, int flag, int mode, struct proc *p)
{
return (tun_dev_close(dev, p));
}
int
tun_dev_close(dev_t dev, struct proc *p)
{
struct tun_softc *sc;
struct ifnet *ifp;
int error = 0;
char name[IFNAMSIZ];
int destroy = 0;
sc = tun_get(dev);
if (sc == NULL)
return (ENXIO);
ifp = &sc->sc_if;
/*
* junk all pending output
*/
CLR(ifp->if_flags, IFF_UP | IFF_RUNNING);
ifq_purge(&ifp->if_snd);
CLR(sc->sc_flags, TUN_ASYNC);
selwakeup(&sc->sc_rsel);
sigio_free(&sc->sc_sigio);
if (!ISSET(sc->sc_flags, TUN_DEAD)) {
/* we can't hold a reference to sc before we start a dtor */
if (!ISSET(sc->sc_flags, TUN_STAYUP)) {
destroy = 1;
strlcpy(name, ifp->if_xname, sizeof(name));
} else {
CLR(ifp->if_flags, IFF_UP | IFF_RUNNING);
tun_link_state(sc, LINK_STATE_DOWN);
}
}
sc->sc_dev = 0;
tun_put(sc);
if (destroy)
if_clone_destroy(name);
return (error);
}
int
tun_init(struct tun_softc *sc)
{
struct ifnet *ifp = &sc->sc_if;
struct ifaddr *ifa;
TUNDEBUG(("%s: tun_init\n", ifp->if_xname));
ifp->if_flags |= IFF_UP | IFF_RUNNING;
sc->sc_flags &= ~(TUN_IASET|TUN_DSTADDR|TUN_BRDADDR);
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
if (ifa->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *sin;
sin = satosin(ifa->ifa_addr);
if (sin && sin->sin_addr.s_addr)
sc->sc_flags |= TUN_IASET;
if (ifp->if_flags & IFF_POINTOPOINT) {
sin = satosin(ifa->ifa_dstaddr);
if (sin && sin->sin_addr.s_addr)
sc->sc_flags |= TUN_DSTADDR;
} else
sc->sc_flags &= ~TUN_DSTADDR;
if (ifp->if_flags & IFF_BROADCAST) {
sin = satosin(ifa->ifa_broadaddr);
if (sin && sin->sin_addr.s_addr)
sc->sc_flags |= TUN_BRDADDR;
} else
sc->sc_flags &= ~TUN_BRDADDR;
}
#ifdef INET6
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6;
sin6 = satosin6(ifa->ifa_addr);
if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
sc->sc_flags |= TUN_IASET;
if (ifp->if_flags & IFF_POINTOPOINT) {
sin6 = satosin6(ifa->ifa_dstaddr);
if (sin6 &&
!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
sc->sc_flags |= TUN_DSTADDR;
} else
sc->sc_flags &= ~TUN_DSTADDR;
}
#endif /* INET6 */
}
return (0);
}
/*
* Process an ioctl request.
*/
int
tun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct tun_softc *sc = (struct tun_softc *)(ifp->if_softc);
struct ifreq *ifr = (struct ifreq *)data;
int error = 0;
switch (cmd) {
case SIOCSIFADDR:
tun_init(sc);
break;
case SIOCSIFFLAGS:
if (ISSET(ifp->if_flags, IFF_UP))
SET(ifp->if_flags, IFF_RUNNING);
else
CLR(ifp->if_flags, IFF_RUNNING);
break;
case SIOCSIFDSTADDR:
tun_init(sc);
TUNDEBUG(("%s: destination address set\n", ifp->if_xname));
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > TUNMRU)
error = EINVAL;
else
ifp->if_mtu = ifr->ifr_mtu;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
break;
default:
if (sc->sc_flags & TUN_LAYER2)
error = ether_ioctl(ifp, &sc->sc_ac, cmd, data);
else
error = ENOTTY;
}
return (error);
}
/*
* tun_output - queue packets from higher level ready to put out.
*/
int
tun_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
struct rtentry *rt)
{
u_int32_t *af;
if (!ISSET(ifp->if_flags, IFF_RUNNING)) {
m_freem(m0);
return (EHOSTDOWN);
}
M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
if (m0 == NULL)
return (ENOBUFS);
af = mtod(m0, u_int32_t *);
*af = htonl(dst->sa_family);
return (if_enqueue(ifp, m0));
}
int
tun_enqueue(struct ifnet *ifp, struct mbuf *m0)
{
struct tun_softc *sc = ifp->if_softc;
int error;
error = ifq_enqueue(&ifp->if_snd, m0);
if (error != 0)
return (error);
tun_wakeup(sc);
return (0);
}
void
tun_wakeup(struct tun_softc *sc)
{
if (sc->sc_reading)
wakeup(&sc->sc_if.if_snd);
selwakeup(&sc->sc_rsel);
if (sc->sc_flags & TUN_ASYNC)
pgsigio(&sc->sc_sigio, SIGIO, 0);
}
/*
* the cdevsw interface is now pretty minimal.
*/
int
tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
return (tun_dev_ioctl(dev, cmd, data));
}
int
tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
return (tun_dev_ioctl(dev, cmd, data));
}
int
tun_dev_ioctl(dev_t dev, u_long cmd, void *data)
{
struct tun_softc *sc;
struct tuninfo *tunp;
int error = 0;
sc = tun_get(dev);
if (sc == NULL)
return (ENXIO);
switch (cmd) {
case TUNSIFINFO:
tunp = (struct tuninfo *)data;
if (tunp->mtu < ETHERMIN || tunp->mtu > TUNMRU) {
error = EINVAL;
break;
}
if (tunp->type != sc->sc_if.if_type) {
error = EINVAL;
break;
}
sc->sc_if.if_mtu = tunp->mtu;
sc->sc_if.if_flags =
(tunp->flags & TUN_IFF_FLAGS) |
(sc->sc_if.if_flags & ~TUN_IFF_FLAGS);
sc->sc_if.if_baudrate = tunp->baudrate;
break;
case TUNGIFINFO:
tunp = (struct tuninfo *)data;
tunp->mtu = sc->sc_if.if_mtu;
tunp->type = sc->sc_if.if_type;
tunp->flags = sc->sc_if.if_flags;
tunp->baudrate = sc->sc_if.if_baudrate;
break;
#ifdef TUN_DEBUG
case TUNSDEBUG:
tundebug = *(int *)data;
break;
case TUNGDEBUG:
*(int *)data = tundebug;
break;
#endif
case TUNSIFMODE:
switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
case IFF_POINTOPOINT:
case IFF_BROADCAST:
sc->sc_if.if_flags &= ~TUN_IFF_FLAGS;
sc->sc_if.if_flags |= *(int *)data & TUN_IFF_FLAGS;
break;
default:
error = EINVAL;
break;
}
break;
case FIONBIO:
break;
case FIOASYNC:
if (*(int *)data)
sc->sc_flags |= TUN_ASYNC;
else
sc->sc_flags &= ~TUN_ASYNC;
break;
case FIONREAD:
*(int *)data = ifq_hdatalen(&sc->sc_if.if_snd);
break;
case FIOSETOWN:
case TIOCSPGRP:
return (sigio_setown(&sc->sc_sigio, cmd, data));
case FIOGETOWN:
case TIOCGPGRP:
sigio_getown(&sc->sc_sigio, cmd, data);
break;
case SIOCGIFADDR:
if (!(sc->sc_flags & TUN_LAYER2)) {
error = EINVAL;
break;
}
bcopy(sc->sc_ac.ac_enaddr, data,
sizeof(sc->sc_ac.ac_enaddr));
break;
case SIOCSIFADDR:
if (!(sc->sc_flags & TUN_LAYER2)) {
error = EINVAL;
break;
}
bcopy(data, sc->sc_ac.ac_enaddr,
sizeof(sc->sc_ac.ac_enaddr));
break;
default:
error = ENOTTY;
break;
}
tun_put(sc);
return (error);
}
/*
* The cdevsw read interface - reads a packet at a time, or at
* least as much of a packet as can be read.
*/
int
tunread(dev_t dev, struct uio *uio, int ioflag)
{
return (tun_dev_read(dev, uio, ioflag));
}
int
tapread(dev_t dev, struct uio *uio, int ioflag)
{
return (tun_dev_read(dev, uio, ioflag));
}
int
tun_dev_read(dev_t dev, struct uio *uio, int ioflag)
{
struct tun_softc *sc;
struct ifnet *ifp;
struct mbuf *m, *m0;
int error = 0;
sc = tun_get(dev);
if (sc == NULL)
return (ENXIO);
ifp = &sc->sc_if;
error = ifq_deq_sleep(&ifp->if_snd, &m0, ISSET(ioflag, IO_NDELAY),
(PZERO + 1)|PCATCH, "tunread", &sc->sc_reading, &sc->sc_dev);
if (error != 0)
goto put;
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
#endif
m = m0;
while (uio->uio_resid > 0) {
size_t len = ulmin(uio->uio_resid, m->m_len);
if (len > 0) {
error = uiomove(mtod(m, void *), len, uio);
if (error != 0)
break;
}
m = m->m_next;
if (m == NULL)
break;
}
m_freem(m0);
put:
tun_put(sc);
return (error);
}
/*
* the cdevsw write interface - an atomic write is a packet - or else!
*/
int
tunwrite(dev_t dev, struct uio *uio, int ioflag)
{
return (tun_dev_write(dev, uio, ioflag, 0));
}
int
tapwrite(dev_t dev, struct uio *uio, int ioflag)
{
return (tun_dev_write(dev, uio, ioflag, ETHER_ALIGN));
}
int
tun_dev_write(dev_t dev, struct uio *uio, int ioflag, int align)
{
struct tun_softc *sc;
struct ifnet *ifp;
struct mbuf *m0;
int error = 0;
size_t mlen;
sc = tun_get(dev);
if (sc == NULL)
return (ENXIO);
ifp = &sc->sc_if;
if (uio->uio_resid < ifp->if_hdrlen ||
uio->uio_resid > (ifp->if_hdrlen + ifp->if_hardmtu)) {
error = EMSGSIZE;
goto put;
}
align += max_linkhdr;
mlen = align + uio->uio_resid;
m0 = m_gethdr(M_DONTWAIT, MT_DATA);
if (m0 == NULL) {
error = ENOMEM;
goto put;
}
if (mlen > MHLEN) {
m_clget(m0, M_DONTWAIT, mlen);
if (!ISSET(m0->m_flags, M_EXT)) {
error = ENOMEM;
goto drop;
}
}
m_align(m0, mlen);
m0->m_pkthdr.len = m0->m_len = mlen;
m_adj(m0, align);
error = uiomove(mtod(m0, void *), m0->m_len, uio);
if (error != 0)
goto drop;
NET_LOCK();
if_vinput(ifp, m0);
NET_UNLOCK();
tun_put(sc);
return (0);
drop:
m_freem(m0);
put:
tun_put(sc);
return (error);
}
void
tun_input(struct ifnet *ifp, struct mbuf *m0)
{
uint32_t af;
KASSERT(m0->m_len >= sizeof(af));
af = *mtod(m0, uint32_t *);
/* strip the tunnel header */
m_adj(m0, sizeof(af));
switch (ntohl(af)) {
case AF_INET:
ipv4_input(ifp, m0);
break;
#ifdef INET6
case AF_INET6:
ipv6_input(ifp, m0);
break;
#endif
#ifdef MPLS
case AF_MPLS:
mpls_input(ifp, m0);
break;
#endif
default:
m_freem(m0);
break;
}
}
/*
* tunpoll - the poll interface, this is only useful on reads
* really. The write detect always returns true, write never blocks
* anyway, it either accepts the packet or drops it.
*/
int
tunpoll(dev_t dev, int events, struct proc *p)
{
return (tun_dev_poll(dev, events, p));
}
int
tappoll(dev_t dev, int events, struct proc *p)
{
return (tun_dev_poll(dev, events, p));
}
int
tun_dev_poll(dev_t dev, int events, struct proc *p)
{
struct tun_softc *sc;
struct ifnet *ifp;
int revents;
sc = tun_get(dev);
if (sc == NULL)
return (POLLERR);
ifp = &sc->sc_if;
revents = 0;
if (events & (POLLIN | POLLRDNORM)) {
if (!ifq_empty(&ifp->if_snd))
revents |= events & (POLLIN | POLLRDNORM);
else
selrecord(p, &sc->sc_rsel);
}
if (events & (POLLOUT | POLLWRNORM))
revents |= events & (POLLOUT | POLLWRNORM);
tun_put(sc);
return (revents);
}
int
tunkqfilter(dev_t dev, struct knote *kn)
{
return (tun_dev_kqfilter(dev, kn));
}
int
tapkqfilter(dev_t dev, struct knote *kn)
{
return (tun_dev_kqfilter(dev, kn));
}
int
tun_dev_kqfilter(dev_t dev, struct knote *kn)
{
struct tun_softc *sc;
struct ifnet *ifp;
struct klist *klist;
int error = 0;
int s;
sc = tun_get(dev);
if (sc == NULL)
return (ENXIO);
ifp = &sc->sc_if;
switch (kn->kn_filter) {
case EVFILT_READ:
klist = &sc->sc_rsel.si_note;
kn->kn_fop = &tunread_filtops;
break;
case EVFILT_WRITE:
klist = &sc->sc_wsel.si_note;
kn->kn_fop = &tunwrite_filtops;
break;
default:
error = EINVAL;
goto put;
}
kn->kn_hook = (caddr_t)sc; /* XXX give the sc_ref to the hook? */
s = splhigh();
klist_insert(klist, kn);
splx(s);
put:
tun_put(sc);
return (error);
}
void
filt_tunrdetach(struct knote *kn)
{
int s;
struct tun_softc *sc = kn->kn_hook;
s = splhigh();
klist_remove(&sc->sc_rsel.si_note, kn);
splx(s);
}
int
filt_tunread(struct knote *kn, long hint)
{
struct tun_softc *sc = kn->kn_hook;
struct ifnet *ifp = &sc->sc_if;
kn->kn_data = ifq_hdatalen(&ifp->if_snd);
return (kn->kn_data > 0);
}
void
filt_tunwdetach(struct knote *kn)
{
int s;
struct tun_softc *sc = kn->kn_hook;
s = splhigh();
klist_remove(&sc->sc_wsel.si_note, kn);
splx(s);
}
int
filt_tunwrite(struct knote *kn, long hint)
{
struct tun_softc *sc = kn->kn_hook;
struct ifnet *ifp = &sc->sc_if;
kn->kn_data = ifp->if_hdrlen + ifp->if_hardmtu;
return (1);
}
void
tun_start(struct ifnet *ifp)
{
struct tun_softc *sc = ifp->if_softc;
splassert(IPL_NET);
if (ifq_len(&ifp->if_snd))
tun_wakeup(sc);
}
void
tun_link_state(struct tun_softc *sc, int link_state)
{
struct ifnet *ifp = &sc->sc_if;
if (ifp->if_link_state != link_state) {
ifp->if_link_state = link_state;
if_link_state_change(ifp);
}
}
|