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);
154 void mctp_key_unref(struct mctp_sk_key *key)
158 if (!refcount_dec_and_test(&key->refs))
161 /* even though no refs exist here, the lock allows us to stay
162 * consistent with the locking requirement of mctp_dev_release_key
164 spin_lock_irqsave(&key->lock, flags);
165 mctp_dev_release_key(key->dev, key);
166 spin_unlock_irqrestore(&key->lock, flags);
171 static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk)
173 struct net *net = sock_net(&msk->sk);
174 struct mctp_sk_key *tmp;
178 spin_lock_irqsave(&net->mctp.keys_lock, flags);
180 hlist_for_each_entry(tmp, &net->mctp.keys, hlist) {
181 if (mctp_key_match(tmp, key->local_addr, key->peer_addr,
183 spin_lock(&tmp->lock);
186 spin_unlock(&tmp->lock);
193 refcount_inc(&key->refs);
194 key->expiry = jiffies + mctp_key_lifetime;
195 timer_reduce(&msk->key_expiry, key->expiry);
197 hlist_add_head(&key->hlist, &net->mctp.keys);
198 hlist_add_head(&key->sklist, &msk->keys);
201 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
206 /* Helper for mctp_route_input().
207 * We're done with the key; unlock and unref the key.
208 * For the usual case of automatic expiry we remove the key from lists.
209 * In the case that manual allocation is set on a key we release the lock
210 * and local ref, reset reassembly, but don't remove from lists.
212 static void __mctp_key_done_in(struct mctp_sk_key *key, struct net *net,
213 unsigned long flags, unsigned long reason)
214 __releases(&key->lock)
218 trace_mctp_key_release(key, reason);
219 skb = key->reasm_head;
220 key->reasm_head = NULL;
222 if (!key->manual_alloc) {
223 key->reasm_dead = true;
225 mctp_dev_release_key(key->dev, key);
227 spin_unlock_irqrestore(&key->lock, flags);
229 if (!key->manual_alloc) {
230 spin_lock_irqsave(&net->mctp.keys_lock, flags);
231 hlist_del(&key->hlist);
232 hlist_del(&key->sklist);
233 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
235 /* unref for the lists */
239 /* and one for the local reference */
245 #ifdef CONFIG_MCTP_FLOWS
246 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key)
248 struct mctp_flow *flow;
250 flow = skb_ext_add(skb, SKB_EXT_MCTP);
254 refcount_inc(&key->refs);
258 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev)
260 struct mctp_sk_key *key;
261 struct mctp_flow *flow;
263 flow = skb_ext_find(skb, SKB_EXT_MCTP);
269 if (WARN_ON(key->dev && key->dev != dev))
272 mctp_dev_set_key(dev, key);
275 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) {}
276 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) {}
279 static int mctp_frag_queue(struct mctp_sk_key *key, struct sk_buff *skb)
281 struct mctp_hdr *hdr = mctp_hdr(skb);
282 u8 exp_seq, this_seq;
284 this_seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT)
287 if (!key->reasm_head) {
288 key->reasm_head = skb;
289 key->reasm_tailp = &(skb_shinfo(skb)->frag_list);
290 key->last_seq = this_seq;
294 exp_seq = (key->last_seq + 1) & MCTP_HDR_SEQ_MASK;
296 if (this_seq != exp_seq)
299 if (key->reasm_head->len + skb->len > mctp_message_maxlen)
304 *key->reasm_tailp = skb;
305 key->reasm_tailp = &skb->next;
307 key->last_seq = this_seq;
309 key->reasm_head->data_len += skb->len;
310 key->reasm_head->len += skb->len;
311 key->reasm_head->truesize += skb->truesize;
316 static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
318 struct net *net = dev_net(skb->dev);
319 struct mctp_sk_key *key;
320 struct mctp_sock *msk;
329 /* we may be receiving a locally-routed packet; drop source sk
334 /* ensure we have enough data for a header and a type */
335 if (skb->len < sizeof(struct mctp_hdr) + 1)
338 /* grab header, advance data ptr */
340 skb_pull(skb, sizeof(struct mctp_hdr));
345 flags = mh->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
346 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
350 /* lookup socket / reasm context, exactly matching (src,dest,tag).
351 * we hold a ref on the key, and key->lock held.
353 key = mctp_lookup_key(net, skb, mh->src, &f);
355 if (flags & MCTP_HDR_FLAG_SOM) {
357 msk = container_of(key->sk, struct mctp_sock, sk);
359 /* first response to a broadcast? do a more general
360 * key lookup to find the socket, but don't use this
361 * key for reassembly - we'll create a more specific
362 * one for future packets if required (ie, !EOM).
364 key = mctp_lookup_key(net, skb, MCTP_ADDR_ANY, &f);
366 msk = container_of(key->sk,
367 struct mctp_sock, sk);
368 spin_unlock_irqrestore(&key->lock, f);
374 if (!key && !msk && (tag & MCTP_HDR_FLAG_TO))
375 msk = mctp_lookup_bind(net, skb);
382 /* single-packet message? deliver to socket, clean up any
385 if (flags & MCTP_HDR_FLAG_EOM) {
386 sock_queue_rcv_skb(&msk->sk, skb);
388 /* we've hit a pending reassembly; not much we
391 __mctp_key_done_in(key, net, f,
392 MCTP_TRACE_KEY_REPLIED);
399 /* broadcast response or a bind() - create a key for further
400 * packets for this message
403 key = mctp_key_alloc(msk, mh->dest, mh->src,
410 /* we can queue without the key lock here, as the
411 * key isn't observable yet
413 mctp_frag_queue(key, skb);
415 /* if the key_add fails, we've raced with another
416 * SOM packet with the same src, dest and tag. There's
417 * no way to distinguish future packets, so all we
418 * can do is drop; we'll free the skb on exit from
421 rc = mctp_key_add(key, msk);
425 trace_mctp_key_acquire(key);
427 /* we don't need to release key->lock on exit */
433 if (key->reasm_head || key->reasm_dead) {
434 /* duplicate start? drop everything */
435 __mctp_key_done_in(key, net, f,
436 MCTP_TRACE_KEY_INVALIDATED);
440 rc = mctp_frag_queue(key, skb);
445 /* this packet continues a previous message; reassemble
446 * using the message-specific key
449 /* we need to be continuing an existing reassembly... */
450 if (!key->reasm_head)
453 rc = mctp_frag_queue(key, skb);
455 /* end of message? deliver to socket, and we're done with
456 * the reassembly/response key
458 if (!rc && flags & MCTP_HDR_FLAG_EOM) {
459 sock_queue_rcv_skb(key->sk, key->reasm_head);
460 key->reasm_head = NULL;
461 __mctp_key_done_in(key, net, f, MCTP_TRACE_KEY_REPLIED);
466 /* not a start, no matching key */
473 spin_unlock_irqrestore(&key->lock, f);
482 static unsigned int mctp_route_mtu(struct mctp_route *rt)
484 return rt->mtu ?: READ_ONCE(rt->dev->dev->mtu);
487 static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
489 struct mctp_skb_cb *cb = mctp_cb(skb);
490 struct mctp_hdr *hdr = mctp_hdr(skb);
491 char daddr_buf[MAX_ADDR_LEN];
496 skb->protocol = htons(ETH_P_MCTP);
498 mtu = READ_ONCE(skb->dev->mtu);
499 if (skb->len > mtu) {
505 /* direct route; use the hwaddr we stashed in sendmsg */
506 if (cb->halen != skb->dev->addr_len) {
507 /* sanity check, sendmsg should have already caught this */
513 /* If lookup fails let the device handle daddr==NULL */
514 if (mctp_neigh_lookup(route->dev, hdr->dest, daddr_buf) == 0)
518 rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol),
519 daddr, skb->dev->dev_addr, skb->len);
522 return -EHOSTUNREACH;
525 mctp_flow_prepare_output(skb, route->dev);
527 rc = dev_queue_xmit(skb);
529 rc = net_xmit_errno(rc);
534 /* route alloc/release */
535 static void mctp_route_release(struct mctp_route *rt)
537 if (refcount_dec_and_test(&rt->refs)) {
538 mctp_dev_put(rt->dev);
543 /* returns a route with the refcount at 1 */
544 static struct mctp_route *mctp_route_alloc(void)
546 struct mctp_route *rt;
548 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
552 INIT_LIST_HEAD(&rt->list);
553 refcount_set(&rt->refs, 1);
554 rt->output = mctp_route_discard;
559 unsigned int mctp_default_net(struct net *net)
561 return READ_ONCE(net->mctp.default_net);
564 int mctp_default_net_set(struct net *net, unsigned int index)
568 WRITE_ONCE(net->mctp.default_net, index);
573 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
574 struct mctp_sock *msk)
576 struct netns_mctp *mns = &net->mctp;
578 lockdep_assert_held(&mns->keys_lock);
580 key->expiry = jiffies + mctp_key_lifetime;
581 timer_reduce(&msk->key_expiry, key->expiry);
583 /* we hold the net->key_lock here, allowing updates to both
586 hlist_add_head_rcu(&key->hlist, &mns->keys);
587 hlist_add_head_rcu(&key->sklist, &msk->keys);
588 refcount_inc(&key->refs);
591 /* Allocate a locally-owned tag value for (saddr, daddr), and reserve
592 * it for the socket msk
594 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
595 mctp_eid_t daddr, mctp_eid_t saddr,
596 bool manual, u8 *tagp)
598 struct net *net = sock_net(&msk->sk);
599 struct netns_mctp *mns = &net->mctp;
600 struct mctp_sk_key *key, *tmp;
604 /* for NULL destination EIDs, we may get a response from any peer */
605 if (daddr == MCTP_ADDR_NULL)
606 daddr = MCTP_ADDR_ANY;
608 /* be optimistic, alloc now */
609 key = mctp_key_alloc(msk, saddr, daddr, 0, GFP_KERNEL);
611 return ERR_PTR(-ENOMEM);
613 /* 8 possible tag values */
616 spin_lock_irqsave(&mns->keys_lock, flags);
618 /* Walk through the existing keys, looking for potential conflicting
619 * tags. If we find a conflict, clear that bit from tagbits
621 hlist_for_each_entry(tmp, &mns->keys, hlist) {
622 /* We can check the lookup fields (*_addr, tag) without the
623 * lock held, they don't change over the lifetime of the key.
626 /* if we don't own the tag, it can't conflict */
627 if (tmp->tag & MCTP_HDR_FLAG_TO)
630 if (!(mctp_address_matches(tmp->peer_addr, daddr) &&
631 mctp_address_matches(tmp->local_addr, saddr)))
634 spin_lock(&tmp->lock);
635 /* key must still be valid. If we find a match, clear the
636 * potential tag value
639 tagbits &= ~(1 << tmp->tag);
640 spin_unlock(&tmp->lock);
647 key->tag = __ffs(tagbits);
648 mctp_reserve_tag(net, key, msk);
649 trace_mctp_key_acquire(key);
651 key->manual_alloc = manual;
655 spin_unlock_irqrestore(&mns->keys_lock, flags);
659 return ERR_PTR(-EBUSY);
665 static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk,
667 u8 req_tag, u8 *tagp)
669 struct net *net = sock_net(&msk->sk);
670 struct netns_mctp *mns = &net->mctp;
671 struct mctp_sk_key *key, *tmp;
674 req_tag &= ~(MCTP_TAG_PREALLOC | MCTP_TAG_OWNER);
677 spin_lock_irqsave(&mns->keys_lock, flags);
679 hlist_for_each_entry(tmp, &mns->keys, hlist) {
680 if (tmp->tag != req_tag)
683 if (!mctp_address_matches(tmp->peer_addr, daddr))
686 if (!tmp->manual_alloc)
689 spin_lock(&tmp->lock);
692 refcount_inc(&key->refs);
693 spin_unlock(&tmp->lock);
696 spin_unlock(&tmp->lock);
698 spin_unlock_irqrestore(&mns->keys_lock, flags);
701 return ERR_PTR(-ENOENT);
709 /* routing lookups */
710 static bool mctp_rt_match_eid(struct mctp_route *rt,
711 unsigned int net, mctp_eid_t eid)
713 return READ_ONCE(rt->dev->net) == net &&
714 rt->min <= eid && rt->max >= eid;
717 /* compares match, used for duplicate prevention */
718 static bool mctp_rt_compare_exact(struct mctp_route *rt1,
719 struct mctp_route *rt2)
722 return rt1->dev->net == rt2->dev->net &&
723 rt1->min == rt2->min &&
724 rt1->max == rt2->max;
727 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
730 struct mctp_route *tmp, *rt = NULL;
732 list_for_each_entry_rcu(tmp, &net->mctp.routes, list) {
733 /* TODO: add metrics */
734 if (mctp_rt_match_eid(tmp, dnet, daddr)) {
735 if (refcount_inc_not_zero(&tmp->refs)) {
745 static struct mctp_route *mctp_route_lookup_null(struct net *net,
746 struct net_device *dev)
748 struct mctp_route *rt;
750 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
751 if (rt->dev->dev == dev && rt->type == RTN_LOCAL &&
752 refcount_inc_not_zero(&rt->refs))
759 static int mctp_do_fragment_route(struct mctp_route *rt, struct sk_buff *skb,
760 unsigned int mtu, u8 tag)
762 const unsigned int hlen = sizeof(struct mctp_hdr);
763 struct mctp_hdr *hdr, *hdr2;
764 unsigned int pos, size, headroom;
765 struct sk_buff *skb2;
773 if (mtu < hlen + 1) {
778 /* keep same headroom as the original skb */
779 headroom = skb_headroom(skb);
781 /* we've got the header */
784 for (pos = 0; pos < skb->len;) {
785 /* size of message payload */
786 size = min(mtu - hlen, skb->len - pos);
788 skb2 = alloc_skb(headroom + hlen + size, GFP_KERNEL);
794 /* generic skb copy */
795 skb2->protocol = skb->protocol;
796 skb2->priority = skb->priority;
797 skb2->dev = skb->dev;
798 memcpy(skb2->cb, skb->cb, sizeof(skb2->cb));
801 skb_set_owner_w(skb2, skb->sk);
803 /* establish packet */
804 skb_reserve(skb2, headroom);
805 skb_reset_network_header(skb2);
806 skb_put(skb2, hlen + size);
807 skb2->transport_header = skb2->network_header + hlen;
809 /* copy header fields, calculate SOM/EOM flags & seq */
810 hdr2 = mctp_hdr(skb2);
811 hdr2->ver = hdr->ver;
812 hdr2->dest = hdr->dest;
813 hdr2->src = hdr->src;
814 hdr2->flags_seq_tag = tag &
815 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
818 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
820 if (pos + size == skb->len)
821 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
823 hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT;
825 /* copy message payload */
826 skb_copy_bits(skb, pos, skb_transport_header(skb2), size);
829 rc = rt->output(rt, skb2);
833 seq = (seq + 1) & MCTP_HDR_SEQ_MASK;
841 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
842 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag)
844 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);
845 struct mctp_skb_cb *cb = mctp_cb(skb);
846 struct mctp_route tmp_rt = {0};
847 struct mctp_sk_key *key;
848 struct mctp_hdr *hdr;
860 if (WARN_ON(!rt->dev))
863 } else if (cb->ifindex) {
864 struct net_device *dev;
870 dev = dev_get_by_index_rcu(sock_net(sk), cb->ifindex);
875 rt->dev = __mctp_dev_get(dev);
881 /* establish temporary route - we set up enough to keep
882 * mctp_route_output happy
884 rt->output = mctp_route_output;
891 spin_lock_irqsave(&rt->dev->addrs_lock, flags);
892 if (rt->dev->num_addrs == 0) {
895 /* use the outbound interface's first address as our source */
896 saddr = rt->dev->addrs[0];
899 spin_unlock_irqrestore(&rt->dev->addrs_lock, flags);
904 if (req_tag & MCTP_TAG_OWNER) {
905 if (req_tag & MCTP_TAG_PREALLOC)
906 key = mctp_lookup_prealloc_tag(msk, daddr,
909 key = mctp_alloc_local_tag(msk, daddr, saddr,
916 mctp_skb_set_flow(skb, key);
917 /* done with the key in this scope */
919 tag |= MCTP_HDR_FLAG_TO;
922 tag = req_tag & MCTP_TAG_MASK;
925 skb->protocol = htons(ETH_P_MCTP);
927 skb_reset_transport_header(skb);
928 skb_push(skb, sizeof(struct mctp_hdr));
929 skb_reset_network_header(skb);
930 skb->dev = rt->dev->dev;
932 /* cb->net will have been set on initial ingress */
935 /* set up common header fields */
941 mtu = mctp_route_mtu(rt);
943 if (skb->len + sizeof(struct mctp_hdr) <= mtu) {
944 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM |
945 MCTP_HDR_FLAG_EOM | tag;
946 rc = rt->output(rt, skb);
948 rc = mctp_do_fragment_route(rt, skb, mtu, tag);
953 mctp_route_release(rt);
955 mctp_dev_put(tmp_rt.dev);
960 /* route management */
961 static int mctp_route_add(struct mctp_dev *mdev, mctp_eid_t daddr_start,
962 unsigned int daddr_extent, unsigned int mtu,
965 int (*rtfn)(struct mctp_route *rt, struct sk_buff *skb);
966 struct net *net = dev_net(mdev->dev);
967 struct mctp_route *rt, *ert;
969 if (!mctp_address_unicast(daddr_start))
972 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
977 rtfn = mctp_route_input;
980 rtfn = mctp_route_output;
986 rt = mctp_route_alloc();
990 rt->min = daddr_start;
991 rt->max = daddr_start + daddr_extent;
994 mctp_dev_hold(rt->dev);
999 /* Prevent duplicate identical routes. */
1000 list_for_each_entry(ert, &net->mctp.routes, list) {
1001 if (mctp_rt_compare_exact(rt, ert)) {
1002 mctp_route_release(rt);
1007 list_add_rcu(&rt->list, &net->mctp.routes);
1012 static int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,
1013 unsigned int daddr_extent, unsigned char type)
1015 struct net *net = dev_net(mdev->dev);
1016 struct mctp_route *rt, *tmp;
1017 mctp_eid_t daddr_end;
1020 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
1023 daddr_end = daddr_start + daddr_extent;
1028 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1029 if (rt->dev == mdev &&
1030 rt->min == daddr_start && rt->max == daddr_end &&
1032 list_del_rcu(&rt->list);
1033 /* TODO: immediate RTM_DELROUTE */
1034 mctp_route_release(rt);
1039 return dropped ? 0 : -ENOENT;
1042 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr)
1044 return mctp_route_add(mdev, addr, 0, 0, RTN_LOCAL);
1047 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr)
1049 return mctp_route_remove(mdev, addr, 0, RTN_LOCAL);
1052 /* removes all entries for a given device */
1053 void mctp_route_remove_dev(struct mctp_dev *mdev)
1055 struct net *net = dev_net(mdev->dev);
1056 struct mctp_route *rt, *tmp;
1059 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1060 if (rt->dev == mdev) {
1061 list_del_rcu(&rt->list);
1062 /* TODO: immediate RTM_DELROUTE */
1063 mctp_route_release(rt);
1068 /* Incoming packet-handling */
1070 static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev,
1071 struct packet_type *pt,
1072 struct net_device *orig_dev)
1074 struct net *net = dev_net(dev);
1075 struct mctp_dev *mdev;
1076 struct mctp_skb_cb *cb;
1077 struct mctp_route *rt;
1078 struct mctp_hdr *mh;
1081 mdev = __mctp_dev_get(dev);
1084 /* basic non-data sanity checks */
1088 if (!pskb_may_pull(skb, sizeof(struct mctp_hdr)))
1091 skb_reset_transport_header(skb);
1092 skb_reset_network_header(skb);
1094 /* We have enough for a header; decode and route */
1096 if (mh->ver < MCTP_VER_MIN || mh->ver > MCTP_VER_MAX)
1099 /* source must be valid unicast or null; drop reserved ranges and
1102 if (!(mctp_address_unicast(mh->src) || mctp_address_null(mh->src)))
1105 /* dest address: as above, but allow broadcast */
1106 if (!(mctp_address_unicast(mh->dest) || mctp_address_null(mh->dest) ||
1107 mctp_address_broadcast(mh->dest)))
1110 /* MCTP drivers must populate halen/haddr */
1111 if (dev->type == ARPHRD_MCTP) {
1114 cb = __mctp_cb(skb);
1117 cb->net = READ_ONCE(mdev->net);
1118 cb->ifindex = dev->ifindex;
1120 rt = mctp_route_lookup(net, cb->net, mh->dest);
1122 /* NULL EID, but addressed to our physical address */
1123 if (!rt && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST)
1124 rt = mctp_route_lookup_null(net, dev);
1129 rt->output(rt, skb);
1130 mctp_route_release(rt);
1133 return NET_RX_SUCCESS;
1141 static struct packet_type mctp_packet_type = {
1142 .type = cpu_to_be16(ETH_P_MCTP),
1143 .func = mctp_pkttype_receive,
1146 /* netlink interface */
1148 static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = {
1149 [RTA_DST] = { .type = NLA_U8 },
1150 [RTA_METRICS] = { .type = NLA_NESTED },
1151 [RTA_OIF] = { .type = NLA_U32 },
1154 /* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing.
1155 * tb must hold RTA_MAX+1 elements.
1157 static int mctp_route_nlparse(struct sk_buff *skb, struct nlmsghdr *nlh,
1158 struct netlink_ext_ack *extack,
1159 struct nlattr **tb, struct rtmsg **rtm,
1160 struct mctp_dev **mdev, mctp_eid_t *daddr_start)
1162 struct net *net = sock_net(skb->sk);
1163 struct net_device *dev;
1164 unsigned int ifindex;
1167 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
1168 rta_mctp_policy, extack);
1170 NL_SET_ERR_MSG(extack, "incorrect format");
1175 NL_SET_ERR_MSG(extack, "dst EID missing");
1178 *daddr_start = nla_get_u8(tb[RTA_DST]);
1181 NL_SET_ERR_MSG(extack, "ifindex missing");
1184 ifindex = nla_get_u32(tb[RTA_OIF]);
1186 *rtm = nlmsg_data(nlh);
1187 if ((*rtm)->rtm_family != AF_MCTP) {
1188 NL_SET_ERR_MSG(extack, "route family must be AF_MCTP");
1192 dev = __dev_get_by_index(net, ifindex);
1194 NL_SET_ERR_MSG(extack, "bad ifindex");
1197 *mdev = mctp_dev_get_rtnl(dev);
1201 if (dev->flags & IFF_LOOPBACK) {
1202 NL_SET_ERR_MSG(extack, "no routes to loopback");
1209 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {
1210 [RTAX_MTU] = { .type = NLA_U32 },
1213 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1214 struct netlink_ext_ack *extack)
1216 struct nlattr *tb[RTA_MAX + 1];
1217 struct nlattr *tbx[RTAX_MAX + 1];
1218 mctp_eid_t daddr_start;
1219 struct mctp_dev *mdev;
1224 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1225 &rtm, &mdev, &daddr_start);
1229 if (rtm->rtm_type != RTN_UNICAST) {
1230 NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST");
1235 if (tb[RTA_METRICS]) {
1236 rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS],
1237 rta_metrics_policy, NULL);
1241 mtu = nla_get_u32(tbx[RTAX_MTU]);
1244 if (rtm->rtm_type != RTN_UNICAST)
1247 rc = mctp_route_add(mdev, daddr_start, rtm->rtm_dst_len, mtu,
1252 static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1253 struct netlink_ext_ack *extack)
1255 struct nlattr *tb[RTA_MAX + 1];
1256 mctp_eid_t daddr_start;
1257 struct mctp_dev *mdev;
1261 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1262 &rtm, &mdev, &daddr_start);
1266 /* we only have unicast routes */
1267 if (rtm->rtm_type != RTN_UNICAST)
1270 rc = mctp_route_remove(mdev, daddr_start, rtm->rtm_dst_len, RTN_UNICAST);
1274 static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt,
1275 u32 portid, u32 seq, int event, unsigned int flags)
1277 struct nlmsghdr *nlh;
1281 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
1285 hdr = nlmsg_data(nlh);
1286 hdr->rtm_family = AF_MCTP;
1288 /* we use the _len fields as a number of EIDs, rather than
1289 * a number of bits in the address
1291 hdr->rtm_dst_len = rt->max - rt->min;
1292 hdr->rtm_src_len = 0;
1294 hdr->rtm_table = RT_TABLE_DEFAULT;
1295 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */
1296 hdr->rtm_scope = RT_SCOPE_LINK; /* TODO: scope in mctp_route? */
1297 hdr->rtm_type = rt->type;
1299 if (nla_put_u8(skb, RTA_DST, rt->min))
1302 metrics = nla_nest_start_noflag(skb, RTA_METRICS);
1307 if (nla_put_u32(skb, RTAX_MTU, rt->mtu))
1311 nla_nest_end(skb, metrics);
1314 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex))
1318 /* TODO: conditional neighbour physaddr? */
1320 nlmsg_end(skb, nlh);
1325 nlmsg_cancel(skb, nlh);
1329 static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb)
1331 struct net *net = sock_net(skb->sk);
1332 struct mctp_route *rt;
1335 /* TODO: allow filtering on route data, possibly under
1339 /* TODO: change to struct overlay */
1340 s_idx = cb->args[0];
1344 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
1347 if (mctp_fill_rtinfo(skb, rt,
1348 NETLINK_CB(cb->skb).portid,
1350 RTM_NEWROUTE, NLM_F_MULTI) < 0)
1360 /* net namespace implementation */
1361 static int __net_init mctp_routes_net_init(struct net *net)
1363 struct netns_mctp *ns = &net->mctp;
1365 INIT_LIST_HEAD(&ns->routes);
1366 INIT_HLIST_HEAD(&ns->binds);
1367 mutex_init(&ns->bind_lock);
1368 INIT_HLIST_HEAD(&ns->keys);
1369 spin_lock_init(&ns->keys_lock);
1370 WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET));
1374 static void __net_exit mctp_routes_net_exit(struct net *net)
1376 struct mctp_route *rt;
1379 list_for_each_entry_rcu(rt, &net->mctp.routes, list)
1380 mctp_route_release(rt);
1384 static struct pernet_operations mctp_net_ops = {
1385 .init = mctp_routes_net_init,
1386 .exit = mctp_routes_net_exit,
1389 int __init mctp_routes_init(void)
1391 dev_add_pack(&mctp_packet_type);
1393 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETROUTE,
1394 NULL, mctp_dump_rtinfo, 0);
1395 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWROUTE,
1396 mctp_newroute, NULL, 0);
1397 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELROUTE,
1398 mctp_delroute, NULL, 0);
1400 return register_pernet_subsys(&mctp_net_ops);
1403 void __exit mctp_routes_exit(void)
1405 unregister_pernet_subsys(&mctp_net_ops);
1406 rtnl_unregister(PF_MCTP, RTM_DELROUTE);
1407 rtnl_unregister(PF_MCTP, RTM_NEWROUTE);
1408 rtnl_unregister(PF_MCTP, RTM_GETROUTE);
1409 dev_remove_pack(&mctp_packet_type);
1412 #if IS_ENABLED(CONFIG_MCTP_TEST)
1413 #include "test/route-test.c"