@@ -111,8 +111,32 @@ static u32 HASH_ADDR(const struct in6_addr *addr)
111111#define tunnels_l tunnels[1]
112112#define tunnels_wc tunnels[0]
113113
114- /* Given src, dst and key, find appropriate for input tunnel. */
114+ static bool ip6gre_tunnel_match (struct ip6_tnl * t , int dev_type , int link ,
115+ int * cand_score , struct ip6_tnl * * ret )
116+ {
117+ int score = 0 ;
118+
119+ if (t -> dev -> type != ARPHRD_IP6GRE &&
120+ t -> dev -> type != dev_type )
121+ return false;
122+
123+ if (t -> parms .link != link )
124+ score |= 1 ;
125+ if (t -> dev -> type != dev_type )
126+ score |= 2 ;
127+ if (score == 0 ) {
128+ * ret = t ;
129+ return true;
130+ }
131+
132+ if (score < * cand_score ) {
133+ * ret = t ;
134+ * cand_score = score ;
135+ }
136+ return false;
137+ }
115138
139+ /* Given src, dst and key, find appropriate for input tunnel. */
116140static struct ip6_tnl * ip6gre_tunnel_lookup (struct net_device * dev ,
117141 const struct in6_addr * remote , const struct in6_addr * local ,
118142 __be32 key , __be16 gre_proto )
@@ -127,8 +151,8 @@ static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
127151 gre_proto == htons (ETH_P_ERSPAN ) ||
128152 gre_proto == htons (ETH_P_ERSPAN2 )) ?
129153 ARPHRD_ETHER : ARPHRD_IP6GRE ;
130- int score , cand_score = 4 ;
131154 struct net_device * ndev ;
155+ int cand_score = 4 ;
132156
133157 for_each_ip_tunnel_rcu (t , ign -> tunnels_r_l [h0 ^ h1 ]) {
134158 if (!ipv6_addr_equal (local , & t -> parms .laddr ) ||
@@ -137,22 +161,8 @@ static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
137161 !(t -> dev -> flags & IFF_UP ))
138162 continue ;
139163
140- if (t -> dev -> type != ARPHRD_IP6GRE &&
141- t -> dev -> type != dev_type )
142- continue ;
143-
144- score = 0 ;
145- if (t -> parms .link != link )
146- score |= 1 ;
147- if (t -> dev -> type != dev_type )
148- score |= 2 ;
149- if (score == 0 )
150- return t ;
151-
152- if (score < cand_score ) {
153- cand = t ;
154- cand_score = score ;
155- }
164+ if (ip6gre_tunnel_match (t , dev_type , link , & cand_score , & cand ))
165+ return cand ;
156166 }
157167
158168 for_each_ip_tunnel_rcu (t , ign -> tunnels_r [h0 ^ h1 ]) {
@@ -161,22 +171,8 @@ static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
161171 !(t -> dev -> flags & IFF_UP ))
162172 continue ;
163173
164- if (t -> dev -> type != ARPHRD_IP6GRE &&
165- t -> dev -> type != dev_type )
166- continue ;
167-
168- score = 0 ;
169- if (t -> parms .link != link )
170- score |= 1 ;
171- if (t -> dev -> type != dev_type )
172- score |= 2 ;
173- if (score == 0 )
174- return t ;
175-
176- if (score < cand_score ) {
177- cand = t ;
178- cand_score = score ;
179- }
174+ if (ip6gre_tunnel_match (t , dev_type , link , & cand_score , & cand ))
175+ return cand ;
180176 }
181177
182178 for_each_ip_tunnel_rcu (t , ign -> tunnels_l [h1 ]) {
@@ -187,45 +183,17 @@ static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
187183 !(t -> dev -> flags & IFF_UP ))
188184 continue ;
189185
190- if (t -> dev -> type != ARPHRD_IP6GRE &&
191- t -> dev -> type != dev_type )
192- continue ;
193-
194- score = 0 ;
195- if (t -> parms .link != link )
196- score |= 1 ;
197- if (t -> dev -> type != dev_type )
198- score |= 2 ;
199- if (score == 0 )
200- return t ;
201-
202- if (score < cand_score ) {
203- cand = t ;
204- cand_score = score ;
205- }
186+ if (ip6gre_tunnel_match (t , dev_type , link , & cand_score , & cand ))
187+ return cand ;
206188 }
207189
208190 for_each_ip_tunnel_rcu (t , ign -> tunnels_wc [h1 ]) {
209191 if (t -> parms .i_key != key ||
210192 !(t -> dev -> flags & IFF_UP ))
211193 continue ;
212194
213- if (t -> dev -> type != ARPHRD_IP6GRE &&
214- t -> dev -> type != dev_type )
215- continue ;
216-
217- score = 0 ;
218- if (t -> parms .link != link )
219- score |= 1 ;
220- if (t -> dev -> type != dev_type )
221- score |= 2 ;
222- if (score == 0 )
223- return t ;
224-
225- if (score < cand_score ) {
226- cand = t ;
227- cand_score = score ;
228- }
195+ if (ip6gre_tunnel_match (t , dev_type , link , & cand_score , & cand ))
196+ return cand ;
229197 }
230198
231199 if (cand )
0 commit comments