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 Authors: Stephen Hemminger <shemminger@linux-foundation.org>
24 ******************************************************************************/
26 /* #define PACKET_DEBUG */
32 #include <netinet/in.h>
33 #include <linux/if_packet.h>
34 #include <linux/filter.h>
35 #include <asm/byteorder.h>
37 #include "epoll_loop.h"
38 #include "netif_utils.h"
39 #include "bridge_ctl.h"
43 static struct epoll_event_handler packet_event
;
46 static void dump_packet(const unsigned char *buf
, int cc
)
49 for(i
= 0; i
< cc
; i
+= 16)
51 for(j
= 0; j
< 16 && i
+ j
< cc
; ++j
)
52 printf(" %02x", buf
[i
+ j
]);
61 * To send/receive Spanning Tree packets we use PF_PACKET because
62 * it allows the filtering we want but gives raw data
64 void packet_send(int ifindex
, const struct iovec
*iov
, int iov_count
, int len
)
67 struct sockaddr_ll sl
=
69 .sll_family
= AF_PACKET
,
70 .sll_protocol
= __constant_cpu_to_be16(ETH_P_802_2
),
71 .sll_ifindex
= ifindex
,
72 .sll_halen
= ETH_ALEN
,
75 if(iov_count
> 0 && iov
[0].iov_len
> ETH_ALEN
)
76 memcpy(&sl
.sll_addr
, iov
[0].iov_base
, ETH_ALEN
);
81 .msg_namelen
= sizeof(sl
),
82 .msg_iov
= (struct iovec
*)iov
,
83 .msg_iovlen
= iov_count
,
90 printf("Transmit Dst index %d %02x:%02x:%02x:%02x:%02x:%02x\n",
92 sl
.sll_addr
[0], sl
.sll_addr
[1], sl
.sll_addr
[2],
93 sl
.sll_addr
[3], sl
.sll_addr
[4], sl
.sll_addr
[5]);
96 for(i
= 0; i
< iov_count
; ++i
)
97 dump_packet(iov
[i
].iov_base
, iov
[i
].iov_len
);
101 l
= sendmsg(packet_event
.fd
, &msg
, 0);
105 if(errno
!= EWOULDBLOCK
)
106 ERROR("send failed: %m");
109 ERROR("short write in sendto: %d instead of %d", l
, len
);
112 static void packet_rcv(uint32_t events
, struct epoll_event_handler
*h
)
115 unsigned char buf
[2048];
116 struct sockaddr_ll sl
;
117 socklen_t salen
= sizeof sl
;
119 cc
= recvfrom(h
->fd
, &buf
, sizeof(buf
), 0, (struct sockaddr
*) &sl
, &salen
);
122 ERROR("recvfrom failed: %m");
127 printf("Receive Src ifindex %d %02x:%02x:%02x:%02x:%02x:%02x\n",
129 sl
.sll_addr
[0], sl
.sll_addr
[1], sl
.sll_addr
[2],
130 sl
.sll_addr
[3], sl
.sll_addr
[4], sl
.sll_addr
[5]);
132 dump_packet(buf
, cc
);
135 bridge_bpdu_rcv(sl
.sll_ifindex
, buf
, cc
);
138 /* Berkeley Packet filter code to filter out spanning tree packets.
139 from tcpdump -s 1152 -dd stp
141 static struct sock_filter stp_filter
[] = {
142 { 0x28, 0, 0, 0x0000000c },
143 { 0x25, 3, 0, 0x000005dc },
144 { 0x30, 0, 0, 0x0000000e },
145 { 0x15, 0, 1, 0x00000042 },
146 { 0x6, 0, 0, 0x00000480 },
147 { 0x6, 0, 0, 0x00000000 },
151 * Open up a raw packet socket to catch all 802.2 packets.
152 * and install a packet filter to only see STP (SAP 42)
154 * Since any bridged devices are already in promiscious mode
155 * no need to add multicast address.
157 int packet_sock_init(void)
160 struct sock_fprog prog
=
162 .len
= sizeof(stp_filter
) / sizeof(stp_filter
[0]),
163 .filter
= stp_filter
,
166 s
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_802_2
));
169 ERROR("socket failed: %m");
173 if(setsockopt(s
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
)) < 0)
174 ERROR("setsockopt packet filter failed: %m");
175 else if(fcntl(s
, F_SETFL
, O_NONBLOCK
) < 0)
176 ERROR("fcntl set nonblock failed: %m");
180 packet_event
.handler
= packet_rcv
;
182 if(0 == add_epoll(&packet_event
))