perfdhcp.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466
  1. /*
  2. * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifdef __linux__
  17. #define _GNU_SOURCE
  18. #endif
  19. #include <sys/types.h>
  20. #include <sys/select.h>
  21. #include <sys/socket.h>
  22. #include <sys/wait.h>
  23. #include <net/if.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <ifaddrs.h>
  30. #include <math.h>
  31. #include <netdb.h>
  32. #include <signal.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <time.h>
  38. #include <unistd.h>
  39. /* DHCPv4 defines (to be moved/shared) */
  40. #define DHCP_OFF_OPCODE 0
  41. #define DHCP_OFF_HTYPE 1
  42. #define DHCP_OFF_HLEN 2
  43. #define DHCP_OFF_HOPS 3
  44. #define DHCP_OFF_XID 4
  45. #define DHCP_OFF_SECS 8
  46. #define DHCP_OFF_FLAGS 10
  47. #define DHCP_OFF_CIADDR 12
  48. #define DHCP_OFF_YIADDR 16
  49. #define DHCP_OFF_SADDR 20
  50. #define DHCP_OFF_GIADDR 24
  51. #define DHCP_OFF_CHADDR 28
  52. #define DHCP_OFF_SNAME 44
  53. #define DHCP_OFF_FILE 108
  54. #define DHCP_OFF_COOKIE 236
  55. #define DHCP_OFF_OPTIONS 240
  56. #define BOOTP_OP_REQUEST 1
  57. #define BOOTP_OP_REPLY 2
  58. #define BOOTP_MIN_LEN 300
  59. #define DHCP_OP_DISCOVER 1
  60. #define DHCP_OP_OFFER 2
  61. #define DHCP_OP_REQUEST 3
  62. #define DHCP_OP_DECLINE 4
  63. #define DHCP_OP_ACK 5
  64. #define DHCP_OP_NAK 6
  65. #define DHCP_OP_RELEASE 7
  66. #define DHCP_OP_INFORM 8
  67. #define DHCP_HTYPE_ETHER 1
  68. #define DHCP_OPT_PAD 0
  69. #define DHCP_OPT_SUBNET_MASK 1
  70. #define DHCP_OPT_TIME_OFFSET 2
  71. #define DHCP_OPT_ROUTERS 3
  72. #define DHCP_OPT_DNS_SERVERS 6
  73. #define DHCP_OPT_HOST_NAME 12
  74. #define DHCP_OPT_DOMAIN_NAME 15
  75. #define DHCP_OPT_BROADCAST 28
  76. #define DHCP_OPT_DHCP_ADDRESS 50
  77. #define DHCP_OPT_DHCP_LEASE 51
  78. #define DHCP_OPT_DHCP_MSGTYPE 53
  79. #define DHCP_OPT_DHCP_SRVID 54
  80. #define DHCP_OPT_DHCP_PRL 55
  81. #define DHCP_OPT_END 255
  82. #define DHCP_OPTLEN_SRVID 6
  83. /* DHCPv6 defines (to be moved/shared) */
  84. #define DHCP6_OFF_MSGTYP 0
  85. #define DHCP6_OFF_XID 1
  86. #define DHCP6_OFF_OPTIONS 4
  87. #define DHCP6_OP_SOLICIT 1
  88. #define DHCP6_OP_ADVERTISE 2
  89. #define DHCP6_OP_REQUEST 3
  90. #define DHCP6_OP_REPLY 7
  91. #define DHCP6_OPT_CLIENTID 1
  92. #define DHCP6_OPT_SERVERID 2
  93. #define DHCP6_OPT_IA_NA 3
  94. #define DHCP6_OPT_ORO 6
  95. #define DHCP6_OPT_ELAPSED_TIME 8
  96. #define DHCP6_OPT_STATUS_CODE 13
  97. #define DHCP6_OPT_RAPID_COMMIT 14
  98. #define DHCP6_OPT_NAME_SERVERS 23
  99. #define DHCP6_OPT_DOMAIN_SEARCH 24
  100. #define DHCP6_ST_SUCCESS 0
  101. #define DHCP6_ST_NOADDRSAVAIL 2
  102. #define DHCP6_DUID_LLT 1
  103. #define DHCP6_DUID_EPOCH 946684800
  104. /* tail queue macros (from FreeBSD 8.2 /sys/sys/queue.h, to be moved/shared) */
  105. #define ISC_TAILQ_HEAD(name, type) \
  106. struct name { \
  107. struct type *tqh_first; \
  108. struct type **tqh_last; \
  109. }
  110. #define ISC_TAILQ_ENTRY(type) \
  111. struct { \
  112. struct type *tqe_next; \
  113. struct type **tqe_prev; \
  114. }
  115. #define ISC_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  116. #define ISC_TAILQ_FIRST(head) ((head)->tqh_first)
  117. #define ISC_TAILQ_LAST(head, headname) \
  118. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  119. #define ISC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  120. #define ISC_TAILQ_PREV(elm, headname, field) \
  121. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  122. #define ISC_TAILQ_INIT(head) do { \
  123. ISC_TAILQ_FIRST((head)) = NULL; \
  124. (head)->tqh_last = &ISC_TAILQ_FIRST((head)); \
  125. } while (0)
  126. #define ISC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  127. ISC_TAILQ_NEXT((elm), field) = ISC_TAILQ_FIRST((head)); \
  128. if (!ISC_TAILQ_EMPTY((head))) \
  129. ISC_TAILQ_FIRST((head))->field.tqe_prev = \
  130. &ISC_TAILQ_NEXT((elm), field); \
  131. else \
  132. (head)->tqh_last = &ISC_TAILQ_NEXT((elm), field); \
  133. ISC_TAILQ_FIRST((head)) = (elm); \
  134. (elm)->field.tqe_prev = &ISC_TAILQ_FIRST((head)); \
  135. } while (0)
  136. #define ISC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  137. ISC_TAILQ_NEXT((elm), field) = NULL; \
  138. (elm)->field.tqe_prev = (head)->tqh_last; \
  139. *(head)->tqh_last = (elm); \
  140. (head)->tqh_last = &ISC_TAILQ_NEXT((elm), field); \
  141. } while (0)
  142. #define ISC_TAILQ_REMOVE(head, elm, field) do { \
  143. if ((ISC_TAILQ_NEXT((elm), field)) != NULL) \
  144. ISC_TAILQ_NEXT((elm), field)->field.tqe_prev = \
  145. (elm)->field.tqe_prev; \
  146. else \
  147. (head)->tqh_last = (elm)->field.tqe_prev; \
  148. *(elm)->field.tqe_prev = ISC_TAILQ_NEXT((elm), field); \
  149. } while (0)
  150. #define ISC_TAILQ_FOREACH(var, head, field) \
  151. for ((var) = ISC_TAILQ_FIRST((head)); \
  152. (var); \
  153. (var) = ISC_TAILQ_NEXT((var), field))
  154. #define ISC_TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  155. for ((var) = ISC_TAILQ_FIRST((head)); \
  156. (var) && ((tvar) = ISC_TAILQ_NEXT((var), field), 1); \
  157. (var) = (tvar))
  158. /*
  159. * Data structures
  160. */
  161. /*
  162. * exchange:
  163. * - per exchange values:
  164. * * order (for debugging)
  165. * * xid (even/odd for 4 packet exchanges)
  166. * * random (for debugging)
  167. * * time-stamps
  168. * * server ID (for 3rd packet)
  169. * * IA_NA (for IPv6 3rd packet)
  170. *
  171. * sent/rcvd global chains, "next to be received" on entry cache,
  172. * and hash table for xid -> data structure fast matching
  173. * (using the assumption collisions are unlikely, cf birthday problem)
  174. */
  175. struct exchange { /* per exchange structure */
  176. ISC_TAILQ_ENTRY(exchange) gchain; /* global chaining */
  177. ISC_TAILQ_ENTRY(exchange) hchain; /* hash table chaining */
  178. uint64_t order0, order2; /* number of this exchange */
  179. uint32_t xid; /* transaction ID */
  180. uint32_t rnd; /* random part */
  181. struct timespec ts0, ts1, ts2, ts3; /* timespecs */
  182. uint8_t *sid; /* server ID */
  183. size_t sidlen; /* server ID length */
  184. uint8_t *iana; /* (IPv6) IA_NA */
  185. size_t ianalen; /* (IPv6) IA_NA length */
  186. };
  187. struct exchange *xnext0, *xnext2; /* next to be received */
  188. ISC_TAILQ_HEAD(xlist, exchange); /* exchange list */
  189. struct xlist xsent0, xsent2, xrcvd0, xrcvd2; /* sent and received lists */
  190. uint64_t xscount0, xscount2; /* sent counters */
  191. uint64_t xrcount0, xrcount2; /* received counters */
  192. caddr_t exchanges0, exchanges2; /* hash tables */
  193. uint32_t hashsize0, hashsize2; /* hash table sizes */
  194. /*
  195. * statictics counters and accumulators
  196. */
  197. uint64_t tooshort, orphans, locallimit; /* error counters */
  198. uint64_t latesent, compsend, latercvd; /* rate stats */
  199. uint64_t multrcvd, shortwait, collected[2]; /* rate stats (cont) */
  200. double dmin0 = 999999999., dmin2 = 999999999.; /* minimum delays */
  201. double dmax0 = 0., dmax2 = 0.; /* maximum delays */
  202. double dsum0 = 0., dsum2 = 0.; /* delay sums */
  203. double dsumsq0 = 0., dsumsq2 = 0.; /* square delay sums */
  204. /*
  205. * command line parameters
  206. */
  207. int ipversion = 0; /* IP version */
  208. int simple; /* DO/SA in place of DORR/SARR */
  209. int rate; /* rate in exchange per second */
  210. int report; /* delay between two reports */
  211. uint32_t range; /* randomization range */
  212. uint32_t maxrandom; /* maximum random value */
  213. int basecnt; /* base count */
  214. char *base[4]; /* bases */
  215. int gotnumreq; /* numreq[0] was set */
  216. int numreq[2]; /* number of exchange */
  217. int period; /* test period */
  218. int gotlosttime; /* losttime[0] was set */
  219. double losttime[2] = {1., 1.}; /* time after a request is lost */
  220. int gotmaxdrop; /* max{p}drop[0] was set */
  221. int maxdrop[2]; /* maximum number of lost requests */
  222. double maxpdrop[2] = { 0., 0.}; /* maximum percentage */
  223. char *localname; /* local address or interface */
  224. int isinterface; /* interface vs local address */
  225. int preload; /* preload exchanges */
  226. int aggressivity = 1; /* back to back exchanges */
  227. int localport; /* local port number (host endian) */
  228. int seeded; /* is a seed provided */
  229. unsigned int seed; /* randomization seed */
  230. int isbroadcast; /* use broadcast */
  231. int rapidcommit; /* add rapid commit option */
  232. int usefirst; /* where to take the server-ID */
  233. char *templatefile[2]; /* template file name */
  234. int xidoffset[2] = {-1, -1}; /* template offsets (xid)*/
  235. int rndoffset[2] = {-1, -1}; /* template offsets (random) */
  236. int elpoffset = -1; /* template offset (elapsed time) */
  237. int sidoffset = -1; /* template offset (server ID) */
  238. int ripoffset = -1; /* template offset (requested IP) */
  239. char *diags; /* diagnostic selectors */
  240. char *wrapped; /* wrapped command */
  241. char *servername; /* server */
  242. /*
  243. * global variables
  244. */
  245. struct sockaddr_storage localaddr; /* local socket address */
  246. struct sockaddr_storage serveraddr; /* server socket address */
  247. int sock; /* socket descriptor */
  248. int interrupted, fatal; /* to finish flags */
  249. uint8_t obuf[4096], ibuf[4096]; /* I/O buffers */
  250. char tbuf[8200]; /* template buffer */
  251. struct timespec boot; /* the date of boot */
  252. struct timespec last; /* the date of last send */
  253. struct timespec due; /* the date of next send */
  254. struct timespec dreport; /* the date of next reporting */
  255. struct timespec finished; /* the date of finish */
  256. uint8_t *gsrvid; /* global server id */
  257. size_t gsrvidlen; /* and its length */
  258. uint8_t gsrvidbuf[64]; /* and its storage */
  259. /* MAC address */
  260. uint8_t mac_prefix[6] = { 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04 };
  261. /* DUID prefix */
  262. uint8_t *duid_prefix;
  263. int duid_length;
  264. /* magic cookie for BOOTP/DHCPv4 */
  265. uint8_t dhcp_cookie[4] = { 0x63, 0x82, 0x53, 0x63 };
  266. /*
  267. * templates
  268. *
  269. * note: the only hard point is what are the offsets:
  270. * - xid_discover4 and xid_request4: first of the 4 octet long
  271. * transaction ID (default DHCP_OFF_XID = 4)
  272. * - random_discover4 and random_request4: last of the 6 octet long
  273. * MAC address (default DHCP_OFF_CHADDR + 6 = 28 + 6)
  274. * - elapsed_request4: first of the 2 octet long secs field
  275. * (default DHCP_OFF_SECS = 8, 0 means disabled)
  276. * - serverid_request4: first of the 6 octet long server ID option
  277. * (no default, required)
  278. * - reqaddr_request4: first of the 4 octet long requested IP address
  279. * option content (i.e., the address itself, btw OFFER yiaddr)
  280. * (no default, required)
  281. * - xid_solicit6 and xid_request6: first of the 3 octet long
  282. * transaction ID (default DHCP6_OFF_XID = 1)
  283. * - random_solicit6 and random_request6: last of the DUID in the
  284. * client ID option (no default, required when rate is set)
  285. * - elapsed_request6: first of the 2 octet long content of
  286. * the option elapsed time option (no default, 0 means disabled)
  287. * - serverid_request6: position where the variable length server ID
  288. * option is inserted (no default, required, set to length means append)
  289. * - reqaddr_request6: position where of the variable length requested
  290. * IP address option is inserted (no default, required, set to
  291. * length means append)
  292. */
  293. size_t length_discover4;
  294. uint8_t template_discover4[4096];
  295. size_t xid_discover4;
  296. size_t random_discover4;
  297. size_t length_request4;
  298. uint8_t template_request4[4096];
  299. size_t xid_request4;
  300. size_t elapsed_request4;
  301. size_t random_request4;
  302. size_t serverid_request4;
  303. size_t reqaddr_request4;
  304. size_t length_solicit6;
  305. uint8_t template_solicit6[4096];
  306. size_t xid_solicit6;
  307. size_t random_solicit6;
  308. size_t length_request6;
  309. uint8_t template_request6[4096];
  310. size_t xid_request6;
  311. size_t elapsed_request6;
  312. size_t random_request6;
  313. size_t serverid_request6;
  314. size_t reqaddr_request6;
  315. /*
  316. * initialize data structures handling exchanges
  317. */
  318. void
  319. inits(void)
  320. {
  321. struct xlist *bucket;
  322. caddr_t p;
  323. size_t len, i;
  324. ISC_TAILQ_INIT(&xsent0);
  325. ISC_TAILQ_INIT(&xsent2);
  326. ISC_TAILQ_INIT(&xrcvd0);
  327. ISC_TAILQ_INIT(&xrcvd2);
  328. /// compute hashsizes
  329. hashsize0 = 1024;
  330. len = sizeof(*bucket) * hashsize0;
  331. exchanges0 = malloc(len);
  332. if (exchanges0 == NULL) {
  333. perror("malloc(exchanges0)");
  334. exit(1);
  335. }
  336. for (i = 0, p = exchanges0; i < hashsize0; i++, p += sizeof(*bucket)) {
  337. bucket = (struct xlist *) p;
  338. ISC_TAILQ_INIT(bucket);
  339. }
  340. if (simple != 0)
  341. return;
  342. hashsize2 = 1024;
  343. len = sizeof(*bucket) * hashsize2;
  344. exchanges2 = malloc(len);
  345. if (exchanges2 == NULL) {
  346. perror("malloc(exchanges2)");
  347. exit(1);
  348. }
  349. for (i = 0, p = exchanges2; i < hashsize2; i++, p += sizeof(*bucket)) {
  350. bucket = (struct xlist *) p;
  351. ISC_TAILQ_INIT(bucket);
  352. }
  353. }
  354. /*
  355. * randomize the value of the given field:
  356. * - offset of the field
  357. * - random seed (used as it when suitable)
  358. * - returns the random value which was used
  359. */
  360. uint32_t
  361. randomize(size_t offset, uint32_t r)
  362. {
  363. uint32_t v;
  364. if (range == 0)
  365. return 0;
  366. if (range == UINT32_MAX)
  367. return r;
  368. if (maxrandom != 0)
  369. while (r >= maxrandom)
  370. r = (uint32_t) random();
  371. r %= range + 1;
  372. v = r;
  373. v += obuf[offset];
  374. obuf[offset] = v;
  375. if (v < 256)
  376. return r;
  377. v >>= 8;
  378. v += obuf[offset - 1];
  379. obuf[offset - 1] = v;
  380. if (v < 256)
  381. return r;
  382. v >>= 8;
  383. v += obuf[offset - 2];
  384. obuf[offset - 2] = v;
  385. if (v < 256)
  386. return r;
  387. v >>= 8;
  388. v += obuf[offset - 3];
  389. obuf[offset - 3] = v;
  390. return r;
  391. }
  392. /*
  393. * receive a reply (4th packet), shared between IPv4 and IPv6:
  394. * - transaction ID xid
  395. * - receiving time-stamp now
  396. * called from receive[46]() when the xid is odd
  397. */
  398. void
  399. receive_reply(uint32_t xid, struct timespec *now)
  400. {
  401. struct exchange *x, *t;
  402. struct xlist *bucket;
  403. uint32_t hash;
  404. int checklost;
  405. double delta;
  406. /* bucket is needed even when the next cache matches */
  407. hash = (xid >> 1) & (hashsize2 - 1);
  408. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  409. /* try the 'next to be received' cache */
  410. if ((xnext2 != NULL) && (xnext2->xid == xid)) {
  411. x = xnext2;
  412. goto found;
  413. }
  414. /* usually the lost probability is low for request/reply */
  415. checklost = 1;
  416. /* look for the exchange */
  417. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  418. double waited;
  419. if (x->xid == xid)
  420. goto found;
  421. if (checklost <= 0)
  422. continue;
  423. checklost = 0;
  424. /* check for a timed-out exchange */
  425. waited = now->tv_sec - x->ts2.tv_sec;
  426. waited += (now->tv_nsec - x->ts2.tv_nsec) / 1e9;
  427. if (waited < losttime[1])
  428. continue;
  429. /* garbage collect timed-out exchange */
  430. ISC_TAILQ_REMOVE(bucket, x, hchain);
  431. ISC_TAILQ_REMOVE(&xsent2, x, gchain);
  432. free(x);
  433. collected[1] += 1;
  434. }
  435. /* no match? very late or not for us */
  436. orphans++;
  437. return;
  438. /* got it: update stats and move to the received queue */
  439. found:
  440. xrcount2++;
  441. x->ts3 = *now;
  442. delta = x->ts3.tv_sec - x->ts2.tv_sec;
  443. delta += (x->ts3.tv_nsec - x->ts2.tv_nsec) / 1e9;
  444. if (delta < dmin2)
  445. dmin2 = delta;
  446. if (delta > dmax2)
  447. dmax2 = delta;
  448. dsum2 += delta;
  449. dsumsq2 += delta * delta;
  450. xnext2 = ISC_TAILQ_NEXT(x, gchain);
  451. ISC_TAILQ_REMOVE(bucket, x, hchain);
  452. ISC_TAILQ_REMOVE(&xsent2, x, gchain);
  453. ISC_TAILQ_INSERT_TAIL(&xrcvd2, x, gchain);
  454. }
  455. /*
  456. * get the DHCPv4 socket descriptor
  457. * (the only complexity is broadcast enabling: there is no easy way to
  458. * recognize broadcast addresses, so the command line -B flag)
  459. */
  460. void
  461. getsock4(void)
  462. {
  463. int ret;
  464. /* update local port */
  465. if (localport != 0) {
  466. uint16_t lp = htons((uint16_t) localport);
  467. ((struct sockaddr_in *) &localaddr)->sin_port = lp;
  468. }
  469. sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  470. if (sock < 0) {
  471. perror("socket");
  472. exit(1);
  473. }
  474. ret = bind(sock,
  475. (struct sockaddr *) &localaddr,
  476. sizeof(struct sockaddr_in));
  477. if (ret < 0) {
  478. perror("bind");
  479. exit(1);
  480. }
  481. /* enable broadcast if needed or required */
  482. if (isbroadcast != 0) {
  483. int on = 1;
  484. ret = setsockopt(sock,
  485. SOL_SOCKET, SO_BROADCAST,
  486. &on, sizeof(on));
  487. if (ret < 0) {
  488. perror("setsockopt(SO_BROADCAST)");
  489. exit(1);
  490. }
  491. }
  492. }
  493. /*
  494. * build a DHCPv4 DISCOVER from a relay template
  495. * (implicit parameters are the local (giaddr) and MAC addresses (chaddr))
  496. * (assume the link is Ethernet)
  497. */
  498. void
  499. build_template_discover4(void)
  500. {
  501. uint8_t *p = template_discover4;
  502. length_discover4 = BOOTP_MIN_LEN;
  503. xid_discover4 = DHCP_OFF_XID;
  504. random_discover4 = DHCP_OFF_CHADDR + 6;
  505. /* opcode */
  506. p[DHCP_OFF_OPCODE] = BOOTP_OP_REQUEST;
  507. /* hardware address type */
  508. p[DHCP_OFF_HTYPE] = DHCP_HTYPE_ETHER;
  509. /* hardware address length */
  510. p[DHCP_OFF_HLEN] = 6;
  511. /* hops */
  512. p[DHCP_OFF_HOPS] = 1;
  513. /* gateway address */
  514. memcpy(p + DHCP_OFF_GIADDR,
  515. &((struct sockaddr_in *) &localaddr)->sin_addr,
  516. 4);
  517. /* hardware address */
  518. memcpy(p + DHCP_OFF_CHADDR, mac_prefix, 6);
  519. /* cookie */
  520. memcpy(p + DHCP_OFF_COOKIE, dhcp_cookie, 4);
  521. /* options */
  522. p += DHCP_OFF_OPTIONS;
  523. /* inline DHCP message type */
  524. *p++ = DHCP_OPT_DHCP_MSGTYPE;
  525. *p++ = 1;
  526. *p++ = DHCP_OP_DISCOVER;
  527. /* inline DHCP parameter request list (default) */
  528. *p++ = DHCP_OPT_DHCP_PRL;
  529. *p++ = 7;
  530. *p++ = DHCP_OPT_SUBNET_MASK;
  531. *p++ = DHCP_OPT_BROADCAST;
  532. *p++ = DHCP_OPT_TIME_OFFSET;
  533. *p++ = DHCP_OPT_ROUTERS;
  534. *p++ = DHCP_OPT_DOMAIN_NAME;
  535. *p++ = DHCP_OPT_DNS_SERVERS;
  536. *p++ = DHCP_OPT_HOST_NAME;
  537. /* end */
  538. *p = DHCP_OPT_END;
  539. }
  540. /*
  541. * get a DHCPv4 client/relay first packet (usually a DISCOVER) template
  542. * from the file given in the command line (-T<template-file>)
  543. * and xid/rnd offsets (-X<xid-offset> and -O<random-offset>)
  544. */
  545. void
  546. get_template_discover4(void)
  547. {
  548. uint8_t *p = template_discover4;
  549. int fd, cc, i, j;
  550. fd = open(templatefile[0], O_RDONLY);
  551. if (fd < 0) {
  552. fprintf(stderr, "open(%s): %s\n",
  553. templatefile[0], strerror(errno));
  554. exit(2);
  555. }
  556. cc = read(fd, tbuf, sizeof(tbuf));
  557. (void) close(fd);
  558. if (cc < 0) {
  559. fprintf(stderr, "read(%s): %s\n",
  560. templatefile[0], strerror(errno));
  561. exit(1);
  562. }
  563. if (cc < 100) {
  564. fprintf(stderr, "file '%s' too short\n", templatefile[0]);
  565. exit(2);
  566. }
  567. if (cc > 8193) {
  568. fprintf(stderr,"file '%s' too large\n", templatefile[0]);
  569. exit(2);
  570. }
  571. j = 0;
  572. for (i = 0; i < cc; i++) {
  573. if (isspace((int) tbuf[i]))
  574. continue;
  575. if (!isxdigit((int) tbuf[i])) {
  576. fprintf(stderr,
  577. "illegal char[%d]='%c' in file '%s'\n",
  578. i, (int) tbuf[i], templatefile[0]);
  579. exit(2);
  580. }
  581. tbuf[j] = tbuf[i];
  582. j++;
  583. }
  584. cc = j;
  585. if ((cc & 1) != 0) {
  586. fprintf(stderr,
  587. "odd number of hexadecimal digits in file '%s'\n",
  588. templatefile[0]);
  589. exit(2);
  590. }
  591. length_discover4 = cc >> 1;
  592. for (i = 0; i < cc; i += 2)
  593. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  594. if (xidoffset[0] >= 0)
  595. xid_discover4 = (size_t) xidoffset[0];
  596. else
  597. xid_discover4 = DHCP_OFF_XID;
  598. if (xid_discover4 + 4 > length_discover4) {
  599. fprintf(stderr,
  600. "xid (at %zu) outside the template (length %zu)?\n",
  601. xid_discover4, length_discover4);
  602. exit(2);
  603. }
  604. if (rndoffset[0] >= 0)
  605. random_discover4 = (size_t) rndoffset[0];
  606. else
  607. random_discover4 = DHCP_OFF_CHADDR + 6;
  608. if (random_discover4 > length_discover4) {
  609. fprintf(stderr,
  610. "random (at %zu) outside the template (length %zu)?\n",
  611. random_discover4, length_discover4);
  612. exit(2);
  613. }
  614. }
  615. /*
  616. * build a DHCPv4 REQUEST from a relay template
  617. * (implicit parameters are the local (giaddr) and MAC addresses (chaddr))
  618. * (assume the link is Ethernet)
  619. */
  620. void
  621. build_template_request4(void)
  622. {
  623. uint8_t *p = template_request4;
  624. length_request4 = BOOTP_MIN_LEN;
  625. xid_request4 = DHCP_OFF_XID;
  626. elapsed_request4 = DHCP_OFF_SECS;
  627. random_request4 = DHCP_OFF_CHADDR + 6;
  628. /* opcode */
  629. p[DHCP_OFF_OPCODE] = BOOTP_OP_REQUEST;
  630. /* hardware address type */
  631. p[DHCP_OFF_HTYPE] = DHCP_HTYPE_ETHER;
  632. /* hardware address length */
  633. p[DHCP_OFF_HLEN] = 6;
  634. /* hops */
  635. p[DHCP_OFF_HOPS] = 1;
  636. /* gateway address */
  637. memcpy(p + DHCP_OFF_GIADDR,
  638. &((struct sockaddr_in *) &localaddr)->sin_addr,
  639. 4);
  640. /* hardware address */
  641. memcpy(p + DHCP_OFF_CHADDR, mac_prefix, 6);
  642. /* cookie */
  643. memcpy(p + DHCP_OFF_COOKIE, dhcp_cookie, 4);
  644. /* options */
  645. p += DHCP_OFF_OPTIONS;
  646. /* inline DHCP message type */
  647. *p++ = DHCP_OPT_DHCP_MSGTYPE;
  648. *p++ = 1;
  649. *p++ = DHCP_OP_REQUEST;
  650. /* place for DHCP server id (option) */
  651. serverid_request4 = p - template_request4;
  652. p += DHCP_OPTLEN_SRVID;
  653. /* place for DHCP requested IP address (address) */
  654. *p++ = DHCP_OPT_DHCP_ADDRESS;
  655. *p++ = 4;
  656. reqaddr_request4 = p - template_request4;
  657. p += 4;
  658. /* inline DHCP parameter request list (default) */
  659. *p++ = DHCP_OPT_DHCP_PRL;
  660. *p++ = 7;
  661. *p++ = DHCP_OPT_SUBNET_MASK;
  662. *p++ = DHCP_OPT_BROADCAST;
  663. *p++ = DHCP_OPT_TIME_OFFSET;
  664. *p++ = DHCP_OPT_ROUTERS;
  665. *p++ = DHCP_OPT_DOMAIN_NAME;
  666. *p++ = DHCP_OPT_DNS_SERVERS;
  667. *p++ = DHCP_OPT_HOST_NAME;
  668. /* end */
  669. *p = DHCP_OPT_END;
  670. }
  671. /*
  672. * get a DHCPv4 client/relay third packet (usually a REQUEST) template
  673. * from the file given in the command line (-T<template-file>)
  674. * and offsets (-X,-O,-E,-S,-I).
  675. */
  676. void
  677. get_template_request4(void)
  678. {
  679. uint8_t *p = template_request4;
  680. int fd, cc, i, j;
  681. fd = open(templatefile[1], O_RDONLY);
  682. if (fd < 0) {
  683. fprintf(stderr, "open(%s): %s\n",
  684. templatefile[1], strerror(errno));
  685. exit(2);
  686. }
  687. cc = read(fd, tbuf, sizeof(tbuf));
  688. (void) close(fd);
  689. if (cc < 0) {
  690. fprintf(stderr, "read(%s): %s\n",
  691. templatefile[1], strerror(errno));
  692. exit(1);
  693. }
  694. if (cc < 100) {
  695. fprintf(stderr, "file '%s' too short\n", templatefile[1]);
  696. exit(2);
  697. }
  698. if (cc > 8193) {
  699. fprintf(stderr,"file '%s' too large\n", templatefile[1]);
  700. exit(2);
  701. }
  702. j = 0;
  703. for (i = 0; i < cc; i++) {
  704. if (isspace((int) tbuf[i]))
  705. continue;
  706. if (!isxdigit((int) tbuf[i])) {
  707. fprintf(stderr,
  708. "illegal char[%d]='%c' in file '%s'\n",
  709. i, (int) tbuf[i], templatefile[1]);
  710. exit(2);
  711. }
  712. tbuf[j] = tbuf[i];
  713. j++;
  714. }
  715. cc = j;
  716. if ((cc & 1) != 0) {
  717. fprintf(stderr,
  718. "odd number of hexadecimal digits in file '%s'\n",
  719. templatefile[1]);
  720. exit(2);
  721. }
  722. length_request4 = cc >> 1;
  723. for (i = 0; i < cc; i += 2)
  724. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  725. if (xidoffset[1] >= 0)
  726. xid_request4 = (size_t) xidoffset[1];
  727. else
  728. xid_request4 = DHCP_OFF_XID;
  729. if (xid_request4 + 4 > length_request4) {
  730. fprintf(stderr,
  731. "xid (at %zu) outside the template (length %zu)?\n",
  732. xid_request4, length_request4);
  733. exit(2);
  734. }
  735. if (rndoffset[1] >= 0)
  736. random_request4 = (size_t) rndoffset[1];
  737. else
  738. random_request4 = DHCP_OFF_CHADDR + 6;
  739. if (random_request4 > length_request4) {
  740. fprintf(stderr,
  741. "random (at %zu) outside the template (length %zu)?\n",
  742. random_request4, length_request4);
  743. exit(2);
  744. }
  745. if (elpoffset >= 0)
  746. elapsed_request4 = (size_t) elpoffset;
  747. else
  748. elapsed_request4 = DHCP_OFF_SECS;
  749. if (elapsed_request4 + 2 > length_request4) {
  750. fprintf(stderr,
  751. "secs (at %zu) outside the template (length %zu)?\n",
  752. elapsed_request4, length_request4);
  753. exit(2);
  754. }
  755. serverid_request4 = (size_t) sidoffset;
  756. if (serverid_request4 + 6 > length_request4) {
  757. fprintf(stderr,
  758. "server-id option (at %zu) outside the template "
  759. "(length %zu)?\n",
  760. serverid_request4, length_request4);
  761. exit(2);
  762. }
  763. reqaddr_request4 = (size_t) ripoffset;
  764. if (reqaddr_request4 + 4 > length_request4) {
  765. fprintf(stderr,
  766. "requested-ip-address option (at %zu) outside "
  767. "the template (length %zu)?\n",
  768. reqaddr_request4, length_request4);
  769. exit(2);
  770. }
  771. }
  772. /*
  773. * send the DHCPv4 REQUEST third packet
  774. * (the transaction ID is odd)
  775. * (TODO: check for errors in the OFFER)
  776. */
  777. void
  778. send_request4(struct exchange *x0)
  779. {
  780. struct exchange *x;
  781. struct xlist *bucket;
  782. uint32_t hash;
  783. ssize_t ret;
  784. x = (struct exchange *) malloc(sizeof(*x));
  785. if (x == NULL) {
  786. locallimit++;
  787. perror("send2");
  788. return;
  789. }
  790. memcpy(x, x0, sizeof(*x));
  791. x->order2 = xscount2++;
  792. x->xid |= 1;
  793. hash = x->xid >> 1;
  794. ISC_TAILQ_INSERT_TAIL(&xsent2, x, gchain);
  795. hash &= hashsize2 - 1;
  796. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  797. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  798. memcpy(obuf, template_request4, length_request4);
  799. /* xid */
  800. memcpy(obuf + xid_request4, &x->xid, 4);
  801. /* random */
  802. randomize(random_request4, x->rnd);
  803. /* secs */
  804. if (elapsed_request4 > 0) {
  805. int secs;
  806. secs = x->ts1.tv_sec - x->ts0.tv_sec;
  807. if (x->ts1.tv_nsec < x->ts0.tv_nsec)
  808. secs += 1;
  809. if (secs > 0) {
  810. obuf[elapsed_request4] = secs >> 8;
  811. obuf[elapsed_request4 + 1] = secs & 0xff;
  812. }
  813. }
  814. /* server ID */
  815. memcpy(obuf + serverid_request4, x->sid, x->sidlen);
  816. /* requested IP address */
  817. memcpy(obuf + reqaddr_request4, ibuf + DHCP_OFF_YIADDR, 4);
  818. /* timestamp */
  819. ret = clock_gettime(CLOCK_REALTIME, &x->ts2);
  820. if (ret < 0) {
  821. perror("clock_gettime(send2)");
  822. fatal = 1;
  823. return;
  824. }
  825. ret = sendto(sock, obuf, length_request4, 0,
  826. (struct sockaddr *) &serveraddr,
  827. sizeof(struct sockaddr_in));
  828. if (ret >= 0)
  829. return;
  830. if ((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
  831. (errno == ENOBUFS) || (errno == ENOMEM))
  832. locallimit++;
  833. perror("send2");
  834. }
  835. /*
  836. * send the DHCPv4 DISCOVER first packet
  837. * (for 4-exchange, the transaction ID xid is even)
  838. */
  839. int
  840. send4(void)
  841. {
  842. struct exchange *x;
  843. struct xlist *bucket;
  844. uint32_t hash;
  845. ssize_t ret;
  846. x = (struct exchange *) malloc(sizeof(*x));
  847. if (x == NULL)
  848. return -ENOMEM;
  849. memset(x, 0, sizeof(*x));
  850. x->order0 = xscount0++;
  851. hash = x->rnd = (uint32_t) random();
  852. if (simple == 0)
  853. x->xid = hash << 1;
  854. else
  855. x->xid = hash;
  856. ISC_TAILQ_INSERT_TAIL(&xsent0, x, gchain);
  857. hash &= hashsize0 - 1;
  858. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  859. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  860. memcpy(obuf, template_discover4, length_discover4);
  861. /* xid */
  862. memcpy(obuf + xid_discover4, &x->xid, 4);
  863. /* random */
  864. x->rnd = randomize(random_discover4, x->rnd);
  865. /* timestamp */
  866. ret = clock_gettime(CLOCK_REALTIME, &last);
  867. if (ret < 0) {
  868. perror("clock_gettime(send)");
  869. fatal = 1;
  870. return -errno;
  871. }
  872. x->ts0 = last;
  873. errno = 0;
  874. ret = sendto(sock, obuf, length_discover4, 0,
  875. (struct sockaddr *) &serveraddr,
  876. sizeof(struct sockaddr_in));
  877. if (ret == (ssize_t) length_discover4)
  878. return 0;
  879. return -errno;
  880. }
  881. /*
  882. * scan a DHCPv4 OFFER to get its server-id option
  883. */
  884. int
  885. scan_for_srvid4(struct exchange *x, size_t cc)
  886. {
  887. size_t off = DHCP_OFF_OPTIONS;
  888. for (;;) {
  889. if (off + DHCP_OPTLEN_SRVID > cc) {
  890. fprintf(stderr, "truncated\n");
  891. return -1;
  892. }
  893. if (ibuf[off] == DHCP_OPT_DHCP_SRVID)
  894. break;
  895. if (ibuf[off] == DHCP_OPT_END) {
  896. fprintf(stderr, "server-id not found\n");
  897. return -1;
  898. }
  899. if (ibuf[off] == DHCP_OPT_PAD) {
  900. off++;
  901. continue;
  902. }
  903. off += 2 + ibuf[off + 1];
  904. }
  905. /* check length */
  906. if (ibuf[off + 1] != DHCP_OPTLEN_SRVID - 2) {
  907. fprintf(stderr,
  908. "bad server-id length (%hhu)\n",
  909. ibuf[off + 1]);
  910. return -1;
  911. }
  912. /* cache it in the global variables when required and not yet done */
  913. if ((usefirst != 0) && (gsrvid == NULL)) {
  914. memcpy(gsrvidbuf, ibuf + off, DHCP_OPTLEN_SRVID);
  915. gsrvid = gsrvidbuf;
  916. gsrvidlen = DHCP_OPTLEN_SRVID;
  917. }
  918. x->sid = ibuf + off;
  919. x->sidlen = DHCP_OPTLEN_SRVID;
  920. return 0;
  921. }
  922. /*
  923. * receive a DHCPv4 packet
  924. */
  925. void
  926. receive4(void)
  927. {
  928. struct exchange *x, *t;
  929. struct xlist *bucket;
  930. struct timespec now;
  931. ssize_t cc;
  932. uint32_t xid, hash;
  933. int checklost = 0;
  934. double delta;
  935. cc = recv(sock, ibuf, sizeof(ibuf), 0);
  936. if (cc < 0) {
  937. if ((errno == EAGAIN) ||
  938. (errno == EWOULDBLOCK) ||
  939. (errno == EINTR))
  940. return;
  941. perror("recv");
  942. fatal = 1;
  943. return;
  944. }
  945. /* enforce a reasonable length */
  946. if (cc < BOOTP_MIN_LEN) {
  947. tooshort++;
  948. return;
  949. }
  950. /* must be a BOOTP REPLY */
  951. if (ibuf[DHCP_OFF_OPCODE] != BOOTP_OP_REPLY)
  952. return;
  953. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  954. perror("clock_gettime(receive)");
  955. fatal = 1;
  956. return;
  957. }
  958. memcpy(&xid, ibuf + xid_discover4, 4);
  959. /* 4-packet exchange even/odd xid */
  960. if (simple == 0) {
  961. if ((xid & 1) != 0) {
  962. receive_reply(xid, &now);
  963. return;
  964. }
  965. hash = (xid >> 1) & (hashsize0 - 1);
  966. } else
  967. hash = xid & (hashsize0 - 1);
  968. /* now it is the second packet, get the bucket which is needed */
  969. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  970. /* try the 'next to be received' cache */
  971. if ((xnext0 != NULL) && (xnext0->xid == xid)) {
  972. x = xnext0;
  973. goto found;
  974. }
  975. /* if the rate is not illimited, garbage collect up to 3
  976. timed-out exchanges */
  977. if (rate != 0)
  978. checklost = 3;
  979. /* look for the exchange */
  980. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  981. double waited;
  982. if (x->xid == xid)
  983. goto found;
  984. if (checklost <= 0)
  985. continue;
  986. /* check for a timed-out exchange */
  987. waited = now.tv_sec - x->ts0.tv_sec;
  988. waited += (now.tv_nsec - x->ts0.tv_nsec) / 1e9;
  989. if (waited < losttime[0]) {
  990. checklost = 0;
  991. continue;
  992. }
  993. /* garbage collect timed-out exchange */
  994. checklost--;
  995. ISC_TAILQ_REMOVE(bucket, x, hchain);
  996. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  997. free(x);
  998. collected[0] += 1;
  999. }
  1000. /* no match? very late or not for us */
  1001. orphans++;
  1002. return;
  1003. /* got it: update stats and move to the received queue */
  1004. found:
  1005. xrcount0++;
  1006. x->ts1 = now;
  1007. delta = x->ts1.tv_sec - x->ts0.tv_sec;
  1008. delta += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1009. if (delta < dmin0)
  1010. dmin0 = delta;
  1011. if (delta > dmax0)
  1012. dmax0 = delta;
  1013. dsum0 += delta;
  1014. dsumsq0 += delta * delta;
  1015. xnext0 = ISC_TAILQ_NEXT(x, gchain);
  1016. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1017. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1018. ISC_TAILQ_INSERT_TAIL(&xrcvd0, x, gchain);
  1019. /* if the exchange is not finished, go to the second part */
  1020. if (simple == 0) {
  1021. int ret = 0;
  1022. /* the server-ID option is needed */
  1023. if ((usefirst != 0) && (gsrvid != NULL)) {
  1024. x->sid = gsrvid;
  1025. x->sidlen = gsrvidlen;
  1026. } else
  1027. ret = scan_for_srvid4(x, cc);
  1028. if (ret >= 0)
  1029. send_request4(x);
  1030. }
  1031. }
  1032. /*
  1033. * get the DHCPv6 socket descriptor
  1034. */
  1035. void
  1036. getsock6(void)
  1037. {
  1038. struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &serveraddr;
  1039. int ret;
  1040. /* update local port */
  1041. if (localport != 0) {
  1042. uint16_t lp = htons((uint16_t) localport);
  1043. ((struct sockaddr_in6 *) &localaddr)->sin6_port = lp;
  1044. }
  1045. sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  1046. if (sock < 0) {
  1047. perror("socket");
  1048. exit(1);
  1049. }
  1050. /* perform the multicast stuff when the destination is multicast */
  1051. if (IN6_IS_ADDR_MULTICAST(&s6->sin6_addr)) {
  1052. int hops = 1;
  1053. ret = setsockopt(sock,
  1054. IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1055. &hops, sizeof(hops));
  1056. if (ret < 0) {
  1057. perror("setsockopt(IPV6_MULTICAST_HOPS)");
  1058. exit(1);
  1059. }
  1060. }
  1061. if (isinterface && IN6_IS_ADDR_MULTICAST(&s6->sin6_addr)) {
  1062. unsigned idx = if_nametoindex(localname);
  1063. if (idx == 0) {
  1064. fprintf(stderr,
  1065. "if_nametoindex(%s) failed\n",
  1066. localname);
  1067. exit(1);
  1068. }
  1069. ret = setsockopt(sock,
  1070. IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1071. &idx, sizeof(idx));
  1072. if (ret < 0) {
  1073. perror("setsockopt(IPV6_MULTICAST_IF)");
  1074. exit(1);
  1075. }
  1076. }
  1077. }
  1078. /*
  1079. * build a DHCPv6 SOLICIT template
  1080. * (implicit parameter is the DUID, don't assume an Ethernet link)
  1081. */
  1082. void
  1083. build_template_solicit6(void)
  1084. {
  1085. uint8_t *p = template_solicit6;
  1086. xid_solicit6 = DHCP6_OFF_XID;
  1087. /* message type */
  1088. p[DHCP6_OFF_MSGTYP] = DHCP6_OP_SOLICIT;
  1089. /* options */
  1090. p += DHCP6_OFF_OPTIONS;
  1091. /* elapsed time */
  1092. p[1] = DHCP6_OPT_ELAPSED_TIME;
  1093. p[3] = 2;
  1094. p += 6;
  1095. /* rapid commit */
  1096. if (rapidcommit != 0) {
  1097. p[1] = DHCP6_OPT_RAPID_COMMIT;
  1098. p += 4;
  1099. }
  1100. /* client ID */
  1101. p[1] = DHCP6_OPT_CLIENTID;
  1102. p[3] = duid_length;
  1103. memcpy(p + 4, duid_prefix, duid_length);
  1104. p += 4 + duid_length;
  1105. random_solicit6 = p - template_solicit6;
  1106. /* option request option */
  1107. p[1] = DHCP6_OPT_ORO;
  1108. p[3] = 4;
  1109. p[5] = DHCP6_OPT_NAME_SERVERS;
  1110. p[7] = DHCP6_OPT_DOMAIN_SEARCH;
  1111. p += 8;
  1112. /* IA_NA (IAID = 1, T1 = 3600, T2 = 5400) */
  1113. p[1] = DHCP6_OPT_IA_NA;
  1114. p[3] = 12;
  1115. p[7] = 1;
  1116. p[10] = 3600 >> 8;
  1117. p[11] = 3600 & 0xff;
  1118. p[14] = 5400 >> 8;
  1119. p[15] = 5400 & 0xff;
  1120. p += 16;
  1121. /* set length */
  1122. length_solicit6 = p - template_solicit6;
  1123. }
  1124. /*
  1125. * get a DHCPv6 first packet (usually a SOLICIT) template
  1126. * from the file given in the command line (-T<template-file>)
  1127. * and xid/rnd offsets (-X<xid-offset> and -O<random-offset>)
  1128. */
  1129. void
  1130. get_template_solicit6(void)
  1131. {
  1132. uint8_t *p = template_solicit6;
  1133. int fd, cc, i, j;
  1134. fd = open(templatefile[0], O_RDONLY);
  1135. if (fd < 0) {
  1136. fprintf(stderr, "open(%s): %s\n",
  1137. templatefile[0], strerror(errno));
  1138. exit(2);
  1139. }
  1140. cc = read(fd, tbuf, sizeof(tbuf));
  1141. (void) close(fd);
  1142. if (cc < 0) {
  1143. fprintf(stderr, "read(%s): %s\n",
  1144. templatefile[0], strerror(errno));
  1145. exit(1);
  1146. }
  1147. if (cc < 10) {
  1148. fprintf(stderr, "file '%s' too short\n", templatefile[0]);
  1149. exit(2);
  1150. }
  1151. if (cc > 8193) {
  1152. fprintf(stderr,"file '%s' too large\n", templatefile[0]);
  1153. exit(2);
  1154. }
  1155. j = 0;
  1156. for (i = 0; i < cc; i++) {
  1157. if (isspace((int) tbuf[i]))
  1158. continue;
  1159. if (!isxdigit((int) tbuf[i])) {
  1160. fprintf(stderr,
  1161. "illegal char[%d]='%c' in file '%s'\n",
  1162. i, (int) tbuf[i], templatefile[0]);
  1163. exit(2);
  1164. }
  1165. tbuf[j] = tbuf[i];
  1166. j++;
  1167. }
  1168. cc = j;
  1169. if ((cc & 1) != 0) {
  1170. fprintf(stderr,
  1171. "odd number of hexadecimal digits in file '%s'\n",
  1172. templatefile[0]);
  1173. exit(2);
  1174. }
  1175. length_solicit6 = cc >> 1;
  1176. for (i = 0; i < cc; i += 2)
  1177. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  1178. if (xidoffset[0] >= 0)
  1179. xid_solicit6 = (size_t) xidoffset[0];
  1180. else
  1181. xid_solicit6 = DHCP6_OFF_XID;
  1182. if (xid_solicit6 + 3 > length_solicit6) {
  1183. fprintf(stderr,
  1184. "xid (at %zu) is outside the template (length %zu)?\n",
  1185. xid_solicit6, length_solicit6);
  1186. exit(2);
  1187. }
  1188. if (rndoffset[0] >= 0)
  1189. random_solicit6 = (size_t) rndoffset[0];
  1190. else
  1191. random_solicit6 = 0;
  1192. if (random_solicit6 > length_solicit6) {
  1193. fprintf(stderr,
  1194. "random (at %zu) outside the template (length %zu)?\n",
  1195. random_solicit6, length_solicit6);
  1196. exit(2);
  1197. }
  1198. }
  1199. /*
  1200. * build a DHCPv6 REQUEST template
  1201. * (implicit parameter is the DUID, don't assume an Ethernet link)
  1202. */
  1203. void
  1204. build_template_request6(void)
  1205. {
  1206. uint8_t *p = template_request6;
  1207. xid_request6 = DHCP6_OFF_XID;
  1208. /* message type */
  1209. p[DHCP6_OFF_MSGTYP] = DHCP6_OP_REQUEST;
  1210. /* options */
  1211. p += DHCP6_OFF_OPTIONS;
  1212. /* elapsed time */
  1213. p[1] = DHCP6_OPT_ELAPSED_TIME;
  1214. p[3] = 2;
  1215. p += 4;
  1216. elapsed_request6 = p - template_request6;
  1217. p += 2;
  1218. /* client ID */
  1219. p[1] = DHCP6_OPT_CLIENTID;
  1220. p[3] = duid_length;
  1221. memcpy(p + 4, duid_prefix, duid_length);
  1222. p += 4 + duid_length;
  1223. random_request6 = p - template_request6;
  1224. /* option request option */
  1225. p[1] = DHCP6_OPT_ORO;
  1226. p[3] = 4;
  1227. p[5] = DHCP6_OPT_NAME_SERVERS;
  1228. p[7] = DHCP6_OPT_DOMAIN_SEARCH;
  1229. p += 8;
  1230. /* server ID and IA_NA */
  1231. serverid_request6 = p - template_request6;
  1232. reqaddr_request6 = p - template_request6;
  1233. /* set length */
  1234. length_request6 = p - template_request6;
  1235. }
  1236. /*
  1237. * get a DHCPv6 third packet (usually a REQUEST) template
  1238. * from the file given in the command line (-T<template-file>)
  1239. * and offsets (-X,-O,-E,-S,-I).
  1240. */
  1241. void
  1242. get_template_request6(void)
  1243. {
  1244. uint8_t *p = template_request6;
  1245. int fd, cc, i, j;
  1246. fd = open(templatefile[1], O_RDONLY);
  1247. if (fd < 0) {
  1248. fprintf(stderr, "open(%s): %s\n",
  1249. templatefile[1], strerror(errno));
  1250. exit(2);
  1251. }
  1252. cc = read(fd, tbuf, sizeof(tbuf));
  1253. (void) close(fd);
  1254. if (cc < 0) {
  1255. fprintf(stderr, "read(%s): %s\n",
  1256. templatefile[1], strerror(errno));
  1257. exit(1);
  1258. }
  1259. if (cc < 10) {
  1260. fprintf(stderr, "file '%s' too short\n", templatefile[1]);
  1261. exit(2);
  1262. }
  1263. if (cc > 8193) {
  1264. fprintf(stderr,"file '%s' too large\n", templatefile[1]);
  1265. exit(2);
  1266. }
  1267. j = 0;
  1268. for (i = 0; i < cc; i++) {
  1269. if (isspace((int) tbuf[i]))
  1270. continue;
  1271. if (!isxdigit((int) tbuf[i])) {
  1272. fprintf(stderr,
  1273. "illegal char[%d]='%c' in file '%s'\n",
  1274. i, (int) tbuf[i], templatefile[1]);
  1275. exit(2);
  1276. }
  1277. tbuf[j] = tbuf[i];
  1278. j++;
  1279. }
  1280. cc = j;
  1281. if ((cc & 1) != 0) {
  1282. fprintf(stderr,
  1283. "odd number of hexadecimal digits in file '%s'\n",
  1284. templatefile[1]);
  1285. exit(2);
  1286. }
  1287. length_request6 = cc >> 1;
  1288. for (i = 0; i < cc; i += 2)
  1289. (void) sscanf(tbuf + i, "%02hhx", &p[i >> 1]);
  1290. if (xidoffset[1] >= 0)
  1291. xid_request6 = (size_t) xidoffset[1];
  1292. else
  1293. xid_request6 = DHCP6_OFF_XID;
  1294. if (xid_request6 + 3 > length_request6) {
  1295. fprintf(stderr,
  1296. "xid (at %zu) is outside the template (length %zu)?\n",
  1297. xid_request6, length_request6);
  1298. exit(2);
  1299. }
  1300. if (rndoffset[1] >= 0)
  1301. random_request6 = (size_t) rndoffset[1];
  1302. else
  1303. random_request6 = 0;
  1304. if (random_request6 > length_request6) {
  1305. fprintf(stderr,
  1306. "random (at %zu) outside the template (length %zu)?\n",
  1307. random_request6, length_request6);
  1308. exit(2);
  1309. }
  1310. if (elpoffset >= 0)
  1311. elapsed_request6 = (size_t) elpoffset;
  1312. if (elapsed_request6 + 2 > length_request6) {
  1313. fprintf(stderr,
  1314. "secs (at %zu) outside the template (length %zu)?\n",
  1315. elapsed_request6, length_request6);
  1316. exit(2);
  1317. }
  1318. serverid_request6 = (size_t) sidoffset;
  1319. if (serverid_request6 > length_request6) {
  1320. fprintf(stderr,
  1321. "server-id option (at %zu) outside the template "
  1322. "(length %zu)?\n",
  1323. serverid_request6, length_request6);
  1324. exit(2);
  1325. }
  1326. reqaddr_request6 = (size_t) ripoffset;
  1327. if (reqaddr_request6 > length_request6) {
  1328. fprintf(stderr,
  1329. "requested-ip-address option (at %zu) outside "
  1330. "the template (length %zu)?\n",
  1331. reqaddr_request6, length_request6);
  1332. exit(2);
  1333. }
  1334. }
  1335. /*
  1336. * send the DHCPv6 REQUEST third packet
  1337. * (the transaction ID is odd)
  1338. * (TODO: check for errors in the ADVERTISE)
  1339. */
  1340. void
  1341. send_request6(struct exchange *x0)
  1342. {
  1343. struct exchange *x;
  1344. struct xlist *bucket;
  1345. size_t len;
  1346. uint32_t hash;
  1347. ssize_t ret;
  1348. x = (struct exchange *) malloc(sizeof(*x));
  1349. if (x == NULL) {
  1350. locallimit++;
  1351. perror("send2");
  1352. return;
  1353. }
  1354. memcpy(x, x0, sizeof(*x));
  1355. x->order2 = xscount2++;
  1356. x->xid |= 1;
  1357. hash = x->xid >> 1;
  1358. ISC_TAILQ_INSERT_TAIL(&xsent2, x, gchain);
  1359. hash &= hashsize2 - 1;
  1360. bucket = (struct xlist *) (exchanges2 + hash * sizeof(*bucket));
  1361. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  1362. len = length_request6;
  1363. memcpy(obuf, template_request6, len);
  1364. /* xid */
  1365. obuf[xid_request6] = x->xid >> 16;
  1366. obuf[xid_request6 + 1] = x->xid >> 8;
  1367. obuf[xid_request6 + 2] = x->xid;
  1368. /* random */
  1369. randomize(random_request6, x->rnd);
  1370. /* elapsed time */
  1371. if (elapsed_request6 > 0) {
  1372. int et;
  1373. et = (x->ts1.tv_sec - x->ts0.tv_sec) * 100;
  1374. et += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 10000000;
  1375. if (et > 65535) {
  1376. obuf[elapsed_request6] = 0xff;
  1377. obuf[elapsed_request6 + 1] = 0xff;
  1378. } else if (et > 0) {
  1379. obuf[elapsed_request6] = et >> 8;
  1380. obuf[elapsed_request6 + 1] = et & 0xff;
  1381. }
  1382. }
  1383. /* server ID */
  1384. if (serverid_request6 < length_request6)
  1385. memmove(obuf + serverid_request6 + x->sidlen,
  1386. obuf + serverid_request6,
  1387. x->sidlen);
  1388. memcpy(obuf + serverid_request6, x->sid, x->sidlen);
  1389. len += x->sidlen;
  1390. /* IA_NA */
  1391. if (reqaddr_request6 < serverid_request6) {
  1392. memmove(obuf + reqaddr_request6 + x->ianalen,
  1393. obuf + reqaddr_request6,
  1394. x->ianalen);
  1395. memcpy(obuf + reqaddr_request6, x->iana, x->ianalen);
  1396. } else if (reqaddr_request6 < length_request6) {
  1397. memmove(obuf + reqaddr_request6 + x->sidlen + x->ianalen,
  1398. obuf + reqaddr_request6 + x->sidlen,
  1399. x->ianalen);
  1400. memcpy(obuf + reqaddr_request6+ x->sidlen,
  1401. x->iana,
  1402. x->ianalen);
  1403. } else
  1404. memcpy(obuf + len, x->iana, x->ianalen);
  1405. len += x->ianalen;
  1406. /* timestamp */
  1407. ret = clock_gettime(CLOCK_REALTIME, &x->ts2);
  1408. if (ret < 0) {
  1409. perror("clock_gettime(send2)");
  1410. fatal = 1;
  1411. return;
  1412. }
  1413. ret = sendto(sock, obuf, len, 0,
  1414. (struct sockaddr *) &serveraddr,
  1415. sizeof(struct sockaddr_in6));
  1416. if (ret >= 0)
  1417. return;
  1418. if ((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
  1419. (errno == ENOBUFS) || (errno == ENOMEM))
  1420. locallimit++;
  1421. perror("send2");
  1422. }
  1423. /*
  1424. * send the DHCPv6 SOLICIT first packet
  1425. * (for 4-exchange, the transaction ID xid is even)
  1426. */
  1427. int
  1428. send6(void)
  1429. {
  1430. struct exchange *x;
  1431. struct xlist *bucket;
  1432. uint32_t hash;
  1433. ssize_t ret;
  1434. x = (struct exchange *) malloc(sizeof(*x));
  1435. if (x == NULL)
  1436. return -ENOMEM;
  1437. memset(x, 0, sizeof(*x));
  1438. x->order0 = xscount0++;
  1439. hash = x->rnd = (uint32_t) random();
  1440. if (simple == 0)
  1441. x->xid = (hash << 1) & 0x00ffffff;
  1442. else
  1443. x->xid = hash & 0x00ffffff;
  1444. ISC_TAILQ_INSERT_TAIL(&xsent0, x, gchain);
  1445. hash &= hashsize0 - 1;
  1446. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  1447. ISC_TAILQ_INSERT_TAIL(bucket, x, hchain);
  1448. memcpy(obuf, template_solicit6, length_solicit6);
  1449. /* xid */
  1450. obuf[xid_solicit6] = x->xid >> 16;
  1451. obuf[xid_solicit6 + 1] = x->xid >> 8;
  1452. obuf[xid_solicit6 + 2] = x->xid;
  1453. /* random */
  1454. x->rnd = randomize(random_solicit6, x->rnd);
  1455. /* timestamp */
  1456. ret = clock_gettime(CLOCK_REALTIME, &last);
  1457. if (ret < 0) {
  1458. perror("clock_gettime(send)");
  1459. fatal = 1;
  1460. return -errno;
  1461. }
  1462. x->ts0 = last;
  1463. errno = 0;
  1464. ret = sendto(sock, obuf, length_solicit6, 0,
  1465. (struct sockaddr *) &serveraddr,
  1466. sizeof(struct sockaddr_in6));
  1467. if (ret == (ssize_t) length_solicit6)
  1468. return 0;
  1469. return -errno;
  1470. }
  1471. /*
  1472. * scan a DHCPv6 ADVERTISE to get its server-id option
  1473. */
  1474. int
  1475. scan_for_srvid6(struct exchange *x, size_t cc)
  1476. {
  1477. size_t off = DHCP6_OFF_OPTIONS, len;
  1478. for (;;) {
  1479. if (off + 4 > cc) {
  1480. fprintf(stderr, "server-id not found\n");
  1481. return -1;
  1482. }
  1483. if ((ibuf[off] == 0) && (ibuf[off + 1] == DHCP6_OPT_SERVERID))
  1484. break;
  1485. off += 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1486. }
  1487. len = 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1488. /* cache it in the global variables when required and not yet done */
  1489. if ((usefirst != 0) && (gsrvid == NULL)) {
  1490. if (len <= sizeof(gsrvidbuf)) {
  1491. memcpy(gsrvidbuf, ibuf + off, len);
  1492. gsrvid = gsrvidbuf;
  1493. gsrvidlen = len;
  1494. } else {
  1495. gsrvid = (uint8_t *) malloc(len);
  1496. if (gsrvid == NULL) {
  1497. perror("malloc(gsrvid");
  1498. return -1;
  1499. }
  1500. memcpy(gsrvid, ibuf + off, len);
  1501. gsrvidlen = len;
  1502. }
  1503. }
  1504. x->sid = ibuf + off;
  1505. x->sidlen = len;
  1506. return 0;
  1507. }
  1508. /*
  1509. * scan a DHCPv6 ADVERTISE to get its IA_NA option
  1510. * (TODO: check for errors)
  1511. */
  1512. int
  1513. scan_for_ia_na(struct exchange *x, size_t cc)
  1514. {
  1515. size_t off = DHCP6_OFF_OPTIONS, len;
  1516. for (;;) {
  1517. if (off + 4 > cc) {
  1518. fprintf(stderr, "ia-na not found\n");
  1519. return -1;
  1520. }
  1521. if ((ibuf[off] == 0) && (ibuf[off + 1] == DHCP6_OPT_IA_NA))
  1522. break;
  1523. off += 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1524. }
  1525. len = 4 + (ibuf[off + 2] << 8) + ibuf[off + 3];
  1526. x->iana = ibuf + off;
  1527. x->ianalen = len;
  1528. return 0;
  1529. }
  1530. /*
  1531. * receive a DHCPv6 packet
  1532. */
  1533. void
  1534. receive6(void)
  1535. {
  1536. struct exchange *x, *t;
  1537. struct xlist *bucket;
  1538. struct timespec now;
  1539. ssize_t cc;
  1540. uint32_t xid, hash;
  1541. int checklost = 0;
  1542. double delta;
  1543. cc = recv(sock, ibuf, sizeof(ibuf), 0);
  1544. if (cc < 0) {
  1545. if ((errno == EAGAIN) ||
  1546. (errno == EWOULDBLOCK) ||
  1547. (errno == EINTR))
  1548. return;
  1549. perror("recv");
  1550. fatal = 1;
  1551. return;
  1552. }
  1553. /* enforce a reasonable length */
  1554. if (cc < 22) {
  1555. tooshort++;
  1556. return;
  1557. }
  1558. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  1559. perror("clock_gettime(receive)");
  1560. fatal = 1;
  1561. return;
  1562. }
  1563. xid = ibuf[xid_solicit6] << 16;
  1564. xid |= ibuf[xid_solicit6 + 1] << 8;
  1565. xid |= ibuf[xid_solicit6 + 2];
  1566. /* 4-packet exchange even/odd xid */
  1567. if (simple == 0) {
  1568. if ((xid & 1) != 0) {
  1569. receive_reply(xid, &now);
  1570. return;
  1571. }
  1572. hash = (xid >> 1) & (hashsize0 - 1);
  1573. } else
  1574. hash = xid & (hashsize0 - 1);
  1575. /* now it is the second packet, get the bucket which is needed */
  1576. bucket = (struct xlist *) (exchanges0 + hash * sizeof(*bucket));
  1577. /* try the 'next to be received' cache */
  1578. if ((xnext0 != NULL) && (xnext0->xid == xid)) {
  1579. x = xnext0;
  1580. goto found;
  1581. }
  1582. /* if the rate is not illimited, garbage collect up to 3
  1583. timed-out exchanges */
  1584. if (rate != 0)
  1585. checklost = 3;
  1586. /* look for the exchange */
  1587. ISC_TAILQ_FOREACH_SAFE(x, bucket, hchain, t) {
  1588. double waited;
  1589. if (x->xid == xid)
  1590. goto found;
  1591. if (checklost <= 0)
  1592. continue;
  1593. /* check for a timed-out exchange */
  1594. waited = now.tv_sec - x->ts0.tv_sec;
  1595. waited += (now.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1596. if (waited < losttime[0]) {
  1597. checklost = 0;
  1598. continue;
  1599. }
  1600. /* garbage collect timed-out exchange */
  1601. checklost--;
  1602. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1603. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1604. free(x);
  1605. collected[0] += 1;
  1606. }
  1607. /* no match? very late or not for us */
  1608. orphans++;
  1609. return;
  1610. /* got it: update stats and move to the received queue */
  1611. found:
  1612. xrcount0++;
  1613. x->ts1 = now;
  1614. delta = x->ts1.tv_sec - x->ts0.tv_sec;
  1615. delta += (x->ts1.tv_nsec - x->ts0.tv_nsec) / 1e9;
  1616. if (delta < dmin0)
  1617. dmin0 = delta;
  1618. if (delta > dmax0)
  1619. dmax0 = delta;
  1620. dsum0 += delta;
  1621. dsumsq0 += delta * delta;
  1622. xnext0 = ISC_TAILQ_NEXT(x, gchain);
  1623. ISC_TAILQ_REMOVE(bucket, x, hchain);
  1624. ISC_TAILQ_REMOVE(&xsent0, x, gchain);
  1625. ISC_TAILQ_INSERT_TAIL(&xrcvd0, x, gchain);
  1626. /* if the exchange is not finished, go to the second part */
  1627. if (simple == 0) {
  1628. int ret = 0;
  1629. /* the server-ID option is needed */
  1630. if ((usefirst != 0) && (gsrvid != NULL)) {
  1631. x->sid = gsrvid;
  1632. x->sidlen = gsrvidlen;
  1633. } else
  1634. ret = scan_for_srvid6(x, cc);
  1635. /* and the IA_NA option too */
  1636. if (ret >= 0)
  1637. ret = scan_for_ia_na(x, cc);
  1638. if (ret >= 0)
  1639. send_request6(x);
  1640. }
  1641. }
  1642. /*
  1643. * decode a base command line parameter
  1644. * (currently only MAC address and DUID are supported)
  1645. */
  1646. void
  1647. decodebase(void)
  1648. {
  1649. char *b0 = base[basecnt];
  1650. /* MAC address (alias Ethernet address) */
  1651. if ((strncasecmp(b0, "mac=", 4) == 0) ||
  1652. (strncasecmp(b0, "ether=", 6) == 0)) {
  1653. char *b;
  1654. b = strchr(b0, '=') + 1;
  1655. if (sscanf(b, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  1656. &mac_prefix[0], &mac_prefix[1],
  1657. &mac_prefix[2], &mac_prefix[3],
  1658. &mac_prefix[4], &mac_prefix[5]) == 6) {
  1659. if ((diags != NULL) && (strchr(diags, 'a') != NULL))
  1660. printf("set MAC to %s\n", b);
  1661. return;
  1662. }
  1663. fprintf(stderr,
  1664. "expected base in '%*s=xx:xx:xx:xx:xx:xx' format\n",
  1665. (int) (b - b0 - 1), b0);
  1666. exit(2);
  1667. }
  1668. /* DUID */
  1669. if (strncasecmp(b0, "duid=", 5) == 0) {
  1670. char *b;
  1671. size_t i, l;
  1672. if (duid_prefix != NULL) {
  1673. fprintf(stderr, "duid was already set?\n");
  1674. exit(2);
  1675. }
  1676. b = strchr(b0, '=') + 1;
  1677. l = 0;
  1678. while (*b != '\0') {
  1679. if (!isxdigit((int) *b)) {
  1680. fprintf(stderr,
  1681. "illegal char '%c' in duid\n",
  1682. (int) *b);
  1683. exit(2);
  1684. }
  1685. b++;
  1686. l++;
  1687. }
  1688. if ((l & 1) != 0) {
  1689. fprintf(stderr,
  1690. "odd number of hexadecimal digits in duid\n");
  1691. exit(2);
  1692. }
  1693. l /= 2;
  1694. if (l > 64) {
  1695. fprintf(stderr, "duid too large\n");
  1696. exit(2);
  1697. }
  1698. duid_prefix = (uint8_t *) malloc(l);
  1699. if (duid_prefix == NULL) {
  1700. perror("malloc(duid)");
  1701. exit(1);
  1702. }
  1703. b = strchr(b0, '=') + 1;
  1704. for (i = 0; i < l; i++, b += 2)
  1705. (void) sscanf(b, "%02hhx", &duid_prefix[i]);
  1706. duid_length = l;
  1707. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  1708. b = strchr(b0, '=') + 1;
  1709. printf("set DUID[%d] to %s\n",
  1710. duid_length, b);
  1711. }
  1712. return;
  1713. }
  1714. /* other */
  1715. fprintf(stderr, "not yet supported base '%s'\n", b0);
  1716. exit(2);
  1717. }
  1718. /*
  1719. * get the server socket address from the command line:
  1720. * - flags: inherited from main, 0 or AI_NUMERICHOST (for literals)
  1721. */
  1722. void
  1723. getserveraddr(const int flags)
  1724. {
  1725. struct addrinfo hints, *res;
  1726. char *service;
  1727. int ret;
  1728. memset(&hints, 0, sizeof(hints));
  1729. if (ipversion == 4) {
  1730. hints.ai_family = AF_INET;
  1731. service = "67";
  1732. } else {
  1733. hints.ai_family = AF_INET6;
  1734. service = "547";
  1735. }
  1736. hints.ai_socktype = SOCK_DGRAM;
  1737. hints.ai_flags = AI_NUMERICSERV | flags;
  1738. #if defined(AI_ADDRCONFIG)
  1739. hints.ai_flags |= AI_ADDRCONFIG;
  1740. #endif
  1741. hints.ai_protocol = IPPROTO_UDP;
  1742. ret = getaddrinfo(servername, service, &hints, &res);
  1743. if (ret != 0) {
  1744. fprintf(stderr, "bad server=%s: %s\n",
  1745. servername, gai_strerror(ret));
  1746. exit(2);
  1747. }
  1748. if (res->ai_next != NULL) {
  1749. fprintf(stderr, "ambiguous server=%s\n", servername);
  1750. exit(2);
  1751. }
  1752. memcpy(&serveraddr, res->ai_addr, res->ai_addrlen);
  1753. freeaddrinfo(res);
  1754. }
  1755. /*
  1756. * get the interface from the command line:
  1757. * - name of the interface
  1758. * - socket address to fill
  1759. * (in IPv6, get the first link-local address)
  1760. */
  1761. void
  1762. getinterface(const char *name, struct sockaddr_storage *ss)
  1763. {
  1764. struct ifaddrs *ifaddr, *ifa;
  1765. int ret;
  1766. size_t len;
  1767. ret = getifaddrs(&ifaddr);
  1768. if (ret < 0) {
  1769. perror("getifaddrs");
  1770. exit(1);
  1771. }
  1772. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  1773. if (ifa->ifa_name == NULL)
  1774. continue;
  1775. if (strcmp(ifa->ifa_name, name) != 0)
  1776. continue;
  1777. if ((ifa->ifa_flags & IFF_UP) == 0)
  1778. continue;
  1779. if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
  1780. continue;
  1781. if (ifa->ifa_addr == NULL)
  1782. continue;
  1783. if ((ipversion == 4) &&
  1784. (ifa->ifa_addr->sa_family != AF_INET))
  1785. continue;
  1786. if (ipversion == 6) {
  1787. struct sockaddr_in6 *a6;
  1788. a6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  1789. if (a6->sin6_family != AF_INET6)
  1790. continue;
  1791. if (!IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr))
  1792. continue;
  1793. }
  1794. if (ipversion == 4)
  1795. len = sizeof(struct sockaddr_in);
  1796. else
  1797. len = sizeof(struct sockaddr_in6);
  1798. memcpy(ss, ifa->ifa_addr, len);
  1799. /* fill the server port */
  1800. if (ipversion == 4)
  1801. ((struct sockaddr_in *) ss)->sin_port = htons(67);
  1802. else
  1803. ((struct sockaddr_in6 *) ss)->sin6_port = htons(546);
  1804. return;
  1805. }
  1806. fprintf(stderr, "can't find interface %s\n", name);
  1807. exit(1);
  1808. }
  1809. /*
  1810. * get the local socket address from the command line
  1811. * (if it doesn't work, try an interface name)
  1812. */
  1813. void
  1814. getlocaladdr(void)
  1815. {
  1816. struct addrinfo hints, *res;
  1817. char *service;
  1818. int ret;
  1819. memset(&hints, 0, sizeof(hints));
  1820. if (ipversion == 4) {
  1821. hints.ai_family = AF_INET;
  1822. service = "67";
  1823. } else {
  1824. hints.ai_family = AF_INET6;
  1825. service = "546";
  1826. }
  1827. hints.ai_socktype = SOCK_DGRAM;
  1828. hints.ai_flags = AI_NUMERICSERV;
  1829. #if defined(AI_ADDRCONFIG)
  1830. hints.ai_flags |= AI_ADDRCONFIG;
  1831. #endif
  1832. hints.ai_protocol = IPPROTO_UDP;
  1833. ret = getaddrinfo(localname, service, &hints, &res);
  1834. if ((ret == EAI_NONAME)
  1835. #ifdef EAI_NODATA
  1836. || (ret == EAI_NODATA)
  1837. #endif
  1838. ) {
  1839. isinterface = 1;
  1840. getinterface(localname, &localaddr);
  1841. return;
  1842. }
  1843. if (ret != 0) {
  1844. fprintf(stderr,
  1845. "bad -l<local-addr=%s>: %s\n",
  1846. localname,
  1847. gai_strerror(ret));
  1848. exit(2);
  1849. }
  1850. /* refuse multiple addresses */
  1851. if (res->ai_next != NULL) {
  1852. fprintf(stderr,
  1853. "ambiguous -l<local-addr=%s>\n",
  1854. localname);
  1855. exit(2);
  1856. }
  1857. memcpy(&localaddr, res->ai_addr, res->ai_addrlen);
  1858. freeaddrinfo(res);
  1859. return;
  1860. }
  1861. /*
  1862. * get the local socket address from the server one
  1863. */
  1864. void
  1865. getlocal(void)
  1866. {
  1867. int ret, s;
  1868. socklen_t l;
  1869. if (ipversion == 4) {
  1870. l = sizeof(struct sockaddr_in);
  1871. s = socket(PF_INET, SOCK_DGRAM, 0);
  1872. } else {
  1873. l = sizeof(struct sockaddr_in6);
  1874. s = socket(PF_INET6, SOCK_DGRAM, 0);
  1875. }
  1876. if (s < 0) {
  1877. perror("socket");
  1878. exit(1);
  1879. }
  1880. if ((ipversion == 4) && (isbroadcast != 0)) {
  1881. int on = 1;
  1882. ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
  1883. if (ret < 0) {
  1884. perror("setsockopt(SO_BROADCAST)");
  1885. exit(1);
  1886. }
  1887. }
  1888. ret = connect(s, (struct sockaddr *) &serveraddr, l);
  1889. if (ret < 0) {
  1890. perror("connect");
  1891. exit(1);
  1892. }
  1893. l = sizeof(localaddr);
  1894. ret = getsockname(s, (struct sockaddr *) &localaddr, &l);
  1895. if (ret < 0) {
  1896. perror("getsockname");
  1897. exit(1);
  1898. }
  1899. (void) close(s);
  1900. /* fill the local port */
  1901. if (ipversion == 4)
  1902. ((struct sockaddr_in *) &localaddr)->sin_port = htons(67);
  1903. else
  1904. ((struct sockaddr_in6 *) &localaddr)->sin6_port = htons(546);
  1905. }
  1906. /*
  1907. * intermediate reporting
  1908. * (note: an in-transit packet can be reported as dropped)
  1909. */
  1910. void
  1911. reporting(void)
  1912. {
  1913. dreport.tv_sec += report;
  1914. if (xscount2 == 0) {
  1915. printf("sent: %llu, received: %llu (drops: %lld)",
  1916. (unsigned long long) xscount0,
  1917. (unsigned long long) xrcount0,
  1918. (long long) (xscount0 - xrcount0));
  1919. if (!ISC_TAILQ_EMPTY(&xrcvd0)) {
  1920. double avg;
  1921. avg = dsum0 / xrcount0;
  1922. printf(" average: %.3f ms", avg * 1e3);
  1923. }
  1924. } else {
  1925. printf("sent: %llu/%llu received: %llu/%llu "
  1926. "(drops: %lld/%lld)",
  1927. (unsigned long long) xscount0,
  1928. (unsigned long long) xscount2,
  1929. (unsigned long long) xrcount0,
  1930. (unsigned long long) xrcount2,
  1931. (long long) (xscount0 - xrcount0),
  1932. (long long) (xscount2 - xrcount2));
  1933. if (!ISC_TAILQ_EMPTY(&xrcvd0)) {
  1934. double avg0, avg2;
  1935. avg0 = dsum0 / xrcount0;
  1936. if (xrcount2 != 0)
  1937. avg2 = dsum2 / xrcount2;
  1938. else
  1939. avg2 = 0.;
  1940. printf(" average: %.3f/%.3f ms",
  1941. avg0 * 1e3, avg2 * 1e3);
  1942. }
  1943. }
  1944. printf("\n");
  1945. }
  1946. /*
  1947. * SIGCHLD handler
  1948. */
  1949. void
  1950. reapchild(int sig)
  1951. {
  1952. int status;
  1953. sig = sig;
  1954. while (wait3(&status, WNOHANG, NULL) > 0)
  1955. /* continue */;
  1956. }
  1957. /*
  1958. * SIGINT handler
  1959. */
  1960. void
  1961. interrupt(int sig)
  1962. {
  1963. sig = sig;
  1964. interrupted = 1;
  1965. }
  1966. /*
  1967. * '-v' handler
  1968. */
  1969. void
  1970. version(void)
  1971. {
  1972. fprintf(stderr, "version 0.01\n");
  1973. }
  1974. /*
  1975. * usage (from the wiki)
  1976. */
  1977. void
  1978. usage(void)
  1979. {
  1980. fprintf(stderr, "%s",
  1981. "perfdhcp [-hv] [-4|-6] [-r<rate>] [-t<report>] [-R<range>] [-b<base>]\n"
  1982. " [-n<num-request>] [-p<test-period>] [-d<drop-time>] [-D<max-drop>]\n"
  1983. " [-l<local-addr|interface>] [-P<preload>] [-a<aggressivity>]\n"
  1984. " [-L<local-port>] [-s<seed>] [-i] [-B] [-c] [-1]\n"
  1985. " [-T<template-file>] [-X<xid-offset>] [-O<random-offset]\n"
  1986. " [-E<time-offset>] [-S<srvid-offset>] [-I<ip-offset>]\n"
  1987. " [-x<diagnostic-selector>] [-w<wrapped>] [server]\n"
  1988. "\f\n"
  1989. "The [server] argument is the name/address of the DHCP server to\n"
  1990. "contact. For DHCPv4 operation, exchanges are initiated by\n"
  1991. "transmitting a DHCP DISCOVER to this address.\n"
  1992. "\n"
  1993. "For DHCPv6 operation, exchanges are initiated by transmitting a DHCP\n"
  1994. "SOLICIT to this address. In the DHCPv6 case, the special name 'all'\n"
  1995. "can be used to refer to All_DHCP_Relay_Agents_and_Servers (the\n"
  1996. "multicast address FF02::1:2), or the special name 'servers' to refer\n"
  1997. "to All_DHCP_Servers (the multicast address FF05::1:3). The [server]\n"
  1998. "argument is optional only in the case that -l is used to specify an\n"
  1999. "interface, in which case [server] defaults to 'all'.\n"
  2000. "\n"
  2001. "The default is to perform a single 4-way exchange, effectively pinging\n"
  2002. "the server.\n"
  2003. "The -r option is used to set up a performance test, without\n"
  2004. "it exchanges are initiated as fast as possible.\n"
  2005. "\n"
  2006. "Options:\n"
  2007. "-1: Take the server-ID option from the first received message.\n"
  2008. "-4: DHCPv4 operation (default). This is incompatible with the -6 option.\n"
  2009. "-6: DHCPv6 operation. This is incompatible with the -4 option.\n"
  2010. "-a<aggressivity>: When the target sending rate is not yet reached,\n"
  2011. " control how many exchanges are initiated before the next pause.\n"
  2012. "-b<base>: The base MAC, DUID, IP, etc, used to simulate different\n"
  2013. " clients. This can be specified multiple times, each instance is\n"
  2014. " in the <type>=<value> form, for instance:\n"
  2015. " (and default) MAC=00:0c:01:02:03:04.\n"
  2016. "-d<drop-time>: Specify the time after which a request is treated as\n"
  2017. " having been lost. The value is given in seconds and may contain a\n"
  2018. " fractional component. The default is 1 second.\n"
  2019. "-E<time-offset>: Offset of the (DHCPv4) secs field / (DHCPv6)\n"
  2020. " elapsed-time option in the (second/request) template.\n"
  2021. " The value 0 disables it.\n"
  2022. "-h: Print this help.\n"
  2023. "-i: Do only the initial part of an exchange: DO or SA, depending on\n"
  2024. " whether -6 is given.\n"
  2025. "-I<ip-offset>: Offset of the (DHCPv4) IP address in the requested-IP\n"
  2026. " option / (DHCPv6) IA_NA option in the (second/request) template.\n"
  2027. "-l<local-addr|interface>: For DHCPv4 operation, specify the local\n"
  2028. " hostname/address to use when communicating with the server. By\n"
  2029. " default, the interface address through which traffic would\n"
  2030. " normally be routed to the server is used.\n"
  2031. " For DHCPv6 operation, specify the name of the network interface\n"
  2032. " via which exchanges are initiated.\n"
  2033. "-L<local-port>: Specify the local port to use\n"
  2034. " (the value 0 means to use the default).\n"
  2035. "-O<random-offset>: Offset of the last octet to randomize in the template.\n"
  2036. "-P<preload>: Initiate first <preload> exchanges back to back at startup.\n"
  2037. "-r<rate>: Initiate <rate> DORA/SARR (or if -i is given, DO/SA)\n"
  2038. " exchanges per second. A periodic report is generated showing the\n"
  2039. " number of exchanges which were not completed, as well as the\n"
  2040. " average response latency. The program continues until\n"
  2041. " interrupted, at which point a final report is generated.\n"
  2042. "-R<range>: Specify how many different clients are used. With 1\n"
  2043. " (the default), all requests seem to come from the same client.\n"
  2044. "-s<seed>: Specify the seed for randomization, making it repeatable.\n"
  2045. "-S<srvid-offset>: Offset of the server-ID option in the\n"
  2046. " (second/request) template.\n"
  2047. "-T<template-file>: The name of a file containing the template to use\n"
  2048. " as a stream of hexadecimal digits.\n"
  2049. "-v: Report the version number of this program.\n"
  2050. "-w<wrapped>: Command to call with start/stop at the beginning/end of\n"
  2051. " the program.\n"
  2052. "-x<diagnostic-selector>: Include extended diagnostics in the output.\n"
  2053. " <diagnostic-selector> is a string of single-keywords specifying\n"
  2054. " the operations for which verbose output is desired. The selector\n"
  2055. " keyletters are:\n"
  2056. " * 'a': print the decoded command line arguments\n"
  2057. " * 'e': print the exit reason\n"
  2058. " * 'i': print rate processing details\n"
  2059. " * 'r': print randomization details\n"
  2060. " * 's': print first server-id\n"
  2061. " * 't': when finished, print timers of all successful exchanges\n"
  2062. " * 'T': when finished, print templates\n"
  2063. "-X<xid-offset>: Transaction ID (aka. xid) offset in the template.\n"
  2064. "\n"
  2065. "DHCPv4 only options:\n"
  2066. "-B: Force broadcast handling.\n"
  2067. "\n"
  2068. "DHCPv6 only options:\n"
  2069. "-c: Add a rapid commit option (exchanges will be SA).\n"
  2070. "\n"
  2071. "The remaining options are used only in conjunction with -r:\n"
  2072. "\n"
  2073. "-D<max-drop>: Abort the test if more than <max-drop> requests have\n"
  2074. " been dropped. Use -D0 to abort if even a single request has been\n"
  2075. " dropped. If <max-drop> includes the suffix '%', it specifies a\n"
  2076. " maximum percentage of requests that may be dropped before abort.\n"
  2077. " In this case, testing of the threshold begins after 10 requests\n"
  2078. " have been expected to be received.\n"
  2079. "-n<num-request>: Initiate <num-request> transactions. No report is\n"
  2080. " generated until all transactions have been initiated/waited-for,\n"
  2081. " after which a report is generated and the program terminates.\n"
  2082. "-p<test-period>: Send requests for the given test period, which is\n"
  2083. " specified in the same manner as -d. This can be used as an\n"
  2084. " alternative to -n, or both options can be given, in which case the\n"
  2085. " testing is completed when either limit is reached.\n"
  2086. "-t<report>: Delay in seconds between two periodic reports.\n"
  2087. "\n"
  2088. "Errors:\n"
  2089. "- tooshort: received a too short message\n"
  2090. "- orphans: received a message which doesn't match an exchange\n"
  2091. " (duplicate, late or not related)\n"
  2092. "- locallimit: reached to local system limits when sending a message.\n"
  2093. "\n"
  2094. "Exit status:\n"
  2095. "The exit status is:\n"
  2096. "0 on complete success.\n"
  2097. "1 for a general error.\n"
  2098. "2 if an error is found in the command line arguments.\n"
  2099. "3 if there are no general failures in operation, but one or more\n"
  2100. " exchanges are not successfully completed.\n");
  2101. }
  2102. /*
  2103. * main function / entry point
  2104. */
  2105. int
  2106. main(const int argc, char * const argv[])
  2107. {
  2108. int opt, flags = 0, ret, i;
  2109. long long r;
  2110. char *pc;
  2111. extern char *optarg;
  2112. extern int optind;
  2113. #define OPTIONS "hv46r:t:R:b:n:p:d:D:l:P:a:L:s:iBc1T:X:O:E:S:I:x:w:"
  2114. /* decode options */
  2115. while ((opt = getopt(argc, argv, OPTIONS)) != -1)
  2116. switch (opt) {
  2117. case 'h':
  2118. usage();
  2119. exit(0);
  2120. case 'v':
  2121. version();
  2122. exit(0);
  2123. case '4':
  2124. if (ipversion == 6) {
  2125. fprintf(stderr, "IP version already set to 6\n");
  2126. usage();
  2127. exit(2);
  2128. }
  2129. ipversion = 4;
  2130. break;
  2131. case '6':
  2132. if (ipversion == 4) {
  2133. fprintf(stderr, "IP version already set to 4\n");
  2134. usage();
  2135. exit(2);
  2136. }
  2137. ipversion = 6;
  2138. break;
  2139. case 'r':
  2140. rate = atoi(optarg);
  2141. if (rate <= 0) {
  2142. fprintf(stderr, "rate must be a positive integer\n");
  2143. usage();
  2144. exit(2);
  2145. }
  2146. break;
  2147. case 't':
  2148. report = atoi(optarg);
  2149. if (report <= 0) {
  2150. fprintf(stderr, "report must be a positive integer\n");
  2151. usage();
  2152. exit(2);
  2153. }
  2154. break;
  2155. case 'R':
  2156. r = atoll(optarg);
  2157. if (r < 0) {
  2158. fprintf(stderr,
  2159. "range must not be a negative integer\n");
  2160. usage();
  2161. exit(2);
  2162. }
  2163. range = (uint32_t) r;
  2164. if ((range != 0) && (range != UINT32_MAX)) {
  2165. uint32_t s = range + 1;
  2166. uint64_t b = UINT32_MAX + 1, m;
  2167. m = (b / s) * s;
  2168. if (m == b)
  2169. maxrandom = 0;
  2170. else
  2171. maxrandom = (uint32_t) m;
  2172. }
  2173. break;
  2174. case 'b':
  2175. if (basecnt > 3) {
  2176. fprintf(stderr, "too many bases\n");
  2177. usage();
  2178. exit(2);
  2179. }
  2180. base[basecnt] = optarg;
  2181. decodebase();
  2182. basecnt++;
  2183. break;
  2184. case 'n':
  2185. numreq[gotnumreq] = atoi(optarg);
  2186. if (numreq[gotnumreq] <= 0) {
  2187. fprintf(stderr,
  2188. "num-request must be a positive integer\n");
  2189. usage();
  2190. exit(2);
  2191. }
  2192. gotnumreq = 1;
  2193. break;
  2194. case 'p':
  2195. period = atoi(optarg);
  2196. if (period <= 0) {
  2197. fprintf(stderr,
  2198. "test-period must be a positive integer\n");
  2199. usage();
  2200. exit(2);
  2201. }
  2202. break;
  2203. case 'd':
  2204. losttime[gotlosttime] = atof(optarg);
  2205. if (losttime[gotlosttime] <= 0.) {
  2206. fprintf(stderr,
  2207. "drop-time must be a positive number\n");
  2208. usage();
  2209. exit(2);
  2210. }
  2211. gotlosttime = 1;
  2212. break;
  2213. case 'D':
  2214. pc = strchr(optarg, '%');
  2215. if (pc != NULL) {
  2216. *pc = '\0';
  2217. maxpdrop[gotmaxdrop] = atof(optarg);
  2218. if ((maxpdrop[gotmaxdrop] <= 0) ||
  2219. (maxpdrop[gotmaxdrop] >= 100)) {
  2220. fprintf(stderr,
  2221. "invalid drop-time percentage\n");
  2222. usage();
  2223. exit(2);
  2224. }
  2225. gotmaxdrop = 1;
  2226. break;
  2227. }
  2228. maxdrop[gotmaxdrop] = atoi(optarg);
  2229. if (maxdrop[gotmaxdrop] <= 0) {
  2230. fprintf(stderr,
  2231. "max-drop must be a positive integer\n");
  2232. usage();
  2233. exit(2);
  2234. }
  2235. gotmaxdrop = 1;
  2236. break;
  2237. case 'l':
  2238. localname = optarg;
  2239. break;
  2240. case 'P':
  2241. preload = atoi(optarg);
  2242. if (preload < 0) {
  2243. fprintf(stderr,
  2244. "preload must not be a negative integer\n");
  2245. usage();
  2246. exit(2);
  2247. }
  2248. break;
  2249. case 'a':
  2250. aggressivity = atoi(optarg);
  2251. if (aggressivity <= 0) {
  2252. fprintf(stderr,
  2253. "aggressivity must be a positive integer\n");
  2254. usage();
  2255. exit(2);
  2256. }
  2257. break;
  2258. case 'L':
  2259. localport = atoi(optarg);
  2260. if (localport < 0) {
  2261. fprintf(stderr,
  2262. "local-port must not be a negative integer\n");
  2263. usage();
  2264. exit(2);
  2265. }
  2266. if (localport > (int) UINT16_MAX) {
  2267. fprintf(stderr,
  2268. "local-port must be lower than %d\n",
  2269. (int) UINT16_MAX);
  2270. usage();
  2271. exit(2);
  2272. }
  2273. break;
  2274. case 's':
  2275. seeded = 1;
  2276. seed = (unsigned int) atol(optarg);
  2277. break;
  2278. case 'i':
  2279. simple = 1;
  2280. break;
  2281. case 'B':
  2282. isbroadcast = 1;
  2283. break;
  2284. case 'c':
  2285. rapidcommit = 1;
  2286. break;
  2287. case '1':
  2288. usefirst = 1;
  2289. break;
  2290. case 'T':
  2291. if (templatefile[0] != NULL) {
  2292. if (templatefile[1] != NULL) {
  2293. fprintf(stderr,
  2294. "template-files are already set\n");
  2295. usage();
  2296. exit(2);
  2297. }
  2298. templatefile[1] = optarg;
  2299. } else
  2300. templatefile[0] = optarg;
  2301. break;
  2302. case 'X':
  2303. if (xidoffset[0] >= 0)
  2304. i = 1;
  2305. else
  2306. i = 0;
  2307. xidoffset[i] = atoi(optarg);
  2308. if (xidoffset[i] <= 0) {
  2309. fprintf(stderr,
  2310. "xid-offset must be a positive integer\n");
  2311. usage();
  2312. exit(2);
  2313. }
  2314. break;
  2315. case 'O':
  2316. if (rndoffset[0] >= 0)
  2317. i = 1;
  2318. else
  2319. i = 0;
  2320. rndoffset[i] = atoi(optarg);
  2321. if (rndoffset[i] < 3) {
  2322. fprintf(stderr,
  2323. "random-offset must be greater than 3\n");
  2324. usage();
  2325. exit(2);
  2326. }
  2327. break;
  2328. case 'E':
  2329. elpoffset = atoi(optarg);
  2330. if (elpoffset < 0) {
  2331. fprintf(stderr,
  2332. "time-offset must not be a "
  2333. "negative integer\n");
  2334. usage();
  2335. exit(2);
  2336. }
  2337. break;
  2338. case 'S':
  2339. sidoffset = atoi(optarg);
  2340. if (sidoffset <= 0) {
  2341. fprintf(stderr,
  2342. "srvid-offset must be a positive integer\n");
  2343. usage();
  2344. exit(2);
  2345. }
  2346. break;
  2347. case 'I':
  2348. ripoffset = atoi(optarg);
  2349. if (ripoffset <= 0) {
  2350. fprintf(stderr,
  2351. "ip-offset must be a positive integer\n");
  2352. usage();
  2353. exit(2);
  2354. }
  2355. break;
  2356. case 'x':
  2357. diags = optarg;
  2358. break;
  2359. case 'w':
  2360. wrapped = optarg;
  2361. break;
  2362. default:
  2363. usage();
  2364. exit(2);
  2365. }
  2366. /* adjust some global variables */
  2367. if (ipversion == 0)
  2368. ipversion = 4;
  2369. if (templatefile[1] != NULL) {
  2370. if (xidoffset[1] < 0)
  2371. xidoffset[1] = xidoffset[0];
  2372. if (rndoffset[1] < 0)
  2373. rndoffset[1] = rndoffset[0];
  2374. }
  2375. /* when required, print the internal view of the command line */
  2376. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2377. printf("IPv%d", ipversion);
  2378. if (simple != 0) {
  2379. if (ipversion == 4)
  2380. printf(" DO only");
  2381. else
  2382. printf(" SA only");
  2383. }
  2384. if (rate != 0)
  2385. printf(" rate=%d", rate);
  2386. if (report != 0)
  2387. printf(" report=%d", report);
  2388. if (range != 0) {
  2389. if (strchr(diags, 'r') != NULL)
  2390. printf(" range=0..%d [0x%x]",
  2391. range,
  2392. (unsigned int) maxrandom);
  2393. else
  2394. printf(" range=0..%d", range);
  2395. }
  2396. if (basecnt != 0)
  2397. for (i = 0; i < basecnt; i++)
  2398. printf(" base[%d]='%s'", i, base[i]);
  2399. if (gotnumreq != 0)
  2400. printf(" num-request=%d,%d", numreq[0], numreq[1]);
  2401. if (period != 0)
  2402. printf(" test-period=%d", period);
  2403. printf(" drop-time=%g,%g", losttime[0], losttime[1]);
  2404. if ((maxdrop[0] != 0) || (maxdrop[1] != 0))
  2405. printf(" max-drop=%d,%d", maxdrop[0], maxdrop[1]);
  2406. if ((maxpdrop[0] != 0.) || (maxpdrop[1] != 0.))
  2407. printf(" max-drop=%2.2f%%,%2.2f%%",
  2408. maxpdrop[0], maxpdrop[1]);
  2409. if (preload != 0)
  2410. printf(" preload=%d", preload);
  2411. printf(" aggressivity=%d", aggressivity);
  2412. if (localport != 0)
  2413. printf(" local-port=%d", localport);
  2414. if (seeded)
  2415. printf(" seed=%u", seed);
  2416. if (isbroadcast != 0)
  2417. printf(" broadcast");
  2418. if (rapidcommit != 0)
  2419. printf(" rapid-commit");
  2420. if (usefirst != 0)
  2421. printf(" use-first");
  2422. if ((templatefile[0] != NULL) && (templatefile[1] == NULL))
  2423. printf(" template-file='%s'", templatefile[0]);
  2424. else if (templatefile[1] != NULL)
  2425. printf(" template-file='%s','%s'",
  2426. templatefile[0], templatefile[1]);
  2427. if ((xidoffset[0] >= 0) && (xidoffset[1] < 0))
  2428. printf(" xid-offset=%d", xidoffset[0]);
  2429. else if (xidoffset[1] >= 0)
  2430. printf(" xid-offset=%d,%d",
  2431. xidoffset[0], xidoffset[1]);
  2432. if ((rndoffset[0] >= 0) && (rndoffset[1] < 0))
  2433. printf(" xid-offset=%d", rndoffset[0]);
  2434. else if (rndoffset[1] >= 0)
  2435. printf(" xid-offset=%d,%d",
  2436. rndoffset[0], rndoffset[1]);
  2437. if (elpoffset >= 0)
  2438. printf(" time-offset=%d", elpoffset);
  2439. if (sidoffset >= 0)
  2440. printf(" srvid-offset=%d", sidoffset);
  2441. if (ripoffset >= 0)
  2442. printf(" ip-offset=%d", ripoffset);
  2443. printf(" diagnotic-selectors='%s'", diags);
  2444. if (wrapped != NULL)
  2445. printf(" wrapped='%s'", wrapped);
  2446. printf("\n");
  2447. }
  2448. /* check DHCPv4 only options */
  2449. if ((ipversion != 4) && (isbroadcast != 0)) {
  2450. fprintf(stderr, "-b is not compatible with IPv6 (-6)\n");
  2451. usage();
  2452. exit(2);
  2453. }
  2454. /* check DHCPv6 only options */
  2455. if ((ipversion != 6) && (rapidcommit != 0)) {
  2456. fprintf(stderr, "-6 (IPv6) must be set to use -c\n");
  2457. usage();
  2458. exit(2);
  2459. }
  2460. /* check 4-packet (aka not simple) mode options */
  2461. if ((simple != 0) && (numreq[1] != 0)) {
  2462. fprintf(stderr,
  2463. "second -n<num-request> is not compatible with -i\n");
  2464. usage();
  2465. exit(2);
  2466. }
  2467. if ((simple != 0) && (losttime[1] != 1.)) {
  2468. fprintf(stderr,
  2469. "second -d<drop-time> is not compatible with -i\n");
  2470. usage();
  2471. exit(2);
  2472. }
  2473. if ((simple != 0) &&
  2474. ((maxdrop[1] != 0) || (maxpdrop[1] != 0.))) {
  2475. fprintf(stderr,
  2476. "second -D<max-drop> is not compatible with -i\n");
  2477. usage();
  2478. exit(2);
  2479. }
  2480. if ((simple != 0) && (usefirst != 0)) {
  2481. fprintf(stderr,
  2482. "-1 is not compatible with -i\n");
  2483. usage();
  2484. exit(2);
  2485. }
  2486. if ((simple != 0) && (templatefile[1] != NULL)) {
  2487. fprintf(stderr,
  2488. "second -T<template-file> is not "
  2489. "compatible with -i\n");
  2490. usage();
  2491. exit(2);
  2492. }
  2493. if ((simple != 0) && (xidoffset[1] >= 0)) {
  2494. fprintf(stderr,
  2495. "second -X<xid-offset> is not compatible with -i\n");
  2496. usage();
  2497. exit(2);
  2498. }
  2499. if ((simple != 0) && (rndoffset[1] >= 0)) {
  2500. fprintf(stderr,
  2501. "second -O<random-offset is not compatible with -i\n");
  2502. usage();
  2503. exit(2);
  2504. }
  2505. if ((simple != 0) && (elpoffset >= 0)) {
  2506. fprintf(stderr,
  2507. "-E<time-offset> is not compatible with -i\n");
  2508. usage();
  2509. exit(2);
  2510. }
  2511. if ((simple != 0) && (sidoffset >= 0)) {
  2512. fprintf(stderr,
  2513. "-S<srvid-offset> is not compatible with -i\n");
  2514. usage();
  2515. exit(2);
  2516. }
  2517. if ((simple != 0) && (ripoffset >= 0)) {
  2518. fprintf(stderr,
  2519. "-I<ip-offset> is not compatible with -i\n");
  2520. usage();
  2521. exit(2);
  2522. }
  2523. /* check simple mode options */
  2524. if ((simple == 0) && (rapidcommit != 0)) {
  2525. fprintf(stderr, "-i must be set to use -c\n");
  2526. usage();
  2527. exit(2);
  2528. }
  2529. /* check rate '-r' options */
  2530. if ((rate == 0) && (report != 0)) {
  2531. fprintf(stderr,
  2532. "-r<rate> must be set to use -t<report>\n");
  2533. usage();
  2534. exit(2);
  2535. }
  2536. if ((rate == 0) && ((numreq[0] != 0) || (numreq[1] != 0))) {
  2537. fprintf(stderr,
  2538. "-r<rate> must be set to use -n<num-request>\n");
  2539. usage();
  2540. exit(2);
  2541. }
  2542. if ((rate == 0) && (period != 0)) {
  2543. fprintf(stderr,
  2544. "-r<rate> must be set to use -p<test-period>\n");
  2545. usage();
  2546. exit(2);
  2547. }
  2548. if ((rate == 0) &&
  2549. ((maxdrop[0] != 0) || (maxdrop[1] != 0) ||
  2550. (maxpdrop[0] != 0.) || (maxpdrop[1] != 0.))) {
  2551. fprintf(stderr,
  2552. "-r<rate> must be set to use -D<max-drop>\n");
  2553. usage();
  2554. exit(2);
  2555. }
  2556. /* check (first) template file options */
  2557. if ((templatefile[0] == NULL) && (xidoffset[0] >= 0)) {
  2558. fprintf(stderr,
  2559. "-T<template-file> must be set to "
  2560. "use -X<xid-offset>\n");
  2561. usage();
  2562. exit(2);
  2563. }
  2564. if ((templatefile[0] == NULL) && (rndoffset[0] >= 0)) {
  2565. fprintf(stderr,
  2566. "-T<template-file> must be set to "
  2567. "use -O<random-offset>\n");
  2568. usage();
  2569. exit(2);
  2570. }
  2571. /* check (second) template file options */
  2572. if ((templatefile[1] == NULL) && (elpoffset >= 0)) {
  2573. fprintf(stderr,
  2574. "second/request -T<template-file> must be set to "
  2575. "use -E<time-offset>\n");
  2576. usage();
  2577. exit(2);
  2578. }
  2579. if ((templatefile[1] == NULL) && (sidoffset >= 0)) {
  2580. fprintf(stderr,
  2581. "second/request -T<template-file> must be set to "
  2582. "use -S<srvid-offset>\n");
  2583. usage();
  2584. exit(2);
  2585. }
  2586. if ((templatefile[1] == NULL) && (ripoffset >= 0)) {
  2587. fprintf(stderr,
  2588. "second/request -T<template-file> must be set to "
  2589. "use -I<ip-offset>\n");
  2590. usage();
  2591. exit(2);
  2592. }
  2593. /* check various template file(s) and other condition(s) options */
  2594. if ((templatefile[0] != NULL) && (range > 0) && (rndoffset[0] < 0)) {
  2595. fprintf(stderr,
  2596. "-O<random-offset> must be set when "
  2597. "-T<template-file> and -R<range> are used\n");
  2598. usage();
  2599. exit(2);
  2600. }
  2601. if ((templatefile[1] != NULL) && (sidoffset < 0)) {
  2602. fprintf(stderr,
  2603. "-S<srvid-offset> must be set when second "
  2604. "-T<template-file> is used\n");
  2605. usage();
  2606. exit(2);
  2607. }
  2608. if ((templatefile[1] != NULL) && (ripoffset < 0)) {
  2609. fprintf(stderr,
  2610. "-I<ip-offset> must be set when second "
  2611. "-T<template-file> is used\n");
  2612. usage();
  2613. exit(2);
  2614. }
  2615. /* get the server argument */
  2616. if (optind < argc - 1) {
  2617. fprintf(stderr, "extra arguments?\n");
  2618. usage();
  2619. exit(2);
  2620. }
  2621. if (optind == argc - 1) {
  2622. servername = argv[optind];
  2623. /* decode special cases */
  2624. if ((ipversion == 4) &&
  2625. (strcmp(servername, "all") == 0)) {
  2626. flags = AI_NUMERICHOST;
  2627. isbroadcast = 1;
  2628. servername = "255.255.255.255";
  2629. } else if ((ipversion == 6) &&
  2630. (strcmp(servername, "all") == 0)) {
  2631. flags = AI_NUMERICHOST;
  2632. servername = "FF02::1:2";
  2633. } else if ((ipversion == 6) &&
  2634. (strcmp(servername, "servers") == 0)) {
  2635. flags = AI_NUMERICHOST;
  2636. servername = "FF05::1:3";
  2637. }
  2638. }
  2639. /* handle the local '-l' address/interface */
  2640. if (localname != NULL) {
  2641. /* given */
  2642. getlocaladdr();
  2643. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2644. if (isinterface)
  2645. printf("interface='%s'\n", localname);
  2646. else
  2647. printf("local-addr='%s'\n", localname);
  2648. }
  2649. /* get the not given server from it */
  2650. if (servername == NULL) {
  2651. if (isinterface && (ipversion == 4)) {
  2652. flags = AI_NUMERICHOST;
  2653. isbroadcast = 1;
  2654. servername = "255.255.255.255";
  2655. } else if (isinterface && (ipversion == 6)) {
  2656. flags = AI_NUMERICHOST;
  2657. servername = "FF02::1:2";
  2658. } else {
  2659. fprintf(stderr,
  2660. "without an interface "
  2661. "server is required\n");
  2662. usage();
  2663. exit(2);
  2664. }
  2665. }
  2666. } else if (servername == NULL) {
  2667. fprintf(stderr, "without an interface server is required\n");
  2668. usage();
  2669. exit(2);
  2670. }
  2671. /* get the server socket address */
  2672. getserveraddr(flags);
  2673. /* finish local/server socket address stuff and print it */
  2674. if ((diags != NULL) && (strchr(diags, 'a') != NULL))
  2675. printf("server='%s'\n", servername);
  2676. if (localname == NULL)
  2677. getlocal();
  2678. if ((diags != NULL) && (strchr(diags, 'a') != NULL)) {
  2679. char addr[NI_MAXHOST];
  2680. ret = getnameinfo((struct sockaddr *) &localaddr,
  2681. sizeof(localaddr),
  2682. addr,
  2683. NI_MAXHOST,
  2684. NULL,
  2685. 0,
  2686. NI_NUMERICHOST);
  2687. if (ret != 0) {
  2688. fprintf(stderr,
  2689. "can't get the local address: %s\n",
  2690. gai_strerror(ret));
  2691. exit(1);
  2692. }
  2693. printf("local address='%s'\n", addr);
  2694. }
  2695. /* initialize exchange structures */
  2696. inits();
  2697. /* get the socket descriptor and template(s) */
  2698. if (ipversion == 4) {
  2699. getsock4();
  2700. if (templatefile[0] == NULL)
  2701. build_template_discover4();
  2702. else
  2703. get_template_discover4();
  2704. if (simple == 0) {
  2705. if (templatefile[1] == NULL)
  2706. build_template_request4();
  2707. else
  2708. get_template_request4();
  2709. }
  2710. } else {
  2711. getsock6();
  2712. if (duid_prefix != NULL) {
  2713. if (templatefile[0] == NULL)
  2714. build_template_solicit6();
  2715. else
  2716. get_template_solicit6();
  2717. if (simple == 0) {
  2718. if (templatefile[1] == NULL)
  2719. build_template_request6();
  2720. else
  2721. get_template_request6();
  2722. }
  2723. }
  2724. }
  2725. /* sanity check */
  2726. if ((unsigned) sock > FD_SETSIZE) {
  2727. fprintf(stderr, "socket descriptor (%d) too large?!\n", sock);
  2728. exit(1);
  2729. }
  2730. /* make the socket descriptor not blocking */
  2731. flags = fcntl(sock, F_GETFL, 0);
  2732. if (flags < 0) {
  2733. perror("fcntl(F_GETFL)");
  2734. exit(1);
  2735. }
  2736. if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
  2737. perror("fcntl(F_SETFL)");
  2738. exit(1);
  2739. }
  2740. /* wrapped start */
  2741. if (wrapped != NULL) {
  2742. pid_t pid;
  2743. (void) signal(SIGCHLD, reapchild);
  2744. pid = fork();
  2745. if (pid < 0) {
  2746. perror("fork");
  2747. exit(1);
  2748. } else if (pid == 0)
  2749. (void) execlp(wrapped, "start", (char *) NULL);
  2750. }
  2751. /* boot is done! */
  2752. if (clock_gettime(CLOCK_REALTIME, &boot) < 0) {
  2753. perror("clock_gettime(boot)");
  2754. exit(1);
  2755. }
  2756. /* compute the next intermediate reporting date */
  2757. if (report != 0) {
  2758. dreport.tv_sec = boot.tv_sec + report;
  2759. dreport.tv_nsec = boot.tv_nsec;
  2760. }
  2761. /* compute the DUID (the current date is needed) */
  2762. if ((ipversion == 6) && (duid_prefix == NULL)) {
  2763. uint32_t curdate;
  2764. duid_length = 14;
  2765. duid_prefix = (uint8_t *) malloc(duid_length);
  2766. if (duid_prefix == NULL) {
  2767. perror("malloc(duid)");
  2768. exit(1);
  2769. }
  2770. duid_prefix[0] = DHCP6_DUID_LLT >> 8;
  2771. duid_prefix[1] = DHCP6_DUID_LLT;
  2772. duid_prefix[2] = DHCP_HTYPE_ETHER >> 8;
  2773. duid_prefix[3] = DHCP_HTYPE_ETHER;
  2774. curdate = htonl(boot.tv_sec - DHCP6_DUID_EPOCH);
  2775. memcpy(duid_prefix + 4, &curdate, 4);
  2776. memcpy(duid_prefix + 8, mac_prefix, 6);
  2777. /* the DUID is in template(s) */
  2778. if (templatefile[0] == NULL)
  2779. build_template_solicit6();
  2780. else
  2781. get_template_solicit6();
  2782. if (simple == 0) {
  2783. if (templatefile[1] == NULL)
  2784. build_template_request6();
  2785. else
  2786. get_template_request6();
  2787. }
  2788. }
  2789. /* seed the random generator */
  2790. if (seeded == 0)
  2791. seed = (unsigned int) (boot.tv_sec + boot.tv_nsec);
  2792. srandom(seed);
  2793. /* preload the server with at least one packet */
  2794. compsend = preload + 1;
  2795. for (i = 0; i <= preload; i++) {
  2796. if (ipversion == 4)
  2797. ret = send4();
  2798. else
  2799. ret = send6();
  2800. if (ret < 0) {
  2801. /* failure at the first packet is fatal */
  2802. if (i == 0) {
  2803. fprintf(stderr,
  2804. "initial send failed: %s\n",
  2805. strerror(-ret));
  2806. exit(1);
  2807. }
  2808. if ((errno == EAGAIN) ||
  2809. (errno == EWOULDBLOCK) ||
  2810. (errno == ENOBUFS) ||
  2811. (errno == ENOMEM))
  2812. locallimit++;
  2813. fprintf(stderr, "preload send: %s\n", strerror(-ret));
  2814. break;
  2815. }
  2816. }
  2817. /* required only before the interrupted flag check */
  2818. (void) signal(SIGINT, interrupt);
  2819. /* main loop */
  2820. for (;;) {
  2821. struct timespec now, ts;
  2822. fd_set rfds;
  2823. /* immediate loop exit conditions */
  2824. if (interrupted) {
  2825. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2826. printf("interrupted\n");
  2827. break;
  2828. }
  2829. if (fatal) {
  2830. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2831. printf("got a fatal error\n");
  2832. break;
  2833. }
  2834. /* get the date and use it */
  2835. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  2836. perror("clock_gettime(now)");
  2837. fatal = 1;
  2838. continue;
  2839. }
  2840. if ((period != 0) &&
  2841. ((boot.tv_sec + period < now.tv_sec) ||
  2842. ((boot.tv_sec + period == now.tv_sec) &&
  2843. (boot.tv_nsec < now.tv_nsec)))) {
  2844. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2845. printf("reached test-period\n");
  2846. break;
  2847. }
  2848. if ((report != 0) &&
  2849. ((dreport.tv_sec < now.tv_sec) ||
  2850. ((dreport.tv_sec == now.tv_sec) &&
  2851. (dreport.tv_nsec < now.tv_nsec))))
  2852. reporting();
  2853. /* compute the delay for the next send */
  2854. due = last;
  2855. if (rate == 1)
  2856. due.tv_sec += 1;
  2857. else if (rate != 0)
  2858. due.tv_nsec += 1010000000 / rate;
  2859. else
  2860. due.tv_nsec += 1;
  2861. while (due.tv_nsec >= 1000000000) {
  2862. due.tv_sec += 1;
  2863. due.tv_nsec -= 1000000000;
  2864. }
  2865. ts = due;
  2866. ts.tv_sec -= now.tv_sec;
  2867. ts.tv_nsec -= now.tv_nsec;
  2868. while (ts.tv_nsec < 0) {
  2869. ts.tv_sec -= 1;
  2870. ts.tv_nsec += 1000000000;
  2871. }
  2872. /* the send was already due? */
  2873. if (ts.tv_sec < 0) {
  2874. ts.tv_sec = ts.tv_nsec = 0;
  2875. latesent++;
  2876. }
  2877. /* pselect() */
  2878. FD_ZERO(&rfds);
  2879. FD_SET(sock, &rfds);
  2880. ret = pselect(sock + 1, &rfds, NULL, NULL, &ts, NULL);
  2881. if (ret < 0) {
  2882. if (errno == EINTR)
  2883. continue;
  2884. perror("pselect");
  2885. fatal = 1;
  2886. continue;
  2887. }
  2888. /* packet(s) to receive */
  2889. while (ret == 1) {
  2890. if (ipversion == 4)
  2891. receive4();
  2892. else
  2893. receive6();
  2894. if (recv(sock, ibuf, sizeof(ibuf), MSG_PEEK) <= 0)
  2895. ret = 0;
  2896. else
  2897. multrcvd++;
  2898. }
  2899. if (fatal)
  2900. continue;
  2901. /* check receive loop exit conditions */
  2902. if ((numreq[0] != 0) && ((int) xscount0 >= numreq[0])) {
  2903. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2904. printf("reached num-request0\n");
  2905. break;
  2906. }
  2907. if ((numreq[1] != 0) && ((int) xscount2 >= numreq[1])) {
  2908. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2909. printf("reached num-request2\n");
  2910. break;
  2911. }
  2912. if ((maxdrop[0] != 0) &&
  2913. ((int) (xscount0 - xrcount0) > maxdrop[0])) {
  2914. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2915. printf("reached max-drop%s (absolute)\n",
  2916. simple != 0 ? "" : "0");
  2917. break;
  2918. }
  2919. if ((maxdrop[1] != 0) &&
  2920. ((int) (xscount2 - xrcount2) > maxdrop[1])) {
  2921. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2922. printf("reached max-drop2 (absolute)\n");
  2923. break;
  2924. }
  2925. if ((maxpdrop[0] != 0.) &&
  2926. (xscount0 > 10) &&
  2927. (((100. * (xscount0 - xrcount0)) / xscount0)
  2928. > maxpdrop[0])) {
  2929. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2930. printf("reached max-drop%s (percent)\n",
  2931. simple != 0 ? "" : "0");
  2932. break;
  2933. }
  2934. if ((maxpdrop[1] != 0.) &&
  2935. (xscount2 > 10) &&
  2936. (((100. * (xscount2 - xrcount2)) / xscount2)
  2937. > maxpdrop[1])) {
  2938. if ((diags != NULL) && (strchr(diags, 'e') != NULL))
  2939. printf("reached max-drop2 (percent)\n");
  2940. break;
  2941. }
  2942. /* compute how many packets to send */
  2943. if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
  2944. perror("clock_gettime(now2)");
  2945. fatal = 1;
  2946. continue;
  2947. }
  2948. if ((now.tv_sec > due.tv_sec) ||
  2949. ((now.tv_sec == due.tv_sec) &&
  2950. (now.tv_nsec >= due.tv_nsec))) {
  2951. double tosend;
  2952. if (rate != 0) {
  2953. tosend = (now.tv_nsec - due.tv_nsec) / 1e9;
  2954. tosend += now.tv_sec - due.tv_sec;
  2955. tosend *= rate;
  2956. tosend += 1;
  2957. if (tosend > (double) aggressivity)
  2958. i = aggressivity;
  2959. else
  2960. i = (int) tosend;
  2961. } else
  2962. i = aggressivity;
  2963. compsend += i;
  2964. /* send packets */
  2965. for (;;) {
  2966. if (ipversion == 4)
  2967. ret = send4();
  2968. else
  2969. ret = send6();
  2970. if (ret < 0) {
  2971. if ((errno == EAGAIN) ||
  2972. (errno == EWOULDBLOCK) ||
  2973. (errno == ENOBUFS) ||
  2974. (errno == ENOMEM))
  2975. locallimit++;
  2976. fprintf(stderr,
  2977. "send: %s\n", strerror(-ret));
  2978. break;
  2979. }
  2980. i--;
  2981. if (i == 0)
  2982. break;
  2983. /* check for late packets to receive */
  2984. if (recv(sock, ibuf, sizeof(ibuf),
  2985. MSG_PEEK) > 0) {
  2986. latercvd++;
  2987. if (ipversion == 4)
  2988. receive4();
  2989. else
  2990. receive6();
  2991. }
  2992. }
  2993. } else
  2994. /* there was no packet to send */
  2995. shortwait++;
  2996. }
  2997. /* after main loop: finished */
  2998. if (clock_gettime(CLOCK_REALTIME, &finished) < 0)
  2999. perror("clock_gettime(finished)");
  3000. /* wrapped stop */
  3001. if (wrapped != NULL) {
  3002. pid_t pid;
  3003. pid = fork();
  3004. if (pid == 0)
  3005. (void) execlp(wrapped, "stop", (char *) NULL);
  3006. }
  3007. /* main statictics */
  3008. if (xscount2 == 0)
  3009. printf("sent: %llu, received: %llu (drops: %lld)\n",
  3010. (unsigned long long) xscount0,
  3011. (unsigned long long) xrcount0,
  3012. (long long) (xscount0 - xrcount0));
  3013. else
  3014. printf("sent: %llu/%llu, received: %llu/%llu "
  3015. "(drops: %lld/%lld)\n",
  3016. (unsigned long long) xscount0,
  3017. (unsigned long long) xscount2,
  3018. (unsigned long long) xrcount0,
  3019. (unsigned long long) xrcount2,
  3020. (long long) (xscount0 - xrcount0),
  3021. (long long) (xscount2 - xrcount2));
  3022. printf("tooshort: %llu, orphans: %llu, local limits: %llu\n",
  3023. (unsigned long long) tooshort,
  3024. (unsigned long long) orphans,
  3025. (unsigned long long) locallimit);
  3026. /* print the rate */
  3027. if (finished.tv_sec != 0) {
  3028. double dall, erate;
  3029. dall = (finished.tv_nsec - boot.tv_nsec) / 1e9;
  3030. dall += finished.tv_sec - boot.tv_sec;
  3031. erate = xrcount0 / dall;
  3032. if (rate != 0)
  3033. printf("rate: %f (expected %d)\n", erate, rate);
  3034. else
  3035. printf("rate: %f\n", erate);
  3036. }
  3037. /* rate processing instrumentation */
  3038. if ((diags != NULL) && (strchr(diags, 'i') != NULL)) {
  3039. printf("latesent: %llu, compsend: %llu, shortwait: %llu\n"
  3040. "multrcvd: %llu, latercvd: %llu, collected:%llu/%llu\n",
  3041. (unsigned long long) latesent,
  3042. (unsigned long long) compsend,
  3043. (unsigned long long) shortwait,
  3044. (unsigned long long) multrcvd,
  3045. (unsigned long long) latercvd,
  3046. (unsigned long long) collected[0],
  3047. (unsigned long long) collected[1]);
  3048. }
  3049. /* round-time trip statistics */
  3050. if (xrcount2 != 0) {
  3051. double avg0, avg2, stddev0, stddev2;
  3052. avg0 = dsum0 / xrcount0;
  3053. avg2 = dsum2 / xrcount2;
  3054. stddev0 = sqrt(dsumsq0 / xrcount0 - avg0 * avg0);
  3055. stddev2 = sqrt(dsumsq2 / xrcount2 - avg2 * avg2);
  3056. printf("RTT0: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3057. dmin0 * 1e3, avg0 * 1e3, dmax0 * 1e3, stddev0 * 1e3);
  3058. printf("RTT2: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3059. dmin2 * 1e3, avg2 * 1e3, dmax2 * 1e3, stddev2 * 1e3);
  3060. } else if (xrcount0 != 0) {
  3061. double avg, stddev;
  3062. avg = dsum0 / xrcount0;
  3063. stddev = sqrt(dsumsq0 / xrcount0 - avg * avg);
  3064. printf("RTT%s: min/avg/max/stddev: %.3f/%.3f/%.3f/%.3f ms\n",
  3065. simple != 0 ? "" : "0",
  3066. dmin0 * 1e3, avg * 1e3, dmax0 * 1e3, stddev * 1e3);
  3067. }
  3068. /* (first) server-ID option content */
  3069. if ((diags != NULL) && (strchr(diags, 's') != NULL) &&
  3070. !ISC_TAILQ_EMPTY(&xrcvd0)) {
  3071. struct exchange *x;
  3072. size_t n;
  3073. printf("server-id: ");
  3074. x = ISC_TAILQ_FIRST(&xrcvd0);
  3075. if (ipversion == 4)
  3076. n = 2;
  3077. else
  3078. n = 4;
  3079. for (; n < x->sidlen; n++)
  3080. printf("%02hhx", x->sid[n]);
  3081. printf("\n");
  3082. }
  3083. /* all time-stamps */
  3084. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3085. !ISC_TAILQ_EMPTY(&xrcvd0)) {
  3086. struct exchange *x;
  3087. printf("\n\n");
  3088. ISC_TAILQ_FOREACH(x, &xrcvd0, gchain)
  3089. printf("%ld.%09ld %ld.%09ld\n",
  3090. (long) x->ts0.tv_sec, x->ts0.tv_nsec,
  3091. (long) x->ts1.tv_sec, x->ts1.tv_nsec);
  3092. }
  3093. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3094. !ISC_TAILQ_EMPTY(&xrcvd2)) {
  3095. struct exchange *x;
  3096. printf("--\n");
  3097. ISC_TAILQ_FOREACH(x, &xrcvd2, gchain)
  3098. printf("%ld.%09ld %ld.%09ld %ld.%09ld %ld.%09ld\n",
  3099. (long) x->ts0.tv_sec, x->ts0.tv_nsec,
  3100. (long) x->ts1.tv_sec, x->ts1.tv_nsec,
  3101. (long) x->ts2.tv_sec, x->ts2.tv_nsec,
  3102. (long) x->ts3.tv_sec, x->ts3.tv_nsec);
  3103. }
  3104. if ((diags != NULL) && (strchr(diags, 't') != NULL) &&
  3105. !ISC_TAILQ_EMPTY(&xrcvd0))
  3106. printf("\n\n");
  3107. /* template(s) */
  3108. if ((diags != NULL) && (strchr(diags, 'T') != NULL)) {
  3109. size_t n;
  3110. if (ipversion == 4) {
  3111. printf("length = %zu\n", length_discover4);
  3112. printf("xid offset = %d\n", DHCP_OFF_XID);
  3113. printf("xid length = 4\n");
  3114. printf("random offset = %zu\n", random_discover4);
  3115. printf("content:\n");
  3116. for (n = 0; n < length_discover4; n++) {
  3117. printf("%s%02hhx",
  3118. (n & 15) == 0 ? "" : " ",
  3119. template_discover4[n]);
  3120. if ((n & 15) == 15)
  3121. printf("\n");
  3122. }
  3123. if ((n & 15) != 15)
  3124. printf("\n");
  3125. if (simple != 0)
  3126. goto doneT;
  3127. printf("--\n");
  3128. printf("length = %zu\n", length_request4);
  3129. printf("xid offset = %d\n", DHCP_OFF_XID);
  3130. printf("xid length = 4\n");
  3131. printf("random offset = %zu\n", random_request4);
  3132. if (elapsed_request4 > 0)
  3133. printf("secs offset = %zu\n",
  3134. elapsed_request4);
  3135. printf("server-id offset = %zu\n", serverid_request4);
  3136. printf("server-id length = %d\n", DHCP_OPTLEN_SRVID);
  3137. printf("content:\n");
  3138. printf("requested-ip-address offset = %zu\n",
  3139. reqaddr_request4);
  3140. printf("requested-ip-address length = %d\n", 4);
  3141. for (n = 0; n < length_request4; n++) {
  3142. printf("%s%02hhx",
  3143. (n & 15) == 0 ? "" : " ",
  3144. template_request4[n]);
  3145. if ((n & 15) == 15)
  3146. printf("\n");
  3147. }
  3148. printf("\n");
  3149. } else {
  3150. printf("length = %zu\n", length_solicit6);
  3151. printf("xid offset = %d\n", DHCP6_OFF_XID);
  3152. printf("xid length = 3\n");
  3153. printf("random offset = %zu\n", random_solicit6);
  3154. for (n = 0; n < length_solicit6; n++) {
  3155. printf("%s%02hhx",
  3156. (n & 15) == 0 ? "" : " ",
  3157. template_solicit6[n]);
  3158. if ((n & 15) == 15)
  3159. printf("\n");
  3160. }
  3161. if ((n & 15) != 15)
  3162. printf("\n");
  3163. if (simple != 0)
  3164. goto doneT;
  3165. printf("--\n");
  3166. printf("length = %zu\n", length_request6);
  3167. printf("xid offset = %d\n", DHCP_OFF_XID);
  3168. printf("xid length = 4\n");
  3169. printf("random offset = %zu\n", random_request6);
  3170. if (elapsed_request6 > 0)
  3171. printf("secs offset = %zu\n",
  3172. elapsed_request6);
  3173. printf("server-id offset = %zu\n", serverid_request6);
  3174. printf("content:\n");
  3175. printf("requested-ip-address offset = %zu\n",
  3176. reqaddr_request6);
  3177. for (n = 0; n < length_request6; n++) {
  3178. printf("%s%02hhx",
  3179. (n & 15) == 0 ? "" : " ",
  3180. template_request6[n]);
  3181. if ((n & 15) == 15)
  3182. printf("\n");
  3183. }
  3184. printf("\n");
  3185. }
  3186. }
  3187. doneT:
  3188. /* compute the exit code (and exit) */
  3189. if (fatal)
  3190. exit(1);
  3191. else if ((xscount0 == xrcount0) && (xscount2 == xrcount2))
  3192. exit(0);
  3193. else
  3194. exit(3);
  3195. }