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
*src_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
];
349 if (data
[IFLA_NETKIT_MODE
])
350 mode
= nla_get_u32(data
[IFLA_NETKIT_MODE
]);
351 if (data
[IFLA_NETKIT_PEER_INFO
]) {
352 attr
= data
[IFLA_NETKIT_PEER_INFO
];
353 ifmp
= nla_data(attr
);
354 rtnl_nla_parse_ifinfomsg(peer_tb
, attr
, extack
);
357 if (data
[IFLA_NETKIT_SCRUB
])
358 scrub_prim
= nla_get_u32(data
[IFLA_NETKIT_SCRUB
]);
359 if (data
[IFLA_NETKIT_PEER_SCRUB
])
360 scrub_peer
= nla_get_u32(data
[IFLA_NETKIT_PEER_SCRUB
]);
361 if (data
[IFLA_NETKIT_POLICY
]) {
362 attr
= data
[IFLA_NETKIT_POLICY
];
363 policy_prim
= nla_get_u32(attr
);
364 err
= netkit_check_policy(policy_prim
, attr
, extack
);
368 if (data
[IFLA_NETKIT_PEER_POLICY
]) {
369 attr
= data
[IFLA_NETKIT_PEER_POLICY
];
370 policy_peer
= nla_get_u32(attr
);
371 err
= netkit_check_policy(policy_peer
, attr
, extack
);
377 if (ifmp
&& tbp
[IFLA_IFNAME
]) {
378 nla_strscpy(ifname
, tbp
[IFLA_IFNAME
], IFNAMSIZ
);
379 ifname_assign_type
= NET_NAME_USER
;
381 strscpy(ifname
, "nk%d", IFNAMSIZ
);
382 ifname_assign_type
= NET_NAME_ENUM
;
384 if (mode
!= NETKIT_L2
&&
385 (tb
[IFLA_ADDRESS
] || tbp
[IFLA_ADDRESS
]))
388 net
= rtnl_link_get_net(src_net
, tbp
);
389 peer
= rtnl_create_link(net
, ifname
, ifname_assign_type
,
390 &netkit_link_ops
, tbp
, extack
);
393 return PTR_ERR(peer
);
396 netif_inherit_tso_max(peer
, dev
);
398 if (mode
== NETKIT_L2
&& !(ifmp
&& tbp
[IFLA_ADDRESS
]))
399 eth_hw_addr_random(peer
);
400 if (ifmp
&& dev
->ifindex
)
401 peer
->ifindex
= ifmp
->ifi_index
;
403 nk
= netkit_priv(peer
);
405 nk
->policy
= policy_peer
;
406 nk
->scrub
= scrub_peer
;
408 bpf_mprog_bundle_init(&nk
->bundle
);
410 err
= register_netdevice(peer
);
413 goto err_register_peer
;
414 netif_carrier_off(peer
);
415 if (mode
== NETKIT_L2
)
416 dev_change_flags(peer
, peer
->flags
& ~IFF_NOARP
, NULL
);
418 err
= rtnl_configure_link(peer
, NULL
, 0, NULL
);
420 goto err_configure_peer
;
422 if (mode
== NETKIT_L2
&& !tb
[IFLA_ADDRESS
])
423 eth_hw_addr_random(dev
);
425 nla_strscpy(dev
->name
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
427 strscpy(dev
->name
, "nk%d", IFNAMSIZ
);
429 nk
= netkit_priv(dev
);
431 nk
->policy
= policy_prim
;
432 nk
->scrub
= scrub_prim
;
434 bpf_mprog_bundle_init(&nk
->bundle
);
436 err
= register_netdevice(dev
);
438 goto err_configure_peer
;
439 netif_carrier_off(dev
);
440 if (mode
== NETKIT_L2
)
441 dev_change_flags(dev
, dev
->flags
& ~IFF_NOARP
, NULL
);
443 rcu_assign_pointer(netkit_priv(dev
)->peer
, peer
);
444 rcu_assign_pointer(netkit_priv(peer
)->peer
, dev
);
447 unregister_netdevice(peer
);
454 static struct bpf_mprog_entry
*netkit_entry_fetch(struct net_device
*dev
,
455 bool bundle_fallback
)
457 struct netkit
*nk
= netkit_priv(dev
);
458 struct bpf_mprog_entry
*entry
;
461 entry
= rcu_dereference_rtnl(nk
->active
);
465 return &nk
->bundle
.a
;
469 static void netkit_entry_update(struct net_device
*dev
,
470 struct bpf_mprog_entry
*entry
)
472 struct netkit
*nk
= netkit_priv(dev
);
475 rcu_assign_pointer(nk
->active
, entry
);
478 static void netkit_entry_sync(void)
483 static struct net_device
*netkit_dev_fetch(struct net
*net
, u32 ifindex
, u32 which
)
485 struct net_device
*dev
;
491 case BPF_NETKIT_PRIMARY
:
492 case BPF_NETKIT_PEER
:
495 return ERR_PTR(-EINVAL
);
498 dev
= __dev_get_by_index(net
, ifindex
);
500 return ERR_PTR(-ENODEV
);
501 if (dev
->netdev_ops
!= &netkit_netdev_ops
)
502 return ERR_PTR(-ENXIO
);
504 nk
= netkit_priv(dev
);
506 return ERR_PTR(-EACCES
);
507 if (which
== BPF_NETKIT_PEER
) {
508 dev
= rcu_dereference_rtnl(nk
->peer
);
510 return ERR_PTR(-ENODEV
);
515 int netkit_prog_attach(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
517 struct bpf_mprog_entry
*entry
, *entry_new
;
518 struct bpf_prog
*replace_prog
= NULL
;
519 struct net_device
*dev
;
523 dev
= netkit_dev_fetch(current
->nsproxy
->net_ns
, attr
->target_ifindex
,
529 entry
= netkit_entry_fetch(dev
, true);
530 if (attr
->attach_flags
& BPF_F_REPLACE
) {
531 replace_prog
= bpf_prog_get_type(attr
->replace_bpf_fd
,
533 if (IS_ERR(replace_prog
)) {
534 ret
= PTR_ERR(replace_prog
);
539 ret
= bpf_mprog_attach(entry
, &entry_new
, prog
, NULL
, replace_prog
,
540 attr
->attach_flags
, attr
->relative_fd
,
541 attr
->expected_revision
);
543 if (entry
!= entry_new
) {
544 netkit_entry_update(dev
, entry_new
);
547 bpf_mprog_commit(entry
);
551 bpf_prog_put(replace_prog
);
556 int netkit_prog_detach(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
558 struct bpf_mprog_entry
*entry
, *entry_new
;
559 struct net_device
*dev
;
563 dev
= netkit_dev_fetch(current
->nsproxy
->net_ns
, attr
->target_ifindex
,
569 entry
= netkit_entry_fetch(dev
, false);
574 ret
= bpf_mprog_detach(entry
, &entry_new
, prog
, NULL
, attr
->attach_flags
,
575 attr
->relative_fd
, attr
->expected_revision
);
577 if (!bpf_mprog_total(entry_new
))
579 netkit_entry_update(dev
, entry_new
);
581 bpf_mprog_commit(entry
);
588 int netkit_prog_query(const union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
590 struct net_device
*dev
;
594 dev
= netkit_dev_fetch(current
->nsproxy
->net_ns
,
595 attr
->query
.target_ifindex
,
596 attr
->query
.attach_type
);
601 ret
= bpf_mprog_query(attr
, uattr
, netkit_entry_fetch(dev
, false));
607 static struct netkit_link
*netkit_link(const struct bpf_link
*link
)
609 return container_of(link
, struct netkit_link
, link
);
612 static int netkit_link_prog_attach(struct bpf_link
*link
, u32 flags
,
613 u32 id_or_fd
, u64 revision
)
615 struct netkit_link
*nkl
= netkit_link(link
);
616 struct bpf_mprog_entry
*entry
, *entry_new
;
617 struct net_device
*dev
= nkl
->dev
;
621 entry
= netkit_entry_fetch(dev
, true);
622 ret
= bpf_mprog_attach(entry
, &entry_new
, link
->prog
, link
, NULL
, flags
,
625 if (entry
!= entry_new
) {
626 netkit_entry_update(dev
, entry_new
);
629 bpf_mprog_commit(entry
);
634 static void netkit_link_release(struct bpf_link
*link
)
636 struct netkit_link
*nkl
= netkit_link(link
);
637 struct bpf_mprog_entry
*entry
, *entry_new
;
638 struct net_device
*dev
;
645 entry
= netkit_entry_fetch(dev
, false);
650 ret
= bpf_mprog_detach(entry
, &entry_new
, link
->prog
, link
, 0, 0, 0);
652 if (!bpf_mprog_total(entry_new
))
654 netkit_entry_update(dev
, entry_new
);
656 bpf_mprog_commit(entry
);
664 static int netkit_link_update(struct bpf_link
*link
, struct bpf_prog
*nprog
,
665 struct bpf_prog
*oprog
)
667 struct netkit_link
*nkl
= netkit_link(link
);
668 struct bpf_mprog_entry
*entry
, *entry_new
;
669 struct net_device
*dev
;
678 if (oprog
&& link
->prog
!= oprog
) {
683 if (oprog
== nprog
) {
687 entry
= netkit_entry_fetch(dev
, false);
692 ret
= bpf_mprog_attach(entry
, &entry_new
, nprog
, link
, oprog
,
693 BPF_F_REPLACE
| BPF_F_ID
,
694 link
->prog
->aux
->id
, 0);
696 WARN_ON_ONCE(entry
!= entry_new
);
697 oprog
= xchg(&link
->prog
, nprog
);
699 bpf_mprog_commit(entry
);
706 static void netkit_link_dealloc(struct bpf_link
*link
)
708 kfree(netkit_link(link
));
711 static void netkit_link_fdinfo(const struct bpf_link
*link
, struct seq_file
*seq
)
713 const struct netkit_link
*nkl
= netkit_link(link
);
718 ifindex
= nkl
->dev
->ifindex
;
721 seq_printf(seq
, "ifindex:\t%u\n", ifindex
);
722 seq_printf(seq
, "attach_type:\t%u (%s)\n",
724 nkl
->location
== BPF_NETKIT_PRIMARY
? "primary" : "peer");
727 static int netkit_link_fill_info(const struct bpf_link
*link
,
728 struct bpf_link_info
*info
)
730 const struct netkit_link
*nkl
= netkit_link(link
);
735 ifindex
= nkl
->dev
->ifindex
;
738 info
->netkit
.ifindex
= ifindex
;
739 info
->netkit
.attach_type
= nkl
->location
;
743 static int netkit_link_detach(struct bpf_link
*link
)
745 netkit_link_release(link
);
749 static const struct bpf_link_ops netkit_link_lops
= {
750 .release
= netkit_link_release
,
751 .detach
= netkit_link_detach
,
752 .dealloc
= netkit_link_dealloc
,
753 .update_prog
= netkit_link_update
,
754 .show_fdinfo
= netkit_link_fdinfo
,
755 .fill_link_info
= netkit_link_fill_info
,
758 static int netkit_link_init(struct netkit_link
*nkl
,
759 struct bpf_link_primer
*link_primer
,
760 const union bpf_attr
*attr
,
761 struct net_device
*dev
,
762 struct bpf_prog
*prog
)
764 bpf_link_init(&nkl
->link
, BPF_LINK_TYPE_NETKIT
,
765 &netkit_link_lops
, prog
);
766 nkl
->location
= attr
->link_create
.attach_type
;
768 return bpf_link_prime(&nkl
->link
, link_primer
);
771 int netkit_link_attach(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
773 struct bpf_link_primer link_primer
;
774 struct netkit_link
*nkl
;
775 struct net_device
*dev
;
779 dev
= netkit_dev_fetch(current
->nsproxy
->net_ns
,
780 attr
->link_create
.target_ifindex
,
781 attr
->link_create
.attach_type
);
786 nkl
= kzalloc(sizeof(*nkl
), GFP_KERNEL_ACCOUNT
);
791 ret
= netkit_link_init(nkl
, &link_primer
, attr
, dev
, prog
);
796 ret
= netkit_link_prog_attach(&nkl
->link
,
797 attr
->link_create
.flags
,
798 attr
->link_create
.netkit
.relative_fd
,
799 attr
->link_create
.netkit
.expected_revision
);
802 bpf_link_cleanup(&link_primer
);
805 ret
= bpf_link_settle(&link_primer
);
811 static void netkit_release_all(struct net_device
*dev
)
813 struct bpf_mprog_entry
*entry
;
814 struct bpf_tuple tuple
= {};
815 struct bpf_mprog_fp
*fp
;
816 struct bpf_mprog_cp
*cp
;
818 entry
= netkit_entry_fetch(dev
, false);
821 netkit_entry_update(dev
, NULL
);
823 bpf_mprog_foreach_tuple(entry
, fp
, cp
, tuple
) {
825 netkit_link(tuple
.link
)->dev
= NULL
;
827 bpf_prog_put(tuple
.prog
);
831 static void netkit_uninit(struct net_device
*dev
)
833 netkit_release_all(dev
);
836 static void netkit_del_link(struct net_device
*dev
, struct list_head
*head
)
838 struct netkit
*nk
= netkit_priv(dev
);
839 struct net_device
*peer
= rtnl_dereference(nk
->peer
);
841 RCU_INIT_POINTER(nk
->peer
, NULL
);
842 unregister_netdevice_queue(dev
, head
);
844 nk
= netkit_priv(peer
);
845 RCU_INIT_POINTER(nk
->peer
, NULL
);
846 unregister_netdevice_queue(peer
, head
);
850 static int netkit_change_link(struct net_device
*dev
, struct nlattr
*tb
[],
851 struct nlattr
*data
[],
852 struct netlink_ext_ack
*extack
)
854 struct netkit
*nk
= netkit_priv(dev
);
855 struct net_device
*peer
= rtnl_dereference(nk
->peer
);
856 enum netkit_action policy
;
861 NL_SET_ERR_MSG(extack
,
862 "netkit link settings can be changed only through the primary device");
866 if (data
[IFLA_NETKIT_MODE
]) {
867 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_NETKIT_MODE
],
868 "netkit link operating mode cannot be changed after device creation");
872 if (data
[IFLA_NETKIT_SCRUB
]) {
873 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_NETKIT_SCRUB
],
874 "netkit scrubbing cannot be changed after device creation");
878 if (data
[IFLA_NETKIT_PEER_SCRUB
]) {
879 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_NETKIT_PEER_SCRUB
],
880 "netkit scrubbing cannot be changed after device creation");
884 if (data
[IFLA_NETKIT_PEER_INFO
]) {
885 NL_SET_ERR_MSG_ATTR(extack
, data
[IFLA_NETKIT_PEER_INFO
],
886 "netkit peer info cannot be changed after device creation");
890 if (data
[IFLA_NETKIT_POLICY
]) {
891 attr
= data
[IFLA_NETKIT_POLICY
];
892 policy
= nla_get_u32(attr
);
893 err
= netkit_check_policy(policy
, attr
, extack
);
896 WRITE_ONCE(nk
->policy
, policy
);
899 if (data
[IFLA_NETKIT_PEER_POLICY
]) {
901 attr
= data
[IFLA_NETKIT_PEER_POLICY
];
902 policy
= nla_get_u32(attr
);
904 err
= netkit_check_policy(policy
, attr
, extack
);
907 nk
= netkit_priv(peer
);
908 WRITE_ONCE(nk
->policy
, policy
);
914 static size_t netkit_get_size(const struct net_device
*dev
)
916 return nla_total_size(sizeof(u32
)) + /* IFLA_NETKIT_POLICY */
917 nla_total_size(sizeof(u32
)) + /* IFLA_NETKIT_PEER_POLICY */
918 nla_total_size(sizeof(u32
)) + /* IFLA_NETKIT_SCRUB */
919 nla_total_size(sizeof(u32
)) + /* IFLA_NETKIT_PEER_SCRUB */
920 nla_total_size(sizeof(u32
)) + /* IFLA_NETKIT_MODE */
921 nla_total_size(sizeof(u8
)) + /* IFLA_NETKIT_PRIMARY */
925 static int netkit_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
927 struct netkit
*nk
= netkit_priv(dev
);
928 struct net_device
*peer
= rtnl_dereference(nk
->peer
);
930 if (nla_put_u8(skb
, IFLA_NETKIT_PRIMARY
, nk
->primary
))
932 if (nla_put_u32(skb
, IFLA_NETKIT_POLICY
, nk
->policy
))
934 if (nla_put_u32(skb
, IFLA_NETKIT_MODE
, nk
->mode
))
936 if (nla_put_u32(skb
, IFLA_NETKIT_SCRUB
, nk
->scrub
))
940 nk
= netkit_priv(peer
);
941 if (nla_put_u32(skb
, IFLA_NETKIT_PEER_POLICY
, nk
->policy
))
943 if (nla_put_u32(skb
, IFLA_NETKIT_PEER_SCRUB
, nk
->scrub
))
950 static const struct nla_policy netkit_policy
[IFLA_NETKIT_MAX
+ 1] = {
951 [IFLA_NETKIT_PEER_INFO
] = { .len
= sizeof(struct ifinfomsg
) },
952 [IFLA_NETKIT_MODE
] = NLA_POLICY_MAX(NLA_U32
, NETKIT_L3
),
953 [IFLA_NETKIT_POLICY
] = { .type
= NLA_U32
},
954 [IFLA_NETKIT_PEER_POLICY
] = { .type
= NLA_U32
},
955 [IFLA_NETKIT_SCRUB
] = NLA_POLICY_MAX(NLA_U32
, NETKIT_SCRUB_DEFAULT
),
956 [IFLA_NETKIT_PEER_SCRUB
] = NLA_POLICY_MAX(NLA_U32
, NETKIT_SCRUB_DEFAULT
),
957 [IFLA_NETKIT_PRIMARY
] = { .type
= NLA_REJECT
,
958 .reject_message
= "Primary attribute is read-only" },
961 static struct rtnl_link_ops netkit_link_ops
= {
963 .priv_size
= sizeof(struct netkit
),
964 .setup
= netkit_setup
,
965 .newlink
= netkit_new_link
,
966 .dellink
= netkit_del_link
,
967 .changelink
= netkit_change_link
,
968 .get_link_net
= netkit_get_link_net
,
969 .get_size
= netkit_get_size
,
970 .fill_info
= netkit_fill_info
,
971 .policy
= netkit_policy
,
972 .validate
= netkit_validate
,
973 .peer_type
= IFLA_NETKIT_PEER_INFO
,
974 .maxtype
= IFLA_NETKIT_MAX
,
977 static __init
int netkit_init(void)
979 BUILD_BUG_ON((int)NETKIT_NEXT
!= (int)TCX_NEXT
||
980 (int)NETKIT_PASS
!= (int)TCX_PASS
||
981 (int)NETKIT_DROP
!= (int)TCX_DROP
||
982 (int)NETKIT_REDIRECT
!= (int)TCX_REDIRECT
);
984 return rtnl_link_register(&netkit_link_ops
);
987 static __exit
void netkit_exit(void)
989 rtnl_link_unregister(&netkit_link_ops
);
992 module_init(netkit_init
);
993 module_exit(netkit_exit
);
995 MODULE_DESCRIPTION("BPF-programmable network device");
996 MODULE_AUTHOR("Daniel Borkmann <daniel@iogearbox.net>");
997 MODULE_AUTHOR("Nikolay Aleksandrov <razor@blackwall.org>");
998 MODULE_LICENSE("GPL");
999 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);