if_pflow.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* $OpenBSD: if_pflow.h,v 1.13 2015/07/20 23:15:54 florian Exp $ */
  2. /*
  3. * Copyright (c) 2008 Henning Brauer <henning@openbsd.org>
  4. * Copyright (c) 2008 Joerg Goltermann <jg@osn.de>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
  15. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  16. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef _NET_IF_PFLOW_H_
  19. #define _NET_IF_PFLOW_H_
  20. #define PFLOW_ID_LEN sizeof(u_int64_t)
  21. #define PFLOW_MAXFLOWS 30
  22. #define PFLOW_ENGINE_TYPE 42
  23. #define PFLOW_ENGINE_ID 42
  24. #define PFLOW_MAXBYTES 0xffffffff
  25. #define PFLOW_TIMEOUT 30
  26. #define PFLOW_TMPL_TIMEOUT 30 /* rfc 5101 10.3.6 (p.40) recommends 600 */
  27. #define PFLOW_IPFIX_TMPL_SET_ID 2
  28. /* RFC 5102 Information Element Identifiers */
  29. #define PFIX_IE_octetDeltaCount 1
  30. #define PFIX_IE_packetDeltaCount 2
  31. #define PFIX_IE_protocolIdentifier 4
  32. #define PFIX_IE_ipClassOfService 5
  33. #define PFIX_IE_sourceTransportPort 7
  34. #define PFIX_IE_sourceIPv4Address 8
  35. #define PFIX_IE_ingressInterface 10
  36. #define PFIX_IE_destinationTransportPort 11
  37. #define PFIX_IE_destinationIPv4Address 12
  38. #define PFIX_IE_egressInterface 14
  39. #define PFIX_IE_flowEndSysUpTime 21
  40. #define PFIX_IE_flowStartSysUpTime 22
  41. #define PFIX_IE_sourceIPv6Address 27
  42. #define PFIX_IE_destinationIPv6Address 28
  43. #define PFIX_IE_flowStartMilliseconds 152
  44. #define PFIX_IE_flowEndMilliseconds 153
  45. struct pflow_flow {
  46. u_int32_t src_ip;
  47. u_int32_t dest_ip;
  48. u_int32_t nexthop_ip;
  49. u_int16_t if_index_in;
  50. u_int16_t if_index_out;
  51. u_int32_t flow_packets;
  52. u_int32_t flow_octets;
  53. u_int32_t flow_start;
  54. u_int32_t flow_finish;
  55. u_int16_t src_port;
  56. u_int16_t dest_port;
  57. u_int8_t pad1;
  58. u_int8_t tcp_flags;
  59. u_int8_t protocol;
  60. u_int8_t tos;
  61. u_int16_t src_as;
  62. u_int16_t dest_as;
  63. u_int8_t src_mask;
  64. u_int8_t dest_mask;
  65. u_int16_t pad2;
  66. } __packed;
  67. struct pflow_set_header {
  68. u_int16_t set_id;
  69. u_int16_t set_length; /* total length of the set,
  70. in octets, including the set header */
  71. } __packed;
  72. #define PFLOW_SET_HDRLEN sizeof(struct pflow_set_header)
  73. struct pflow_tmpl_hdr {
  74. u_int16_t tmpl_id;
  75. u_int16_t field_count;
  76. } __packed;
  77. struct pflow_tmpl_fspec {
  78. u_int16_t field_id;
  79. u_int16_t len;
  80. } __packed;
  81. /* update pflow_clone_create() when changing pflow_ipfix_tmpl_ipv4 */
  82. struct pflow_ipfix_tmpl_ipv4 {
  83. struct pflow_tmpl_hdr h;
  84. struct pflow_tmpl_fspec src_ip;
  85. struct pflow_tmpl_fspec dest_ip;
  86. struct pflow_tmpl_fspec if_index_in;
  87. struct pflow_tmpl_fspec if_index_out;
  88. struct pflow_tmpl_fspec packets;
  89. struct pflow_tmpl_fspec octets;
  90. struct pflow_tmpl_fspec start;
  91. struct pflow_tmpl_fspec finish;
  92. struct pflow_tmpl_fspec src_port;
  93. struct pflow_tmpl_fspec dest_port;
  94. struct pflow_tmpl_fspec tos;
  95. struct pflow_tmpl_fspec protocol;
  96. #define PFLOW_IPFIX_TMPL_IPV4_FIELD_COUNT 12
  97. #define PFLOW_IPFIX_TMPL_IPV4_ID 256
  98. } __packed;
  99. /* update pflow_clone_create() when changing pflow_ipfix_tmpl_v6 */
  100. struct pflow_ipfix_tmpl_ipv6 {
  101. struct pflow_tmpl_hdr h;
  102. struct pflow_tmpl_fspec src_ip;
  103. struct pflow_tmpl_fspec dest_ip;
  104. struct pflow_tmpl_fspec if_index_in;
  105. struct pflow_tmpl_fspec if_index_out;
  106. struct pflow_tmpl_fspec packets;
  107. struct pflow_tmpl_fspec octets;
  108. struct pflow_tmpl_fspec start;
  109. struct pflow_tmpl_fspec finish;
  110. struct pflow_tmpl_fspec src_port;
  111. struct pflow_tmpl_fspec dest_port;
  112. struct pflow_tmpl_fspec tos;
  113. struct pflow_tmpl_fspec protocol;
  114. #define PFLOW_IPFIX_TMPL_IPV6_FIELD_COUNT 12
  115. #define PFLOW_IPFIX_TMPL_IPV6_ID 257
  116. } __packed;
  117. struct pflow_ipfix_tmpl {
  118. struct pflow_set_header set_header;
  119. struct pflow_ipfix_tmpl_ipv4 ipv4_tmpl;
  120. struct pflow_ipfix_tmpl_ipv6 ipv6_tmpl;
  121. } __packed;
  122. struct pflow_ipfix_flow4 {
  123. u_int32_t src_ip; /* sourceIPv4Address*/
  124. u_int32_t dest_ip; /* destinationIPv4Address */
  125. u_int32_t if_index_in; /* ingressInterface */
  126. u_int32_t if_index_out; /* egressInterface */
  127. u_int64_t flow_packets; /* packetDeltaCount */
  128. u_int64_t flow_octets; /* octetDeltaCount */
  129. int64_t flow_start; /* flowStartMilliseconds */
  130. int64_t flow_finish; /* flowEndMilliseconds */
  131. u_int16_t src_port; /* sourceTransportPort */
  132. u_int16_t dest_port; /* destinationTransportPort */
  133. u_int8_t tos; /* ipClassOfService */
  134. u_int8_t protocol; /* protocolIdentifier */
  135. /* XXX padding needed? */
  136. } __packed;
  137. struct pflow_ipfix_flow6 {
  138. struct in6_addr src_ip; /* sourceIPv6Address */
  139. struct in6_addr dest_ip; /* destinationIPv6Address */
  140. u_int32_t if_index_in; /* ingressInterface */
  141. u_int32_t if_index_out; /* egressInterface */
  142. u_int64_t flow_packets; /* packetDeltaCount */
  143. u_int64_t flow_octets; /* octetDeltaCount */
  144. int64_t flow_start; /* flowStartMilliseconds */
  145. int64_t flow_finish; /* flowEndMilliseconds */
  146. u_int16_t src_port; /* sourceTransportPort */
  147. u_int16_t dest_port; /* destinationTransportPort */
  148. u_int8_t tos; /* ipClassOfService */
  149. u_int8_t protocol; /* protocolIdentifier */
  150. /* XXX padding needed? */
  151. } __packed;
  152. #ifdef _KERNEL
  153. struct pflow_softc {
  154. struct ifnet sc_if;
  155. struct ifnet *sc_pflow_ifp;
  156. unsigned int sc_count;
  157. unsigned int sc_count4;
  158. unsigned int sc_count6;
  159. unsigned int sc_maxcount;
  160. unsigned int sc_maxcount4;
  161. unsigned int sc_maxcount6;
  162. u_int64_t sc_gcounter;
  163. u_int32_t sc_sequence;
  164. struct timeout sc_tmo;
  165. struct timeout sc_tmo6;
  166. struct timeout sc_tmo_tmpl;
  167. struct socket *so;
  168. struct mbuf *send_nam;
  169. struct in_addr sc_sender_ip;
  170. struct in_addr sc_receiver_ip;
  171. u_int16_t sc_receiver_port;
  172. u_char sc_send_templates;
  173. struct pflow_ipfix_tmpl sc_tmpl_ipfix;
  174. u_int8_t sc_version;
  175. struct mbuf *sc_mbuf; /* current cumulative mbuf */
  176. struct mbuf *sc_mbuf6; /* current cumulative mbuf */
  177. SLIST_ENTRY(pflow_softc) sc_next;
  178. };
  179. extern struct pflow_softc *pflowif;
  180. #endif /* _KERNEL */
  181. struct pflow_header {
  182. u_int16_t version;
  183. u_int16_t count;
  184. u_int32_t uptime_ms;
  185. u_int32_t time_sec;
  186. u_int32_t time_nanosec;
  187. u_int32_t flow_sequence;
  188. u_int8_t engine_type;
  189. u_int8_t engine_id;
  190. u_int8_t reserved1;
  191. u_int8_t reserved2;
  192. } __packed;
  193. #define PFLOW_HDRLEN sizeof(struct pflow_header)
  194. struct pflow_v10_header {
  195. u_int16_t version;
  196. u_int16_t length;
  197. u_int32_t time_sec;
  198. u_int32_t flow_sequence;
  199. u_int32_t observation_dom;
  200. } __packed;
  201. #define PFLOW_IPFIX_HDRLEN sizeof(struct pflow_v10_header)
  202. struct pflowstats {
  203. u_int64_t pflow_flows;
  204. u_int64_t pflow_packets;
  205. u_int64_t pflow_onomem;
  206. u_int64_t pflow_oerrors;
  207. };
  208. /* Supported flow protocols */
  209. #define PFLOW_PROTO_5 5 /* original pflow */
  210. #define PFLOW_PROTO_10 10 /* ipfix */
  211. #define PFLOW_PROTO_MAX 11
  212. #define PFLOW_PROTO_DEFAULT PFLOW_PROTO_5
  213. struct pflow_protos {
  214. const char *ppr_name;
  215. u_int8_t ppr_proto;
  216. };
  217. #define PFLOW_PROTOS { \
  218. { "5", PFLOW_PROTO_5 }, \
  219. { "10", PFLOW_PROTO_10 }, \
  220. }
  221. /*
  222. * Configuration structure for SIOCSETPFLOW SIOCGETPFLOW
  223. */
  224. struct pflowreq {
  225. struct in_addr sender_ip;
  226. struct in_addr receiver_ip;
  227. u_int16_t receiver_port;
  228. u_int16_t addrmask;
  229. u_int8_t version;
  230. #define PFLOW_MASK_SRCIP 0x01
  231. #define PFLOW_MASK_DSTIP 0x02
  232. #define PFLOW_MASK_DSTPRT 0x04
  233. #define PFLOW_MASK_VERSION 0x08
  234. };
  235. #ifdef _KERNEL
  236. int export_pflow(struct pf_state *);
  237. int pflow_sysctl(int *, u_int, void *, size_t *, void *, size_t);
  238. #endif /* _KERNEL */
  239. #endif /* _NET_IF_PFLOW_H_ */