Skip to content

Commit 29a1ff6

Browse files
committed
Merge branch 'igmp_mld_export'
Linus Lüssing says: ==================== Exporting IGMP/MLD checking from bridge code The multicast optimizations in batman-adv are yet only usable and enabled in non-bridged scenarios. To be able to support bridged setups batman-adv needs to be able to detect IGMP/MLD queriers and reports on mesh nodes without bridges, too. See the following link for details: http://www.open-mesh.org/projects/batman-adv/wiki/Multicast-optimizations-listener-reports To avoid duplicate code between the bridge and batman-adv, the IGMP/MLD message validation code is moved from the bridge to the IPv4/IPv6 stack. On the way, some refactoring to increase readability and to iron out some subtle differences between the IGMP and MLD parsing code is done. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents c19ae86 + 9afd85c commit 29a1ff6

8 files changed

Lines changed: 498 additions & 208 deletions

File tree

‎include/linux/igmp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@ extern void ip_mc_unmap(struct in_device *);
130130
extern void ip_mc_remap(struct in_device *);
131131
extern void ip_mc_dec_group(struct in_device *in_dev, __be32 addr);
132132
extern void ip_mc_inc_group(struct in_device *in_dev, __be32 addr);
133+
int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed);
133134

134135
#endif

‎include/linux/skbuff.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,6 +3419,9 @@ static inline void skb_checksum_none_assert(const struct sk_buff *skb)
34193419
bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
34203420

34213421
int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
3422+
struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
3423+
unsigned int transport_len,
3424+
__sum16(*skb_chkf)(struct sk_buff *skb));
34223425

34233426
u32 skb_get_poff(const struct sk_buff *skb);
34243427
u32 __skb_get_poff(const struct sk_buff *skb, void *data,

‎include/net/addrconf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ void ipv6_mc_unmap(struct inet6_dev *idev);
142142
void ipv6_mc_remap(struct inet6_dev *idev);
143143
void ipv6_mc_init_dev(struct inet6_dev *idev);
144144
void ipv6_mc_destroy_dev(struct inet6_dev *idev);
145+
int ipv6_mc_check_mld(struct sk_buff *skb, struct sk_buff **skb_trimmed);
145146
void addrconf_dad_failure(struct inet6_ifaddr *ifp);
146147

147148
bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,

‎net/bridge/br_multicast.c‎

Lines changed: 30 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,6 @@ static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
975975
int err = 0;
976976
__be32 group;
977977

978-
if (!pskb_may_pull(skb, sizeof(*ih)))
979-
return -EINVAL;
980-
981978
ih = igmpv3_report_hdr(skb);
982979
num = ntohs(ih->ngrec);
983980
len = sizeof(*ih);
@@ -1248,25 +1245,14 @@ static int br_ip4_multicast_query(struct net_bridge *br,
12481245
max_delay = 10 * HZ;
12491246
group = 0;
12501247
}
1251-
} else {
1252-
if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1253-
err = -EINVAL;
1254-
goto out;
1255-
}
1256-
1248+
} else if (skb->len >= sizeof(*ih3)) {
12571249
ih3 = igmpv3_query_hdr(skb);
12581250
if (ih3->nsrcs)
12591251
goto out;
12601252

12611253
max_delay = ih3->code ?
12621254
IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1263-
}
1264-
1265-
/* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1266-
* all-systems destination addresses (224.0.0.1) for general queries
1267-
*/
1268-
if (!group && iph->daddr != htonl(INADDR_ALLHOSTS_GROUP)) {
1269-
err = -EINVAL;
1255+
} else {
12701256
goto out;
12711257
}
12721258

@@ -1329,12 +1315,6 @@ static int br_ip6_multicast_query(struct net_bridge *br,
13291315
(port && port->state == BR_STATE_DISABLED))
13301316
goto out;
13311317

