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 */
508 /* If lookup fails let the device handle daddr==NULL */
509 if (mctp_neigh_lookup(route->dev, hdr->dest, daddr_buf) == 0)
513 rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol),
514 daddr, skb->dev->dev_addr, skb->len);
517 return -EHOSTUNREACH;
520 mctp_flow_prepare_output(skb, route->dev);
522 rc = dev_queue_xmit(skb);
524 rc = net_xmit_errno(rc);
529 /* route alloc/release */
530 static void mctp_route_release(struct mctp_route *rt)
532 if (refcount_dec_and_test(&rt->refs)) {
533 mctp_dev_put(rt->dev);
538 /* returns a route with the refcount at 1 */
539 static struct mctp_route *mctp_route_alloc(void)
541 struct mctp_route *rt;
543 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
547 INIT_LIST_HEAD(&rt->list);
548 refcount_set(&rt->refs, 1);
549 rt->output = mctp_route_discard;
554 unsigned int mctp_default_net(struct net *net)
556 return READ_ONCE(net->mctp.default_net);
559 int mctp_default_net_set(struct net *net, unsigned int index)
563 WRITE_ONCE(net->mctp.default_net, index);
568 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
569 struct mctp_sock *msk)
571 struct netns_mctp *mns = &net->mctp;
573 lockdep_assert_held(&mns->keys_lock);
575 key->expiry = jiffies + mctp_key_lifetime;
576 timer_reduce(&msk->key_expiry, key->expiry);
578 /* we hold the net->key_lock here, allowing updates to both
581 hlist_add_head_rcu(&key->hlist, &mns->keys);
582 hlist_add_head_rcu(&key->sklist, &msk->keys);
583 refcount_inc(&key->refs);
586 /* Allocate a locally-owned tag value for (saddr, daddr), and reserve
587 * it for the socket msk
589 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
590 mctp_eid_t daddr, mctp_eid_t saddr,
591 bool manual, u8 *tagp)
593 struct net *net = sock_net(&msk->sk);
594 struct netns_mctp *mns = &net->mctp;
595 struct mctp_sk_key *key, *tmp;
599 /* for NULL destination EIDs, we may get a response from any peer */
600 if (daddr == MCTP_ADDR_NULL)
601 daddr = MCTP_ADDR_ANY;
603 /* be optimistic, alloc now */
604 key = mctp_key_alloc(msk, saddr, daddr, 0, GFP_KERNEL);
606 return ERR_PTR(-ENOMEM);
608 /* 8 possible tag values */
611 spin_lock_irqsave(&mns->keys_lock, flags);
613 /* Walk through the existing keys, looking for potential conflicting
614 * tags. If we find a conflict, clear that bit from tagbits
616 hlist_for_each_entry(tmp, &mns->keys, hlist) {
617 /* We can check the lookup fields (*_addr, tag) without the
618 * lock held, they don't change over the lifetime of the key.
621 /* if we don't own the tag, it can't conflict */
622 if (tmp->tag & MCTP_HDR_FLAG_TO)
625 if (!(mctp_address_matches(tmp->peer_addr, daddr) &&
626 mctp_address_matches(tmp->local_addr, saddr)))
629 spin_lock(&tmp->lock);
630 /* key must still be valid. If we find a match, clear the
631 * potential tag value
634 tagbits &= ~(1 << tmp->tag);
635 spin_unlock(&tmp->lock);
642 key->tag = __ffs(tagbits);
643 mctp_reserve_tag(net, key, msk);
644 trace_mctp_key_acquire(key);
646 key->manual_alloc = manual;
650 spin_unlock_irqrestore(&mns->keys_lock, flags);
654 return ERR_PTR(-EBUSY);
660 static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk,
662 u8 req_tag, u8 *tagp)
664 struct net *net = sock_net(&msk->sk);
665 struct netns_mctp *mns = &net->mctp;
666 struct mctp_sk_key *key, *tmp;
669 req_tag &= ~(MCTP_TAG_PREALLOC | MCTP_TAG_OWNER);
672 spin_lock_irqsave(&mns->keys_lock, flags);
674 hlist_for_each_entry(tmp, &mns->keys, hlist) {
675 if (tmp->tag != req_tag)
678 if (!mctp_address_matches(tmp->peer_addr, daddr))
681 if (!tmp->manual_alloc)
684 spin_lock(&tmp->lock);
687 refcount_inc(&key->refs);
688 spin_unlock(&tmp->lock);
691 spin_unlock(&tmp->lock);
693 spin_unlock_irqrestore(&mns->keys_lock, flags);
696 return ERR_PTR(-ENOENT);
704 /* routing lookups */
705 static bool mctp_rt_match_eid(struct mctp_route *rt,
706 unsigned int net, mctp_eid_t eid)
708 return READ_ONCE(rt->dev->net) == net &&
709 rt->min <= eid && rt->max >= eid;
712 /* compares match, used for duplicate prevention */
713 static bool mctp_rt_compare_exact(struct mctp_route *rt1,
714 struct mctp_route *rt2)
717 return rt1->dev->net == rt2->dev->net &&
718 rt1->min == rt2->min &&
719 rt1->max == rt2->max;
722 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
725 struct mctp_route *tmp, *rt = NULL;
727 list_for_each_entry_rcu(tmp, &net->mctp.routes, list) {
728 /* TODO: add metrics */
729 if (mctp_rt_match_eid(tmp, dnet, daddr)) {
730 if (refcount_inc_not_zero(&tmp->refs)) {
740 static struct mctp_route *mctp_route_lookup_null(struct net *net,
741 struct net_device *dev)
743 struct mctp_route *rt;
745 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
746 if (rt->dev->dev == dev && rt->type == RTN_LOCAL &&
747 refcount_inc_not_zero(&rt->refs))
754 static int mctp_do_fragment_route(struct mctp_route *rt, struct sk_buff *skb,
755 unsigned int mtu, u8 tag)
757 const unsigned int hlen = sizeof(struct mctp_hdr);
758 struct mctp_hdr *hdr, *hdr2;
759 unsigned int pos, size;
760 struct sk_buff *skb2;
768 if (mtu < hlen + 1) {
773 /* we've got the header */
776 for (pos = 0; pos < skb->len;) {
777 /* size of message payload */
778 size = min(mtu - hlen, skb->len - pos);
780 skb2 = alloc_skb(MCTP_HEADER_MAXLEN + hlen + size, GFP_KERNEL);
786 /* generic skb copy */
787 skb2->protocol = skb->protocol;
788 skb2->priority = skb->priority;
789 skb2->dev = skb->dev;
790 memcpy(skb2->cb, skb->cb, sizeof(skb2->cb));
793 skb_set_owner_w(skb2, skb->sk);
795 /* establish packet */
796 skb_reserve(skb2, MCTP_HEADER_MAXLEN);
797 skb_reset_network_header(skb2);
798 skb_put(skb2, hlen + size);
799 skb2->transport_header = skb2->network_header + hlen;
801 /* copy header fields, calculate SOM/EOM flags & seq */
802 hdr2 = mctp_hdr(skb2);
803 hdr2->ver = hdr->ver;
804 hdr2->dest = hdr->dest;
805 hdr2->src = hdr->src;
806 hdr2->flags_seq_tag = tag &
807 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
810 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
812 if (pos + size == skb->len)
813 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
815 hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT;
817 /* copy message payload */
818 skb_copy_bits(skb, pos, skb_transport_header(skb2), size);
821 rc = rt->output(rt, skb2);
825 seq = (seq + 1) & MCTP_HDR_SEQ_MASK;
833 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
834 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag)
836 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);
837 struct mctp_skb_cb *cb = mctp_cb(skb);
838 struct mctp_route tmp_rt = {0};
839 struct mctp_sk_key *key;
840 struct mctp_hdr *hdr;
852 if (WARN_ON(!rt->dev))
855 } else if (cb->ifindex) {
856 struct net_device *dev;
862 dev = dev_get_by_index_rcu(sock_net(sk), cb->ifindex);
867 rt->dev = __mctp_dev_get(dev);
873 /* establish temporary route - we set up enough to keep
874 * mctp_route_output happy
876 rt->output = mctp_route_output;
883 spin_lock_irqsave(&rt->dev->addrs_lock, flags);
884 if (rt->dev->num_addrs == 0) {
887 /* use the outbound interface's first address as our source */
888 saddr = rt->dev->addrs[0];
891 spin_unlock_irqrestore(&rt->dev->addrs_lock, flags);
896 if (req_tag & MCTP_TAG_OWNER) {
897 if (req_tag & MCTP_TAG_PREALLOC)
898 key = mctp_lookup_prealloc_tag(msk, daddr,
901 key = mctp_alloc_local_tag(msk, daddr, saddr,
908 mctp_skb_set_flow(skb, key);
909 /* done with the key in this scope */
911 tag |= MCTP_HDR_FLAG_TO;
914 tag = req_tag & MCTP_TAG_MASK;
917 skb->protocol = htons(ETH_P_MCTP);
919 skb_reset_transport_header(skb);
920 skb_push(skb, sizeof(struct mctp_hdr));
921 skb_reset_network_header(skb);
922 skb->dev = rt->dev->dev;
924 /* cb->net will have been set on initial ingress */
927 /* set up common header fields */
933 mtu = mctp_route_mtu(rt);
935 if (skb->len + sizeof(struct mctp_hdr) <= mtu) {
936 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM |
937 MCTP_HDR_FLAG_EOM | tag;
938 rc = rt->output(rt, skb);
940 rc = mctp_do_fragment_route(rt, skb, mtu, tag);
945 mctp_route_release(rt);
947 mctp_dev_put(tmp_rt.dev);
952 /* route management */
953 static int mctp_route_add(struct mctp_dev *mdev, mctp_eid_t daddr_start,
954 unsigned int daddr_extent, unsigned int mtu,
957 int (*rtfn)(struct mctp_route *rt, struct sk_buff *skb);
958 struct net *net = dev_net(mdev->dev);
959 struct mctp_route *rt, *ert;
961 if (!mctp_address_unicast(daddr_start))
964 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
969 rtfn = mctp_route_input;
972 rtfn = mctp_route_output;
978 rt = mctp_route_alloc();
982 rt->min = daddr_start;
983 rt->max = daddr_start + daddr_extent;
986 mctp_dev_hold(rt->dev);
991 /* Prevent duplicate identical routes. */
992 list_for_each_entry(ert, &net->mctp.routes, list) {
993 if (mctp_rt_compare_exact(rt, ert)) {
994 mctp_route_release(rt);
999 list_add_rcu(&rt->list, &net->mctp.routes);
1004 static int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,
1005 unsigned int daddr_extent, unsigned char type)
1007 struct net *net = dev_net(mdev->dev);
1008 struct mctp_route *rt, *tmp;
1009 mctp_eid_t daddr_end;
1012 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
1015 daddr_end = daddr_start + daddr_extent;
1020 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1021 if (rt->dev == mdev &&
1022 rt->min == daddr_start && rt->max == daddr_end &&
1024 list_del_rcu(&rt->list);
1025 /* TODO: immediate RTM_DELROUTE */
1026 mctp_route_release(rt);
1031 return dropped ? 0 : -ENOENT;
1034 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr)
1036 return mctp_route_add(mdev, addr, 0, 0, RTN_LOCAL);
1039 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr)
1041 return mctp_route_remove(mdev, addr, 0, RTN_LOCAL);
1044 /* removes all entries for a given device */
1045 void mctp_route_remove_dev(struct mctp_dev *mdev)
1047 struct net *net = dev_net(mdev->dev);
1048 struct mctp_route *rt, *tmp;
1051 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1052 if (rt->dev == mdev) {
1053 list_del_rcu(&rt->list);
1054 /* TODO: immediate RTM_DELROUTE */
1055 mctp_route_release(rt);
1060 /* Incoming packet-handling */
1062 static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev,
1063 struct packet_type *pt,
1064 struct net_device *orig_dev)
1066 struct net *net = dev_net(dev);
1067 struct mctp_dev *mdev;
1068 struct mctp_skb_cb *cb;
1069 struct mctp_route *rt;
1070 struct mctp_hdr *mh;
1073 mdev = __mctp_dev_get(dev);
1076 /* basic non-data sanity checks */
1080 if (!pskb_may_pull(skb, sizeof(struct mctp_hdr)))
1083 skb_reset_transport_header(skb);
1084 skb_reset_network_header(skb);
1086 /* We have enough for a header; decode and route */
1088 if (mh->ver < MCTP_VER_MIN || mh->ver > MCTP_VER_MAX)
1091 /* source must be valid unicast or null; drop reserved ranges and
1094 if (!(mctp_address_unicast(mh->src) || mctp_address_null(mh->src)))
1097 /* dest address: as above, but allow broadcast */
1098 if (!(mctp_address_unicast(mh->dest) || mctp_address_null(mh->dest) ||
1099 mctp_address_broadcast(mh->dest)))
1102 /* MCTP drivers must populate halen/haddr */
1103 if (dev->type == ARPHRD_MCTP) {
1106 cb = __mctp_cb(skb);
1109 cb->net = READ_ONCE(mdev->net);
1110 cb->ifindex = dev->ifindex;
1112 rt = mctp_route_lookup(net, cb->net, mh->dest);
1114 /* NULL EID, but addressed to our physical address */
1115 if (!rt && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST)
1116 rt = mctp_route_lookup_null(net, dev);
1121 rt->output(rt, skb);
1122 mctp_route_release(rt);
1125 return NET_RX_SUCCESS;
1133 static struct packet_type mctp_packet_type = {
1134 .type = cpu_to_be16(ETH_P_MCTP),
1135 .func = mctp_pkttype_receive,
1138 /* netlink interface */
1140 static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = {
1141 [RTA_DST] = { .type = NLA_U8 },
1142 [RTA_METRICS] = { .type = NLA_NESTED },
1143 [RTA_OIF] = { .type = NLA_U32 },
1146 /* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing.
1147 * tb must hold RTA_MAX+1 elements.
1149 static int mctp_route_nlparse(struct sk_buff *skb, struct nlmsghdr *nlh,
1150 struct netlink_ext_ack *extack,
1151 struct nlattr **tb, struct rtmsg **rtm,
1152 struct mctp_dev **mdev, mctp_eid_t *daddr_start)
1154 struct net *net = sock_net(skb->sk);
1155 struct net_device *dev;
1156 unsigned int ifindex;
1159 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
1160 rta_mctp_policy, extack);
1162 NL_SET_ERR_MSG(extack, "incorrect format");
1167 NL_SET_ERR_MSG(extack, "dst EID missing");
1170 *daddr_start = nla_get_u8(tb[RTA_DST]);
1173 NL_SET_ERR_MSG(extack, "ifindex missing");
1176 ifindex = nla_get_u32(tb[RTA_OIF]);
1178 *rtm = nlmsg_data(nlh);
1179 if ((*rtm)->rtm_family != AF_MCTP) {
1180 NL_SET_ERR_MSG(extack, "route family must be AF_MCTP");
1184 dev = __dev_get_by_index(net, ifindex);
1186 NL_SET_ERR_MSG(extack, "bad ifindex");
1189 *mdev = mctp_dev_get_rtnl(dev);
1193 if (dev->flags & IFF_LOOPBACK) {
1194 NL_SET_ERR_MSG(extack, "no routes to loopback");
1201 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {
1202 [RTAX_MTU] = { .type = NLA_U32 },
1205 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1206 struct netlink_ext_ack *extack)
1208 struct nlattr *tb[RTA_MAX + 1];
1209 struct nlattr *tbx[RTAX_MAX + 1];
1210 mctp_eid_t daddr_start;
1211 struct mctp_dev *mdev;
1216 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1217 &rtm, &mdev, &daddr_start);
1221 if (rtm->rtm_type != RTN_UNICAST) {
1222 NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST");
1227 if (tb[RTA_METRICS]) {
1228 rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS],
1229 rta_metrics_policy, NULL);
1233 mtu = nla_get_u32(tbx[RTAX_MTU]);
1236 if (rtm->rtm_type != RTN_UNICAST)
1239 rc = mctp_route_add(mdev, daddr_start, rtm->rtm_dst_len, mtu,
1244 static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1245 struct netlink_ext_ack *extack)
1247 struct nlattr *tb[RTA_MAX + 1];
1248 mctp_eid_t daddr_start;
1249 struct mctp_dev *mdev;
1253 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1254 &rtm, &mdev, &daddr_start);
1258 /* we only have unicast routes */
1259 if (rtm->rtm_type != RTN_UNICAST)
1262 rc = mctp_route_remove(mdev, daddr_start, rtm->rtm_dst_len, RTN_UNICAST);
1266 static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt,
1267 u32 portid, u32 seq, int event, unsigned int flags)
1269 struct nlmsghdr *nlh;
1273 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
1277 hdr = nlmsg_data(nlh);
1278 hdr->rtm_family = AF_MCTP;
1280 /* we use the _len fields as a number of EIDs, rather than
1281 * a number of bits in the address
1283 hdr->rtm_dst_len = rt->max - rt->min;
1284 hdr->rtm_src_len = 0;
1286 hdr->rtm_table = RT_TABLE_DEFAULT;
1287 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */
1288 hdr->rtm_scope = RT_SCOPE_LINK; /* TODO: scope in mctp_route? */
1289 hdr->rtm_type = rt->type;
1291 if (nla_put_u8(skb, RTA_DST, rt->min))
1294 metrics = nla_nest_start_noflag(skb, RTA_METRICS);
1299 if (nla_put_u32(skb, RTAX_MTU, rt->mtu))
1303 nla_nest_end(skb, metrics);
1306 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex))
1310 /* TODO: conditional neighbour physaddr? */
1312 nlmsg_end(skb, nlh);
1317 nlmsg_cancel(skb, nlh);
1321 static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb)
1323 struct net *net = sock_net(skb->sk);
1324 struct mctp_route *rt;
1327 /* TODO: allow filtering on route data, possibly under
1331 /* TODO: change to struct overlay */
1332 s_idx = cb->args[0];
1336 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
1339 if (mctp_fill_rtinfo(skb, rt,
1340 NETLINK_CB(cb->skb).portid,
1342 RTM_NEWROUTE, NLM_F_MULTI) < 0)
1352 /* net namespace implementation */
1353 static int __net_init mctp_routes_net_init(struct net *net)
1355 struct netns_mctp *ns = &net->mctp;
1357 INIT_LIST_HEAD(&ns->routes);
1358 INIT_HLIST_HEAD(&ns->binds);
1359 mutex_init(&ns->bind_lock);
1360 INIT_HLIST_HEAD(&ns->keys);
1361 spin_lock_init(&ns->keys_lock);
1362 WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET));
1366 static void __net_exit mctp_routes_net_exit(struct net *net)
1368 struct mctp_route *rt;
1371 list_for_each_entry_rcu(rt, &net->mctp.routes, list)
1372 mctp_route_release(rt);
1376 static struct pernet_operations mctp_net_ops = {
1377 .init = mctp_routes_net_init,
1378 .exit = mctp_routes_net_exit,
1381 int __init mctp_routes_init(void)
1383 dev_add_pack(&mctp_packet_type);
1385 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETROUTE,
1386 NULL, mctp_dump_rtinfo, 0);
1387 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWROUTE,
1388 mctp_newroute, NULL, 0);
1389 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELROUTE,
1390 mctp_delroute, NULL, 0);
1392 return register_pernet_subsys(&mctp_net_ops);
1395 void __exit mctp_routes_exit(void)
1397 unregister_pernet_subsys(&mctp_net_ops);
1398 rtnl_unregister(PF_MCTP, RTM_DELROUTE);
1399 rtnl_unregister(PF_MCTP, RTM_NEWROUTE);
1400 rtnl_unregister(PF_MCTP, RTM_GETROUTE);
1401 dev_remove_pack(&mctp_packet_type);
1404 #if IS_ENABLED(CONFIG_MCTP_TEST)
1405 #include "test/route-test.c"