2 * lib/route/nexthop.c Routing Nexthop
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup nexthop Nexthop
19 #include <netlink-local.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/rtnl.h>
23 #include <netlink/route/route.h>
26 #define NEXTHOP_HAS_FLAGS 0x000001
27 #define NEXTHOP_HAS_WEIGHT 0x000002
28 #define NEXTHOP_HAS_IFINDEX 0x000004
29 #define NEXTHOP_HAS_GATEWAY 0x000008
33 * @name Nexthop Allocation/Freeage
38 * Allocate a routing nexthop.
39 * @return Newly allocated routing nexthop object.
41 struct rtnl_nexthop *rtnl_route_nh_alloc(void)
43 struct rtnl_nexthop *nh;
45 nh = calloc(1, sizeof(*nh));
51 nl_init_list_head(&nh->rtnh_list);
57 * Free a routing nexthop.
58 * @arg nh Routing nexthop to be freed.
60 void rtnl_route_nh_free(struct rtnl_nexthop *nh)
62 nl_addr_put(nh->rtnh_gateway);
68 * @name Attribute: Weight
72 * Set weight of routing nexthop.
73 * @arg nh Routing nexthop.
74 * @arg weight New weight value.
76 void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
78 nh->rtnh_weight = weight;
79 nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
83 * Get weight of routing nexthop.
84 * @arg nh Routing nexthop.
85 * @return Weight value or 0 if not available.
87 int rtnl_route_nh_get_weight(struct rtnl_nexthop *nh)
89 if (nh->rtnh_mask & NEXTHOP_HAS_WEIGHT)
90 return nh->rtnh_weight;
97 * @name Attribute: Interface Index
102 * Set interface index for outgoing interface of routing nexthop.
103 * @arg nh Routing nexthop.
104 * @arg ifindex New interface index.
106 void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
108 nh->rtnh_ifindex = ifindex;
109 nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
113 * Get interface index of outgoing index of routing nexthop.
114 * @arg nh Routing nexthop.
115 * @return Interface index or -1 if not available.
117 int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
119 if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
120 return nh->rtnh_ifindex;
127 * @name Attribute: Gateway Address
132 * Set gateway address of routing nexthop.
133 * @arg nh Routing nexthop.
134 * @arg addr New gateway address.
136 * An eventual existing gateway address will be freed and a
137 * reference is acquried of the new address.
139 void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
141 struct nl_addr *old = nh->rtnh_gateway;
143 nh->rtnh_gateway = nl_addr_get(addr);
147 nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
151 * Get gateway address of routing nexthop.
152 * @arg nh Routing nexthop.
153 * @return Gateway address or NULL if not available.
155 struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
157 if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
158 return nh->rtnh_gateway;
165 * @name Attribute: Flags
170 * Set flags of routing nexthop.
171 * @arg nh Routing nexthop.
172 * @arg flags Flags to be set.
174 void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
176 nh->rtnh_flag_mask |= flags;
177 nh->rtnh_flags |= flags;
178 nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
182 * Unset flags of routing nexthop.
183 * @arg nh Routing nexthop.
184 * @arg flags Flags to be unset.
186 void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
188 nh->rtnh_flag_mask |= flags;
189 nh->rtnh_flags &= ~flags;
190 nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
194 * Get flags of routing nexthop.
195 * @arg nh Routing nexthop.
196 * @return Flags or 0 if not available.
198 unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
200 if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
201 return nh->rtnh_flags;