]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/rt_netlink.c
Merge pull request #3048 from donaldsharp/pim_6.0
[mirror_frr.git] / zebra / rt_netlink.c
index b639cf0c49c5dc68fc44ab79b9b1a67bbdc29a7e..3683596b41394b9ed3d2a331ad835fafd9a2b98a 100644 (file)
@@ -246,6 +246,33 @@ static vrf_id_t vrf_lookup_by_table(uint32_t table_id, ns_id_t ns_id)
        return VRF_DEFAULT;
 }
 
+/**
+ * @parse_encap_mpls() - Parses encapsulated mpls attributes
+ * @tb:         Pointer to rtattr to look for nested items in.
+ * @labels:     Pointer to store labels in.
+ *
+ * Return:      Number of mpls labels found.
+ */
+static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels)
+{
+       struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0};
+       mpls_lse_t *lses = NULL;
+       int num_labels = 0;
+       uint32_t ttl = 0;
+       uint32_t bos = 0;
+       uint32_t exp = 0;
+       mpls_label_t label = 0;
+
+       netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb);
+       lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]);
+       while (!bos && num_labels < MPLS_MAX_LABELS) {
+               mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos);
+               labels[num_labels++] = label;
+       }
+
+       return num_labels;
+}
+
 /* Looking up routing table by netlink interface. */
 static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                                             int startup)
@@ -274,6 +301,10 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
        void *src = NULL;     /* IPv6 srcdest   source prefix */
        enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
 
+       /* MPLS labels */
+       mpls_label_t labels[MPLS_MAX_LABELS] = {0};
+       int num_labels = 0;
+
        rtm = NLMSG_DATA(h);
 
        if (startup && h->nlmsg_type != RTM_NEWROUTE)
@@ -384,29 +415,46 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
 
        if (rtm->rtm_family == AF_INET) {
                p.family = AF_INET;
+               if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) {
+                       zlog_err(
+                               "Invalid destination prefix length: %u received from kernel route change",
+                               rtm->rtm_dst_len);
+                       return -1;
+               }
                memcpy(&p.u.prefix4, dest, 4);
                p.prefixlen = rtm->rtm_dst_len;
 
-               src_p.prefixlen =
-                       0; // Forces debug below to not display anything
+               if (rtm->rtm_src_len != 0) {
+                       char buf[PREFIX_STRLEN];
+                       zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)",
+                                 prefix2str(&p, buf, sizeof(buf)), vrf_id);
+                       return 0;
+               }
+
+               /* Force debug below to not display anything for source */
+               src_p.prefixlen = 0;
        } else if (rtm->rtm_family == AF_INET6) {
                p.family = AF_INET6;
+               if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) {
+                       zlog_err(
+                               "Invalid destination prefix length: %u received from kernel route change",
+                               rtm->rtm_dst_len);
+                       return -1;
+               }
                memcpy(&p.u.prefix6, dest, 16);
                p.prefixlen = rtm->rtm_dst_len;
 
                src_p.family = AF_INET6;
+               if (rtm->rtm_src_len > IPV6_MAX_BITLEN) {
+                       zlog_err(
+                               "Invalid source prefix length: %u received from kernel route change",
+                               rtm->rtm_src_len);
+                       return -1;
+               }
                memcpy(&src_p.prefix, src, 16);
                src_p.prefixlen = rtm->rtm_src_len;
        }
 
-       if (rtm->rtm_src_len != 0) {
-               char buf[PREFIX_STRLEN];
-               zlog_warn(
-                       "unsupported IPv[4|6] sourcedest route (dest %s vrf %u)",
-                       prefix2str(&p, buf, sizeof(buf)), vrf_id);
-               return 0;
-       }
-
        /*
         * For ZEBRA_ROUTE_KERNEL types:
         *
@@ -491,8 +539,19 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                        }
                        nh.vrf_id = nh_vrf_id;
 
+                       if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
+                           && *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
+                                      == LWTUNNEL_ENCAP_MPLS) {
+                               num_labels =
+                                       parse_encap_mpls(tb[RTA_ENCAP], labels);
+                       }
+
+                       if (num_labels)
+                               nexthop_add_labels(&nh, ZEBRA_LSP_STATIC,
+                                                  num_labels, labels);
+
                        rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p,
-                               NULL, &nh, table, metric, mtu, distance, tag);
+                               &src_p, &nh, table, metric, mtu, distance, tag);
                } else {
                        /* This is a multipath route */
 
