perfdhcp.c 85 KB

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