1 /* Copyright (c) 2002-2011 InMon Corp. Licensed under the terms of the InMon sFlow licence: */
2 /* http://www.inmon.com/technology/sflowlicense.txt */
3 
4 #ifndef SFLOW_V2V4_H
5 #define SFLOW_V2V4_H 1
6 
7 #include "config.h"
8 
9 #include <sys/types.h>
10 #ifdef HAVE_STDINT_H
11 #include <stdint.h>
12 #endif
13 
14 #include <sflow.h>
15 
16 #if defined(__cplusplus)
17 extern "C" {
18 #endif
19 
20 enum INMAddress_type {
21   INMADDRESSTYPE_IP_V4 = 1,
22   INMADDRESSTYPE_IP_V6 = 2
23 };
24 
25 typedef union _INMAddress_value {
26   SFLIPv4 ip_v4;
27   SFLIPv6 ip_v6;
28 } INMAddress_value;
29 
30 typedef struct _INMAddress {
31   uint32_t type;           /* enum INMAddress_type */
32   INMAddress_value address;
33 } INMAddress;
34 
35 /* Packet header data */
36 
37 #define INM_MAX_HEADER_SIZE 256   /* The maximum sampled header size. */
38 #define INM_DEFAULT_HEADER_SIZE 128
39 #define INM_DEFAULT_COLLECTOR_PORT 6343
40 #define INM_DEFAULT_SAMPLING_RATE 400
41 
42 /* The header protocol describes the format of the sampled header */
43 enum INMHeader_protocol {
44   INMHEADER_ETHERNET_ISO8023     = 1,
45   INMHEADER_ISO88024_TOKENBUS    = 2,
46   INMHEADER_ISO88025_TOKENRING   = 3,
47   INMHEADER_FDDI                 = 4,
48   INMHEADER_FRAME_RELAY          = 5,
49   INMHEADER_X25                  = 6,
50   INMHEADER_PPP                  = 7,
51   INMHEADER_SMDS                 = 8,
52   INMHEADER_AAL5                 = 9,
53   INMHEADER_AAL5_IP              = 10, /* e.g. Cisco AAL5 mux */
54   INMHEADER_IPv4                 = 11,
55   INMHEADER_IPv6                 = 12
56 };
57 
58 typedef struct _INMSampled_header {
59   uint32_t header_protocol;            /* (enum INMHeader_protocol) */
60   uint32_t frame_length;               /* Original length of packet before sampling */
61   uint32_t header_length;              /* length of sampled header bytes to follow */
62   uint8_t header[INM_MAX_HEADER_SIZE]; /* Header bytes */
63 } INMSampled_header;
64 
65 /* Packet IP version 4 data */
66 
67 typedef struct _INMSampled_ipv4 {
68   uint32_t length;      /* The length of the IP packet
69 			    excluding lower layer encapsulations */
70   uint32_t protocol;    /* IP Protocol type (for example, TCP = 6, UDP = 17) */
71   SFLIPv4 src_ip; /* Source IP Address */
72   SFLIPv4 dst_ip; /* Destination IP Address */
73   uint32_t src_port;    /* TCP/UDP source port number or equivalent */
74   uint32_t dst_port;    /* TCP/UDP destination port number or equivalent */
75   uint32_t tcp_flags;   /* TCP flags */
76   uint32_t tos;         /* IP type of service */
77 } INMSampled_ipv4;
78 
79 /* Packet IP version 6 data */
80 
81 typedef struct _INMSampled_ipv6 {
82   uint32_t length;       /* The length of the IP packet
83 			     excluding lower layer encapsulations */
84   uint32_t protocol;     /* IP Protocol type (for example, TCP = 6, UDP = 17) */
85   SFLIPv6 src_ip; /* Source IP Address */
86   SFLIPv6 dst_ip; /* Destination IP Address */
87   uint32_t src_port;     /* TCP/UDP source port number or equivalent */
88   uint32_t dst_port;     /* TCP/UDP destination port number or equivalent */
89   uint32_t tcp_flags;    /* TCP flags */
90   uint32_t tos;          /* IP type of service */
91 } INMSampled_ipv6;
92 
93 
94 /* Packet data */
95 
96 enum INMPacket_information_type {
97   INMPACKETTYPE_HEADER  = 1,      /* Packet headers are sampled */
98   INMPACKETTYPE_IPV4    = 2,      /* IP version 4 data */
99   INMPACKETTYPE_IPV6    = 3       /* IP version 4 data */
100 };
101 
102 typedef union _INMPacket_data_type {
103   INMSampled_header header;
104   INMSampled_ipv4 ipv4;
105   INMSampled_ipv6 ipv6;
106 } INMPacket_data_type;
107 
108 /* Extended data types */
109 
110 /* Extended switch data */
111 
112 typedef struct _INMExtended_switch {
113   uint32_t src_vlan;       /* The 802.1Q VLAN id of incomming frame */
114   uint32_t src_priority;   /* The 802.1p priority */
115   uint32_t dst_vlan;       /* The 802.1Q VLAN id of outgoing frame */
116   uint32_t dst_priority;   /* The 802.1p priority */
117 } INMExtended_switch;
118 
119 /* Extended router data */
120 
121 typedef struct _INMExtended_router {
122   INMAddress nexthop;               /* IP address of next hop router */
123   uint32_t src_mask;               /* Source address prefix mask bits */
124   uint32_t dst_mask;               /* Destination address prefix mask bits */
125 } INMExtended_router;
126 
127 /* Extended gateway data */
128 
129 enum INMExtended_as_path_segment_type {
130   INMEXTENDED_AS_SET = 1,      /* Unordered set of ASs */
131   INMEXTENDED_AS_SEQUENCE = 2  /* Ordered sequence of ASs */
132 };
133 
134 typedef struct _INMExtended_as_path_segment {
135   uint32_t type;   /* enum INMExtended_as_path_segment_type */
136   uint32_t length; /* number of AS numbers in set/sequence */
137   union {
138     uint32_t *set;
139     uint32_t *seq;
140   } as;
141 } INMExtended_as_path_segment;
142 
143 /* note: the INMExtended_gateway structure has changed between v2 and v4.
144    Here is the old version first... */
145 
146 typedef struct _INMExtended_gateway_v2 {
147   uint32_t as;                             /* AS number for this gateway */
148   uint32_t src_as;                         /* AS number of source (origin) */
149   uint32_t src_peer_as;                    /* AS number of source peer */
150   uint32_t dst_as_path_length;             /* number of AS numbers in path */
151   uint32_t *dst_as_path;
152 } INMExtended_gateway_v2;
153 
154 /* now here is the new version... */
155 
156 typedef struct _INMExtended_gateway_v4 {
157   uint32_t as;                             /* AS number for this gateway */
158   uint32_t src_as;                         /* AS number of source (origin) */
159   uint32_t src_peer_as;                    /* AS number of source peer */
160   uint32_t dst_as_path_segments;           /* number of segments in path */
161   INMExtended_as_path_segment *dst_as_path; /* list of seqs or sets */
162   uint32_t communities_length;             /* number of communities */
163   uint32_t *communities;                   /* set of communities */
164   uint32_t localpref;                      /* LocalPref associated with this route */
165 } INMExtended_gateway_v4;
166 
167 /* Extended user data */
168 typedef struct _INMExtended_user {
169   uint32_t src_user_len;
170   char *src_user;
171   uint32_t dst_user_len;
172   char *dst_user;
173 } INMExtended_user;
174 enum INMExtended_url_direction {
175   INMEXTENDED_URL_SRC = 1, /* URL is associated with source address */
176   INMEXTENDED_URL_DST = 2  /* URL is associated with destination address */
177 };
178 
179 typedef struct _INMExtended_url {
180   uint32_t direction; /* enum INMExtended_url_direction */
181   uint32_t url_len;
182   char *url;
183 } INMExtended_url;
184 
185 /* Extended data */
186 
187 enum INMExtended_information_type {
188   INMEXTENDED_SWITCH    = 1,      /* Extended switch information */
189   INMEXTENDED_ROUTER    = 2,      /* Extended router information */
190   INMEXTENDED_GATEWAY   = 3,      /* Extended gateway router information */
191   INMEXTENDED_USER      = 4,      /* Extended TACAS/RADIUS user information */
192   INMEXTENDED_URL       = 5       /* Extended URL information */
193 };
194 
195 /* Format of a single sample */
196 
197 typedef struct _INMFlow_sample {
198   uint32_t sequence_number;      /* Incremented with each flow sample
199 				     generated */
200   uint32_t source_id;            /* fsSourceId */
201   uint32_t sampling_rate;        /* fsPacketSamplingRate */
202   uint32_t sample_pool;          /* Total number of packets that could have been
203 				     sampled (i.e. packets skipped by sampling
204 				     process + total number of samples) */
205   uint32_t drops;                /* Number of times a packet was dropped due to
206 				     lack of resources */
207   uint32_t input;                /* SNMP ifIndex of input interface.
208 				     0 if interface is not known. */
209   uint32_t output;               /* SNMP ifIndex of output interface,
210 				     0 if interface is not known.
211 				     Set most significant bit to indicate
212 				     multiple destination interfaces
213 				     (i.e. in case of broadcast or multicast)
214 				     and set lower order bits to indicate
215 				     number of destination interfaces.
216 				     Examples:
217 				     0x00000002  indicates ifIndex = 2
218 				     0x00000000  ifIndex unknown.
219 				     0x80000007  indicates a packet sent
220 				     to 7 interfaces.
221 				     0x80000000  indicates a packet sent to
222 				     an unknown number of
223 				     interfaces greater than 1.*/
224   uint32_t packet_data_tag;       /* enum INMPacket_information_type */
225   INMPacket_data_type packet_data; /* Information about sampled packet */
226 
227   /* in the sFlow packet spec the next field is the number of extended objects
228      followed by the data for each one (tagged with the type).  Here we just
229      provide space for each one, and flags to enable them.  The correct format
230      is then put together by the serialization code */
231   int gotSwitch;
232   INMExtended_switch switchDevice;
233   int gotRouter;
234   INMExtended_router router;
235   int gotGateway;
236   union {
237     INMExtended_gateway_v2 v2;  /* make the version explicit so that there is */
238     INMExtended_gateway_v4 v4;  /* less danger of mistakes when upgrading code */
239   } gateway;
240   int gotUser;
241   INMExtended_user user;
242   int gotUrl;
243   INMExtended_url url;
244 } INMFlow_sample;
245 
246 /* Counter types */
247 
248 /* Generic interface counters - see RFC 1573, 2233 */
249 
250 typedef struct _INMIf_counters {
251   uint32_t ifIndex;
252   uint32_t ifType;
253   uint64_t ifSpeed;
254   uint32_t ifDirection;        /* Derived from MAU MIB (RFC 2239)
255 				   0 = unknown, 1 = full-duplex,
256 				   2 = half-duplex, 3 = in, 4 = out */
257   uint32_t ifStatus;           /* bit field with the following bits assigned:
258 				   bit 0 = ifAdminStatus (0 = down, 1 = up)
259 				   bit 1 = ifOperStatus (0 = down, 1 = up) */
260   uint64_t ifInOctets;
261   uint32_t ifInUcastPkts;
262   uint32_t ifInMulticastPkts;
263   uint32_t ifInBroadcastPkts;
264   uint32_t ifInDiscards;
265   uint32_t ifInErrors;
266   uint32_t ifInUnknownProtos;
267   uint64_t ifOutOctets;
268   uint32_t ifOutUcastPkts;
269   uint32_t ifOutMulticastPkts;
270   uint32_t ifOutBroadcastPkts;
271   uint32_t ifOutDiscards;
272   uint32_t ifOutErrors;
273   uint32_t ifPromiscuousMode;
274 } INMIf_counters;
275 
276 /* Ethernet interface counters - see RFC 2358 */
277 typedef struct _INMEthernet_specific_counters {
278   uint32_t dot3StatsAlignmentErrors;
279   uint32_t dot3StatsFCSErrors;
280   uint32_t dot3StatsSingleCollisionFrames;
281   uint32_t dot3StatsMultipleCollisionFrames;
282   uint32_t dot3StatsSQETestErrors;
283   uint32_t dot3StatsDeferredTransmissions;
284   uint32_t dot3StatsLateCollisions;
285   uint32_t dot3StatsExcessiveCollisions;
286   uint32_t dot3StatsInternalMacTransmitErrors;
287   uint32_t dot3StatsCarrierSenseErrors;
288   uint32_t dot3StatsFrameTooLongs;
289   uint32_t dot3StatsInternalMacReceiveErrors;
290   uint32_t dot3StatsSymbolErrors;
291 } INMEthernet_specific_counters;
292 
293 typedef struct _INMEthernet_counters {
294   INMIf_counters generic;
295   INMEthernet_specific_counters ethernet;
296 } INMEthernet_counters;
297 
298 /* FDDI interface counters - see RFC 1512 */
299 typedef struct _INMFddi_counters {
300   INMIf_counters generic;
301 } INMFddi_counters;
302 
303 /* Token ring counters - see RFC 1748 */
304 
305 typedef struct _INMTokenring_specific_counters {
306   uint32_t dot5StatsLineErrors;
307   uint32_t dot5StatsBurstErrors;
308   uint32_t dot5StatsACErrors;
309   uint32_t dot5StatsAbortTransErrors;
310   uint32_t dot5StatsInternalErrors;
311   uint32_t dot5StatsLostFrameErrors;
312   uint32_t dot5StatsReceiveCongestions;
313   uint32_t dot5StatsFrameCopiedErrors;
314   uint32_t dot5StatsTokenErrors;
315   uint32_t dot5StatsSoftErrors;
316   uint32_t dot5StatsHardErrors;
317   uint32_t dot5StatsSignalLoss;
318   uint32_t dot5StatsTransmitBeacons;
319   uint32_t dot5StatsRecoverys;
320   uint32_t dot5StatsLobeWires;
321   uint32_t dot5StatsRemoves;
322   uint32_t dot5StatsSingles;
323   uint32_t dot5StatsFreqErrors;
324 } INMTokenring_specific_counters;
325 
326 typedef struct _INMTokenring_counters {
327   INMIf_counters generic;
328   INMTokenring_specific_counters tokenring;
329 } INMTokenring_counters;
330 
331 /* 100 BaseVG interface counters - see RFC 2020 */
332 
333 typedef struct _INMVg_specific_counters {
334   uint32_t dot12InHighPriorityFrames;
335   uint64_t dot12InHighPriorityOctets;
336   uint32_t dot12InNormPriorityFrames;
337   uint64_t dot12InNormPriorityOctets;
338   uint32_t dot12InIPMErrors;
339   uint32_t dot12InOversizeFrameErrors;
340   uint32_t dot12InDataErrors;
341   uint32_t dot12InNullAddressedFrames;
342   uint32_t dot12OutHighPriorityFrames;
343   uint64_t dot12OutHighPriorityOctets;
344   uint32_t dot12TransitionIntoTrainings;
345   uint64_t dot12HCInHighPriorityOctets;
346   uint64_t dot12HCInNormPriorityOctets;
347   uint64_t dot12HCOutHighPriorityOctets;
348 } INMVg_specific_counters;
349 
350 typedef struct _INMVg_counters {
351   INMIf_counters generic;
352   INMVg_specific_counters vg;
353 } INMVg_counters;
354 
355 /* WAN counters */
356 
357 typedef struct _INMWan_counters {
358   INMIf_counters generic;
359 } INMWan_counters;
360 
361 typedef struct _INMVlan_counters {
362   uint32_t vlan_id;
363   uint64_t octets;
364   uint32_t ucastPkts;
365   uint32_t multicastPkts;
366   uint32_t broadcastPkts;
367   uint32_t discards;
368 } INMVlan_counters;
369 
370 /* Counters data */
371 
372 enum INMCounters_version {
373   INMCOUNTERSVERSION_GENERIC      = 1,
374   INMCOUNTERSVERSION_ETHERNET     = 2,
375   INMCOUNTERSVERSION_TOKENRING    = 3,
376   INMCOUNTERSVERSION_FDDI         = 4,
377   INMCOUNTERSVERSION_VG           = 5,
378   INMCOUNTERSVERSION_WAN          = 6,
379   INMCOUNTERSVERSION_VLAN         = 7
380 };
381 
382 typedef union _INMCounters_type {
383   INMIf_counters generic;
384   INMEthernet_counters ethernet;
385   INMTokenring_counters tokenring;
386   INMFddi_counters fddi;
387   INMVg_counters vg;
388   INMWan_counters wan;
389   INMVlan_counters vlan;
390 } INMCounters_type;
391 
392 typedef struct _INMCounters_sample_hdr {
393   uint32_t sequence_number;    /* Incremented with each counters sample
394 				   generated by this source_id */
395   uint32_t source_id;          /* fsSourceId */
396   uint32_t sampling_interval;  /* fsCounterSamplingInterval */
397 } INMCounters_sample_hdr;
398 
399 typedef struct _INMCounters_sample {
400   INMCounters_sample_hdr hdr;
401   uint32_t counters_type_tag;  /* Enum INMCounters_version */
402   INMCounters_type counters;    /* Counter set for this interface type */
403 } INMCounters_sample;
404 
405 /* when I turn on optimisation with the Microsoft compiler it seems to change
406    the values of these enumerated types and break the program - not sure why */
407 enum INMSample_types {
408    FLOWSAMPLE  = 1,
409    COUNTERSSAMPLE = 2
410 };
411 
412 typedef union _INMSample_type {
413   INMFlow_sample flowsample;
414   INMCounters_sample counterssample;
415 } INMSample_type;
416 
417 /* Format of a sample datagram */
418 
419 enum INMDatagram_version {
420   INMDATAGRAM_VERSION2 = 2,
421   INMDATAGRAM_VERSION4 = 4
422 };
423 
424 typedef struct _INMSample_datagram_hdr {
425   uint32_t datagram_version;      /* (enum INMDatagram_version) = VERSION4 */
426   INMAddress agent_address;        /* IP address of sampling agent */
427   uint32_t sequence_number;       /* Incremented with each sample datagram
428 				      generated */
429   uint32_t uptime;                /* Current time (in milliseconds since device
430 				      last booted). Should be set as close to
431 				      datagram transmission time as possible.*/
432   uint32_t num_samples;           /* Number of flow and counters samples to follow */
433 } INMSample_datagram_hdr;
434 
435 #define INM_MAX_DATAGRAM_SIZE 1500
436 #define INM_MIN_DATAGRAM_SIZE 200
437 #define INM_DEFAULT_DATAGRAM_SIZE 1400
438 
439 #define INM_DATA_PAD 400
440 
441 #if defined(__cplusplus)
442 }  /* extern "C" */
443 #endif
444 
445 #endif /* SFLOW_V2V4_H */
446