net_io.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1993-1989 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * Author: David B. Golub, Carnegie Mellon University
  28. * Date: 3/98
  29. *
  30. * Network IO.
  31. *
  32. * Packet filter code taken from vaxif/enet.c written
  33. * CMU and Stanford.
  34. */
  35. /*
  36. * Note: don't depend on anything in this file.
  37. * It may change a lot real soon. -cmaeda 11 June 1993
  38. */
  39. #include <sys/types.h>
  40. #include <string.h>
  41. #include <device/net_status.h>
  42. #include <machine/machspl.h> /* spl definitions */
  43. #include <device/net_io.h>
  44. #include <device/if_hdr.h>
  45. #include <device/io_req.h>
  46. #include <device/ds_routines.h>
  47. #include <mach/boolean.h>
  48. #include <mach/vm_param.h>
  49. #include <ipc/ipc_port.h>
  50. #include <ipc/ipc_kmsg.h>
  51. #include <ipc/ipc_mqueue.h>
  52. #include <kern/counters.h>
  53. #include <kern/debug.h>
  54. #include <kern/lock.h>
  55. #include <kern/printf.h>
  56. #include <kern/queue.h>
  57. #include <kern/sched_prim.h>
  58. #include <kern/slab.h>
  59. #include <kern/thread.h>
  60. #include <machine/machspl.h>
  61. #if MACH_TTD
  62. #include <ttd/ttd_stub.h>
  63. #endif /* MACH_TTD */
  64. #if MACH_TTD
  65. int kttd_async_counter= 0;
  66. #endif /* MACH_TTD */
  67. /*
  68. * Packet Buffer Management
  69. *
  70. * This module manages a private pool of kmsg buffers.
  71. */
  72. /*
  73. * List of net kmsgs queued to be sent to users.
  74. * Messages can be high priority or low priority.
  75. * The network thread processes high priority messages first.
  76. */
  77. decl_simple_lock_data(,net_queue_lock)
  78. boolean_t net_thread_awake = FALSE;
  79. struct ipc_kmsg_queue net_queue_high;
  80. int net_queue_high_size = 0;
  81. int net_queue_high_max = 0; /* for debugging */
  82. struct ipc_kmsg_queue net_queue_low;
  83. int net_queue_low_size = 0;
  84. int net_queue_low_max = 0; /* for debugging */
  85. /*
  86. * List of net kmsgs that can be touched at interrupt level.
  87. * If it is empty, we will also steal low priority messages.
  88. */
  89. decl_simple_lock_data(,net_queue_free_lock)
  90. struct ipc_kmsg_queue net_queue_free;
  91. int net_queue_free_size = 0; /* on free list */
  92. int net_queue_free_max = 0; /* for debugging */
  93. /*
  94. * This value is critical to network performance.
  95. * At least this many buffers should be sitting in net_queue_free.
  96. * If this is set too small, we will drop network packets.
  97. * Even a low drop rate (<1%) can cause severe network throughput problems.
  98. * We add one to net_queue_free_min for every filter.
  99. */
  100. int net_queue_free_min = 3;
  101. int net_queue_free_hits = 0; /* for debugging */
  102. int net_queue_free_steals = 0; /* for debugging */
  103. int net_queue_free_misses = 0; /* for debugging */
  104. int net_kmsg_send_high_hits = 0; /* for debugging */
  105. int net_kmsg_send_low_hits = 0; /* for debugging */
  106. int net_kmsg_send_high_misses = 0; /* for debugging */
  107. int net_kmsg_send_low_misses = 0; /* for debugging */
  108. int net_thread_awaken = 0; /* for debugging */
  109. int net_ast_taken = 0; /* for debugging */
  110. decl_simple_lock_data(,net_kmsg_total_lock)
  111. int net_kmsg_total = 0; /* total allocated */
  112. int net_kmsg_max; /* initialized below */
  113. vm_size_t net_kmsg_size; /* initialized below */
  114. /*
  115. * We want more buffers when there aren't enough in the free queue
  116. * and the low priority queue. However, we don't want to allocate
  117. * more than net_kmsg_max.
  118. */
  119. #define net_kmsg_want_more() \
  120. (((net_queue_free_size + net_queue_low_size) < net_queue_free_min) && \
  121. (net_kmsg_total < net_kmsg_max))
  122. ipc_kmsg_t
  123. net_kmsg_get(void)
  124. {
  125. ipc_kmsg_t kmsg;
  126. spl_t s;
  127. /*
  128. * First check the list of free buffers.
  129. */
  130. s = splimp();
  131. simple_lock(&net_queue_free_lock);
  132. kmsg = ipc_kmsg_queue_first(&net_queue_free);
  133. if (kmsg != IKM_NULL) {
  134. ipc_kmsg_rmqueue_first_macro(&net_queue_free, kmsg);
  135. net_queue_free_size--;
  136. net_queue_free_hits++;
  137. }
  138. simple_unlock(&net_queue_free_lock);
  139. if (kmsg == IKM_NULL) {
  140. /*
  141. * Try to steal from the low priority queue.
  142. */
  143. simple_lock(&net_queue_lock);
  144. kmsg = ipc_kmsg_queue_first(&net_queue_low);
  145. if (kmsg != IKM_NULL) {
  146. ipc_kmsg_rmqueue_first_macro(&net_queue_low, kmsg);
  147. net_queue_low_size--;
  148. net_queue_free_steals++;
  149. }
  150. simple_unlock(&net_queue_lock);
  151. }
  152. if (kmsg == IKM_NULL)
  153. net_queue_free_misses++;
  154. (void) splx(s);
  155. if (net_kmsg_want_more() || (kmsg == IKM_NULL)) {
  156. boolean_t awake;
  157. s = splimp();
  158. simple_lock(&net_queue_lock);
  159. awake = net_thread_awake;
  160. net_thread_awake = TRUE;
  161. simple_unlock(&net_queue_lock);
  162. (void) splx(s);
  163. if (!awake)
  164. thread_wakeup((event_t) &net_thread_awake);
  165. }
  166. return kmsg;
  167. }
  168. void
  169. net_kmsg_put(const ipc_kmsg_t kmsg)
  170. {
  171. spl_t s;
  172. s = splimp();
  173. simple_lock(&net_queue_free_lock);
  174. ipc_kmsg_enqueue_macro(&net_queue_free, kmsg);
  175. if (++net_queue_free_size > net_queue_free_max)
  176. net_queue_free_max = net_queue_free_size;
  177. simple_unlock(&net_queue_free_lock);
  178. (void) splx(s);
  179. }
  180. void
  181. net_kmsg_collect(void)
  182. {
  183. ipc_kmsg_t kmsg;
  184. spl_t s;
  185. s = splimp();
  186. simple_lock(&net_queue_free_lock);
  187. while (net_queue_free_size > net_queue_free_min) {
  188. kmsg = ipc_kmsg_dequeue(&net_queue_free);
  189. net_queue_free_size--;
  190. simple_unlock(&net_queue_free_lock);
  191. (void) splx(s);
  192. net_kmsg_free(kmsg);
  193. simple_lock(&net_kmsg_total_lock);
  194. net_kmsg_total--;
  195. simple_unlock(&net_kmsg_total_lock);
  196. s = splimp();
  197. simple_lock(&net_queue_free_lock);
  198. }
  199. simple_unlock(&net_queue_free_lock);
  200. (void) splx(s);
  201. }
  202. void
  203. net_kmsg_more(void)
  204. {
  205. ipc_kmsg_t kmsg;
  206. /*
  207. * Replenish net kmsg pool if low. We don't have the locks
  208. * necessary to look at these variables, but that's OK because
  209. * misread values aren't critical. The danger in this code is
  210. * that while we allocate buffers, interrupts are happening
  211. * which take buffers out of the free list. If we are not
  212. * careful, we will sit in the loop and allocate a zillion
  213. * buffers while a burst of packets arrives. So we count
  214. * buffers in the low priority queue as available, because
  215. * net_kmsg_get will make use of them, and we cap the total
  216. * number of buffers we are willing to allocate.
  217. */
  218. while (net_kmsg_want_more()) {
  219. simple_lock(&net_kmsg_total_lock);
  220. net_kmsg_total++;
  221. simple_unlock(&net_kmsg_total_lock);
  222. kmsg = net_kmsg_alloc();
  223. net_kmsg_put(kmsg);
  224. }
  225. }
  226. /*
  227. * Packet Filter Data Structures
  228. *
  229. * Each network interface has a set of packet filters
  230. * that are run on incoming packets.
  231. *
  232. * Each packet filter may represent a single network
  233. * session or multiple network sessions. For example,
  234. * all application level TCP sessions would be represented
  235. * by a single packet filter data structure.
  236. *
  237. * If a packet filter has a single session, we use a
  238. * struct net_rcv_port to represent it. If the packet
  239. * filter represents multiple sessions, we use a
  240. * struct net_hash_header to represent it.
  241. */
  242. /*
  243. * Each interface has a write port and a set of read ports.
  244. * Each read port has one or more filters to determine what packets
  245. * should go to that port.
  246. */
  247. /*
  248. * Receive port for net, with packet filter.
  249. * This data structure by itself represents a packet
  250. * filter for a single session.
  251. */
  252. struct net_rcv_port {
  253. queue_chain_t input; /* list of input open_descriptors */
  254. queue_chain_t output; /* list of output open_descriptors */
  255. ipc_port_t rcv_port; /* port to send packet to */
  256. int rcv_qlimit; /* port's qlimit */
  257. int rcv_count; /* number of packets received */
  258. int priority; /* priority for filter */
  259. filter_t *filter_end; /* pointer to end of filter */
  260. filter_t filter[NET_MAX_FILTER];
  261. /* filter operations */
  262. };
  263. struct kmem_cache net_rcv_cache; /* cache of net_rcv_port structs */
  264. #define NET_HASH_SIZE 256
  265. #define N_NET_HASH 4
  266. #define N_NET_HASH_KEYS 4
  267. /*
  268. * A single hash entry.
  269. */
  270. struct net_hash_entry {
  271. queue_chain_t chain; /* list of entries with same hval */
  272. #define he_next chain.next
  273. #define he_prev chain.prev
  274. ipc_port_t rcv_port; /* destination port */
  275. int rcv_qlimit; /* qlimit for the port */
  276. unsigned int keys[N_NET_HASH_KEYS];
  277. };
  278. struct kmem_cache net_hash_entry_cache;
  279. /*
  280. * This structure represents a packet filter with multiple sessions.
  281. *
  282. * For example, all application level TCP sessions might be
  283. * represented by one of these structures. It looks like a
  284. * net_rcv_port struct so that both types can live on the
  285. * same packet filter queues.
  286. */
  287. struct net_hash_header {
  288. struct net_rcv_port rcv;
  289. int n_keys; /* zero if not used */
  290. int ref_count; /* reference count */
  291. net_hash_entry_t table[NET_HASH_SIZE];
  292. } filter_hash_header[N_NET_HASH];
  293. decl_simple_lock_data(,net_hash_header_lock)
  294. #define HASH_ITERATE(head, elt) (elt) = (net_hash_entry_t) (head); do {
  295. #define HASH_ITERATE_END(head, elt) \
  296. (elt) = (net_hash_entry_t) queue_next((queue_entry_t) (elt)); \
  297. } while ((elt) != (head));
  298. #define FILTER_ITERATE(if_port_list, fp, nextfp, chain) \
  299. for ((fp) = (net_rcv_port_t) queue_first(if_port_list); \
  300. !queue_end(if_port_list, (queue_entry_t)(fp)); \
  301. (fp) = (nextfp)) { \
  302. (nextfp) = (net_rcv_port_t) queue_next(chain);
  303. #define FILTER_ITERATE_END }
  304. /* entry_p must be net_rcv_port_t or net_hash_entry_t */
  305. #define ENQUEUE_DEAD(dead, entry_p, chain) { \
  306. (entry_p)->chain.next = (queue_entry_t) (dead); \
  307. (dead) = (queue_entry_t)(entry_p); \
  308. }
  309. /*
  310. * ethernet_priority:
  311. *
  312. * This function properly belongs in the ethernet interfaces;
  313. * it should not be called by this module. (We get packet
  314. * priorities as an argument to net_filter.) It is here
  315. * to avoid massive code duplication.
  316. *
  317. * Returns TRUE for high-priority packets.
  318. */
  319. boolean_t ethernet_priority(kmsg)
  320. const ipc_kmsg_t kmsg;
  321. {
  322. unsigned char *addr =
  323. (unsigned char *) net_kmsg(kmsg)->header;
  324. /*
  325. * A simplistic check for broadcast packets.
  326. */
  327. if ((addr[0] == 0xff) && (addr[1] == 0xff) &&
  328. (addr[2] == 0xff) && (addr[3] == 0xff) &&
  329. (addr[4] == 0xff) && (addr[5] == 0xff))
  330. return FALSE;
  331. else
  332. return TRUE;
  333. }
  334. mach_msg_type_t header_type = {
  335. MACH_MSG_TYPE_BYTE,
  336. 8,
  337. NET_HDW_HDR_MAX,
  338. TRUE,
  339. FALSE,
  340. FALSE,
  341. 0
  342. };
  343. mach_msg_type_t packet_type = {
  344. MACH_MSG_TYPE_BYTE, /* name */
  345. 8, /* size */
  346. 0, /* number */
  347. TRUE, /* inline */
  348. FALSE, /* longform */
  349. FALSE /* deallocate */
  350. };
  351. /*
  352. * net_deliver:
  353. *
  354. * Called and returns holding net_queue_lock, at splimp.
  355. * Dequeues a message and delivers it at spl0.
  356. * Returns FALSE if no messages.
  357. */
  358. boolean_t net_deliver(boolean_t nonblocking)
  359. {
  360. ipc_kmsg_t kmsg;
  361. boolean_t high_priority;
  362. struct ipc_kmsg_queue send_list;
  363. /*
  364. * Pick up a pending network message and deliver it.
  365. * Deliver high priority messages before low priority.
  366. */
  367. if ((kmsg = ipc_kmsg_dequeue(&net_queue_high)) != IKM_NULL) {
  368. net_queue_high_size--;
  369. high_priority = TRUE;
  370. } else if ((kmsg = ipc_kmsg_dequeue(&net_queue_low)) != IKM_NULL) {
  371. net_queue_low_size--;
  372. high_priority = FALSE;
  373. } else
  374. return FALSE;
  375. simple_unlock(&net_queue_lock);
  376. (void) spl0();
  377. /*
  378. * Run the packet through the filters,
  379. * getting back a queue of packets to send.
  380. */
  381. net_filter(kmsg, &send_list);
  382. if (!nonblocking) {
  383. /*
  384. * There is a danger of running out of available buffers
  385. * because they all get moved into the high priority queue
  386. * or a port queue. In particular, we might need to
  387. * allocate more buffers as we pull (previously available)
  388. * buffers out of the low priority queue. But we can only
  389. * allocate if we are allowed to block.
  390. */
  391. net_kmsg_more();
  392. }
  393. while ((kmsg = ipc_kmsg_dequeue(&send_list)) != IKM_NULL) {
  394. int count;
  395. /*
  396. * Fill in the rest of the kmsg.
  397. */
  398. count = net_kmsg(kmsg)->net_rcv_msg_packet_count;
  399. ikm_init_special(kmsg, IKM_SIZE_NETWORK);
  400. kmsg->ikm_header.msgh_bits =
  401. MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0);
  402. /* remember message sizes must be rounded up */
  403. kmsg->ikm_header.msgh_size =
  404. (((mach_msg_size_t) (sizeof(struct net_rcv_msg)
  405. - sizeof net_kmsg(kmsg)->sent
  406. - NET_RCV_MAX + count)) + 3) &~ 3;
  407. kmsg->ikm_header.msgh_local_port = MACH_PORT_NULL;
  408. kmsg->ikm_header.msgh_kind = MACH_MSGH_KIND_NORMAL;
  409. kmsg->ikm_header.msgh_id = NET_RCV_MSG_ID;
  410. net_kmsg(kmsg)->header_type = header_type;
  411. net_kmsg(kmsg)->packet_type = packet_type;
  412. net_kmsg(kmsg)->net_rcv_msg_packet_count = count;
  413. /*
  414. * Send the packet to the destination port. Drop it
  415. * if the destination port is over its backlog.
  416. */
  417. if (ipc_mqueue_send(kmsg, MACH_SEND_TIMEOUT, 0) ==
  418. MACH_MSG_SUCCESS) {
  419. if (high_priority)
  420. net_kmsg_send_high_hits++;
  421. else
  422. net_kmsg_send_low_hits++;
  423. /* the receiver is responsible for the message now */
  424. } else {
  425. if (high_priority)
  426. net_kmsg_send_high_misses++;
  427. else
  428. net_kmsg_send_low_misses++;
  429. ipc_kmsg_destroy(kmsg);
  430. }
  431. }
  432. (void) splimp();
  433. simple_lock(&net_queue_lock);
  434. return TRUE;
  435. }
  436. /*
  437. * We want to deliver packets using ASTs, so we can avoid the
  438. * thread_wakeup/thread_block needed to get to the network
  439. * thread. However, we can't allocate memory in the AST handler,
  440. * because memory allocation might block. Hence we have the
  441. * network thread to allocate memory. The network thread also
  442. * delivers packets, so it can be allocating and delivering for a
  443. * burst. net_thread_awake is protected by net_queue_lock
  444. * (instead of net_queue_free_lock) so that net_packet and
  445. * net_ast can safely determine if the network thread is running.
  446. * This prevents a race that might leave a packet sitting without
  447. * being delivered. It is possible for net_kmsg_get to think
  448. * the network thread is awake, and so avoid a wakeup, and then
  449. * have the network thread sleep without allocating. The next
  450. * net_kmsg_get will do a wakeup.
  451. */
  452. void net_ast(void)
  453. {
  454. spl_t s;
  455. net_ast_taken++;
  456. /*
  457. * If the network thread is awake, then we would
  458. * rather deliver messages from it, because
  459. * it can also allocate memory.
  460. */
  461. s = splimp();
  462. simple_lock(&net_queue_lock);
  463. while (!net_thread_awake && net_deliver(TRUE))
  464. continue;
  465. /*
  466. * Prevent an unnecessary AST. Either the network
  467. * thread will deliver the messages, or there are
  468. * no messages left to deliver.
  469. */
  470. simple_unlock(&net_queue_lock);
  471. (void) splsched();
  472. ast_off(cpu_number(), AST_NETWORK);
  473. (void) splx(s);
  474. }
  475. void __attribute__ ((noreturn)) net_thread_continue(void)
  476. {
  477. for (;;) {
  478. spl_t s;
  479. net_thread_awaken++;
  480. /*
  481. * First get more buffers.
  482. */
  483. net_kmsg_more();
  484. s = splimp();
  485. simple_lock(&net_queue_lock);
  486. while (net_deliver(FALSE))
  487. continue;
  488. net_thread_awake = FALSE;
  489. assert_wait(&net_thread_awake, FALSE);
  490. simple_unlock(&net_queue_lock);
  491. (void) splx(s);
  492. counter(c_net_thread_block++);
  493. thread_block(net_thread_continue);
  494. }
  495. }
  496. void net_thread(void)
  497. {
  498. spl_t s;
  499. /*
  500. * We should be very high priority.
  501. */
  502. thread_set_own_priority(0);
  503. /*
  504. * We sleep initially, so that we don't allocate any buffers
  505. * unless the network is really in use and they are needed.
  506. */
  507. s = splimp();
  508. simple_lock(&net_queue_lock);
  509. net_thread_awake = FALSE;
  510. assert_wait(&net_thread_awake, FALSE);
  511. simple_unlock(&net_queue_lock);
  512. (void) splx(s);
  513. counter(c_net_thread_block++);
  514. thread_block(net_thread_continue);
  515. net_thread_continue();
  516. /*NOTREACHED*/
  517. }
  518. void
  519. reorder_queue(
  520. queue_t first,
  521. queue_t last)
  522. {
  523. queue_entry_t prev, next;
  524. prev = first->prev;
  525. next = last->next;
  526. prev->next = last;
  527. next->prev = first;
  528. last->prev = prev;
  529. last->next = first;
  530. first->next = next;
  531. first->prev = last;
  532. }
  533. /*
  534. * Incoming packet. Header has already been moved to proper place.
  535. * We are already at splimp.
  536. */
  537. void
  538. net_packet(
  539. struct ifnet *ifp,
  540. ipc_kmsg_t kmsg,
  541. unsigned int count,
  542. boolean_t priority)
  543. {
  544. boolean_t awake;
  545. #if MACH_TTD
  546. /*
  547. * Do a quick check to see if it is a kernel TTD packet.
  548. *
  549. * Only check if KernelTTD is enabled, ie. the current
  550. * device driver supports TTD, and the bootp succeeded.
  551. */
  552. if (kttd_enabled && kttd_handle_async(kmsg)) {
  553. /*
  554. * Packet was a valid ttd packet and
  555. * doesn't need to be passed up to filter.
  556. * The ttd code put the used kmsg buffer
  557. * back onto the free list.
  558. */
  559. if (kttd_debug)
  560. printf("**%x**", kttd_async_counter++);
  561. return;
  562. }
  563. #endif /* MACH_TTD */
  564. kmsg->ikm_header.msgh_remote_port = (mach_port_t) ifp;
  565. net_kmsg(kmsg)->net_rcv_msg_packet_count = count;
  566. simple_lock(&net_queue_lock);
  567. if (priority) {
  568. ipc_kmsg_enqueue(&net_queue_high, kmsg);
  569. if (++net_queue_high_size > net_queue_high_max)
  570. net_queue_high_max = net_queue_high_size;
  571. } else {
  572. ipc_kmsg_enqueue(&net_queue_low, kmsg);
  573. if (++net_queue_low_size > net_queue_low_max)
  574. net_queue_low_max = net_queue_low_size;
  575. }
  576. /*
  577. * If the network thread is awake, then we don't
  578. * need to take an AST, because the thread will
  579. * deliver the packet.
  580. */
  581. awake = net_thread_awake;
  582. simple_unlock(&net_queue_lock);
  583. if (!awake) {
  584. spl_t s = splsched();
  585. ast_on(cpu_number(), AST_NETWORK);
  586. (void) splx(s);
  587. }
  588. }
  589. int net_filter_queue_reorder = 0; /* non-zero to enable reordering */
  590. /*
  591. * Run a packet through the filters, returning a list of messages.
  592. * We are *not* called at interrupt level.
  593. */
  594. void
  595. net_filter(kmsg, send_list)
  596. const ipc_kmsg_t kmsg;
  597. ipc_kmsg_queue_t send_list;
  598. {
  599. struct ifnet *ifp;
  600. net_rcv_port_t infp, nextfp;
  601. ipc_kmsg_t new_kmsg;
  602. net_hash_entry_t entp, *hash_headp;
  603. ipc_port_t dest;
  604. queue_entry_t dead_infp = (queue_entry_t) 0;
  605. queue_entry_t dead_entp = (queue_entry_t) 0;
  606. unsigned int ret_count;
  607. queue_head_t *if_port_list;
  608. int count = net_kmsg(kmsg)->net_rcv_msg_packet_count;
  609. ifp = (struct ifnet *) kmsg->ikm_header.msgh_remote_port;
  610. ipc_kmsg_queue_init(send_list);
  611. if (net_kmsg(kmsg)->sent)
  612. if_port_list = &ifp->if_snd_port_list;
  613. else
  614. if_port_list = &ifp->if_rcv_port_list;
  615. /*
  616. * Unfortunately we can't allocate or deallocate memory
  617. * while holding these locks. And we can't drop the locks
  618. * while examining the filter lists.
  619. * Both locks are hold in case a filter is removed from both
  620. * queues.
  621. */
  622. simple_lock(&ifp->if_rcv_port_list_lock);
  623. simple_lock(&ifp->if_snd_port_list_lock);
  624. FILTER_ITERATE(if_port_list, infp, nextfp,
  625. net_kmsg(kmsg)->sent ? &infp->output : &infp->input)
  626. {
  627. entp = (net_hash_entry_t) 0;
  628. if ((infp->filter[0] & NETF_TYPE_MASK) == NETF_BPF) {
  629. ret_count = bpf_do_filter(infp, net_kmsg(kmsg)->packet
  630. + sizeof(struct packet_header),
  631. count - sizeof(struct packet_header),
  632. net_kmsg(kmsg)->header,
  633. ifp->if_header_size, &hash_headp,
  634. &entp);
  635. if (entp == (net_hash_entry_t) 0)
  636. dest = infp->rcv_port;
  637. else
  638. dest = entp->rcv_port;
  639. if (ret_count)
  640. ret_count += sizeof(struct packet_header);
  641. } else {
  642. ret_count = net_do_filter(infp, net_kmsg(kmsg)->packet, count,
  643. net_kmsg(kmsg)->header);
  644. if (ret_count)
  645. ret_count = count;
  646. dest = infp->rcv_port;
  647. }
  648. if (ret_count) {
  649. /*
  650. * Make a send right for the destination.
  651. */
  652. dest = ipc_port_copy_send(dest);
  653. if (!IP_VALID(dest)) {
  654. /*
  655. * This filter is dead. We remove it from the
  656. * filter list and set it aside for deallocation.
  657. */
  658. if (entp == (net_hash_entry_t) 0) {
  659. if (infp->filter[0] & NETF_IN)
  660. queue_remove(&ifp->if_rcv_port_list, infp,
  661. net_rcv_port_t, input);
  662. if (infp->filter[0] & NETF_OUT)
  663. queue_remove(&ifp->if_snd_port_list, infp,
  664. net_rcv_port_t, output);
  665. /* Use input only for queues of dead filters. */
  666. ENQUEUE_DEAD(dead_infp, infp, input);
  667. continue;
  668. } else {
  669. hash_ent_remove (ifp,
  670. (net_hash_header_t)infp,
  671. FALSE, /* no longer used */
  672. hash_headp,
  673. entp,
  674. &dead_entp);
  675. continue;
  676. }
  677. }
  678. /*
  679. * Deliver copy of packet to this channel.
  680. */
  681. if (ipc_kmsg_queue_empty(send_list)) {
  682. /*
  683. * Only receiver, so far
  684. */
  685. new_kmsg = kmsg;
  686. } else {
  687. /*
  688. * Other receivers - must allocate message and copy.
  689. */
  690. new_kmsg = net_kmsg_get();
  691. if (new_kmsg == IKM_NULL) {
  692. ipc_port_release_send(dest);
  693. break;
  694. }
  695. memcpy(
  696. net_kmsg(new_kmsg)->packet,
  697. net_kmsg(kmsg)->packet,
  698. ret_count);
  699. memcpy(
  700. net_kmsg(new_kmsg)->header,
  701. net_kmsg(kmsg)->header,
  702. NET_HDW_HDR_MAX);
  703. }
  704. net_kmsg(new_kmsg)->net_rcv_msg_packet_count = ret_count;
  705. new_kmsg->ikm_header.msgh_remote_port = (mach_port_t) dest;
  706. ipc_kmsg_enqueue(send_list, new_kmsg);
  707. {
  708. net_rcv_port_t prevfp;
  709. int rcount = ++infp->rcv_count;
  710. /*
  711. * See if ordering of filters is wrong
  712. */
  713. if (infp->priority >= NET_HI_PRI) {
  714. #define REORDER_PRIO(chain) \
  715. prevfp = (net_rcv_port_t) queue_prev(&infp->chain); \
  716. /* \
  717. * If infp is not the first element on the queue, \
  718. * and the previous element is at equal priority \
  719. * but has a lower count, then promote infp to \
  720. * be in front of prevfp. \
  721. */ \
  722. if ((queue_t)prevfp != if_port_list && \
  723. infp->priority == prevfp->priority) { \
  724. /* \
  725. * Threshold difference to prevent thrashing \
  726. */ \
  727. if (net_filter_queue_reorder \
  728. && (100 + prevfp->rcv_count < rcount)) \
  729. reorder_queue(&prevfp->chain, &infp->chain);\
  730. }
  731. REORDER_PRIO(input);
  732. REORDER_PRIO(output);
  733. /*
  734. * High-priority filter -> no more deliveries
  735. */
  736. break;
  737. }
  738. }
  739. }
  740. }
  741. FILTER_ITERATE_END
  742. simple_unlock(&ifp->if_snd_port_list_lock);
  743. simple_unlock(&ifp->if_rcv_port_list_lock);
  744. /*
  745. * Deallocate dead filters.
  746. */
  747. if (dead_infp != 0)
  748. net_free_dead_infp(dead_infp);
  749. if (dead_entp != 0)
  750. net_free_dead_entp(dead_entp);
  751. if (ipc_kmsg_queue_empty(send_list)) {
  752. /* Not sent - recycle */
  753. net_kmsg_put(kmsg);
  754. }
  755. }
  756. boolean_t
  757. net_do_filter(infp, data, data_count, header)
  758. net_rcv_port_t infp;
  759. const char * data;
  760. unsigned int data_count;
  761. const char * header;
  762. {
  763. int stack[NET_FILTER_STACK_DEPTH+1];
  764. int *sp;
  765. filter_t *fp, *fpe;
  766. unsigned int op, arg;
  767. /*
  768. * The filter accesses the header and data
  769. * as unsigned short words.
  770. */
  771. data_count /= sizeof(unsigned short);
  772. #define data_word ((unsigned short *)data)
  773. #define header_word ((unsigned short *)header)
  774. sp = &stack[NET_FILTER_STACK_DEPTH];
  775. fp = &infp->filter[1]; /* filter[0] used for flags */
  776. fpe = infp->filter_end;
  777. *sp = TRUE;
  778. while (fp < fpe) {
  779. arg = *fp++;
  780. op = NETF_OP(arg);
  781. arg = NETF_ARG(arg);
  782. switch (arg) {
  783. case NETF_NOPUSH:
  784. arg = *sp++;
  785. break;
  786. case NETF_PUSHZERO:
  787. arg = 0;
  788. break;
  789. case NETF_PUSHLIT:
  790. arg = *fp++;
  791. break;
  792. case NETF_PUSHIND:
  793. arg = *sp++;
  794. if (arg >= data_count)
  795. return FALSE;
  796. arg = data_word[arg];
  797. break;
  798. case NETF_PUSHHDRIND:
  799. arg = *sp++;
  800. if (arg >= NET_HDW_HDR_MAX/sizeof(unsigned short))
  801. return FALSE;
  802. arg = header_word[arg];
  803. break;
  804. default:
  805. if (arg >= NETF_PUSHSTK) {
  806. arg = sp[arg - NETF_PUSHSTK];
  807. }
  808. else if (arg >= NETF_PUSHHDR) {
  809. arg = header_word[arg - NETF_PUSHHDR];
  810. }
  811. else {
  812. arg -= NETF_PUSHWORD;
  813. if (arg >= data_count)
  814. return FALSE;
  815. arg = data_word[arg];
  816. }
  817. break;
  818. }
  819. switch (op) {
  820. case NETF_OP(NETF_NOP):
  821. *--sp = arg;
  822. break;
  823. case NETF_OP(NETF_AND):
  824. *sp &= arg;
  825. break;
  826. case NETF_OP(NETF_OR):
  827. *sp |= arg;
  828. break;
  829. case NETF_OP(NETF_XOR):
  830. *sp ^= arg;
  831. break;
  832. case NETF_OP(NETF_EQ):
  833. *sp = (*sp == arg);
  834. break;
  835. case NETF_OP(NETF_NEQ):
  836. *sp = (*sp != arg);
  837. break;
  838. case NETF_OP(NETF_LT):
  839. *sp = (*sp < arg);
  840. break;
  841. case NETF_OP(NETF_LE):
  842. *sp = (*sp <= arg);
  843. break;
  844. case NETF_OP(NETF_GT):
  845. *sp = (*sp > arg);
  846. break;
  847. case NETF_OP(NETF_GE):
  848. *sp = (*sp >= arg);
  849. break;
  850. case NETF_OP(NETF_COR):
  851. if (*sp++ == arg)
  852. return (TRUE);
  853. break;
  854. case NETF_OP(NETF_CAND):
  855. if (*sp++ != arg)
  856. return (FALSE);
  857. break;
  858. case NETF_OP(NETF_CNOR):
  859. if (*sp++ == arg)
  860. return (FALSE);
  861. break;
  862. case NETF_OP(NETF_CNAND):
  863. if (*sp++ != arg)
  864. return (TRUE);
  865. break;
  866. case NETF_OP(NETF_LSH):
  867. *sp <<= arg;
  868. break;
  869. case NETF_OP(NETF_RSH):
  870. *sp >>= arg;
  871. break;
  872. case NETF_OP(NETF_ADD):
  873. *sp += arg;
  874. break;
  875. case NETF_OP(NETF_SUB):
  876. *sp -= arg;
  877. break;
  878. }
  879. }
  880. return ((*sp) ? TRUE : FALSE);
  881. #undef data_word
  882. #undef header_word
  883. }
  884. /*
  885. * Check filter for invalid operations or stack over/under-flow.
  886. */
  887. boolean_t
  888. parse_net_filter(
  889. filter_t *filter,
  890. unsigned int count)
  891. {
  892. int sp;
  893. filter_t *fpe = &filter[count];
  894. filter_t op, arg;
  895. /*
  896. * count is at least 1, and filter[0] is used for flags.
  897. */
  898. filter++;
  899. sp = NET_FILTER_STACK_DEPTH;
  900. for (; filter < fpe; filter++) {
  901. op = NETF_OP(*filter);
  902. arg = NETF_ARG(*filter);
  903. switch (arg) {
  904. case NETF_NOPUSH:
  905. break;
  906. case NETF_PUSHZERO:
  907. sp--;
  908. break;
  909. case NETF_PUSHLIT:
  910. filter++;
  911. if (filter >= fpe)
  912. return (FALSE); /* literal value not in filter */
  913. sp--;
  914. break;
  915. case NETF_PUSHIND:
  916. case NETF_PUSHHDRIND:
  917. break;
  918. default:
  919. if (arg >= NETF_PUSHSTK) {
  920. if (arg - NETF_PUSHSTK + sp > NET_FILTER_STACK_DEPTH)
  921. return FALSE;
  922. }
  923. else if (arg >= NETF_PUSHHDR) {
  924. if (arg - NETF_PUSHHDR >=
  925. NET_HDW_HDR_MAX/sizeof(unsigned short))
  926. return FALSE;
  927. }
  928. /* else... cannot check for packet bounds
  929. without packet */
  930. sp--;
  931. break;
  932. }
  933. if (sp < 2) {
  934. return (FALSE); /* stack overflow */
  935. }
  936. if (op == NETF_OP(NETF_NOP))
  937. continue;
  938. /*
  939. * all non-NOP operators are binary.
  940. */
  941. if (sp > NET_MAX_FILTER-2)
  942. return (FALSE);
  943. sp++;
  944. switch (op) {
  945. case NETF_OP(NETF_AND):
  946. case NETF_OP(NETF_OR):
  947. case NETF_OP(NETF_XOR):
  948. case NETF_OP(NETF_EQ):
  949. case NETF_OP(NETF_NEQ):
  950. case NETF_OP(NETF_LT):
  951. case NETF_OP(NETF_LE):
  952. case NETF_OP(NETF_GT):
  953. case NETF_OP(NETF_GE):
  954. case NETF_OP(NETF_COR):
  955. case NETF_OP(NETF_CAND):
  956. case NETF_OP(NETF_CNOR):
  957. case NETF_OP(NETF_CNAND):
  958. case NETF_OP(NETF_LSH):
  959. case NETF_OP(NETF_RSH):
  960. case NETF_OP(NETF_ADD):
  961. case NETF_OP(NETF_SUB):
  962. break;
  963. default:
  964. return (FALSE);
  965. }
  966. }
  967. return (TRUE);
  968. }
  969. /*
  970. * Set a filter for a network interface.
  971. *
  972. * We are given a naked send right for the rcv_port.
  973. * If we are successful, we must consume that right.
  974. */
  975. io_return_t
  976. net_set_filter(
  977. struct ifnet *ifp,
  978. ipc_port_t rcv_port,
  979. int priority,
  980. filter_t *filter,
  981. unsigned int filter_count)
  982. {
  983. int filter_bytes;
  984. bpf_insn_t match;
  985. net_rcv_port_t infp, my_infp;
  986. net_rcv_port_t nextfp;
  987. net_hash_header_t hhp;
  988. net_hash_entry_t entp;
  989. net_hash_entry_t *head, nextentp;
  990. queue_entry_t dead_infp, dead_entp;
  991. int i;
  992. int ret, is_new_infp;
  993. io_return_t rval;
  994. boolean_t in, out;
  995. /* Initialize hash_entp to NULL to quiet GCC
  996. * warning about uninitialized variable. hash_entp is only
  997. * used when match != 0; in that case it is properly initialized
  998. * by kmem_cache_alloc().
  999. */
  1000. net_hash_entry_t hash_entp = NULL;
  1001. /*
  1002. * Check the filter syntax.
  1003. */
  1004. filter_bytes = CSPF_BYTES(filter_count);
  1005. match = (bpf_insn_t) 0;
  1006. if (filter_count == 0) {
  1007. return (D_INVALID_OPERATION);
  1008. } else if (!((filter[0] & NETF_IN) || (filter[0] & NETF_OUT))) {
  1009. return (D_INVALID_OPERATION); /* NETF_IN or NETF_OUT required */
  1010. } else if ((filter[0] & NETF_TYPE_MASK) == NETF_BPF) {
  1011. ret = bpf_validate((bpf_insn_t)filter, filter_bytes, &match);
  1012. if (!ret)
  1013. return (D_INVALID_OPERATION);
  1014. } else if ((filter[0] & NETF_TYPE_MASK) == 0) {
  1015. if (!parse_net_filter(filter, filter_count))
  1016. return (D_INVALID_OPERATION);
  1017. } else {
  1018. return (D_INVALID_OPERATION);
  1019. }
  1020. rval = D_SUCCESS; /* default return value */
  1021. dead_infp = dead_entp = 0;
  1022. if (match == (bpf_insn_t) 0) {
  1023. /*
  1024. * If there is no match instruction, we allocate
  1025. * a normal packet filter structure.
  1026. */
  1027. my_infp = (net_rcv_port_t) kmem_cache_alloc(&net_rcv_cache);
  1028. my_infp->rcv_port = rcv_port;
  1029. is_new_infp = TRUE;
  1030. } else {
  1031. /*
  1032. * If there is a match instruction, we assume there will be
  1033. * multiple sessions with a common substructure and allocate
  1034. * a hash table to deal with them.
  1035. */
  1036. my_infp = 0;
  1037. hash_entp = (net_hash_entry_t) kmem_cache_alloc(&net_hash_entry_cache);
  1038. is_new_infp = FALSE;
  1039. }
  1040. /*
  1041. * Look for an existing filter on the same reply port.
  1042. * Look for filters with dead ports (for GC).
  1043. * Look for a filter with the same code except KEY insns.
  1044. */
  1045. void check_filter_list(queue_head_t *if_port_list)
  1046. {
  1047. FILTER_ITERATE(if_port_list, infp, nextfp,
  1048. (if_port_list == &ifp->if_rcv_port_list)
  1049. ? &infp->input : &infp->output)
  1050. {
  1051. if (infp->rcv_port == MACH_PORT_NULL) {
  1052. if (match != 0
  1053. && infp->priority == priority
  1054. && my_infp == 0
  1055. && (infp->filter_end - infp->filter) == filter_count
  1056. && bpf_eq((bpf_insn_t)infp->filter,
  1057. (bpf_insn_t)filter, filter_bytes))
  1058. my_infp = infp;
  1059. for (i = 0; i < NET_HASH_SIZE; i++) {
  1060. head = &((net_hash_header_t) infp)->table[i];
  1061. if (*head == 0)
  1062. continue;
  1063. /*
  1064. * Check each hash entry to make sure the
  1065. * destination port is still valid. Remove
  1066. * any invalid entries.
  1067. */
  1068. entp = *head;
  1069. do {
  1070. nextentp = (net_hash_entry_t) entp->he_next;
  1071. /* checked without
  1072. ip_lock(entp->rcv_port) */
  1073. if (entp->rcv_port == rcv_port
  1074. || !IP_VALID(entp->rcv_port)
  1075. || !ip_active(entp->rcv_port)) {
  1076. ret = hash_ent_remove (ifp,
  1077. (net_hash_header_t)infp,
  1078. (my_infp == infp),
  1079. head,
  1080. entp,
  1081. &dead_entp);
  1082. if (ret)
  1083. goto hash_loop_end;
  1084. }
  1085. entp = nextentp;
  1086. /* While test checks head since hash_ent_remove
  1087. might modify it.
  1088. */
  1089. } while (*head != 0 && entp != *head);
  1090. }
  1091. hash_loop_end:
  1092. ;
  1093. } else if (infp->rcv_port == rcv_port
  1094. || !IP_VALID(infp->rcv_port)
  1095. || !ip_active(infp->rcv_port)) {
  1096. /* Remove the old filter from lists */
  1097. if (infp->filter[0] & NETF_IN)
  1098. queue_remove(&ifp->if_rcv_port_list, infp,
  1099. net_rcv_port_t, input);
  1100. if (infp->filter[0] & NETF_OUT)
  1101. queue_remove(&ifp->if_snd_port_list, infp,
  1102. net_rcv_port_t, output);
  1103. ENQUEUE_DEAD(dead_infp, infp, input);
  1104. }
  1105. }
  1106. FILTER_ITERATE_END
  1107. }
  1108. in = (filter[0] & NETF_IN) != 0;
  1109. out = (filter[0] & NETF_OUT) != 0;
  1110. simple_lock(&ifp->if_rcv_port_list_lock);
  1111. simple_lock(&ifp->if_snd_port_list_lock);
  1112. if (in)
  1113. check_filter_list(&ifp->if_rcv_port_list);
  1114. if (out)
  1115. check_filter_list(&ifp->if_snd_port_list);
  1116. if (my_infp == 0) {
  1117. /* Allocate a dummy infp */
  1118. simple_lock(&net_hash_header_lock);
  1119. for (i = 0; i < N_NET_HASH; i++) {
  1120. if (filter_hash_header[i].n_keys == 0)
  1121. break;
  1122. }
  1123. if (i == N_NET_HASH) {
  1124. simple_unlock(&net_hash_header_lock);
  1125. simple_unlock(&ifp->if_snd_port_list_lock);
  1126. simple_unlock(&ifp->if_rcv_port_list_lock);
  1127. ipc_port_release_send(rcv_port);
  1128. if (match != 0)
  1129. kmem_cache_free(&net_hash_entry_cache,
  1130. (vm_offset_t)hash_entp);
  1131. rval = D_NO_MEMORY;
  1132. goto clean_and_return;
  1133. }
  1134. hhp = &filter_hash_header[i];
  1135. hhp->n_keys = match->jt;
  1136. simple_unlock(&net_hash_header_lock);
  1137. hhp->ref_count = 0;
  1138. for (i = 0; i < NET_HASH_SIZE; i++)
  1139. hhp->table[i] = 0;
  1140. my_infp = (net_rcv_port_t)hhp;
  1141. my_infp->rcv_port = MACH_PORT_NULL; /* indication of dummy */
  1142. is_new_infp = TRUE;
  1143. }
  1144. if (is_new_infp) {
  1145. my_infp->priority = priority;
  1146. my_infp->rcv_count = 0;
  1147. /* Copy filter program. */
  1148. memcpy (my_infp->filter, filter, filter_bytes);
  1149. my_infp->filter_end =
  1150. (filter_t *)((char *)my_infp->filter + filter_bytes);
  1151. if (match == 0) {
  1152. my_infp->rcv_qlimit = net_add_q_info(rcv_port);
  1153. } else {
  1154. my_infp->rcv_qlimit = 0;
  1155. }
  1156. /* Insert my_infp according to priority */
  1157. if (in) {
  1158. queue_iterate(&ifp->if_rcv_port_list, infp, net_rcv_port_t, input)
  1159. if (priority > infp->priority)
  1160. break;
  1161. queue_enter(&ifp->if_rcv_port_list, my_infp, net_rcv_port_t, input);
  1162. }
  1163. if (out) {
  1164. queue_iterate(&ifp->if_snd_port_list, infp, net_rcv_port_t, output)
  1165. if (priority > infp->priority)
  1166. break;
  1167. queue_enter(&ifp->if_snd_port_list, my_infp, net_rcv_port_t, output);
  1168. }
  1169. }
  1170. if (match != 0)
  1171. { /* Insert to hash list */
  1172. net_hash_entry_t *p;
  1173. hash_entp->rcv_port = rcv_port;
  1174. for (i = 0; i < match->jt; i++) /* match->jt is n_keys */
  1175. hash_entp->keys[i] = match[i+1].k;
  1176. p = &((net_hash_header_t)my_infp)->
  1177. table[bpf_hash(match->jt, hash_entp->keys)];
  1178. /* Not checking for the same key values */
  1179. if (*p == 0) {
  1180. queue_init (&hash_entp->chain);
  1181. *p = hash_entp;
  1182. } else {
  1183. enqueue_tail(&(*p)->chain, &hash_entp->chain);
  1184. }
  1185. ((net_hash_header_t)my_infp)->ref_count++;
  1186. hash_entp->rcv_qlimit = net_add_q_info(rcv_port);
  1187. }
  1188. simple_unlock(&ifp->if_snd_port_list_lock);
  1189. simple_unlock(&ifp->if_rcv_port_list_lock);
  1190. clean_and_return:
  1191. /* No locks are held at this point. */
  1192. if (dead_infp != 0)
  1193. net_free_dead_infp(dead_infp);
  1194. if (dead_entp != 0)
  1195. net_free_dead_entp(dead_entp);
  1196. return (rval);
  1197. }
  1198. /*
  1199. * Other network operations
  1200. */
  1201. io_return_t
  1202. net_getstat(
  1203. struct ifnet *ifp,
  1204. dev_flavor_t flavor,
  1205. dev_status_t status, /* pointer to OUT array */
  1206. natural_t *count) /* OUT */
  1207. {
  1208. switch (flavor) {
  1209. case NET_STATUS:
  1210. {
  1211. struct net_status *ns = (struct net_status *)status;
  1212. if (*count < NET_STATUS_COUNT)
  1213. return (D_INVALID_OPERATION);
  1214. ns->min_packet_size = ifp->if_header_size;
  1215. ns->max_packet_size = ifp->if_header_size + ifp->if_mtu;
  1216. ns->header_format = ifp->if_header_format;
  1217. ns->header_size = ifp->if_header_size;
  1218. ns->address_size = ifp->if_address_size;
  1219. ns->flags = ifp->if_flags;
  1220. ns->mapped_size = 0;
  1221. *count = NET_STATUS_COUNT;
  1222. break;
  1223. }
  1224. case NET_ADDRESS:
  1225. {
  1226. int addr_byte_count;
  1227. int addr_int_count;
  1228. int i;
  1229. addr_byte_count = ifp->if_address_size;
  1230. addr_int_count = (addr_byte_count + (sizeof(int)-1))
  1231. / sizeof(int);
  1232. if (*count < addr_int_count)
  1233. {
  1234. /* XXX debug hack. */
  1235. printf ("net_getstat: count: %d, addr_int_count: %d\n",
  1236. *count, addr_int_count);
  1237. return (D_INVALID_OPERATION);
  1238. }
  1239. memcpy(status, ifp->if_address, addr_byte_count);
  1240. if (addr_byte_count < addr_int_count * sizeof(int))
  1241. memset((char *)status + addr_byte_count, 0,
  1242. (addr_int_count * sizeof(int)
  1243. - addr_byte_count));
  1244. for (i = 0; i < addr_int_count; i++) {
  1245. int word;
  1246. word = status[i];
  1247. status[i] = htonl(word);
  1248. }
  1249. *count = addr_int_count;
  1250. break;
  1251. }
  1252. default:
  1253. return (D_INVALID_OPERATION);
  1254. }
  1255. return (D_SUCCESS);
  1256. }
  1257. io_return_t
  1258. net_write(
  1259. struct ifnet *ifp,
  1260. int (*start)(),
  1261. io_req_t ior)
  1262. {
  1263. spl_t s;
  1264. kern_return_t rc;
  1265. boolean_t wait;
  1266. /*
  1267. * Reject the write if the interface is down.
  1268. */
  1269. if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
  1270. return (D_DEVICE_DOWN);
  1271. /*
  1272. * Reject the write if the packet is too large or too small.
  1273. */
  1274. if (ior->io_count < ifp->if_header_size ||
  1275. ior->io_count > ifp->if_header_size + ifp->if_mtu)
  1276. return (D_INVALID_SIZE);
  1277. /*
  1278. * Wire down the memory.
  1279. */
  1280. rc = device_write_get(ior, &wait);
  1281. if (rc != KERN_SUCCESS)
  1282. return (rc);
  1283. /*
  1284. * Network interfaces can't cope with VM continuations.
  1285. * If wait is set, just panic.
  1286. */
  1287. if (wait) {
  1288. panic("net_write: VM continuation");
  1289. }
  1290. /*
  1291. * Queue the packet on the output queue, and
  1292. * start the device.
  1293. */
  1294. s = splimp();
  1295. IF_ENQUEUE(&ifp->if_snd, ior);
  1296. (*start)(ifp->if_unit);
  1297. splx(s);
  1298. return (D_IO_QUEUED);
  1299. }
  1300. /*
  1301. * Initialize the whole package.
  1302. */
  1303. void
  1304. net_io_init(void)
  1305. {
  1306. vm_size_t size;
  1307. size = sizeof(struct net_rcv_port);
  1308. kmem_cache_init(&net_rcv_cache, "net_rcv_port", size, 0,
  1309. NULL, 0);
  1310. size = sizeof(struct net_hash_entry);
  1311. kmem_cache_init(&net_hash_entry_cache, "net_hash_entry", size, 0,
  1312. NULL, 0);
  1313. size = ikm_plus_overhead(sizeof(struct net_rcv_msg));
  1314. net_kmsg_size = round_page(size);
  1315. /*
  1316. * net_kmsg_max caps the number of buffers
  1317. * we are willing to allocate. By default,
  1318. * we allow for net_queue_free_min plus
  1319. * the queue limit for each filter.
  1320. * (Added as the filters are added.)
  1321. */
  1322. simple_lock_init(&net_kmsg_total_lock);
  1323. if (net_kmsg_max == 0)
  1324. net_kmsg_max = net_queue_free_min;
  1325. simple_lock_init(&net_queue_free_lock);
  1326. ipc_kmsg_queue_init(&net_queue_free);
  1327. simple_lock_init(&net_queue_lock);
  1328. ipc_kmsg_queue_init(&net_queue_high);
  1329. ipc_kmsg_queue_init(&net_queue_low);
  1330. simple_lock_init(&net_hash_header_lock);
  1331. }
  1332. /* ======== BPF: Berkeley Packet Filter ======== */
  1333. /*-
  1334. * Copyright (c) 1990-1991 The Regents of the University of California.
  1335. * All rights reserved.
  1336. *
  1337. * This code is derived from the Stanford/CMU enet packet filter,
  1338. * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  1339. * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  1340. * Berkeley Laboratory.
  1341. *
  1342. * Redistribution and use in source and binary forms, with or without
  1343. * modification, are permitted provided that the following conditions
  1344. * are met:
  1345. * 1. Redistributions of source code must retain the above copyright
  1346. * notice, this list of conditions and the following disclaimer.
  1347. * 2. Redistributions in binary form must reproduce the above copyright
  1348. * notice, this list of conditions and the following disclaimer in the
  1349. * documentation and/or other materials provided with the distribution.
  1350. * 4. Neither the name of the University nor the names of its contributors
  1351. * may be used to endorse or promote products derived from this software
  1352. * without specific prior written permission.
  1353. *
  1354. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1355. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1356. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1357. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1358. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1359. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1360. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1361. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1362. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1363. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1364. * SUCH DAMAGE.
  1365. *
  1366. * @(#)bpf.c 7.5 (Berkeley) 7/15/91
  1367. */
  1368. #if defined(sparc) || defined(mips) || defined(ibm032) || defined(alpha)
  1369. #define BPF_ALIGN
  1370. #endif
  1371. #ifndef BPF_ALIGN
  1372. #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
  1373. #define EXTRACT_LONG(p) (ntohl(*(u_long *)p))
  1374. #else
  1375. #define EXTRACT_SHORT(p)\
  1376. ((u_short)\
  1377. ((u_short)*((u_char *)p+0)<<8|\
  1378. (u_short)*((u_char *)p+1)<<0))
  1379. #define EXTRACT_LONG(p)\
  1380. ((u_long)*((u_char *)p+0)<<24|\
  1381. (u_long)*((u_char *)p+1)<<16|\
  1382. (u_long)*((u_char *)p+2)<<8|\
  1383. (u_long)*((u_char *)p+3)<<0)
  1384. #endif
  1385. /*
  1386. * Execute the filter program starting at pc on the packet p
  1387. * wirelen is the length of the original packet
  1388. * buflen is the amount of data present
  1389. */
  1390. int
  1391. bpf_do_filter(
  1392. net_rcv_port_t infp,
  1393. char * p, /* packet data */
  1394. unsigned int wirelen, /* data_count (in bytes) */
  1395. char * header,
  1396. unsigned int hlen, /* header len (in bytes) */
  1397. net_hash_entry_t **hash_headpp,
  1398. net_hash_entry_t *entpp) /* out */
  1399. {
  1400. bpf_insn_t pc, pc_end;
  1401. unsigned int buflen;
  1402. unsigned int A, X;
  1403. int k;
  1404. unsigned int mem[BPF_MEMWORDS];
  1405. /* Generic pointer to either HEADER or P according to the specified offset. */
  1406. char *data = NULL;
  1407. pc = ((bpf_insn_t) infp->filter) + 1;
  1408. /* filter[0].code is (NETF_BPF | flags) */
  1409. pc_end = (bpf_insn_t)infp->filter_end;
  1410. buflen = NET_RCV_MAX;
  1411. *entpp = 0; /* default */
  1412. A = 0;
  1413. X = 0;
  1414. for (; pc < pc_end; ++pc) {
  1415. switch (pc->code) {
  1416. default:
  1417. #ifdef KERNEL
  1418. return 0;
  1419. #else
  1420. abort();
  1421. #endif
  1422. case BPF_RET|BPF_K:
  1423. if (infp->rcv_port == MACH_PORT_NULL &&
  1424. *entpp == 0) {
  1425. return 0;
  1426. }
  1427. return ((u_int)pc->k <= wirelen) ?
  1428. pc->k : wirelen;
  1429. case BPF_RET|BPF_A:
  1430. if (infp->rcv_port == MACH_PORT_NULL &&
  1431. *entpp == 0) {
  1432. return 0;
  1433. }
  1434. return ((u_int)A <= wirelen) ?
  1435. A : wirelen;
  1436. case BPF_RET|BPF_MATCH_IMM:
  1437. if (bpf_match ((net_hash_header_t)infp, pc->jt, mem,
  1438. hash_headpp, entpp)) {
  1439. return ((u_int)pc->k <= wirelen) ?
  1440. pc->k : wirelen;
  1441. }
  1442. return 0;
  1443. case BPF_LD|BPF_W|BPF_ABS:
  1444. k = pc->k;
  1445. load_word:
  1446. if ((u_int)k + sizeof(int) <= hlen)
  1447. data = header;
  1448. else if ((u_int)k + sizeof(int) <= buflen) {
  1449. k -= hlen;
  1450. data = p;
  1451. } else
  1452. return 0;
  1453. #ifdef BPF_ALIGN
  1454. if (((int)(data + k) & 3) != 0)
  1455. A = EXTRACT_LONG(&data[k]);
  1456. else
  1457. #endif
  1458. A = ntohl(*(int *)(data + k));
  1459. continue;
  1460. case BPF_LD|BPF_H|BPF_ABS:
  1461. k = pc->k;
  1462. load_half:
  1463. if ((u_int)k + sizeof(short) <= hlen)
  1464. data = header;
  1465. else if ((u_int)k + sizeof(short) <= buflen) {
  1466. k -= hlen;
  1467. data = p;
  1468. } else
  1469. return 0;
  1470. A = EXTRACT_SHORT(&data[k]);
  1471. continue;
  1472. case BPF_LD|BPF_B|BPF_ABS:
  1473. k = pc->k;
  1474. load_byte:
  1475. if ((u_int)k < hlen)
  1476. data = header;
  1477. else if ((u_int)k < buflen) {
  1478. data = p;
  1479. k -= hlen;
  1480. } else
  1481. return 0;
  1482. A = data[k];
  1483. continue;
  1484. case BPF_LD|BPF_W|BPF_LEN:
  1485. A = wirelen;
  1486. continue;
  1487. case BPF_LDX|BPF_W|BPF_LEN:
  1488. X = wirelen;
  1489. continue;
  1490. case BPF_LD|BPF_W|BPF_IND:
  1491. k = X + pc->k;
  1492. goto load_word;
  1493. case BPF_LD|BPF_H|BPF_IND:
  1494. k = X + pc->k;
  1495. goto load_half;
  1496. case BPF_LD|BPF_B|BPF_IND:
  1497. k = X + pc->k;
  1498. goto load_byte;
  1499. case BPF_LDX|BPF_MSH|BPF_B:
  1500. k = pc->k;
  1501. if (k < hlen)
  1502. data = header;
  1503. else if (k < buflen) {
  1504. data = p;
  1505. k -= hlen;
  1506. } else
  1507. return 0;
  1508. X = (data[k] & 0xf) << 2;
  1509. continue;
  1510. case BPF_LD|BPF_IMM:
  1511. A = pc->k;
  1512. continue;
  1513. case BPF_LDX|BPF_IMM:
  1514. X = pc->k;
  1515. continue;
  1516. case BPF_LD|BPF_MEM:
  1517. A = mem[pc->k];
  1518. continue;
  1519. case BPF_LDX|BPF_MEM:
  1520. X = mem[pc->k];
  1521. continue;
  1522. case BPF_ST:
  1523. mem[pc->k] = A;
  1524. continue;
  1525. case BPF_STX:
  1526. mem[pc->k] = X;
  1527. continue;
  1528. case BPF_JMP|BPF_JA:
  1529. pc += pc->k;
  1530. continue;
  1531. case BPF_JMP|BPF_JGT|BPF_K:
  1532. pc += (A > pc->k) ? pc->jt : pc->jf;
  1533. continue;
  1534. case BPF_JMP|BPF_JGE|BPF_K:
  1535. pc += (A >= pc->k) ? pc->jt : pc->jf;
  1536. continue;
  1537. case BPF_JMP|BPF_JEQ|BPF_K:
  1538. pc += (A == pc->k) ? pc->jt : pc->jf;
  1539. continue;
  1540. case BPF_JMP|BPF_JSET|BPF_K:
  1541. pc += (A & pc->k) ? pc->jt : pc->jf;
  1542. continue;
  1543. case BPF_JMP|BPF_JGT|BPF_X:
  1544. pc += (A > X) ? pc->jt : pc->jf;
  1545. continue;
  1546. case BPF_JMP|BPF_JGE|BPF_X:
  1547. pc += (A >= X) ? pc->jt : pc->jf;
  1548. continue;
  1549. case BPF_JMP|BPF_JEQ|BPF_X:
  1550. pc += (A == X) ? pc->jt : pc->jf;
  1551. continue;
  1552. case BPF_JMP|BPF_JSET|BPF_X:
  1553. pc += (A & X) ? pc->jt : pc->jf;
  1554. continue;
  1555. case BPF_ALU|BPF_ADD|BPF_X:
  1556. A += X;
  1557. continue;
  1558. case BPF_ALU|BPF_SUB|BPF_X:
  1559. A -= X;
  1560. continue;
  1561. case BPF_ALU|BPF_MUL|BPF_X:
  1562. A *= X;
  1563. continue;
  1564. case BPF_ALU|BPF_DIV|BPF_X:
  1565. if (X == 0)
  1566. return 0;
  1567. A /= X;
  1568. continue;
  1569. case BPF_ALU|BPF_AND|BPF_X:
  1570. A &= X;
  1571. continue;
  1572. case BPF_ALU|BPF_OR|BPF_X:
  1573. A |= X;
  1574. continue;
  1575. case BPF_ALU|BPF_LSH|BPF_X:
  1576. A <<= X;
  1577. continue;
  1578. case BPF_ALU|BPF_RSH|BPF_X:
  1579. A >>= X;
  1580. continue;
  1581. case BPF_ALU|BPF_ADD|BPF_K:
  1582. A += pc->k;
  1583. continue;
  1584. case BPF_ALU|BPF_SUB|BPF_K:
  1585. A -= pc->k;
  1586. continue;
  1587. case BPF_ALU|BPF_MUL|BPF_K:
  1588. A *= pc->k;
  1589. continue;
  1590. case BPF_ALU|BPF_DIV|BPF_K:
  1591. A /= pc->k;
  1592. continue;
  1593. case BPF_ALU|BPF_AND|BPF_K:
  1594. A &= pc->k;
  1595. continue;
  1596. case BPF_ALU|BPF_OR|BPF_K:
  1597. A |= pc->k;
  1598. continue;
  1599. case BPF_ALU|BPF_LSH|BPF_K:
  1600. A <<= pc->k;
  1601. continue;
  1602. case BPF_ALU|BPF_RSH|BPF_K:
  1603. A >>= pc->k;
  1604. continue;
  1605. case BPF_ALU|BPF_NEG:
  1606. A = -A;
  1607. continue;
  1608. case BPF_MISC|BPF_TAX:
  1609. X = A;
  1610. continue;
  1611. case BPF_MISC|BPF_TXA:
  1612. A = X;
  1613. continue;
  1614. }
  1615. }
  1616. return 0;
  1617. }
  1618. /*
  1619. * Return 1 if the 'f' is a valid filter program without a MATCH
  1620. * instruction. Return 2 if it is a valid filter program with a MATCH
  1621. * instruction. Otherwise, return 0.
  1622. * The constraints are that each jump be forward and to a valid
  1623. * code. The code must terminate with either an accept or reject.
  1624. * 'valid' is an array for use by the routine (it must be at least
  1625. * 'len' bytes long).
  1626. *
  1627. * The kernel needs to be able to verify an application's filter code.
  1628. * Otherwise, a bogus program could easily crash the system.
  1629. */
  1630. int
  1631. bpf_validate(
  1632. bpf_insn_t f,
  1633. int bytes,
  1634. bpf_insn_t *match)
  1635. {
  1636. int i, j, len;
  1637. bpf_insn_t p;
  1638. len = BPF_BYTES2LEN(bytes);
  1639. /*
  1640. * f[0].code is already checked to be (NETF_BPF | flags).
  1641. * So skip f[0].
  1642. */
  1643. for (i = 1; i < len; ++i) {
  1644. /*
  1645. * Check that that jumps are forward, and within
  1646. * the code block.
  1647. */
  1648. p = &f[i];
  1649. if (BPF_CLASS(p->code) == BPF_JMP) {
  1650. int from = i + 1;
  1651. if (BPF_OP(p->code) == BPF_JA) {
  1652. if (from + p->k >= len)
  1653. return 0;
  1654. }
  1655. else if (from + p->jt >= len || from + p->jf >= len)
  1656. return 0;
  1657. }
  1658. /*
  1659. * Check that memory operations use valid addresses.
  1660. */
  1661. if ((BPF_CLASS(p->code) == BPF_ST ||
  1662. (BPF_CLASS(p->code) == BPF_LD &&
  1663. (p->code & 0xe0) == BPF_MEM)) &&
  1664. (p->k >= BPF_MEMWORDS || p->k < 0))
  1665. return 0;
  1666. /*
  1667. * Check for constant division by 0.
  1668. */
  1669. if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
  1670. return 0;
  1671. /*
  1672. * Check for match instruction.
  1673. * Only one match instruction per filter is allowed.
  1674. */
  1675. if (p->code == (BPF_RET|BPF_MATCH_IMM)) {
  1676. if (*match != 0 ||
  1677. p->jt == 0 ||
  1678. p->jt > N_NET_HASH_KEYS)
  1679. return 0;
  1680. i += p->jt; /* skip keys */
  1681. if (i + 1 > len)
  1682. return 0;
  1683. for (j = 1; j <= p->jt; j++) {
  1684. if (p[j].code != (BPF_MISC|BPF_KEY))
  1685. return 0;
  1686. }
  1687. *match = p;
  1688. }
  1689. }
  1690. if (BPF_CLASS(f[len - 1].code) == BPF_RET)
  1691. return ((*match == 0) ? 1 : 2);
  1692. else
  1693. return 0;
  1694. }
  1695. int
  1696. bpf_eq(
  1697. bpf_insn_t f1,
  1698. bpf_insn_t f2,
  1699. int bytes)
  1700. {
  1701. int count;
  1702. count = BPF_BYTES2LEN(bytes);
  1703. for (; count--; f1++, f2++) {
  1704. if (!BPF_INSN_EQ(f1, f2)) {
  1705. if ( f1->code == (BPF_MISC|BPF_KEY) &&
  1706. f2->code == (BPF_MISC|BPF_KEY) )
  1707. continue;
  1708. return FALSE;
  1709. }
  1710. };
  1711. return TRUE;
  1712. }
  1713. unsigned int
  1714. bpf_hash (n, keys)
  1715. int n;
  1716. const unsigned int *keys;
  1717. {
  1718. unsigned int hval = 0;
  1719. while (n--) {
  1720. hval += *keys++;
  1721. }
  1722. return (hval % NET_HASH_SIZE);
  1723. }
  1724. int
  1725. bpf_match (hash, n_keys, keys, hash_headpp, entpp)
  1726. net_hash_header_t hash;
  1727. int n_keys;
  1728. const unsigned int *keys;
  1729. net_hash_entry_t **hash_headpp, *entpp;
  1730. {
  1731. net_hash_entry_t head, entp;
  1732. int i;
  1733. if (n_keys != hash->n_keys)
  1734. return FALSE;
  1735. *hash_headpp = &hash->table[bpf_hash(n_keys, keys)];
  1736. head = **hash_headpp;
  1737. if (head == 0)
  1738. return FALSE;
  1739. HASH_ITERATE (head, entp)
  1740. {
  1741. for (i = 0; i < n_keys; i++) {
  1742. if (keys[i] != entp->keys[i])
  1743. break;
  1744. }
  1745. if (i == n_keys) {
  1746. *entpp = entp;
  1747. return TRUE;
  1748. }
  1749. }
  1750. HASH_ITERATE_END (head, entp)
  1751. return FALSE;
  1752. }
  1753. /*
  1754. * Removes a hash entry (ENTP) from its queue (HEAD).
  1755. * If the reference count of filter (HP) becomes zero and not USED,
  1756. * HP is removed from the corresponding port lists and is freed.
  1757. */
  1758. int
  1759. hash_ent_remove(
  1760. struct ifnet *ifp,
  1761. net_hash_header_t hp,
  1762. int used,
  1763. net_hash_entry_t *head,
  1764. net_hash_entry_t entp,
  1765. queue_entry_t *dead_p)
  1766. {
  1767. hp->ref_count--;
  1768. if (*head == entp) {
  1769. if (queue_empty((queue_t) entp)) {
  1770. *head = 0;
  1771. ENQUEUE_DEAD(*dead_p, entp, chain);
  1772. if (hp->ref_count == 0 && !used) {
  1773. if (((net_rcv_port_t)hp)->filter[0] & NETF_IN)
  1774. queue_remove(&ifp->if_rcv_port_list,
  1775. (net_rcv_port_t)hp,
  1776. net_rcv_port_t, input);
  1777. if (((net_rcv_port_t)hp)->filter[0] & NETF_OUT)
  1778. queue_remove(&ifp->if_snd_port_list,
  1779. (net_rcv_port_t)hp,
  1780. net_rcv_port_t, output);
  1781. hp->n_keys = 0;
  1782. return TRUE;
  1783. }
  1784. return FALSE;
  1785. } else {
  1786. *head = (net_hash_entry_t)queue_next((queue_t) entp);
  1787. }
  1788. }
  1789. remqueue((queue_t)*head, (queue_entry_t)entp);
  1790. ENQUEUE_DEAD(*dead_p, entp, chain);
  1791. return FALSE;
  1792. }
  1793. int
  1794. net_add_q_info(ipc_port_t rcv_port)
  1795. {
  1796. mach_port_msgcount_t qlimit = 0;
  1797. /*
  1798. * We use a new port, so increase net_queue_free_min
  1799. * and net_kmsg_max to allow for more queued messages.
  1800. */
  1801. if (IP_VALID(rcv_port)) {
  1802. ip_lock(rcv_port);
  1803. if (ip_active(rcv_port))
  1804. qlimit = rcv_port->ip_qlimit;
  1805. ip_unlock(rcv_port);
  1806. }
  1807. simple_lock(&net_kmsg_total_lock);
  1808. net_queue_free_min++;
  1809. net_kmsg_max += qlimit + 1;
  1810. simple_unlock(&net_kmsg_total_lock);
  1811. return (int)qlimit;
  1812. }
  1813. void
  1814. net_del_q_info(int qlimit)
  1815. {
  1816. simple_lock(&net_kmsg_total_lock);
  1817. net_queue_free_min--;
  1818. net_kmsg_max -= qlimit + 1;
  1819. simple_unlock(&net_kmsg_total_lock);
  1820. }
  1821. /*
  1822. * net_free_dead_infp (dead_infp)
  1823. * queue_entry_t dead_infp; list of dead net_rcv_port_t.
  1824. *
  1825. * Deallocates dead net_rcv_port_t.
  1826. * No locks should be held when called.
  1827. */
  1828. void
  1829. net_free_dead_infp(queue_entry_t dead_infp)
  1830. {
  1831. net_rcv_port_t infp, nextfp;
  1832. for (infp = (net_rcv_port_t) dead_infp; infp != 0; infp = nextfp)
  1833. {
  1834. nextfp = (net_rcv_port_t) queue_next(&infp->input);
  1835. ipc_port_release_send(infp->rcv_port);
  1836. net_del_q_info(infp->rcv_qlimit);
  1837. kmem_cache_free(&net_rcv_cache, (vm_offset_t) infp);
  1838. }
  1839. }
  1840. /*
  1841. * net_free_dead_entp (dead_entp)
  1842. * queue_entry_t dead_entp; list of dead net_hash_entry_t.
  1843. *
  1844. * Deallocates dead net_hash_entry_t.
  1845. * No locks should be held when called.
  1846. */
  1847. void
  1848. net_free_dead_entp(queue_entry_t dead_entp)
  1849. {
  1850. net_hash_entry_t entp, nextentp;
  1851. for (entp = (net_hash_entry_t)dead_entp; entp != 0; entp = nextentp)
  1852. {
  1853. nextentp = (net_hash_entry_t) queue_next(&entp->chain);
  1854. ipc_port_release_send(entp->rcv_port);
  1855. net_del_q_info(entp->rcv_qlimit);
  1856. kmem_cache_free(&net_hash_entry_cache, (vm_offset_t) entp);
  1857. }
  1858. }