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 (msk->bind_addr != MCTP_ADDR_ANY &&
68 msk->bind_addr != mh->dest)
77 static bool mctp_key_match(struct mctp_sk_key *key, mctp_eid_t local,
78 mctp_eid_t peer, u8 tag)
80 if (key->local_addr != local)
83 if (key->peer_addr != peer)
92 /* returns a key (with key->lock held, and refcounted), or NULL if no such
95 static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb,
97 unsigned long *irqflags)
98 __acquires(&key->lock)
100 struct mctp_sk_key *key, *ret;
106 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
109 spin_lock_irqsave(&net->mctp.keys_lock, flags);
111 hlist_for_each_entry(key, &net->mctp.keys, hlist) {
112 if (!mctp_key_match(key, mh->dest, peer, tag))
115 spin_lock(&key->lock);
117 refcount_inc(&key->refs);
121 spin_unlock(&key->lock);
125 spin_unlock(&net->mctp.keys_lock);
128 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
134 static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
135 mctp_eid_t local, mctp_eid_t peer,
138 struct mctp_sk_key *key;
140 key = kzalloc(sizeof(*key), gfp);
144 key->peer_addr = peer;
145 key->local_addr = local;
149 spin_lock_init(&key->lock);
150 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);
172 static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk)
174 struct net *net = sock_net(&msk->sk);
175 struct mctp_sk_key *tmp;
179 spin_lock_irqsave(&net->mctp.keys_lock, flags);
181 hlist_for_each_entry(tmp, &net->mctp.keys, hlist) {
182 if (mctp_key_match(tmp, key->local_addr, key->peer_addr,
184 spin_lock(&tmp->lock);
187 spin_unlock(&tmp->lock);
194 refcount_inc(&key->refs);
195 key->expiry = jiffies + mctp_key_lifetime;
196 timer_reduce(&msk->key_expiry, key->expiry);
198 hlist_add_head(&key->hlist, &net->mctp.keys);
199 hlist_add_head(&key->sklist, &msk->keys);
202 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
207 /* We're done with the key; unset valid and remove from lists. There may still
208 * be outstanding refs on the key though...
210 static void __mctp_key_unlock_drop(struct mctp_sk_key *key, struct net *net,
212 __releases(&key->lock)
216 skb = key->reasm_head;
217 key->reasm_head = NULL;
218 key->reasm_dead = true;
220 mctp_dev_release_key(key->dev, key);
221 spin_unlock_irqrestore(&key->lock, flags);
223 spin_lock_irqsave(&net->mctp.keys_lock, flags);
224 hlist_del(&key->hlist);
225 hlist_del(&key->sklist);
226 spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
228 /* one unref for the lists */
231 /* and one for the local reference */
237 #ifdef CONFIG_MCTP_FLOWS
238 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key)
240 struct mctp_flow *flow;
242 flow = skb_ext_add(skb, SKB_EXT_MCTP);
246 refcount_inc(&key->refs);
250 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev)
252 struct mctp_sk_key *key;
253 struct mctp_flow *flow;
255 flow = skb_ext_find(skb, SKB_EXT_MCTP);
261 if (WARN_ON(key->dev && key->dev != dev))
264 mctp_dev_set_key(dev, key);
267 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) {}
268 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) {}
271 static int mctp_frag_queue(struct mctp_sk_key *key, struct sk_buff *skb)
273 struct mctp_hdr *hdr = mctp_hdr(skb);
274 u8 exp_seq, this_seq;
276 this_seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT)
279 if (!key->reasm_head) {
280 key->reasm_head = skb;
281 key->reasm_tailp = &(skb_shinfo(skb)->frag_list);
282 key->last_seq = this_seq;
286 exp_seq = (key->last_seq + 1) & MCTP_HDR_SEQ_MASK;
288 if (this_seq != exp_seq)
291 if (key->reasm_head->len + skb->len > mctp_message_maxlen)
296 *key->reasm_tailp = skb;
297 key->reasm_tailp = &skb->next;
299 key->last_seq = this_seq;
301 key->reasm_head->data_len += skb->len;
302 key->reasm_head->len += skb->len;
303 key->reasm_head->truesize += skb->truesize;
308 static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
310 struct net *net = dev_net(skb->dev);
311 struct mctp_sk_key *key;
312 struct mctp_sock *msk;
321 /* we may be receiving a locally-routed packet; drop source sk
326 /* ensure we have enough data for a header and a type */
327 if (skb->len < sizeof(struct mctp_hdr) + 1)
330 /* grab header, advance data ptr */
332 skb_pull(skb, sizeof(struct mctp_hdr));
337 flags = mh->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
338 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
342 /* lookup socket / reasm context, exactly matching (src,dest,tag).
343 * we hold a ref on the key, and key->lock held.
345 key = mctp_lookup_key(net, skb, mh->src, &f);
347 if (flags & MCTP_HDR_FLAG_SOM) {
349 msk = container_of(key->sk, struct mctp_sock, sk);
351 /* first response to a broadcast? do a more general
352 * key lookup to find the socket, but don't use this
353 * key for reassembly - we'll create a more specific
354 * one for future packets if required (ie, !EOM).
356 key = mctp_lookup_key(net, skb, MCTP_ADDR_ANY, &f);
358 msk = container_of(key->sk,
359 struct mctp_sock, sk);
360 spin_unlock_irqrestore(&key->lock, f);
366 if (!key && !msk && (tag & MCTP_HDR_FLAG_TO))
367 msk = mctp_lookup_bind(net, skb);
374 /* single-packet message? deliver to socket, clean up any
377 if (flags & MCTP_HDR_FLAG_EOM) {
378 sock_queue_rcv_skb(&msk->sk, skb);
380 /* we've hit a pending reassembly; not much we
383 trace_mctp_key_release(key,
384 MCTP_TRACE_KEY_REPLIED);
385 __mctp_key_unlock_drop(key, net, f);
392 /* broadcast response or a bind() - create a key for further
393 * packets for this message
396 key = mctp_key_alloc(msk, mh->dest, mh->src,
403 /* we can queue without the key lock here, as the
404 * key isn't observable yet
406 mctp_frag_queue(key, skb);
408 /* if the key_add fails, we've raced with another
409 * SOM packet with the same src, dest and tag. There's
410 * no way to distinguish future packets, so all we
411 * can do is drop; we'll free the skb on exit from
414 rc = mctp_key_add(key, msk);
418 trace_mctp_key_acquire(key);
420 /* we don't need to release key->lock on exit */
426 if (key->reasm_head || key->reasm_dead) {
427 /* duplicate start? drop everything */
428 trace_mctp_key_release(key,
429 MCTP_TRACE_KEY_INVALIDATED);
430 __mctp_key_unlock_drop(key, net, f);
434 rc = mctp_frag_queue(key, skb);
439 /* this packet continues a previous message; reassemble
440 * using the message-specific key
443 /* we need to be continuing an existing reassembly... */
444 if (!key->reasm_head)
447 rc = mctp_frag_queue(key, skb);
449 /* end of message? deliver to socket, and we're done with
450 * the reassembly/response key
452 if (!rc && flags & MCTP_HDR_FLAG_EOM) {
453 sock_queue_rcv_skb(key->sk, key->reasm_head);
454 key->reasm_head = NULL;
455 trace_mctp_key_release(key, MCTP_TRACE_KEY_REPLIED);
456 __mctp_key_unlock_drop(key, net, f);
461 /* not a start, no matching key */
468 spin_unlock_irqrestore(&key->lock, f);
477 static unsigned int mctp_route_mtu(struct mctp_route *rt)
479 return rt->mtu ?: READ_ONCE(rt->dev->dev->mtu);
482 static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
484 struct mctp_skb_cb *cb = mctp_cb(skb);
485 struct mctp_hdr *hdr = mctp_hdr(skb);
486 char daddr_buf[MAX_ADDR_LEN];
491 skb->protocol = htons(ETH_P_MCTP);
493 mtu = READ_ONCE(skb->dev->mtu);
494 if (skb->len > mtu) {
500 /* direct route; use the hwaddr we stashed in sendmsg */
503 /* If lookup fails let the device handle daddr==NULL */
504 if (mctp_neigh_lookup(route->dev, hdr->dest, daddr_buf) == 0)
508 rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol),
509 daddr, skb->dev->dev_addr, skb->len);
512 return -EHOSTUNREACH;
515 mctp_flow_prepare_output(skb, route->dev);
517 rc = dev_queue_xmit(skb);
519 rc = net_xmit_errno(rc);
524 /* route alloc/release */
525 static void mctp_route_release(struct mctp_route *rt)
527 if (refcount_dec_and_test(&rt->refs)) {
528 mctp_dev_put(rt->dev);
533 /* returns a route with the refcount at 1 */
534 static struct mctp_route *mctp_route_alloc(void)
536 struct mctp_route *rt;
538 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
542 INIT_LIST_HEAD(&rt->list);
543 refcount_set(&rt->refs, 1);
544 rt->output = mctp_route_discard;
549 unsigned int mctp_default_net(struct net *net)
551 return READ_ONCE(net->mctp.default_net);
554 int mctp_default_net_set(struct net *net, unsigned int index)
558 WRITE_ONCE(net->mctp.default_net, index);
563 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
564 struct mctp_sock *msk)
566 struct netns_mctp *mns = &net->mctp;
568 lockdep_assert_held(&mns->keys_lock);
570 key->expiry = jiffies + mctp_key_lifetime;
571 timer_reduce(&msk->key_expiry, key->expiry);
573 /* we hold the net->key_lock here, allowing updates to both
576 hlist_add_head_rcu(&key->hlist, &mns->keys);
577 hlist_add_head_rcu(&key->sklist, &msk->keys);
578 refcount_inc(&key->refs);
581 /* Allocate a locally-owned tag value for (saddr, daddr), and reserve
582 * it for the socket msk
584 static struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
586 mctp_eid_t daddr, u8 *tagp)
588 struct net *net = sock_net(&msk->sk);
589 struct netns_mctp *mns = &net->mctp;
590 struct mctp_sk_key *key, *tmp;
594 /* for NULL destination EIDs, we may get a response from any peer */
595 if (daddr == MCTP_ADDR_NULL)
596 daddr = MCTP_ADDR_ANY;
598 /* be optimistic, alloc now */
599 key = mctp_key_alloc(msk, saddr, daddr, 0, GFP_KERNEL);
601 return ERR_PTR(-ENOMEM);
603 /* 8 possible tag values */
606 spin_lock_irqsave(&mns->keys_lock, flags);
608 /* Walk through the existing keys, looking for potential conflicting
609 * tags. If we find a conflict, clear that bit from tagbits
611 hlist_for_each_entry(tmp, &mns->keys, hlist) {
612 /* We can check the lookup fields (*_addr, tag) without the
613 * lock held, they don't change over the lifetime of the key.
616 /* if we don't own the tag, it can't conflict */
617 if (tmp->tag & MCTP_HDR_FLAG_TO)
620 if (!((tmp->peer_addr == daddr ||
621 tmp->peer_addr == MCTP_ADDR_ANY) &&
622 tmp->local_addr == saddr))
625 spin_lock(&tmp->lock);
626 /* key must still be valid. If we find a match, clear the
627 * potential tag value
630 tagbits &= ~(1 << tmp->tag);
631 spin_unlock(&tmp->lock);
638 key->tag = __ffs(tagbits);
639 mctp_reserve_tag(net, key, msk);
640 trace_mctp_key_acquire(key);
645 spin_unlock_irqrestore(&mns->keys_lock, flags);
649 return ERR_PTR(-EBUSY);
655 /* routing lookups */
656 static bool mctp_rt_match_eid(struct mctp_route *rt,
657 unsigned int net, mctp_eid_t eid)
659 return READ_ONCE(rt->dev->net) == net &&
660 rt->min <= eid && rt->max >= eid;
663 /* compares match, used for duplicate prevention */
664 static bool mctp_rt_compare_exact(struct mctp_route *rt1,
665 struct mctp_route *rt2)
668 return rt1->dev->net == rt2->dev->net &&
669 rt1->min == rt2->min &&
670 rt1->max == rt2->max;
673 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
676 struct mctp_route *tmp, *rt = NULL;
678 list_for_each_entry_rcu(tmp, &net->mctp.routes, list) {
679 /* TODO: add metrics */
680 if (mctp_rt_match_eid(tmp, dnet, daddr)) {
681 if (refcount_inc_not_zero(&tmp->refs)) {
691 static struct mctp_route *mctp_route_lookup_null(struct net *net,
692 struct net_device *dev)
694 struct mctp_route *rt;
696 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
697 if (rt->dev->dev == dev && rt->type == RTN_LOCAL &&
698 refcount_inc_not_zero(&rt->refs))
705 static int mctp_do_fragment_route(struct mctp_route *rt, struct sk_buff *skb,
706 unsigned int mtu, u8 tag)
708 const unsigned int hlen = sizeof(struct mctp_hdr);
709 struct mctp_hdr *hdr, *hdr2;
710 unsigned int pos, size;
711 struct sk_buff *skb2;
719 if (mtu < hlen + 1) {
724 /* we've got the header */
727 for (pos = 0; pos < skb->len;) {
728 /* size of message payload */
729 size = min(mtu - hlen, skb->len - pos);
731 skb2 = alloc_skb(MCTP_HEADER_MAXLEN + hlen + size, GFP_KERNEL);
737 /* generic skb copy */
738 skb2->protocol = skb->protocol;
739 skb2->priority = skb->priority;
740 skb2->dev = skb->dev;
741 memcpy(skb2->cb, skb->cb, sizeof(skb2->cb));
744 skb_set_owner_w(skb2, skb->sk);
746 /* establish packet */
747 skb_reserve(skb2, MCTP_HEADER_MAXLEN);
748 skb_reset_network_header(skb2);
749 skb_put(skb2, hlen + size);
750 skb2->transport_header = skb2->network_header + hlen;
752 /* copy header fields, calculate SOM/EOM flags & seq */
753 hdr2 = mctp_hdr(skb2);
754 hdr2->ver = hdr->ver;
755 hdr2->dest = hdr->dest;
756 hdr2->src = hdr->src;
757 hdr2->flags_seq_tag = tag &
758 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO);
761 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
763 if (pos + size == skb->len)
764 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
766 hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT;
768 /* copy message payload */
769 skb_copy_bits(skb, pos, skb_transport_header(skb2), size);
772 rc = rt->output(rt, skb2);
776 seq = (seq + 1) & MCTP_HDR_SEQ_MASK;
784 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
785 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag)
787 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);
788 struct mctp_skb_cb *cb = mctp_cb(skb);
789 struct mctp_route tmp_rt;
790 struct mctp_sk_key *key;
791 struct net_device *dev;
792 struct mctp_hdr *hdr;
806 if (WARN_ON(!rt->dev))
809 } else if (cb->ifindex) {
814 dev = dev_get_by_index_rcu(sock_net(sk), cb->ifindex);
820 rt->dev = __mctp_dev_get(dev);
826 /* establish temporary route - we set up enough to keep
827 * mctp_route_output happy
829 rt->output = mctp_route_output;
836 spin_lock_irqsave(&rt->dev->addrs_lock, flags);
837 if (rt->dev->num_addrs == 0) {
840 /* use the outbound interface's first address as our source */
841 saddr = rt->dev->addrs[0];
844 spin_unlock_irqrestore(&rt->dev->addrs_lock, flags);
849 if (req_tag & MCTP_HDR_FLAG_TO) {
850 key = mctp_alloc_local_tag(msk, saddr, daddr, &tag);
855 mctp_skb_set_flow(skb, key);
856 /* done with the key in this scope */
858 tag |= MCTP_HDR_FLAG_TO;
864 skb->protocol = htons(ETH_P_MCTP);
866 skb_reset_transport_header(skb);
867 skb_push(skb, sizeof(struct mctp_hdr));
868 skb_reset_network_header(skb);
869 skb->dev = rt->dev->dev;
871 /* cb->net will have been set on initial ingress */
874 /* set up common header fields */
880 mtu = mctp_route_mtu(rt);
882 if (skb->len + sizeof(struct mctp_hdr) <= mtu) {
883 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM |
884 MCTP_HDR_FLAG_EOM | tag;
885 rc = rt->output(rt, skb);
887 rc = mctp_do_fragment_route(rt, skb, mtu, tag);
892 mctp_route_release(rt);
900 /* route management */
901 static int mctp_route_add(struct mctp_dev *mdev, mctp_eid_t daddr_start,
902 unsigned int daddr_extent, unsigned int mtu,
905 int (*rtfn)(struct mctp_route *rt, struct sk_buff *skb);
906 struct net *net = dev_net(mdev->dev);
907 struct mctp_route *rt, *ert;
909 if (!mctp_address_ok(daddr_start))
912 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
917 rtfn = mctp_route_input;
920 rtfn = mctp_route_output;
926 rt = mctp_route_alloc();
930 rt->min = daddr_start;
931 rt->max = daddr_start + daddr_extent;
934 mctp_dev_hold(rt->dev);
939 /* Prevent duplicate identical routes. */
940 list_for_each_entry(ert, &net->mctp.routes, list) {
941 if (mctp_rt_compare_exact(rt, ert)) {
942 mctp_route_release(rt);
947 list_add_rcu(&rt->list, &net->mctp.routes);
952 static int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,
953 unsigned int daddr_extent, unsigned char type)
955 struct net *net = dev_net(mdev->dev);
956 struct mctp_route *rt, *tmp;
957 mctp_eid_t daddr_end;
960 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255)
963 daddr_end = daddr_start + daddr_extent;
968 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
969 if (rt->dev == mdev &&
970 rt->min == daddr_start && rt->max == daddr_end &&
972 list_del_rcu(&rt->list);
973 /* TODO: immediate RTM_DELROUTE */
974 mctp_route_release(rt);
979 return dropped ? 0 : -ENOENT;
982 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr)
984 return mctp_route_add(mdev, addr, 0, 0, RTN_LOCAL);
987 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr)
989 return mctp_route_remove(mdev, addr, 0, RTN_LOCAL);
992 /* removes all entries for a given device */
993 void mctp_route_remove_dev(struct mctp_dev *mdev)
995 struct net *net = dev_net(mdev->dev);
996 struct mctp_route *rt, *tmp;
999 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1000 if (rt->dev == mdev) {
1001 list_del_rcu(&rt->list);
1002 /* TODO: immediate RTM_DELROUTE */
1003 mctp_route_release(rt);
1008 /* Incoming packet-handling */
1010 static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev,
1011 struct packet_type *pt,
1012 struct net_device *orig_dev)
1014 struct net *net = dev_net(dev);
1015 struct mctp_dev *mdev;
1016 struct mctp_skb_cb *cb;
1017 struct mctp_route *rt;
1018 struct mctp_hdr *mh;
1021 mdev = __mctp_dev_get(dev);
1024 /* basic non-data sanity checks */
1028 if (!pskb_may_pull(skb, sizeof(struct mctp_hdr)))
1031 skb_reset_transport_header(skb);
1032 skb_reset_network_header(skb);
1034 /* We have enough for a header; decode and route */
1036 if (mh->ver < MCTP_VER_MIN || mh->ver > MCTP_VER_MAX)
1039 /* MCTP drivers must populate halen/haddr */
1040 if (dev->type == ARPHRD_MCTP) {
1043 cb = __mctp_cb(skb);
1046 cb->net = READ_ONCE(mdev->net);
1047 cb->ifindex = dev->ifindex;
1049 rt = mctp_route_lookup(net, cb->net, mh->dest);
1051 /* NULL EID, but addressed to our physical address */
1052 if (!rt && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST)
1053 rt = mctp_route_lookup_null(net, dev);
1058 rt->output(rt, skb);
1059 mctp_route_release(rt);
1061 return NET_RX_SUCCESS;
1068 static struct packet_type mctp_packet_type = {
1069 .type = cpu_to_be16(ETH_P_MCTP),
1070 .func = mctp_pkttype_receive,
1073 /* netlink interface */
1075 static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = {
1076 [RTA_DST] = { .type = NLA_U8 },
1077 [RTA_METRICS] = { .type = NLA_NESTED },
1078 [RTA_OIF] = { .type = NLA_U32 },
1081 /* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing.
1082 * tb must hold RTA_MAX+1 elements.
1084 static int mctp_route_nlparse(struct sk_buff *skb, struct nlmsghdr *nlh,
1085 struct netlink_ext_ack *extack,
1086 struct nlattr **tb, struct rtmsg **rtm,
1087 struct mctp_dev **mdev, mctp_eid_t *daddr_start)
1089 struct net *net = sock_net(skb->sk);
1090 struct net_device *dev;
1091 unsigned int ifindex;
1094 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
1095 rta_mctp_policy, extack);
1097 NL_SET_ERR_MSG(extack, "incorrect format");
1102 NL_SET_ERR_MSG(extack, "dst EID missing");
1105 *daddr_start = nla_get_u8(tb[RTA_DST]);
1108 NL_SET_ERR_MSG(extack, "ifindex missing");
1111 ifindex = nla_get_u32(tb[RTA_OIF]);
1113 *rtm = nlmsg_data(nlh);
1114 if ((*rtm)->rtm_family != AF_MCTP) {
1115 NL_SET_ERR_MSG(extack, "route family must be AF_MCTP");
1119 dev = __dev_get_by_index(net, ifindex);
1121 NL_SET_ERR_MSG(extack, "bad ifindex");
1124 *mdev = mctp_dev_get_rtnl(dev);
1128 if (dev->flags & IFF_LOOPBACK) {
1129 NL_SET_ERR_MSG(extack, "no routes to loopback");
1136 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {
1137 [RTAX_MTU] = { .type = NLA_U32 },
1140 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1141 struct netlink_ext_ack *extack)
1143 struct nlattr *tb[RTA_MAX + 1];
1144 struct nlattr *tbx[RTAX_MAX + 1];
1145 mctp_eid_t daddr_start;
1146 struct mctp_dev *mdev;
1151 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1152 &rtm, &mdev, &daddr_start);
1156 if (rtm->rtm_type != RTN_UNICAST) {
1157 NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST");
1162 if (tb[RTA_METRICS]) {
1163 rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS],
1164 rta_metrics_policy, NULL);
1168 mtu = nla_get_u32(tbx[RTAX_MTU]);
1171 if (rtm->rtm_type != RTN_UNICAST)
1174 rc = mctp_route_add(mdev, daddr_start, rtm->rtm_dst_len, mtu,
1179 static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
1180 struct netlink_ext_ack *extack)
1182 struct nlattr *tb[RTA_MAX + 1];
1183 mctp_eid_t daddr_start;
1184 struct mctp_dev *mdev;
1188 rc = mctp_route_nlparse(skb, nlh, extack, tb,
1189 &rtm, &mdev, &daddr_start);
1193 /* we only have unicast routes */
1194 if (rtm->rtm_type != RTN_UNICAST)
1197 rc = mctp_route_remove(mdev, daddr_start, rtm->rtm_dst_len, RTN_UNICAST);
1201 static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt,
1202 u32 portid, u32 seq, int event, unsigned int flags)
1204 struct nlmsghdr *nlh;
1208 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
1212 hdr = nlmsg_data(nlh);
1213 hdr->rtm_family = AF_MCTP;
1215 /* we use the _len fields as a number of EIDs, rather than
1216 * a number of bits in the address
1218 hdr->rtm_dst_len = rt->max - rt->min;
1219 hdr->rtm_src_len = 0;
1221 hdr->rtm_table = RT_TABLE_DEFAULT;
1222 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */
1223 hdr->rtm_scope = RT_SCOPE_LINK; /* TODO: scope in mctp_route? */
1224 hdr->rtm_type = rt->type;
1226 if (nla_put_u8(skb, RTA_DST, rt->min))
1229 metrics = nla_nest_start_noflag(skb, RTA_METRICS);
1234 if (nla_put_u32(skb, RTAX_MTU, rt->mtu))
1238 nla_nest_end(skb, metrics);
1241 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex))
1245 /* TODO: conditional neighbour physaddr? */
1247 nlmsg_end(skb, nlh);
1252 nlmsg_cancel(skb, nlh);
1256 static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb)
1258 struct net *net = sock_net(skb->sk);
1259 struct mctp_route *rt;
1262 /* TODO: allow filtering on route data, possibly under
1266 /* TODO: change to struct overlay */
1267 s_idx = cb->args[0];
1271 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
1274 if (mctp_fill_rtinfo(skb, rt,
1275 NETLINK_CB(cb->skb).portid,
1277 RTM_NEWROUTE, NLM_F_MULTI) < 0)
1287 /* net namespace implementation */
1288 static int __net_init mctp_routes_net_init(struct net *net)
1290 struct netns_mctp *ns = &net->mctp;
1292 INIT_LIST_HEAD(&ns->routes);
1293 INIT_HLIST_HEAD(&ns->binds);
1294 mutex_init(&ns->bind_lock);
1295 INIT_HLIST_HEAD(&ns->keys);
1296 spin_lock_init(&ns->keys_lock);
1297 WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET));
1301 static void __net_exit mctp_routes_net_exit(struct net *net)
1303 struct mctp_route *rt;
1306 list_for_each_entry_rcu(rt, &net->mctp.routes, list)
1307 mctp_route_release(rt);
1311 static struct pernet_operations mctp_net_ops = {
1312 .init = mctp_routes_net_init,
1313 .exit = mctp_routes_net_exit,
1316 int __init mctp_routes_init(void)
1318 dev_add_pack(&mctp_packet_type);
1320 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETROUTE,
1321 NULL, mctp_dump_rtinfo, 0);
1322 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWROUTE,
1323 mctp_newroute, NULL, 0);
1324 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELROUTE,
1325 mctp_delroute, NULL, 0);
1327 return register_pernet_subsys(&mctp_net_ops);
1330 void __exit mctp_routes_exit(void)
1332 unregister_pernet_subsys(&mctp_net_ops);
1333 rtnl_unregister(PF_MCTP, RTM_DELROUTE);
1334 rtnl_unregister(PF_MCTP, RTM_NEWROUTE);
1335 rtnl_unregister(PF_MCTP, RTM_GETROUTE);
1336 dev_remove_pack(&mctp_packet_type);
1339 #if IS_ENABLED(CONFIG_MCTP_TEST)
1340 #include "test/route-test.c"