-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.c
1763 lines (1536 loc) · 49.1 KB
/
bridge.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 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
#include "driver-config.h"
#define EXPORT_SYMTAB
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/mm.h>
#include "compat_skbuff.h"
#include <linux/sockios.h>
#include <linux/spinlock.h>
#include "compat_sock.h"
#define __KERNEL_SYSCALLS__
#include <asm/io.h>
#include <linux/proc_fs.h>
#include <linux/file.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/tcp.h>
#include <net/ipv6.h>
#ifdef CONFIG_NET_RADIO
# include <linux/wireless.h>
#endif
#include "vmnetInt.h"
#include "compat_netdevice.h"
#include "vnetInt.h"
#include "smac.h"
#define VNET_BRIDGE_HISTORY 48
/*
* Bytes reserved before start of packet. As Ethernet header has 14 bytes,
* to get aligned IP header we must skip 2 bytes before packet. Not that it
* matters a lot for us, but using 2 is compatible with what newer 2.6.x
* kernels do.
*/
#ifndef NET_IP_ALIGN
#define NET_IP_ALIGN 2
#endif
#if LOGLEVEL >= 4
static struct timeval vnetTime;
#endif
typedef struct VNetBridge VNetBridge;
struct VNetBridge {
struct notifier_block notifier; // for device state changes
char name[VNET_NAME_LEN]; // name of net device (e.g., "eth0")
struct net_device *dev; // device structure for 'name'
struct sock *sk; // socket associated with skb's
struct packet_type pt; // used to add packet handler
Bool enabledPromisc; // track if promisc enabled
Bool warnPromisc; // tracks if warning has been logged
Bool forceSmac; // whether to use smac unconditionally
struct sk_buff *history[VNET_BRIDGE_HISTORY]; // avoid duplicate packets
spinlock_t historyLock; // protects 'history'
VNetPort port; // connection to virtual hub
Bool wirelessAdapter; // connected to wireless adapter?
struct SMACState *smac; // device structure for wireless
VNetEvent_Sender *eventSender; // event sender
};
typedef PacketStatus (* SMACINT SMACFunc)(struct SMACState *, SMACPackets *);
static int VNetBridgeUp(VNetBridge *bridge, Bool rtnlLock);
static void VNetBridgeDown(VNetBridge *bridge, Bool rtnlLock);
static int VNetBridgeNotify(struct notifier_block *this, u_long msg,
void *data);
static int VNetBridgeReceiveFromDev(struct sk_buff *skb,
struct net_device *dev,
struct packet_type *pt,
struct net_device *real_dev);
static void VNetBridgeFree(VNetJack *this);
static void VNetBridgeReceiveFromVNet(VNetJack *this, struct sk_buff *skb);
static Bool VNetBridgeCycleDetect(VNetJack *this, int generation);
static Bool VNetBridgeIsDeviceWireless(struct net_device *dev);
static void VNetBridgePortsChanged(VNetJack *this);
static int VNetBridgeIsBridged(VNetJack *this);
static int VNetBridgeProcRead(char *page, char **start, off_t off,
int count, int *eof, void *data);
static void VNetBridgeComputeHeaderPosIPv6(struct sk_buff *skb);
static PacketStatus VNetCallSMACFunc(struct SMACState *state,
struct sk_buff **skb, void *startOfData,
SMACFunc func, unsigned int len);
/*
*----------------------------------------------------------------------
*
* VNetBridgeStartPromisc --
*
* Set IFF_PROMISC on the peer interface.
*
* Results:
* None.
*
* Side effects:
* The peer interface IFF_PROMISC flag may be changed.
*
*----------------------------------------------------------------------
*/
static void
VNetBridgeStartPromisc(VNetBridge *bridge, // IN:
Bool rtnlLock) // IN: Acquire RTNL lock
{
struct net_device *dev = bridge->dev;
/*
* Disable wireless cards from going into promiscous mode because those
* cards which do support RF monitoring would not be able to function
* correctly i.e. they would not be able to send data packets.
*/
if (rtnlLock) {
rtnl_lock();
}
if (!bridge->enabledPromisc && !bridge->wirelessAdapter) {
dev_set_promiscuity(dev, 1);
bridge->enabledPromisc = TRUE;
bridge->warnPromisc = FALSE;
LOG(0, (KERN_NOTICE "bridge-%s: enabled promiscuous mode\n",
bridge->name));
}
if (rtnlLock) {
rtnl_unlock();
}
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeStopPromisc --
*
* Restore saved IFF_PROMISC on the peer interface.
*
* Results:
* None.
*
* Side effects:
* The peer interface IFF_PROMISC flag may be changed.
*
*----------------------------------------------------------------------
*/
static void
VNetBridgeStopPromisc(VNetBridge *bridge, // IN:
Bool rtnlLock) // IN: Acquire RTNL lock
{
struct net_device *dev = bridge->dev;
if (rtnlLock) {
rtnl_lock();
}
if (bridge->enabledPromisc && !bridge->wirelessAdapter) {
dev_set_promiscuity(dev, -1);
bridge->enabledPromisc = FALSE;
LOG(0, (KERN_NOTICE "bridge-%s: disabled promiscuous mode\n",
bridge->name));
}
if (rtnlLock) {
rtnl_unlock();
}
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeDevCompatible --
*
* Check whether bridge and network device are compatible.
*
* Results:
* Non-zero if device is good enough for bridge. Zero otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static INLINE_SINGLE_CALLER int
VNetBridgeDevCompatible(VNetBridge *bridge, // IN: Bridge
struct net_device *net) // IN: Network device
{
#ifdef VMW_NETDEV_HAS_NET
if (compat_dev_net(net) != &init_net) {
return 0;
}
#endif
return strcmp(net->name, bridge->name) == 0;
}
/*
*----------------------------------------------------------------------
*
* VNetBridge_Create --
*
* Creates a bridge. Allocates struct, allocates internal device,
* initializes port/jack, and creates a proc entry. Finally, creates an
* event sender and register itself with the kernel for device state
* change notifications.
*
* At this time the bridge is not yet plugged into the hub, because this
* will be done by the caller, i.e. the driver. But we need to know the
* hub in order to create an event sender. This allows for enabling
* the notification mechanism, which will instantly start firing, which in
* turn will bring up the bridge (if present), which eventually will
* inject bridge events. Moreover, the bridge will start injecting
* packets, which will be dropped on the floor. All in all, this is not
* that elegant. Alternatively, we could (i) plug into the hub inside of
* this function, which would require adding a few parameters, (ii) split
* the function into a create part and a registration part. Both ways are
* not consistent with how driver.c plugs the ports into the hub.
*
* Results:
* Errno. Also returns an allocated jack to connect to,
* NULL on error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
VNetBridge_Create(const char *devName, // IN: name of device (e.g., "eth0")
uint32 flags, // IN: configuration flags
VNetJack *hubJack, // IN: the future hub
VNetPort **ret) // OUT: port to virtual hub
{
VNetBridge *bridge = NULL;
static unsigned id = 0;
int retval = 0;
*ret = NULL;
/*
* Its an error if device name is empty.
*/
if (devName[0] == '\0') {
retval = -EINVAL;
goto out;
}
/* complain about unknown/unsupported flags */
if (flags & ~VNET_BRFLAG_FORCE_SMAC) {
retval = -EINVAL;
goto out;
}
/*
* Allocate bridge structure
*/
bridge = kmalloc(sizeof *bridge, GFP_USER);
if (bridge == NULL) {
retval = -ENOMEM;
goto out;
}
memset(bridge, 0, sizeof *bridge);
spin_lock_init(&bridge->historyLock);
memcpy(bridge->name, devName, sizeof bridge->name);
NULL_TERMINATE_STRING(bridge->name);
/*
* Initialize jack.
*/
bridge->port.id = id++;
bridge->port.next = NULL;
bridge->port.jack.peer = NULL;
bridge->port.jack.numPorts = 1;
VNetSnprintf(bridge->port.jack.name, sizeof bridge->port.jack.name,
"bridge%u", bridge->port.id);
bridge->port.jack.private = bridge;
bridge->port.jack.index = 0;
bridge->port.jack.procEntry = NULL;
bridge->port.jack.free = VNetBridgeFree;
bridge->port.jack.rcv = VNetBridgeReceiveFromVNet;
bridge->port.jack.cycleDetect = VNetBridgeCycleDetect;
bridge->port.jack.portsChanged = VNetBridgePortsChanged;
bridge->port.jack.isBridged = VNetBridgeIsBridged;
/*
* Make proc entry for this jack.
*/
retval = VNetProc_MakeEntry(bridge->port.jack.name, S_IFREG,
&bridge->port.jack.procEntry);
if (retval) {
if (retval == -ENXIO) {
bridge->port.jack.procEntry = NULL;
} else {
goto out;
}
} else {
bridge->port.jack.procEntry->read_proc = VNetBridgeProcRead;
bridge->port.jack.procEntry->data = bridge;
}
/*
* Rest of fields.
*/
bridge->port.flags = IFF_RUNNING;
memset(bridge->port.paddr, 0, sizeof bridge->port.paddr);
memset(bridge->port.ladrf, 0, sizeof bridge->port.ladrf);
bridge->port.paddr[0] = VMX86_STATIC_OUI0;
bridge->port.paddr[1] = VMX86_STATIC_OUI1;
bridge->port.paddr[2] = VMX86_STATIC_OUI2;
bridge->port.fileOpRead = NULL;
bridge->port.fileOpWrite = NULL;
bridge->port.fileOpIoctl = NULL;
bridge->port.fileOpPoll = NULL;
/* misc. configuration */
bridge->forceSmac = (flags & VNET_BRFLAG_FORCE_SMAC) ? TRUE : FALSE;
/* create event sender */
retval = VNetHub_CreateSender(hubJack, &bridge->eventSender);
if (retval != 0) {
goto out;
}
/*
* on RHEL3 Linux 2.4.21-47 (others maybe too) the notifier does not fire
* and bring up the bridge as expected, thus we bring it up manually
* *before* registering the notifier (PR306435)
*/
VNetBridgeUp(bridge, TRUE);
/*
* register notifier for network device state change notifications, the
* notifier will fire right away, and the notifier handler will bring up
* the bridge (see exception above)
*/
bridge->notifier.notifier_call = VNetBridgeNotify;
bridge->notifier.priority = 0;
register_netdevice_notifier(&bridge->notifier);
/* return bridge */
*ret = &bridge->port;
LOG(1, (KERN_DEBUG "bridge-%s: attached\n", bridge->name));
return 0;
out:
if (bridge != NULL) {
kfree(bridge);
}
return retval;
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeFree --
*
* Unregister from device state notifications, disable the bridge,
* destroy sender, remove proc entry, cleanup smac, and deallocate
* struct.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
VNetBridgeFree(VNetJack *this) // IN: jack to free
{
VNetBridge *bridge = (VNetBridge*)this->private;
/* unregister notifier */
if (bridge->notifier.notifier_call != NULL) {
int err;
err = compat_unregister_netdevice_notifier(&bridge->notifier);
if (err != 0) {
LOG(0, (KERN_NOTICE "Can't unregister netdevice notifier (%d)\n",
err));
}
bridge->notifier.notifier_call = NULL;
}
/* disable bridge */
if (bridge->dev != NULL) {
LOG(1, (KERN_DEBUG "bridge-%s: disabling the bridge\n", bridge->name));
VNetBridgeDown(bridge, TRUE);
}
/* destroy event sender */
VNetEvent_DestroySender(bridge->eventSender);
bridge->eventSender = NULL;
/* remove /proc entry */
if (this->procEntry) {
VNetProc_RemoveEntry(this->procEntry);
}
if (bridge->smac){
SMAC_CleanupState(&(bridge->smac));
}
/* free bridge */
LOG(1, (KERN_DEBUG "bridge-%s: detached\n", bridge->name));
kfree(bridge);
}
/*
*----------------------------------------------------------------------
*
* VNetCallSMACFunc --
*
* Wrapper for SMAC functions. The skb must be linear.
*
* Results:
* Packet Status.
*
* Side effects:
* The skb buffer is freed if not successful otherwise it points to
* the clone.
*
*----------------------------------------------------------------------
*/
static PacketStatus
VNetCallSMACFunc(struct SMACState *state, // IN: pointer to state
struct sk_buff **skb, // IN/OUT: packet to process
void *startOfData, // IN: points to start of data
SMACFunc func, // IN: function to be called
unsigned int len) // IN: length including ETH header
{
SMACPackets packets = { {0} };
PacketStatus status;
SKB_LINEAR_ASSERT(*skb);
packets.orig.skb = *skb;
packets.orig.startOfData = startOfData;
packets.orig.len = len;
status = func(state, &packets);
if (status != PacketStatusForwardPacket) {
dev_kfree_skb(*skb);
return status;
}
if (packets.clone.skb) {
dev_kfree_skb(*skb);
*skb = packets.clone.skb;
}
return status;
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeReceiveFromVNet --
*
* This jack is receiving a packet from a vnet. This function
* sends down (i.e., out on the host net device) if the packet
* isn't destined for the host, and it sends up (i.e.,
* simulates a receive for the host) if the packet
* satisfies the host's packet filter.
*
* When the function sends up it keeps a reference to the
* packet in a history list so that we can avoid handing
* a VM a copy of its own packet.
*
* Results:
* None.
*
* Side effects:
* Frees skb. Checks if host device is still using
* promiscuous mode.
*
*----------------------------------------------------------------------
*/
void
VNetBridgeReceiveFromVNet(VNetJack *this, // IN: jack
struct sk_buff *skb) // IN: pkt to receive
{
VNetBridge *bridge = (VNetBridge*)this->private;
struct net_device *dev = bridge->dev;
uint8 dest[ETH_ALEN];
struct sk_buff *clone;
LOG(3, (KERN_DEBUG "bridge-%s: transmit %d\n",
bridge->name, (int) skb->len));
if (!dev) {
dev_kfree_skb(skb);
return;
}
/*
* skb might be freed by wireless code, so need to keep
* a local copy of the MAC rather than a pointer to it.
*/
memcpy(dest, SKB_2_DESTMAC(skb), ETH_ALEN);
#ifdef notdef
// xxx;
/*
* We need to send the packet both up to the host and down
* to the interface.
* However, we ignore packets destined only for this hub.
*/
for (i = 0; i < VNET_PORTS_PER_HUB; i++) {
VNetPort *p = &port->hub->port[i];
if (UP_AND_RUNNING(p->flags) && MAC_EQ(dest, p->paddr)) {
return;
}
}
#endif
/*
* SMAC processing. SMAC interfaces that the skb is linear, so ensure that
* this is the case prior to calling out.
*/
if (bridge->smac) {
if (compat_skb_is_nonlinear(skb) && compat_skb_linearize(skb)) {
LOG(4, (KERN_NOTICE "bridge-%s: couldn't linearize, packet dropped\n",
bridge->name));
return;
}
if (VNetCallSMACFunc(bridge->smac, &skb, skb->data,
SMAC_CheckPacketToHost, skb->len) !=
PacketStatusForwardPacket) {
LOG(4, (KERN_NOTICE "bridge-%s: packet dropped\n", bridge->name));
return;
}
}
/*
* Send down (imitate packet_sendmsg)
*
* Do this only if the packet is not addressed to the peer,
* and the packet size is not too big.
*/
dev_lock_list();
if (MAC_EQ(dest, dev->dev_addr) ||
skb->len > dev->mtu + dev->hard_header_len) {
dev_unlock_list();
} else {
# if 0 // XXX we should do header translation
if ((dev->flags & IFF_SOFTHEADERS) != 0) {
if (skb->len > dev->mtu) {
clone = NULL;
} else {
clone = dev_alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
}
if (clone != NULL) {
skb_reserve(clone, dev->hard_header_len);
if (dev->hard_header != NULL) {
dev->hard_header(clone, dev, ETH_P_IP, NULL, NULL, skb->len);
}
memcpy(skb_put(clone, skb->len), skb->data, skb->len);
}
}
# endif
clone = skb_clone(skb, GFP_ATOMIC);
if (clone == NULL) {
dev_unlock_list();
} else {
skb_set_owner_w(clone, bridge->sk);
clone->protocol = ((struct ethhdr *)skb->data)->h_proto; // XXX
if ((dev->flags & IFF_UP) != 0) {
dev_unlock_list();
DEV_QUEUE_XMIT(clone, dev, 0);
} else {
dev_unlock_list();
dev_kfree_skb(clone);
}
}
}
/*
* Send up (imitate Ethernet receive)
*
* Do this if the packet is addressed to the peer (or is broadcast, etc.).
*
* This packet will get back to us, via VNetBridgeReceive.
* We save it so we can recognize it (and its clones) again.
*/
if (VNetPacketMatch(dest, dev->dev_addr, NULL, 0, allMultiFilter, dev->flags)) {
clone = skb_clone(skb, GFP_ATOMIC);
if (clone) {
unsigned long flags;
int i;
atomic_inc(&clone->users);
clone->dev = dev;
clone->protocol = eth_type_trans(clone, dev);
spin_lock_irqsave(&bridge->historyLock, flags);
for (i = 0; i < VNET_BRIDGE_HISTORY; i++) {
if (bridge->history[i] == NULL) {
bridge->history[i] = clone;
# if LOGLEVEL >= 3
{
int j;
int count = 0;
for (j = 0; j < VNET_BRIDGE_HISTORY; j++) {
if (bridge->history[j] != NULL) {
count++;
}
}
LOG(3, (KERN_DEBUG "bridge-%s: host slot %d history %d\n",
bridge->name, i, count));
}
# endif
break;
}
}
if (i >= VNET_BRIDGE_HISTORY) {
LOG(1, (KERN_NOTICE "bridge-%s: history full\n",
bridge->name));
for (i = 0; i < VNET_BRIDGE_HISTORY; i++) {
struct sk_buff *s = bridge->history[i];
/*
* We special case 0 to avoid races with another thread on
* another cpu wanting to use the 0 entry. This could happen
* when we release the lock to free the former entry.
* See bug 11231 for details.
*/
if (i == 0) {
bridge->history[0] = clone;
} else {
bridge->history[i] = NULL;
}
if (s) {
spin_unlock_irqrestore(&bridge->historyLock, flags);
dev_kfree_skb(s);
spin_lock_irqsave(&bridge->historyLock, flags);
}
}
}
spin_unlock_irqrestore(&bridge->historyLock, flags);
/*
* We used to cli() before calling netif_rx() here. It was probably
* unneeded (as we never did it in netif.c, and the code worked). In
* any case, now that we are using netif_rx_ni(), we should certainly
* not do it, or netif_rx_ni() will deadlock on the cli() lock --hpreg
*/
netif_rx_ni(clone);
# if LOGLEVEL >= 4
do_gettimeofday(&vnetTime);
# endif
}
}
// xxx;
dev_kfree_skb(skb);
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeCycleDetect --
*
* Cycle detection algorithm.
*
* Results:
* TRUE if a cycle was detected, FALSE otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Bool
VNetBridgeCycleDetect(VNetJack *this, // IN: jack
int generation) // IN: generation
{
VNetBridge *bridge = (VNetBridge*)this->private;
return VNetCycleDetectIf(bridge->name, generation);
}
/*
*----------------------------------------------------------------------
*
* VNetBridgePortsChanged --
*
* The number of ports connected to this jack has change, react
* accordingly by starting/stopping promiscuous mode based on
* whether any peers exist.
*
* Results:
* None.
*
* Side effects:
* Promiscuous mode may be started or stopped.
*
*----------------------------------------------------------------------
*/
void
VNetBridgePortsChanged(VNetJack *this) // IN: jack
{
VNetBridge *bridge = (VNetBridge*)this->private;
if (bridge->dev) {
if (VNetGetAttachedPorts(this)) {
VNetBridgeStartPromisc(bridge, TRUE);
} else {
VNetBridgeStopPromisc(bridge, TRUE);
}
}
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeIsBridged --
*
* Reports if the bridged interface is up or down.
*
* Results:
* 1 - we are bridged but the interface is not up
* 2 - we are bridged and the interface is up
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
VNetBridgeIsBridged(VNetJack *this) // IN: jack
{
VNetBridge *bridge = (VNetBridge*)this->private;
if (bridge->dev) {
return 2;
} else {
return 1;
}
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeIsDeviceWireless --
*
* Check if the device is a wireless adapter, depending on the version
* of the wireless extension present in the kernel.
*
* Results:
* TRUE if the device is wireless, FALSE otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static Bool
VNetBridgeIsDeviceWireless(struct net_device *dev) //IN: sock
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
# if defined(CONFIG_WIRELESS_EXT)
return dev->ieee80211_ptr != NULL || dev->wireless_handlers != NULL;
# else
return dev->ieee80211_ptr != NULL;
# endif
#elif defined(CONFIG_WIRELESS_EXT)
return dev->wireless_handlers != NULL;
#elif !defined(CONFIG_NET_RADIO)
return FALSE;
#elif defined WIRELESS_EXT && WIRELESS_EXT > 19
return dev->wireless_handlers != NULL;
#elif defined WIRELESS_EXT && WIRELESS_EXT > 12
return dev->wireless_handlers != NULL || dev->get_wireless_stats != NULL;
#else
return dev->get_wireless_stats != NULL;
#endif
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeSendLinkStateEvent --
*
* Sends a link state event.
*
* Results:
* Returns 0 if successful, or a negative value if an error occurs.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VNetBridgeSendLinkStateEvent(VNetBridge *bridge, // IN: the bridge
uint32 adapter, // IN: the adapter
Bool up) // IN: the link state
{
VNet_LinkStateEvent event;
int res;
event.header.size = sizeof event;
res = VNetEvent_GetSenderId(bridge->eventSender, &event.header.senderId);
if (res != 0) {
LOG(1, (KERN_NOTICE "bridge-%s: can't send link state event, "
"getSenderId failed (%d)\n", bridge->name, res));
return res;
}
event.header.eventId = 0;
event.header.classSet = VNET_EVENT_CLASS_UPLINK;
event.header.type = VNET_EVENT_TYPE_LINK_STATE;
event.adapter = adapter;
event.up = up;
res = VNetEvent_Send(bridge->eventSender, &event.header);
if (res != 0) {
LOG(1, (KERN_NOTICE "bridge-%s: can't send link state event, send "
"failed (%d)\n", bridge->name, res));
}
return res;
}
/*
*----------------------------------------------------------------------
*
* VNetBridgeUp --
*
* Bring a bridge up. Gets peer's device structure, verifies
* that interface is up, checks the header length,
* allocates a socket, adds a packet handler to the network
* stack, and then places the peer's device in promiscuous
* mode.
*
* Results:
* errno.
*
* Side effects:
* Bridging may be brought up with a peer interface.
*
*----------------------------------------------------------------------
*/
static int
VNetBridgeUp(VNetBridge *bridge, // IN: bridge struct
Bool rtnlLock) // IN: acquire RTNL lock
{
int retval = 0;
if (bridge->dev != NULL) {
LOG(0, (KERN_NOTICE "bridge-%s: already up\n", bridge->name));
goto out;
}
/*
* Get peer device structure
*/
dev_lock_list();
bridge->dev = DEV_GET(bridge);
LOG(2, (KERN_DEBUG "bridge-%s: got dev %p\n",
bridge->name, bridge->dev));
if (bridge->dev == NULL) {
dev_unlock_list();
retval = -ENODEV;
goto out;
}
if (!(bridge->dev->flags & IFF_UP)) {
LOG(2, (KERN_DEBUG "bridge-%s: interface %s is not up\n",
bridge->name, bridge->dev->name));
dev_unlock_list();
retval = -ENODEV;
goto out;
}
/*
* At a minimum, the header size should be the same as ours.
*
* XXX we should either do header translation or ensure this
* is an Ethernet.
*/
if (bridge->dev->hard_header_len != ETH_HLEN) {
LOG(1, (KERN_DEBUG "bridge-%s: can't bridge with %s, bad header length %d\n",
bridge->name, bridge->dev->name, bridge->dev->hard_header_len));
dev_unlock_list();
retval = -EINVAL;
goto out;
}
/*
* Get a socket to play with
*
* We set the dead field so we don't get a call back from dev_kfree_skb().
* (The alternative is to support the callback.)
*/
bridge->sk = compat_sk_alloc(bridge, GFP_ATOMIC);
if (bridge->sk == NULL) {
dev_unlock_list();
retval = -ENOMEM;
goto out;
}
sock_init_data(NULL, bridge->sk);
sock_set_flag(bridge->sk, SOCK_DEAD);
if (VNetBridgeIsDeviceWireless(bridge->dev)) {
LOG(1, (KERN_NOTICE "bridge-%s: device is wireless, enabling SMAC\n",
bridge->name));
bridge->wirelessAdapter = TRUE;
}
/*
* If it is a wireless adapter initialize smac struct.
*/
if (bridge->wirelessAdapter || bridge->forceSmac) {
SMAC_InitState(&(bridge->smac));
if (bridge->smac) {
/*
* Store the MAC address of the adapter
*/
SMAC_SetMac(bridge->smac, bridge->dev->dev_addr);
}
}
/*
* Link up with the peer device by adding a
* packet handler to the networking stack.
*/
bridge->pt.func = VNetBridgeReceiveFromDev;
bridge->pt.type = htons(ETH_P_ALL);
bridge->pt.dev = bridge->dev;
bridge->pt.af_packet_priv = bridge->sk;
bridge->enabledPromisc = FALSE;
bridge->warnPromisc = FALSE;
dev_add_pack(&bridge->pt);
dev_unlock_list();
/*
* Put in promiscuous mode if need be.