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