br_multicast.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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/if_ether.h>
  14. #include <linux/igmp.h>
  15. #include <linux/jhash.h>
  16. #include <linux/kernel.h>
  17. #include <linux/log2.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/netfilter_bridge.h>
  20. #include <linux/random.h>
  21. #include <linux/rculist.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/slab.h>
  24. #include <linux/timer.h>
  25. #include <net/ip.h>
  26. #if IS_ENABLED(CONFIG_IPV6)
  27. #include <net/ipv6.h>
  28. #include <net/mld.h>
  29. #include <net/addrconf.h>
  30. #include <net/ip6_checksum.h>
  31. #endif
  32. #include "br_private.h"
  33. #define mlock_dereference(X, br) \
  34. rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
  35. static void br_multicast_add_router(struct net_bridge *br,
  36. struct net_bridge_port *port);
  37. #if IS_ENABLED(CONFIG_IPV6)
  38. static inline int ipv6_is_transient_multicast(const struct in6_addr *addr)
  39. {
  40. if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr))
  41. return 1;
  42. return 0;
  43. }
  44. #endif
  45. static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
  46. {
  47. if (a->proto != b->proto)
  48. return 0;
  49. switch (a->proto) {
  50. case htons(ETH_P_IP):
  51. return a->u.ip4 == b->u.ip4;
  52. #if IS_ENABLED(CONFIG_IPV6)
  53. case htons(ETH_P_IPV6):
  54. return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
  55. #endif
  56. }
  57. return 0;
  58. }
  59. static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
  60. {
  61. return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
  62. }
  63. #if IS_ENABLED(CONFIG_IPV6)
  64. static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
  65. const struct in6_addr *ip)
  66. {
  67. return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
  68. }
  69. #endif
  70. static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
  71. struct br_ip *ip)
  72. {
  73. switch (ip->proto) {
  74. case htons(ETH_P_IP):
  75. return __br_ip4_hash(mdb, ip->u.ip4);
  76. #if IS_ENABLED(CONFIG_IPV6)
  77. case htons(ETH_P_IPV6):
  78. return __br_ip6_hash(mdb, &ip->u.ip6);
  79. #endif
  80. }
  81. return 0;
  82. }
  83. static struct net_bridge_mdb_entry *__br_mdb_ip_get(
  84. struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
  85. {
  86. struct net_bridge_mdb_entry *mp;
  87. struct hlist_node *p;
  88. hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
  89. if (br_ip_equal(&mp->addr, dst))
  90. return mp;
  91. }
  92. return NULL;
  93. }
  94. static struct net_bridge_mdb_entry *br_mdb_ip_get(
  95. struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
  96. {
  97. if (!mdb)
  98. return NULL;
  99. return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
  100. }
  101. static struct net_bridge_mdb_entry *br_mdb_ip4_get(
  102. struct net_bridge_mdb_htable *mdb, __be32 dst)
  103. {
  104. struct br_ip br_dst;
  105. br_dst.u.ip4 = dst;
  106. br_dst.proto = htons(ETH_P_IP);
  107. return br_mdb_ip_get(mdb, &br_dst);
  108. }
  109. #if IS_ENABLED(CONFIG_IPV6)
  110. static struct net_bridge_mdb_entry *br_mdb_ip6_get(
  111. struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
  112. {
  113. struct br_ip br_dst;
  114. br_dst.u.ip6 = *dst;
  115. br_dst.proto = htons(ETH_P_IPV6);
  116. return br_mdb_ip_get(mdb, &br_dst);
  117. }
  118. #endif
  119. struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  120. struct sk_buff *skb)
  121. {
  122. struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
  123. struct br_ip ip;
  124. if (br->multicast_disabled)
  125. return NULL;
  126. if (BR_INPUT_SKB_CB(skb)->igmp)
  127. return NULL;
  128. ip.proto = skb->protocol;
  129. switch (skb->protocol) {
  130. case htons(ETH_P_IP):
  131. ip.u.ip4 = ip_hdr(skb)->daddr;
  132. break;
  133. #if IS_ENABLED(CONFIG_IPV6)
  134. case htons(ETH_P_IPV6):
  135. ip.u.ip6 = ipv6_hdr(skb)->daddr;
  136. break;
  137. #endif
  138. default:
  139. return NULL;
  140. }
  141. return br_mdb_ip_get(mdb, &ip);
  142. }
  143. static void br_mdb_free(struct rcu_head *head)
  144. {
  145. struct net_bridge_mdb_htable *mdb =
  146. container_of(head, struct net_bridge_mdb_htable, rcu);
  147. struct net_bridge_mdb_htable *old = mdb->old;
  148. mdb->old = NULL;
  149. kfree(old->mhash);
  150. kfree(old);
  151. }
  152. static int br_mdb_copy(struct net_bridge_mdb_htable *new,
  153. struct net_bridge_mdb_htable *old,
  154. int elasticity)
  155. {
  156. struct net_bridge_mdb_entry *mp;
  157. struct hlist_node *p;
  158. int maxlen;
  159. int len;
  160. int i;
  161. for (i = 0; i < old->max; i++)
  162. hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
  163. hlist_add_head(&mp->hlist[new->ver],
  164. &new->mhash[br_ip_hash(new, &mp->addr)]);
  165. if (!elasticity)
  166. return 0;
  167. maxlen = 0;
  168. for (i = 0; i < new->max; i++) {
  169. len = 0;
  170. hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
  171. len++;
  172. if (len > maxlen)
  173. maxlen = len;
  174. }
  175. return maxlen > elasticity ? -EINVAL : 0;
  176. }
  177. static void br_multicast_free_pg(struct rcu_head *head)
  178. {
  179. struct net_bridge_port_group *p =
  180. container_of(head, struct net_bridge_port_group, rcu);
  181. kfree(p);
  182. }
  183. static void br_multicast_free_group(struct rcu_head *head)
  184. {
  185. struct net_bridge_mdb_entry *mp =
  186. container_of(head, struct net_bridge_mdb_entry, rcu);
  187. kfree(mp);
  188. }
  189. static void br_multicast_group_expired(unsigned long data)
  190. {
  191. struct net_bridge_mdb_entry *mp = (void *)data;
  192. struct net_bridge *br = mp->br;
  193. struct net_bridge_mdb_htable *mdb;
  194. spin_lock(&br->multicast_lock);
  195. if (!netif_running(br->dev) || timer_pending(&mp->timer))
  196. goto out;
  197. mp->mglist = false;
  198. if (mp->ports)
  199. goto out;
  200. mdb = mlock_dereference(br->mdb, br);
  201. hlist_del_rcu(&mp->hlist[mdb->ver]);
  202. mdb->size--;
  203. call_rcu_bh(&mp->rcu, br_multicast_free_group);
  204. out:
  205. spin_unlock(&br->multicast_lock);
  206. }
  207. static void br_multicast_del_pg(struct net_bridge *br,
  208. struct net_bridge_port_group *pg)
  209. {
  210. struct net_bridge_mdb_htable *mdb;
  211. struct net_bridge_mdb_entry *mp;
  212. struct net_bridge_port_group *p;
  213. struct net_bridge_port_group __rcu **pp;
  214. mdb = mlock_dereference(br->mdb, br);
  215. mp = br_mdb_ip_get(mdb, &pg->addr);
  216. if (WARN_ON(!mp))
  217. return;
  218. for (pp = &mp->ports;
  219. (p = mlock_dereference(*pp, br)) != NULL;
  220. pp = &p->next) {
  221. if (p != pg)
  222. continue;
  223. rcu_assign_pointer(*pp, p->next);
  224. hlist_del_init(&p->mglist);
  225. del_timer(&p->timer);
  226. call_rcu_bh(&p->rcu, br_multicast_free_pg);
  227. if (!mp->ports && !mp->mglist &&
  228. netif_running(br->dev))
  229. mod_timer(&mp->timer, jiffies);
  230. return;
  231. }
  232. WARN_ON(1);
  233. }
  234. static void br_multicast_port_group_expired(unsigned long data)
  235. {
  236. struct net_bridge_port_group *pg = (void *)data;
  237. struct net_bridge *br = pg->port->br;
  238. spin_lock(&br->multicast_lock);
  239. if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
  240. hlist_unhashed(&pg->mglist))
  241. goto out;
  242. br_multicast_del_pg(br, pg);
  243. out:
  244. spin_unlock(&br->multicast_lock);
  245. }
  246. static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
  247. int elasticity)
  248. {
  249. struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
  250. struct net_bridge_mdb_htable *mdb;
  251. int err;
  252. mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
  253. if (!mdb)
  254. return -ENOMEM;
  255. mdb->max = max;
  256. mdb->old = old;
  257. mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
  258. if (!mdb->mhash) {
  259. kfree(mdb);
  260. return -ENOMEM;
  261. }
  262. mdb->size = old ? old->size : 0;
  263. mdb->ver = old ? old->ver ^ 1 : 0;
  264. if (!old || elasticity)
  265. get_random_bytes(&mdb->secret, sizeof(mdb->secret));
  266. else
  267. mdb->secret = old->secret;
  268. if (!old)
  269. goto out;
  270. err = br_mdb_copy(mdb, old, elasticity);
  271. if (err) {
  272. kfree(mdb->mhash);
  273. kfree(mdb);
  274. return err;
  275. }
  276. call_rcu_bh(&mdb->rcu, br_mdb_free);
  277. out:
  278. rcu_assign_pointer(*mdbp, mdb);
  279. return 0;
  280. }
  281. static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
  282. __be32 group)
  283. {
  284. struct sk_buff *skb;
  285. struct igmphdr *ih;
  286. struct ethhdr *eth;
  287. struct iphdr *iph;
  288. skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
  289. sizeof(*ih) + 4);
  290. if (!skb)
  291. goto out;
  292. skb->protocol = htons(ETH_P_IP);
  293. skb_reset_mac_header(skb);
  294. eth = eth_hdr(skb);
  295. memcpy(eth->h_source, br->dev->dev_addr, 6);
  296. eth->h_dest[0] = 1;
  297. eth->h_dest[1] = 0;
  298. eth->h_dest[2] = 0x5e;
  299. eth->h_dest[3] = 0;
  300. eth->h_dest[4] = 0;
  301. eth->h_dest[5] = 1;
  302. eth->h_proto = htons(ETH_P_IP);
  303. skb_put(skb, sizeof(*eth));
  304. skb_set_network_header(skb, skb->len);
  305. iph = ip_hdr(skb);
  306. iph->version = 4;
  307. iph->ihl = 6;
  308. iph->tos = 0xc0;
  309. iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
  310. iph->id = 0;
  311. iph->frag_off = htons(IP_DF);
  312. iph->ttl = 1;
  313. iph->protocol = IPPROTO_IGMP;
  314. iph->saddr = 0;
  315. iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
  316. ((u8 *)&iph[1])[0] = IPOPT_RA;
  317. ((u8 *)&iph[1])[1] = 4;
  318. ((u8 *)&iph[1])[2] = 0;
  319. ((u8 *)&iph[1])[3] = 0;
  320. ip_send_check(iph);
  321. skb_put(skb, 24);
  322. skb_set_transport_header(skb, skb->len);
  323. ih = igmp_hdr(skb);
  324. ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
  325. ih->code = (group ? br->multicast_last_member_interval :
  326. br->multicast_query_response_interval) /
  327. (HZ / IGMP_TIMER_SCALE);
  328. ih->group = group;
  329. ih->csum = 0;
  330. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  331. skb_put(skb, sizeof(*ih));
  332. __skb_pull(skb, sizeof(*eth));
  333. out:
  334. return skb;
  335. }
  336. #if IS_ENABLED(CONFIG_IPV6)
  337. static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
  338. const struct in6_addr *group)
  339. {
  340. struct sk_buff *skb;
  341. struct ipv6hdr *ip6h;
  342. struct mld_msg *mldq;
  343. struct ethhdr *eth;
  344. u8 *hopopt;
  345. unsigned long interval;
  346. skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
  347. 8 + sizeof(*mldq));
  348. if (!skb)
  349. goto out;
  350. skb->protocol = htons(ETH_P_IPV6);
  351. /* Ethernet header */
  352. skb_reset_mac_header(skb);
  353. eth = eth_hdr(skb);
  354. memcpy(eth->h_source, br->dev->dev_addr, 6);
  355. eth->h_proto = htons(ETH_P_IPV6);
  356. skb_put(skb, sizeof(*eth));
  357. /* IPv6 header + HbH option */
  358. skb_set_network_header(skb, skb->len);
  359. ip6h = ipv6_hdr(skb);
  360. *(__force __be32 *)ip6h = htonl(0x60000000);
  361. ip6h->payload_len = htons(8 + sizeof(*mldq));
  362. ip6h->nexthdr = IPPROTO_HOPOPTS;
  363. ip6h->hop_limit = 1;
  364. ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
  365. if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
  366. &ip6h->saddr)) {
  367. kfree_skb(skb);
  368. return NULL;
  369. }
  370. ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
  371. hopopt = (u8 *)(ip6h + 1);
  372. hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
  373. hopopt[1] = 0; /* length of HbH */
  374. hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
  375. hopopt[3] = 2; /* Length of RA Option */
  376. hopopt[4] = 0; /* Type = 0x0000 (MLD) */
  377. hopopt[5] = 0;
  378. hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
  379. hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
  380. skb_put(skb, sizeof(*ip6h) + 8);
  381. /* ICMPv6 */
  382. skb_set_transport_header(skb, skb->len);
  383. mldq = (struct mld_msg *) icmp6_hdr(skb);
  384. interval = ipv6_addr_any(group) ?
  385. br->multicast_query_response_interval :
  386. br->multicast_last_member_interval;
  387. mldq->mld_type = ICMPV6_MGM_QUERY;
  388. mldq->mld_code = 0;
  389. mldq->mld_cksum = 0;
  390. mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
  391. mldq->mld_reserved = 0;
  392. mldq->mld_mca = *group;
  393. /* checksum */
  394. mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  395. sizeof(*mldq), IPPROTO_ICMPV6,
  396. csum_partial(mldq,
  397. sizeof(*mldq), 0));
  398. skb_put(skb, sizeof(*mldq));
  399. __skb_pull(skb, sizeof(*eth));
  400. out:
  401. return skb;
  402. }
  403. #endif
  404. static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
  405. struct br_ip *addr)
  406. {
  407. switch (addr->proto) {
  408. case htons(ETH_P_IP):
  409. return br_ip4_multicast_alloc_query(br, addr->u.ip4);
  410. #if IS_ENABLED(CONFIG_IPV6)
  411. case htons(ETH_P_IPV6):
  412. return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
  413. #endif
  414. }
  415. return NULL;
  416. }
  417. static struct net_bridge_mdb_entry *br_multicast_get_group(
  418. struct net_bridge *br, struct net_bridge_port *port,
  419. struct br_ip *group, int hash)
  420. {
  421. struct net_bridge_mdb_htable *mdb;
  422. struct net_bridge_mdb_entry *mp;
  423. struct hlist_node *p;
  424. unsigned count = 0;
  425. unsigned max;
  426. int elasticity;
  427. int err;
  428. mdb = rcu_dereference_protected(br->mdb, 1);
  429. hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
  430. count++;
  431. if (unlikely(br_ip_equal(group, &mp->addr)))
  432. return mp;
  433. }
  434. elasticity = 0;
  435. max = mdb->max;
  436. if (unlikely(count > br->hash_elasticity && count)) {
  437. if (net_ratelimit())
  438. br_info(br, "Multicast hash table "
  439. "chain limit reached: %s\n",
  440. port ? port->dev->name : br->dev->name);
  441. elasticity = br->hash_elasticity;
  442. }
  443. if (mdb->size >= max) {
  444. max *= 2;
  445. if (unlikely(max >= br->hash_max)) {
  446. br_warn(br, "Multicast hash table maximum "
  447. "reached, disabling snooping: %s, %d\n",
  448. port ? port->dev->name : br->dev->name, max);
  449. err = -E2BIG;
  450. disable:
  451. br->multicast_disabled = 1;
  452. goto err;
  453. }
  454. }
  455. if (max > mdb->max || elasticity) {
  456. if (mdb->old) {
  457. if (net_ratelimit())
  458. br_info(br, "Multicast hash table "
  459. "on fire: %s\n",
  460. port ? port->dev->name : br->dev->name);
  461. err = -EEXIST;
  462. goto err;
  463. }
  464. err = br_mdb_rehash(&br->mdb, max, elasticity);
  465. if (err) {
  466. br_warn(br, "Cannot rehash multicast "
  467. "hash table, disabling snooping: %s, %d, %d\n",
  468. port ? port->dev->name : br->dev->name,
  469. mdb->size, err);
  470. goto disable;
  471. }
  472. err = -EAGAIN;
  473. goto err;
  474. }
  475. return NULL;
  476. err:
  477. mp = ERR_PTR(err);
  478. return mp;
  479. }
  480. static struct net_bridge_mdb_entry *br_multicast_new_group(
  481. struct net_bridge *br, struct net_bridge_port *port,
  482. struct br_ip *group)
  483. {
  484. struct net_bridge_mdb_htable *mdb;
  485. struct net_bridge_mdb_entry *mp;
  486. int hash;
  487. int err;
  488. mdb = rcu_dereference_protected(br->mdb, 1);
  489. if (!mdb) {
  490. err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
  491. if (err)
  492. return ERR_PTR(err);
  493. goto rehash;
  494. }
  495. hash = br_ip_hash(mdb, group);
  496. mp = br_multicast_get_group(br, port, group, hash);
  497. switch (PTR_ERR(mp)) {
  498. case 0:
  499. break;
  500. case -EAGAIN:
  501. rehash:
  502. mdb = rcu_dereference_protected(br->mdb, 1);
  503. hash = br_ip_hash(mdb, group);
  504. break;
  505. default:
  506. goto out;
  507. }
  508. mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
  509. if (unlikely(!mp))
  510. return ERR_PTR(-ENOMEM);
  511. mp->br = br;
  512. mp->addr = *group;
  513. setup_timer(&mp->timer, br_multicast_group_expired,
  514. (unsigned long)mp);
  515. hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
  516. mdb->size++;
  517. out:
  518. return mp;
  519. }
  520. static int br_multicast_add_group(struct net_bridge *br,
  521. struct net_bridge_port *port,
  522. struct br_ip *group)
  523. {
  524. struct net_bridge_mdb_entry *mp;
  525. struct net_bridge_port_group *p;
  526. struct net_bridge_port_group __rcu **pp;
  527. unsigned long now = jiffies;
  528. int err;
  529. spin_lock(&br->multicast_lock);
  530. if (!netif_running(br->dev) ||
  531. (port && port->state == BR_STATE_DISABLED))
  532. goto out;
  533. mp = br_multicast_new_group(br, port, group);
  534. err = PTR_ERR(mp);
  535. if (IS_ERR(mp))
  536. goto err;
  537. if (!port) {
  538. mp->mglist = true;
  539. mod_timer(&mp->timer, now + br->multicast_membership_interval);
  540. goto out;
  541. }
  542. for (pp = &mp->ports;
  543. (p = mlock_dereference(*pp, br)) != NULL;
  544. pp = &p->next) {
  545. if (p->port == port)
  546. goto found;
  547. if ((unsigned long)p->port < (unsigned long)port)
  548. break;
  549. }
  550. p = kzalloc(sizeof(*p), GFP_ATOMIC);
  551. err = -ENOMEM;
  552. if (unlikely(!p))
  553. goto err;
  554. p->addr = *group;
  555. p->port = port;
  556. p->next = *pp;
  557. hlist_add_head(&p->mglist, &port->mglist);
  558. setup_timer(&p->timer, br_multicast_port_group_expired,
  559. (unsigned long)p);
  560. rcu_assign_pointer(*pp, p);
  561. found:
  562. mod_timer(&p->timer, now + br->multicast_membership_interval);
  563. out:
  564. err = 0;
  565. err:
  566. spin_unlock(&br->multicast_lock);
  567. return err;
  568. }
  569. static int br_ip4_multicast_add_group(struct net_bridge *br,
  570. struct net_bridge_port *port,
  571. __be32 group)
  572. {
  573. struct br_ip br_group;
  574. if (ipv4_is_local_multicast(group))
  575. return 0;
  576. br_group.u.ip4 = group;
  577. br_group.proto = htons(ETH_P_IP);
  578. return br_multicast_add_group(br, port, &br_group);
  579. }
  580. #if IS_ENABLED(CONFIG_IPV6)
  581. static int br_ip6_multicast_add_group(struct net_bridge *br,
  582. struct net_bridge_port *port,
  583. const struct in6_addr *group)
  584. {
  585. struct br_ip br_group;
  586. if (!ipv6_is_transient_multicast(group))
  587. return 0;
  588. br_group.u.ip6 = *group;
  589. br_group.proto = htons(ETH_P_IPV6);
  590. return br_multicast_add_group(br, port, &br_group);
  591. }
  592. #endif
  593. static void br_multicast_router_expired(unsigned long data)
  594. {
  595. struct net_bridge_port *port = (void *)data;
  596. struct net_bridge *br = port->br;
  597. spin_lock(&br->multicast_lock);
  598. if (port->multicast_router != 1 ||
  599. timer_pending(&port->multicast_router_timer) ||
  600. hlist_unhashed(&port->rlist))
  601. goto out;
  602. hlist_del_init_rcu(&port->rlist);
  603. out:
  604. spin_unlock(&br->multicast_lock);
  605. }
  606. static void br_multicast_local_router_expired(unsigned long data)
  607. {
  608. }
  609. static void __br_multicast_send_query(struct net_bridge *br,
  610. struct net_bridge_port *port,
  611. struct br_ip *ip)
  612. {
  613. struct sk_buff *skb;
  614. skb = br_multicast_alloc_query(br, ip);
  615. if (!skb)
  616. return;
  617. if (port) {
  618. __skb_push(skb, sizeof(struct ethhdr));
  619. skb->dev = port->dev;
  620. NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
  621. dev_queue_xmit);
  622. } else
  623. netif_rx(skb);
  624. }
  625. static void br_multicast_send_query(struct net_bridge *br,
  626. struct net_bridge_port *port, u32 sent)
  627. {
  628. unsigned long time;
  629. struct br_ip br_group;
  630. if (!netif_running(br->dev) || br->multicast_disabled ||
  631. timer_pending(&br->multicast_querier_timer))
  632. return;
  633. memset(&br_group.u, 0, sizeof(br_group.u));
  634. br_group.proto = htons(ETH_P_IP);
  635. __br_multicast_send_query(br, port, &br_group);
  636. #if IS_ENABLED(CONFIG_IPV6)
  637. br_group.proto = htons(ETH_P_IPV6);
  638. __br_multicast_send_query(br, port, &br_group);
  639. #endif
  640. time = jiffies;
  641. time += sent < br->multicast_startup_query_count ?
  642. br->multicast_startup_query_interval :
  643. br->multicast_query_interval;
  644. mod_timer(port ? &port->multicast_query_timer :
  645. &br->multicast_query_timer, time);
  646. }
  647. static void br_multicast_port_query_expired(unsigned long data)
  648. {
  649. struct net_bridge_port *port = (void *)data;
  650. struct net_bridge *br = port->br;
  651. spin_lock(&br->multicast_lock);
  652. if (port->state == BR_STATE_DISABLED ||
  653. port->state == BR_STATE_BLOCKING)
  654. goto out;
  655. if (port->multicast_startup_queries_sent <
  656. br->multicast_startup_query_count)
  657. port->multicast_startup_queries_sent++;
  658. br_multicast_send_query(port->br, port,
  659. port->multicast_startup_queries_sent);
  660. out:
  661. spin_unlock(&br->multicast_lock);
  662. }
  663. void br_multicast_add_port(struct net_bridge_port *port)
  664. {
  665. port->multicast_router = 1;
  666. setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
  667. (unsigned long)port);
  668. setup_timer(&port->multicast_query_timer,
  669. br_multicast_port_query_expired, (unsigned long)port);
  670. }
  671. void br_multicast_del_port(struct net_bridge_port *port)
  672. {
  673. del_timer_sync(&port->multicast_router_timer);
  674. }
  675. static void __br_multicast_enable_port(struct net_bridge_port *port)
  676. {
  677. port->multicast_startup_queries_sent = 0;
  678. if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
  679. del_timer(&port->multicast_query_timer))
  680. mod_timer(&port->multicast_query_timer, jiffies);
  681. }
  682. void br_multicast_enable_port(struct net_bridge_port *port)
  683. {
  684. struct net_bridge *br = port->br;
  685. spin_lock(&br->multicast_lock);
  686. if (br->multicast_disabled || !netif_running(br->dev))
  687. goto out;
  688. __br_multicast_enable_port(port);
  689. if (port->multicast_router == 2 && hlist_unhashed(&port->rlist))
  690. br_multicast_add_router(br, port);
  691. out:
  692. spin_unlock(&br->multicast_lock);
  693. }
  694. void br_multicast_disable_port(struct net_bridge_port *port)
  695. {
  696. struct net_bridge *br = port->br;
  697. struct net_bridge_port_group *pg;
  698. struct hlist_node *p, *n;
  699. spin_lock(&br->multicast_lock);
  700. hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
  701. br_multicast_del_pg(br, pg);
  702. if (!hlist_unhashed(&port->rlist))
  703. hlist_del_init_rcu(&port->rlist);
  704. del_timer(&port->multicast_router_timer);
  705. del_timer(&port->multicast_query_timer);
  706. spin_unlock(&br->multicast_lock);
  707. }
  708. static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
  709. struct net_bridge_port *port,
  710. struct sk_buff *skb)
  711. {
  712. struct igmpv3_report *ih;
  713. struct igmpv3_grec *grec;
  714. int i;
  715. int len;
  716. int num;
  717. int type;
  718. int err = 0;
  719. __be32 group;
  720. if (!pskb_may_pull(skb, sizeof(*ih)))
  721. return -EINVAL;
  722. ih = igmpv3_report_hdr(skb);
  723. num = ntohs(ih->ngrec);
  724. len = sizeof(*ih);
  725. for (i = 0; i < num; i++) {
  726. len += sizeof(*grec);
  727. if (!pskb_may_pull(skb, len))
  728. return -EINVAL;
  729. grec = (void *)(skb->data + len - sizeof(*grec));
  730. group = grec->grec_mca;
  731. type = grec->grec_type;
  732. len += ntohs(grec->grec_nsrcs) * 4;
  733. if (!pskb_may_pull(skb, len))
  734. return -EINVAL;
  735. /* We treat this as an IGMPv2 report for now. */
  736. switch (type) {
  737. case IGMPV3_MODE_IS_INCLUDE:
  738. case IGMPV3_MODE_IS_EXCLUDE:
  739. case IGMPV3_CHANGE_TO_INCLUDE:
  740. case IGMPV3_CHANGE_TO_EXCLUDE:
  741. case IGMPV3_ALLOW_NEW_SOURCES:
  742. case IGMPV3_BLOCK_OLD_SOURCES:
  743. break;
  744. default:
  745. continue;
  746. }
  747. err = br_ip4_multicast_add_group(br, port, group);
  748. if (err)
  749. break;
  750. }
  751. return err;
  752. }
  753. #if IS_ENABLED(CONFIG_IPV6)
  754. static int br_ip6_multicast_mld2_report(struct net_bridge *br,
  755. struct net_bridge_port *port,
  756. struct sk_buff *skb)
  757. {
  758. struct icmp6hdr *icmp6h;
  759. struct mld2_grec *grec;
  760. int i;
  761. int len;
  762. int num;
  763. int err = 0;
  764. if (!pskb_may_pull(skb, sizeof(*icmp6h)))
  765. return -EINVAL;
  766. icmp6h = icmp6_hdr(skb);
  767. num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
  768. len = sizeof(*icmp6h);
  769. for (i = 0; i < num; i++) {
  770. __be16 *nsrcs, _nsrcs;
  771. nsrcs = skb_header_pointer(skb,
  772. len + offsetof(struct mld2_grec,
  773. grec_nsrcs),
  774. sizeof(_nsrcs), &_nsrcs);
  775. if (!nsrcs)
  776. return -EINVAL;
  777. if (!pskb_may_pull(skb,
  778. len + sizeof(*grec) +
  779. sizeof(struct in6_addr) * ntohs(*nsrcs)))
  780. return -EINVAL;
  781. grec = (struct mld2_grec *)(skb->data + len);
  782. len += sizeof(*grec) +
  783. sizeof(struct in6_addr) * ntohs(*nsrcs);
  784. /* We treat these as MLDv1 reports for now. */
  785. switch (grec->grec_type) {
  786. case MLD2_MODE_IS_INCLUDE:
  787. case MLD2_MODE_IS_EXCLUDE:
  788. case MLD2_CHANGE_TO_INCLUDE:
  789. case MLD2_CHANGE_TO_EXCLUDE:
  790. case MLD2_ALLOW_NEW_SOURCES:
  791. case MLD2_BLOCK_OLD_SOURCES:
  792. break;
  793. default:
  794. continue;
  795. }
  796. err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
  797. if (err)
  798. break;
  799. }
  800. return err;
  801. }
  802. #endif
  803. /*
  804. * Add port to rotuer_list
  805. * list is maintained ordered by pointer value
  806. * and locked by br->multicast_lock and RCU
  807. */
  808. static void br_multicast_add_router(struct net_bridge *br,
  809. struct net_bridge_port *port)
  810. {
  811. struct net_bridge_port *p;
  812. struct hlist_node *n, *slot = NULL;
  813. if (!hlist_unhashed(&port->rlist))
  814. return;
  815. hlist_for_each_entry(p, n, &br->router_list, rlist) {
  816. if ((unsigned long) port >= (unsigned long) p)
  817. break;
  818. slot = n;
  819. }
  820. if (slot)
  821. hlist_add_after_rcu(slot, &port->rlist);
  822. else
  823. hlist_add_head_rcu(&port->rlist, &br->router_list);
  824. }
  825. static void br_multicast_mark_router(struct net_bridge *br,
  826. struct net_bridge_port *port)
  827. {
  828. unsigned long now = jiffies;
  829. if (!port) {
  830. if (br->multicast_router == 1)
  831. mod_timer(&br->multicast_router_timer,
  832. now + br->multicast_querier_interval);
  833. return;
  834. }
  835. if (port->multicast_router != 1)
  836. return;
  837. br_multicast_add_router(br, port);
  838. mod_timer(&port->multicast_router_timer,
  839. now + br->multicast_querier_interval);
  840. }
  841. static void br_multicast_query_received(struct net_bridge *br,
  842. struct net_bridge_port *port,
  843. int saddr)
  844. {
  845. if (saddr)
  846. mod_timer(&br->multicast_querier_timer,
  847. jiffies + br->multicast_querier_interval);
  848. else if (timer_pending(&br->multicast_querier_timer))
  849. return;
  850. br_multicast_mark_router(br, port);
  851. }
  852. static int br_ip4_multicast_query(struct net_bridge *br,
  853. struct net_bridge_port *port,
  854. struct sk_buff *skb)
  855. {
  856. const struct iphdr *iph = ip_hdr(skb);
  857. struct igmphdr *ih = igmp_hdr(skb);
  858. struct net_bridge_mdb_entry *mp;
  859. struct igmpv3_query *ih3;
  860. struct net_bridge_port_group *p;
  861. struct net_bridge_port_group __rcu **pp;
  862. unsigned long max_delay;
  863. unsigned long now = jiffies;
  864. __be32 group;
  865. int err = 0;
  866. spin_lock(&br->multicast_lock);
  867. if (!netif_running(br->dev) ||
  868. (port && port->state == BR_STATE_DISABLED))
  869. goto out;
  870. br_multicast_query_received(br, port, !!iph->saddr);
  871. group = ih->group;
  872. if (skb->len == sizeof(*ih)) {
  873. max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
  874. if (!max_delay) {
  875. max_delay = 10 * HZ;
  876. group = 0;
  877. }
  878. } else {
  879. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
  880. err = -EINVAL;
  881. goto out;
  882. }
  883. ih3 = igmpv3_query_hdr(skb);
  884. if (ih3->nsrcs)
  885. goto out;
  886. max_delay = ih3->code ?
  887. IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
  888. }
  889. if (!group)
  890. goto out;
  891. mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group);
  892. if (!mp)
  893. goto out;
  894. max_delay *= br->multicast_last_member_count;
  895. if (mp->mglist &&
  896. (timer_pending(&mp->timer) ?
  897. time_after(mp->timer.expires, now + max_delay) :
  898. try_to_del_timer_sync(&mp->timer) >= 0))
  899. mod_timer(&mp->timer, now + max_delay);
  900. for (pp = &mp->ports;
  901. (p = mlock_dereference(*pp, br)) != NULL;
  902. pp = &p->next) {
  903. if (timer_pending(&p->timer) ?
  904. time_after(p->timer.expires, now + max_delay) :
  905. try_to_del_timer_sync(&p->timer) >= 0)
  906. mod_timer(&p->timer, now + max_delay);
  907. }
  908. out:
  909. spin_unlock(&br->multicast_lock);
  910. return err;
  911. }
  912. #if IS_ENABLED(CONFIG_IPV6)
  913. static int br_ip6_multicast_query(struct net_bridge *br,
  914. struct net_bridge_port *port,
  915. struct sk_buff *skb)
  916. {
  917. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  918. struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
  919. struct net_bridge_mdb_entry *mp;
  920. struct mld2_query *mld2q;
  921. struct net_bridge_port_group *p;
  922. struct net_bridge_port_group __rcu **pp;
  923. unsigned long max_delay;
  924. unsigned long now = jiffies;
  925. const struct in6_addr *group = NULL;
  926. int err = 0;
  927. spin_lock(&br->multicast_lock);
  928. if (!netif_running(br->dev) ||
  929. (port && port->state == BR_STATE_DISABLED))
  930. goto out;
  931. br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
  932. /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
  933. if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) {
  934. err = -EINVAL;
  935. goto out;
  936. }
  937. if (skb->len == sizeof(*mld)) {
  938. if (!pskb_may_pull(skb, sizeof(*mld))) {
  939. err = -EINVAL;
  940. goto out;
  941. }
  942. mld = (struct mld_msg *) icmp6_hdr(skb);
  943. max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
  944. if (max_delay)
  945. group = &mld->mld_mca;
  946. } else if (skb->len >= sizeof(*mld2q)) {
  947. if (!pskb_may_pull(skb, sizeof(*mld2q))) {
  948. err = -EINVAL;
  949. goto out;
  950. }
  951. mld2q = (struct mld2_query *)icmp6_hdr(skb);
  952. if (!mld2q->mld2q_nsrcs)
  953. group = &mld2q->mld2q_mca;
  954. max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mld2q->mld2q_mrc))), 1UL);
  955. }
  956. if (!group)
  957. goto out;
  958. mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group);
  959. if (!mp)
  960. goto out;
  961. max_delay *= br->multicast_last_member_count;
  962. if (mp->mglist &&
  963. (timer_pending(&mp->timer) ?
  964. time_after(mp->timer.expires, now + max_delay) :
  965. try_to_del_timer_sync(&mp->timer) >= 0))
  966. mod_timer(&mp->timer, now + max_delay);
  967. for (pp = &mp->ports;
  968. (p = mlock_dereference(*pp, br)) != NULL;
  969. pp = &p->next) {
  970. if (timer_pending(&p->timer) ?
  971. time_after(p->timer.expires, now + max_delay) :
  972. try_to_del_timer_sync(&p->timer) >= 0)
  973. mod_timer(&p->timer, now + max_delay);
  974. }
  975. out:
  976. spin_unlock(&br->multicast_lock);
  977. return err;
  978. }
  979. #endif
  980. static void br_multicast_leave_group(struct net_bridge *br,
  981. struct net_bridge_port *port,
  982. struct br_ip *group)
  983. {
  984. struct net_bridge_mdb_htable *mdb;
  985. struct net_bridge_mdb_entry *mp;
  986. struct net_bridge_port_group *p;
  987. unsigned long now;
  988. unsigned long time;
  989. spin_lock(&br->multicast_lock);
  990. if (!netif_running(br->dev) ||
  991. (port && port->state == BR_STATE_DISABLED) ||
  992. timer_pending(&br->multicast_querier_timer))
  993. goto out;
  994. mdb = mlock_dereference(br->mdb, br);
  995. mp = br_mdb_ip_get(mdb, group);
  996. if (!mp)
  997. goto out;
  998. now = jiffies;
  999. time = now + br->multicast_last_member_count *
  1000. br->multicast_last_member_interval;
  1001. if (!port) {
  1002. if (mp->mglist &&
  1003. (timer_pending(&mp->timer) ?
  1004. time_after(mp->timer.expires, time) :
  1005. try_to_del_timer_sync(&mp->timer) >= 0)) {
  1006. mod_timer(&mp->timer, time);
  1007. }
  1008. goto out;
  1009. }
  1010. for (p = mlock_dereference(mp->ports, br);
  1011. p != NULL;
  1012. p = mlock_dereference(p->next, br)) {
  1013. if (p->port != port)
  1014. continue;
  1015. if (!hlist_unhashed(&p->mglist) &&
  1016. (timer_pending(&p->timer) ?
  1017. time_after(p->timer.expires, time) :
  1018. try_to_del_timer_sync(&p->timer) >= 0)) {
  1019. mod_timer(&p->timer, time);
  1020. }
  1021. break;
  1022. }
  1023. out:
  1024. spin_unlock(&br->multicast_lock);
  1025. }
  1026. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  1027. struct net_bridge_port *port,
  1028. __be32 group)
  1029. {
  1030. struct br_ip br_group;
  1031. if (ipv4_is_local_multicast(group))
  1032. return;
  1033. br_group.u.ip4 = group;
  1034. br_group.proto = htons(ETH_P_IP);
  1035. br_multicast_leave_group(br, port, &br_group);
  1036. }
  1037. #if IS_ENABLED(CONFIG_IPV6)
  1038. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  1039. struct net_bridge_port *port,
  1040. const struct in6_addr *group)
  1041. {
  1042. struct br_ip br_group;
  1043. if (!ipv6_is_transient_multicast(group))
  1044. return;
  1045. br_group.u.ip6 = *group;
  1046. br_group.proto = htons(ETH_P_IPV6);
  1047. br_multicast_leave_group(br, port, &br_group);
  1048. }
  1049. #endif
  1050. static int br_multicast_ipv4_rcv(struct net_bridge *br,
  1051. struct net_bridge_port *port,
  1052. struct sk_buff *skb)
  1053. {
  1054. struct sk_buff *skb2 = skb;
  1055. const struct iphdr *iph;
  1056. struct igmphdr *ih;
  1057. unsigned len;
  1058. unsigned offset;
  1059. int err;
  1060. /* We treat OOM as packet loss for now. */
  1061. if (!pskb_may_pull(skb, sizeof(*iph)))
  1062. return -EINVAL;
  1063. iph = ip_hdr(skb);
  1064. if (iph->ihl < 5 || iph->version != 4)
  1065. return -EINVAL;
  1066. if (!pskb_may_pull(skb, ip_hdrlen(skb)))
  1067. return -EINVAL;
  1068. iph = ip_hdr(skb);
  1069. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  1070. return -EINVAL;
  1071. if (iph->protocol != IPPROTO_IGMP) {
  1072. if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP)
  1073. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1074. return 0;
  1075. }
  1076. len = ntohs(iph->tot_len);
  1077. if (skb->len < len || len < ip_hdrlen(skb))
  1078. return -EINVAL;
  1079. if (skb->len > len) {
  1080. skb2 = skb_clone(skb, GFP_ATOMIC);
  1081. if (!skb2)
  1082. return -ENOMEM;
  1083. err = pskb_trim_rcsum(skb2, len);
  1084. if (err)
  1085. goto err_out;
  1086. }
  1087. len -= ip_hdrlen(skb2);
  1088. offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
  1089. __skb_pull(skb2, offset);
  1090. skb_reset_transport_header(skb2);
  1091. err = -EINVAL;
  1092. if (!pskb_may_pull(skb2, sizeof(*ih)))
  1093. goto out;
  1094. switch (skb2->ip_summed) {
  1095. case CHECKSUM_COMPLETE:
  1096. if (!csum_fold(skb2->csum))
  1097. break;
  1098. /* fall through */
  1099. case CHECKSUM_NONE:
  1100. skb2->csum = 0;
  1101. if (skb_checksum_complete(skb2))
  1102. goto out;
  1103. }
  1104. err = 0;
  1105. BR_INPUT_SKB_CB(skb)->igmp = 1;
  1106. ih = igmp_hdr(skb2);
  1107. switch (ih->type) {
  1108. case IGMP_HOST_MEMBERSHIP_REPORT:
  1109. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1110. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1111. err = br_ip4_multicast_add_group(br, port, ih->group);
  1112. break;
  1113. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1114. err = br_ip4_multicast_igmp3_report(br, port, skb2);
  1115. break;
  1116. case IGMP_HOST_MEMBERSHIP_QUERY:
  1117. err = br_ip4_multicast_query(br, port, skb2);
  1118. break;
  1119. case IGMP_HOST_LEAVE_MESSAGE:
  1120. br_ip4_multicast_leave_group(br, port, ih->group);
  1121. break;
  1122. }
  1123. out:
  1124. __skb_push(skb2, offset);
  1125. err_out:
  1126. if (skb2 != skb)
  1127. kfree_skb(skb2);
  1128. return err;
  1129. }
  1130. #if IS_ENABLED(CONFIG_IPV6)
  1131. static int br_multicast_ipv6_rcv(struct net_bridge *br,
  1132. struct net_bridge_port *port,
  1133. struct sk_buff *skb)
  1134. {
  1135. struct sk_buff *skb2;
  1136. const struct ipv6hdr *ip6h;
  1137. u8 icmp6_type;
  1138. u8 nexthdr;
  1139. __be16 frag_off;
  1140. unsigned len;
  1141. int offset;
  1142. int err;
  1143. if (!pskb_may_pull(skb, sizeof(*ip6h)))
  1144. return -EINVAL;
  1145. ip6h = ipv6_hdr(skb);
  1146. /*
  1147. * We're interested in MLD messages only.
  1148. * - Version is 6
  1149. * - MLD has always Router Alert hop-by-hop option
  1150. * - But we do not support jumbrograms.
  1151. */
  1152. if (ip6h->version != 6 ||
  1153. ip6h->nexthdr != IPPROTO_HOPOPTS ||
  1154. ip6h->payload_len == 0)
  1155. return 0;
  1156. len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
  1157. if (skb->len < len)
  1158. return -EINVAL;
  1159. nexthdr = ip6h->nexthdr;
  1160. offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
  1161. if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
  1162. return 0;
  1163. /* Okay, we found ICMPv6 header */
  1164. skb2 = skb_clone(skb, GFP_ATOMIC);
  1165. if (!skb2)
  1166. return -ENOMEM;
  1167. err = -EINVAL;
  1168. if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
  1169. goto out;
  1170. len -= offset - skb_network_offset(skb2);
  1171. __skb_pull(skb2, offset);
  1172. skb_reset_transport_header(skb2);
  1173. skb_postpull_rcsum(skb2, skb_network_header(skb2),
  1174. skb_network_header_len(skb2));
  1175. icmp6_type = icmp6_hdr(skb2)->icmp6_type;
  1176. switch (icmp6_type) {
  1177. case ICMPV6_MGM_QUERY:
  1178. case ICMPV6_MGM_REPORT:
  1179. case ICMPV6_MGM_REDUCTION:
  1180. case ICMPV6_MLD2_REPORT:
  1181. break;
  1182. default:
  1183. err = 0;
  1184. goto out;
  1185. }
  1186. /* Okay, we found MLD message. Check further. */
  1187. if (skb2->len > len) {
  1188. err = pskb_trim_rcsum(skb2, len);
  1189. if (err)
  1190. goto out;
  1191. err = -EINVAL;
  1192. }
  1193. ip6h = ipv6_hdr(skb2);
  1194. switch (skb2->ip_summed) {
  1195. case CHECKSUM_COMPLETE:
  1196. if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
  1197. IPPROTO_ICMPV6, skb2->csum))
  1198. break;
  1199. /*FALLTHROUGH*/
  1200. case CHECKSUM_NONE:
  1201. skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
  1202. &ip6h->daddr,
  1203. skb2->len,
  1204. IPPROTO_ICMPV6, 0));
  1205. if (__skb_checksum_complete(skb2))
  1206. goto out;
  1207. }
  1208. err = 0;
  1209. BR_INPUT_SKB_CB(skb)->igmp = 1;
  1210. switch (icmp6_type) {
  1211. case ICMPV6_MGM_REPORT:
  1212. {
  1213. struct mld_msg *mld;
  1214. if (!pskb_may_pull(skb2, sizeof(*mld))) {
  1215. err = -EINVAL;
  1216. goto out;
  1217. }
  1218. mld = (struct mld_msg *)skb_transport_header(skb2);
  1219. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  1220. err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
  1221. break;
  1222. }
  1223. case ICMPV6_MLD2_REPORT:
  1224. err = br_ip6_multicast_mld2_report(br, port, skb2);
  1225. break;
  1226. case ICMPV6_MGM_QUERY:
  1227. err = br_ip6_multicast_query(br, port, skb2);
  1228. break;
  1229. case ICMPV6_MGM_REDUCTION:
  1230. {
  1231. struct mld_msg *mld;
  1232. if (!pskb_may_pull(skb2, sizeof(*mld))) {
  1233. err = -EINVAL;
  1234. goto out;
  1235. }
  1236. mld = (struct mld_msg *)skb_transport_header(skb2);
  1237. br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
  1238. }
  1239. }
  1240. out:
  1241. kfree_skb(skb2);
  1242. return err;
  1243. }
  1244. #endif
  1245. int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
  1246. struct sk_buff *skb)
  1247. {
  1248. BR_INPUT_SKB_CB(skb)->igmp = 0;
  1249. BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
  1250. if (br->multicast_disabled)
  1251. return 0;
  1252. switch (skb->protocol) {
  1253. case htons(ETH_P_IP):
  1254. return br_multicast_ipv4_rcv(br, port, skb);
  1255. #if IS_ENABLED(CONFIG_IPV6)
  1256. case htons(ETH_P_IPV6):
  1257. return br_multicast_ipv6_rcv(br, port, skb);
  1258. #endif
  1259. }
  1260. return 0;
  1261. }
  1262. static void br_multicast_query_expired(unsigned long data)
  1263. {
  1264. struct net_bridge *br = (void *)data;
  1265. spin_lock(&br->multicast_lock);
  1266. if (br->multicast_startup_queries_sent <
  1267. br->multicast_startup_query_count)
  1268. br->multicast_startup_queries_sent++;
  1269. br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
  1270. spin_unlock(&br->multicast_lock);
  1271. }
  1272. void br_multicast_init(struct net_bridge *br)
  1273. {
  1274. br->hash_elasticity = 4;
  1275. br->hash_max = 512;
  1276. br->multicast_router = 1;
  1277. br->multicast_last_member_count = 2;
  1278. br->multicast_startup_query_count = 2;
  1279. br->multicast_last_member_interval = HZ;
  1280. br->multicast_query_response_interval = 10 * HZ;
  1281. br->multicast_startup_query_interval = 125 * HZ / 4;
  1282. br->multicast_query_interval = 125 * HZ;
  1283. br->multicast_querier_interval = 255 * HZ;
  1284. br->multicast_membership_interval = 260 * HZ;
  1285. spin_lock_init(&br->multicast_lock);
  1286. setup_timer(&br->multicast_router_timer,
  1287. br_multicast_local_router_expired, 0);
  1288. setup_timer(&br->multicast_querier_timer,
  1289. br_multicast_local_router_expired, 0);
  1290. setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
  1291. (unsigned long)br);
  1292. }
  1293. void br_multicast_open(struct net_bridge *br)
  1294. {
  1295. br->multicast_startup_queries_sent = 0;
  1296. if (br->multicast_disabled)
  1297. return;
  1298. mod_timer(&br->multicast_query_timer, jiffies);
  1299. }
  1300. void br_multicast_stop(struct net_bridge *br)
  1301. {
  1302. struct net_bridge_mdb_htable *mdb;
  1303. struct net_bridge_mdb_entry *mp;
  1304. struct hlist_node *p, *n;
  1305. u32 ver;
  1306. int i;
  1307. del_timer_sync(&br->multicast_router_timer);
  1308. del_timer_sync(&br->multicast_querier_timer);
  1309. del_timer_sync(&br->multicast_query_timer);
  1310. spin_lock_bh(&br->multicast_lock);
  1311. mdb = mlock_dereference(br->mdb, br);
  1312. if (!mdb)
  1313. goto out;
  1314. br->mdb = NULL;
  1315. ver = mdb->ver;
  1316. for (i = 0; i < mdb->max; i++) {
  1317. hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
  1318. hlist[ver]) {
  1319. del_timer(&mp->timer);
  1320. call_rcu_bh(&mp->rcu, br_multicast_free_group);
  1321. }
  1322. }
  1323. if (mdb->old) {
  1324. spin_unlock_bh(&br->multicast_lock);
  1325. rcu_barrier_bh();
  1326. spin_lock_bh(&br->multicast_lock);
  1327. WARN_ON(mdb->old);
  1328. }
  1329. mdb->old = mdb;
  1330. call_rcu_bh(&mdb->rcu, br_mdb_free);
  1331. out:
  1332. spin_unlock_bh(&br->multicast_lock);
  1333. }
  1334. int br_multicast_set_router(struct net_bridge *br, unsigned long val)
  1335. {
  1336. int err = -ENOENT;
  1337. spin_lock_bh(&br->multicast_lock);
  1338. if (!netif_running(br->dev))
  1339. goto unlock;
  1340. switch (val) {
  1341. case 0:
  1342. case 2:
  1343. del_timer(&br->multicast_router_timer);
  1344. /* fall through */
  1345. case 1:
  1346. br->multicast_router = val;
  1347. err = 0;
  1348. break;
  1349. default:
  1350. err = -EINVAL;
  1351. break;
  1352. }
  1353. unlock:
  1354. spin_unlock_bh(&br->multicast_lock);
  1355. return err;
  1356. }
  1357. int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
  1358. {
  1359. struct net_bridge *br = p->br;
  1360. int err = -ENOENT;
  1361. spin_lock(&br->multicast_lock);
  1362. if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
  1363. goto unlock;
  1364. switch (val) {
  1365. case 0:
  1366. case 1:
  1367. case 2:
  1368. p->multicast_router = val;
  1369. err = 0;
  1370. if (val < 2 && !hlist_unhashed(&p->rlist))
  1371. hlist_del_init_rcu(&p->rlist);
  1372. if (val == 1)
  1373. break;
  1374. del_timer(&p->multicast_router_timer);
  1375. if (val == 0)
  1376. break;
  1377. br_multicast_add_router(br, p);
  1378. break;
  1379. default:
  1380. err = -EINVAL;
  1381. break;
  1382. }
  1383. unlock:
  1384. spin_unlock(&br->multicast_lock);
  1385. return err;
  1386. }
  1387. int br_multicast_toggle(struct net_bridge *br, unsigned long val)
  1388. {
  1389. struct net_bridge_port *port;
  1390. int err = 0;
  1391. struct net_bridge_mdb_htable *mdb;
  1392. spin_lock_bh(&br->multicast_lock);
  1393. if (br->multicast_disabled == !val)
  1394. goto unlock;
  1395. br->multicast_disabled = !val;
  1396. if (br->multicast_disabled)
  1397. goto unlock;
  1398. if (!netif_running(br->dev))
  1399. goto unlock;
  1400. mdb = mlock_dereference(br->mdb, br);
  1401. if (mdb) {
  1402. if (mdb->old) {
  1403. err = -EEXIST;
  1404. rollback:
  1405. br->multicast_disabled = !!val;
  1406. goto unlock;
  1407. }
  1408. err = br_mdb_rehash(&br->mdb, mdb->max,
  1409. br->hash_elasticity);
  1410. if (err)
  1411. goto rollback;
  1412. }
  1413. br_multicast_open(br);
  1414. list_for_each_entry(port, &br->port_list, list) {
  1415. if (port->state == BR_STATE_DISABLED ||
  1416. port->state == BR_STATE_BLOCKING)
  1417. continue;
  1418. __br_multicast_enable_port(port);
  1419. }
  1420. unlock:
  1421. spin_unlock_bh(&br->multicast_lock);
  1422. return err;
  1423. }
  1424. int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
  1425. {
  1426. int err = -ENOENT;
  1427. u32 old;
  1428. struct net_bridge_mdb_htable *mdb;
  1429. spin_lock_bh(&br->multicast_lock);
  1430. if (!netif_running(br->dev))
  1431. goto unlock;
  1432. err = -EINVAL;
  1433. if (!is_power_of_2(val))
  1434. goto unlock;
  1435. mdb = mlock_dereference(br->mdb, br);
  1436. if (mdb && val < mdb->size)
  1437. goto unlock;
  1438. err = 0;
  1439. old = br->hash_max;
  1440. br->hash_max = val;
  1441. if (mdb) {
  1442. if (mdb->old) {
  1443. err = -EEXIST;
  1444. rollback:
  1445. br->hash_max = old;
  1446. goto unlock;
  1447. }
  1448. err = br_mdb_rehash(&br->mdb, br->hash_max,
  1449. br->hash_elasticity);
  1450. if (err)
  1451. goto rollback;
  1452. }
  1453. unlock:
  1454. spin_unlock_bh(&br->multicast_lock);
  1455. return err;
  1456. }