1
0

645-bridge_multicast_to_unicast.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: [PATCH] bridge: multicast to unicast
  3. Implement optinal multicast->unicast conversion for igmp snooping
  4. ---
  5. --- a/include/linux/if_bridge.h
  6. +++ b/include/linux/if_bridge.h
  7. @@ -46,6 +46,7 @@ struct br_ip_list {
  8. #define BR_LEARNING_SYNC BIT(9)
  9. #define BR_PROXYARP_WIFI BIT(10)
  10. #define BR_ISOLATE_MODE BIT(11)
  11. +#define BR_MULTICAST_TO_UCAST BIT(12)
  12. #define BR_DEFAULT_AGEING_TIME (300 * HZ)
  13. --- a/net/bridge/br_multicast.c
  14. +++ b/net/bridge/br_multicast.c
  15. @@ -42,12 +42,13 @@ static void br_multicast_add_router(stru
  16. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  17. struct net_bridge_port *port,
  18. __be32 group,
  19. - __u16 vid);
  20. + __u16 vid,
  21. + const unsigned char *src);
  22. #if IS_ENABLED(CONFIG_IPV6)
  23. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  24. struct net_bridge_port *port,
  25. const struct in6_addr *group,
  26. - __u16 vid);
  27. + __u16 vid, const unsigned char *src);
  28. #endif
  29. unsigned int br_mdb_rehash_seq;
  30. @@ -652,7 +653,8 @@ struct net_bridge_port_group *br_multica
  31. struct net_bridge_port *port,
  32. struct br_ip *group,
  33. struct net_bridge_port_group __rcu *next,
  34. - unsigned char state)
  35. + unsigned char state,
  36. + const unsigned char *src)
  37. {
  38. struct net_bridge_port_group *p;
  39. @@ -667,12 +669,33 @@ struct net_bridge_port_group *br_multica
  40. hlist_add_head(&p->mglist, &port->mglist);
  41. setup_timer(&p->timer, br_multicast_port_group_expired,
  42. (unsigned long)p);
  43. + if ((port->flags & BR_MULTICAST_TO_UCAST) && src) {
  44. + memcpy(p->eth_addr, src, ETH_ALEN);
  45. + p->unicast = true;
  46. + }
  47. return p;
  48. }
  49. +static bool br_port_group_equal(struct net_bridge_port_group *p,
  50. + struct net_bridge_port *port,
  51. + const unsigned char *src)
  52. +{
  53. + if (p->port != port)
  54. + return false;
  55. +
  56. + if (!p->unicast)
  57. + return true;
  58. +
  59. + if (!src)
  60. + return false;
  61. +
  62. + return ether_addr_equal(src, p->eth_addr);
  63. +}
  64. +
  65. static int br_multicast_add_group(struct net_bridge *br,
  66. struct net_bridge_port *port,
  67. - struct br_ip *group)
  68. + struct br_ip *group,
  69. + const unsigned char *src)
  70. {
  71. struct net_bridge_mdb_entry *mp;
  72. struct net_bridge_port_group *p;
  73. @@ -699,13 +722,13 @@ static int br_multicast_add_group(struct
  74. for (pp = &mp->ports;
  75. (p = mlock_dereference(*pp, br)) != NULL;
  76. pp = &p->next) {
  77. - if (p->port == port)
  78. + if (br_port_group_equal(p, port, src))
  79. goto found;
  80. if ((unsigned long)p->port < (unsigned long)port)
  81. break;
  82. }
  83. - p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
  84. + p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY, src);
  85. if (unlikely(!p))
  86. goto err;
  87. rcu_assign_pointer(*pp, p);
  88. @@ -724,7 +747,7 @@ err:
  89. static int br_ip4_multicast_add_group(struct net_bridge *br,
  90. struct net_bridge_port *port,
  91. __be32 group,
  92. - __u16 vid)
  93. + __u16 vid, const unsigned char *src)
  94. {
  95. struct br_ip br_group;
  96. @@ -735,14 +758,14 @@ static int br_ip4_multicast_add_group(st
  97. br_group.proto = htons(ETH_P_IP);
  98. br_group.vid = vid;
  99. - return br_multicast_add_group(br, port, &br_group);
  100. + return br_multicast_add_group(br, port, &br_group, src);
  101. }
  102. #if IS_ENABLED(CONFIG_IPV6)
  103. static int br_ip6_multicast_add_group(struct net_bridge *br,
  104. struct net_bridge_port *port,
  105. const struct in6_addr *group,
  106. - __u16 vid)
  107. + __u16 vid, const unsigned char *src)
  108. {
  109. struct br_ip br_group;
  110. @@ -753,7 +776,7 @@ static int br_ip6_multicast_add_group(st
  111. br_group.proto = htons(ETH_P_IPV6);
  112. br_group.vid = vid;
  113. - return br_multicast_add_group(br, port, &br_group);
  114. + return br_multicast_add_group(br, port, &br_group, src);
  115. }
  116. #endif
  117. @@ -1003,6 +1026,7 @@ static int br_ip4_multicast_igmp3_report
  118. struct sk_buff *skb,
  119. u16 vid)
  120. {
  121. + const unsigned char *src;
  122. struct igmpv3_report *ih;
  123. struct igmpv3_grec *grec;
  124. int i;
  125. @@ -1043,12 +1067,13 @@ static int br_ip4_multicast_igmp3_report
  126. continue;
  127. }
  128. + src = eth_hdr(skb)->h_source;
  129. if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
  130. type == IGMPV3_MODE_IS_INCLUDE) &&
  131. ntohs(grec->grec_nsrcs) == 0) {
  132. - br_ip4_multicast_leave_group(br, port, group, vid);
  133. + br_ip4_multicast_leave_group(br, port, group, vid, src);
  134. } else {
  135. - err = br_ip4_multicast_add_group(br, port, group, vid);
  136. + err = br_ip4_multicast_add_group(br, port, group, vid, src);
  137. if (err)
  138. break;
  139. }
  140. @@ -1063,6 +1088,7 @@ static int br_ip6_multicast_mld2_report(
  141. struct sk_buff *skb,
  142. u16 vid)
  143. {
  144. + const unsigned char *src = eth_hdr(skb)->h_source;
  145. struct icmp6hdr *icmp6h;
  146. struct mld2_grec *grec;
  147. int i;
  148. @@ -1114,10 +1140,10 @@ static int br_ip6_multicast_mld2_report(
  149. grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
  150. ntohs(*nsrcs) == 0) {
  151. br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
  152. - vid);
  153. + vid, src);
  154. } else {
  155. err = br_ip6_multicast_add_group(br, port,
  156. - &grec->grec_mca, vid);
  157. + &grec->grec_mca, vid, src);
  158. if (err)
  159. break;
  160. }
  161. @@ -1432,7 +1458,8 @@ br_multicast_leave_group(struct net_brid
  162. struct net_bridge_port *port,
  163. struct br_ip *group,
  164. struct bridge_mcast_other_query *other_query,
  165. - struct bridge_mcast_own_query *own_query)
  166. + struct bridge_mcast_own_query *own_query,
  167. + const unsigned char *src)
  168. {
  169. struct net_bridge_mdb_htable *mdb;
  170. struct net_bridge_mdb_entry *mp;
  171. @@ -1456,7 +1483,7 @@ br_multicast_leave_group(struct net_brid
  172. for (pp = &mp->ports;
  173. (p = mlock_dereference(*pp, br)) != NULL;
  174. pp = &p->next) {
  175. - if (p->port != port)
  176. + if (!br_port_group_equal(p, port, src))
  177. continue;
  178. rcu_assign_pointer(*pp, p->next);
  179. @@ -1519,7 +1546,7 @@ br_multicast_leave_group(struct net_brid
  180. for (p = mlock_dereference(mp->ports, br);
  181. p != NULL;
  182. p = mlock_dereference(p->next, br)) {
  183. - if (p->port != port)
  184. + if (!br_port_group_equal(p, port, src))
  185. continue;
  186. if (!hlist_unhashed(&p->mglist) &&
  187. @@ -1537,8 +1564,8 @@ out:
  188. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  189. struct net_bridge_port *port,
  190. - __be32 group,
  191. - __u16 vid)
  192. + __be32 group, __u16 vid,
  193. + const unsigned char *src)
  194. {
  195. struct br_ip br_group;
  196. struct bridge_mcast_own_query *own_query;
  197. @@ -1553,14 +1580,14 @@ static void br_ip4_multicast_leave_group
  198. br_group.vid = vid;
  199. br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
  200. - own_query);
  201. + own_query, src);
  202. }
  203. #if IS_ENABLED(CONFIG_IPV6)
  204. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  205. struct net_bridge_port *port,
  206. const struct in6_addr *group,
  207. - __u16 vid)
  208. + __u16 vid, const unsigned char *src)
  209. {
  210. struct br_ip br_group;
  211. struct bridge_mcast_own_query *own_query;
  212. @@ -1575,7 +1602,7 @@ static void br_ip6_multicast_leave_group
  213. br_group.vid = vid;
  214. br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
  215. - own_query);
  216. + own_query, src);
  217. }
  218. #endif
  219. @@ -1584,6 +1611,7 @@ static int br_multicast_ipv4_rcv(struct
  220. struct sk_buff *skb,
  221. u16 vid)
  222. {
  223. + const unsigned char *src;
  224. struct sk_buff *skb_trimmed = NULL;
  225. struct igmphdr *ih;
  226. int err;
  227. @@ -1600,12 +1628,13 @@ static int br_multicast_ipv4_rcv(struct
  228. BR_INPUT_SKB_CB(skb)->igmp = 1;
  229. ih = igmp_hdr(skb);
  230. + src = eth_hdr(skb)->h_source;
  231. switch (ih->type) {
  232. case IGMP_HOST_MEMBERSHIP_REPORT:
  233. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  234. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  235. - err = br_ip4_multicast_add_group(br, port, ih->group, vid);
  236. + err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
  237. break;
  238. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  239. err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
  240. @@ -1614,7 +1643,7 @@ static int br_multicast_ipv4_rcv(struct
  241. err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
  242. break;
  243. case IGMP_HOST_LEAVE_MESSAGE:
  244. - br_ip4_multicast_leave_group(br, port, ih->group, vid);
  245. + br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
  246. break;
  247. }
  248. @@ -1630,6 +1659,7 @@ static int br_multicast_ipv6_rcv(struct
  249. struct sk_buff *skb,
  250. u16 vid)
  251. {
  252. + const unsigned char *src;
  253. struct sk_buff *skb_trimmed = NULL;
  254. struct mld_msg *mld;
  255. int err;
  256. @@ -1649,8 +1679,9 @@ static int br_multicast_ipv6_rcv(struct
  257. switch (mld->mld_type) {
  258. case ICMPV6_MGM_REPORT:
  259. + src = eth_hdr(skb)->h_source;
  260. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  261. - err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
  262. + err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid, src);
  263. break;
  264. case ICMPV6_MLD2_REPORT:
  265. err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
  266. @@ -1659,7 +1690,8 @@ static int br_multicast_ipv6_rcv(struct
  267. err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
  268. break;
  269. case ICMPV6_MGM_REDUCTION:
  270. - br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
  271. + src = eth_hdr(skb)->h_source;
  272. + br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
  273. break;
  274. }
  275. --- a/net/bridge/br_private.h
  276. +++ b/net/bridge/br_private.h
  277. @@ -157,7 +157,9 @@ struct net_bridge_port_group {
  278. struct rcu_head rcu;
  279. struct timer_list timer;
  280. struct br_ip addr;
  281. + unsigned char eth_addr[ETH_ALEN];
  282. unsigned char state;
  283. + bool unicast;
  284. };
  285. struct net_bridge_mdb_entry
  286. @@ -555,7 +557,8 @@ void br_multicast_free_pg(struct rcu_hea
  287. struct net_bridge_port_group *
  288. br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
  289. struct net_bridge_port_group __rcu *next,
  290. - unsigned char state);
  291. + unsigned char state,
  292. + const unsigned char *src);
  293. void br_mdb_init(void);
  294. void br_mdb_uninit(void);
  295. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  296. --- a/net/bridge/br_mdb.c
  297. +++ b/net/bridge/br_mdb.c
  298. @@ -416,7 +416,7 @@ static int br_mdb_add_group(struct net_b
  299. break;
  300. }
  301. - p = br_multicast_new_port_group(port, group, *pp, state);
  302. + p = br_multicast_new_port_group(port, group, *pp, state, NULL);
  303. if (unlikely(!p))
  304. return -ENOMEM;
  305. rcu_assign_pointer(*pp, p);
  306. --- a/net/bridge/br_forward.c
  307. +++ b/net/bridge/br_forward.c
  308. @@ -192,6 +192,34 @@ out:
  309. return p;
  310. }
  311. +static struct net_bridge_port *maybe_deliver_addr(
  312. + struct net_bridge_port *prev, struct net_bridge_port *p,
  313. + struct sk_buff *skb, const unsigned char *addr,
  314. + void (*__packet_hook)(const struct net_bridge_port *p,
  315. + struct sk_buff *skb))
  316. +{
  317. + struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  318. + const unsigned char *src = eth_hdr(skb)->h_source;
  319. +
  320. + if (!should_deliver(p, skb))
  321. + return prev;
  322. +
  323. + /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
  324. + if (skb->dev == p->dev && ether_addr_equal(src, addr))
  325. + return prev;
  326. +
  327. + skb = skb_copy(skb, GFP_ATOMIC);
  328. + if (!skb) {
  329. + dev->stats.tx_dropped++;
  330. + return prev;
  331. + }
  332. +
  333. + memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
  334. + __packet_hook(p, skb);
  335. +
  336. + return prev;
  337. +}
  338. +
  339. /* called under bridge lock */
  340. static void br_flood(struct net_bridge *br, struct sk_buff *skb,
  341. struct sk_buff *skb0,
  342. @@ -264,6 +292,7 @@ static void br_multicast_flood(struct ne
  343. struct net_bridge_port *prev = NULL;
  344. struct net_bridge_port_group *p;
  345. struct hlist_node *rp;
  346. + const unsigned char *addr;
  347. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  348. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  349. @@ -274,10 +303,19 @@ static void br_multicast_flood(struct ne
  350. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  351. NULL;
  352. - port = (unsigned long)lport > (unsigned long)rport ?
  353. - lport : rport;
  354. -
  355. - prev = maybe_deliver(prev, port, skb, __packet_hook);
  356. + if ((unsigned long)lport > (unsigned long)rport) {
  357. + port = lport;
  358. + addr = p->unicast ? p->eth_addr : NULL;
  359. + } else {
  360. + port = rport;
  361. + addr = NULL;
  362. + }
  363. +
  364. + if (addr)
  365. + prev = maybe_deliver_addr(prev, port, skb, addr,
  366. + __packet_hook);
  367. + else
  368. + prev = maybe_deliver(prev, port, skb, __packet_hook);
  369. if (IS_ERR(prev))
  370. goto out;
  371. --- a/net/bridge/br_sysfs_if.c
  372. +++ b/net/bridge/br_sysfs_if.c
  373. @@ -204,6 +204,7 @@ static BRPORT_ATTR(multicast_router, S_I
  374. store_multicast_router);
  375. BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
  376. +BRPORT_ATTR_FLAG(multicast_to_unicast, BR_MULTICAST_TO_UCAST);
  377. #endif
  378. static const struct brport_attribute *brport_attrs[] = {
  379. @@ -230,6 +231,7 @@ static const struct brport_attribute *br
  380. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  381. &brport_attr_multicast_router,
  382. &brport_attr_multicast_fast_leave,
  383. + &brport_attr_multicast_to_unicast,
  384. #endif
  385. &brport_attr_proxyarp,
  386. &brport_attr_proxyarp_wifi,