1332-
/* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1333-
if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) {
1334-
err = -EINVAL;
1335-
goto out;
1336-
}
1337-
13381318
if (skb->len == sizeof(*mld)) {
13391319
if (!pskb_may_pull(skb, sizeof(*mld))) {
13401320
err = -EINVAL;
@@ -1358,14 +1338,6 @@ static int br_ip6_multicast_query(struct net_bridge *br,
13581338

13591339
is_general_query = group && ipv6_addr_any(group);
13601340

1361-
/* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
1362-
* all-nodes destination address (ff02::1) for general queries
1363-
*/
1364-
if (is_general_query && !ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) {
1365-
err = -EINVAL;
1366-
goto out;
1367-
}
1368-
13691341
if (is_general_query) {
13701342
saddr.proto = htons(ETH_P_IPV6);
13711343
saddr.u.ip6 = ip6h->saddr;
@@ -1557,74 +1529,22 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
15571529
struct sk_buff *skb,
15581530
u16 vid)
15591531
{
1560-
struct sk_buff *skb2 = skb;
1561-
const struct iphdr *iph;
1532+
struct sk_buff *skb_trimmed = NULL;
15621533
struct igmphdr *ih;
1563-
unsigned int len;
1564-
unsigned int offset;
15651534
int err;
15661535

1567-
/* We treat OOM as packet loss for now. */
1568-
if (!pskb_may_pull(skb, sizeof(*iph)))
1569-
return -EINVAL;
1570-
1571-
iph = ip_hdr(skb);
1572-
1573-
if (iph->ihl < 5 || iph->version != 4)
1574-
return -EINVAL;
1575-
1576-
if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1577-
return -EINVAL;
1578-
1579-
iph = ip_hdr(skb);
1580-
1581-
if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1582-
return -EINVAL;
1536+
err = ip_mc_check_igmp(skb, &skb_trimmed);
15831537

1584-
if (iph->protocol != IPPROTO_IGMP) {
1585-
if (!ipv4_is_local_multicast(iph->daddr))
1538+
if (err == -ENOMSG) {
1539+
if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
15861540
BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
15871541
return 0;
1542+
} else if (err < 0) {
1543+
return err;
15881544
}
15891545

1590-
len = ntohs(iph->tot_len);
1591-
if (skb->len < len || len < ip_hdrlen(skb))
1592-
return -EINVAL;
1593-
1594-
if (skb->len > len) {
1595-
skb2 = skb_clone(skb, GFP_ATOMIC);
1596-
if (!skb2)
1597-
return -ENOMEM;
1598-
1599-
err = pskb_trim_rcsum(skb2, len);
1600-
if (err)
1601-
goto err_out;
1602-
}
1603-
1604-
len -= ip_hdrlen(skb2);
1605-
offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1606-
__skb_pull(skb2, offset);
1607-
skb_reset_transport_header(skb2);
1608-
1609-
err = -EINVAL;
1610-
if (!pskb_may_pull(skb2, sizeof(*ih)))
1611-
goto out;
1612-
1613-
switch (skb2->ip_summed) {
1614-
case CHECKSUM_COMPLETE:
1615-
if (!csum_fold(skb2->csum))
1616-
break;
1617-
/* fall through */
1618-
case CHECKSUM_NONE:
1619-
skb2->csum = 0;
1620-
if (skb_checksum_complete(skb2))
1621-
goto out;
1622-
}
1623-
1624-
err = 0;
1625-
16261546
BR_INPUT_SKB_CB(skb)->igmp = 1;
1627-
ih = igmp_hdr(skb2);
1547+
ih = igmp_hdr(skb);
16281548

16291549
switch (ih->type) {
16301550
case IGMP_HOST_MEMBERSHIP_REPORT:
@@ -1633,21 +1553,19 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
16331553
err = br_ip4_multicast_add_group(br, port, ih->group, vid);
16341554
break;
16351555
case IGMPV3_HOST_MEMBERSHIP_REPORT:
1636-
err = br_ip4_multicast_igmp3_report(br, port, skb2, vid);
1556+
err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
16371557
break;
16381558
case IGMP_HOST_MEMBERSHIP_QUERY:
1639-
err = br_ip4_multicast_query(br, port, skb2, vid);
1559+
err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
16401560
break;
16411561
case IGMP_HOST_LEAVE_MESSAGE:
16421562
br_ip4_multicast_leave_group(br, port, ih->group, vid);
16431563
break;
16441564
}
16451565

1646-
out:
1647-
__skb_push(skb2, offset);
1648-
err_out:
1649-
if (skb2 != skb)
1650-
kfree_skb(skb2);
1566+
if (skb_trimmed)
1567+
kfree_skb(skb_trimmed);
1568+
16511569
return err;
16521570
}
16531571

@@ -1657,138 +1575,42 @@ static int br_multicast_ipv6_rcv(struct net_bridge *br,
16571575
struct sk_buff *skb,
16581576
u16 vid)
16591577
{
1660-
struct sk_buff *skb2;
1661-
const struct ipv6hdr *ip6h;
1662-
u8 icmp6_type;
1663-
u8 nexthdr;
1664-
__be16 frag_off;
1665-
unsigned int len;
1666-
int offset;
1578+
struct sk_buff *skb_trimmed = NULL;
1579+
struct mld_msg *mld;
16671580
int err;
16681581

1669-
if (!pskb_may_pull(skb, sizeof(*ip6h)))
1670-
return -EINVAL;
1671-
1672-
ip6h = ipv6_hdr(skb);
1673-
1674-
/*
1675-
* We're interested in MLD messages only.
1676-
* - Version is 6
1677-
* - MLD has always Router Alert hop-by-hop option
1678-
* - But we do not support jumbrograms.
1679-
*/
1680-
if (ip6h->version != 6)
1681-
return 0;
1682-
1683-
/* Prevent flooding this packet if there is no listener present */
1684-
if (!ipv6_addr_is_ll_all_nodes(&ip6h->daddr))
1685-
BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1582+
err = ipv6_mc_check_mld(skb, &skb_trimmed);
16861583

1687-
if (ip6h->nexthdr != IPPROTO_HOPOPTS ||
1688-
ip6h->payload_len == 0)
1689-
return 0;
1690-
1691-
len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
1692-
if (skb->len < len)
1693-
return -EINVAL;
1694-
1695-
nexthdr = ip6h->nexthdr;
1696-
offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
1697-
1698-
if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1584+
if (err == -ENOMSG) {
1585+
if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1586+
BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
16991587
return 0;
1700-
1701-
/* Okay, we found ICMPv6 header */
1702-
skb2 = skb_clone(skb, GFP_ATOMIC);
1703-
if (!skb2)
1704-
return -ENOMEM;
1705-
1706-
err = -EINVAL;
1707-
if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1708-
goto out;
1709-
1710-
len -= offset - skb_network_offset(skb2);
1711-
1712-
__skb_pull(skb2, offset);
1713-
skb_reset_transport_header(skb2);
1714-
skb_postpull_rcsum(skb2, skb_network_header(skb2),
1715-
skb_network_header_len(skb2));
1716-
1717-
icmp6_type = icmp6_hdr(skb2)->icmp6_type;
1718-
1719-
switch (icmp6_type) {
1720-
case ICMPV6_MGM_QUERY:
1721-
case ICMPV6_MGM_REPORT:
1722-
case ICMPV6_MGM_REDUCTION:
1723-
case ICMPV6_MLD2_REPORT:
1724-
break;
1725-
default:
1726-
err = 0;
1727-
goto out;
1728-
}
1729-
1730-
/* Okay, we found MLD message. Check further. */
1731-
if (skb2->len > len) {
1732-
err = pskb_trim_rcsum(skb2, len);
1733-
if (err)
1734-
goto out;
1735-
err = -EINVAL;
1736-
}
1737-
1738-
ip6h = ipv6_hdr(skb2);
1739-
1740-
switch (skb2->ip_summed) {
1741-
case CHECKSUM_COMPLETE:
1742-
if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1743-
IPPROTO_ICMPV6, skb2->csum))
1744-
break;
1745-
/*FALLTHROUGH*/
1746-
case CHECKSUM_NONE:
1747-
skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1748-
&ip6h->daddr,
1749-
skb2->len,
1750-
IPPROTO_ICMPV6, 0));
1751-
if (__skb_checksum_complete(skb2))
1752-
goto out;
1588+
} else if (err < 0) {
1589+
return err;
17531590
}
17541591

