2
0

645-bridge_multicast_to_unicast.patch 11 KB

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