1 // SPDX-License-Identifier: GPL-2.0
3 * Management Component Transport Protocol (MCTP) - routing
6 * This is currently based on a simple routing table, with no dst cache. The
7 * number of routes should stay fairly small, so the lookup cost is small.
9 * Copyright (c) 2021 Code Construct
10 * Copyright (c) 2021 Google
13 #include <linux/idr.h>
14 #include <linux/kconfig.h>
15 #include <linux/mctp.h>
16 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/skbuff.h>
20 #include <uapi/linux/if_arp.h>
23 #include <net/mctpdevice.h>
24 #include <net/netlink.h>
27 #include <trace/events/mctp.h>
29 static const unsigned int mctp_message_maxlen = 64 * 1024;
30 static const unsigned long mctp_key_lifetime = 6 * CONFIG_HZ;
32 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev);
34 /* route output callbacks */
35 static int mctp_route_discard(struct mctp_route *route, struct sk_buff *skb)
41 static struct mctp_sock *mctp_lookup_bind(struct net *net, struct sk_buff *skb)
43 struct mctp_skb_cb *cb = mctp_cb(skb);
48 WARN_ON(!rcu_read_lock_held());
50 /* TODO: look up in skb->cb? */
53 if (!skb_headlen(skb))
56 type = (*(u8 *)skb->data) & 0x7f;
58 sk_for_each_rcu(sk, &net->mctp.binds) {
59 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);
61 if (msk->bind_net != MCTP_NET_ANY && msk->bind_net != cb->net)
64 if (msk->bind_type != type)
67 if (!mctp_address_matches(msk->bind_addr, mh->dest))
76 static bool mctp_key_match(struct mctp_sk_key *key, mctp_eid_t local,
77 mctp_eid_t peer, u8 tag)
79 if (!mctp_address_matches(key->local_addr, local))
82 if (key->peer_addr != peer)
91 /* returns a key (with key->lock held, and refcounted), or NULL if no such
94 static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb,
96 unsigned long *irqflags)
97 __acquires(&key->lock)
99 struct mctp_sk_key *key, *ret;
105 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
108 spin_lock_irqsave(&net->mctp.keys_lock, flags);
110 hlist_for_each_entry(key, &net->mctp.keys, hlist) {
111 if (!mctp_key_match(key, mh->dest, peer, tag))
114 spin_lock(&key->lock);
116 refcount_inc(&key->refs);
120 spin_unlock(&key->lock);
124 spin_unlock(&net->mctp.keys_lock);
127 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
133 static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
134 mctp_eid_t local, mctp_eid_t peer,
137 struct mctp_sk_key *key;
139 key = kzalloc(sizeof(*key), gfp);
143 key->peer_addr = peer;
144 key->local_addr = local;
148 spin_lock_init(&key->lock);
149 refcount_set(&key->refs, 1);
155 void mctp_key_unref(struct mctp_sk_key *key)
159 if (!refcount_dec_and_test(&key->refs))
162 /* even though no refs exist here, the lock allows us to stay
163 * consistent with the locking requirement of mctp_dev_release_key
165 spin_lock_irqsave(&key->lock, flags);
166 mctp_dev_release_key(key->dev, key);
167 spin_unlock_irqrestore(&key->lock, flags);
173 static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk)
175 struct net *net = sock_net(&msk->sk);
176 struct mctp_sk_key *tmp;
180 spin_lock_irqsave(&net->mctp.keys_lock, flags);
182 if (sock_flag(&msk->sk, SOCK_DEAD)) {
187 hlist_for_each_entry(tmp, &net->mctp.keys, hlist) {
188 if (mctp_key_match(tmp, key->local_addr, key->peer_addr,
190 spin_lock(&tmp->lock);
193 spin_unlock(&tmp->lock);
200 refcount_inc(&key->refs);
201 key->expiry = jiffies + mctp_key_lifetime;
202 timer_reduce(&msk->key_expiry, key->expiry);
204 hlist_add_head(&key->hlist, &net->mctp.keys);
205 hlist_add_head(&key->sklist, &msk->keys);
209 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
214 /* Helper for mctp_route_input().
215 * We're done with the key; unlock and unref the key.
216 * For the usual case of automatic expiry we remove the key from lists.
217 * In the case that manual allocation is set on a key we release the lock
218 * and local ref, reset reassembly, but don't remove from lists.
220 static void __mctp_key_done_in(struct mctp_sk_key *key, struct net *net,
221 unsigned long flags, unsigned long reason)
222 __releases(&key->lock)
226 trace_mctp_key_release(key, reason);
227 skb = key->reasm_head;
228 key->reasm_head = NULL;
230 if (!key->manual_alloc) {
231 key->reasm_dead = true;
233 mctp_dev_release_key(key->dev, key);
235 spin_unlock_irqrestore(&key->lock, flags);
237 if (!key->manual_alloc) {
238 spin_lock_irqsave(&net->mctp.keys_lock, flags);
239 if (!hlist_unhashed(&key->hlist)) {
240 hlist_del_init(&key->hlist);
241 hlist_del_init(&key->sklist);
244 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
247 /* and one for the local reference */
253 #ifdef CONFIG_MCTP_FLOWS
254 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key)
256 struct mctp_flow *flow;
258 flow = skb_ext_add(skb, SKB_EXT_MCTP);
262 refcount_inc(&key->refs);
266 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev)
268 struct mctp_sk_key *key;
269 struct mctp_flow *flow;
271 flow = skb_ext_find(skb, SKB_EXT_MCTP);
277 if (WARN_ON(key->dev && key->dev != dev))
280 mctp_dev_set_key(dev, key);
283 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) {}
284 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) {}
287 static int mctp_frag_queue(struct mctp_sk_key *key, struct sk_buff *skb)
289 struct mctp_hdr *hdr = mctp_hdr(skb);
290 u8 exp_seq, this_seq;
292 this_seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT)
295 if (!key->reasm_head) {
296 key->reasm_head = skb;
297 key->reasm_tailp = &(skb_shinfo(skb)->frag_list);
298 key->last_seq = this_seq;
302 exp_seq = (key->last_seq + 1) & MCTP_HDR_SEQ_MASK;
304 if (this_seq != exp_seq)
307 if (key->reasm_head->len + skb->len > mctp_message_maxlen)
312 *key->reasm_tailp = skb;
313 key->reasm_tailp = &skb->next;
315 key->last_seq = this_seq;
317 key->reasm_head->data_len += skb->len;
318 key->reasm_head->len += skb->len;
319 key->reasm_head->truesize += skb->truesize;
324 static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
326 struct mctp_sk_key *key, *any_key = NULL;
327 struct net *net = dev_net(skb->dev);
328 struct mctp_sock *msk;
337 /* we may be receiving a locally-routed packet; drop source sk
342 /* ensure we have enough data for a header and a type */
343 if (skb->len < sizeof(struct mctp_hdr) + 1)
346 /* grab header, advance data ptr */
348 skb_pull(skb, sizeof(struct mctp_hdr));
353 flags = mh->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
354 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
358 /* lookup socket / reasm context, exactly matching (src,dest,tag).
359 * we hold a ref on the key, and key->lock held.
361 key = mctp_lookup_key(net, skb, mh->src, &f);
363 if (flags & MCTP_HDR_FLAG_SOM) {
365 msk = container_of(key->sk, struct mctp_sock, sk);
367 /* first response to a broadcast? do a more general
368 * key lookup to find the socket, but don't use this
369 * key for reassembly - we'll create a more specific
370 * one for future packets if required (ie, !EOM).
372 any_key = mctp_lookup_key(net, skb, MCTP_ADDR_ANY, &f);
374 msk = container_of(any_key->sk,
375 struct mctp_sock, sk);
376 spin_unlock_irqrestore(&any_key->lock, f);
380 if (!key && !msk && (tag & MCTP_HDR_FLAG_TO))
381 msk = mctp_lookup_bind(net, skb);
388 /* single-packet message? deliver to socket, clean up any
391 if (flags & MCTP_HDR_FLAG_EOM) {
392 sock_queue_rcv_skb(&msk->sk, skb);
394 /* we've hit a pending reassembly; not much we
397 __mctp_key_done_in(key, net, f,
398 MCTP_TRACE_KEY_REPLIED);
405 /* broadcast response or a bind() - create a key for further
406 * packets for this message
409 key = mctp_key_alloc(msk, mh->dest, mh->src,
416 /* we can queue without the key lock here, as the
417 * key isn't observable yet
419 mctp_frag_queue(key, skb);
421 /* if the key_add fails, we've raced with another
422 * SOM packet with the same src, dest and tag. There's
423 * no way to distinguish future packets, so all we
424 * can do is drop; we'll free the skb on exit from
427 rc = mctp_key_add(key, msk);
429 trace_mctp_key_acquire(key);
431 /* we don't need to release key->lock on exit, so
432 * clean up here and suppress the unlock via
439 if (key->reasm_head || key->reasm_dead) {
440 /* duplicate start? drop everything */
441 __mctp_key_done_in(key, net, f,
442 MCTP_TRACE_KEY_INVALIDATED);
446 rc = mctp_frag_queue(key, skb);
451 /* this packet continues a previous message; reassemble
452 * using the message-specific key
455 /* we need to be continuing an existing reassembly... */
456 if (!key->reasm_head)
459 rc = mctp_frag_queue(key, skb);
461 /* end of message? deliver to socket, and we're done with
462 * the reassembly/response key
464 if (!rc && flags & MCTP_HDR_FLAG_EOM) {
465 sock_queue_rcv_skb(key->sk, key->reasm_head);
466 key->reasm_head = NULL;
467 __mctp_key_done_in(key, net, f, MCTP_TRACE_KEY_REPLIED);
472 /* not a start, no matching key */
479 spin_unlock_irqrestore(&key->lock, f);
483 mctp_key_unref(any_key);
490 static unsigned int mctp_route_mtu(struct mctp_route *rt)
492 return rt->mtu ?: READ_ONCE(rt->dev->dev->mtu);
495 static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
497 struct mctp_skb_cb *cb = mctp_cb(skb);
498 struct mctp_hdr *hdr = mctp_hdr(skb);
499 char daddr_buf[MAX_ADDR_LEN];
504 skb->protocol = htons(ETH_P_MCTP);
506 mtu = READ_ONCE(skb->dev->mtu);
507 if (skb->len > mtu) {
513 /* direct route; use the hwaddr we stashed in sendmsg */
514 if (cb->halen != skb->dev->addr_len) {
515 /* sanity check, sendmsg should have already caught this */
521 /* If lookup fails let the device handle daddr==NULL */
522 if (mctp_neigh_lookup(route->dev, hdr->dest, daddr_buf) == 0)
526 rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol),
527 daddr, skb->dev->dev_addr, skb->len);
530 return -EHOSTUNREACH;
533 mctp_flow_prepare_output(skb, route->dev);
535 rc = dev_queue_xmit(skb);
537 rc = net_xmit_errno(rc);
542 /* route alloc/release */
543 static void mctp_route_release(struct mctp_route *rt)
545 if (refcount_dec_and_test(&rt->refs)) {
546 mctp_dev_put(rt->dev);
551 /* returns a route with the refcount at 1 */
552 static struct mctp_route *mctp_route_alloc(void)
554 struct mctp_route *rt;
556 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
560 INIT_LIST_HEAD(&rt->list);
561 refcount_set(&rt->refs, 1);
562 rt->output = mctp_route_discard;
567 unsigned int mctp_default_net(struct net *net)
569 return READ_ONCE(net->mctp.default_net);
572 int mctp_default_net_set(struct net *net, unsigned int index)
576 WRITE_ONCE(net->mctp.default_net, index);
581 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
582 struct mctp_sock *msk)
584 struct netns_mctp *mns = &net->mctp;
586 lockdep_assert_held(&mns->keys_lock);
588 key->expiry = jiffies + mctp_key_lifetime;
589 timer_reduce(&msk->key_expiry, key->expiry);
591 /* we hold the net->key_lock here, allowing updates to both
594 hlist_add_head_rcu(&key->hlist, &mns->keys);
595 hlist_add_head_rcu(&key->sklist, &msk->keys);
596 refcount_inc(&key->refs);
599 /* Allocate a locally-owned tag value for (saddr, daddr), and reserve
600 * it for the socket msk
602 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
603 mctp_eid_t daddr, mctp_eid_t saddr,
604 bool manual, u8 *tagp)
606 struct net *net = sock_net(&msk->sk);
607 struct netns_mctp *mns = &net->mctp;
608 struct mctp_sk_key *key, *tmp;
612 /* for NULL destination EIDs, we may get a response from any peer */
613 if (daddr == MCTP_ADDR_NULL)
614 daddr = MCTP_ADDR_ANY;
616 /* be optimistic, alloc now */
617 key = mctp_key_alloc(msk, saddr, daddr, 0, GFP_KERNEL);
619 return ERR_PTR(-ENOMEM);
621 /* 8 possible tag values */
624 spin_lock_irqsave(&mns->keys_lock, flags);
626 /* Walk through the existing keys, looking for potential conflicting
627 * tags. If we find a conflict, clear that bit from tagbits
629 hlist_for_each_entry(tmp, &mns->keys, hlist) {
630 /* We can check the lookup fields (*_addr, tag) without the
631 * lock held, they don't change over the lifetime of the key.
634 /* if we don't own the tag, it can't conflict */
635 if (tmp->tag & MCTP_HDR_FLAG_TO)
638 if (!(mctp_address_matches(tmp->peer_addr, daddr) &&
639 mctp_address_matches(tmp->local_addr, saddr)))
642 spin_lock(&tmp->lock);
643 /* key must still be valid. If we find a match, clear the
644 * potential tag value
647 tagbits &= ~(1 << tmp->tag);
648 spin_unlock(&tmp->lock);
655 key->tag = __ffs(tagbits);
656 mctp_reserve_tag(net, key, msk);
657 trace_mctp_key_acquire(key);
659 key->manual_alloc = manual;
663 spin_unlock_irqrestore(&mns->keys_lock, flags);
667 return ERR_PTR(-EBUSY);
673 static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk,
675 u8 req_tag, u8 *tagp)
677 struct net *net = sock_net(&msk->sk);
678 struct netns_mctp *mns = &net->mctp;
679 struct mctp_sk_key *key, *tmp;
682 req_tag &= ~(MCTP_TAG_PREALLOC | MCTP_TAG_OWNER);
685 spin_lock_irqsave(&mns->keys_lock, flags);
687 hlist_for_each_entry(tmp, &mns->keys, hlist) {
688 if (tmp->tag != req_tag)
691 if (!mctp_address_matches(tmp->peer_addr, daddr))
694 if (!tmp->manual_alloc)
697 spin_lock(&tmp->lock);
700 refcount_inc(&key->refs);
701 spin_unlock(&tmp->lock);
704 spin_unlock(&tmp->lock);
706 spin_unlock_irqrestore(&mns->keys_lock, flags);
709 return ERR_PTR(-ENOENT);
717 /* routing lookups */
718 static bool mctp_rt_match_eid(struct mctp_route *rt,
719 unsigned int net, mctp_eid_t eid)
721 return READ_ONCE(rt->dev->net) == net &&
722 rt->min <= eid && rt->max >= eid;
725 /* compares match, used for duplicate prevention */
726 static bool mctp_rt_compare_exact(struct mctp_route *rt1,
727 struct mctp_route *rt2)
730 return rt1->dev->net == rt2->dev->net &&
731 rt1->min == rt2->min &&
732 rt1->max == rt2->max;
735 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
738 struct mctp_route *tmp, *rt = NULL;
742 list_for_each_entry_rcu(tmp, &net->mctp.routes, list) {
743 /* TODO: add metrics */
744 if (mctp_rt_match_eid(tmp, dnet, daddr)) {
745 if (refcount_inc_not_zero(&tmp->refs)) {
757 static struct mctp_route *mctp_route_lookup_null(struct net *net,
758 struct net_device *dev)
760 struct mctp_route *tmp, *rt = NULL;
764 list_for_each_entry_rcu(tmp, &net->mctp.routes, list) {
765 if (tmp->dev->dev == dev && tmp->type == RTN_LOCAL &&
766 refcount_inc_not_zero(&tmp->refs)) {
777 static int mctp_do_fragment_route(struct mctp_route *rt, struct sk_buff *skb,
778 unsigned int mtu, u8 tag)
780 const unsigned int hlen = sizeof(struct mctp_hdr);
781 struct mctp_hdr *hdr, *hdr2;
782 unsigned int pos, size, headroom;
783 struct sk_buff *skb2;
791 if (mtu < hlen + 1) {
796 /* keep same headroom as the original skb */
797 headroom = skb_headroom(skb);
799 /* we've got the header */
802 for (pos = 0; pos < skb->len;) {
803 /* size of message payload */
804 size = min(mtu - hlen, skb->len - pos);
806 skb2 = alloc_skb(headroom + hlen + size, GFP_KERNEL);
812 /* generic skb copy */
813 skb2->protocol = skb->protocol;
814 skb2->priority = skb->priority;
815 skb2->dev = skb->dev;
816 memcpy(skb2->cb, skb->cb, sizeof(skb2->cb));
819 skb_set_owner_w(skb2, skb->sk);
821 /* establish packet */
822 skb_reserve(skb2, headroom);
823 skb_reset_network_header(skb2);
824 skb_put(skb2, hlen + size);
825 skb2->transport_header = skb2->network_header + hlen;
827 /* copy header fields, calculate SOM/EOM flags & seq */
828 hdr2 = mctp_hdr(skb2);
829 hdr2->ver = hdr->ver;
830 hdr2->dest = hdr->dest;
831 hdr2->src = hdr->src;
832 hdr2->flags_seq_tag = tag &
833 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
836 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
838 if (pos + size == skb->len)
839 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
841 hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT;
843 /* copy message payload */
844 skb_copy_bits(skb, pos, skb_transport_header(skb2), size);
847 rc = rt->output(rt, skb2);
851 seq = (seq + 1) & MCTP_HDR_SEQ_MASK;
859 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
860 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag)
862 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);
863 struct mctp_skb_cb *cb = mctp_cb(skb);
864 struct mctp_route tmp_rt = {0};
865 struct mctp_sk_key *key;
866 struct mctp_hdr *hdr;
878 if (WARN_ON(!rt->dev))
881 } else if (cb->ifindex) {
882 struct net_device *dev;
888 dev = dev_get_by_index_rcu(sock_net(sk), cb->ifindex);
893 rt->dev = __mctp_dev_get(dev);
899 /* establish temporary route - we set up enough to keep
900 * mctp_route_output happy
902 rt->output = mctp_route_output;
909 spin_lock_irqsave(&rt->dev->addrs_lock, flags);
910 if (rt->dev->num_addrs == 0) {
913 /* use the outbound interface's first address as our source */
914 saddr = rt->dev->addrs[0];
917 spin_unlock_irqrestore(&rt->dev->addrs_lock, flags);
922 if (req_tag & MCTP_TAG_OWNER) {
923 if (req_tag & MCTP_TAG_PREALLOC)
924 key = mctp_lookup_prealloc_tag(msk, daddr,
927 key = mctp_alloc_local_tag(msk, daddr, saddr,
934 mctp_skb_set_flow(skb, key);
935 /* done with the key in this scope */
937 tag |= MCTP_HDR_FLAG_TO;
940 tag = req_tag & MCTP_TAG_MASK;
943 skb->protocol = htons(ETH_P_MCTP);
945 skb_reset_transport_header(skb);
946 skb_push(skb, sizeof(struct mctp_hdr));
947 skb_reset_network_header(skb);
948 skb->dev = rt->dev->dev;
950 /* cb->net will have been set on initial ingress */
953 /* set up common header fields */
959 mtu = mctp_route_mtu(rt);
961 if (skb->len + sizeof(struct mctp_hdr) <= mtu) {
962 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM |
963 MCTP_HDR_FLAG_EOM | tag;
964 rc = rt->output(rt, skb);
966 rc = mctp_do_fragment_route(rt, skb, mtu, tag);
971 mctp_route_release(rt);
973 mctp_dev_put(tmp_rt.dev);
978 /* route management */
979 static int mctp_route_add(struct mctp_dev *mdev, mctp_eid_t daddr_start,
980 unsigned int daddr_extent, unsigned int mtu,
983 int (*rtfn)(struct mctp_route *rt, struct sk_buff *skb);
984 struct net *net = dev_net(mdev->dev);
985 struct mctp_route *rt, *ert;
987 if (!mctp_address_unicast(daddr_start))
990 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
995 rtfn = mctp_route_input;
998 rtfn = mctp_route_output;
1004 rt = mctp_route_alloc();
1008 rt->min = daddr_start;
1009 rt->max = daddr_start + daddr_extent;
1012 mctp_dev_hold(rt->dev);
1017 /* Prevent duplicate identical routes. */
1018 list_for_each_entry(ert, &net->mctp.routes, list) {
1019 if (mctp_rt_compare_exact(rt, ert)) {
1020 mctp_route_release(rt);
1025 list_add_rcu(&rt->list, &net->mctp.routes);
1030 static int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,
1031 unsigned int daddr_extent, unsigned char type)
1033 struct net *net = dev_net(mdev->dev);
1034 struct mctp_route *rt, *tmp;
1035 mctp_eid_t daddr_end;
1038 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
1041 daddr_end = daddr_start + daddr_extent;
1046 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1047 if (rt->dev == mdev &&
1048 rt->min == daddr_start && rt->max == daddr_end &&
1050 list_del_rcu(&rt->list);
1051 /* TODO: immediate RTM_DELROUTE */
1052 mctp_route_release(rt);
1057 return dropped ? 0 : -ENOENT;
1060 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr)
1062 return mctp_route_add(mdev, addr, 0, 0, RTN_LOCAL);
1065 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr)
1067 return mctp_route_remove(mdev, addr, 0, RTN_LOCAL);
1070 /* removes all entries for a given device */
1071 void mctp_route_remove_dev(struct mctp_dev *mdev)
1073 struct net *net = dev_net(mdev->dev);
1074 struct mctp_route *rt, *tmp;
1077 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1078 if (rt->dev == mdev) {
1079 list_del_rcu(&rt->list);
1080 /* TODO: immediate RTM_DELROUTE */
1081 mctp_route_release(rt);
1086 /* Incoming packet-handling */
1088 static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev,
1089 struct packet_type *pt,
1090 struct net_device *orig_dev)
1092 struct net *net = dev_net(dev);
1093 struct mctp_dev *mdev;
1094 struct mctp_skb_cb *cb;
1095 struct mctp_route *rt;
1096 struct mctp_hdr *mh;
1099 mdev = __mctp_dev_get(dev);
1102 /* basic non-data sanity checks */
1106 if (!pskb_may_pull(skb, sizeof(struct mctp_hdr)))
1109 skb_reset_transport_header(skb);
1110 skb_reset_network_header(skb);
1112 /* We have enough for a header; decode and route */
1114 if (mh->ver < MCTP_VER_MIN || mh->ver > MCTP_VER_MAX)
1117 /* source must be valid unicast or null; drop reserved ranges and
1120 if (!(mctp_address_unicast(mh->src) || mctp_address_null(mh->src)))
1123 /* dest address: as above, but allow broadcast */
1124 if (!(mctp_address_unicast(mh->dest) || mctp_address_null(mh->dest) ||
1125 mctp_address_broadcast(mh->dest)))
1128 /* MCTP drivers must populate halen/haddr */
1129 if (dev->type == ARPHRD_MCTP) {
1132 cb = __mctp_cb(skb);
1135 cb->net = READ_ONCE(mdev->net);
1136 cb->ifindex = dev->ifindex;
1138 rt = mctp_route_lookup(net, cb->net, mh->dest);
1140 /* NULL EID, but addressed to our physical address */
1141 if (!rt && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST)
1142 rt = mctp_route_lookup_null(net, dev);
1147 rt->output(rt, skb);
1148 mctp_route_release(rt);
1151 return NET_RX_SUCCESS;
1159 static struct packet_type mctp_packet_type = {
1160 .type = cpu_to_be16(ETH_P_MCTP),
1161 .func = mctp_pkttype_receive,
1164 /* netlink interface */
1166 static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = {
1167 [RTA_DST] = { .type = NLA_U8 },
1168 [RTA_METRICS] = { .type = NLA_NESTED },
1169 [RTA_OIF] = { .type = NLA_U32 },
1172 /* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing.
1173 * tb must hold RTA_MAX+1 elements.
1175 static int mctp_route_nlparse(struct sk_buff *skb, struct nlmsghdr *nlh,
1176 struct netlink_ext_ack *extack,
1177 struct nlattr **tb, struct rtmsg **rtm,
1178 struct mctp_dev **mdev, mctp_eid_t *daddr_start)
1180 struct net *net = sock_net(skb->sk);
1181 struct net_device *dev;
1182 unsigned int ifindex;
1185 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
1186 rta_mctp_policy, extack);
1188 NL_SET_ERR_MSG(extack, "incorrect format");
1193 NL_SET_ERR_MSG(extack, "dst EID missing");
1196 *daddr_start = nla_get_u8(tb[RTA_DST]);
1199 NL_SET_ERR_MSG(extack, "ifindex missing");
1202 ifindex = nla_get_u32(tb[RTA_OIF]);
1204 *rtm = nlmsg_data(nlh);
1205 if ((*rtm)->rtm_family != AF_MCTP) {
1206 NL_SET_ERR_MSG(extack, "route family must be AF_MCTP");
1210 dev = __dev_get_by_index(net, ifindex);
1212 NL_SET_ERR_MSG(extack, "bad ifindex");
1215 *mdev = mctp_dev_get_rtnl(dev);
1219 if (dev->flags & IFF_LOOPBACK) {
1220 NL_SET_ERR_MSG(extack, "no routes to loopback");
1227 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {
1228 [RTAX_MTU] = { .type = NLA_U32 },
1231 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1232 struct netlink_ext_ack *extack)
1234 struct nlattr *tb[RTA_MAX + 1];
1235 struct nlattr *tbx[RTAX_MAX + 1];
1236 mctp_eid_t daddr_start;
1237 struct mctp_dev *mdev;
1242 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1243 &rtm, &mdev, &daddr_start);
1247 if (rtm->rtm_type != RTN_UNICAST) {
1248 NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST");
1253 if (tb[RTA_METRICS]) {
1254 rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS],
1255 rta_metrics_policy, NULL);
1259 mtu = nla_get_u32(tbx[RTAX_MTU]);
1262 rc = mctp_route_add(mdev, daddr_start, rtm->rtm_dst_len, mtu,
1267 static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1268 struct netlink_ext_ack *extack)
1270 struct nlattr *tb[RTA_MAX + 1];
1271 mctp_eid_t daddr_start;
1272 struct mctp_dev *mdev;
1276 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1277 &rtm, &mdev, &daddr_start);
1281 /* we only have unicast routes */
1282 if (rtm->rtm_type != RTN_UNICAST)
1285 rc = mctp_route_remove(mdev, daddr_start, rtm->rtm_dst_len, RTN_UNICAST);
1289 static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt,
1290 u32 portid, u32 seq, int event, unsigned int flags)
1292 struct nlmsghdr *nlh;
1296 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
1300 hdr = nlmsg_data(nlh);
1301 hdr->rtm_family = AF_MCTP;
1303 /* we use the _len fields as a number of EIDs, rather than
1304 * a number of bits in the address
1306 hdr->rtm_dst_len = rt->max - rt->min;
1307 hdr->rtm_src_len = 0;
1309 hdr->rtm_table = RT_TABLE_DEFAULT;
1310 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */
1311 hdr->rtm_scope = RT_SCOPE_LINK; /* TODO: scope in mctp_route? */
1312 hdr->rtm_type = rt->type;
1314 if (nla_put_u8(skb, RTA_DST, rt->min))
1317 metrics = nla_nest_start_noflag(skb, RTA_METRICS);
1322 if (nla_put_u32(skb, RTAX_MTU, rt->mtu))
1326 nla_nest_end(skb, metrics);
1329 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex))
1333 /* TODO: conditional neighbour physaddr? */
1335 nlmsg_end(skb, nlh);
1340 nlmsg_cancel(skb, nlh);
1344 static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb)
1346 struct net *net = sock_net(skb->sk);
1347 struct mctp_route *rt;
1350 /* TODO: allow filtering on route data, possibly under
1354 /* TODO: change to struct overlay */
1355 s_idx = cb->args[0];
1359 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
1362 if (mctp_fill_rtinfo(skb, rt,
1363 NETLINK_CB(cb->skb).portid,
1365 RTM_NEWROUTE, NLM_F_MULTI) < 0)
1375 /* net namespace implementation */
1376 static int __net_init mctp_routes_net_init(struct net *net)
1378 struct netns_mctp *ns = &net->mctp;
1380 INIT_LIST_HEAD(&ns->routes);
1381 INIT_HLIST_HEAD(&ns->binds);
1382 mutex_init(&ns->bind_lock);
1383 INIT_HLIST_HEAD(&ns->keys);
1384 spin_lock_init(&ns->keys_lock);
1385 WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET));
1389 static void __net_exit mctp_routes_net_exit(struct net *net)
1391 struct mctp_route *rt;
1394 list_for_each_entry_rcu(rt, &net->mctp.routes, list)
1395 mctp_route_release(rt);
1399 static struct pernet_operations mctp_net_ops = {
1400 .init = mctp_routes_net_init,
1401 .exit = mctp_routes_net_exit,
1404 int __init mctp_routes_init(void)
1406 dev_add_pack(&mctp_packet_type);
1408 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETROUTE,
1409 NULL, mctp_dump_rtinfo, 0);
1410 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWROUTE,
1411 mctp_newroute, NULL, 0);
1412 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELROUTE,
1413 mctp_delroute, NULL, 0);
1415 return register_pernet_subsys(&mctp_net_ops);
1418 void mctp_routes_exit(void)
1420 unregister_pernet_subsys(&mctp_net_ops);
1421 rtnl_unregister(PF_MCTP, RTM_DELROUTE);
1422 rtnl_unregister(PF_MCTP, RTM_NEWROUTE);
1423 rtnl_unregister(PF_MCTP, RTM_GETROUTE);
1424 dev_remove_pack(&mctp_packet_type);
1427 #if IS_ENABLED(CONFIG_MCTP_TEST)
1428 #include "test/route-test.c"