Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: mctp: allow NL parsing directly into a struct mctp_route

The netlink route parsing functions end up setting a bunch of output
variables from the rt attributes. This will get messy when the routes
become more complex.

So, split the rt parsing into two types: a lookup (returning route
target data suitable for a route lookup, like when deleting a route) and
a populate (setting fields of a struct mctp_route).

In doing this, we need to separate the route allocation from
mctp_route_add, so add some comments on the lifetime semantics for the
latter.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Link: https://patch.msgid.link/20250702-dev-forwarding-v5-12-1468191da8a4@codeconstruct.com.au
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Jeremy Kerr and committed by
Paolo Abeni
28ddbb2a 4a1de053

+147 -69
+147 -69
net/mctp/route.c
··· 1091 1091 } 1092 1092 1093 1093 /* route management */ 1094 - static int mctp_route_add(struct net *net, struct mctp_dev *mdev, 1095 - mctp_eid_t daddr_start, unsigned int daddr_extent, 1096 - unsigned int mtu, unsigned char type) 1094 + 1095 + /* mctp_route_add(): Add the provided route, previously allocated via 1096 + * mctp_route_alloc(). On success, takes ownership of @rt, which includes a 1097 + * hold on rt->dev for usage in the route table. On failure a caller will want 1098 + * to mctp_route_release(). 1099 + * 1100 + * We expect that the caller has set rt->type, rt->min, rt->max, rt->dev and 1101 + * rt->mtu, and that the route holds a reference to rt->dev (via mctp_dev_hold). 1102 + * Other fields will be populated. 1103 + */ 1104 + static int mctp_route_add(struct net *net, struct mctp_route *rt) 1097 1105 { 1098 - int (*rtfn)(struct mctp_dst *dst, struct sk_buff *skb); 1099 - struct mctp_route *rt, *ert; 1106 + struct mctp_route *ert; 1100 1107 1101 - if (!mctp_address_unicast(daddr_start)) 1108 + if (!mctp_address_unicast(rt->min) || !mctp_address_unicast(rt->max)) 1102 1109 return -EINVAL; 1103 1110 1104 - if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255) 1111 + if (!rt->dev) 1105 1112 return -EINVAL; 1106 1113 1107 - switch (type) { 1114 + switch (rt->type) { 1108 1115 case RTN_LOCAL: 1109 - rtfn = mctp_dst_input; 1116 + rt->output = mctp_dst_input; 1110 1117 break; 1111 1118 case RTN_UNICAST: 1112 - rtfn = mctp_dst_output; 1119 + rt->output = mctp_dst_output; 1113 1120 break; 1114 1121 default: 1115 1122 return -EINVAL; ··· 1124 1117 1125 1118 ASSERT_RTNL(); 1126 1119 1127 - rt = mctp_route_alloc(); 1128 - if (!rt) 1129 - return -ENOMEM; 1130 - 1131 - rt->min = daddr_start; 1132 - rt->max = daddr_start + daddr_extent; 1133 - rt->mtu = mtu; 1134 - rt->dev = mdev; 1135 - mctp_dev_hold(rt->dev); 1136 - rt->type = type; 1137 - rt->output = rtfn; 1138 - 1139 1120 /* Prevent duplicate identical routes. */ 1140 1121 list_for_each_entry(ert, &net->mctp.routes, list) { 1141 1122 if (mctp_rt_compare_exact(rt, ert)) { 1142 - mctp_route_release(rt); 1143 1123 return -EEXIST; 1144 1124 } 1145 1125 } ··· 1168 1174 1169 1175 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr) 1170 1176 { 1171 - return mctp_route_add(dev_net(mdev->dev), mdev, addr, 0, 0, RTN_LOCAL); 1177 + struct mctp_route *rt; 1178 + int rc; 1179 + 1180 + rt = mctp_route_alloc(); 1181 + if (!rt) 1182 + return -ENOMEM; 1183 + 1184 + rt->min = addr; 1185 + rt->max = addr; 1186 + rt->dev = mdev; 1187 + rt->type = RTN_LOCAL; 1188 + 1189 + mctp_dev_hold(rt->dev); 1190 + 1191 + rc = mctp_route_add(dev_net(mdev->dev), rt); 1192 + if (rc) 1193 + mctp_route_release(rt); 1194 + 1195 + return rc; 1172 1196 } 1173 1197 1174 1198 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr) ··· 1298 1286 [RTA_OIF] = { .type = NLA_U32 }, 1299 1287 }; 1300 1288 1301 - /* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing. 1302 - * tb must hold RTA_MAX+1 elements. 1303 - */ 1304 - static int mctp_route_nlparse(struct net *net, struct nlmsghdr *nlh, 1305 - struct netlink_ext_ack *extack, 1306 - struct nlattr **tb, struct rtmsg **rtm, 1307 - struct mctp_dev **mdev, mctp_eid_t *daddr_start) 1289 + static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = { 1290 + [RTAX_MTU] = { .type = NLA_U32 }, 1291 + }; 1292 + 1293 + /* base parsing; common to both _lookup and _populate variants */ 1294 + static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh, 1295 + struct netlink_ext_ack *extack, 1296 + struct nlattr **tb, struct rtmsg **rtm, 1297 + struct mctp_dev **mdev, 1298 + mctp_eid_t *daddr_start) 1308 1299 { 1309 1300 struct net_device *dev; 1310 1301 unsigned int ifindex; ··· 1338 1323 return -EINVAL; 1339 1324 } 1340 1325 1326 + if ((*rtm)->rtm_type != RTN_UNICAST) { 1327 + NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST"); 1328 + return -EINVAL; 1329 + } 1330 + 1341 1331 dev = __dev_get_by_index(net, ifindex); 1342 1332 if (!dev) { 1343 1333 NL_SET_ERR_MSG(extack, "bad ifindex"); 1344 1334 return -ENODEV; 1345 1335 } 1336 + 1346 1337 *mdev = mctp_dev_get_rtnl(dev); 1347 1338 if (!*mdev) 1348 1339 return -ENODEV; 1349 1340 1350 - if (dev->flags & IFF_LOOPBACK) { 1351 - NL_SET_ERR_MSG(extack, "no routes to loopback"); 1352 - return -EINVAL; 1353 - } 1341 + return 0; 1342 + } 1343 + 1344 + /* Route parsing for lookup operations; we only need the "route target" 1345 + * components (ie., network and dest-EID range). 1346 + */ 1347 + static int mctp_route_nlparse_lookup(struct net *net, struct nlmsghdr *nlh, 1348 + struct netlink_ext_ack *extack, 1349 + unsigned char *type, unsigned int *netid, 1350 + mctp_eid_t *daddr_start, 1351 + unsigned int *daddr_extent) 1352 + { 1353 + struct nlattr *tb[RTA_MAX + 1]; 1354 + struct mctp_dev *mdev; 1355 + struct rtmsg *rtm; 1356 + int rc; 1357 + 1358 + rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, 1359 + &mdev, daddr_start); 1360 + if (rc) 1361 + return rc; 1362 + 1363 + *netid = mdev->net; 1364 + *type = rtm->rtm_type; 1365 + *daddr_extent = rtm->rtm_dst_len; 1354 1366 1355 1367 return 0; 1356 1368 } 1357 1369 1358 - static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = { 1359 - [RTAX_MTU] = { .type = NLA_U32 }, 1360 - }; 1370 + /* Full route parse for RTM_NEWROUTE: populate @rt. On success, the route will 1371 + * hold a reference to the dev. 1372 + */ 1373 + static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, 1374 + struct netlink_ext_ack *extack, 1375 + struct mctp_route *rt) 1376 + { 1377 + struct nlattr *tbx[RTAX_MAX + 1]; 1378 + struct nlattr *tb[RTA_MAX + 1]; 1379 + unsigned int daddr_extent; 1380 + mctp_eid_t daddr_start; 1381 + struct mctp_dev *dev; 1382 + struct rtmsg *rtm; 1383 + u32 mtu = 0; 1384 + int rc; 1385 + 1386 + rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, 1387 + &dev, &daddr_start); 1388 + if (rc) 1389 + return rc; 1390 + 1391 + daddr_extent = rtm->rtm_dst_len; 1392 + 1393 + if (daddr_extent > 0xff || daddr_extent + daddr_start >= 255) { 1394 + NL_SET_ERR_MSG(extack, "invalid eid range"); 1395 + return -EINVAL; 1396 + } 1397 + 1398 + if (tb[RTA_METRICS]) { 1399 + rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS], 1400 + rta_metrics_policy, NULL); 1401 + if (rc < 0) { 1402 + NL_SET_ERR_MSG(extack, "incorrect RTA_METRICS format"); 1403 + return rc; 1404 + } 1405 + if (tbx[RTAX_MTU]) 1406 + mtu = nla_get_u32(tbx[RTAX_MTU]); 1407 + } 1408 + 1409 + rt->type = rtm->rtm_type; 1410 + rt->min = daddr_start; 1411 + rt->max = daddr_start + daddr_extent; 1412 + rt->mtu = mtu; 1413 + rt->dev = dev; 1414 + mctp_dev_hold(rt->dev); 1415 + 1416 + return 0; 1417 + } 1361 1418 1362 1419 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 1363 1420 struct netlink_ext_ack *extack) 1364 1421 { 1365 1422 struct net *net = sock_net(skb->sk); 1366 - struct nlattr *tb[RTA_MAX + 1]; 1367 - struct nlattr *tbx[RTAX_MAX + 1]; 1368 - mctp_eid_t daddr_start; 1369 - struct mctp_dev *mdev; 1370 - struct rtmsg *rtm; 1371 - unsigned int mtu; 1423 + struct mctp_route *rt; 1372 1424 int rc; 1373 1425 1374 - rc = mctp_route_nlparse(net, nlh, extack, tb, 1375 - &rtm, &mdev, &daddr_start); 1426 + rt = mctp_route_alloc(); 1427 + if (!rt) 1428 + return -ENOMEM; 1429 + 1430 + rc = mctp_route_nlparse_populate(net, nlh, extack, rt); 1376 1431 if (rc < 0) 1377 - return rc; 1432 + goto err_free; 1378 1433 1379 - if (rtm->rtm_type != RTN_UNICAST) { 1380 - NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST"); 1381 - return -EINVAL; 1434 + if (rt->dev->dev->flags & IFF_LOOPBACK) { 1435 + NL_SET_ERR_MSG(extack, "no routes to loopback"); 1436 + rc = -EINVAL; 1437 + goto err_free; 1382 1438 } 1383 1439 1384 - mtu = 0; 1385 - if (tb[RTA_METRICS]) { 1386 - rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS], 1387 - rta_metrics_policy, NULL); 1388 - if (rc < 0) 1389 - return rc; 1390 - if (tbx[RTAX_MTU]) 1391 - mtu = nla_get_u32(tbx[RTAX_MTU]); 1392 - } 1440 + rc = mctp_route_add(net, rt); 1441 + if (!rc) 1442 + return 0; 1393 1443 1394 - rc = mctp_route_add(net, mdev, daddr_start, rtm->rtm_dst_len, mtu, 1395 - rtm->rtm_type); 1444 + err_free: 1445 + mctp_route_release(rt); 1396 1446 return rc; 1397 1447 } 1398 1448 ··· 1465 1385 struct netlink_ext_ack *extack) 1466 1386 { 1467 1387 struct net *net = sock_net(skb->sk); 1468 - struct nlattr *tb[RTA_MAX + 1]; 1388 + unsigned int netid, daddr_extent; 1389 + unsigned char type = RTN_UNSPEC; 1469 1390 mctp_eid_t daddr_start; 1470 - struct mctp_dev *mdev; 1471 - struct rtmsg *rtm; 1472 1391 int rc; 1473 1392 1474 - rc = mctp_route_nlparse(net, nlh, extack, tb, 1475 - &rtm, &mdev, &daddr_start); 1393 + rc = mctp_route_nlparse_lookup(net, nlh, extack, &type, &netid, 1394 + &daddr_start, &daddr_extent); 1476 1395 if (rc < 0) 1477 1396 return rc; 1478 1397 1479 1398 /* we only have unicast routes */ 1480 - if (rtm->rtm_type != RTN_UNICAST) 1399 + if (type != RTN_UNICAST) 1481 1400 return -EINVAL; 1482 1401 1483 - rc = mctp_route_remove(net, mdev->net, daddr_start, rtm->rtm_dst_len, 1484 - RTN_UNICAST); 1402 + rc = mctp_route_remove(net, netid, daddr_start, daddr_extent, type); 1485 1403 return rc; 1486 1404 } 1487 1405