br_multicast.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  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. rcu_read_lock();
  1644. list_for_each_entry_rcu(port, &br->port_list, list) {
  1645. if (port->state == BR_STATE_DISABLED ||
  1646. port->state == BR_STATE_BLOCKING)
  1647. continue;
  1648. if (query == &br->ip4_own_query)
  1649. br_multicast_enable(&port->ip4_own_query);
  1650. #if IS_ENABLED(CONFIG_IPV6)
  1651. else
  1652. br_multicast_enable(&port->ip6_own_query);
  1653. #endif
  1654. }
  1655. rcu_read_unlock();
  1656. }
  1657. int br_multicast_toggle(struct net_bridge *br, unsigned long val)
  1658. {
  1659. struct net_bridge_mdb_htable *mdb;
  1660. struct net_bridge_port *port;
  1661. int err = 0;
  1662. spin_lock_bh(&br->multicast_lock);
  1663. if (br->multicast_disabled == !val)
  1664. goto unlock;
  1665. br->multicast_disabled = !val;
  1666. if (br->multicast_disabled)
  1667. goto unlock;
  1668. if (!netif_running(br->dev))
  1669. goto unlock;
  1670. mdb = mlock_dereference(br->mdb, br);
  1671. if (mdb) {
  1672. if (mdb->old) {
  1673. err = -EEXIST;
  1674. rollback:
  1675. br->multicast_disabled = !!val;
  1676. goto unlock;
  1677. }
  1678. err = br_mdb_rehash(&br->mdb, mdb->max,
  1679. br->hash_elasticity);
  1680. if (err)
  1681. goto rollback;
  1682. }
  1683. br_multicast_open(br);
  1684. list_for_each_entry(port, &br->port_list, list)
  1685. __br_multicast_enable_port(port);
  1686. unlock:
  1687. spin_unlock_bh(&br->multicast_lock);
  1688. return err;
  1689. }
  1690. int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
  1691. {
  1692. unsigned long max_delay;
  1693. val = !!val;
  1694. spin_lock_bh(&br->multicast_lock);
  1695. if (br->multicast_querier == val)
  1696. goto unlock;
  1697. br->multicast_querier = val;
  1698. if (!val)
  1699. goto unlock;
  1700. max_delay = br->multicast_query_response_interval;
  1701. if (!timer_pending(&br->ip4_other_query.timer))
  1702. br->ip4_other_query.delay_time = jiffies + max_delay;
  1703. br_multicast_start_querier(br, &br->ip4_own_query);
  1704. #if IS_ENABLED(CONFIG_IPV6)
  1705. if (!timer_pending(&br->ip6_other_query.timer))
  1706. br->ip6_other_query.delay_time = jiffies + max_delay;
  1707. br_multicast_start_querier(br, &br->ip6_own_query);
  1708. #endif
  1709. unlock:
  1710. spin_unlock_bh(&br->multicast_lock);
  1711. return 0;
  1712. }
  1713. int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
  1714. {
  1715. int err = -EINVAL;
  1716. u32 old;
  1717. struct net_bridge_mdb_htable *mdb;
  1718. spin_lock_bh(&br->multicast_lock);
  1719. if (!is_power_of_2(val))
  1720. goto unlock;
  1721. mdb = mlock_dereference(br->mdb, br);
  1722. if (mdb && val < mdb->size)
  1723. goto unlock;
  1724. err = 0;
  1725. old = br->hash_max;
  1726. br->hash_max = val;
  1727. if (mdb) {
  1728. if (mdb->old) {
  1729. err = -EEXIST;
  1730. rollback:
  1731. br->hash_max = old;
  1732. goto unlock;
  1733. }
  1734. err = br_mdb_rehash(&br->mdb, br->hash_max,
  1735. br->hash_elasticity);
  1736. if (err)
  1737. goto rollback;
  1738. }
  1739. unlock:
  1740. spin_unlock_bh(&br->multicast_lock);
  1741. return err;
  1742. }
  1743. /**
  1744. * br_multicast_list_adjacent - Returns snooped multicast addresses
  1745. * @dev: The bridge port adjacent to which to retrieve addresses
  1746. * @br_ip_list: The list to store found, snooped multicast IP addresses in
  1747. *
  1748. * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
  1749. * snooping feature on all bridge ports of dev's bridge device, excluding
  1750. * the addresses from dev itself.
  1751. *
  1752. * Returns the number of items added to br_ip_list.
  1753. *
  1754. * Notes:
  1755. * - br_ip_list needs to be initialized by caller
  1756. * - br_ip_list might contain duplicates in the end
  1757. * (needs to be taken care of by caller)
  1758. * - br_ip_list needs to be freed by caller
  1759. */
  1760. int br_multicast_list_adjacent(struct net_device *dev,
  1761. struct list_head *br_ip_list)
  1762. {
  1763. struct net_bridge *br;
  1764. struct net_bridge_port *port;
  1765. struct net_bridge_port_group *group;
  1766. struct br_ip_list *entry;
  1767. int count = 0;
  1768. rcu_read_lock();
  1769. if (!br_ip_list || !br_port_exists(dev))
  1770. goto unlock;
  1771. port = br_port_get_rcu(dev);
  1772. if (!port || !port->br)
  1773. goto unlock;
  1774. br = port->br;
  1775. list_for_each_entry_rcu(port, &br->port_list, list) {
  1776. if (!port->dev || port->dev == dev)
  1777. continue;
  1778. hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
  1779. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1780. if (!entry)
  1781. goto unlock;
  1782. entry->addr = group->addr;
  1783. list_add(&entry->list, br_ip_list);
  1784. count++;
  1785. }
  1786. }
  1787. unlock:
  1788. rcu_read_unlock();
  1789. return count;
  1790. }
  1791. EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
  1792. /**
  1793. * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
  1794. * @dev: The bridge port providing the bridge on which to check for a querier
  1795. * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
  1796. *
  1797. * Checks whether the given interface has a bridge on top and if so returns
  1798. * true if a valid querier exists anywhere on the bridged link layer.
  1799. * Otherwise returns false.
  1800. */
  1801. bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
  1802. {
  1803. struct net_bridge *br;
  1804. struct net_bridge_port *port;
  1805. struct ethhdr eth;
  1806. bool ret = false;
  1807. rcu_read_lock();
  1808. if (!br_port_exists(dev))
  1809. goto unlock;
  1810. port = br_port_get_rcu(dev);
  1811. if (!port || !port->br)
  1812. goto unlock;
  1813. br = port->br;
  1814. memset(&eth, 0, sizeof(eth));
  1815. eth.h_proto = htons(proto);
  1816. ret = br_multicast_querier_exists(br, &eth);
  1817. unlock:
  1818. rcu_read_unlock();
  1819. return ret;
  1820. }
  1821. EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
  1822. /**
  1823. * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
  1824. * @dev: The bridge port adjacent to which to check for a querier
  1825. * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
  1826. *
  1827. * Checks whether the given interface has a bridge on top and if so returns
  1828. * true if a selected querier is behind one of the other ports of this
  1829. * bridge. Otherwise returns false.
  1830. */
  1831. bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
  1832. {
  1833. struct net_bridge *br;
  1834. struct net_bridge_port *port;
  1835. bool ret = false;
  1836. rcu_read_lock();
  1837. if (!br_port_exists(dev))
  1838. goto unlock;
  1839. port = br_port_get_rcu(dev);
  1840. if (!port || !port->br)
  1841. goto unlock;
  1842. br = port->br;
  1843. switch (proto) {
  1844. case ETH_P_IP:
  1845. if (!timer_pending(&br->ip4_other_query.timer) ||
  1846. rcu_dereference(br->ip4_querier.port) == port)
  1847. goto unlock;
  1848. break;
  1849. #if IS_ENABLED(CONFIG_IPV6)
  1850. case ETH_P_IPV6:
  1851. if (!timer_pending(&br->ip6_other_query.timer) ||
  1852. rcu_dereference(br->ip6_querier.port) == port)
  1853. goto unlock;
  1854. break;
  1855. #endif
  1856. default:
  1857. goto unlock;
  1858. }
  1859. ret = true;
  1860. unlock:
  1861. rcu_read_unlock();
  1862. return ret;
  1863. }
  1864. EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
  1865. static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
  1866. const struct sk_buff *skb, u8 type, u8 dir)
  1867. {
  1868. struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
  1869. __be16 proto = skb->protocol;
  1870. unsigned int t_len;
  1871. u64_stats_update_begin(&pstats->syncp);
  1872. switch (proto) {
  1873. case htons(ETH_P_IP):
  1874. t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
  1875. switch (type) {
  1876. case IGMP_HOST_MEMBERSHIP_REPORT:
  1877. pstats->mstats.igmp_v1reports[dir]++;
  1878. break;
  1879. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1880. pstats->mstats.igmp_v2reports[dir]++;
  1881. break;
  1882. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1883. pstats->mstats.igmp_v3reports[dir]++;
  1884. break;
  1885. case IGMP_HOST_MEMBERSHIP_QUERY:
  1886. if (t_len != sizeof(struct igmphdr)) {
  1887. pstats->mstats.igmp_v3queries[dir]++;
  1888. } else {
  1889. unsigned int offset = skb_transport_offset(skb);
  1890. struct igmphdr *ih, _ihdr;
  1891. ih = skb_header_pointer(skb, offset,
  1892. sizeof(_ihdr), &_ihdr);
  1893. if (!ih)
  1894. break;
  1895. if (!ih->code)
  1896. pstats->mstats.igmp_v1queries[dir]++;
  1897. else
  1898. pstats->mstats.igmp_v2queries[dir]++;
  1899. }
  1900. break;
  1901. case IGMP_HOST_LEAVE_MESSAGE:
  1902. pstats->mstats.igmp_leaves[dir]++;
  1903. break;
  1904. }
  1905. break;
  1906. #if IS_ENABLED(CONFIG_IPV6)
  1907. case htons(ETH_P_IPV6):
  1908. t_len = ntohs(ipv6_hdr(skb)->payload_len) +
  1909. sizeof(struct ipv6hdr);
  1910. t_len -= skb_network_header_len(skb);
  1911. switch (type) {
  1912. case ICMPV6_MGM_REPORT:
  1913. pstats->mstats.mld_v1reports[dir]++;
  1914. break;
  1915. case ICMPV6_MLD2_REPORT:
  1916. pstats->mstats.mld_v2reports[dir]++;
  1917. break;
  1918. case ICMPV6_MGM_QUERY:
  1919. if (t_len != sizeof(struct mld_msg))
  1920. pstats->mstats.mld_v2queries[dir]++;
  1921. else
  1922. pstats->mstats.mld_v1queries[dir]++;
  1923. break;
  1924. case ICMPV6_MGM_REDUCTION:
  1925. pstats->mstats.mld_leaves[dir]++;
  1926. break;
  1927. }
  1928. break;
  1929. #endif /* CONFIG_IPV6 */
  1930. }
  1931. u64_stats_update_end(&pstats->syncp);
  1932. }
  1933. void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
  1934. const struct sk_buff *skb, u8 type, u8 dir)
  1935. {
  1936. struct bridge_mcast_stats __percpu *stats;
  1937. /* if multicast_disabled is true then igmp type can't be set */
  1938. if (!type || !br->multicast_stats_enabled)
  1939. return;
  1940. if (p)
  1941. stats = p->mcast_stats;
  1942. else
  1943. stats = br->mcast_stats;
  1944. if (WARN_ON(!stats))
  1945. return;
  1946. br_mcast_stats_add(stats, skb, type, dir);
  1947. }
  1948. int br_multicast_init_stats(struct net_bridge *br)
  1949. {
  1950. br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
  1951. if (!br->mcast_stats)
  1952. return -ENOMEM;
  1953. return 0;
  1954. }
  1955. static void mcast_stats_add_dir(u64 *dst, u64 *src)
  1956. {
  1957. dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
  1958. dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
  1959. }
  1960. void br_multicast_get_stats(const struct net_bridge *br,
  1961. const struct net_bridge_port *p,
  1962. struct br_mcast_stats *dest)
  1963. {
  1964. struct bridge_mcast_stats __percpu *stats;
  1965. struct br_mcast_stats tdst;
  1966. int i;
  1967. memset(dest, 0, sizeof(*dest));
  1968. if (p)
  1969. stats = p->mcast_stats;
  1970. else
  1971. stats = br->mcast_stats;
  1972. if (WARN_ON(!stats))
  1973. return;
  1974. memset(&tdst, 0, sizeof(tdst));
  1975. for_each_possible_cpu(i) {
  1976. struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
  1977. struct br_mcast_stats temp;
  1978. unsigned int start;
  1979. do {
  1980. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  1981. memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
  1982. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  1983. mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
  1984. mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
  1985. mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
  1986. mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
  1987. mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
  1988. mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
  1989. mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
  1990. tdst.igmp_parse_errors += temp.igmp_parse_errors;
  1991. mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
  1992. mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
  1993. mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
  1994. mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
  1995. mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
  1996. tdst.mld_parse_errors += temp.mld_parse_errors;
  1997. }
  1998. memcpy(dest, &tdst, sizeof(*dest));
  1999. }