History

net: mctp: add gateway routing support

This change allows for gateway routing, where a route table entry may reference a routable endpoint (by network and EID), instead of routing directly to a netdevice. We add support for a RTM_GATEWAY attribute for netlink route updates, with an attribute format of: struct mctp_fq_addr { unsigned int net; mctp_eid_t eid; } - we need the net here to uniquely identify the target EID, as we no longer have the device reference directly (which would provide the net id in the case of direct routes). This makes route lookups recursive, as a route lookup that returns a gateway route must be resolved into a direct route (ie, to a device) eventually. We provide a limit to the route lookups, to prevent infinite loop routing. The route lookup populates a new 'nexthop' field in the dst structure, which now specifies the key for the neighbour table lookup on device output, rather than using the packet destination address directly.

Jeremy Kerr committed 1 year ago

Commit ad39c121 parent 28ddbb24 files · +173 −57Browse files

Unified · Split

4 files
  1. include/net/mctp.h +12 −1
  2. include/uapi/linux/mctp.h +8 −0
  3. net/mctp/route.c +151 −55
  4. net/mctp/test/utils.c +2 −1
include/net/mctp.h+12−1
@@ -237,8 +237,18 @@ struct mctp_route { · 236 unmodified lines
237237 mctp_eid_t min, max;
238238
239239 unsigned char type;
240+
240241 unsigned int mtu;
241− struct mctp_dev *dev;
242+
243+ enum {
244+ MCTP_ROUTE_DIRECT,
245+ MCTP_ROUTE_GATEWAY,
246+ } dst_type;
247+ union {
248+ struct mctp_dev *dev;
249+ struct mctp_fq_addr gateway;
250+ };
251+
242252 int (*output)(struct mctp_dst *dst,
243253 struct sk_buff *skb);
244254
@@ -256,6 +266,7 @@ struct mctp_route { · 11 unmodified lines
256266 struct mctp_dst {
257267 struct mctp_dev *dev;
258268 unsigned int mtu;
269+ mctp_eid_t nexthop;
259270
260271 /* set for direct addressing */
261272 unsigned char halen;
include/uapi/linux/mctp.h+8−0
@@ -37,6 +37,14 @@ struct sockaddr_mctp_ext { · 36 unmodified lines
3737 __u8 smctp_haddr[MAX_ADDR_LEN];
3838 };
3939
40+/* A "fully qualified" MCTP address, which includes the system-local network ID,
41+ * required to uniquely resolve a routable EID.
42+ */
43+struct mctp_fq_addr {
44+ unsigned int net;
45+ mctp_eid_t eid;
46+};
47+
4048 #define MCTP_NET_ANY 0x0
4149
4250 #define MCTP_ADDR_NULL 0x00
net/mctp/route.c+151−55
@@ -563,7 +563,6 @@ out: · 562 unmodified lines
563563
564564 static int mctp_dst_output(struct mctp_dst *dst, struct sk_buff *skb)
565565 {
566− struct mctp_hdr *hdr = mctp_hdr(skb);
567566 char daddr_buf[MAX_ADDR_LEN];
568567 char *daddr = NULL;
569568 int rc;
@@ -586,7 +585,7 @@ static int mctp_dst_output(struct mctp_dst *dst, struct sk_buff *skb) · 16 unmodified lines
586585 daddr = dst->haddr;
587586 } else {
588587 /* If lookup fails let the device handle daddr==NULL */
589− if (mctp_neigh_lookup(dst->dev, hdr->dest, daddr_buf) == 0)
588+ if (mctp_neigh_lookup(dst->dev, dst->nexthop, daddr_buf) == 0)
590589 daddr = daddr_buf;
591590 }
592591
@@ -610,7 +609,8 @@ static int mctp_dst_output(struct mctp_dst *dst, struct sk_buff *skb) · 17 unmodified lines
610609 static void mctp_route_release(struct mctp_route *rt)
611610 {
612611 if (refcount_dec_and_test(&rt->refs)) {
613− mctp_dev_put(rt->dev);
612+ if (rt->dst_type == MCTP_ROUTE_DIRECT)
613+ mctp_dev_put(rt->dev);
614614 kfree_rcu(rt, rcu);
615615 }
616616 }
@@ -799,10 +799,16 @@ static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk, · 182 unmodified lines
799799 }
800800
801801 /* routing lookups */
802+static unsigned int mctp_route_netid(struct mctp_route *rt)
803+{
804+ return rt->dst_type == MCTP_ROUTE_DIRECT ?
805+ READ_ONCE(rt->dev->net) : rt->gateway.net;
806+}
807+
802808 static bool mctp_rt_match_eid(struct mctp_route *rt,
803809 unsigned int net, mctp_eid_t eid)
804810 {
805− return READ_ONCE(rt->dev->net) == net &&
811+ return mctp_route_netid(rt) == net &&
806812 rt->min <= eid && rt->max >= eid;
807813 }
808814
@@ -811,16 +817,21 @@ static bool mctp_rt_compare_exact(struct mctp_route *rt1, · 2 unmodified lines
811817 struct mctp_route *rt2)
812818 {
813819 ASSERT_RTNL();
814− return rt1->dev->net == rt2->dev->net &&
820+ return mctp_route_netid(rt1) == mctp_route_netid(rt2) &&
815821 rt1->min == rt2->min &&
816822 rt1->max == rt2->max;
817823 }
818824
819−static void mctp_dst_from_route(struct mctp_dst *dst, struct mctp_route *route)
825+/* must only be called on a direct route, as the final output hop */
826+static void mctp_dst_from_route(struct mctp_dst *dst, mctp_eid_t eid,
827+ unsigned int mtu, struct mctp_route *route)
820828 {
821829 mctp_dev_hold(route->dev);
830+ dst->nexthop = eid;
822831 dst->dev = route->dev;
823− dst->mtu = route->mtu ?: READ_ONCE(dst->dev->dev->mtu);
832+ dst->mtu = READ_ONCE(dst->dev->dev->mtu);
833+ if (mtu)
834+ dst->mtu = min(dst->mtu, mtu);
824835 dst->halen = 0;
825836 dst->output = route->output;
826837 }
@@ -854,6 +865,7 @@ int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex, · 27 unmodified lines
854865 dst->mtu = READ_ONCE(netdev->mtu);
855866 dst->halen = halen;
856867 dst->output = mctp_dst_output;
868+ dst->nexthop = 0;
857869 memcpy(dst->haddr, haddr, halen);
858870
859871 rc = 0;
@@ -868,24 +880,54 @@ void mctp_dst_release(struct mctp_dst *dst) · 8 unmodified lines
868880 mctp_dev_put(dst->dev);
869881 }
870882
883+static struct mctp_route *mctp_route_lookup_single(struct net *net,
884+ unsigned int dnet,
885+ mctp_eid_t daddr)
886+{
887+ struct mctp_route *rt;
888+
889+ list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
890+ if (mctp_rt_match_eid(rt, dnet, daddr))
891+ return rt;
892+ }
893+
894+ return NULL;
895+}
896+
871897 /* populates *dst on successful lookup, if set */
872898 int mctp_route_lookup(struct net *net, unsigned int dnet,
873899 mctp_eid_t daddr, struct mctp_dst *dst)
874900 {
901+ const unsigned int max_depth = 32;
902+ unsigned int depth, mtu = 0;
875903 int rc = -EHOSTUNREACH;
876− struct mctp_route *rt;
877904
878905 rcu_read_lock();
879906
880− list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
881− /* TODO: add metrics */
882− if (!mctp_rt_match_eid(rt, dnet, daddr))
883− continue;
907+ for (depth = 0; depth < max_depth; depth++) {
908+ struct mctp_route *rt;
884909
885− if (dst)
886− mctp_dst_from_route(dst, rt);
887− rc = 0;
888− break;
910+ rt = mctp_route_lookup_single(net, dnet, daddr);
911+ if (!rt)
912+ break;
913+
914+ /* clamp mtu to the smallest in the path, allowing 0
915+ * to specify no restrictions
916+ */
917+ if (mtu && rt->mtu)
918+ mtu = min(mtu, rt->mtu);
919+ else
920+ mtu = mtu ?: rt->mtu;
921+
922+ if (rt->dst_type == MCTP_ROUTE_DIRECT) {
923+ if (dst)
924+ mctp_dst_from_route(dst, daddr, mtu, rt);
925+ rc = 0;
926+ break;
927+
928+ } else if (rt->dst_type == MCTP_ROUTE_GATEWAY) {
929+ daddr = rt->gateway.eid;
930+ }
889931 }
890932
891933 rcu_read_unlock();
@@ -902,10 +944,13 @@ static int mctp_route_lookup_null(struct net *net, struct net_device *dev, · 10 unmodified lines
902944 rcu_read_lock();
903945
904946 list_for_each_entry_rcu(rt, &net->mctp.routes, list) {
905− if (rt->dev->dev != dev || rt->type != RTN_LOCAL)
947+ if (rt->dst_type != MCTP_ROUTE_DIRECT || rt->type != RTN_LOCAL)
948+ continue;
949+
950+ if (rt->dev->dev != dev)
906951 continue;
907952
908− mctp_dst_from_route(dst, rt);
953+ mctp_dst_from_route(dst, 0, 0, rt);
909954 rc = 0;
910955 break;
911956 }
@@ -1085,11 +1130,6 @@ out_release: · 173 unmodified lines
10851130 return rc;
10861131 }
10871132
1088−static unsigned int mctp_route_netid(struct mctp_route *rt)
1089−{
1090− return rt->dev->net;
1091−}
1092−
10931133 /* route management */
10941134
10951135 /* mctp_route_add(): Add the provided route, previously allocated via
@@ -1097,9 +1137,9 @@ static unsigned int mctp_route_netid(struct mctp_route *rt) · 1 unmodified line
10971137 * hold on rt->dev for usage in the route table. On failure a caller will want
10981138 * to mctp_route_release().
10991139 *
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.
1140+ * We expect that the caller has set rt->type, rt->dst_type, rt->min, rt->max,
1141+ * rt->mtu and either rt->dev (with a reference held appropriately) or
1142+ * rt->gateway. Other fields will be populated.
11031143 */
11041144 static int mctp_route_add(struct net *net, struct mctp_route *rt)
11051145 {
@@ -1108,7 +1148,10 @@ static int mctp_route_add(struct net *net, struct mctp_route *rt) · 2 unmodified lines
11081148 if (!mctp_address_unicast(rt->min) || !mctp_address_unicast(rt->max))
11091149 return -EINVAL;
11101150
1111− if (!rt->dev)
1151+ if (rt->dst_type == MCTP_ROUTE_DIRECT && !rt->dev)
1152+ return -EINVAL;
1153+
1154+ if (rt->dst_type == MCTP_ROUTE_GATEWAY && !rt->gateway.eid)
11121155 return -EINVAL;
11131156
11141157 switch (rt->type) {
@@ -1177,6 +1220,7 @@ int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr) · 62 unmodified lines
11771220
11781221 rt->min = addr;
11791222 rt->max = addr;
1223+ rt->dst_type = MCTP_ROUTE_DIRECT;
11801224 rt->dev = mdev;
11811225 rt->type = RTN_LOCAL;
11821226
@@ -1203,7 +1247,7 @@ void mctp_route_remove_dev(struct mctp_dev *mdev) · 20 unmodified lines
12031247
12041248 ASSERT_RTNL();
12051249 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) {
1206− if (rt->dev == mdev) {
1250+ if (rt->dst_type == MCTP_ROUTE_DIRECT && rt->dev == mdev) {
12071251 list_del_rcu(&rt->list);
12081252 /* TODO: immediate RTM_DELROUTE */
12091253 mctp_route_release(rt);
@@ -1296,21 +1340,28 @@ static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = { · 86 unmodified lines
12961340 [RTA_DST] = { .type = NLA_U8 },
12971341 [RTA_METRICS] = { .type = NLA_NESTED },
12981342 [RTA_OIF] = { .type = NLA_U32 },
1343+ [RTA_GATEWAY] = NLA_POLICY_EXACT_LEN(sizeof(struct mctp_fq_addr)),
12991344 };
13001345
13011346 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {
13021347 [RTAX_MTU] = { .type = NLA_U32 },
13031348 };
13041349
1305−/* base parsing; common to both _lookup and _populate variants */
1350+/* base parsing; common to both _lookup and _populate variants.
1351+ *
1352+ * For gateway routes (which have a RTA_GATEWAY, and no RTA_OIF), we populate
1353+ * *gatweayp. for direct routes (RTA_OIF, no RTA_GATEWAY), we populate *mdev.
1354+ */
13061355 static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh,
13071356 struct netlink_ext_ack *extack,
13081357 struct nlattr **tb, struct rtmsg **rtm,
13091358 struct mctp_dev **mdev,
1359+ struct mctp_fq_addr *gatewayp,
13101360 mctp_eid_t *daddr_start)
13111361 {
1362+ struct mctp_fq_addr *gateway = NULL;
1363+ unsigned int ifindex = 0;
13121364 struct net_device *dev;
1313− unsigned int ifindex;
13141365 int rc;
13151366
13161367 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX,
@@ -1326,11 +1377,44 @@ static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh, · 9 unmodified lines
13261377 }
13271378 *daddr_start = nla_get_u8(tb[RTA_DST]);
13281379
1329− if (!tb[RTA_OIF]) {
1330− NL_SET_ERR_MSG(extack, "ifindex missing");
1380+ if (tb[RTA_OIF])
1381+ ifindex = nla_get_u32(tb[RTA_OIF]);
1382+
1383+ if (tb[RTA_GATEWAY])
1384+ gateway = nla_data(tb[RTA_GATEWAY]);
1385+
1386+ if (ifindex && gateway) {
1387+ NL_SET_ERR_MSG(extack,
1388+ "cannot specify both ifindex and gateway");
1389+ return -EINVAL;
1390+
1391+ } else if (ifindex) {
1392+ dev = __dev_get_by_index(net, ifindex);
1393+ if (!dev) {
1394+ NL_SET_ERR_MSG(extack, "bad ifindex");
1395+ return -ENODEV;
1396+ }
1397+ *mdev = mctp_dev_get_rtnl(dev);
1398+ if (!*mdev)
1399+ return -ENODEV;
1400+ gatewayp->eid = 0;
1401+
1402+ } else if (gateway) {
1403+ if (!mctp_address_unicast(gateway->eid)) {
1404+ NL_SET_ERR_MSG(extack, "bad gateway");
1405+ return -EINVAL;
1406+ }
1407+
1408+ gatewayp->eid = gateway->eid;
1409+ gatewayp->net = gateway->net != MCTP_NET_ANY ?
1410+ gateway->net :
1411+ READ_ONCE(net->mctp.default_net);
1412+ *mdev = NULL;
1413+
1414+ } else {
1415+ NL_SET_ERR_MSG(extack, "no route output provided");
13311416 return -EINVAL;
13321417 }
1333− ifindex = nla_get_u32(tb[RTA_OIF]);
13341418
13351419 *rtm = nlmsg_data(nlh);
13361420 if ((*rtm)->rtm_family != AF_MCTP) {
@@ -1343,16 +1427,6 @@ static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh, · 6 unmodified lines
13431427 return -EINVAL;
13441428 }
13451429
1346− dev = __dev_get_by_index(net, ifindex);
1347− if (!dev) {
1348− NL_SET_ERR_MSG(extack, "bad ifindex");
1349− return -ENODEV;
1350− }
1351−
1352− *mdev = mctp_dev_get_rtnl(dev);
1353− if (!*mdev)
1354− return -ENODEV;
1355−
13561430 return 0;
13571431 }
13581432
@@ -1366,24 +1440,34 @@ static int mctp_route_nlparse_lookup(struct net *net, struct nlmsghdr *nlh, · 7 unmodified lines
13661440 unsigned int *daddr_extent)
13671441 {
13681442 struct nlattr *tb[RTA_MAX + 1];
1443+ struct mctp_fq_addr gw;
13691444 struct mctp_dev *mdev;
13701445 struct rtmsg *rtm;
13711446 int rc;
13721447
13731448 rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm,
1374− &mdev, daddr_start);
1449+ &mdev, &gw, daddr_start);
13751450 if (rc)
13761451 return rc;
13771452
1378− *netid = mdev->net;
1453+ if (mdev) {
1454+ *netid = mdev->net;
1455+ } else if (gw.eid) {
1456+ *netid = gw.net;
1457+ } else {
1458+ /* bug: _nlparse_common should not allow this */
1459+ return -1;
1460+ }
1461+
13791462 *type = rtm->rtm_type;
13801463 *daddr_extent = rtm->rtm_dst_len;
13811464
13821465 return 0;
13831466 }
13841467
1385−/* Full route parse for RTM_NEWROUTE: populate @rt. On success, the route will
1386− * hold a reference to the dev.
1468+/* Full route parse for RTM_NEWROUTE: populate @rt. On success,
1469+ * MCTP_ROUTE_DIRECT routes (ie, those with a direct dev) will hold a reference
1470+ * to that dev.
13871471 */
13881472 static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh,
13891473 struct netlink_ext_ack *extack,
@@ -1392,6 +1476,7 @@ static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, · 2 unmodified lines
13921476 struct nlattr *tbx[RTAX_MAX + 1];
13931477 struct nlattr *tb[RTA_MAX + 1];
13941478 unsigned int daddr_extent;
1479+ struct mctp_fq_addr gw;
13951480 mctp_eid_t daddr_start;
13961481 struct mctp_dev *dev;
13971482 struct rtmsg *rtm;
@@ -1399,7 +1484,7 @@ static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, · 1 unmodified line
13991484 int rc;
14001485
14011486 rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm,
1402− &dev, &daddr_start);
1487+ &dev, &gw, &daddr_start);
14031488 if (rc)
14041489 return rc;
14051490
@@ -1425,8 +1510,15 @@ static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, · 19 unmodified lines
14251510 rt->min = daddr_start;
14261511 rt->max = daddr_start + daddr_extent;
14271512 rt->mtu = mtu;
1428− rt->dev = dev;
1429− mctp_dev_hold(rt->dev);
1513+ if (gw.eid) {
1514+ rt->dst_type = MCTP_ROUTE_GATEWAY;
1515+ rt->gateway.eid = gw.eid;
1516+ rt->gateway.net = gw.net;
1517+ } else {
1518+ rt->dst_type = MCTP_ROUTE_DIRECT;
1519+ rt->dev = dev;
1520+ mctp_dev_hold(rt->dev);
1521+ }
14301522
14311523 return 0;
14321524 }
@@ -1446,7 +1538,8 @@ static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, · 13 unmodified lines
14461538 if (rc < 0)
14471539 goto err_free;
14481540
1449− if (rt->dev->dev->flags & IFF_LOOPBACK) {
1541+ if (rt->dst_type == MCTP_ROUTE_DIRECT &&
1542+ rt->dev->dev->flags & IFF_LOOPBACK) {
14501543 NL_SET_ERR_MSG(extack, "no routes to loopback");
14511544 rc = -EINVAL;
14521545 goto err_free;
@@ -1505,7 +1598,6 @@ static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt, · 52 unmodified lines
15051598 hdr->rtm_tos = 0;
15061599 hdr->rtm_table = RT_TABLE_DEFAULT;
15071600 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */
1508− hdr->rtm_scope = RT_SCOPE_LINK; /* TODO: scope in mctp_route? */
15091601 hdr->rtm_type = rt->type;
15101602
15111603 if (nla_put_u8(skb, RTA_DST, rt->min))
@@ -1522,13 +1614,17 @@ static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt, · 10 unmodified lines
15221614
15231615 nla_nest_end(skb, metrics);
15241616
1525− if (rt->dev) {
1617+ if (rt->dst_type == MCTP_ROUTE_DIRECT) {
1618+ hdr->rtm_scope = RT_SCOPE_LINK;
15261619 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex))
15271620 goto cancel;
1621+ } else if (rt->dst_type == MCTP_ROUTE_GATEWAY) {
1622+ hdr->rtm_scope = RT_SCOPE_UNIVERSE;
1623+ if (nla_put(skb, RTA_GATEWAY,
1624+ sizeof(rt->gateway), &rt->gateway))
1625+ goto cancel;
15281626 }
15291627
1530− /* TODO: conditional neighbour physaddr? */
1531−
15321628 nlmsg_end(skb, nlh);
15331629
15341630 return 0;
net/mctp/test/utils.c+2−1
@@ -134,6 +134,7 @@ struct mctp_test_route *mctp_test_create_route(struct net *net, · 133 unmodified lines
134134 rt->rt.max = eid;
135135 rt->rt.mtu = mtu;
136136 rt->rt.type = RTN_UNSPEC;
137+ rt->rt.dst_type = MCTP_ROUTE_DIRECT;
137138 if (dev)
138139 mctp_dev_hold(dev);
139140 rt->rt.dev = dev;
@@ -176,7 +177,7 @@ void mctp_test_route_destroy(struct kunit *test, struct mctp_test_route *rt) · 36 unmodified lines
176177 list_del_rcu(&rt->rt.list);
177178 rtnl_unlock();
178179
179− if (rt->rt.dev)
180+ if (rt->rt.dst_type == MCTP_ROUTE_DIRECT && rt->rt.dev)
180181 mctp_dev_put(rt->rt.dev);
181182
182183 refs = refcount_read(&rt->rt.refs);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t