br_multicast.c 62 KB

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