@@ -515,6 +574,7 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                        re->tag = tag;
 
                        for (;;) {
+                               struct nexthop *nh = NULL;
                                vrf_id_t nh_vrf_id;
                                if (len < (int)sizeof(*rtnh)
                                    || rtnh->rtnh_len > len)
@@ -552,35 +612,49 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                                        if (tb[RTA_GATEWAY])
                                                gate = RTA_DATA(
                                                        tb[RTA_GATEWAY]);
+                                       if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
+                                           && *(uint16_t *)RTA_DATA(
+                                                      tb[RTA_ENCAP_TYPE])
+                                                      == LWTUNNEL_ENCAP_MPLS) {
+                                               num_labels = parse_encap_mpls(
+                                                       tb[RTA_ENCAP], labels);
+                                       }
                                }
 
                                if (gate) {
                                        if (rtm->rtm_family == AF_INET) {
                                                if (index)
-                                                       route_entry_nexthop_ipv4_ifindex_add(
+                                                       nh = route_entry_nexthop_ipv4_ifindex_add(
                                                                re, gate,
                                                                prefsrc, index,
                                                                nh_vrf_id);
                                                else
-                                                       route_entry_nexthop_ipv4_add(
+                                                       nh = route_entry_nexthop_ipv4_add(
                                                                re, gate,
                                                                prefsrc,
                                                                nh_vrf_id);
                                        } else if (rtm->rtm_family
                                                   == AF_INET6) {
                                                if (index)
-                                                       route_entry_nexthop_ipv6_ifindex_add(
+                                                       nh = route_entry_nexthop_ipv6_ifindex_add(
                                                                re, gate, index,
                                                                nh_vrf_id);
                                                else
-                                                       route_entry_nexthop_ipv6_add(
+                                                       nh = route_entry_nexthop_ipv6_add(
                                                                re, gate,
                                                                nh_vrf_id);
                                        }
                                } else
-                                       route_entry_nexthop_ifindex_add(
+                                       nh = route_entry_nexthop_ifindex_add(
                                                re, index, nh_vrf_id);
 
+                               if (nh && num_labels)
+                                       nexthop_add_labels(nh, ZEBRA_LSP_STATIC,
+                                                          num_labels, labels);
+
+                               if (rtnh->rtnh_len == 0)
+                                       break;
+
                                len -= NLMSG_ALIGN(rtnh->rtnh_len);
                                rtnh = RTNH_NEXT(rtnh);
                        }
@@ -591,8 +665,8 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                        if (re->nexthop_num == 0)
                                XFREE(MTYPE_RE, re);
                        else
-                               rib_add_multipath(afi, SAFI_UNICAST, &p, NULL,
-                                                 re);
+                               rib_add_multipath(afi, SAFI_UNICAST, &p,
+                                                 &src_p, re);
                }
        } else {
                if (!tb[RTA_MULTIPATH]) {
@@ -624,12 +698,14 @@ static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id,
                        if (gate)
                                memcpy(&nh.gate, gate, sz);
                        rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
-                                  &p, NULL, &nh, table, metric, true);
+                                  &p, &src_p, &nh, table, metric, distance,
+                                  true);
                } else {
                        /* XXX: need to compare the entire list of nexthops
                         * here for NLM_F_APPEND stupidity */
                        rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags,
-                                  &p, NULL, NULL, table, metric, true);
+                                  &p, &src_p, NULL, table, metric, distance,
+                                  true);
                }
        }
 
@@ -701,6 +777,9 @@ static int netlink_route_change_read_multicast(struct nlmsghdr *h,
                        oif[oif_count] = rtnh->rtnh_ifindex;
                        oif_count++;
 
+                       if (rtnh->rtnh_len == 0)
+                               break;
+
                        len -= NLMSG_ALIGN(rtnh->rtnh_len);
                        rtnh = RTNH_NEXT(rtnh);
                }
@@ -714,15 +793,17 @@ static int netlink_route_change_read_multicast(struct nlmsghdr *h,
                        ifp = if_lookup_by_index(oif[count], vrf);
                        char temp[256];
 
-                       sprintf(temp, "%s ", ifp->name);
+                       sprintf(temp, "%s(%d) ", ifp ? ifp->name : "Unknown",
+                               oif[count]);
                        strcat(oif_list, temp);
                }
                struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf);
                ifp = if_lookup_by_index(iif, vrf);
-               zlog_debug(
-                       "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld",
-                       zvrf->vrf->name, vrf, nl_msg_type_to_str(h->nlmsg_type),
-                       sbuf, gbuf, ifp->name, oif_list, m->lastused);
+               zlog_debug("MCAST VRF: %s(%d) %s (%s,%s) IIF: %s(%d) OIF: %s jiffies: %lld",
+                          zvrf->vrf->name, vrf,
+                          nl_msg_type_to_str(h->nlmsg_type),
+                          sbuf, gbuf, ifp ? ifp->name : "Unknown", iif,
+                          oif_list, m->lastused);
        }
        return 0;
 }
