br_multicast.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /*
  2. * Bridge multicast support.
  3. *
  4. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/igmp.h>
  16. #include <linux/jhash.h>
  17. #include <linux/kernel.h>
  18. #include <linux/log2.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/netfilter_bridge.h>
  21. #include <linux/random.h>
  22. #include <linux/rculist.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/slab.h>
  25. #include <linux/timer.h>
  26. #include <linux/inetdevice.h>
  27. #include <net/ip.h>
  28. #if IS_ENABLED(CONFIG_IPV6)
  29. #include <net/ipv6.h>
  30. #include <net/mld.h>
  31. #include <net/ip6_checksum.h>
  32. #include <net/addrconf.h>
  33. #endif
  34. #include "br_private.h"
  35. static void br_multicast_start_querier(struct net_bridge *br,
  36. struct bridge_mcast_own_query *query);
  37. static void br_multicast_add_router(struct net_bridge *br,
  38. struct net_bridge_port *port);
  39. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  40. struct net_bridge_port *port,
  41. __be32 group,
  42. __u16 vid);
  43. #if IS_ENABLED(CONFIG_IPV6)
  44. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  45. struct net_bridge_port *port,
  46. const struct in6_addr *group,
  47. __u16 vid);
  48. #endif
  49. unsigned int br_mdb_rehash_seq;
  50. static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
  51. {
  52. if (a->proto != b->proto)
  53. return 0;
  54. if (a->vid != b->vid)
  55. return 0;
  56. switch (a->proto) {
  57. case htons(ETH_P_IP):
  58. return a->u.ip4 == b->u.ip4;
  59. #if IS_ENABLED(CONFIG_IPV6)
  60. case htons(ETH_P_IPV6):
  61. return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
  62. #endif
  63. }
  64. return 0;
  65. }
  66. static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
  67. __u16 vid)
  68. {
  69. return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
  70. }
  71. #if IS_ENABLED(CONFIG_IPV6)
  72. static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
  73. const struct in6_addr *ip,
  74. __u16 vid)
  75. {
  76. return jhash_2words(ipv6_addr_hash(ip), vid,
  77. mdb->secret) & (mdb->max - 1);
  78. }
  79. #endif
  80. static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
  81. struct br_ip *ip)
  82. {
  83. switch (ip->proto) {
  84. case htons(ETH_P_IP):
  85. return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
  86. #if IS_ENABLED(CONFIG_IPV6)
  87. case htons(ETH_P_IPV6):
  88. return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
  89. #endif
  90. }
  91. return 0;
  92. }
  93. static struct net_bridge_mdb_entry *__br_mdb_ip_get(
  94. struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
  95. {
  96. struct net_bridge_mdb_entry *mp;
  97. hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
  98. if (br_ip_equal(&mp->addr, dst))
  99. return mp;
  100. }
  101. return NULL;
  102. }
  103. struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
  104. struct br_ip *dst)
  105. {
  106. if (!mdb)
  107. return NULL;
  108. return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
  109. }
  110. static struct net_bridge_mdb_entry *br_mdb_ip4_get(
  111. struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
  112. {
  113. struct br_ip br_dst;
  114. br_dst.u.ip4 = dst;
  115. br_dst.proto = htons(ETH_P_IP);
  116. br_dst.vid = vid;
  117. return br_mdb_ip_get(mdb, &br_dst);
  118. }
  119. #if IS_ENABLED(CONFIG_IPV6)
  120. static struct net_bridge_mdb_entry *br_mdb_ip6_get(
  121. struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
  122. __u16 vid)
  123. {
  124. struct br_ip br_dst;
  125. br_dst.u.ip6 = *dst;
  126. br_dst.proto = htons(ETH_P_IPV6);
  127. br_dst.vid = vid;
  128. return br_mdb_ip_get(mdb, &br_dst);
  129. }
  130. #endif
  131. struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  132. struct sk_buff *skb, u16 vid)
  133. {
  134. struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
  135. struct br_ip ip;
  136. if (br->multicast_disabled)
  137. return NULL;
  138. if (BR_INPUT_SKB_CB(skb)->igmp)
  139. return NULL;
  140. ip.proto = skb->protocol;
  141. ip.vid = vid;
  142. switch (skb->protocol) {
  143. case htons(ETH_P_IP):
  144. ip.u.ip4 = ip_hdr(skb)->daddr;
  145. break;
  146. #if IS_ENABLED(CONFIG_IPV6)
  147. case htons(ETH_P_IPV6):
  148. ip.u.ip6 = ipv6_hdr(skb)->daddr;
  149. break;
  150. #endif
  151. default:
  152. return NULL;
  153. }
  154. return br_mdb_ip_get(mdb, &ip);
  155. }
  156. static void br_mdb_free(struct rcu_head *head)
  157. {
  158. struct net_bridge_mdb_htable *mdb =
  159. container_of(head, struct net_bridge_mdb_htable, rcu);
  160. struct net_bridge_mdb_htable *old = mdb->old;
  161. mdb->old = NULL;
  162. kfree(old->mhash);
  163. kfree(old);
  164. }
  165. static int br_mdb_copy(struct net_bridge_mdb_htable *new,
  166. struct net_bridge_mdb_htable *old,
  167. int elasticity)
  168. {
  169. struct net_bridge_mdb_entry *mp;
  170. int maxlen;
  171. int len;
  172. int i;
  173. for (i = 0; i < old->max; i++)
  174. hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
  175. hlist_add_head(&mp->hlist[new->ver],
  176. &new->mhash[br_ip_hash(new, &mp->addr)]);
  177. if (!elasticity)
  178. return 0;
  179. maxlen = 0;
  180. for (i = 0; i < new->max; i++) {
  181. len = 0;
  182. hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
  183. len++;
  184. if (len > maxlen)
  185. maxlen = len;
  186. }
  187. return maxlen > elasticity ? -EINVAL : 0;
  188. }
  189. void br_multicast_free_pg(struct rcu_head *head)
  190. {
  191. struct net_bridge_port_group *p =
  192. container_of(head, struct net_bridge_port_group, rcu);
  193. kfree(p);
  194. }
  195. static void br_multicast_free_group(struct rcu_head *head)
  196. {
  197. struct net_bridge_mdb_entry *mp =
  198. container_of(head, struct net_bridge_mdb_entry, rcu);
  199. kfree(mp);
  200. }
  201. static void br_multicast_group_expired(unsigned long data)
  202. {
  203. struct net_bridge_mdb_entry *mp = (void *)data;
  204. struct net_bridge *br = mp->br;
  205. struct net_bridge_mdb_htable *mdb;
  206. spin_lock(&br->multicast_lock);
  207. if (!netif_running(br->dev) || timer_pending(&mp->timer))
  208. goto out;
  209. mp->mglist = false;
  210. if (mp->ports)
  211. goto out;
  212. mdb = mlock_dereference(br->mdb, br);
  213. hlist_del_rcu(&mp->hlist[mdb->ver]);
  214. mdb->size--;
  215. call_rcu_bh(&mp->rcu, br_multicast_free_group);
  216. out:
  217. spin_unlock(&br->multicast_lock);
  218. }
  219. static void br_multicast_del_pg(struct net_bridge *br,
  220. struct net_bridge_port_group *pg)
  221. {
  222. struct net_bridge_mdb_htable *mdb;
  223. struct net_bridge_mdb_entry *mp;
  224. struct net_bridge_port_group *p;
  225. struct net_bridge_port_group __rcu **pp;
  226. mdb = mlock_dereference(br->mdb, br);
  227. mp = br_mdb_ip_get(mdb, &pg->addr);
  228. if (WARN_ON(!mp))
  229. return;
  230. for (pp = &mp->ports;
  231. (p = mlock_dereference(*pp, br)) != NULL;
  232. pp = &p->next) {
  233. if (p != pg)
  234. continue;
  235. rcu_assign_pointer(*pp, p->next);
  236. hlist_del_init(&p->mglist);
  237. del_timer(&p->timer);
  238. br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
  239. p->flags);
  240. call_rcu_bh(&p->rcu, br_multicast_free_pg);
  241. if (!mp->ports && !mp->mglist &&
  242. netif_running(br->dev))
  243. mod_timer(&mp->timer, jiffies);
  244. return;
  245. }
  246. WARN_ON(1);
  247. }
  248. static void br_multicast_port_group_expired(unsigned long data)
  249. {
  250. struct net_bridge_port_group *pg = (void *)data;
  251. struct net_bridge *br = pg->port->br;
  252. spin_lock(&br->multicast_lock);
  253. if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
  254. hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
  255. goto out;
  256. br_multicast_del_pg(br, pg);
  257. out:
  258. spin_unlock(&br->multicast_lock);
  259. }
  260. static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
  261. int elasticity)
  262. {
  263. struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
  264. struct net_bridge_mdb_htable *mdb;
  265. int err;
  266. mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
  267. if (!mdb)
  268. return -ENOMEM;
  269. mdb->max = max;
  270. mdb->old = old;
  271. mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
  272. if (!mdb->mhash) {
  273. kfree(mdb);
  274. return -ENOMEM;
  275. }
  276. mdb->size = old ? old->size : 0;
  277. mdb->ver = old ? old->ver ^ 1 : 0;
  278. if (!old || elasticity)
  279. get_random_bytes(&mdb->secret, sizeof(mdb->secret));
  280. else
  281. mdb->secret = old->secret;
  282. if (!old)
  283. goto out;
  284. err = br_mdb_copy(mdb, old, elasticity);
  285. if (err) {
  286. kfree(mdb->mhash);
  287. kfree(mdb);
  288. return err;
  289. }
  290. br_mdb_rehash_seq++;
  291. call_rcu_bh(&mdb->rcu, br_mdb_free);
  292. out:
  293. rcu_assign_pointer(*mdbp, mdb);
  294. return 0;
  295. }
  296. static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
  297. __be32 group,
  298. u8 *igmp_type)
  299. {
  300. struct sk_buff *skb;
  301. struct igmphdr *ih;
  302. struct ethhdr *eth;
  303. struct iphdr *iph;
  304. skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
  305. sizeof(*ih) + 4);
  306. if (!skb)
  307. goto out;
  308. skb->protocol = htons(ETH_P_IP);
  309. skb_reset_mac_header(skb);
  310. eth = eth_hdr(skb);
  311. ether_addr_copy(eth->h_source, br->dev->dev_addr);
  312. eth->h_dest[0] = 1;
  313. eth->h_dest[1] = 0;
  314. eth->h_dest[2] = 0x5e;
  315. eth->h_dest[3] = 0;
  316. eth->h_dest[4] = 0;
  317. eth->h_dest[5] = 1;
  318. eth->h_proto = htons(ETH_P_IP);
  319. skb_put(skb, sizeof(*eth));
  320. skb_set_network_header(skb, skb->len);
  321. iph = ip_hdr(skb);
  322. iph->version = 4;
  323. iph->ihl = 6;
  324. iph->tos = 0xc0;
  325. iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
  326. iph->id = 0;
  327. iph->frag_off = htons(IP_DF);
  328. iph->ttl = 1;
  329. iph->protocol = IPPROTO_IGMP;
  330. iph->saddr = br->multicast_query_use_ifaddr ?
  331. inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
  332. iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
  333. ((u8 *)&iph[1])[0] = IPOPT_RA;
  334. ((u8 *)&iph[1])[1] = 4;
  335. ((u8 *)&iph[1])[2] = 0;
  336. ((u8 *)&iph[1])[3] = 0;
  337. ip_send_check(iph);
  338. skb_put(skb, 24);
  339. skb_set_transport_header(skb, skb->len);
  340. ih = igmp_hdr(skb);
  341. *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
  342. ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
  343. ih->code = (group ? br->multicast_last_member_interval :
  344. br->multicast_query_response_interval) /
  345. (HZ / IGMP_TIMER_SCALE);
  346. ih->group = group;
  347. ih->csum = 0;
  348. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  349. skb_put(skb, sizeof(*ih));
  350. __skb_pull(skb, sizeof(*eth));
  351. out:
  352. return skb;
  353. }
  354. #if IS_ENABLED(CONFIG_IPV6)
  355. static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
  356. const struct in6_addr *grp,
  357. u8 *igmp_type)
  358. {
  359. struct sk_buff *skb;
  360. struct ipv6hdr *ip6h;
  361. struct mld_msg *mldq;
  362. struct ethhdr *eth;
  363. u8 *hopopt;
  364. unsigned long interval;
  365. skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
  366. 8 + sizeof(*mldq));
  367. if (!skb)
  368. goto out;
  369. skb->protocol = htons(ETH_P_IPV6);
  370. /* Ethernet header */
  371. skb_reset_mac_header(skb);
  372. eth = eth_hdr(skb);
  373. ether_addr_copy(eth->h_source, br->dev->dev_addr);
  374. eth->h_proto = htons(ETH_P_IPV6);
  375. skb_put(skb, sizeof(*eth));
  376. /* IPv6 header + HbH option */
  377. skb_set_network_header(skb, skb->len);
  378. ip6h = ipv6_hdr(skb);
  379. *(__force __be32 *)ip6h = htonl(0x60000000);
  380. ip6h->payload_len = htons(8 + sizeof(*mldq));
  381. ip6h->nexthdr = IPPROTO_HOPOPTS;
  382. ip6h->hop_limit = 1;
  383. ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
  384. if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
  385. &ip6h->saddr)) {
  386. kfree_skb(skb);
  387. br->has_ipv6_addr = 0;
  388. return NULL;
  389. }
  390. br->has_ipv6_addr = 1;
  391. ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
  392. hopopt = (u8 *)(ip6h + 1);
  393. hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
  394. hopopt[1] = 0; /* length of HbH */
  395. hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
  396. hopopt[3] = 2; /* Length of RA Option */
  397. hopopt[4] = 0; /* Type = 0x0000 (MLD) */
  398. hopopt[5] = 0;
  399. hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
  400. hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
  401. skb_put(skb, sizeof(*ip6h) + 8);
  402. /* ICMPv6 */
  403. skb_set_transport_header(skb, skb->len);
  404. mldq = (struct mld_msg *) icmp6_hdr(skb);
  405. interval = ipv6_addr_any(grp) ?
  406. br->multicast_query_response_interval :
  407. br->multicast_last_member_interval;
  408. *igmp_type = ICMPV6_MGM_QUERY;
  409. mldq->mld_type = ICMPV6_MGM_QUERY;
  410. mldq->mld_code = 0;
  411. mldq->mld_cksum = 0;
  412. mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
  413. mldq->mld_reserved = 0;
  414. mldq->mld_mca = *grp;
  415. /* checksum */
  416. mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  417. sizeof(*mldq), IPPROTO_ICMPV6,
  418. csum_partial(mldq,
  419. sizeof(*mldq), 0));
  420. skb_put(skb, sizeof(*mldq));
  421. __skb_pull(skb, sizeof(*eth));
  422. out:
  423. return skb;
  424. }
  425. #endif
  426. static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
  427. struct br_ip *addr,
  428. u8 *igmp_type)
  429. {
  430. switch (addr->proto) {
  431. case htons(ETH_P_IP):
  432. return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
  433. #if IS_ENABLED(CONFIG_IPV6)
  434. case htons(ETH_P_IPV6):
  435. return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
  436. igmp_type);
  437. #endif
  438. }
  439. return NULL;
  440. }
  441. static struct net_bridge_mdb_entry *br_multicast_get_group(
  442. struct net_bridge *br, struct net_bridge_port *port,
  443. struct br_ip *group, int hash)
  444. {
  445. struct net_bridge_mdb_htable *mdb;
  446. struct net_bridge_mdb_entry *mp;
  447. unsigned int count = 0;
  448. unsigned int max;
  449. int elasticity;
  450. int err;
  451. mdb = rcu_dereference_protected(br->mdb, 1);
  452. hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
  453. count++;
  454. if (unlikely(br_ip_equal(group, &mp->addr)))
  455. return mp;
  456. }
  457. elasticity = 0;
  458. max = mdb->max;
  459. if (unlikely(count > br->hash_elasticity && count)) {
  460. if (net_ratelimit())
  461. br_info(br, "Multicast hash table "
  462. "chain limit reached: %s\n",
  463. port ? port->dev->name : br->dev->name);
  464. elasticity = br->hash_elasticity;
  465. }
  466. if (mdb->size >= max) {
  467. max *= 2;
  468. if (unlikely(max > br->hash_max)) {
  469. br_warn(br, "Multicast hash table maximum of %d "
  470. "reached, disabling snooping: %s\n",
  471. br->hash_max,
  472. port ? port->dev->name : br->dev->name);
  473. err = -E2BIG;
  474. disable:
  475. br->multicast_disabled = 1;
  476. goto err;
  477. }
  478. }
  479. if (max > mdb->max || elasticity) {
  480. if (mdb->old) {
  481. if (net_ratelimit())
  482. br_info(br, "Multicast hash table "
  483. "on fire: %s\n",
  484. port ? port->dev->name : br->dev->name);
  485. err = -EEXIST;
  486. goto err;
  487. }
  488. err = br_mdb_rehash(&br->mdb, max, elasticity);
  489. if (err) {
  490. br_warn(br, "Cannot rehash multicast "
  491. "hash table, disabling snooping: %s, %d, %d\n",
  492. port ? port->dev->name : br->dev->name,
  493. mdb->size, err);
  494. goto disable;
  495. }
  496. err = -EAGAIN;
  497. goto err;
  498. }
  499. return NULL;
  500. err:
  501. mp = ERR_PTR(err);
  502. return mp;
  503. }
  504. struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
  505. struct net_bridge_port *port, struct br_ip *group)
  506. {
  507. struct net_bridge_mdb_htable *mdb;
  508. struct net_bridge_mdb_entry *mp;
  509. int hash;
  510. int err;
  511. mdb = rcu_dereference_protected(br->mdb, 1);
  512. if (!mdb) {
  513. err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
  514. if (err)
  515. return ERR_PTR(err);
  516. goto rehash;
  517. }
  518. hash = br_ip_hash(mdb, group);
  519. mp = br_multicast_get_group(br, port, group, hash);
  520. switch (PTR_ERR(mp)) {
  521. case 0:
  522. break;
  523. case -EAGAIN:
  524. rehash:
  525. mdb = rcu_dereference_protected(br->mdb, 1);
  526. hash = br_ip_hash(mdb, group);
  527. break;
  528. default:
  529. goto out;
  530. }
  531. mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
  532. if (unlikely(!mp))
  533. return ERR_PTR(-ENOMEM);
  534. mp->br = br;
  535. mp->addr = *group;
  536. setup_timer(&mp->timer, br_multicast_group_expired,
  537. (unsigned long)mp);
  538. hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
  539. mdb->size++;
  540. out:
  541. return mp;
  542. }
  543. struct net_bridge_port_group *br_multicast_new_port_group(
  544. struct net_bridge_port *port,
  545. struct br_ip *group,
  546. struct net_bridge_port_group __rcu *next,
  547. unsigned char flags)
  548. {
  549. struct net_bridge_port_group *p;
  550. p = kzalloc(sizeof(*p), GFP_ATOMIC);
  551. if (unlikely(!p))
  552. return NULL;
  553. p->addr = *group;
  554. p->port = port;
  555. p->flags = flags;
  556. rcu_assign_pointer(p->next, next);
  557. hlist_add_head(&p->mglist, &port->mglist);
  558. setup_timer(&p->timer, br_multicast_port_group_expired,
  559. (unsigned long)p);
  560. return p;
  561. }
  562. static int br_multicast_add_group(struct net_bridge *br,
  563. struct net_bridge_port *port,
  564. struct br_ip *group)
  565. {
  566. struct net_bridge_mdb_entry *mp;
  567. struct net_bridge_port_group *p;
  568. struct net_bridge_port_group __rcu **pp;
  569. unsigned long now = jiffies;
  570. int err;
  571. spin_lock(&br->multicast_lock);
  572. if (!netif_running(br->dev) ||
  573. (port && port->state == BR_STATE_DISABLED))
  574. goto out;
  575. mp = br_multicast_new_group(br, port, group);
  576. err = PTR_ERR(mp);
  577. if (IS_ERR(mp))
  578. goto err;
  579. if (!port) {
  580. mp->mglist = true;
  581. mod_timer(&mp->timer, now + br->multicast_membership_interval);
  582. goto out;
  583. }
  584. for (pp = &mp->ports;
  585. (p = mlock_dereference(*pp, br)) != NULL;
  586. pp = &p->next) {
  587. if (p->port == port)
  588. goto found;
  589. if ((unsigned long)p->port < (unsigned long)port)
  590. break;
  591. }
  592. p = br_multicast_new_port_group(port, group, *pp, 0);
  593. if (unlikely(!p))
  594. goto err;
  595. rcu_assign_pointer(*pp, p);
  596. br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
  597. found:
  598. mod_timer(&p->timer, now + br->multicast_membership_interval);
  599. out:
  600. err = 0;
  601. err:
  602. spin_unlock(&br->multicast_lock);
  603. return err;
  604. }
  605. static int br_ip4_multicast_add_group(struct net_bridge *br,
  606. struct net_bridge_port *port,
  607. __be32 group,
  608. __u16 vid)
  609. {
  610. struct br_ip br_group;
  611. if (ipv4_is_local_multicast(group))
  612. return 0;
  613. br_group.u.ip4 = group;
  614. br_group.proto = htons(ETH_P_IP);
  615. br_group.vid = vid;
  616. return br_multicast_add_group(br, port, &br_group);
  617. }
  618. #if IS_ENABLED(CONFIG_IPV6)
  619. static int br_ip6_multicast_add_group(struct net_bridge *br,
  620. struct net_bridge_port *port,
  621. const struct in6_addr *group,
  622. __u16 vid)
  623. {
  624. struct br_ip br_group;
  625. if (ipv6_addr_is_ll_all_nodes(group))
  626. return 0;
  627. br_group.u.ip6 = *group;
  628. br_group.proto = htons(ETH_P_IPV6);
  629. br_group.vid = vid;
  630. return br_multicast_add_group(br, port, &br_group);
  631. }
  632. #endif
  633. static void br_multicast_router_expired(unsigned long data)
  634. {
  635. struct net_bridge_port *port = (void *)data;
  636. struct net_bridge *br = port->br;
  637. spin_lock(&br->multicast_lock);
  638. if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
  639. port->multicast_router == MDB_RTR_TYPE_PERM ||
  640. timer_pending(&port->multicast_router_timer) ||
  641. hlist_unhashed(&port->rlist))
  642. goto out;
  643. hlist_del_init_rcu(&port->rlist);
  644. br_rtr_notify(br->dev, port, RTM_DELMDB);
  645. /* Don't allow timer refresh if the router expired */
  646. if (port->multicast_router == MDB_RTR_TYPE_TEMP)
  647. port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
  648. out:
  649. spin_unlock(&br->multicast_lock);
  650. }
  651. static void br_multicast_local_router_expired(unsigned long data)
  652. {
  653. }
  654. static void br_multicast_querier_expired(struct net_bridge *br,
  655. struct bridge_mcast_own_query *query)
  656. {
  657. spin_lock(&br->multicast_lock);
  658. if (!netif_running(br->dev) || br->multicast_disabled)
  659. goto out;
  660. br_multicast_start_querier(br, query);
  661. out:
  662. spin_unlock(&br->multicast_lock);
  663. }
  664. static void br_ip4_multicast_querier_expired(unsigned long data)
  665. {
  666. struct net_bridge *br = (void *)data;
  667. br_multicast_querier_expired(br, &br->ip4_own_query);
  668. }
  669. #if IS_ENABLED(CONFIG_IPV6)
  670. static void br_ip6_multicast_querier_expired(unsigned long data)
  671. {
  672. struct net_bridge *br = (void *)data;
  673. br_multicast_querier_expired(br, &br->ip6_own_query);
  674. }
  675. #endif
  676. static void br_multicast_select_own_querier(struct net_bridge *br,
  677. struct br_ip *ip,
  678. struct sk_buff *skb)
  679. {
  680. if (ip->proto == htons(ETH_P_IP))
  681. br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
  682. #if IS_ENABLED(CONFIG_IPV6)
  683. else
  684. br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
  685. #endif
  686. }
  687. static void __br_multicast_send_query(struct net_bridge *br,
  688. struct net_bridge_port *port,
  689. struct br_ip *ip)
  690. {
  691. struct sk_buff *skb;
  692. u8 igmp_type;
  693. skb = br_multicast_alloc_query(br, ip, &igmp_type);
  694. if (!skb)
  695. return;
  696. if (port) {
  697. skb->dev = port->dev;
  698. br_multicast_count(br, port, skb, igmp_type,
  699. BR_MCAST_DIR_TX);
  700. NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
  701. dev_net(port->dev), NULL, skb, NULL, skb->dev,
  702. br_dev_queue_push_xmit);
  703. } else {
  704. br_multicast_select_own_querier(br, ip, skb);
  705. br_multicast_count(br, port, skb, igmp_type,
  706. BR_MCAST_DIR_RX);
  707. netif_rx(skb);
  708. }
  709. }
  710. static void br_multicast_send_query(struct net_bridge *br,
  711. struct net_bridge_port *port,
  712. struct bridge_mcast_own_query *own_query)
  713. {
  714. unsigned long time;
  715. struct br_ip br_group;
  716. struct bridge_mcast_other_query *other_query = NULL;
  717. if (!netif_running(br->dev) || br->multicast_disabled ||
  718. !br->multicast_querier)
  719. return;
  720. memset(&br_group.u, 0, sizeof(br_group.u));
  721. if (port ? (own_query == &port->ip4_own_query) :
  722. (own_query == &br->ip4_own_query)) {
  723. other_query = &br->ip4_other_query;
  724. br_group.proto = htons(ETH_P_IP);
  725. #if IS_ENABLED(CONFIG_IPV6)
  726. } else {
  727. other_query = &br->ip6_other_query;
  728. br_group.proto = htons(ETH_P_IPV6);
  729. #endif
  730. }
  731. if (!other_query || timer_pending(&other_query->timer))
  732. return;
  733. __br_multicast_send_query(br, port, &br_group);
  734. time = jiffies;
  735. time += own_query->startup_sent < br->multicast_startup_query_count ?
  736. br->multicast_startup_query_interval :
  737. br->multicast_query_interval;
  738. mod_timer(&own_query->timer, time);
  739. }
  740. static void
  741. br_multicast_port_query_expired(struct net_bridge_port *port,
  742. struct bridge_mcast_own_query *query)
  743. {
  744. struct net_bridge *br = port->br;
  745. spin_lock(&br->multicast_lock);
  746. if (port->state == BR_STATE_DISABLED ||
  747. port->state == BR_STATE_BLOCKING)
  748. goto out;
  749. if (query->startup_sent < br->multicast_startup_query_count)
  750. query->startup_sent++;
  751. br_multicast_send_query(port->br, port, query);
  752. out:
  753. spin_unlock(&br->multicast_lock);
  754. }
  755. static void br_ip4_multicast_port_query_expired(unsigned long data)
  756. {
  757. struct net_bridge_port *port = (void *)data;
  758. br_multicast_port_query_expired(port, &port->ip4_own_query);
  759. }
  760. #if IS_ENABLED(CONFIG_IPV6)
  761. static void br_ip6_multicast_port_query_expired(unsigned long data)
  762. {
  763. struct net_bridge_port *port = (void *)data;
  764. br_multicast_port_query_expired(port, &port->ip6_own_query);
  765. }
  766. #endif
  767. int br_multicast_add_port(struct net_bridge_port *port)
  768. {
  769. port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
  770. setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
  771. (unsigned long)port);
  772. setup_timer(&port->ip4_own_query.timer,
  773. br_ip4_multicast_port_query_expired, (unsigned long)port);
  774. #if IS_ENABLED(CONFIG_IPV6)
  775. setup_timer(&port->ip6_own_query.timer,
  776. br_ip6_multicast_port_query_expired, (unsigned long)port);
  777. #endif
  778. port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
  779. if (!port->mcast_stats)
  780. return -ENOMEM;
  781. return 0;
  782. }
  783. void br_multicast_del_port(struct net_bridge_port *port)
  784. {
  785. struct net_bridge *br = port->br;
  786. struct net_bridge_port_group *pg;
  787. struct hlist_node *n;
  788. /* Take care of the remaining groups, only perm ones should be left */
  789. spin_lock_bh(&br->multicast_lock);
  790. hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
  791. br_multicast_del_pg(br, pg);
  792. spin_unlock_bh(&br->multicast_lock);
  793. del_timer_sync(&port->multicast_router_timer);
  794. free_percpu(port->mcast_stats);
  795. }
  796. static void br_multicast_enable(struct bridge_mcast_own_query *query)
  797. {
  798. query->startup_sent = 0;
  799. if (try_to_del_timer_sync(&query->timer) >= 0 ||
  800. del_timer(&query->timer))
  801. mod_timer(&query->timer, jiffies);
  802. }
  803. static void __br_multicast_enable_port(struct net_bridge_port *port)
  804. {
  805. struct net_bridge *br = port->br;
  806. if (br->multicast_disabled || !netif_running(br->dev))
  807. return;
  808. br_multicast_enable(&port->ip4_own_query);
  809. #if IS_ENABLED(CONFIG_IPV6)
  810. br_multicast_enable(&port->ip6_own_query);
  811. #endif
  812. if (port->multicast_router == MDB_RTR_TYPE_PERM &&
  813. hlist_unhashed(&port->rlist))
  814. br_multicast_add_router(br, port);
  815. }
  816. void br_multicast_enable_port(struct net_bridge_port *port)
  817. {
  818. struct net_bridge *br = port->br;
  819. spin_lock(&br->multicast_lock);
  820. __br_multicast_enable_port(port);
  821. spin_unlock(&br->multicast_lock);
  822. }
  823. void br_multicast_disable_port(struct net_bridge_port *port)
  824. {
  825. struct net_bridge *br = port->br;
  826. struct net_bridge_port_group *pg;
  827. struct hlist_node *n;
  828. spin_lock(&br->multicast_lock);
  829. hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
  830. if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
  831. br_multicast_del_pg(br, pg);
  832. if (!hlist_unhashed(&port->rlist)) {
  833. hlist_del_init_rcu(&port->rlist);
  834. br_rtr_notify(br->dev, port, RTM_DELMDB);
  835. /* Don't allow timer refresh if disabling */
  836. if (port->multicast_router == MDB_RTR_TYPE_TEMP)
  837. port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
  838. }
  839. del_timer(&port->multicast_router_timer);
  840. del_timer(&port->ip4_own_query.timer);
  841. #if IS_ENABLED(CONFIG_IPV6)
  842. del_timer(&port->ip6_own_query.timer);
  843. #endif
  844. spin_unlock(&br->multicast_lock);
  845. }
  846. static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
  847. struct net_bridge_port *port,
  848. struct sk_buff *skb,
  849. u16 vid)
  850. {
  851. struct igmpv3_report *ih;
  852. struct igmpv3_grec *grec;
  853. int i;
  854. int len;
  855. int num;
  856. int type;
  857. int err = 0;
  858. __be32 group;
  859. ih = igmpv3_report_hdr(skb);
  860. num = ntohs(ih->ngrec);
  861. len = skb_transport_offset(skb) + sizeof(*ih);
  862. for (i = 0; i < num; i++) {
  863. len += sizeof(*grec);
  864. if (!pskb_may_pull(skb, len))
  865. return -EINVAL;
  866. grec = (void *)(skb->data + len - sizeof(*grec));
  867. group = grec->grec_mca;
  868. type = grec->grec_type;
  869. len += ntohs(grec->grec_nsrcs) * 4;
  870. if (!pskb_may_pull(skb, len))
  871. return -EINVAL;
  872. /* We treat this as an IGMPv2 report for now. */
  873. switch (type) {
  874. case IGMPV3_MODE_IS_INCLUDE:
  875. case IGMPV3_MODE_IS_EXCLUDE:
  876. case IGMPV3_CHANGE_TO_INCLUDE:
  877. case IGMPV3_CHANGE_TO_EXCLUDE:
  878. case IGMPV3_ALLOW_NEW_SOURCES:
  879. case IGMPV3_BLOCK_OLD_SOURCES:
  880. break;
  881. default:
  882. continue;
  883. }
  884. if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
  885. type == IGMPV3_MODE_IS_INCLUDE) &&
  886. ntohs(grec->grec_nsrcs) == 0) {
  887. br_ip4_multicast_leave_group(br, port, group, vid);
  888. } else {
  889. err = br_ip4_multicast_add_group(br, port, group, vid);
  890. if (err)
  891. break;
  892. }
  893. }
  894. return err;
  895. }
  896. #if IS_ENABLED(CONFIG_IPV6)
  897. static int br_ip6_multicast_mld2_report(struct net_bridge *br,
  898. struct net_bridge_port *port,
  899. struct sk_buff *skb,
  900. u16 vid)
  901. {
  902. struct icmp6hdr *icmp6h;
  903. struct mld2_grec *grec;
  904. int i;
  905. int len;
  906. int num;
  907. int err = 0;
  908. if (!pskb_may_pull(skb, sizeof(*icmp6h)))
  909. return -EINVAL;
  910. icmp6h = icmp6_hdr(skb);
  911. num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
  912. len = skb_transport_offset(skb) + sizeof(*icmp6h);
  913. for (i = 0; i < num; i++) {
  914. __be16 *nsrcs, _nsrcs;
  915. nsrcs = skb_header_pointer(skb,
  916. len + offsetof(struct mld2_grec,
  917. grec_nsrcs),
  918. sizeof(_nsrcs), &_nsrcs);
  919. if (!nsrcs)
  920. return -EINVAL;
  921. if (!pskb_may_pull(skb,
  922. len + sizeof(*grec) +
  923. sizeof(struct in6_addr) * ntohs(*nsrcs)))
  924. return -EINVAL;
  925. grec = (struct mld2_grec *)(skb->data + len);
  926. len += sizeof(*grec) +
  927. sizeof(struct in6_addr) * ntohs(*nsrcs);
  928. /* We treat these as MLDv1 reports for now. */
  929. switch (grec->grec_type) {
  930. case MLD2_MODE_IS_INCLUDE:
  931. case MLD2_MODE_IS_EXCLUDE:
  932. case MLD2_CHANGE_TO_INCLUDE:
  933. case MLD2_CHANGE_TO_EXCLUDE:
  934. case MLD2_ALLOW_NEW_SOURCES:
  935. case MLD2_BLOCK_OLD_SOURCES:
  936. break;
  937. default:
  938. continue;
  939. }
  940. if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
  941. grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
  942. ntohs(*nsrcs) == 0) {
  943. br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
  944. vid);
  945. } else {
  946. err = br_ip6_multicast_add_group(br, port,
  947. &grec->grec_mca, vid);
  948. if (err)
  949. break;
  950. }
  951. }
  952. return err;
  953. }
  954. #endif
  955. static bool br_ip4_multicast_select_querier(struct net_bridge *br,
  956. struct net_bridge_port *port,
  957. __be32 saddr)
  958. {
  959. if (!timer_pending(&br->ip4_own_query.timer) &&
  960. !timer_pending(&br->ip4_other_query.timer))
  961. goto update;
  962. if (!br->ip4_querier.addr.u.ip4)
  963. goto update;
  964. if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
  965. goto update;
  966. return false;
  967. update:
  968. br->ip4_querier.addr.u.ip4 = saddr;
  969. /* update protected by general multicast_lock by caller */
  970. rcu_assign_pointer(br->ip4_querier.port, port);
  971. return true;
  972. }
  973. #if IS_ENABLED(CONFIG_IPV6)
  974. static bool br_ip6_multicast_select_querier(struct net_bridge *br,
  975. struct net_bridge_port *port,
  976. struct in6_addr *saddr)
  977. {
  978. if (!timer_pending(&br->ip6_own_query.timer) &&
  979. !timer_pending(&br->ip6_other_query.timer))
  980. goto update;
  981. if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
  982. goto update;
  983. return false;
  984. update:
  985. br->ip6_querier.addr.u.ip6 = *saddr;
  986. /* update protected by general multicast_lock by caller */
  987. rcu_assign_pointer(br->ip6_querier.port, port);
  988. return true;
  989. }
  990. #endif
  991. static bool br_multicast_select_querier(struct net_bridge *br,
  992. struct net_bridge_port *port,
  993. struct br_ip *saddr)
  994. {
  995. switch (saddr->proto) {
  996. case htons(ETH_P_IP):
  997. return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
  998. #if IS_ENABLED(CONFIG_IPV6)
  999. case htons(ETH_P_IPV6):
  1000. return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
  1001. #endif
  1002. }
  1003. return false;
  1004. }
  1005. static void
  1006. br_multicast_update_query_timer(struct net_bridge *br,
  1007. struct bridge_mcast_other_query *query,
  1008. unsigned long max_delay)
  1009. {
  1010. if (!timer_pending(&query->timer))
  1011. query->delay_time = jiffies + max_delay;
  1012. mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
  1013. }
  1014. /*
  1015. * Add port to router_list
  1016. * list is maintained ordered by pointer value
  1017. * and locked by br->multicast_lock and RCU
  1018. */
  1019. static void br_multicast_add_router(struct net_bridge *br,
  1020. struct net_bridge_port *port)
  1021. {
  1022. struct net_bridge_port *p;
  1023. struct hlist_node *slot = NULL;
  1024. if (!hlist_unhashed(&port->rlist))
  1025. return;
  1026. hlist_for_each_entry(p, &br->router_list, rlist) {
  1027. if ((unsigned long) port >= (unsigned long) p)
  1028. break;
  1029. slot = &p->rlist;
  1030. }
  1031. if (slot)
  1032. hlist_add_behind_rcu(&port->rlist, slot);
  1033. else
  1034. hlist_add_head_rcu(&port->rlist, &br->router_list);
  1035. br_rtr_notify(br->dev, port, RTM_NEWMDB);
  1036. }
  1037. static void br_multicast_mark_router(struct net_bridge *br,
  1038. struct net_bridge_port *port)
  1039. {
  1040. unsigned long now = jiffies;
  1041. if (!port) {
  1042. if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
  1043. mod_timer(&br->multicast_router_timer,
  1044. now + br->multicast_querier_interval);
  1045. return;
  1046. }
  1047. if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
  1048. port->multicast_router == MDB_RTR_TYPE_PERM)
  1049. return;
  1050. br_multicast_add_router(br, port);
  1051. mod_timer(&port->multicast_router_timer,
  1052. now + br->multicast_querier_interval);
  1053. }
  1054. static void br_multicast_query_received(struct net_bridge *br,
  1055. struct net_bridge_port *port,
  1056. struct bridge_mcast_other_query *query,
  1057. struct br_ip *saddr,
  1058. unsigned long max_delay)
  1059. {
  1060. if (!br_multicast_select_querier(br, port, saddr))
  1061. return;
  1062. br_multicast_update_query_timer(br, query, max_delay);
  1063. br_multicast_mark_router(br, port);
  1064. }
  1065. static int br_ip4_multicast_query(struct net_bridge *br,
  1066. struct net_bridge_port *port,
  1067. struct sk_buff *skb,
  1068. u16 vid)
  1069. {
  1070. const struct iphdr *iph = ip_hdr(skb);
  1071. struct igmphdr *ih = igmp_hdr(skb);
  1072. struct net_bridge_mdb_entry *mp;
  1073. struct igmpv3_query *ih3;
  1074. struct net_bridge_port_group *p;
  1075. struct net_bridge_port_group __rcu **pp;
  1076. struct br_ip saddr;
  1077. unsigned long max_delay;
  1078. unsigned long now = jiffies;
  1079. unsigned int offset = skb_transport_offset(skb);
  1080. __be32 group;
  1081. int err = 0;
  1082. spin_lock(&br->multicast_lock);
  1083. if (!netif_running(br->dev) ||
  1084. (port && port->state == BR_STATE_DISABLED))
  1085. goto out;
  1086. group = ih->group;
  1087. if (skb->len == offset + sizeof(*ih)) {
  1088. max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
  1089. if (!max_delay) {
  1090. max_delay = 10 * HZ;
  1091. group = 0;
  1092. }
  1093. } else if (skb->len >= offset + sizeof(*ih3)) {
  1094. ih3 = igmpv3_query_hdr(skb);
  1095. if (ih3->nsrcs)
  1096. goto out;
  1097. max_delay = ih3->code ?
  1098. IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
  1099. } else {
  1100. goto out;
  1101. }
  1102. if (!group) {
  1103. saddr.proto = htons(ETH_P_IP);
  1104. saddr.u.ip4 = iph->saddr;
  1105. br_multicast_query_received(br, port, &br->ip4_other_query,
  1106. &saddr, max_delay);
  1107. goto out;
  1108. }
  1109. mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
  1110. if (!mp)
  1111. goto out;
  1112. max_delay *= br->multicast_last_member_count;
  1113. if (mp->mglist &&
  1114. (timer_pending(&mp->timer) ?
  1115. time_after(mp->timer.expires, now + max_delay) :
  1116. try_to_del_timer_sync(&mp->timer) >= 0))
  1117. mod_timer(&mp->timer, now + max_delay);
  1118. for (pp = &mp->ports;
  1119. (p = mlock_dereference(*pp, br)) != NULL;
  1120. pp = &p->next) {
  1121. if (timer_pending(&p->timer) ?
  1122. time_after(p->timer.expires, now + max_delay) :
  1123. try_to_del_timer_sync(&p->timer) >= 0)
  1124. mod_timer(&p->timer, now + max_delay);
  1125. }
  1126. out:
  1127. spin_unlock(&br->multicast_lock);
  1128. return err;
  1129. }
  1130. #if IS_ENABLED(CONFIG_IPV6)
  1131. static int br_ip6_multicast_query(struct net_bridge *br,
  1132. struct net_bridge_port *port,
  1133. struct sk_buff *skb,
  1134. u16 vid)
  1135. {
  1136. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1137. struct mld_msg *mld;
  1138. struct net_bridge_mdb_entry *mp;
  1139. struct mld2_query *mld2q;
  1140. struct net_bridge_port_group *p;
  1141. struct net_bridge_port_group __rcu **pp;
  1142. struct br_ip saddr;
  1143. unsigned long max_delay;
  1144. unsigned long now = jiffies;
  1145. unsigned int offset = skb_transport_offset(skb);
  1146. const struct in6_addr *group = NULL;
  1147. bool is_general_query;
  1148. int err = 0;
  1149. spin_lock(&br->multicast_lock);
  1150. if (!netif_running(br->dev) ||
  1151. (port && port->state == BR_STATE_DISABLED))
  1152. goto out;
  1153. if (skb->len == offset + sizeof(*mld)) {
  1154. if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
  1155. err = -EINVAL;
  1156. goto out;
  1157. }
  1158. mld = (struct mld_msg *) icmp6_hdr(skb);
  1159. max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
  1160. if (max_delay)
  1161. group = &mld->mld_mca;
  1162. } else {
  1163. if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
  1164. err = -EINVAL;
  1165. goto out;
  1166. }
  1167. mld2q = (struct mld2_query *)icmp6_hdr(skb);
  1168. if (!mld2q->mld2q_nsrcs)
  1169. group = &mld2q->mld2q_mca;
  1170. max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
  1171. }
  1172. is_general_query = group && ipv6_addr_any(group);
  1173. if (is_general_query) {
  1174. saddr.proto = htons(ETH_P_IPV6);
  1175. saddr.u.ip6 = ip6h->saddr;
  1176. br_multicast_query_received(br, port, &br->ip6_other_query,
  1177. &saddr, max_delay);
  1178. goto out;
  1179. } else if (!group) {
  1180. goto out;
  1181. }
  1182. mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
  1183. if (!mp)
  1184. goto out;
  1185. max_delay *= br->multicast_last_member_count;
  1186. if (mp->mglist &&
  1187. (timer_pending(&mp->timer) ?
  1188. time_after(mp->timer.expires, now + max_delay) :
  1189. try_to_del_timer_sync(&mp->timer) >= 0))
  1190. mod_timer(&mp->timer, now + max_delay);
  1191. for (pp = &mp->ports;
  1192. (p = mlock_dereference(*pp, br)) != NULL;
  1193. pp = &p->next) {
  1194. if (timer_pending(&p->timer) ?
  1195. time_after(p->timer.expires, now + max_delay) :
  1196. try_to_del_timer_sync(&p->timer) >= 0)
  1197. mod_timer(&p->timer, now + max_delay);
  1198. }
  1199. out:
  1200. spin_unlock(&br->multicast_lock);
  1201. return err;
  1202. }
  1203. #endif
  1204. static void
  1205. br_multicast_leave_group(struct net_bridge *br,
  1206. struct net_bridge_port *port,
  1207. struct br_ip *group,
  1208. struct bridge_mcast_other_query *other_query,
  1209. struct bridge_mcast_own_query *own_query)
  1210. {
  1211. struct net_bridge_mdb_htable *mdb;
  1212. struct net_bridge_mdb_entry *mp;
  1213. struct net_bridge_port_group *p;
  1214. unsigned long now;
  1215. unsigned long time;
  1216. spin_lock(&br->multicast_lock);
  1217. if (!netif_running(br->dev) ||
  1218. (port && port->state == BR_STATE_DISABLED))
  1219. goto out;
  1220. mdb = mlock_dereference(br->mdb, br);
  1221. mp = br_mdb_ip_get(mdb, group);
  1222. if (!mp)
  1223. goto out;
  1224. if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
  1225. struct net_bridge_port_group __rcu **pp;
  1226. for (pp = &mp->ports;
  1227. (p = mlock_dereference(*pp, br)) != NULL;
  1228. pp = &p->next) {
  1229. if (p->port != port)
  1230. continue;
  1231. rcu_assign_pointer(*pp, p->next);
  1232. hlist_del_init(&p->mglist);
  1233. del_timer(&p->timer);
  1234. call_rcu_bh(&p->rcu, br_multicast_free_pg);
  1235. br_mdb_notify(br->dev, port, group, RTM_DELMDB,
  1236. p->flags);
  1237. if (!mp->ports && !mp->mglist &&
  1238. netif_running(br->dev))
  1239. mod_timer(&mp->timer, jiffies);
  1240. }
  1241. goto out;
  1242. }
  1243. if (timer_pending(&other_query->timer))
  1244. goto out;
  1245. if (br->multicast_querier) {
  1246. __br_multicast_send_query(br, port, &mp->addr);
  1247. time = jiffies + br->multicast_last_member_count *
  1248. br->multicast_last_member_interval;
  1249. mod_timer(&own_query->timer, time);
  1250. for (p = mlock_dereference(mp->ports, br);
  1251. p != NULL;
  1252. p = mlock_dereference(p->next, br)) {
  1253. if (p->port != port)
  1254. continue;
  1255. if (!hlist_unhashed(&p->mglist) &&
  1256. (timer_pending(&p->timer) ?
  1257. time_after(p->timer.expires, time) :
  1258. try_to_del_timer_sync(&p->timer) >= 0)) {
  1259. mod_timer(&p->timer, time);
  1260. }
  1261. break;
  1262. }
  1263. }
  1264. now = jiffies;
  1265. time = now + br->multicast_last_member_count *
  1266. br->multicast_last_member_interval;
  1267. if (!port) {
  1268. if (mp->mglist &&
  1269. (timer_pending(&mp->timer) ?
  1270. time_after(mp->timer.expires, time) :
  1271. try_to_del_timer_sync(&mp->timer) >= 0)) {
  1272. mod_timer(&mp->timer, time);
  1273. }
  1274. goto out;
  1275. }
  1276. for (p = mlock_dereference(mp->ports, br);
  1277. p != NULL;
  1278. p = mlock_dereference(p->next, br)) {
  1279. if (p->port != port)
  1280. continue;
  1281. if (!hlist_unhashed(&p->mglist) &&
  1282. (timer_pending(&p->timer) ?
  1283. time_after(p->timer.expires, time) :
  1284. try_to_del_timer_sync(&p->timer) >= 0)) {
  1285. mod_timer(&p->timer, time);
  1286. }
  1287. break;
  1288. }
  1289. out:
  1290. spin_unlock(&br->multicast_lock);
  1291. }
  1292. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  1293. struct net_bridge_port *port,
  1294. __be32 group,
  1295. __u16 vid)
  1296. {
  1297. struct br_ip br_group;
  1298. struct bridge_mcast_own_query *own_query;
  1299. if (ipv4_is_local_multicast(group))
  1300. return;
  1301. own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
  1302. br_group.u.ip4 = group;
  1303. br_group.proto = htons(ETH_P_IP);
  1304. br_group.vid = vid;
  1305. br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
  1306. own_query);
  1307. }
  1308. #if IS_ENABLED(CONFIG_IPV6)
  1309. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  1310. struct net_bridge_port *port,
  1311. const struct in6_addr *group,
  1312. __u16 vid)
  1313. {
  1314. struct br_ip br_group;
  1315. struct bridge_mcast_own_query *own_query;
  1316. if (ipv6_addr_is_ll_all_nodes(group))
  1317. return;
  1318. own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
  1319. br_group.u.ip6 = *group;
  1320. br_group.proto = htons(ETH_P_IPV6);
  1321. br_group.vid = vid;
  1322. br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
  1323. own_query);
  1324. }
  1325. #endif
  1326. static void br_multicast_err_count(const struct net_bridge *br,
  1327. const struct net_bridge_port *p,
  1328. __be16 proto)
  1329. {
  1330. struct bridge_mcast_stats __percpu *stats;
  1331. struct bridge_mcast_stats *pstats;
  1332. if (!br->multicast_stats_enabled)
  1333. return;
  1334. if (p)
  1335. stats = p->mcast_stats;
  1336. else
  1337. stats = br->mcast_stats;
  1338. if (WARN_ON(!stats))
  1339. return;
  1340. pstats = this_cpu_ptr(stats);
  1341. u64_stats_update_begin(&pstats->syncp);
  1342. switch (proto) {
  1343. case htons(ETH_P_IP):
  1344. pstats->mstats.igmp_parse_errors++;
  1345. break;
  1346. #if IS_ENABLED(CONFIG_IPV6)
  1347. case htons(ETH_P_IPV6):
  1348. pstats->mstats.mld_parse_errors++;
  1349. break;
  1350. #endif
  1351. }
  1352. u64_stats_update_end(&pstats->syncp);
  1353. }
  1354. static int br_multicast_ipv4_rcv(struct net_bridge *br,
  1355. struct net_bridge_port *port,
  1356. struct sk_buff *skb,
  1357. u16 vid)
  1358. {
  1359. struct sk_buff *skb_trimmed = NULL;
  1360. struct igmphdr *ih;
  1361. int err;
  1362. err = ip_mc_check_igmp(skb, &skb_trimmed);
  1363. if (err == -ENOMSG) {
  1364. if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
  1365. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1366. return 0;
  1367. } else if (err < 0) {
  1368. br_multicast_err_count(br, port, skb->protocol);
  1369. return err;
  1370. }
  1371. ih = igmp_hdr(skb);
  1372. BR_INPUT_SKB_CB(skb)->igmp = ih->type;
  1373. switch (ih->type) {
  1374. case IGMP_HOST_MEMBERSHIP_REPORT:
  1375. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1376. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1377. err = br_ip4_multicast_add_group(br, port, ih->group, vid);
  1378. break;
  1379. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1380. err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
  1381. break;
  1382. case IGMP_HOST_MEMBERSHIP_QUERY:
  1383. err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
  1384. break;
  1385. case IGMP_HOST_LEAVE_MESSAGE:
  1386. br_ip4_multicast_leave_group(br, port, ih->group, vid);
  1387. break;
  1388. }
  1389. if (skb_trimmed && skb_trimmed != skb)
  1390. kfree_skb(skb_trimmed);
  1391. br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
  1392. BR_MCAST_DIR_RX);
  1393. return err;
  1394. }
  1395. #if IS_ENABLED(CONFIG_IPV6)
  1396. static int br_multicast_ipv6_rcv(struct net_bridge *br,
  1397. struct net_bridge_port *port,
  1398. struct sk_buff *skb,
  1399. u16 vid)
  1400. {
  1401. struct sk_buff *skb_trimmed = NULL;
  1402. struct mld_msg *mld;
  1403. int err;
  1404. err = ipv6_mc_check_mld(skb, &skb_trimmed);
  1405. if (err == -ENOMSG) {
  1406. if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
  1407. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1408. return 0;
  1409. } else if (err < 0) {
  1410. br_multicast_err_count(br, port, skb->protocol);
  1411. return err;
  1412. }
  1413. mld = (struct mld_msg *)skb_transport_header(skb);
  1414. BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
  1415. switch (mld->mld_type) {
  1416. case ICMPV6_MGM_REPORT:
  1417. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1418. err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
  1419. break;
  1420. case ICMPV6_MLD2_REPORT:
  1421. err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
  1422. break;
  1423. case ICMPV6_MGM_QUERY:
  1424. err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
  1425. break;
  1426. case ICMPV6_MGM_REDUCTION:
  1427. br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
  1428. break;
  1429. }
  1430. if (skb_trimmed && skb_trimmed != skb)
  1431. kfree_skb(skb_trimmed);
  1432. br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
  1433. BR_MCAST_DIR_RX);
  1434. return err;
  1435. }
  1436. #endif
  1437. int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
  1438. struct sk_buff *skb, u16 vid)
  1439. {
  1440. int ret = 0;
  1441. BR_INPUT_SKB_CB(skb)->igmp = 0;
  1442. BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
  1443. if (br->multicast_disabled)
  1444. return 0;
  1445. switch (skb->protocol) {
  1446. case htons(ETH_P_IP):
  1447. ret = br_multicast_ipv4_rcv(br, port, skb, vid);
  1448. break;
  1449. #if IS_ENABLED(CONFIG_IPV6)
  1450. case htons(ETH_P_IPV6):
  1451. ret = br_multicast_ipv6_rcv(br, port, skb, vid);
  1452. break;
  1453. #endif
  1454. }
  1455. return ret;
  1456. }
  1457. static void br_multicast_query_expired(struct net_bridge *br,
  1458. struct bridge_mcast_own_query *query,
  1459. struct bridge_mcast_querier *querier)
  1460. {
  1461. spin_lock(&br->multicast_lock);
  1462. if (query->startup_sent < br->multicast_startup_query_count)
  1463. query->startup_sent++;
  1464. RCU_INIT_POINTER(querier->port, NULL);
  1465. br_multicast_send_query(br, NULL, query);
  1466. spin_unlock(&br->multicast_lock);
  1467. }
  1468. static void br_ip4_multicast_query_expired(unsigned long data)
  1469. {
  1470. struct net_bridge *br = (void *)data;
  1471. br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
  1472. }
  1473. #if IS_ENABLED(CONFIG_IPV6)
  1474. static void br_ip6_multicast_query_expired(unsigned long data)
  1475. {
  1476. struct net_bridge *br = (void *)data;
  1477. br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
  1478. }
  1479. #endif
  1480. void br_multicast_init(struct net_bridge *br)
  1481. {
  1482. br->hash_elasticity = 4;
  1483. br->hash_max = 512;
  1484. br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
  1485. br->multicast_querier = 0;
  1486. br->multicast_query_use_ifaddr = 0;
  1487. br->multicast_last_member_count = 2;
  1488. br->multicast_startup_query_count = 2;
  1489. br->multicast_last_member_interval = HZ;
  1490. br->multicast_query_response_interval = 10 * HZ;
  1491. br->multicast_startup_query_interval = 125 * HZ / 4;
  1492. br->multicast_query_interval = 125 * HZ;
  1493. br->multicast_querier_interval = 255 * HZ;
  1494. br->multicast_membership_interval = 260 * HZ;
  1495. br->ip4_other_query.delay_time = 0;
  1496. br->ip4_querier.port = NULL;
  1497. #if IS_ENABLED(CONFIG_IPV6)
  1498. br->ip6_other_query.delay_time = 0;
  1499. br->ip6_querier.port = NULL;
  1500. #endif
  1501. br->has_ipv6_addr = 1;
  1502. spin_lock_init(&br->multicast_lock);
  1503. setup_timer(&br->multicast_router_timer,
  1504. br_multicast_local_router_expired, 0);
  1505. setup_timer(&br->ip4_other_query.timer,
  1506. br_ip4_multicast_querier_expired, (unsigned long)br);
  1507. setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
  1508. (unsigned long)br);
  1509. #if IS_ENABLED(CONFIG_IPV6)
  1510. setup_timer(&br->ip6_other_query.timer,
  1511. br_ip6_multicast_querier_expired, (unsigned long)br);
  1512. setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
  1513. (unsigned long)br);
  1514. #endif
  1515. }
  1516. static void __br_multicast_open(struct net_bridge *br,
  1517. struct bridge_mcast_own_query *query)
  1518. {
  1519. query->startup_sent = 0;
  1520. if (br->multicast_disabled)
  1521. return;
  1522. mod_timer(&query->timer, jiffies);
  1523. }
  1524. void br_multicast_open(struct net_bridge *br)
  1525. {
  1526. __br_multicast_open(br, &br->ip4_own_query);
  1527. #if IS_ENABLED(CONFIG_IPV6)
  1528. __br_multicast_open(br, &br->ip6_own_query);
  1529. #endif
  1530. }
  1531. void br_multicast_stop(struct net_bridge *br)
  1532. {
  1533. del_timer_sync(&br->multicast_router_timer);
  1534. del_timer_sync(&br->ip4_other_query.timer);
  1535. del_timer_sync(&br->ip4_own_query.timer);
  1536. #if IS_ENABLED(CONFIG_IPV6)
  1537. del_timer_sync(&br->ip6_other_query.timer);
  1538. del_timer_sync(&br->ip6_own_query.timer);
  1539. #endif
  1540. }
  1541. void br_multicast_dev_del(struct net_bridge *br)
  1542. {
  1543. struct net_bridge_mdb_htable *mdb;
  1544. struct net_bridge_mdb_entry *mp;
  1545. struct hlist_node *n;
  1546. u32 ver;
  1547. int i;
  1548. spin_lock_bh(&br->multicast_lock);
  1549. mdb = mlock_dereference(br->mdb, br);
  1550. if (!mdb)
  1551. goto out;
  1552. br->mdb = NULL;
  1553. ver = mdb->ver;
  1554. for (i = 0; i < mdb->max; i++) {
  1555. hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
  1556. hlist[ver]) {
  1557. del_timer(&mp->timer);
  1558. call_rcu_bh(&mp->rcu, br_multicast_free_group);
  1559. }
  1560. }
  1561. if (mdb->old) {
  1562. spin_unlock_bh(&br->multicast_lock);
  1563. rcu_barrier_bh();
  1564. spin_lock_bh(&br->multicast_lock);
  1565. WARN_ON(mdb->old);
  1566. }
  1567. mdb->old = mdb;
  1568. call_rcu_bh(&mdb->rcu, br_mdb_free);
  1569. out:
  1570. spin_unlock_bh(&br->multicast_lock);
  1571. free_percpu(br->mcast_stats);
  1572. }
  1573. int br_multicast_set_router(struct net_bridge *br, unsigned long val)
  1574. {
  1575. int err = -EINVAL;
  1576. spin_lock_bh(&br->multicast_lock);
  1577. switch (val) {
  1578. case MDB_RTR_TYPE_DISABLED:
  1579. case MDB_RTR_TYPE_PERM:
  1580. del_timer(&br->multicast_router_timer);
  1581. /* fall through */
  1582. case MDB_RTR_TYPE_TEMP_QUERY:
  1583. br->multicast_router = val;
  1584. err = 0;
  1585. break;
  1586. }
  1587. spin_unlock_bh(&br->multicast_lock);
  1588. return err;
  1589. }
  1590. static void __del_port_router(struct net_bridge_port *p)
  1591. {
  1592. if (hlist_unhashed(&p->rlist))
  1593. return;
  1594. hlist_del_init_rcu(&p->rlist);
  1595. br_rtr_notify(p->br->dev, p, RTM_DELMDB);
  1596. }
  1597. int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
  1598. {
  1599. struct net_bridge *br = p->br;
  1600. unsigned long now = jiffies;
  1601. int err = -EINVAL;
  1602. spin_lock(&br->multicast_lock);
  1603. if (p->multicast_router == val) {
  1604. /* Refresh the temp router port timer */
  1605. if (p->multicast_router == MDB_RTR_TYPE_TEMP)
  1606. mod_timer(&p->multicast_router_timer,
  1607. now + br->multicast_querier_interval);
  1608. err = 0;
  1609. goto unlock;
  1610. }
  1611. switch (val) {
  1612. case MDB_RTR_TYPE_DISABLED:
  1613. p->multicast_router = MDB_RTR_TYPE_DISABLED;
  1614. __del_port_router(p);
  1615. del_timer(&p->multicast_router_timer);
  1616. break;
  1617. case MDB_RTR_TYPE_TEMP_QUERY:
  1618. p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
  1619. __del_port_router(p);
  1620. break;
  1621. case MDB_RTR_TYPE_PERM:
  1622. p->multicast_router = MDB_RTR_TYPE_PERM;
  1623. del_timer(&p->multicast_router_timer);
  1624. br_multicast_add_router(br, p);
  1625. break;
  1626. case MDB_RTR_TYPE_TEMP:
  1627. p->multicast_router = MDB_RTR_TYPE_TEMP;
  1628. br_multicast_mark_router(br, p);
  1629. break;
  1630. default:
  1631. goto unlock;
  1632. }
  1633. err = 0;
  1634. unlock:
  1635. spin_unlock(&br->multicast_lock);
  1636. return err;
  1637. }
  1638. static void br_multicast_start_querier(struct net_bridge *br,
  1639. struct bridge_mcast_own_query *query)
  1640. {
  1641. struct net_bridge_port *port;
  1642. __br_multicast_open(br, query);
  1643. list_for_each_entry(port, &br->port_list, list) {
  1644. if (port->state == BR_STATE_DISABLED ||
  1645. port->state == BR_STATE_BLOCKING)
  1646. continue;
  1647. if (query == &br->ip4_own_query)
  1648. br_multicast_enable(&port->ip4_own_query);
  1649. #if IS_ENABLED(CONFIG_IPV6)
  1650. else
  1651. br_multicast_enable(&port->ip6_own_query);
  1652. #endif
  1653. }
  1654. }
  1655. int br_multicast_toggle(struct net_bridge *br, unsigned long val)
  1656. {
  1657. struct net_bridge_mdb_htable *mdb;
  1658. struct net_bridge_port *port;
  1659. int err = 0;
  1660. spin_lock_bh(&br->multicast_lock);
  1661. if (br->multicast_disabled == !val)
  1662. goto unlock;
  1663. br->multicast_disabled = !val;
  1664. if (br->multicast_disabled)
  1665. goto unlock;
  1666. if (!netif_running(br->dev))
  1667. goto unlock;
  1668. mdb = mlock_dereference(br->mdb, br);
  1669. if (mdb) {
  1670. if (mdb->old) {
  1671. err = -EEXIST;
  1672. rollback:
  1673. br->multicast_disabled = !!val;
  1674. goto unlock;
  1675. }
  1676. err = br_mdb_rehash(&br->mdb, mdb->max,
  1677. br->hash_elasticity);
  1678. if (err)
  1679. goto rollback;
  1680. }
  1681. br_multicast_open(br);
  1682. list_for_each_entry(port, &br->port_list, list)
  1683. __br_multicast_enable_port(port);
  1684. unlock:
  1685. spin_unlock_bh(&br->multicast_lock);
  1686. return err;
  1687. }
  1688. int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
  1689. {
  1690. unsigned long max_delay;
  1691. val = !!val;
  1692. spin_lock_bh(&br->multicast_lock);
  1693. if (br->multicast_querier == val)
  1694. goto unlock;
  1695. br->multicast_querier = val;
  1696. if (!val)
  1697. goto unlock;
  1698. max_delay = br->multicast_query_response_interval;
  1699. if (!timer_pending(&br->ip4_other_query.timer))
  1700. br->ip4_other_query.delay_time = jiffies + max_delay;
  1701. br_multicast_start_querier(br, &br->ip4_own_query);
  1702. #if IS_ENABLED(CONFIG_IPV6)
  1703. if (!timer_pending(&br->ip6_other_query.timer))
  1704. br->ip6_other_query.delay_time = jiffies + max_delay;
  1705. br_multicast_start_querier(br, &br->ip6_own_query);
  1706. #endif
  1707. unlock:
  1708. spin_unlock_bh(&br->multicast_lock);
  1709. return 0;
  1710. }
  1711. int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
  1712. {
  1713. int err = -EINVAL;
  1714. u32 old;
  1715. struct net_bridge_mdb_htable *mdb;
  1716. spin_lock_bh(&br->multicast_lock);
  1717. if (!is_power_of_2(val))
  1718. goto unlock;
  1719. mdb = mlock_dereference(br->mdb, br);
  1720. if (mdb && val < mdb->size)
  1721. goto unlock;
  1722. err = 0;
  1723. old = br->hash_max;
  1724. br->hash_max = val;
  1725. if (mdb) {
  1726. if (mdb->old) {
  1727. err = -EEXIST;
  1728. rollback:
  1729. br->hash_max = old;
  1730. goto unlock;
  1731. }
  1732. err = br_mdb_rehash(&br->mdb, br->hash_max,
  1733. br->hash_elasticity);
  1734. if (err)
  1735. goto rollback;
  1736. }
  1737. unlock:
  1738. spin_unlock_bh(&br->multicast_lock);
  1739. return err;
  1740. }
  1741. /**
  1742. * br_multicast_list_adjacent - Returns snooped multicast addresses
  1743. * @dev: The bridge port adjacent to which to retrieve addresses
  1744. * @br_ip_list: The list to store found, snooped multicast IP addresses in
  1745. *
  1746. * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
  1747. * snooping feature on all bridge ports of dev's bridge device, excluding
  1748. * the addresses from dev itself.
  1749. *
  1750. * Returns the number of items added to br_ip_list.
  1751. *
  1752. * Notes:
  1753. * - br_ip_list needs to be initialized by caller
  1754. * - br_ip_list might contain duplicates in the end
  1755. * (needs to be taken care of by caller)
  1756. * - br_ip_list needs to be freed by caller
  1757. */
  1758. int br_multicast_list_adjacent(struct net_device *dev,
  1759. struct list_head *br_ip_list)
  1760. {
  1761. struct net_bridge *br;
  1762. struct net_bridge_port *port;
  1763. struct net_bridge_port_group *group;
  1764. struct br_ip_list *entry;
  1765. int count = 0;
  1766. rcu_read_lock();
  1767. if (!br_ip_list || !br_port_exists(dev))
  1768. goto unlock;
  1769. port = br_port_get_rcu(dev);
  1770. if (!port || !port->br)
  1771. goto unlock;
  1772. br = port->br;
  1773. list_for_each_entry_rcu(port, &br->port_list, list) {
  1774. if (!port->dev || port->dev == dev)
  1775. continue;
  1776. hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
  1777. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1778. if (!entry)
  1779. goto unlock;
  1780. entry->addr = group->addr;
  1781. list_add(&entry->list, br_ip_list);
  1782. count++;
  1783. }
  1784. }
  1785. unlock:
  1786. rcu_read_unlock();
  1787. return count;
  1788. }
  1789. EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
  1790. /**
  1791. * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
  1792. * @dev: The bridge port providing the bridge on which to check for a querier
  1793. * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
  1794. *
  1795. * Checks whether the given interface has a bridge on top and if so returns
  1796. * true if a valid querier exists anywhere on the bridged link layer.
  1797. * Otherwise returns false.
  1798. */
  1799. bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
  1800. {
  1801. struct net_bridge *br;
  1802. struct net_bridge_port *port;
  1803. struct ethhdr eth;
  1804. bool ret = false;
  1805. rcu_read_lock();
  1806. if (!br_port_exists(dev))
  1807. goto unlock;
  1808. port = br_port_get_rcu(dev);
  1809. if (!port || !port->br)
  1810. goto unlock;
  1811. br = port->br;
  1812. memset(&eth, 0, sizeof(eth));
  1813. eth.h_proto = htons(proto);
  1814. ret = br_multicast_querier_exists(br, &eth);
  1815. unlock:
  1816. rcu_read_unlock();
  1817. return ret;
  1818. }
  1819. EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
  1820. /**
  1821. * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
  1822. * @dev: The bridge port adjacent to which to check for a querier
  1823. * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
  1824. *
  1825. * Checks whether the given interface has a bridge on top and if so returns
  1826. * true if a selected querier is behind one of the other ports of this
  1827. * bridge. Otherwise returns false.
  1828. */
  1829. bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
  1830. {
  1831. struct net_bridge *br;
  1832. struct net_bridge_port *port;
  1833. bool ret = false;
  1834. rcu_read_lock();
  1835. if (!br_port_exists(dev))
  1836. goto unlock;
  1837. port = br_port_get_rcu(dev);
  1838. if (!port || !port->br)
  1839. goto unlock;
  1840. br = port->br;
  1841. switch (proto) {
  1842. case ETH_P_IP:
  1843. if (!timer_pending(&br->ip4_other_query.timer) ||
  1844. rcu_dereference(br->ip4_querier.port) == port)
  1845. goto unlock;
  1846. break;
  1847. #if IS_ENABLED(CONFIG_IPV6)
  1848. case ETH_P_IPV6:
  1849. if (!timer_pending(&br->ip6_other_query.timer) ||
  1850. rcu_dereference(br->ip6_querier.port) == port)
  1851. goto unlock;
  1852. break;
  1853. #endif
  1854. default:
  1855. goto unlock;
  1856. }
  1857. ret = true;
  1858. unlock:
  1859. rcu_read_unlock();
  1860. return ret;
  1861. }
  1862. EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
  1863. static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
  1864. const struct sk_buff *skb, u8 type, u8 dir)
  1865. {
  1866. struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
  1867. __be16 proto = skb->protocol;
  1868. unsigned int t_len;
  1869. u64_stats_update_begin(&pstats->syncp);
  1870. switch (proto) {
  1871. case htons(ETH_P_IP):
  1872. t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
  1873. switch (type) {
  1874. case IGMP_HOST_MEMBERSHIP_REPORT:
  1875. pstats->mstats.igmp_v1reports[dir]++;
  1876. break;
  1877. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1878. pstats->mstats.igmp_v2reports[dir]++;
  1879. break;
  1880. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1881. pstats->mstats.igmp_v3reports[dir]++;
  1882. break;
  1883. case IGMP_HOST_MEMBERSHIP_QUERY:
  1884. if (t_len != sizeof(struct igmphdr)) {
  1885. pstats->mstats.igmp_v3queries[dir]++;
  1886. } else {
  1887. unsigned int offset = skb_transport_offset(skb);
  1888. struct igmphdr *ih, _ihdr;
  1889. ih = skb_header_pointer(skb, offset,
  1890. sizeof(_ihdr), &_ihdr);
  1891. if (!ih)
  1892. break;
  1893. if (!ih->code)
  1894. pstats->mstats.igmp_v1queries[dir]++;
  1895. else
  1896. pstats->mstats.igmp_v2queries[dir]++;
  1897. }
  1898. break;
  1899. case IGMP_HOST_LEAVE_MESSAGE:
  1900. pstats->mstats.igmp_leaves[dir]++;
  1901. break;
  1902. }
  1903. break;
  1904. #if IS_ENABLED(CONFIG_IPV6)
  1905. case htons(ETH_P_IPV6):
  1906. t_len = ntohs(ipv6_hdr(skb)->payload_len) +
  1907. sizeof(struct ipv6hdr);
  1908. t_len -= skb_network_header_len(skb);
  1909. switch (type) {
  1910. case ICMPV6_MGM_REPORT:
  1911. pstats->mstats.mld_v1reports[dir]++;
  1912. break;
  1913. case ICMPV6_MLD2_REPORT:
  1914. pstats->mstats.mld_v2reports[dir]++;
  1915. break;
  1916. case ICMPV6_MGM_QUERY:
  1917. if (t_len != sizeof(struct mld_msg))
  1918. pstats->mstats.mld_v2queries[dir]++;
  1919. else
  1920. pstats->mstats.mld_v1queries[dir]++;
  1921. break;
  1922. case ICMPV6_MGM_REDUCTION:
  1923. pstats->mstats.mld_leaves[dir]++;
  1924. break;
  1925. }
  1926. break;
  1927. #endif /* CONFIG_IPV6 */
  1928. }
  1929. u64_stats_update_end(&pstats->syncp);
  1930. }
  1931. void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
  1932. const struct sk_buff *skb, u8 type, u8 dir)
  1933. {
  1934. struct bridge_mcast_stats __percpu *stats;
  1935. /* if multicast_disabled is true then igmp type can't be set */
  1936. if (!type || !br->multicast_stats_enabled)
  1937. return;
  1938. if (p)
  1939. stats = p->mcast_stats;
  1940. else
  1941. stats = br->mcast_stats;
  1942. if (WARN_ON(!stats))
  1943. return;
  1944. br_mcast_stats_add(stats, skb, type, dir);
  1945. }
  1946. int br_multicast_init_stats(struct net_bridge *br)
  1947. {
  1948. br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
  1949. if (!br->mcast_stats)
  1950. return -ENOMEM;
  1951. return 0;
  1952. }
  1953. static void mcast_stats_add_dir(u64 *dst, u64 *src)
  1954. {
  1955. dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
  1956. dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
  1957. }
  1958. void br_multicast_get_stats(const struct net_bridge *br,
  1959. const struct net_bridge_port *p,
  1960. struct br_mcast_stats *dest)
  1961. {
  1962. struct bridge_mcast_stats __percpu *stats;
  1963. struct br_mcast_stats tdst;
  1964. int i;
  1965. memset(dest, 0, sizeof(*dest));
  1966. if (p)
  1967. stats = p->mcast_stats;
  1968. else
  1969. stats = br->mcast_stats;
  1970. if (WARN_ON(!stats))
  1971. return;
  1972. memset(&tdst, 0, sizeof(tdst));
  1973. for_each_possible_cpu(i) {
  1974. struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
  1975. struct br_mcast_stats temp;
  1976. unsigned int start;
  1977. do {
  1978. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  1979. memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
  1980. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  1981. mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
  1982. mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
  1983. mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
  1984. mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
  1985. mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
  1986. mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
  1987. mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
  1988. tdst.igmp_parse_errors += temp.igmp_parse_errors;
  1989. mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
  1990. mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
  1991. mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
  1992. mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
  1993. mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
  1994. tdst.mld_parse_errors += temp.mld_parse_errors;
  1995. }
  1996. memcpy(dest, &tdst, sizeof(*dest));
  1997. }