1 // SPDX-License-Identifier: GPL-2.0
3 * Management Component Transport Protocol (MCTP) - device implementation.
5 * Copyright (c) 2021 Code Construct
6 * Copyright (c) 2021 Google
9 #include <linux/if_arp.h>
10 #include <linux/if_link.h>
11 #include <linux/mctp.h>
12 #include <linux/netdevice.h>
13 #include <linux/rcupdate.h>
14 #include <linux/rtnetlink.h>
16 #include <net/addrconf.h>
17 #include <net/netlink.h>
19 #include <net/mctpdevice.h>
28 /* unlocked: caller must hold rcu_read_lock.
29 * Returned mctp_dev has its refcount incremented, or NULL if unset.
31 struct mctp_dev
*__mctp_dev_get(const struct net_device
*dev
)
33 struct mctp_dev
*mdev
= rcu_dereference(dev
->mctp_ptr
);
35 /* RCU guarantees that any mdev is still live.
36 * Zero refcount implies a pending free, return NULL.
39 if (!refcount_inc_not_zero(&mdev
->refs
))
44 /* Returned mctp_dev does not have refcount incremented. The returned pointer
45 * remains live while rtnl_lock is held, as that prevents mctp_unregister()
47 struct mctp_dev
*mctp_dev_get_rtnl(const struct net_device
*dev
)
49 return rtnl_dereference(dev
->mctp_ptr
);
52 static int mctp_addrinfo_size(void)
54 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
55 + nla_total_size(1) // IFA_LOCAL
56 + nla_total_size(1) // IFA_ADDRESS
60 /* flag should be NLM_F_MULTI for dump calls */
61 static int mctp_fill_addrinfo(struct sk_buff
*skb
,
62 struct mctp_dev
*mdev
, mctp_eid_t eid
,
63 int msg_type
, u32 portid
, u32 seq
, int flag
)
65 struct ifaddrmsg
*hdr
;
68 nlh
= nlmsg_put(skb
, portid
, seq
,
69 msg_type
, sizeof(*hdr
), flag
);
73 hdr
= nlmsg_data(nlh
);
74 hdr
->ifa_family
= AF_MCTP
;
75 hdr
->ifa_prefixlen
= 0;
78 hdr
->ifa_index
= mdev
->dev
->ifindex
;
80 if (nla_put_u8(skb
, IFA_LOCAL
, eid
))
83 if (nla_put_u8(skb
, IFA_ADDRESS
, eid
))
91 nlmsg_cancel(skb
, nlh
);
95 static int mctp_dump_dev_addrinfo(struct mctp_dev
*mdev
, struct sk_buff
*skb
,
96 struct netlink_callback
*cb
)
98 struct mctp_dump_cb
*mcb
= (void *)cb
->ctx
;
102 portid
= NETLINK_CB(cb
->skb
).portid
;
103 seq
= cb
->nlh
->nlmsg_seq
;
104 for (; mcb
->a_idx
< mdev
->num_addrs
; mcb
->a_idx
++) {
105 rc
= mctp_fill_addrinfo(skb
, mdev
, mdev
->addrs
[mcb
->a_idx
],
106 RTM_NEWADDR
, portid
, seq
, NLM_F_MULTI
);
114 static int mctp_dump_addrinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
116 struct mctp_dump_cb
*mcb
= (void *)cb
->ctx
;
117 struct net
*net
= sock_net(skb
->sk
);
118 struct hlist_head
*head
;
119 struct net_device
*dev
;
120 struct ifaddrmsg
*hdr
;
121 struct mctp_dev
*mdev
;
125 hdr
= nlmsg_data(cb
->nlh
);
126 // filter by ifindex if requested
127 ifindex
= hdr
->ifa_index
;
130 for (; mcb
->h
< NETDEV_HASHENTRIES
; mcb
->h
++, mcb
->idx
= 0) {
132 head
= &net
->dev_index_head
[mcb
->h
];
133 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
134 if (idx
>= mcb
->idx
&&
135 (ifindex
== 0 || ifindex
== dev
->ifindex
)) {
136 mdev
= __mctp_dev_get(dev
);
138 rc
= mctp_dump_dev_addrinfo(mdev
,
141 // Error indicates full buffer, this
142 // callback will get retried.
148 // reset for next iteration
159 static void mctp_addr_notify(struct mctp_dev
*mdev
, mctp_eid_t eid
, int msg_type
,
160 struct sk_buff
*req_skb
, struct nlmsghdr
*req_nlh
)
162 u32 portid
= NETLINK_CB(req_skb
).portid
;
163 struct net
*net
= dev_net(mdev
->dev
);
167 skb
= nlmsg_new(mctp_addrinfo_size(), GFP_KERNEL
);
171 rc
= mctp_fill_addrinfo(skb
, mdev
, eid
, msg_type
,
172 portid
, req_nlh
->nlmsg_seq
, 0);
174 WARN_ON_ONCE(rc
== -EMSGSIZE
);
178 rtnl_notify(skb
, net
, portid
, RTNLGRP_MCTP_IFADDR
, req_nlh
, GFP_KERNEL
);
182 rtnl_set_sk_err(net
, RTNLGRP_MCTP_IFADDR
, rc
);
185 static const struct nla_policy ifa_mctp_policy
[IFA_MAX
+ 1] = {
186 [IFA_ADDRESS
] = { .type
= NLA_U8
},
187 [IFA_LOCAL
] = { .type
= NLA_U8
},
190 static int mctp_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
191 struct netlink_ext_ack
*extack
)
193 struct net
*net
= sock_net(skb
->sk
);
194 struct nlattr
*tb
[IFA_MAX
+ 1];
195 struct net_device
*dev
;
196 struct mctp_addr
*addr
;
197 struct mctp_dev
*mdev
;
198 struct ifaddrmsg
*ifm
;
203 rc
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_mctp_policy
,
208 ifm
= nlmsg_data(nlh
);
211 addr
= nla_data(tb
[IFA_LOCAL
]);
212 else if (tb
[IFA_ADDRESS
])
213 addr
= nla_data(tb
[IFA_ADDRESS
]);
218 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
222 mdev
= mctp_dev_get_rtnl(dev
);
226 if (!mctp_address_unicast(addr
->s_addr
))
229 /* Prevent duplicates. Under RTNL so don't need to lock for reading */
230 if (memchr(mdev
->addrs
, addr
->s_addr
, mdev
->num_addrs
))
233 tmp_addrs
= kmalloc(mdev
->num_addrs
+ 1, GFP_KERNEL
);
236 memcpy(tmp_addrs
, mdev
->addrs
, mdev
->num_addrs
);
237 tmp_addrs
[mdev
->num_addrs
] = addr
->s_addr
;
240 spin_lock_irqsave(&mdev
->addrs_lock
, flags
);
242 swap(mdev
->addrs
, tmp_addrs
);
243 spin_unlock_irqrestore(&mdev
->addrs_lock
, flags
);
247 mctp_addr_notify(mdev
, addr
->s_addr
, RTM_NEWADDR
, skb
, nlh
);
248 mctp_route_add_local(mdev
, addr
->s_addr
);
253 static int mctp_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
254 struct netlink_ext_ack
*extack
)
256 struct net
*net
= sock_net(skb
->sk
);
257 struct nlattr
*tb
[IFA_MAX
+ 1];
258 struct net_device
*dev
;
259 struct mctp_addr
*addr
;
260 struct mctp_dev
*mdev
;
261 struct ifaddrmsg
*ifm
;
266 rc
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_mctp_policy
,
271 ifm
= nlmsg_data(nlh
);
274 addr
= nla_data(tb
[IFA_LOCAL
]);
275 else if (tb
[IFA_ADDRESS
])
276 addr
= nla_data(tb
[IFA_ADDRESS
]);
281 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
285 mdev
= mctp_dev_get_rtnl(dev
);
289 pos
= memchr(mdev
->addrs
, addr
->s_addr
, mdev
->num_addrs
);
293 rc
= mctp_route_remove_local(mdev
, addr
->s_addr
);
294 // we can ignore -ENOENT in the case a route was already removed
295 if (rc
< 0 && rc
!= -ENOENT
)
298 spin_lock_irqsave(&mdev
->addrs_lock
, flags
);
299 memmove(pos
, pos
+ 1, mdev
->num_addrs
- 1 - (pos
- mdev
->addrs
));
301 spin_unlock_irqrestore(&mdev
->addrs_lock
, flags
);
303 mctp_addr_notify(mdev
, addr
->s_addr
, RTM_DELADDR
, skb
, nlh
);
308 void mctp_dev_hold(struct mctp_dev
*mdev
)
310 refcount_inc(&mdev
->refs
);
313 void mctp_dev_put(struct mctp_dev
*mdev
)
315 if (mdev
&& refcount_dec_and_test(&mdev
->refs
)) {
318 kfree_rcu(mdev
, rcu
);
322 void mctp_dev_release_key(struct mctp_dev
*dev
, struct mctp_sk_key
*key
)
323 __must_hold(&key
->lock
)
327 if (dev
->ops
&& dev
->ops
->release_flow
)
328 dev
->ops
->release_flow(dev
, key
);
333 void mctp_dev_set_key(struct mctp_dev
*dev
, struct mctp_sk_key
*key
)
334 __must_hold(&key
->lock
)
340 static struct mctp_dev
*mctp_add_dev(struct net_device
*dev
)
342 struct mctp_dev
*mdev
;
346 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
348 return ERR_PTR(-ENOMEM
);
350 spin_lock_init(&mdev
->addrs_lock
);
352 mdev
->net
= mctp_default_net(dev_net(dev
));
354 /* associate to net_device */
355 refcount_set(&mdev
->refs
, 1);
356 rcu_assign_pointer(dev
->mctp_ptr
, mdev
);
364 static int mctp_fill_link_af(struct sk_buff
*skb
,
365 const struct net_device
*dev
, u32 ext_filter_mask
)
367 struct mctp_dev
*mdev
;
369 mdev
= mctp_dev_get_rtnl(dev
);
372 if (nla_put_u32(skb
, IFLA_MCTP_NET
, mdev
->net
))
377 static size_t mctp_get_link_af_size(const struct net_device
*dev
,
380 struct mctp_dev
*mdev
;
383 /* caller holds RCU */
384 mdev
= __mctp_dev_get(dev
);
387 ret
= nla_total_size(4); /* IFLA_MCTP_NET */
392 static const struct nla_policy ifla_af_mctp_policy
[IFLA_MCTP_MAX
+ 1] = {
393 [IFLA_MCTP_NET
] = { .type
= NLA_U32
},
396 static int mctp_set_link_af(struct net_device
*dev
, const struct nlattr
*attr
,
397 struct netlink_ext_ack
*extack
)
399 struct nlattr
*tb
[IFLA_MCTP_MAX
+ 1];
400 struct mctp_dev
*mdev
;
403 rc
= nla_parse_nested(tb
, IFLA_MCTP_MAX
, attr
, ifla_af_mctp_policy
,
408 mdev
= mctp_dev_get_rtnl(dev
);
412 if (tb
[IFLA_MCTP_NET
])
413 WRITE_ONCE(mdev
->net
, nla_get_u32(tb
[IFLA_MCTP_NET
]));
418 /* Matches netdev types that should have MCTP handling */
419 static bool mctp_known(struct net_device
*dev
)
421 /* only register specific types (inc. NONE for TUN devices) */
422 return dev
->type
== ARPHRD_MCTP
||
423 dev
->type
== ARPHRD_LOOPBACK
||
424 dev
->type
== ARPHRD_NONE
;
427 static void mctp_unregister(struct net_device
*dev
)
429 struct mctp_dev
*mdev
;
431 mdev
= mctp_dev_get_rtnl(dev
);
435 RCU_INIT_POINTER(mdev
->dev
->mctp_ptr
, NULL
);
437 mctp_route_remove_dev(mdev
);
438 mctp_neigh_remove_dev(mdev
);
443 static int mctp_register(struct net_device
*dev
)
445 struct mctp_dev
*mdev
;
447 /* Already registered? */
448 if (rtnl_dereference(dev
->mctp_ptr
))
451 /* only register specific types */
452 if (!mctp_known(dev
))
455 mdev
= mctp_add_dev(dev
);
457 return PTR_ERR(mdev
);
462 static int mctp_dev_notify(struct notifier_block
*this, unsigned long event
,
465 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
469 case NETDEV_REGISTER
:
470 rc
= mctp_register(dev
);
472 return notifier_from_errno(rc
);
474 case NETDEV_UNREGISTER
:
475 mctp_unregister(dev
);
482 static int mctp_register_netdevice(struct net_device
*dev
,
483 const struct mctp_netdev_ops
*ops
)
485 struct mctp_dev
*mdev
;
487 mdev
= mctp_add_dev(dev
);
489 return PTR_ERR(mdev
);
493 return register_netdevice(dev
);
496 int mctp_register_netdev(struct net_device
*dev
,
497 const struct mctp_netdev_ops
*ops
)
502 rc
= mctp_register_netdevice(dev
, ops
);
507 EXPORT_SYMBOL_GPL(mctp_register_netdev
);
509 void mctp_unregister_netdev(struct net_device
*dev
)
511 unregister_netdev(dev
);
513 EXPORT_SYMBOL_GPL(mctp_unregister_netdev
);
515 static struct rtnl_af_ops mctp_af_ops
= {
517 .fill_link_af
= mctp_fill_link_af
,
518 .get_link_af_size
= mctp_get_link_af_size
,
519 .set_link_af
= mctp_set_link_af
,
522 static struct notifier_block mctp_dev_nb
= {
523 .notifier_call
= mctp_dev_notify
,
524 .priority
= ADDRCONF_NOTIFY_PRIORITY
,
527 static const struct rtnl_msg_handler mctp_device_rtnl_msg_handlers
[] = {
528 {THIS_MODULE
, PF_MCTP
, RTM_NEWADDR
, mctp_rtm_newaddr
, NULL
, 0},
529 {THIS_MODULE
, PF_MCTP
, RTM_DELADDR
, mctp_rtm_deladdr
, NULL
, 0},
530 {THIS_MODULE
, PF_MCTP
, RTM_GETADDR
, NULL
, mctp_dump_addrinfo
, 0},
533 int __init
mctp_device_init(void)
537 register_netdevice_notifier(&mctp_dev_nb
);
538 rtnl_af_register(&mctp_af_ops
);
540 err
= rtnl_register_many(mctp_device_rtnl_msg_handlers
);
542 rtnl_af_unregister(&mctp_af_ops
);
543 unregister_netdevice_notifier(&mctp_dev_nb
);
549 void __exit
mctp_device_exit(void)
551 rtnl_unregister_many(mctp_device_rtnl_msg_handlers
);
552 rtnl_af_unregister(&mctp_af_ops
);
553 unregister_netdevice_notifier(&mctp_dev_nb
);