1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2023 Isovalent */
4 #include <linux/netdevice.h>
5 #include <linux/ethtool.h>
6 #include <linux/etherdevice.h>
7 #include <linux/filter.h>
8 #include <linux/netfilter_netdev.h>
9 #include <linux/bpf_mprog.h>
10 #include <linux/indirect_call_wrapper.h>
12 #include <net/netkit.h>
16 #define DRV_NAME "netkit"
19 /* Needed in fast-path */
20 struct net_device __rcu *peer;
21 struct bpf_mprog_entry __rcu *active;
22 enum netkit_action policy;
23 enum netkit_scrub scrub;
24 struct bpf_mprog_bundle bundle;
26 /* Needed in slow-path */
27 enum netkit_mode mode;
34 struct net_device *dev;
38 static __always_inline int
39 netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
40 enum netkit_action ret)
42 const struct bpf_mprog_fp *fp;
43 const struct bpf_prog *prog;
45 bpf_mprog_foreach_prog(entry, fp, prog) {
46 bpf_compute_data_pointers(skb);
47 ret = bpf_prog_run(prog, skb);
48 if (ret != NETKIT_NEXT)
54 static void netkit_xnet(struct sk_buff *skb)
60 static void netkit_prep_forward(struct sk_buff *skb,
61 bool xnet, bool xnet_scrub)
63 skb_scrub_packet(skb, false);
64 nf_skip_egress(skb, true);
65 skb_reset_mac_header(skb);
69 skb_clear_tstamp(skb);
74 static struct netkit *netkit_priv(const struct net_device *dev)
76 return netdev_priv(dev);
79 static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev)
81 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
82 struct netkit *nk = netkit_priv(dev);
83 enum netkit_action ret = READ_ONCE(nk->policy);
84 netdev_tx_t ret_dev = NET_XMIT_SUCCESS;
85 const struct bpf_mprog_entry *entry;
86 struct net_device *peer;
89 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
91 peer = rcu_dereference(nk->peer);
92 if (unlikely(!peer || !(peer->flags & IFF_UP) ||
93 !pskb_may_pull(skb, ETH_HLEN) ||
94 skb_orphan_frags(skb, GFP_ATOMIC)))
96 netkit_prep_forward(skb, !net_eq(dev_net(dev), dev_net(peer)),
98 eth_skb_pkt_type(skb, peer);
100 entry = rcu_dereference(nk->active);
102 ret = netkit_run(entry, skb, ret);
106 eth_skb_pull_mac(skb);
107 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
108 if (likely(__netif_rx(skb) == NET_RX_SUCCESS)) {
109 dev_sw_netstats_tx_add(dev, 1, len);
110 dev_sw_netstats_rx_add(peer, len);
115 case NETKIT_REDIRECT:
116 dev_sw_netstats_tx_add(dev, 1, len);
117 skb_do_redirect(skb);
124 dev_core_stats_tx_dropped_inc(dev);
125 ret_dev = NET_XMIT_DROP;
129 bpf_net_ctx_clear(bpf_net_ctx);
133 static int netkit_open(struct net_device *dev)
135 struct netkit *nk = netkit_priv(dev);
136 struct net_device *peer = rtnl_dereference(nk->peer);
140 if (peer->flags & IFF_UP) {
141 netif_carrier_on(dev);
142 netif_carrier_on(peer);
147 static int netkit_close(struct net_device *dev)
149 struct netkit *nk = netkit_priv(dev);
150 struct net_device *peer = rtnl_dereference(nk->peer);
152 netif_carrier_off(dev);
154 netif_carrier_off(peer);
158 static int netkit_get_iflink(const struct net_device *dev)
160 struct netkit *nk = netkit_priv(dev);
161 struct net_device *peer;
165 peer = rcu_dereference(nk->peer);
167 iflink = READ_ONCE(peer->ifindex);
172 static void netkit_set_multicast(struct net_device *dev)
174 /* Nothing to do, we receive whatever gets pushed to us! */
177 static int netkit_set_macaddr(struct net_device *dev, void *sa)
179 struct netkit *nk = netkit_priv(dev);
181 if (nk->mode != NETKIT_L2)
184 return eth_mac_addr(dev, sa);
187 static void netkit_set_headroom(struct net_device *dev, int headroom)
189 struct netkit *nk = netkit_priv(dev), *nk2;
190 struct net_device *peer;
193 headroom = NET_SKB_PAD;
196 peer = rcu_dereference(nk->peer);
200 nk2 = netkit_priv(peer);
201 nk->headroom = headroom;
202 headroom = max(nk->headroom, nk2->headroom);
204 peer->needed_headroom = headroom;
205 dev->needed_headroom = headroom;
210 INDIRECT_CALLABLE_SCOPE struct net_device *netkit_peer_dev(struct net_device *dev)
212 return rcu_dereference(netkit_priv(dev)->peer);
215 static void netkit_get_stats(struct net_device *dev,
216 struct rtnl_link_stats64 *stats)
218 dev_fetch_sw_netstats(stats, dev->tstats);
219 stats->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
222 static void netkit_uninit(struct net_device *dev);
224 static const struct net_device_ops netkit_netdev_ops = {
225 .ndo_open = netkit_open,
226 .ndo_stop = netkit_close,
227 .ndo_start_xmit = netkit_xmit,
228 .ndo_set_rx_mode = netkit_set_multicast,
229 .ndo_set_rx_headroom = netkit_set_headroom,
230 .ndo_set_mac_address = netkit_set_macaddr,
231 .ndo_get_iflink = netkit_get_iflink,
232 .ndo_get_peer_dev = netkit_peer_dev,
233 .ndo_get_stats64 = netkit_get_stats,
234 .ndo_uninit = netkit_uninit,
235 .ndo_features_check = passthru_features_check,
238 static void netkit_get_drvinfo(struct net_device *dev,
239 struct ethtool_drvinfo *info)
241 strscpy(info->driver, DRV_NAME, sizeof(info->driver));
244 static const struct ethtool_ops netkit_ethtool_ops = {
245 .get_drvinfo = netkit_get_drvinfo,
248 static void netkit_setup(struct net_device *dev)
250 static const netdev_features_t netkit_features_hw_vlan =
251 NETIF_F_HW_VLAN_CTAG_TX |
252 NETIF_F_HW_VLAN_CTAG_RX |
253 NETIF_F_HW_VLAN_STAG_TX |
254 NETIF_F_HW_VLAN_STAG_RX;
255 static const netdev_features_t netkit_features =
256 netkit_features_hw_vlan |
263 NETIF_F_GSO_SOFTWARE |
264 NETIF_F_GSO_ENCAP_ALL;
267 dev->max_mtu = ETH_MAX_MTU;
268 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
270 dev->flags |= IFF_NOARP;
271 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
272 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
273 dev->priv_flags |= IFF_PHONY_HEADROOM;
274 dev->priv_flags |= IFF_NO_QUEUE;
275 dev->priv_flags |= IFF_DISABLE_NETPOLL;
278 dev->ethtool_ops = &netkit_ethtool_ops;
279 dev->netdev_ops = &netkit_netdev_ops;
281 dev->features |= netkit_features;
282 dev->hw_features = netkit_features;
283 dev->hw_enc_features = netkit_features;
284 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
285 dev->vlan_features = dev->features & ~netkit_features_hw_vlan;
287 dev->needs_free_netdev = true;
289 netif_set_tso_max_size(dev, GSO_MAX_SIZE);
292 static struct net *netkit_get_link_net(const struct net_device *dev)
294 struct netkit *nk = netkit_priv(dev);
295 struct net_device *peer = rtnl_dereference(nk->peer);
297 return peer ? dev_net(peer) : dev_net(dev);
300 static int netkit_check_policy(int policy, struct nlattr *tb,
301 struct netlink_ext_ack *extack)
308 NL_SET_ERR_MSG_ATTR(extack, tb,
309 "Provided default xmit policy not supported");
314 static int netkit_validate(struct nlattr *tb[], struct nlattr *data[],
315 struct netlink_ext_ack *extack)
317 struct nlattr *attr = tb[IFLA_ADDRESS];
321 if (nla_len(attr) != ETH_ALEN)
323 if (!is_valid_ether_addr(nla_data(attr)))
324 return -EADDRNOTAVAIL;
328 static struct rtnl_link_ops netkit_link_ops;
330 static int netkit_new_link(struct net *peer_net, struct net_device *dev,
331 struct nlattr *tb[], struct nlattr *data[],
332 struct netlink_ext_ack *extack)
334 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp = tb, *attr;
335 enum netkit_action policy_prim = NETKIT_PASS;
336 enum netkit_action policy_peer = NETKIT_PASS;
337 enum netkit_scrub scrub_prim = NETKIT_SCRUB_DEFAULT;
338 enum netkit_scrub scrub_peer = NETKIT_SCRUB_DEFAULT;
339 enum netkit_mode mode = NETKIT_L3;
340 unsigned char ifname_assign_type;
341 struct ifinfomsg *ifmp = NULL;
342 struct net_device *peer;
343 char ifname[IFNAMSIZ];
348 if (data[IFLA_NETKIT_MODE])
349 mode = nla_get_u32(data[IFLA_NETKIT_MODE]);
350 if (data[IFLA_NETKIT_PEER_INFO]) {
351 attr = data[IFLA_NETKIT_PEER_INFO];
352 ifmp = nla_data(attr);
353 rtnl_nla_parse_ifinfomsg(peer_tb, attr, extack);
356 if (data[IFLA_NETKIT_SCRUB])
357 scrub_prim = nla_get_u32(data[IFLA_NETKIT_SCRUB]);
358 if (data[IFLA_NETKIT_PEER_SCRUB])
359 scrub_peer = nla_get_u32(data[IFLA_NETKIT_PEER_SCRUB]);
360 if (data[IFLA_NETKIT_POLICY]) {
361 attr = data[IFLA_NETKIT_POLICY];
362 policy_prim = nla_get_u32(attr);
363 err = netkit_check_policy(policy_prim, attr, extack);
367 if (data[IFLA_NETKIT_PEER_POLICY]) {
368 attr = data[IFLA_NETKIT_PEER_POLICY];
369 policy_peer = nla_get_u32(attr);
370 err = netkit_check_policy(policy_peer, attr, extack);
376 if (ifmp && tbp[IFLA_IFNAME]) {
377 nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
378 ifname_assign_type = NET_NAME_USER;
380 strscpy(ifname, "nk%d", IFNAMSIZ);
381 ifname_assign_type = NET_NAME_ENUM;
383 if (mode != NETKIT_L2 &&
384 (tb[IFLA_ADDRESS] || tbp[IFLA_ADDRESS]))
387 peer = rtnl_create_link(peer_net, ifname, ifname_assign_type,
388 &netkit_link_ops, tbp, extack);
390 return PTR_ERR(peer);
392 netif_inherit_tso_max(peer, dev);
394 if (mode == NETKIT_L2 && !(ifmp && tbp[IFLA_ADDRESS]))
395 eth_hw_addr_random(peer);
396 if (ifmp && dev->ifindex)
397 peer->ifindex = ifmp->ifi_index;
399 nk = netkit_priv(peer);
401 nk->policy = policy_peer;
402 nk->scrub = scrub_peer;
404 bpf_mprog_bundle_init(&nk->bundle);
406 err = register_netdevice(peer);
408 goto err_register_peer;
409 netif_carrier_off(peer);
410 if (mode == NETKIT_L2)
411 dev_change_flags(peer, peer->flags & ~IFF_NOARP, NULL);
413 err = rtnl_configure_link(peer, NULL, 0, NULL);
415 goto err_configure_peer;
417 if (mode == NETKIT_L2 && !tb[IFLA_ADDRESS])
418 eth_hw_addr_random(dev);
420 nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
422 strscpy(dev->name, "nk%d", IFNAMSIZ);
424 nk = netkit_priv(dev);
426 nk->policy = policy_prim;
427 nk->scrub = scrub_prim;
429 bpf_mprog_bundle_init(&nk->bundle);
431 err = register_netdevice(dev);
433 goto err_configure_peer;
434 netif_carrier_off(dev);
435 if (mode == NETKIT_L2)
436 dev_change_flags(dev, dev->flags & ~IFF_NOARP, NULL);
438 rcu_assign_pointer(netkit_priv(dev)->peer, peer);
439 rcu_assign_pointer(netkit_priv(peer)->peer, dev);
442 unregister_netdevice(peer);
449 static struct bpf_mprog_entry *netkit_entry_fetch(struct net_device *dev,
450 bool bundle_fallback)
452 struct netkit *nk = netkit_priv(dev);
453 struct bpf_mprog_entry *entry;
456 entry = rcu_dereference_rtnl(nk->active);
460 return &nk->bundle.a;
464 static void netkit_entry_update(struct net_device *dev,
465 struct bpf_mprog_entry *entry)
467 struct netkit *nk = netkit_priv(dev);
470 rcu_assign_pointer(nk->active, entry);
473 static void netkit_entry_sync(void)
478 static struct net_device *netkit_dev_fetch(struct net *net, u32 ifindex, u32 which)
480 struct net_device *dev;
486 case BPF_NETKIT_PRIMARY:
487 case BPF_NETKIT_PEER:
490 return ERR_PTR(-EINVAL);
493 dev = __dev_get_by_index(net, ifindex);
495 return ERR_PTR(-ENODEV);
496 if (dev->netdev_ops != &netkit_netdev_ops)
497 return ERR_PTR(-ENXIO);
499 nk = netkit_priv(dev);
501 return ERR_PTR(-EACCES);
502 if (which == BPF_NETKIT_PEER) {
503 dev = rcu_dereference_rtnl(nk->peer);
505 return ERR_PTR(-ENODEV);
510 int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
512 struct bpf_mprog_entry *entry, *entry_new;
513 struct bpf_prog *replace_prog = NULL;
514 struct net_device *dev;
518 dev = netkit_dev_fetch(current->nsproxy->net_ns, attr->target_ifindex,
524 entry = netkit_entry_fetch(dev, true);
525 if (attr->attach_flags & BPF_F_REPLACE) {
526 replace_prog = bpf_prog_get_type(attr->replace_bpf_fd,
528 if (IS_ERR(replace_prog)) {
529 ret = PTR_ERR(replace_prog);
534 ret = bpf_mprog_attach(entry, &entry_new, prog, NULL, replace_prog,
535 attr->attach_flags, attr->relative_fd,
536 attr->expected_revision);
538 if (entry != entry_new) {
539 netkit_entry_update(dev, entry_new);
542 bpf_mprog_commit(entry);
546 bpf_prog_put(replace_prog);
551 int netkit_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog)
553 struct bpf_mprog_entry *entry, *entry_new;
554 struct net_device *dev;
558 dev = netkit_dev_fetch(current->nsproxy->net_ns, attr->target_ifindex,
564 entry = netkit_entry_fetch(dev, false);
569 ret = bpf_mprog_detach(entry, &entry_new, prog, NULL, attr->attach_flags,
570 attr->relative_fd, attr->expected_revision);
572 if (!bpf_mprog_total(entry_new))
574 netkit_entry_update(dev, entry_new);
576 bpf_mprog_commit(entry);
583 int netkit_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
585 struct net_device *dev;
589 dev = netkit_dev_fetch(current->nsproxy->net_ns,
590 attr->query.target_ifindex,
591 attr->query.attach_type);
596 ret = bpf_mprog_query(attr, uattr, netkit_entry_fetch(dev, false));
602 static struct netkit_link *netkit_link(const struct bpf_link *link)
604 return container_of(link, struct netkit_link, link);
607 static int netkit_link_prog_attach(struct bpf_link *link, u32 flags,
608 u32 id_or_fd, u64 revision)
610 struct netkit_link *nkl = netkit_link(link);
611 struct bpf_mprog_entry *entry, *entry_new;
612 struct net_device *dev = nkl->dev;
616 entry = netkit_entry_fetch(dev, true);
617 ret = bpf_mprog_attach(entry, &entry_new, link->prog, link, NULL, flags,
620 if (entry != entry_new) {
621 netkit_entry_update(dev, entry_new);
624 bpf_mprog_commit(entry);
629 static void netkit_link_release(struct bpf_link *link)
631 struct netkit_link *nkl = netkit_link(link);
632 struct bpf_mprog_entry *entry, *entry_new;
633 struct net_device *dev;
640 entry = netkit_entry_fetch(dev, false);
645 ret = bpf_mprog_detach(entry, &entry_new, link->prog, link, 0, 0, 0);
647 if (!bpf_mprog_total(entry_new))
649 netkit_entry_update(dev, entry_new);
651 bpf_mprog_commit(entry);
659 static int netkit_link_update(struct bpf_link *link, struct bpf_prog *nprog,
660 struct bpf_prog *oprog)
662 struct netkit_link *nkl = netkit_link(link);
663 struct bpf_mprog_entry *entry, *entry_new;
664 struct net_device *dev;
673 if (oprog && link->prog != oprog) {
678 if (oprog == nprog) {
682 entry = netkit_entry_fetch(dev, false);
687 ret = bpf_mprog_attach(entry, &entry_new, nprog, link, oprog,
688 BPF_F_REPLACE | BPF_F_ID,
689 link->prog->aux->id, 0);
691 WARN_ON_ONCE(entry != entry_new);
692 oprog = xchg(&link->prog, nprog);
694 bpf_mprog_commit(entry);
701 static void netkit_link_dealloc(struct bpf_link *link)
703 kfree(netkit_link(link));
706 static void netkit_link_fdinfo(const struct bpf_link *link, struct seq_file *seq)
708 const struct netkit_link *nkl = netkit_link(link);
713 ifindex = nkl->dev->ifindex;
716 seq_printf(seq, "ifindex:\t%u\n", ifindex);
717 seq_printf(seq, "attach_type:\t%u (%s)\n",
719 nkl->location == BPF_NETKIT_PRIMARY ? "primary" : "peer");
722 static int netkit_link_fill_info(const struct bpf_link *link,
723 struct bpf_link_info *info)
725 const struct netkit_link *nkl = netkit_link(link);
730 ifindex = nkl->dev->ifindex;
733 info->netkit.ifindex = ifindex;
734 info->netkit.attach_type = nkl->location;
738 static int netkit_link_detach(struct bpf_link *link)
740 netkit_link_release(link);
744 static const struct bpf_link_ops netkit_link_lops = {
745 .release = netkit_link_release,
746 .detach = netkit_link_detach,
747 .dealloc = netkit_link_dealloc,
748 .update_prog = netkit_link_update,
749 .show_fdinfo = netkit_link_fdinfo,
750 .fill_link_info = netkit_link_fill_info,
753 static int netkit_link_init(struct netkit_link *nkl,
754 struct bpf_link_primer *link_primer,
755 const union bpf_attr *attr,
756 struct net_device *dev,
757 struct bpf_prog *prog)
759 bpf_link_init(&nkl->link, BPF_LINK_TYPE_NETKIT,
760 &netkit_link_lops, prog);
761 nkl->location = attr->link_create.attach_type;
763 return bpf_link_prime(&nkl->link, link_primer);
766 int netkit_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
768 struct bpf_link_primer link_primer;
769 struct netkit_link *nkl;
770 struct net_device *dev;
774 dev = netkit_dev_fetch(current->nsproxy->net_ns,
775 attr->link_create.target_ifindex,
776 attr->link_create.attach_type);
781 nkl = kzalloc(sizeof(*nkl), GFP_KERNEL_ACCOUNT);
786 ret = netkit_link_init(nkl, &link_primer, attr, dev, prog);
791 ret = netkit_link_prog_attach(&nkl->link,
792 attr->link_create.flags,
793 attr->link_create.netkit.relative_fd,
794 attr->link_create.netkit.expected_revision);
797 bpf_link_cleanup(&link_primer);
800 ret = bpf_link_settle(&link_primer);
806 static void netkit_release_all(struct net_device *dev)
808 struct bpf_mprog_entry *entry;
809 struct bpf_tuple tuple = {};
810 struct bpf_mprog_fp *fp;
811 struct bpf_mprog_cp *cp;
813 entry = netkit_entry_fetch(dev, false);
816 netkit_entry_update(dev, NULL);
818 bpf_mprog_foreach_tuple(entry, fp, cp, tuple) {
820 netkit_link(tuple.link)->dev = NULL;
822 bpf_prog_put(tuple.prog);
826 static void netkit_uninit(struct net_device *dev)
828 netkit_release_all(dev);
831 static void netkit_del_link(struct net_device *dev, struct list_head *head)
833 struct netkit *nk = netkit_priv(dev);
834 struct net_device *peer = rtnl_dereference(nk->peer);
836 RCU_INIT_POINTER(nk->peer, NULL);
837 unregister_netdevice_queue(dev, head);
839 nk = netkit_priv(peer);
840 RCU_INIT_POINTER(nk->peer, NULL);
841 unregister_netdevice_queue(peer, head);
845 static int netkit_change_link(struct net_device *dev, struct nlattr *tb[],
846 struct nlattr *data[],
847 struct netlink_ext_ack *extack)
849 struct netkit *nk = netkit_priv(dev);
850 struct net_device *peer = rtnl_dereference(nk->peer);
851 enum netkit_action policy;
856 NL_SET_ERR_MSG(extack,
857 "netkit link settings can be changed only through the primary device");
861 if (data[IFLA_NETKIT_MODE]) {
862 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_NETKIT_MODE],
863 "netkit link operating mode cannot be changed after device creation");
867 if (data[IFLA_NETKIT_SCRUB]) {
868 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_NETKIT_SCRUB],
869 "netkit scrubbing cannot be changed after device creation");
873 if (data[IFLA_NETKIT_PEER_SCRUB]) {
874 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_NETKIT_PEER_SCRUB],
875 "netkit scrubbing cannot be changed after device creation");
879 if (data[IFLA_NETKIT_PEER_INFO]) {
880 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_NETKIT_PEER_INFO],
881 "netkit peer info cannot be changed after device creation");
885 if (data[IFLA_NETKIT_POLICY]) {
886 attr = data[IFLA_NETKIT_POLICY];
887 policy = nla_get_u32(attr);
888 err = netkit_check_policy(policy, attr, extack);
891 WRITE_ONCE(nk->policy, policy);
894 if (data[IFLA_NETKIT_PEER_POLICY]) {
896 attr = data[IFLA_NETKIT_PEER_POLICY];
897 policy = nla_get_u32(attr);
899 err = netkit_check_policy(policy, attr, extack);
902 nk = netkit_priv(peer);
903 WRITE_ONCE(nk->policy, policy);
909 static size_t netkit_get_size(const struct net_device *dev)
911 return nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_POLICY */
912 nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_PEER_POLICY */
913 nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_SCRUB */
914 nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_PEER_SCRUB */
915 nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_MODE */
916 nla_total_size(sizeof(u8)) + /* IFLA_NETKIT_PRIMARY */
920 static int netkit_fill_info(struct sk_buff *skb, const struct net_device *dev)
922 struct netkit *nk = netkit_priv(dev);
923 struct net_device *peer = rtnl_dereference(nk->peer);
925 if (nla_put_u8(skb, IFLA_NETKIT_PRIMARY, nk->primary))
927 if (nla_put_u32(skb, IFLA_NETKIT_POLICY, nk->policy))
929 if (nla_put_u32(skb, IFLA_NETKIT_MODE, nk->mode))
931 if (nla_put_u32(skb, IFLA_NETKIT_SCRUB, nk->scrub))
935 nk = netkit_priv(peer);
936 if (nla_put_u32(skb, IFLA_NETKIT_PEER_POLICY, nk->policy))
938 if (nla_put_u32(skb, IFLA_NETKIT_PEER_SCRUB, nk->scrub))
945 static const struct nla_policy netkit_policy[IFLA_NETKIT_MAX + 1] = {
946 [IFLA_NETKIT_PEER_INFO] = { .len = sizeof(struct ifinfomsg) },
947 [IFLA_NETKIT_MODE] = NLA_POLICY_MAX(NLA_U32, NETKIT_L3),
948 [IFLA_NETKIT_POLICY] = { .type = NLA_U32 },
949 [IFLA_NETKIT_PEER_POLICY] = { .type = NLA_U32 },
950 [IFLA_NETKIT_SCRUB] = NLA_POLICY_MAX(NLA_U32, NETKIT_SCRUB_DEFAULT),
951 [IFLA_NETKIT_PEER_SCRUB] = NLA_POLICY_MAX(NLA_U32, NETKIT_SCRUB_DEFAULT),
952 [IFLA_NETKIT_PRIMARY] = { .type = NLA_REJECT,
953 .reject_message = "Primary attribute is read-only" },
956 static struct rtnl_link_ops netkit_link_ops = {
958 .priv_size = sizeof(struct netkit),
959 .setup = netkit_setup,
960 .newlink = netkit_new_link,
961 .dellink = netkit_del_link,
962 .changelink = netkit_change_link,
963 .get_link_net = netkit_get_link_net,
964 .get_size = netkit_get_size,
965 .fill_info = netkit_fill_info,
966 .policy = netkit_policy,
967 .validate = netkit_validate,
968 .peer_type = IFLA_NETKIT_PEER_INFO,
969 .maxtype = IFLA_NETKIT_MAX,
972 static __init int netkit_init(void)
974 BUILD_BUG_ON((int)NETKIT_NEXT != (int)TCX_NEXT ||
975 (int)NETKIT_PASS != (int)TCX_PASS ||
976 (int)NETKIT_DROP != (int)TCX_DROP ||
977 (int)NETKIT_REDIRECT != (int)TCX_REDIRECT);
979 return rtnl_link_register(&netkit_link_ops);
982 static __exit void netkit_exit(void)
984 rtnl_link_unregister(&netkit_link_ops);
987 module_init(netkit_init);
988 module_exit(netkit_exit);
990 MODULE_DESCRIPTION("BPF-programmable network device");
993 MODULE_LICENSE("GPL");
994 MODULE_ALIAS_RTNL_LINK(DRV_NAME);