645-bridge_multicast_to_unicast.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. extern void brioctl_set(int (*ioctl_hook)(struct net *, unsigned int, void __user *));
  13. --- a/net/bridge/br_multicast.c
  14. +++ b/net/bridge/br_multicast.c
  15. @@ -635,7 +635,8 @@ struct net_bridge_port_group *br_multica
  16. struct net_bridge_port *port,
  17. struct br_ip *group,
  18. struct net_bridge_port_group __rcu *next,
  19. - unsigned char state)
  20. + unsigned char state,
  21. + const unsigned char *src)
  22. {
  23. struct net_bridge_port_group *p;
  24. @@ -650,12 +651,33 @@ struct net_bridge_port_group *br_multica
  25. hlist_add_head(&p->mglist, &port->mglist);
  26. setup_timer(&p->timer, br_multicast_port_group_expired,
  27. (unsigned long)p);
  28. + if ((port->flags & BR_MULTICAST_TO_UCAST) && src) {
  29. + memcpy(p->eth_addr, src, ETH_ALEN);
  30. + p->unicast = true;
  31. + }
  32. return p;
  33. }
  34. +static bool br_port_group_equal(struct net_bridge_port_group *p,
  35. + struct net_bridge_port *port,
  36. + const unsigned char *src)
  37. +{
  38. + if (p->port != port)
  39. + return false;
  40. +
  41. + if (!p->unicast)
  42. + return true;
  43. +
  44. + if (!src)
  45. + return false;
  46. +
  47. + return ether_addr_equal(src, p->eth_addr);
  48. +}
  49. +
  50. static int br_multicast_add_group(struct net_bridge *br,
  51. struct net_bridge_port *port,
  52. - struct br_ip *group)
  53. + struct br_ip *group,
  54. + const unsigned char *src)
  55. {
  56. struct net_bridge_mdb_entry *mp;
  57. struct net_bridge_port_group *p;
  58. @@ -682,13 +704,13 @@ static int br_multicast_add_group(struct
  59. for (pp = &mp->ports;
  60. (p = mlock_dereference(*pp, br)) != NULL;
  61. pp = &p->next) {
  62. - if (p->port == port)
  63. + if (br_port_group_equal(p, port, src))
  64. goto found;
  65. if ((unsigned long)p->port < (unsigned long)port)
  66. break;
  67. }
  68. - p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
  69. + p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY, src);
  70. if (unlikely(!p))
  71. goto err;
  72. rcu_assign_pointer(*pp, p);
  73. @@ -707,7 +729,7 @@ err:
  74. static int br_ip4_multicast_add_group(struct net_bridge *br,
  75. struct net_bridge_port *port,
  76. __be32 group,
  77. - __u16 vid)
  78. + __u16 vid, const unsigned char *src)
  79. {
  80. struct br_ip br_group;
  81. @@ -718,14 +740,14 @@ static int br_ip4_multicast_add_group(st
  82. br_group.proto = htons(ETH_P_IP);
  83. br_group.vid = vid;
  84. - return br_multicast_add_group(br, port, &br_group);
  85. + return br_multicast_add_group(br, port, &br_group, src);
  86. }
  87. #if IS_ENABLED(CONFIG_IPV6)
  88. static int br_ip6_multicast_add_group(struct net_bridge *br,
  89. struct net_bridge_port *port,
  90. const struct in6_addr *group,
  91. - __u16 vid)
  92. + __u16 vid, const unsigned char *src)
  93. {
  94. struct br_ip br_group;
  95. @@ -736,7 +758,7 @@ static int br_ip6_multicast_add_group(st
  96. br_group.proto = htons(ETH_P_IPV6);
  97. br_group.vid = vid;
  98. - return br_multicast_add_group(br, port, &br_group);
  99. + return br_multicast_add_group(br, port, &br_group, src);
  100. }
  101. #endif
  102. @@ -966,6 +988,7 @@ static int br_ip4_multicast_igmp3_report
  103. struct sk_buff *skb,
  104. u16 vid)
  105. {
  106. + const unsigned char *src;
  107. struct igmpv3_report *ih;
  108. struct igmpv3_grec *grec;
  109. int i;
  110. @@ -1009,7 +1032,8 @@ static int br_ip4_multicast_igmp3_report
  111. continue;
  112. }
  113. - err = br_ip4_multicast_add_group(br, port, group, vid);
  114. + src = eth_hdr(skb)->h_source;
  115. + err = br_ip4_multicast_add_group(br, port, group, vid, src);
  116. if (err)
  117. break;
  118. }
  119. @@ -1023,6 +1047,7 @@ static int br_ip6_multicast_mld2_report(
  120. struct sk_buff *skb,
  121. u16 vid)
  122. {
  123. + const unsigned char *src = eth_hdr(skb)->h_source;
  124. struct icmp6hdr *icmp6h;
  125. struct mld2_grec *grec;
  126. int i;
  127. @@ -1071,7 +1096,7 @@ static int br_ip6_multicast_mld2_report(
  128. }
  129. err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
  130. - vid);
  131. + vid, src);
  132. if (err)
  133. break;
  134. }
  135. @@ -1407,7 +1432,8 @@ br_multicast_leave_group(struct net_brid
  136. struct net_bridge_port *port,
  137. struct br_ip *group,
  138. struct bridge_mcast_other_query *other_query,
  139. - struct bridge_mcast_own_query *own_query)
  140. + struct bridge_mcast_own_query *own_query,
  141. + const unsigned char *src)
  142. {
  143. struct net_bridge_mdb_htable *mdb;
  144. struct net_bridge_mdb_entry *mp;
  145. @@ -1457,7 +1483,7 @@ br_multicast_leave_group(struct net_brid
  146. for (pp = &mp->ports;
  147. (p = mlock_dereference(*pp, br)) != NULL;
  148. pp = &p->next) {
  149. - if (p->port != port)
  150. + if (!br_port_group_equal(p, port, src))
  151. continue;
  152. rcu_assign_pointer(*pp, p->next);
  153. @@ -1491,7 +1517,7 @@ br_multicast_leave_group(struct net_brid
  154. for (p = mlock_dereference(mp->ports, br);
  155. p != NULL;
  156. p = mlock_dereference(p->next, br)) {
  157. - if (p->port != port)
  158. + if (!br_port_group_equal(p, port, src))
  159. continue;
  160. if (!hlist_unhashed(&p->mglist) &&
  161. @@ -1509,8 +1535,8 @@ out:
  162. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  163. struct net_bridge_port *port,
  164. - __be32 group,
  165. - __u16 vid)
  166. + __be32 group, __u16 vid,
  167. + const unsigned char *src)
  168. {
  169. struct br_ip br_group;
  170. struct bridge_mcast_own_query *own_query;
  171. @@ -1525,14 +1551,14 @@ static void br_ip4_multicast_leave_group
  172. br_group.vid = vid;
  173. br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
  174. - own_query);
  175. + own_query, src);
  176. }
  177. #if IS_ENABLED(CONFIG_IPV6)
  178. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  179. struct net_bridge_port *port,
  180. const struct in6_addr *group,
  181. - __u16 vid)
  182. + __u16 vid, const unsigned char *src)
  183. {
  184. struct br_ip br_group;
  185. struct bridge_mcast_own_query *own_query;
  186. @@ -1547,7 +1573,7 @@ static void br_ip6_multicast_leave_group
  187. br_group.vid = vid;
  188. br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
  189. - own_query);
  190. + own_query, src);
  191. }
  192. #endif
  193. @@ -1556,6 +1582,7 @@ static int br_multicast_ipv4_rcv(struct
  194. struct sk_buff *skb,
  195. u16 vid)
  196. {
  197. + const unsigned char *src;
  198. struct sk_buff *skb2 = skb;
  199. const struct iphdr *iph;
  200. struct igmphdr *ih;
  201. @@ -1624,12 +1651,13 @@ static int br_multicast_ipv4_rcv(struct
  202. BR_INPUT_SKB_CB(skb)->igmp = 1;
  203. ih = igmp_hdr(skb2);
  204. + src = eth_hdr(skb)->h_source;
  205. switch (ih->type) {
  206. case IGMP_HOST_MEMBERSHIP_REPORT:
  207. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  208. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  209. - err = br_ip4_multicast_add_group(br, port, ih->group, vid);
  210. + err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
  211. break;
  212. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  213. err = br_ip4_multicast_igmp3_report(br, port, skb2, vid);
  214. @@ -1638,7 +1666,7 @@ static int br_multicast_ipv4_rcv(struct
  215. err = br_ip4_multicast_query(br, port, skb2, vid);
  216. break;
  217. case IGMP_HOST_LEAVE_MESSAGE:
  218. - br_ip4_multicast_leave_group(br, port, ih->group, vid);
  219. + br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
  220. break;
  221. }
  222. @@ -1656,6 +1684,7 @@ static int br_multicast_ipv6_rcv(struct
  223. struct sk_buff *skb,
  224. u16 vid)
  225. {
  226. + const unsigned char *src;
  227. struct sk_buff *skb2;
  228. const struct ipv6hdr *ip6h;
  229. u8 icmp6_type;
  230. @@ -1763,9 +1792,11 @@ static int br_multicast_ipv6_rcv(struct
  231. err = -EINVAL;
  232. goto out;
  233. }
  234. + src = eth_hdr(skb)->h_source;
  235. mld = (struct mld_msg *)skb_transport_header(skb2);
  236. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  237. - err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
  238. + err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
  239. + src);
  240. break;
  241. }
  242. case ICMPV6_MLD2_REPORT:
  243. @@ -1781,8 +1812,9 @@ static int br_multicast_ipv6_rcv(struct
  244. err = -EINVAL;
  245. goto out;
  246. }
  247. + src = eth_hdr(skb)->h_source;
  248. mld = (struct mld_msg *)skb_transport_header(skb2);
  249. - br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
  250. + br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
  251. }
  252. }
  253. --- a/net/bridge/br_private.h
  254. +++ b/net/bridge/br_private.h
  255. @@ -113,6 +113,9 @@ struct net_bridge_port_group {
  256. struct timer_list timer;
  257. struct br_ip addr;
  258. unsigned char state;
  259. +
  260. + unsigned char eth_addr[ETH_ALEN];
  261. + bool unicast;
  262. };
  263. struct net_bridge_mdb_entry
  264. @@ -481,7 +484,8 @@ void br_multicast_free_pg(struct rcu_hea
  265. struct net_bridge_port_group *
  266. br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
  267. struct net_bridge_port_group __rcu *next,
  268. - unsigned char state);
  269. + unsigned char state,
  270. + const unsigned char *src);
  271. void br_mdb_init(void);
  272. void br_mdb_uninit(void);
  273. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  274. --- a/net/bridge/br_mdb.c
  275. +++ b/net/bridge/br_mdb.c
  276. @@ -343,7 +343,7 @@ static int br_mdb_add_group(struct net_b
  277. break;
  278. }
  279. - p = br_multicast_new_port_group(port, group, *pp, state);
  280. + p = br_multicast_new_port_group(port, group, *pp, state, NULL);
  281. if (unlikely(!p))
  282. return -ENOMEM;
  283. rcu_assign_pointer(*pp, p);
  284. --- a/net/bridge/br_forward.c
  285. +++ b/net/bridge/br_forward.c
  286. @@ -170,6 +170,34 @@ out:
  287. return p;
  288. }
  289. +static struct net_bridge_port *maybe_deliver_addr(
  290. + struct net_bridge_port *prev, struct net_bridge_port *p,
  291. + struct sk_buff *skb, const unsigned char *addr,
  292. + void (*__packet_hook)(const struct net_bridge_port *p,
  293. + struct sk_buff *skb))
  294. +{
  295. + struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  296. + const unsigned char *src = eth_hdr(skb)->h_source;
  297. +
  298. + if (!should_deliver(p, skb))
  299. + return prev;
  300. +
  301. + /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
  302. + if (skb->dev == p->dev && ether_addr_equal(src, addr))
  303. + return prev;
  304. +
  305. + skb = skb_copy(skb, GFP_ATOMIC);
  306. + if (!skb) {
  307. + dev->stats.tx_dropped++;
  308. + return prev;
  309. + }
  310. +
  311. + memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
  312. + __packet_hook(p, skb);
  313. +
  314. + return prev;
  315. +}
  316. +
  317. /* called under bridge lock */
  318. static void br_flood(struct net_bridge *br, struct sk_buff *skb,
  319. struct sk_buff *skb0,
  320. @@ -242,6 +270,7 @@ static void br_multicast_flood(struct ne
  321. struct net_bridge_port *prev = NULL;
  322. struct net_bridge_port_group *p;
  323. struct hlist_node *rp;
  324. + const unsigned char *addr;
  325. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  326. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  327. @@ -252,10 +281,19 @@ static void br_multicast_flood(struct ne
  328. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  329. NULL;
  330. - port = (unsigned long)lport > (unsigned long)rport ?
  331. - lport : rport;
  332. -
  333. - prev = maybe_deliver(prev, port, skb, __packet_hook);
  334. + if ((unsigned long)lport > (unsigned long)rport) {
  335. + port = lport;
  336. + addr = p->unicast ? p->eth_addr : NULL;
  337. + } else {
  338. + port = rport;
  339. + addr = NULL;
  340. + }
  341. +
  342. + if (addr)
  343. + prev = maybe_deliver_addr(prev, port, skb, addr,
  344. + __packet_hook);
  345. + else
  346. + prev = maybe_deliver(prev, port, skb, __packet_hook);
  347. if (IS_ERR(prev))
  348. goto out;
  349. --- a/net/bridge/br_sysfs_if.c
  350. +++ b/net/bridge/br_sysfs_if.c
  351. @@ -204,6 +204,7 @@ static BRPORT_ATTR(multicast_router, S_I
  352. store_multicast_router);
  353. BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
  354. +BRPORT_ATTR_FLAG(multicast_to_unicast, BR_MULTICAST_TO_UCAST);
  355. #endif
  356. static const struct brport_attribute *brport_attrs[] = {
  357. @@ -230,6 +231,7 @@ static const struct brport_attribute *br
  358. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  359. &brport_attr_multicast_router,
  360. &brport_attr_multicast_fast_leave,
  361. + &brport_attr_multicast_to_unicast,
  362. #endif
  363. &brport_attr_proxyarp,
  364. &brport_attr_proxyarp_wifi,