cherry-pick.softflowd-0.9.9-12-g8ea92c3.new-implementation-of-ipfix-netflow-v9-for-supporting-vlan-and-mac-address.patch 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. Subject: New implementation of IPFIX/Netflow v9 for supporting VLAN and Mac-address
  2. Origin: softflowd-0.9.9-12-g8ea92c3 <https://github.com/irino/softflowd/commit/softflowd-0.9.9-12-g8ea92c3>
  3. Upstream-Author: Hitoshi Irino <irino@sfc.wide.ad.jp>
  4. Date: Sun Aug 6 08:17:43 2017 +0900
  5. --- a/common.h
  6. +++ b/common.h
  7. @@ -45,6 +45,7 @@
  8. #include <netinet/tcp.h>
  9. #include <netinet/udp.h>
  10. #include <arpa/inet.h>
  11. +#include <net/ethernet.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. --- a/ipfix.c
  15. +++ b/ipfix.c
  16. @@ -11,7 +11,7 @@
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. @@ -29,1048 +29,1173 @@
  26. #include "softflowd.h"
  27. #if defined (HAVE_DECL_HTONLL) && !defined (HAVE_DECL_HTOBE64)
  28. -#define htobe64 htonll
  29. +#define htobe64 htonll
  30. #endif
  31. -#define JAN_1970 2208988800UL /* 1970 - 1900 in seconds */
  32. +#define JAN_1970 2208988800UL /* 1970 - 1900 in seconds */
  33. /* IPFIX a.k.a. Netflow v.10 */
  34. -struct IPFIX_HEADER {
  35. - u_int16_t version, length;
  36. - u_int32_t export_time; /* in seconds */
  37. - u_int32_t sequence, od_id;
  38. -} __packed;
  39. -struct IPFIX_SET_HEADER {
  40. - u_int16_t set_id, length;
  41. -} __packed;
  42. -struct IPFIX_TEMPLATE_RECORD_HEADER {
  43. - u_int16_t template_id, count;
  44. -} __packed;
  45. -struct IPFIX_TEMPLATE_SET_HEADER {
  46. - struct IPFIX_SET_HEADER c;
  47. - struct IPFIX_TEMPLATE_RECORD_HEADER r;
  48. -} __packed;
  49. -struct IPFIX_OPTION_TEMPLATE_SET_HEADER {
  50. - struct IPFIX_SET_HEADER c;
  51. - struct IPFIX_TEMPLATE_RECORD_HEADER r;
  52. - u_int16_t scope_count;
  53. -} __packed;
  54. -struct IPFIX_FIELD_SPECIFIER {
  55. - u_int16_t ie, length;
  56. -} __packed;
  57. -struct IPFIX_VENDOR_FIELD_SPECIFIER {
  58. - u_int16_t ie, length;
  59. - u_int32_t pen;
  60. +struct IPFIX_HEADER
  61. +{
  62. + u_int16_t version, length;
  63. + u_int32_t export_time; /* in seconds */
  64. + u_int32_t sequence, od_id;
  65. +} __packed;
  66. +struct NFLOW9_HEADER
  67. +{
  68. + u_int16_t version, flows;
  69. + u_int32_t uptime_ms;
  70. + u_int32_t export_time; /* in seconds */
  71. + u_int32_t sequence, od_id;
  72. +} __packed;
  73. +struct IPFIX_SET_HEADER
  74. +{
  75. + u_int16_t set_id, length;
  76. +} __packed;
  77. +struct IPFIX_TEMPLATE_RECORD_HEADER
  78. +{
  79. + u_int16_t template_id, count;
  80. +} __packed;
  81. +struct IPFIX_TEMPLATE_SET_HEADER
  82. +{
  83. + struct IPFIX_SET_HEADER c;
  84. + struct IPFIX_TEMPLATE_RECORD_HEADER r;
  85. +} __packed;
  86. +struct IPFIX_OPTION_TEMPLATE_SET_HEADER
  87. +{
  88. + struct IPFIX_SET_HEADER c;
  89. + union
  90. + {
  91. + struct
  92. + {
  93. + struct IPFIX_TEMPLATE_RECORD_HEADER r;
  94. + u_int16_t scope_count;
  95. + } i;
  96. + struct
  97. + {
  98. + u_int16_t template_id;
  99. + u_int16_t scope_length;
  100. + u_int16_t option_length;
  101. + } n;
  102. + } u;
  103. +} __packed;
  104. +struct IPFIX_FIELD_SPECIFIER
  105. +{
  106. + u_int16_t ie, length;
  107. +} __packed;
  108. +struct IPFIX_VENDOR_FIELD_SPECIFIER
  109. +{
  110. + u_int16_t ie, length;
  111. + u_int32_t pen;
  112. } __packed;
  113. #define REVERSE_PEN 29305
  114. -#define IPFIX_TEMPLATE_SET_ID 2
  115. -#define IPFIX_OPTION_TEMPLATE_SET_ID 3
  116. -#define IPFIX_MIN_RECORD_SET_ID 256
  117. +#define NFLOW9_TEMPLATE_SET_ID 0
  118. +#define NFLOW9_OPTION_TEMPLATE_SET_ID 1
  119. +#define IPFIX_TEMPLATE_SET_ID 2
  120. +#define IPFIX_OPTION_TEMPLATE_SET_ID 3
  121. +#define IPFIX_MIN_RECORD_SET_ID 256
  122. /* Flowset record ies the we care about */
  123. -#define IPFIX_octetDeltaCount 1
  124. -#define IPFIX_packetDeltaCount 2
  125. +#define IPFIX_octetDeltaCount 1
  126. +#define IPFIX_packetDeltaCount 2
  127. /* ... */
  128. -#define IPFIX_protocolIdentifier 4
  129. -#define IPFIX_ipClassOfService 5
  130. +#define IPFIX_protocolIdentifier 4
  131. +#define IPFIX_ipClassOfService 5
  132. /* ... */
  133. -#define IPFIX_tcpControlBits 6
  134. -#define IPFIX_sourceTransportPort 7
  135. -#define IPFIX_sourceIPv4Address 8
  136. +#define IPFIX_tcpControlBits 6
  137. +#define IPFIX_sourceTransportPort 7
  138. +#define IPFIX_sourceIPv4Address 8
  139. /* ... */
  140. -#define IPFIX_ingressInterface 10
  141. -#define IPFIX_destinationTransportPort 11
  142. -#define IPFIX_destinationIPv4Address 12
  143. +#define IPFIX_ingressInterface 10
  144. +#define IPFIX_destinationTransportPort 11
  145. +#define IPFIX_destinationIPv4Address 12
  146. /* ... */
  147. -#define IPFIX_egressInterface 14
  148. +#define IPFIX_egressInterface 14
  149. /* ... */
  150. -#define IPFIX_flowEndSysUpTime 21
  151. -#define IPFIX_flowStartSysUpTime 22
  152. +#define IPFIX_flowEndSysUpTime 21
  153. +#define IPFIX_flowStartSysUpTime 22
  154. /* ... */
  155. -#define IPFIX_sourceIPv6Address 27
  156. -#define IPFIX_destinationIPv6Address 28
  157. +#define IPFIX_sourceIPv6Address 27
  158. +#define IPFIX_destinationIPv6Address 28
  159. /* ... */
  160. -#define IPFIX_icmpTypeCodeIPv4 32
  161. +#define IPFIX_icmpTypeCodeIPv4 32
  162. /* ... */
  163. +#define NFLOW9_SAMPLING_INTERVAL 34
  164. +#define NFLOW9_SAMPLING_ALGORITHM 35
  165. /* ... */
  166. -#define IPFIX_vlanId 58
  167. +#define IPFIX_sourceMacAddress 56
  168. +#define IPFIX_postDestinationMacAddress 57
  169. +#define IPFIX_vlanId 58
  170. +#define IPFIX_postVlanId 59
  171. -#define IPFIX_ipVersion 60
  172. +#define IPFIX_ipVersion 60
  173. /* ... */
  174. -#define IPFIX_icmpTypeCodeIPv6 139
  175. +#define IPFIX_icmpTypeCodeIPv6 139
  176. /* ... */
  177. -#define IPFIX_meteringProcessId 143
  178. +#define IPFIX_meteringProcessId 143
  179. /* ... */
  180. -#define IPFIX_flowStartSeconds 150
  181. -#define IPFIX_flowEndSeconds 151
  182. -#define IPFIX_flowStartMilliSeconds 152
  183. -#define IPFIX_flowEndMilliSeconds 153
  184. -#define IPFIX_flowStartMicroSeconds 154
  185. -#define IPFIX_flowEndMicroSeconds 155
  186. -#define IPFIX_flowStartNanoSeconds 156
  187. -#define IPFIX_flowEndNanoSeconds 157
  188. +#define IPFIX_flowStartSeconds 150
  189. +#define IPFIX_flowEndSeconds 151
  190. +#define IPFIX_flowStartMilliSeconds 152
  191. +#define IPFIX_flowEndMilliSeconds 153
  192. +#define IPFIX_flowStartMicroSeconds 154
  193. +#define IPFIX_flowEndMicroSeconds 155
  194. +#define IPFIX_flowStartNanoSeconds 156
  195. +#define IPFIX_flowEndNanoSeconds 157
  196. /* ... */
  197. -#define IPFIX_systemInitTimeMilliseconds 160
  198. +#define IPFIX_systemInitTimeMilliseconds 160
  199. /* ... */
  200. -#define PSAMP_selectorAlgorithm 304
  201. -#define PSAMP_samplingPacketInterval 305
  202. -#define PSAMP_samplingPacketSpace 306
  203. +#define PSAMP_selectorAlgorithm 304
  204. +#define PSAMP_samplingPacketInterval 305
  205. +#define PSAMP_samplingPacketSpace 306
  206. +
  207. +#define PSAMP_selectorAlgorithm_count 1
  208. +
  209. +#define NFLOW9_OPTION_SCOPE_INTERFACE 2
  210. +#define NFLOW9_SAMPLING_ALGORITHM_DETERMINISTIC 1
  211. +
  212. +const struct IPFIX_FIELD_SPECIFIER field_v4[] = {
  213. + {IPFIX_sourceIPv4Address, 4},
  214. + {IPFIX_destinationIPv4Address, 4}
  215. +};
  216. +
  217. +const struct IPFIX_FIELD_SPECIFIER field_v6[] = {
  218. + {IPFIX_sourceIPv6Address, 16},
  219. + {IPFIX_destinationIPv6Address, 16}
  220. +};
  221. +
  222. +const struct IPFIX_FIELD_SPECIFIER field_common[] = {
  223. + {IPFIX_octetDeltaCount, 4},
  224. + {IPFIX_packetDeltaCount, 4},
  225. + {IPFIX_ingressInterface, 4},
  226. + {IPFIX_egressInterface, 4}
  227. +};
  228. +
  229. +const struct IPFIX_FIELD_SPECIFIER field_transport[] = {
  230. + {IPFIX_sourceTransportPort, 2},
  231. + {IPFIX_destinationTransportPort, 2},
  232. + {IPFIX_protocolIdentifier, 1},
  233. + {IPFIX_tcpControlBits, 1},
  234. + {IPFIX_ipVersion, 1},
  235. + {IPFIX_ipClassOfService, 1}
  236. +};
  237. +
  238. +const struct IPFIX_FIELD_SPECIFIER field_icmp4[] = {
  239. + {IPFIX_icmpTypeCodeIPv4, 2},
  240. + {IPFIX_ipVersion, 1},
  241. + {IPFIX_ipClassOfService, 1}
  242. +};
  243. +
  244. +const struct IPFIX_FIELD_SPECIFIER field_icmp6[] = {
  245. + {IPFIX_icmpTypeCodeIPv6, 2},
  246. + {IPFIX_ipVersion, 1},
  247. + {IPFIX_ipClassOfService, 1}
  248. +};
  249. +
  250. +const struct IPFIX_FIELD_SPECIFIER field_vlan[] = {
  251. + {IPFIX_vlanId, 2},
  252. + {IPFIX_postVlanId, 2}
  253. +};
  254. +
  255. +const struct IPFIX_FIELD_SPECIFIER field_ether[] = {
  256. + {IPFIX_sourceMacAddress, 6},
  257. + {IPFIX_postDestinationMacAddress, 6}
  258. +};
  259. +
  260. +const struct IPFIX_FIELD_SPECIFIER field_timesec[] = {
  261. + {IPFIX_flowStartSeconds, 4},
  262. + {IPFIX_flowEndSeconds, 4}
  263. +};
  264. +
  265. +const struct IPFIX_FIELD_SPECIFIER field_timemsec[] = {
  266. + {IPFIX_flowStartMilliSeconds, 8},
  267. + {IPFIX_flowEndMilliSeconds, 8}
  268. +};
  269. +
  270. +const struct IPFIX_FIELD_SPECIFIER field_timeusec[] = {
  271. + {IPFIX_flowStartMicroSeconds, 8},
  272. + {IPFIX_flowEndMicroSeconds, 8}
  273. +};
  274. +
  275. +const struct IPFIX_FIELD_SPECIFIER field_timensec[] = {
  276. + {IPFIX_flowStartNanoSeconds, 8},
  277. + {IPFIX_flowEndNanoSeconds, 8}
  278. +};
  279. +
  280. +const struct IPFIX_FIELD_SPECIFIER field_timesysup[] = {
  281. + {IPFIX_flowStartSysUpTime, 4},
  282. + {IPFIX_flowEndSysUpTime, 4}
  283. +};
  284. +
  285. +const struct IPFIX_FIELD_SPECIFIER field_bicommon[] = {
  286. + {IPFIX_octetDeltaCount, 4},
  287. + {IPFIX_packetDeltaCount, 4},
  288. + {IPFIX_ipClassOfService, 1}
  289. +};
  290. -#define PSAMP_selectorAlgorithm_count 1
  291. +const struct IPFIX_FIELD_SPECIFIER field_bitransport[] =
  292. + { {IPFIX_tcpControlBits, 1} };
  293. +
  294. +const struct IPFIX_FIELD_SPECIFIER field_biicmp4[] =
  295. + { {IPFIX_icmpTypeCodeIPv4, 2} };
  296. +
  297. +const struct IPFIX_FIELD_SPECIFIER field_biicmp6[] =
  298. + { {IPFIX_icmpTypeCodeIPv6, 2} };
  299. +
  300. +const struct IPFIX_FIELD_SPECIFIER field_scope[] =
  301. + { {IPFIX_meteringProcessId, 4} };
  302. +
  303. +const struct IPFIX_FIELD_SPECIFIER field_option[] = {
  304. + {IPFIX_systemInitTimeMilliseconds, 8},
  305. + {PSAMP_samplingPacketInterval, 4},
  306. + {PSAMP_samplingPacketSpace, 4},
  307. + {PSAMP_selectorAlgorithm, 2}
  308. +};
  309. +
  310. +const struct IPFIX_FIELD_SPECIFIER field_nf9scope[] =
  311. + { {NFLOW9_OPTION_SCOPE_INTERFACE, 4} };
  312. +
  313. +const struct IPFIX_FIELD_SPECIFIER field_nf9option[] = {
  314. + {NFLOW9_SAMPLING_INTERVAL, 4},
  315. + {NFLOW9_SAMPLING_ALGORITHM, 1}
  316. +};
  317. /* Stuff pertaining to the templates that softflowd uses */
  318. -#define IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS 14
  319. -#define IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS 2
  320. -#define IPFIX_SOFTFLOWD_TEMPLATE_VENDORRECORDS 5
  321. -
  322. -#define IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS \
  323. - IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS + \
  324. - IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS
  325. -
  326. -#define IPFIX_SOFTFLOWD_TEMPLATE_BIDIRECTION_NRECORDS \
  327. - IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS + \
  328. - IPFIX_SOFTFLOWD_TEMPLATE_VENDORRECORDS + \
  329. - IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS
  330. -
  331. -struct IPFIX_SOFTFLOWD_TEMPLATE {
  332. - struct IPFIX_TEMPLATE_SET_HEADER h;
  333. - struct IPFIX_FIELD_SPECIFIER r[IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS];
  334. -} __packed;
  335. -
  336. -struct IPFIX_SOFTFLOWD_BIDIRECTION_TEMPLATE {
  337. - struct IPFIX_TEMPLATE_SET_HEADER h;
  338. - struct IPFIX_FIELD_SPECIFIER r[IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS];
  339. - struct IPFIX_VENDOR_FIELD_SPECIFIER v[IPFIX_SOFTFLOWD_TEMPLATE_VENDORRECORDS];
  340. - struct IPFIX_FIELD_SPECIFIER t[IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS];
  341. -} __packed;
  342. -
  343. -#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS 1
  344. -#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS 4
  345. -struct IPFIX_SOFTFLOWD_OPTION_TEMPLATE {
  346. - struct IPFIX_OPTION_TEMPLATE_SET_HEADER h;
  347. - struct IPFIX_FIELD_SPECIFIER s[IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS];
  348. - struct IPFIX_FIELD_SPECIFIER r[IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS];
  349. +#define IPFIX_SOFTFLOWD_TEMPLATE_IPRECORDS \
  350. + sizeof(field_v4) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  351. +#define IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS \
  352. + sizeof(field_timesysup) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  353. +#define IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS \
  354. + sizeof(field_common) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  355. +#define IPFIX_SOFTFLOWD_TEMPLATE_TRANSPORTRECORDS \
  356. + sizeof(field_transport) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  357. +#define IPFIX_SOFTFLOWD_TEMPLATE_ICMPRECORDS \
  358. + sizeof(field_icmp4) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  359. +#define IPFIX_SOFTFLOWD_TEMPLATE_VLANRECORDS \
  360. + sizeof(field_vlan) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  361. +#define IPFIX_SOFTFLOWD_TEMPLATE_ETHERRECORDS \
  362. + sizeof(field_ether) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  363. +//#define IPFIX_SOFTFLOWD_TEMPLATE_VENDORRECORDS 5
  364. +#define IPFIX_SOFTFLOWD_TEMPLATE_BICOMMONRECORDS \
  365. + sizeof(field_bicommon) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  366. +#define IPFIX_SOFTFLOWD_TEMPLATE_BITRANSPORTRECORDS \
  367. + sizeof(field_bitransport) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  368. +#define IPFIX_SOFTFLOWD_TEMPLATE_BIICMPRECORDS \
  369. + sizeof(field_biicmp4) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  370. +
  371. +#define IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS \
  372. + IPFIX_SOFTFLOWD_TEMPLATE_IPRECORDS + \
  373. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS + \
  374. + IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS + \
  375. + IPFIX_SOFTFLOWD_TEMPLATE_TRANSPORTRECORDS + \
  376. + IPFIX_SOFTFLOWD_TEMPLATE_VLANRECORDS + \
  377. + IPFIX_SOFTFLOWD_TEMPLATE_ETHERRECORDS
  378. +
  379. +#define IPFIX_SOFTFLOWD_TEMPLATE_BI_NRECORDS \
  380. + IPFIX_SOFTFLOWD_TEMPLATE_BICOMMONRECORDS + \
  381. + IPFIX_SOFTFLOWD_TEMPLATE_BITRANSPORTRECORDS
  382. +
  383. +struct IPFIX_SOFTFLOWD_TEMPLATE
  384. +{
  385. + struct IPFIX_TEMPLATE_SET_HEADER h;
  386. + struct IPFIX_FIELD_SPECIFIER r[IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS];
  387. + struct IPFIX_VENDOR_FIELD_SPECIFIER
  388. + v[IPFIX_SOFTFLOWD_TEMPLATE_BI_NRECORDS];
  389. + u_int16_t data_len, bi_count;
  390. +} __packed;
  391. +
  392. +#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS \
  393. + sizeof(field_scope) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  394. +#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS \
  395. + sizeof(field_option) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  396. +
  397. +#define NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS \
  398. + sizeof(field_nf9scope) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  399. +#define NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS \
  400. + sizeof(field_nf9option) / sizeof(struct IPFIX_FIELD_SPECIFIER)
  401. +
  402. +
  403. +struct IPFIX_SOFTFLOWD_OPTION_TEMPLATE
  404. +{
  405. + struct IPFIX_OPTION_TEMPLATE_SET_HEADER h;
  406. + struct IPFIX_FIELD_SPECIFIER
  407. + s[IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS];
  408. + struct IPFIX_FIELD_SPECIFIER r[IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS];
  409. } __packed;
  410. /* softflowd data set */
  411. -struct IPFIX_SOFTFLOWD_DATA_COMMON {
  412. - u_int32_t octetDeltaCount, packetDeltaCount;
  413. - u_int32_t ingressInterface, egressInterface;
  414. - u_int16_t sourceTransportPort, destinationTransportPort;
  415. - u_int8_t protocolIdentifier, tcpControlBits, ipVersion, ipClassOfService;
  416. - u_int16_t icmpTypeCode, vlanId;
  417. -} __packed;
  418. -
  419. -struct IPFIX_SOFTFLOWD_DATA_BIDIRECTION {
  420. - u_int32_t octetDeltaCount, packetDeltaCount;
  421. - u_int8_t tcpControlBits, ipClassOfService;
  422. - u_int16_t icmpTypeCode;
  423. -} __packed;
  424. -
  425. -union IPFIX_SOFTFLOWD_DATA_TIME {
  426. - struct {
  427. - u_int32_t start;
  428. - u_int32_t end;
  429. - } u32;
  430. - struct {
  431. - u_int64_t start;
  432. - u_int64_t end;
  433. - } u64;
  434. -};
  435. +struct IPFIX_SOFTFLOWD_DATA_COMMON
  436. +{
  437. + u_int32_t octetDeltaCount, packetDeltaCount;
  438. + u_int32_t ingressInterface, egressInterface;
  439. +} __packed;
  440. +
  441. +struct IPFIX_SOFTFLOWD_DATA_TRANSPORT
  442. +{
  443. + u_int16_t sourceTransportPort, destinationTransportPort;
  444. + u_int8_t protocolIdentifier, tcpControlBits, ipVersion, ipClassOfService;
  445. +} __packed;
  446. +
  447. +struct IPFIX_SOFTFLOWD_DATA_ICMP
  448. +{
  449. + u_int16_t icmpTypeCode;
  450. + u_int8_t ipVersion, ipClassOfService;
  451. +} __packed;
  452. +
  453. +struct IPFIX_SOFTFLOWD_DATA_VLAN
  454. +{
  455. + u_int16_t vlanId, postVlanId;
  456. +} __packed;
  457. -struct IPFIX_SOFTFLOWD_DATA_V4 {
  458. - u_int32_t sourceIPv4Address, destinationIPv4Address;
  459. - struct IPFIX_SOFTFLOWD_DATA_COMMON c;
  460. - union IPFIX_SOFTFLOWD_DATA_TIME t;
  461. +struct IPFIX_SOFTFLOWD_DATA_ETHER
  462. +{
  463. + u_int8_t sourceMacAddress[6], destinationMacAddress[6];
  464. } __packed;
  465. -struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V4 {
  466. - u_int32_t sourceIPv4Address, destinationIPv4Address;
  467. - struct IPFIX_SOFTFLOWD_DATA_COMMON c;
  468. - struct IPFIX_SOFTFLOWD_DATA_BIDIRECTION b;
  469. - union IPFIX_SOFTFLOWD_DATA_TIME t;
  470. +struct IPFIX_SOFTFLOWD_DATA_BICOMMON
  471. +{
  472. + u_int32_t octetDeltaCount, packetDeltaCount;
  473. + u_int8_t ipClassOfService;
  474. } __packed;
  475. -struct IPFIX_SOFTFLOWD_DATA_V6 {
  476. - struct in6_addr sourceIPv6Address, destinationIPv6Address;
  477. - struct IPFIX_SOFTFLOWD_DATA_COMMON c;
  478. - union IPFIX_SOFTFLOWD_DATA_TIME t;
  479. +struct IPFIX_SOFTFLOWD_DATA_BITRANSPORT
  480. +{
  481. + u_int8_t tcpControlBits;
  482. } __packed;
  483. -struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V6 {
  484. - struct in6_addr sourceIPv6Address, destinationIPv6Address;
  485. - struct IPFIX_SOFTFLOWD_DATA_COMMON c;
  486. - struct IPFIX_SOFTFLOWD_DATA_BIDIRECTION b;
  487. - union IPFIX_SOFTFLOWD_DATA_TIME t;
  488. +struct IPFIX_SOFTFLOWD_DATA_BIICMP
  489. +{
  490. + u_int16_t icmpTypeCode;
  491. } __packed;
  492. -struct IPFIX_SOFTFLOWD_OPTION_DATA {
  493. - struct IPFIX_SET_HEADER c;
  494. - u_int32_t scope_pid;
  495. - u_int64_t systemInitTimeMilliseconds;
  496. - u_int16_t samplingAlgorithm;
  497. - u_int16_t samplingInterval;
  498. - u_int32_t samplingSpace;
  499. +union IPFIX_SOFTFLOWD_DATA_TIME
  500. +{
  501. + struct
  502. + {
  503. + u_int32_t start;
  504. + u_int32_t end;
  505. + } u32;
  506. + struct
  507. + {
  508. + u_int64_t start;
  509. + u_int64_t end;
  510. + } u64;
  511. +};
  512. +
  513. +struct IPFIX_SOFTFLOWD_DATA_V4ADDR
  514. +{
  515. + u_int32_t sourceIPv4Address, destinationIPv4Address;
  516. +} __packed;
  517. +
  518. +struct IPFIX_SOFTFLOWD_DATA_V6ADDR
  519. +{
  520. + struct in6_addr sourceIPv6Address, destinationIPv6Address;
  521. } __packed;
  522. -
  523. +
  524. +struct IPFIX_SOFTFLOWD_OPTION_DATA
  525. +{
  526. + struct IPFIX_SET_HEADER c;
  527. + u_int32_t scope_pid;
  528. + u_int64_t systemInitTimeMilliseconds;
  529. + u_int32_t samplingInterval;
  530. + u_int32_t samplingSpace;
  531. + u_int16_t samplingAlgorithm;
  532. +} __packed;
  533. +
  534. +struct NFLOW9_SOFTFLOWD_OPTION_DATA
  535. +{
  536. + struct IPFIX_SET_HEADER c;
  537. + u_int32_t scope_ifidx;
  538. + u_int32_t samplingInterval;
  539. + u_int8_t samplingAlgorithm;
  540. +} __packed;
  541. +
  542. /* Local data: templates and counters */
  543. -#define IPFIX_SOFTFLOWD_MAX_PACKET_SIZE 512
  544. -#define IPFIX_SOFTFLOWD_V4_TEMPLATE_ID 1024
  545. -#define IPFIX_SOFTFLOWD_V6_TEMPLATE_ID 2048
  546. -#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID 256
  547. +#define IPFIX_SOFTFLOWD_MAX_PACKET_SIZE 1428
  548. +#define IPFIX_SOFTFLOWD_V4_TEMPLATE_ID 1024
  549. +#define IPFIX_SOFTFLOWD_ICMPV4_TEMPLATE_ID 1025
  550. +#define IPFIX_SOFTFLOWD_V6_TEMPLATE_ID 2048
  551. +#define IPFIX_SOFTFLOWD_ICMPV6_TEMPLATE_ID 2049
  552. +#define IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID 256
  553. -#define IPFIX_DEFAULT_TEMPLATE_INTERVAL 16
  554. +#define IPFIX_DEFAULT_TEMPLATE_INTERVAL 16
  555. /* ... */
  556. -#define IPFIX_OPTION_SCOPE_SYSTEM 1
  557. -#define IPFIX_OPTION_SCOPE_INTERFACE 2
  558. -#define IPFIX_OPTION_SCOPE_LINECARD 3
  559. -#define IPFIX_OPTION_SCOPE_CACHE 4
  560. -#define IPFIX_OPTION_SCOPE_TEMPLATE 5
  561. +#define IPFIX_OPTION_SCOPE_SYSTEM 1
  562. +#define IPFIX_OPTION_SCOPE_INTERFACE 2
  563. +#define IPFIX_OPTION_SCOPE_LINECARD 3
  564. +#define IPFIX_OPTION_SCOPE_CACHE 4
  565. +#define IPFIX_OPTION_SCOPE_TEMPLATE 5
  566. /* ... */
  567. -#define IPFIX_SAMPLING_ALGORITHM_DETERMINISTIC 1
  568. -#define IPFIX_SAMPLING_ALGORITHM_RANDOM 2
  569. +#define IPFIX_SAMPLING_ALGORITHM_DETERMINISTIC 1
  570. +#define IPFIX_SAMPLING_ALGORITHM_RANDOM 2
  571. /* ... */
  572. -static struct IPFIX_SOFTFLOWD_TEMPLATE v4_template;
  573. -static struct IPFIX_SOFTFLOWD_TEMPLATE v6_template;
  574. -static struct IPFIX_SOFTFLOWD_BIDIRECTION_TEMPLATE v4_bidirection_template;
  575. -static struct IPFIX_SOFTFLOWD_BIDIRECTION_TEMPLATE v6_bidirection_template;
  576. +enum
  577. +{ TMPLV4, TMPLICMPV4, TMPLV6, TMPLICMPV6, TMPLMAX };
  578. +static struct IPFIX_SOFTFLOWD_TEMPLATE template[TMPLMAX];
  579. static struct IPFIX_SOFTFLOWD_OPTION_TEMPLATE option_template;
  580. static struct IPFIX_SOFTFLOWD_OPTION_DATA option_data;
  581. +static struct NFLOW9_SOFTFLOWD_OPTION_DATA nf9opt_data;
  582. +
  583. static int ipfix_pkts_until_template = -1;
  584. +static int
  585. +ipfix_init_fields (struct IPFIX_FIELD_SPECIFIER *dst,
  586. + u_int * index,
  587. + const struct IPFIX_FIELD_SPECIFIER *src,
  588. + u_int field_number)
  589. +{
  590. + int length = 0;
  591. + for (int i = 0; i < field_number; i++)
  592. + {
  593. + dst[*index + i].ie = htons (src[i].ie);
  594. + dst[*index + i].length = htons (src[i].length);
  595. + length += src[i].length;
  596. + }
  597. + *index += field_number;
  598. + return length;
  599. +}
  600. +
  601. +static int
  602. +ipfix_init_bifields (struct IPFIX_SOFTFLOWD_TEMPLATE *template,
  603. + u_int * index,
  604. + const struct IPFIX_FIELD_SPECIFIER *fields,
  605. + u_int field_number)
  606. +{
  607. + int length = 0;
  608. + for (int i = 0; i < field_number; i++)
  609. + {
  610. + template->v[*index + i].ie = htons (fields[i].ie | 0x8000);
  611. + template->v[*index + i].length = htons (fields[i].length);
  612. + template->v[*index + i].pen = htonl (REVERSE_PEN);
  613. + length += fields[i].length;
  614. + }
  615. + *index += field_number;
  616. + return length;
  617. +}
  618. +
  619. +static int
  620. +ipfix_init_template_time (struct FLOWTRACKPARAMETERS *param,
  621. + struct IPFIX_SOFTFLOWD_TEMPLATE *template,
  622. + u_int * index)
  623. +{
  624. + int length = 0;
  625. + if (param->time_format == 's')
  626. + {
  627. + length = ipfix_init_fields (template->r, index,
  628. + field_timesec,
  629. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS);
  630. + }
  631. + else if (param->time_format == 'm')
  632. + {
  633. + length = ipfix_init_fields (template->r, index,
  634. + field_timemsec,
  635. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS);
  636. + }
  637. + else if (param->time_format == 'M')
  638. + {
  639. + length = ipfix_init_fields (template->r, index,
  640. + field_timeusec,
  641. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS);
  642. + }
  643. + else if (param->time_format == 'n')
  644. + {
  645. + length = ipfix_init_fields (template->r, index,
  646. + field_timensec,
  647. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS);
  648. + }
  649. + else
  650. + {
  651. + length = ipfix_init_fields (template->r, index,
  652. + field_timesysup,
  653. + IPFIX_SOFTFLOWD_TEMPLATE_TIMERECORDS);
  654. + }
  655. + return length;
  656. +}
  657. +
  658. static void
  659. -ipfix_init_template(struct FLOWTRACKPARAMETERS *param)
  660. +ipfix_init_template_unity (struct FLOWTRACKPARAMETERS *param,
  661. + struct IPFIX_SOFTFLOWD_TEMPLATE *template,
  662. + u_int template_id, u_int8_t v6_flag,
  663. + u_int8_t icmp_flag, u_int8_t bi_flag,
  664. + u_int16_t version)
  665. {
  666. - bzero(&v4_template, sizeof(v4_template));
  667. - v4_template.h.c.set_id = htons(IPFIX_TEMPLATE_SET_ID);
  668. - v4_template.h.c.length = htons(sizeof(v4_template));
  669. - v4_template.h.r.template_id = htons(IPFIX_SOFTFLOWD_V4_TEMPLATE_ID);
  670. - v4_template.h.r.count = htons(IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS);
  671. - v4_template.r[0].ie = htons(IPFIX_sourceIPv4Address);
  672. - v4_template.r[0].length = htons(4);
  673. - v4_template.r[1].ie = htons(IPFIX_destinationIPv4Address);
  674. - v4_template.r[1].length = htons(4);
  675. - v4_template.r[2].ie = htons(IPFIX_octetDeltaCount);
  676. - v4_template.r[2].length = htons(4);
  677. - v4_template.r[3].ie = htons(IPFIX_packetDeltaCount);
  678. - v4_template.r[3].length = htons(4);
  679. - v4_template.r[4].ie = htons(IPFIX_ingressInterface);
  680. - v4_template.r[4].length = htons(4);
  681. - v4_template.r[5].ie = htons(IPFIX_egressInterface);
  682. - v4_template.r[5].length = htons(4);
  683. - v4_template.r[6].ie = htons(IPFIX_sourceTransportPort);
  684. - v4_template.r[6].length = htons(2);
  685. - v4_template.r[7].ie = htons(IPFIX_destinationTransportPort);
  686. - v4_template.r[7].length = htons(2);
  687. - v4_template.r[8].ie = htons(IPFIX_protocolIdentifier);
  688. - v4_template.r[8].length = htons(1);
  689. - v4_template.r[9].ie = htons(IPFIX_tcpControlBits);
  690. - v4_template.r[9].length = htons(1);
  691. - v4_template.r[10].ie = htons(IPFIX_ipVersion);
  692. - v4_template.r[10].length = htons(1);
  693. - v4_template.r[11].ie = htons(IPFIX_ipClassOfService);
  694. - v4_template.r[11].length = htons(1);
  695. - v4_template.r[12].ie = htons(IPFIX_icmpTypeCodeIPv4);
  696. - v4_template.r[12].length = htons(2);
  697. - v4_template.r[13].ie = htons(IPFIX_vlanId);
  698. - v4_template.r[13].length = htons(2);
  699. - if (param->time_format == 's') {
  700. - v4_template.r[14].ie = htons(IPFIX_flowStartSeconds);
  701. - v4_template.r[14].length = htons(sizeof(u_int32_t));
  702. - v4_template.r[15].ie = htons(IPFIX_flowEndSeconds);
  703. - v4_template.r[15].length = htons(sizeof(u_int32_t));
  704. - } else if (param->time_format == 'm') {
  705. - v4_template.r[14].ie = htons(IPFIX_flowStartMilliSeconds);
  706. - v4_template.r[14].length = htons(sizeof(u_int64_t));
  707. - v4_template.r[15].ie = htons(IPFIX_flowEndMilliSeconds);
  708. - v4_template.r[15].length = htons(sizeof(u_int64_t));
  709. - } else if (param->time_format == 'M') {
  710. - v4_template.r[14].ie = htons(IPFIX_flowStartMicroSeconds);
  711. - v4_template.r[14].length = htons(sizeof(u_int64_t));
  712. - v4_template.r[15].ie = htons(IPFIX_flowEndMicroSeconds);
  713. - v4_template.r[15].length = htons(sizeof(u_int64_t));
  714. - } else if (param->time_format == 'n') {
  715. - v4_template.r[14].ie = htons(IPFIX_flowStartNanoSeconds);
  716. - v4_template.r[14].length = htons(sizeof(u_int64_t));
  717. - v4_template.r[15].ie = htons(IPFIX_flowEndNanoSeconds);
  718. - v4_template.r[15].length = htons(sizeof(u_int64_t));
  719. - } else {
  720. - v4_template.r[14].ie = htons(IPFIX_flowStartSysUpTime);
  721. - v4_template.r[14].length = htons(sizeof(u_int32_t));
  722. - v4_template.r[15].ie = htons(IPFIX_flowEndSysUpTime);
  723. - v4_template.r[15].length = htons(sizeof(u_int32_t));
  724. - }
  725. -
  726. - bzero(&v6_template, sizeof(v6_template));
  727. - v6_template.h.c.set_id = htons(IPFIX_TEMPLATE_SET_ID);
  728. - v6_template.h.c.length = htons(sizeof(v6_template));
  729. - v6_template.h.r.template_id = htons(IPFIX_SOFTFLOWD_V6_TEMPLATE_ID);
  730. - v6_template.h.r.count = htons(IPFIX_SOFTFLOWD_TEMPLATE_NRECORDS);
  731. - v6_template.r[0].ie = htons(IPFIX_sourceIPv6Address);
  732. - v6_template.r[0].length = htons(16);
  733. - v6_template.r[1].ie = htons(IPFIX_destinationIPv6Address);
  734. - v6_template.r[1].length = htons(16);
  735. - v6_template.r[2].ie = htons(IPFIX_octetDeltaCount);
  736. - v6_template.r[2].length = htons(4);
  737. - v6_template.r[3].ie = htons(IPFIX_packetDeltaCount);
  738. - v6_template.r[3].length = htons(4);
  739. - v6_template.r[4].ie = htons(IPFIX_ingressInterface);
  740. - v6_template.r[4].length = htons(4);
  741. - v6_template.r[5].ie = htons(IPFIX_egressInterface);
  742. - v6_template.r[5].length = htons(4);
  743. - v6_template.r[6].ie = htons(IPFIX_sourceTransportPort);
  744. - v6_template.r[6].length = htons(2);
  745. - v6_template.r[7].ie = htons(IPFIX_destinationTransportPort);
  746. - v6_template.r[7].length = htons(2);
  747. - v6_template.r[8].ie = htons(IPFIX_protocolIdentifier);
  748. - v6_template.r[8].length = htons(1);
  749. - v6_template.r[9].ie = htons(IPFIX_tcpControlBits);
  750. - v6_template.r[9].length = htons(1);
  751. - v6_template.r[10].ie = htons(IPFIX_ipVersion);
  752. - v6_template.r[10].length = htons(1);
  753. - v6_template.r[11].ie = htons(IPFIX_ipClassOfService);
  754. - v6_template.r[11].length = htons(1);
  755. - v6_template.r[12].ie = htons(IPFIX_icmpTypeCodeIPv6);
  756. - v6_template.r[12].length = htons(2);
  757. - v6_template.r[13].ie = htons(IPFIX_vlanId);
  758. - v6_template.r[13].length = htons(2);
  759. - if (param->time_format == 's') {
  760. - v6_template.r[14].ie = htons(IPFIX_flowStartSeconds);
  761. - v6_template.r[14].length = htons(sizeof(u_int32_t));
  762. - v6_template.r[15].ie = htons(IPFIX_flowEndSeconds);
  763. - v6_template.r[15].length = htons(sizeof(u_int32_t));
  764. - } else if (param->time_format == 'm') {
  765. - v6_template.r[14].ie = htons(IPFIX_flowStartMilliSeconds);
  766. - v6_template.r[14].length = htons(sizeof(u_int64_t));
  767. - v6_template.r[15].ie = htons(IPFIX_flowEndMilliSeconds);
  768. - v6_template.r[15].length = htons(sizeof(u_int64_t));
  769. - } else if (param->time_format == 'M') {
  770. - v6_template.r[14].ie = htons(IPFIX_flowStartMicroSeconds);
  771. - v6_template.r[14].length = htons(sizeof(u_int64_t));
  772. - v6_template.r[15].ie = htons(IPFIX_flowEndMicroSeconds);
  773. - v6_template.r[15].length = htons(sizeof(u_int64_t));
  774. - } else if (param->time_format == 'n') {
  775. - v6_template.r[14].ie = htons(IPFIX_flowStartNanoSeconds);
  776. - v6_template.r[14].length = htons(sizeof(u_int64_t));
  777. - v6_template.r[15].ie = htons(IPFIX_flowEndNanoSeconds);
  778. - v6_template.r[15].length = htons(sizeof(u_int64_t));
  779. - } else {
  780. - v6_template.r[14].ie = htons(IPFIX_flowStartSysUpTime);
  781. - v6_template.r[14].length = htons(sizeof(u_int32_t));
  782. - v6_template.r[15].ie = htons(IPFIX_flowEndSysUpTime);
  783. - v6_template.r[15].length = htons(sizeof(u_int32_t));
  784. - }
  785. + u_int index = 0, bi_index = 0, length = 0;
  786. + bzero (template, sizeof (*template));
  787. + template->h.c.set_id = htons (version == 10 ?
  788. + IPFIX_TEMPLATE_SET_ID :
  789. + NFLOW9_TEMPLATE_SET_ID);
  790. + template->h.r.template_id = htons (template_id);
  791. + if (v6_flag)
  792. + {
  793. + length += ipfix_init_fields (template->r, &index,
  794. + field_v6,
  795. + IPFIX_SOFTFLOWD_TEMPLATE_IPRECORDS);
  796. + }
  797. + else
  798. + {
  799. + length += ipfix_init_fields (template->r, &index,
  800. + field_v4,
  801. + IPFIX_SOFTFLOWD_TEMPLATE_IPRECORDS);
  802. + }
  803. + length += ipfix_init_template_time (param, template, &index);
  804. + length += ipfix_init_fields (template->r, &index,
  805. + field_common,
  806. + IPFIX_SOFTFLOWD_TEMPLATE_COMMONRECORDS);
  807. + if (icmp_flag)
  808. + {
  809. + if (v6_flag)
  810. + {
  811. + length += ipfix_init_fields (template->r, &index,
  812. + field_icmp6,
  813. + IPFIX_SOFTFLOWD_TEMPLATE_ICMPRECORDS);
  814. + }
  815. + else
  816. + {
  817. + length += ipfix_init_fields (template->r, &index,
  818. + field_icmp4,
  819. + IPFIX_SOFTFLOWD_TEMPLATE_ICMPRECORDS);
  820. + }
  821. + }
  822. + else
  823. + {
  824. + length += ipfix_init_fields (template->r, &index,
  825. + field_transport,
  826. + IPFIX_SOFTFLOWD_TEMPLATE_TRANSPORTRECORDS);
  827. + }
  828. + if (param->track_level >= TRACK_FULL_VLAN)
  829. + {
  830. + length += ipfix_init_fields (template->r, &index,
  831. + field_vlan,
  832. + IPFIX_SOFTFLOWD_TEMPLATE_VLANRECORDS);
  833. + }
  834. + if (param->track_level >= TRACK_FULL_VLAN_ETHER)
  835. + {
  836. + length += ipfix_init_fields (template->r, &index,
  837. + field_ether,
  838. + IPFIX_SOFTFLOWD_TEMPLATE_ETHERRECORDS);
  839. + }
  840. + if (bi_flag)
  841. + {
  842. + length +=
  843. + ipfix_init_bifields (template, &bi_index,
  844. + field_bicommon,
  845. + IPFIX_SOFTFLOWD_TEMPLATE_BICOMMONRECORDS);
  846. + if (icmp_flag)
  847. + {
  848. + if (v6_flag)
  849. + {
  850. + length +=
  851. + ipfix_init_bifields (template, &bi_index,
  852. + field_biicmp6,
  853. + IPFIX_SOFTFLOWD_TEMPLATE_BIICMPRECORDS);
  854. + }
  855. + else
  856. + {
  857. + length +=
  858. + ipfix_init_bifields (template, &bi_index,
  859. + field_biicmp4,
  860. + IPFIX_SOFTFLOWD_TEMPLATE_BIICMPRECORDS);
  861. + }
  862. + }
  863. + else
  864. + {
  865. + length +=
  866. + ipfix_init_bifields (template, &bi_index,
  867. + field_bitransport,
  868. + IPFIX_SOFTFLOWD_TEMPLATE_BITRANSPORTRECORDS);
  869. +
  870. + }
  871. + }
  872. + template->bi_count = bi_index;
  873. + template->h.r.count = htons (index + bi_index);
  874. + template->h.c.length =
  875. + htons (sizeof (struct IPFIX_TEMPLATE_SET_HEADER) +
  876. + index * sizeof (struct IPFIX_FIELD_SPECIFIER) +
  877. + bi_index * sizeof (struct IPFIX_VENDOR_FIELD_SPECIFIER));
  878. + template->data_len = length;
  879. }
  880. static void
  881. -ipfix_init_template_bidirection(struct FLOWTRACKPARAMETERS *param)
  882. +ipfix_init_template (struct FLOWTRACKPARAMETERS *param,
  883. + u_int8_t bi_flag, u_int16_t version)
  884. {
  885. - bzero(&v4_bidirection_template, sizeof(v4_bidirection_template));
  886. - v4_bidirection_template.h.c.set_id = htons(IPFIX_TEMPLATE_SET_ID);
  887. - v4_bidirection_template.h.c.length = htons(sizeof(v4_bidirection_template));
  888. - v4_bidirection_template.h.r.template_id = htons(IPFIX_SOFTFLOWD_V4_TEMPLATE_ID);
  889. - v4_bidirection_template.h.r.count = htons(IPFIX_SOFTFLOWD_TEMPLATE_BIDIRECTION_NRECORDS);
  890. - v4_bidirection_template.r[0].ie = htons(IPFIX_sourceIPv4Address);
  891. - v4_bidirection_template.r[0].length = htons(4);
  892. - v4_bidirection_template.r[1].ie = htons(IPFIX_destinationIPv4Address);
  893. - v4_bidirection_template.r[1].length = htons(4);
  894. - v4_bidirection_template.r[2].ie = htons(IPFIX_octetDeltaCount);
  895. - v4_bidirection_template.r[2].length = htons(4);
  896. - v4_bidirection_template.r[3].ie = htons(IPFIX_packetDeltaCount);
  897. - v4_bidirection_template.r[3].length = htons(4);
  898. - v4_bidirection_template.r[4].ie = htons(IPFIX_ingressInterface);
  899. - v4_bidirection_template.r[4].length = htons(4);
  900. - v4_bidirection_template.r[5].ie = htons(IPFIX_egressInterface);
  901. - v4_bidirection_template.r[5].length = htons(4);
  902. - v4_bidirection_template.r[6].ie = htons(IPFIX_sourceTransportPort);
  903. - v4_bidirection_template.r[6].length = htons(2);
  904. - v4_bidirection_template.r[7].ie = htons(IPFIX_destinationTransportPort);
  905. - v4_bidirection_template.r[7].length = htons(2);
  906. - v4_bidirection_template.r[8].ie = htons(IPFIX_protocolIdentifier);
  907. - v4_bidirection_template.r[8].length = htons(1);
  908. - v4_bidirection_template.r[9].ie = htons(IPFIX_tcpControlBits);
  909. - v4_bidirection_template.r[9].length = htons(1);
  910. - v4_bidirection_template.r[10].ie = htons(IPFIX_ipVersion);
  911. - v4_bidirection_template.r[10].length = htons(1);
  912. - v4_bidirection_template.r[11].ie = htons(IPFIX_ipClassOfService);
  913. - v4_bidirection_template.r[11].length = htons(1);
  914. - v4_bidirection_template.r[12].ie = htons(IPFIX_icmpTypeCodeIPv4);
  915. - v4_bidirection_template.r[12].length = htons(2);
  916. - v4_bidirection_template.r[13].ie = htons(IPFIX_vlanId);
  917. - v4_bidirection_template.r[13].length = htons(2);
  918. - v4_bidirection_template.v[0].ie = htons(IPFIX_octetDeltaCount | 0x8000);
  919. - v4_bidirection_template.v[0].length = htons(4);
  920. - v4_bidirection_template.v[0].pen = htonl(REVERSE_PEN);
  921. - v4_bidirection_template.v[1].ie = htons(IPFIX_packetDeltaCount | 0x8000);
  922. - v4_bidirection_template.v[1].length = htons(4);
  923. - v4_bidirection_template.v[1].pen = htonl(REVERSE_PEN);
  924. - v4_bidirection_template.v[2].ie = htons(IPFIX_tcpControlBits | 0x8000);
  925. - v4_bidirection_template.v[2].length = htons(1);
  926. - v4_bidirection_template.v[2].pen = htonl(REVERSE_PEN);
  927. - v4_bidirection_template.v[3].ie = htons(IPFIX_ipClassOfService | 0x8000);
  928. - v4_bidirection_template.v[3].length = htons(1);
  929. - v4_bidirection_template.v[3].pen = htonl(REVERSE_PEN);
  930. - v4_bidirection_template.v[4].ie = htons(IPFIX_icmpTypeCodeIPv4 | 0x8000);
  931. - v4_bidirection_template.v[4].length = htons(2);
  932. - v4_bidirection_template.v[4].pen = htonl(REVERSE_PEN);
  933. - if (param->time_format == 's') {
  934. - v4_bidirection_template.t[0].ie = htons(IPFIX_flowStartSeconds);
  935. - v4_bidirection_template.t[0].length = htons(sizeof(u_int32_t));
  936. - v4_bidirection_template.t[1].ie = htons(IPFIX_flowEndSeconds);
  937. - v4_bidirection_template.t[1].length = htons(sizeof(u_int32_t));
  938. - } else if (param->time_format == 'm') {
  939. - v4_bidirection_template.t[0].ie = htons(IPFIX_flowStartMilliSeconds);
  940. - v4_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  941. - v4_bidirection_template.t[1].ie = htons(IPFIX_flowEndMilliSeconds);
  942. - v4_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  943. - } else if (param->time_format == 'M') {
  944. - v4_bidirection_template.t[0].ie = htons(IPFIX_flowStartMicroSeconds);
  945. - v4_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  946. - v4_bidirection_template.t[1].ie = htons(IPFIX_flowEndMicroSeconds);
  947. - v4_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  948. - } else if (param->time_format == 'n') {
  949. - v4_bidirection_template.t[0].ie = htons(IPFIX_flowStartNanoSeconds);
  950. - v4_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  951. - v4_bidirection_template.t[1].ie = htons(IPFIX_flowEndNanoSeconds);
  952. - v4_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  953. - } else {
  954. - v4_bidirection_template.t[0].ie = htons(IPFIX_flowStartSysUpTime);
  955. - v4_bidirection_template.t[0].length = htons(sizeof(u_int32_t));
  956. - v4_bidirection_template.t[1].ie = htons(IPFIX_flowEndSysUpTime);
  957. - v4_bidirection_template.t[1].length = htons(sizeof(u_int32_t));
  958. - }
  959. -
  960. - bzero(&v6_bidirection_template, sizeof(v6_bidirection_template));
  961. - v6_bidirection_template.h.c.set_id = htons(IPFIX_TEMPLATE_SET_ID);
  962. - v6_bidirection_template.h.c.length = htons(sizeof(v6_bidirection_template));
  963. - v6_bidirection_template.h.r.template_id = htons(IPFIX_SOFTFLOWD_V6_TEMPLATE_ID);
  964. - v6_bidirection_template.h.r.count = htons(IPFIX_SOFTFLOWD_TEMPLATE_BIDIRECTION_NRECORDS);
  965. - v6_bidirection_template.r[0].ie = htons(IPFIX_sourceIPv6Address);
  966. - v6_bidirection_template.r[0].length = htons(16);
  967. - v6_bidirection_template.r[1].ie = htons(IPFIX_destinationIPv6Address);
  968. - v6_bidirection_template.r[1].length = htons(16);
  969. - v6_bidirection_template.r[2].ie = htons(IPFIX_octetDeltaCount);
  970. - v6_bidirection_template.r[2].length = htons(4);
  971. - v6_bidirection_template.r[3].ie = htons(IPFIX_packetDeltaCount);
  972. - v6_bidirection_template.r[3].length = htons(4);
  973. - v6_bidirection_template.r[4].ie = htons(IPFIX_ingressInterface);
  974. - v6_bidirection_template.r[4].length = htons(4);
  975. - v6_bidirection_template.r[5].ie = htons(IPFIX_egressInterface);
  976. - v6_bidirection_template.r[5].length = htons(4);
  977. - v6_bidirection_template.r[6].ie = htons(IPFIX_sourceTransportPort);
  978. - v6_bidirection_template.r[6].length = htons(2);
  979. - v6_bidirection_template.r[7].ie = htons(IPFIX_destinationTransportPort);
  980. - v6_bidirection_template.r[7].length = htons(2);
  981. - v6_bidirection_template.r[8].ie = htons(IPFIX_protocolIdentifier);
  982. - v6_bidirection_template.r[8].length = htons(1);
  983. - v6_bidirection_template.r[9].ie = htons(IPFIX_tcpControlBits);
  984. - v6_bidirection_template.r[9].length = htons(1);
  985. - v6_bidirection_template.r[10].ie = htons(IPFIX_ipVersion);
  986. - v6_bidirection_template.r[10].length = htons(1);
  987. - v6_bidirection_template.r[11].ie = htons(IPFIX_ipClassOfService);
  988. - v6_bidirection_template.r[11].length = htons(1);
  989. - v6_bidirection_template.r[12].ie = htons(IPFIX_icmpTypeCodeIPv6);
  990. - v6_bidirection_template.r[12].length = htons(2);
  991. - v6_bidirection_template.r[13].ie = htons(IPFIX_vlanId);
  992. - v6_bidirection_template.r[13].length = htons(2);
  993. - v6_bidirection_template.v[0].ie = htons(IPFIX_octetDeltaCount | 0x8000);
  994. - v6_bidirection_template.v[0].length = htons(4);
  995. - v6_bidirection_template.v[0].pen = htonl(REVERSE_PEN);
  996. - v6_bidirection_template.v[1].ie = htons(IPFIX_packetDeltaCount | 0x8000);
  997. - v6_bidirection_template.v[1].length = htons(4);
  998. - v6_bidirection_template.v[1].pen = htonl(REVERSE_PEN);
  999. - v6_bidirection_template.v[2].ie = htons(IPFIX_tcpControlBits | 0x8000);
  1000. - v6_bidirection_template.v[2].length = htons(1);
  1001. - v6_bidirection_template.v[2].pen = htonl(REVERSE_PEN);
  1002. - v6_bidirection_template.v[3].ie = htons(IPFIX_ipClassOfService | 0x8000);
  1003. - v6_bidirection_template.v[3].length = htons(1);
  1004. - v6_bidirection_template.v[3].pen = htonl(REVERSE_PEN);
  1005. - v6_bidirection_template.v[4].ie = htons(IPFIX_icmpTypeCodeIPv6 | 0x8000);
  1006. - v6_bidirection_template.v[4].length = htons(2);
  1007. - v6_bidirection_template.v[4].pen = htonl(REVERSE_PEN);
  1008. - if (param->time_format == 's') {
  1009. - v6_bidirection_template.t[0].ie = htons(IPFIX_flowStartSeconds);
  1010. - v6_bidirection_template.t[0].length = htons(sizeof(u_int32_t));
  1011. - v6_bidirection_template.t[1].ie = htons(IPFIX_flowEndSeconds);
  1012. - v6_bidirection_template.t[1].length = htons(sizeof(u_int32_t));
  1013. - } else if (param->time_format == 'm') {
  1014. - v6_bidirection_template.t[0].ie = htons(IPFIX_flowStartMilliSeconds);
  1015. - v6_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  1016. - v6_bidirection_template.t[1].ie = htons(IPFIX_flowEndMilliSeconds);
  1017. - v6_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  1018. - } else if (param->time_format == 'M') {
  1019. - v6_bidirection_template.t[0].ie = htons(IPFIX_flowStartMicroSeconds);
  1020. - v6_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  1021. - v6_bidirection_template.t[1].ie = htons(IPFIX_flowEndMicroSeconds);
  1022. - v6_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  1023. - } else if (param->time_format == 'n') {
  1024. - v6_bidirection_template.t[0].ie = htons(IPFIX_flowStartNanoSeconds);
  1025. - v6_bidirection_template.t[0].length = htons(sizeof(u_int64_t));
  1026. - v6_bidirection_template.t[1].ie = htons(IPFIX_flowEndNanoSeconds);
  1027. - v6_bidirection_template.t[1].length = htons(sizeof(u_int64_t));
  1028. - } else {
  1029. - v6_bidirection_template.t[0].ie = htons(IPFIX_flowStartSysUpTime);
  1030. - v6_bidirection_template.t[0].length = htons(sizeof(u_int32_t));
  1031. - v6_bidirection_template.t[1].ie = htons(IPFIX_flowEndSysUpTime);
  1032. - v6_bidirection_template.t[1].length = htons(sizeof(u_int32_t));
  1033. - }
  1034. + u_int8_t v6_flag = 0, icmp_flag = 0;
  1035. + u_int16_t template_id = 0;
  1036. + for (int i = 0; i < TMPLMAX; i++)
  1037. + {
  1038. + switch (i)
  1039. + {
  1040. + case TMPLV4:
  1041. + v6_flag = 0;
  1042. + icmp_flag = 0;
  1043. + template_id = IPFIX_SOFTFLOWD_V4_TEMPLATE_ID;
  1044. + break;
  1045. + case TMPLICMPV4:
  1046. + v6_flag = 0;
  1047. + icmp_flag = 1;
  1048. + template_id = IPFIX_SOFTFLOWD_ICMPV4_TEMPLATE_ID;
  1049. + break;
  1050. + case TMPLV6:
  1051. + v6_flag = 1;
  1052. + icmp_flag = 0;
  1053. + template_id = IPFIX_SOFTFLOWD_V6_TEMPLATE_ID;
  1054. + break;
  1055. + case TMPLICMPV6:
  1056. + v6_flag = 1;
  1057. + icmp_flag = 1;
  1058. + template_id = IPFIX_SOFTFLOWD_ICMPV6_TEMPLATE_ID;
  1059. + break;
  1060. + }
  1061. + ipfix_init_template_unity (param, &template[i],
  1062. + template_id, v6_flag,
  1063. + icmp_flag, bi_flag, version);
  1064. + }
  1065. }
  1066. +static void
  1067. +nflow9_init_option (u_int16_t ifidx, struct OPTION *option)
  1068. +{
  1069. + u_int scope_index = 0, option_index = 0;
  1070. + u_int16_t scope_len =
  1071. + NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS *
  1072. + sizeof (struct IPFIX_FIELD_SPECIFIER);
  1073. + u_int16_t opt_len =
  1074. + NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS *
  1075. + sizeof (struct IPFIX_FIELD_SPECIFIER);
  1076. +
  1077. + bzero (&option_template, sizeof (option_template));
  1078. + option_template.h.c.set_id = htons (NFLOW9_OPTION_TEMPLATE_SET_ID);
  1079. + option_template.h.c.length =
  1080. + htons (sizeof (option_template.h) + scope_len + opt_len);
  1081. + option_template.h.u.n.template_id
  1082. + = htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1083. + option_template.h.u.n.scope_length = htons (scope_len);
  1084. + option_template.h.u.n.option_length = htons (opt_len);
  1085. + ipfix_init_fields (option_template.s, &scope_index,
  1086. + field_nf9scope,
  1087. + NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS);
  1088. + ipfix_init_fields (option_template.r, &option_index,
  1089. + field_nf9option,
  1090. + NFLOW9_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS);
  1091. + bzero (&nf9opt_data, sizeof (nf9opt_data));
  1092. + nf9opt_data.c.set_id = htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1093. + nf9opt_data.c.length = htons (sizeof (nf9opt_data));
  1094. + nf9opt_data.scope_ifidx = htonl (ifidx);
  1095. + nf9opt_data.samplingInterval =
  1096. + htonl (option->sample > 1 ? option->sample : 1);
  1097. + nf9opt_data.samplingAlgorithm = NFLOW9_SAMPLING_ALGORITHM_DETERMINISTIC;
  1098. +}
  1099. static void
  1100. -ipfix_init_option(struct timeval *system_boot_time, struct OPTION *option) {
  1101. - bzero(&option_template, sizeof(option_template));
  1102. - option_template.h.c.set_id = htons(IPFIX_OPTION_TEMPLATE_SET_ID);
  1103. - option_template.h.c.length = htons(sizeof(option_template));
  1104. - option_template.h.r.template_id = htons(IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1105. - option_template.h.r.count = htons(IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS + IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS);
  1106. - option_template.h.scope_count = htons(IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS);
  1107. - option_template.s[0].ie = htons(IPFIX_meteringProcessId);
  1108. - option_template.s[0].length = htons(sizeof(option_data.scope_pid));
  1109. - option_template.r[0].ie = htons(IPFIX_systemInitTimeMilliseconds);
  1110. - option_template.r[0].length = htons(sizeof(option_data.systemInitTimeMilliseconds));
  1111. - option_template.r[1].ie = htons(PSAMP_selectorAlgorithm);
  1112. - option_template.r[1].length = htons(sizeof(option_data.samplingAlgorithm));
  1113. - option_template.r[2].ie = htons(PSAMP_samplingPacketInterval);
  1114. - option_template.r[2].length = htons(sizeof(option_data.samplingInterval));
  1115. - option_template.r[3].ie = htons(PSAMP_samplingPacketSpace);
  1116. - option_template.r[3].length = htons(sizeof(option_data.samplingSpace));
  1117. -
  1118. - bzero(&option_data, sizeof(option_data));
  1119. - option_data.c.set_id = htons(IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1120. - option_data.c.length = htons(sizeof(option_data));
  1121. - option_data.scope_pid = htonl((u_int32_t)option->meteringProcessId);
  1122. +ipfix_init_option (struct timeval *system_boot_time, struct OPTION *option)
  1123. +{
  1124. + u_int scope_index = 0, option_index = 0;
  1125. + bzero (&option_template, sizeof (option_template));
  1126. + option_template.h.c.set_id = htons (IPFIX_OPTION_TEMPLATE_SET_ID);
  1127. + option_template.h.c.length = htons (sizeof (option_template));
  1128. + option_template.h.u.i.r.template_id =
  1129. + htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1130. + option_template.h.u.i.r.count =
  1131. + htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS +
  1132. + IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS);
  1133. + option_template.h.u.i.scope_count =
  1134. + htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS);
  1135. +
  1136. + ipfix_init_fields (option_template.s, &scope_index,
  1137. + field_scope,
  1138. + IPFIX_SOFTFLOWD_OPTION_TEMPLATE_SCOPE_RECORDS);
  1139. + ipfix_init_fields (option_template.r, &option_index, field_option,
  1140. + IPFIX_SOFTFLOWD_OPTION_TEMPLATE_NRECORDS);
  1141. +
  1142. + bzero (&option_data, sizeof (option_data));
  1143. + option_data.c.set_id = htons (IPFIX_SOFTFLOWD_OPTION_TEMPLATE_ID);
  1144. + option_data.c.length = htons (sizeof (option_data));
  1145. + option_data.scope_pid = htonl ((u_int32_t) option->meteringProcessId);
  1146. #if defined(htobe64) || defined(HAVE_DECL_HTOBE64)
  1147. - option_data.systemInitTimeMilliseconds = htobe64((u_int64_t)system_boot_time->tv_sec * 1000 + (u_int64_t)system_boot_time->tv_usec / 1000);
  1148. + option_data.systemInitTimeMilliseconds =
  1149. + htobe64 ((u_int64_t) system_boot_time->tv_sec * 1000 +
  1150. + (u_int64_t) system_boot_time->tv_usec / 1000);
  1151. #endif
  1152. - option_data.samplingAlgorithm = htons(PSAMP_selectorAlgorithm_count);
  1153. - option_data.samplingInterval = htons(1);
  1154. - option_data.samplingSpace = htonl(option->sample > 0 ? option->sample - 1 : 0);
  1155. + option_data.samplingAlgorithm = htons (PSAMP_selectorAlgorithm_count);
  1156. + option_data.samplingInterval = htonl (1);
  1157. + option_data.samplingSpace =
  1158. + htonl (option->sample > 0 ? option->sample - 1 : 0);
  1159. }
  1160. static int
  1161. -ipfix_flow_to_flowset(const struct FLOW *flow, u_char *packet, u_int len,
  1162. - u_int16_t ifidx, const struct timeval *system_boot_time, u_int *len_used,
  1163. - struct FLOWTRACKPARAMETERS *param)
  1164. -{
  1165. - union {
  1166. - struct IPFIX_SOFTFLOWD_DATA_V4 d4;
  1167. - struct IPFIX_SOFTFLOWD_DATA_V6 d6;
  1168. - } d[2];
  1169. - struct IPFIX_SOFTFLOWD_DATA_COMMON *dc[2];
  1170. - union IPFIX_SOFTFLOWD_DATA_TIME *dt[2];
  1171. - u_int freclen, ret_len, nflows;
  1172. -
  1173. - bzero(d, sizeof(d));
  1174. - *len_used = nflows = ret_len = 0;
  1175. - switch (flow->af) {
  1176. - case AF_INET:
  1177. - freclen = sizeof(struct IPFIX_SOFTFLOWD_DATA_V4);
  1178. - if (!(param->time_format == 'm' || param->time_format == 'M' || param->time_format == 'n')) {
  1179. - freclen -= (sizeof(u_int64_t) - sizeof(u_int32_t)) * 2;
  1180. - }
  1181. - memcpy(&d[0].d4.sourceIPv4Address, &flow->addr[0].v4, 4);
  1182. - memcpy(&d[0].d4.destinationIPv4Address, &flow->addr[1].v4, 4);
  1183. - memcpy(&d[1].d4.sourceIPv4Address, &flow->addr[1].v4, 4);
  1184. - memcpy(&d[1].d4.destinationIPv4Address, &flow->addr[0].v4, 4);
  1185. - dc[0] = &d[0].d4.c;
  1186. - dc[1] = &d[1].d4.c;
  1187. - dt[0] = &d[0].d4.t;
  1188. - dt[1] = &d[1].d4.t;
  1189. - dc[0]->ipVersion = dc[1]->ipVersion = 4;
  1190. - break;
  1191. - case AF_INET6:
  1192. - freclen = sizeof(struct IPFIX_SOFTFLOWD_DATA_V6);
  1193. - if (!(param->time_format == 'm' || param->time_format == 'M' || param->time_format == 'n')) {
  1194. - freclen -= (sizeof(u_int64_t) - sizeof(u_int32_t)) * 2;
  1195. - }
  1196. - memcpy(&d[0].d6.sourceIPv6Address, &flow->addr[0].v6, 16);
  1197. - memcpy(&d[0].d6.destinationIPv6Address, &flow->addr[1].v6, 16);
  1198. - memcpy(&d[1].d6.sourceIPv6Address, &flow->addr[1].v6, 16);
  1199. - memcpy(&d[1].d6.destinationIPv6Address, &flow->addr[0].v6, 16);
  1200. - dc[0] = &d[0].d6.c;
  1201. - dc[1] = &d[1].d6.c;
  1202. - dt[0] = &d[0].d6.t;
  1203. - dt[1] = &d[1].d6.t;
  1204. - dc[0]->ipVersion = dc[1]->ipVersion = 6;
  1205. - break;
  1206. - default:
  1207. - return (-1);
  1208. - }
  1209. -
  1210. - if (param->time_format == 's') {
  1211. - dt[0]->u32.start = dt[1]->u32.start =
  1212. - htonl(flow->flow_start.tv_sec);
  1213. - dt[0]->u32.end = dt[1]->u32.end =
  1214. - htonl(flow->flow_last.tv_sec);
  1215. - }
  1216. +copy_data_time (union IPFIX_SOFTFLOWD_DATA_TIME *dt,
  1217. + const struct FLOW *flow,
  1218. + const struct timeval *system_boot_time,
  1219. + struct FLOWTRACKPARAMETERS *param)
  1220. +{
  1221. + int length = (param->time_format == 'm' || param->time_format == 'M'
  1222. + || param->time_format == 'n') ? 16 : 8;
  1223. + if (dt == NULL)
  1224. + return -1;
  1225. +
  1226. + if (param->time_format == 's')
  1227. + {
  1228. + dt->u32.start = htonl (flow->flow_start.tv_sec);
  1229. + dt->u32.end = htonl (flow->flow_last.tv_sec);
  1230. + }
  1231. #if defined(htobe64) || defined(HAVE_DECL_HTOBE64)
  1232. - else if (param->time_format == 'm') { /* milliseconds */
  1233. - dt[0]->u64.start = dt[1]->u64.start =
  1234. - htobe64((u_int64_t)flow->flow_start.tv_sec * 1000 + (u_int64_t)flow->flow_start.tv_usec / 1000);
  1235. - dt[0]->u64.end = dt[1]->u64.end =
  1236. - htobe64((u_int64_t)flow->flow_last.tv_sec * 1000 + (u_int64_t)flow->flow_last.tv_usec / 1000);
  1237. - } else if (param->time_format == 'M') { /* microseconds */
  1238. - dt[0]->u64.start = dt[1]->u64.start =
  1239. - htobe64(((u_int64_t)flow->flow_start.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_start.tv_usec << 32) / 1e6));
  1240. - dt[0]->u64.end = dt[1]->u64.end =
  1241. - htobe64(((u_int64_t)flow->flow_last.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_last.tv_usec << 32) / 1e6));
  1242. - } else if (param->time_format == 'n') { /* nanoseconds */
  1243. - dt[0]->u64.start = dt[1]->u64.start =
  1244. - htobe64(((u_int64_t)flow->flow_start.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_start.tv_usec << 32) / 1e9));
  1245. - dt[0]->u64.end = dt[1]->u64.end =
  1246. - htobe64(((u_int64_t)flow->flow_last.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_last.tv_usec << 32) / 1e9));
  1247. - }
  1248. + else if (param->time_format == 'm')
  1249. + { /* milliseconds */
  1250. + dt->u64.start =
  1251. + htobe64 ((u_int64_t) flow->flow_start.tv_sec * 1000 +
  1252. + (u_int64_t) flow->flow_start.tv_usec / 1000);
  1253. + dt->u64.end =
  1254. + htobe64 ((u_int64_t) flow->flow_last.tv_sec * 1000 +
  1255. + (u_int64_t) flow->flow_last.tv_usec / 1000);
  1256. + }
  1257. + else if (param->time_format == 'M')
  1258. + { /* microseconds */
  1259. + dt->u64.start =
  1260. + htobe64 (((u_int64_t) flow->flow_start.tv_sec +
  1261. + JAN_1970) << 32 | (u_int32_t) (((u_int64_t) flow->
  1262. + flow_start.
  1263. + tv_usec << 32) /
  1264. + 1e6));
  1265. + dt->u64.end =
  1266. + htobe64 (((u_int64_t) flow->flow_last.tv_sec +
  1267. + JAN_1970) << 32 | (u_int32_t) (((u_int64_t) flow->
  1268. + flow_last.
  1269. + tv_usec << 32) /
  1270. + 1e6));
  1271. + }
  1272. + else if (param->time_format == 'n')
  1273. + { /* nanoseconds */
  1274. + dt->u64.start =
  1275. + htobe64 (((u_int64_t) flow->flow_start.tv_sec +
  1276. + JAN_1970) << 32 | (u_int32_t) (((u_int64_t) flow->
  1277. + flow_start.
  1278. + tv_usec << 32) /
  1279. + 1e9));
  1280. + dt->u64.end =
  1281. + htobe64 (((u_int64_t) flow->flow_last.tv_sec +
  1282. + JAN_1970) << 32 | (u_int32_t) (((u_int64_t) flow->
  1283. + flow_last.
  1284. + tv_usec << 32) /
  1285. + 1e9));
  1286. + }
  1287. #endif
  1288. - else {
  1289. - dt[0]->u32.start = dt[1]->u32.start =
  1290. - htonl(timeval_sub_ms(&flow->flow_start, system_boot_time));
  1291. - dt[0]->u32.end = dt[1]->u32.end =
  1292. - htonl(timeval_sub_ms(&flow->flow_last, system_boot_time));
  1293. - }
  1294. - dc[0]->octetDeltaCount = htonl(flow->octets[0]);
  1295. - dc[1]->octetDeltaCount = htonl(flow->octets[1]);
  1296. - dc[0]->packetDeltaCount = htonl(flow->packets[0]);
  1297. - dc[1]->packetDeltaCount = htonl(flow->packets[1]);
  1298. - dc[0]->ingressInterface = dc[0]->egressInterface = htonl(ifidx);
  1299. - dc[1]->ingressInterface = dc[1]->egressInterface = htonl(ifidx);
  1300. - dc[0]->sourceTransportPort = dc[1]->destinationTransportPort = flow->port[0];
  1301. - dc[1]->sourceTransportPort = dc[0]->destinationTransportPort = flow->port[1];
  1302. - dc[0]->protocolIdentifier = dc[1]->protocolIdentifier = flow->protocol;
  1303. - dc[0]->tcpControlBits = flow->tcp_flags[0];
  1304. - dc[1]->tcpControlBits = flow->tcp_flags[1];
  1305. - dc[0]->ipClassOfService = flow->tos[0];
  1306. - dc[1]->ipClassOfService = flow->tos[1];
  1307. - if (flow->protocol == IPPROTO_ICMP || flow->protocol == IPPROTO_ICMPV6) {
  1308. - dc[0]->icmpTypeCode = dc[0]->destinationTransportPort;
  1309. - dc[1]->icmpTypeCode = dc[1]->destinationTransportPort;
  1310. - }
  1311. - dc[0]->vlanId = dc[1]->vlanId = htons(flow->vlanid);
  1312. -
  1313. - if (flow->octets[0] > 0) {
  1314. - if (ret_len + freclen > len)
  1315. - return (-1);
  1316. - memcpy(packet + ret_len, &d[0], freclen);
  1317. - ret_len += freclen;
  1318. - nflows++;
  1319. - }
  1320. - if (flow->octets[1] > 0) {
  1321. - if (ret_len + freclen > len)
  1322. - return (-1);
  1323. - memcpy(packet + ret_len, &d[1], freclen);
  1324. - ret_len += freclen;
  1325. - nflows++;
  1326. - }
  1327. + else
  1328. + {
  1329. + dt->u32.start =
  1330. + htonl (timeval_sub_ms (&flow->flow_start, system_boot_time));
  1331. + dt->u32.end =
  1332. + htonl (timeval_sub_ms (&flow->flow_last, system_boot_time));
  1333. + }
  1334. + return length;
  1335. +}
  1336. - *len_used = ret_len;
  1337. - return (nflows);
  1338. +static u_int
  1339. +ipfix_flow_to_template_index (const struct FLOW *flow)
  1340. +{
  1341. + u_int index = 0;
  1342. + if (flow->af == AF_INET)
  1343. + {
  1344. + index = (flow->protocol == IPPROTO_ICMP) ? TMPLICMPV4 : TMPLV4;
  1345. + }
  1346. + else if (flow->af == AF_INET6)
  1347. + {
  1348. + index = (flow->protocol == IPPROTO_ICMPV6) ? TMPLICMPV6 : TMPLV6;
  1349. + }
  1350. + return index;
  1351. }
  1352. static int
  1353. -ipfix_flow_to_bidirection_flowset(const struct FLOW *flow, u_char *packet,
  1354. - u_int len, u_int16_t ifidx,
  1355. - const struct timeval *system_boot_time,
  1356. - u_int *len_used,
  1357. - struct FLOWTRACKPARAMETERS *param)
  1358. -{
  1359. - union {
  1360. - struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V4 d4;
  1361. - struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V6 d6;
  1362. - } d;
  1363. - struct IPFIX_SOFTFLOWD_DATA_COMMON *dc;
  1364. - struct IPFIX_SOFTFLOWD_DATA_BIDIRECTION *db;
  1365. - union IPFIX_SOFTFLOWD_DATA_TIME *dt;
  1366. - u_int freclen, ret_len, nflows;
  1367. -
  1368. - bzero(&d, sizeof(d));
  1369. - *len_used = nflows = ret_len = 0;
  1370. - switch (flow->af) {
  1371. - case AF_INET:
  1372. - freclen = sizeof(struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V4);
  1373. - if (!(param->time_format == 'm' || param->time_format == 'M' || param->time_format == 'n')) {
  1374. - freclen -= (sizeof(u_int64_t) - sizeof(u_int32_t)) * 2;
  1375. - }
  1376. - memcpy(&d.d4.sourceIPv4Address, &flow->addr[0].v4, 4);
  1377. - memcpy(&d.d4.destinationIPv4Address, &flow->addr[1].v4, 4);
  1378. - dc = &d.d4.c;
  1379. - db = &d.d4.b;
  1380. - dt = &d.d4.t;
  1381. - dc->ipVersion = 4;
  1382. - break;
  1383. - case AF_INET6:
  1384. - freclen = sizeof(struct IPFIX_SOFTFLOWD_BIDIRECTION_DATA_V6);
  1385. - if (!(param->time_format == 'm' || param->time_format == 'M' || param->time_format == 'n')) {
  1386. - freclen -= (sizeof(u_int64_t) - sizeof(u_int32_t)) * 2;
  1387. - }
  1388. - memcpy(&d.d6.sourceIPv6Address, &flow->addr[0].v6, 16);
  1389. - memcpy(&d.d6.destinationIPv6Address, &flow->addr[1].v6, 16);
  1390. - dc = &d.d6.c;
  1391. - db = &d.d6.b;
  1392. - dt = &d.d6.t;
  1393. - dc->ipVersion = 6;
  1394. - break;
  1395. - default:
  1396. - return (-1);
  1397. - }
  1398. -
  1399. - if (param->time_format == 's') {
  1400. - dt->u32.start = htonl(flow->flow_start.tv_sec);
  1401. - dt->u32.end = htonl(flow->flow_last.tv_sec);
  1402. - }
  1403. -#if defined(htobe64) || defined(HAVE_DECL_HTOBE64)
  1404. - else if (param->time_format == 'm') { /* milliseconds */
  1405. - dt->u64.start =
  1406. - htobe64((u_int64_t)flow->flow_start.tv_sec * 1000 + (u_int64_t)flow->flow_start.tv_usec / 1000);
  1407. - dt->u64.end =
  1408. - htobe64((u_int64_t)flow->flow_last.tv_sec * 1000 + (u_int64_t)flow->flow_last.tv_usec / 1000);
  1409. - } else if (param->time_format == 'M') { /* microseconds */
  1410. - dt->u64.start =
  1411. - htobe64(((u_int64_t)flow->flow_start.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_start.tv_usec << 32) / 1e6));
  1412. - dt->u64.end =
  1413. - htobe64(((u_int64_t)flow->flow_last.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_last.tv_usec << 32) / 1e6));
  1414. - } else if (param->time_format == 'n') { /* nanoseconds */
  1415. - dt->u64.start =
  1416. - htobe64(((u_int64_t)flow->flow_start.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_start.tv_usec << 32) / 1e9));
  1417. - dt->u64.end =
  1418. - htobe64(((u_int64_t)flow->flow_last.tv_sec + JAN_1970) << 32 | (u_int32_t)(((u_int64_t)flow->flow_last.tv_usec << 32) / 1e9));
  1419. - }
  1420. -#endif
  1421. - else {
  1422. - dt->u32.start =
  1423. - htonl(timeval_sub_ms(&flow->flow_start, system_boot_time));
  1424. - dt->u32.end =
  1425. - htonl(timeval_sub_ms(&flow->flow_last, system_boot_time));
  1426. - }
  1427. - dc->octetDeltaCount = htonl(flow->octets[0]);
  1428. - db->octetDeltaCount = htonl(flow->octets[1]);
  1429. - dc->packetDeltaCount = htonl(flow->packets[0]);
  1430. - db->packetDeltaCount = htonl(flow->packets[1]);
  1431. - dc->ingressInterface = dc->egressInterface = htonl(ifidx);
  1432. - dc->sourceTransportPort = flow->port[0];
  1433. - dc->destinationTransportPort = flow->port[1];
  1434. - dc->protocolIdentifier = flow->protocol;
  1435. - dc->tcpControlBits = flow->tcp_flags[0];
  1436. - db->tcpControlBits = flow->tcp_flags[1];
  1437. - dc->ipClassOfService = flow->tos[0];
  1438. - db->ipClassOfService = flow->tos[1];
  1439. - if (flow->protocol == IPPROTO_ICMP || flow->protocol == IPPROTO_ICMPV6) {
  1440. - dc->icmpTypeCode = flow->port[1];
  1441. - db->icmpTypeCode = flow->port[0];
  1442. - }
  1443. - dc->vlanId = htons(flow->vlanid);
  1444. -
  1445. - if (flow->octets[0] > 0 || flow->octets[1] > 0) {
  1446. - if (ret_len + freclen > len)
  1447. - return (-1);
  1448. - memcpy(packet + ret_len, &d, freclen);
  1449. - ret_len += freclen;
  1450. - nflows++;
  1451. - }
  1452. +ipfix_flow_to_flowset (const struct FLOW *flow, u_char * packet,
  1453. + u_int len, u_int16_t ifidx,
  1454. + const struct timeval *system_boot_time,
  1455. + u_int * len_used,
  1456. + struct FLOWTRACKPARAMETERS *param, u_int8_t bi_flag)
  1457. +{
  1458. + struct IPFIX_SOFTFLOWD_DATA_V4ADDR *d4[2] = { NULL, NULL };
  1459. + struct IPFIX_SOFTFLOWD_DATA_V6ADDR *d6[2] = { NULL, NULL };
  1460. + union IPFIX_SOFTFLOWD_DATA_TIME *dt[2] = { NULL, NULL };
  1461. + struct IPFIX_SOFTFLOWD_DATA_COMMON *dc[2] = { NULL, NULL };
  1462. + struct IPFIX_SOFTFLOWD_DATA_TRANSPORT *dtr[2] = { NULL, NULL };
  1463. + struct IPFIX_SOFTFLOWD_DATA_ICMP *di[2] = { NULL, NULL };
  1464. + struct IPFIX_SOFTFLOWD_DATA_VLAN *dv[2] = { NULL, NULL };
  1465. + struct IPFIX_SOFTFLOWD_DATA_ETHER *de[2] = { NULL, NULL };
  1466. + struct IPFIX_SOFTFLOWD_DATA_BICOMMON *dbc = NULL;
  1467. + struct IPFIX_SOFTFLOWD_DATA_BITRANSPORT *dbtr = NULL;
  1468. + struct IPFIX_SOFTFLOWD_DATA_BIICMP *dbi = NULL;
  1469. +
  1470. + u_int freclen = 0, nflows = 0, offset = 0;
  1471. + u_int frecnum = bi_flag ? 1 : 2;
  1472. + u_int tmplindex = ipfix_flow_to_template_index (flow);
  1473. + freclen = template[tmplindex].data_len;
  1474. + if (len < freclen * frecnum)
  1475. + return (-1);
  1476. +
  1477. + for (int i = 0; i < frecnum; i++)
  1478. + {
  1479. + if (bi_flag == 0 && flow->octets[i] == 0)
  1480. + continue;
  1481. + nflows++;
  1482. + if (flow->af == AF_INET)
  1483. + {
  1484. + d4[i] =
  1485. + (struct IPFIX_SOFTFLOWD_DATA_V4ADDR *) &packet[offset];
  1486. + memcpy (&d4[i]->sourceIPv4Address, &flow->addr[i].v4, 4);
  1487. + memcpy (&d4[i]->destinationIPv4Address,
  1488. + &flow->addr[i ^ 1].v4, 4);
  1489. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_V4ADDR);
  1490. + }
  1491. + else if (flow->af == AF_INET6)
  1492. + {
  1493. + d6[i] =
  1494. + (struct IPFIX_SOFTFLOWD_DATA_V6ADDR *) &packet[offset];
  1495. + memcpy (&d6[i]->sourceIPv6Address, &flow->addr[i].v6, 16);
  1496. + memcpy (&d6[i]->destinationIPv6Address,
  1497. + &flow->addr[i ^ 1].v6, 16);
  1498. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_V6ADDR);
  1499. + }
  1500. +
  1501. + dt[i] = (union IPFIX_SOFTFLOWD_DATA_TIME *) &packet[offset];
  1502. + offset += copy_data_time (dt[i], flow, system_boot_time, param);
  1503. +
  1504. + dc[i] = (struct IPFIX_SOFTFLOWD_DATA_COMMON *) &packet[offset];
  1505. + dc[i]->octetDeltaCount = htonl (flow->octets[i]);
  1506. + dc[i]->packetDeltaCount = htonl (flow->packets[i]);
  1507. + dc[i]->ingressInterface = dc[i]->egressInterface = htonl (ifidx);
  1508. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_COMMON);
  1509. +
  1510. + if (flow->protocol != IPPROTO_ICMP
  1511. + && flow->protocol != IPPROTO_ICMPV6)
  1512. + {
  1513. + dtr[i] =
  1514. + (struct IPFIX_SOFTFLOWD_DATA_TRANSPORT *) &packet[offset];
  1515. + dtr[i]->sourceTransportPort = flow->port[i];
  1516. + dtr[i]->destinationTransportPort = flow->port[i ^ 1];
  1517. + dtr[i]->protocolIdentifier = flow->protocol;
  1518. + dtr[i]->tcpControlBits = flow->tcp_flags[i];
  1519. + dtr[i]->ipClassOfService = flow->tos[i];
  1520. + dtr[i]->ipVersion = (flow->af == AF_INET) ? 4 : 6;
  1521. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_TRANSPORT);
  1522. + }
  1523. + else
  1524. + {
  1525. + di[i] = (struct IPFIX_SOFTFLOWD_DATA_ICMP *) &packet[offset];
  1526. + di[i]->icmpTypeCode = flow->port[i ^ 1];
  1527. + di[i]->ipClassOfService = flow->tos[i];
  1528. + di[i]->ipVersion = (flow->af == AF_INET) ? 4 : 6;
  1529. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_ICMP);
  1530. + }
  1531. + if (param->track_level >= TRACK_FULL_VLAN)
  1532. + {
  1533. + dv[i] = (struct IPFIX_SOFTFLOWD_DATA_VLAN *) &packet[offset];
  1534. + dv[i]->vlanId = flow->vlanid[i];
  1535. + dv[i]->postVlanId = flow->vlanid[i ^ 1];
  1536. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_VLAN);
  1537. + }
  1538. + if (param->track_level >= TRACK_FULL_VLAN_ETHER)
  1539. + {
  1540. + de[i] = (struct IPFIX_SOFTFLOWD_DATA_ETHER *) &packet[offset];
  1541. + memcpy (&de[i]->sourceMacAddress, &flow->ethermac[i], 6);
  1542. + memcpy (&de[i]->destinationMacAddress,
  1543. + &flow->ethermac[i ^ 1], 6);
  1544. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_ETHER);
  1545. + }
  1546. + if (bi_flag && i == 0)
  1547. + {
  1548. + dbc =
  1549. + (struct IPFIX_SOFTFLOWD_DATA_BICOMMON *) &packet[offset];
  1550. + dbc->octetDeltaCount = htonl (flow->octets[1]);
  1551. + dbc->packetDeltaCount = htonl (flow->packets[1]);
  1552. + dbc->ipClassOfService = flow->tos[1];
  1553. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_BICOMMON);
  1554. + if (flow->protocol != IPPROTO_ICMP
  1555. + && flow->protocol != IPPROTO_ICMPV6)
  1556. + {
  1557. + dbtr =
  1558. + (struct IPFIX_SOFTFLOWD_DATA_BITRANSPORT *)
  1559. + &packet[offset];
  1560. + dbtr->tcpControlBits = flow->tcp_flags[1];
  1561. + offset +=
  1562. + sizeof (struct IPFIX_SOFTFLOWD_DATA_BITRANSPORT);
  1563. + }
  1564. + else
  1565. + {
  1566. + dbi =
  1567. + (struct IPFIX_SOFTFLOWD_DATA_BIICMP *)
  1568. + &packet[offset];
  1569. + dbi->icmpTypeCode = flow->port[1];
  1570. + offset += sizeof (struct IPFIX_SOFTFLOWD_DATA_BIICMP);
  1571. + }
  1572. + }
  1573. + }
  1574. + *len_used = offset;
  1575. + return (nflows);
  1576. +}
  1577. - *len_used = ret_len;
  1578. - return (nflows);
  1579. +static int
  1580. +valuate_icmp (struct FLOW *flow)
  1581. +{
  1582. + if (flow == NULL)
  1583. + return -1;
  1584. + if (flow->af == AF_INET)
  1585. + if (flow->protocol == IPPROTO_ICMP)
  1586. + return 1;
  1587. + else
  1588. + return 0;
  1589. + else if (flow->af == AF_INET6)
  1590. + if (flow->protocol == IPPROTO_ICMPV6)
  1591. + return 1;
  1592. + else
  1593. + return 0;
  1594. + return -1;
  1595. }
  1596. +void
  1597. +ipfix_resend_template (void)
  1598. +{
  1599. + if (ipfix_pkts_until_template > 0)
  1600. + ipfix_pkts_until_template = 0;
  1601. +}
  1602. +
  1603. +void
  1604. +memcpy_template (u_char * packet, u_int * offset,
  1605. + struct IPFIX_SOFTFLOWD_TEMPLATE *template, u_int8_t bi_flag)
  1606. +{
  1607. + int size = ntohs (template->h.c.length) -
  1608. + template->bi_count * sizeof (struct IPFIX_VENDOR_FIELD_SPECIFIER);
  1609. + memcpy (packet + *offset, template, size);
  1610. + *offset += size;
  1611. + if (bi_flag)
  1612. + {
  1613. + size =
  1614. + template->bi_count *
  1615. + sizeof (struct IPFIX_VENDOR_FIELD_SPECIFIER);
  1616. + memcpy (packet + *offset, template->v, size);
  1617. + *offset += size;
  1618. + }
  1619. +}
  1620. /*
  1621. - * Given an array of expired flows, send netflow v9 report packets
  1622. + * Given an array of expired flows, send ipfix report packets
  1623. * Returns number of packets sent or -1 on error
  1624. */
  1625. -int
  1626. -send_ipfix(struct FLOW **flows, int num_flows, int nfsock,
  1627. - u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  1628. - int verbose_flag)
  1629. -{
  1630. - struct IPFIX_HEADER *ipfix;
  1631. - struct IPFIX_SET_HEADER *dh;
  1632. - struct timeval now;
  1633. - u_int offset, last_af, i, j, num_packets, inc, last_valid;
  1634. - socklen_t errsz;
  1635. - int err, r;
  1636. - u_int records;
  1637. - u_char packet[IPFIX_SOFTFLOWD_MAX_PACKET_SIZE];
  1638. - struct timeval *system_boot_time = &param->system_boot_time;
  1639. - u_int64_t *flows_exported = &param->flows_exported;
  1640. - u_int64_t *records_sent = &param->records_sent;
  1641. - struct OPTION *option = &param->option;
  1642. -
  1643. - gettimeofday(&now, NULL);
  1644. -
  1645. - if (ipfix_pkts_until_template == -1) {
  1646. - ipfix_init_template(param);
  1647. - ipfix_pkts_until_template = 0;
  1648. - if (option != NULL){
  1649. - ipfix_init_option(system_boot_time, option);
  1650. - }
  1651. - }
  1652. -
  1653. - last_valid = num_packets = 0;
  1654. - for (j = 0; j < num_flows;) {
  1655. - bzero(packet, sizeof(packet));
  1656. - ipfix = (struct IPFIX_HEADER *)packet;
  1657. -
  1658. - ipfix->version = htons(10);
  1659. - ipfix->length = 0; /* Filled as we go, htons at end */
  1660. - ipfix->export_time = htonl(time(NULL));
  1661. - ipfix->od_id = 0;
  1662. - offset = sizeof(*ipfix);
  1663. -
  1664. - /* Refresh template headers if we need to */
  1665. - if (ipfix_pkts_until_template <= 0) {
  1666. - memcpy(packet + offset, &v4_template,
  1667. - sizeof(v4_template));
  1668. - offset += sizeof(v4_template);
  1669. - memcpy(packet + offset, &v6_template,
  1670. - sizeof(v6_template));
  1671. - offset += sizeof(v6_template);
  1672. - if (option != NULL){
  1673. - memcpy(packet + offset, &option_template,
  1674. - sizeof(option_template));
  1675. - offset += sizeof(option_template);
  1676. - memcpy(packet + offset, &option_data,
  1677. - sizeof(option_data));
  1678. - offset += sizeof(option_data);
  1679. - }
  1680. -
  1681. - ipfix_pkts_until_template = IPFIX_DEFAULT_TEMPLATE_INTERVAL;
  1682. - }
  1683. -
  1684. - dh = NULL;
  1685. - last_af = 0;
  1686. - records = 0;
  1687. - for (i = 0; i + j < num_flows; i++) {
  1688. - if (dh == NULL || flows[i + j]->af != last_af) {
  1689. - if (dh != NULL) {
  1690. - if (offset % 4 != 0) {
  1691. - /* Pad to multiple of 4 */
  1692. - dh->length += 4 - (offset % 4);
  1693. - offset += 4 - (offset % 4);
  1694. - }
  1695. - /* Finalise last header */
  1696. - dh->length = htons(dh->length);
  1697. - }
  1698. - if (offset + sizeof(*dh) > sizeof(packet)) {
  1699. - /* Mark header is finished */
  1700. - dh = NULL;
  1701. - break;
  1702. - }
  1703. - dh = (struct IPFIX_SET_HEADER *)
  1704. - (packet + offset);
  1705. - dh->set_id =
  1706. - (flows[i + j]->af == AF_INET) ?
  1707. - v4_template.h.r.template_id :
  1708. - v6_template.h.r.template_id;
  1709. - last_af = flows[i + j]->af;
  1710. - last_valid = offset;
  1711. - dh->length = sizeof(*dh); /* Filled as we go */
  1712. - offset += sizeof(*dh);
  1713. - }
  1714. -
  1715. - r = ipfix_flow_to_flowset(flows[i + j], packet + offset,
  1716. - sizeof(packet) - offset, ifidx, system_boot_time, &inc, param);
  1717. - if (r <= 0) {
  1718. - /* yank off data header, if we had to go back */
  1719. - if (last_valid)
  1720. - offset = last_valid;
  1721. - break;
  1722. - }
  1723. - records += (u_int)r;
  1724. - offset += inc;
  1725. - dh->length += inc;
  1726. - last_valid = 0; /* Don't clobber this header now */
  1727. - if (verbose_flag) {
  1728. - logit(LOG_DEBUG, "Flow %d/%d: "
  1729. - "r %d offset %d ie %04x len %d(0x%04x)",
  1730. - r, i, j, offset,
  1731. - dh->set_id, dh->length,
  1732. - dh->length);
  1733. - }
  1734. - }
  1735. - /* Don't finish header if it has already been done */
  1736. - if (dh != NULL) {
  1737. - if (offset % 4 != 0) {
  1738. - /* Pad to multiple of 4 */
  1739. - dh->length += 4 - (offset % 4);
  1740. - offset += 4 - (offset % 4);
  1741. - }
  1742. - /* Finalise last header */
  1743. - dh->length = htons(dh->length);
  1744. - }
  1745. - ipfix->length = htons(offset);
  1746. - *records_sent += records;
  1747. - ipfix->sequence = htonl((u_int32_t)(*records_sent & 0x00000000ffffffff));
  1748. -
  1749. - if (verbose_flag)
  1750. - logit(LOG_DEBUG, "Sending flow packet len = %d", offset);
  1751. - errsz = sizeof(err);
  1752. - /* Clear ICMP errors */
  1753. - getsockopt(nfsock, SOL_SOCKET, SO_ERROR, &err, &errsz);
  1754. - if (send(nfsock, packet, (size_t)offset, 0) == -1)
  1755. - return (-1);
  1756. - num_packets++;
  1757. - ipfix_pkts_until_template--;
  1758. +static int
  1759. +send_ipfix_common (struct FLOW **flows, int num_flows,
  1760. + int nfsock, u_int16_t ifidx,
  1761. + struct FLOWTRACKPARAMETERS *param,
  1762. + int verbose_flag, u_int8_t bi_flag, u_int16_t version)
  1763. +{
  1764. + struct IPFIX_HEADER *ipfix;
  1765. + struct NFLOW9_HEADER *nf9;
  1766. + struct IPFIX_SET_HEADER *dh;
  1767. + struct timeval now;
  1768. + u_int offset, last_af, i, j, num_packets, inc, last_valid;
  1769. + int8_t icmp_flag, last_icmp_flag;
  1770. + socklen_t errsz;
  1771. + int err, r;
  1772. + u_int records;
  1773. + u_char packet[IPFIX_SOFTFLOWD_MAX_PACKET_SIZE];
  1774. + struct timeval *system_boot_time = &param->system_boot_time;
  1775. + u_int64_t *flows_exported = &param->flows_exported;
  1776. + u_int64_t *records_sent = &param->records_sent;
  1777. + struct OPTION *option = &param->option;
  1778. + u_int tmplindex = 0;
  1779. +
  1780. + gettimeofday (&now, NULL);
  1781. +
  1782. + if (ipfix_pkts_until_template == -1)
  1783. + {
  1784. + ipfix_init_template (param, bi_flag, version);
  1785. + ipfix_pkts_until_template = 0;
  1786. + if (option != NULL)
  1787. + {
  1788. + if (version == 10)
  1789. + {
  1790. + ipfix_init_option (system_boot_time, option);
  1791. + }
  1792. + else
  1793. + {
  1794. + nflow9_init_option (ifidx, option);
  1795. + }
  1796. + }
  1797. + }
  1798. +
  1799. + last_valid = num_packets = 0;
  1800. + for (j = 0; j < num_flows;)
  1801. + {
  1802. + bzero (packet, sizeof (packet));
  1803. + if (version == 10)
  1804. + {
  1805. + ipfix = (struct IPFIX_HEADER *) packet;
  1806. + ipfix->version = htons (version);
  1807. + ipfix->length = 0; /* Filled as we go, htons at end */
  1808. + ipfix->export_time = htonl (time (NULL));
  1809. + ipfix->od_id = 0;
  1810. + offset = sizeof (*ipfix);
  1811. + }
  1812. + else if (version == 9)
  1813. + {
  1814. + nf9 = (struct NFLOW9_HEADER *) packet;
  1815. + nf9->version = htons (version);
  1816. + nf9->flows = 0; /* Filled as we go, htons at end */
  1817. + nf9->uptime_ms =
  1818. + htonl (timeval_sub_ms (&now, system_boot_time));
  1819. +
  1820. + nf9->export_time = htonl (time (NULL));
  1821. + nf9->od_id = 0;
  1822. + offset = sizeof (*nf9);
  1823. + }
  1824. +
  1825. + /* Refresh template headers if we need to */
  1826. + if (ipfix_pkts_until_template <= 0)
  1827. + {
  1828. + for (int i = 0; i < TMPLMAX; i++)
  1829. + {
  1830. + memcpy_template (packet, &offset,
  1831. + &template[i], bi_flag);
  1832. + }
  1833. + if (option != NULL)
  1834. + {
  1835. + u_int16_t opt_tmpl_len =
  1836. + ntohs (option_template.h.c.length);
  1837. + memcpy (packet + offset, &option_template,
  1838. + opt_tmpl_len);
  1839. + offset += opt_tmpl_len;
  1840. + if (version == 10)
  1841. + {
  1842. + memcpy (packet + offset, &option_data,
  1843. + sizeof (option_data));
  1844. + offset += sizeof (option_data);
  1845. + }
  1846. + else if (version == 9)
  1847. + {
  1848. + memcpy (packet + offset, &nf9opt_data,
  1849. + sizeof (nf9opt_data));
  1850. + offset += sizeof (nf9opt_data);
  1851. + }
  1852. + }
  1853. +
  1854. + ipfix_pkts_until_template = IPFIX_DEFAULT_TEMPLATE_INTERVAL;
  1855. + }
  1856. +
  1857. + dh = NULL;
  1858. + last_af = 0;
  1859. + last_icmp_flag = -1;
  1860. + records = 0;
  1861. + for (i = 0; i + j < num_flows; i++)
  1862. + {
  1863. + icmp_flag = valuate_icmp (flows[i + j]);
  1864. + if (dh == NULL || flows[i + j]->af != last_af ||
  1865. + icmp_flag != last_icmp_flag)
  1866. + {
  1867. + if (dh != NULL)
  1868. + {
  1869. + if (offset % 4 != 0)
  1870. + {
  1871. + /* Pad to multiple of 4 */
  1872. + dh->length += 4 - (offset % 4);
  1873. + offset += 4 - (offset % 4);
  1874. + }
  1875. + /* Finalise last header */
  1876. + dh->length = htons (dh->length);
  1877. + }
  1878. + if (offset + sizeof (*dh) > sizeof (packet))
  1879. + {
  1880. + /* Mark header is finished */
  1881. + dh = NULL;
  1882. + break;
  1883. + }
  1884. + dh = (struct IPFIX_SET_HEADER *) (packet + offset);
  1885. + tmplindex = ipfix_flow_to_template_index (flows[i + j]);
  1886. + dh->set_id = template[tmplindex].h.r.template_id;
  1887. + last_af = flows[i + j]->af;
  1888. + last_icmp_flag = icmp_flag;
  1889. + last_valid = offset;
  1890. + dh->length = sizeof (*dh); /* Filled as we go */
  1891. + offset += sizeof (*dh);
  1892. + }
  1893. + r = ipfix_flow_to_flowset (flows[i + j],
  1894. + packet + offset,
  1895. + sizeof (packet) - offset,
  1896. + ifidx, system_boot_time,
  1897. + &inc, param, bi_flag);
  1898. + if (r <= 0)
  1899. + {
  1900. + /* yank off data header, if we had to go back */
  1901. + if (last_valid)
  1902. + offset = last_valid;
  1903. + break;
  1904. + }
  1905. + records += (u_int) r;
  1906. + offset += inc;
  1907. + dh->length += inc;
  1908. + last_valid = 0; /* Don't clobber this header now */
  1909. + if (verbose_flag)
  1910. + {
  1911. + logit (LOG_DEBUG, "Flow %d/%d: "
  1912. + "r %d offset %d ie %04x len %d(0x%04x)",
  1913. + r, i, j, offset,
  1914. + dh->set_id, dh->length, dh->length);
  1915. + }
  1916. + }
  1917. + /* Don't finish header if it has already been done */
  1918. + if (dh != NULL)
  1919. + {
  1920. + if (offset % 4 != 0)
  1921. + {
  1922. + /* Pad to multiple of 4 */
  1923. + dh->length += 4 - (offset % 4);
  1924. + offset += 4 - (offset % 4);
  1925. + }
  1926. + /* Finalise last header */
  1927. + dh->length = htons (dh->length);
  1928. + }
  1929. + ipfix->length = htons (offset);
  1930. + *records_sent += records;
  1931. + ipfix->sequence =
  1932. + htonl ((u_int32_t) (*records_sent & 0x00000000ffffffff));
  1933. +
  1934. + if (verbose_flag)
  1935. + logit (LOG_DEBUG, "Sending flow packet len = %d", offset);
  1936. + errsz = sizeof (err);
  1937. + /* Clear ICMP errors */
  1938. + getsockopt (nfsock, SOL_SOCKET, SO_ERROR, &err, &errsz);
  1939. + if (send (nfsock, packet, (size_t) offset, 0) == -1)
  1940. + return (-1);
  1941. + num_packets++;
  1942. + ipfix_pkts_until_template--;
  1943. - j += i;
  1944. - }
  1945. + j += i;
  1946. + }
  1947. - *flows_exported += j;
  1948. - return (num_packets);
  1949. + *flows_exported += j;
  1950. + return (num_packets);
  1951. }
  1952. -void
  1953. -ipfix_resend_template(void)
  1954. +int
  1955. +send_nflow9 (struct FLOW **flows, int num_flows, int nfsock,
  1956. + u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  1957. + int verbose_flag)
  1958. {
  1959. - if (ipfix_pkts_until_template > 0)
  1960. - ipfix_pkts_until_template = 0;
  1961. + return send_ipfix_common (flows, num_flows, nfsock, ifidx,
  1962. + param, verbose_flag, 0, 9);
  1963. }
  1964. -/*
  1965. - * Given an array of expired flows, send netflow v9 report packets
  1966. - * Returns number of packets sent or -1 on error
  1967. - */
  1968. int
  1969. -send_ipfix_bidirection(struct FLOW **flows, int num_flows, int nfsock,
  1970. - u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  1971. - int verbose_flag)
  1972. -{
  1973. - struct IPFIX_HEADER *ipfix;
  1974. - struct IPFIX_SET_HEADER *dh;
  1975. - struct timeval now;
  1976. - u_int offset, last_af, i, j, num_packets, inc, last_valid;
  1977. - socklen_t errsz;
  1978. - int err, r;
  1979. - u_int records;
  1980. - u_char packet[IPFIX_SOFTFLOWD_MAX_PACKET_SIZE];
  1981. - struct timeval *system_boot_time = &param->system_boot_time;
  1982. - u_int64_t *flows_exported = &param->flows_exported;
  1983. - u_int64_t *records_sent = &param->records_sent;
  1984. - struct OPTION *option = &param->option;
  1985. -
  1986. - gettimeofday(&now, NULL);
  1987. -
  1988. - if (ipfix_pkts_until_template == -1) {
  1989. - ipfix_init_template_bidirection(param);
  1990. - ipfix_pkts_until_template = 0;
  1991. - if (option != NULL){
  1992. - ipfix_init_option(system_boot_time, option);
  1993. - }
  1994. - }
  1995. -
  1996. - last_valid = num_packets = 0;
  1997. - for (j = 0; j < num_flows;) {
  1998. - bzero(packet, sizeof(packet));
  1999. - ipfix = (struct IPFIX_HEADER *)packet;
  2000. -
  2001. - ipfix->version = htons(10);
  2002. - ipfix->length = 0; /* Filled as we go, htons at end */
  2003. - ipfix->export_time = htonl(time(NULL));
  2004. - ipfix->od_id = 0;
  2005. - offset = sizeof(*ipfix);
  2006. -
  2007. - /* Refresh template headers if we need to */
  2008. - if (ipfix_pkts_until_template <= 0) {
  2009. - memcpy(packet + offset, &v4_bidirection_template,
  2010. - sizeof(v4_bidirection_template));
  2011. - offset += sizeof(v4_bidirection_template);
  2012. - memcpy(packet + offset, &v6_bidirection_template,
  2013. - sizeof(v6_bidirection_template));
  2014. - offset += sizeof(v6_bidirection_template);
  2015. - if (option != NULL){
  2016. - memcpy(packet + offset, &option_template,
  2017. - sizeof(option_template));
  2018. - offset += sizeof(option_template);
  2019. - memcpy(packet + offset, &option_data,
  2020. - sizeof(option_data));
  2021. - offset += sizeof(option_data);
  2022. - }
  2023. -
  2024. - ipfix_pkts_until_template = IPFIX_DEFAULT_TEMPLATE_INTERVAL;
  2025. - }
  2026. -
  2027. - dh = NULL;
  2028. - last_af = 0;
  2029. - records = 0;
  2030. - for (i = 0; i + j < num_flows; i++) {
  2031. - if (dh == NULL || flows[i + j]->af != last_af) {
  2032. - if (dh != NULL) {
  2033. - if (offset % 4 != 0) {
  2034. - /* Pad to multiple of 4 */
  2035. - dh->length += 4 - (offset % 4);
  2036. - offset += 4 - (offset % 4);
  2037. - }
  2038. - /* Finalise last header */
  2039. - dh->length = htons(dh->length);
  2040. - }
  2041. - if (offset + sizeof(*dh) > sizeof(packet)) {
  2042. - /* Mark header is finished */
  2043. - dh = NULL;
  2044. - break;
  2045. - }
  2046. - dh = (struct IPFIX_SET_HEADER *)
  2047. - (packet + offset);
  2048. - dh->set_id =
  2049. - (flows[i + j]->af == AF_INET) ?
  2050. - v4_bidirection_template.h.r.template_id :
  2051. - v6_bidirection_template.h.r.template_id;
  2052. - last_af = flows[i + j]->af;
  2053. - last_valid = offset;
  2054. - dh->length = sizeof(*dh); /* Filled as we go */
  2055. - offset += sizeof(*dh);
  2056. - }
  2057. -
  2058. - r = ipfix_flow_to_bidirection_flowset(flows[i + j],
  2059. - packet + offset,
  2060. - sizeof(packet) - offset,
  2061. - ifidx,
  2062. - system_boot_time,
  2063. - &inc, param);
  2064. - if (r <= 0) {
  2065. - /* yank off data header, if we had to go back */
  2066. - if (last_valid)
  2067. - offset = last_valid;
  2068. - break;
  2069. - }
  2070. - records += (u_int)r;
  2071. - offset += inc;
  2072. - dh->length += inc;
  2073. - last_valid = 0; /* Don't clobber this header now */
  2074. - if (verbose_flag) {
  2075. - logit(LOG_DEBUG, "Flow %d/%d: "
  2076. - "r %d offset %d ie %04x len %d(0x%04x)",
  2077. - r, i, j, offset,
  2078. - dh->set_id, dh->length,
  2079. - dh->length);
  2080. - }
  2081. - }
  2082. - /* Don't finish header if it has already been done */
  2083. - if (dh != NULL) {
  2084. - if (offset % 4 != 0) {
  2085. - /* Pad to multiple of 4 */
  2086. - dh->length += 4 - (offset % 4);
  2087. - offset += 4 - (offset % 4);
  2088. - }
  2089. - /* Finalise last header */
  2090. - dh->length = htons(dh->length);
  2091. - }
  2092. - ipfix->length = htons(offset);
  2093. - *records_sent += records;
  2094. - ipfix->sequence = htonl((u_int32_t)(*records_sent & 0x00000000ffffffff));
  2095. -
  2096. - if (verbose_flag)
  2097. - logit(LOG_DEBUG, "Sending flow packet len = %d", offset);
  2098. - errsz = sizeof(err);
  2099. - /* Clear ICMP errors */
  2100. - getsockopt(nfsock, SOL_SOCKET, SO_ERROR, &err, &errsz);
  2101. - if (send(nfsock, packet, (size_t)offset, 0) == -1)
  2102. - return (-1);
  2103. - num_packets++;
  2104. - ipfix_pkts_until_template--;
  2105. -
  2106. - j += i;
  2107. - }
  2108. +send_ipfix (struct FLOW **flows, int num_flows, int nfsock,
  2109. + u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  2110. + int verbose_flag)
  2111. +{
  2112. + return send_ipfix_common (flows, num_flows, nfsock, ifidx,
  2113. + param, verbose_flag, 0, 10);
  2114. +}
  2115. - *flows_exported += j;
  2116. - return (num_packets);
  2117. +int
  2118. +send_ipfix_bi (struct FLOW **flows, int num_flows, int nfsock,
  2119. + u_int16_t ifidx,
  2120. + struct FLOWTRACKPARAMETERS *param, int verbose_flag)
  2121. +{
  2122. + return send_ipfix_common (flows, num_flows, nfsock, ifidx,
  2123. + param, verbose_flag, 1, 10);
  2124. }
  2125. --- a/softflowd.c
  2126. +++ b/softflowd.c
  2127. @@ -110,8 +110,9 @@
  2128. static const struct NETFLOW_SENDER nf[] = {
  2129. { 5, send_netflow_v5, NULL, 0 },
  2130. { 1, send_netflow_v1, NULL, 0 },
  2131. - { 9, send_netflow_v9, NULL, 1 },
  2132. - { 10, send_ipfix, send_ipfix_bidirection, 1 },
  2133. + //{ 9, send_netflow_v9, NULL, 1 },
  2134. + { 9, send_nflow9, NULL, 1 },
  2135. + { 10, send_ipfix, send_ipfix_bi, 1 },
  2136. { -1, NULL, NULL, 0 },
  2137. };
  2138. @@ -147,6 +148,12 @@
  2139. if (a->vlanid != b->vlanid)
  2140. return (a->vlanid > b->vlanid ? 1 : -1);
  2141. + if ((r = memcmp(&a->ethermac[0], &b->ethermac[0], 6)) != 0)
  2142. + return (r > 0 ? 1 : -1);
  2143. +
  2144. + if ((r = memcmp(&a->ethermac[1], &b->ethermac[1], 6)) != 0)
  2145. + return (r > 0 ? 1 : -1);
  2146. +
  2147. if (a->af != b->af)
  2148. return (a->af > b->af ? 1 : -1);
  2149. @@ -356,29 +363,34 @@
  2150. return (0);
  2151. }
  2152. -/* Convert a IPv4 packet to a partial flow record (used for comparison) */
  2153. static int
  2154. -ipv4_to_flowrec(struct FLOW *flow, const u_int8_t *pkt, size_t caplen,
  2155. - size_t len, int *isfrag, int af, u_int16_t vlanid)
  2156. -{
  2157. - const struct ip *ip = (const struct ip *)pkt;
  2158. - int ndx;
  2159. -
  2160. +make_ndx_ipv4(const struct ip *ip, size_t caplen) {
  2161. if (caplen < 20 || caplen < ip->ip_hl * 4)
  2162. return (-1); /* Runt packet */
  2163. if (ip->ip_v != 4)
  2164. return (-1); /* Unsupported IP version */
  2165. -
  2166. +
  2167. /* Prepare to store flow in canonical format */
  2168. - ndx = memcmp(&ip->ip_src, &ip->ip_dst, sizeof(ip->ip_src)) > 0 ? 1 : 0;
  2169. -
  2170. + return(memcmp(&ip->ip_src, &ip->ip_dst, sizeof(ip->ip_src)) > 0 ? 1 : 0);
  2171. +}
  2172. +
  2173. +/* Convert a IPv4 packet to a partial flow record (used for comparison) */
  2174. +static int
  2175. +ipv4_to_flowrec(struct FLOW *flow, const u_int8_t *pkt, size_t caplen,
  2176. + size_t len, int *isfrag, int af, int ndx)
  2177. +{
  2178. + const struct ip *ip = (const struct ip *)pkt;
  2179. + //int ndx = make_ndx_ipv4 (ip, caplen);
  2180. + if (ndx < 0)
  2181. + return (-1);
  2182. +
  2183. flow->af = af;
  2184. flow->addr[ndx].v4 = ip->ip_src;
  2185. flow->addr[ndx ^ 1].v4 = ip->ip_dst;
  2186. flow->protocol = ip->ip_p;
  2187. flow->octets[ndx] = len;
  2188. flow->packets[ndx] = 1;
  2189. - flow->vlanid = vlanid;
  2190. + flow->tos[ndx] = ip->ip_tos;
  2191. *isfrag = (ntohs(ip->ip_off) & (IP_OFFMASK|IP_MF)) ? 1 : 0;
  2192. @@ -390,16 +402,8 @@
  2193. caplen - (ip->ip_hl * 4), *isfrag, ip->ip_p, ndx));
  2194. }
  2195. -/* Convert a IPv6 packet to a partial flow record (used for comparison) */
  2196. static int
  2197. -ipv6_to_flowrec(struct FLOW *flow, const u_int8_t *pkt, size_t caplen,
  2198. - size_t len, int *isfrag, int af, u_int16_t vlanid)
  2199. -{
  2200. - const struct ip6_hdr *ip6 = (const struct ip6_hdr *)pkt;
  2201. - const struct ip6_ext *eh6;
  2202. - const struct ip6_frag *fh6;
  2203. - int ndx, nxt;
  2204. -
  2205. +make_ndx_ipv6(const struct ip6_hdr *ip6, size_t caplen) {
  2206. if (caplen < sizeof(*ip6))
  2207. return (-1); /* Runt packet */
  2208. @@ -407,16 +411,30 @@
  2209. return (-1); /* Unsupported IPv6 version */
  2210. /* Prepare to store flow in canonical format */
  2211. - ndx = memcmp(&ip6->ip6_src, &ip6->ip6_dst,
  2212. - sizeof(ip6->ip6_src)) > 0 ? 1 : 0;
  2213. -
  2214. + return(memcmp(&ip6->ip6_src, &ip6->ip6_dst,
  2215. + sizeof(ip6->ip6_src)) > 0 ? 1 : 0);
  2216. +}
  2217. +
  2218. +/* Convert a IPv6 packet to a partial flow record (used for comparison) */
  2219. +static int
  2220. +ipv6_to_flowrec(struct FLOW *flow, const u_int8_t *pkt, size_t caplen,
  2221. + size_t len, int *isfrag, int af, int ndx)
  2222. +{
  2223. + const struct ip6_hdr *ip6 = (const struct ip6_hdr *)pkt;
  2224. + const struct ip6_ext *eh6;
  2225. + const struct ip6_frag *fh6;
  2226. + int nxt;
  2227. +
  2228. + if (ndx < 0)
  2229. + return (-1);
  2230. +
  2231. flow->af = af;
  2232. flow->ip6_flowlabel[ndx] = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
  2233. flow->addr[ndx].v6 = ip6->ip6_src;
  2234. flow->addr[ndx ^ 1].v6 = ip6->ip6_dst;
  2235. flow->octets[ndx] = len;
  2236. flow->packets[ndx] = 1;
  2237. - flow->vlanid = vlanid;
  2238. + flow->tos[ndx] = (ntohl(ip6->ip6_flow) & ntohl(0x0ff00000)) >> 20;
  2239. *isfrag = 0;
  2240. nxt = ip6->ip6_nxt;
  2241. @@ -457,6 +475,27 @@
  2242. return (transport_to_flowrec(flow, pkt, caplen, *isfrag, nxt, ndx));
  2243. }
  2244. +static int
  2245. +vlan_to_flowrec(struct FLOW *flow, u_int16_t vlanid, int ndx)
  2246. +{
  2247. + if (ndx < 0)
  2248. + return (-1);
  2249. + return (flow->vlanid[ndx] = vlanid);
  2250. +
  2251. +}
  2252. +
  2253. +static int
  2254. +ether_to_flowrec(struct FLOW *flow, struct ether_header *ether, int ndx)
  2255. +{
  2256. + if (ndx < 0)
  2257. + return (-1);
  2258. + if (ether == NULL)
  2259. + return (-1);
  2260. + memcpy(flow->ethermac[ndx], ether->ether_shost, ETH_ALEN);
  2261. + memcpy(flow->ethermac[ndx^1], ether->ether_dhost, ETH_ALEN);
  2262. + return (1);
  2263. +}
  2264. +
  2265. static void
  2266. flow_update_expiry(struct FLOWTRACK *ft, struct FLOW *flow)
  2267. {
  2268. @@ -555,11 +594,11 @@
  2269. */
  2270. static int
  2271. process_packet(struct FLOWTRACK *ft, const u_int8_t *pkt, int af,
  2272. - const u_int32_t caplen, const u_int32_t len, u_int16_t vlanid,
  2273. - const struct timeval *received_time)
  2274. + const u_int32_t caplen, const u_int32_t len, struct ether_header *ether,
  2275. + u_int16_t vlanid, const struct timeval *received_time)
  2276. {
  2277. struct FLOW tmp, *flow;
  2278. - int frag;
  2279. + int frag, ndx;
  2280. ft->param.total_packets++;
  2281. @@ -567,11 +606,13 @@
  2282. memset(&tmp, 0, sizeof(tmp));
  2283. switch (af) {
  2284. case AF_INET:
  2285. - if (ipv4_to_flowrec(&tmp, pkt, caplen, len, &frag, af, vlanid) == -1)
  2286. + ndx = make_ndx_ipv4((const struct ip *)pkt, caplen);
  2287. + if (ipv4_to_flowrec(&tmp, pkt, caplen, len, &frag, af, ndx) == -1)
  2288. goto bad;
  2289. break;
  2290. case AF_INET6:
  2291. - if (ipv6_to_flowrec(&tmp, pkt, caplen, len, &frag, af, vlanid) == -1)
  2292. + ndx = make_ndx_ipv6((const struct ip6_hdr *)pkt, caplen);
  2293. + if (ipv6_to_flowrec(&tmp, pkt, caplen, len, &frag, af, ndx) == -1)
  2294. goto bad;
  2295. break;
  2296. default:
  2297. @@ -593,8 +634,11 @@
  2298. tmp.tcp_flags[0] = tmp.tcp_flags[1] = 0;
  2299. /* FALLTHROUGH */
  2300. case TRACK_FULL:
  2301. - tmp.vlanid = 0;
  2302. + //tmp.vlanid = 0;
  2303. case TRACK_FULL_VLAN:
  2304. + vlan_to_flowrec(&tmp, vlanid, ndx);
  2305. + case TRACK_FULL_VLAN_ETHER:
  2306. + ether_to_flowrec(&tmp, ether, ndx);
  2307. break;
  2308. }
  2309. @@ -1092,7 +1136,7 @@
  2310. * packet should be skipped
  2311. */
  2312. static int
  2313. -datalink_check(int linktype, const u_int8_t *pkt, u_int32_t caplen, int *af, u_int16_t *vlanid)
  2314. +datalink_check(int linktype, const u_int8_t *pkt, u_int32_t caplen, int *af, struct ether_header **ether, u_int16_t *vlanid)
  2315. {
  2316. int i, j;
  2317. u_int32_t frametype;
  2318. @@ -1116,12 +1160,14 @@
  2319. /* Processing 802.1Q vlan in ethernet */
  2320. if (linktype == DLT_EN10MB) {
  2321. + if (ether != NULL)
  2322. + *ether = (struct ether_header *)pkt;
  2323. for (j = 0; j < dl->ft_len; j++) {
  2324. frametype <<= 8;
  2325. frametype |= pkt[j + dl->ft_off];
  2326. }
  2327. frametype &= dl->ft_mask;
  2328. - if (frametype == 0x8100) {
  2329. + if (frametype == ETHERTYPE_VLAN) {
  2330. for (j = 0; j < 2; j++) {
  2331. *vlanid <<= 8;
  2332. *vlanid |= pkt[j + dl->skiplen];
  2333. @@ -1166,6 +1212,7 @@
  2334. struct CB_CTXT *cb_ctxt = (struct CB_CTXT *)user_data;
  2335. struct timeval tv;
  2336. u_int16_t vlanid = 0;
  2337. + struct ether_header *ether = NULL;
  2338. if (cb_ctxt->ft->param.option.sample &&
  2339. (cb_ctxt->ft->param.total_packets +
  2340. @@ -1174,14 +1221,14 @@
  2341. cb_ctxt->ft->param.non_sampled_packets++;
  2342. return;
  2343. }
  2344. - s = datalink_check(cb_ctxt->linktype, pkt, phdr->caplen, &af, &vlanid);
  2345. + s = datalink_check(cb_ctxt->linktype, pkt, phdr->caplen, &af, &ether, &vlanid);
  2346. if (s < 0 || (!cb_ctxt->want_v6 && af == AF_INET6)) {
  2347. cb_ctxt->ft->param.non_ip_packets++;
  2348. } else {
  2349. tv.tv_sec = phdr->ts.tv_sec;
  2350. tv.tv_usec = phdr->ts.tv_usec;
  2351. if (process_packet(cb_ctxt->ft, pkt + s, af, phdr->caplen - s,
  2352. - phdr->len - s, vlanid, &tv) == PP_MALLOC_FAIL)
  2353. + phdr->len - s, ether, vlanid, &tv) == PP_MALLOC_FAIL)
  2354. cb_ctxt->fatal = 1;
  2355. }
  2356. }
  2357. @@ -1433,7 +1480,7 @@
  2358. bpf_net = bpf_mask = 0;
  2359. }
  2360. *linktype = pcap_datalink(*pcap);
  2361. - if (datalink_check(*linktype, NULL, 0, NULL, NULL) == -1) {
  2362. + if (datalink_check(*linktype, NULL, 0, NULL, NULL, NULL) == -1) {
  2363. fprintf(stderr, "Unsupported datalink type %d\n", *linktype);
  2364. exit(1);
  2365. }
  2366. @@ -1819,6 +1866,9 @@
  2367. flowtrack.param.track_level = TRACK_IP_ONLY;
  2368. else if (strcasecmp(optarg, "vlan") == 0)
  2369. flowtrack.param.track_level = TRACK_FULL_VLAN;
  2370. + else if (strcasecmp(optarg, "ether") == 0)
  2371. + flowtrack.param.track_level = TRACK_FULL_VLAN_ETHER;
  2372. +
  2373. else {
  2374. fprintf(stderr, "Unknown flow tracking "
  2375. "level\n");
  2376. @@ -1953,7 +2003,12 @@
  2377. if ((pidfile = fopen(pidfile_path, "r")) != NULL) {
  2378. int pid;
  2379. - fscanf(pidfile,"%u", &pid);
  2380. + if (fscanf(pidfile,"%u", &pid) == EOF) {
  2381. + //fscanf error
  2382. + if (ferror(pidfile)){
  2383. + perror("fscanf");
  2384. + }
  2385. + }
  2386. fclose(pidfile);
  2387. /* Check if the pid exists */
  2388. --- a/softflowd.h
  2389. +++ b/softflowd.h
  2390. @@ -74,6 +74,7 @@
  2391. #define TRACK_IP_PROTO 3 /* src/dst/proto 3-tuple */
  2392. #define TRACK_IP_ONLY 4 /* src/dst tuple */
  2393. #define TRACK_FULL_VLAN 5 /* src/dst/addr/port/proto/tos/vlanid 7-tuple */
  2394. +#define TRACK_FULL_VLAN_ETHER 6 /* src/dst/addr/port/proto/tos/vlanid/src-mac/dst-mac 9-tuple */
  2395. /*
  2396. * This structure contains optional information carried by Option Data
  2397. @@ -190,7 +191,8 @@
  2398. u_int16_t port[2]; /* Endpoint ports */
  2399. u_int8_t tcp_flags[2]; /* Cumulative OR of flags */
  2400. u_int8_t tos[2]; /* Tos */
  2401. - u_int16_t vlanid; /* vlanid */
  2402. + u_int16_t vlanid[2]; /* vlanid */
  2403. + uint8_t ethermac[2][6];
  2404. u_int8_t protocol; /* Protocol */
  2405. };
  2406. @@ -232,13 +234,16 @@
  2407. int send_netflow_v9(struct FLOW **flows, int num_flows, int nfsock,
  2408. u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  2409. int verbose_flag);
  2410. +int send_nflow9(struct FLOW **flows, int num_flows, int nfsock,
  2411. + u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  2412. + int verbose_flag);
  2413. int send_ipfix(struct FLOW **flows, int num_flows, int nfsock,
  2414. - u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  2415. - int verbose_flag);
  2416. -int send_ipfix_bidirection(struct FLOW **flows, int num_flows, int nfsock,
  2417. - u_int16_t ifidx,
  2418. - struct FLOWTRACKPARAMETERS *param,
  2419. + u_int16_t ifidx, struct FLOWTRACKPARAMETERS *param,
  2420. int verbose_flag);
  2421. +int send_ipfix_bi(struct FLOW **flows, int num_flows, int nfsock,
  2422. + u_int16_t ifidx,
  2423. + struct FLOWTRACKPARAMETERS *param,
  2424. + int verbose_flag);
  2425. /* Force a resend of the flow template */
  2426. void netflow9_resend_template(void);