ืฉืืื. ืื ืืืืจ ืฉืืฉ ืจืฉืช ืฉื 5k ืืงืืืืช. ืืืืจืื ื ืงืจื ืจืืข ืื ื ืขืื ืืืืืื - ืืืจืื ืืจืฉืช ืืฉ ืื ื Brocade RX8 ืืืื ืืชืืื ืืฉืืื ืืจืื ืืืืืืช unicast ืื ืืืืขืืช, ืืืืืื ืฉืืจืฉืช ืืืืืงืช ื-vlans - ืื ืืืงืืช ืื ืืขืื, ืืื ืืฉ vlans ืืืืืืื ืขืืืจ ืืชืืืืช ืืื ืืช ืืื'. ืืื ืืชืืืื ืืื ืืืืืื ืื ืฉื ืืจืฉืช. ืื ืขืืฉืื ืืืืื ื ืืจืืื ื ืื ืกืช ืืืชืืืช ืฉื ืืงืื ืฉืื ืืืื ืืชืืืื ืื"ื ืืืืจืืื ืืื ืขืคื ืืขืืจ ืงืืฉืืจ ืจืืื ืืืคืจ ืืืฉืื (ืื ืืืื) - ืืขืจืืฅ ืกืชืื - ืืืงืืืืช ืืืขืกืื - ืขืฆื...
ืืืืจื ืืื ืืืคืื ืืื ืืคืืฆ'ืจ. ืืฉืืชื ืืืืืื ืฉื q-in-q ืขื vlan ืืงืื ืืื, ืืื ืื ืืื ื ืืืืจื ืืื P3310, ืืืฉืจ dot1q ืืืคืขื, ืืคืกืืงื ืืขืืืจ DHCP, ืื ืื ืื ืืืืขืื ืืื ืืขืฉืืช qinq ืกืืงืืืื ืืืืืืืืช ืจืืืช ืฉื ืืืกืื ืืื. ืื ืื ip ืืื ืืกืคืจ ืืืื ืื ืขืืื? ืืงืฆืจื ืืืื: ืืชืืืช ืฉืขืจ + ืืกืืื ืืืืฉืง. ืขืืืจ ืืืฉืืื ืฉืื ื, ืื ืื ื ืฆืจืืืื: ืืืชืื ืืช ืืืขืฆื, ืืืคืืฅ ืืชืืืืช ืืืงืืืืช, ืืืืกืืฃ ืืกืืืืื ืืืงืืืืช ืืจื ืืืฉืงืื ืืกืืืืื. ืืื ืืขืฉืืช ืืช ืื ืื? Shaper - lisg, dhcp - db2dhcp ืืฉื ื ืฉืจืชืื ืขืฆืืืืื, dhcprelay ืคืืขื ืขื ืฉืจืชื ืืืืฉื, ucarp ืคืืขื ืื ืขื ืฉืจืชื ืืืืฉื - ืืืืืื. ืืื ืืื ืืืกืืคืื ืืกืืืืื? ืืคืฉืจ ืืืืกืืฃ ืืื ืืจืืฉ ืขื ืกืงืจืืคื ืืืื - ืืื ืื ืื ื ืืื. ืื ื ืขืฉื ืงื ืืืชื ืขืฆืื.
ืืืืจ ืืืคืืฉ ืืขืืืง ืืืื ืืจื ื, ืืฆืืชื ืกืคืจืืื ื ืคืืื ืืจืื ืืืืื ืขืืืจ C++, ืืืืคืฉืจืช ืื ืืืจืื ืืคื ืชืขืืืจื. ืืืืืืจืืชื ืฉื ืืชืืื ืืช ืฉืืืกืืคื ืืกืืืืื ืืื ืืืืงืื - ืื ื ืืืืื ืื ืืืงืฉืืช arp ืืืืฉืง, ืื ืืฉ ืื ื ืืชืืืช ืืืืฉืง lo ืืฉืจืช ืืืืืงืฉ, ืื ื ืืกืืฃ ืืกืืื ืืจื ืืืืฉืง ืืื ืื ืืกืืฃ arp ืกืืื ืืืงืืื ื-IP ืืื - ืืืืคื ืืืื, ืืื ืืขืชืง-ืืืืงืื, ืงืฆืช ืฉื ืชืืืจ ืืกืืืืช
ืืงืืจืืช ื'ื ืชื'
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <tins/tins.h>
#include <map>
#include <iostream>
#include <functional>
#include <sstream>
using std::cout;
using std::endl;
using std::map;
using std::bind;
using std::string;
using std::stringstream;
using namespace Tins;
class arp_monitor {
public:
void run(Sniffer &sniffer);
void reroute();
void makegws();
string iface;
map <string, string> gws;
private:
bool callback(const PDU &pdu);
map <string, string> route_map;
map <string, string> mac_map;
map <IPv4Address, HWAddress<6>> addresses;
};
void arp_monitor::makegws() {
struct ifaddrs *ifAddrStruct = NULL;
struct ifaddrs *ifa = NULL;
void *tmpAddrPtr = NULL;
gws.clear();
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
string ifName = ifa->ifa_name;
if (ifName == "lo") {
char addressBuffer[INET_ADDRSTRLEN];
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
} else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr = &((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr;
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
} else {
continue;
}
gws[addressBuffer] = addressBuffer;
cout << "GW " << addressBuffer << " is added" << endl;
}
}
if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
}
void arp_monitor::run(Sniffer &sniffer) {
cout << "RUNNED" << endl;
sniffer.sniff_loop(
bind(
&arp_monitor::callback,
this,
std::placeholders::_1
)
);
}
void arp_monitor::reroute() {
cout << "REROUTING" << endl;
map<string, string>::iterator it;
for ( it = route_map.begin(); it != route_map.end(); it++ ) {
if (this->gws.count(it->second) && !this->gws.count(it->second)) {
string cmd = "ip route replace ";
cmd += it->first;
cmd += " dev " + this->iface;
cmd += " src " + it->second;
cmd += " proto static";
cout << cmd << std::endl;
cout << "REROUTE " << it->first << " SRC " << it->second << endl;
system(cmd.c_str());
cmd = "arp -s ";
cmd += it->first;
cmd += " ";
cmd += mac_map[it->first];
cout << cmd << endl;
system(cmd.c_str());
}
}
for ( it = gws.begin(); it != gws.end(); it++ ) {
string cmd = "arping -U -s ";
cmd += it->first;
cmd += " -I ";
cmd += this->iface;
cmd += " -b -c 1 ";
cmd += it->first;
system(cmd.c_str());
}
cout << "REROUTED" << endl;
}
bool arp_monitor::callback(const PDU &pdu) {
// Retrieve the ARP layer
const ARP &arp = pdu.rfind_pdu<ARP>();
if (arp.opcode() == ARP::REQUEST) {
string target = arp.target_ip_addr().to_string();
string sender = arp.sender_ip_addr().to_string();
this->route_map[sender] = target;
this->mac_map[sender] = arp.sender_hw_addr().to_string();
cout << "save sender " << sender << ":" << this->mac_map[sender] << " want taregt " << target << endl;
if (this->gws.count(target) && !this->gws.count(sender)) {
string cmd = "ip route replace ";
cmd += sender;
cmd += " dev " + this->iface;
cmd += " src " + target;
cmd += " proto static";
// cout << cmd << std::endl;
/* cout << "ARP REQUEST FROM " << arp.sender_ip_addr()
<< " for address " << arp.target_ip_addr()
<< " sender hw address " << arp.sender_hw_addr() << std::endl
<< " run cmd: " << cmd << endl;*/
system(cmd.c_str());
cmd = "arp -s ";
cmd += arp.sender_ip_addr().to_string();
cmd += " ";
cmd += arp.sender_hw_addr().to_string();
cout << cmd << endl;
system(cmd.c_str());
}
}
return true;
}
arp_monitor monitor;
void reroute(int signum) {
monitor.makegws();
monitor.reroute();
}
int main(int argc, char *argv[]) {
string test;
cout << sizeof(string) << endl;
if (argc != 2) {
cout << "Usage: " << *argv << " <interface>" << endl;
return 1;
}
signal(SIGHUP, reroute);
monitor.iface = argv[1];
// Sniffer configuration
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter("arp");
monitor.makegws();
try {
// Sniff on the provided interface in promiscuous mode
Sniffer sniffer(argv[1], config);
// Only capture arp packets
monitor.run(sniffer);
}
catch (std::exception &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
ืกืงืจืืคื ืืชืงื ื ืฉื libtins
#!/bin/bash
git clone https://github.com/mfontanini/libtins.git
cd libtins
mkdir build
cd build
cmake ../
make
make install
ldconfig
ืคืงืืื ืืื ืืช ืืช ืืืื ืืจื
g++ main.cpp -o arp-rt -O3 -std=c++11 -lpthread -ltins
ืืื ืืืคืขืื ืืืชื?
start-stop-daemon --start --exec /opt/ipoe/arp-routes/arp-rt -b -m -p /opt/ipoe/arp-routes/daemons/eth0.800.pid -- eth0.800
ืื - ืื ืืื ื ืืืืฉ ืืช ืืืืืืืช ืขื ืกืื ืืืช HUP. ืืื ืื ืืฉืชืืฉืช ื-netlink? ืื ืคืฉืื ืขืฆืืืช ืืืื ืืงืก ืื ืกืงืจืืคื ืขื ืกืงืจืืคื - ืื ืืื ืืกืืจ. ืืืื, ืืกืืืืื ืื ืืกืืืืื, ืื ืืืื? ืืืืจ ืืื, ืขืืื ื ืืฉืืื ืืช ืืืกืืืืื ืฉื ืืฆืืื ืืฉืจืช ืืื ืื ืืืืื - ืืื, ืขืงื ืืืชื ืืืืจื ืืืืฉื ืช, ื ืงืื ื ืื ืชืื ืฉื ืืืชื ืืืืช ืืงืื ื ืืืืชืจ - ืืงืฆื ืืช ืืืฉืืื ืืื ื-BGP.
bgp configืฉื ืืืจื *******
ืกืืกืื *******
ืงืืืฅ ืืืื /var/log/bgp.log
!
# ืืกืคืจ AS, ืืชืืืืช ืืจืฉืชืืช ืื ืืืืืื
ื ืชื bgp 12345
bgp router-id 1.2.3.4
ืืืคืืฅ ืืืืฉ ืืืืืจืื
ืืืคืืฅ ืืืืฉ ืกืืื
ืฉืื 1.2.3.1 ืฉืื-ื-12345
ืฉืื 1.2.3.1 ืืื-ืืืค-ืขืฆืื
ืฉืื 1.2.3.1 ืืกืืื-ืืคืช ืืื ื
ืฉืื 1.2.3.1 ืืืฆืื ืืคืช ืืกืืื ืืืืฆื
!
ืืืชืจ ืืืฆืื ืฉื ืจืฉืืืช ืืืฉื 1.2.3.0/24
!
ืืืชืจ ืืืฆืื ืืคืช ืืกืืื 10
ืืชืืื ืืืืฆืื ืืชืืืช ื-IP
!
ืืืืืช ืืืฆืื ืืคืช ืืกืืื 20
ืืื ื ืืฉืื. ืขื ืื ืช ืฉืืฉืจืช ืืืื ืืืงืฉืืช arp, ืขืืื ืืืคืขืื ืืช arp proxy.
echo 1 > /proc/sys/net/ipv4/conf/eth0.800/proxy_arp
ืืืื ื ืืฉืื ืืืื - ืืืงืจืค. ืื ืื ื ืืืชืืื ืืขืฆืื ื ืืช ืชืกืจืืื ืืืฉืงื ืื ืก ืืื.
ืืืืื ืืืคืขืืช ืืืื ืืื
start-stop-daemon --start --exec /usr/sbin/ucarp -b -m -p /opt/ipoe/ucarp-gen2/daemons/$iface.$vhid.$virtualaddr.pid -- --interface=eth0.800 --srcip=1.2.3.4 --vhid=1 --pass=carpasword --addr=10.10.10.1 --upscript=/opt/ipoe/ucarp-gen2/up.sh --downscript=/opt/ipoe/ucarp-gen2/down.sh -z -k 10 -P --xparam="10.10.10.0/24"
up.sh
#!/bin/bash
iface=$1
addr=$2
gw=$3
vlan=`echo $1 | sed "s/eth0.//"`
ip ad ad $addr/32 dev lo
ip ro add blackhole $gw
echo 1 > /proc/sys/net/ipv4/conf/$iface/proxy_arp
killall -9 dhcrelay
/etc/init.d/dhcrelay zap
/etc/init.d/dhcrelay start
killall -HUP arp-rt
ืืืื.ืฉ
#!/bin/bash
iface=$1
addr=$2
gw=$3
ip ad d $addr/32 dev lo
ip ro de blackhole $gw
echo 0 > /proc/sys/net/ipv4/conf/$iface/proxy_arp
killall -9 dhcrelay
/etc/init.d/dhcrelay zap
/etc/init.d/dhcrelay start
ืืื ืฉ-dhcprelay ืืขืืื ืขื ืืืฉืง, ืืื ืฆืจืื ืืชืืืช. ืืื, ืขื ืืืืฉืงืื ืืื ืื ื ืืฉืชืืฉืื ื ืืกืืฃ ืืชืืืืช ืฉืืืืืืช - ืืืฉื 10.255.255.1/32, 10.255.255.2/32 ืืื'. ืื ื ืื ืืืื ืื ืืื ืืืืืืจ ืืช ืืืืกืจ - ืืื ืคืฉืื.
ืื ืื ืืฉ ืื ื? ืืืืื ืฉืขืจืื, ืืืืจื ืืืืืืืืช ืฉื ืืกืืืืื, dhcp. ืื ืืกื ืืืื ืืืื - ืื lisg ืขืืืคืช ืืื ืกืืืื ืืืืจ ืืฉ ืื ื ืืขืฆื. ืืื ืืื ืื ืื ืืจืื ืืืกืืื? ืืื ืื ืงื ืืืชืจ ืืงืืช accel-pppd ืืืืฉืชืืฉ ื-pppoe ืืืืจื? ืื, ืื ืื ืคืฉืื ืืืชืจ - ืื ืฉืื ืืงืืฉื ืืืืืื ืืืื ืืก ืืื patchcord ืื ืชื, ืฉืื ืืืืจ ืขื pppoe. accel-ppp ืื ืืืจ ืืื ืื - ืืื ืื ืื ืขืื ืื ื - ืืฉ ืืจืื ืฉืืืืืช ืืงืื - ืืื ืืชืคืืจืจ, ืืื ืืืชื ืืฆืืจื ืขืงืืื, ืืืืืจ ืืื ืขืฆืื ืืื ืฉืื ืื ืืชืืืจ - ืื ืื ืฉืื ืฆืจืืืื ืืืขืื ืืืืฉ ืืื - ืืืืคืื ืื ืืืืืื - ืื ืื ืขืื ืืืื. ืื ืืืชืจืื ืืฉืืืืฉ ื-ucarp ืืื ื-leived? ืื, ืืื ืืืจ - ืืฉ 100 ืฉืขืจืื, keepalived ืืฉืืืื ืืืช ืืชืฆืืจื - ืืื ืื ืขืืื. ืฉืขืจ 1 ืื ืขืืื ืขื ucarp. ืืืื ืืืืืื ืืืืจืื ืฉืืฉืืืืืื ืืจืฉืื ืืขืฆืื ืืชืืืืช ืืืฉืชืืฉื ืืื ื-share - ืืื ืืฉืืื ืืจืืข ืืื, ืืืืจื ื dhcp-snooping + source-guard + arp inspection ืขื ืื ืืืชืืื/olts/bases. ืื ืืืงืื ืืื dhpc ืืื ืกืืื - acces-list ืืคืืจื.
ืืื ืื ืื ื ืขืฉื? ืืืจืืก ืชื ืืขื ืื ืจืฆืืื. ืขืืฉืื ืืื ืืชื ืืฉ vlan ืืฉืื ื-unknown-unicast ืืืจ ืื ืืคืืื, ืืืืืื ืฉืืื ืฆืจืื ืืืืช ืจืง ืืืฆืืื ืืืช ืืื ืืืืื... ืืืื, ืชืืคืขืืช ืืืืืื ืื ืชืฆืืจืช ืฆืืื ืกืื ืืจืืืช, ืืขืืืืช ืจืื ืืืชืจ ืืืงืฆืืช ืืจืื ืืชืืืืช.
ืืืฆื ืืืืืืจ ืืช lisg ืืื ื ืืฉื ื ืคืจื. ืืฆืืจืคืื ืงืืฉืืจืื ืืกืคืจืืืช. ืืืื ืืืืืจ ืืขืื ืืขืืืจ ืืืืฉืื ืืืฉืืช ืืืจืืชืื. ืืจืกื 6 ืขืืืื ืื ืืืืฉืืช ืืจืฉืช ืฉืื ื - ืืื ืชืืื ืืขืื - ืืฉ ืชืืื ืืืช ืืฉืืชื ืืช lisg ืืืจืกื 6, ืืืืื ืฆืืจื ืืชืงื ืืช ืืชืืื ืืช ืฉืืืกืืคื ืืกืืืืื.
ืืงืืจ: www.habr.com