|
40 | 40 | #include <linux/list.h>
|
41 | 41 | #include <linux/lockdep.h>
|
42 | 42 | #include <linux/netdevice.h>
|
| 43 | +#include <linux/netlink.h> |
43 | 44 | #include <linux/printk.h>
|
44 | 45 | #include <linux/rculist.h>
|
45 | 46 | #include <linux/rcupdate.h>
|
|
52 | 53 | #include <linux/types.h>
|
53 | 54 | #include <linux/workqueue.h>
|
54 | 55 | #include <net/addrconf.h>
|
| 56 | +#include <net/genetlink.h> |
55 | 57 | #include <net/if_inet6.h>
|
56 | 58 | #include <net/ip.h>
|
57 | 59 | #include <net/ipv6.h>
|
| 60 | +#include <net/netlink.h> |
| 61 | +#include <net/sock.h> |
58 | 62 | #include <uapi/linux/batadv_packet.h>
|
| 63 | +#include <uapi/linux/batman_adv.h> |
59 | 64 |
|
60 | 65 | #include "hard-interface.h"
|
61 | 66 | #include "hash.h"
|
62 | 67 | #include "log.h"
|
| 68 | +#include "netlink.h" |
| 69 | +#include "soft-interface.h" |
63 | 70 | #include "translation-table.h"
|
64 | 71 | #include "tvlv.h"
|
65 | 72 |
|
@@ -1333,6 +1340,236 @@ int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
|
1333 | 1340 | }
|
1334 | 1341 | #endif
|
1335 | 1342 |
|
| 1343 | +/** |
| 1344 | + * batadv_mcast_mesh_info_put() - put multicast info into a netlink message |
| 1345 | + * @msg: buffer for the message |
| 1346 | + * @bat_priv: the bat priv with all the soft interface information |
| 1347 | + * |
| 1348 | + * Return: 0 or error code. |
| 1349 | + */ |
| 1350 | +int batadv_mcast_mesh_info_put(struct sk_buff *msg, |
| 1351 | + struct batadv_priv *bat_priv) |
| 1352 | +{ |
| 1353 | + u32 flags = bat_priv->mcast.flags; |
| 1354 | + u32 flags_priv = BATADV_NO_FLAGS; |
| 1355 | + |
| 1356 | + if (bat_priv->mcast.bridged) { |
| 1357 | + flags_priv |= BATADV_MCAST_FLAGS_BRIDGED; |
| 1358 | + |
| 1359 | + if (bat_priv->mcast.querier_ipv4.exists) |
| 1360 | + flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS; |
| 1361 | + if (bat_priv->mcast.querier_ipv6.exists) |
| 1362 | + flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS; |
| 1363 | + if (bat_priv->mcast.querier_ipv4.shadowing) |
| 1364 | + flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING; |
| 1365 | + if (bat_priv->mcast.querier_ipv6.shadowing) |
| 1366 | + flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING; |
| 1367 | + } |
| 1368 | + |
| 1369 | + if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) || |
| 1370 | + nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv)) |
| 1371 | + return -EMSGSIZE; |
| 1372 | + |
| 1373 | + return 0; |
| 1374 | +} |
| 1375 | + |
| 1376 | +/** |
| 1377 | + * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table |
| 1378 | + * to a netlink socket |
| 1379 | + * @msg: buffer for the message |
| 1380 | + * @portid: netlink port |
| 1381 | + * @seq: Sequence number of netlink message |
| 1382 | + * @orig_node: originator to dump the multicast flags of |
| 1383 | + * |
| 1384 | + * Return: 0 or error code. |
| 1385 | + */ |
| 1386 | +static int |
| 1387 | +batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, |
| 1388 | + struct batadv_orig_node *orig_node) |
| 1389 | +{ |
| 1390 | + void *hdr; |
| 1391 | + |
| 1392 | + hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, |
| 1393 | + NLM_F_MULTI, BATADV_CMD_GET_MCAST_FLAGS); |
| 1394 | + if (!hdr) |
| 1395 | + return -ENOBUFS; |
| 1396 | + |
| 1397 | + if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, |
| 1398 | + orig_node->orig)) { |
| 1399 | + genlmsg_cancel(msg, hdr); |
| 1400 | + return -EMSGSIZE; |
| 1401 | + } |
| 1402 | + |
| 1403 | + if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, |
| 1404 | + &orig_node->capabilities)) { |
| 1405 | + if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, |
| 1406 | + orig_node->mcast_flags)) { |
| 1407 | + genlmsg_cancel(msg, hdr); |
| 1408 | + return -EMSGSIZE; |
| 1409 | + } |
| 1410 | + } |
| 1411 | + |
| 1412 | + genlmsg_end(msg, hdr); |
| 1413 | + return 0; |
| 1414 | +} |
| 1415 | + |
| 1416 | +/** |
| 1417 | + * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags |
| 1418 | + * table to a netlink socket |
| 1419 | + * @msg: buffer for the message |
| 1420 | + * @portid: netlink port |
| 1421 | + * @seq: Sequence number of netlink message |
| 1422 | + * @head: bucket to dump |
| 1423 | + * @idx_skip: How many entries to skip |
| 1424 | + * |
| 1425 | + * Return: 0 or error code. |
| 1426 | + */ |
| 1427 | +static int |
| 1428 | +batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq, |
| 1429 | + struct hlist_head *head, long *idx_skip) |
| 1430 | +{ |
| 1431 | + struct batadv_orig_node *orig_node; |
| 1432 | + long idx = 0; |
| 1433 | + |
| 1434 | + rcu_read_lock(); |
| 1435 | + hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
| 1436 | + if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, |
| 1437 | + &orig_node->capa_initialized)) |
| 1438 | + continue; |
| 1439 | + |
| 1440 | + if (idx < *idx_skip) |
| 1441 | + goto skip; |
| 1442 | + |
| 1443 | + if (batadv_mcast_flags_dump_entry(msg, portid, seq, |
| 1444 | + orig_node)) { |
| 1445 | + rcu_read_unlock(); |
| 1446 | + *idx_skip = idx; |
| 1447 | + |
| 1448 | + return -EMSGSIZE; |
| 1449 | + } |
| 1450 | + |
| 1451 | +skip: |
| 1452 | + idx++; |
| 1453 | + } |
| 1454 | + rcu_read_unlock(); |
| 1455 | + |
| 1456 | + return 0; |
| 1457 | +} |
| 1458 | + |
| 1459 | +/** |
| 1460 | + * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket |
| 1461 | + * @msg: buffer for the message |
| 1462 | + * @portid: netlink port |
| 1463 | + * @seq: Sequence number of netlink message |
| 1464 | + * @bat_priv: the bat priv with all the soft interface information |
| 1465 | + * @bucket: current bucket to dump |
| 1466 | + * @idx: index in current bucket to the next entry to dump |
| 1467 | + * |
| 1468 | + * Return: 0 or error code. |
| 1469 | + */ |
| 1470 | +static int |
| 1471 | +__batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, u32 seq, |
| 1472 | + struct batadv_priv *bat_priv, long *bucket, long *idx) |
| 1473 | +{ |
| 1474 | + struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 1475 | + long bucket_tmp = *bucket; |
| 1476 | + struct hlist_head *head; |
| 1477 | + long idx_tmp = *idx; |
| 1478 | + |
| 1479 | + while (bucket_tmp < hash->size) { |
| 1480 | + head = &hash->table[bucket_tmp]; |
| 1481 | + |
| 1482 | + if (batadv_mcast_flags_dump_bucket(msg, portid, seq, head, |
| 1483 | + &idx_tmp)) |
| 1484 | + break; |
| 1485 | + |
| 1486 | + bucket_tmp++; |
| 1487 | + idx_tmp = 0; |
| 1488 | + } |
| 1489 | + |
| 1490 | + *bucket = bucket_tmp; |
| 1491 | + *idx = idx_tmp; |
| 1492 | + |
| 1493 | + return msg->len; |
| 1494 | +} |
| 1495 | + |
| 1496 | +/** |
| 1497 | + * batadv_mcast_netlink_get_primary() - get primary interface from netlink |
| 1498 | + * callback |
| 1499 | + * @cb: netlink callback structure |
| 1500 | + * @primary_if: the primary interface pointer to return the result in |
| 1501 | + * |
| 1502 | + * Return: 0 or error code. |
| 1503 | + */ |
| 1504 | +static int |
| 1505 | +batadv_mcast_netlink_get_primary(struct netlink_callback *cb, |
| 1506 | + struct batadv_hard_iface **primary_if) |
| 1507 | +{ |
| 1508 | + struct batadv_hard_iface *hard_iface = NULL; |
| 1509 | + struct net *net = sock_net(cb->skb->sk); |
| 1510 | + struct net_device *soft_iface; |
| 1511 | + struct batadv_priv *bat_priv; |
| 1512 | + int ifindex; |
| 1513 | + int ret = 0; |
| 1514 | + |
| 1515 | + ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX); |
| 1516 | + if (!ifindex) |
| 1517 | + return -EINVAL; |
| 1518 | + |
| 1519 | + soft_iface = dev_get_by_index(net, ifindex); |
| 1520 | + if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { |
| 1521 | + ret = -ENODEV; |
| 1522 | + goto out; |
| 1523 | + } |
| 1524 | + |
| 1525 | + bat_priv = netdev_priv(soft_iface); |
| 1526 | + |
| 1527 | + hard_iface = batadv_primary_if_get_selected(bat_priv); |
| 1528 | + if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) { |
| 1529 | + ret = -ENOENT; |
| 1530 | + goto out; |
| 1531 | + } |
| 1532 | + |
| 1533 | +out: |
| 1534 | + if (soft_iface) |
| 1535 | + dev_put(soft_iface); |
| 1536 | + |
| 1537 | + if (!ret && primary_if) |
| 1538 | + *primary_if = hard_iface; |
| 1539 | + else |
| 1540 | + batadv_hardif_put(hard_iface); |
| 1541 | + |
| 1542 | + return ret; |
| 1543 | +} |
| 1544 | + |
| 1545 | +/** |
| 1546 | + * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket |
| 1547 | + * @msg: buffer for the message |
| 1548 | + * @cb: callback structure containing arguments |
| 1549 | + * |
| 1550 | + * Return: message length. |
| 1551 | + */ |
| 1552 | +int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb) |
| 1553 | +{ |
| 1554 | + struct batadv_hard_iface *primary_if = NULL; |
| 1555 | + int portid = NETLINK_CB(cb->skb).portid; |
| 1556 | + struct batadv_priv *bat_priv; |
| 1557 | + long *bucket = &cb->args[0]; |
| 1558 | + long *idx = &cb->args[1]; |
| 1559 | + int ret; |
| 1560 | + |
| 1561 | + ret = batadv_mcast_netlink_get_primary(cb, &primary_if); |
| 1562 | + if (ret) |
| 1563 | + return ret; |
| 1564 | + |
| 1565 | + bat_priv = netdev_priv(primary_if->soft_iface); |
| 1566 | + ret = __batadv_mcast_flags_dump(msg, portid, cb->nlh->nlmsg_seq, |
| 1567 | + bat_priv, bucket, idx); |
| 1568 | + |
| 1569 | + batadv_hardif_put(primary_if); |
| 1570 | + return ret; |
| 1571 | +} |
| 1572 | + |
1336 | 1573 | /**
|
1337 | 1574 | * batadv_mcast_free() - free the multicast optimizations structures
|
1338 | 1575 | * @bat_priv: the bat priv with all the soft interface information
|
|
0 commit comments