| 1 |
|
| 2 |
#include "protoCap.h" |
| 3 |
#include "protoDebug.h" |
| 4 |
#include "protoSocket.h" |
| 5 |
|
| 6 |
#include <unistd.h> |
| 7 |
#include <sys/socket.h> |
| 8 |
#include <features.h> /* for the glibc version number */ |
| 9 |
#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1 |
| 10 |
#include <netpacket/packet.h> |
| 11 |
#include <net/ethernet.h> /* the L2 protocols */ |
| 12 |
#else |
| 13 |
#include <asm/types.h> |
| 14 |
#include <linux/if_packet.h> |
| 15 |
#include <linux/if_ether.h> /* The L2 protocols */ |
| 16 |
#endif |
| 17 |
#include <netinet/in.h> |
| 18 |
|
| 19 |
/** This implementation of ProtoCap uses the |
| 20 |
* PF_PACKET socket type available on Linux systems |
| 21 |
*/ |
| 22 |
|
| 23 |
class LinuxCap : public ProtoCap |
| 24 |
{ |
| 25 |
public: |
| 26 |
LinuxCap(); |
| 27 |
~LinuxCap(); |
| 28 |
|
| 29 |
bool Open(const char* interfaceName = NULL); |
| 30 |
void Close(); |
| 31 |
bool Send(const char* buffer, unsigned int& numBytes); |
| 32 |
bool Recv(char* buffer, unsigned int& numBytes, Direction* direction = NULL); |
| 33 |
|
| 34 |
}; // end class LinuxCap |
| 35 |
|
| 36 |
ProtoCap* ProtoCap::Create() |
| 37 |
{ |
| 38 |
return static_cast<ProtoCap*>(new LinuxCap()); |
| 39 |
} // end ProtoCap::Create() |
| 40 |
|
| 41 |
LinuxCap::LinuxCap() |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
LinuxCap::~LinuxCap() |
| 46 |
{ |
| 47 |
Close(); |
| 48 |
} |
| 49 |
|
| 50 |
bool LinuxCap::Open(const char* interfaceName) |
| 51 |
{ |
| 52 |
char buffer[256]; |
| 53 |
if (NULL == interfaceName) |
| 54 |
{ |
| 55 |
// Try to determine a "default" interface |
| 56 |
ProtoAddress localAddress; |
| 57 |
if (!localAddress.ResolveLocalAddress()) |
| 58 |
{ |
| 59 |
PLOG(PL_ERROR, "LinuxCap::Open() error: couldn't auto determine local interface\n"); |
| 60 |
return false; |
| 61 |
} |
| 62 |
if (!ProtoSocket::GetInterfaceName(localAddress, buffer, 256)) |
| 63 |
{ |
| 64 |
PLOG(PL_ERROR, "LinuxCap::Open() error: couldn't determine local interface name\n"); |
| 65 |
return false; |
| 66 |
} |
| 67 |
interfaceName = buffer; |
| 68 |
} |
| 69 |
|
| 70 |
if ((descriptor = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) |
| 71 |
{ |
| 72 |
PLOG(PL_ERROR, "LinuxCap::Open() socket(PF_PACKET) error: %s\n", GetErrorString()); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
// try to turn on broadcast capability (why?) |
| 77 |
int enable = 1; |
| 78 |
if (setsockopt(descriptor, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable)) < 0) |
| 79 |
PLOG(PL_ERROR, "LinuxCap::Open() setsockopt(SO_BROADCAST) warning: %s\n", |
| 80 |
GetErrorString()); |
| 81 |
|
| 82 |
int ifIndex = ProtoSocket::GetInterfaceIndex(interfaceName); |
| 83 |
if (0 == ifIndex) |
| 84 |
{ |
| 85 |
PLOG(PL_ERROR, "LinuxCap::Open() error getting interface index\n"); |
| 86 |
Close(); |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
// Set interface to promiscuous mode |
| 91 |
// (TBD) add ProtoCap method to control interface promiscuity |
| 92 |
struct packet_mreq mreq; |
| 93 |
memset(&mreq, 0, sizeof(struct packet_mreq)); |
| 94 |
mreq.mr_ifindex = ifIndex; |
| 95 |
mreq.mr_type = PACKET_MR_PROMISC; |
| 96 |
if (setsockopt(descriptor, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) |
| 97 |
PLOG(PL_ERROR, "LinuxCap::Open() setsockopt(PACKET_MR_PROMISC) warning: %s\n", |
| 98 |
GetErrorString()); |
| 99 |
|
| 100 |
|
| 101 |
if (!ProtoSocket::GetInterfaceAddress(interfaceName, ProtoAddress::ETH, if_addr)) |
| 102 |
{ |
| 103 |
PLOG(PL_ERROR, "LinuxCap::Open() error getting interface MAC address\n"); |
| 104 |
Close(); |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
// Init our interface address structure |
| 109 |
struct sockaddr_ll ifaceAddr; |
| 110 |
memset((char*)&ifaceAddr, 0, sizeof(ifaceAddr)); |
| 111 |
ifaceAddr.sll_protocol = htons(ETH_P_ALL); |
| 112 |
ifaceAddr.sll_ifindex = ifIndex; |
| 113 |
ifaceAddr.sll_family = AF_PACKET; |
| 114 |
memcpy(ifaceAddr.sll_addr, if_addr.GetRawHostAddress(), 6); |
| 115 |
ifaceAddr.sll_halen = if_addr.GetLength(); |
| 116 |
|
| 117 |
// bind() the socket to the specified interface |
| 118 |
if (bind(descriptor, (struct sockaddr*)&ifaceAddr, sizeof(ifaceAddr)) < 0) |
| 119 |
{ |
| 120 |
PLOG(PL_ERROR, "LinuxCap::Open() bind error: %s\n", GetErrorString()); |
| 121 |
Close(); |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
// Explicitly call ProtoCap::Open so that ProtoChannel stuff is properly set up |
| 126 |
if (!ProtoCap::Open(interfaceName)) |
| 127 |
{ |
| 128 |
PLOG(PL_ERROR, "LinuxCap::Open() ProtoCap::Open() error\n"); |
| 129 |
Close(); |
| 130 |
return false; |
| 131 |
} |
| 132 |
if_index = ifIndex; |
| 133 |
return true; |
| 134 |
} // end LinuxCap::Open() |
| 135 |
|
| 136 |
void LinuxCap::Close() |
| 137 |
{ |
| 138 |
ProtoCap::Close(); |
| 139 |
if (INVALID_HANDLE != descriptor) |
| 140 |
{ |
| 141 |
close(descriptor); |
| 142 |
descriptor = INVALID_HANDLE; |
| 143 |
} |
| 144 |
} // end LinuxCap::Close() |
| 145 |
|
| 146 |
bool LinuxCap::Send(const char* buffer, unsigned int& numBytes) |
| 147 |
{ |
| 148 |
// Make sure packet is a type that is OK for us to send |
| 149 |
// (Some packets seem to cause a PF_PACKET socket trouble) |
| 150 |
UINT16 type; |
| 151 |
memcpy(&type, buffer+12, 2); |
| 152 |
type = ntohs(type); |
| 153 |
if (type <= 0x05dc) // assume it's 802.3 Length and ignore |
| 154 |
{ |
| 155 |
PLOG(PL_DEBUG, "LinuxCap::Send() unsupported 802.3 frame (len = %04x)\n", type); |
| 156 |
return false; |
| 157 |
} |
| 158 |
for(;;) |
| 159 |
{ |
| 160 |
ssize_t result = write(descriptor, buffer, numBytes); |
| 161 |
if (result < 0) |
| 162 |
{ |
| 163 |
switch (errno) |
| 164 |
{ |
| 165 |
case EINTR: |
| 166 |
continue; // try again |
| 167 |
case EWOULDBLOCK: |
| 168 |
numBytes = 0; |
| 169 |
case ENOBUFS: |
| 170 |
// because this doesn't block write() |
| 171 |
default: |
| 172 |
PLOG(PL_WARN, "LinuxCap::Send() error: %s\n", GetErrorString()); |
| 173 |
break; |
| 174 |
} |
| 175 |
return false; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
ASSERT(result == numBytes); |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
return true; |
| 184 |
} // end LinuxCap::Send() |
| 185 |
|
| 186 |
bool LinuxCap::Recv(char* buffer, unsigned int& numBytes, Direction* direction) |
| 187 |
{ |
| 188 |
struct sockaddr_ll pktAddr; |
| 189 |
socklen_t addrLen = sizeof(pktAddr); |
| 190 |
int result = recvfrom(descriptor, buffer, (size_t)numBytes, 0, |
| 191 |
(struct sockaddr*)&pktAddr, &addrLen); |
| 192 |
if (result < 0) |
| 193 |
{ |
| 194 |
numBytes = 0; |
| 195 |
switch (errno) |
| 196 |
{ |
| 197 |
case EINTR: |
| 198 |
case EAGAIN: |
| 199 |
return true; |
| 200 |
default: |
| 201 |
PLOG(PL_ERROR, "LinuxCap::Recv() error: %s\n", GetErrorString()); |
| 202 |
break; |
| 203 |
} |
| 204 |
return false; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
if (NULL != direction) |
| 209 |
{ |
| 210 |
if (pktAddr.sll_pkttype == PACKET_OUTGOING) |
| 211 |
*direction = OUTBOUND; |
| 212 |
else |
| 213 |
*direction = INBOUND; |
| 214 |
} |
| 215 |
numBytes = result; |
| 216 |
return true; |
| 217 |
} |
| 218 |
} // end LinuxCap::Recv() |