| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Management Component Transport Protocol (MCTP) - routing |
| 4 |
* implementation. |
| 5 |
* |
| 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. |
| 8 |
* |
| 9 |
* Copyright (c) 2021 Code Construct |
| 10 |
* Copyright (c) 2021 Google |
| 11 |
*/ |
| 12 |
|
| 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> |
| 19 |
|
| 20 |
#include <kunit/static_stub.h> |
| 21 |
|
| 22 |
#include <uapi/linux/if_arp.h> |
| 23 |
|
| 24 |
#include <net/mctp.h> |
| 25 |
#include <net/mctpdevice.h> |
| 26 |
#include <net/netlink.h> |
| 27 |
#include <net/sock.h> |
| 28 |
|
| 29 |
#include <trace/events/mctp.h> |
| 30 |
|
| 31 |
static const unsigned int mctp_message_maxlen = 64 * 1024; |
| 32 |
static const unsigned long mctp_key_lifetime = 6 * CONFIG_HZ; |
| 33 |
|
| 34 |
static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev); |
| 35 |
|
| 36 |
/* route output callbacks */ |
| 37 |
static int mctp_dst_discard(struct mctp_dst *dst, struct sk_buff *skb) |
| 38 |
{ |
| 39 |
kfree_skb(skb); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static struct mctp_sock *mctp_lookup_bind_details(struct net *net, |
| 44 |
struct sk_buff *skb, |
| 45 |
u8 type, u8 dest, |
| 46 |
u8 src, bool allow_net_any) |
| 47 |
{ |
| 48 |
struct mctp_skb_cb *cb = mctp_cb(skb); |
| 49 |
struct sock *sk; |
| 50 |
u8 hash; |
| 51 |
|
| 52 |
WARN_ON_ONCE(!rcu_read_lock_held()); |
| 53 |
|
| 54 |
hash = mctp_bind_hash(type, dest, src); |
| 55 |
|
| 56 |
sk_for_each_rcu(sk, &net->mctp.binds[hash]) { |
| 57 |
struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 58 |
|
| 59 |
if (!allow_net_any && msk->bind_net == MCTP_NET_ANY) |
| 60 |
continue; |
| 61 |
|
| 62 |
if (msk->bind_net != MCTP_NET_ANY && msk->bind_net != cb->net) |
| 63 |
continue; |
| 64 |
|
| 65 |
if (msk->bind_type != type) |
| 66 |
continue; |
| 67 |
|
| 68 |
if (msk->bind_peer_set && |
| 69 |
!mctp_address_matches(msk->bind_peer_addr, src)) |
| 70 |
continue; |
| 71 |
|
| 72 |
if (!mctp_address_matches(msk->bind_local_addr, dest)) |
| 73 |
continue; |
| 74 |
|
| 75 |
return msk; |
| 76 |
} |
| 77 |
|
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
static struct mctp_sock *mctp_lookup_bind(struct net *net, struct sk_buff *skb) |
| 82 |
{ |
| 83 |
struct mctp_sock *msk; |
| 84 |
struct mctp_hdr *mh; |
| 85 |
u8 type; |
| 86 |
|
| 87 |
/* TODO: look up in skb->cb? */ |
| 88 |
mh = mctp_hdr(skb); |
| 89 |
|
| 90 |
if (!skb_headlen(skb)) |
| 91 |
return NULL; |
| 92 |
|
| 93 |
type = (*(u8 *)skb->data) & 0x7f; |
| 94 |
|
| 95 |
/* Look for binds in order of widening scope. A given destination or |
| 96 |
* source address also implies matching on a particular network. |
| 97 |
* |
| 98 |
* - Matching destination and source |
| 99 |
* - Matching destination |
| 100 |
* - Matching source |
| 101 |
* - Matching network, any address |
| 102 |
* - Any network or address |
| 103 |
*/ |
| 104 |
|
| 105 |
msk = mctp_lookup_bind_details(net, skb, type, mh->dest, mh->src, |
| 106 |
false); |
| 107 |
if (msk) |
| 108 |
return msk; |
| 109 |
msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, mh->src, |
| 110 |
false); |
| 111 |
if (msk) |
| 112 |
return msk; |
| 113 |
msk = mctp_lookup_bind_details(net, skb, type, mh->dest, MCTP_ADDR_ANY, |
| 114 |
false); |
| 115 |
if (msk) |
| 116 |
return msk; |
| 117 |
msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, |
| 118 |
MCTP_ADDR_ANY, false); |
| 119 |
if (msk) |
| 120 |
return msk; |
| 121 |
msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, |
| 122 |
MCTP_ADDR_ANY, true); |
| 123 |
if (msk) |
| 124 |
return msk; |
| 125 |
|
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
/* A note on the key allocations. |
| 130 |
* |
| 131 |
* struct net->mctp.keys contains our set of currently-allocated keys for |
| 132 |
* MCTP tag management. The lookup tuple for these is the peer EID, |
| 133 |
* local EID and MCTP tag. |
| 134 |
* |
| 135 |
* In some cases, the peer EID may be MCTP_EID_ANY: for example, when a |
| 136 |
* broadcast message is sent, we may receive responses from any peer EID. |
| 137 |
* Because the broadcast dest address is equivalent to ANY, we create |
| 138 |
* a key with (local = local-eid, peer = ANY). This allows a match on the |
| 139 |
* incoming broadcast responses from any peer. |
| 140 |
* |
| 141 |
* We perform lookups when packets are received, and when tags are allocated |
| 142 |
* in two scenarios: |
| 143 |
* |
| 144 |
* - when a packet is sent, with a locally-owned tag: we need to find an |
| 145 |
* unused tag value for the (local, peer) EID pair. |
| 146 |
* |
| 147 |
* - when a tag is manually allocated: we need to find an unused tag value |
| 148 |
* for the peer EID, but don't have a specific local EID at that stage. |
| 149 |
* |
| 150 |
* in the latter case, on successful allocation, we end up with a tag with |
| 151 |
* (local = ANY, peer = peer-eid). |
| 152 |
* |
| 153 |
* So, the key set allows both a local EID of ANY, as well as a peer EID of |
| 154 |
* ANY in the lookup tuple. Both may be ANY if we prealloc for a broadcast. |
| 155 |
* The matching (in mctp_key_match()) during lookup allows the match value to |
| 156 |
* be ANY in either the dest or source addresses. |
| 157 |
* |
| 158 |
* When allocating (+ inserting) a tag, we need to check for conflicts amongst |
| 159 |
* the existing tag set. This requires macthing either exactly on the local |
| 160 |
* and peer addresses, or either being ANY. |
| 161 |
*/ |
| 162 |
|
| 163 |
static bool mctp_key_match(struct mctp_sk_key *key, unsigned int net, |
| 164 |
mctp_eid_t local, mctp_eid_t peer, u8 tag) |
| 165 |
{ |
| 166 |
if (key->net != net) |
| 167 |
return false; |
| 168 |
|
| 169 |
if (!mctp_address_matches(key->local_addr, local)) |
| 170 |
return false; |
| 171 |
|
| 172 |
if (!mctp_address_matches(key->peer_addr, peer)) |
| 173 |
return false; |
| 174 |
|
| 175 |
if (key->tag != tag) |
| 176 |
return false; |
| 177 |
|
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
/* returns a key (with key->lock held, and refcounted), or NULL if no such |
| 182 |
* key exists. |
| 183 |
*/ |
| 184 |
static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb, |
| 185 |
unsigned int netid, mctp_eid_t peer, |
| 186 |
unsigned long *irqflags) |
| 187 |
__acquires(&key->lock) |
| 188 |
{ |
| 189 |
struct mctp_sk_key *key, *ret; |
| 190 |
unsigned long flags; |
| 191 |
struct mctp_hdr *mh; |
| 192 |
u8 tag; |
| 193 |
|
| 194 |
mh = mctp_hdr(skb); |
| 195 |
tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); |
| 196 |
|
| 197 |
ret = NULL; |
| 198 |
spin_lock_irqsave(&net->mctp.keys_lock, flags); |
| 199 |
|
| 200 |
hlist_for_each_entry(key, &net->mctp.keys, hlist) { |
| 201 |
if (!mctp_key_match(key, netid, mh->dest, peer, tag)) |
| 202 |
continue; |
| 203 |
|
| 204 |
spin_lock(&key->lock); |
| 205 |
if (key->valid) { |
| 206 |
refcount_inc(&key->refs); |
| 207 |
ret = key; |
| 208 |
break; |
| 209 |
} |
| 210 |
spin_unlock(&key->lock); |
| 211 |
} |
| 212 |
|
| 213 |
if (ret) { |
| 214 |
spin_unlock(&net->mctp.keys_lock); |
| 215 |
*irqflags = flags; |
| 216 |
} else { |
| 217 |
spin_unlock_irqrestore(&net->mctp.keys_lock, flags); |
| 218 |
} |
| 219 |
|
| 220 |
return ret; |
| 221 |
} |
| 222 |
|
| 223 |
static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk, |
| 224 |
unsigned int net, |
| 225 |
mctp_eid_t local, mctp_eid_t peer, |
| 226 |
u8 tag, gfp_t gfp) |
| 227 |
{ |
| 228 |
struct mctp_sk_key *key; |
| 229 |
|
| 230 |
key = kzalloc_obj(*key, gfp); |
| 231 |
if (!key) |
| 232 |
return NULL; |
| 233 |
|
| 234 |
key->net = net; |
| 235 |
key->peer_addr = peer; |
| 236 |
key->local_addr = local; |
| 237 |
key->tag = tag; |
| 238 |
key->sk = &msk->sk; |
| 239 |
key->valid = true; |
| 240 |
spin_lock_init(&key->lock); |
| 241 |
refcount_set(&key->refs, 1); |
| 242 |
sock_hold(key->sk); |
| 243 |
|
| 244 |
return key; |
| 245 |
} |
| 246 |
|
| 247 |
void mctp_key_unref(struct mctp_sk_key *key) |
| 248 |
{ |
| 249 |
unsigned long flags; |
| 250 |
|
| 251 |
if (!refcount_dec_and_test(&key->refs)) |
| 252 |
return; |
| 253 |
|
| 254 |
/* even though no refs exist here, the lock allows us to stay |
| 255 |
* consistent with the locking requirement of mctp_dev_release_key |
| 256 |
*/ |
| 257 |
spin_lock_irqsave(&key->lock, flags); |
| 258 |
mctp_dev_release_key(key->dev, key); |
| 259 |
spin_unlock_irqrestore(&key->lock, flags); |
| 260 |
|
| 261 |
sock_put(key->sk); |
| 262 |
kfree(key); |
| 263 |
} |
| 264 |
|
| 265 |
static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk) |
| 266 |
{ |
| 267 |
struct net *net = sock_net(&msk->sk); |
| 268 |
struct mctp_sk_key *tmp; |
| 269 |
unsigned long flags; |
| 270 |
int rc = 0; |
| 271 |
|
| 272 |
spin_lock_irqsave(&net->mctp.keys_lock, flags); |
| 273 |
|
| 274 |
if (sock_flag(&msk->sk, SOCK_DEAD)) { |
| 275 |
rc = -EINVAL; |
| 276 |
goto out_unlock; |
| 277 |
} |
| 278 |
|
| 279 |
hlist_for_each_entry(tmp, &net->mctp.keys, hlist) { |
| 280 |
if (mctp_key_match(tmp, key->net, key->local_addr, |
| 281 |
key->peer_addr, key->tag)) { |
| 282 |
spin_lock(&tmp->lock); |
| 283 |
if (tmp->valid) |
| 284 |
rc = -EEXIST; |
| 285 |
spin_unlock(&tmp->lock); |
| 286 |
if (rc) |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (!rc) { |
| 292 |
refcount_inc(&key->refs); |
| 293 |
key->expiry = jiffies + mctp_key_lifetime; |
| 294 |
timer_reduce(&msk->key_expiry, key->expiry); |
| 295 |
|
| 296 |
hlist_add_head(&key->hlist, &net->mctp.keys); |
| 297 |
hlist_add_head(&key->sklist, &msk->keys); |
| 298 |
} |
| 299 |
|
| 300 |
out_unlock: |
| 301 |
spin_unlock_irqrestore(&net->mctp.keys_lock, flags); |
| 302 |
|
| 303 |
return rc; |
| 304 |
} |
| 305 |
|
| 306 |
/* Helper for mctp_route_input(). |
| 307 |
* We're done with the key; unlock and unref the key. |
| 308 |
* For the usual case of automatic expiry we remove the key from lists. |
| 309 |
* In the case that manual allocation is set on a key we release the lock |
| 310 |
* and local ref, reset reassembly, but don't remove from lists. |
| 311 |
*/ |
| 312 |
static void __mctp_key_done_in(struct mctp_sk_key *key, struct net *net, |
| 313 |
unsigned long flags, unsigned long reason) |
| 314 |
__releases(&key->lock) |
| 315 |
{ |
| 316 |
struct sk_buff *skb; |
| 317 |
|
| 318 |
trace_mctp_key_release(key, reason); |
| 319 |
skb = key->reasm_head; |
| 320 |
key->reasm_head = NULL; |
| 321 |
|
| 322 |
if (!key->manual_alloc) { |
| 323 |
key->reasm_dead = true; |
| 324 |
key->valid = false; |
| 325 |
mctp_dev_release_key(key->dev, key); |
| 326 |
} |
| 327 |
spin_unlock_irqrestore(&key->lock, flags); |
| 328 |
|
| 329 |
if (!key->manual_alloc) { |
| 330 |
spin_lock_irqsave(&net->mctp.keys_lock, flags); |
| 331 |
if (!hlist_unhashed(&key->hlist)) { |
| 332 |
hlist_del_init(&key->hlist); |
| 333 |
hlist_del_init(&key->sklist); |
| 334 |
mctp_key_unref(key); |
| 335 |
} |
| 336 |
spin_unlock_irqrestore(&net->mctp.keys_lock, flags); |
| 337 |
} |
| 338 |
|
| 339 |
/* and one for the local reference */ |
| 340 |
mctp_key_unref(key); |
| 341 |
|
| 342 |
kfree_skb(skb); |
| 343 |
} |
| 344 |
|
| 345 |
#ifdef CONFIG_MCTP_FLOWS |
| 346 |
static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) |
| 347 |
{ |
| 348 |
struct mctp_flow *flow; |
| 349 |
|
| 350 |
flow = skb_ext_add(skb, SKB_EXT_MCTP); |
| 351 |
if (!flow) |
| 352 |
return; |
| 353 |
|
| 354 |
refcount_inc(&key->refs); |
| 355 |
flow->key = key; |
| 356 |
} |
| 357 |
|
| 358 |
static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) |
| 359 |
{ |
| 360 |
struct mctp_sk_key *key; |
| 361 |
struct mctp_flow *flow; |
| 362 |
unsigned long flags; |
| 363 |
|
| 364 |
flow = skb_ext_find(skb, SKB_EXT_MCTP); |
| 365 |
if (!flow) |
| 366 |
return; |
| 367 |
|
| 368 |
key = flow->key; |
| 369 |
|
| 370 |
spin_lock_irqsave(&key->lock, flags); |
| 371 |
|
| 372 |
if (!key->dev) |
| 373 |
mctp_dev_set_key(dev, key); |
| 374 |
else |
| 375 |
WARN_ON(key->dev != dev); |
| 376 |
|
| 377 |
spin_unlock_irqrestore(&key->lock, flags); |
| 378 |
} |
| 379 |
#else |
| 380 |
static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) {} |
| 381 |
static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) {} |
| 382 |
#endif |
| 383 |
|
| 384 |
/* takes ownership of skb, both in success and failure cases */ |
| 385 |
static int mctp_frag_queue(struct mctp_sk_key *key, struct sk_buff *skb) |
| 386 |
{ |
| 387 |
struct mctp_hdr *hdr = mctp_hdr(skb); |
| 388 |
u8 exp_seq, this_seq; |
| 389 |
|
| 390 |
this_seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) |
| 391 |
& MCTP_HDR_SEQ_MASK; |
| 392 |
|
| 393 |
if (!key->reasm_head) { |
| 394 |
/* Since we're manipulating the shared frag_list, ensure it |
| 395 |
* isn't shared with any other SKBs. In the cloned case, |
| 396 |
* this will free the skb; callers can no longer access it |
| 397 |
* safely. |
| 398 |
*/ |
| 399 |
key->reasm_head = skb_unshare(skb, GFP_ATOMIC); |
| 400 |
if (!key->reasm_head) |
| 401 |
return -ENOMEM; |
| 402 |
|
| 403 |
key->reasm_tailp = &(skb_shinfo(key->reasm_head)->frag_list); |
| 404 |
key->last_seq = this_seq; |
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
exp_seq = (key->last_seq + 1) & MCTP_HDR_SEQ_MASK; |
| 409 |
|
| 410 |
if (this_seq != exp_seq) |
| 411 |
goto err_free; |
| 412 |
|
| 413 |
if (key->reasm_head->len + skb->len > mctp_message_maxlen) |
| 414 |
goto err_free; |
| 415 |
|
| 416 |
skb->next = NULL; |
| 417 |
skb->sk = NULL; |
| 418 |
*key->reasm_tailp = skb; |
| 419 |
key->reasm_tailp = &skb->next; |
| 420 |
|
| 421 |
key->last_seq = this_seq; |
| 422 |
|
| 423 |
key->reasm_head->data_len += skb->len; |
| 424 |
key->reasm_head->len += skb->len; |
| 425 |
key->reasm_head->truesize += skb->truesize; |
| 426 |
|
| 427 |
return 0; |
| 428 |
|
| 429 |
err_free: |
| 430 |
kfree_skb(skb); |
| 431 |
return -EINVAL; |
| 432 |
} |
| 433 |
|
| 434 |
static int mctp_dst_input(struct mctp_dst *dst, struct sk_buff *skb) |
| 435 |
{ |
| 436 |
struct mctp_sk_key *key, *any_key = NULL; |
| 437 |
struct net *net = dev_net(skb->dev); |
| 438 |
struct mctp_sock *msk; |
| 439 |
struct mctp_hdr *mh; |
| 440 |
unsigned int netid; |
| 441 |
unsigned long f; |
| 442 |
u8 tag, flags; |
| 443 |
int rc; |
| 444 |
u8 ver; |
| 445 |
|
| 446 |
msk = NULL; |
| 447 |
rc = -EINVAL; |
| 448 |
|
| 449 |
/* We may be receiving a locally-routed packet; drop source sk |
| 450 |
* accounting. |
| 451 |
* |
| 452 |
* From here, we will either queue the skb - either to a frag_queue, or |
| 453 |
* to a receiving socket. When that succeeds, we clear the skb pointer; |
| 454 |
* a non-NULL skb on exit will be otherwise unowned, and hence |
| 455 |
* kfree_skb()-ed. |
| 456 |
*/ |
| 457 |
skb_orphan(skb); |
| 458 |
|
| 459 |
if (skb->pkt_type == PACKET_OUTGOING) |
| 460 |
skb->pkt_type = PACKET_LOOPBACK; |
| 461 |
|
| 462 |
/* ensure we have enough data for a header and a type */ |
| 463 |
if (skb->len < sizeof(struct mctp_hdr) + 1) |
| 464 |
goto out; |
| 465 |
|
| 466 |
/* grab header, advance data ptr */ |
| 467 |
mh = mctp_hdr(skb); |
| 468 |
netid = mctp_cb(skb)->net; |
| 469 |
skb_pull(skb, sizeof(struct mctp_hdr)); |
| 470 |
|
| 471 |
ver = mh->ver & MCTP_HDR_VER_MASK; |
| 472 |
if (ver < MCTP_VER_MIN || ver > MCTP_VER_MAX) |
| 473 |
goto out; |
| 474 |
|
| 475 |
flags = mh->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM); |
| 476 |
tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); |
| 477 |
|
| 478 |
rcu_read_lock(); |
| 479 |
|
| 480 |
/* lookup socket / reasm context, exactly matching (src,dest,tag). |
| 481 |
* we hold a ref on the key, and key->lock held. |
| 482 |
*/ |
| 483 |
key = mctp_lookup_key(net, skb, netid, mh->src, &f); |
| 484 |
|
| 485 |
if (flags & MCTP_HDR_FLAG_SOM) { |
| 486 |
if (key) { |
| 487 |
msk = container_of(key->sk, struct mctp_sock, sk); |
| 488 |
} else { |
| 489 |
/* first response to a broadcast? do a more general |
| 490 |
* key lookup to find the socket, but don't use this |
| 491 |
* key for reassembly - we'll create a more specific |
| 492 |
* one for future packets if required (ie, !EOM). |
| 493 |
* |
| 494 |
* this lookup requires key->peer to be MCTP_ADDR_ANY, |
| 495 |
* it doesn't match just any key->peer. |
| 496 |
*/ |
| 497 |
any_key = mctp_lookup_key(net, skb, netid, |
| 498 |
MCTP_ADDR_ANY, &f); |
| 499 |
if (any_key) { |
| 500 |
msk = container_of(any_key->sk, |
| 501 |
struct mctp_sock, sk); |
| 502 |
spin_unlock_irqrestore(&any_key->lock, f); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
if (!key && !msk && (tag & MCTP_HDR_FLAG_TO)) |
| 507 |
msk = mctp_lookup_bind(net, skb); |
| 508 |
|
| 509 |
if (!msk) { |
| 510 |
rc = -ENOENT; |
| 511 |
goto out_unlock; |
| 512 |
} |
| 513 |
|
| 514 |
/* single-packet message? deliver to socket, clean up any |
| 515 |
* pending key. |
| 516 |
*/ |
| 517 |
if (flags & MCTP_HDR_FLAG_EOM) { |
| 518 |
rc = sock_queue_rcv_skb(&msk->sk, skb); |
| 519 |
if (!rc) |
| 520 |
skb = NULL; |
| 521 |
if (key) { |
| 522 |
/* we've hit a pending reassembly; not much we |
| 523 |
* can do but drop it |
| 524 |
*/ |
| 525 |
__mctp_key_done_in(key, net, f, |
| 526 |
MCTP_TRACE_KEY_REPLIED); |
| 527 |
key = NULL; |
| 528 |
} |
| 529 |
goto out_unlock; |
| 530 |
} |
| 531 |
|
| 532 |
/* broadcast response or a bind() - create a key for further |
| 533 |
* packets for this message |
| 534 |
*/ |
| 535 |
if (!key) { |
| 536 |
key = mctp_key_alloc(msk, netid, mh->dest, mh->src, |
| 537 |
tag, GFP_ATOMIC); |
| 538 |
if (!key) { |
| 539 |
rc = -ENOMEM; |
| 540 |
goto out_unlock; |
| 541 |
} |
| 542 |
|
| 543 |
/* we can queue without the key lock here, as the |
| 544 |
* key isn't observable yet |
| 545 |
*/ |
| 546 |
mctp_frag_queue(key, skb); |
| 547 |
skb = NULL; |
| 548 |
|
| 549 |
/* if the key_add fails, we've raced with another |
| 550 |
* SOM packet with the same src, dest and tag. There's |
| 551 |
* no way to distinguish future packets, so all we |
| 552 |
* can do is drop. |
| 553 |
*/ |
| 554 |
rc = mctp_key_add(key, msk); |
| 555 |
if (!rc) |
| 556 |
trace_mctp_key_acquire(key); |
| 557 |
|
| 558 |
/* we don't need to release key->lock on exit, so |
| 559 |
* clean up here and suppress the unlock via |
| 560 |
* setting to NULL |
| 561 |
*/ |
| 562 |
mctp_key_unref(key); |
| 563 |
key = NULL; |
| 564 |
|
| 565 |
} else { |
| 566 |
if (key->reasm_head || key->reasm_dead) { |
| 567 |
/* duplicate start? drop everything */ |
| 568 |
__mctp_key_done_in(key, net, f, |
| 569 |
MCTP_TRACE_KEY_INVALIDATED); |
| 570 |
rc = -EEXIST; |
| 571 |
key = NULL; |
| 572 |
} else { |
| 573 |
rc = mctp_frag_queue(key, skb); |
| 574 |
skb = NULL; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
} else if (key) { |
| 579 |
/* this packet continues a previous message; reassemble |
| 580 |
* using the message-specific key |
| 581 |
*/ |
| 582 |
|
| 583 |
/* we need to be continuing an existing reassembly... */ |
| 584 |
if (!key->reasm_head) { |
| 585 |
rc = -EINVAL; |
| 586 |
} else { |
| 587 |
rc = mctp_frag_queue(key, skb); |
| 588 |
skb = NULL; |
| 589 |
} |
| 590 |
|
| 591 |
if (rc) |
| 592 |
goto out_unlock; |
| 593 |
|
| 594 |
/* end of message? deliver to socket, and we're done with |
| 595 |
* the reassembly/response key |
| 596 |
*/ |
| 597 |
if (flags & MCTP_HDR_FLAG_EOM) { |
| 598 |
rc = sock_queue_rcv_skb(key->sk, key->reasm_head); |
| 599 |
if (!rc) |
| 600 |
key->reasm_head = NULL; |
| 601 |
__mctp_key_done_in(key, net, f, MCTP_TRACE_KEY_REPLIED); |
| 602 |
key = NULL; |
| 603 |
} |
| 604 |
|
| 605 |
} else { |
| 606 |
/* not a start, no matching key */ |
| 607 |
rc = -ENOENT; |
| 608 |
} |
| 609 |
|
| 610 |
out_unlock: |
| 611 |
rcu_read_unlock(); |
| 612 |
if (key) { |
| 613 |
spin_unlock_irqrestore(&key->lock, f); |
| 614 |
mctp_key_unref(key); |
| 615 |
} |
| 616 |
if (any_key) |
| 617 |
mctp_key_unref(any_key); |
| 618 |
out: |
| 619 |
kfree_skb(skb); |
| 620 |
return rc; |
| 621 |
} |
| 622 |
|
| 623 |
static int mctp_dst_output(struct mctp_dst *dst, struct sk_buff *skb) |
| 624 |
{ |
| 625 |
char daddr_buf[MAX_ADDR_LEN]; |
| 626 |
char *daddr = NULL; |
| 627 |
int rc; |
| 628 |
|
| 629 |
skb->protocol = htons(ETH_P_MCTP); |
| 630 |
skb->pkt_type = PACKET_OUTGOING; |
| 631 |
skb->dev = dst->dev->dev; |
| 632 |
|
| 633 |
if (skb->len > dst->mtu) { |
| 634 |
kfree_skb(skb); |
| 635 |
return -EMSGSIZE; |
| 636 |
} |
| 637 |
|
| 638 |
/* direct route; use the hwaddr we stashed in sendmsg */ |
| 639 |
if (dst->halen) { |
| 640 |
if (dst->halen != skb->dev->addr_len) { |
| 641 |
/* sanity check, sendmsg should have already caught this */ |
| 642 |
kfree_skb(skb); |
| 643 |
return -EMSGSIZE; |
| 644 |
} |
| 645 |
daddr = dst->haddr; |
| 646 |
} else { |
| 647 |
/* If lookup fails let the device handle daddr==NULL */ |
| 648 |
if (mctp_neigh_lookup(dst->dev, dst->nexthop, daddr_buf) == 0) |
| 649 |
daddr = daddr_buf; |
| 650 |
} |
| 651 |
|
| 652 |
rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol), |
| 653 |
daddr, skb->dev->dev_addr, skb->len); |
| 654 |
if (rc < 0) { |
| 655 |
kfree_skb(skb); |
| 656 |
return -EHOSTUNREACH; |
| 657 |
} |
| 658 |
|
| 659 |
mctp_flow_prepare_output(skb, dst->dev); |
| 660 |
|
| 661 |
rc = dev_queue_xmit(skb); |
| 662 |
if (rc) |
| 663 |
rc = net_xmit_errno(rc); |
| 664 |
|
| 665 |
return rc; |
| 666 |
} |
| 667 |
|
| 668 |
/* route alloc/release */ |
| 669 |
static void mctp_route_release(struct mctp_route *rt) |
| 670 |
{ |
| 671 |
if (refcount_dec_and_test(&rt->refs)) { |
| 672 |
if (rt->dst_type == MCTP_ROUTE_DIRECT) |
| 673 |
mctp_dev_put(rt->dev); |
| 674 |
kfree_rcu(rt, rcu); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
/* returns a route with the refcount at 1 */ |
| 679 |
static struct mctp_route *mctp_route_alloc(void) |
| 680 |
{ |
| 681 |
struct mctp_route *rt; |
| 682 |
|
| 683 |
rt = kzalloc_obj(*rt); |
| 684 |
if (!rt) |
| 685 |
return NULL; |
| 686 |
|
| 687 |
INIT_LIST_HEAD(&rt->list); |
| 688 |
refcount_set(&rt->refs, 1); |
| 689 |
rt->output = mctp_dst_discard; |
| 690 |
|
| 691 |
return rt; |
| 692 |
} |
| 693 |
|
| 694 |
unsigned int mctp_default_net(struct net *net) |
| 695 |
{ |
| 696 |
return READ_ONCE(net->mctp.default_net); |
| 697 |
} |
| 698 |
|
| 699 |
int mctp_default_net_set(struct net *net, unsigned int index) |
| 700 |
{ |
| 701 |
if (index == 0) |
| 702 |
return -EINVAL; |
| 703 |
WRITE_ONCE(net->mctp.default_net, index); |
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
/* tag management */ |
| 708 |
static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key, |
| 709 |
struct mctp_sock *msk) |
| 710 |
{ |
| 711 |
struct netns_mctp *mns = &net->mctp; |
| 712 |
|
| 713 |
lockdep_assert_held(&mns->keys_lock); |
| 714 |
|
| 715 |
key->expiry = jiffies + mctp_key_lifetime; |
| 716 |
timer_reduce(&msk->key_expiry, key->expiry); |
| 717 |
|
| 718 |
/* we hold the net->key_lock here, allowing updates to both |
| 719 |
* then net and sk |
| 720 |
*/ |
| 721 |
hlist_add_head_rcu(&key->hlist, &mns->keys); |
| 722 |
hlist_add_head_rcu(&key->sklist, &msk->keys); |
| 723 |
refcount_inc(&key->refs); |
| 724 |
} |
| 725 |
|
| 726 |
/* Allocate a locally-owned tag value for (local, peer), and reserve |
| 727 |
* it for the socket msk |
| 728 |
*/ |
| 729 |
struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk, |
| 730 |
unsigned int netid, |
| 731 |
mctp_eid_t local, mctp_eid_t peer, |
| 732 |
bool manual, u8 *tagp) |
| 733 |
{ |
| 734 |
struct net *net = sock_net(&msk->sk); |
| 735 |
struct netns_mctp *mns = &net->mctp; |
| 736 |
struct mctp_sk_key *key, *tmp; |
| 737 |
unsigned long flags; |
| 738 |
u8 tagbits; |
| 739 |
|
| 740 |
/* for NULL destination EIDs, we may get a response from any peer */ |
| 741 |
if (peer == MCTP_ADDR_NULL) |
| 742 |
peer = MCTP_ADDR_ANY; |
| 743 |
|
| 744 |
/* be optimistic, alloc now */ |
| 745 |
key = mctp_key_alloc(msk, netid, local, peer, 0, GFP_KERNEL); |
| 746 |
if (!key) |
| 747 |
return ERR_PTR(-ENOMEM); |
| 748 |
|
| 749 |
/* 8 possible tag values */ |
| 750 |
tagbits = 0xff; |
| 751 |
|
| 752 |
spin_lock_irqsave(&mns->keys_lock, flags); |
| 753 |
|
| 754 |
/* Walk through the existing keys, looking for potential conflicting |
| 755 |
* tags. If we find a conflict, clear that bit from tagbits |
| 756 |
*/ |
| 757 |
hlist_for_each_entry(tmp, &mns->keys, hlist) { |
| 758 |
/* We can check the lookup fields (*_addr, tag) without the |
| 759 |
* lock held, they don't change over the lifetime of the key. |
| 760 |
*/ |
| 761 |
|
| 762 |
/* tags are net-specific */ |
| 763 |
if (tmp->net != netid) |
| 764 |
continue; |
| 765 |
|
| 766 |
/* if we don't own the tag, it can't conflict */ |
| 767 |
if (tmp->tag & MCTP_HDR_FLAG_TO) |
| 768 |
continue; |
| 769 |
|
| 770 |
/* Since we're avoiding conflicting entries, match peer and |
| 771 |
* local addresses, including with a wildcard on ANY. See |
| 772 |
* 'A note on key allocations' for background. |
| 773 |
*/ |
| 774 |
if (peer != MCTP_ADDR_ANY && |
| 775 |
!mctp_address_matches(tmp->peer_addr, peer)) |
| 776 |
continue; |
| 777 |
|
| 778 |
if (local != MCTP_ADDR_ANY && |
| 779 |
!mctp_address_matches(tmp->local_addr, local)) |
| 780 |
continue; |
| 781 |
|
| 782 |
spin_lock(&tmp->lock); |
| 783 |
/* key must still be valid. If we find a match, clear the |
| 784 |
* potential tag value |
| 785 |
*/ |
| 786 |
if (tmp->valid) |
| 787 |
tagbits &= ~(1 << tmp->tag); |
| 788 |
spin_unlock(&tmp->lock); |
| 789 |
|
| 790 |
if (!tagbits) |
| 791 |
break; |
| 792 |
} |
| 793 |
|
| 794 |
if (tagbits) { |
| 795 |
key->tag = __ffs(tagbits); |
| 796 |
mctp_reserve_tag(net, key, msk); |
| 797 |
trace_mctp_key_acquire(key); |
| 798 |
|
| 799 |
key->manual_alloc = manual; |
| 800 |
*tagp = key->tag; |
| 801 |
} |
| 802 |
|
| 803 |
spin_unlock_irqrestore(&mns->keys_lock, flags); |
| 804 |
|
| 805 |
if (!tagbits) { |
| 806 |
mctp_key_unref(key); |
| 807 |
return ERR_PTR(-EBUSY); |
| 808 |
} |
| 809 |
|
| 810 |
return key; |
| 811 |
} |
| 812 |
|
| 813 |
static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk, |
| 814 |
unsigned int netid, |
| 815 |
mctp_eid_t daddr, |
| 816 |
u8 req_tag, u8 *tagp) |
| 817 |
{ |
| 818 |
struct net *net = sock_net(&msk->sk); |
| 819 |
struct netns_mctp *mns = &net->mctp; |
| 820 |
struct mctp_sk_key *key, *tmp; |
| 821 |
unsigned long flags; |
| 822 |
|
| 823 |
req_tag &= ~(MCTP_TAG_PREALLOC | MCTP_TAG_OWNER); |
| 824 |
key = NULL; |
| 825 |
|
| 826 |
spin_lock_irqsave(&mns->keys_lock, flags); |
| 827 |
|
| 828 |
hlist_for_each_entry(tmp, &mns->keys, hlist) { |
| 829 |
if (tmp->net != netid) |
| 830 |
continue; |
| 831 |
|
| 832 |
if (tmp->tag != req_tag) |
| 833 |
continue; |
| 834 |
|
| 835 |
if (!mctp_address_matches(tmp->peer_addr, daddr)) |
| 836 |
continue; |
| 837 |
|
| 838 |
if (!tmp->manual_alloc) |
| 839 |
continue; |
| 840 |
|
| 841 |
spin_lock(&tmp->lock); |
| 842 |
if (tmp->valid) { |
| 843 |
key = tmp; |
| 844 |
refcount_inc(&key->refs); |
| 845 |
spin_unlock(&tmp->lock); |
| 846 |
break; |
| 847 |
} |
| 848 |
spin_unlock(&tmp->lock); |
| 849 |
} |
| 850 |
spin_unlock_irqrestore(&mns->keys_lock, flags); |
| 851 |
|
| 852 |
if (!key) |
| 853 |
return ERR_PTR(-ENOENT); |
| 854 |
|
| 855 |
if (tagp) |
| 856 |
*tagp = key->tag; |
| 857 |
|
| 858 |
return key; |
| 859 |
} |
| 860 |
|
| 861 |
/* routing lookups */ |
| 862 |
static unsigned int mctp_route_netid(struct mctp_route *rt) |
| 863 |
{ |
| 864 |
return rt->dst_type == MCTP_ROUTE_DIRECT ? |
| 865 |
READ_ONCE(rt->dev->net) : rt->gateway.net; |
| 866 |
} |
| 867 |
|
| 868 |
static bool mctp_rt_match_eid(struct mctp_route *rt, |
| 869 |
unsigned int net, mctp_eid_t eid) |
| 870 |
{ |
| 871 |
return mctp_route_netid(rt) == net && |
| 872 |
rt->min <= eid && rt->max >= eid; |
| 873 |
} |
| 874 |
|
| 875 |
/* compares match, used for duplicate prevention */ |
| 876 |
static bool mctp_rt_compare_exact(struct mctp_route *rt1, |
| 877 |
struct mctp_route *rt2) |
| 878 |
{ |
| 879 |
ASSERT_RTNL(); |
| 880 |
return mctp_route_netid(rt1) == mctp_route_netid(rt2) && |
| 881 |
rt1->min == rt2->min && |
| 882 |
rt1->max == rt2->max; |
| 883 |
} |
| 884 |
|
| 885 |
/* must only be called on a direct route, as the final output hop */ |
| 886 |
static void mctp_dst_from_route(struct mctp_dst *dst, mctp_eid_t eid, |
| 887 |
unsigned int mtu, struct mctp_route *route) |
| 888 |
{ |
| 889 |
mctp_dev_hold(route->dev); |
| 890 |
dst->nexthop = eid; |
| 891 |
dst->dev = route->dev; |
| 892 |
dst->mtu = READ_ONCE(dst->dev->dev->mtu); |
| 893 |
if (mtu) |
| 894 |
dst->mtu = min(dst->mtu, mtu); |
| 895 |
dst->halen = 0; |
| 896 |
dst->output = route->output; |
| 897 |
} |
| 898 |
|
| 899 |
int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex, |
| 900 |
unsigned char halen, const unsigned char *haddr) |
| 901 |
{ |
| 902 |
struct net_device *netdev; |
| 903 |
struct mctp_dev *dev; |
| 904 |
int rc = -ENOENT; |
| 905 |
|
| 906 |
if (halen > sizeof(dst->haddr)) |
| 907 |
return -EINVAL; |
| 908 |
|
| 909 |
rcu_read_lock(); |
| 910 |
|
| 911 |
netdev = dev_get_by_index_rcu(net, ifindex); |
| 912 |
if (!netdev) |
| 913 |
goto out_unlock; |
| 914 |
|
| 915 |
if (netdev->addr_len != halen) { |
| 916 |
rc = -EINVAL; |
| 917 |
goto out_unlock; |
| 918 |
} |
| 919 |
|
| 920 |
dev = __mctp_dev_get(netdev); |
| 921 |
if (!dev) |
| 922 |
goto out_unlock; |
| 923 |
|
| 924 |
dst->dev = dev; |
| 925 |
dst->mtu = READ_ONCE(netdev->mtu); |
| 926 |
dst->halen = halen; |
| 927 |
dst->output = mctp_dst_output; |
| 928 |
dst->nexthop = 0; |
| 929 |
memcpy(dst->haddr, haddr, halen); |
| 930 |
|
| 931 |
rc = 0; |
| 932 |
|
| 933 |
out_unlock: |
| 934 |
rcu_read_unlock(); |
| 935 |
return rc; |
| 936 |
} |
| 937 |
|
| 938 |
void mctp_dst_release(struct mctp_dst *dst) |
| 939 |
{ |
| 940 |
mctp_dev_put(dst->dev); |
| 941 |
} |
| 942 |
|
| 943 |
static struct mctp_route *mctp_route_lookup_single(struct net *net, |
| 944 |
unsigned int dnet, |
| 945 |
mctp_eid_t daddr) |
| 946 |
{ |
| 947 |
struct mctp_route *rt; |
| 948 |
|
| 949 |
list_for_each_entry_rcu(rt, &net->mctp.routes, list) { |
| 950 |
if (mctp_rt_match_eid(rt, dnet, daddr)) |
| 951 |
return rt; |
| 952 |
} |
| 953 |
|
| 954 |
return NULL; |
| 955 |
} |
| 956 |
|
| 957 |
/* populates *dst on successful lookup, if set */ |
| 958 |
int mctp_route_lookup(struct net *net, unsigned int dnet, |
| 959 |
mctp_eid_t daddr, struct mctp_dst *dst) |
| 960 |
{ |
| 961 |
const unsigned int max_depth = 32; |
| 962 |
unsigned int depth, mtu = 0; |
| 963 |
int rc = -EHOSTUNREACH; |
| 964 |
|
| 965 |
rcu_read_lock(); |
| 966 |
|
| 967 |
for (depth = 0; depth < max_depth; depth++) { |
| 968 |
struct mctp_route *rt; |
| 969 |
|
| 970 |
rt = mctp_route_lookup_single(net, dnet, daddr); |
| 971 |
if (!rt) |
| 972 |
break; |
| 973 |
|
| 974 |
/* clamp mtu to the smallest in the path, allowing 0 |
| 975 |
* to specify no restrictions |
| 976 |
*/ |
| 977 |
if (mtu && rt->mtu) |
| 978 |
mtu = min(mtu, rt->mtu); |
| 979 |
else |
| 980 |
mtu = mtu ?: rt->mtu; |
| 981 |
|
| 982 |
if (rt->dst_type == MCTP_ROUTE_DIRECT) { |
| 983 |
if (dst) |
| 984 |
mctp_dst_from_route(dst, daddr, mtu, rt); |
| 985 |
rc = 0; |
| 986 |
break; |
| 987 |
|
| 988 |
} else if (rt->dst_type == MCTP_ROUTE_GATEWAY) { |
| 989 |
daddr = rt->gateway.eid; |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
rcu_read_unlock(); |
| 994 |
|
| 995 |
return rc; |
| 996 |
} |
| 997 |
|
| 998 |
static int mctp_route_lookup_null(struct net *net, struct net_device *dev, |
| 999 |
struct mctp_dst *dst) |
| 1000 |
{ |
| 1001 |
int rc = -EHOSTUNREACH; |
| 1002 |
struct mctp_route *rt; |
| 1003 |
|
| 1004 |
rcu_read_lock(); |
| 1005 |
|
| 1006 |
list_for_each_entry_rcu(rt, &net->mctp.routes, list) { |
| 1007 |
if (rt->dst_type != MCTP_ROUTE_DIRECT || rt->type != RTN_LOCAL) |
| 1008 |
continue; |
| 1009 |
|
| 1010 |
if (rt->dev->dev != dev) |
| 1011 |
continue; |
| 1012 |
|
| 1013 |
mctp_dst_from_route(dst, 0, 0, rt); |
| 1014 |
rc = 0; |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
|
| 1018 |
rcu_read_unlock(); |
| 1019 |
|
| 1020 |
return rc; |
| 1021 |
} |
| 1022 |
|
| 1023 |
static int mctp_do_fragment_route(struct mctp_dst *dst, struct sk_buff *skb, |
| 1024 |
unsigned int mtu, u8 tag) |
| 1025 |
{ |
| 1026 |
const unsigned int hlen = sizeof(struct mctp_hdr); |
| 1027 |
struct mctp_hdr *hdr, *hdr2; |
| 1028 |
unsigned int pos, size, headroom; |
| 1029 |
struct sk_buff *skb2; |
| 1030 |
int rc; |
| 1031 |
u8 seq; |
| 1032 |
|
| 1033 |
hdr = mctp_hdr(skb); |
| 1034 |
seq = 0; |
| 1035 |
rc = 0; |
| 1036 |
|
| 1037 |
if (mtu < hlen + 1) { |
| 1038 |
kfree_skb(skb); |
| 1039 |
return -EMSGSIZE; |
| 1040 |
} |
| 1041 |
|
| 1042 |
/* keep same headroom as the original skb */ |
| 1043 |
headroom = skb_headroom(skb); |
| 1044 |
|
| 1045 |
/* we've got the header */ |
| 1046 |
skb_pull(skb, hlen); |
| 1047 |
|
| 1048 |
for (pos = 0; pos < skb->len;) { |
| 1049 |
/* size of message payload */ |
| 1050 |
size = min(mtu - hlen, skb->len - pos); |
| 1051 |
|
| 1052 |
skb2 = alloc_skb(headroom + hlen + size, GFP_KERNEL); |
| 1053 |
if (!skb2) { |
| 1054 |
rc = -ENOMEM; |
| 1055 |
break; |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* generic skb copy */ |
| 1059 |
skb2->protocol = skb->protocol; |
| 1060 |
skb2->priority = skb->priority; |
| 1061 |
skb2->dev = skb->dev; |
| 1062 |
memcpy(skb2->cb, skb->cb, sizeof(skb2->cb)); |
| 1063 |
|
| 1064 |
if (skb->sk) |
| 1065 |
skb_set_owner_w(skb2, skb->sk); |
| 1066 |
|
| 1067 |
/* establish packet */ |
| 1068 |
skb_reserve(skb2, headroom); |
| 1069 |
skb_reset_network_header(skb2); |
| 1070 |
skb_put(skb2, hlen + size); |
| 1071 |
skb2->transport_header = skb2->network_header + hlen; |
| 1072 |
|
| 1073 |
/* copy header fields, calculate SOM/EOM flags & seq */ |
| 1074 |
hdr2 = mctp_hdr(skb2); |
| 1075 |
hdr2->ver = hdr->ver; |
| 1076 |
hdr2->dest = hdr->dest; |
| 1077 |
hdr2->src = hdr->src; |
| 1078 |
hdr2->flags_seq_tag = tag & |
| 1079 |
(MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); |
| 1080 |
|
| 1081 |
if (pos == 0) |
| 1082 |
hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM; |
| 1083 |
|
| 1084 |
if (pos + size == skb->len) |
| 1085 |
hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM; |
| 1086 |
|
| 1087 |
hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT; |
| 1088 |
|
| 1089 |
/* copy message payload */ |
| 1090 |
skb_copy_bits(skb, pos, skb_transport_header(skb2), size); |
| 1091 |
|
| 1092 |
/* we need to copy the extensions, for MCTP flow data */ |
| 1093 |
skb_ext_copy(skb2, skb); |
| 1094 |
|
| 1095 |
/* do route */ |
| 1096 |
rc = dst->output(dst, skb2); |
| 1097 |
if (rc) |
| 1098 |
break; |
| 1099 |
|
| 1100 |
seq = (seq + 1) & MCTP_HDR_SEQ_MASK; |
| 1101 |
pos += size; |
| 1102 |
} |
| 1103 |
|
| 1104 |
consume_skb(skb); |
| 1105 |
return rc; |
| 1106 |
} |
| 1107 |
|
| 1108 |
int mctp_local_output(struct sock *sk, struct mctp_dst *dst, |
| 1109 |
struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag) |
| 1110 |
{ |
| 1111 |
struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 1112 |
struct mctp_sk_key *key; |
| 1113 |
struct mctp_hdr *hdr; |
| 1114 |
unsigned long flags; |
| 1115 |
unsigned int netid; |
| 1116 |
unsigned int mtu; |
| 1117 |
mctp_eid_t saddr; |
| 1118 |
int rc; |
| 1119 |
u8 tag; |
| 1120 |
|
| 1121 |
KUNIT_STATIC_STUB_REDIRECT(mctp_local_output, sk, dst, skb, daddr, |
| 1122 |
req_tag); |
| 1123 |
|
| 1124 |
rc = -ENODEV; |
| 1125 |
|
| 1126 |
spin_lock_irqsave(&dst->dev->addrs_lock, flags); |
| 1127 |
if (dst->dev->num_addrs == 0) { |
| 1128 |
rc = -EHOSTUNREACH; |
| 1129 |
} else { |
| 1130 |
/* use the outbound interface's first address as our source */ |
| 1131 |
saddr = dst->dev->addrs[0]; |
| 1132 |
rc = 0; |
| 1133 |
} |
| 1134 |
spin_unlock_irqrestore(&dst->dev->addrs_lock, flags); |
| 1135 |
netid = READ_ONCE(dst->dev->net); |
| 1136 |
|
| 1137 |
if (rc) |
| 1138 |
goto out_release; |
| 1139 |
|
| 1140 |
if (req_tag & MCTP_TAG_OWNER) { |
| 1141 |
if (req_tag & MCTP_TAG_PREALLOC) |
| 1142 |
key = mctp_lookup_prealloc_tag(msk, netid, daddr, |
| 1143 |
req_tag, &tag); |
| 1144 |
else |
| 1145 |
key = mctp_alloc_local_tag(msk, netid, saddr, daddr, |
| 1146 |
false, &tag); |
| 1147 |
|
| 1148 |
if (IS_ERR(key)) { |
| 1149 |
rc = PTR_ERR(key); |
| 1150 |
goto out_release; |
| 1151 |
} |
| 1152 |
mctp_skb_set_flow(skb, key); |
| 1153 |
/* done with the key in this scope */ |
| 1154 |
mctp_key_unref(key); |
| 1155 |
tag |= MCTP_HDR_FLAG_TO; |
| 1156 |
} else { |
| 1157 |
key = NULL; |
| 1158 |
tag = req_tag & MCTP_TAG_MASK; |
| 1159 |
} |
| 1160 |
|
| 1161 |
skb->pkt_type = PACKET_OUTGOING; |
| 1162 |
skb->protocol = htons(ETH_P_MCTP); |
| 1163 |
skb->priority = 0; |
| 1164 |
skb_reset_transport_header(skb); |
| 1165 |
skb_push(skb, sizeof(struct mctp_hdr)); |
| 1166 |
skb_reset_network_header(skb); |
| 1167 |
skb->dev = dst->dev->dev; |
| 1168 |
|
| 1169 |
/* set up common header fields */ |
| 1170 |
hdr = mctp_hdr(skb); |
| 1171 |
hdr->ver = 1; |
| 1172 |
hdr->dest = daddr; |
| 1173 |
hdr->src = saddr; |
| 1174 |
|
| 1175 |
mtu = dst->mtu; |
| 1176 |
|
| 1177 |
if (skb->len + sizeof(struct mctp_hdr) <= mtu) { |
| 1178 |
hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | |
| 1179 |
MCTP_HDR_FLAG_EOM | tag; |
| 1180 |
rc = dst->output(dst, skb); |
| 1181 |
} else { |
| 1182 |
rc = mctp_do_fragment_route(dst, skb, mtu, tag); |
| 1183 |
} |
| 1184 |
|
| 1185 |
/* route output functions consume the skb, even on error */ |
| 1186 |
skb = NULL; |
| 1187 |
|
| 1188 |
out_release: |
| 1189 |
kfree_skb(skb); |
| 1190 |
return rc; |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* route management */ |
| 1194 |
|
| 1195 |
/* mctp_route_add(): Add the provided route, previously allocated via |
| 1196 |
* mctp_route_alloc(). On success, takes ownership of @rt, which includes a |
| 1197 |
* hold on rt->dev for usage in the route table. On failure a caller will want |
| 1198 |
* to mctp_route_release(). |
| 1199 |
* |
| 1200 |
* We expect that the caller has set rt->type, rt->dst_type, rt->min, rt->max, |
| 1201 |
* rt->mtu and either rt->dev (with a reference held appropriately) or |
| 1202 |
* rt->gateway. Other fields will be populated. |
| 1203 |
*/ |
| 1204 |
static int mctp_route_add(struct net *net, struct mctp_route *rt) |
| 1205 |
{ |
| 1206 |
struct mctp_route *ert; |
| 1207 |
|
| 1208 |
if (!mctp_address_unicast(rt->min) || !mctp_address_unicast(rt->max)) |
| 1209 |
return -EINVAL; |
| 1210 |
|
| 1211 |
if (rt->dst_type == MCTP_ROUTE_DIRECT && !rt->dev) |
| 1212 |
return -EINVAL; |
| 1213 |
|
| 1214 |
if (rt->dst_type == MCTP_ROUTE_GATEWAY && !rt->gateway.eid) |
| 1215 |
return -EINVAL; |
| 1216 |
|
| 1217 |
switch (rt->type) { |
| 1218 |
case RTN_LOCAL: |
| 1219 |
rt->output = mctp_dst_input; |
| 1220 |
break; |
| 1221 |
case RTN_UNICAST: |
| 1222 |
rt->output = mctp_dst_output; |
| 1223 |
break; |
| 1224 |
default: |
| 1225 |
return -EINVAL; |
| 1226 |
} |
| 1227 |
|
| 1228 |
ASSERT_RTNL(); |
| 1229 |
|
| 1230 |
/* Prevent duplicate identical routes. */ |
| 1231 |
list_for_each_entry(ert, &net->mctp.routes, list) { |
| 1232 |
if (mctp_rt_compare_exact(rt, ert)) { |
| 1233 |
return -EEXIST; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
list_add_rcu(&rt->list, &net->mctp.routes); |
| 1238 |
|
| 1239 |
return 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
static int mctp_route_remove(struct net *net, unsigned int netid, |
| 1243 |
mctp_eid_t daddr_start, unsigned int daddr_extent, |
| 1244 |
unsigned char type) |
| 1245 |
{ |
| 1246 |
struct mctp_route *rt, *tmp; |
| 1247 |
mctp_eid_t daddr_end; |
| 1248 |
bool dropped; |
| 1249 |
|
| 1250 |
if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255) |
| 1251 |
return -EINVAL; |
| 1252 |
|
| 1253 |
daddr_end = daddr_start + daddr_extent; |
| 1254 |
dropped = false; |
| 1255 |
|
| 1256 |
ASSERT_RTNL(); |
| 1257 |
|
| 1258 |
list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) { |
| 1259 |
if (mctp_route_netid(rt) == netid && |
| 1260 |
rt->min == daddr_start && rt->max == daddr_end && |
| 1261 |
rt->type == type) { |
| 1262 |
list_del_rcu(&rt->list); |
| 1263 |
/* TODO: immediate RTM_DELROUTE */ |
| 1264 |
mctp_route_release(rt); |
| 1265 |
dropped = true; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
return dropped ? 0 : -ENOENT; |
| 1270 |
} |
| 1271 |
|
| 1272 |
int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr) |
| 1273 |
{ |
| 1274 |
struct mctp_route *rt; |
| 1275 |
int rc; |
| 1276 |
|
| 1277 |
rt = mctp_route_alloc(); |
| 1278 |
if (!rt) |
| 1279 |
return -ENOMEM; |
| 1280 |
|
| 1281 |
rt->min = addr; |
| 1282 |
rt->max = addr; |
| 1283 |
rt->dst_type = MCTP_ROUTE_DIRECT; |
| 1284 |
rt->dev = mdev; |
| 1285 |
rt->type = RTN_LOCAL; |
| 1286 |
|
| 1287 |
mctp_dev_hold(rt->dev); |
| 1288 |
|
| 1289 |
rc = mctp_route_add(dev_net(mdev->dev), rt); |
| 1290 |
if (rc) |
| 1291 |
mctp_route_release(rt); |
| 1292 |
|
| 1293 |
return rc; |
| 1294 |
} |
| 1295 |
|
| 1296 |
int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr) |
| 1297 |
{ |
| 1298 |
return mctp_route_remove(dev_net(mdev->dev), mdev->net, |
| 1299 |
addr, 0, RTN_LOCAL); |
| 1300 |
} |
| 1301 |
|
| 1302 |
/* removes all entries for a given device */ |
| 1303 |
void mctp_route_remove_dev(struct mctp_dev *mdev) |
| 1304 |
{ |
| 1305 |
struct net *net = dev_net(mdev->dev); |
| 1306 |
struct mctp_route *rt, *tmp; |
| 1307 |
|
| 1308 |
ASSERT_RTNL(); |
| 1309 |
list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) { |
| 1310 |
if (rt->dst_type == MCTP_ROUTE_DIRECT && rt->dev == mdev) { |
| 1311 |
list_del_rcu(&rt->list); |
| 1312 |
/* TODO: immediate RTM_DELROUTE */ |
| 1313 |
mctp_route_release(rt); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* Incoming packet-handling */ |
| 1319 |
|
| 1320 |
static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev, |
| 1321 |
struct packet_type *pt, |
| 1322 |
struct net_device *orig_dev) |
| 1323 |
{ |
| 1324 |
struct net *net = dev_net(dev); |
| 1325 |
struct mctp_dev *mdev; |
| 1326 |
struct mctp_skb_cb *cb; |
| 1327 |
struct mctp_dst dst; |
| 1328 |
struct mctp_hdr *mh; |
| 1329 |
int rc; |
| 1330 |
u8 ver; |
| 1331 |
|
| 1332 |
rcu_read_lock(); |
| 1333 |
mdev = __mctp_dev_get(dev); |
| 1334 |
rcu_read_unlock(); |
| 1335 |
if (!mdev) { |
| 1336 |
/* basic non-data sanity checks */ |
| 1337 |
goto err_drop; |
| 1338 |
} |
| 1339 |
|
| 1340 |
if (!pskb_may_pull(skb, sizeof(struct mctp_hdr))) |
| 1341 |
goto err_drop; |
| 1342 |
|
| 1343 |
skb_reset_transport_header(skb); |
| 1344 |
skb_reset_network_header(skb); |
| 1345 |
|
| 1346 |
/* We have enough for a header; decode and route */ |
| 1347 |
mh = mctp_hdr(skb); |
| 1348 |
ver = mh->ver & MCTP_HDR_VER_MASK; |
| 1349 |
if (ver < MCTP_VER_MIN || ver > MCTP_VER_MAX) |
| 1350 |
goto err_drop; |
| 1351 |
|
| 1352 |
/* source must be valid unicast or null; drop reserved ranges and |
| 1353 |
* broadcast |
| 1354 |
*/ |
| 1355 |
if (!(mctp_address_unicast(mh->src) || mctp_address_null(mh->src))) |
| 1356 |
goto err_drop; |
| 1357 |
|
| 1358 |
/* dest address: as above, but allow broadcast */ |
| 1359 |
if (!(mctp_address_unicast(mh->dest) || mctp_address_null(mh->dest) || |
| 1360 |
mctp_address_broadcast(mh->dest))) |
| 1361 |
goto err_drop; |
| 1362 |
|
| 1363 |
/* MCTP drivers must populate halen/haddr */ |
| 1364 |
if (dev->type == ARPHRD_MCTP) { |
| 1365 |
cb = mctp_cb(skb); |
| 1366 |
} else { |
| 1367 |
cb = __mctp_cb(skb); |
| 1368 |
cb->halen = 0; |
| 1369 |
} |
| 1370 |
cb->net = READ_ONCE(mdev->net); |
| 1371 |
cb->ifindex = dev->ifindex; |
| 1372 |
|
| 1373 |
rc = mctp_route_lookup(net, cb->net, mh->dest, &dst); |
| 1374 |
|
| 1375 |
/* NULL EID, but addressed to our physical address */ |
| 1376 |
if (rc && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST) |
| 1377 |
rc = mctp_route_lookup_null(net, dev, &dst); |
| 1378 |
|
| 1379 |
if (rc) |
| 1380 |
goto err_drop; |
| 1381 |
|
| 1382 |
dst.output(&dst, skb); |
| 1383 |
mctp_dst_release(&dst); |
| 1384 |
mctp_dev_put(mdev); |
| 1385 |
|
| 1386 |
return NET_RX_SUCCESS; |
| 1387 |
|
| 1388 |
err_drop: |
| 1389 |
kfree_skb(skb); |
| 1390 |
mctp_dev_put(mdev); |
| 1391 |
return NET_RX_DROP; |
| 1392 |
} |
| 1393 |
|
| 1394 |
static struct packet_type mctp_packet_type = { |
| 1395 |
.type = cpu_to_be16(ETH_P_MCTP), |
| 1396 |
.func = mctp_pkttype_receive, |
| 1397 |
}; |
| 1398 |
|
| 1399 |
/* netlink interface */ |
| 1400 |
|
| 1401 |
static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = { |
| 1402 |
[RTA_DST] = { .type = NLA_U8 }, |
| 1403 |
[RTA_METRICS] = { .type = NLA_NESTED }, |
| 1404 |
[RTA_OIF] = { .type = NLA_U32 }, |
| 1405 |
[RTA_GATEWAY] = NLA_POLICY_EXACT_LEN(sizeof(struct mctp_fq_addr)), |
| 1406 |
}; |
| 1407 |
|
| 1408 |
static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = { |
| 1409 |
[RTAX_MTU] = { .type = NLA_U32 }, |
| 1410 |
}; |
| 1411 |
|
| 1412 |
/* base parsing; common to both _lookup and _populate variants. |
| 1413 |
* |
| 1414 |
* For gateway routes (which have a RTA_GATEWAY, and no RTA_OIF), we populate |
| 1415 |
* *gatweayp. for direct routes (RTA_OIF, no RTA_GATEWAY), we populate *mdev. |
| 1416 |
*/ |
| 1417 |
static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh, |
| 1418 |
struct netlink_ext_ack *extack, |
| 1419 |
struct nlattr **tb, struct rtmsg **rtm, |
| 1420 |
struct mctp_dev **mdev, |
| 1421 |
struct mctp_fq_addr *gatewayp, |
| 1422 |
mctp_eid_t *daddr_start) |
| 1423 |
{ |
| 1424 |
struct mctp_fq_addr *gateway = NULL; |
| 1425 |
unsigned int ifindex = 0; |
| 1426 |
struct net_device *dev; |
| 1427 |
int rc; |
| 1428 |
|
| 1429 |
rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, |
| 1430 |
rta_mctp_policy, extack); |
| 1431 |
if (rc < 0) { |
| 1432 |
NL_SET_ERR_MSG(extack, "incorrect format"); |
| 1433 |
return rc; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if (!tb[RTA_DST]) { |
| 1437 |
NL_SET_ERR_MSG(extack, "dst EID missing"); |
| 1438 |
return -EINVAL; |
| 1439 |
} |
| 1440 |
*daddr_start = nla_get_u8(tb[RTA_DST]); |
| 1441 |
|
| 1442 |
if (tb[RTA_OIF]) |
| 1443 |
ifindex = nla_get_u32(tb[RTA_OIF]); |
| 1444 |
|
| 1445 |
if (tb[RTA_GATEWAY]) |
| 1446 |
gateway = nla_data(tb[RTA_GATEWAY]); |
| 1447 |
|
| 1448 |
if (ifindex && gateway) { |
| 1449 |
NL_SET_ERR_MSG(extack, |
| 1450 |
"cannot specify both ifindex and gateway"); |
| 1451 |
return -EINVAL; |
| 1452 |
|
| 1453 |
} else if (ifindex) { |
| 1454 |
dev = __dev_get_by_index(net, ifindex); |
| 1455 |
if (!dev) { |
| 1456 |
NL_SET_ERR_MSG(extack, "bad ifindex"); |
| 1457 |
return -ENODEV; |
| 1458 |
} |
| 1459 |
*mdev = mctp_dev_get_rtnl(dev); |
| 1460 |
if (!*mdev) |
| 1461 |
return -ENODEV; |
| 1462 |
gatewayp->eid = 0; |
| 1463 |
|
| 1464 |
} else if (gateway) { |
| 1465 |
if (!mctp_address_unicast(gateway->eid)) { |
| 1466 |
NL_SET_ERR_MSG(extack, "bad gateway"); |
| 1467 |
return -EINVAL; |
| 1468 |
} |
| 1469 |
|
| 1470 |
gatewayp->eid = gateway->eid; |
| 1471 |
gatewayp->net = gateway->net != MCTP_NET_ANY ? |
| 1472 |
gateway->net : |
| 1473 |
READ_ONCE(net->mctp.default_net); |
| 1474 |
*mdev = NULL; |
| 1475 |
|
| 1476 |
} else { |
| 1477 |
NL_SET_ERR_MSG(extack, "no route output provided"); |
| 1478 |
return -EINVAL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
*rtm = nlmsg_data(nlh); |
| 1482 |
if ((*rtm)->rtm_family != AF_MCTP) { |
| 1483 |
NL_SET_ERR_MSG(extack, "route family must be AF_MCTP"); |
| 1484 |
return -EINVAL; |
| 1485 |
} |
| 1486 |
|
| 1487 |
if ((*rtm)->rtm_type != RTN_UNICAST) { |
| 1488 |
NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST"); |
| 1489 |
return -EINVAL; |
| 1490 |
} |
| 1491 |
|
| 1492 |
return 0; |
| 1493 |
} |
| 1494 |
|
| 1495 |
/* Route parsing for lookup operations; we only need the "route target" |
| 1496 |
* components (ie., network and dest-EID range). |
| 1497 |
*/ |
| 1498 |
static int mctp_route_nlparse_lookup(struct net *net, struct nlmsghdr *nlh, |
| 1499 |
struct netlink_ext_ack *extack, |
| 1500 |
unsigned char *type, unsigned int *netid, |
| 1501 |
mctp_eid_t *daddr_start, |
| 1502 |
unsigned int *daddr_extent) |
| 1503 |
{ |
| 1504 |
struct nlattr *tb[RTA_MAX + 1]; |
| 1505 |
struct mctp_fq_addr gw; |
| 1506 |
struct mctp_dev *mdev; |
| 1507 |
struct rtmsg *rtm; |
| 1508 |
int rc; |
| 1509 |
|
| 1510 |
rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, |
| 1511 |
&mdev, &gw, daddr_start); |
| 1512 |
if (rc) |
| 1513 |
return rc; |
| 1514 |
|
| 1515 |
if (mdev) { |
| 1516 |
*netid = mdev->net; |
| 1517 |
} else if (gw.eid) { |
| 1518 |
*netid = gw.net; |
| 1519 |
} else { |
| 1520 |
/* bug: _nlparse_common should not allow this */ |
| 1521 |
return -1; |
| 1522 |
} |
| 1523 |
|
| 1524 |
*type = rtm->rtm_type; |
| 1525 |
*daddr_extent = rtm->rtm_dst_len; |
| 1526 |
|
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
/* Full route parse for RTM_NEWROUTE: populate @rt. On success, |
| 1531 |
* MCTP_ROUTE_DIRECT routes (ie, those with a direct dev) will hold a reference |
| 1532 |
* to that dev. |
| 1533 |
*/ |
| 1534 |
static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, |
| 1535 |
struct netlink_ext_ack *extack, |
| 1536 |
struct mctp_route *rt) |
| 1537 |
{ |
| 1538 |
struct nlattr *tbx[RTAX_MAX + 1]; |
| 1539 |
struct nlattr *tb[RTA_MAX + 1]; |
| 1540 |
unsigned int daddr_extent; |
| 1541 |
struct mctp_fq_addr gw; |
| 1542 |
mctp_eid_t daddr_start; |
| 1543 |
struct mctp_dev *dev; |
| 1544 |
struct rtmsg *rtm; |
| 1545 |
u32 mtu = 0; |
| 1546 |
int rc; |
| 1547 |
|
| 1548 |
rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, |
| 1549 |
&dev, &gw, &daddr_start); |
| 1550 |
if (rc) |
| 1551 |
return rc; |
| 1552 |
|
| 1553 |
daddr_extent = rtm->rtm_dst_len; |
| 1554 |
|
| 1555 |
if (daddr_extent > 0xff || daddr_extent + daddr_start >= 255) { |
| 1556 |
NL_SET_ERR_MSG(extack, "invalid eid range"); |
| 1557 |
return -EINVAL; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (tb[RTA_METRICS]) { |
| 1561 |
rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS], |
| 1562 |
rta_metrics_policy, NULL); |
| 1563 |
if (rc < 0) { |
| 1564 |
NL_SET_ERR_MSG(extack, "incorrect RTA_METRICS format"); |
| 1565 |
return rc; |
| 1566 |
} |
| 1567 |
if (tbx[RTAX_MTU]) |
| 1568 |
mtu = nla_get_u32(tbx[RTAX_MTU]); |
| 1569 |
} |
| 1570 |
|
| 1571 |
rt->type = rtm->rtm_type; |
| 1572 |
rt->min = daddr_start; |
| 1573 |
rt->max = daddr_start + daddr_extent; |
| 1574 |
rt->mtu = mtu; |
| 1575 |
if (gw.eid) { |
| 1576 |
rt->dst_type = MCTP_ROUTE_GATEWAY; |
| 1577 |
rt->gateway.eid = gw.eid; |
| 1578 |
rt->gateway.net = gw.net; |
| 1579 |
} else { |
| 1580 |
rt->dst_type = MCTP_ROUTE_DIRECT; |
| 1581 |
rt->dev = dev; |
| 1582 |
mctp_dev_hold(rt->dev); |
| 1583 |
} |
| 1584 |
|
| 1585 |
return 0; |
| 1586 |
} |
| 1587 |
|
| 1588 |
static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1589 |
struct netlink_ext_ack *extack) |
| 1590 |
{ |
| 1591 |
struct net *net = sock_net(skb->sk); |
| 1592 |
struct mctp_route *rt; |
| 1593 |
int rc; |
| 1594 |
|
| 1595 |
rt = mctp_route_alloc(); |
| 1596 |
if (!rt) |
| 1597 |
return -ENOMEM; |
| 1598 |
|
| 1599 |
rc = mctp_route_nlparse_populate(net, nlh, extack, rt); |
| 1600 |
if (rc < 0) |
| 1601 |
goto err_free; |
| 1602 |
|
| 1603 |
if (rt->dst_type == MCTP_ROUTE_DIRECT && |
| 1604 |
rt->dev->dev->flags & IFF_LOOPBACK) { |
| 1605 |
NL_SET_ERR_MSG(extack, "no routes to loopback"); |
| 1606 |
rc = -EINVAL; |
| 1607 |
goto err_free; |
| 1608 |
} |
| 1609 |
|
| 1610 |
rc = mctp_route_add(net, rt); |
| 1611 |
if (!rc) |
| 1612 |
return 0; |
| 1613 |
|
| 1614 |
err_free: |
| 1615 |
mctp_route_release(rt); |
| 1616 |
return rc; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1620 |
struct netlink_ext_ack *extack) |
| 1621 |
{ |
| 1622 |
struct net *net = sock_net(skb->sk); |
| 1623 |
unsigned int netid, daddr_extent; |
| 1624 |
unsigned char type = RTN_UNSPEC; |
| 1625 |
mctp_eid_t daddr_start; |
| 1626 |
int rc; |
| 1627 |
|
| 1628 |
rc = mctp_route_nlparse_lookup(net, nlh, extack, &type, &netid, |
| 1629 |
&daddr_start, &daddr_extent); |
| 1630 |
if (rc < 0) |
| 1631 |
return rc; |
| 1632 |
|
| 1633 |
/* we only have unicast routes */ |
| 1634 |
if (type != RTN_UNICAST) |
| 1635 |
return -EINVAL; |
| 1636 |
|
| 1637 |
rc = mctp_route_remove(net, netid, daddr_start, daddr_extent, type); |
| 1638 |
return rc; |
| 1639 |
} |
| 1640 |
|
| 1641 |
static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt, |
| 1642 |
u32 portid, u32 seq, int event, unsigned int flags) |
| 1643 |
{ |
| 1644 |
struct nlmsghdr *nlh; |
| 1645 |
struct rtmsg *hdr; |
| 1646 |
void *metrics; |
| 1647 |
|
| 1648 |
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); |
| 1649 |
if (!nlh) |
| 1650 |
return -EMSGSIZE; |
| 1651 |
|
| 1652 |
hdr = nlmsg_data(nlh); |
| 1653 |
memset(hdr, 0, sizeof(*hdr)); |
| 1654 |
hdr->rtm_family = AF_MCTP; |
| 1655 |
|
| 1656 |
/* we use the _len fields as a number of EIDs, rather than |
| 1657 |
* a number of bits in the address |
| 1658 |
*/ |
| 1659 |
hdr->rtm_dst_len = rt->max - rt->min; |
| 1660 |
hdr->rtm_src_len = 0; |
| 1661 |
hdr->rtm_tos = 0; |
| 1662 |
hdr->rtm_table = RT_TABLE_DEFAULT; |
| 1663 |
hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */ |
| 1664 |
hdr->rtm_type = rt->type; |
| 1665 |
|
| 1666 |
if (nla_put_u8(skb, RTA_DST, rt->min)) |
| 1667 |
goto cancel; |
| 1668 |
|
| 1669 |
metrics = nla_nest_start_noflag(skb, RTA_METRICS); |
| 1670 |
if (!metrics) |
| 1671 |
goto cancel; |
| 1672 |
|
| 1673 |
if (rt->mtu) { |
| 1674 |
if (nla_put_u32(skb, RTAX_MTU, rt->mtu)) |
| 1675 |
goto cancel; |
| 1676 |
} |
| 1677 |
|
| 1678 |
nla_nest_end(skb, metrics); |
| 1679 |
|
| 1680 |
if (rt->dst_type == MCTP_ROUTE_DIRECT) { |
| 1681 |
hdr->rtm_scope = RT_SCOPE_LINK; |
| 1682 |
if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex)) |
| 1683 |
goto cancel; |
| 1684 |
} else if (rt->dst_type == MCTP_ROUTE_GATEWAY) { |
| 1685 |
hdr->rtm_scope = RT_SCOPE_UNIVERSE; |
| 1686 |
if (nla_put(skb, RTA_GATEWAY, |
| 1687 |
sizeof(rt->gateway), &rt->gateway)) |
| 1688 |
goto cancel; |
| 1689 |
} |
| 1690 |
|
| 1691 |
nlmsg_end(skb, nlh); |
| 1692 |
|
| 1693 |
return 0; |
| 1694 |
|
| 1695 |
cancel: |
| 1696 |
nlmsg_cancel(skb, nlh); |
| 1697 |
return -EMSGSIZE; |
| 1698 |
} |
| 1699 |
|
| 1700 |
static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb) |
| 1701 |
{ |
| 1702 |
struct net *net = sock_net(skb->sk); |
| 1703 |
struct mctp_route *rt; |
| 1704 |
int s_idx, idx; |
| 1705 |
|
| 1706 |
/* TODO: allow filtering on route data, possibly under |
| 1707 |
* cb->strict_check |
| 1708 |
*/ |
| 1709 |
|
| 1710 |
/* TODO: change to struct overlay */ |
| 1711 |
s_idx = cb->args[0]; |
| 1712 |
idx = 0; |
| 1713 |
|
| 1714 |
rcu_read_lock(); |
| 1715 |
list_for_each_entry_rcu(rt, &net->mctp.routes, list) { |
| 1716 |
if (idx++ < s_idx) |
| 1717 |
continue; |
| 1718 |
if (mctp_fill_rtinfo(skb, rt, |
| 1719 |
NETLINK_CB(cb->skb).portid, |
| 1720 |
cb->nlh->nlmsg_seq, |
| 1721 |
RTM_NEWROUTE, NLM_F_MULTI) < 0) |
| 1722 |
break; |
| 1723 |
} |
| 1724 |
|
| 1725 |
rcu_read_unlock(); |
| 1726 |
cb->args[0] = idx; |
| 1727 |
|
| 1728 |
return skb->len; |
| 1729 |
} |
| 1730 |
|
| 1731 |
/* net namespace implementation */ |
| 1732 |
static int __net_init mctp_routes_net_init(struct net *net) |
| 1733 |
{ |
| 1734 |
struct netns_mctp *ns = &net->mctp; |
| 1735 |
|
| 1736 |
INIT_LIST_HEAD(&ns->routes); |
| 1737 |
hash_init(ns->binds); |
| 1738 |
mutex_init(&ns->bind_lock); |
| 1739 |
INIT_HLIST_HEAD(&ns->keys); |
| 1740 |
spin_lock_init(&ns->keys_lock); |
| 1741 |
WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET)); |
| 1742 |
return 0; |
| 1743 |
} |
| 1744 |
|
| 1745 |
static void __net_exit mctp_routes_net_exit(struct net *net) |
| 1746 |
{ |
| 1747 |
struct mctp_route *rt; |
| 1748 |
|
| 1749 |
rcu_read_lock(); |
| 1750 |
list_for_each_entry_rcu(rt, &net->mctp.routes, list) |
| 1751 |
mctp_route_release(rt); |
| 1752 |
rcu_read_unlock(); |
| 1753 |
} |
| 1754 |
|
| 1755 |
static struct pernet_operations mctp_net_ops = { |
| 1756 |
.init = mctp_routes_net_init, |
| 1757 |
.exit = mctp_routes_net_exit, |
| 1758 |
}; |
| 1759 |
|
| 1760 |
static const struct rtnl_msg_handler mctp_route_rtnl_msg_handlers[] = { |
| 1761 |
{THIS_MODULE, PF_MCTP, RTM_NEWROUTE, mctp_newroute, NULL, 0}, |
| 1762 |
{THIS_MODULE, PF_MCTP, RTM_DELROUTE, mctp_delroute, NULL, 0}, |
| 1763 |
{THIS_MODULE, PF_MCTP, RTM_GETROUTE, NULL, mctp_dump_rtinfo, 0}, |
| 1764 |
}; |
| 1765 |
|
| 1766 |
int __init mctp_routes_init(void) |
| 1767 |
{ |
| 1768 |
int err; |
| 1769 |
|
| 1770 |
dev_add_pack(&mctp_packet_type); |
| 1771 |
|
| 1772 |
err = register_pernet_subsys(&mctp_net_ops); |
| 1773 |
if (err) |
| 1774 |
goto err_pernet; |
| 1775 |
|
| 1776 |
err = rtnl_register_many(mctp_route_rtnl_msg_handlers); |
| 1777 |
if (err) |
| 1778 |
goto err_rtnl; |
| 1779 |
|
| 1780 |
return 0; |
| 1781 |
|
| 1782 |
err_rtnl: |
| 1783 |
unregister_pernet_subsys(&mctp_net_ops); |
| 1784 |
err_pernet: |
| 1785 |
dev_remove_pack(&mctp_packet_type); |
| 1786 |
return err; |
| 1787 |
} |
| 1788 |
|
| 1789 |
void mctp_routes_exit(void) |
| 1790 |
{ |
| 1791 |
rtnl_unregister_many(mctp_route_rtnl_msg_handlers); |
| 1792 |
unregister_pernet_subsys(&mctp_net_ops); |
| 1793 |
dev_remove_pack(&mctp_packet_type); |
| 1794 |
} |
| 1795 |
|
| 1796 |
#if IS_ENABLED(CONFIG_MCTP_TEST) |
| 1797 |
#include "test/route-test.c" |
| 1798 |
#endif |