@@ -190,3 +190,117 @@ struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
190190 return tot ;
191191}
192192EXPORT_SYMBOL_GPL (ip_tunnel_get_stats64 );
193+
194+ static const struct nla_policy ip_tun_policy [IP_TUN_MAX + 1 ] = {
195+ [IP_TUN_ID ] = { .type = NLA_U64 },
196+ [IP_TUN_DST ] = { .type = NLA_U32 },
197+ [IP_TUN_SRC ] = { .type = NLA_U32 },
198+ [IP_TUN_TTL ] = { .type = NLA_U8 },
199+ [IP_TUN_TOS ] = { .type = NLA_U8 },
200+ [IP_TUN_SPORT ] = { .type = NLA_U16 },
201+ [IP_TUN_DPORT ] = { .type = NLA_U16 },
202+ [IP_TUN_FLAGS ] = { .type = NLA_U16 },
203+ };
204+
205+ static int ip_tun_build_state (struct net_device * dev , struct nlattr * attr ,
206+ struct lwtunnel_state * * ts )
207+ {
208+ struct ip_tunnel_info * tun_info ;
209+ struct lwtunnel_state * new_state ;
210+ struct nlattr * tb [IP_TUN_MAX + 1 ];
211+ int err ;
212+
213+ err = nla_parse_nested (tb , IP_TUN_MAX , attr , ip_tun_policy );
214+ if (err < 0 )
215+ return err ;
216+
217+ new_state = lwtunnel_state_alloc (sizeof (* tun_info ));
218+ if (!new_state )
219+ return - ENOMEM ;
220+
221+ new_state -> type = LWTUNNEL_ENCAP_IP ;
222+
223+ tun_info = lwt_tun_info (new_state );
224+
225+ if (tb [IP_TUN_ID ])
226+ tun_info -> key .tun_id = nla_get_u64 (tb [IP_TUN_ID ]);
227+
228+ if (tb [IP_TUN_DST ])
229+ tun_info -> key .ipv4_dst = nla_get_be32 (tb [IP_TUN_DST ]);
230+
231+ if (tb [IP_TUN_SRC ])
232+ tun_info -> key .ipv4_src = nla_get_be32 (tb [IP_TUN_SRC ]);
233+
234+ if (tb [IP_TUN_TTL ])
235+ tun_info -> key .ipv4_ttl = nla_get_u8 (tb [IP_TUN_TTL ]);
236+
237+ if (tb [IP_TUN_TOS ])
238+ tun_info -> key .ipv4_tos = nla_get_u8 (tb [IP_TUN_TOS ]);
239+
240+ if (tb [IP_TUN_SPORT ])
241+ tun_info -> key .tp_src = nla_get_be16 (tb [IP_TUN_SPORT ]);
242+
243+ if (tb [IP_TUN_DPORT ])
244+ tun_info -> key .tp_dst = nla_get_be16 (tb [IP_TUN_DPORT ]);
245+
246+ if (tb [IP_TUN_FLAGS ])
247+ tun_info -> key .tun_flags = nla_get_u16 (tb [IP_TUN_FLAGS ]);
248+
249+ tun_info -> mode = IP_TUNNEL_INFO_TX ;
250+ tun_info -> options = NULL ;
251+ tun_info -> options_len = 0 ;
252+
253+ * ts = new_state ;
254+
255+ return 0 ;
256+ }
257+
258+ static int ip_tun_fill_encap_info (struct sk_buff * skb ,
259+ struct lwtunnel_state * lwtstate )
260+ {
261+ struct ip_tunnel_info * tun_info = lwt_tun_info (lwtstate );
262+
263+ if (nla_put_u64 (skb , IP_TUN_ID , tun_info -> key .tun_id ) ||
264+ nla_put_be32 (skb , IP_TUN_DST , tun_info -> key .ipv4_dst ) ||
265+ nla_put_be32 (skb , IP_TUN_SRC , tun_info -> key .ipv4_src ) ||
266+ nla_put_u8 (skb , IP_TUN_TOS , tun_info -> key .ipv4_tos ) ||
267+ nla_put_u8 (skb , IP_TUN_TTL , tun_info -> key .ipv4_ttl ) ||
268+ nla_put_u16 (skb , IP_TUN_SPORT , tun_info -> key .tp_src ) ||
269+ nla_put_u16 (skb , IP_TUN_DPORT , tun_info -> key .tp_dst ) ||
270+ nla_put_u16 (skb , IP_TUN_FLAGS , tun_info -> key .tun_flags ))
271+ return - ENOMEM ;
272+
273+ return 0 ;
274+ }
275+
276+ static int ip_tun_encap_nlsize (struct lwtunnel_state * lwtstate )
277+ {
278+ return nla_total_size (8 ) /* IP_TUN_ID */
279+ + nla_total_size (4 ) /* IP_TUN_DST */
280+ + nla_total_size (4 ) /* IP_TUN_SRC */
281+ + nla_total_size (1 ) /* IP_TUN_TOS */
282+ + nla_total_size (1 ) /* IP_TUN_TTL */
283+ + nla_total_size (2 ) /* IP_TUN_SPORT */
284+ + nla_total_size (2 ) /* IP_TUN_DPORT */
285+ + nla_total_size (2 ); /* IP_TUN_FLAGS */
286+ }
287+
288+ static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
289+ .build_state = ip_tun_build_state ,
290+ .fill_encap = ip_tun_fill_encap_info ,
291+ .get_encap_size = ip_tun_encap_nlsize ,
292+ };
293+
294+ static int __init ip_tunnel_core_init (void )
295+ {
296+ lwtunnel_encap_add_ops (& ip_tun_lwt_ops , LWTUNNEL_ENCAP_IP );
297+
298+ return 0 ;
299+ }
300+ module_init (ip_tunnel_core_init );
301+
302+ static void __exit ip_tunnel_core_exit (void )
303+ {
304+ lwtunnel_encap_del_ops (& ip_tun_lwt_ops , LWTUNNEL_ENCAP_IP );
305+ }
306+ module_exit (ip_tunnel_core_exit );
0 commit comments