021-bridge-multicast-to-unicast.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Date: Thu, 19 Jan 2017 03:45:10 +0100
  3. Subject: [PATCH] bridge: multicast to unicast
  4. MIME-Version: 1.0
  5. Content-Type: text/plain; charset=UTF-8
  6. Content-Transfer-Encoding: 8bit
  7. Implements an optional, per bridge port flag and feature to deliver
  8. multicast packets to any host on the according port via unicast
  9. individually. This is done by copying the packet per host and
  10. changing the multicast destination MAC to a unicast one accordingly.
  11. multicast-to-unicast works on top of the multicast snooping feature of
  12. the bridge. Which means unicast copies are only delivered to hosts which
  13. are interested in it and signalized this via IGMP/MLD reports
  14. previously.
  15. This feature is intended for interface types which have a more reliable
  16. and/or efficient way to deliver unicast packets than broadcast ones
  17. (e.g. wifi).
  18. However, it should only be enabled on interfaces where no IGMPv2/MLDv1
  19. report suppression takes place. This feature is disabled by default.
  20. The initial patch and idea is from Felix Fietkau.
  21. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  22. [linus.luessing@c0d3.blue: various bug + style fixes, commit message]
  23. Signed-off-by: Linus Lüssing <linus.luessing@c0d3.blue>
  24. ---
  25. --- a/include/linux/if_bridge.h
  26. +++ b/include/linux/if_bridge.h
  27. @@ -46,6 +46,7 @@ struct br_ip_list {
  28. #define BR_LEARNING_SYNC BIT(9)
  29. #define BR_PROXYARP_WIFI BIT(10)
  30. #define BR_MCAST_FLOOD BIT(11)
  31. +#define BR_MULTICAST_TO_UNICAST BIT(12)
  32. #define BR_DEFAULT_AGEING_TIME (300 * HZ)
  33. --- a/include/uapi/linux/if_link.h
  34. +++ b/include/uapi/linux/if_link.h
  35. @@ -319,6 +319,7 @@ enum {
  36. IFLA_BRPORT_MULTICAST_ROUTER,
  37. IFLA_BRPORT_PAD,
  38. IFLA_BRPORT_MCAST_FLOOD,
  39. + IFLA_BRPORT_MCAST_TO_UCAST,
  40. __IFLA_BRPORT_MAX
  41. };
  42. #define IFLA_BRPORT_MAX (__IFLA_BRPORT_MAX - 1)
  43. --- a/net/bridge/br_forward.c
  44. +++ b/net/bridge/br_forward.c
  45. @@ -174,6 +174,29 @@ out:
  46. return p;
  47. }
  48. +static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb,
  49. + const unsigned char *addr, bool local_orig)
  50. +{
  51. + struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  52. + const unsigned char *src = eth_hdr(skb)->h_source;
  53. +
  54. + if (!should_deliver(p, skb))
  55. + return;
  56. +
  57. + /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
  58. + if (skb->dev == p->dev && ether_addr_equal(src, addr))
  59. + return;
  60. +
  61. + skb = skb_copy(skb, GFP_ATOMIC);
  62. + if (!skb) {
  63. + dev->stats.tx_dropped++;
  64. + return;
  65. + }
  66. +
  67. + memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
  68. + __br_forward(p, skb, local_orig);
  69. +}
  70. +
  71. /* called under rcu_read_lock */
  72. void br_flood(struct net_bridge *br, struct sk_buff *skb,
  73. enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
  74. @@ -242,10 +265,20 @@ void br_multicast_flood(struct net_bridg
  75. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  76. NULL;
  77. - port = (unsigned long)lport > (unsigned long)rport ?
  78. - lport : rport;
  79. + if ((unsigned long)lport > (unsigned long)rport) {
  80. + port = lport;
  81. +
  82. + if (p->flags & MDB_PG_FLAGS_MCAST_TO_UCAST) {
  83. + maybe_deliver_addr(lport, skb, p->eth_addr,
  84. + local_orig);
  85. + goto delivered;
  86. + }
  87. + } else {
  88. + port = rport;
  89. + }
  90. prev = maybe_deliver(prev, port, skb, local_orig);
  91. +delivered:
  92. if (IS_ERR(prev))
  93. goto out;
  94. if (prev == port)
  95. --- a/net/bridge/br_mdb.c
  96. +++ b/net/bridge/br_mdb.c
  97. @@ -532,7 +532,7 @@ static int br_mdb_add_group(struct net_b
  98. break;
  99. }
  100. - p = br_multicast_new_port_group(port, group, *pp, state);
  101. + p = br_multicast_new_port_group(port, group, *pp, state, NULL);
  102. if (unlikely(!p))
  103. return -ENOMEM;
  104. rcu_assign_pointer(*pp, p);
  105. --- a/net/bridge/br_multicast.c
  106. +++ b/net/bridge/br_multicast.c
  107. @@ -42,12 +42,14 @@ static void br_multicast_add_router(stru
  108. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  109. struct net_bridge_port *port,
  110. __be32 group,
  111. - __u16 vid);
  112. + __u16 vid,
  113. + const unsigned char *src);
  114. +
  115. #if IS_ENABLED(CONFIG_IPV6)
  116. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  117. struct net_bridge_port *port,
  118. const struct in6_addr *group,
  119. - __u16 vid);
  120. + __u16 vid, const unsigned char *src);
  121. #endif
  122. unsigned int br_mdb_rehash_seq;
  123. @@ -658,7 +660,8 @@ struct net_bridge_port_group *br_multica
  124. struct net_bridge_port *port,
  125. struct br_ip *group,
  126. struct net_bridge_port_group __rcu *next,
  127. - unsigned char flags)
  128. + unsigned char flags,
  129. + const unsigned char *src)
  130. {
  131. struct net_bridge_port_group *p;
  132. @@ -673,12 +676,39 @@ struct net_bridge_port_group *br_multica
  133. hlist_add_head(&p->mglist, &port->mglist);
  134. setup_timer(&p->timer, br_multicast_port_group_expired,
  135. (unsigned long)p);
  136. +
  137. + if ((port->flags & BR_MULTICAST_TO_UNICAST) && src) {
  138. + memcpy(p->eth_addr, src, ETH_ALEN);
  139. + p->flags |= MDB_PG_FLAGS_MCAST_TO_UCAST;
  140. + }
  141. +
  142. return p;
  143. }
  144. +static bool br_port_group_equal(struct net_bridge_port_group *p,
  145. + struct net_bridge_port *port,
  146. + const unsigned char *src)
  147. +{
  148. + if (p->port != port)
  149. + return false;
  150. +
  151. + if (!(p->flags & MDB_PG_FLAGS_MCAST_TO_UCAST) !=
  152. + !(port->flags & BR_MULTICAST_TO_UNICAST))
  153. + return false;
  154. +
  155. + if (!(p->flags & MDB_PG_FLAGS_MCAST_TO_UCAST))
  156. + return true;
  157. +
  158. + if (!src)
  159. + return false;
  160. +
  161. + return ether_addr_equal(src, p->eth_addr);
  162. +}
  163. +
  164. static int br_multicast_add_group(struct net_bridge *br,
  165. struct net_bridge_port *port,
  166. - struct br_ip *group)
  167. + struct br_ip *group,
  168. + const unsigned char *src)
  169. {
  170. struct net_bridge_mdb_entry *mp;
  171. struct net_bridge_port_group *p;
  172. @@ -705,13 +735,13 @@ static int br_multicast_add_group(struct
  173. for (pp = &mp->ports;
  174. (p = mlock_dereference(*pp, br)) != NULL;
  175. pp = &p->next) {
  176. - if (p->port == port)
  177. + if (br_port_group_equal(p, port, src))
  178. goto found;
  179. if ((unsigned long)p->port < (unsigned long)port)
  180. break;
  181. }
  182. - p = br_multicast_new_port_group(port, group, *pp, 0);
  183. + p = br_multicast_new_port_group(port, group, *pp, 0, src);
  184. if (unlikely(!p))
  185. goto err;
  186. rcu_assign_pointer(*pp, p);
  187. @@ -730,7 +760,8 @@ err:
  188. static int br_ip4_multicast_add_group(struct net_bridge *br,
  189. struct net_bridge_port *port,
  190. __be32 group,
  191. - __u16 vid)
  192. + __u16 vid,
  193. + const unsigned char *src)
  194. {
  195. struct br_ip br_group;
  196. @@ -741,14 +772,15 @@ static int br_ip4_multicast_add_group(st
  197. br_group.proto = htons(ETH_P_IP);
  198. br_group.vid = vid;
  199. - return br_multicast_add_group(br, port, &br_group);
  200. + return br_multicast_add_group(br, port, &br_group, src);
  201. }
  202. #if IS_ENABLED(CONFIG_IPV6)
  203. static int br_ip6_multicast_add_group(struct net_bridge *br,
  204. struct net_bridge_port *port,
  205. const struct in6_addr *group,
  206. - __u16 vid)
  207. + __u16 vid,
  208. + const unsigned char *src)
  209. {
  210. struct br_ip br_group;
  211. @@ -759,7 +791,7 @@ static int br_ip6_multicast_add_group(st
  212. br_group.proto = htons(ETH_P_IPV6);
  213. br_group.vid = vid;
  214. - return br_multicast_add_group(br, port, &br_group);
  215. + return br_multicast_add_group(br, port, &br_group, src);
  216. }
  217. #endif
  218. @@ -1028,6 +1060,7 @@ static int br_ip4_multicast_igmp3_report
  219. struct sk_buff *skb,
  220. u16 vid)
  221. {
  222. + const unsigned char *src;
  223. struct igmpv3_report *ih;
  224. struct igmpv3_grec *grec;
  225. int i;
  226. @@ -1068,12 +1101,14 @@ static int br_ip4_multicast_igmp3_report
  227. continue;
  228. }
  229. + src = eth_hdr(skb)->h_source;
  230. if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
  231. type == IGMPV3_MODE_IS_INCLUDE) &&
  232. ntohs(grec->grec_nsrcs) == 0) {
  233. - br_ip4_multicast_leave_group(br, port, group, vid);
  234. + br_ip4_multicast_leave_group(br, port, group, vid, src);
  235. } else {
  236. - err = br_ip4_multicast_add_group(br, port, group, vid);
  237. + err = br_ip4_multicast_add_group(br, port, group, vid,
  238. + src);
  239. if (err)
  240. break;
  241. }
  242. @@ -1088,6 +1123,7 @@ static int br_ip6_multicast_mld2_report(
  243. struct sk_buff *skb,
  244. u16 vid)
  245. {
  246. + const unsigned char *src = eth_hdr(skb)->h_source;
  247. struct icmp6hdr *icmp6h;
  248. struct mld2_grec *grec;
  249. int i;
  250. @@ -1139,10 +1175,11 @@ static int br_ip6_multicast_mld2_report(
  251. grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
  252. ntohs(*nsrcs) == 0) {
  253. br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
  254. - vid);
  255. + vid, src);
  256. } else {
  257. err = br_ip6_multicast_add_group(br, port,
  258. - &grec->grec_mca, vid);
  259. + &grec->grec_mca, vid,
  260. + src);
  261. if (err)
  262. break;
  263. }
  264. @@ -1458,7 +1495,8 @@ br_multicast_leave_group(struct net_brid
  265. struct net_bridge_port *port,
  266. struct br_ip *group,
  267. struct bridge_mcast_other_query *other_query,
  268. - struct bridge_mcast_own_query *own_query)
  269. + struct bridge_mcast_own_query *own_query,
  270. + const unsigned char *src)
  271. {
  272. struct net_bridge_mdb_htable *mdb;
  273. struct net_bridge_mdb_entry *mp;
  274. @@ -1482,7 +1520,7 @@ br_multicast_leave_group(struct net_brid
  275. for (pp = &mp->ports;
  276. (p = mlock_dereference(*pp, br)) != NULL;
  277. pp = &p->next) {
  278. - if (p->port != port)
  279. + if (!br_port_group_equal(p, port, src))
  280. continue;
  281. rcu_assign_pointer(*pp, p->next);
  282. @@ -1513,7 +1551,7 @@ br_multicast_leave_group(struct net_brid
  283. for (p = mlock_dereference(mp->ports, br);
  284. p != NULL;
  285. p = mlock_dereference(p->next, br)) {
  286. - if (p->port != port)
  287. + if (!br_port_group_equal(p, port, src))
  288. continue;
  289. if (!hlist_unhashed(&p->mglist) &&
  290. @@ -1564,7 +1602,8 @@ out:
  291. static void br_ip4_multicast_leave_group(struct net_bridge *br,
  292. struct net_bridge_port *port,
  293. __be32 group,
  294. - __u16 vid)
  295. + __u16 vid,
  296. + const unsigned char *src)
  297. {
  298. struct br_ip br_group;
  299. struct bridge_mcast_own_query *own_query;
  300. @@ -1579,14 +1618,15 @@ static void br_ip4_multicast_leave_group
  301. br_group.vid = vid;
  302. br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
  303. - own_query);
  304. + own_query, src);
  305. }
  306. #if IS_ENABLED(CONFIG_IPV6)
  307. static void br_ip6_multicast_leave_group(struct net_bridge *br,
  308. struct net_bridge_port *port,
  309. const struct in6_addr *group,
  310. - __u16 vid)
  311. + __u16 vid,
  312. + const unsigned char *src)
  313. {
  314. struct br_ip br_group;
  315. struct bridge_mcast_own_query *own_query;
  316. @@ -1601,7 +1641,7 @@ static void br_ip6_multicast_leave_group
  317. br_group.vid = vid;
  318. br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
  319. - own_query);
  320. + own_query, src);
  321. }
  322. #endif
  323. @@ -1644,6 +1684,7 @@ static int br_multicast_ipv4_rcv(struct
  324. u16 vid)
  325. {
  326. struct sk_buff *skb_trimmed = NULL;
  327. + const unsigned char *src;
  328. struct igmphdr *ih;
  329. int err;
  330. @@ -1659,13 +1700,14 @@ static int br_multicast_ipv4_rcv(struct
  331. }
  332. ih = igmp_hdr(skb);
  333. + src = eth_hdr(skb)->h_source;
  334. BR_INPUT_SKB_CB(skb)->igmp = ih->type;
  335. switch (ih->type) {
  336. case IGMP_HOST_MEMBERSHIP_REPORT:
  337. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  338. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  339. - err = br_ip4_multicast_add_group(br, port, ih->group, vid);
  340. + err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
  341. break;
  342. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  343. err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
  344. @@ -1674,7 +1716,7 @@ static int br_multicast_ipv4_rcv(struct
  345. err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
  346. break;
  347. case IGMP_HOST_LEAVE_MESSAGE:
  348. - br_ip4_multicast_leave_group(br, port, ih->group, vid);
  349. + br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
  350. break;
  351. }
  352. @@ -1694,6 +1736,7 @@ static int br_multicast_ipv6_rcv(struct
  353. u16 vid)
  354. {
  355. struct sk_buff *skb_trimmed = NULL;
  356. + const unsigned char *src;
  357. struct mld_msg *mld;
  358. int err;
  359. @@ -1713,8 +1756,10 @@ static int br_multicast_ipv6_rcv(struct
  360. switch (mld->mld_type) {
  361. case ICMPV6_MGM_REPORT:
  362. + src = eth_hdr(skb)->h_source;
  363. BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
  364. - err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
  365. + err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
  366. + src);
  367. break;
  368. case ICMPV6_MLD2_REPORT:
  369. err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
  370. @@ -1723,7 +1768,8 @@ static int br_multicast_ipv6_rcv(struct
  371. err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
  372. break;
  373. case ICMPV6_MGM_REDUCTION:
  374. - br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
  375. + src = eth_hdr(skb)->h_source;
  376. + br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
  377. break;
  378. }
  379. --- a/net/bridge/br_netlink.c
  380. +++ b/net/bridge/br_netlink.c
  381. @@ -123,6 +123,7 @@ static inline size_t br_port_info_size(v
  382. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  383. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  384. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  385. + + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
  386. + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
  387. + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
  388. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
  389. @@ -173,6 +174,8 @@ static int br_port_fill_attrs(struct sk_
  390. !!(p->flags & BR_ROOT_BLOCK)) ||
  391. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
  392. !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
  393. + nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
  394. + !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
  395. nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
  396. nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
  397. !!(p->flags & BR_FLOOD)) ||
  398. @@ -586,6 +589,7 @@ static const struct nla_policy br_port_p
  399. [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
  400. [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
  401. [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
  402. + [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
  403. };
  404. /* Change the state of the port and notify spanning tree */
  405. @@ -636,6 +640,7 @@ static int br_setport(struct net_bridge_
  406. br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
  407. br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
  408. br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
  409. + br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
  410. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
  411. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
  412. --- a/net/bridge/br_private.h
  413. +++ b/net/bridge/br_private.h
  414. @@ -166,8 +166,9 @@ struct net_bridge_fdb_entry
  415. struct rcu_head rcu;
  416. };
  417. -#define MDB_PG_FLAGS_PERMANENT BIT(0)
  418. -#define MDB_PG_FLAGS_OFFLOAD BIT(1)
  419. +#define MDB_PG_FLAGS_PERMANENT BIT(0)
  420. +#define MDB_PG_FLAGS_OFFLOAD BIT(1)
  421. +#define MDB_PG_FLAGS_MCAST_TO_UCAST BIT(2)
  422. struct net_bridge_port_group {
  423. struct net_bridge_port *port;
  424. @@ -177,6 +178,7 @@ struct net_bridge_port_group {
  425. struct timer_list timer;
  426. struct br_ip addr;
  427. unsigned char flags;
  428. + unsigned char eth_addr[ETH_ALEN];
  429. };
  430. struct net_bridge_mdb_entry
  431. @@ -591,7 +593,7 @@ void br_multicast_free_pg(struct rcu_hea
  432. struct net_bridge_port_group *
  433. br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
  434. struct net_bridge_port_group __rcu *next,
  435. - unsigned char flags);
  436. + unsigned char flags, const unsigned char *src);
  437. void br_mdb_init(void);
  438. void br_mdb_uninit(void);
  439. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  440. --- a/net/bridge/br_sysfs_if.c
  441. +++ b/net/bridge/br_sysfs_if.c
  442. @@ -188,6 +188,7 @@ static BRPORT_ATTR(multicast_router, S_I
  443. store_multicast_router);
  444. BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
  445. +BRPORT_ATTR_FLAG(multicast_to_unicast, BR_MULTICAST_TO_UNICAST);
  446. #endif
  447. static const struct brport_attribute *brport_attrs[] = {
  448. @@ -214,6 +215,7 @@ static const struct brport_attribute *br
  449. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  450. &brport_attr_multicast_router,
  451. &brport_attr_multicast_fast_leave,
  452. + &brport_attr_multicast_to_unicast,
  453. #endif
  454. &brport_attr_proxyarp,
  455. &brport_attr_proxyarp_wifi,