-/* Interface between zebra message and rtm message. */
-static int kernel_rtm_ipv4(int cmd, const struct prefix *p,
- const struct nexthop_group *ng, uint32_t metric)
-
-{
- struct sockaddr_in *mask = NULL;
- struct sockaddr_in sin_dest, sin_mask, sin_gate;
-#ifdef __OpenBSD__
- struct sockaddr_mpls smpls;
-#endif
- union sockunion *smplsp = NULL;
- struct nexthop *nexthop;
- int nexthop_num = 0;
- ifindex_t ifindex = 0;
- int gate = 0;
- int error;
- char prefix_buf[PREFIX_STRLEN];
- enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
-
- if (IS_ZEBRA_DEBUG_RIB)
- prefix2str(p, prefix_buf, sizeof(prefix_buf));
- memset(&sin_dest, 0, sizeof(struct sockaddr_in));
- sin_dest.sin_family = AF_INET;
-#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- sin_dest.sin_len = sizeof(struct sockaddr_in);
-#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
- sin_dest.sin_addr = p->u.prefix4;
-
- memset(&sin_mask, 0, sizeof(struct sockaddr_in));
-
- memset(&sin_gate, 0, sizeof(struct sockaddr_in));
- sin_gate.sin_family = AF_INET;
-#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- sin_gate.sin_len = sizeof(struct sockaddr_in);
-#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
-
- /* Make gateway. */
- for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
- if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
- continue;
-
- gate = 0;
- char gate_buf[INET_ADDRSTRLEN] = "NULL";
-
- /*
- * XXX We need to refrain from kernel operations in some cases,
- * but this if statement seems overly cautious - what about
- * other than ADD and DELETE?
- */
- if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
- || (cmd == RTM_DELETE)) {
- if (nexthop->type == NEXTHOP_TYPE_IPV4
- || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
- sin_gate.sin_addr = nexthop->gate.ipv4;
- gate = 1;
- }
- if (nexthop->type == NEXTHOP_TYPE_IFINDEX
- || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
- ifindex = nexthop->ifindex;
- if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
- struct in_addr loopback;
- loopback.s_addr = htonl(INADDR_LOOPBACK);
- sin_gate.sin_addr = loopback;
- bh_type = nexthop->bh_type;
- gate = 1;
- }
-
- if (gate && p->prefixlen == 32)
- mask = NULL;
- else {
- masklen2ip(p->prefixlen, &sin_mask.sin_addr);
- sin_mask.sin_family = AF_INET;
-#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
- sin_mask.sin_len =
- sin_masklen(sin_mask.sin_addr);
-#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
- mask = &sin_mask;
- }
-
-#ifdef __OpenBSD__
- if (nexthop->nh_label
- && !kernel_rtm_add_labels(nexthop->nh_label,
- &smpls))
- continue;
- smplsp = (union sockunion *)&smpls;
-#endif
-
- error = rtm_write(cmd, (union sockunion *)&sin_dest,
- (union sockunion *)mask,
- gate ? (union sockunion *)&sin_gate
- : NULL,
- smplsp, ifindex, bh_type, metric);
-
- if (IS_ZEBRA_DEBUG_KERNEL) {
- if (!gate) {
- zlog_debug(
- "%s: %s: attention! gate not found for re",
- __func__, prefix_buf);
- } else
- inet_ntop(AF_INET, &sin_gate.sin_addr,
- gate_buf, INET_ADDRSTRLEN);
- }
-
- switch (error) {
- /* We only flag nexthops as being in FIB if rtm_write()
- * did its work. */
- case ZEBRA_ERR_NOERROR:
- nexthop_num++;
- if (IS_ZEBRA_DEBUG_KERNEL)
- zlog_debug(
- "%s: %s: successfully did NH %s",
- __func__, prefix_buf, gate_buf);
-
- if (cmd == RTM_ADD)
- SET_FLAG(nexthop->flags,
- NEXTHOP_FLAG_FIB);
-
- break;
-
- /* The only valid case for this error is kernel's
- * failure to install
- * a multipath route, which is common for FreeBSD. This
- * should be
- * ignored silently, but logged as an error otherwise.
- */
- case ZEBRA_ERR_RTEXIST:
- if (cmd != RTM_ADD)
- flog_err(
- EC_LIB_SYSTEM_CALL,
- "%s: rtm_write() returned %d for command %d",
- __func__, error, cmd);
- continue;
-
- /* Note any unexpected status returns */
- default:
- flog_err(
- EC_LIB_SYSTEM_CALL,
- "%s: %s: rtm_write() unexpectedly returned %d for command %s",
- __func__,
- prefix2str(p, prefix_buf,
- sizeof(prefix_buf)),
- error,
- lookup_msg(rtm_type_str, cmd, NULL));
- break;
- }
- } /* if (cmd and flags make sense) */
- else if (IS_ZEBRA_DEBUG_KERNEL)
- zlog_debug("%s: odd command %s for flags %d", __func__,
- lookup_msg(rtm_type_str, cmd, NULL),
- nexthop->flags);
- } /* for (ALL_NEXTHOPS(...))*/
-
- /* If there was no useful nexthop, then complain. */
- if (nexthop_num == 0) {
- if (IS_ZEBRA_DEBUG_KERNEL)
- zlog_debug("%s: No useful nexthops were found in RIB prefix %s",
- __func__, prefix2str(p, prefix_buf,
- sizeof(prefix_buf)));
- return 1;
- }
-
- return 0; /*XXX*/
-}
-