sctp_bsd_addr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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_pcb.h>
  41. #include <netinet/sctp_header.h>
  42. #include <netinet/sctputil.h>
  43. #include <netinet/sctp_output.h>
  44. #include <netinet/sctp_bsd_addr.h>
  45. #include <netinet/sctp_uio.h>
  46. #include <netinet/sctputil.h>
  47. #include <netinet/sctp_timer.h>
  48. #include <netinet/sctp_asconf.h>
  49. #include <netinet/sctp_sysctl.h>
  50. #include <netinet/sctp_indata.h>
  51. #if defined(__FreeBSD__) && !defined(__Userspace__)
  52. #include <sys/unistd.h>
  53. #endif
  54. /* Declare all of our malloc named types */
  55. MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
  56. MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
  57. MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
  58. MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
  59. MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
  60. MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
  61. MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
  62. MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
  63. MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
  64. MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
  65. MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
  66. MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
  67. MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
  68. MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
  69. MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
  70. MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
  71. MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
  72. MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
  73. MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
  74. MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
  75. /* Global NON-VNET structure that controls the iterator */
  76. struct iterator_control sctp_it_ctl;
  77. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  78. static void
  79. sctp_cleanup_itqueue(void)
  80. {
  81. struct sctp_iterator *it, *nit;
  82. TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
  83. if (it->function_atend != NULL) {
  84. (*it->function_atend) (it->pointer, it->val);
  85. }
  86. TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
  87. SCTP_FREE(it, SCTP_M_ITER);
  88. }
  89. }
  90. #endif
  91. #if defined(__Userspace__)
  92. /*__Userspace__ TODO if we use thread based iterator
  93. * then the implementation of wakeup will need to change.
  94. * Currently we are using timeo_cond for ident so_timeo
  95. * but that is not sufficient if we need to use another ident
  96. * like wakeup(&sctppcbinfo.iterator_running);
  97. */
  98. #endif
  99. void
  100. sctp_wakeup_iterator(void)
  101. {
  102. #if defined(SCTP_PROCESS_LEVEL_LOCKS)
  103. #if defined(_WIN32)
  104. WakeAllConditionVariable(&sctp_it_ctl.iterator_wakeup);
  105. #else
  106. pthread_cond_broadcast(&sctp_it_ctl.iterator_wakeup);
  107. #endif
  108. #else
  109. wakeup(&sctp_it_ctl.iterator_running);
  110. #endif
  111. }
  112. #if defined(__Userspace__)
  113. static void *
  114. #else
  115. static void
  116. #endif
  117. sctp_iterator_thread(void *v SCTP_UNUSED)
  118. {
  119. #if defined(__Userspace__)
  120. sctp_userspace_set_threadname("SCTP iterator");
  121. #endif
  122. SCTP_IPI_ITERATOR_WQ_LOCK();
  123. /* In FreeBSD this thread never terminates. */
  124. #if defined(__FreeBSD__) && !defined(__Userspace__)
  125. for (;;) {
  126. #else
  127. while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) == 0) {
  128. #endif
  129. #if !defined(__Userspace__)
  130. msleep(&sctp_it_ctl.iterator_running,
  131. #if defined(__FreeBSD__)
  132. &sctp_it_ctl.ipi_iterator_wq_mtx,
  133. #elif defined(__APPLE__)
  134. sctp_it_ctl.ipi_iterator_wq_mtx,
  135. #endif
  136. 0, "waiting_for_work", 0);
  137. #else
  138. #if defined(_WIN32)
  139. SleepConditionVariableCS(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx, INFINITE);
  140. #else
  141. pthread_cond_wait(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx);
  142. #endif
  143. #endif
  144. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  145. if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
  146. break;
  147. }
  148. #endif
  149. sctp_iterator_worker();
  150. }
  151. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  152. /* Now this thread needs to be terminated */
  153. sctp_cleanup_itqueue();
  154. sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_EXITED;
  155. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  156. #if defined(__Userspace__)
  157. sctp_wakeup_iterator();
  158. return (NULL);
  159. #else
  160. wakeup(&sctp_it_ctl.iterator_flags);
  161. thread_terminate(current_thread());
  162. #ifdef INVARIANTS
  163. panic("Hmm. thread_terminate() continues...");
  164. #endif
  165. #endif
  166. #endif
  167. }
  168. void
  169. sctp_startup_iterator(void)
  170. {
  171. if (sctp_it_ctl.thread_proc) {
  172. /* You only get one */
  173. return;
  174. }
  175. /* Initialize global locks here, thus only once. */
  176. SCTP_ITERATOR_LOCK_INIT();
  177. SCTP_IPI_ITERATOR_WQ_INIT();
  178. TAILQ_INIT(&sctp_it_ctl.iteratorhead);
  179. #if defined(__Userspace__)
  180. if (sctp_userspace_thread_create(&sctp_it_ctl.thread_proc, &sctp_iterator_thread)) {
  181. SCTP_PRINTF("ERROR: Creating sctp_iterator_thread failed.\n");
  182. } else {
  183. SCTP_BASE_VAR(iterator_thread_started) = 1;
  184. }
  185. #elif defined(__FreeBSD__)
  186. kproc_create(sctp_iterator_thread,
  187. (void *)NULL,
  188. &sctp_it_ctl.thread_proc,
  189. 0,
  190. SCTP_KTHREAD_PAGES,
  191. SCTP_KTRHEAD_NAME);
  192. #elif defined(__APPLE__)
  193. kernel_thread_start((thread_continue_t)sctp_iterator_thread, NULL, &sctp_it_ctl.thread_proc);
  194. #endif
  195. }
  196. #ifdef INET6
  197. #if defined(__Userspace__)
  198. /* __Userspace__ TODO. struct in6_ifaddr is defined in sys/netinet6/in6_var.h
  199. ip6_use_deprecated is defined as int ip6_use_deprecated = 1; in /src/sys/netinet6/in6_proto.c
  200. */
  201. void
  202. sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
  203. {
  204. return; /* stub */
  205. }
  206. #else
  207. void
  208. sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
  209. {
  210. struct in6_ifaddr *ifa6;
  211. ifa6 = (struct in6_ifaddr *)ifa->ifa;
  212. ifa->flags = ifa6->ia6_flags;
  213. if (!MODULE_GLOBAL(ip6_use_deprecated)) {
  214. if (ifa->flags &
  215. IN6_IFF_DEPRECATED) {
  216. ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
  217. } else {
  218. ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
  219. }
  220. } else {
  221. ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
  222. }
  223. if (ifa->flags &
  224. (IN6_IFF_DETACHED |
  225. IN6_IFF_ANYCAST |
  226. IN6_IFF_NOTREADY)) {
  227. ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
  228. } else {
  229. ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
  230. }
  231. }
  232. #endif /* __Userspace__ */
  233. #endif /* INET6 */
  234. #if !defined(__Userspace__)
  235. static uint32_t
  236. sctp_is_desired_interface_type(struct ifnet *ifn)
  237. {
  238. int result;
  239. /* check the interface type to see if it's one we care about */
  240. #if defined(__APPLE__) && !defined(__Userspace__)
  241. switch(ifnet_type(ifn)) {
  242. #else
  243. switch (ifn->if_type) {
  244. #endif
  245. case IFT_ETHER:
  246. case IFT_ISO88023:
  247. case IFT_ISO88024:
  248. case IFT_ISO88025:
  249. case IFT_ISO88026:
  250. case IFT_STARLAN:
  251. case IFT_P10:
  252. case IFT_P80:
  253. case IFT_HY:
  254. case IFT_FDDI:
  255. case IFT_XETHER:
  256. case IFT_ISDNBASIC:
  257. case IFT_ISDNPRIMARY:
  258. case IFT_PTPSERIAL:
  259. case IFT_OTHER:
  260. case IFT_PPP:
  261. case IFT_LOOP:
  262. case IFT_SLIP:
  263. case IFT_GIF:
  264. case IFT_L2VLAN:
  265. case IFT_STF:
  266. #if !(defined(__APPLE__) && !defined(__Userspace__))
  267. case IFT_IP:
  268. case IFT_IPOVERCDLC:
  269. case IFT_IPOVERCLAW:
  270. case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
  271. case IFT_VIRTUALIPADDRESS:
  272. #endif
  273. result = 1;
  274. break;
  275. default:
  276. result = 0;
  277. }
  278. return (result);
  279. }
  280. #endif
  281. #if defined(__APPLE__) && !defined(__Userspace__)
  282. int
  283. sctp_is_vmware_interface(struct ifnet *ifn)
  284. {
  285. return (strncmp(ifnet_name(ifn), "vmnet", 5) == 0);
  286. }
  287. #endif
  288. #if defined(_WIN32) && defined(__Userspace__)
  289. #ifdef MALLOC
  290. #undef MALLOC
  291. #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
  292. #endif
  293. #ifdef FREE
  294. #undef FREE
  295. #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
  296. #endif
  297. static void
  298. sctp_init_ifns_for_vrf(int vrfid)
  299. {
  300. #if defined(INET) || defined(INET6)
  301. struct sctp_ifa *sctp_ifa;
  302. DWORD Err, AdapterAddrsSize;
  303. PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
  304. PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
  305. #endif
  306. #ifdef INET
  307. AdapterAddrsSize = 0;
  308. if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
  309. if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
  310. SCTP_PRINTF("GetAdaptersV4Addresses() sizing failed with error code %d\n", Err);
  311. SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
  312. return;
  313. }
  314. }
  315. /* Allocate memory from sizing information */
  316. if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
  317. SCTP_PRINTF("Memory allocation error!\n");
  318. return;
  319. }
  320. /* Get actual adapter information */
  321. if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
  322. SCTP_PRINTF("GetAdaptersV4Addresses() failed with error code %d\n", Err);
  323. FREE(pAdapterAddrs);
  324. return;
  325. }
  326. /* Enumerate through each returned adapter and save its information */
  327. for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
  328. if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
  329. for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
  330. if (IN4_ISLINKLOCAL_ADDRESS(&(((struct sockaddr_in *)(pUnicast->Address.lpSockaddr))->sin_addr))) {
  331. continue;
  332. }
  333. sctp_ifa = sctp_add_addr_to_vrf(0,
  334. NULL,
  335. pAdapt->IfIndex,
  336. (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
  337. pAdapt->AdapterName,
  338. NULL,
  339. pUnicast->Address.lpSockaddr,
  340. pAdapt->Flags,
  341. 0);
  342. if (sctp_ifa) {
  343. sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  344. }
  345. }
  346. }
  347. }
  348. FREE(pAdapterAddrs);
  349. #endif
  350. #ifdef INET6
  351. AdapterAddrsSize = 0;
  352. if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
  353. if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
  354. SCTP_PRINTF("GetAdaptersV6Addresses() sizing failed with error code %d\n", Err);
  355. SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
  356. return;
  357. }
  358. }
  359. /* Allocate memory from sizing information */
  360. if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
  361. SCTP_PRINTF("Memory allocation error!\n");
  362. return;
  363. }
  364. /* Get actual adapter information */
  365. if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
  366. SCTP_PRINTF("GetAdaptersV6Addresses() failed with error code %d\n", Err);
  367. FREE(pAdapterAddrs);
  368. return;
  369. }
  370. /* Enumerate through each returned adapter and save its information */
  371. for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
  372. if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
  373. for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
  374. sctp_ifa = sctp_add_addr_to_vrf(0,
  375. NULL,
  376. pAdapt->Ipv6IfIndex,
  377. (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
  378. pAdapt->AdapterName,
  379. NULL,
  380. pUnicast->Address.lpSockaddr,
  381. pAdapt->Flags,
  382. 0);
  383. if (sctp_ifa) {
  384. sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  385. }
  386. }
  387. }
  388. }
  389. FREE(pAdapterAddrs);
  390. #endif
  391. }
  392. #elif defined(__Userspace__)
  393. static void
  394. sctp_init_ifns_for_vrf(int vrfid)
  395. {
  396. #if defined(INET) || defined(INET6)
  397. int rc;
  398. struct ifaddrs *ifa, *ifas;
  399. struct sctp_ifa *sctp_ifa;
  400. uint32_t ifa_flags;
  401. rc = getifaddrs(&ifas);
  402. if (rc != 0) {
  403. return;
  404. }
  405. for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
  406. if (ifa->ifa_addr == NULL) {
  407. continue;
  408. }
  409. #if !defined(INET)
  410. if (ifa->ifa_addr->sa_family != AF_INET6) {
  411. /* non inet6 skip */
  412. continue;
  413. }
  414. #elif !defined(INET6)
  415. if (ifa->ifa_addr->sa_family != AF_INET) {
  416. /* non inet skip */
  417. continue;
  418. }
  419. #else
  420. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
  421. /* non inet/inet6 skip */
  422. continue;
  423. }
  424. #endif
  425. #if defined(INET6)
  426. if ((ifa->ifa_addr->sa_family == AF_INET6) &&
  427. IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
  428. /* skip unspecified addresses */
  429. continue;
  430. }
  431. #endif
  432. #if defined(INET)
  433. if (ifa->ifa_addr->sa_family == AF_INET &&
  434. ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
  435. continue;
  436. }
  437. #endif
  438. ifa_flags = 0;
  439. sctp_ifa = sctp_add_addr_to_vrf(vrfid,
  440. NULL,
  441. if_nametoindex(ifa->ifa_name),
  442. 0,
  443. ifa->ifa_name,
  444. NULL,
  445. ifa->ifa_addr,
  446. ifa_flags,
  447. 0);
  448. if (sctp_ifa) {
  449. sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  450. }
  451. }
  452. freeifaddrs(ifas);
  453. #endif
  454. }
  455. #endif
  456. #if defined(__APPLE__) && !defined(__Userspace__)
  457. static void
  458. sctp_init_ifns_for_vrf(int vrfid)
  459. {
  460. /* Here we must apply ANY locks needed by the
  461. * IFN we access and also make sure we lock
  462. * any IFA that exists as we float through the
  463. * list of IFA's
  464. */
  465. struct ifnet **ifnetlist;
  466. uint32_t i, j, count;
  467. char name[SCTP_IFNAMSIZ];
  468. struct ifnet *ifn;
  469. struct ifaddr **ifaddrlist;
  470. struct ifaddr *ifa;
  471. struct in6_ifaddr *ifa6;
  472. struct sctp_ifa *sctp_ifa;
  473. uint32_t ifa_flags;
  474. if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
  475. return;
  476. }
  477. for (i = 0; i < count; i++) {
  478. ifn = ifnetlist[i];
  479. if (SCTP_BASE_SYSCTL(sctp_ignore_vmware_interfaces) && sctp_is_vmware_interface(ifn)) {
  480. continue;
  481. }
  482. if (sctp_is_desired_interface_type(ifn) == 0) {
  483. /* non desired type */
  484. continue;
  485. }
  486. if (ifnet_get_address_list(ifn, &ifaddrlist) != 0) {
  487. continue;
  488. }
  489. for (j = 0; ifaddrlist[j] != NULL; j++) {
  490. ifa = ifaddrlist[j];
  491. if (ifa->ifa_addr == NULL) {
  492. continue;
  493. }
  494. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
  495. /* non inet/inet6 skip */
  496. continue;
  497. }
  498. if (ifa->ifa_addr->sa_family == AF_INET6) {
  499. if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
  500. /* skip unspecified addresses */
  501. continue;
  502. }
  503. } else {
  504. if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) {
  505. continue;
  506. }
  507. }
  508. if (ifa->ifa_addr->sa_family == AF_INET6) {
  509. ifa6 = (struct in6_ifaddr *)ifa;
  510. ifa_flags = ifa6->ia6_flags;
  511. } else {
  512. ifa_flags = 0;
  513. }
  514. SCTP_SNPRINTF(name, SCTP_IFNAMSIZ, "%s%d", ifnet_name(ifn), ifnet_unit(ifn));
  515. sctp_ifa = sctp_add_addr_to_vrf(vrfid,
  516. (void *)ifn, /* XXX */
  517. ifnet_index(ifn),
  518. ifnet_type(ifn),
  519. name,
  520. (void *)ifa, /* XXX */
  521. ifa->ifa_addr,
  522. ifa_flags,
  523. 0);
  524. if (sctp_ifa) {
  525. sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  526. }
  527. }
  528. ifnet_free_address_list(ifaddrlist);
  529. }
  530. ifnet_list_free(ifnetlist);
  531. }
  532. #endif
  533. #if defined(__FreeBSD__) && !defined(__Userspace__)
  534. static void
  535. sctp_init_ifns_for_vrf(int vrfid)
  536. {
  537. /* Here we must apply ANY locks needed by the
  538. * IFN we access and also make sure we lock
  539. * any IFA that exists as we float through the
  540. * list of IFA's
  541. */
  542. struct epoch_tracker et;
  543. struct ifnet *ifn;
  544. struct ifaddr *ifa;
  545. struct sctp_ifa *sctp_ifa;
  546. uint32_t ifa_flags;
  547. #ifdef INET6
  548. struct in6_ifaddr *ifa6;
  549. #endif
  550. IFNET_RLOCK();
  551. NET_EPOCH_ENTER(et);
  552. CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
  553. if (sctp_is_desired_interface_type(ifn) == 0) {
  554. /* non desired type */
  555. continue;
  556. }
  557. CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
  558. if (ifa->ifa_addr == NULL) {
  559. continue;
  560. }
  561. switch (ifa->ifa_addr->sa_family) {
  562. #ifdef INET
  563. case AF_INET:
  564. if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
  565. continue;
  566. }
  567. break;
  568. #endif
  569. #ifdef INET6
  570. case AF_INET6:
  571. if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
  572. /* skip unspecified addresses */
  573. continue;
  574. }
  575. break;
  576. #endif
  577. default:
  578. continue;
  579. }
  580. switch (ifa->ifa_addr->sa_family) {
  581. #ifdef INET
  582. case AF_INET:
  583. ifa_flags = 0;
  584. break;
  585. #endif
  586. #ifdef INET6
  587. case AF_INET6:
  588. ifa6 = (struct in6_ifaddr *)ifa;
  589. ifa_flags = ifa6->ia6_flags;
  590. break;
  591. #endif
  592. default:
  593. ifa_flags = 0;
  594. break;
  595. }
  596. sctp_ifa = sctp_add_addr_to_vrf(vrfid,
  597. (void *)ifn,
  598. ifn->if_index,
  599. ifn->if_type,
  600. ifn->if_xname,
  601. (void *)ifa,
  602. ifa->ifa_addr,
  603. ifa_flags,
  604. 0);
  605. if (sctp_ifa) {
  606. sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  607. }
  608. }
  609. }
  610. NET_EPOCH_EXIT(et);
  611. IFNET_RUNLOCK();
  612. }
  613. #endif
  614. void
  615. sctp_init_vrf_list(int vrfid)
  616. {
  617. if (vrfid > SCTP_MAX_VRF_ID)
  618. /* can't do that */
  619. return;
  620. /* Don't care about return here */
  621. (void)sctp_allocate_vrf(vrfid);
  622. /* Now we need to build all the ifn's
  623. * for this vrf and there addresses
  624. */
  625. sctp_init_ifns_for_vrf(vrfid);
  626. }
  627. void
  628. sctp_addr_change(struct ifaddr *ifa, int cmd)
  629. {
  630. #if defined(__Userspace__)
  631. return;
  632. #else
  633. uint32_t ifa_flags = 0;
  634. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  635. return;
  636. }
  637. /* BSD only has one VRF, if this changes
  638. * we will need to hook in the right
  639. * things here to get the id to pass to
  640. * the address management routine.
  641. */
  642. if (SCTP_BASE_VAR(first_time) == 0) {
  643. /* Special test to see if my ::1 will showup with this */
  644. SCTP_BASE_VAR(first_time) = 1;
  645. sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
  646. }
  647. if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
  648. /* don't know what to do with this */
  649. return;
  650. }
  651. if (ifa->ifa_addr == NULL) {
  652. return;
  653. }
  654. if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
  655. /* non desired type */
  656. return;
  657. }
  658. switch (ifa->ifa_addr->sa_family) {
  659. #ifdef INET
  660. case AF_INET:
  661. if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
  662. return;
  663. }
  664. break;
  665. #endif
  666. #ifdef INET6
  667. case AF_INET6:
  668. ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
  669. if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
  670. /* skip unspecified addresses */
  671. return;
  672. }
  673. break;
  674. #endif
  675. default:
  676. /* non inet/inet6 skip */
  677. return;
  678. }
  679. if (cmd == RTM_ADD) {
  680. (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
  681. #if defined(__APPLE__) && !defined(__Userspace__)
  682. ifnet_index(ifa->ifa_ifp), ifnet_type(ifa->ifa_ifp), ifnet_name(ifa->ifa_ifp),
  683. #else
  684. ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
  685. #endif
  686. (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
  687. } else {
  688. sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
  689. #if defined(__APPLE__) && !defined(__Userspace__)
  690. ifnet_index(ifa->ifa_ifp),
  691. ifnet_name(ifa->ifa_ifp));
  692. #else
  693. ifa->ifa_ifp->if_index,
  694. ifa->ifa_ifp->if_xname);
  695. #endif
  696. /* We don't bump refcount here so when it completes
  697. * the final delete will happen.
  698. */
  699. }
  700. #endif
  701. }
  702. #if defined(__FreeBSD__) && !defined(__Userspace__)
  703. void
  704. sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd) {
  705. sctp_addr_change(ifa, cmd);
  706. }
  707. #endif
  708. #if defined(__APPLE__) && !defined(__Userspace__)
  709. void
  710. sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add)
  711. {
  712. struct ifnet **ifnetlist;
  713. struct ifaddr **ifaddrlist;
  714. uint32_t i, j, count;
  715. if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
  716. return;
  717. }
  718. for (i = 0; i < count; i++) {
  719. if (!(*pred)(ifnetlist[i])) {
  720. continue;
  721. }
  722. if (ifnet_get_address_list(ifnetlist[i], &ifaddrlist) != 0) {
  723. continue;
  724. }
  725. for (j = 0; ifaddrlist[j] != NULL; j++) {
  726. sctp_addr_change(ifaddrlist[j], add ? RTM_ADD : RTM_DELETE);
  727. }
  728. ifnet_free_address_list(ifaddrlist);
  729. }
  730. ifnet_list_free(ifnetlist);
  731. return;
  732. }
  733. #endif
  734. struct mbuf *
  735. sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
  736. int how, int allonebuf, int type)
  737. {
  738. struct mbuf *m = NULL;
  739. #if defined(__FreeBSD__) || defined(__Userspace__)
  740. #if defined(__Userspace__)
  741. m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0, allonebuf);
  742. #else
  743. m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
  744. #endif
  745. if (m == NULL) {
  746. /* bad, no memory */
  747. return (m);
  748. }
  749. #if !defined(__Userspace__)
  750. if (allonebuf) {
  751. if (SCTP_BUF_SIZE(m) < space_needed) {
  752. m_freem(m);
  753. return (NULL);
  754. }
  755. KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __func__));
  756. }
  757. #endif
  758. #ifdef SCTP_MBUF_LOGGING
  759. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  760. sctp_log_mb(m, SCTP_MBUF_IALLOC);
  761. }
  762. #endif
  763. #else
  764. int mbuf_threshold;
  765. unsigned int size;
  766. if (want_header) {
  767. MGETHDR(m, how, type);
  768. size = MHLEN;
  769. } else {
  770. MGET(m, how, type);
  771. size = MLEN;
  772. }
  773. if (m == NULL) {
  774. return (NULL);
  775. }
  776. if (allonebuf == 0) {
  777. mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
  778. } else {
  779. mbuf_threshold = 1;
  780. }
  781. if (space_needed > (unsigned int)(((mbuf_threshold - 1) * MLEN) + MHLEN)) {
  782. MCLGET(m, how);
  783. if (m == NULL) {
  784. return (NULL);
  785. }
  786. if (SCTP_BUF_IS_EXTENDED(m) == 0) {
  787. sctp_m_freem(m);
  788. return (NULL);
  789. }
  790. size = SCTP_BUF_EXTEND_SIZE(m);
  791. }
  792. if (allonebuf != 0 && size < space_needed) {
  793. m_freem(m);
  794. return (NULL);
  795. }
  796. SCTP_BUF_LEN(m) = 0;
  797. SCTP_BUF_NEXT(m) = SCTP_BUF_NEXT_PKT(m) = NULL;
  798. #ifdef SCTP_MBUF_LOGGING
  799. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  800. sctp_log_mb(m, SCTP_MBUF_IALLOC);
  801. }
  802. #endif
  803. #endif
  804. return (m);
  805. }
  806. #ifdef SCTP_PACKET_LOGGING
  807. void
  808. sctp_packet_log(struct mbuf *m)
  809. {
  810. int *lenat, thisone;
  811. void *copyto;
  812. uint32_t *tick_tock;
  813. int length;
  814. int total_len;
  815. int grabbed_lock = 0;
  816. int value, newval, thisend, thisbegin;
  817. /*
  818. * Buffer layout.
  819. * -sizeof this entry (total_len)
  820. * -previous end (value)
  821. * -ticks of log (ticks)
  822. * o -ip packet
  823. * o -as logged
  824. * - where this started (thisbegin)
  825. * x <--end points here
  826. */
  827. length = SCTP_HEADER_LEN(m);
  828. total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
  829. /* Log a packet to the buffer. */
  830. if (total_len> SCTP_PACKET_LOG_SIZE) {
  831. /* Can't log this packet I have not a buffer big enough */
  832. return;
  833. }
  834. if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
  835. return;
  836. }
  837. atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
  838. try_again:
  839. if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
  840. SCTP_IP_PKTLOG_LOCK();
  841. grabbed_lock = 1;
  842. again_locked:
  843. value = SCTP_BASE_VAR(packet_log_end);
  844. newval = SCTP_BASE_VAR(packet_log_end) + total_len;
  845. if (newval >= SCTP_PACKET_LOG_SIZE) {
  846. /* we wrapped */
  847. thisbegin = 0;
  848. thisend = total_len;
  849. } else {
  850. thisbegin = SCTP_BASE_VAR(packet_log_end);
  851. thisend = newval;
  852. }
  853. if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
  854. goto again_locked;
  855. }
  856. } else {
  857. value = SCTP_BASE_VAR(packet_log_end);
  858. newval = SCTP_BASE_VAR(packet_log_end) + total_len;
  859. if (newval >= SCTP_PACKET_LOG_SIZE) {
  860. /* we wrapped */
  861. thisbegin = 0;
  862. thisend = total_len;
  863. } else {
  864. thisbegin = SCTP_BASE_VAR(packet_log_end);
  865. thisend = newval;
  866. }
  867. if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
  868. goto try_again;
  869. }
  870. }
  871. /* Sanity check */
  872. if (thisend >= SCTP_PACKET_LOG_SIZE) {
  873. SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
  874. thisbegin,
  875. thisend,
  876. SCTP_BASE_VAR(packet_log_writers),
  877. grabbed_lock,
  878. SCTP_BASE_VAR(packet_log_end));
  879. SCTP_BASE_VAR(packet_log_end) = 0;
  880. goto no_log;
  881. }
  882. lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
  883. *lenat = total_len;
  884. lenat++;
  885. *lenat = value;
  886. lenat++;
  887. tick_tock = (uint32_t *)lenat;
  888. lenat++;
  889. *tick_tock = sctp_get_tick_count();
  890. copyto = (void *)lenat;
  891. thisone = thisend - sizeof(int);
  892. lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
  893. *lenat = thisbegin;
  894. if (grabbed_lock) {
  895. SCTP_IP_PKTLOG_UNLOCK();
  896. grabbed_lock = 0;
  897. }
  898. m_copydata(m, 0, length, (caddr_t)copyto);
  899. no_log:
  900. if (grabbed_lock) {
  901. SCTP_IP_PKTLOG_UNLOCK();
  902. }
  903. atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
  904. }
  905. int
  906. sctp_copy_out_packet_log(uint8_t *target, int length)
  907. {
  908. /* We wind through the packet log starting at
  909. * start copying up to length bytes out.
  910. * We return the number of bytes copied.
  911. */
  912. int this_copy;
  913. int *lenat;
  914. int did_delay = 0;
  915. if (length < (int)(2 * sizeof(int))) {
  916. /* not enough room */
  917. return (0);
  918. }
  919. if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
  920. atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
  921. again:
  922. if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
  923. /* we delay here for just a moment hoping the writer(s) that were
  924. * present when we entered will have left and we only have
  925. * locking ones that will contend with us for the lock. This
  926. * does not assure 100% access, but its good enough for
  927. * a logging facility like this.
  928. */
  929. did_delay = 1;
  930. DELAY(10);
  931. goto again;
  932. }
  933. }
  934. SCTP_IP_PKTLOG_LOCK();
  935. lenat = (int *)target;
  936. *lenat = SCTP_BASE_VAR(packet_log_end);
  937. lenat++;
  938. this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
  939. memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
  940. if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
  941. atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
  942. SCTP_PKTLOG_WRITERS_NEED_LOCK);
  943. }
  944. SCTP_IP_PKTLOG_UNLOCK();
  945. return (this_copy + sizeof(int));
  946. }
  947. #endif