~ubuntu-branches/ubuntu/trusty/ntop/trusty

« back to all changes in this revision

Viewing changes to pbuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2008-06-15 14:38:28 UTC
  • mfrom: (2.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615143828-oalh84nda2hje4do
Tags: 3:3.3-11
Correction of Polish translation encoding, closes: #479490. Thanks
to Christian Perrier <bubulle@debian.org> for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
3
 *                          http://www.ntop.org
4
4
 *
5
 
 * Copyright (C) 1998-2005 Luca Deri <deri@ntop.org>
 
5
 * Copyright (C) 1998-2007 Luca Deri <deri@ntop.org>
6
6
 *
7
7
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
8
8
 *
63
63
static u_char ethBroadcast[] = { 255, 255, 255, 255, 255, 255 };
64
64
static u_char lowMemoryMsgShown = 0;
65
65
 
 
66
static void     updateASTraffic(int actualDeviceId, u_int16_t src_as_id,
 
67
                                                    u_int16_t dst_as_id, u_int octets);
 
68
 
66
69
/* ******************************* */
67
70
 
68
71
static u_int32_t getFcProtocol (u_int8_t r_ctl, u_int8_t type)
149
152
int handleIP(u_short port, HostTraffic *srcHost, HostTraffic *dstHost,
150
153
             const u_int _length, u_short isPassiveSess,
151
154
             u_short isVoipSess,
152
 
             u_short p2pSessionIdx, int actualDeviceId, 
 
155
             u_short p2pSessionIdx, int actualDeviceId,
153
156
             u_short newSession) {
154
157
  int idx;
155
158
  Counter length = (Counter)_length;
605
608
    incrementUsageCounter(&srcHost->contactedRouters, router, actualDeviceId);
606
609
 
607
610
#ifdef DEBUG
608
 
    traceEvent(CONST_TRACE_INFO, "(%s/%s/%s) -> (%s/%s/%s) routed by [idx=%d/%s/%s/%s]",
 
611
    traceEvent(CONST_TRACE_INFO, "(%s/%s/%s) -> (%s/%s/%s) routed by [%s/%s/%s]",
609
612
               srcHost->ethAddressString, srcHost->hostNumIpAddress, srcHost->hostResolvedName,
610
613
               dstHost->ethAddressString, dstHost->hostNumIpAddress, dstHost->hostResolvedName,
611
614
               router->ethAddressString,
652
655
    return;
653
656
  }
654
657
 
 
658
  updateASTraffic(actualDeviceId, srcHost->hostAS, dstHost->hostAS, length.value);
 
659
 
655
660
  if (!myGlobals.runningPref.printIpOnly) {
656
661
      if (srcHost == dstHost) {
657
662
          /* Fabric controllers exchange link messages where the S_ID & D_ID
718
723
      incrementTrafficCounter(&srcHost->bytesBroadcastSent, length.value);
719
724
    }
720
725
    incrementTrafficCounter(&myGlobals.device[actualDeviceId].broadcastPkts, numPkts);
721
 
  } else if(isMulticastAddress(&(dstHost->hostIpAddress))) {
 
726
  } else if(isMulticastAddress(&(dstHost->hostIpAddress), NULL, NULL)) {
722
727
#ifdef DEBUG
723
728
    traceEvent(CONST_TRACE_INFO, "%s->%s",
724
729
               srcHost->hostResolvedName, dstHost->hostResolvedName);
794
799
 
795
800
  accessMutex(&myGlobals.purgePortsMutex, "updateInterfacePorts");
796
801
 
 
802
  if(myGlobals.device[actualDeviceId].ipPorts == NULL)
 
803
    allocDeviceMemory(actualDeviceId);
 
804
 
797
805
  if(myGlobals.device[actualDeviceId].ipPorts[sport] == NULL) {
798
806
    myGlobals.device[actualDeviceId].ipPorts[sport] = (PortCounter*)malloc(sizeof(PortCounter));
799
807
    if(myGlobals.device[actualDeviceId].ipPorts[sport] == NULL) return;
925
933
 
926
934
/* ************************************ */
927
935
 
 
936
static AsStats* allocASStats(u_int16_t as_id) {
 
937
  AsStats *asStats = (AsStats*)malloc(sizeof(AsStats));
 
938
 
 
939
  if(0) traceEvent(CONST_TRACE_WARNING, "Allocating stats for AS %d", as_id);
 
940
 
 
941
  if(asStats != NULL) {
 
942
    memset(asStats, 0, sizeof(AsStats));
 
943
    asStats->as_id = as_id;
 
944
    resetTrafficCounter(&asStats->outBytes);
 
945
    resetTrafficCounter(&asStats->outPkts);
 
946
    resetTrafficCounter(&asStats->inBytes);
 
947
    resetTrafficCounter(&asStats->inPkts);
 
948
    resetTrafficCounter(&asStats->selfBytes);
 
949
    resetTrafficCounter(&asStats->selfPkts);
 
950
  }
 
951
 
 
952
  return(asStats);
 
953
}
 
954
 
 
955
/* ************************************ */
 
956
 
 
957
static void updateASTraffic(int actualDeviceId, u_int16_t src_as_id,
 
958
                                                        u_int16_t dst_as_id, u_int octets) {
 
959
  AsStats *stats, *prev_stats = NULL;
 
960
  u_char found_src = 0, found_dst = 0;
 
961
 
 
962
  if(0) 
 
963
    traceEvent(CONST_TRACE_INFO, "updateASTraffic(actualDeviceId=%d, src_as_id=%d, dst_as_id=%d, octets=%d)",
 
964
               actualDeviceId, src_as_id, dst_as_id, octets);
 
965
 
 
966
  if((src_as_id == 0) && (dst_as_id == 0))
 
967
    return;
 
968
 
 
969
  accessMutex(&myGlobals.device[actualDeviceId].asMutex, "updateASTraffic");
 
970
 
 
971
  stats = myGlobals.device[actualDeviceId].asStats;
 
972
 
 
973
  while(stats) {
 
974
    if(stats->as_id == src_as_id) {
 
975
      stats->lastUpdate = myGlobals.actTime;
 
976
      incrementTrafficCounter(&stats->outBytes, octets), incrementTrafficCounter(&stats->outPkts, 1), stats->totPktsSinceLastRRDDump++;
 
977
      if(src_as_id == dst_as_id) {
 
978
        incrementTrafficCounter(&stats->selfBytes, octets), incrementTrafficCounter(&stats->selfPkts, 1);
 
979
        releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
980
        return;
 
981
      }
 
982
 
 
983
      if(dst_as_id == 0) {
 
984
        releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
985
        return;
 
986
      } else
 
987
        found_src = 1;
 
988
 
 
989
    } else if(stats->as_id == dst_as_id) {
 
990
      stats->lastUpdate = myGlobals.actTime;
 
991
      incrementTrafficCounter(&stats->inBytes, octets), incrementTrafficCounter(&stats->inPkts, 1), stats->totPktsSinceLastRRDDump++;
 
992
      if(src_as_id == dst_as_id) {
 
993
        incrementTrafficCounter(&stats->selfBytes, octets), incrementTrafficCounter(&stats->selfPkts, 1);
 
994
        releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
995
        return;
 
996
      }
 
997
 
 
998
      if(src_as_id == 0) {
 
999
        releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
1000
        return;
 
1001
      } else
 
1002
        found_dst = 1;
 
1003
    }
 
1004
 
 
1005
    if(found_src && found_dst) {
 
1006
      releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
1007
      return;
 
1008
    }
 
1009
 
 
1010
    if((myGlobals.actTime-stats->lastUpdate) > PARM_AS_MAXIMUM_IDLE) {
 
1011
      AsStats *next = stats->next;
 
1012
            
 
1013
      if(0) traceEvent(CONST_TRACE_INFO, "Purging stats about AS %d", stats->as_id);
 
1014
      if(prev_stats == NULL)
 
1015
        myGlobals.device[actualDeviceId].asStats = next;
 
1016
      else
 
1017
        prev_stats->next = next;
 
1018
      
 
1019
      free(stats);
 
1020
      stats = next;      
 
1021
    } else {
 
1022
      prev_stats = stats;
 
1023
      stats = stats->next;
 
1024
    }
 
1025
  } /* while */
 
1026
 
 
1027
  /* One (or both) ASs are missing */
 
1028
  if((src_as_id != 0) && (!found_src)) {
 
1029
    stats = allocASStats(src_as_id);
 
1030
    stats->next = myGlobals.device[actualDeviceId].asStats;
 
1031
    stats->lastUpdate = myGlobals.actTime;
 
1032
    myGlobals.device[actualDeviceId].asStats = stats;
 
1033
  }
 
1034
 
 
1035
  if((dst_as_id != 0) && (dst_as_id != src_as_id) && (!found_dst)) {
 
1036
    stats = allocASStats(dst_as_id);
 
1037
    stats->next = myGlobals.device[actualDeviceId].asStats;
 
1038
    stats->lastUpdate = myGlobals.actTime;
 
1039
    myGlobals.device[actualDeviceId].asStats = stats;
 
1040
  }
 
1041
 
 
1042
  releaseMutex(&myGlobals.device[actualDeviceId].asMutex);
 
1043
 
 
1044
  updateASTraffic(actualDeviceId, src_as_id, dst_as_id, octets);
 
1045
}
 
1046
 
 
1047
/* ************************************ */
 
1048
 
928
1049
static void processIpPkt(const u_char *bp,
929
1050
                         const struct pcap_pkthdr *h,
930
1051
                         u_int length,
987
1108
#ifdef INET6
988
1109
  if(ip6 == NULL)
989
1110
#endif
990
 
    if((bp != NULL) && (in_cksum((const u_short *)bp, hlen, 0) != 0)) {
 
1111
    if((bp != NULL)
 
1112
       && (myGlobals.device[actualDeviceId].datalink != DLT_NULL)
 
1113
       && (in_cksum((const u_short *)bp, hlen, 0) != 0)
 
1114
       ) {
991
1115
      incrementTrafficCounter(&myGlobals.device[actualDeviceId].rcvdPktStats.badChecksum, 1);
992
1116
      return;
993
1117
    }
1057
1181
  if(ip6 == NULL) {
1058
1182
#endif
1059
1183
    if((!myGlobals.runningPref.dontTrustMACaddr)
1060
 
       && isBroadcastAddress(&dstAddr)
 
1184
       && isBroadcastAddress(&dstAddr, NULL, NULL)
1061
1185
       && (ether_src != NULL) && (ether_dst != NULL) /* PPP has no ethernet */
1062
1186
       && (memcmp(ether_dst, ethBroadcast, 6) != 0)) {
1063
1187
      /* forceUsingIPaddress = 1; */
1064
1188
 
1065
1189
      srcHost = lookupHost(NULL, ether_src, vlanId, 0, 0, actualDeviceId);
1066
1190
      if(srcHost != NULL) {
1067
 
        if(vlanId != -1) srcHost->vlanId = vlanId;
 
1191
        if(vlanId != NO_VLAN) srcHost->vlanId = vlanId;
1068
1192
        if(myGlobals.runningPref.enableSuspiciousPacketDump && (!hasWrongNetmask(srcHost))) {
1069
1193
          /* Dump the first packet only */
1070
1194
          char etherbuf[LEN_ETHERNET_ADDRESS_DISPLAY];
1097
1221
                         Don't check for multihoming when
1098
1222
                         the destination address is a broadcast address
1099
1223
                       */
1100
 
                       (!isBroadcastAddress(&dstAddr)),
 
1224
                       (!isBroadcastAddress(&dstAddr, NULL, NULL)),
1101
1225
                       forceUsingIPaddress, actualDeviceId);
1102
1226
 
1103
1227
  if(srcHost == NULL) {
1109
1233
               srcHost to be freed */
1110
1234
  }
1111
1235
 
1112
 
  if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
1236
  if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
1113
1237
 
1114
1238
#ifdef DEBUG
1115
1239
  if(myGlobals.runningPref.rFileName != NULL) {
1235
1359
    }
1236
1360
  }
1237
1361
 
 
1362
 
1238
1363
#ifdef INET6
1239
1364
  if(ip6) {
1240
1365
    advance = sizeof(struct ip6_hdr);
1340
1465
        char WSS[3], _MSS[5];
1341
1466
        struct tcphdr *tcp = (struct tcphdr*)(bp+hlen);
1342
1467
        u_char *tcp_opt = (u_char *)(tcp + 1);
1343
 
        u_char *tcp_data = (u_char *)((int)tcp + tp.th_off * 4);
 
1468
        u_char *tcp_data = (u_char *)(tcp + tp.th_off * 4);
1344
1469
 
1345
 
        if(tcp->th_flags & TH_SYN) {   /* only SYN or SYN-2ACK packets */         
 
1470
        if(tcp->th_flags & TH_SYN) {   /* only SYN or SYN-2ACK packets */
1346
1471
          if(tcpUdpLen > 0) {
1347
1472
#ifdef INET6
1348
1473
            if(ip6) {
1353
1478
 
1354
1479
            WIN = ntohs(tcp->th_win);  /* TCP window size */
1355
1480
 
1356
 
            if(tcp_data != tcp_opt) { /* there are some tcp_option to be parsed */              
1357
 
              u_char *opt_ptr = tcp_opt;
 
1481
            if(tcp_data != tcp_opt) { /* there are some tcp_option to be parsed */
 
1482
              u_char *opt_start = tcp_opt, *opt_end = tcp_data;
 
1483
              u_short num_loops = 0;
1358
1484
 
1359
 
              while(opt_ptr < tcp_data) {
1360
 
                switch(*opt_ptr) {
 
1485
              while(opt_start < opt_end) {
 
1486
                switch(*opt_start) {
1361
1487
                case TCPOPT_EOL:        /* end option: exit */
1362
 
                  opt_ptr = tcp_data;
 
1488
                  opt_start = opt_end;
1363
1489
                  break;
1364
1490
                case TCPOPT_NOP:
1365
1491
                  N = 1;
1366
 
                  opt_ptr++;
 
1492
                  opt_start++;
1367
1493
                  break;
1368
1494
                case TCPOPT_SACKOK:
1369
1495
                  S = 1;
1370
 
                  opt_ptr += 2;
 
1496
                  opt_start += 2;
1371
1497
                  break;
1372
1498
                case TCPOPT_MAXSEG:
1373
 
                  opt_ptr += 2;
1374
 
                  MSS = ntohs(ptohs(opt_ptr));
1375
 
                  opt_ptr += 2;
 
1499
                  opt_start += 2;
 
1500
                  MSS = ntohs(ptohs(opt_start));
 
1501
                  opt_start += 2;
1376
1502
                  break;
1377
1503
                case TCPOPT_WSCALE:
1378
 
                  opt_ptr += 2;
1379
 
                  WS = *opt_ptr;
1380
 
                  opt_ptr++;
 
1504
                  opt_start += 2;
 
1505
                  WS = *opt_start;
 
1506
                  opt_start++;
1381
1507
                  break;
1382
1508
                case TCPOPT_TIMESTAMP:
1383
1509
                  T = 1;
1384
 
                  opt_ptr++;
1385
 
                  opt_ptr += (*opt_ptr - 1);
 
1510
                  opt_start++;
 
1511
                  opt_start += (*opt_start - 1);
1386
1512
                  break;
1387
1513
                default:
1388
 
                  opt_ptr++;
1389
 
                  if(*opt_ptr > 0)
1390
 
                    opt_ptr += (*opt_ptr - 1);
1391
 
                  break;
1392
 
                }
1393
 
              }
 
1514
                  opt_start++;
 
1515
                  if(*opt_start > 0)
 
1516
                    opt_start += (*opt_start - 1);
 
1517
                  break;
 
1518
                }
 
1519
 
 
1520
                num_loops++;
 
1521
                if(num_loops > 16) {
 
1522
                  /* Suspicious packet: maybe the TCP options are wrong */
 
1523
                  break;
 
1524
                }
 
1525
              }      
1394
1526
            }
1395
1527
 
1396
 
            if(WS == -1) { safe_snprintf(__FILE__, __LINE__, WSS, sizeof(WSS), "WS"); }
1397
 
            else { safe_snprintf(__FILE__, __LINE__, WSS, sizeof(WSS), "%02d", WS); }
 
1528
            if(WS == -1)
 
1529
              safe_snprintf(__FILE__, __LINE__, WSS, sizeof(WSS), "WS");
 
1530
            else
 
1531
              safe_snprintf(__FILE__, __LINE__, WSS, sizeof(WSS), "%02X", WS & 0xFFFF);
1398
1532
 
1399
 
            if(MSS == -1) { safe_snprintf(__FILE__, __LINE__, _MSS, sizeof(_MSS), "_MSS"); }
1400
 
            else { safe_snprintf(__FILE__, __LINE__, _MSS, sizeof(_MSS), "%04X", MSS); }
 
1533
            if(MSS == -1)
 
1534
              safe_snprintf(__FILE__, __LINE__, _MSS, sizeof(_MSS), "_MSS");
 
1535
            else
 
1536
              safe_snprintf(__FILE__, __LINE__, _MSS, sizeof(_MSS), "%04X", MSS & 0xFFFFFFFF);
1401
1537
 
1402
1538
            safe_snprintf(__FILE__, __LINE__, fingerprint, sizeof(fingerprint),
1403
1539
                          "%04X:%s:%02X:%s:%d:%d:%d:%d:%c:%02X",
1456
1592
          if(ip6)
1457
1593
            theSession = handleSession(h, fragmented, tp.th_win,
1458
1594
                                       srcHost, sport, dstHost,
1459
 
                                       dport, ntohs(ip6->ip6_plen), &tp, 
 
1595
                                       dport, ntohs(ip6->ip6_plen), &tp,
1460
1596
                                       tcpDataLength,
1461
 
                                       theData, actualDeviceId, &newSession);
 
1597
                                       theData, actualDeviceId, &newSession, 1);
1462
1598
          else
1463
1599
#endif
1464
1600
            theSession = handleSession(h, (off & 0x3fff), tp.th_win,
1465
1601
                                       srcHost, sport, dstHost,
1466
 
                                       dport, ip_len, &tp, 
 
1602
                                       dport, ip_len, &tp,
1467
1603
                                       tcpDataLength,
1468
 
                                       theData, actualDeviceId, &newSession);
 
1604
                                       theData, actualDeviceId, &newSession, 1);
1469
1605
          if(theSession == NULL)
1470
1606
            isPassiveSess = isVoipSess = 0;
1471
1607
          else {
1484
1620
          */
1485
1621
          dumpOtherPacket(actualDeviceId);
1486
1622
        }
1487
 
        
 
1623
 
1488
1624
        /* choose most likely port for protocol traffic accounting
1489
1625
         * by trying lower number port first. This is based
1490
1626
         * on the assumption that lower port numbers are more likely
1526
1662
    else
1527
1663
#endif
1528
1664
      break;
1529
 
    
 
1665
 
1530
1666
  case IPPROTO_UDP:
1531
1667
    proto = "UDP";
1532
1668
    incrementTrafficCounter(&myGlobals.device[actualDeviceId].udpBytes, length);
1533
 
    incrementTrafficCounter(&myGlobals.device[actualDeviceId].udpGlobalTrafficStats.totalFlows, 1);  
 
1669
    incrementTrafficCounter(&myGlobals.device[actualDeviceId].udpGlobalTrafficStats.totalFlows, 1);
1534
1670
 
1535
1671
    if(tcpUdpLen < sizeof(struct udphdr)) {
1536
1672
      if(myGlobals.runningPref.enableSuspiciousPacketDump) {
1557
1693
           || ((sport == 5353) && (dport == 5353)) /* Multicast DNS */) {
1558
1694
          short isRequest = 0, positiveReply = 0;
1559
1695
          u_int16_t transactionId = 0;
1560
 
          
 
1696
 
1561
1697
          if(myGlobals.runningPref.enablePacketDecoding
1562
1698
             && (bp != NULL) /* packet long enough */) {
1563
1699
            /* The DNS chain will be checked here */
1740
1876
        sportIdx = mapGlobalToLocalIdx(sport), dportIdx = mapGlobalToLocalIdx(dport);
1741
1877
 
1742
1878
        if((myGlobals.runningPref.enableOtherPacketDump) && ((sportIdx == -1) && (dportIdx == -1))) {
1743
 
            /* 
1744
 
               Both source & destination port are unknown. 
 
1879
            /*
 
1880
               Both source & destination port are unknown.
1745
1881
               The packet will be counted to "Other TCP/UDP prot.".
1746
1882
               We dump the packet if requested */
1747
1883
            dumpOtherPacket(actualDeviceId);
1756
1892
                                        dport, ntohs(ip6->ip6_plen), NULL,
1757
1893
                                        udpDataLength,
1758
1894
                                        (u_char*)(bp+hlen+sizeof(struct udphdr)),
1759
 
                                        actualDeviceId, &newSession);
 
1895
                                        actualDeviceId, &newSession, 1);
1760
1896
          else
1761
1897
#endif
1762
1898
            theSession =  handleSession (h, (off & 0x3fff), 0,
1763
1899
                                         srcHost, sport, dstHost,
1764
1900
                                         dport, ip_len, NULL, udpDataLength,
1765
1901
                                         (u_char*)(bp+hlen+sizeof(struct udphdr)),
1766
 
                                         actualDeviceId, &newSession);
1767
 
        } 
 
1902
                                         actualDeviceId, &newSession, 1);
 
1903
        }
1768
1904
 
1769
1905
        isPassiveSess = 0;
1770
1906
 
1771
1907
        if(theSession == NULL)
1772
1908
          isVoipSess = 0;
1773
1909
        else
1774
 
          isVoipSess = theSession->voipSession; 
 
1910
          isVoipSess = theSession->voipSession;
1775
1911
 
1776
1912
        newSession = 1; /* Trick to account flows anyway */
1777
1913
 
1789
1925
           (BMS 12-2001)
1790
1926
        */
1791
1927
        if(dport < sport) {
1792
 
          if(handleIP(dport, srcHost, dstHost, length, isPassiveSess, isVoipSess, 
 
1928
          if(handleIP(dport, srcHost, dstHost, length, isPassiveSess, isVoipSess,
1793
1929
                      0, actualDeviceId, newSession) == -1)
1794
 
            handleIP(sport, srcHost, dstHost, length, isPassiveSess, isVoipSess, 
 
1930
            handleIP(sport, srcHost, dstHost, length, isPassiveSess, isVoipSess,
1795
1931
                     0, actualDeviceId, newSession);
1796
1932
        } else {
1797
 
          if(handleIP(sport, srcHost, dstHost, length, isPassiveSess, isVoipSess, 
 
1933
          if(handleIP(sport, srcHost, dstHost, length, isPassiveSess, isVoipSess,
1798
1934
                      0, actualDeviceId, newSession) == -1)
1799
 
            handleIP(dport, srcHost, dstHost, length, isPassiveSess, isVoipSess, 
 
1935
            handleIP(dport, srcHost, dstHost, length, isPassiveSess, isVoipSess,
1800
1936
                     0, actualDeviceId, newSession);
1801
1937
        }
1802
1938
      }
2227
2363
 
2228
2364
/* ************************************ */
2229
2365
 
 
2366
#undef DEBUG
 
2367
 
2230
2368
void queuePacket(u_char *_deviceId,
2231
2369
                 const struct pcap_pkthdr *h,
2232
2370
                 const u_char *p) {
2260
2398
 
2261
2399
  if(myGlobals.ntopRunState > FLAG_NTOPSTATE_RUN) return;
2262
2400
 
2263
 
#ifdef DEBUG
2264
 
  traceEvent(CONST_TRACE_INFO, "Got packet from %s (%d)", myGlobals.device[*_deviceId].name, *_deviceId);
2265
 
#endif
2266
 
 
2267
 
  deviceId = (int)_deviceId;
 
2401
  deviceId = (int)((long)_deviceId);
2268
2402
  actDeviceId = getActualInterface(deviceId);
2269
2403
  incrementTrafficCounter(&myGlobals.device[actDeviceId].receivedPkts, 1);
2270
 
  
2271
 
  if(myGlobals.device[actDeviceId].samplingRate > 1) {
2272
 
    if(myGlobals.device[actDeviceId].droppedSamples < myGlobals.device[actDeviceId].samplingRate) {
2273
 
      myGlobals.device[actDeviceId].droppedSamples++;      
2274
 
      return; /* Not enough samples received */
2275
 
    } else 
2276
 
      myGlobals.device[actDeviceId].droppedSamples = 0;
 
2404
 
 
2405
  /* We assume that if there's a packet to queue for the sFlow interface
 
2406
     then this has been queued by the sFlow plugins, while it was
 
2407
     probably handling a queued packet */
 
2408
 
 
2409
#ifdef DEBUG
 
2410
  traceEvent(CONST_TRACE_INFO, "queuePacket: got packet from %s (%d)",
 
2411
             myGlobals.device[deviceId].name, deviceId);
 
2412
#endif
 
2413
 
 
2414
  /* We don't sample on sFlow sampled interfaces */
 
2415
  if(myGlobals.device[deviceId].sflowGlobals == NULL) {
 
2416
    if(myGlobals.device[actDeviceId].samplingRate > 1) {
 
2417
      if(myGlobals.device[actDeviceId].droppedSamples < myGlobals.device[actDeviceId].samplingRate) {
 
2418
        myGlobals.device[actDeviceId].droppedSamples++;
 
2419
        return; /* Not enough samples received */
 
2420
      } else
 
2421
        myGlobals.device[actDeviceId].droppedSamples = 0;
 
2422
    }
2277
2423
  }
2278
2424
 
2279
2425
  if(myGlobals.runningPref.dontTrustMACaddr && (h->len <= 64)) {
2282
2428
    return;
2283
2429
  }
2284
2430
 
2285
 
  if(tryLockMutex(&myGlobals.packetProcessMutex, "queuePacket") == 0) {
 
2431
  if(tryLockMutex(&myGlobals.device[deviceId].packetProcessMutex, "queuePacket") == 0) {
2286
2432
    /* Locked so we can process the packet now */
2287
2433
    u_char p1[MAX_PACKET_LEN];
2288
2434
 
2296
2442
        if(len >= DEFAULT_SNAPLEN) len = DEFAULT_SNAPLEN-1;
2297
2443
    }
2298
2444
 
2299
 
    if(h->len >= MAX_PACKET_LEN) {
2300
 
      traceEvent(CONST_TRACE_WARNING, "packet truncated (%d->%d)", h->len, MAX_PACKET_LEN);
2301
 
      ((struct pcap_pkthdr*)h)->len = MAX_PACKET_LEN-1;
2302
 
    }
2303
 
 
2304
 
    if(len >= MAX_PACKET_LEN) {
2305
 
      bpf_u_int32 *caplen = (bpf_u_int32*)&(h->caplen);
2306
 
 
2307
 
      len = MAX_PACKET_LEN-1;
2308
 
      /*
2309
 
         Trick needed to avoid compilation errors
2310
 
         as caplen is defined as 'const'
2311
 
      */
2312
 
      (*caplen) = len;
 
2445
    if(h->caplen >= MAX_PACKET_LEN) {
 
2446
      if(h->caplen > myGlobals.device[deviceId].mtuSize) {
 
2447
        traceEvent(CONST_TRACE_WARNING, "packet truncated (%d->%d)",
 
2448
                   h->len, MAX_PACKET_LEN);
 
2449
      }
 
2450
 
 
2451
      ((struct pcap_pkthdr*)h)->caplen = MAX_PACKET_LEN-1;
2313
2452
    }
2314
2453
 
2315
2454
    memcpy(p1, p, len);
2316
2455
 
2317
2456
    processPacket(_deviceId, h, p1);
2318
 
    releaseMutex(&myGlobals.packetProcessMutex);
 
2457
    releaseMutex(&myGlobals.device[deviceId].packetProcessMutex);
2319
2458
    return;
2320
2459
  }
2321
2460
 
2322
2461
  /*
2323
 
    If we reach this point it means that somebody was already processing
2324
 
    a packet so we need to queue it
 
2462
    If we reach this point it means that somebody was already
 
2463
    processing a packet so we need to queue it.
2325
2464
  */
2326
 
  if(myGlobals.packetQueueLen >= CONST_PACKET_QUEUE_LENGTH) {
 
2465
  if(myGlobals.device[deviceId].packetQueueLen >= CONST_PACKET_QUEUE_LENGTH) {
2327
2466
#ifdef DEBUG
2328
 
    traceEvent(CONST_TRACE_INFO, "Dropping packet!!! [packet queue=%d/max=%d]",
2329
 
               myGlobals.packetQueueLen, myGlobals.maxPacketQueueLen);
 
2467
    traceEvent(CONST_TRACE_INFO, "Dropping packet [packet queue=%d/max=%d][id=%d]",
 
2468
               myGlobals.device[deviceId].packetQueueLen, myGlobals.maxPacketQueueLen, deviceId);
2330
2469
#endif
2331
2470
 
2332
2471
    myGlobals.receivedPacketsLostQ++;
2333
 
 
2334
2472
    incrementTrafficCounter(&myGlobals.device[getActualInterface(deviceId)].droppedPkts, 1);
2335
 
 
2336
2473
    ntop_conditional_sched_yield(); /* Allow other threads (dequeue) to run */
2337
2474
    sleep(1);
2338
2475
  } else {
2339
2476
#ifdef DEBUG
2340
2477
    traceEvent(CONST_TRACE_INFO, "About to queue packet... ");
2341
2478
#endif
2342
 
    accessMutex(&myGlobals.packetQueueMutex, "queuePacket");
 
2479
    accessMutex(&myGlobals.device[deviceId].packetQueueMutex, "queuePacket");
2343
2480
    myGlobals.receivedPacketsQueued++;
2344
 
    memcpy(&myGlobals.packetQueue[myGlobals.packetQueueHead].h, h, sizeof(struct pcap_pkthdr));
2345
 
    memset(myGlobals.packetQueue[myGlobals.packetQueueHead].p, 0,
2346
 
           sizeof(myGlobals.packetQueue[myGlobals.packetQueueHead].p));
 
2481
    memcpy(&myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].h,
 
2482
           h, sizeof(struct pcap_pkthdr));
 
2483
    memset(myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].p, 0,
 
2484
           sizeof(myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].p));
2347
2485
    /* Just to be safe */
2348
2486
    len = h->caplen;
2349
 
    if (myGlobals.runningPref.printIpOnly) {
2350
 
        if(len >= DEFAULT_SNAPLEN) len = DEFAULT_SNAPLEN-1;
2351
 
        memcpy(myGlobals.packetQueue[myGlobals.packetQueueHead].p, p, len);
2352
 
        myGlobals.packetQueue[myGlobals.packetQueueHead].h.caplen = len;
2353
 
    }
2354
 
    else {
2355
 
        memcpy(myGlobals.packetQueue[myGlobals.packetQueueHead].p, p, len);
2356
 
        myGlobals.packetQueue[myGlobals.packetQueueHead].h.caplen = len;
 
2487
    if(myGlobals.runningPref.printIpOnly) {
 
2488
      if(len >= DEFAULT_SNAPLEN) len = DEFAULT_SNAPLEN-1;
 
2489
      memcpy(myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].p, p, len);
 
2490
      myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].h.caplen = len;
 
2491
    } else {
 
2492
      memcpy(myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].p, p, len);
 
2493
      myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].h.caplen = len;
2357
2494
    }
2358
2495
 
2359
 
    myGlobals.packetQueue[myGlobals.packetQueueHead].deviceId = (int)((void*)_deviceId);
2360
 
    myGlobals.packetQueueHead = (myGlobals.packetQueueHead+1) % CONST_PACKET_QUEUE_LENGTH;
2361
 
    myGlobals.packetQueueLen++;
2362
 
    if(myGlobals.packetQueueLen > myGlobals.maxPacketQueueLen)
2363
 
      myGlobals.maxPacketQueueLen = myGlobals.packetQueueLen;
2364
 
    releaseMutex(&myGlobals.packetQueueMutex);
 
2496
    myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueHead].deviceId = 
 
2497
      (int)((long)((void*)_deviceId));
 
2498
    myGlobals.device[deviceId].packetQueueHead = (myGlobals.device[deviceId].packetQueueHead+1)
 
2499
      % CONST_PACKET_QUEUE_LENGTH;
 
2500
    myGlobals.device[deviceId].packetQueueLen++;
 
2501
    if(myGlobals.device[deviceId].packetQueueLen > myGlobals.device[deviceId].maxPacketQueueLen)
 
2502
      myGlobals.device[deviceId].maxPacketQueueLen = myGlobals.device[deviceId].packetQueueLen;
 
2503
    releaseMutex(&myGlobals.device[deviceId].packetQueueMutex);
2365
2504
#ifdef DEBUG
2366
2505
    traceEvent(CONST_TRACE_INFO, "Queued packet... [packet queue=%d/max=%d]",
2367
 
               myGlobals.packetQueueLen, myGlobals.maxPacketQueueLen);
 
2506
               myGlobals.device[deviceId].packetQueueLen, myGlobals.maxPacketQueueLen);
2368
2507
#endif
2369
2508
 
2370
2509
#ifdef DEBUG_THREADS
2371
2510
    traceEvent(CONST_TRACE_INFO, "+ [packet queue=%d/max=%d]",
2372
 
               myGlobals.packetQueueLen, myGlobals.maxPacketQueueLen);
 
2511
               myGlobals.device[deviceId].packetQueueLen, myGlobals.maxPacketQueueLen);
2373
2512
#endif
2374
2513
  }
2375
2514
 
2376
 
  signalCondvar(&myGlobals.queueCondvar);
 
2515
  signalCondvar(&myGlobals.device[deviceId].queueCondvar);
2377
2516
 
2378
2517
  ntop_conditional_sched_yield(); /* Allow other threads (dequeue) to run */
2379
2518
}
2386
2525
 
2387
2526
/* ************************************ */
2388
2527
 
2389
 
void* dequeuePacket(void* notUsed _UNUSED_) {
2390
 
  u_short deviceId;
 
2528
void* dequeuePacket(void* _deviceId) {
 
2529
  u_int deviceId = (u_int)((long)_deviceId);
2391
2530
  struct pcap_pkthdr h;
2392
2531
  u_char p[MAX_PACKET_LEN];
2393
2532
 
2402
2541
    traceEvent(CONST_TRACE_INFO, "Waiting for packet...");
2403
2542
#endif
2404
2543
 
2405
 
    while((myGlobals.packetQueueLen == 0) &&
 
2544
    while((myGlobals.device[deviceId].packetQueueLen == 0) &&
2406
2545
          (myGlobals.ntopRunState <= FLAG_NTOPSTATE_RUN) /* Courtesy of Wies-Software <wies@wiessoft.de> */) {
2407
 
      waitCondvar(&myGlobals.queueCondvar);
 
2546
      waitCondvar(&myGlobals.device[deviceId].queueCondvar);
2408
2547
    }
2409
2548
 
2410
2549
    if(myGlobals.ntopRunState > FLAG_NTOPSTATE_RUN) break;
2412
2551
#ifdef DEBUG
2413
2552
    traceEvent(CONST_TRACE_INFO, "Got packet...");
2414
2553
#endif
2415
 
    accessMutex(&myGlobals.packetQueueMutex, "dequeuePacket");
2416
 
    memcpy(&h, &myGlobals.packetQueue[myGlobals.packetQueueTail].h,
 
2554
    accessMutex(&myGlobals.device[deviceId].packetQueueMutex, "dequeuePacket");
 
2555
    memcpy(&h, &myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueTail].h,
2417
2556
           sizeof(struct pcap_pkthdr));
2418
2557
 
2419
 
    deviceId = myGlobals.packetQueue[myGlobals.packetQueueTail].deviceId;
 
2558
    deviceId = myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueTail].deviceId;
2420
2559
 
2421
2560
    /* This code should be changed ASAP. It is a bad trick that avoids ntop to
2422
2561
       go beyond packet boundaries (L.Deri 17/03/2003)
2430
2569
       && (myGlobals.runningPref.enablePacketDecoding /* Courtesy of Ken Beaty <ken@ait.com> */))
2431
2570
      traceEvent (CONST_TRACE_WARNING, "dequeuePacket: caplen %d != len %d\n", h.caplen, h.len);
2432
2571
 
2433
 
    if (myGlobals.runningPref.printIpOnly) {
2434
 
        memcpy(p, myGlobals.packetQueue[myGlobals.packetQueueTail].p, DEFAULT_SNAPLEN);
2435
 
    } else {
2436
 
      memcpy(p, myGlobals.packetQueue[myGlobals.packetQueueTail].p, MAX_PACKET_LEN);
2437
 
    }
 
2572
    if (myGlobals.runningPref.printIpOnly)
 
2573
      memcpy(p, myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueTail].p, DEFAULT_SNAPLEN);
 
2574
    else
 
2575
      memcpy(p, myGlobals.device[deviceId].packetQueue[myGlobals.device[deviceId].packetQueueTail].p, MAX_PACKET_LEN);
2438
2576
 
2439
2577
    if(h.len > MAX_PACKET_LEN) {
2440
2578
      traceEvent(CONST_TRACE_WARNING, "packet truncated (%d->%d)", h.len, MAX_PACKET_LEN);
2441
2579
      h.len = MAX_PACKET_LEN;
2442
2580
    }
2443
2581
 
2444
 
    myGlobals.packetQueueTail = (myGlobals.packetQueueTail+1) % CONST_PACKET_QUEUE_LENGTH;
2445
 
    myGlobals.packetQueueLen--;
2446
 
    releaseMutex(&myGlobals.packetQueueMutex);
 
2582
    myGlobals.device[deviceId].packetQueueTail = (myGlobals.device[deviceId].packetQueueTail+1) % CONST_PACKET_QUEUE_LENGTH;
 
2583
    myGlobals.device[deviceId].packetQueueLen--;
 
2584
    releaseMutex(&myGlobals.device[deviceId].packetQueueMutex);
2447
2585
#ifdef DEBUG_THREADS
2448
 
    traceEvent(CONST_TRACE_INFO, "- [packet queue=%d/max=%d]", myGlobals.packetQueueLen, myGlobals.maxPacketQueueLen);
 
2586
    traceEvent(CONST_TRACE_INFO, "- [packet queue=%d/max=%d]", myGlobals.device[deviceId].packetQueueLen, myGlobals.maxPacketQueueLen);
2449
2587
#endif
2450
2588
 
2451
2589
#ifdef DEBUG
2452
 
    traceEvent(CONST_TRACE_INFO, "Processing packet... [packet queue=%d/max=%d]",
2453
 
               myGlobals.packetQueueLen, myGlobals.maxPacketQueueLen);
 
2590
    traceEvent(CONST_TRACE_INFO, "Processing packet... [packet queue=%d/max=%d][id=%d]",
 
2591
               myGlobals.device[deviceId].packetQueueLen, myGlobals.maxPacketQueueLen, deviceId);
2454
2592
#endif
2455
2593
 
2456
2594
    myGlobals.actTime = time(NULL);
2457
 
    accessMutex(&myGlobals.packetProcessMutex, "dequeuePacket");
 
2595
    accessMutex(&myGlobals.device[deviceId].packetProcessMutex, "dequeuePacket");
2458
2596
    processPacket((u_char*)((long)deviceId), &h, p);
2459
 
    releaseMutex(&myGlobals.packetProcessMutex);
 
2597
    releaseMutex(&myGlobals.device[deviceId].packetProcessMutex);
2460
2598
  }
2461
2599
 
2462
 
  myGlobals.dequeueThreadId = 0;
 
2600
  myGlobals.device[deviceId].dequeuePacketThreadId = 0;
2463
2601
 
2464
2602
  traceEvent(CONST_TRACE_INFO,
2465
 
             "THREADMGMT[t%lu]: NPA: network packet analyzer (packet processor) thread terminated [p%d]",
2466
 
             pthread_self(), getpid());
 
2603
             "THREADMGMT[t%lu]: NPA: network packet analyzer (%s) thread terminated [p%d]",
 
2604
             pthread_self(), myGlobals.device[deviceId].humanFriendlyName, getpid());
2467
2605
 
2468
2606
  return(NULL);
2469
2607
}
2597
2735
/* ***************************************************** */
2598
2736
 
2599
2737
void dumpSuspiciousPacket(int actualDeviceId) {
2600
 
  if(myGlobals.device[actualDeviceId].pcapErrDumper != NULL)
 
2738
  if(myGlobals.device[actualDeviceId].pcapErrDumper != NULL) {
2601
2739
    pcap_dump((u_char*)myGlobals.device[actualDeviceId].pcapErrDumper, h_save, p_save);
 
2740
    traceEvent(CONST_TRACE_INFO, "Dumped %d bytes suspicious packet", h_save->caplen);
 
2741
  }
2602
2742
}
2603
2743
 
2604
2744
/* ***************************************************** */
2631
2771
  /* Token-Ring Strings */
2632
2772
  struct tokenRing_llc *trllc;
2633
2773
  unsigned char ipxBuffer[128];
2634
 
  int deviceId, actualDeviceId, vlanId=-1;
 
2774
  int deviceId, actualDeviceId;
 
2775
  u_int16_t vlanId=NO_VLAN;
2635
2776
  static time_t lastUpdateThptTime = 0;
2636
2777
#ifdef LINUX
2637
2778
  AnyHeader *anyHeader;
2684
2825
  */
2685
2826
  myGlobals.actTime = h->ts.tv_sec;
2686
2827
 
2687
 
  deviceId = (int)_deviceId;
 
2828
  deviceId = (int)((long)_deviceId);
2688
2829
 
2689
2830
  actualDeviceId = getActualInterface(deviceId);
2690
2831
 
2752
2893
 
2753
2894
  hlen = (myGlobals.device[deviceId].datalink == DLT_NULL) ? CONST_NULL_HDRLEN : sizeof(struct ether_header);
2754
2895
 
2755
 
  if (!myGlobals.initialSniffTime && (myGlobals.runningPref.rFileName != NULL)) {
 
2896
  if(!myGlobals.initialSniffTime && (myGlobals.runningPref.rFileName != NULL)) {
2756
2897
      myGlobals.initialSniffTime = h->ts.tv_sec;
2757
2898
      myGlobals.device[deviceId].lastThptUpdate = myGlobals.device[deviceId].lastMinThptUpdate =
2758
2899
          myGlobals.device[deviceId].lastHourThptUpdate = myGlobals.device[deviceId].lastFiveMinsThptUpdate = myGlobals.initialSniffTime;
2759
2900
  }
2760
2901
 
2761
2902
  memcpy(&myGlobals.lastPktTime, &h->ts, sizeof(myGlobals.lastPktTime));
2762
 
  
 
2903
 
2763
2904
  if(caplen >= hlen) {
2764
2905
    HostTraffic *srcHost=NULL, *dstHost=NULL;
2765
2906
 
2831
2972
      length -= CONST_NULL_HDRLEN; /* don't count nullhdr */
2832
2973
 
2833
2974
      /* All this crap is due to the old little/big endian story... */
2834
 
      if((p[0] == 0) && (p[1] == 0) && (p[2] == 8) && (p[3] == 0))
 
2975
      if(((p[0] == 0) && (p[1] == 0) && (p[2] == 8) && (p[3] == 0))
 
2976
         || ((p[0] == 2) && (p[1] == 0) && (p[2] == 0) && (p[3] == 0)) /* OSX */)
2835
2977
        eth_type = ETHERTYPE_IP;
2836
 
      else if((p[0] == 0) && (p[1] == 0) && (p[2] == 0x86) && (p[3] == 0xdd))
 
2978
      else if(((p[0] == 0) && (p[1] == 0) && (p[2] == 0x86) && (p[3] == 0xdd))
 
2979
              || ((p[0] == 0x1E) && (p[1] == 0) && (p[2] == 0) && (p[3] == 0)) /* OSX */)
2837
2980
        eth_type = ETHERTYPE_IPv6;
 
2981
      else {
 
2982
        // traceEvent(CONST_TRACE_INFO, "[%d][%d][%d][%d]", p[0], p[1], p[2], p[3]);
 
2983
      }
2838
2984
      ether_src = ether_dst = myGlobals.dummyEthAddress;
2839
2985
      break;
2840
2986
 
2976
3122
          lockHostsHashMutex(dstHost, "processPacket-dst");
2977
3123
        }
2978
3124
 
2979
 
        if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
3125
        if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
2980
3126
 
2981
3127
        memcpy((char *)&ipxPkt, (char *)p+sizeof(struct ether_header), sizeof(IPXpacket));
2982
3128
 
2993
3139
          if(dstHost->nonIPTraffic == NULL) dstHost->nonIPTraffic = (NonIPTraffic*)calloc(1, sizeof(NonIPTraffic));
2994
3140
 
2995
3141
          if((srcHost->nonIPTraffic == NULL) || (dstHost->nonIPTraffic == NULL)) return;
2996
 
          incrementTrafficCounter(&srcHost->nonIPTraffic->ipxSent, length), incrementTrafficCounter(&dstHost->nonIPTraffic->ipxRcvd, length);
 
3142
          incrementTrafficCounter(&srcHost->nonIPTraffic->ipxSent, length),
 
3143
            incrementTrafficCounter(&dstHost->nonIPTraffic->ipxRcvd, length);
2997
3144
          incrementTrafficCounter(&myGlobals.device[actualDeviceId].ipxBytes, length);
2998
3145
 
2999
3146
          ctr.value = length;
3026
3173
          unlockHostsHashMutex(srcHost);
3027
3174
          lowMemoryMsgShown = 1;
3028
3175
          return;
3029
 
        } else {          
 
3176
        } else {
3030
3177
          lockHostsHashMutex(dstHost, "processPacket-dst-2");
3031
3178
        }
3032
3179
 
3033
 
        if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
3180
        if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
3034
3181
 
3035
3182
        if(srcHost->nonIPTraffic == NULL) srcHost->nonIPTraffic = (NonIPTraffic*)calloc(1, sizeof(NonIPTraffic));
3036
3183
        if(dstHost->nonIPTraffic == NULL) dstHost->nonIPTraffic = (NonIPTraffic*)calloc(1, sizeof(NonIPTraffic));
3087
3234
            lockHostsHashMutex(dstHost, "processPacket-dst-3");
3088
3235
          }
3089
3236
 
3090
 
          if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
3237
          if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
3091
3238
 
3092
3239
          if(srcHost->nonIPTraffic == NULL) srcHost->nonIPTraffic = (NonIPTraffic*)calloc(1, sizeof(NonIPTraffic));
3093
3240
          if(dstHost->nonIPTraffic == NULL) dstHost->nonIPTraffic = (NonIPTraffic*)calloc(1, sizeof(NonIPTraffic));
3105
3252
            int llcLen;
3106
3253
            lockHostsHashMutex(srcHost, "processPacket-src-4");
3107
3254
            lockHostsHashMutex(dstHost, "processPacket-dst-4");
3108
 
            if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
3255
            if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
3109
3256
            p1 = (u_char*)(p+hlen);
3110
3257
 
3111
3258
            /* Watch out for possible alignment problems */
3123
3270
               ) {
3124
3271
              u_char *cdp;
3125
3272
              int cdp_idx = 0;
3126
 
              
 
3273
 
3127
3274
              cdp = (u_char*)(p+hlen+llcLen);
3128
3275
 
3129
3276
              if(cdp[cdp_idx] == 0x02) {
3133
3280
                  u_int16_t cdp_len;
3134
3281
                  // u_char cdp_content[255];
3135
3282
                };
3136
 
                                
 
3283
 
3137
3284
                cdp_idx = 4;
3138
3285
                while((cdp_idx+sizeof(struct cdp_element)) < (length-(hlen+llcLen))) {
3139
3286
                  struct cdp_element element;
3140
 
                  u_short len;
3141
3287
 
3142
3288
                  memcpy(&element, &cdp[cdp_idx], sizeof(struct cdp_element));
3143
3289
 
3145
3291
                  element.cdp_len  = ntohs(element.cdp_len);
3146
3292
                  element.cdp_type  = ntohs(element.cdp_type);
3147
3293
                  if(element.cdp_len == 0) break; /* Sanity check */
3148
 
                  
 
3294
 
3149
3295
                  switch(element.cdp_type) {
3150
3296
                  case 0x0001: /* Device Id */
3151
3297
                    if((srcHost->hostResolvedName[0] == '\0') || (strcmp(srcHost->hostResolvedName, srcHost->hostNumIpAddress))) {
3152
3298
                      u_short tmpStrLen = min(element.cdp_len-4, MAX_LEN_SYM_HOST_NAME-1);
3153
 
                      strncpy(srcHost->hostResolvedName, &cdp[cdp_idx], tmpStrLen);
 
3299
                      strncpy(srcHost->hostResolvedName, (char*)&cdp[cdp_idx], tmpStrLen);
3154
3300
                      srcHost->hostResolvedName[tmpStrLen] = '\0';
3155
3301
                    }
3156
3302
                    break;
3196
3342
 
3197
3343
                if(srcHost->fingerprint == NULL)
3198
3344
                  srcHost->fingerprint = strdup(":Cisco"); /* Default */
3199
 
              }       
 
3345
              }
3200
3346
            }
3201
3347
 
3202
3348
            if(sap_type != 0x42 /* !STP */) {
3529
3675
        } else {
3530
3676
          processIpPkt(p+hlen, h, length, ether_src, ether_dst, actualDeviceId, vlanId);
3531
3677
        }
 
3678
      } else if(eth_type == 0xDEAD) /* Agilent */ {
 
3679
        typedef struct {
 
3680
          u_int8_t  version;        /* Protocol Version */
 
3681
          u_int8_t  response_pdu;   /* 0=Request, 1=Response */
 
3682
          u_int8_t  fragment_id;    /* Fragment Id (Only for
 
3683
                                       fragmented FORWARD responses) */
 
3684
          u_int8_t  pdu_type;       /* See sgbic_pdu_type */
 
3685
          u_int16_t pdu_id;         /* Unique (serial) PDU identifier */
 
3686
          u_int16_t pdu_len;        /* length (bytes) of the PDU (not
 
3687
                                       including this header) */
 
3688
          u_char    digest[16];     /* MD5 digest */
 
3689
        } sgbic_header_v1;
 
3690
 
 
3691
        sgbic_header_v1 *pdu;
 
3692
 
 
3693
        if(length > hlen+sizeof(sgbic_header_v1)) {
 
3694
          pdu = (sgbic_header_v1*)(p+hlen);
 
3695
 
 
3696
          if((pdu->version == 1) && (pdu->pdu_type == 2 /* forward */)) {
 
3697
            static u_short last_pdu_len, last_pdu_id;
 
3698
            u_short size_shift = hlen+sizeof(sgbic_header_v1);
 
3699
 
 
3700
            if(pdu->fragment_id == 0) {
 
3701
              struct pcap_pkthdr h1;
 
3702
 
 
3703
              h1.caplen     = h->caplen-size_shift;
 
3704
              h1.len        = h->len-size_shift;
 
3705
              h1.ts.tv_sec  = h->ts.tv_sec;
 
3706
              h1.ts.tv_usec = h->ts.tv_usec;
 
3707
 
 
3708
 
 
3709
              if(last_pdu_id == pdu->pdu_id) {
 
3710
                if(0)
 
3711
                  traceEvent(CONST_TRACE_ERROR, "[vers=%d][pdu_type=%d][pdu_id=%d][len=%d][fragment_id=%d]",
 
3712
                             pdu->version, pdu->pdu_type, pdu->pdu_id, ntohs(pdu->pdu_len), pdu->fragment_id);
 
3713
 
 
3714
                h1.len += last_pdu_len;
 
3715
                if(0)
 
3716
                  traceEvent(CONST_TRACE_ERROR, "[caplen=%d][len=%d][last_pdu_len=%d]",
 
3717
                             h1.caplen, h1.len, last_pdu_len);
 
3718
              }
 
3719
 
 
3720
              processPacket(_deviceId, &h1, p+size_shift);
 
3721
            } else {
 
3722
              last_pdu_len = h->len-size_shift, last_pdu_id = pdu->pdu_id;
 
3723
 
 
3724
              if(0)
 
3725
                traceEvent(CONST_TRACE_ERROR, "[vers=%d][pdu_type=%d][pdu_id=%d][len=%d][fragment_id=%d]",
 
3726
                           pdu->version, pdu->pdu_type, pdu->pdu_id, ntohs(pdu->pdu_len), pdu->fragment_id);
 
3727
 
 
3728
            }
 
3729
          }
 
3730
        }
 
3731
 
3532
3732
      } else if(eth_type == 0x8864) /* PPPOE */ {
3533
 
        /* PPPoE - Courtesy of Andreas Pfaller Feb2003
 
3733
        /* PPPoE - Courtesy of Andreas Pfaller Feb20032
3534
3734
         *   This strips the PPPoE encapsulation for traffic transiting the network.
3535
3735
         */
3536
3736
        struct pppoe_hdr *pppoe_hdr=(struct pppoe_hdr *) (p+hlen);
3561
3761
          return;
3562
3762
        } else {
3563
3763
          /*
3564
 
            traceEvent(CONST_TRACE_INFO, "lockHostsHashMutex(%s)(%s)", 
 
3764
            traceEvent(CONST_TRACE_INFO, "lockHostsHashMutex(%s)(%s)",
3565
3765
            etheraddr_string(ether_src, ipxBuffer),
3566
3766
            srcHost->ethAddressString);
3567
3767
          */
3580
3780
          lockHostsHashMutex(dstHost, "processPacket-src-5");
3581
3781
        }
3582
3782
 
3583
 
        if(vlanId != -1) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
 
3783
        if(vlanId != NO_VLAN) { srcHost->vlanId = vlanId; dstHost->vlanId = vlanId; }
3584
3784
 
3585
3785
        switch(eth_type) {
3586
3786
        case ETHERTYPE_ARP: /* ARP - Address resolution Protocol */
3612
3812
                if(dstHost->nonIPTraffic == NULL) return;
3613
3813
                incrementTrafficCounter(&dstHost->nonIPTraffic->arpReplyPktsRcvd, 1);
3614
3814
              }
3615
 
                
 
3815
 
3616
3816
 
3617
3817
              /* DO NOT ADD A break ABOVE ! */
3618
3818
            case ARPOP_REQUEST: /* ARP request */
3629
3829
                if(myGlobals.runningPref.numericFlag == 0)
3630
3830
                  ipaddr2str(srcHost->hostIpAddress, 1);
3631
3831
 
3632
 
                if(isPseudoLocalAddress(&srcHost->hostIpAddress, actualDeviceId)) {
 
3832
                if(isPseudoLocalAddress(&srcHost->hostIpAddress, actualDeviceId, NULL, NULL)) {
3633
3833
                  FD_SET(FLAG_SUBNET_LOCALHOST, &srcHost->flags);
3634
3834
                  FD_SET(FLAG_SUBNET_PSEUDO_LOCALHOST, &srcHost->flags);
3635
3835
                } else {