@@ -740,12 +821,9 @@ int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
                return 0;
        }
 
-       if (!(rtm->rtm_family == AF_INET || rtm->rtm_family == AF_INET6
-             || rtm->rtm_family == AF_ETHERNET || rtm->rtm_family == AF_EVPN
-             || rtm->rtm_family == AF_UNSPEC
-             || rtm->rtm_family == AF_FLOWSPEC)) {
+       if (!(rtm->rtm_family == AF_INET || rtm->rtm_family == AF_INET6)) {
                zlog_warn(
-                       "Invalid address family: %d recieved from kernel route change: %d",
+                       "Invalid address family: %u received from kernel route change: %u",
                        rtm->rtm_family, h->nlmsg_type);
                return 0;
        }
@@ -758,10 +836,6 @@ int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
                           nl_rttype_to_str(rtm->rtm_type),
                           nl_rtproto_to_str(rtm->rtm_protocol), ns_id);
 
-       /* We don't care about change notifications for the MPLS table. */
-       /* TODO: Revisit this. */
-       if (rtm->rtm_family == AF_MPLS)
-               return 0;
 
        len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
        if (len < 0) {
@@ -2161,6 +2235,7 @@ static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
        char buf2[INET6_ADDRSTRLEN];
        int mac_present = 0;
        uint8_t ext_learned;
+       uint8_t router_flag;
 
        ndm = NLMSG_DATA(h);
 
@@ -2251,6 +2326,7 @@ static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
                }
 
                ext_learned = (ndm->ndm_flags & NTF_EXT_LEARNED) ? 1 : 0;
+               router_flag = (ndm->ndm_flags & NTF_ROUTER) ? 1 : 0;
 
                if (IS_ZEBRA_DEBUG_KERNEL)
                        zlog_debug(
@@ -2273,7 +2349,7 @@ static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id)
                if (ndm->ndm_state & NUD_VALID)
                        return zebra_vxlan_handle_kernel_neigh_update(
                                ifp, link_if, &ip, &mac, ndm->ndm_state,
-                               ext_learned);
+                               ext_learned, router_flag);
 
                return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip);
        }
@@ -2398,7 +2474,7 @@ int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
                return netlink_ipneigh_change(h, len, ns_id);
        else {
                zlog_warn(
-                       "Invalid address family: %d recieved from kernel neighbor change: %d",
+                       "Invalid address family: %u received from kernel neighbor change: %u",
                        ndm->ndm_family, h->nlmsg_type);
                return 0;
        }
@@ -2407,7 +2483,8 @@ int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id)
 }
 
 static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip,
-                                struct ethaddr *mac, uint32_t flags, int cmd)
+                                struct ethaddr *mac, uint8_t flags,
+                                uint16_t state, int cmd)
 {
        struct {
                struct nlmsghdr n;
@@ -2430,11 +2507,10 @@ static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip,
                req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
        req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH
        req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6;
-       req.ndm.ndm_state = flags;
+       req.ndm.ndm_state = state;
        req.ndm.ndm_ifindex = ifp->ifindex;
        req.ndm.ndm_type = RTN_UNICAST;
-       req.ndm.ndm_flags = NTF_EXT_LEARNED;
-
+       req.ndm.ndm_flags = flags;
 
        ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN;
        addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len);
@@ -2442,12 +2518,12 @@ static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip,
                addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6);
 
        if (IS_ZEBRA_DEBUG_KERNEL)
-               zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s",
+               zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x",
                           nl_msg_type_to_str(cmd),
                           nl_family_to_str(req.ndm.ndm_family), ifp->name,
                           ifp->ifindex, ipaddr2str(ip, buf, sizeof(buf)),
                           mac ? prefix_mac2str(mac, buf2, sizeof(buf2))
-                              : "null");
+                              : "null", flags);
 
        return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
                            0);
@@ -2468,14 +2544,15 @@ int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
 }
 
 int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
-                    struct ethaddr *mac)
+                    struct ethaddr *mac, uint8_t flags)
 {
-       return netlink_neigh_update2(ifp, ip, mac, NUD_NOARP, RTM_NEWNEIGH);
+       return netlink_neigh_update2(ifp, ip, mac, flags,
+                                    NUD_NOARP, RTM_NEWNEIGH);
 }
 
 int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
 {
-       return netlink_neigh_update2(ifp, ip, NULL, 0, RTM_DELNEIGH);
+       return netlink_neigh_update2(ifp, ip, NULL, 0, 0, RTM_DELNEIGH);
 }
 
 /*