2 * Code for encoding/decoding FPM messages that are in netlink format.
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of GNU Zebra.
10 * GNU Zebra is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * GNU Zebra is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GNU Zebra; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 #include "rt_netlink.h"
34 #include "zebra_fpm_private.h"
39 * Returns string representation of an address of the given AF.
41 static inline const char *
42 addr_to_a (u_char af
, void *addr
)
45 return "<No address>";
51 return inet_ntoa (*((struct in_addr
*) addr
));
55 return inet6_ntoa (*((struct in6_addr
*) addr
));
59 return "<Addr in unknown AF>";
66 * Convience wrapper that returns a human-readable string for the
67 * address in a prefix.
70 prefix_addr_to_a (struct prefix
*prefix
)
73 return "<No address>";
75 return addr_to_a (prefix
->family
, &prefix
->u
.prefix
);
81 * The size of an address in a given address family.
84 af_addr_size (u_char af
)
106 * Holds information about a single nexthop for netlink. These info
107 * structures are transient and may contain pointers into rib
108 * data structures for convenience.
110 typedef struct netlink_nh_info_t_
113 union g_addr
*gateway
;
116 * Information from the struct nexthop from which this nh was
117 * derived. For debug purposes only.
120 enum nexthop_types_t type
;
124 * netlink_route_info_t
126 * A structure for holding information for a netlink route message.
128 typedef struct netlink_route_info_t_
135 struct prefix
*prefix
;
140 * Nexthop structures. We keep things simple for now by enforcing a
141 * maximum of 64 in case MULTIPATH_NUM is 0;
143 netlink_nh_info_t nhs
[MAX (MULTIPATH_NUM
, 64)];
144 union g_addr
*pref_src
;
145 } netlink_route_info_t
;
148 * netlink_route_info_add_nh
150 * Add information about the given nexthop to the given route info
153 * Returns TRUE if a nexthop was added, FALSE otherwise.
156 netlink_route_info_add_nh (netlink_route_info_t
*ri
, struct nexthop
*nexthop
,
159 netlink_nh_info_t nhi
;
162 memset (&nhi
, 0, sizeof (nhi
));
165 if (ri
->num_nhs
>= (int) ZEBRA_NUM_OF (ri
->nhs
))
168 nhi
.recursive
= recursive
;
169 nhi
.type
= nexthop
->type
;
170 nhi
.if_index
= nexthop
->ifindex
;
172 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
173 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
175 nhi
.gateway
= &nexthop
->gate
;
176 if (nexthop
->src
.ipv4
.s_addr
)
181 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
182 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
183 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
185 nhi
.gateway
= &nexthop
->gate
;
187 #endif /* HAVE_IPV6 */
189 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
190 || nexthop
->type
== NEXTHOP_TYPE_IFNAME
)
192 if (nexthop
->src
.ipv4
.s_addr
)
196 if (!nhi
.gateway
&& nhi
.if_index
== 0)
200 * We have a valid nhi. Copy the structure over to the route_info.
202 ri
->nhs
[ri
->num_nhs
] = nhi
;
205 if (src
&& !ri
->pref_src
)
212 * netlink_proto_from_route_type
215 netlink_proto_from_route_type (int type
)
219 case ZEBRA_ROUTE_KERNEL
:
220 case ZEBRA_ROUTE_CONNECT
:
221 return RTPROT_KERNEL
;
229 * netlink_route_info_fill
231 * Fill out the route information object from the given route.
233 * Returns TRUE on success and FALSE on failure.
236 netlink_route_info_fill (netlink_route_info_t
*ri
, int cmd
,
237 rib_dest_t
*dest
, struct rib
*rib
)
239 struct nexthop
*nexthop
, *tnexthop
;
243 memset (ri
, 0, sizeof (*ri
));
245 ri
->prefix
= rib_dest_prefix (dest
);
246 ri
->af
= rib_dest_af (dest
);
248 ri
->nlmsg_type
= cmd
;
249 ri
->rtm_table
= rib_dest_vrf (dest
)->id
;
250 ri
->rtm_protocol
= RTPROT_UNSPEC
;
253 * An RTM_DELROUTE need not be accompanied by any nexthops,
254 * particularly in our communication with the FPM.
256 if (cmd
== RTM_DELROUTE
&& !rib
)
260 ri
->rtm_protocol
= netlink_proto_from_route_type (rib
->type
);
262 if ((rib
->flags
& ZEBRA_FLAG_BLACKHOLE
) || (rib
->flags
& ZEBRA_FLAG_REJECT
))
267 if (cmd
== RTM_NEWROUTE
)
271 if (rib
->flags
& ZEBRA_FLAG_BLACKHOLE
)
272 ri
->rtm_type
= RTN_BLACKHOLE
;
273 else if (rib
->flags
& ZEBRA_FLAG_REJECT
)
274 ri
->rtm_type
= RTN_UNREACHABLE
;
279 ri
->rtm_type
= RTN_UNICAST
;
282 ri
->metric
= &rib
->metric
;
289 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
291 if (MULTIPATH_NUM
!= 0 && ri
->num_nhs
>= MULTIPATH_NUM
)
294 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
297 if ((cmd
== RTM_NEWROUTE
298 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
299 || (cmd
== RTM_DELROUTE
300 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
302 netlink_route_info_add_nh (ri
, nexthop
, recursing
);
306 /* If there is no useful nexthop then return. */
307 if (ri
->num_nhs
== 0)
309 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
318 * netlink_route_info_encode
320 * Returns the number of bytes written to the buffer. 0 or a negative
321 * value indicates an error.
324 netlink_route_info_encode (netlink_route_info_t
*ri
, char *in_buf
,
330 netlink_nh_info_t
*nhi
;
339 req
= (void *) in_buf
;
341 buf_offset
= ((char *) req
->buf
) - ((char *) req
);
343 if (in_buf_len
< buf_offset
) {
348 memset (req
, 0, buf_offset
);
350 bytelen
= af_addr_size (ri
->af
);
352 req
->n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
353 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
354 req
->n
.nlmsg_type
= ri
->nlmsg_type
;
355 req
->r
.rtm_family
= ri
->af
;
356 req
->r
.rtm_table
= ri
->rtm_table
;
357 req
->r
.rtm_dst_len
= ri
->prefix
->prefixlen
;
358 req
->r
.rtm_protocol
= ri
->rtm_protocol
;
359 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
361 addattr_l (&req
->n
, in_buf_len
, RTA_DST
, &ri
->prefix
->u
.prefix
, bytelen
);
363 req
->r
.rtm_type
= ri
->rtm_type
;
367 addattr32 (&req
->n
, in_buf_len
, RTA_PRIORITY
, *ri
->metric
);
369 if (ri
->num_nhs
== 0)
372 if (ri
->num_nhs
== 1)
378 addattr_l (&req
->n
, in_buf_len
, RTA_GATEWAY
, nhi
->gateway
,
384 addattr32 (&req
->n
, in_buf_len
, RTA_OIF
, nhi
->if_index
);
394 char buf
[NL_PKT_BUF_SIZE
];
395 struct rtattr
*rta
= (void *) buf
;
396 struct rtnexthop
*rtnh
;
398 rta
->rta_type
= RTA_MULTIPATH
;
399 rta
->rta_len
= RTA_LENGTH (0);
400 rtnh
= RTA_DATA (rta
);
402 for (nexthop_num
= 0; nexthop_num
< ri
->num_nhs
; nexthop_num
++)
404 nhi
= &ri
->nhs
[nexthop_num
];
406 rtnh
->rtnh_len
= sizeof (*rtnh
);
407 rtnh
->rtnh_flags
= 0;
409 rtnh
->rtnh_ifindex
= 0;
410 rta
->rta_len
+= rtnh
->rtnh_len
;
414 rta_addattr_l (rta
, sizeof (buf
), RTA_GATEWAY
, nhi
->gateway
, bytelen
);
415 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + bytelen
;
420 rtnh
->rtnh_ifindex
= nhi
->if_index
;
423 rtnh
= RTNH_NEXT (rtnh
);
426 assert (rta
->rta_len
> RTA_LENGTH (0));
427 addattr_l (&req
->n
, in_buf_len
, RTA_MULTIPATH
, RTA_DATA (rta
),
434 addattr_l (&req
->n
, in_buf_len
, RTA_PREFSRC
, &ri
->pref_src
, bytelen
);
437 assert (req
->n
.nlmsg_len
< in_buf_len
);
438 return req
->n
.nlmsg_len
;
442 * zfpm_log_route_info
444 * Helper function to log the information in a route_info structure.
447 zfpm_log_route_info (netlink_route_info_t
*ri
, const char *label
)
449 netlink_nh_info_t
*nhi
;
452 zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label
,
453 nl_msg_type_to_str (ri
->nlmsg_type
),
454 prefix_addr_to_a (ri
->prefix
), ri
->prefix
->prefixlen
,
455 nl_rtproto_to_str (ri
->rtm_protocol
),
456 ri
->metric
? *ri
->metric
: 0);
458 for (i
= 0; i
< ri
->num_nhs
; i
++)
461 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
462 nhi
->if_index
, addr_to_a (ri
->af
, nhi
->gateway
),
463 nhi
->recursive
? "yes" : "no",
464 nexthop_type_to_str (nhi
->type
));
469 * zfpm_netlink_encode_route
471 * Create a netlink message corresponding to the given route in the
472 * given buffer space.
474 * Returns the number of bytes written to the buffer. 0 or a negative
475 * value indicates an error.
478 zfpm_netlink_encode_route (int cmd
, rib_dest_t
*dest
, struct rib
*rib
,
479 char *in_buf
, size_t in_buf_len
)
481 netlink_route_info_t ri_space
, *ri
;
485 if (!netlink_route_info_fill (ri
, cmd
, dest
, rib
))
488 zfpm_log_route_info (ri
, __FUNCTION__
);
490 return netlink_route_info_encode (ri
, in_buf
, in_buf_len
);