1755-
err = 0;
1756-
17571592
BR_INPUT_SKB_CB(skb)->igmp = 1;
1593+
mld = (struct mld_msg *)skb_transport_header(skb);
17581594

1759-
switch (icmp6_type) {
1595+
switch (mld->mld_type) {
17601596
case ICMPV6_MGM_REPORT:
1761-
{
1762-
struct mld_msg *mld;
1763-
if (!pskb_may_pull(skb2, sizeof(*mld))) {
1764-
err = -EINVAL;
1765-
goto out;
1766-
}
1767-
mld = (struct mld_msg *)skb_transport_header(skb2);
17681597
BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
17691598
err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
17701599
break;
1771-
}
17721600
case ICMPV6_MLD2_REPORT:
1773-
err = br_ip6_multicast_mld2_report(br, port, skb2, vid);
1601+
err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
17741602
break;
17751603
case ICMPV6_MGM_QUERY:
1776-
err = br_ip6_multicast_query(br, port, skb2, vid);
1604+
err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
17771605
break;
17781606
case ICMPV6_MGM_REDUCTION:
1779-
{
1780-
struct mld_msg *mld;
1781-
if (!pskb_may_pull(skb2, sizeof(*mld))) {
1782-
err = -EINVAL;
1783-
goto out;
1784-
}
1785-
mld = (struct mld_msg *)skb_transport_header(skb2);
17861607
br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
1787-
}
1608+
break;
17881609
}
17891610

1790-
out:
1791-
kfree_skb(skb2);
1611+
if (skb_trimmed)
1612+
kfree_skb(skb_trimmed);
1613+
17921614
return err;
17931615
}
17941616
#endif

0 commit comments

Comments
 (0)