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
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "sinsp.h"
#include "sinsp_int.h"
sinsp_network_interfaces::sinsp_network_interfaces(sinsp* inspector)
: m_inspector(inspector)
{
if(inet_pton(AF_INET6, "::1", m_ipv6_loopback_addr.m_b) != 1)
{
throw sinsp_exception("Could not convert ipv6 loopback address ::1 to ipv6addr struct");
}
}
sinsp_ipv4_ifinfo::sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t bcast, const char* name)
{
m_addr = addr;
m_netmask = netmask;
m_bcast = bcast;
m_name = name;
}
void sinsp_ipv4_ifinfo::convert_to_string(char * dest, const uint32_t addr)
{
sprintf(
dest,
"%d.%d.%d.%d",
(addr & 0xFF),
((addr & 0xFF00) >> 8),
((addr & 0xFF0000) >> 16),
((addr & 0xFF000000) >> 24));
}
string sinsp_ipv4_ifinfo::address() const
{
char str_addr[16];
convert_to_string(str_addr, m_addr);
return string(str_addr);
}
string sinsp_ipv4_ifinfo::to_string() const
{
char s[100];
char str_addr[16];
char s_netmask[16];
char s_bcast[16];
convert_to_string(str_addr, m_addr);
convert_to_string(s_netmask, m_netmask);
convert_to_string(s_bcast, m_bcast);
snprintf(s, sizeof(s), "%s inet %s netmask %s broadcast %s", m_name.c_str(), str_addr, s_netmask, s_bcast);
return string(s);
}
uint32_t sinsp_network_interfaces::infer_ipv4_address(uint32_t destination_address)
{
vector<sinsp_ipv4_ifinfo>::iterator it;
// first try to find exact match
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if(it->m_addr == destination_address)
{
return it->m_addr;
}
}
// try to find an interface for the same subnet
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if((it->m_addr & it->m_netmask) == (destination_address & it->m_netmask))
{
return it->m_addr;
}
}
// otherwise take the first non loopback interface
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if(it->m_addr != LOOPBACK_ADDR)
{
return it->m_addr;
}
}
return 0;
}
void sinsp_network_interfaces::update_fd(sinsp_fdinfo_t *fd)
{
ipv4tuple *pipv4info = &(fd->m_sockinfo.m_ipv4info);
ipv6tuple *pipv6info = &(fd->m_sockinfo.m_ipv6info);
//
// only handle ipv4/ipv6 udp sockets
//
if(fd->m_type != SCAP_FD_IPV4_SOCK &&
fd->m_type != SCAP_FD_IPV6_SOCK)
{
return;
}
if(fd->m_type == SCAP_FD_IPV4_SOCK)
{
if(0 != pipv4info->m_fields.m_sip && 0 != pipv4info->m_fields.m_dip)
{
return;
}
if(0 == pipv4info->m_fields.m_sip)
{
uint32_t newaddr;
newaddr = infer_ipv4_address(pipv4info->m_fields.m_dip);
if(newaddr == pipv4info->m_fields.m_dip)
{
if(pipv4info->m_fields.m_sport == pipv4info->m_fields.m_dport)
{
return;
}
}
pipv4info->m_fields.m_sip = newaddr;
}
else
{
uint32_t newaddr;
newaddr = infer_ipv4_address(pipv4info->m_fields.m_sip);
if(newaddr == pipv4info->m_fields.m_sip)
{
if(pipv4info->m_fields.m_sport == pipv4info->m_fields.m_dport)
{
return;
}
}
pipv4info->m_fields.m_dip = newaddr;
}
}
else if(fd->m_type == SCAP_FD_IPV6_SOCK)
{
if(ipv6addr::empty_address != pipv6info->m_fields.m_sip &&
ipv6addr::empty_address != pipv6info->m_fields.m_dip)
{
return;
}
if(ipv6addr::empty_address == pipv6info->m_fields.m_sip)
{
ipv6addr newaddr;
newaddr = infer_ipv6_address(pipv6info->m_fields.m_dip);
if(newaddr == pipv6info->m_fields.m_dip)
{
if(pipv6info->m_fields.m_sport == pipv6info->m_fields.m_dport)
{
return;
}
}
pipv6info->m_fields.m_sip = newaddr;
}
else
{
ipv6addr newaddr;
newaddr = infer_ipv6_address(pipv6info->m_fields.m_sip);
if(newaddr == pipv6info->m_fields.m_sip)
{
if(pipv6info->m_fields.m_sport == pipv6info->m_fields.m_dport)
{
return;
}
}
pipv6info->m_fields.m_dip = newaddr;
}
}
}
bool sinsp_network_interfaces::is_ipv4addr_in_subnet(uint32_t addr)
{
vector<sinsp_ipv4_ifinfo>::iterator it;
//
// Accept everything that comes from 192.168.0.0/16 or 10.0.0.0/8
//
if((addr & 0x000000ff) == 0x0000000a ||
(addr & 0x0000ffff) == 0x0000a8c0 ||
(addr & 0x00003fff) == 0x000010ac)
{
return true;
}
// try to find an interface for the same subnet
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if((it->m_addr & it->m_netmask) == (addr & it->m_netmask))
{
return true;
}
}
return false;
}
bool sinsp_network_interfaces::is_ipv4addr_in_local_machine(uint32_t addr, sinsp_threadinfo* tinfo)
{
if(!tinfo->m_container_id.empty())
{
const sinsp_container_info::ptr_t container_info =
m_inspector->m_container_manager.get_container(tinfo->m_container_id);
//
// Note: if we don't have container info, any pick we make is arbitrary.
// To at least achieve consistency across client and server, we just match the host interface addresses.
//
if(container_info)
{
if(container_info->m_container_ip != 0)
{
//
// We have a container info with a valid container IP. Let's use it.
//
if(addr == htonl(container_info->m_container_ip))
{
return true;
}
}
else
{
//
// Container info is valid, but the IP address is zero.
// Scan the list of the containers looking for matches.
// If no match is found, we just jump to checking the
// host interfaces.
//
if(!container_info->is_successful())
{
g_logger.format(sinsp_logger::SEV_DEBUG,
"Checking IP address of container %s with incomplete metadata (state=%d)",
tinfo->m_container_id.c_str(), container_info->m_lookup_state);
}
const sinsp_container_manager::map_ptr_t clist = m_inspector->m_container_manager.get_containers();
for(const auto& it : *clist)
{
if(!it.second->is_successful())
{
g_logger.format(sinsp_logger::SEV_DEBUG,
"Checking IP address of container %s with incomplete metadata (in context of %s; state=%d)",
it.second->m_id.c_str(), tinfo->m_container_id.c_str(),
it.second->m_lookup_state);
}
if(htonl(it.second->m_container_ip) == addr)
{
return true;
}
}
}
}
}
vector<sinsp_ipv4_ifinfo>::iterator it;
// try to find an interface that has the given IP as address
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if(it->m_addr == addr)
{
return true;
}
}
return false;
}
void sinsp_network_interfaces::import_ipv4_ifaddr_list(uint32_t count, scap_ifinfo_ipv4* plist)
{
if (count == 0)
{
return;
}
for(uint32_t j = 0; j < count; j++)
{
sinsp_ipv4_ifinfo info;
info.m_addr = plist->addr;
info.m_netmask = plist->netmask;
info.m_bcast = plist->bcast;
info.m_name = plist->ifname;
m_ipv4_interfaces.push_back(info);
plist++;
}
}
ipv6addr sinsp_network_interfaces::infer_ipv6_address(ipv6addr &destination_address)
{
vector<sinsp_ipv6_ifinfo>::iterator it;
// first try to find exact match
for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++)
{
if(destination_address == it->m_net)
{
return it->m_net;
}
}
// try to find an interface for the same subnet
for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++)
{
if(it->m_net.in_subnet(destination_address))
{
return it->m_net;
}
}
// otherwise take the first non loopback interface
for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++)
{
if(it->m_net != m_ipv6_loopback_addr)
{
return it->m_net;
}
}
return ipv6addr::empty_address;
}
bool sinsp_network_interfaces::is_ipv6addr_in_local_machine(ipv6addr &addr, sinsp_threadinfo* tinfo)
{
if(!tinfo->m_container_id.empty())
{
// For now, not supporting ipv6 networking for containers. So always return false;
return false;
}
vector<sinsp_ipv6_ifinfo>::iterator it;
// try to find an interface that has the given IP as address
for(it = m_ipv6_interfaces.begin(); it != m_ipv6_interfaces.end(); it++)
{
if(addr.in_subnet(it->m_net))
{
return true;
}
}
return false;
}
void sinsp_network_interfaces::import_ipv6_ifaddr_list(uint32_t count, scap_ifinfo_ipv6* plist)
{
if (count == 0)
{
return;
}
for(uint32_t j = 0; j < count; j++)
{
sinsp_ipv6_ifinfo info;
// Only saving the address portion. (Assumes
// convention of first 48 bits for network, next 16
// bits for subnet).
memcpy(info.m_net.m_b, plist->addr, SCAP_IPV6_ADDR_LEN);
info.m_name = plist->ifname;
m_ipv6_interfaces.push_back(info);
plist++;
}
}
void sinsp_network_interfaces::import_interfaces(scap_addrlist* paddrlist)
{
if(NULL != paddrlist)
{
clear();
import_ipv4_ifaddr_list(paddrlist->n_v4_addrs, paddrlist->v4list);
import_ipv6_ifaddr_list(paddrlist->n_v6_addrs, paddrlist->v6list);
}
}
void sinsp_network_interfaces::import_ipv4_interface(const sinsp_ipv4_ifinfo& ifinfo)
{
m_ipv4_interfaces.push_back(ifinfo);
}
void sinsp_network_interfaces::import_ipv6_interface(const sinsp_ipv6_ifinfo& ifinfo)
{
m_ipv6_interfaces.push_back(ifinfo);
}
vector<sinsp_ipv4_ifinfo>* sinsp_network_interfaces::get_ipv4_list()
{
return &m_ipv4_interfaces;
}
vector<sinsp_ipv6_ifinfo>* sinsp_network_interfaces::get_ipv6_list()
{
return &m_ipv6_interfaces;
}
|