1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 The full GNU General Public License is included in this distribution in the
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
22 Stephen Hemminger <shemminger@linux-foundation.org>
24 ******************************************************************************/
27 #include "epoll_loop.h"
28 #include "netif_utils.h"
29 #include "bridge_ctl.h"
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
40 #include <netinet/in.h>
43 #include <linux/if_ether.h>
44 #include <linux/if_packet.h>
45 #include <linux/filter.h>
49 static struct epoll_event_handler packet_event
;
52 static void dump_packet(const unsigned char *buf
, int cc
)
55 for (i
= 0; i
< cc
; i
+= 16) {
56 for (j
= 0; j
< 16 && i
+ j
< cc
; j
++)
57 printf(" %02x", buf
[i
+ j
]);
66 * To send/receive Spanning Tree packets we use PF_PACKET because
67 * it allows the filtering we want but gives raw data
69 void packet_send(int ifindex
, const unsigned char *data
, int len
)
72 struct sockaddr_ll sl
= {
73 .sll_family
= AF_PACKET
,
74 .sll_protocol
= htons(ETH_P_802_2
),
75 .sll_ifindex
= ifindex
,
76 .sll_halen
= ETH_ALEN
,
79 memcpy(&sl
.sll_addr
, data
, ETH_ALEN
);
82 printf("Transmit Dst index %d %02x:%02x:%02x:%02x:%02x:%02x\n",
84 sl
.sll_addr
[0], sl
.sll_addr
[1], sl
.sll_addr
[2],
85 sl
.sll_addr
[3], sl
.sll_addr
[4], sl
.sll_addr
[5]);
87 dump_packet(data
, len
);
90 l
= sendto(packet_event
.fd
, data
, len
, 0,
91 (struct sockaddr
*) &sl
, sizeof(sl
));
94 if (errno
!= EWOULDBLOCK
)
95 ERROR("send failed: %m");
97 ERROR("short write in sendto: %d instead of %d", l
, len
);
100 static void packet_rcv(uint32_t events
, struct epoll_event_handler
*h
)
103 unsigned char buf
[2048];
104 struct sockaddr_ll sl
;
105 socklen_t salen
= sizeof sl
;
107 cc
= recvfrom(h
->fd
, &buf
, sizeof(buf
), 0, (struct sockaddr
*) &sl
, &salen
);
109 ERROR("recvfrom failed: %m");
114 printf("Receive Src ifindex %d %02x:%02x:%02x:%02x:%02x:%02x\n",
116 sl
.sll_addr
[0], sl
.sll_addr
[1], sl
.sll_addr
[2],
117 sl
.sll_addr
[3], sl
.sll_addr
[4], sl
.sll_addr
[5]);
119 dump_packet(buf
, cc
);
122 bridge_bpdu_rcv(sl
.sll_ifindex
, buf
, cc
);
125 /* Berkeley Packet filter code to filter out spanning tree packets.
128 static struct sock_filter stp_filter
[] = {
129 { 0x28, 0, 0, 0x0000000c },
130 { 0x25, 3, 0, 0x000005dc },
131 { 0x30, 0, 0, 0x0000000e },
132 { 0x15, 0, 1, 0x00000042 },
133 { 0x6, 0, 0, 0x00000060 },
134 { 0x6, 0, 0, 0x00000000 },
138 * Open up a raw packet socket to catch all 802.2 packets.
139 * and install a packet filter to only see STP (SAP 42)
141 * Since any bridged devices are already in promiscious mode
142 * no need to add multicast address.
144 int packet_sock_init(void)
147 struct sock_fprog prog
= {
148 .len
= sizeof(stp_filter
) / sizeof(stp_filter
[0]),
149 .filter
= stp_filter
,
152 s
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_802_2
));
154 ERROR("socket failed: %m");
158 if (setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
)) < 0)
159 ERROR("setsockopt packet filter failed: %m");
161 else if (fcntl(s
, F_SETFL
, O_NONBLOCK
) < 0)
162 ERROR("fcntl set nonblock failed: %m");
166 packet_event
.handler
= packet_rcv
;
168 if (add_epoll(&packet_event
) == 0)