-
Notifications
You must be signed in to change notification settings - Fork 2
/
if_gre.c
1606 lines (1369 loc) · 37.6 KB
/
if_gre.c
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
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* Copyright (c) 2014 Andrey V. Elsukov <[email protected]>
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Heiko W.Rupp <[email protected]>
*
* IPv6-over-GRE contributed by Gert Doering <[email protected]>
*
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*
* $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $
*/
#include <libkern/OSAtomic.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kpi_mbuf.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/bpf.h>
#include <net/kpi_protocol.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/kpi_ipfilter.h>
#include "kernel_build.h"
#include "gre_locks.h"
#include "gre_ip_encap.h"
#include "if_gre.h"
/*
* It is not easy to calculate the right value for a GRE MTU.
* We leave this task to the admin and use the same default that
* other vendors use.
*/
#define GREMTU 1500
#define GRE_MIN_MTU 576
#define GRENAME "gre"
#define GRE_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits(short int) */
#define GRE_CONTROL_NAME "org.gmshake.nke.gre_control"
// FIXME:
#if defined(M_DEVBUF)
#define M_GRE M_DEVBUF
#else
#define M_GRE M_TEMP
#endif
static struct gre_softc * gre_sc_allocate(u_int32_t);
static void gre_sc_free(struct gre_softc *);
static errno_t gre_attach_proto_family(ifnet_t, protocol_family_t);
static void gre_detach_proto_family(ifnet_t, protocol_family_t);
static errno_t gre_add_proto(ifnet_t, protocol_family_t,
const struct ifnet_demux_desc *, u_int32_t);
static errno_t gre_del_proto(ifnet_t, protocol_family_t);
static errno_t gre_ioctl(ifnet_t, unsigned long, void *);
static int gre_set_tunnel(ifnet_t, struct sockaddr *, struct sockaddr *);
static void gre_delete_tunnel(ifnet_t);
static void gre_if_detached(ifnet_t);
static int gre_demux(ifnet_t, mbuf_t, char *, protocol_family_t *);
static errno_t gre_media_input(ifnet_t, protocol_family_t, mbuf_t, char *);
static errno_t gre_pre_output(ifnet_t, protocol_family_t, mbuf_t *,
const struct sockaddr *, void *, char *, char *);
static errno_t gre_output(ifnet_t, mbuf_t);
static int gre_check_nesting(ifnet_t, mbuf_t);
// sysctl
extern struct sysctl_oid sysctl__net_gre_ttl;
extern struct sysctl_oid sysctl__net_gre_hlim;
// vars
static lck_rw_t *gre_lck = NULL; // protect gre_softc_list
static lck_rw_t *gre_ioctl_sx = NULL; // protect gre_ioctl
static LIST_HEAD(, gre_softc) gre_softc_list;
static volatile SInt32 ngre = 0; /* number of interfaces */
// hack: default gre_if_family
static ifnet_family_t gre_if_family = IFNET_FAMILY_TUN;
/*
* This var controls the default upper limitation on nesting of gre tunnels.
* Since, setting a large value to this macro with a careless configuration
* may introduce system crash, we don't allow any nestings by default.
* If you need to configure nested gre tunnels, you can define this macro
* in your kernel configuration file. However, if you do so, please be
* careful to configure the tunnels so that it won't make a loop.
*/
static int max_gre_nesting = 1;
SYSCTL_NODE(_net, OID_AUTO, gre, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Generic Routing Encapsulation");
SYSCTL_INT(_net_gre, OID_AUTO, max_nesting, CTLTYPE_INT | CTLFLAG_RW, &max_gre_nesting, 0, "Max nested tunnels");
inline void
gre_sc_reference(struct gre_softc *sc)
{
OSIncrementAtomic(&sc->sc_refcnt);
}
inline SInt32
gre_sc_release(struct gre_softc *sc)
{
SInt32 oldval = OSDecrementAtomic(&sc->sc_refcnt);
if (oldval == 1) { // now refcnt reach 0, free safely
gre_sc_free(sc);
}
return oldval - 1;
}
/* register INET and INET6 protocol families */
int
gre_proto_register(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
int err;
err = mbuf_tag_id_find(GRE_CONTROL_NAME, &gre_if_family);
if (err != 0) {
printf("%s: mbuf_tag_id_find failed: %d\n", __FUNCTION__, err);
return err;
} else if ((gre_if_family & 0xffff) != gre_if_family) {
printf("%s: gre_if_family overflow: %d\n", __FUNCTION__, gre_if_family);
return ENOENT;
}
#ifdef DEBUG
printf("%s: gre_if_family=%d \n", __FUNCTION__, gre_if_family);
#endif
err = proto_register_plumber(PF_INET, gre_if_family, gre_attach_proto_family, gre_detach_proto_family);
if (err) {
printf("%s: could not register PF_INET protocol family: %d\n", __FUNCTION__, err);
goto fail;
}
err = proto_register_plumber(PF_INET6, gre_if_family, gre_attach_proto_family, gre_detach_proto_family);
if (err) {
proto_unregister_plumber(PF_INET, gre_if_family);
printf("%s: could not register PF_INET6 protocol family: %d\n", __FUNCTION__, err);
goto fail;
}
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return 0;
fail:
return -1;
}
/* unregister INET and INET6 protocol families */
void
gre_proto_unregister(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
proto_unregister_plumber(PF_INET6, gre_if_family);
proto_unregister_plumber(PF_INET, gre_if_family);
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
}
int
gre_if_init(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
if (gre_lck != NULL) {
#ifdef DEBUG
printf("%s: warnning: has inited...\n", __FUNCTION__);
#endif
goto success;
}
gre_lck = lck_rw_alloc_init(gre_lck_grp, gre_lck_attributes);
if (gre_lck == NULL) {
#ifdef DEBUG
printf("%s: faild, not enough mem???\n", __FUNCTION__);
#endif
goto failed;
}
gre_ioctl_sx = lck_rw_alloc_init(gre_lck_grp, gre_lck_attributes);
if (gre_ioctl_sx == NULL) {
#ifdef DEBUG
printf("%s: faild, not enough mem???\n", __FUNCTION__);
#endif
lck_rw_free(gre_lck, gre_lck_grp);
goto failed;
}
LIST_INIT(&gre_softc_list);
sysctl_register_oid(&sysctl__net_gre);
sysctl_register_oid(&sysctl__net_gre_max_nesting);
sysctl_register_oid(&sysctl__net_gre_ttl);
sysctl_register_oid(&sysctl__net_gre_hlim);
success:
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return 0;
failed:
#ifdef DEBUG
printf("%s: fail\n", __FUNCTION__);
#endif
return -1;
}
int
gre_if_dispose(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
if (gre_lck == NULL) {
goto success;
}
/* check if any interface are in use or any resources has not been freed */
int busy = 0;
struct gre_softc *sc, *tp_sc;
lck_rw_lock_exclusive(gre_lck);
LIST_FOREACH(sc, &gre_softc_list, gre_list) {
if (sc->sc_refcnt > 1)
busy++;
}
if (busy > 0) {
lck_rw_unlock_exclusive(gre_lck);
#ifdef DEBUG
printf("%s: resouces busy, please try later\n", __FUNCTION__);
#endif
goto ebusy;
}
// safe to dispose
LIST_FOREACH_SAFE(sc, &gre_softc_list, gre_list, tp_sc) {
LIST_REMOVE(sc, gre_list);
gre_sc_release(sc);
}
lck_rw_unlock_exclusive(gre_lck);
sysctl_unregister_oid(&sysctl__net_gre_hlim);
sysctl_unregister_oid(&sysctl__net_gre_ttl);
sysctl_unregister_oid(&sysctl__net_gre_max_nesting);
sysctl_unregister_oid(&sysctl__net_gre);
lck_rw_free(gre_ioctl_sx, gre_lck_grp);
lck_rw_free(gre_lck, gre_lck_grp);
gre_lck = NULL;
#ifdef DEBUG
printf("%s: current ngre = %d\n", __FUNCTION__, ngre);
#endif
success:
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return 0;
ebusy:
#ifdef DEBUG
printf("%s: fail\n", __FUNCTION__);
#endif
return EBUSY;
}
/*
* gre_if_attach(), attach a new interface
*/
int
gre_if_attach(void)
{
struct gre_softc *sc;
lck_rw_lock_shared(gre_lck);
/* Check for unused gre interface */
LIST_FOREACH(sc, &gre_softc_list, gre_list) {
/* If unused, return, no need to create a new interface */
if (sc->gre_ifp && (ifnet_flags(sc->gre_ifp) & IFF_RUNNING) == 0) {
lck_rw_unlock_shared(gre_lck);
return 0;
}
}
lck_rw_unlock_shared(gre_lck);
// if reach max unit number
SInt32 unit;
if ((unit = OSIncrementAtomic(&ngre)) >= GRE_MAXUNIT) {
OSDecrementAtomic(&ngre);
return ENXIO;
}
// FIXME: find usable ifnet unit
sc = gre_sc_allocate(unit);
if (sc == NULL) {
OSDecrementAtomic(&ngre);
return ENOMEM;
}
gre_sc_reference(sc); // retain
lck_rw_lock_exclusive(gre_lck);
LIST_INSERT_HEAD(&gre_softc_list, sc, gre_list);
lck_rw_unlock_exclusive(gre_lck);
return 0;
}
static errno_t
gre_remove_address(ifnet_t ifp, protocol_family_t af, ifaddr_t addr, socket_t socket)
{
errno_t result = EPROTONOSUPPORT;
/* Attempt a detach */
if (af == PF_INET) {
struct ifreq ifr;
bzero(&ifr, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", ifnet_name(ifp), ifnet_unit(ifp));
result = ifaddr_address(addr, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
if (result == 0) {
result = sock_ioctl(socket, SIOCDIFADDR, &ifr);
if (result != 0)
printf("%s - SIOCDIFADDR failed: %d", __FUNCTION__, result);
} else
printf("%s - ifaddr_address failed: %d", __FUNCTION__, result);
} else if (af == PF_INET6) {
struct in6_ifreq ifr6;
bzero(&ifr6, sizeof(ifr6));
snprintf(ifr6.ifr_name, sizeof(ifr6.ifr_name), "%s%d", ifnet_name(ifp), ifnet_unit(ifp));
result = ifaddr_address(addr, (struct sockaddr*)&ifr6.ifr_addr, sizeof(ifr6.ifr_addr));
if (result == 0) {
result = sock_ioctl(socket, SIOCDIFADDR_IN6, &ifr6);
if (result != 0)
printf("%s - SIOCDIFADDR_IN6 failed: %d", __FUNCTION__, result);
} else
printf("%s - ifaddr_address failed: %d", __FUNCTION__, result);
}
return result;
}
static void
gre_cleanup_family(ifnet_t ifp, protocol_family_t af)
{
#ifdef DEBUG
printf("%s (%s%d, %d) ...\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), af);
#endif
errno_t err = 0;
socket_t sock;
ifaddr_t *addrs;
if (af != PF_INET && af != PF_INET6) {
printf("%s: invalid protocol family %d\n", __FUNCTION__, af);
return;
}
/* Create a socket for removing addresses and detaching the protocol */
err = sock_socket(af, SOCK_DGRAM, 0, NULL, NULL, &sock);
if (err != 0) {
if (err != EAFNOSUPPORT)
printf("%s: failed to create %s socket: %d\n", __FUNCTION__,
af == PF_INET ? "IP" : "IPv6", err);
goto cleanup;
}
err = ifnet_get_address_list_family(ifp, &addrs, af);
if (err != 0) {
if (err != ENXIO)
printf("%s: ifnet_get_address_list_family(%s%d, %p, %s) - failed: %d\n",
__FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), &addrs,
af == PF_INET ? "PF_INET" : "PF_INET6", err);
goto cleanup;
}
for (int i = 0; addrs[i] != NULL; i++) {
gre_remove_address(ifp, af, addrs[i], sock);
}
ifnet_free_address_list(addrs);
cleanup:
if (sock != NULL)
sock_close(sock);
#ifdef DEBUG
printf("%s (%s%d, %d) done\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), af);
#endif
}
static struct gre_softc *
gre_sc_allocate(u_int32_t unit)
{
struct gre_softc * sc;
sc = (struct gre_softc *) _MALLOC(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
if (sc == NULL) // SHOULD NOT HAPPEN since M_WAITOK
return NULL;
sc->gre_lock = lck_rw_alloc_init(gre_sc_lck_grp, gre_sc_lck_attributes);
if (sc->gre_lock == NULL) {
_FREE(sc, M_GRE);
return NULL;
}
sc->detach_mtx = lck_mtx_alloc_init(gre_sc_lck_grp, gre_sc_lck_attributes);
if (sc->detach_mtx == NULL) {
lck_rw_free(sc->gre_lock, gre_sc_lck_grp);
_FREE(sc, M_GRE);
return NULL;
}
struct ifnet_init_params init;
bzero(&init, sizeof(init));
init.name = GRENAME;
init.unit = unit;
init.type = IFT_OTHER;
init.family = gre_if_family;
init.output = gre_output;
init.demux = gre_demux;
init.add_proto = gre_add_proto;
init.del_proto = gre_del_proto;
init.softc = sc;
init.ioctl = gre_ioctl;
init.detach = gre_if_detached;
errno_t err = ifnet_allocate(&init, &sc->gre_ifp);
if (err) {
lck_rw_free(sc->gre_lock, gre_sc_lck_grp);
lck_mtx_free(sc->detach_mtx, gre_sc_lck_grp);
_FREE(sc, M_GRE);
printf("%s: ifnet_allocate() failed - %d\n", __FUNCTION__, err);
return NULL;
}
sc->gre_mtu = GREMTU;
sc->gre_hlen = sizeof(struct greip);
ifnet_t ifp = sc->gre_ifp;
ifnet_set_addrlen(ifp, 0);
ifnet_set_mtu(ifp, sc->gre_mtu - sc->gre_hlen);
ifnet_set_hdrlen(ifp, sc->gre_hlen);
ifnet_set_flags(ifp, IFF_POINTOPOINT | IFF_MULTICAST, 0xffff);
// reset the status in case as the interface may has been recycled
struct ifnet_stats_param param;
bzero(¶m, sizeof(param));
ifnet_set_stat(ifp, ¶m);
ifnet_touch_lastchange(ifp);
err = ifnet_attach(ifp, NULL);
if (err) {
printf("%s: ifnet_attach() failed - %d\n", __FUNCTION__, err);
ifnet_release(ifp);
lck_rw_free(sc->gre_lock, gre_sc_lck_grp);
lck_mtx_free(sc->detach_mtx, gre_sc_lck_grp);
_FREE(sc, M_GRE);
return NULL;
}
bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
#ifdef DEBUG
printf("%s: %s%d sc -> %p, ifp -> %p\n", __FUNCTION__, GRENAME, unit, sc, ifp);
#endif
return sc;
}
static void
gre_sc_free(struct gre_softc *sc)
{
ifnet_t ifp = sc->gre_ifp;
sx_xlock(gre_ioctl_sx);
gre_delete_tunnel(ifp);
sx_xunlock(gre_ioctl_sx);
// mark interface down
ifnet_set_flags(ifp, 0, IFF_UP);
// clean up
gre_cleanup_family(ifp, PF_INET6);
gre_cleanup_family(ifp, PF_INET);
gre_detach_proto_family(ifp, PF_INET6);
gre_detach_proto_family(ifp, PF_INET);
lck_mtx_lock(sc->detach_mtx);
sc->is_detaching = 1;
lck_mtx_unlock(sc->detach_mtx);
#ifdef DEBUG
printf("%s: ifnet_detach(%s%d) ...\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp));
#endif
errno_t err = ifnet_detach(ifp);
#ifdef DEBUG
printf("%s: ifnet_detach(%s%d) ret -> %d\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), err);
#endif
if (err) { // maybe it has already been detached
printf("%s: ifnet_detach %s%d error: %d\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), err);
lck_mtx_lock(sc->detach_mtx);
sc->is_detaching = 0;
lck_mtx_unlock(sc->detach_mtx);
} else { // err == 0
int max_cnt = 10;
do {
lck_mtx_lock(sc->detach_mtx);
if (sc->is_detaching) {
/* interface release is in progress, wait for callback */
#ifdef DEBUG
printf("%s: detaching is in progress...\n", __FUNCTION__);
#endif
struct timespec tv;
bzero(&tv, sizeof(tv));
tv.tv_sec = 0;
tv.tv_nsec = NSEC_PER_SEC / 5; // 200ms
msleep(&sc->is_detaching, sc->detach_mtx, PDROP, "gre_sc_free", &tv); // sc->mtx will be unlocked by msleep
} else {
lck_mtx_unlock(sc->detach_mtx);
break;
}
} while (--max_cnt > 0);
}
if (sc->is_detaching) {
printf("%s: detach %s%d failed, continue, mem leaks\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp));
return;
}
// now it's safe to release
ifnet_release(ifp);
lck_rw_free(sc->gre_lock, gre_sc_lck_grp);
lck_mtx_free(sc->detach_mtx, gre_sc_lck_grp);
_FREE(sc, M_GRE);
OSDecrementAtomic(&ngre);
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
}
/*
* gre_if_detached() is called when ifp detaching is done,
* then it is safe to call ifnet_release()
*/
static void
gre_if_detached(ifnet_t ifp)
{
#ifdef DEBUG
printf("%s: %s%d ...\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp));
#endif
struct gre_softc *sc = ifnet_softc(ifp);
lck_mtx_lock(sc->detach_mtx);
if (sc->is_detaching) {
sc->is_detaching = 0;
wakeup(&sc->is_detaching);
}
lck_mtx_unlock(sc->detach_mtx);
#ifdef DEBUG
printf("%s: detach done\n", __FUNCTION__);
#endif
}
/* attach inet/inet6 to a GRE interface through DLIL */
static errno_t
gre_attach_proto_family(ifnet_t ifp, protocol_family_t protocol_family)
{
#ifdef DEBUG
printf("%s: fam=0x%x\n", __FUNCTION__, protocol_family);
#endif
struct ifnet_attach_proto_param proto;
errno_t err;
bzero(&proto, sizeof(proto));
proto.input = gre_media_input;
proto.pre_output = gre_pre_output;
err = ifnet_attach_protocol(ifp, protocol_family, &proto);
if (err && err != EEXIST)
printf("%s: ifnet_attach_protocol can't attach interface %s%d fam=0x%x\n", \
__FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), protocol_family);
#ifdef DEBUG
if (err == EEXIST)
printf("%s: ifnet_attach_protocol(), %s%d with proto: 0x%x, error = EEXIST\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), protocol_family);
printf("%s: fam=0x%x done\n", __FUNCTION__, protocol_family);
#endif
return err;
}
static void
gre_detach_proto_family(ifnet_t ifp, protocol_family_t protocol)
{
#ifdef DEBUG
printf("%s: fam=0x%x\n", __FUNCTION__, protocol);
#endif
errno_t err = ifnet_detach_protocol(ifp, protocol);
if (err && err != ENOENT && err != ENXIO)
printf("%s: ifnet_detach_protocol() %s%d error = 0x%x\n", \
__FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), err);
#ifdef DEBUG
if (err == ENOENT || err == ENXIO)
printf("%s: ifnet_detach_protocol(), %s%d with proto: 0x%x, error = %s\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), protocol, err == ENOENT ? "ENOENT" : "ENXIO");
printf("%s: fam=0x%x done\n", __FUNCTION__, protocol);
#endif
}
/*
* is called by the stack when a protocol is attached to gre interface.
*/
static errno_t
gre_add_proto(ifnet_t ifp, protocol_family_t protocol,
const struct ifnet_demux_desc *demux_array, u_int32_t demux_count)
{
switch (protocol) {
case AF_INET:
case AF_INET6:
break;
default:
return ENOPROTOOPT; // happen for unknown protocol, or for empty descriptor
}
#ifdef DEBUG
printf("%s: add proto 0x%x for %s%d\n", __FUNCTION__, protocol,
ifnet_name(ifp), ifnet_unit(ifp));
#endif
return 0;
}
/*
* is called by the stack when a protocol is being detached from gre interface.
*/
static errno_t
gre_del_proto(ifnet_t ifp, protocol_family_t protocol)
{
#ifdef DEBUG
printf("%s: del proto %d for %s%d\n", __FUNCTION__, protocol, ifnet_name(ifp), ifnet_unit(ifp));
#endif
switch (protocol) {
case AF_INET:
case AF_INET6:
break;
default:
return EINVAL; // happen for unknown protocol, or for empty descriptor
}
return 0;
}
static void
gre_updatehdr(struct gre_softc *sc)
{
struct grehdr *gh = NULL;
uint32_t *opts;
uint16_t flags;
switch (sc->gre_family) {
case AF_INET:
sc->gre_hlen = sizeof(struct greip);
sc->gre_oip.ip_v = IPPROTO_IPV4;
sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
sc->gre_oip.ip_p = IPPROTO_GRE;
gh = &sc->gre_gihdr->gi_gre;
break;
case AF_INET6:
sc->gre_hlen = sizeof(struct greip6);
sc->gre_oip6.ip6_vfc = IPV6_VERSION;
sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
gh = &sc->gre_gi6hdr->gi6_gre;
break;
default:
return;
}
flags = 0;
opts = gh->gre_opts;
if (sc->gre_options & GRE_ENABLE_CSUM) {
flags |= GRE_FLAGS_CP;
sc->gre_hlen += 2 * sizeof(uint16_t);
*opts++ = 0;
}
if (sc->gre_key != 0) {
flags |= GRE_FLAGS_KP;
sc->gre_hlen += sizeof(uint32_t);
*opts++ = htonl(sc->gre_key);
}
if (sc->gre_options & GRE_ENABLE_SEQ) {
flags |= GRE_FLAGS_SP;
sc->gre_hlen += sizeof(uint32_t);
*opts++ = 0;
} else
sc->gre_oseq = 0;
gh->gre_flags = htons(flags);
//GRE2IFP(sc)->if_mtu = sc->gre_mtu - sc->gre_hlen;
ifnet_set_mtu(sc->gre_ifp, sc->gre_mtu - sc->gre_hlen);
ifnet_set_hdrlen(sc->gre_ifp, sc->gre_hlen);
}
/*
* communicate ioctls from the stack to the driver.
*/
static errno_t
gre_ioctl(ifnet_t ifp, unsigned long cmd, void *data)
{
struct ifreq *ifr = (struct ifreq *)data;
struct sockaddr *src, *dst;
struct gre_softc *sc;
struct sockaddr_in *sin = NULL;
struct sockaddr_in6 *sin6 = NULL;
uint32_t opt;
errno_t error = 0;
#ifdef DEBUG
printf("%s: %s%d cmd -> %lu, data -> %p\n", __FUNCTION__, ifnet_name(ifp), ifnet_unit(ifp), cmd & 0xff, data);
#endif
switch (cmd) {
case SIOCSIFMTU:
if (ifr->ifr_mtu < GRE_MIN_MTU || ifr->ifr_mtu > IF_MAXMTU)
return EINVAL;
break;
case SIOCSIFADDR:
ifnet_set_flags(ifp, IFF_UP, IFF_UP);
case SIOCSIFFLAGS:
case SIOCADDMULTI:
case SIOCDELMULTI:
return 0;
case GRESADDRS:
case GRESADDRD:
case GREGADDRS:
case GREGADDRD:
case GRESPROTO:
case GREGPROTO:
return EOPNOTSUPP;
}
src = dst = NULL;
sx_xlock(gre_ioctl_sx);
sc = ifnet_softc(ifp);
if (sc == NULL) {
error = ENXIO;
goto end;
}
error = 0;
switch (cmd) {
case SIOCSIFMTU:
GRE_WLOCK(sc);
sc->gre_mtu = ifr->ifr_mtu;
gre_updatehdr(sc);
GRE_WUNLOCK(sc);
goto end;
case SIOCSIFPHYADDR:
case SIOCSIFPHYADDR_IN6:
error = EINVAL;
switch (cmd) {
case SIOCSIFPHYADDR:
src = (struct sockaddr *) \
&(((struct in_aliasreq *)data)->ifra_addr);
dst = (struct sockaddr *) \
&(((struct in_aliasreq *)data)->ifra_dstaddr);
break;
case SIOCSIFPHYADDR_IN6:
src = (struct sockaddr *) \
&(((struct in6_aliasreq *)data)->ifra_addr);
dst = (struct sockaddr *) \
&(((struct in6_aliasreq *)data)->ifra_dstaddr);
break;
default:
error = EAFNOSUPPORT;
break;
}
/* sa_family must be equal */
if (src->sa_family != dst->sa_family || \
src->sa_len != dst->sa_len)
goto end;
/* validate sa_len */
switch (src->sa_family) {
case AF_INET:
if (src->sa_len != sizeof(struct sockaddr_in))
goto end;
break;
case AF_INET6:
if (src->sa_len != sizeof(struct sockaddr_in6))
goto end;
break;
default:
error = EAFNOSUPPORT;
goto end;
}
/* check sa_family looks sane for the cmd */
error = EAFNOSUPPORT;
switch (cmd) {
case SIOCSIFPHYADDR:
if (src->sa_family == AF_INET)
break;
goto end;
case SIOCSIFPHYADDR_IN6:
if (src->sa_family == AF_INET6)
break;
goto end;
}
error = EADDRNOTAVAIL;
switch (src->sa_family) {
case AF_INET:
if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
satosin(dst)->sin_addr.s_addr == INADDR_ANY)
goto end;
break;
case AF_INET6:
if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
||
IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
goto end;
/*
* Check validity of the scope zone ID of the
* addresses, and convert it into the kernel
* internal form if necessary.
*/
// error = sa6_embedscope(satosin6(src), 0);
// if (error != 0)
// goto end;
// error = sa6_embedscope(satosin6(dst), 0);
// if (error != 0)
// goto end;
};
error = gre_set_tunnel(ifp, src, dst);
break;
case SIOCDIFPHYADDR:
gre_delete_tunnel(ifp);
break;
case SIOCGIFPSRCADDR:
case SIOCGIFPDSTADDR:
case SIOCGIFPSRCADDR_IN6:
case SIOCGIFPDSTADDR_IN6:
if (sc->gre_family == 0) {
error = EADDRNOTAVAIL;
break;
}
GRE_RLOCK(sc);
switch (cmd) {
case SIOCGIFPSRCADDR:
case SIOCGIFPDSTADDR:
if (sc->gre_family != AF_INET) {
error = EADDRNOTAVAIL;
break;
}
sin = (struct sockaddr_in *)&ifr->ifr_addr;
bzero(sin, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
break;
case SIOCGIFPSRCADDR_IN6:
case SIOCGIFPDSTADDR_IN6:
if (sc->gre_family != AF_INET6) {
error = EADDRNOTAVAIL;
break;
}
sin6 = (struct sockaddr_in6 *)
&(((struct in6_ifreq *)data)->ifr_addr);
bzero(sin6, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
break;
}
if (error == 0) {
switch (cmd) {
case SIOCGIFPSRCADDR:
sin->sin_addr = sc->gre_oip.ip_src;
break;
case SIOCGIFPDSTADDR:
sin->sin_addr = sc->gre_oip.ip_dst;
break;
case SIOCGIFPSRCADDR_IN6:
sin6->sin6_addr = sc->gre_oip6.ip6_src;
break;
case SIOCGIFPDSTADDR_IN6:
sin6->sin6_addr = sc->gre_oip6.ip6_dst;
break;
}
}
GRE_RUNLOCK(sc);
if (error != 0)
break;
switch (cmd) {
case SIOCGIFPSRCADDR:
case SIOCGIFPDSTADDR:
// error = prison_if(curthread->td_ucred,
// (struct sockaddr *)sin);
if (error != 0)
bzero(sin, sizeof(*sin));
break;
case SIOCGIFPSRCADDR_IN6:
case SIOCGIFPDSTADDR_IN6:
// error = prison_if(curthread->td_ucred,
// (struct sockaddr *)sin6);
// if (error == 0)