File: netflow1.c

package info (click to toggle)
softflowd 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 680 kB
  • sloc: ansic: 5,321; sh: 634; perl: 196; makefile: 43
file content (178 lines) | stat: -rw-r--r-- 6,358 bytes parent folder | download
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

/*
 * Copyright 2002 Damien Miller <djm@mindrot.org> All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "common.h"
#include "log.h"
#include "treetype.h"
#include "softflowd.h"

#ifdef ENABLE_LEGACY
/*
 * This is the Cisco Netflow(tm) version 1 packet format
 * Based on:
 * http://www.cisco.com/en/US/products/sw/netmgtsw/ps1964/products_implementation_design_guide09186a00800d6a11.html 
 */
struct NF1_HEADER {
  u_int16_t version, flows;
  u_int32_t uptime_ms, time_sec, time_nanosec;
};
struct NF1_FLOW {
  u_int32_t src_ip, dest_ip, nexthop_ip;
  u_int16_t if_index_in, if_index_out;
  u_int32_t flow_packets, flow_octets;
  u_int32_t flow_start, flow_finish;
  u_int16_t src_port, dest_port;
  u_int16_t pad1;
  u_int8_t protocol, tos, tcp_flags;
  u_int8_t pad2, pad3, pad4;
  u_int32_t reserved1;
#if 0
  u_int8_t reserved2;           /* XXX: no longer used */
#endif
};
/* Maximum of 24 flows per packet */
#define NF1_MAXFLOWS		24
#define NF1_MAXPACKET_SIZE	(sizeof(struct NF1_HEADER) + \
				 (NF1_MAXFLOWS * sizeof(struct NF1_FLOW)))

/*
 * Given an array of expired flows, send netflow v1 report packets
 * Returns number of packets sent or -1 on error
 */
int
send_netflow_v1 (struct SENDPARAMETER sp) {
  struct FLOW **flows = sp.flows;
  int num_flows = sp.num_flows;
  u_int16_t ifidx = sp.ifidx;
  struct FLOWTRACKPARAMETERS *param = sp.param;
  int verbose_flag = sp.verbose_flag;
  struct timeval now;
  u_int32_t uptime_ms;
  u_int8_t packet[NF1_MAXPACKET_SIZE];  /* Maximum allowed packet size (24 flows) */
  struct NF1_HEADER *hdr = NULL;
  struct NF1_FLOW *flw = NULL;
  int i, j, offset, num_packets;
  struct timeval *system_boot_time = &param->system_boot_time;
  u_int64_t *flows_exported = &param->flows_exported;

  if (param->adjust_time)
    now = param->last_packet_time;
  else
    gettimeofday (&now, NULL);
  uptime_ms = timeval_sub_ms (&now, system_boot_time);

  hdr = (struct NF1_HEADER *) packet;
  for (num_packets = offset = j = i = 0; i < num_flows; i++) {
    if (j >= NF1_MAXFLOWS - 1) {
      if (verbose_flag)
        logit (LOG_DEBUG, "Sending flow packet len = %d", offset);
      param->records_sent += hdr->flows;
      hdr->flows = htons (hdr->flows);
      if (send_multi_destinations
          (sp.target->num_destinations, sp.target->destinations,
           sp.target->is_loadbalance, packet, offset) < 0)
        return (-1);
      *flows_exported += j;
      j = 0;
      num_packets++;
    }
    if (j == 0) {
      memset (&packet, '\0', sizeof (packet));
      hdr->version = htons (1);
      hdr->flows = 0;           /* Filled in as we go */
      hdr->uptime_ms = htonl (uptime_ms);
      hdr->time_sec = htonl (now.tv_sec);
      hdr->time_nanosec = htonl (now.tv_usec * 1000);
      offset = sizeof (*hdr);
    }

    flw = (struct NF1_FLOW *) (packet + offset);
    flw->if_index_in = flw->if_index_out = htons (ifidx);

    /* NetFlow v.1 doesn't do IPv6 */
    if (flows[i]->af != AF_INET)
      continue;
    if (flows[i]->octets[0] > 0) {
      flw->src_ip = flows[i]->addr[0].v4.s_addr;
      flw->dest_ip = flows[i]->addr[1].v4.s_addr;
      flw->src_port = flows[i]->port[0];
      flw->dest_port = flows[i]->port[1];
      flw->flow_packets = htonl (flows[i]->packets[0]);
      flw->flow_octets = htonl (flows[i]->octets[0]);
      flw->flow_start =
        htonl (timeval_sub_ms (&flows[i]->flow_start, system_boot_time));
      flw->flow_finish =
        htonl (timeval_sub_ms (&flows[i]->flow_last, system_boot_time));
      flw->protocol = flows[i]->protocol;
      flw->tcp_flags = flows[i]->tcp_flags[0];
      flw->tos = flows[i]->tos[0];
      offset += sizeof (*flw);
      j++;
      hdr->flows++;
    }

    flw = (struct NF1_FLOW *) (packet + offset);
    flw->if_index_in = flw->if_index_out = htons (ifidx);
    if (flows[i]->octets[1] > 0) {
      flw->src_ip = flows[i]->addr[1].v4.s_addr;
      flw->dest_ip = flows[i]->addr[0].v4.s_addr;
      flw->src_port = flows[i]->port[1];
      flw->dest_port = flows[i]->port[0];
      flw->flow_packets = htonl (flows[i]->packets[1]);
      flw->flow_octets = htonl (flows[i]->octets[1]);
      flw->flow_start =
        htonl (timeval_sub_ms (&flows[i]->flow_start, system_boot_time));
      flw->flow_finish =
        htonl (timeval_sub_ms (&flows[i]->flow_last, system_boot_time));
      flw->protocol = flows[i]->protocol;
      flw->tcp_flags = flows[i]->tcp_flags[1];
      flw->tos = flows[i]->tos[1];
      offset += sizeof (*flw);
      j++;
      hdr->flows++;
    }
  }

  /* Send any leftovers */
  if (j != 0) {
    if (verbose_flag)
      logit (LOG_DEBUG, "Sending flow packet len = %d", offset);
    param->records_sent += hdr->flows;
    hdr->flows = htons (hdr->flows);
    if (send_multi_destinations
        (sp.target->num_destinations, sp.target->destinations,
         sp.target->is_loadbalance, packet, offset) < 0)
      return (-1);
    num_packets++;
  }

  *flows_exported += j;
  param->packets_sent += num_packets;
#ifdef ENABLE_PTHREAD
  if (use_thread)
    free (sp.flows);
#endif /* ENABLE_PTHREAD */
  return (num_packets);
}
#endif /* ENABLE_LEGACY */