1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <kunit/test.h>
4
5 #include "utils.h"
6
7 struct mctp_test_route {
8 struct mctp_route rt;
9 struct sk_buff_head pkts;
10 };
11
mctp_test_route_output(struct mctp_route * rt,struct sk_buff * skb)12 static int mctp_test_route_output(struct mctp_route *rt, struct sk_buff *skb)
13 {
14 struct mctp_test_route *test_rt = container_of(rt, struct mctp_test_route, rt);
15
16 skb_queue_tail(&test_rt->pkts, skb);
17
18 return 0;
19 }
20
21 /* local version of mctp_route_alloc() */
mctp_route_test_alloc(void)22 static struct mctp_test_route *mctp_route_test_alloc(void)
23 {
24 struct mctp_test_route *rt;
25
26 rt = kzalloc(sizeof(*rt), GFP_KERNEL);
27 if (!rt)
28 return NULL;
29
30 INIT_LIST_HEAD(&rt->rt.list);
31 refcount_set(&rt->rt.refs, 1);
32 rt->rt.output = mctp_test_route_output;
33
34 skb_queue_head_init(&rt->pkts);
35
36 return rt;
37 }
38
mctp_test_create_route(struct net * net,struct mctp_dev * dev,mctp_eid_t eid,unsigned int mtu)39 static struct mctp_test_route *mctp_test_create_route(struct net *net,
40 struct mctp_dev *dev,
41 mctp_eid_t eid,
42 unsigned int mtu)
43 {
44 struct mctp_test_route *rt;
45
46 rt = mctp_route_test_alloc();
47 if (!rt)
48 return NULL;
49
50 rt->rt.min = eid;
51 rt->rt.max = eid;
52 rt->rt.mtu = mtu;
53 rt->rt.type = RTN_UNSPEC;
54 if (dev)
55 mctp_dev_hold(dev);
56 rt->rt.dev = dev;
57
58 list_add_rcu(&rt->rt.list, &net->mctp.routes);
59
60 return rt;
61 }
62
mctp_test_route_destroy(struct kunit * test,struct mctp_test_route * rt)63 static void mctp_test_route_destroy(struct kunit *test,
64 struct mctp_test_route *rt)
65 {
66 unsigned int refs;
67
68 rtnl_lock();
69 list_del_rcu(&rt->rt.list);
70 rtnl_unlock();
71
72 skb_queue_purge(&rt->pkts);
73 if (rt->rt.dev)
74 mctp_dev_put(rt->rt.dev);
75
76 refs = refcount_read(&rt->rt.refs);
77 KUNIT_ASSERT_EQ_MSG(test, refs, 1, "route ref imbalance");
78
79 kfree_rcu(&rt->rt, rcu);
80 }
81
mctp_test_skb_set_dev(struct sk_buff * skb,struct mctp_test_dev * dev)82 static void mctp_test_skb_set_dev(struct sk_buff *skb,
83 struct mctp_test_dev *dev)
84 {
85 struct mctp_skb_cb *cb;
86
87 cb = mctp_cb(skb);
88 cb->net = READ_ONCE(dev->mdev->net);
89 skb->dev = dev->ndev;
90 }
91
mctp_test_create_skb(const struct mctp_hdr * hdr,unsigned int data_len)92 static struct sk_buff *mctp_test_create_skb(const struct mctp_hdr *hdr,
93 unsigned int data_len)
94 {
95 size_t hdr_len = sizeof(*hdr);
96 struct sk_buff *skb;
97 unsigned int i;
98 u8 *buf;
99
100 skb = alloc_skb(hdr_len + data_len, GFP_KERNEL);
101 if (!skb)
102 return NULL;
103
104 __mctp_cb(skb);
105 memcpy(skb_put(skb, hdr_len), hdr, hdr_len);
106
107 buf = skb_put(skb, data_len);
108 for (i = 0; i < data_len; i++)
109 buf[i] = i & 0xff;
110
111 return skb;
112 }
113
__mctp_test_create_skb_data(const struct mctp_hdr * hdr,const void * data,size_t data_len)114 static struct sk_buff *__mctp_test_create_skb_data(const struct mctp_hdr *hdr,
115 const void *data,
116 size_t data_len)
117 {
118 size_t hdr_len = sizeof(*hdr);
119 struct sk_buff *skb;
120
121 skb = alloc_skb(hdr_len + data_len, GFP_KERNEL);
122 if (!skb)
123 return NULL;
124
125 __mctp_cb(skb);
126 memcpy(skb_put(skb, hdr_len), hdr, hdr_len);
127 memcpy(skb_put(skb, data_len), data, data_len);
128
129 return skb;
130 }
131
132 #define mctp_test_create_skb_data(h, d) \
133 __mctp_test_create_skb_data(h, d, sizeof(*d))
134
135 struct mctp_frag_test {
136 unsigned int mtu;
137 unsigned int msgsize;
138 unsigned int n_frags;
139 };
140
mctp_test_fragment(struct kunit * test)141 static void mctp_test_fragment(struct kunit *test)
142 {
143 const struct mctp_frag_test *params;
144 int rc, i, n, mtu, msgsize;
145 struct mctp_test_route *rt;
146 struct sk_buff *skb;
147 struct mctp_hdr hdr;
148 u8 seq;
149
150 params = test->param_value;
151 mtu = params->mtu;
152 msgsize = params->msgsize;
153
154 hdr.ver = 1;
155 hdr.src = 8;
156 hdr.dest = 10;
157 hdr.flags_seq_tag = MCTP_HDR_FLAG_TO;
158
159 skb = mctp_test_create_skb(&hdr, msgsize);
160 KUNIT_ASSERT_TRUE(test, skb);
161
162 rt = mctp_test_create_route(&init_net, NULL, 10, mtu);
163 KUNIT_ASSERT_TRUE(test, rt);
164
165 rc = mctp_do_fragment_route(&rt->rt, skb, mtu, MCTP_TAG_OWNER);
166 KUNIT_EXPECT_FALSE(test, rc);
167
168 n = rt->pkts.qlen;
169
170 KUNIT_EXPECT_EQ(test, n, params->n_frags);
171
172 for (i = 0;; i++) {
173 struct mctp_hdr *hdr2;
174 struct sk_buff *skb2;
175 u8 tag_mask, seq2;
176 bool first, last;
177
178 first = i == 0;
179 last = i == (n - 1);
180
181 skb2 = skb_dequeue(&rt->pkts);
182
183 if (!skb2)
184 break;
185
186 hdr2 = mctp_hdr(skb2);
187
188 tag_mask = MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO;
189
190 KUNIT_EXPECT_EQ(test, hdr2->ver, hdr.ver);
191 KUNIT_EXPECT_EQ(test, hdr2->src, hdr.src);
192 KUNIT_EXPECT_EQ(test, hdr2->dest, hdr.dest);
193 KUNIT_EXPECT_EQ(test, hdr2->flags_seq_tag & tag_mask,
194 hdr.flags_seq_tag & tag_mask);
195
196 KUNIT_EXPECT_EQ(test,
197 !!(hdr2->flags_seq_tag & MCTP_HDR_FLAG_SOM), first);
198 KUNIT_EXPECT_EQ(test,
199 !!(hdr2->flags_seq_tag & MCTP_HDR_FLAG_EOM), last);
200
201 seq2 = (hdr2->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) &
202 MCTP_HDR_SEQ_MASK;
203
204 if (first) {
205 seq = seq2;
206 } else {
207 seq++;
208 KUNIT_EXPECT_EQ(test, seq2, seq & MCTP_HDR_SEQ_MASK);
209 }
210
211 if (!last)
212 KUNIT_EXPECT_EQ(test, skb2->len, mtu);
213 else
214 KUNIT_EXPECT_LE(test, skb2->len, mtu);
215
216 kfree_skb(skb2);
217 }
218
219 mctp_test_route_destroy(test, rt);
220 }
221
222 static const struct mctp_frag_test mctp_frag_tests[] = {
223 {.mtu = 68, .msgsize = 63, .n_frags = 1},
224 {.mtu = 68, .msgsize = 64, .n_frags = 1},
225 {.mtu = 68, .msgsize = 65, .n_frags = 2},
226 {.mtu = 68, .msgsize = 66, .n_frags = 2},
227 {.mtu = 68, .msgsize = 127, .n_frags = 2},
228 {.mtu = 68, .msgsize = 128, .n_frags = 2},
229 {.mtu = 68, .msgsize = 129, .n_frags = 3},
230 {.mtu = 68, .msgsize = 130, .n_frags = 3},
231 };
232
mctp_frag_test_to_desc(const struct mctp_frag_test * t,char * desc)233 static void mctp_frag_test_to_desc(const struct mctp_frag_test *t, char *desc)
234 {
235 sprintf(desc, "mtu %d len %d -> %d frags",
236 t->msgsize, t->mtu, t->n_frags);
237 }
238
239 KUNIT_ARRAY_PARAM(mctp_frag, mctp_frag_tests, mctp_frag_test_to_desc);
240
241 struct mctp_rx_input_test {
242 struct mctp_hdr hdr;
243 bool input;
244 };
245
mctp_test_rx_input(struct kunit * test)246 static void mctp_test_rx_input(struct kunit *test)
247 {
248 const struct mctp_rx_input_test *params;
249 struct mctp_test_route *rt;
250 struct mctp_test_dev *dev;
251 struct sk_buff *skb;
252
253 params = test->param_value;
254
255 dev = mctp_test_create_dev();
256 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
257
258 rt = mctp_test_create_route(&init_net, dev->mdev, 8, 68);
259 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rt);
260
261 skb = mctp_test_create_skb(¶ms->hdr, 1);
262 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
263
264 mctp_pkttype_receive(skb, dev->ndev, &mctp_packet_type, NULL);
265
266 KUNIT_EXPECT_EQ(test, !!rt->pkts.qlen, params->input);
267
268 mctp_test_route_destroy(test, rt);
269 mctp_test_destroy_dev(dev);
270 }
271
272 #define RX_HDR(_ver, _src, _dest, _fst) \
273 { .ver = _ver, .src = _src, .dest = _dest, .flags_seq_tag = _fst }
274
275 /* we have a route for EID 8 only */
276 static const struct mctp_rx_input_test mctp_rx_input_tests[] = {
277 { .hdr = RX_HDR(1, 10, 8, 0), .input = true },
278 { .hdr = RX_HDR(1, 10, 9, 0), .input = false }, /* no input route */
279 { .hdr = RX_HDR(2, 10, 8, 0), .input = false }, /* invalid version */
280 };
281
mctp_rx_input_test_to_desc(const struct mctp_rx_input_test * t,char * desc)282 static void mctp_rx_input_test_to_desc(const struct mctp_rx_input_test *t,
283 char *desc)
284 {
285 sprintf(desc, "{%x,%x,%x,%x}", t->hdr.ver, t->hdr.src, t->hdr.dest,
286 t->hdr.flags_seq_tag);
287 }
288
289 KUNIT_ARRAY_PARAM(mctp_rx_input, mctp_rx_input_tests,
290 mctp_rx_input_test_to_desc);
291
292 /* set up a local dev, route on EID 8, and a socket listening on type 0 */
__mctp_route_test_init(struct kunit * test,struct mctp_test_dev ** devp,struct mctp_test_route ** rtp,struct socket ** sockp,unsigned int netid)293 static void __mctp_route_test_init(struct kunit *test,
294 struct mctp_test_dev **devp,
295 struct mctp_test_route **rtp,
296 struct socket **sockp,
297 unsigned int netid)
298 {
299 struct sockaddr_mctp addr = {0};
300 struct mctp_test_route *rt;
301 struct mctp_test_dev *dev;
302 struct socket *sock;
303 int rc;
304
305 dev = mctp_test_create_dev();
306 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
307 if (netid != MCTP_NET_ANY)
308 WRITE_ONCE(dev->mdev->net, netid);
309
310 rt = mctp_test_create_route(&init_net, dev->mdev, 8, 68);
311 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rt);
312
313 rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, &sock);
314 KUNIT_ASSERT_EQ(test, rc, 0);
315
316 addr.smctp_family = AF_MCTP;
317 addr.smctp_network = netid;
318 addr.smctp_addr.s_addr = 8;
319 addr.smctp_type = 0;
320 rc = kernel_bind(sock, (struct sockaddr *)&addr, sizeof(addr));
321 KUNIT_ASSERT_EQ(test, rc, 0);
322
323 *rtp = rt;
324 *devp = dev;
325 *sockp = sock;
326 }
327
__mctp_route_test_fini(struct kunit * test,struct mctp_test_dev * dev,struct mctp_test_route * rt,struct socket * sock)328 static void __mctp_route_test_fini(struct kunit *test,
329 struct mctp_test_dev *dev,
330 struct mctp_test_route *rt,
331 struct socket *sock)
332 {
333 sock_release(sock);
334 mctp_test_route_destroy(test, rt);
335 mctp_test_destroy_dev(dev);
336 }
337
338 struct mctp_route_input_sk_test {
339 struct mctp_hdr hdr;
340 u8 type;
341 bool deliver;
342 };
343
mctp_test_route_input_sk(struct kunit * test)344 static void mctp_test_route_input_sk(struct kunit *test)
345 {
346 const struct mctp_route_input_sk_test *params;
347 struct sk_buff *skb, *skb2;
348 struct mctp_test_route *rt;
349 struct mctp_test_dev *dev;
350 struct socket *sock;
351 int rc;
352
353 params = test->param_value;
354
355 __mctp_route_test_init(test, &dev, &rt, &sock, MCTP_NET_ANY);
356
357 skb = mctp_test_create_skb_data(¶ms->hdr, ¶ms->type);
358 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
359
360 mctp_test_skb_set_dev(skb, dev);
361
362 rc = mctp_route_input(&rt->rt, skb);
363
364 if (params->deliver) {
365 KUNIT_EXPECT_EQ(test, rc, 0);
366
367 skb2 = skb_recv_datagram(sock->sk, MSG_DONTWAIT, &rc);
368 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, skb2);
369 KUNIT_EXPECT_EQ(test, skb2->len, 1);
370
371 skb_free_datagram(sock->sk, skb2);
372
373 } else {
374 KUNIT_EXPECT_NE(test, rc, 0);
375 skb2 = skb_recv_datagram(sock->sk, MSG_DONTWAIT, &rc);
376 KUNIT_EXPECT_NULL(test, skb2);
377 }
378
379 __mctp_route_test_fini(test, dev, rt, sock);
380 }
381
382 #define FL_S (MCTP_HDR_FLAG_SOM)
383 #define FL_E (MCTP_HDR_FLAG_EOM)
384 #define FL_TO (MCTP_HDR_FLAG_TO)
385 #define FL_T(t) ((t) & MCTP_HDR_TAG_MASK)
386
387 static const struct mctp_route_input_sk_test mctp_route_input_sk_tests[] = {
388 { .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_TO), .type = 0, .deliver = true },
389 { .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_TO), .type = 1, .deliver = false },
390 { .hdr = RX_HDR(1, 10, 8, FL_S | FL_E), .type = 0, .deliver = false },
391 { .hdr = RX_HDR(1, 10, 8, FL_E | FL_TO), .type = 0, .deliver = false },
392 { .hdr = RX_HDR(1, 10, 8, FL_TO), .type = 0, .deliver = false },
393 { .hdr = RX_HDR(1, 10, 8, 0), .type = 0, .deliver = false },
394 };
395
mctp_route_input_sk_to_desc(const struct mctp_route_input_sk_test * t,char * desc)396 static void mctp_route_input_sk_to_desc(const struct mctp_route_input_sk_test *t,
397 char *desc)
398 {
399 sprintf(desc, "{%x,%x,%x,%x} type %d", t->hdr.ver, t->hdr.src,
400 t->hdr.dest, t->hdr.flags_seq_tag, t->type);
401 }
402
403 KUNIT_ARRAY_PARAM(mctp_route_input_sk, mctp_route_input_sk_tests,
404 mctp_route_input_sk_to_desc);
405
406 struct mctp_route_input_sk_reasm_test {
407 const char *name;
408 struct mctp_hdr hdrs[4];
409 int n_hdrs;
410 int rx_len;
411 };
412
mctp_test_route_input_sk_reasm(struct kunit * test)413 static void mctp_test_route_input_sk_reasm(struct kunit *test)
414 {
415 const struct mctp_route_input_sk_reasm_test *params;
416 struct sk_buff *skb, *skb2;
417 struct mctp_test_route *rt;
418 struct mctp_test_dev *dev;
419 struct socket *sock;
420 int i, rc;
421 u8 c;
422
423 params = test->param_value;
424
425 __mctp_route_test_init(test, &dev, &rt, &sock, MCTP_NET_ANY);
426
427 for (i = 0; i < params->n_hdrs; i++) {
428 c = i;
429 skb = mctp_test_create_skb_data(¶ms->hdrs[i], &c);
430 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
431
432 mctp_test_skb_set_dev(skb, dev);
433
434 rc = mctp_route_input(&rt->rt, skb);
435 }
436
437 skb2 = skb_recv_datagram(sock->sk, MSG_DONTWAIT, &rc);
438
439 if (params->rx_len) {
440 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, skb2);
441 KUNIT_EXPECT_EQ(test, skb2->len, params->rx_len);
442 skb_free_datagram(sock->sk, skb2);
443
444 } else {
445 KUNIT_EXPECT_NULL(test, skb2);
446 }
447
448 __mctp_route_test_fini(test, dev, rt, sock);
449 }
450
451 #define RX_FRAG(f, s) RX_HDR(1, 10, 8, FL_TO | (f) | ((s) << MCTP_HDR_SEQ_SHIFT))
452
453 static const struct mctp_route_input_sk_reasm_test mctp_route_input_sk_reasm_tests[] = {
454 {
455 .name = "single packet",
456 .hdrs = {
457 RX_FRAG(FL_S | FL_E, 0),
458 },
459 .n_hdrs = 1,
460 .rx_len = 1,
461 },
462 {
463 .name = "single packet, offset seq",
464 .hdrs = {
465 RX_FRAG(FL_S | FL_E, 1),
466 },
467 .n_hdrs = 1,
468 .rx_len = 1,
469 },
470 {
471 .name = "start & end packets",
472 .hdrs = {
473 RX_FRAG(FL_S, 0),
474 RX_FRAG(FL_E, 1),
475 },
476 .n_hdrs = 2,
477 .rx_len = 2,
478 },
479 {
480 .name = "start & end packets, offset seq",
481 .hdrs = {
482 RX_FRAG(FL_S, 1),
483 RX_FRAG(FL_E, 2),
484 },
485 .n_hdrs = 2,
486 .rx_len = 2,
487 },
488 {
489 .name = "start & end packets, out of order",
490 .hdrs = {
491 RX_FRAG(FL_E, 1),
492 RX_FRAG(FL_S, 0),
493 },
494 .n_hdrs = 2,
495 .rx_len = 0,
496 },
497 {
498 .name = "start, middle & end packets",
499 .hdrs = {
500 RX_FRAG(FL_S, 0),
501 RX_FRAG(0, 1),
502 RX_FRAG(FL_E, 2),
503 },
504 .n_hdrs = 3,
505 .rx_len = 3,
506 },
507 {
508 .name = "missing seq",
509 .hdrs = {
510 RX_FRAG(FL_S, 0),
511 RX_FRAG(FL_E, 2),
512 },
513 .n_hdrs = 2,
514 .rx_len = 0,
515 },
516 {
517 .name = "seq wrap",
518 .hdrs = {
519 RX_FRAG(FL_S, 3),
520 RX_FRAG(FL_E, 0),
521 },
522 .n_hdrs = 2,
523 .rx_len = 2,
524 },
525 };
526
mctp_route_input_sk_reasm_to_desc(const struct mctp_route_input_sk_reasm_test * t,char * desc)527 static void mctp_route_input_sk_reasm_to_desc(
528 const struct mctp_route_input_sk_reasm_test *t,
529 char *desc)
530 {
531 sprintf(desc, "%s", t->name);
532 }
533
534 KUNIT_ARRAY_PARAM(mctp_route_input_sk_reasm, mctp_route_input_sk_reasm_tests,
535 mctp_route_input_sk_reasm_to_desc);
536
537 struct mctp_route_input_sk_keys_test {
538 const char *name;
539 mctp_eid_t key_peer_addr;
540 mctp_eid_t key_local_addr;
541 u8 key_tag;
542 struct mctp_hdr hdr;
543 bool deliver;
544 };
545
546 /* test packet rx in the presence of various key configurations */
mctp_test_route_input_sk_keys(struct kunit * test)547 static void mctp_test_route_input_sk_keys(struct kunit *test)
548 {
549 const struct mctp_route_input_sk_keys_test *params;
550 struct mctp_test_route *rt;
551 struct sk_buff *skb, *skb2;
552 struct mctp_test_dev *dev;
553 struct mctp_sk_key *key;
554 struct netns_mctp *mns;
555 struct mctp_sock *msk;
556 struct socket *sock;
557 unsigned long flags;
558 unsigned int net;
559 int rc;
560 u8 c;
561
562 params = test->param_value;
563
564 dev = mctp_test_create_dev();
565 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
566 net = READ_ONCE(dev->mdev->net);
567
568 rt = mctp_test_create_route(&init_net, dev->mdev, 8, 68);
569 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rt);
570
571 rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, &sock);
572 KUNIT_ASSERT_EQ(test, rc, 0);
573
574 msk = container_of(sock->sk, struct mctp_sock, sk);
575 mns = &sock_net(sock->sk)->mctp;
576
577 /* set the incoming tag according to test params */
578 key = mctp_key_alloc(msk, net, params->key_local_addr,
579 params->key_peer_addr, params->key_tag,
580 GFP_KERNEL);
581
582 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, key);
583
584 spin_lock_irqsave(&mns->keys_lock, flags);
585 mctp_reserve_tag(&init_net, key, msk);
586 spin_unlock_irqrestore(&mns->keys_lock, flags);
587
588 /* create packet and route */
589 c = 0;
590 skb = mctp_test_create_skb_data(¶ms->hdr, &c);
591 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
592
593 mctp_test_skb_set_dev(skb, dev);
594
595 rc = mctp_route_input(&rt->rt, skb);
596
597 /* (potentially) receive message */
598 skb2 = skb_recv_datagram(sock->sk, MSG_DONTWAIT, &rc);
599
600 if (params->deliver)
601 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, skb2);
602 else
603 KUNIT_EXPECT_PTR_EQ(test, skb2, NULL);
604
605 if (skb2)
606 skb_free_datagram(sock->sk, skb2);
607
608 mctp_key_unref(key);
609 __mctp_route_test_fini(test, dev, rt, sock);
610 }
611
612 static const struct mctp_route_input_sk_keys_test mctp_route_input_sk_keys_tests[] = {
613 {
614 .name = "direct match",
615 .key_peer_addr = 9,
616 .key_local_addr = 8,
617 .key_tag = 1,
618 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
619 .deliver = true,
620 },
621 {
622 .name = "flipped src/dest",
623 .key_peer_addr = 8,
624 .key_local_addr = 9,
625 .key_tag = 1,
626 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
627 .deliver = false,
628 },
629 {
630 .name = "peer addr mismatch",
631 .key_peer_addr = 9,
632 .key_local_addr = 8,
633 .key_tag = 1,
634 .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_T(1)),
635 .deliver = false,
636 },
637 {
638 .name = "tag value mismatch",
639 .key_peer_addr = 9,
640 .key_local_addr = 8,
641 .key_tag = 1,
642 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(2)),
643 .deliver = false,
644 },
645 {
646 .name = "TO mismatch",
647 .key_peer_addr = 9,
648 .key_local_addr = 8,
649 .key_tag = 1,
650 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1) | FL_TO),
651 .deliver = false,
652 },
653 {
654 .name = "broadcast response",
655 .key_peer_addr = MCTP_ADDR_ANY,
656 .key_local_addr = 8,
657 .key_tag = 1,
658 .hdr = RX_HDR(1, 11, 8, FL_S | FL_E | FL_T(1)),
659 .deliver = true,
660 },
661 {
662 .name = "any local match",
663 .key_peer_addr = 12,
664 .key_local_addr = MCTP_ADDR_ANY,
665 .key_tag = 1,
666 .hdr = RX_HDR(1, 12, 8, FL_S | FL_E | FL_T(1)),
667 .deliver = true,
668 },
669 };
670
mctp_route_input_sk_keys_to_desc(const struct mctp_route_input_sk_keys_test * t,char * desc)671 static void mctp_route_input_sk_keys_to_desc(
672 const struct mctp_route_input_sk_keys_test *t,
673 char *desc)
674 {
675 sprintf(desc, "%s", t->name);
676 }
677
678 KUNIT_ARRAY_PARAM(mctp_route_input_sk_keys, mctp_route_input_sk_keys_tests,
679 mctp_route_input_sk_keys_to_desc);
680
681 struct test_net {
682 unsigned int netid;
683 struct mctp_test_dev *dev;
684 struct mctp_test_route *rt;
685 struct socket *sock;
686 struct sk_buff *skb;
687 struct mctp_sk_key *key;
688 struct {
689 u8 type;
690 unsigned int data;
691 } msg;
692 };
693
694 static void
mctp_test_route_input_multiple_nets_bind_init(struct kunit * test,struct test_net * t)695 mctp_test_route_input_multiple_nets_bind_init(struct kunit *test,
696 struct test_net *t)
697 {
698 struct mctp_hdr hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1) | FL_TO);
699
700 t->msg.data = t->netid;
701
702 __mctp_route_test_init(test, &t->dev, &t->rt, &t->sock, t->netid);
703
704 t->skb = mctp_test_create_skb_data(&hdr, &t->msg);
705 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t->skb);
706 mctp_test_skb_set_dev(t->skb, t->dev);
707 }
708
709 static void
mctp_test_route_input_multiple_nets_bind_fini(struct kunit * test,struct test_net * t)710 mctp_test_route_input_multiple_nets_bind_fini(struct kunit *test,
711 struct test_net *t)
712 {
713 __mctp_route_test_fini(test, t->dev, t->rt, t->sock);
714 }
715
716 /* Test that skbs from different nets (otherwise identical) get routed to their
717 * corresponding socket via the sockets' bind()
718 */
mctp_test_route_input_multiple_nets_bind(struct kunit * test)719 static void mctp_test_route_input_multiple_nets_bind(struct kunit *test)
720 {
721 struct sk_buff *rx_skb1, *rx_skb2;
722 struct test_net t1, t2;
723 int rc;
724
725 t1.netid = 1;
726 t2.netid = 2;
727
728 t1.msg.type = 0;
729 t2.msg.type = 0;
730
731 mctp_test_route_input_multiple_nets_bind_init(test, &t1);
732 mctp_test_route_input_multiple_nets_bind_init(test, &t2);
733
734 rc = mctp_route_input(&t1.rt->rt, t1.skb);
735 KUNIT_ASSERT_EQ(test, rc, 0);
736 rc = mctp_route_input(&t2.rt->rt, t2.skb);
737 KUNIT_ASSERT_EQ(test, rc, 0);
738
739 rx_skb1 = skb_recv_datagram(t1.sock->sk, MSG_DONTWAIT, &rc);
740 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, rx_skb1);
741 KUNIT_EXPECT_EQ(test, rx_skb1->len, sizeof(t1.msg));
742 KUNIT_EXPECT_EQ(test,
743 *(unsigned int *)skb_pull(rx_skb1, sizeof(t1.msg.data)),
744 t1.netid);
745 kfree_skb(rx_skb1);
746
747 rx_skb2 = skb_recv_datagram(t2.sock->sk, MSG_DONTWAIT, &rc);
748 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, rx_skb2);
749 KUNIT_EXPECT_EQ(test, rx_skb2->len, sizeof(t2.msg));
750 KUNIT_EXPECT_EQ(test,
751 *(unsigned int *)skb_pull(rx_skb2, sizeof(t2.msg.data)),
752 t2.netid);
753 kfree_skb(rx_skb2);
754
755 mctp_test_route_input_multiple_nets_bind_fini(test, &t1);
756 mctp_test_route_input_multiple_nets_bind_fini(test, &t2);
757 }
758
759 static void
mctp_test_route_input_multiple_nets_key_init(struct kunit * test,struct test_net * t)760 mctp_test_route_input_multiple_nets_key_init(struct kunit *test,
761 struct test_net *t)
762 {
763 struct mctp_hdr hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1));
764 struct mctp_sock *msk;
765 struct netns_mctp *mns;
766 unsigned long flags;
767
768 t->msg.data = t->netid;
769
770 __mctp_route_test_init(test, &t->dev, &t->rt, &t->sock, t->netid);
771
772 msk = container_of(t->sock->sk, struct mctp_sock, sk);
773
774 t->key = mctp_key_alloc(msk, t->netid, hdr.dest, hdr.src, 1, GFP_KERNEL);
775 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t->key);
776
777 mns = &sock_net(t->sock->sk)->mctp;
778 spin_lock_irqsave(&mns->keys_lock, flags);
779 mctp_reserve_tag(&init_net, t->key, msk);
780 spin_unlock_irqrestore(&mns->keys_lock, flags);
781
782 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t->key);
783 t->skb = mctp_test_create_skb_data(&hdr, &t->msg);
784 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t->skb);
785 mctp_test_skb_set_dev(t->skb, t->dev);
786 }
787
788 static void
mctp_test_route_input_multiple_nets_key_fini(struct kunit * test,struct test_net * t)789 mctp_test_route_input_multiple_nets_key_fini(struct kunit *test,
790 struct test_net *t)
791 {
792 mctp_key_unref(t->key);
793 __mctp_route_test_fini(test, t->dev, t->rt, t->sock);
794 }
795
796 /* test that skbs from different nets (otherwise identical) get routed to their
797 * corresponding socket via the sk_key
798 */
mctp_test_route_input_multiple_nets_key(struct kunit * test)799 static void mctp_test_route_input_multiple_nets_key(struct kunit *test)
800 {
801 struct sk_buff *rx_skb1, *rx_skb2;
802 struct test_net t1, t2;
803 int rc;
804
805 t1.netid = 1;
806 t2.netid = 2;
807
808 /* use type 1 which is not bound */
809 t1.msg.type = 1;
810 t2.msg.type = 1;
811
812 mctp_test_route_input_multiple_nets_key_init(test, &t1);
813 mctp_test_route_input_multiple_nets_key_init(test, &t2);
814
815 rc = mctp_route_input(&t1.rt->rt, t1.skb);
816 KUNIT_ASSERT_EQ(test, rc, 0);
817 rc = mctp_route_input(&t2.rt->rt, t2.skb);
818 KUNIT_ASSERT_EQ(test, rc, 0);
819
820 rx_skb1 = skb_recv_datagram(t1.sock->sk, MSG_DONTWAIT, &rc);
821 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, rx_skb1);
822 KUNIT_EXPECT_EQ(test, rx_skb1->len, sizeof(t1.msg));
823 KUNIT_EXPECT_EQ(test,
824 *(unsigned int *)skb_pull(rx_skb1, sizeof(t1.msg.data)),
825 t1.netid);
826 kfree_skb(rx_skb1);
827
828 rx_skb2 = skb_recv_datagram(t2.sock->sk, MSG_DONTWAIT, &rc);
829 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, rx_skb2);
830 KUNIT_EXPECT_EQ(test, rx_skb2->len, sizeof(t2.msg));
831 KUNIT_EXPECT_EQ(test,
832 *(unsigned int *)skb_pull(rx_skb2, sizeof(t2.msg.data)),
833 t2.netid);
834 kfree_skb(rx_skb2);
835
836 mctp_test_route_input_multiple_nets_key_fini(test, &t1);
837 mctp_test_route_input_multiple_nets_key_fini(test, &t2);
838 }
839
840 /* Input route to socket, using a single-packet message, where sock delivery
841 * fails. Ensure we're handling the failure appropriately.
842 */
mctp_test_route_input_sk_fail_single(struct kunit * test)843 static void mctp_test_route_input_sk_fail_single(struct kunit *test)
844 {
845 const struct mctp_hdr hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_TO);
846 struct mctp_test_route *rt;
847 struct mctp_test_dev *dev;
848 struct socket *sock;
849 struct sk_buff *skb;
850 int rc;
851
852 __mctp_route_test_init(test, &dev, &rt, &sock, MCTP_NET_ANY);
853
854 /* No rcvbuf space, so delivery should fail. __sock_set_rcvbuf will
855 * clamp the minimum to SOCK_MIN_RCVBUF, so we open-code this.
856 */
857 lock_sock(sock->sk);
858 WRITE_ONCE(sock->sk->sk_rcvbuf, 0);
859 release_sock(sock->sk);
860
861 skb = mctp_test_create_skb(&hdr, 10);
862 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
863 skb_get(skb);
864
865 mctp_test_skb_set_dev(skb, dev);
866
867 /* do route input, which should fail */
868 rc = mctp_route_input(&rt->rt, skb);
869 KUNIT_EXPECT_NE(test, rc, 0);
870
871 /* we should hold the only reference to skb */
872 KUNIT_EXPECT_EQ(test, refcount_read(&skb->users), 1);
873 kfree_skb(skb);
874
875 __mctp_route_test_fini(test, dev, rt, sock);
876 }
877
878 /* Input route to socket, using a fragmented message, where sock delivery fails.
879 */
mctp_test_route_input_sk_fail_frag(struct kunit * test)880 static void mctp_test_route_input_sk_fail_frag(struct kunit *test)
881 {
882 const struct mctp_hdr hdrs[2] = { RX_FRAG(FL_S, 0), RX_FRAG(FL_E, 1) };
883 struct mctp_test_route *rt;
884 struct mctp_test_dev *dev;
885 struct sk_buff *skbs[2];
886 struct socket *sock;
887 unsigned int i;
888 int rc;
889
890 __mctp_route_test_init(test, &dev, &rt, &sock, MCTP_NET_ANY);
891
892 lock_sock(sock->sk);
893 WRITE_ONCE(sock->sk->sk_rcvbuf, 0);
894 release_sock(sock->sk);
895
896 for (i = 0; i < ARRAY_SIZE(skbs); i++) {
897 skbs[i] = mctp_test_create_skb(&hdrs[i], 10);
898 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skbs[i]);
899 skb_get(skbs[i]);
900
901 mctp_test_skb_set_dev(skbs[i], dev);
902 }
903
904 /* first route input should succeed, we're only queueing to the
905 * frag list
906 */
907 rc = mctp_route_input(&rt->rt, skbs[0]);
908 KUNIT_EXPECT_EQ(test, rc, 0);
909
910 /* final route input should fail to deliver to the socket */
911 rc = mctp_route_input(&rt->rt, skbs[1]);
912 KUNIT_EXPECT_NE(test, rc, 0);
913
914 /* we should hold the only reference to both skbs */
915 KUNIT_EXPECT_EQ(test, refcount_read(&skbs[0]->users), 1);
916 kfree_skb(skbs[0]);
917
918 KUNIT_EXPECT_EQ(test, refcount_read(&skbs[1]->users), 1);
919 kfree_skb(skbs[1]);
920
921 __mctp_route_test_fini(test, dev, rt, sock);
922 }
923
924 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
925
mctp_test_flow_init(struct kunit * test,struct mctp_test_dev ** devp,struct mctp_test_route ** rtp,struct socket ** sock,struct sk_buff ** skbp,unsigned int len)926 static void mctp_test_flow_init(struct kunit *test,
927 struct mctp_test_dev **devp,
928 struct mctp_test_route **rtp,
929 struct socket **sock,
930 struct sk_buff **skbp,
931 unsigned int len)
932 {
933 struct mctp_test_route *rt;
934 struct mctp_test_dev *dev;
935 struct sk_buff *skb;
936
937 /* we have a slightly odd routing setup here; the test route
938 * is for EID 8, which is our local EID. We don't do a routing
939 * lookup, so that's fine - all we require is a path through
940 * mctp_local_output, which will call rt->output on whatever
941 * route we provide
942 */
943 __mctp_route_test_init(test, &dev, &rt, sock, MCTP_NET_ANY);
944
945 /* Assign a single EID. ->addrs is freed on mctp netdev release */
946 dev->mdev->addrs = kmalloc(sizeof(u8), GFP_KERNEL);
947 dev->mdev->num_addrs = 1;
948 dev->mdev->addrs[0] = 8;
949
950 skb = alloc_skb(len + sizeof(struct mctp_hdr) + 1, GFP_KERNEL);
951 KUNIT_ASSERT_TRUE(test, skb);
952 __mctp_cb(skb);
953 skb_reserve(skb, sizeof(struct mctp_hdr) + 1);
954 memset(skb_put(skb, len), 0, len);
955
956 /* take a ref for the route, we'll decrement in local output */
957 refcount_inc(&rt->rt.refs);
958
959 *devp = dev;
960 *rtp = rt;
961 *skbp = skb;
962 }
963
mctp_test_flow_fini(struct kunit * test,struct mctp_test_dev * dev,struct mctp_test_route * rt,struct socket * sock)964 static void mctp_test_flow_fini(struct kunit *test,
965 struct mctp_test_dev *dev,
966 struct mctp_test_route *rt,
967 struct socket *sock)
968 {
969 __mctp_route_test_fini(test, dev, rt, sock);
970 }
971
972 /* test that an outgoing skb has the correct MCTP extension data set */
mctp_test_packet_flow(struct kunit * test)973 static void mctp_test_packet_flow(struct kunit *test)
974 {
975 struct sk_buff *skb, *skb2;
976 struct mctp_test_route *rt;
977 struct mctp_test_dev *dev;
978 struct mctp_flow *flow;
979 struct socket *sock;
980 u8 dst = 8;
981 int n, rc;
982
983 mctp_test_flow_init(test, &dev, &rt, &sock, &skb, 30);
984
985 rc = mctp_local_output(sock->sk, &rt->rt, skb, dst, MCTP_TAG_OWNER);
986 KUNIT_ASSERT_EQ(test, rc, 0);
987
988 n = rt->pkts.qlen;
989 KUNIT_ASSERT_EQ(test, n, 1);
990
991 skb2 = skb_dequeue(&rt->pkts);
992 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb2);
993
994 flow = skb_ext_find(skb2, SKB_EXT_MCTP);
995 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, flow);
996 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, flow->key);
997 KUNIT_ASSERT_PTR_EQ(test, flow->key->sk, sock->sk);
998
999 kfree_skb(skb2);
1000 mctp_test_flow_fini(test, dev, rt, sock);
1001 }
1002
1003 /* test that outgoing skbs, after fragmentation, all have the correct MCTP
1004 * extension data set.
1005 */
mctp_test_fragment_flow(struct kunit * test)1006 static void mctp_test_fragment_flow(struct kunit *test)
1007 {
1008 struct mctp_flow *flows[2];
1009 struct sk_buff *tx_skbs[2];
1010 struct mctp_test_route *rt;
1011 struct mctp_test_dev *dev;
1012 struct sk_buff *skb;
1013 struct socket *sock;
1014 u8 dst = 8;
1015 int n, rc;
1016
1017 mctp_test_flow_init(test, &dev, &rt, &sock, &skb, 100);
1018
1019 rc = mctp_local_output(sock->sk, &rt->rt, skb, dst, MCTP_TAG_OWNER);
1020 KUNIT_ASSERT_EQ(test, rc, 0);
1021
1022 n = rt->pkts.qlen;
1023 KUNIT_ASSERT_EQ(test, n, 2);
1024
1025 /* both resulting packets should have the same flow data */
1026 tx_skbs[0] = skb_dequeue(&rt->pkts);
1027 tx_skbs[1] = skb_dequeue(&rt->pkts);
1028
1029 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tx_skbs[0]);
1030 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tx_skbs[1]);
1031
1032 flows[0] = skb_ext_find(tx_skbs[0], SKB_EXT_MCTP);
1033 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, flows[0]);
1034 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, flows[0]->key);
1035 KUNIT_ASSERT_PTR_EQ(test, flows[0]->key->sk, sock->sk);
1036
1037 flows[1] = skb_ext_find(tx_skbs[1], SKB_EXT_MCTP);
1038 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, flows[1]);
1039 KUNIT_ASSERT_PTR_EQ(test, flows[1]->key, flows[0]->key);
1040
1041 kfree_skb(tx_skbs[0]);
1042 kfree_skb(tx_skbs[1]);
1043 mctp_test_flow_fini(test, dev, rt, sock);
1044 }
1045
1046 #else
mctp_test_packet_flow(struct kunit * test)1047 static void mctp_test_packet_flow(struct kunit *test)
1048 {
1049 kunit_skip(test, "Requires CONFIG_MCTP_FLOWS=y");
1050 }
1051
mctp_test_fragment_flow(struct kunit * test)1052 static void mctp_test_fragment_flow(struct kunit *test)
1053 {
1054 kunit_skip(test, "Requires CONFIG_MCTP_FLOWS=y");
1055 }
1056 #endif
1057
1058 /* Test that outgoing skbs cause a suitable tag to be created */
mctp_test_route_output_key_create(struct kunit * test)1059 static void mctp_test_route_output_key_create(struct kunit *test)
1060 {
1061 const unsigned int netid = 50;
1062 const u8 dst = 26, src = 15;
1063 struct mctp_test_route *rt;
1064 struct mctp_test_dev *dev;
1065 struct mctp_sk_key *key;
1066 struct netns_mctp *mns;
1067 unsigned long flags;
1068 struct socket *sock;
1069 struct sk_buff *skb;
1070 bool empty, single;
1071 const int len = 2;
1072 int rc;
1073
1074 dev = mctp_test_create_dev();
1075 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
1076 WRITE_ONCE(dev->mdev->net, netid);
1077
1078 rt = mctp_test_create_route(&init_net, dev->mdev, dst, 68);
1079 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rt);
1080
1081 rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, &sock);
1082 KUNIT_ASSERT_EQ(test, rc, 0);
1083
1084 dev->mdev->addrs = kmalloc(sizeof(u8), GFP_KERNEL);
1085 dev->mdev->num_addrs = 1;
1086 dev->mdev->addrs[0] = src;
1087
1088 skb = alloc_skb(sizeof(struct mctp_hdr) + 1 + len, GFP_KERNEL);
1089 KUNIT_ASSERT_TRUE(test, skb);
1090 __mctp_cb(skb);
1091 skb_reserve(skb, sizeof(struct mctp_hdr) + 1 + len);
1092 memset(skb_put(skb, len), 0, len);
1093
1094 refcount_inc(&rt->rt.refs);
1095
1096 mns = &sock_net(sock->sk)->mctp;
1097
1098 /* We assume we're starting from an empty keys list, which requires
1099 * preceding tests to clean up correctly!
1100 */
1101 spin_lock_irqsave(&mns->keys_lock, flags);
1102 empty = hlist_empty(&mns->keys);
1103 spin_unlock_irqrestore(&mns->keys_lock, flags);
1104 KUNIT_ASSERT_TRUE(test, empty);
1105
1106 rc = mctp_local_output(sock->sk, &rt->rt, skb, dst, MCTP_TAG_OWNER);
1107 KUNIT_ASSERT_EQ(test, rc, 0);
1108
1109 key = NULL;
1110 single = false;
1111 spin_lock_irqsave(&mns->keys_lock, flags);
1112 if (!hlist_empty(&mns->keys)) {
1113 key = hlist_entry(mns->keys.first, struct mctp_sk_key, hlist);
1114 single = hlist_is_singular_node(&key->hlist, &mns->keys);
1115 }
1116 spin_unlock_irqrestore(&mns->keys_lock, flags);
1117
1118 KUNIT_ASSERT_NOT_NULL(test, key);
1119 KUNIT_ASSERT_TRUE(test, single);
1120
1121 KUNIT_EXPECT_EQ(test, key->net, netid);
1122 KUNIT_EXPECT_EQ(test, key->local_addr, src);
1123 KUNIT_EXPECT_EQ(test, key->peer_addr, dst);
1124 /* key has incoming tag, so inverse of what we sent */
1125 KUNIT_EXPECT_FALSE(test, key->tag & MCTP_TAG_OWNER);
1126
1127 sock_release(sock);
1128 mctp_test_route_destroy(test, rt);
1129 mctp_test_destroy_dev(dev);
1130 }
1131
1132 static struct kunit_case mctp_test_cases[] = {
1133 KUNIT_CASE_PARAM(mctp_test_fragment, mctp_frag_gen_params),
1134 KUNIT_CASE_PARAM(mctp_test_rx_input, mctp_rx_input_gen_params),
1135 KUNIT_CASE_PARAM(mctp_test_route_input_sk, mctp_route_input_sk_gen_params),
1136 KUNIT_CASE_PARAM(mctp_test_route_input_sk_reasm,
1137 mctp_route_input_sk_reasm_gen_params),
1138 KUNIT_CASE_PARAM(mctp_test_route_input_sk_keys,
1139 mctp_route_input_sk_keys_gen_params),
1140 KUNIT_CASE(mctp_test_route_input_sk_fail_single),
1141 KUNIT_CASE(mctp_test_route_input_sk_fail_frag),
1142 KUNIT_CASE(mctp_test_route_input_multiple_nets_bind),
1143 KUNIT_CASE(mctp_test_route_input_multiple_nets_key),
1144 KUNIT_CASE(mctp_test_packet_flow),
1145 KUNIT_CASE(mctp_test_fragment_flow),
1146 KUNIT_CASE(mctp_test_route_output_key_create),
1147 {}
1148 };
1149
1150 static struct kunit_suite mctp_test_suite = {
1151 .name = "mctp",
1152 .test_cases = mctp_test_cases,
1153 };
1154
1155 kunit_test_suite(mctp_test_suite);
1156