1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/netdevice.h> 4 #include <linux/notifier.h> 5 #include <linux/rtnetlink.h> 6 #include <net/net_namespace.h> 7 #include <net/sock.h> 8 #include <net/xdp.h> 9 10 #include "netdev-genl-gen.h" 11 12 static int 13 netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp, 14 const struct genl_info *info) 15 { 16 u64 xdp_rx_meta = 0; 17 void *hdr; 18 19 hdr = genlmsg_iput(rsp, info); 20 if (!hdr) 21 return -EMSGSIZE; 22 23 #define XDP_METADATA_KFUNC(_, flag, __, xmo) \ 24 if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \ 25 xdp_rx_meta |= flag; 26 XDP_METADATA_KFUNC_xxx 27 #undef XDP_METADATA_KFUNC 28 29 if (nla_put_u32(rsp, NETDEV_A_DEV_IFINDEX, netdev->ifindex) || 30 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_FEATURES, 31 netdev->xdp_features, NETDEV_A_DEV_PAD) || 32 nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_RX_METADATA_FEATURES, 33 xdp_rx_meta, NETDEV_A_DEV_PAD)) { 34 genlmsg_cancel(rsp, hdr); 35 return -EINVAL; 36 } 37 38 if (netdev->xdp_features & NETDEV_XDP_ACT_XSK_ZEROCOPY) { 39 if (nla_put_u32(rsp, NETDEV_A_DEV_XDP_ZC_MAX_SEGS, 40 netdev->xdp_zc_max_segs)) { 41 genlmsg_cancel(rsp, hdr); 42 return -EINVAL; 43 } 44 } 45 46 genlmsg_end(rsp, hdr); 47 48 return 0; 49 } 50 51 static void 52 netdev_genl_dev_notify(struct net_device *netdev, int cmd) 53 { 54 struct genl_info info; 55 struct sk_buff *ntf; 56 57 if (!genl_has_listeners(&netdev_nl_family, dev_net(netdev), 58 NETDEV_NLGRP_MGMT)) 59 return; 60 61 genl_info_init_ntf(&info, &netdev_nl_family, cmd); 62 63 ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 64 if (!ntf) 65 return; 66 67 if (netdev_nl_dev_fill(netdev, ntf, &info)) { 68 nlmsg_free(ntf); 69 return; 70 } 71 72 genlmsg_multicast_netns(&netdev_nl_family, dev_net(netdev), ntf, 73 0, NETDEV_NLGRP_MGMT, GFP_KERNEL); 74 } 75 76 int netdev_nl_dev_get_doit(struct sk_buff *skb, struct genl_info *info) 77 { 78 struct net_device *netdev; 79 struct sk_buff *rsp; 80 u32 ifindex; 81 int err; 82 83 if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_DEV_IFINDEX)) 84 return -EINVAL; 85 86 ifindex = nla_get_u32(info->attrs[NETDEV_A_DEV_IFINDEX]); 87 88 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 89 if (!rsp) 90 return -ENOMEM; 91 92 rtnl_lock(); 93 94 netdev = __dev_get_by_index(genl_info_net(info), ifindex); 95 if (netdev) 96 err = netdev_nl_dev_fill(netdev, rsp, info); 97 else 98 err = -ENODEV; 99 100 rtnl_unlock(); 101 102 if (err) 103 goto err_free_msg; 104 105 return genlmsg_reply(rsp, info); 106 107 err_free_msg: 108 nlmsg_free(rsp); 109 return err; 110 } 111 112 int netdev_nl_dev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 113 { 114 struct net *net = sock_net(skb->sk); 115 struct net_device *netdev; 116 int err = 0; 117 118 rtnl_lock(); 119 for_each_netdev_dump(net, netdev, cb->args[0]) { 120 err = netdev_nl_dev_fill(netdev, skb, genl_info_dump(cb)); 121 if (err < 0) 122 break; 123 } 124 rtnl_unlock(); 125 126 if (err != -EMSGSIZE) 127 return err; 128 129 return skb->len; 130 } 131 132 static int netdev_genl_netdevice_event(struct notifier_block *nb, 133 unsigned long event, void *ptr) 134 { 135 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 136 137 switch (event) { 138 case NETDEV_REGISTER: 139 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_ADD_NTF); 140 break; 141 case NETDEV_UNREGISTER: 142 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_DEL_NTF); 143 break; 144 case NETDEV_XDP_FEAT_CHANGE: 145 netdev_genl_dev_notify(netdev, NETDEV_CMD_DEV_CHANGE_NTF); 146 break; 147 } 148 149 return NOTIFY_OK; 150 } 151 152 static struct notifier_block netdev_genl_nb = { 153 .notifier_call = netdev_genl_netdevice_event, 154 }; 155 156 static int __init netdev_genl_init(void) 157 { 158 int err; 159 160 err = register_netdevice_notifier(&netdev_genl_nb); 161 if (err) 162 return err; 163 164 err = genl_register_family(&netdev_nl_family); 165 if (err) 166 goto err_unreg_ntf; 167 168 return 0; 169 170 err_unreg_ntf: 171 unregister_netdevice_notifier(&netdev_genl_nb); 172 return err; 173 } 174 175 subsys_initcall(netdev_genl_init); 176