77
88#include <netlink/route/nexthop.h>
99
10- #include "nl-route.h"
1110#include "nexthop-encap.h"
11+ #include "nl-aux-core/nl-core.h"
12+ #include "nl-aux-route/nl-route.h"
13+ #include "nl-route.h"
1214
1315struct mpls_iptunnel_encap {
1416 struct nl_addr * dst ;
@@ -54,8 +56,9 @@ static struct nla_policy mpls_encap_policy[MPLS_IPTUNNEL_MAX + 1] = {
5456
5557static int mpls_encap_parse_msg (struct nlattr * nla , struct rtnl_nexthop * nh )
5658{
59+ _nl_auto_rtnl_nh_encap struct rtnl_nh_encap * nh_encap = NULL ;
60+ _nl_auto_nl_addr struct nl_addr * labels = NULL ;
5761 struct nlattr * tb [MPLS_IPTUNNEL_MAX + 1 ];
58- struct nl_addr * labels ;
5962 uint8_t ttl = 0 ;
6063 int err ;
6164
@@ -73,11 +76,19 @@ static int mpls_encap_parse_msg(struct nlattr *nla, struct rtnl_nexthop *nh)
7376 if (tb [MPLS_IPTUNNEL_TTL ])
7477 ttl = nla_get_u8 (tb [MPLS_IPTUNNEL_TTL ]);
7578
76- err = rtnl_route_nh_encap_mpls (nh , labels , ttl );
79+ nh_encap = rtnl_nh_encap_alloc ();
80+ if (!nh_encap )
81+ return - NLE_NOMEM ;
7782
78- nl_addr_put (labels );
83+ err = rtnl_nh_encap_mpls (nh_encap , labels , ttl );
84+ if (err < 0 )
85+ return err ;
7986
80- return err ;
87+ err = rtnl_route_nh_set_encap (nh , _nl_steal_pointer (& nh_encap ));
88+ if (err < 0 )
89+ return err ;
90+
91+ return 0 ;
8192}
8293
8394static int mpls_encap_compare (void * _a , void * _b )
@@ -101,62 +112,86 @@ struct nh_encap_ops mpls_encap_ops = {
101112 .destructor = mpls_encap_destructor ,
102113};
103114
104- int rtnl_route_nh_encap_mpls (struct rtnl_nexthop * nh , struct nl_addr * addr ,
105- uint8_t ttl )
115+ int rtnl_nh_encap_mpls (struct rtnl_nh_encap * nh_encap , struct nl_addr * dst ,
116+ uint8_t ttl )
106117{
107118 struct mpls_iptunnel_encap * mpls_encap ;
108- struct rtnl_nh_encap * rtnh_encap ;
109119
110- if (!addr )
120+ if (!dst || ! nh_encap )
111121 return - NLE_INVAL ;
112122
113- rtnh_encap = calloc (1 , sizeof (* rtnh_encap ));
114- if (!rtnh_encap )
115- return - NLE_NOMEM ;
116-
117123 mpls_encap = calloc (1 , sizeof (* mpls_encap ));
118124 if (!mpls_encap ) {
119- free (rtnh_encap );
120125 return - NLE_NOMEM ;
121126 }
122127
123- mpls_encap -> dst = nl_addr_get (addr );
128+ mpls_encap -> dst = nl_addr_get (dst );
124129 mpls_encap -> ttl = ttl ;
125130
126- rtnh_encap -> priv = mpls_encap ;
127- rtnh_encap -> ops = & mpls_encap_ops ;
131+ nh_encap -> priv = mpls_encap ;
132+ nh_encap -> ops = & mpls_encap_ops ;
133+
134+ return 0 ;
135+ }
128136
129- nh_set_encap (nh , rtnh_encap );
137+ int rtnl_route_nh_encap_mpls (struct rtnl_nexthop * nh , struct nl_addr * addr ,
138+ uint8_t ttl )
139+ {
140+ _nl_auto_rtnl_nh_encap struct rtnl_nh_encap * rtnh_encap = NULL ;
141+ int ret ;
142+
143+ rtnh_encap = rtnl_nh_encap_alloc ();
144+ if (!rtnh_encap )
145+ return - NLE_NOMEM ;
146+
147+ ret = rtnl_nh_encap_mpls (rtnh_encap , addr , ttl );
148+ if (ret < 0 )
149+ return ret ;
150+
151+ rtnl_route_nh_set_encap (nh , _nl_steal_pointer (& rtnh_encap ));
130152
131153 return 0 ;
132154}
133155
134- struct nl_addr * rtnl_route_nh_get_encap_mpls_dst (struct rtnl_nexthop * nh )
156+ struct nl_addr * rtnl_nh_get_encap_mpls_dst (struct rtnl_nh_encap * nh_encap )
135157{
136158 struct mpls_iptunnel_encap * mpls_encap ;
137159
138- if (!nh -> rtnh_encap ||
139- nh -> rtnh_encap -> ops -> encap_type != LWTUNNEL_ENCAP_MPLS )
160+ if (!nh_encap || nh_encap -> ops -> encap_type != LWTUNNEL_ENCAP_MPLS )
140161 return NULL ;
141162
142- mpls_encap = (struct mpls_iptunnel_encap * )nh -> rtnh_encap -> priv ;
163+ mpls_encap = (struct mpls_iptunnel_encap * )nh_encap -> priv ;
143164 if (!mpls_encap )
144165 return NULL ;
145166
146167 return mpls_encap -> dst ;
147168}
148169
149- uint8_t rtnl_route_nh_get_encap_mpls_ttl (struct rtnl_nexthop * nh )
170+ struct nl_addr * rtnl_route_nh_get_encap_mpls_dst (struct rtnl_nexthop * nh )
171+ {
172+ return rtnl_nh_get_encap_mpls_dst (nh -> rtnh_encap );
173+ }
174+
175+ int rtnl_nh_get_encap_mpls_ttl (struct rtnl_nh_encap * nh_encap )
150176{
151177 struct mpls_iptunnel_encap * mpls_encap ;
152178
153- if (!nh -> rtnh_encap ||
154- nh -> rtnh_encap -> ops -> encap_type != LWTUNNEL_ENCAP_MPLS )
155- return 0 ;
179+ if (!nh_encap || ! nh_encap -> ops ||
180+ nh_encap -> ops -> encap_type != LWTUNNEL_ENCAP_MPLS )
181+ return - NLE_INVAL ;
156182
157- mpls_encap = (struct mpls_iptunnel_encap * )nh -> rtnh_encap -> priv ;
183+ mpls_encap = (struct mpls_iptunnel_encap * )nh_encap -> priv ;
158184 if (!mpls_encap )
159- return 0 ;
185+ return - NLE_INVAL ;
160186
161187 return mpls_encap -> ttl ;
162188}
189+
190+ uint8_t rtnl_route_nh_get_encap_mpls_ttl (struct rtnl_nexthop * nh )
191+ {
192+ int ttl = rtnl_nh_get_encap_mpls_ttl (nh -> rtnh_encap );
193+
194+ if (ttl < 0 )
195+ return 0 ;
196+ return (uint8_t )ttl ;
197+ }
0 commit comments