sctp_asconf.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * a) Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * b) Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the distribution.
  17. *
  18. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #if defined(__FreeBSD__) && !defined(__Userspace__)
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #endif
  38. #include <netinet/sctp_os.h>
  39. #include <netinet/sctp_var.h>
  40. #include <netinet/sctp_sysctl.h>
  41. #include <netinet/sctp_pcb.h>
  42. #include <netinet/sctp_header.h>
  43. #include <netinet/sctputil.h>
  44. #include <netinet/sctp_output.h>
  45. #include <netinet/sctp_asconf.h>
  46. #include <netinet/sctp_timer.h>
  47. /*
  48. * debug flags:
  49. * SCTP_DEBUG_ASCONF1: protocol info, general info and errors
  50. * SCTP_DEBUG_ASCONF2: detailed info
  51. */
  52. /*
  53. * RFC 5061
  54. *
  55. * An ASCONF parameter queue exists per asoc which holds the pending address
  56. * operations. Lists are updated upon receipt of ASCONF-ACK.
  57. *
  58. * A restricted_addrs list exists per assoc to hold local addresses that are
  59. * not (yet) usable by the assoc as a source address. These addresses are
  60. * either pending an ASCONF operation (and exist on the ASCONF parameter
  61. * queue), or they are permanently restricted (the peer has returned an
  62. * ERROR indication to an ASCONF(ADD), or the peer does not support ASCONF).
  63. *
  64. * Deleted addresses are always immediately removed from the lists as they will
  65. * (shortly) no longer exist in the kernel. We send ASCONFs as a courtesy,
  66. * only if allowed.
  67. */
  68. /*
  69. * ASCONF parameter processing.
  70. * response_required: set if a reply is required (eg. SUCCESS_REPORT).
  71. * returns a mbuf to an "error" response parameter or NULL/"success" if ok.
  72. * FIX: allocating this many mbufs on the fly is pretty inefficient...
  73. */
  74. static struct mbuf *
  75. sctp_asconf_success_response(uint32_t id)
  76. {
  77. struct mbuf *m_reply = NULL;
  78. struct sctp_asconf_paramhdr *aph;
  79. m_reply = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_paramhdr),
  80. 0, M_NOWAIT, 1, MT_DATA);
  81. if (m_reply == NULL) {
  82. SCTPDBG(SCTP_DEBUG_ASCONF1,
  83. "asconf_success_response: couldn't get mbuf!\n");
  84. return (NULL);
  85. }
  86. aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
  87. aph->correlation_id = id;
  88. aph->ph.param_type = htons(SCTP_SUCCESS_REPORT);
  89. aph->ph.param_length = sizeof(struct sctp_asconf_paramhdr);
  90. SCTP_BUF_LEN(m_reply) = aph->ph.param_length;
  91. aph->ph.param_length = htons(aph->ph.param_length);
  92. return (m_reply);
  93. }
  94. static struct mbuf *
  95. sctp_asconf_error_response(uint32_t id, uint16_t cause, uint8_t *error_tlv,
  96. uint16_t tlv_length)
  97. {
  98. struct mbuf *m_reply = NULL;
  99. struct sctp_asconf_paramhdr *aph;
  100. struct sctp_error_cause *error;
  101. uint32_t buf_len;
  102. uint16_t i, param_length, cause_length, padding_length;
  103. uint8_t *tlv;
  104. if (error_tlv == NULL) {
  105. tlv_length = 0;
  106. }
  107. cause_length = sizeof(struct sctp_error_cause) + tlv_length;
  108. param_length = sizeof(struct sctp_asconf_paramhdr) + cause_length;
  109. padding_length = tlv_length % 4;
  110. if (padding_length != 0) {
  111. padding_length = 4 - padding_length;
  112. }
  113. buf_len = param_length + padding_length;
  114. if (buf_len > MLEN) {
  115. SCTPDBG(SCTP_DEBUG_ASCONF1,
  116. "asconf_error_response: tlv_length (%xh) too big\n",
  117. tlv_length);
  118. return (NULL);
  119. }
  120. m_reply = sctp_get_mbuf_for_msg(buf_len, 0, M_NOWAIT, 1, MT_DATA);
  121. if (m_reply == NULL) {
  122. SCTPDBG(SCTP_DEBUG_ASCONF1,
  123. "asconf_error_response: couldn't get mbuf!\n");
  124. return (NULL);
  125. }
  126. aph = mtod(m_reply, struct sctp_asconf_paramhdr *);
  127. aph->ph.param_type = htons(SCTP_ERROR_CAUSE_IND);
  128. aph->ph.param_length = htons(param_length);
  129. aph->correlation_id = id;
  130. error = (struct sctp_error_cause *)(aph + 1);
  131. error->code = htons(cause);
  132. error->length = htons(cause_length);
  133. if (error_tlv != NULL) {
  134. tlv = (uint8_t *) (error + 1);
  135. memcpy(tlv, error_tlv, tlv_length);
  136. for (i = 0; i < padding_length; i++) {
  137. tlv[tlv_length + i] = 0;
  138. }
  139. }
  140. SCTP_BUF_LEN(m_reply) = buf_len;
  141. return (m_reply);
  142. }
  143. static struct mbuf *
  144. sctp_process_asconf_add_ip(struct sockaddr *src, struct sctp_asconf_paramhdr *aph,
  145. struct sctp_tcb *stcb, int send_hb, int response_required)
  146. {
  147. struct sctp_nets *net;
  148. struct mbuf *m_reply = NULL;
  149. union sctp_sockstore store;
  150. struct sctp_paramhdr *ph;
  151. uint16_t param_type, aparam_length;
  152. #if defined(INET) || defined(INET6)
  153. uint16_t param_length;
  154. #endif
  155. struct sockaddr *sa;
  156. int zero_address = 0;
  157. int bad_address = 0;
  158. #ifdef INET
  159. struct sockaddr_in *sin;
  160. struct sctp_ipv4addr_param *v4addr;
  161. #endif
  162. #ifdef INET6
  163. struct sockaddr_in6 *sin6;
  164. struct sctp_ipv6addr_param *v6addr;
  165. #endif
  166. aparam_length = ntohs(aph->ph.param_length);
  167. if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
  168. return (NULL);
  169. }
  170. ph = (struct sctp_paramhdr *)(aph + 1);
  171. param_type = ntohs(ph->param_type);
  172. #if defined(INET) || defined(INET6)
  173. param_length = ntohs(ph->param_length);
  174. if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
  175. return (NULL);
  176. }
  177. #endif
  178. sa = &store.sa;
  179. switch (param_type) {
  180. #ifdef INET
  181. case SCTP_IPV4_ADDRESS:
  182. if (param_length != sizeof(struct sctp_ipv4addr_param)) {
  183. /* invalid param size */
  184. return (NULL);
  185. }
  186. v4addr = (struct sctp_ipv4addr_param *)ph;
  187. sin = &store.sin;
  188. memset(sin, 0, sizeof(*sin));
  189. sin->sin_family = AF_INET;
  190. #ifdef HAVE_SIN_LEN
  191. sin->sin_len = sizeof(struct sockaddr_in);
  192. #endif
  193. sin->sin_port = stcb->rport;
  194. sin->sin_addr.s_addr = v4addr->addr;
  195. if ((sin->sin_addr.s_addr == INADDR_BROADCAST) ||
  196. IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
  197. bad_address = 1;
  198. }
  199. if (sin->sin_addr.s_addr == INADDR_ANY)
  200. zero_address = 1;
  201. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
  202. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  203. break;
  204. #endif
  205. #ifdef INET6
  206. case SCTP_IPV6_ADDRESS:
  207. if (param_length != sizeof(struct sctp_ipv6addr_param)) {
  208. /* invalid param size */
  209. return (NULL);
  210. }
  211. v6addr = (struct sctp_ipv6addr_param *)ph;
  212. sin6 = &store.sin6;
  213. memset(sin6, 0, sizeof(*sin6));
  214. sin6->sin6_family = AF_INET6;
  215. #ifdef HAVE_SIN6_LEN
  216. sin6->sin6_len = sizeof(struct sockaddr_in6);
  217. #endif
  218. sin6->sin6_port = stcb->rport;
  219. memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
  220. sizeof(struct in6_addr));
  221. if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
  222. bad_address = 1;
  223. }
  224. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  225. zero_address = 1;
  226. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_add_ip: adding ");
  227. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  228. break;
  229. #endif
  230. default:
  231. m_reply = sctp_asconf_error_response(aph->correlation_id,
  232. SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
  233. aparam_length);
  234. return (m_reply);
  235. } /* end switch */
  236. /* if 0.0.0.0/::0, add the source address instead */
  237. if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
  238. sa = src;
  239. SCTPDBG(SCTP_DEBUG_ASCONF1,
  240. "process_asconf_add_ip: using source addr ");
  241. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
  242. }
  243. net = NULL;
  244. /* add the address */
  245. if (bad_address) {
  246. m_reply = sctp_asconf_error_response(aph->correlation_id,
  247. SCTP_CAUSE_INVALID_PARAM, (uint8_t *) aph,
  248. aparam_length);
  249. } else if (sctp_add_remote_addr(stcb, sa, &net, stcb->asoc.port,
  250. SCTP_DONOT_SETSCOPE,
  251. SCTP_ADDR_DYNAMIC_ADDED) != 0) {
  252. SCTPDBG(SCTP_DEBUG_ASCONF1,
  253. "process_asconf_add_ip: error adding address\n");
  254. m_reply = sctp_asconf_error_response(aph->correlation_id,
  255. SCTP_CAUSE_RESOURCE_SHORTAGE, (uint8_t *) aph,
  256. aparam_length);
  257. } else {
  258. if (response_required) {
  259. m_reply =
  260. sctp_asconf_success_response(aph->correlation_id);
  261. }
  262. if (net != NULL) {
  263. /* notify upper layer */
  264. sctp_ulp_notify(SCTP_NOTIFY_ASCONF_ADD_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
  265. sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
  266. sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
  267. stcb, net);
  268. if (send_hb) {
  269. sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
  270. }
  271. }
  272. }
  273. return (m_reply);
  274. }
  275. static int
  276. sctp_asconf_del_remote_addrs_except(struct sctp_tcb *stcb, struct sockaddr *src)
  277. {
  278. struct sctp_nets *src_net, *net, *nnet;
  279. /* make sure the source address exists as a destination net */
  280. src_net = sctp_findnet(stcb, src);
  281. if (src_net == NULL) {
  282. /* not found */
  283. return (-1);
  284. }
  285. /* delete all destination addresses except the source */
  286. TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
  287. if (net != src_net) {
  288. /* delete this address */
  289. SCTPDBG(SCTP_DEBUG_ASCONF1,
  290. "asconf_del_remote_addrs_except: deleting ");
  291. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1,
  292. (struct sockaddr *)&net->ro._l_addr);
  293. /* notify upper layer */
  294. sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0,
  295. (struct sockaddr *)&net->ro._l_addr, SCTP_SO_NOT_LOCKED);
  296. sctp_remove_net(stcb, net);
  297. }
  298. }
  299. return (0);
  300. }
  301. static struct mbuf *
  302. sctp_process_asconf_delete_ip(struct sockaddr *src,
  303. struct sctp_asconf_paramhdr *aph,
  304. struct sctp_tcb *stcb, int response_required)
  305. {
  306. struct mbuf *m_reply = NULL;
  307. union sctp_sockstore store;
  308. struct sctp_paramhdr *ph;
  309. uint16_t param_type, aparam_length;
  310. #if defined(INET) || defined(INET6)
  311. uint16_t param_length;
  312. #endif
  313. struct sockaddr *sa;
  314. int zero_address = 0;
  315. int result;
  316. #ifdef INET
  317. struct sockaddr_in *sin;
  318. struct sctp_ipv4addr_param *v4addr;
  319. #endif
  320. #ifdef INET6
  321. struct sockaddr_in6 *sin6;
  322. struct sctp_ipv6addr_param *v6addr;
  323. #endif
  324. aparam_length = ntohs(aph->ph.param_length);
  325. if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
  326. return (NULL);
  327. }
  328. ph = (struct sctp_paramhdr *)(aph + 1);
  329. param_type = ntohs(ph->param_type);
  330. #if defined(INET) || defined(INET6)
  331. param_length = ntohs(ph->param_length);
  332. if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
  333. return (NULL);
  334. }
  335. #endif
  336. sa = &store.sa;
  337. switch (param_type) {
  338. #ifdef INET
  339. case SCTP_IPV4_ADDRESS:
  340. if (param_length != sizeof(struct sctp_ipv4addr_param)) {
  341. /* invalid param size */
  342. return (NULL);
  343. }
  344. v4addr = (struct sctp_ipv4addr_param *)ph;
  345. sin = &store.sin;
  346. memset(sin, 0, sizeof(*sin));
  347. sin->sin_family = AF_INET;
  348. #ifdef HAVE_SIN_LEN
  349. sin->sin_len = sizeof(struct sockaddr_in);
  350. #endif
  351. sin->sin_port = stcb->rport;
  352. sin->sin_addr.s_addr = v4addr->addr;
  353. if (sin->sin_addr.s_addr == INADDR_ANY)
  354. zero_address = 1;
  355. SCTPDBG(SCTP_DEBUG_ASCONF1,
  356. "process_asconf_delete_ip: deleting ");
  357. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  358. break;
  359. #endif
  360. #ifdef INET6
  361. case SCTP_IPV6_ADDRESS:
  362. if (param_length != sizeof(struct sctp_ipv6addr_param)) {
  363. /* invalid param size */
  364. return (NULL);
  365. }
  366. v6addr = (struct sctp_ipv6addr_param *)ph;
  367. sin6 = &store.sin6;
  368. memset(sin6, 0, sizeof(*sin6));
  369. sin6->sin6_family = AF_INET6;
  370. #ifdef HAVE_SIN6_LEN
  371. sin6->sin6_len = sizeof(struct sockaddr_in6);
  372. #endif
  373. sin6->sin6_port = stcb->rport;
  374. memcpy(&sin6->sin6_addr, v6addr->addr,
  375. sizeof(struct in6_addr));
  376. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  377. zero_address = 1;
  378. SCTPDBG(SCTP_DEBUG_ASCONF1,
  379. "process_asconf_delete_ip: deleting ");
  380. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  381. break;
  382. #endif
  383. default:
  384. m_reply = sctp_asconf_error_response(aph->correlation_id,
  385. SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
  386. aparam_length);
  387. return (m_reply);
  388. }
  389. /* make sure the source address is not being deleted */
  390. if (sctp_cmpaddr(sa, src)) {
  391. /* trying to delete the source address! */
  392. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete source addr\n");
  393. m_reply = sctp_asconf_error_response(aph->correlation_id,
  394. SCTP_CAUSE_DELETING_SRC_ADDR, (uint8_t *) aph,
  395. aparam_length);
  396. return (m_reply);
  397. }
  398. /* if deleting 0.0.0.0/::0, delete all addresses except src addr */
  399. if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
  400. result = sctp_asconf_del_remote_addrs_except(stcb, src);
  401. if (result) {
  402. /* src address did not exist? */
  403. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: src addr does not exist?\n");
  404. /* what error to reply with?? */
  405. m_reply =
  406. sctp_asconf_error_response(aph->correlation_id,
  407. SCTP_CAUSE_REQUEST_REFUSED, (uint8_t *) aph,
  408. aparam_length);
  409. } else if (response_required) {
  410. m_reply =
  411. sctp_asconf_success_response(aph->correlation_id);
  412. }
  413. return (m_reply);
  414. }
  415. /* delete the address */
  416. result = sctp_del_remote_addr(stcb, sa);
  417. /*
  418. * note if result == -2, the address doesn't exist in the asoc but
  419. * since it's being deleted anyways, we just ack the delete -- but
  420. * this probably means something has already gone awry
  421. */
  422. if (result == -1) {
  423. /* only one address in the asoc */
  424. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_delete_ip: tried to delete last IP addr!\n");
  425. m_reply = sctp_asconf_error_response(aph->correlation_id,
  426. SCTP_CAUSE_DELETING_LAST_ADDR, (uint8_t *) aph,
  427. aparam_length);
  428. } else {
  429. if (response_required) {
  430. m_reply = sctp_asconf_success_response(aph->correlation_id);
  431. }
  432. /* notify upper layer */
  433. sctp_ulp_notify(SCTP_NOTIFY_ASCONF_DELETE_IP, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
  434. }
  435. return (m_reply);
  436. }
  437. static struct mbuf *
  438. sctp_process_asconf_set_primary(struct sockaddr *src,
  439. struct sctp_asconf_paramhdr *aph,
  440. struct sctp_tcb *stcb, int response_required)
  441. {
  442. struct mbuf *m_reply = NULL;
  443. union sctp_sockstore store;
  444. struct sctp_paramhdr *ph;
  445. uint16_t param_type, aparam_length;
  446. #if defined(INET) || defined(INET6)
  447. uint16_t param_length;
  448. #endif
  449. struct sockaddr *sa;
  450. int zero_address = 0;
  451. #ifdef INET
  452. struct sockaddr_in *sin;
  453. struct sctp_ipv4addr_param *v4addr;
  454. #endif
  455. #ifdef INET6
  456. struct sockaddr_in6 *sin6;
  457. struct sctp_ipv6addr_param *v6addr;
  458. #endif
  459. aparam_length = ntohs(aph->ph.param_length);
  460. if (aparam_length < sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_paramhdr)) {
  461. return (NULL);
  462. }
  463. ph = (struct sctp_paramhdr *)(aph + 1);
  464. param_type = ntohs(ph->param_type);
  465. #if defined(INET) || defined(INET6)
  466. param_length = ntohs(ph->param_length);
  467. if (param_length + sizeof(struct sctp_asconf_paramhdr) != aparam_length) {
  468. return (NULL);
  469. }
  470. #endif
  471. sa = &store.sa;
  472. switch (param_type) {
  473. #ifdef INET
  474. case SCTP_IPV4_ADDRESS:
  475. if (param_length != sizeof(struct sctp_ipv4addr_param)) {
  476. /* invalid param size */
  477. return (NULL);
  478. }
  479. v4addr = (struct sctp_ipv4addr_param *)ph;
  480. sin = &store.sin;
  481. memset(sin, 0, sizeof(*sin));
  482. sin->sin_family = AF_INET;
  483. #ifdef HAVE_SIN_LEN
  484. sin->sin_len = sizeof(struct sockaddr_in);
  485. #endif
  486. sin->sin_addr.s_addr = v4addr->addr;
  487. if (sin->sin_addr.s_addr == INADDR_ANY)
  488. zero_address = 1;
  489. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
  490. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  491. break;
  492. #endif
  493. #ifdef INET6
  494. case SCTP_IPV6_ADDRESS:
  495. if (param_length != sizeof(struct sctp_ipv6addr_param)) {
  496. /* invalid param size */
  497. return (NULL);
  498. }
  499. v6addr = (struct sctp_ipv6addr_param *)ph;
  500. sin6 = &store.sin6;
  501. memset(sin6, 0, sizeof(*sin6));
  502. sin6->sin6_family = AF_INET6;
  503. #ifdef HAVE_SIN6_LEN
  504. sin6->sin6_len = sizeof(struct sockaddr_in6);
  505. #endif
  506. memcpy((caddr_t)&sin6->sin6_addr, v6addr->addr,
  507. sizeof(struct in6_addr));
  508. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  509. zero_address = 1;
  510. SCTPDBG(SCTP_DEBUG_ASCONF1, "process_asconf_set_primary: ");
  511. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  512. break;
  513. #endif
  514. default:
  515. m_reply = sctp_asconf_error_response(aph->correlation_id,
  516. SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
  517. aparam_length);
  518. return (m_reply);
  519. }
  520. /* if 0.0.0.0/::0, use the source address instead */
  521. if (zero_address && SCTP_BASE_SYSCTL(sctp_nat_friendly)) {
  522. sa = src;
  523. SCTPDBG(SCTP_DEBUG_ASCONF1,
  524. "process_asconf_set_primary: using source addr ");
  525. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
  526. }
  527. /* set the primary address */
  528. if (sctp_set_primary_addr(stcb, sa, NULL) == 0) {
  529. SCTPDBG(SCTP_DEBUG_ASCONF1,
  530. "process_asconf_set_primary: primary address set\n");
  531. /* notify upper layer */
  532. sctp_ulp_notify(SCTP_NOTIFY_ASCONF_SET_PRIMARY, stcb, 0, sa, SCTP_SO_NOT_LOCKED);
  533. if ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_REACHABLE) &&
  534. ((stcb->asoc.primary_destination->dest_state & SCTP_ADDR_PF) == 0) &&
  535. (stcb->asoc.alternate != NULL)) {
  536. sctp_free_remote_addr(stcb->asoc.alternate);
  537. stcb->asoc.alternate = NULL;
  538. }
  539. if (response_required) {
  540. m_reply = sctp_asconf_success_response(aph->correlation_id);
  541. }
  542. /* Mobility adaptation.
  543. Ideally, when the reception of SET PRIMARY with DELETE IP
  544. ADDRESS of the previous primary destination, unacknowledged
  545. DATA are retransmitted immediately to the new primary
  546. destination for seamless handover.
  547. If the destination is UNCONFIRMED and marked to REQ_PRIM,
  548. The retransmission occur when reception of the
  549. HEARTBEAT-ACK. (See sctp_handle_heartbeat_ack in
  550. sctp_input.c)
  551. Also, when change of the primary destination, it is better
  552. that all subsequent new DATA containing already queued DATA
  553. are transmitted to the new primary destination. (by micchie)
  554. */
  555. if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
  556. SCTP_MOBILITY_BASE) ||
  557. sctp_is_mobility_feature_on(stcb->sctp_ep,
  558. SCTP_MOBILITY_FASTHANDOFF)) &&
  559. sctp_is_mobility_feature_on(stcb->sctp_ep,
  560. SCTP_MOBILITY_PRIM_DELETED) &&
  561. (stcb->asoc.primary_destination->dest_state &
  562. SCTP_ADDR_UNCONFIRMED) == 0) {
  563. sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
  564. stcb->sctp_ep, stcb, NULL,
  565. SCTP_FROM_SCTP_ASCONF + SCTP_LOC_1);
  566. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  567. SCTP_MOBILITY_FASTHANDOFF)) {
  568. sctp_assoc_immediate_retrans(stcb,
  569. stcb->asoc.primary_destination);
  570. }
  571. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  572. SCTP_MOBILITY_BASE)) {
  573. sctp_move_chunks_from_net(stcb,
  574. stcb->asoc.deleted_primary);
  575. }
  576. sctp_delete_prim_timer(stcb->sctp_ep, stcb);
  577. }
  578. } else {
  579. /* couldn't set the requested primary address! */
  580. SCTPDBG(SCTP_DEBUG_ASCONF1,
  581. "process_asconf_set_primary: set primary failed!\n");
  582. /* must have been an invalid address, so report */
  583. m_reply = sctp_asconf_error_response(aph->correlation_id,
  584. SCTP_CAUSE_UNRESOLVABLE_ADDR, (uint8_t *) aph,
  585. aparam_length);
  586. }
  587. return (m_reply);
  588. }
  589. /*
  590. * handles an ASCONF chunk.
  591. * if all parameters are processed ok, send a plain (empty) ASCONF-ACK
  592. */
  593. void
  594. sctp_handle_asconf(struct mbuf *m, unsigned int offset,
  595. struct sockaddr *src,
  596. struct sctp_asconf_chunk *cp, struct sctp_tcb *stcb,
  597. int first)
  598. {
  599. struct sctp_association *asoc;
  600. uint32_t serial_num;
  601. struct mbuf *n, *m_ack, *m_result, *m_tail;
  602. struct sctp_asconf_ack_chunk *ack_cp;
  603. struct sctp_asconf_paramhdr *aph;
  604. struct sctp_ipv6addr_param *p_addr;
  605. unsigned int asconf_limit, cnt;
  606. int error = 0; /* did an error occur? */
  607. /* asconf param buffer */
  608. uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
  609. struct sctp_asconf_ack *ack, *ack_next;
  610. /* verify minimum length */
  611. if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_chunk)) {
  612. SCTPDBG(SCTP_DEBUG_ASCONF1,
  613. "handle_asconf: chunk too small = %xh\n",
  614. ntohs(cp->ch.chunk_length));
  615. return;
  616. }
  617. asoc = &stcb->asoc;
  618. serial_num = ntohl(cp->serial_number);
  619. if (SCTP_TSN_GE(asoc->asconf_seq_in, serial_num)) {
  620. /* got a duplicate ASCONF */
  621. SCTPDBG(SCTP_DEBUG_ASCONF1,
  622. "handle_asconf: got duplicate serial number = %xh\n",
  623. serial_num);
  624. return;
  625. } else if (serial_num != (asoc->asconf_seq_in + 1)) {
  626. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: incorrect serial number = %xh (expected next = %xh)\n",
  627. serial_num, asoc->asconf_seq_in + 1);
  628. return;
  629. }
  630. /* it's the expected "next" sequence number, so process it */
  631. asoc->asconf_seq_in = serial_num; /* update sequence */
  632. /* get length of all the param's in the ASCONF */
  633. asconf_limit = offset + ntohs(cp->ch.chunk_length);
  634. SCTPDBG(SCTP_DEBUG_ASCONF1,
  635. "handle_asconf: asconf_limit=%u, sequence=%xh\n",
  636. asconf_limit, serial_num);
  637. if (first) {
  638. /* delete old cache */
  639. SCTPDBG(SCTP_DEBUG_ASCONF1,"handle_asconf: Now processing first ASCONF. Try to delete old cache\n");
  640. TAILQ_FOREACH_SAFE(ack, &asoc->asconf_ack_sent, next, ack_next) {
  641. if (ack->serial_number == serial_num)
  642. break;
  643. SCTPDBG(SCTP_DEBUG_ASCONF1,"handle_asconf: delete old(%u) < first(%u)\n",
  644. ack->serial_number, serial_num);
  645. TAILQ_REMOVE(&asoc->asconf_ack_sent, ack, next);
  646. if (ack->data != NULL) {
  647. sctp_m_freem(ack->data);
  648. }
  649. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), ack);
  650. }
  651. }
  652. m_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_ack_chunk), 0,
  653. M_NOWAIT, 1, MT_DATA);
  654. if (m_ack == NULL) {
  655. SCTPDBG(SCTP_DEBUG_ASCONF1,
  656. "handle_asconf: couldn't get mbuf!\n");
  657. return;
  658. }
  659. m_tail = m_ack; /* current reply chain's tail */
  660. /* fill in ASCONF-ACK header */
  661. ack_cp = mtod(m_ack, struct sctp_asconf_ack_chunk *);
  662. ack_cp->ch.chunk_type = SCTP_ASCONF_ACK;
  663. ack_cp->ch.chunk_flags = 0;
  664. ack_cp->serial_number = htonl(serial_num);
  665. /* set initial lengths (eg. just an ASCONF-ACK), ntohx at the end! */
  666. SCTP_BUF_LEN(m_ack) = sizeof(struct sctp_asconf_ack_chunk);
  667. ack_cp->ch.chunk_length = sizeof(struct sctp_asconf_ack_chunk);
  668. /* skip the lookup address parameter */
  669. offset += sizeof(struct sctp_asconf_chunk);
  670. p_addr = (struct sctp_ipv6addr_param *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), (uint8_t *)&aparam_buf);
  671. if (p_addr == NULL) {
  672. SCTPDBG(SCTP_DEBUG_ASCONF1,
  673. "handle_asconf: couldn't get lookup addr!\n");
  674. /* respond with a missing/invalid mandatory parameter error */
  675. sctp_m_freem(m_ack);
  676. return;
  677. }
  678. /* skip lookup addr */
  679. offset += SCTP_SIZE32(ntohs(p_addr->ph.param_length));
  680. /* get pointer to first asconf param in ASCONF */
  681. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_asconf_paramhdr), (uint8_t *)&aparam_buf);
  682. if (aph == NULL) {
  683. SCTPDBG(SCTP_DEBUG_ASCONF1, "Empty ASCONF received?\n");
  684. goto send_reply;
  685. }
  686. /* process through all parameters */
  687. cnt = 0;
  688. while (aph != NULL) {
  689. unsigned int param_length, param_type;
  690. param_type = ntohs(aph->ph.param_type);
  691. param_length = ntohs(aph->ph.param_length);
  692. if (offset + param_length > asconf_limit) {
  693. /* parameter goes beyond end of chunk! */
  694. sctp_m_freem(m_ack);
  695. return;
  696. }
  697. m_result = NULL;
  698. if (param_length > sizeof(aparam_buf)) {
  699. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) larger than buffer size!\n", param_length);
  700. sctp_m_freem(m_ack);
  701. return;
  702. }
  703. if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
  704. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: param length (%u) too short\n", param_length);
  705. sctp_m_freem(m_ack);
  706. return;
  707. }
  708. /* get the entire parameter */
  709. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
  710. if (aph == NULL) {
  711. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: couldn't get entire param\n");
  712. sctp_m_freem(m_ack);
  713. return;
  714. }
  715. switch (param_type) {
  716. case SCTP_ADD_IP_ADDRESS:
  717. m_result = sctp_process_asconf_add_ip(src, aph, stcb,
  718. (cnt < SCTP_BASE_SYSCTL(sctp_hb_maxburst)), error);
  719. cnt++;
  720. break;
  721. case SCTP_DEL_IP_ADDRESS:
  722. m_result = sctp_process_asconf_delete_ip(src, aph, stcb,
  723. error);
  724. break;
  725. case SCTP_ERROR_CAUSE_IND:
  726. /* not valid in an ASCONF chunk */
  727. break;
  728. case SCTP_SET_PRIM_ADDR:
  729. m_result = sctp_process_asconf_set_primary(src, aph,
  730. stcb, error);
  731. break;
  732. case SCTP_NAT_VTAGS:
  733. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: sees a NAT VTAG state parameter\n");
  734. break;
  735. case SCTP_SUCCESS_REPORT:
  736. /* not valid in an ASCONF chunk */
  737. break;
  738. case SCTP_ULP_ADAPTATION:
  739. /* FIX */
  740. break;
  741. default:
  742. if ((param_type & 0x8000) == 0) {
  743. /* Been told to STOP at this param */
  744. asconf_limit = offset;
  745. /*
  746. * FIX FIX - We need to call
  747. * sctp_arethere_unrecognized_parameters()
  748. * to get a operr and send it for any
  749. * param's with the 0x4000 bit set OR do it
  750. * here ourselves... note we still must STOP
  751. * if the 0x8000 bit is clear.
  752. */
  753. }
  754. /* unknown/invalid param type */
  755. break;
  756. } /* switch */
  757. /* add any (error) result to the reply mbuf chain */
  758. if (m_result != NULL) {
  759. SCTP_BUF_NEXT(m_tail) = m_result;
  760. m_tail = m_result;
  761. ack_cp->ch.chunk_length += SCTP_BUF_LEN(m_result);
  762. /* set flag to force success reports */
  763. error = 1;
  764. }
  765. offset += SCTP_SIZE32(param_length);
  766. /* update remaining ASCONF message length to process */
  767. if (offset >= asconf_limit) {
  768. /* no more data in the mbuf chain */
  769. break;
  770. }
  771. /* get pointer to next asconf param */
  772. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
  773. sizeof(struct sctp_asconf_paramhdr),
  774. (uint8_t *)&aparam_buf);
  775. if (aph == NULL) {
  776. /* can't get an asconf paramhdr */
  777. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: can't get asconf param hdr!\n");
  778. /* FIX ME - add error here... */
  779. }
  780. }
  781. send_reply:
  782. ack_cp->ch.chunk_length = htons(ack_cp->ch.chunk_length);
  783. /* save the ASCONF-ACK reply */
  784. ack = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asconf_ack),
  785. struct sctp_asconf_ack);
  786. if (ack == NULL) {
  787. sctp_m_freem(m_ack);
  788. return;
  789. }
  790. ack->serial_number = serial_num;
  791. ack->last_sent_to = NULL;
  792. ack->data = m_ack;
  793. ack->len = 0;
  794. for (n = m_ack; n != NULL; n = SCTP_BUF_NEXT(n)) {
  795. ack->len += SCTP_BUF_LEN(n);
  796. }
  797. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_ack_sent, ack, next);
  798. /* see if last_control_chunk_from is set properly (use IP src addr) */
  799. if (stcb->asoc.last_control_chunk_from == NULL) {
  800. /*
  801. * this could happen if the source address was just newly
  802. * added
  803. */
  804. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: looking up net for IP source address\n");
  805. SCTPDBG(SCTP_DEBUG_ASCONF1, "Looking for IP source: ");
  806. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, src);
  807. /* look up the from address */
  808. stcb->asoc.last_control_chunk_from = sctp_findnet(stcb, src);
  809. #ifdef SCTP_DEBUG
  810. if (stcb->asoc.last_control_chunk_from == NULL) {
  811. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf: IP source address not found?!\n");
  812. }
  813. #endif
  814. }
  815. }
  816. /*
  817. * does the address match? returns 0 if not, 1 if so
  818. */
  819. static uint32_t
  820. sctp_asconf_addr_match(struct sctp_asconf_addr *aa, struct sockaddr *sa)
  821. {
  822. switch (sa->sa_family) {
  823. #ifdef INET6
  824. case AF_INET6:
  825. {
  826. /* XXX scopeid */
  827. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  828. if ((aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) &&
  829. (memcmp(&aa->ap.addrp.addr, &sin6->sin6_addr,
  830. sizeof(struct in6_addr)) == 0)) {
  831. return (1);
  832. }
  833. break;
  834. }
  835. #endif
  836. #ifdef INET
  837. case AF_INET:
  838. {
  839. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  840. if ((aa->ap.addrp.ph.param_type == SCTP_IPV4_ADDRESS) &&
  841. (memcmp(&aa->ap.addrp.addr, &sin->sin_addr,
  842. sizeof(struct in_addr)) == 0)) {
  843. return (1);
  844. }
  845. break;
  846. }
  847. #endif
  848. default:
  849. break;
  850. }
  851. return (0);
  852. }
  853. /*
  854. * does the address match? returns 0 if not, 1 if so
  855. */
  856. static uint32_t
  857. sctp_addr_match(struct sctp_paramhdr *ph, struct sockaddr *sa)
  858. {
  859. #if defined(INET) || defined(INET6)
  860. uint16_t param_type, param_length;
  861. param_type = ntohs(ph->param_type);
  862. param_length = ntohs(ph->param_length);
  863. #endif
  864. switch (sa->sa_family) {
  865. #ifdef INET6
  866. case AF_INET6:
  867. {
  868. /* XXX scopeid */
  869. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  870. struct sctp_ipv6addr_param *v6addr;
  871. v6addr = (struct sctp_ipv6addr_param *)ph;
  872. if ((param_type == SCTP_IPV6_ADDRESS) &&
  873. (param_length == sizeof(struct sctp_ipv6addr_param)) &&
  874. (memcmp(&v6addr->addr, &sin6->sin6_addr,
  875. sizeof(struct in6_addr)) == 0)) {
  876. return (1);
  877. }
  878. break;
  879. }
  880. #endif
  881. #ifdef INET
  882. case AF_INET:
  883. {
  884. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  885. struct sctp_ipv4addr_param *v4addr;
  886. v4addr = (struct sctp_ipv4addr_param *)ph;
  887. if ((param_type == SCTP_IPV4_ADDRESS) &&
  888. (param_length == sizeof(struct sctp_ipv4addr_param)) &&
  889. (memcmp(&v4addr->addr, &sin->sin_addr,
  890. sizeof(struct in_addr)) == 0)) {
  891. return (1);
  892. }
  893. break;
  894. }
  895. #endif
  896. default:
  897. break;
  898. }
  899. return (0);
  900. }
  901. /*
  902. * Cleanup for non-responded/OP ERR'd ASCONF
  903. */
  904. void
  905. sctp_asconf_cleanup(struct sctp_tcb *stcb)
  906. {
  907. /*
  908. * clear out any existing asconfs going out
  909. */
  910. sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
  911. SCTP_FROM_SCTP_ASCONF + SCTP_LOC_2);
  912. stcb->asoc.asconf_seq_out_acked = stcb->asoc.asconf_seq_out;
  913. /* remove the old ASCONF on our outbound queue */
  914. sctp_toss_old_asconf(stcb);
  915. }
  916. /*
  917. * cleanup any cached source addresses that may be topologically
  918. * incorrect after a new address has been added to this interface.
  919. */
  920. static void
  921. sctp_asconf_nets_cleanup(struct sctp_tcb *stcb, struct sctp_ifn *ifn)
  922. {
  923. struct sctp_nets *net;
  924. /*
  925. * Ideally, we want to only clear cached routes and source addresses
  926. * that are topologically incorrect. But since there is no easy way
  927. * to know whether the newly added address on the ifn would cause a
  928. * routing change (i.e. a new egress interface would be chosen)
  929. * without doing a new routing lookup and source address selection,
  930. * we will (for now) just flush any cached route using a different
  931. * ifn (and cached source addrs) and let output re-choose them during
  932. * the next send on that net.
  933. */
  934. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  935. /*
  936. * clear any cached route (and cached source address) if the
  937. * route's interface is NOT the same as the address change.
  938. * If it's the same interface, just clear the cached source
  939. * address.
  940. */
  941. if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro) &&
  942. ((ifn == NULL) ||
  943. (SCTP_GET_IF_INDEX_FROM_ROUTE(&net->ro) != ifn->ifn_index))) {
  944. /* clear any cached route */
  945. #if defined(__FreeBSD__) && !defined(__Userspace__)
  946. RO_NHFREE(&net->ro);
  947. #else
  948. RTFREE(net->ro.ro_rt);
  949. net->ro.ro_rt = NULL;
  950. #endif
  951. }
  952. /* clear any cached source address */
  953. if (net->src_addr_selected) {
  954. sctp_free_ifa(net->ro._s_addr);
  955. net->ro._s_addr = NULL;
  956. net->src_addr_selected = 0;
  957. }
  958. }
  959. }
  960. void
  961. sctp_assoc_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *dstnet)
  962. {
  963. int error;
  964. if (dstnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
  965. return;
  966. }
  967. if (stcb->asoc.deleted_primary == NULL) {
  968. return;
  969. }
  970. if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
  971. SCTPDBG(SCTP_DEBUG_ASCONF1, "assoc_immediate_retrans: Deleted primary is ");
  972. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.deleted_primary->ro._l_addr.sa);
  973. SCTPDBG(SCTP_DEBUG_ASCONF1, "Current Primary is ");
  974. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, &stcb->asoc.primary_destination->ro._l_addr.sa);
  975. sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb,
  976. stcb->asoc.deleted_primary,
  977. SCTP_FROM_SCTP_ASCONF + SCTP_LOC_3);
  978. stcb->asoc.num_send_timers_up--;
  979. if (stcb->asoc.num_send_timers_up < 0) {
  980. stcb->asoc.num_send_timers_up = 0;
  981. }
  982. SCTP_TCB_LOCK_ASSERT(stcb);
  983. error = sctp_t3rxt_timer(stcb->sctp_ep, stcb,
  984. stcb->asoc.deleted_primary);
  985. if (error) {
  986. SCTP_INP_DECR_REF(stcb->sctp_ep);
  987. return;
  988. }
  989. SCTP_TCB_LOCK_ASSERT(stcb);
  990. #ifdef SCTP_AUDITING_ENABLED
  991. sctp_auditing(4, stcb->sctp_ep, stcb, stcb->asoc.deleted_primary);
  992. #endif
  993. sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  994. if ((stcb->asoc.num_send_timers_up == 0) &&
  995. (stcb->asoc.sent_queue_cnt > 0)) {
  996. struct sctp_tmit_chunk *chk;
  997. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  998. if (chk->whoTo != NULL) {
  999. break;
  1000. }
  1001. }
  1002. if (chk != NULL) {
  1003. sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
  1004. }
  1005. }
  1006. }
  1007. return;
  1008. }
  1009. #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
  1010. static int
  1011. sctp_asconf_queue_mgmt(struct sctp_tcb *, struct sctp_ifa *, uint16_t);
  1012. void
  1013. sctp_net_immediate_retrans(struct sctp_tcb *stcb, struct sctp_nets *net)
  1014. {
  1015. struct sctp_tmit_chunk *chk;
  1016. SCTPDBG(SCTP_DEBUG_ASCONF1, "net_immediate_retrans: RTO is %d\n", net->RTO);
  1017. sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net,
  1018. SCTP_FROM_SCTP_ASCONF + SCTP_LOC_4);
  1019. stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
  1020. net->error_count = 0;
  1021. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  1022. if (chk->whoTo == net) {
  1023. if (chk->sent < SCTP_DATAGRAM_RESEND) {
  1024. chk->sent = SCTP_DATAGRAM_RESEND;
  1025. sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  1026. sctp_flight_size_decrease(chk);
  1027. sctp_total_flight_decrease(stcb, chk);
  1028. net->marked_retrans++;
  1029. stcb->asoc.marked_retrans++;
  1030. }
  1031. }
  1032. }
  1033. if (net->marked_retrans) {
  1034. sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  1035. }
  1036. }
  1037. static void
  1038. sctp_path_check_and_react(struct sctp_tcb *stcb, struct sctp_ifa *newifa)
  1039. {
  1040. struct sctp_nets *net;
  1041. int addrnum, changed;
  1042. /* If number of local valid addresses is 1, the valid address is
  1043. probably newly added address.
  1044. Several valid addresses in this association. A source address
  1045. may not be changed. Additionally, they can be configured on a
  1046. same interface as "alias" addresses. (by micchie)
  1047. */
  1048. addrnum = sctp_local_addr_count(stcb);
  1049. SCTPDBG(SCTP_DEBUG_ASCONF1, "p_check_react(): %d local addresses\n",
  1050. addrnum);
  1051. if (addrnum == 1) {
  1052. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1053. /* clear any cached route and source address */
  1054. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1055. RO_NHFREE(&net->ro);
  1056. #else
  1057. if (net->ro.ro_rt) {
  1058. RTFREE(net->ro.ro_rt);
  1059. net->ro.ro_rt = NULL;
  1060. }
  1061. #endif
  1062. if (net->src_addr_selected) {
  1063. sctp_free_ifa(net->ro._s_addr);
  1064. net->ro._s_addr = NULL;
  1065. net->src_addr_selected = 0;
  1066. }
  1067. /* Retransmit unacknowledged DATA chunks immediately */
  1068. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  1069. SCTP_MOBILITY_FASTHANDOFF)) {
  1070. sctp_net_immediate_retrans(stcb, net);
  1071. }
  1072. /* also, SET PRIMARY is maybe already sent */
  1073. }
  1074. return;
  1075. }
  1076. /* Multiple local addresses exist in the association. */
  1077. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1078. /* clear any cached route and source address */
  1079. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1080. RO_NHFREE(&net->ro);
  1081. #else
  1082. if (net->ro.ro_rt) {
  1083. RTFREE(net->ro.ro_rt);
  1084. net->ro.ro_rt = NULL;
  1085. }
  1086. #endif
  1087. if (net->src_addr_selected) {
  1088. sctp_free_ifa(net->ro._s_addr);
  1089. net->ro._s_addr = NULL;
  1090. net->src_addr_selected = 0;
  1091. }
  1092. /* Check if the nexthop is corresponding to the new address.
  1093. If the new address is corresponding to the current nexthop,
  1094. the path will be changed.
  1095. If the new address is NOT corresponding to the current
  1096. nexthop, the path will not be changed.
  1097. */
  1098. SCTP_RTALLOC((sctp_route_t *)&net->ro,
  1099. stcb->sctp_ep->def_vrf_id,
  1100. stcb->sctp_ep->fibnum);
  1101. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1102. if (net->ro.ro_nh == NULL)
  1103. #else
  1104. if (net->ro.ro_rt == NULL)
  1105. #endif
  1106. continue;
  1107. changed = 0;
  1108. switch (net->ro._l_addr.sa.sa_family) {
  1109. #ifdef INET
  1110. case AF_INET:
  1111. if (sctp_v4src_match_nexthop(newifa, (sctp_route_t *)&net->ro)) {
  1112. changed = 1;
  1113. }
  1114. break;
  1115. #endif
  1116. #ifdef INET6
  1117. case AF_INET6:
  1118. if (sctp_v6src_match_nexthop(
  1119. &newifa->address.sin6, (sctp_route_t *)&net->ro)) {
  1120. changed = 1;
  1121. }
  1122. break;
  1123. #endif
  1124. default:
  1125. break;
  1126. }
  1127. /* if the newly added address does not relate routing
  1128. information, we skip.
  1129. */
  1130. if (changed == 0)
  1131. continue;
  1132. /* Retransmit unacknowledged DATA chunks immediately */
  1133. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  1134. SCTP_MOBILITY_FASTHANDOFF)) {
  1135. sctp_net_immediate_retrans(stcb, net);
  1136. }
  1137. /* Send SET PRIMARY for this new address */
  1138. if (net == stcb->asoc.primary_destination) {
  1139. (void)sctp_asconf_queue_mgmt(stcb, newifa,
  1140. SCTP_SET_PRIM_ADDR);
  1141. }
  1142. }
  1143. }
  1144. #endif
  1145. /*
  1146. * process an ADD/DELETE IP ack from peer.
  1147. * addr: corresponding sctp_ifa to the address being added/deleted.
  1148. * type: SCTP_ADD_IP_ADDRESS or SCTP_DEL_IP_ADDRESS.
  1149. * flag: 1=success, 0=failure.
  1150. */
  1151. static void
  1152. sctp_asconf_addr_mgmt_ack(struct sctp_tcb *stcb, struct sctp_ifa *addr, uint32_t flag)
  1153. {
  1154. /*
  1155. * do the necessary asoc list work- if we get a failure indication,
  1156. * leave the address on the assoc's restricted list. If we get a
  1157. * success indication, remove the address from the restricted list.
  1158. */
  1159. /*
  1160. * Note: this will only occur for ADD_IP_ADDRESS, since
  1161. * DEL_IP_ADDRESS is never actually added to the list...
  1162. */
  1163. if (flag) {
  1164. /* success case, so remove from the restricted list */
  1165. sctp_del_local_addr_restricted(stcb, addr);
  1166. #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
  1167. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  1168. SCTP_MOBILITY_BASE) ||
  1169. sctp_is_mobility_feature_on(stcb->sctp_ep,
  1170. SCTP_MOBILITY_FASTHANDOFF)) {
  1171. sctp_path_check_and_react(stcb, addr);
  1172. return;
  1173. }
  1174. #endif
  1175. /* clear any cached/topologically incorrect source addresses */
  1176. sctp_asconf_nets_cleanup(stcb, addr->ifn_p);
  1177. }
  1178. /* else, leave it on the list */
  1179. }
  1180. /*
  1181. * add an asconf add/delete/set primary IP address parameter to the queue.
  1182. * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
  1183. * returns 0 if queued, -1 if not queued/removed.
  1184. * NOTE: if adding, but a delete for the same address is already scheduled
  1185. * (and not yet sent out), simply remove it from queue. Same for deleting
  1186. * an address already scheduled for add. If a duplicate operation is found,
  1187. * ignore the new one.
  1188. */
  1189. static int
  1190. sctp_asconf_queue_mgmt(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
  1191. uint16_t type)
  1192. {
  1193. struct sctp_asconf_addr *aa, *aa_next;
  1194. /* make sure the request isn't already in the queue */
  1195. TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
  1196. /* address match? */
  1197. if (sctp_asconf_addr_match(aa, &ifa->address.sa) == 0)
  1198. continue;
  1199. /* is the request already in queue but not sent?
  1200. * pass the request already sent in order to resolve the following case:
  1201. * 1. arrival of ADD, then sent
  1202. * 2. arrival of DEL. we can't remove the ADD request already sent
  1203. * 3. arrival of ADD
  1204. */
  1205. if (aa->ap.aph.ph.param_type == type && aa->sent == 0) {
  1206. return (-1);
  1207. }
  1208. /* is the negative request already in queue, and not sent */
  1209. if ((aa->sent == 0) && (type == SCTP_ADD_IP_ADDRESS) &&
  1210. (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS)) {
  1211. /* add requested, delete already queued */
  1212. TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
  1213. /* remove the ifa from the restricted list */
  1214. sctp_del_local_addr_restricted(stcb, ifa);
  1215. /* free the asconf param */
  1216. SCTP_FREE(aa, SCTP_M_ASC_ADDR);
  1217. SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: add removes queued entry\n");
  1218. return (-1);
  1219. }
  1220. if ((aa->sent == 0) && (type == SCTP_DEL_IP_ADDRESS) &&
  1221. (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS)) {
  1222. /* delete requested, add already queued */
  1223. TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
  1224. /* remove the aa->ifa from the restricted list */
  1225. sctp_del_local_addr_restricted(stcb, aa->ifa);
  1226. /* free the asconf param */
  1227. SCTP_FREE(aa, SCTP_M_ASC_ADDR);
  1228. SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_mgmt: delete removes queued entry\n");
  1229. return (-1);
  1230. }
  1231. } /* for each aa */
  1232. /* adding new request to the queue */
  1233. SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
  1234. SCTP_M_ASC_ADDR);
  1235. if (aa == NULL) {
  1236. /* didn't get memory */
  1237. SCTPDBG(SCTP_DEBUG_ASCONF1, "asconf_queue_mgmt: failed to get memory!\n");
  1238. return (-1);
  1239. }
  1240. aa->special_del = 0;
  1241. /* fill in asconf address parameter fields */
  1242. /* top level elements are "networked" during send */
  1243. aa->ap.aph.ph.param_type = type;
  1244. aa->ifa = ifa;
  1245. atomic_add_int(&ifa->refcount, 1);
  1246. /* correlation_id filled in during send routine later... */
  1247. switch (ifa->address.sa.sa_family) {
  1248. #ifdef INET6
  1249. case AF_INET6:
  1250. {
  1251. struct sockaddr_in6 *sin6;
  1252. sin6 = &ifa->address.sin6;
  1253. aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
  1254. aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
  1255. aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
  1256. sizeof(struct sctp_ipv6addr_param);
  1257. memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
  1258. sizeof(struct in6_addr));
  1259. break;
  1260. }
  1261. #endif
  1262. #ifdef INET
  1263. case AF_INET:
  1264. {
  1265. struct sockaddr_in *sin;
  1266. sin = &ifa->address.sin;
  1267. aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
  1268. aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
  1269. aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) +
  1270. sizeof(struct sctp_ipv4addr_param);
  1271. memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
  1272. sizeof(struct in_addr));
  1273. break;
  1274. }
  1275. #endif
  1276. default:
  1277. /* invalid family! */
  1278. SCTP_FREE(aa, SCTP_M_ASC_ADDR);
  1279. sctp_free_ifa(ifa);
  1280. return (-1);
  1281. }
  1282. aa->sent = 0; /* clear sent flag */
  1283. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
  1284. #ifdef SCTP_DEBUG
  1285. if (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_ASCONF2) {
  1286. if (type == SCTP_ADD_IP_ADDRESS) {
  1287. SCTP_PRINTF("asconf_queue_mgmt: inserted asconf ADD_IP_ADDRESS: ");
  1288. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
  1289. } else if (type == SCTP_DEL_IP_ADDRESS) {
  1290. SCTP_PRINTF("asconf_queue_mgmt: appended asconf DEL_IP_ADDRESS: ");
  1291. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
  1292. } else {
  1293. SCTP_PRINTF("asconf_queue_mgmt: appended asconf SET_PRIM_ADDR: ");
  1294. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, &ifa->address.sa);
  1295. }
  1296. }
  1297. #endif
  1298. return (0);
  1299. }
  1300. /*
  1301. * add an asconf operation for the given ifa and type.
  1302. * type = SCTP_ADD_IP_ADDRESS, SCTP_DEL_IP_ADDRESS, SCTP_SET_PRIM_ADDR.
  1303. * returns 0 if completed, -1 if not completed, 1 if immediate send is
  1304. * advisable.
  1305. */
  1306. static int
  1307. sctp_asconf_queue_add(struct sctp_tcb *stcb, struct sctp_ifa *ifa,
  1308. uint16_t type)
  1309. {
  1310. uint32_t status;
  1311. int pending_delete_queued = 0;
  1312. int last;
  1313. /* see if peer supports ASCONF */
  1314. if (stcb->asoc.asconf_supported == 0) {
  1315. return (-1);
  1316. }
  1317. /*
  1318. * if this is deleting the last address from the assoc, mark it as
  1319. * pending.
  1320. */
  1321. if ((type == SCTP_DEL_IP_ADDRESS) && !stcb->asoc.asconf_del_pending) {
  1322. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  1323. last = (sctp_local_addr_count(stcb) == 0);
  1324. } else {
  1325. last = (sctp_local_addr_count(stcb) == 1);
  1326. }
  1327. if (last) {
  1328. /* set the pending delete info only */
  1329. stcb->asoc.asconf_del_pending = 1;
  1330. stcb->asoc.asconf_addr_del_pending = ifa;
  1331. atomic_add_int(&ifa->refcount, 1);
  1332. SCTPDBG(SCTP_DEBUG_ASCONF2,
  1333. "asconf_queue_add: mark delete last address pending\n");
  1334. return (-1);
  1335. }
  1336. }
  1337. /* queue an asconf parameter */
  1338. status = sctp_asconf_queue_mgmt(stcb, ifa, type);
  1339. /*
  1340. * if this is an add, and there is a delete also pending (i.e. the
  1341. * last local address is being changed), queue the pending delete too.
  1342. */
  1343. if ((type == SCTP_ADD_IP_ADDRESS) && stcb->asoc.asconf_del_pending && (status == 0)) {
  1344. /* queue in the pending delete */
  1345. if (sctp_asconf_queue_mgmt(stcb,
  1346. stcb->asoc.asconf_addr_del_pending,
  1347. SCTP_DEL_IP_ADDRESS) == 0) {
  1348. SCTPDBG(SCTP_DEBUG_ASCONF2, "asconf_queue_add: queuing pending delete\n");
  1349. pending_delete_queued = 1;
  1350. /* clear out the pending delete info */
  1351. stcb->asoc.asconf_del_pending = 0;
  1352. sctp_free_ifa(stcb->asoc.asconf_addr_del_pending);
  1353. stcb->asoc.asconf_addr_del_pending = NULL;
  1354. }
  1355. }
  1356. if (pending_delete_queued) {
  1357. struct sctp_nets *net;
  1358. /*
  1359. * since we know that the only/last address is now being
  1360. * changed in this case, reset the cwnd/rto on all nets to
  1361. * start as a new address and path. Also clear the error
  1362. * counts to give the assoc the best chance to complete the
  1363. * address change.
  1364. */
  1365. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1366. stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb,
  1367. net);
  1368. net->RTO = 0;
  1369. net->error_count = 0;
  1370. }
  1371. stcb->asoc.overall_error_count = 0;
  1372. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
  1373. sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
  1374. stcb->asoc.overall_error_count,
  1375. 0,
  1376. SCTP_FROM_SCTP_ASCONF,
  1377. __LINE__);
  1378. }
  1379. /* queue in an advisory set primary too */
  1380. (void)sctp_asconf_queue_mgmt(stcb, ifa, SCTP_SET_PRIM_ADDR);
  1381. /* let caller know we should send this out immediately */
  1382. status = 1;
  1383. }
  1384. return (status);
  1385. }
  1386. /*-
  1387. * add an asconf delete IP address parameter to the queue by sockaddr and
  1388. * possibly with no sctp_ifa available. This is only called by the routine
  1389. * that checks the addresses in an INIT-ACK against the current address list.
  1390. * returns 0 if completed, non-zero if not completed.
  1391. * NOTE: if an add is already scheduled (and not yet sent out), simply
  1392. * remove it from queue. If a duplicate operation is found, ignore the
  1393. * new one.
  1394. */
  1395. static int
  1396. sctp_asconf_queue_sa_delete(struct sctp_tcb *stcb, struct sockaddr *sa)
  1397. {
  1398. struct sctp_ifa *ifa;
  1399. struct sctp_asconf_addr *aa, *aa_next;
  1400. if (stcb == NULL) {
  1401. return (-1);
  1402. }
  1403. /* see if peer supports ASCONF */
  1404. if (stcb->asoc.asconf_supported == 0) {
  1405. return (-1);
  1406. }
  1407. /* make sure the request isn't already in the queue */
  1408. TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
  1409. /* address match? */
  1410. if (sctp_asconf_addr_match(aa, sa) == 0)
  1411. continue;
  1412. /* is the request already in queue (sent or not) */
  1413. if (aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
  1414. return (-1);
  1415. }
  1416. /* is the negative request already in queue, and not sent */
  1417. if (aa->sent == 1)
  1418. continue;
  1419. if (aa->ap.aph.ph.param_type == SCTP_ADD_IP_ADDRESS) {
  1420. /* add already queued, so remove existing entry */
  1421. TAILQ_REMOVE(&stcb->asoc.asconf_queue, aa, next);
  1422. sctp_del_local_addr_restricted(stcb, aa->ifa);
  1423. /* free the entry */
  1424. SCTP_FREE(aa, SCTP_M_ASC_ADDR);
  1425. return (-1);
  1426. }
  1427. } /* for each aa */
  1428. /* find any existing ifa-- NOTE ifa CAN be allowed to be NULL */
  1429. ifa = sctp_find_ifa_by_addr(sa, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
  1430. /* adding new request to the queue */
  1431. SCTP_MALLOC(aa, struct sctp_asconf_addr *, sizeof(*aa),
  1432. SCTP_M_ASC_ADDR);
  1433. if (aa == NULL) {
  1434. /* didn't get memory */
  1435. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1436. "sctp_asconf_queue_sa_delete: failed to get memory!\n");
  1437. return (-1);
  1438. }
  1439. aa->special_del = 0;
  1440. /* fill in asconf address parameter fields */
  1441. /* top level elements are "networked" during send */
  1442. aa->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
  1443. aa->ifa = ifa;
  1444. if (ifa)
  1445. atomic_add_int(&ifa->refcount, 1);
  1446. /* correlation_id filled in during send routine later... */
  1447. switch (sa->sa_family) {
  1448. #ifdef INET6
  1449. case AF_INET6:
  1450. {
  1451. /* IPv6 address */
  1452. struct sockaddr_in6 *sin6;
  1453. sin6 = (struct sockaddr_in6 *)sa;
  1454. aa->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
  1455. aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv6addr_param));
  1456. aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv6addr_param);
  1457. memcpy(&aa->ap.addrp.addr, &sin6->sin6_addr,
  1458. sizeof(struct in6_addr));
  1459. break;
  1460. }
  1461. #endif
  1462. #ifdef INET
  1463. case AF_INET:
  1464. {
  1465. /* IPv4 address */
  1466. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  1467. aa->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
  1468. aa->ap.addrp.ph.param_length = (sizeof(struct sctp_ipv4addr_param));
  1469. aa->ap.aph.ph.param_length = sizeof(struct sctp_asconf_paramhdr) + sizeof(struct sctp_ipv4addr_param);
  1470. memcpy(&aa->ap.addrp.addr, &sin->sin_addr,
  1471. sizeof(struct in_addr));
  1472. break;
  1473. }
  1474. #endif
  1475. default:
  1476. /* invalid family! */
  1477. SCTP_FREE(aa, SCTP_M_ASC_ADDR);
  1478. if (ifa)
  1479. sctp_free_ifa(ifa);
  1480. return (-1);
  1481. }
  1482. aa->sent = 0; /* clear sent flag */
  1483. /* delete goes to the back of the queue */
  1484. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa, next);
  1485. /* sa_ignore MEMLEAK {memory is put on the tailq} */
  1486. return (0);
  1487. }
  1488. /*
  1489. * find a specific asconf param on our "sent" queue
  1490. */
  1491. static struct sctp_asconf_addr *
  1492. sctp_asconf_find_param(struct sctp_tcb *stcb, uint32_t correlation_id)
  1493. {
  1494. struct sctp_asconf_addr *aa;
  1495. TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
  1496. if (aa->ap.aph.correlation_id == correlation_id &&
  1497. aa->sent == 1) {
  1498. /* found it */
  1499. return (aa);
  1500. }
  1501. }
  1502. /* didn't find it */
  1503. return (NULL);
  1504. }
  1505. /*
  1506. * process an SCTP_ERROR_CAUSE_IND for a ASCONF-ACK parameter and do
  1507. * notifications based on the error response
  1508. */
  1509. static void
  1510. sctp_asconf_process_error(struct sctp_tcb *stcb SCTP_UNUSED,
  1511. struct sctp_asconf_paramhdr *aph)
  1512. {
  1513. struct sctp_error_cause *eh;
  1514. struct sctp_paramhdr *ph;
  1515. uint16_t param_type;
  1516. uint16_t error_code;
  1517. eh = (struct sctp_error_cause *)(aph + 1);
  1518. ph = (struct sctp_paramhdr *)(eh + 1);
  1519. /* validate lengths */
  1520. if (htons(eh->length) + sizeof(struct sctp_error_cause) >
  1521. htons(aph->ph.param_length)) {
  1522. /* invalid error cause length */
  1523. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1524. "asconf_process_error: cause element too long\n");
  1525. return;
  1526. }
  1527. if (htons(ph->param_length) + sizeof(struct sctp_paramhdr) >
  1528. htons(eh->length)) {
  1529. /* invalid included TLV length */
  1530. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1531. "asconf_process_error: included TLV too long\n");
  1532. return;
  1533. }
  1534. /* which error code ? */
  1535. error_code = ntohs(eh->code);
  1536. param_type = ntohs(aph->ph.param_type);
  1537. /* FIX: this should go back up the REMOTE_ERROR ULP notify */
  1538. switch (error_code) {
  1539. case SCTP_CAUSE_RESOURCE_SHORTAGE:
  1540. /* we allow ourselves to "try again" for this error */
  1541. break;
  1542. default:
  1543. /* peer can't handle it... */
  1544. switch (param_type) {
  1545. case SCTP_ADD_IP_ADDRESS:
  1546. case SCTP_DEL_IP_ADDRESS:
  1547. case SCTP_SET_PRIM_ADDR:
  1548. break;
  1549. default:
  1550. break;
  1551. }
  1552. }
  1553. }
  1554. /*
  1555. * process an asconf queue param.
  1556. * aparam: parameter to process, will be removed from the queue.
  1557. * flag: 1=success case, 0=failure case
  1558. */
  1559. static void
  1560. sctp_asconf_process_param_ack(struct sctp_tcb *stcb,
  1561. struct sctp_asconf_addr *aparam, uint32_t flag)
  1562. {
  1563. uint16_t param_type;
  1564. /* process this param */
  1565. param_type = aparam->ap.aph.ph.param_type;
  1566. switch (param_type) {
  1567. case SCTP_ADD_IP_ADDRESS:
  1568. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1569. "process_param_ack: added IP address\n");
  1570. sctp_asconf_addr_mgmt_ack(stcb, aparam->ifa, flag);
  1571. break;
  1572. case SCTP_DEL_IP_ADDRESS:
  1573. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1574. "process_param_ack: deleted IP address\n");
  1575. /* nothing really to do... lists already updated */
  1576. break;
  1577. case SCTP_SET_PRIM_ADDR:
  1578. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1579. "process_param_ack: set primary IP address\n");
  1580. /* nothing to do... peer may start using this addr */
  1581. break;
  1582. default:
  1583. /* should NEVER happen */
  1584. break;
  1585. }
  1586. /* remove the param and free it */
  1587. TAILQ_REMOVE(&stcb->asoc.asconf_queue, aparam, next);
  1588. if (aparam->ifa)
  1589. sctp_free_ifa(aparam->ifa);
  1590. SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
  1591. }
  1592. /*
  1593. * cleanup from a bad asconf ack parameter
  1594. */
  1595. static void
  1596. sctp_asconf_ack_clear(struct sctp_tcb *stcb SCTP_UNUSED)
  1597. {
  1598. /* assume peer doesn't really know how to do asconfs */
  1599. /* XXX we could free the pending queue here */
  1600. }
  1601. void
  1602. sctp_handle_asconf_ack(struct mbuf *m, int offset,
  1603. struct sctp_asconf_ack_chunk *cp, struct sctp_tcb *stcb,
  1604. struct sctp_nets *net, int *abort_no_unlock)
  1605. {
  1606. struct sctp_association *asoc;
  1607. uint32_t serial_num;
  1608. uint16_t ack_length;
  1609. struct sctp_asconf_paramhdr *aph;
  1610. struct sctp_asconf_addr *aa, *aa_next;
  1611. uint32_t last_error_id = 0; /* last error correlation id */
  1612. uint32_t id;
  1613. struct sctp_asconf_addr *ap;
  1614. /* asconf param buffer */
  1615. uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
  1616. /* verify minimum length */
  1617. if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_asconf_ack_chunk)) {
  1618. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1619. "handle_asconf_ack: chunk too small = %xh\n",
  1620. ntohs(cp->ch.chunk_length));
  1621. return;
  1622. }
  1623. asoc = &stcb->asoc;
  1624. serial_num = ntohl(cp->serial_number);
  1625. /*
  1626. * NOTE: we may want to handle this differently- currently, we will
  1627. * abort when we get an ack for the expected serial number + 1 (eg.
  1628. * we didn't send it), process an ack normally if it is the expected
  1629. * serial number, and re-send the previous ack for *ALL* other
  1630. * serial numbers
  1631. */
  1632. /*
  1633. * if the serial number is the next expected, but I didn't send it,
  1634. * abort the asoc, since someone probably just hijacked us...
  1635. */
  1636. if (serial_num == (asoc->asconf_seq_out + 1)) {
  1637. struct mbuf *op_err;
  1638. char msg[SCTP_DIAG_INFO_LEN];
  1639. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got unexpected next serial number! Aborting asoc!\n");
  1640. SCTP_SNPRINTF(msg, sizeof(msg), "Never sent serial number %8.8x", serial_num);
  1641. op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
  1642. sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, false, SCTP_SO_NOT_LOCKED);
  1643. *abort_no_unlock = 1;
  1644. return;
  1645. }
  1646. if (serial_num != asoc->asconf_seq_out_acked + 1) {
  1647. /* got a duplicate/unexpected ASCONF-ACK */
  1648. SCTPDBG(SCTP_DEBUG_ASCONF1, "handle_asconf_ack: got duplicate/unexpected serial number = %xh (expected = %xh)\n",
  1649. serial_num, asoc->asconf_seq_out_acked + 1);
  1650. return;
  1651. }
  1652. if (serial_num == asoc->asconf_seq_out - 1) {
  1653. /* stop our timer */
  1654. sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep, stcb, NULL,
  1655. SCTP_FROM_SCTP_ASCONF + SCTP_LOC_5);
  1656. }
  1657. /* process the ASCONF-ACK contents */
  1658. ack_length = ntohs(cp->ch.chunk_length) -
  1659. sizeof(struct sctp_asconf_ack_chunk);
  1660. offset += sizeof(struct sctp_asconf_ack_chunk);
  1661. /* process through all parameters */
  1662. while (ack_length >= sizeof(struct sctp_asconf_paramhdr)) {
  1663. unsigned int param_length, param_type;
  1664. /* get pointer to next asconf parameter */
  1665. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset,
  1666. sizeof(struct sctp_asconf_paramhdr), aparam_buf);
  1667. if (aph == NULL) {
  1668. /* can't get an asconf paramhdr */
  1669. sctp_asconf_ack_clear(stcb);
  1670. return;
  1671. }
  1672. param_type = ntohs(aph->ph.param_type);
  1673. param_length = ntohs(aph->ph.param_length);
  1674. if (param_length > ack_length) {
  1675. sctp_asconf_ack_clear(stcb);
  1676. return;
  1677. }
  1678. if (param_length < sizeof(struct sctp_asconf_paramhdr)) {
  1679. sctp_asconf_ack_clear(stcb);
  1680. return;
  1681. }
  1682. /* get the complete parameter... */
  1683. if (param_length > sizeof(aparam_buf)) {
  1684. SCTPDBG(SCTP_DEBUG_ASCONF1,
  1685. "param length (%u) larger than buffer size!\n", param_length);
  1686. sctp_asconf_ack_clear(stcb);
  1687. return;
  1688. }
  1689. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(m, offset, param_length, aparam_buf);
  1690. if (aph == NULL) {
  1691. sctp_asconf_ack_clear(stcb);
  1692. return;
  1693. }
  1694. /* correlation_id is transparent to peer, no ntohl needed */
  1695. id = aph->correlation_id;
  1696. switch (param_type) {
  1697. case SCTP_ERROR_CAUSE_IND:
  1698. last_error_id = id;
  1699. /* find the corresponding asconf param in our queue */
  1700. ap = sctp_asconf_find_param(stcb, id);
  1701. if (ap == NULL) {
  1702. /* hmm... can't find this in our queue! */
  1703. break;
  1704. }
  1705. /* process the parameter, failed flag */
  1706. sctp_asconf_process_param_ack(stcb, ap, 0);
  1707. /* process the error response */
  1708. sctp_asconf_process_error(stcb, aph);
  1709. break;
  1710. case SCTP_SUCCESS_REPORT:
  1711. /* find the corresponding asconf param in our queue */
  1712. ap = sctp_asconf_find_param(stcb, id);
  1713. if (ap == NULL) {
  1714. /* hmm... can't find this in our queue! */
  1715. break;
  1716. }
  1717. /* process the parameter, success flag */
  1718. sctp_asconf_process_param_ack(stcb, ap, 1);
  1719. break;
  1720. default:
  1721. break;
  1722. } /* switch */
  1723. /* update remaining ASCONF-ACK message length to process */
  1724. if (ack_length > SCTP_SIZE32(param_length)) {
  1725. ack_length -= SCTP_SIZE32(param_length);
  1726. } else {
  1727. break;
  1728. }
  1729. offset += SCTP_SIZE32(param_length);
  1730. } /* while */
  1731. /*
  1732. * if there are any "sent" params still on the queue, these are
  1733. * implicitly "success", or "failed" (if we got an error back) ...
  1734. * so process these appropriately
  1735. *
  1736. * we assume that the correlation_id's are monotonically increasing
  1737. * beginning from 1 and that we don't have *that* many outstanding
  1738. * at any given time
  1739. */
  1740. if (last_error_id == 0)
  1741. last_error_id--; /* set to "max" value */
  1742. TAILQ_FOREACH_SAFE(aa, &stcb->asoc.asconf_queue, next, aa_next) {
  1743. if (aa->sent == 1) {
  1744. /*
  1745. * implicitly successful or failed if correlation_id
  1746. * < last_error_id, then success else, failure
  1747. */
  1748. if (aa->ap.aph.correlation_id < last_error_id)
  1749. sctp_asconf_process_param_ack(stcb, aa, 1);
  1750. else
  1751. sctp_asconf_process_param_ack(stcb, aa, 0);
  1752. } else {
  1753. /*
  1754. * since we always process in order (FIFO queue) if
  1755. * we reach one that hasn't been sent, the rest
  1756. * should not have been sent either. so, we're
  1757. * done...
  1758. */
  1759. break;
  1760. }
  1761. }
  1762. /* update the next sequence number to use */
  1763. asoc->asconf_seq_out_acked++;
  1764. /* remove the old ASCONF on our outbound queue */
  1765. sctp_toss_old_asconf(stcb);
  1766. if (!TAILQ_EMPTY(&stcb->asoc.asconf_queue)) {
  1767. #ifdef SCTP_TIMER_BASED_ASCONF
  1768. /* we have more params, so restart our timer */
  1769. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, stcb->sctp_ep,
  1770. stcb, net);
  1771. #else
  1772. /* we have more params, so send out more */
  1773. sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
  1774. #endif
  1775. }
  1776. }
  1777. #ifdef INET6
  1778. static uint32_t
  1779. sctp_is_scopeid_in_nets(struct sctp_tcb *stcb, struct sockaddr *sa)
  1780. {
  1781. struct sockaddr_in6 *sin6, *net6;
  1782. struct sctp_nets *net;
  1783. if (sa->sa_family != AF_INET6) {
  1784. /* wrong family */
  1785. return (0);
  1786. }
  1787. sin6 = (struct sockaddr_in6 *)sa;
  1788. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) == 0) {
  1789. /* not link local address */
  1790. return (0);
  1791. }
  1792. /* hunt through our destination nets list for this scope_id */
  1793. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1794. if (((struct sockaddr *)(&net->ro._l_addr))->sa_family !=
  1795. AF_INET6)
  1796. continue;
  1797. net6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  1798. if (IN6_IS_ADDR_LINKLOCAL(&net6->sin6_addr) == 0)
  1799. continue;
  1800. if (sctp_is_same_scope(sin6, net6)) {
  1801. /* found one */
  1802. return (1);
  1803. }
  1804. }
  1805. /* didn't find one */
  1806. return (0);
  1807. }
  1808. #endif
  1809. /*
  1810. * address management functions
  1811. */
  1812. static void
  1813. sctp_addr_mgmt_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1814. struct sctp_ifa *ifa, uint16_t type, int addr_locked)
  1815. {
  1816. int status;
  1817. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0 ||
  1818. sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
  1819. /* subset bound, no ASCONF allowed case, so ignore */
  1820. return;
  1821. }
  1822. /*
  1823. * note: we know this is not the subset bound, no ASCONF case eg.
  1824. * this is boundall or subset bound w/ASCONF allowed
  1825. */
  1826. /* first, make sure that the address is IPv4 or IPv6 and not jailed */
  1827. switch (ifa->address.sa.sa_family) {
  1828. #ifdef INET6
  1829. case AF_INET6:
  1830. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1831. if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  1832. &ifa->address.sin6.sin6_addr) != 0) {
  1833. return;
  1834. }
  1835. #endif
  1836. break;
  1837. #endif
  1838. #ifdef INET
  1839. case AF_INET:
  1840. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1841. if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  1842. &ifa->address.sin.sin_addr) != 0) {
  1843. return;
  1844. }
  1845. #endif
  1846. break;
  1847. #endif
  1848. default:
  1849. return;
  1850. }
  1851. #ifdef INET6
  1852. /* make sure we're "allowed" to add this type of addr */
  1853. if (ifa->address.sa.sa_family == AF_INET6) {
  1854. /* invalid if we're not a v6 endpoint */
  1855. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)
  1856. return;
  1857. /* is the v6 addr really valid ? */
  1858. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  1859. return;
  1860. }
  1861. }
  1862. #endif
  1863. /* put this address on the "pending/do not use yet" list */
  1864. sctp_add_local_addr_restricted(stcb, ifa);
  1865. /*
  1866. * check address scope if address is out of scope, don't queue
  1867. * anything... note: this would leave the address on both inp and
  1868. * asoc lists
  1869. */
  1870. switch (ifa->address.sa.sa_family) {
  1871. #ifdef INET6
  1872. case AF_INET6:
  1873. {
  1874. struct sockaddr_in6 *sin6;
  1875. sin6 = &ifa->address.sin6;
  1876. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  1877. /* we skip unspecified addresses */
  1878. return;
  1879. }
  1880. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  1881. if (stcb->asoc.scope.local_scope == 0) {
  1882. return;
  1883. }
  1884. /* is it the right link local scope? */
  1885. if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
  1886. return;
  1887. }
  1888. }
  1889. if (stcb->asoc.scope.site_scope == 0 &&
  1890. IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
  1891. return;
  1892. }
  1893. break;
  1894. }
  1895. #endif
  1896. #ifdef INET
  1897. case AF_INET:
  1898. {
  1899. struct sockaddr_in *sin;
  1900. /* invalid if we are a v6 only endpoint */
  1901. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  1902. SCTP_IPV6_V6ONLY(inp))
  1903. return;
  1904. sin = &ifa->address.sin;
  1905. if (sin->sin_addr.s_addr == 0) {
  1906. /* we skip unspecified addresses */
  1907. return;
  1908. }
  1909. if (stcb->asoc.scope.ipv4_local_scope == 0 &&
  1910. IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
  1911. return;
  1912. }
  1913. break;
  1914. }
  1915. #endif
  1916. default:
  1917. /* else, not AF_INET or AF_INET6, so skip */
  1918. return;
  1919. }
  1920. /* queue an asconf for this address add/delete */
  1921. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
  1922. /* does the peer do asconf? */
  1923. if (stcb->asoc.asconf_supported) {
  1924. /* queue an asconf for this addr */
  1925. status = sctp_asconf_queue_add(stcb, ifa, type);
  1926. /*
  1927. * if queued ok, and in the open state, send out the
  1928. * ASCONF. If in the non-open state, these will be
  1929. * sent when the state goes open.
  1930. */
  1931. if (status == 0 &&
  1932. ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  1933. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED))) {
  1934. #ifdef SCTP_TIMER_BASED_ASCONF
  1935. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
  1936. stcb, stcb->asoc.primary_destination);
  1937. #else
  1938. sctp_send_asconf(stcb, NULL, addr_locked);
  1939. #endif
  1940. }
  1941. }
  1942. }
  1943. }
  1944. int
  1945. sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
  1946. {
  1947. struct sctp_asconf_iterator *asc;
  1948. struct sctp_ifa *ifa;
  1949. struct sctp_laddr *l;
  1950. int cnt_invalid = 0;
  1951. asc = (struct sctp_asconf_iterator *)ptr;
  1952. LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
  1953. ifa = l->ifa;
  1954. switch (ifa->address.sa.sa_family) {
  1955. #ifdef INET6
  1956. case AF_INET6:
  1957. /* invalid if we're not a v6 endpoint */
  1958. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
  1959. cnt_invalid++;
  1960. if (asc->cnt == cnt_invalid)
  1961. return (1);
  1962. }
  1963. break;
  1964. #endif
  1965. #ifdef INET
  1966. case AF_INET:
  1967. {
  1968. /* invalid if we are a v6 only endpoint */
  1969. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  1970. SCTP_IPV6_V6ONLY(inp)) {
  1971. cnt_invalid++;
  1972. if (asc->cnt == cnt_invalid)
  1973. return (1);
  1974. }
  1975. break;
  1976. }
  1977. #endif
  1978. default:
  1979. /* invalid address family */
  1980. cnt_invalid++;
  1981. if (asc->cnt == cnt_invalid)
  1982. return (1);
  1983. }
  1984. }
  1985. return (0);
  1986. }
  1987. static int
  1988. sctp_asconf_iterator_ep_end(struct sctp_inpcb *inp, void *ptr, uint32_t val SCTP_UNUSED)
  1989. {
  1990. struct sctp_ifa *ifa;
  1991. struct sctp_asconf_iterator *asc;
  1992. struct sctp_laddr *laddr, *nladdr, *l;
  1993. /* Only for specific case not bound all */
  1994. asc = (struct sctp_asconf_iterator *)ptr;
  1995. LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
  1996. ifa = l->ifa;
  1997. if (l->action == SCTP_ADD_IP_ADDRESS) {
  1998. LIST_FOREACH(laddr, &inp->sctp_addr_list,
  1999. sctp_nxt_addr) {
  2000. if (laddr->ifa == ifa) {
  2001. laddr->action = 0;
  2002. break;
  2003. }
  2004. }
  2005. } else if (l->action == SCTP_DEL_IP_ADDRESS) {
  2006. LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
  2007. /* remove only after all guys are done */
  2008. if (laddr->ifa == ifa) {
  2009. sctp_del_local_addr_ep(inp, ifa);
  2010. }
  2011. }
  2012. }
  2013. }
  2014. return (0);
  2015. }
  2016. void
  2017. sctp_asconf_iterator_stcb(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  2018. void *ptr, uint32_t val SCTP_UNUSED)
  2019. {
  2020. struct sctp_asconf_iterator *asc;
  2021. struct sctp_ifa *ifa;
  2022. struct sctp_laddr *l;
  2023. int cnt_invalid = 0;
  2024. int type, status;
  2025. int num_queued = 0;
  2026. asc = (struct sctp_asconf_iterator *)ptr;
  2027. LIST_FOREACH(l, &asc->list_of_work, sctp_nxt_addr) {
  2028. ifa = l->ifa;
  2029. type = l->action;
  2030. /* address's vrf_id must be the vrf_id of the assoc */
  2031. if (ifa->vrf_id != stcb->asoc.vrf_id) {
  2032. continue;
  2033. }
  2034. /* Same checks again for assoc */
  2035. switch (ifa->address.sa.sa_family) {
  2036. #ifdef INET6
  2037. case AF_INET6:
  2038. {
  2039. /* invalid if we're not a v6 endpoint */
  2040. struct sockaddr_in6 *sin6;
  2041. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
  2042. cnt_invalid++;
  2043. if (asc->cnt == cnt_invalid)
  2044. return;
  2045. else
  2046. continue;
  2047. }
  2048. sin6 = &ifa->address.sin6;
  2049. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  2050. /* we skip unspecified addresses */
  2051. continue;
  2052. }
  2053. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2054. if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2055. &sin6->sin6_addr) != 0) {
  2056. continue;
  2057. }
  2058. #endif
  2059. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  2060. if (stcb->asoc.scope.local_scope == 0) {
  2061. continue;
  2062. }
  2063. /* is it the right link local scope? */
  2064. if (sctp_is_scopeid_in_nets(stcb, &ifa->address.sa) == 0) {
  2065. continue;
  2066. }
  2067. }
  2068. break;
  2069. }
  2070. #endif
  2071. #ifdef INET
  2072. case AF_INET:
  2073. {
  2074. /* invalid if we are a v6 only endpoint */
  2075. struct sockaddr_in *sin;
  2076. /* invalid if we are a v6 only endpoint */
  2077. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  2078. SCTP_IPV6_V6ONLY(inp))
  2079. continue;
  2080. sin = &ifa->address.sin;
  2081. if (sin->sin_addr.s_addr == 0) {
  2082. /* we skip unspecified addresses */
  2083. continue;
  2084. }
  2085. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2086. if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2087. &sin->sin_addr) != 0) {
  2088. continue;
  2089. }
  2090. #endif
  2091. if (stcb->asoc.scope.ipv4_local_scope == 0 &&
  2092. IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
  2093. continue;
  2094. }
  2095. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  2096. SCTP_IPV6_V6ONLY(inp)) {
  2097. cnt_invalid++;
  2098. if (asc->cnt == cnt_invalid)
  2099. return;
  2100. else
  2101. continue;
  2102. }
  2103. break;
  2104. }
  2105. #endif
  2106. default:
  2107. /* invalid address family */
  2108. cnt_invalid++;
  2109. if (asc->cnt == cnt_invalid)
  2110. return;
  2111. else
  2112. continue;
  2113. }
  2114. if (type == SCTP_ADD_IP_ADDRESS) {
  2115. /* prevent this address from being used as a source */
  2116. sctp_add_local_addr_restricted(stcb, ifa);
  2117. } else if (type == SCTP_DEL_IP_ADDRESS) {
  2118. struct sctp_nets *net;
  2119. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  2120. /* delete this address if cached */
  2121. if (net->ro._s_addr == ifa) {
  2122. sctp_free_ifa(net->ro._s_addr);
  2123. net->ro._s_addr = NULL;
  2124. net->src_addr_selected = 0;
  2125. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2126. RO_NHFREE(&net->ro);
  2127. #else
  2128. if (net->ro.ro_rt) {
  2129. RTFREE(net->ro.ro_rt);
  2130. net->ro.ro_rt = NULL;
  2131. }
  2132. #endif
  2133. /*
  2134. * Now we deleted our src address,
  2135. * should we not also now reset the
  2136. * cwnd/rto to start as if its a new
  2137. * address?
  2138. */
  2139. stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
  2140. net->RTO = 0;
  2141. }
  2142. }
  2143. } else if (type == SCTP_SET_PRIM_ADDR) {
  2144. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
  2145. /* must validate the ifa is in the ep */
  2146. if (sctp_is_addr_in_ep(stcb->sctp_ep, ifa) == 0) {
  2147. continue;
  2148. }
  2149. } else {
  2150. /* Need to check scopes for this guy */
  2151. if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
  2152. continue;
  2153. }
  2154. }
  2155. }
  2156. /* queue an asconf for this address add/delete */
  2157. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF) &&
  2158. stcb->asoc.asconf_supported == 1) {
  2159. /* queue an asconf for this addr */
  2160. status = sctp_asconf_queue_add(stcb, ifa, type);
  2161. /*
  2162. * if queued ok, and in the open state, update the
  2163. * count of queued params. If in the non-open state,
  2164. * these get sent when the assoc goes open.
  2165. */
  2166. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  2167. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  2168. if (status >= 0) {
  2169. num_queued++;
  2170. }
  2171. }
  2172. }
  2173. }
  2174. /*
  2175. * If we have queued params in the open state, send out an ASCONF.
  2176. */
  2177. if (num_queued > 0) {
  2178. sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
  2179. }
  2180. }
  2181. void
  2182. sctp_asconf_iterator_end(void *ptr, uint32_t val SCTP_UNUSED)
  2183. {
  2184. struct sctp_asconf_iterator *asc;
  2185. struct sctp_ifa *ifa;
  2186. struct sctp_laddr *l, *nl;
  2187. asc = (struct sctp_asconf_iterator *)ptr;
  2188. LIST_FOREACH_SAFE(l, &asc->list_of_work, sctp_nxt_addr, nl) {
  2189. ifa = l->ifa;
  2190. if (l->action == SCTP_ADD_IP_ADDRESS) {
  2191. /* Clear the defer use flag */
  2192. ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  2193. }
  2194. sctp_free_ifa(ifa);
  2195. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), l);
  2196. SCTP_DECR_LADDR_COUNT();
  2197. }
  2198. SCTP_FREE(asc, SCTP_M_ASC_IT);
  2199. }
  2200. /*
  2201. * sa is the sockaddr to ask the peer to set primary to.
  2202. * returns: 0 = completed, -1 = error
  2203. */
  2204. int32_t
  2205. sctp_set_primary_ip_address_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
  2206. {
  2207. uint32_t vrf_id;
  2208. struct sctp_ifa *ifa;
  2209. /* find the ifa for the desired set primary */
  2210. vrf_id = stcb->asoc.vrf_id;
  2211. ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
  2212. if (ifa == NULL) {
  2213. /* Invalid address */
  2214. return (-1);
  2215. }
  2216. /* queue an ASCONF:SET_PRIM_ADDR to be sent */
  2217. if (!sctp_asconf_queue_add(stcb, ifa, SCTP_SET_PRIM_ADDR)) {
  2218. /* set primary queuing succeeded */
  2219. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2220. "set_primary_ip_address_sa: queued on tcb=%p, ",
  2221. (void *)stcb);
  2222. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  2223. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  2224. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  2225. #ifdef SCTP_TIMER_BASED_ASCONF
  2226. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
  2227. stcb->sctp_ep, stcb,
  2228. stcb->asoc.primary_destination);
  2229. #else
  2230. sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
  2231. #endif
  2232. }
  2233. } else {
  2234. SCTPDBG(SCTP_DEBUG_ASCONF1, "set_primary_ip_address_sa: failed to add to queue on tcb=%p, ",
  2235. (void *)stcb);
  2236. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF1, sa);
  2237. return (-1);
  2238. }
  2239. return (0);
  2240. }
  2241. int
  2242. sctp_is_addr_pending(struct sctp_tcb *stcb, struct sctp_ifa *sctp_ifa)
  2243. {
  2244. struct sctp_tmit_chunk *chk, *nchk;
  2245. unsigned int offset, asconf_limit;
  2246. struct sctp_asconf_chunk *acp;
  2247. struct sctp_asconf_paramhdr *aph;
  2248. uint8_t aparam_buf[SCTP_PARAM_BUFFER_SIZE];
  2249. struct sctp_paramhdr *ph;
  2250. int add_cnt, del_cnt;
  2251. uint16_t last_param_type;
  2252. add_cnt = del_cnt = 0;
  2253. last_param_type = 0;
  2254. TAILQ_FOREACH_SAFE(chk, &stcb->asoc.asconf_send_queue, sctp_next, nchk) {
  2255. if (chk->data == NULL) {
  2256. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: No mbuf data?\n");
  2257. continue;
  2258. }
  2259. offset = 0;
  2260. acp = mtod(chk->data, struct sctp_asconf_chunk *);
  2261. offset += sizeof(struct sctp_asconf_chunk);
  2262. asconf_limit = ntohs(acp->ch.chunk_length);
  2263. ph = (struct sctp_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_paramhdr), aparam_buf);
  2264. if (ph == NULL) {
  2265. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get lookup addr!\n");
  2266. continue;
  2267. }
  2268. offset += ntohs(ph->param_length);
  2269. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
  2270. if (aph == NULL) {
  2271. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: Empty ASCONF will be sent?\n");
  2272. continue;
  2273. }
  2274. while (aph != NULL) {
  2275. unsigned int param_length, param_type;
  2276. param_type = ntohs(aph->ph.param_type);
  2277. param_length = ntohs(aph->ph.param_length);
  2278. if (offset + param_length > asconf_limit) {
  2279. /* parameter goes beyond end of chunk! */
  2280. break;
  2281. }
  2282. if (param_length > sizeof(aparam_buf)) {
  2283. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length (%u) larger than buffer size!\n", param_length);
  2284. break;
  2285. }
  2286. if (param_length <= sizeof(struct sctp_paramhdr)) {
  2287. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: param length(%u) too short\n", param_length);
  2288. break;
  2289. }
  2290. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, param_length, aparam_buf);
  2291. if (aph == NULL) {
  2292. SCTPDBG(SCTP_DEBUG_ASCONF1, "is_addr_pending: couldn't get entire param\n");
  2293. break;
  2294. }
  2295. ph = (struct sctp_paramhdr *)(aph + 1);
  2296. if (sctp_addr_match(ph, &sctp_ifa->address.sa) != 0) {
  2297. switch (param_type) {
  2298. case SCTP_ADD_IP_ADDRESS:
  2299. add_cnt++;
  2300. break;
  2301. case SCTP_DEL_IP_ADDRESS:
  2302. del_cnt++;
  2303. break;
  2304. default:
  2305. break;
  2306. }
  2307. last_param_type = param_type;
  2308. }
  2309. offset += SCTP_SIZE32(param_length);
  2310. if (offset >= asconf_limit) {
  2311. /* no more data in the mbuf chain */
  2312. break;
  2313. }
  2314. /* get pointer to next asconf param */
  2315. aph = (struct sctp_asconf_paramhdr *)sctp_m_getptr(chk->data, offset, sizeof(struct sctp_asconf_paramhdr), aparam_buf);
  2316. }
  2317. }
  2318. /* we want to find the sequences which consist of ADD -> DEL -> ADD or DEL -> ADD */
  2319. if (add_cnt > del_cnt ||
  2320. (add_cnt == del_cnt && last_param_type == SCTP_ADD_IP_ADDRESS)) {
  2321. return (1);
  2322. }
  2323. return (0);
  2324. }
  2325. static struct sockaddr *
  2326. sctp_find_valid_localaddr(struct sctp_tcb *stcb, int addr_locked)
  2327. {
  2328. struct sctp_vrf *vrf = NULL;
  2329. struct sctp_ifn *sctp_ifn;
  2330. struct sctp_ifa *sctp_ifa;
  2331. if (addr_locked == SCTP_ADDR_NOT_LOCKED)
  2332. SCTP_IPI_ADDR_RLOCK();
  2333. vrf = sctp_find_vrf(stcb->asoc.vrf_id);
  2334. if (vrf == NULL) {
  2335. if (addr_locked == SCTP_ADDR_NOT_LOCKED)
  2336. SCTP_IPI_ADDR_RUNLOCK();
  2337. return (NULL);
  2338. }
  2339. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  2340. if (stcb->asoc.scope.loopback_scope == 0 &&
  2341. SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  2342. /* Skip if loopback_scope not set */
  2343. continue;
  2344. }
  2345. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  2346. switch (sctp_ifa->address.sa.sa_family) {
  2347. #ifdef INET
  2348. case AF_INET:
  2349. if (stcb->asoc.scope.ipv4_addr_legal) {
  2350. struct sockaddr_in *sin;
  2351. sin = &sctp_ifa->address.sin;
  2352. if (sin->sin_addr.s_addr == 0) {
  2353. /* skip unspecified addresses */
  2354. continue;
  2355. }
  2356. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2357. if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
  2358. &sin->sin_addr) != 0) {
  2359. continue;
  2360. }
  2361. #endif
  2362. if (stcb->asoc.scope.ipv4_local_scope == 0 &&
  2363. IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))
  2364. continue;
  2365. if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
  2366. (!sctp_is_addr_pending(stcb, sctp_ifa)))
  2367. continue;
  2368. /* found a valid local v4 address to use */
  2369. if (addr_locked == SCTP_ADDR_NOT_LOCKED)
  2370. SCTP_IPI_ADDR_RUNLOCK();
  2371. return (&sctp_ifa->address.sa);
  2372. }
  2373. break;
  2374. #endif
  2375. #ifdef INET6
  2376. case AF_INET6:
  2377. if (stcb->asoc.scope.ipv6_addr_legal) {
  2378. struct sockaddr_in6 *sin6;
  2379. if (sctp_ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  2380. continue;
  2381. }
  2382. sin6 = &sctp_ifa->address.sin6;
  2383. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  2384. /* we skip unspecified addresses */
  2385. continue;
  2386. }
  2387. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2388. if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
  2389. &sin6->sin6_addr) != 0) {
  2390. continue;
  2391. }
  2392. #endif
  2393. if (stcb->asoc.scope.local_scope == 0 &&
  2394. IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
  2395. continue;
  2396. if (stcb->asoc.scope.site_scope == 0 &&
  2397. IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
  2398. continue;
  2399. if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
  2400. (!sctp_is_addr_pending(stcb, sctp_ifa)))
  2401. continue;
  2402. /* found a valid local v6 address to use */
  2403. if (addr_locked == SCTP_ADDR_NOT_LOCKED)
  2404. SCTP_IPI_ADDR_RUNLOCK();
  2405. return (&sctp_ifa->address.sa);
  2406. }
  2407. break;
  2408. #endif
  2409. default:
  2410. break;
  2411. }
  2412. }
  2413. }
  2414. /* no valid addresses found */
  2415. if (addr_locked == SCTP_ADDR_NOT_LOCKED)
  2416. SCTP_IPI_ADDR_RUNLOCK();
  2417. return (NULL);
  2418. }
  2419. static struct sockaddr *
  2420. sctp_find_valid_localaddr_ep(struct sctp_tcb *stcb)
  2421. {
  2422. struct sctp_laddr *laddr;
  2423. LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
  2424. if (laddr->ifa == NULL) {
  2425. continue;
  2426. }
  2427. /* is the address restricted ? */
  2428. if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
  2429. (!sctp_is_addr_pending(stcb, laddr->ifa)))
  2430. continue;
  2431. /* found a valid local address to use */
  2432. return (&laddr->ifa->address.sa);
  2433. }
  2434. /* no valid addresses found */
  2435. return (NULL);
  2436. }
  2437. /*
  2438. * builds an ASCONF chunk from queued ASCONF params.
  2439. * returns NULL on error (no mbuf, no ASCONF params queued, etc).
  2440. */
  2441. struct mbuf *
  2442. sctp_compose_asconf(struct sctp_tcb *stcb, int *retlen, int addr_locked)
  2443. {
  2444. struct mbuf *m_asconf, *m_asconf_chk;
  2445. struct sctp_asconf_addr *aa;
  2446. struct sctp_asconf_chunk *acp;
  2447. struct sctp_asconf_paramhdr *aph;
  2448. struct sctp_asconf_addr_param *aap;
  2449. uint32_t p_length, overhead;
  2450. uint32_t correlation_id = 1; /* 0 is reserved... */
  2451. caddr_t ptr, lookup_ptr;
  2452. uint8_t lookup_used = 0;
  2453. /* are there any asconf params to send? */
  2454. TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
  2455. if (aa->sent == 0)
  2456. break;
  2457. }
  2458. if (aa == NULL)
  2459. return (NULL);
  2460. /* Consider IP header and SCTP common header. */
  2461. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  2462. overhead = SCTP_MIN_OVERHEAD;
  2463. } else {
  2464. overhead = SCTP_MIN_V4_OVERHEAD;
  2465. }
  2466. /* Consider ASONF chunk. */
  2467. overhead += sizeof(struct sctp_asconf_chunk);
  2468. /* Consider AUTH chunk. */
  2469. overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  2470. if (stcb->asoc.smallest_mtu <= overhead) {
  2471. /* MTU too small. */
  2472. return (NULL);
  2473. }
  2474. /*
  2475. * get a chunk header mbuf and a cluster for the asconf params since
  2476. * it's simpler to fill in the asconf chunk header lookup address on
  2477. * the fly
  2478. */
  2479. m_asconf_chk = sctp_get_mbuf_for_msg(sizeof(struct sctp_asconf_chunk), 0, M_NOWAIT, 1, MT_DATA);
  2480. if (m_asconf_chk == NULL) {
  2481. /* no mbuf's */
  2482. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2483. "sctp_compose_asconf: couldn't get chunk mbuf!\n");
  2484. return (NULL);
  2485. }
  2486. m_asconf = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  2487. if (m_asconf == NULL) {
  2488. /* no mbuf's */
  2489. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2490. "sctp_compose_asconf: couldn't get mbuf!\n");
  2491. sctp_m_freem(m_asconf_chk);
  2492. return (NULL);
  2493. }
  2494. SCTP_BUF_LEN(m_asconf_chk) = sizeof(struct sctp_asconf_chunk);
  2495. SCTP_BUF_LEN(m_asconf) = 0;
  2496. acp = mtod(m_asconf_chk, struct sctp_asconf_chunk *);
  2497. memset(acp, 0, sizeof(struct sctp_asconf_chunk));
  2498. /* save pointers to lookup address and asconf params */
  2499. lookup_ptr = (caddr_t)(acp + 1); /* after the header */
  2500. ptr = mtod(m_asconf, caddr_t); /* beginning of cluster */
  2501. /* fill in chunk header info */
  2502. acp->ch.chunk_type = SCTP_ASCONF;
  2503. acp->ch.chunk_flags = 0;
  2504. acp->serial_number = htonl(stcb->asoc.asconf_seq_out);
  2505. stcb->asoc.asconf_seq_out++;
  2506. /* add parameters... up to smallest MTU allowed */
  2507. TAILQ_FOREACH(aa, &stcb->asoc.asconf_queue, next) {
  2508. if (aa->sent)
  2509. continue;
  2510. /* get the parameter length */
  2511. p_length = SCTP_SIZE32(aa->ap.aph.ph.param_length);
  2512. /* will it fit in current chunk? */
  2513. if ((SCTP_BUF_LEN(m_asconf) + p_length > stcb->asoc.smallest_mtu - overhead) ||
  2514. (SCTP_BUF_LEN(m_asconf) + p_length > MCLBYTES)) {
  2515. /* won't fit, so we're done with this chunk */
  2516. break;
  2517. }
  2518. /* assign (and store) a correlation id */
  2519. aa->ap.aph.correlation_id = correlation_id++;
  2520. /*
  2521. * fill in address if we're doing a delete this is a simple
  2522. * way for us to fill in the correlation address, which
  2523. * should only be used by the peer if we're deleting our
  2524. * source address and adding a new address (e.g. renumbering
  2525. * case)
  2526. */
  2527. if (lookup_used == 0 &&
  2528. (aa->special_del == 0) &&
  2529. aa->ap.aph.ph.param_type == SCTP_DEL_IP_ADDRESS) {
  2530. struct sctp_ipv6addr_param *lookup;
  2531. uint16_t p_size, addr_size;
  2532. lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
  2533. lookup->ph.param_type =
  2534. htons(aa->ap.addrp.ph.param_type);
  2535. if (aa->ap.addrp.ph.param_type == SCTP_IPV6_ADDRESS) {
  2536. /* copy IPv6 address */
  2537. p_size = sizeof(struct sctp_ipv6addr_param);
  2538. addr_size = sizeof(struct in6_addr);
  2539. } else {
  2540. /* copy IPv4 address */
  2541. p_size = sizeof(struct sctp_ipv4addr_param);
  2542. addr_size = sizeof(struct in_addr);
  2543. }
  2544. lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
  2545. memcpy(lookup->addr, &aa->ap.addrp.addr, addr_size);
  2546. SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
  2547. lookup_used = 1;
  2548. }
  2549. /* copy into current space */
  2550. memcpy(ptr, &aa->ap, p_length);
  2551. /* network elements and update lengths */
  2552. aph = (struct sctp_asconf_paramhdr *)ptr;
  2553. aap = (struct sctp_asconf_addr_param *)ptr;
  2554. /* correlation_id is transparent to peer, no htonl needed */
  2555. aph->ph.param_type = htons(aph->ph.param_type);
  2556. aph->ph.param_length = htons(aph->ph.param_length);
  2557. aap->addrp.ph.param_type = htons(aap->addrp.ph.param_type);
  2558. aap->addrp.ph.param_length = htons(aap->addrp.ph.param_length);
  2559. SCTP_BUF_LEN(m_asconf) += SCTP_SIZE32(p_length);
  2560. ptr += SCTP_SIZE32(p_length);
  2561. /*
  2562. * these params are removed off the pending list upon
  2563. * getting an ASCONF-ACK back from the peer, just set flag
  2564. */
  2565. aa->sent = 1;
  2566. }
  2567. /* check to see if the lookup addr has been populated yet */
  2568. if (lookup_used == 0) {
  2569. /* NOTE: if the address param is optional, can skip this... */
  2570. /* add any valid (existing) address... */
  2571. struct sctp_ipv6addr_param *lookup;
  2572. uint16_t p_size, addr_size;
  2573. struct sockaddr *found_addr;
  2574. caddr_t addr_ptr;
  2575. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)
  2576. found_addr = sctp_find_valid_localaddr(stcb,
  2577. addr_locked);
  2578. else
  2579. found_addr = sctp_find_valid_localaddr_ep(stcb);
  2580. lookup = (struct sctp_ipv6addr_param *)lookup_ptr;
  2581. if (found_addr != NULL) {
  2582. switch (found_addr->sa_family) {
  2583. #ifdef INET6
  2584. case AF_INET6:
  2585. /* copy IPv6 address */
  2586. lookup->ph.param_type =
  2587. htons(SCTP_IPV6_ADDRESS);
  2588. p_size = sizeof(struct sctp_ipv6addr_param);
  2589. addr_size = sizeof(struct in6_addr);
  2590. addr_ptr = (caddr_t)&((struct sockaddr_in6 *)
  2591. found_addr)->sin6_addr;
  2592. break;
  2593. #endif
  2594. #ifdef INET
  2595. case AF_INET:
  2596. /* copy IPv4 address */
  2597. lookup->ph.param_type =
  2598. htons(SCTP_IPV4_ADDRESS);
  2599. p_size = sizeof(struct sctp_ipv4addr_param);
  2600. addr_size = sizeof(struct in_addr);
  2601. addr_ptr = (caddr_t)&((struct sockaddr_in *)
  2602. found_addr)->sin_addr;
  2603. break;
  2604. #endif
  2605. default:
  2606. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2607. "sctp_compose_asconf: no usable lookup addr (family = %d)!\n",
  2608. found_addr->sa_family);
  2609. sctp_m_freem(m_asconf_chk);
  2610. sctp_m_freem(m_asconf);
  2611. return (NULL);
  2612. }
  2613. lookup->ph.param_length = htons(SCTP_SIZE32(p_size));
  2614. memcpy(lookup->addr, addr_ptr, addr_size);
  2615. SCTP_BUF_LEN(m_asconf_chk) += SCTP_SIZE32(p_size);
  2616. } else {
  2617. /* uh oh... don't have any address?? */
  2618. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2619. "sctp_compose_asconf: no lookup addr!\n");
  2620. sctp_m_freem(m_asconf_chk);
  2621. sctp_m_freem(m_asconf);
  2622. return (NULL);
  2623. }
  2624. }
  2625. /* chain it all together */
  2626. SCTP_BUF_NEXT(m_asconf_chk) = m_asconf;
  2627. *retlen = SCTP_BUF_LEN(m_asconf_chk) + SCTP_BUF_LEN(m_asconf);
  2628. acp->ch.chunk_length = htons(*retlen);
  2629. return (m_asconf_chk);
  2630. }
  2631. /*
  2632. * section to handle address changes before an association is up eg. changes
  2633. * during INIT/INIT-ACK/COOKIE-ECHO handshake
  2634. */
  2635. /*
  2636. * processes the (local) addresses in the INIT-ACK chunk
  2637. */
  2638. static void
  2639. sctp_process_initack_addresses(struct sctp_tcb *stcb, struct mbuf *m,
  2640. unsigned int offset, unsigned int length)
  2641. {
  2642. struct sctp_paramhdr tmp_param, *ph;
  2643. uint16_t plen, ptype;
  2644. struct sctp_ifa *sctp_ifa;
  2645. union sctp_sockstore store;
  2646. #ifdef INET6
  2647. struct sctp_ipv6addr_param addr6_store;
  2648. #endif
  2649. #ifdef INET
  2650. struct sctp_ipv4addr_param addr4_store;
  2651. #endif
  2652. SCTPDBG(SCTP_DEBUG_ASCONF2, "processing init-ack addresses\n");
  2653. if (stcb == NULL) /* Un-needed check for SA */
  2654. return;
  2655. /* convert to upper bound */
  2656. length += offset;
  2657. if ((offset + sizeof(struct sctp_paramhdr)) > length) {
  2658. return;
  2659. }
  2660. /* go through the addresses in the init-ack */
  2661. ph = (struct sctp_paramhdr *)
  2662. sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
  2663. (uint8_t *)&tmp_param);
  2664. while (ph != NULL) {
  2665. ptype = ntohs(ph->param_type);
  2666. plen = ntohs(ph->param_length);
  2667. switch (ptype) {
  2668. #ifdef INET6
  2669. case SCTP_IPV6_ADDRESS:
  2670. {
  2671. struct sctp_ipv6addr_param *a6p;
  2672. /* get the entire IPv6 address param */
  2673. a6p = (struct sctp_ipv6addr_param *)
  2674. sctp_m_getptr(m, offset,
  2675. sizeof(struct sctp_ipv6addr_param),
  2676. (uint8_t *)&addr6_store);
  2677. if (plen != sizeof(struct sctp_ipv6addr_param) ||
  2678. a6p == NULL) {
  2679. return;
  2680. }
  2681. memset(&store, 0, sizeof(union sctp_sockstore));
  2682. store.sin6.sin6_family = AF_INET6;
  2683. #ifdef HAVE_SIN6_LEN
  2684. store.sin6.sin6_len = sizeof(struct sockaddr_in6);
  2685. #endif
  2686. store.sin6.sin6_port = stcb->rport;
  2687. memcpy(&store.sin6.sin6_addr, a6p->addr, sizeof(struct in6_addr));
  2688. break;
  2689. }
  2690. #endif
  2691. #ifdef INET
  2692. case SCTP_IPV4_ADDRESS:
  2693. {
  2694. struct sctp_ipv4addr_param *a4p;
  2695. /* get the entire IPv4 address param */
  2696. a4p = (struct sctp_ipv4addr_param *)sctp_m_getptr(m, offset,
  2697. sizeof(struct sctp_ipv4addr_param),
  2698. (uint8_t *)&addr4_store);
  2699. if (plen != sizeof(struct sctp_ipv4addr_param) ||
  2700. a4p == NULL) {
  2701. return;
  2702. }
  2703. memset(&store, 0, sizeof(union sctp_sockstore));
  2704. store.sin.sin_family = AF_INET;
  2705. #ifdef HAVE_SIN_LEN
  2706. store.sin.sin_len = sizeof(struct sockaddr_in);
  2707. #endif
  2708. store.sin.sin_port = stcb->rport;
  2709. store.sin.sin_addr.s_addr = a4p->addr;
  2710. break;
  2711. }
  2712. #endif
  2713. default:
  2714. goto next_addr;
  2715. }
  2716. /* see if this address really (still) exists */
  2717. sctp_ifa = sctp_find_ifa_by_addr(&store.sa, stcb->asoc.vrf_id,
  2718. SCTP_ADDR_NOT_LOCKED);
  2719. if (sctp_ifa == NULL) {
  2720. /* address doesn't exist anymore */
  2721. int status;
  2722. /* are ASCONFs allowed ? */
  2723. if ((sctp_is_feature_on(stcb->sctp_ep,
  2724. SCTP_PCB_FLAGS_DO_ASCONF)) &&
  2725. stcb->asoc.asconf_supported) {
  2726. /* queue an ASCONF DEL_IP_ADDRESS */
  2727. status = sctp_asconf_queue_sa_delete(stcb, &store.sa);
  2728. /*
  2729. * if queued ok, and in correct state, send
  2730. * out the ASCONF.
  2731. */
  2732. if (status == 0 &&
  2733. SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
  2734. #ifdef SCTP_TIMER_BASED_ASCONF
  2735. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
  2736. stcb->sctp_ep, stcb,
  2737. stcb->asoc.primary_destination);
  2738. #else
  2739. sctp_send_asconf(stcb, NULL, SCTP_ADDR_NOT_LOCKED);
  2740. #endif
  2741. }
  2742. }
  2743. }
  2744. next_addr:
  2745. /*
  2746. * Sanity check: Make sure the length isn't 0, otherwise
  2747. * we'll be stuck in this loop for a long time...
  2748. */
  2749. if (SCTP_SIZE32(plen) == 0) {
  2750. SCTP_PRINTF("process_initack_addrs: bad len (%d) type=%xh\n",
  2751. plen, ptype);
  2752. return;
  2753. }
  2754. /* get next parameter */
  2755. offset += SCTP_SIZE32(plen);
  2756. if ((offset + sizeof(struct sctp_paramhdr)) > length)
  2757. return;
  2758. ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
  2759. sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
  2760. } /* while */
  2761. }
  2762. /* FIX ME: need to verify return result for v6 address type if v6 disabled */
  2763. /*
  2764. * checks to see if a specific address is in the initack address list returns
  2765. * 1 if found, 0 if not
  2766. */
  2767. static uint32_t
  2768. sctp_addr_in_initack(struct mbuf *m, uint32_t offset, uint32_t length, struct sockaddr *sa)
  2769. {
  2770. struct sctp_paramhdr tmp_param, *ph;
  2771. uint16_t plen, ptype;
  2772. #ifdef INET
  2773. struct sockaddr_in *sin;
  2774. struct sctp_ipv4addr_param *a4p;
  2775. struct sctp_ipv6addr_param addr4_store;
  2776. #endif
  2777. #ifdef INET6
  2778. struct sockaddr_in6 *sin6;
  2779. struct sctp_ipv6addr_param *a6p;
  2780. struct sctp_ipv6addr_param addr6_store;
  2781. #ifdef SCTP_EMBEDDED_V6_SCOPE
  2782. struct sockaddr_in6 sin6_tmp;
  2783. #endif
  2784. #endif
  2785. switch (sa->sa_family) {
  2786. #ifdef INET
  2787. case AF_INET:
  2788. break;
  2789. #endif
  2790. #ifdef INET6
  2791. case AF_INET6:
  2792. break;
  2793. #endif
  2794. default:
  2795. return (0);
  2796. }
  2797. SCTPDBG(SCTP_DEBUG_ASCONF2, "find_initack_addr: starting search for ");
  2798. SCTPDBG_ADDR(SCTP_DEBUG_ASCONF2, sa);
  2799. /* convert to upper bound */
  2800. length += offset;
  2801. if ((offset + sizeof(struct sctp_paramhdr)) > length) {
  2802. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2803. "find_initack_addr: invalid offset?\n");
  2804. return (0);
  2805. }
  2806. /* go through the addresses in the init-ack */
  2807. ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
  2808. sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
  2809. while (ph != NULL) {
  2810. ptype = ntohs(ph->param_type);
  2811. plen = ntohs(ph->param_length);
  2812. switch (ptype) {
  2813. #ifdef INET6
  2814. case SCTP_IPV6_ADDRESS:
  2815. if (sa->sa_family == AF_INET6) {
  2816. /* get the entire IPv6 address param */
  2817. if (plen != sizeof(struct sctp_ipv6addr_param)) {
  2818. break;
  2819. }
  2820. /* get the entire IPv6 address param */
  2821. a6p = (struct sctp_ipv6addr_param *)
  2822. sctp_m_getptr(m, offset,
  2823. sizeof(struct sctp_ipv6addr_param),
  2824. (uint8_t *)&addr6_store);
  2825. if (a6p == NULL) {
  2826. return (0);
  2827. }
  2828. sin6 = (struct sockaddr_in6 *)sa;
  2829. #ifdef SCTP_EMBEDDED_V6_SCOPE
  2830. if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
  2831. /* create a copy and clear scope */
  2832. memcpy(&sin6_tmp, sin6,
  2833. sizeof(struct sockaddr_in6));
  2834. sin6 = &sin6_tmp;
  2835. in6_clearscope(&sin6->sin6_addr);
  2836. }
  2837. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  2838. if (memcmp(&sin6->sin6_addr, a6p->addr,
  2839. sizeof(struct in6_addr)) == 0) {
  2840. /* found it */
  2841. return (1);
  2842. }
  2843. }
  2844. break;
  2845. #endif /* INET6 */
  2846. #ifdef INET
  2847. case SCTP_IPV4_ADDRESS:
  2848. if (sa->sa_family == AF_INET) {
  2849. if (plen != sizeof(struct sctp_ipv4addr_param)) {
  2850. break;
  2851. }
  2852. /* get the entire IPv4 address param */
  2853. a4p = (struct sctp_ipv4addr_param *)
  2854. sctp_m_getptr(m, offset,
  2855. sizeof(struct sctp_ipv4addr_param),
  2856. (uint8_t *)&addr4_store);
  2857. if (a4p == NULL) {
  2858. return (0);
  2859. }
  2860. sin = (struct sockaddr_in *)sa;
  2861. if (sin->sin_addr.s_addr == a4p->addr) {
  2862. /* found it */
  2863. return (1);
  2864. }
  2865. }
  2866. break;
  2867. #endif
  2868. default:
  2869. break;
  2870. }
  2871. /* get next parameter */
  2872. offset += SCTP_SIZE32(plen);
  2873. if (offset + sizeof(struct sctp_paramhdr) > length) {
  2874. return (0);
  2875. }
  2876. ph = (struct sctp_paramhdr *)
  2877. sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
  2878. (uint8_t *) & tmp_param);
  2879. } /* while */
  2880. /* not found! */
  2881. return (0);
  2882. }
  2883. /*
  2884. * makes sure that the current endpoint local addr list is consistent with
  2885. * the new association (eg. subset bound, asconf allowed) adds addresses as
  2886. * necessary
  2887. */
  2888. static void
  2889. sctp_check_address_list_ep(struct sctp_tcb *stcb, struct mbuf *m, int offset,
  2890. int length, struct sockaddr *init_addr)
  2891. {
  2892. struct sctp_laddr *laddr;
  2893. /* go through the endpoint list */
  2894. LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
  2895. /* be paranoid and validate the laddr */
  2896. if (laddr->ifa == NULL) {
  2897. SCTPDBG(SCTP_DEBUG_ASCONF1,
  2898. "check_addr_list_ep: laddr->ifa is NULL");
  2899. continue;
  2900. }
  2901. /* do i have it implicitly? */
  2902. if (sctp_cmpaddr(&laddr->ifa->address.sa, init_addr)) {
  2903. continue;
  2904. }
  2905. /* check to see if in the init-ack */
  2906. if (!sctp_addr_in_initack(m, offset, length, &laddr->ifa->address.sa)) {
  2907. /* try to add it */
  2908. sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb, laddr->ifa,
  2909. SCTP_ADD_IP_ADDRESS, SCTP_ADDR_NOT_LOCKED);
  2910. }
  2911. }
  2912. }
  2913. /*
  2914. * makes sure that the current kernel address list is consistent with the new
  2915. * association (with all addrs bound) adds addresses as necessary
  2916. */
  2917. static void
  2918. sctp_check_address_list_all(struct sctp_tcb *stcb, struct mbuf *m, int offset,
  2919. int length, struct sockaddr *init_addr,
  2920. uint16_t local_scope, uint16_t site_scope,
  2921. uint16_t ipv4_scope, uint16_t loopback_scope)
  2922. {
  2923. struct sctp_vrf *vrf = NULL;
  2924. struct sctp_ifn *sctp_ifn;
  2925. struct sctp_ifa *sctp_ifa;
  2926. uint32_t vrf_id;
  2927. #ifdef INET
  2928. struct sockaddr_in *sin;
  2929. #endif
  2930. #ifdef INET6
  2931. struct sockaddr_in6 *sin6;
  2932. #endif
  2933. if (stcb) {
  2934. vrf_id = stcb->asoc.vrf_id;
  2935. } else {
  2936. return;
  2937. }
  2938. SCTP_IPI_ADDR_RLOCK();
  2939. vrf = sctp_find_vrf(vrf_id);
  2940. if (vrf == NULL) {
  2941. SCTP_IPI_ADDR_RUNLOCK();
  2942. return;
  2943. }
  2944. /* go through all our known interfaces */
  2945. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  2946. if (loopback_scope == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  2947. /* skip loopback interface */
  2948. continue;
  2949. }
  2950. /* go through each interface address */
  2951. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  2952. /* do i have it implicitly? */
  2953. if (sctp_cmpaddr(&sctp_ifa->address.sa, init_addr)) {
  2954. continue;
  2955. }
  2956. switch (sctp_ifa->address.sa.sa_family) {
  2957. #ifdef INET
  2958. case AF_INET:
  2959. sin = &sctp_ifa->address.sin;
  2960. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2961. if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
  2962. &sin->sin_addr) != 0) {
  2963. continue;
  2964. }
  2965. #endif
  2966. if ((ipv4_scope == 0) &&
  2967. (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
  2968. /* private address not in scope */
  2969. continue;
  2970. }
  2971. break;
  2972. #endif
  2973. #ifdef INET6
  2974. case AF_INET6:
  2975. sin6 = &sctp_ifa->address.sin6;
  2976. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2977. if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
  2978. &sin6->sin6_addr) != 0) {
  2979. continue;
  2980. }
  2981. #endif
  2982. if ((local_scope == 0) &&
  2983. (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
  2984. continue;
  2985. }
  2986. if ((site_scope == 0) &&
  2987. (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
  2988. continue;
  2989. }
  2990. break;
  2991. #endif
  2992. default:
  2993. break;
  2994. }
  2995. /* check to see if in the init-ack */
  2996. if (!sctp_addr_in_initack(m, offset, length, &sctp_ifa->address.sa)) {
  2997. /* try to add it */
  2998. sctp_addr_mgmt_assoc(stcb->sctp_ep, stcb,
  2999. sctp_ifa, SCTP_ADD_IP_ADDRESS,
  3000. SCTP_ADDR_LOCKED);
  3001. }
  3002. } /* end foreach ifa */
  3003. } /* end foreach ifn */
  3004. SCTP_IPI_ADDR_RUNLOCK();
  3005. }
  3006. /*
  3007. * validates an init-ack chunk (from a cookie-echo) with current addresses
  3008. * adds addresses from the init-ack into our local address list, if needed
  3009. * queues asconf adds/deletes addresses as needed and makes appropriate list
  3010. * changes for source address selection m, offset: points to the start of the
  3011. * address list in an init-ack chunk length: total length of the address
  3012. * params only init_addr: address where my INIT-ACK was sent from
  3013. */
  3014. void
  3015. sctp_check_address_list(struct sctp_tcb *stcb, struct mbuf *m, int offset,
  3016. int length, struct sockaddr *init_addr,
  3017. uint16_t local_scope, uint16_t site_scope,
  3018. uint16_t ipv4_scope, uint16_t loopback_scope)
  3019. {
  3020. /* process the local addresses in the initack */
  3021. sctp_process_initack_addresses(stcb, m, offset, length);
  3022. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  3023. /* bound all case */
  3024. sctp_check_address_list_all(stcb, m, offset, length, init_addr,
  3025. local_scope, site_scope, ipv4_scope, loopback_scope);
  3026. } else {
  3027. /* subset bound case */
  3028. if (sctp_is_feature_on(stcb->sctp_ep,
  3029. SCTP_PCB_FLAGS_DO_ASCONF)) {
  3030. /* asconf's allowed */
  3031. sctp_check_address_list_ep(stcb, m, offset, length,
  3032. init_addr);
  3033. }
  3034. /* else, no asconfs allowed, so what we sent is what we get */
  3035. }
  3036. }
  3037. /*
  3038. * sctp_bindx() support
  3039. */
  3040. uint32_t
  3041. sctp_addr_mgmt_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa,
  3042. uint32_t type, uint32_t vrf_id)
  3043. {
  3044. struct sctp_ifa *ifa;
  3045. struct sctp_laddr *laddr, *nladdr;
  3046. #ifdef HAVE_SA_LEN
  3047. if (sa->sa_len == 0) {
  3048. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
  3049. return (EINVAL);
  3050. }
  3051. #endif
  3052. if (type == SCTP_ADD_IP_ADDRESS) {
  3053. /* For an add the address MUST be on the system */
  3054. ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
  3055. } else if (type == SCTP_DEL_IP_ADDRESS) {
  3056. /* For a delete we need to find it in the inp */
  3057. ifa = sctp_find_ifa_in_ep(inp, sa, SCTP_ADDR_NOT_LOCKED);
  3058. } else {
  3059. ifa = NULL;
  3060. }
  3061. if (ifa != NULL) {
  3062. if (type == SCTP_ADD_IP_ADDRESS) {
  3063. sctp_add_local_addr_ep(inp, ifa, type);
  3064. } else if (type == SCTP_DEL_IP_ADDRESS) {
  3065. if (inp->laddr_count < 2) {
  3066. /* can't delete the last local address */
  3067. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EINVAL);
  3068. return (EINVAL);
  3069. }
  3070. LIST_FOREACH(laddr, &inp->sctp_addr_list,
  3071. sctp_nxt_addr) {
  3072. if (ifa == laddr->ifa) {
  3073. /* Mark in the delete */
  3074. laddr->action = type;
  3075. }
  3076. }
  3077. }
  3078. if (LIST_EMPTY(&inp->sctp_asoc_list)) {
  3079. /*
  3080. * There is no need to start the iterator if
  3081. * the inp has no associations.
  3082. */
  3083. if (type == SCTP_DEL_IP_ADDRESS) {
  3084. LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
  3085. if (laddr->ifa == ifa) {
  3086. sctp_del_local_addr_ep(inp, ifa);
  3087. }
  3088. }
  3089. }
  3090. } else {
  3091. struct sctp_asconf_iterator *asc;
  3092. struct sctp_laddr *wi;
  3093. int ret;
  3094. SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
  3095. sizeof(struct sctp_asconf_iterator),
  3096. SCTP_M_ASC_IT);
  3097. if (asc == NULL) {
  3098. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
  3099. return (ENOMEM);
  3100. }
  3101. wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  3102. if (wi == NULL) {
  3103. SCTP_FREE(asc, SCTP_M_ASC_IT);
  3104. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, ENOMEM);
  3105. return (ENOMEM);
  3106. }
  3107. LIST_INIT(&asc->list_of_work);
  3108. asc->cnt = 1;
  3109. SCTP_INCR_LADDR_COUNT();
  3110. wi->ifa = ifa;
  3111. wi->action = type;
  3112. atomic_add_int(&ifa->refcount, 1);
  3113. LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
  3114. ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
  3115. sctp_asconf_iterator_stcb,
  3116. sctp_asconf_iterator_ep_end,
  3117. SCTP_PCB_ANY_FLAGS,
  3118. SCTP_PCB_ANY_FEATURES,
  3119. SCTP_ASOC_ANY_STATE,
  3120. (void *)asc, 0,
  3121. sctp_asconf_iterator_end, inp, 0);
  3122. if (ret) {
  3123. SCTP_PRINTF("Failed to initiate iterator for addr_mgmt_ep_sa\n");
  3124. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EFAULT);
  3125. sctp_asconf_iterator_end(asc, 0);
  3126. return (EFAULT);
  3127. }
  3128. }
  3129. return (0);
  3130. } else {
  3131. /* invalid address! */
  3132. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_ASCONF, EADDRNOTAVAIL);
  3133. return (EADDRNOTAVAIL);
  3134. }
  3135. }
  3136. void
  3137. sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb, struct sctp_nets *net)
  3138. {
  3139. struct sctp_asconf_addr *aa_vtag, *aa_add, *aa_del;
  3140. struct sctp_ifa *sctp_ifap;
  3141. struct sctp_asconf_tag_param *vtag;
  3142. #ifdef INET
  3143. struct sockaddr_in *to;
  3144. #endif
  3145. #ifdef INET6
  3146. struct sockaddr_in6 *to6;
  3147. #endif
  3148. if (net == NULL) {
  3149. SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing net\n");
  3150. return;
  3151. }
  3152. if (stcb == NULL) {
  3153. SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: Missing stcb\n");
  3154. return;
  3155. }
  3156. /* Need to have in the ASCONF:
  3157. * - VTAG(my_vtag/peer_vtag)
  3158. * - ADD(wildcard)
  3159. * - DEL(wildcard)
  3160. * - ADD(Any global addresses)
  3161. */
  3162. SCTP_MALLOC(aa_vtag, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
  3163. SCTP_MALLOC(aa_add, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
  3164. SCTP_MALLOC(aa_del, struct sctp_asconf_addr *, sizeof(struct sctp_asconf_addr), SCTP_M_ASC_ADDR);
  3165. if ((aa_vtag == NULL) || (aa_add == NULL) || (aa_del == NULL)) {
  3166. /* Didn't get memory */
  3167. SCTPDBG(SCTP_DEBUG_ASCONF1, "sctp_asconf_send_nat_state_update: failed to get memory!\n");
  3168. out:
  3169. if (aa_vtag != NULL) {
  3170. SCTP_FREE(aa_vtag, SCTP_M_ASC_ADDR);
  3171. }
  3172. if (aa_add != NULL) {
  3173. SCTP_FREE(aa_add, SCTP_M_ASC_ADDR);
  3174. }
  3175. if (aa_del != NULL) {
  3176. SCTP_FREE(aa_del, SCTP_M_ASC_ADDR);
  3177. }
  3178. return;
  3179. }
  3180. memset(aa_vtag, 0, sizeof(struct sctp_asconf_addr));
  3181. aa_vtag->special_del = 0;
  3182. /* Fill in ASCONF address parameter fields. */
  3183. /* Top level elements are "networked" during send. */
  3184. aa_vtag->ifa = NULL;
  3185. aa_vtag->sent = 0; /* clear sent flag */
  3186. vtag = (struct sctp_asconf_tag_param *)&aa_vtag->ap.aph;
  3187. vtag->aph.ph.param_type = SCTP_NAT_VTAGS;
  3188. vtag->aph.ph.param_length = sizeof(struct sctp_asconf_tag_param);
  3189. vtag->local_vtag = htonl(stcb->asoc.my_vtag);
  3190. vtag->remote_vtag = htonl(stcb->asoc.peer_vtag);
  3191. memset(aa_add, 0, sizeof(struct sctp_asconf_addr));
  3192. memset(aa_del, 0, sizeof(struct sctp_asconf_addr));
  3193. switch (net->ro._l_addr.sa.sa_family) {
  3194. #ifdef INET
  3195. case AF_INET:
  3196. aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
  3197. aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
  3198. aa_add->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
  3199. aa_add->ap.addrp.ph.param_length = sizeof (struct sctp_ipv4addr_param);
  3200. /* No need to fill the address, we are using 0.0.0.0 */
  3201. aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
  3202. aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addrv4_param);
  3203. aa_del->ap.addrp.ph.param_type = SCTP_IPV4_ADDRESS;
  3204. aa_del->ap.addrp.ph.param_length = sizeof (struct sctp_ipv4addr_param);
  3205. /* No need to fill the address, we are using 0.0.0.0 */
  3206. break;
  3207. #endif
  3208. #ifdef INET6
  3209. case AF_INET6:
  3210. aa_add->ap.aph.ph.param_type = SCTP_ADD_IP_ADDRESS;
  3211. aa_add->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
  3212. aa_add->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
  3213. aa_add->ap.addrp.ph.param_length = sizeof (struct sctp_ipv6addr_param);
  3214. /* No need to fill the address, we are using ::0 */
  3215. aa_del->ap.aph.ph.param_type = SCTP_DEL_IP_ADDRESS;
  3216. aa_del->ap.aph.ph.param_length = sizeof(struct sctp_asconf_addr_param);
  3217. aa_del->ap.addrp.ph.param_type = SCTP_IPV6_ADDRESS;
  3218. aa_del->ap.addrp.ph.param_length = sizeof (struct sctp_ipv6addr_param);
  3219. /* No need to fill the address, we are using ::0 */
  3220. break;
  3221. #endif
  3222. default:
  3223. SCTPDBG(SCTP_DEBUG_ASCONF1,
  3224. "sctp_asconf_send_nat_state_update: unknown address family %d\n",
  3225. net->ro._l_addr.sa.sa_family);
  3226. goto out;
  3227. }
  3228. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_vtag, next);
  3229. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_add, next);
  3230. TAILQ_INSERT_TAIL(&stcb->asoc.asconf_queue, aa_del, next);
  3231. /* Now we must hunt the addresses and add all global addresses */
  3232. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  3233. struct sctp_vrf *vrf = NULL;
  3234. struct sctp_ifn *sctp_ifnp;
  3235. uint32_t vrf_id;
  3236. vrf_id = stcb->sctp_ep->def_vrf_id;
  3237. vrf = sctp_find_vrf(vrf_id);
  3238. if (vrf == NULL) {
  3239. goto skip_rest;
  3240. }
  3241. SCTP_IPI_ADDR_RLOCK();
  3242. LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
  3243. LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
  3244. switch (sctp_ifap->address.sa.sa_family) {
  3245. #ifdef INET
  3246. case AF_INET:
  3247. to = &sctp_ifap->address.sin;
  3248. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3249. if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
  3250. &to->sin_addr) != 0) {
  3251. continue;
  3252. }
  3253. #endif
  3254. if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
  3255. continue;
  3256. }
  3257. if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
  3258. continue;
  3259. }
  3260. break;
  3261. #endif
  3262. #ifdef INET6
  3263. case AF_INET6:
  3264. to6 = &sctp_ifap->address.sin6;
  3265. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3266. if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
  3267. &to6->sin6_addr) != 0) {
  3268. continue;
  3269. }
  3270. #endif
  3271. if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
  3272. continue;
  3273. }
  3274. if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
  3275. continue;
  3276. }
  3277. break;
  3278. #endif
  3279. default:
  3280. continue;
  3281. }
  3282. sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
  3283. }
  3284. }
  3285. SCTP_IPI_ADDR_RUNLOCK();
  3286. } else {
  3287. struct sctp_laddr *laddr;
  3288. LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
  3289. if (laddr->ifa == NULL) {
  3290. continue;
  3291. }
  3292. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
  3293. /* Address being deleted by the system, dont
  3294. * list.
  3295. */
  3296. continue;
  3297. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  3298. /* Address being deleted on this ep
  3299. * don't list.
  3300. */
  3301. continue;
  3302. }
  3303. sctp_ifap = laddr->ifa;
  3304. switch (sctp_ifap->address.sa.sa_family) {
  3305. #ifdef INET
  3306. case AF_INET:
  3307. to = &sctp_ifap->address.sin;
  3308. if (IN4_ISPRIVATE_ADDRESS(&to->sin_addr)) {
  3309. continue;
  3310. }
  3311. if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
  3312. continue;
  3313. }
  3314. break;
  3315. #endif
  3316. #ifdef INET6
  3317. case AF_INET6:
  3318. to6 = &sctp_ifap->address.sin6;
  3319. if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
  3320. continue;
  3321. }
  3322. if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
  3323. continue;
  3324. }
  3325. break;
  3326. #endif
  3327. default:
  3328. continue;
  3329. }
  3330. sctp_asconf_queue_mgmt(stcb, sctp_ifap, SCTP_ADD_IP_ADDRESS);
  3331. }
  3332. }
  3333. skip_rest:
  3334. /* Now we must send the asconf into the queue */
  3335. sctp_send_asconf(stcb, net, SCTP_ADDR_NOT_LOCKED);
  3336. }