ip_mroute.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1989 Stephen Deering
  5. * Copyright (c) 1992, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Stephen Deering of Stanford University.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. /*
  36. * IP multicast forwarding procedures
  37. *
  38. * Written by David Waitzman, BBN Labs, August 1988.
  39. * Modified by Steve Deering, Stanford, February 1989.
  40. * Modified by Mark J. Steiglitz, Stanford, May, 1991
  41. * Modified by Van Jacobson, LBL, January 1993
  42. * Modified by Ajit Thyagarajan, PARC, August 1993
  43. * Modified by Bill Fenner, PARC, April 1995
  44. * Modified by Ahmed Helmy, SGI, June 1996
  45. * Modified by George Edmond Eddy (Rusty), ISI, February 1998
  46. * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
  47. * Modified by Hitoshi Asaeda, WIDE, August 2000
  48. * Modified by Pavlin Radoslavov, ICSI, October 2002
  49. * Modified by Wojciech Macek, Semihalf, May 2021
  50. *
  51. * MROUTING Revision: 3.5
  52. * and PIM-SMv2 and PIM-DM support, advanced API support,
  53. * bandwidth metering and signaling
  54. */
  55. /*
  56. * TODO: Prefix functions with ipmf_.
  57. * TODO: Maintain a refcount on if_allmulti() in ifnet or in the protocol
  58. * domain attachment (if_afdata) so we can track consumers of that service.
  59. * TODO: Deprecate routing socket path for SIOCGETSGCNT and SIOCGETVIFCNT,
  60. * move it to socket options.
  61. * TODO: Cleanup LSRR removal further.
  62. * TODO: Push RSVP stubs into raw_ip.c.
  63. * TODO: Use bitstring.h for vif set.
  64. * TODO: Fix mrt6_ioctl dangling ref when dynamically loaded.
  65. * TODO: Sync ip6_mroute.c with this file.
  66. */
  67. #include <sys/cdefs.h>
  68. #include "opt_inet.h"
  69. #include "opt_mrouting.h"
  70. #define _PIM_VT 1
  71. #include <sys/types.h>
  72. #include <sys/param.h>
  73. #include <sys/kernel.h>
  74. #include <sys/stddef.h>
  75. #include <sys/condvar.h>
  76. #include <sys/eventhandler.h>
  77. #include <sys/lock.h>
  78. #include <sys/kthread.h>
  79. #include <sys/ktr.h>
  80. #include <sys/malloc.h>
  81. #include <sys/mbuf.h>
  82. #include <sys/module.h>
  83. #include <sys/priv.h>
  84. #include <sys/protosw.h>
  85. #include <sys/signalvar.h>
  86. #include <sys/socket.h>
  87. #include <sys/socketvar.h>
  88. #include <sys/sockio.h>
  89. #include <sys/sx.h>
  90. #include <sys/sysctl.h>
  91. #include <sys/syslog.h>
  92. #include <sys/systm.h>
  93. #include <sys/taskqueue.h>
  94. #include <sys/time.h>
  95. #include <sys/counter.h>
  96. #include <machine/atomic.h>
  97. #include <net/if.h>
  98. #include <net/if_var.h>
  99. #include <net/if_private.h>
  100. #include <net/if_types.h>
  101. #include <net/netisr.h>
  102. #include <net/route.h>
  103. #include <net/vnet.h>
  104. #include <netinet/in.h>
  105. #include <netinet/igmp.h>
  106. #include <netinet/in_systm.h>
  107. #include <netinet/in_var.h>
  108. #include <netinet/ip.h>
  109. #include <netinet/ip_encap.h>
  110. #include <netinet/ip_mroute.h>
  111. #include <netinet/ip_var.h>
  112. #include <netinet/ip_options.h>
  113. #include <netinet/pim.h>
  114. #include <netinet/pim_var.h>
  115. #include <netinet/udp.h>
  116. #include <machine/in_cksum.h>
  117. #ifndef KTR_IPMF
  118. #define KTR_IPMF KTR_INET
  119. #endif
  120. #define VIFI_INVALID ((vifi_t) -1)
  121. static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast forwarding cache");
  122. /*
  123. * Locking. We use two locks: one for the virtual interface table and
  124. * one for the forwarding table. These locks may be nested in which case
  125. * the VIF lock must always be taken first. Note that each lock is used
  126. * to cover not only the specific data structure but also related data
  127. * structures.
  128. */
  129. static struct rwlock mrouter_lock;
  130. #define MRW_RLOCK() rw_rlock(&mrouter_lock)
  131. #define MRW_WLOCK() rw_wlock(&mrouter_lock)
  132. #define MRW_RUNLOCK() rw_runlock(&mrouter_lock)
  133. #define MRW_WUNLOCK() rw_wunlock(&mrouter_lock)
  134. #define MRW_UNLOCK() rw_unlock(&mrouter_lock)
  135. #define MRW_LOCK_ASSERT() rw_assert(&mrouter_lock, RA_LOCKED)
  136. #define MRW_WLOCK_ASSERT() rw_assert(&mrouter_lock, RA_WLOCKED)
  137. #define MRW_LOCK_TRY_UPGRADE() rw_try_upgrade(&mrouter_lock)
  138. #define MRW_WOWNED() rw_wowned(&mrouter_lock)
  139. #define MRW_LOCK_INIT() \
  140. rw_init(&mrouter_lock, "IPv4 multicast forwarding")
  141. #define MRW_LOCK_DESTROY() rw_destroy(&mrouter_lock)
  142. static int ip_mrouter_cnt; /* # of vnets with active mrouters */
  143. static int ip_mrouter_unloading; /* Allow no more V_ip_mrouter sockets */
  144. VNET_PCPUSTAT_DEFINE_STATIC(struct mrtstat, mrtstat);
  145. VNET_PCPUSTAT_SYSINIT(mrtstat);
  146. VNET_PCPUSTAT_SYSUNINIT(mrtstat);
  147. SYSCTL_VNET_PCPUSTAT(_net_inet_ip, OID_AUTO, mrtstat, struct mrtstat,
  148. mrtstat, "IPv4 Multicast Forwarding Statistics (struct mrtstat, "
  149. "netinet/ip_mroute.h)");
  150. VNET_DEFINE_STATIC(u_long, mfchash);
  151. #define V_mfchash VNET(mfchash)
  152. #define MFCHASH(a, g) \
  153. ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
  154. ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & V_mfchash)
  155. #define MFCHASHSIZE 256
  156. static u_long mfchashsize = MFCHASHSIZE; /* Hash size */
  157. SYSCTL_ULONG(_net_inet_ip, OID_AUTO, mfchashsize, CTLFLAG_RDTUN,
  158. &mfchashsize, 0, "IPv4 Multicast Forwarding Table hash size");
  159. VNET_DEFINE_STATIC(u_char *, nexpire); /* 0..mfchashsize-1 */
  160. #define V_nexpire VNET(nexpire)
  161. VNET_DEFINE_STATIC(LIST_HEAD(mfchashhdr, mfc)*, mfchashtbl);
  162. #define V_mfchashtbl VNET(mfchashtbl)
  163. VNET_DEFINE_STATIC(struct taskqueue *, task_queue);
  164. #define V_task_queue VNET(task_queue)
  165. VNET_DEFINE_STATIC(struct task, task);
  166. #define V_task VNET(task)
  167. VNET_DEFINE_STATIC(vifi_t, numvifs);
  168. #define V_numvifs VNET(numvifs)
  169. VNET_DEFINE_STATIC(struct vif *, viftable);
  170. #define V_viftable VNET(viftable)
  171. static eventhandler_tag if_detach_event_tag = NULL;
  172. VNET_DEFINE_STATIC(struct callout, expire_upcalls_ch);
  173. #define V_expire_upcalls_ch VNET(expire_upcalls_ch)
  174. VNET_DEFINE_STATIC(struct mtx, buf_ring_mtx);
  175. #define V_buf_ring_mtx VNET(buf_ring_mtx)
  176. #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
  177. #define UPCALL_EXPIRE 6 /* number of timeouts */
  178. /*
  179. * Bandwidth meter variables and constants
  180. */
  181. static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
  182. /*
  183. * Pending upcalls are stored in a ring which is flushed when
  184. * full, or periodically
  185. */
  186. VNET_DEFINE_STATIC(struct callout, bw_upcalls_ch);
  187. #define V_bw_upcalls_ch VNET(bw_upcalls_ch)
  188. VNET_DEFINE_STATIC(struct buf_ring *, bw_upcalls_ring);
  189. #define V_bw_upcalls_ring VNET(bw_upcalls_ring)
  190. VNET_DEFINE_STATIC(struct mtx, bw_upcalls_ring_mtx);
  191. #define V_bw_upcalls_ring_mtx VNET(bw_upcalls_ring_mtx)
  192. #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
  193. VNET_PCPUSTAT_DEFINE_STATIC(struct pimstat, pimstat);
  194. VNET_PCPUSTAT_SYSINIT(pimstat);
  195. VNET_PCPUSTAT_SYSUNINIT(pimstat);
  196. SYSCTL_NODE(_net_inet, IPPROTO_PIM, pim, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  197. "PIM");
  198. SYSCTL_VNET_PCPUSTAT(_net_inet_pim, PIMCTL_STATS, stats, struct pimstat,
  199. pimstat, "PIM Statistics (struct pimstat, netinet/pim_var.h)");
  200. static u_long pim_squelch_wholepkt = 0;
  201. SYSCTL_ULONG(_net_inet_pim, OID_AUTO, squelch_wholepkt, CTLFLAG_RWTUN,
  202. &pim_squelch_wholepkt, 0,
  203. "Disable IGMP_WHOLEPKT notifications if rendezvous point is unspecified");
  204. static const struct encaptab *pim_encap_cookie;
  205. static int pim_encapcheck(const struct mbuf *, int, int, void *);
  206. static int pim_input(struct mbuf *, int, int, void *);
  207. extern int in_mcast_loop;
  208. static const struct encap_config ipv4_encap_cfg = {
  209. .proto = IPPROTO_PIM,
  210. .min_length = sizeof(struct ip) + PIM_MINLEN,
  211. .exact_match = 8,
  212. .check = pim_encapcheck,
  213. .input = pim_input
  214. };
  215. /*
  216. * Note: the PIM Register encapsulation adds the following in front of a
  217. * data packet:
  218. *
  219. * struct pim_encap_hdr {
  220. * struct ip ip;
  221. * struct pim_encap_pimhdr pim;
  222. * }
  223. *
  224. */
  225. struct pim_encap_pimhdr {
  226. struct pim pim;
  227. uint32_t flags;
  228. };
  229. #define PIM_ENCAP_TTL 64
  230. static struct ip pim_encap_iphdr = {
  231. #if BYTE_ORDER == LITTLE_ENDIAN
  232. sizeof(struct ip) >> 2,
  233. IPVERSION,
  234. #else
  235. IPVERSION,
  236. sizeof(struct ip) >> 2,
  237. #endif
  238. 0, /* tos */
  239. sizeof(struct ip), /* total length */
  240. 0, /* id */
  241. 0, /* frag offset */
  242. PIM_ENCAP_TTL,
  243. IPPROTO_PIM,
  244. 0, /* checksum */
  245. };
  246. static struct pim_encap_pimhdr pim_encap_pimhdr = {
  247. {
  248. PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
  249. 0, /* reserved */
  250. 0, /* checksum */
  251. },
  252. 0 /* flags */
  253. };
  254. VNET_DEFINE_STATIC(vifi_t, reg_vif_num) = VIFI_INVALID;
  255. #define V_reg_vif_num VNET(reg_vif_num)
  256. VNET_DEFINE_STATIC(struct ifnet *, multicast_register_if);
  257. #define V_multicast_register_if VNET(multicast_register_if)
  258. /*
  259. * Private variables.
  260. */
  261. static u_long X_ip_mcast_src(int);
  262. static int X_ip_mforward(struct ip *, struct ifnet *, struct mbuf *,
  263. struct ip_moptions *);
  264. static int X_ip_mrouter_done(void);
  265. static int X_ip_mrouter_get(struct socket *, struct sockopt *);
  266. static int X_ip_mrouter_set(struct socket *, struct sockopt *);
  267. static int X_legal_vif_num(int);
  268. static int X_mrt_ioctl(u_long, caddr_t, int);
  269. static int add_bw_upcall(struct bw_upcall *);
  270. static int add_mfc(struct mfcctl2 *);
  271. static int add_vif(struct vifctl *);
  272. static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *);
  273. static void bw_meter_geq_receive_packet(struct bw_meter *, int,
  274. struct timeval *);
  275. static void bw_upcalls_send(void);
  276. static int del_bw_upcall(struct bw_upcall *);
  277. static int del_mfc(struct mfcctl2 *);
  278. static int del_vif(vifi_t);
  279. static int del_vif_locked(vifi_t, struct ifnet **, struct ifnet **);
  280. static void expire_bw_upcalls_send(void *);
  281. static void expire_mfc(struct mfc *);
  282. static void expire_upcalls(void *);
  283. static void free_bw_list(struct bw_meter *);
  284. static int get_sg_cnt(struct sioc_sg_req *);
  285. static int get_vif_cnt(struct sioc_vif_req *);
  286. static void if_detached_event(void *, struct ifnet *);
  287. static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
  288. static int ip_mrouter_init(struct socket *, int);
  289. static __inline struct mfc *
  290. mfc_find(struct in_addr *, struct in_addr *);
  291. static void phyint_send(struct ip *, struct vif *, struct mbuf *);
  292. static struct mbuf *
  293. pim_register_prepare(struct ip *, struct mbuf *);
  294. static int pim_register_send(struct ip *, struct vif *,
  295. struct mbuf *, struct mfc *);
  296. static int pim_register_send_rp(struct ip *, struct vif *,
  297. struct mbuf *, struct mfc *);
  298. static int pim_register_send_upcall(struct ip *, struct vif *,
  299. struct mbuf *, struct mfc *);
  300. static void send_packet(struct vif *, struct mbuf *);
  301. static int set_api_config(uint32_t *);
  302. static int set_assert(int);
  303. static int socket_send(struct socket *, struct mbuf *,
  304. struct sockaddr_in *);
  305. /*
  306. * Kernel multicast forwarding API capabilities and setup.
  307. * If more API capabilities are added to the kernel, they should be
  308. * recorded in `mrt_api_support'.
  309. */
  310. #define MRT_API_VERSION 0x0305
  311. static const int mrt_api_version = MRT_API_VERSION;
  312. static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
  313. MRT_MFC_FLAGS_BORDER_VIF |
  314. MRT_MFC_RP |
  315. MRT_MFC_BW_UPCALL);
  316. VNET_DEFINE_STATIC(uint32_t, mrt_api_config);
  317. #define V_mrt_api_config VNET(mrt_api_config)
  318. VNET_DEFINE_STATIC(int, pim_assert_enabled);
  319. #define V_pim_assert_enabled VNET(pim_assert_enabled)
  320. static struct timeval pim_assert_interval = { 3, 0 }; /* Rate limit */
  321. /*
  322. * Find a route for a given origin IP address and multicast group address.
  323. * Statistics must be updated by the caller.
  324. */
  325. static __inline struct mfc *
  326. mfc_find(struct in_addr *o, struct in_addr *g)
  327. {
  328. struct mfc *rt;
  329. /*
  330. * Might be called both RLOCK and WLOCK.
  331. * Check if any, it's caller responsibility
  332. * to choose correct option.
  333. */
  334. MRW_LOCK_ASSERT();
  335. LIST_FOREACH(rt, &V_mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
  336. if (in_hosteq(rt->mfc_origin, *o) &&
  337. in_hosteq(rt->mfc_mcastgrp, *g) &&
  338. buf_ring_empty(rt->mfc_stall_ring))
  339. break;
  340. }
  341. return (rt);
  342. }
  343. static __inline struct mfc *
  344. mfc_alloc(void)
  345. {
  346. struct mfc *rt;
  347. rt = malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT | M_ZERO);
  348. if (rt == NULL)
  349. return rt;
  350. rt->mfc_stall_ring = buf_ring_alloc(MAX_UPQ, M_MRTABLE,
  351. M_NOWAIT, &V_buf_ring_mtx);
  352. if (rt->mfc_stall_ring == NULL) {
  353. free(rt, M_MRTABLE);
  354. return NULL;
  355. }
  356. return rt;
  357. }
  358. /*
  359. * Handle MRT setsockopt commands to modify the multicast forwarding tables.
  360. */
  361. static int
  362. X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
  363. {
  364. int error, optval;
  365. vifi_t vifi;
  366. struct vifctl vifc;
  367. struct mfcctl2 mfc;
  368. struct bw_upcall bw_upcall;
  369. uint32_t i;
  370. if (so != V_ip_mrouter && sopt->sopt_name != MRT_INIT)
  371. return EPERM;
  372. error = 0;
  373. switch (sopt->sopt_name) {
  374. case MRT_INIT:
  375. error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
  376. if (error)
  377. break;
  378. error = ip_mrouter_init(so, optval);
  379. break;
  380. case MRT_DONE:
  381. error = ip_mrouter_done();
  382. break;
  383. case MRT_ADD_VIF:
  384. error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
  385. if (error)
  386. break;
  387. error = add_vif(&vifc);
  388. break;
  389. case MRT_DEL_VIF:
  390. error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
  391. if (error)
  392. break;
  393. error = del_vif(vifi);
  394. break;
  395. case MRT_ADD_MFC:
  396. case MRT_DEL_MFC:
  397. /*
  398. * select data size depending on API version.
  399. */
  400. if (sopt->sopt_name == MRT_ADD_MFC &&
  401. V_mrt_api_config & MRT_API_FLAGS_ALL) {
  402. error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2),
  403. sizeof(struct mfcctl2));
  404. } else {
  405. error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl),
  406. sizeof(struct mfcctl));
  407. bzero((caddr_t)&mfc + sizeof(struct mfcctl),
  408. sizeof(mfc) - sizeof(struct mfcctl));
  409. }
  410. if (error)
  411. break;
  412. if (sopt->sopt_name == MRT_ADD_MFC)
  413. error = add_mfc(&mfc);
  414. else
  415. error = del_mfc(&mfc);
  416. break;
  417. case MRT_ASSERT:
  418. error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval);
  419. if (error)
  420. break;
  421. set_assert(optval);
  422. break;
  423. case MRT_API_CONFIG:
  424. error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
  425. if (!error)
  426. error = set_api_config(&i);
  427. if (!error)
  428. error = sooptcopyout(sopt, &i, sizeof i);
  429. break;
  430. case MRT_ADD_BW_UPCALL:
  431. case MRT_DEL_BW_UPCALL:
  432. error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall,
  433. sizeof bw_upcall);
  434. if (error)
  435. break;
  436. if (sopt->sopt_name == MRT_ADD_BW_UPCALL)
  437. error = add_bw_upcall(&bw_upcall);
  438. else
  439. error = del_bw_upcall(&bw_upcall);
  440. break;
  441. default:
  442. error = EOPNOTSUPP;
  443. break;
  444. }
  445. return error;
  446. }
  447. /*
  448. * Handle MRT getsockopt commands
  449. */
  450. static int
  451. X_ip_mrouter_get(struct socket *so, struct sockopt *sopt)
  452. {
  453. int error;
  454. switch (sopt->sopt_name) {
  455. case MRT_VERSION:
  456. error = sooptcopyout(sopt, &mrt_api_version,
  457. sizeof mrt_api_version);
  458. break;
  459. case MRT_ASSERT:
  460. error = sooptcopyout(sopt, &V_pim_assert_enabled,
  461. sizeof V_pim_assert_enabled);
  462. break;
  463. case MRT_API_SUPPORT:
  464. error = sooptcopyout(sopt, &mrt_api_support,
  465. sizeof mrt_api_support);
  466. break;
  467. case MRT_API_CONFIG:
  468. error = sooptcopyout(sopt, &V_mrt_api_config,
  469. sizeof V_mrt_api_config);
  470. break;
  471. default:
  472. error = EOPNOTSUPP;
  473. break;
  474. }
  475. return error;
  476. }
  477. /*
  478. * Handle ioctl commands to obtain information from the cache
  479. */
  480. static int
  481. X_mrt_ioctl(u_long cmd, caddr_t data, int fibnum __unused)
  482. {
  483. int error;
  484. /*
  485. * Currently the only function calling this ioctl routine is rtioctl_fib().
  486. * Typically, only root can create the raw socket in order to execute
  487. * this ioctl method, however the request might be coming from a prison
  488. */
  489. error = priv_check(curthread, PRIV_NETINET_MROUTE);
  490. if (error)
  491. return (error);
  492. switch (cmd) {
  493. case (SIOCGETVIFCNT):
  494. error = get_vif_cnt((struct sioc_vif_req *)data);
  495. break;
  496. case (SIOCGETSGCNT):
  497. error = get_sg_cnt((struct sioc_sg_req *)data);
  498. break;
  499. default:
  500. error = EINVAL;
  501. break;
  502. }
  503. return error;
  504. }
  505. /*
  506. * returns the packet, byte, rpf-failure count for the source group provided
  507. */
  508. static int
  509. get_sg_cnt(struct sioc_sg_req *req)
  510. {
  511. struct mfc *rt;
  512. MRW_RLOCK();
  513. rt = mfc_find(&req->src, &req->grp);
  514. if (rt == NULL) {
  515. MRW_RUNLOCK();
  516. req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
  517. return EADDRNOTAVAIL;
  518. }
  519. req->pktcnt = rt->mfc_pkt_cnt;
  520. req->bytecnt = rt->mfc_byte_cnt;
  521. req->wrong_if = rt->mfc_wrong_if;
  522. MRW_RUNLOCK();
  523. return 0;
  524. }
  525. /*
  526. * returns the input and output packet and byte counts on the vif provided
  527. */
  528. static int
  529. get_vif_cnt(struct sioc_vif_req *req)
  530. {
  531. vifi_t vifi = req->vifi;
  532. MRW_RLOCK();
  533. if (vifi >= V_numvifs) {
  534. MRW_RUNLOCK();
  535. return EINVAL;
  536. }
  537. mtx_lock_spin(&V_viftable[vifi].v_spin);
  538. req->icount = V_viftable[vifi].v_pkt_in;
  539. req->ocount = V_viftable[vifi].v_pkt_out;
  540. req->ibytes = V_viftable[vifi].v_bytes_in;
  541. req->obytes = V_viftable[vifi].v_bytes_out;
  542. mtx_unlock_spin(&V_viftable[vifi].v_spin);
  543. MRW_RUNLOCK();
  544. return 0;
  545. }
  546. static void
  547. if_detached_event(void *arg __unused, struct ifnet *ifp)
  548. {
  549. vifi_t vifi;
  550. u_long i, vifi_cnt = 0;
  551. struct ifnet *free_ptr, *multi_leave;
  552. MRW_WLOCK();
  553. if (V_ip_mrouter == NULL) {
  554. MRW_WUNLOCK();
  555. return;
  556. }
  557. /*
  558. * Tear down multicast forwarder state associated with this ifnet.
  559. * 1. Walk the vif list, matching vifs against this ifnet.
  560. * 2. Walk the multicast forwarding cache (mfc) looking for
  561. * inner matches with this vif's index.
  562. * 3. Expire any matching multicast forwarding cache entries.
  563. * 4. Free vif state. This should disable ALLMULTI on the interface.
  564. */
  565. restart:
  566. for (vifi = 0; vifi < V_numvifs; vifi++) {
  567. if (V_viftable[vifi].v_ifp != ifp)
  568. continue;
  569. for (i = 0; i < mfchashsize; i++) {
  570. struct mfc *rt, *nrt;
  571. LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
  572. if (rt->mfc_parent == vifi) {
  573. expire_mfc(rt);
  574. }
  575. }
  576. }
  577. del_vif_locked(vifi, &multi_leave, &free_ptr);
  578. if (free_ptr != NULL)
  579. vifi_cnt++;
  580. if (multi_leave) {
  581. MRW_WUNLOCK();
  582. if_allmulti(multi_leave, 0);
  583. MRW_WLOCK();
  584. goto restart;
  585. }
  586. }
  587. MRW_WUNLOCK();
  588. /*
  589. * Free IFP. We don't have to use free_ptr here as it is the same
  590. * that ifp. Perform free as many times as required in case
  591. * refcount is greater than 1.
  592. */
  593. for (i = 0; i < vifi_cnt; i++)
  594. if_free(ifp);
  595. }
  596. static void
  597. ip_mrouter_upcall_thread(void *arg, int pending __unused)
  598. {
  599. CURVNET_SET((struct vnet *) arg);
  600. MRW_WLOCK();
  601. bw_upcalls_send();
  602. MRW_WUNLOCK();
  603. CURVNET_RESTORE();
  604. }
  605. /*
  606. * Enable multicast forwarding.
  607. */
  608. static int
  609. ip_mrouter_init(struct socket *so, int version)
  610. {
  611. CTR2(KTR_IPMF, "%s: so %p", __func__, so);
  612. if (version != 1)
  613. return ENOPROTOOPT;
  614. MRW_WLOCK();
  615. if (ip_mrouter_unloading) {
  616. MRW_WUNLOCK();
  617. return ENOPROTOOPT;
  618. }
  619. if (V_ip_mrouter != NULL) {
  620. MRW_WUNLOCK();
  621. return EADDRINUSE;
  622. }
  623. V_mfchashtbl = hashinit_flags(mfchashsize, M_MRTABLE, &V_mfchash,
  624. HASH_NOWAIT);
  625. if (V_mfchashtbl == NULL) {
  626. MRW_WUNLOCK();
  627. return (ENOMEM);
  628. }
  629. /* Create upcall ring */
  630. mtx_init(&V_bw_upcalls_ring_mtx, "mroute upcall buf_ring mtx", NULL, MTX_DEF);
  631. V_bw_upcalls_ring = buf_ring_alloc(BW_UPCALLS_MAX, M_MRTABLE,
  632. M_NOWAIT, &V_bw_upcalls_ring_mtx);
  633. if (!V_bw_upcalls_ring) {
  634. MRW_WUNLOCK();
  635. return (ENOMEM);
  636. }
  637. TASK_INIT(&V_task, 0, ip_mrouter_upcall_thread, curvnet);
  638. taskqueue_cancel(V_task_queue, &V_task, NULL);
  639. taskqueue_unblock(V_task_queue);
  640. callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
  641. curvnet);
  642. callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
  643. curvnet);
  644. V_ip_mrouter = so;
  645. atomic_add_int(&ip_mrouter_cnt, 1);
  646. /* This is a mutex required by buf_ring init, but not used internally */
  647. mtx_init(&V_buf_ring_mtx, "mroute buf_ring mtx", NULL, MTX_DEF);
  648. MRW_WUNLOCK();
  649. CTR1(KTR_IPMF, "%s: done", __func__);
  650. return 0;
  651. }
  652. /*
  653. * Disable multicast forwarding.
  654. */
  655. static int
  656. X_ip_mrouter_done(void)
  657. {
  658. struct ifnet **ifps;
  659. int nifp;
  660. u_long i;
  661. vifi_t vifi;
  662. struct bw_upcall *bu;
  663. if (V_ip_mrouter == NULL)
  664. return (EINVAL);
  665. /*
  666. * Detach/disable hooks to the reset of the system.
  667. */
  668. V_ip_mrouter = NULL;
  669. atomic_subtract_int(&ip_mrouter_cnt, 1);
  670. V_mrt_api_config = 0;
  671. /*
  672. * Wait for all epoch sections to complete to ensure
  673. * V_ip_mrouter = NULL is visible to others.
  674. */
  675. NET_EPOCH_WAIT();
  676. /* Stop and drain task queue */
  677. taskqueue_block(V_task_queue);
  678. while (taskqueue_cancel(V_task_queue, &V_task, NULL)) {
  679. taskqueue_drain(V_task_queue, &V_task);
  680. }
  681. ifps = malloc(MAXVIFS * sizeof(*ifps), M_TEMP, M_WAITOK);
  682. MRW_WLOCK();
  683. taskqueue_cancel(V_task_queue, &V_task, NULL);
  684. /* Destroy upcall ring */
  685. while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
  686. free(bu, M_MRTABLE);
  687. }
  688. buf_ring_free(V_bw_upcalls_ring, M_MRTABLE);
  689. mtx_destroy(&V_bw_upcalls_ring_mtx);
  690. /*
  691. * For each phyint in use, prepare to disable promiscuous reception
  692. * of all IP multicasts. Defer the actual call until the lock is released;
  693. * just record the list of interfaces while locked. Some interfaces use
  694. * sx locks in their ioctl routines, which is not allowed while holding
  695. * a non-sleepable lock.
  696. */
  697. KASSERT(V_numvifs <= MAXVIFS, ("More vifs than possible"));
  698. for (vifi = 0, nifp = 0; vifi < V_numvifs; vifi++) {
  699. if (!in_nullhost(V_viftable[vifi].v_lcl_addr) &&
  700. !(V_viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
  701. ifps[nifp++] = V_viftable[vifi].v_ifp;
  702. }
  703. }
  704. bzero((caddr_t)V_viftable, sizeof(*V_viftable) * MAXVIFS);
  705. V_numvifs = 0;
  706. V_pim_assert_enabled = 0;
  707. callout_stop(&V_expire_upcalls_ch);
  708. callout_stop(&V_bw_upcalls_ch);
  709. /*
  710. * Free all multicast forwarding cache entries.
  711. * Do not use hashdestroy(), as we must perform other cleanup.
  712. */
  713. for (i = 0; i < mfchashsize; i++) {
  714. struct mfc *rt, *nrt;
  715. LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
  716. expire_mfc(rt);
  717. }
  718. }
  719. free(V_mfchashtbl, M_MRTABLE);
  720. V_mfchashtbl = NULL;
  721. bzero(V_nexpire, sizeof(V_nexpire[0]) * mfchashsize);
  722. V_reg_vif_num = VIFI_INVALID;
  723. mtx_destroy(&V_buf_ring_mtx);
  724. MRW_WUNLOCK();
  725. /*
  726. * Now drop our claim on promiscuous multicast on the interfaces recorded
  727. * above. This is safe to do now because ALLMULTI is reference counted.
  728. */
  729. for (vifi = 0; vifi < nifp; vifi++)
  730. if_allmulti(ifps[vifi], 0);
  731. free(ifps, M_TEMP);
  732. CTR1(KTR_IPMF, "%s: done", __func__);
  733. return 0;
  734. }
  735. /*
  736. * Set PIM assert processing global
  737. */
  738. static int
  739. set_assert(int i)
  740. {
  741. if ((i != 1) && (i != 0))
  742. return EINVAL;
  743. V_pim_assert_enabled = i;
  744. return 0;
  745. }
  746. /*
  747. * Configure API capabilities
  748. */
  749. int
  750. set_api_config(uint32_t *apival)
  751. {
  752. u_long i;
  753. /*
  754. * We can set the API capabilities only if it is the first operation
  755. * after MRT_INIT. I.e.:
  756. * - there are no vifs installed
  757. * - pim_assert is not enabled
  758. * - the MFC table is empty
  759. */
  760. if (V_numvifs > 0) {
  761. *apival = 0;
  762. return EPERM;
  763. }
  764. if (V_pim_assert_enabled) {
  765. *apival = 0;
  766. return EPERM;
  767. }
  768. MRW_RLOCK();
  769. for (i = 0; i < mfchashsize; i++) {
  770. if (LIST_FIRST(&V_mfchashtbl[i]) != NULL) {
  771. MRW_RUNLOCK();
  772. *apival = 0;
  773. return EPERM;
  774. }
  775. }
  776. MRW_RUNLOCK();
  777. V_mrt_api_config = *apival & mrt_api_support;
  778. *apival = V_mrt_api_config;
  779. return 0;
  780. }
  781. /*
  782. * Add a vif to the vif table
  783. */
  784. static int
  785. add_vif(struct vifctl *vifcp)
  786. {
  787. struct vif *vifp = V_viftable + vifcp->vifc_vifi;
  788. struct sockaddr_in sin = {sizeof sin, AF_INET};
  789. struct ifaddr *ifa;
  790. struct ifnet *ifp;
  791. int error;
  792. if (vifcp->vifc_vifi >= MAXVIFS)
  793. return EINVAL;
  794. /* rate limiting is no longer supported by this code */
  795. if (vifcp->vifc_rate_limit != 0) {
  796. log(LOG_ERR, "rate limiting is no longer supported\n");
  797. return EINVAL;
  798. }
  799. if (in_nullhost(vifcp->vifc_lcl_addr))
  800. return EADDRNOTAVAIL;
  801. /* Find the interface with an address in AF_INET family */
  802. if (vifcp->vifc_flags & VIFF_REGISTER) {
  803. /*
  804. * XXX: Because VIFF_REGISTER does not really need a valid
  805. * local interface (e.g. it could be 127.0.0.2), we don't
  806. * check its address.
  807. */
  808. ifp = NULL;
  809. } else {
  810. struct epoch_tracker et;
  811. sin.sin_addr = vifcp->vifc_lcl_addr;
  812. NET_EPOCH_ENTER(et);
  813. ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
  814. if (ifa == NULL) {
  815. NET_EPOCH_EXIT(et);
  816. return EADDRNOTAVAIL;
  817. }
  818. ifp = ifa->ifa_ifp;
  819. /* XXX FIXME we need to take a ref on ifp and cleanup properly! */
  820. NET_EPOCH_EXIT(et);
  821. }
  822. if ((vifcp->vifc_flags & VIFF_TUNNEL) != 0) {
  823. CTR1(KTR_IPMF, "%s: tunnels are no longer supported", __func__);
  824. return EOPNOTSUPP;
  825. } else if (vifcp->vifc_flags & VIFF_REGISTER) {
  826. ifp = V_multicast_register_if = if_alloc(IFT_LOOP);
  827. CTR2(KTR_IPMF, "%s: add register vif for ifp %p", __func__, ifp);
  828. if (V_reg_vif_num == VIFI_INVALID) {
  829. if_initname(V_multicast_register_if, "register_vif", 0);
  830. V_reg_vif_num = vifcp->vifc_vifi;
  831. }
  832. } else { /* Make sure the interface supports multicast */
  833. if ((ifp->if_flags & IFF_MULTICAST) == 0)
  834. return EOPNOTSUPP;
  835. /* Enable promiscuous reception of all IP multicasts from the if */
  836. error = if_allmulti(ifp, 1);
  837. if (error)
  838. return error;
  839. }
  840. MRW_WLOCK();
  841. if (!in_nullhost(vifp->v_lcl_addr)) {
  842. if (ifp)
  843. V_multicast_register_if = NULL;
  844. MRW_WUNLOCK();
  845. if (ifp)
  846. if_free(ifp);
  847. return EADDRINUSE;
  848. }
  849. vifp->v_flags = vifcp->vifc_flags;
  850. vifp->v_threshold = vifcp->vifc_threshold;
  851. vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
  852. vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
  853. vifp->v_ifp = ifp;
  854. /* initialize per vif pkt counters */
  855. vifp->v_pkt_in = 0;
  856. vifp->v_pkt_out = 0;
  857. vifp->v_bytes_in = 0;
  858. vifp->v_bytes_out = 0;
  859. sprintf(vifp->v_spin_name, "BM[%d] spin", vifcp->vifc_vifi);
  860. mtx_init(&vifp->v_spin, vifp->v_spin_name, NULL, MTX_SPIN);
  861. /* Adjust numvifs up if the vifi is higher than numvifs */
  862. if (V_numvifs <= vifcp->vifc_vifi)
  863. V_numvifs = vifcp->vifc_vifi + 1;
  864. MRW_WUNLOCK();
  865. CTR4(KTR_IPMF, "%s: add vif %d laddr 0x%08x thresh %x", __func__,
  866. (int)vifcp->vifc_vifi, ntohl(vifcp->vifc_lcl_addr.s_addr),
  867. (int)vifcp->vifc_threshold);
  868. return 0;
  869. }
  870. /*
  871. * Delete a vif from the vif table
  872. */
  873. static int
  874. del_vif_locked(vifi_t vifi, struct ifnet **ifp_multi_leave, struct ifnet **ifp_free)
  875. {
  876. struct vif *vifp;
  877. *ifp_free = NULL;
  878. *ifp_multi_leave = NULL;
  879. MRW_WLOCK_ASSERT();
  880. if (vifi >= V_numvifs) {
  881. return EINVAL;
  882. }
  883. vifp = &V_viftable[vifi];
  884. if (in_nullhost(vifp->v_lcl_addr)) {
  885. return EADDRNOTAVAIL;
  886. }
  887. if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
  888. *ifp_multi_leave = vifp->v_ifp;
  889. if (vifp->v_flags & VIFF_REGISTER) {
  890. V_reg_vif_num = VIFI_INVALID;
  891. if (vifp->v_ifp) {
  892. if (vifp->v_ifp == V_multicast_register_if)
  893. V_multicast_register_if = NULL;
  894. *ifp_free = vifp->v_ifp;
  895. }
  896. }
  897. mtx_destroy(&vifp->v_spin);
  898. bzero((caddr_t)vifp, sizeof (*vifp));
  899. CTR2(KTR_IPMF, "%s: delete vif %d", __func__, (int)vifi);
  900. /* Adjust numvifs down */
  901. for (vifi = V_numvifs; vifi > 0; vifi--)
  902. if (!in_nullhost(V_viftable[vifi-1].v_lcl_addr))
  903. break;
  904. V_numvifs = vifi;
  905. return 0;
  906. }
  907. static int
  908. del_vif(vifi_t vifi)
  909. {
  910. int cc;
  911. struct ifnet *free_ptr, *multi_leave;
  912. MRW_WLOCK();
  913. cc = del_vif_locked(vifi, &multi_leave, &free_ptr);
  914. MRW_WUNLOCK();
  915. if (multi_leave)
  916. if_allmulti(multi_leave, 0);
  917. if (free_ptr) {
  918. if_free(free_ptr);
  919. }
  920. return cc;
  921. }
  922. /*
  923. * update an mfc entry without resetting counters and S,G addresses.
  924. */
  925. static void
  926. update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
  927. {
  928. int i;
  929. rt->mfc_parent = mfccp->mfcc_parent;
  930. for (i = 0; i < V_numvifs; i++) {
  931. rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
  932. rt->mfc_flags[i] = mfccp->mfcc_flags[i] & V_mrt_api_config &
  933. MRT_MFC_FLAGS_ALL;
  934. }
  935. /* set the RP address */
  936. if (V_mrt_api_config & MRT_MFC_RP)
  937. rt->mfc_rp = mfccp->mfcc_rp;
  938. else
  939. rt->mfc_rp.s_addr = INADDR_ANY;
  940. }
  941. /*
  942. * fully initialize an mfc entry from the parameter.
  943. */
  944. static void
  945. init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
  946. {
  947. rt->mfc_origin = mfccp->mfcc_origin;
  948. rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
  949. update_mfc_params(rt, mfccp);
  950. /* initialize pkt counters per src-grp */
  951. rt->mfc_pkt_cnt = 0;
  952. rt->mfc_byte_cnt = 0;
  953. rt->mfc_wrong_if = 0;
  954. timevalclear(&rt->mfc_last_assert);
  955. }
  956. static void
  957. expire_mfc(struct mfc *rt)
  958. {
  959. struct rtdetq *rte;
  960. MRW_WLOCK_ASSERT();
  961. free_bw_list(rt->mfc_bw_meter_leq);
  962. free_bw_list(rt->mfc_bw_meter_geq);
  963. while (!buf_ring_empty(rt->mfc_stall_ring)) {
  964. rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
  965. if (rte) {
  966. m_freem(rte->m);
  967. free(rte, M_MRTABLE);
  968. }
  969. }
  970. buf_ring_free(rt->mfc_stall_ring, M_MRTABLE);
  971. LIST_REMOVE(rt, mfc_hash);
  972. free(rt, M_MRTABLE);
  973. }
  974. /*
  975. * Add an mfc entry
  976. */
  977. static int
  978. add_mfc(struct mfcctl2 *mfccp)
  979. {
  980. struct mfc *rt;
  981. struct rtdetq *rte;
  982. u_long hash = 0;
  983. u_short nstl;
  984. struct epoch_tracker et;
  985. MRW_WLOCK();
  986. rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
  987. /* If an entry already exists, just update the fields */
  988. if (rt) {
  989. CTR4(KTR_IPMF, "%s: update mfc orig 0x%08x group %lx parent %x",
  990. __func__, ntohl(mfccp->mfcc_origin.s_addr),
  991. (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
  992. mfccp->mfcc_parent);
  993. update_mfc_params(rt, mfccp);
  994. MRW_WUNLOCK();
  995. return (0);
  996. }
  997. /*
  998. * Find the entry for which the upcall was made and update
  999. */
  1000. nstl = 0;
  1001. hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
  1002. NET_EPOCH_ENTER(et);
  1003. LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
  1004. if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
  1005. in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
  1006. !buf_ring_empty(rt->mfc_stall_ring)) {
  1007. CTR5(KTR_IPMF,
  1008. "%s: add mfc orig 0x%08x group %lx parent %x qh %p",
  1009. __func__, ntohl(mfccp->mfcc_origin.s_addr),
  1010. (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
  1011. mfccp->mfcc_parent,
  1012. rt->mfc_stall_ring);
  1013. if (nstl++)
  1014. CTR1(KTR_IPMF, "%s: multiple matches", __func__);
  1015. init_mfc_params(rt, mfccp);
  1016. rt->mfc_expire = 0; /* Don't clean this guy up */
  1017. V_nexpire[hash]--;
  1018. /* Free queued packets, but attempt to forward them first. */
  1019. while (!buf_ring_empty(rt->mfc_stall_ring)) {
  1020. rte = buf_ring_dequeue_mc(rt->mfc_stall_ring);
  1021. if (rte->ifp != NULL)
  1022. ip_mdq(rte->m, rte->ifp, rt, -1);
  1023. m_freem(rte->m);
  1024. free(rte, M_MRTABLE);
  1025. }
  1026. }
  1027. }
  1028. NET_EPOCH_EXIT(et);
  1029. /*
  1030. * It is possible that an entry is being inserted without an upcall
  1031. */
  1032. if (nstl == 0) {
  1033. CTR1(KTR_IPMF, "%s: adding mfc w/o upcall", __func__);
  1034. LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash) {
  1035. if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
  1036. in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
  1037. init_mfc_params(rt, mfccp);
  1038. if (rt->mfc_expire)
  1039. V_nexpire[hash]--;
  1040. rt->mfc_expire = 0;
  1041. break; /* XXX */
  1042. }
  1043. }
  1044. if (rt == NULL) { /* no upcall, so make a new entry */
  1045. rt = mfc_alloc();
  1046. if (rt == NULL) {
  1047. MRW_WUNLOCK();
  1048. return (ENOBUFS);
  1049. }
  1050. init_mfc_params(rt, mfccp);
  1051. rt->mfc_expire = 0;
  1052. rt->mfc_bw_meter_leq = NULL;
  1053. rt->mfc_bw_meter_geq = NULL;
  1054. /* insert new entry at head of hash chain */
  1055. LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
  1056. }
  1057. }
  1058. MRW_WUNLOCK();
  1059. return (0);
  1060. }
  1061. /*
  1062. * Delete an mfc entry
  1063. */
  1064. static int
  1065. del_mfc(struct mfcctl2 *mfccp)
  1066. {
  1067. struct in_addr origin;
  1068. struct in_addr mcastgrp;
  1069. struct mfc *rt;
  1070. origin = mfccp->mfcc_origin;
  1071. mcastgrp = mfccp->mfcc_mcastgrp;
  1072. CTR3(KTR_IPMF, "%s: delete mfc orig 0x%08x group %lx", __func__,
  1073. ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
  1074. MRW_WLOCK();
  1075. LIST_FOREACH(rt, &V_mfchashtbl[MFCHASH(origin, mcastgrp)], mfc_hash) {
  1076. if (in_hosteq(rt->mfc_origin, origin) &&
  1077. in_hosteq(rt->mfc_mcastgrp, mcastgrp))
  1078. break;
  1079. }
  1080. if (rt == NULL) {
  1081. MRW_WUNLOCK();
  1082. return EADDRNOTAVAIL;
  1083. }
  1084. expire_mfc(rt);
  1085. MRW_WUNLOCK();
  1086. return (0);
  1087. }
  1088. /*
  1089. * Send a message to the routing daemon on the multicast routing socket.
  1090. */
  1091. static int
  1092. socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
  1093. {
  1094. if (s) {
  1095. SOCKBUF_LOCK(&s->so_rcv);
  1096. if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
  1097. NULL) != 0) {
  1098. sorwakeup_locked(s);
  1099. return 0;
  1100. }
  1101. soroverflow_locked(s);
  1102. }
  1103. m_freem(mm);
  1104. return -1;
  1105. }
  1106. /*
  1107. * IP multicast forwarding function. This function assumes that the packet
  1108. * pointed to by "ip" has arrived on (or is about to be sent to) the interface
  1109. * pointed to by "ifp", and the packet is to be relayed to other networks
  1110. * that have members of the packet's destination IP multicast group.
  1111. *
  1112. * The packet is returned unscathed to the caller, unless it is
  1113. * erroneous, in which case a non-zero return value tells the caller to
  1114. * discard it.
  1115. */
  1116. #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
  1117. static int
  1118. X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
  1119. struct ip_moptions *imo)
  1120. {
  1121. struct mfc *rt;
  1122. int error;
  1123. vifi_t vifi;
  1124. struct mbuf *mb0;
  1125. struct rtdetq *rte;
  1126. u_long hash;
  1127. int hlen;
  1128. CTR3(KTR_IPMF, "ip_mforward: delete mfc orig 0x%08x group %lx ifp %p",
  1129. ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr), ifp);
  1130. if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
  1131. ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
  1132. /*
  1133. * Packet arrived via a physical interface or
  1134. * an encapsulated tunnel or a register_vif.
  1135. */
  1136. } else {
  1137. /*
  1138. * Packet arrived through a source-route tunnel.
  1139. * Source-route tunnels are no longer supported.
  1140. */
  1141. return (1);
  1142. }
  1143. /*
  1144. * BEGIN: MCAST ROUTING HOT PATH
  1145. */
  1146. MRW_RLOCK();
  1147. if (imo && ((vifi = imo->imo_multicast_vif) < V_numvifs)) {
  1148. if (ip->ip_ttl < MAXTTL)
  1149. ip->ip_ttl++; /* compensate for -1 in *_send routines */
  1150. error = ip_mdq(m, ifp, NULL, vifi);
  1151. MRW_RUNLOCK();
  1152. return error;
  1153. }
  1154. /*
  1155. * Don't forward a packet with time-to-live of zero or one,
  1156. * or a packet destined to a local-only group.
  1157. */
  1158. if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ntohl(ip->ip_dst.s_addr))) {
  1159. MRW_RUNLOCK();
  1160. return 0;
  1161. }
  1162. mfc_find_retry:
  1163. /*
  1164. * Determine forwarding vifs from the forwarding cache table
  1165. */
  1166. MRTSTAT_INC(mrts_mfc_lookups);
  1167. rt = mfc_find(&ip->ip_src, &ip->ip_dst);
  1168. /* Entry exists, so forward if necessary */
  1169. if (rt != NULL) {
  1170. error = ip_mdq(m, ifp, rt, -1);
  1171. /* Generic unlock here as we might release R or W lock */
  1172. MRW_UNLOCK();
  1173. return error;
  1174. }
  1175. /*
  1176. * END: MCAST ROUTING HOT PATH
  1177. */
  1178. /* Further processing must be done with WLOCK taken */
  1179. if ((MRW_WOWNED() == 0) && (MRW_LOCK_TRY_UPGRADE() == 0)) {
  1180. MRW_RUNLOCK();
  1181. MRW_WLOCK();
  1182. goto mfc_find_retry;
  1183. }
  1184. /*
  1185. * If we don't have a route for packet's origin,
  1186. * Make a copy of the packet & send message to routing daemon
  1187. */
  1188. hlen = ip->ip_hl << 2;
  1189. MRTSTAT_INC(mrts_mfc_misses);
  1190. MRTSTAT_INC(mrts_no_route);
  1191. CTR2(KTR_IPMF, "ip_mforward: no mfc for (0x%08x,%lx)",
  1192. ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr));
  1193. /*
  1194. * Allocate mbufs early so that we don't do extra work if we are
  1195. * just going to fail anyway. Make sure to pullup the header so
  1196. * that other people can't step on it.
  1197. */
  1198. rte = malloc((sizeof *rte), M_MRTABLE, M_NOWAIT|M_ZERO);
  1199. if (rte == NULL) {
  1200. MRW_WUNLOCK();
  1201. return ENOBUFS;
  1202. }
  1203. mb0 = m_copypacket(m, M_NOWAIT);
  1204. if (mb0 && (!M_WRITABLE(mb0) || mb0->m_len < hlen))
  1205. mb0 = m_pullup(mb0, hlen);
  1206. if (mb0 == NULL) {
  1207. free(rte, M_MRTABLE);
  1208. MRW_WUNLOCK();
  1209. return ENOBUFS;
  1210. }
  1211. /* is there an upcall waiting for this flow ? */
  1212. hash = MFCHASH(ip->ip_src, ip->ip_dst);
  1213. LIST_FOREACH(rt, &V_mfchashtbl[hash], mfc_hash)
  1214. {
  1215. if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
  1216. in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
  1217. !buf_ring_empty(rt->mfc_stall_ring))
  1218. break;
  1219. }
  1220. if (rt == NULL) {
  1221. int i;
  1222. struct igmpmsg *im;
  1223. struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
  1224. struct mbuf *mm;
  1225. /*
  1226. * Locate the vifi for the incoming interface for this packet.
  1227. * If none found, drop packet.
  1228. */
  1229. for (vifi = 0; vifi < V_numvifs &&
  1230. V_viftable[vifi].v_ifp != ifp; vifi++)
  1231. ;
  1232. if (vifi >= V_numvifs) /* vif not found, drop packet */
  1233. goto non_fatal;
  1234. /* no upcall, so make a new entry */
  1235. rt = mfc_alloc();
  1236. if (rt == NULL)
  1237. goto fail;
  1238. /* Make a copy of the header to send to the user level process */
  1239. mm = m_copym(mb0, 0, hlen, M_NOWAIT);
  1240. if (mm == NULL)
  1241. goto fail1;
  1242. /*
  1243. * Send message to routing daemon to install
  1244. * a route into the kernel table
  1245. */
  1246. im = mtod(mm, struct igmpmsg*);
  1247. im->im_msgtype = IGMPMSG_NOCACHE;
  1248. im->im_mbz = 0;
  1249. im->im_vif = vifi;
  1250. MRTSTAT_INC(mrts_upcalls);
  1251. k_igmpsrc.sin_addr = ip->ip_src;
  1252. if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
  1253. CTR0(KTR_IPMF, "ip_mforward: socket queue full");
  1254. MRTSTAT_INC(mrts_upq_sockfull);
  1255. fail1: free(rt, M_MRTABLE);
  1256. fail: free(rte, M_MRTABLE);
  1257. m_freem(mb0);
  1258. MRW_WUNLOCK();
  1259. return ENOBUFS;
  1260. }
  1261. /* insert new entry at head of hash chain */
  1262. rt->mfc_origin.s_addr = ip->ip_src.s_addr;
  1263. rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
  1264. rt->mfc_expire = UPCALL_EXPIRE;
  1265. V_nexpire[hash]++;
  1266. for (i = 0; i < V_numvifs; i++) {
  1267. rt->mfc_ttls[i] = 0;
  1268. rt->mfc_flags[i] = 0;
  1269. }
  1270. rt->mfc_parent = -1;
  1271. /* clear the RP address */
  1272. rt->mfc_rp.s_addr = INADDR_ANY;
  1273. rt->mfc_bw_meter_leq = NULL;
  1274. rt->mfc_bw_meter_geq = NULL;
  1275. /* initialize pkt counters per src-grp */
  1276. rt->mfc_pkt_cnt = 0;
  1277. rt->mfc_byte_cnt = 0;
  1278. rt->mfc_wrong_if = 0;
  1279. timevalclear(&rt->mfc_last_assert);
  1280. buf_ring_enqueue(rt->mfc_stall_ring, rte);
  1281. /* Add RT to hashtable as it didn't exist before */
  1282. LIST_INSERT_HEAD(&V_mfchashtbl[hash], rt, mfc_hash);
  1283. } else {
  1284. /* determine if queue has overflowed */
  1285. if (buf_ring_full(rt->mfc_stall_ring)) {
  1286. MRTSTAT_INC(mrts_upq_ovflw);
  1287. non_fatal: free(rte, M_MRTABLE);
  1288. m_freem(mb0);
  1289. MRW_WUNLOCK();
  1290. return (0);
  1291. }
  1292. buf_ring_enqueue(rt->mfc_stall_ring, rte);
  1293. }
  1294. rte->m = mb0;
  1295. rte->ifp = ifp;
  1296. MRW_WUNLOCK();
  1297. return 0;
  1298. }
  1299. /*
  1300. * Clean up the cache entry if upcall is not serviced
  1301. */
  1302. static void
  1303. expire_upcalls(void *arg)
  1304. {
  1305. u_long i;
  1306. CURVNET_SET((struct vnet *) arg);
  1307. /*This callout is always run with MRW_WLOCK taken. */
  1308. for (i = 0; i < mfchashsize; i++) {
  1309. struct mfc *rt, *nrt;
  1310. if (V_nexpire[i] == 0)
  1311. continue;
  1312. LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) {
  1313. if (buf_ring_empty(rt->mfc_stall_ring))
  1314. continue;
  1315. if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
  1316. continue;
  1317. MRTSTAT_INC(mrts_cache_cleanups);
  1318. CTR3(KTR_IPMF, "%s: expire (%lx, %lx)", __func__,
  1319. (u_long)ntohl(rt->mfc_origin.s_addr),
  1320. (u_long)ntohl(rt->mfc_mcastgrp.s_addr));
  1321. expire_mfc(rt);
  1322. }
  1323. }
  1324. callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls,
  1325. curvnet);
  1326. CURVNET_RESTORE();
  1327. }
  1328. /*
  1329. * Packet forwarding routine once entry in the cache is made
  1330. */
  1331. static int
  1332. ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
  1333. {
  1334. struct ip *ip = mtod(m, struct ip *);
  1335. vifi_t vifi;
  1336. int plen = ntohs(ip->ip_len);
  1337. MRW_LOCK_ASSERT();
  1338. NET_EPOCH_ASSERT();
  1339. /*
  1340. * If xmt_vif is not -1, send on only the requested vif.
  1341. *
  1342. * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
  1343. */
  1344. if (xmt_vif < V_numvifs) {
  1345. if (V_viftable[xmt_vif].v_flags & VIFF_REGISTER)
  1346. pim_register_send(ip, V_viftable + xmt_vif, m, rt);
  1347. else
  1348. phyint_send(ip, V_viftable + xmt_vif, m);
  1349. return 1;
  1350. }
  1351. /*
  1352. * Don't forward if it didn't arrive from the parent vif for its origin.
  1353. */
  1354. vifi = rt->mfc_parent;
  1355. if ((vifi >= V_numvifs) || (V_viftable[vifi].v_ifp != ifp)) {
  1356. CTR4(KTR_IPMF, "%s: rx on wrong ifp %p (vifi %d, v_ifp %p)",
  1357. __func__, ifp, (int)vifi, V_viftable[vifi].v_ifp);
  1358. MRTSTAT_INC(mrts_wrong_if);
  1359. ++rt->mfc_wrong_if;
  1360. /*
  1361. * If we are doing PIM assert processing, send a message
  1362. * to the routing daemon.
  1363. *
  1364. * XXX: A PIM-SM router needs the WRONGVIF detection so it
  1365. * can complete the SPT switch, regardless of the type
  1366. * of the iif (broadcast media, GRE tunnel, etc).
  1367. */
  1368. if (V_pim_assert_enabled && (vifi < V_numvifs) &&
  1369. V_viftable[vifi].v_ifp) {
  1370. if (ifp == V_multicast_register_if)
  1371. PIMSTAT_INC(pims_rcv_registers_wrongiif);
  1372. /* Get vifi for the incoming packet */
  1373. for (vifi = 0; vifi < V_numvifs && V_viftable[vifi].v_ifp != ifp; vifi++)
  1374. ;
  1375. if (vifi >= V_numvifs)
  1376. return 0; /* The iif is not found: ignore the packet. */
  1377. if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
  1378. return 0; /* WRONGVIF disabled: ignore the packet */
  1379. if (ratecheck(&rt->mfc_last_assert, &pim_assert_interval)) {
  1380. struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
  1381. struct igmpmsg *im;
  1382. int hlen = ip->ip_hl << 2;
  1383. struct mbuf *mm = m_copym(m, 0, hlen, M_NOWAIT);
  1384. if (mm && (!M_WRITABLE(mm) || mm->m_len < hlen))
  1385. mm = m_pullup(mm, hlen);
  1386. if (mm == NULL)
  1387. return ENOBUFS;
  1388. im = mtod(mm, struct igmpmsg *);
  1389. im->im_msgtype = IGMPMSG_WRONGVIF;
  1390. im->im_mbz = 0;
  1391. im->im_vif = vifi;
  1392. MRTSTAT_INC(mrts_upcalls);
  1393. k_igmpsrc.sin_addr = im->im_src;
  1394. if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) {
  1395. CTR1(KTR_IPMF, "%s: socket queue full", __func__);
  1396. MRTSTAT_INC(mrts_upq_sockfull);
  1397. return ENOBUFS;
  1398. }
  1399. }
  1400. }
  1401. return 0;
  1402. }
  1403. /* If I sourced this packet, it counts as output, else it was input. */
  1404. mtx_lock_spin(&V_viftable[vifi].v_spin);
  1405. if (in_hosteq(ip->ip_src, V_viftable[vifi].v_lcl_addr)) {
  1406. V_viftable[vifi].v_pkt_out++;
  1407. V_viftable[vifi].v_bytes_out += plen;
  1408. } else {
  1409. V_viftable[vifi].v_pkt_in++;
  1410. V_viftable[vifi].v_bytes_in += plen;
  1411. }
  1412. mtx_unlock_spin(&V_viftable[vifi].v_spin);
  1413. rt->mfc_pkt_cnt++;
  1414. rt->mfc_byte_cnt += plen;
  1415. /*
  1416. * For each vif, decide if a copy of the packet should be forwarded.
  1417. * Forward if:
  1418. * - the ttl exceeds the vif's threshold
  1419. * - there are group members downstream on interface
  1420. */
  1421. for (vifi = 0; vifi < V_numvifs; vifi++)
  1422. if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
  1423. V_viftable[vifi].v_pkt_out++;
  1424. V_viftable[vifi].v_bytes_out += plen;
  1425. if (V_viftable[vifi].v_flags & VIFF_REGISTER)
  1426. pim_register_send(ip, V_viftable + vifi, m, rt);
  1427. else
  1428. phyint_send(ip, V_viftable + vifi, m);
  1429. }
  1430. /*
  1431. * Perform upcall-related bw measuring.
  1432. */
  1433. if ((rt->mfc_bw_meter_geq != NULL) || (rt->mfc_bw_meter_leq != NULL)) {
  1434. struct bw_meter *x;
  1435. struct timeval now;
  1436. microtime(&now);
  1437. /* Process meters for Greater-or-EQual case */
  1438. for (x = rt->mfc_bw_meter_geq; x != NULL; x = x->bm_mfc_next)
  1439. bw_meter_geq_receive_packet(x, plen, &now);
  1440. /* Process meters for Lower-or-EQual case */
  1441. for (x = rt->mfc_bw_meter_leq; x != NULL; x = x->bm_mfc_next) {
  1442. /*
  1443. * Record that a packet is received.
  1444. * Spin lock has to be taken as callout context
  1445. * (expire_bw_meter_leq) might modify these fields
  1446. * as well
  1447. */
  1448. mtx_lock_spin(&x->bm_spin);
  1449. x->bm_measured.b_packets++;
  1450. x->bm_measured.b_bytes += plen;
  1451. mtx_unlock_spin(&x->bm_spin);
  1452. }
  1453. }
  1454. return 0;
  1455. }
  1456. /*
  1457. * Check if a vif number is legal/ok. This is used by in_mcast.c.
  1458. */
  1459. static int
  1460. X_legal_vif_num(int vif)
  1461. {
  1462. int ret;
  1463. ret = 0;
  1464. if (vif < 0)
  1465. return (ret);
  1466. MRW_RLOCK();
  1467. if (vif < V_numvifs)
  1468. ret = 1;
  1469. MRW_RUNLOCK();
  1470. return (ret);
  1471. }
  1472. /*
  1473. * Return the local address used by this vif
  1474. */
  1475. static u_long
  1476. X_ip_mcast_src(int vifi)
  1477. {
  1478. in_addr_t addr;
  1479. addr = INADDR_ANY;
  1480. if (vifi < 0)
  1481. return (addr);
  1482. MRW_RLOCK();
  1483. if (vifi < V_numvifs)
  1484. addr = V_viftable[vifi].v_lcl_addr.s_addr;
  1485. MRW_RUNLOCK();
  1486. return (addr);
  1487. }
  1488. static void
  1489. phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
  1490. {
  1491. struct mbuf *mb_copy;
  1492. int hlen = ip->ip_hl << 2;
  1493. MRW_LOCK_ASSERT();
  1494. /*
  1495. * Make a new reference to the packet; make sure that
  1496. * the IP header is actually copied, not just referenced,
  1497. * so that ip_output() only scribbles on the copy.
  1498. */
  1499. mb_copy = m_copypacket(m, M_NOWAIT);
  1500. if (mb_copy && (!M_WRITABLE(mb_copy) || mb_copy->m_len < hlen))
  1501. mb_copy = m_pullup(mb_copy, hlen);
  1502. if (mb_copy == NULL)
  1503. return;
  1504. send_packet(vifp, mb_copy);
  1505. }
  1506. static void
  1507. send_packet(struct vif *vifp, struct mbuf *m)
  1508. {
  1509. struct ip_moptions imo;
  1510. int error __unused;
  1511. MRW_LOCK_ASSERT();
  1512. NET_EPOCH_ASSERT();
  1513. imo.imo_multicast_ifp = vifp->v_ifp;
  1514. imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
  1515. imo.imo_multicast_loop = !!in_mcast_loop;
  1516. imo.imo_multicast_vif = -1;
  1517. STAILQ_INIT(&imo.imo_head);
  1518. /*
  1519. * Re-entrancy should not be a problem here, because
  1520. * the packets that we send out and are looped back at us
  1521. * should get rejected because they appear to come from
  1522. * the loopback interface, thus preventing looping.
  1523. */
  1524. error = ip_output(m, NULL, NULL, IP_FORWARDING, &imo, NULL);
  1525. CTR3(KTR_IPMF, "%s: vif %td err %d", __func__,
  1526. (ptrdiff_t)(vifp - V_viftable), error);
  1527. }
  1528. /*
  1529. * Stubs for old RSVP socket shim implementation.
  1530. */
  1531. static int
  1532. X_ip_rsvp_vif(struct socket *so __unused, struct sockopt *sopt __unused)
  1533. {
  1534. return (EOPNOTSUPP);
  1535. }
  1536. static void
  1537. X_ip_rsvp_force_done(struct socket *so __unused)
  1538. {
  1539. }
  1540. static int
  1541. X_rsvp_input(struct mbuf **mp, int *offp, int proto)
  1542. {
  1543. struct mbuf *m;
  1544. m = *mp;
  1545. *mp = NULL;
  1546. if (!V_rsvp_on)
  1547. m_freem(m);
  1548. return (IPPROTO_DONE);
  1549. }
  1550. /*
  1551. * Code for bandwidth monitors
  1552. */
  1553. /*
  1554. * Define common interface for timeval-related methods
  1555. */
  1556. #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
  1557. #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
  1558. #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
  1559. static uint32_t
  1560. compute_bw_meter_flags(struct bw_upcall *req)
  1561. {
  1562. uint32_t flags = 0;
  1563. if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
  1564. flags |= BW_METER_UNIT_PACKETS;
  1565. if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
  1566. flags |= BW_METER_UNIT_BYTES;
  1567. if (req->bu_flags & BW_UPCALL_GEQ)
  1568. flags |= BW_METER_GEQ;
  1569. if (req->bu_flags & BW_UPCALL_LEQ)
  1570. flags |= BW_METER_LEQ;
  1571. return flags;
  1572. }
  1573. static void
  1574. expire_bw_meter_leq(void *arg)
  1575. {
  1576. struct bw_meter *x = arg;
  1577. struct timeval now;
  1578. /*
  1579. * INFO:
  1580. * callout is always executed with MRW_WLOCK taken
  1581. */
  1582. CURVNET_SET((struct vnet *)x->arg);
  1583. microtime(&now);
  1584. /*
  1585. * Test if we should deliver an upcall
  1586. */
  1587. if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
  1588. (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
  1589. ((x->bm_flags & BW_METER_UNIT_BYTES) &&
  1590. (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
  1591. /* Prepare an upcall for delivery */
  1592. bw_meter_prepare_upcall(x, &now);
  1593. }
  1594. /* Send all upcalls that are pending delivery */
  1595. taskqueue_enqueue(V_task_queue, &V_task);
  1596. /* Reset counters */
  1597. x->bm_start_time = now;
  1598. /* Spin lock has to be taken as ip_forward context
  1599. * might modify these fields as well
  1600. */
  1601. mtx_lock_spin(&x->bm_spin);
  1602. x->bm_measured.b_bytes = 0;
  1603. x->bm_measured.b_packets = 0;
  1604. mtx_unlock_spin(&x->bm_spin);
  1605. callout_schedule(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time));
  1606. CURVNET_RESTORE();
  1607. }
  1608. /*
  1609. * Add a bw_meter entry
  1610. */
  1611. static int
  1612. add_bw_upcall(struct bw_upcall *req)
  1613. {
  1614. struct mfc *mfc;
  1615. struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
  1616. BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
  1617. struct timeval now;
  1618. struct bw_meter *x, **bwm_ptr;
  1619. uint32_t flags;
  1620. if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
  1621. return EOPNOTSUPP;
  1622. /* Test if the flags are valid */
  1623. if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
  1624. return EINVAL;
  1625. if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
  1626. return EINVAL;
  1627. if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
  1628. return EINVAL;
  1629. /* Test if the threshold time interval is valid */
  1630. if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
  1631. return EINVAL;
  1632. flags = compute_bw_meter_flags(req);
  1633. /*
  1634. * Find if we have already same bw_meter entry
  1635. */
  1636. MRW_WLOCK();
  1637. mfc = mfc_find(&req->bu_src, &req->bu_dst);
  1638. if (mfc == NULL) {
  1639. MRW_WUNLOCK();
  1640. return EADDRNOTAVAIL;
  1641. }
  1642. /* Choose an appropriate bw_meter list */
  1643. if (req->bu_flags & BW_UPCALL_GEQ)
  1644. bwm_ptr = &mfc->mfc_bw_meter_geq;
  1645. else
  1646. bwm_ptr = &mfc->mfc_bw_meter_leq;
  1647. for (x = *bwm_ptr; x != NULL; x = x->bm_mfc_next) {
  1648. if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
  1649. &req->bu_threshold.b_time, ==))
  1650. && (x->bm_threshold.b_packets
  1651. == req->bu_threshold.b_packets)
  1652. && (x->bm_threshold.b_bytes
  1653. == req->bu_threshold.b_bytes)
  1654. && (x->bm_flags & BW_METER_USER_FLAGS)
  1655. == flags) {
  1656. MRW_WUNLOCK();
  1657. return 0; /* XXX Already installed */
  1658. }
  1659. }
  1660. /* Allocate the new bw_meter entry */
  1661. x = malloc(sizeof(*x), M_BWMETER, M_ZERO | M_NOWAIT);
  1662. if (x == NULL) {
  1663. MRW_WUNLOCK();
  1664. return ENOBUFS;
  1665. }
  1666. /* Set the new bw_meter entry */
  1667. x->bm_threshold.b_time = req->bu_threshold.b_time;
  1668. microtime(&now);
  1669. x->bm_start_time = now;
  1670. x->bm_threshold.b_packets = req->bu_threshold.b_packets;
  1671. x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
  1672. x->bm_measured.b_packets = 0;
  1673. x->bm_measured.b_bytes = 0;
  1674. x->bm_flags = flags;
  1675. x->bm_time_next = NULL;
  1676. x->bm_mfc = mfc;
  1677. x->arg = curvnet;
  1678. sprintf(x->bm_spin_name, "BM spin %p", x);
  1679. mtx_init(&x->bm_spin, x->bm_spin_name, NULL, MTX_SPIN);
  1680. /* For LEQ case create periodic callout */
  1681. if (req->bu_flags & BW_UPCALL_LEQ) {
  1682. callout_init_rw(&x->bm_meter_callout, &mrouter_lock, CALLOUT_SHAREDLOCK);
  1683. callout_reset(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time),
  1684. expire_bw_meter_leq, x);
  1685. }
  1686. /* Add the new bw_meter entry to the front of entries for this MFC */
  1687. x->bm_mfc_next = *bwm_ptr;
  1688. *bwm_ptr = x;
  1689. MRW_WUNLOCK();
  1690. return 0;
  1691. }
  1692. static void
  1693. free_bw_list(struct bw_meter *list)
  1694. {
  1695. while (list != NULL) {
  1696. struct bw_meter *x = list;
  1697. /* MRW_WLOCK must be held here */
  1698. if (x->bm_flags & BW_METER_LEQ) {
  1699. callout_drain(&x->bm_meter_callout);
  1700. mtx_destroy(&x->bm_spin);
  1701. }
  1702. list = list->bm_mfc_next;
  1703. free(x, M_BWMETER);
  1704. }
  1705. }
  1706. /*
  1707. * Delete one or multiple bw_meter entries
  1708. */
  1709. static int
  1710. del_bw_upcall(struct bw_upcall *req)
  1711. {
  1712. struct mfc *mfc;
  1713. struct bw_meter *x, **bwm_ptr;
  1714. if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL))
  1715. return EOPNOTSUPP;
  1716. MRW_WLOCK();
  1717. /* Find the corresponding MFC entry */
  1718. mfc = mfc_find(&req->bu_src, &req->bu_dst);
  1719. if (mfc == NULL) {
  1720. MRW_WUNLOCK();
  1721. return EADDRNOTAVAIL;
  1722. } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
  1723. /*
  1724. * Delete all bw_meter entries for this mfc
  1725. */
  1726. struct bw_meter *list;
  1727. /* Free LEQ list */
  1728. list = mfc->mfc_bw_meter_leq;
  1729. mfc->mfc_bw_meter_leq = NULL;
  1730. free_bw_list(list);
  1731. /* Free GEQ list */
  1732. list = mfc->mfc_bw_meter_geq;
  1733. mfc->mfc_bw_meter_geq = NULL;
  1734. free_bw_list(list);
  1735. MRW_WUNLOCK();
  1736. return 0;
  1737. } else { /* Delete a single bw_meter entry */
  1738. struct bw_meter *prev;
  1739. uint32_t flags = 0;
  1740. flags = compute_bw_meter_flags(req);
  1741. /* Choose an appropriate bw_meter list */
  1742. if (req->bu_flags & BW_UPCALL_GEQ)
  1743. bwm_ptr = &mfc->mfc_bw_meter_geq;
  1744. else
  1745. bwm_ptr = &mfc->mfc_bw_meter_leq;
  1746. /* Find the bw_meter entry to delete */
  1747. for (prev = NULL, x = *bwm_ptr; x != NULL;
  1748. prev = x, x = x->bm_mfc_next) {
  1749. if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, &req->bu_threshold.b_time, ==)) &&
  1750. (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
  1751. (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
  1752. (x->bm_flags & BW_METER_USER_FLAGS) == flags)
  1753. break;
  1754. }
  1755. if (x != NULL) { /* Delete entry from the list for this MFC */
  1756. if (prev != NULL)
  1757. prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
  1758. else
  1759. *bwm_ptr = x->bm_mfc_next;/* new head of list */
  1760. if (req->bu_flags & BW_UPCALL_LEQ)
  1761. callout_stop(&x->bm_meter_callout);
  1762. MRW_WUNLOCK();
  1763. /* Free the bw_meter entry */
  1764. free(x, M_BWMETER);
  1765. return 0;
  1766. } else {
  1767. MRW_WUNLOCK();
  1768. return EINVAL;
  1769. }
  1770. }
  1771. __assert_unreachable();
  1772. }
  1773. /*
  1774. * Perform bandwidth measurement processing that may result in an upcall
  1775. */
  1776. static void
  1777. bw_meter_geq_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
  1778. {
  1779. struct timeval delta;
  1780. MRW_LOCK_ASSERT();
  1781. delta = *nowp;
  1782. BW_TIMEVALDECR(&delta, &x->bm_start_time);
  1783. /*
  1784. * Processing for ">=" type of bw_meter entry.
  1785. * bm_spin does not have to be hold here as in GEQ
  1786. * case this is the only context accessing bm_measured.
  1787. */
  1788. if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
  1789. /* Reset the bw_meter entry */
  1790. x->bm_start_time = *nowp;
  1791. x->bm_measured.b_packets = 0;
  1792. x->bm_measured.b_bytes = 0;
  1793. x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
  1794. }
  1795. /* Record that a packet is received */
  1796. x->bm_measured.b_packets++;
  1797. x->bm_measured.b_bytes += plen;
  1798. /*
  1799. * Test if we should deliver an upcall
  1800. */
  1801. if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
  1802. if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
  1803. (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
  1804. ((x->bm_flags & BW_METER_UNIT_BYTES) &&
  1805. (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
  1806. /* Prepare an upcall for delivery */
  1807. bw_meter_prepare_upcall(x, nowp);
  1808. x->bm_flags |= BW_METER_UPCALL_DELIVERED;
  1809. }
  1810. }
  1811. }
  1812. /*
  1813. * Prepare a bandwidth-related upcall
  1814. */
  1815. static void
  1816. bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
  1817. {
  1818. struct timeval delta;
  1819. struct bw_upcall *u;
  1820. MRW_LOCK_ASSERT();
  1821. /*
  1822. * Compute the measured time interval
  1823. */
  1824. delta = *nowp;
  1825. BW_TIMEVALDECR(&delta, &x->bm_start_time);
  1826. /*
  1827. * Set the bw_upcall entry
  1828. */
  1829. u = malloc(sizeof(struct bw_upcall), M_MRTABLE, M_NOWAIT | M_ZERO);
  1830. if (!u) {
  1831. log(LOG_WARNING, "bw_meter_prepare_upcall: cannot allocate entry\n");
  1832. return;
  1833. }
  1834. u->bu_src = x->bm_mfc->mfc_origin;
  1835. u->bu_dst = x->bm_mfc->mfc_mcastgrp;
  1836. u->bu_threshold.b_time = x->bm_threshold.b_time;
  1837. u->bu_threshold.b_packets = x->bm_threshold.b_packets;
  1838. u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
  1839. u->bu_measured.b_time = delta;
  1840. u->bu_measured.b_packets = x->bm_measured.b_packets;
  1841. u->bu_measured.b_bytes = x->bm_measured.b_bytes;
  1842. u->bu_flags = 0;
  1843. if (x->bm_flags & BW_METER_UNIT_PACKETS)
  1844. u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
  1845. if (x->bm_flags & BW_METER_UNIT_BYTES)
  1846. u->bu_flags |= BW_UPCALL_UNIT_BYTES;
  1847. if (x->bm_flags & BW_METER_GEQ)
  1848. u->bu_flags |= BW_UPCALL_GEQ;
  1849. if (x->bm_flags & BW_METER_LEQ)
  1850. u->bu_flags |= BW_UPCALL_LEQ;
  1851. if (buf_ring_enqueue(V_bw_upcalls_ring, u))
  1852. log(LOG_WARNING, "bw_meter_prepare_upcall: cannot enqueue upcall\n");
  1853. if (buf_ring_count(V_bw_upcalls_ring) > (BW_UPCALLS_MAX / 2)) {
  1854. taskqueue_enqueue(V_task_queue, &V_task);
  1855. }
  1856. }
  1857. /*
  1858. * Send the pending bandwidth-related upcalls
  1859. */
  1860. static void
  1861. bw_upcalls_send(void)
  1862. {
  1863. struct mbuf *m;
  1864. int len = 0;
  1865. struct bw_upcall *bu;
  1866. struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
  1867. static struct igmpmsg igmpmsg = {
  1868. 0, /* unused1 */
  1869. 0, /* unused2 */
  1870. IGMPMSG_BW_UPCALL,/* im_msgtype */
  1871. 0, /* im_mbz */
  1872. 0, /* im_vif */
  1873. 0, /* unused3 */
  1874. { 0 }, /* im_src */
  1875. { 0 } /* im_dst */
  1876. };
  1877. MRW_LOCK_ASSERT();
  1878. if (buf_ring_empty(V_bw_upcalls_ring))
  1879. return;
  1880. /*
  1881. * Allocate a new mbuf, initialize it with the header and
  1882. * the payload for the pending calls.
  1883. */
  1884. m = m_gethdr(M_NOWAIT, MT_DATA);
  1885. if (m == NULL) {
  1886. log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
  1887. return;
  1888. }
  1889. m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
  1890. len += sizeof(struct igmpmsg);
  1891. while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) {
  1892. m_copyback(m, len, sizeof(struct bw_upcall), (caddr_t)bu);
  1893. len += sizeof(struct bw_upcall);
  1894. free(bu, M_MRTABLE);
  1895. }
  1896. /*
  1897. * Send the upcalls
  1898. * XXX do we need to set the address in k_igmpsrc ?
  1899. */
  1900. MRTSTAT_INC(mrts_upcalls);
  1901. if (socket_send(V_ip_mrouter, m, &k_igmpsrc) < 0) {
  1902. log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
  1903. MRTSTAT_INC(mrts_upq_sockfull);
  1904. }
  1905. }
  1906. /*
  1907. * A periodic function for sending all upcalls that are pending delivery
  1908. */
  1909. static void
  1910. expire_bw_upcalls_send(void *arg)
  1911. {
  1912. CURVNET_SET((struct vnet *) arg);
  1913. /* This callout is run with MRW_RLOCK taken */
  1914. bw_upcalls_send();
  1915. callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send,
  1916. curvnet);
  1917. CURVNET_RESTORE();
  1918. }
  1919. /*
  1920. * End of bandwidth monitoring code
  1921. */
  1922. /*
  1923. * Send the packet up to the user daemon, or eventually do kernel encapsulation
  1924. *
  1925. */
  1926. static int
  1927. pim_register_send(struct ip *ip, struct vif *vifp, struct mbuf *m,
  1928. struct mfc *rt)
  1929. {
  1930. struct mbuf *mb_copy, *mm;
  1931. /*
  1932. * Do not send IGMP_WHOLEPKT notifications to userland, if the
  1933. * rendezvous point was unspecified, and we were told not to.
  1934. */
  1935. if (pim_squelch_wholepkt != 0 && (V_mrt_api_config & MRT_MFC_RP) &&
  1936. in_nullhost(rt->mfc_rp))
  1937. return 0;
  1938. mb_copy = pim_register_prepare(ip, m);
  1939. if (mb_copy == NULL)
  1940. return ENOBUFS;
  1941. /*
  1942. * Send all the fragments. Note that the mbuf for each fragment
  1943. * is freed by the sending machinery.
  1944. */
  1945. for (mm = mb_copy; mm; mm = mb_copy) {
  1946. mb_copy = mm->m_nextpkt;
  1947. mm->m_nextpkt = 0;
  1948. mm = m_pullup(mm, sizeof(struct ip));
  1949. if (mm != NULL) {
  1950. ip = mtod(mm, struct ip *);
  1951. if ((V_mrt_api_config & MRT_MFC_RP) && !in_nullhost(rt->mfc_rp)) {
  1952. pim_register_send_rp(ip, vifp, mm, rt);
  1953. } else {
  1954. pim_register_send_upcall(ip, vifp, mm, rt);
  1955. }
  1956. }
  1957. }
  1958. return 0;
  1959. }
  1960. /*
  1961. * Return a copy of the data packet that is ready for PIM Register
  1962. * encapsulation.
  1963. * XXX: Note that in the returned copy the IP header is a valid one.
  1964. */
  1965. static struct mbuf *
  1966. pim_register_prepare(struct ip *ip, struct mbuf *m)
  1967. {
  1968. struct mbuf *mb_copy = NULL;
  1969. int mtu;
  1970. /* Take care of delayed checksums */
  1971. if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  1972. in_delayed_cksum(m);
  1973. m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  1974. }
  1975. /*
  1976. * Copy the old packet & pullup its IP header into the
  1977. * new mbuf so we can modify it.
  1978. */
  1979. mb_copy = m_copypacket(m, M_NOWAIT);
  1980. if (mb_copy == NULL)
  1981. return NULL;
  1982. mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
  1983. if (mb_copy == NULL)
  1984. return NULL;
  1985. /* take care of the TTL */
  1986. ip = mtod(mb_copy, struct ip *);
  1987. --ip->ip_ttl;
  1988. /* Compute the MTU after the PIM Register encapsulation */
  1989. mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
  1990. if (ntohs(ip->ip_len) <= mtu) {
  1991. /* Turn the IP header into a valid one */
  1992. ip->ip_sum = 0;
  1993. ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
  1994. } else {
  1995. /* Fragment the packet */
  1996. mb_copy->m_pkthdr.csum_flags |= CSUM_IP;
  1997. if (ip_fragment(ip, &mb_copy, mtu, 0) != 0) {
  1998. m_freem(mb_copy);
  1999. return NULL;
  2000. }
  2001. }
  2002. return mb_copy;
  2003. }
  2004. /*
  2005. * Send an upcall with the data packet to the user-level process.
  2006. */
  2007. static int
  2008. pim_register_send_upcall(struct ip *ip, struct vif *vifp,
  2009. struct mbuf *mb_copy, struct mfc *rt)
  2010. {
  2011. struct mbuf *mb_first;
  2012. int len = ntohs(ip->ip_len);
  2013. struct igmpmsg *im;
  2014. struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
  2015. MRW_LOCK_ASSERT();
  2016. /*
  2017. * Add a new mbuf with an upcall header
  2018. */
  2019. mb_first = m_gethdr(M_NOWAIT, MT_DATA);
  2020. if (mb_first == NULL) {
  2021. m_freem(mb_copy);
  2022. return ENOBUFS;
  2023. }
  2024. mb_first->m_data += max_linkhdr;
  2025. mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
  2026. mb_first->m_len = sizeof(struct igmpmsg);
  2027. mb_first->m_next = mb_copy;
  2028. /* Send message to routing daemon */
  2029. im = mtod(mb_first, struct igmpmsg *);
  2030. im->im_msgtype = IGMPMSG_WHOLEPKT;
  2031. im->im_mbz = 0;
  2032. im->im_vif = vifp - V_viftable;
  2033. im->im_src = ip->ip_src;
  2034. im->im_dst = ip->ip_dst;
  2035. k_igmpsrc.sin_addr = ip->ip_src;
  2036. MRTSTAT_INC(mrts_upcalls);
  2037. if (socket_send(V_ip_mrouter, mb_first, &k_igmpsrc) < 0) {
  2038. CTR1(KTR_IPMF, "%s: socket queue full", __func__);
  2039. MRTSTAT_INC(mrts_upq_sockfull);
  2040. return ENOBUFS;
  2041. }
  2042. /* Keep statistics */
  2043. PIMSTAT_INC(pims_snd_registers_msgs);
  2044. PIMSTAT_ADD(pims_snd_registers_bytes, len);
  2045. return 0;
  2046. }
  2047. /*
  2048. * Encapsulate the data packet in PIM Register message and send it to the RP.
  2049. */
  2050. static int
  2051. pim_register_send_rp(struct ip *ip, struct vif *vifp, struct mbuf *mb_copy,
  2052. struct mfc *rt)
  2053. {
  2054. struct mbuf *mb_first;
  2055. struct ip *ip_outer;
  2056. struct pim_encap_pimhdr *pimhdr;
  2057. int len = ntohs(ip->ip_len);
  2058. vifi_t vifi = rt->mfc_parent;
  2059. MRW_LOCK_ASSERT();
  2060. if ((vifi >= V_numvifs) || in_nullhost(V_viftable[vifi].v_lcl_addr)) {
  2061. m_freem(mb_copy);
  2062. return EADDRNOTAVAIL; /* The iif vif is invalid */
  2063. }
  2064. /*
  2065. * Add a new mbuf with the encapsulating header
  2066. */
  2067. mb_first = m_gethdr(M_NOWAIT, MT_DATA);
  2068. if (mb_first == NULL) {
  2069. m_freem(mb_copy);
  2070. return ENOBUFS;
  2071. }
  2072. mb_first->m_data += max_linkhdr;
  2073. mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
  2074. mb_first->m_next = mb_copy;
  2075. mb_first->m_pkthdr.len = len + mb_first->m_len;
  2076. /*
  2077. * Fill in the encapsulating IP and PIM header
  2078. */
  2079. ip_outer = mtod(mb_first, struct ip *);
  2080. *ip_outer = pim_encap_iphdr;
  2081. ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
  2082. sizeof(pim_encap_pimhdr));
  2083. ip_outer->ip_src = V_viftable[vifi].v_lcl_addr;
  2084. ip_outer->ip_dst = rt->mfc_rp;
  2085. /*
  2086. * Copy the inner header TOS to the outer header, and take care of the
  2087. * IP_DF bit.
  2088. */
  2089. ip_outer->ip_tos = ip->ip_tos;
  2090. if (ip->ip_off & htons(IP_DF))
  2091. ip_outer->ip_off |= htons(IP_DF);
  2092. ip_fillid(ip_outer);
  2093. pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
  2094. + sizeof(pim_encap_iphdr));
  2095. *pimhdr = pim_encap_pimhdr;
  2096. /* If the iif crosses a border, set the Border-bit */
  2097. if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & V_mrt_api_config)
  2098. pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
  2099. mb_first->m_data += sizeof(pim_encap_iphdr);
  2100. pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
  2101. mb_first->m_data -= sizeof(pim_encap_iphdr);
  2102. send_packet(vifp, mb_first);
  2103. /* Keep statistics */
  2104. PIMSTAT_INC(pims_snd_registers_msgs);
  2105. PIMSTAT_ADD(pims_snd_registers_bytes, len);
  2106. return 0;
  2107. }
  2108. /*
  2109. * pim_encapcheck() is called by the encap4_input() path at runtime to
  2110. * determine if a packet is for PIM; allowing PIM to be dynamically loaded
  2111. * into the kernel.
  2112. */
  2113. static int
  2114. pim_encapcheck(const struct mbuf *m __unused, int off __unused,
  2115. int proto __unused, void *arg __unused)
  2116. {
  2117. KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM"));
  2118. return (8); /* claim the datagram. */
  2119. }
  2120. /*
  2121. * PIM-SMv2 and PIM-DM messages processing.
  2122. * Receives and verifies the PIM control messages, and passes them
  2123. * up to the listening socket, using rip_input().
  2124. * The only message with special processing is the PIM_REGISTER message
  2125. * (used by PIM-SM): the PIM header is stripped off, and the inner packet
  2126. * is passed to if_simloop().
  2127. */
  2128. static int
  2129. pim_input(struct mbuf *m, int off, int proto, void *arg __unused)
  2130. {
  2131. struct ip *ip = mtod(m, struct ip *);
  2132. struct pim *pim;
  2133. int iphlen = off;
  2134. int minlen;
  2135. int datalen = ntohs(ip->ip_len) - iphlen;
  2136. int ip_tos;
  2137. /* Keep statistics */
  2138. PIMSTAT_INC(pims_rcv_total_msgs);
  2139. PIMSTAT_ADD(pims_rcv_total_bytes, datalen);
  2140. /*
  2141. * Validate lengths
  2142. */
  2143. if (datalen < PIM_MINLEN) {
  2144. PIMSTAT_INC(pims_rcv_tooshort);
  2145. CTR3(KTR_IPMF, "%s: short packet (%d) from 0x%08x",
  2146. __func__, datalen, ntohl(ip->ip_src.s_addr));
  2147. m_freem(m);
  2148. return (IPPROTO_DONE);
  2149. }
  2150. /*
  2151. * If the packet is at least as big as a REGISTER, go agead
  2152. * and grab the PIM REGISTER header size, to avoid another
  2153. * possible m_pullup() later.
  2154. *
  2155. * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
  2156. * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
  2157. */
  2158. minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
  2159. /*
  2160. * Get the IP and PIM headers in contiguous memory, and
  2161. * possibly the PIM REGISTER header.
  2162. */
  2163. if (m->m_len < minlen && (m = m_pullup(m, minlen)) == NULL) {
  2164. CTR1(KTR_IPMF, "%s: m_pullup() failed", __func__);
  2165. return (IPPROTO_DONE);
  2166. }
  2167. /* m_pullup() may have given us a new mbuf so reset ip. */
  2168. ip = mtod(m, struct ip *);
  2169. ip_tos = ip->ip_tos;
  2170. /* adjust mbuf to point to the PIM header */
  2171. m->m_data += iphlen;
  2172. m->m_len -= iphlen;
  2173. pim = mtod(m, struct pim *);
  2174. /*
  2175. * Validate checksum. If PIM REGISTER, exclude the data packet.
  2176. *
  2177. * XXX: some older PIMv2 implementations don't make this distinction,
  2178. * so for compatibility reason perform the checksum over part of the
  2179. * message, and if error, then over the whole message.
  2180. */
  2181. if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
  2182. /* do nothing, checksum okay */
  2183. } else if (in_cksum(m, datalen)) {
  2184. PIMSTAT_INC(pims_rcv_badsum);
  2185. CTR1(KTR_IPMF, "%s: invalid checksum", __func__);
  2186. m_freem(m);
  2187. return (IPPROTO_DONE);
  2188. }
  2189. /* PIM version check */
  2190. if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
  2191. PIMSTAT_INC(pims_rcv_badversion);
  2192. CTR3(KTR_IPMF, "%s: bad version %d expect %d", __func__,
  2193. (int)PIM_VT_V(pim->pim_vt), PIM_VERSION);
  2194. m_freem(m);
  2195. return (IPPROTO_DONE);
  2196. }
  2197. /* restore mbuf back to the outer IP */
  2198. m->m_data -= iphlen;
  2199. m->m_len += iphlen;
  2200. if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
  2201. /*
  2202. * Since this is a REGISTER, we'll make a copy of the register
  2203. * headers ip + pim + u_int32 + encap_ip, to be passed up to the
  2204. * routing daemon.
  2205. */
  2206. struct sockaddr_in dst = { sizeof(dst), AF_INET };
  2207. struct mbuf *mcp;
  2208. struct ip *encap_ip;
  2209. u_int32_t *reghdr;
  2210. struct ifnet *vifp;
  2211. MRW_RLOCK();
  2212. if ((V_reg_vif_num >= V_numvifs) || (V_reg_vif_num == VIFI_INVALID)) {
  2213. MRW_RUNLOCK();
  2214. CTR2(KTR_IPMF, "%s: register vif not set: %d", __func__,
  2215. (int)V_reg_vif_num);
  2216. m_freem(m);
  2217. return (IPPROTO_DONE);
  2218. }
  2219. /* XXX need refcnt? */
  2220. vifp = V_viftable[V_reg_vif_num].v_ifp;
  2221. MRW_RUNLOCK();
  2222. /*
  2223. * Validate length
  2224. */
  2225. if (datalen < PIM_REG_MINLEN) {
  2226. PIMSTAT_INC(pims_rcv_tooshort);
  2227. PIMSTAT_INC(pims_rcv_badregisters);
  2228. CTR1(KTR_IPMF, "%s: register packet size too small", __func__);
  2229. m_freem(m);
  2230. return (IPPROTO_DONE);
  2231. }
  2232. reghdr = (u_int32_t *)(pim + 1);
  2233. encap_ip = (struct ip *)(reghdr + 1);
  2234. CTR3(KTR_IPMF, "%s: register: encap ip src 0x%08x len %d",
  2235. __func__, ntohl(encap_ip->ip_src.s_addr),
  2236. ntohs(encap_ip->ip_len));
  2237. /* verify the version number of the inner packet */
  2238. if (encap_ip->ip_v != IPVERSION) {
  2239. PIMSTAT_INC(pims_rcv_badregisters);
  2240. CTR1(KTR_IPMF, "%s: bad encap ip version", __func__);
  2241. m_freem(m);
  2242. return (IPPROTO_DONE);
  2243. }
  2244. /* verify the inner packet is destined to a mcast group */
  2245. if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
  2246. PIMSTAT_INC(pims_rcv_badregisters);
  2247. CTR2(KTR_IPMF, "%s: bad encap ip dest 0x%08x", __func__,
  2248. ntohl(encap_ip->ip_dst.s_addr));
  2249. m_freem(m);
  2250. return (IPPROTO_DONE);
  2251. }
  2252. /* If a NULL_REGISTER, pass it to the daemon */
  2253. if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
  2254. goto pim_input_to_daemon;
  2255. /*
  2256. * Copy the TOS from the outer IP header to the inner IP header.
  2257. */
  2258. if (encap_ip->ip_tos != ip_tos) {
  2259. /* Outer TOS -> inner TOS */
  2260. encap_ip->ip_tos = ip_tos;
  2261. /* Recompute the inner header checksum. Sigh... */
  2262. /* adjust mbuf to point to the inner IP header */
  2263. m->m_data += (iphlen + PIM_MINLEN);
  2264. m->m_len -= (iphlen + PIM_MINLEN);
  2265. encap_ip->ip_sum = 0;
  2266. encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
  2267. /* restore mbuf to point back to the outer IP header */
  2268. m->m_data -= (iphlen + PIM_MINLEN);
  2269. m->m_len += (iphlen + PIM_MINLEN);
  2270. }
  2271. /*
  2272. * Decapsulate the inner IP packet and loopback to forward it
  2273. * as a normal multicast packet. Also, make a copy of the
  2274. * outer_iphdr + pimhdr + reghdr + encap_iphdr
  2275. * to pass to the daemon later, so it can take the appropriate
  2276. * actions (e.g., send back PIM_REGISTER_STOP).
  2277. * XXX: here m->m_data points to the outer IP header.
  2278. */
  2279. mcp = m_copym(m, 0, iphlen + PIM_REG_MINLEN, M_NOWAIT);
  2280. if (mcp == NULL) {
  2281. CTR1(KTR_IPMF, "%s: m_copym() failed", __func__);
  2282. m_freem(m);
  2283. return (IPPROTO_DONE);
  2284. }
  2285. /* Keep statistics */
  2286. /* XXX: registers_bytes include only the encap. mcast pkt */
  2287. PIMSTAT_INC(pims_rcv_registers_msgs);
  2288. PIMSTAT_ADD(pims_rcv_registers_bytes, ntohs(encap_ip->ip_len));
  2289. /*
  2290. * forward the inner ip packet; point m_data at the inner ip.
  2291. */
  2292. m_adj(m, iphlen + PIM_MINLEN);
  2293. CTR4(KTR_IPMF,
  2294. "%s: forward decap'd REGISTER: src %lx dst %lx vif %d",
  2295. __func__,
  2296. (u_long)ntohl(encap_ip->ip_src.s_addr),
  2297. (u_long)ntohl(encap_ip->ip_dst.s_addr),
  2298. (int)V_reg_vif_num);
  2299. /* NB: vifp was collected above; can it change on us? */
  2300. if_simloop(vifp, m, dst.sin_family, 0);
  2301. /* prepare the register head to send to the mrouting daemon */
  2302. m = mcp;
  2303. }
  2304. pim_input_to_daemon:
  2305. /*
  2306. * Pass the PIM message up to the daemon; if it is a Register message,
  2307. * pass the 'head' only up to the daemon. This includes the
  2308. * outer IP header, PIM header, PIM-Register header and the
  2309. * inner IP header.
  2310. * XXX: the outer IP header pkt size of a Register is not adjust to
  2311. * reflect the fact that the inner multicast data is truncated.
  2312. */
  2313. return (rip_input(&m, &off, proto));
  2314. }
  2315. static int
  2316. sysctl_mfctable(SYSCTL_HANDLER_ARGS)
  2317. {
  2318. struct mfc *rt;
  2319. int error, i;
  2320. if (req->newptr)
  2321. return (EPERM);
  2322. if (V_mfchashtbl == NULL) /* XXX unlocked */
  2323. return (0);
  2324. error = sysctl_wire_old_buffer(req, 0);
  2325. if (error)
  2326. return (error);
  2327. MRW_RLOCK();
  2328. for (i = 0; i < mfchashsize; i++) {
  2329. LIST_FOREACH(rt, &V_mfchashtbl[i], mfc_hash) {
  2330. error = SYSCTL_OUT(req, rt, sizeof(struct mfc));
  2331. if (error)
  2332. goto out_locked;
  2333. }
  2334. }
  2335. out_locked:
  2336. MRW_RUNLOCK();
  2337. return (error);
  2338. }
  2339. static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mfctable,
  2340. CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mfctable,
  2341. "IPv4 Multicast Forwarding Table "
  2342. "(struct *mfc[mfchashsize], netinet/ip_mroute.h)");
  2343. static int
  2344. sysctl_viflist(SYSCTL_HANDLER_ARGS)
  2345. {
  2346. int error, i;
  2347. if (req->newptr)
  2348. return (EPERM);
  2349. if (V_viftable == NULL) /* XXX unlocked */
  2350. return (0);
  2351. error = sysctl_wire_old_buffer(req, MROUTE_VIF_SYSCTL_LEN * MAXVIFS);
  2352. if (error)
  2353. return (error);
  2354. MRW_RLOCK();
  2355. /* Copy out user-visible portion of vif entry. */
  2356. for (i = 0; i < MAXVIFS; i++) {
  2357. error = SYSCTL_OUT(req, &V_viftable[i], MROUTE_VIF_SYSCTL_LEN);
  2358. if (error)
  2359. break;
  2360. }
  2361. MRW_RUNLOCK();
  2362. return (error);
  2363. }
  2364. SYSCTL_PROC(_net_inet_ip, OID_AUTO, viftable,
  2365. CTLTYPE_OPAQUE | CTLFLAG_VNET | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
  2366. sysctl_viflist, "S,vif[MAXVIFS]",
  2367. "IPv4 Multicast Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
  2368. static void
  2369. vnet_mroute_init(const void *unused __unused)
  2370. {
  2371. V_nexpire = malloc(mfchashsize, M_MRTABLE, M_WAITOK|M_ZERO);
  2372. V_viftable = mallocarray(MAXVIFS, sizeof(*V_viftable),
  2373. M_MRTABLE, M_WAITOK|M_ZERO);
  2374. callout_init_rw(&V_expire_upcalls_ch, &mrouter_lock, 0);
  2375. callout_init_rw(&V_bw_upcalls_ch, &mrouter_lock, 0);
  2376. /* Prepare taskqueue */
  2377. V_task_queue = taskqueue_create_fast("ip_mroute_tskq", M_NOWAIT,
  2378. taskqueue_thread_enqueue, &V_task_queue);
  2379. taskqueue_start_threads(&V_task_queue, 1, PI_NET, "ip_mroute_tskq task");
  2380. }
  2381. VNET_SYSINIT(vnet_mroute_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mroute_init,
  2382. NULL);
  2383. static void
  2384. vnet_mroute_uninit(const void *unused __unused)
  2385. {
  2386. /* Taskqueue should be cancelled and drained before freeing */
  2387. taskqueue_free(V_task_queue);
  2388. free(V_viftable, M_MRTABLE);
  2389. free(V_nexpire, M_MRTABLE);
  2390. V_nexpire = NULL;
  2391. }
  2392. VNET_SYSUNINIT(vnet_mroute_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE,
  2393. vnet_mroute_uninit, NULL);
  2394. static int
  2395. ip_mroute_modevent(module_t mod, int type, void *unused)
  2396. {
  2397. switch (type) {
  2398. case MOD_LOAD:
  2399. MRW_LOCK_INIT();
  2400. if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
  2401. if_detached_event, NULL, EVENTHANDLER_PRI_ANY);
  2402. if (if_detach_event_tag == NULL) {
  2403. printf("ip_mroute: unable to register "
  2404. "ifnet_departure_event handler\n");
  2405. MRW_LOCK_DESTROY();
  2406. return (EINVAL);
  2407. }
  2408. if (!powerof2(mfchashsize)) {
  2409. printf("WARNING: %s not a power of 2; using default\n",
  2410. "net.inet.ip.mfchashsize");
  2411. mfchashsize = MFCHASHSIZE;
  2412. }
  2413. pim_encap_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
  2414. ip_mcast_src = X_ip_mcast_src;
  2415. ip_mforward = X_ip_mforward;
  2416. ip_mrouter_done = X_ip_mrouter_done;
  2417. ip_mrouter_get = X_ip_mrouter_get;
  2418. ip_mrouter_set = X_ip_mrouter_set;
  2419. ip_rsvp_force_done = X_ip_rsvp_force_done;
  2420. ip_rsvp_vif = X_ip_rsvp_vif;
  2421. legal_vif_num = X_legal_vif_num;
  2422. mrt_ioctl = X_mrt_ioctl;
  2423. rsvp_input_p = X_rsvp_input;
  2424. break;
  2425. case MOD_UNLOAD:
  2426. /*
  2427. * Typically module unload happens after the user-level
  2428. * process has shutdown the kernel services (the check
  2429. * below insures someone can't just yank the module out
  2430. * from under a running process). But if the module is
  2431. * just loaded and then unloaded w/o starting up a user
  2432. * process we still need to cleanup.
  2433. */
  2434. MRW_WLOCK();
  2435. if (ip_mrouter_cnt != 0) {
  2436. MRW_WUNLOCK();
  2437. return (EINVAL);
  2438. }
  2439. ip_mrouter_unloading = 1;
  2440. MRW_WUNLOCK();
  2441. EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
  2442. if (pim_encap_cookie) {
  2443. ip_encap_detach(pim_encap_cookie);
  2444. pim_encap_cookie = NULL;
  2445. }
  2446. ip_mcast_src = NULL;
  2447. ip_mforward = NULL;
  2448. ip_mrouter_done = NULL;
  2449. ip_mrouter_get = NULL;
  2450. ip_mrouter_set = NULL;
  2451. ip_rsvp_force_done = NULL;
  2452. ip_rsvp_vif = NULL;
  2453. legal_vif_num = NULL;
  2454. mrt_ioctl = NULL;
  2455. rsvp_input_p = NULL;
  2456. MRW_LOCK_DESTROY();
  2457. break;
  2458. default:
  2459. return EOPNOTSUPP;
  2460. }
  2461. return 0;
  2462. }
  2463. static moduledata_t ip_mroutemod = {
  2464. "ip_mroute",
  2465. ip_mroute_modevent,
  2466. 0
  2467. };
  2468. DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);