nft_tunnel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/seqlock.h>
  6. #include <linux/netlink.h>
  7. #include <linux/netfilter.h>
  8. #include <linux/netfilter/nf_tables.h>
  9. #include <net/netfilter/nf_tables.h>
  10. #include <net/dst_metadata.h>
  11. #include <net/ip_tunnels.h>
  12. #include <net/vxlan.h>
  13. #include <net/erspan.h>
  14. struct nft_tunnel {
  15. enum nft_tunnel_keys key:8;
  16. enum nft_registers dreg:8;
  17. };
  18. static void nft_tunnel_get_eval(const struct nft_expr *expr,
  19. struct nft_regs *regs,
  20. const struct nft_pktinfo *pkt)
  21. {
  22. const struct nft_tunnel *priv = nft_expr_priv(expr);
  23. u32 *dest = &regs->data[priv->dreg];
  24. struct ip_tunnel_info *tun_info;
  25. tun_info = skb_tunnel_info(pkt->skb);
  26. switch (priv->key) {
  27. case NFT_TUNNEL_PATH:
  28. nft_reg_store8(dest, !!tun_info);
  29. break;
  30. case NFT_TUNNEL_ID:
  31. if (!tun_info) {
  32. regs->verdict.code = NFT_BREAK;
  33. return;
  34. }
  35. *dest = ntohl(tunnel_id_to_key32(tun_info->key.tun_id));
  36. break;
  37. default:
  38. WARN_ON(1);
  39. regs->verdict.code = NFT_BREAK;
  40. }
  41. }
  42. static const struct nla_policy nft_tunnel_policy[NFTA_TUNNEL_MAX + 1] = {
  43. [NFTA_TUNNEL_KEY] = { .type = NLA_U32 },
  44. [NFTA_TUNNEL_DREG] = { .type = NLA_U32 },
  45. };
  46. static int nft_tunnel_get_init(const struct nft_ctx *ctx,
  47. const struct nft_expr *expr,
  48. const struct nlattr * const tb[])
  49. {
  50. struct nft_tunnel *priv = nft_expr_priv(expr);
  51. u32 len;
  52. if (!tb[NFTA_TUNNEL_KEY] ||
  53. !tb[NFTA_TUNNEL_DREG])
  54. return -EINVAL;
  55. priv->key = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY]));
  56. switch (priv->key) {
  57. case NFT_TUNNEL_PATH:
  58. len = sizeof(u8);
  59. break;
  60. case NFT_TUNNEL_ID:
  61. len = sizeof(u32);
  62. break;
  63. default:
  64. return -EOPNOTSUPP;
  65. }
  66. priv->dreg = nft_parse_register(tb[NFTA_TUNNEL_DREG]);
  67. return nft_validate_register_store(ctx, priv->dreg, NULL,
  68. NFT_DATA_VALUE, len);
  69. }
  70. static int nft_tunnel_get_dump(struct sk_buff *skb,
  71. const struct nft_expr *expr)
  72. {
  73. const struct nft_tunnel *priv = nft_expr_priv(expr);
  74. if (nla_put_be32(skb, NFTA_TUNNEL_KEY, htonl(priv->key)))
  75. goto nla_put_failure;
  76. if (nft_dump_register(skb, NFTA_TUNNEL_DREG, priv->dreg))
  77. goto nla_put_failure;
  78. return 0;
  79. nla_put_failure:
  80. return -1;
  81. }
  82. static struct nft_expr_type nft_tunnel_type;
  83. static const struct nft_expr_ops nft_tunnel_get_ops = {
  84. .type = &nft_tunnel_type,
  85. .size = NFT_EXPR_SIZE(sizeof(struct nft_tunnel)),
  86. .eval = nft_tunnel_get_eval,
  87. .init = nft_tunnel_get_init,
  88. .dump = nft_tunnel_get_dump,
  89. };
  90. static struct nft_expr_type nft_tunnel_type __read_mostly = {
  91. .name = "tunnel",
  92. .ops = &nft_tunnel_get_ops,
  93. .policy = nft_tunnel_policy,
  94. .maxattr = NFTA_TUNNEL_MAX,
  95. .owner = THIS_MODULE,
  96. };
  97. struct nft_tunnel_opts {
  98. union {
  99. struct vxlan_metadata vxlan;
  100. struct erspan_metadata erspan;
  101. } u;
  102. u32 len;
  103. __be16 flags;
  104. };
  105. struct nft_tunnel_obj {
  106. struct metadata_dst *md;
  107. struct nft_tunnel_opts opts;
  108. };
  109. static const struct nla_policy nft_tunnel_ip_policy[NFTA_TUNNEL_KEY_IP_MAX + 1] = {
  110. [NFTA_TUNNEL_KEY_IP_SRC] = { .type = NLA_U32 },
  111. [NFTA_TUNNEL_KEY_IP_DST] = { .type = NLA_U32 },
  112. };
  113. static int nft_tunnel_obj_ip_init(const struct nft_ctx *ctx,
  114. const struct nlattr *attr,
  115. struct ip_tunnel_info *info)
  116. {
  117. struct nlattr *tb[NFTA_TUNNEL_KEY_IP_MAX + 1];
  118. int err;
  119. err = nla_parse_nested(tb, NFTA_TUNNEL_KEY_IP_MAX, attr,
  120. nft_tunnel_ip_policy, NULL);
  121. if (err < 0)
  122. return err;
  123. if (!tb[NFTA_TUNNEL_KEY_IP_DST])
  124. return -EINVAL;
  125. if (tb[NFTA_TUNNEL_KEY_IP_SRC])
  126. info->key.u.ipv4.src = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP_SRC]);
  127. if (tb[NFTA_TUNNEL_KEY_IP_DST])
  128. info->key.u.ipv4.dst = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP_DST]);
  129. return 0;
  130. }
  131. static const struct nla_policy nft_tunnel_ip6_policy[NFTA_TUNNEL_KEY_IP6_MAX + 1] = {
  132. [NFTA_TUNNEL_KEY_IP6_SRC] = { .len = sizeof(struct in6_addr), },
  133. [NFTA_TUNNEL_KEY_IP6_DST] = { .len = sizeof(struct in6_addr), },
  134. [NFTA_TUNNEL_KEY_IP6_FLOWLABEL] = { .type = NLA_U32, }
  135. };
  136. static int nft_tunnel_obj_ip6_init(const struct nft_ctx *ctx,
  137. const struct nlattr *attr,
  138. struct ip_tunnel_info *info)
  139. {
  140. struct nlattr *tb[NFTA_TUNNEL_KEY_IP6_MAX + 1];
  141. int err;
  142. err = nla_parse_nested(tb, NFTA_TUNNEL_KEY_IP6_MAX, attr,
  143. nft_tunnel_ip6_policy, NULL);
  144. if (err < 0)
  145. return err;
  146. if (!tb[NFTA_TUNNEL_KEY_IP6_DST])
  147. return -EINVAL;
  148. if (tb[NFTA_TUNNEL_KEY_IP6_SRC]) {
  149. memcpy(&info->key.u.ipv6.src,
  150. nla_data(tb[NFTA_TUNNEL_KEY_IP6_SRC]),
  151. sizeof(struct in6_addr));
  152. }
  153. if (tb[NFTA_TUNNEL_KEY_IP6_DST]) {
  154. memcpy(&info->key.u.ipv6.dst,
  155. nla_data(tb[NFTA_TUNNEL_KEY_IP6_DST]),
  156. sizeof(struct in6_addr));
  157. }
  158. if (tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL])
  159. info->key.label = nla_get_be32(tb[NFTA_TUNNEL_KEY_IP6_FLOWLABEL]);
  160. info->mode |= IP_TUNNEL_INFO_IPV6;
  161. return 0;
  162. }
  163. static const struct nla_policy nft_tunnel_opts_vxlan_policy[NFTA_TUNNEL_KEY_VXLAN_MAX + 1] = {
  164. [NFTA_TUNNEL_KEY_VXLAN_GBP] = { .type = NLA_U32 },
  165. };
  166. static int nft_tunnel_obj_vxlan_init(const struct nlattr *attr,
  167. struct nft_tunnel_opts *opts)
  168. {
  169. struct nlattr *tb[NFTA_TUNNEL_KEY_VXLAN_MAX + 1];
  170. int err;
  171. err = nla_parse_nested(tb, NFTA_TUNNEL_KEY_VXLAN_MAX, attr,
  172. nft_tunnel_opts_vxlan_policy, NULL);
  173. if (err < 0)
  174. return err;
  175. if (!tb[NFTA_TUNNEL_KEY_VXLAN_GBP])
  176. return -EINVAL;
  177. opts->u.vxlan.gbp = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_VXLAN_GBP]));
  178. opts->len = sizeof(struct vxlan_metadata);
  179. opts->flags = TUNNEL_VXLAN_OPT;
  180. return 0;
  181. }
  182. static const struct nla_policy nft_tunnel_opts_erspan_policy[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1] = {
  183. [NFTA_TUNNEL_KEY_ERSPAN_VERSION] = { .type = NLA_U32 },
  184. [NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX] = { .type = NLA_U32 },
  185. [NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] = { .type = NLA_U8 },
  186. [NFTA_TUNNEL_KEY_ERSPAN_V2_HWID] = { .type = NLA_U8 },
  187. };
  188. static int nft_tunnel_obj_erspan_init(const struct nlattr *attr,
  189. struct nft_tunnel_opts *opts)
  190. {
  191. struct nlattr *tb[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1];
  192. uint8_t hwid, dir;
  193. int err, version;
  194. err = nla_parse_nested(tb, NFTA_TUNNEL_KEY_ERSPAN_MAX, attr,
  195. nft_tunnel_opts_erspan_policy, NULL);
  196. if (err < 0)
  197. return err;
  198. if (!tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION])
  199. return -EINVAL;
  200. version = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_VERSION]));
  201. switch (version) {
  202. case ERSPAN_VERSION:
  203. if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX])
  204. return -EINVAL;
  205. opts->u.erspan.u.index =
  206. nla_get_be32(tb[NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX]);
  207. break;
  208. case ERSPAN_VERSION2:
  209. if (!tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] ||
  210. !tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID])
  211. return -EINVAL;
  212. hwid = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_HWID]);
  213. dir = nla_get_u8(tb[NFTA_TUNNEL_KEY_ERSPAN_V2_DIR]);
  214. set_hwid(&opts->u.erspan.u.md2, hwid);
  215. opts->u.erspan.u.md2.dir = dir;
  216. break;
  217. default:
  218. return -EOPNOTSUPP;
  219. }
  220. opts->u.erspan.version = version;
  221. opts->len = sizeof(struct erspan_metadata);
  222. opts->flags = TUNNEL_ERSPAN_OPT;
  223. return 0;
  224. }
  225. static const struct nla_policy nft_tunnel_opts_policy[NFTA_TUNNEL_KEY_OPTS_MAX + 1] = {
  226. [NFTA_TUNNEL_KEY_OPTS_VXLAN] = { .type = NLA_NESTED, },
  227. [NFTA_TUNNEL_KEY_OPTS_ERSPAN] = { .type = NLA_NESTED, },
  228. };
  229. static int nft_tunnel_obj_opts_init(const struct nft_ctx *ctx,
  230. const struct nlattr *attr,
  231. struct ip_tunnel_info *info,
  232. struct nft_tunnel_opts *opts)
  233. {
  234. struct nlattr *tb[NFTA_TUNNEL_KEY_OPTS_MAX + 1];
  235. int err;
  236. err = nla_parse_nested(tb, NFTA_TUNNEL_KEY_OPTS_MAX, attr,
  237. nft_tunnel_opts_policy, NULL);
  238. if (err < 0)
  239. return err;
  240. if (tb[NFTA_TUNNEL_KEY_OPTS_VXLAN]) {
  241. err = nft_tunnel_obj_vxlan_init(tb[NFTA_TUNNEL_KEY_OPTS_VXLAN],
  242. opts);
  243. } else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
  244. err = nft_tunnel_obj_erspan_init(tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
  245. opts);
  246. } else {
  247. return -EOPNOTSUPP;
  248. }
  249. return err;
  250. }
  251. static const struct nla_policy nft_tunnel_key_policy[NFTA_TUNNEL_KEY_MAX + 1] = {
  252. [NFTA_TUNNEL_KEY_IP] = { .type = NLA_NESTED, },
  253. [NFTA_TUNNEL_KEY_IP6] = { .type = NLA_NESTED, },
  254. [NFTA_TUNNEL_KEY_ID] = { .type = NLA_U32, },
  255. [NFTA_TUNNEL_KEY_FLAGS] = { .type = NLA_U32, },
  256. [NFTA_TUNNEL_KEY_TOS] = { .type = NLA_U8, },
  257. [NFTA_TUNNEL_KEY_TTL] = { .type = NLA_U8, },
  258. [NFTA_TUNNEL_KEY_SPORT] = { .type = NLA_U16, },
  259. [NFTA_TUNNEL_KEY_DPORT] = { .type = NLA_U16, },
  260. [NFTA_TUNNEL_KEY_OPTS] = { .type = NLA_NESTED, },
  261. };
  262. static int nft_tunnel_obj_init(const struct nft_ctx *ctx,
  263. const struct nlattr * const tb[],
  264. struct nft_object *obj)
  265. {
  266. struct nft_tunnel_obj *priv = nft_obj_data(obj);
  267. struct ip_tunnel_info info;
  268. struct metadata_dst *md;
  269. int err;
  270. if (!tb[NFTA_TUNNEL_KEY_ID])
  271. return -EINVAL;
  272. memset(&info, 0, sizeof(info));
  273. info.mode = IP_TUNNEL_INFO_TX;
  274. info.key.tun_id = key32_to_tunnel_id(nla_get_be32(tb[NFTA_TUNNEL_KEY_ID]));
  275. info.key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
  276. if (tb[NFTA_TUNNEL_KEY_IP]) {
  277. err = nft_tunnel_obj_ip_init(ctx, tb[NFTA_TUNNEL_KEY_IP], &info);
  278. if (err < 0)
  279. return err;
  280. } else if (tb[NFTA_TUNNEL_KEY_IP6]) {
  281. err = nft_tunnel_obj_ip6_init(ctx, tb[NFTA_TUNNEL_KEY_IP6], &info);
  282. if (err < 0)
  283. return err;
  284. } else {
  285. return -EINVAL;
  286. }
  287. if (tb[NFTA_TUNNEL_KEY_SPORT]) {
  288. info.key.tp_src = nla_get_be16(tb[NFTA_TUNNEL_KEY_SPORT]);
  289. }
  290. if (tb[NFTA_TUNNEL_KEY_DPORT]) {
  291. info.key.tp_dst = nla_get_be16(tb[NFTA_TUNNEL_KEY_DPORT]);
  292. }
  293. if (tb[NFTA_TUNNEL_KEY_FLAGS]) {
  294. u32 tun_flags;
  295. tun_flags = ntohl(nla_get_be32(tb[NFTA_TUNNEL_KEY_FLAGS]));
  296. if (tun_flags & ~NFT_TUNNEL_F_MASK)
  297. return -EOPNOTSUPP;
  298. if (tun_flags & NFT_TUNNEL_F_ZERO_CSUM_TX)
  299. info.key.tun_flags &= ~TUNNEL_CSUM;
  300. if (tun_flags & NFT_TUNNEL_F_DONT_FRAGMENT)
  301. info.key.tun_flags |= TUNNEL_DONT_FRAGMENT;
  302. if (tun_flags & NFT_TUNNEL_F_SEQ_NUMBER)
  303. info.key.tun_flags |= TUNNEL_SEQ;
  304. }
  305. if (tb[NFTA_TUNNEL_KEY_TOS])
  306. info.key.tos = nla_get_u8(tb[NFTA_TUNNEL_KEY_TOS]);
  307. if (tb[NFTA_TUNNEL_KEY_TTL])
  308. info.key.ttl = nla_get_u8(tb[NFTA_TUNNEL_KEY_TTL]);
  309. else
  310. info.key.ttl = U8_MAX;
  311. if (tb[NFTA_TUNNEL_KEY_OPTS]) {
  312. err = nft_tunnel_obj_opts_init(ctx, tb[NFTA_TUNNEL_KEY_OPTS],
  313. &info, &priv->opts);
  314. if (err < 0)
  315. return err;
  316. }
  317. md = metadata_dst_alloc(priv->opts.len, METADATA_IP_TUNNEL, GFP_KERNEL);
  318. if (!md)
  319. return -ENOMEM;
  320. memcpy(&md->u.tun_info, &info, sizeof(info));
  321. ip_tunnel_info_opts_set(&md->u.tun_info, &priv->opts.u, priv->opts.len,
  322. priv->opts.flags);
  323. priv->md = md;
  324. return 0;
  325. }
  326. static inline void nft_tunnel_obj_eval(struct nft_object *obj,
  327. struct nft_regs *regs,
  328. const struct nft_pktinfo *pkt)
  329. {
  330. struct nft_tunnel_obj *priv = nft_obj_data(obj);
  331. struct sk_buff *skb = pkt->skb;
  332. skb_dst_drop(skb);
  333. dst_hold((struct dst_entry *) priv->md);
  334. skb_dst_set(skb, (struct dst_entry *) priv->md);
  335. }
  336. static int nft_tunnel_ip_dump(struct sk_buff *skb, struct ip_tunnel_info *info)
  337. {
  338. struct nlattr *nest;
  339. if (info->mode & IP_TUNNEL_INFO_IPV6) {
  340. nest = nla_nest_start(skb, NFTA_TUNNEL_KEY_IP6);
  341. if (!nest)
  342. return -1;
  343. if (nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_SRC, &info->key.u.ipv6.src) < 0 ||
  344. nla_put_in6_addr(skb, NFTA_TUNNEL_KEY_IP6_DST, &info->key.u.ipv6.dst) < 0 ||
  345. nla_put_be32(skb, NFTA_TUNNEL_KEY_IP6_FLOWLABEL, info->key.label))
  346. return -1;
  347. nla_nest_end(skb, nest);
  348. } else {
  349. nest = nla_nest_start(skb, NFTA_TUNNEL_KEY_IP);
  350. if (!nest)
  351. return -1;
  352. if (nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_SRC, info->key.u.ipv4.src) < 0 ||
  353. nla_put_in_addr(skb, NFTA_TUNNEL_KEY_IP_DST, info->key.u.ipv4.dst) < 0)
  354. return -1;
  355. nla_nest_end(skb, nest);
  356. }
  357. return 0;
  358. }
  359. static int nft_tunnel_opts_dump(struct sk_buff *skb,
  360. struct nft_tunnel_obj *priv)
  361. {
  362. struct nft_tunnel_opts *opts = &priv->opts;
  363. struct nlattr *nest;
  364. nest = nla_nest_start(skb, NFTA_TUNNEL_KEY_OPTS);
  365. if (!nest)
  366. return -1;
  367. if (opts->flags & TUNNEL_VXLAN_OPT) {
  368. if (nla_put_be32(skb, NFTA_TUNNEL_KEY_VXLAN_GBP,
  369. htonl(opts->u.vxlan.gbp)))
  370. return -1;
  371. } else if (opts->flags & TUNNEL_ERSPAN_OPT) {
  372. switch (opts->u.erspan.version) {
  373. case ERSPAN_VERSION:
  374. if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX,
  375. opts->u.erspan.u.index))
  376. return -1;
  377. break;
  378. case ERSPAN_VERSION2:
  379. if (nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_HWID,
  380. get_hwid(&opts->u.erspan.u.md2)) ||
  381. nla_put_u8(skb, NFTA_TUNNEL_KEY_ERSPAN_V2_DIR,
  382. opts->u.erspan.u.md2.dir))
  383. return -1;
  384. break;
  385. }
  386. }
  387. nla_nest_end(skb, nest);
  388. return 0;
  389. }
  390. static int nft_tunnel_ports_dump(struct sk_buff *skb,
  391. struct ip_tunnel_info *info)
  392. {
  393. if (nla_put_be16(skb, NFTA_TUNNEL_KEY_SPORT, info->key.tp_src) < 0 ||
  394. nla_put_be16(skb, NFTA_TUNNEL_KEY_DPORT, info->key.tp_dst) < 0)
  395. return -1;
  396. return 0;
  397. }
  398. static int nft_tunnel_flags_dump(struct sk_buff *skb,
  399. struct ip_tunnel_info *info)
  400. {
  401. u32 flags = 0;
  402. if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT)
  403. flags |= NFT_TUNNEL_F_DONT_FRAGMENT;
  404. if (!(info->key.tun_flags & TUNNEL_CSUM))
  405. flags |= NFT_TUNNEL_F_ZERO_CSUM_TX;
  406. if (info->key.tun_flags & TUNNEL_SEQ)
  407. flags |= NFT_TUNNEL_F_SEQ_NUMBER;
  408. if (nla_put_be32(skb, NFTA_TUNNEL_KEY_FLAGS, htonl(flags)) < 0)
  409. return -1;
  410. return 0;
  411. }
  412. static int nft_tunnel_obj_dump(struct sk_buff *skb,
  413. struct nft_object *obj, bool reset)
  414. {
  415. struct nft_tunnel_obj *priv = nft_obj_data(obj);
  416. struct ip_tunnel_info *info = &priv->md->u.tun_info;
  417. if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ID,
  418. tunnel_id_to_key32(info->key.tun_id)) ||
  419. nft_tunnel_ip_dump(skb, info) < 0 ||
  420. nft_tunnel_ports_dump(skb, info) < 0 ||
  421. nft_tunnel_flags_dump(skb, info) < 0 ||
  422. nla_put_u8(skb, NFTA_TUNNEL_KEY_TOS, info->key.tos) ||
  423. nla_put_u8(skb, NFTA_TUNNEL_KEY_TTL, info->key.ttl) ||
  424. nft_tunnel_opts_dump(skb, priv) < 0)
  425. goto nla_put_failure;
  426. return 0;
  427. nla_put_failure:
  428. return -1;
  429. }
  430. static void nft_tunnel_obj_destroy(const struct nft_ctx *ctx,
  431. struct nft_object *obj)
  432. {
  433. struct nft_tunnel_obj *priv = nft_obj_data(obj);
  434. metadata_dst_free(priv->md);
  435. }
  436. static struct nft_object_type nft_tunnel_obj_type;
  437. static const struct nft_object_ops nft_tunnel_obj_ops = {
  438. .type = &nft_tunnel_obj_type,
  439. .size = sizeof(struct nft_tunnel_obj),
  440. .eval = nft_tunnel_obj_eval,
  441. .init = nft_tunnel_obj_init,
  442. .destroy = nft_tunnel_obj_destroy,
  443. .dump = nft_tunnel_obj_dump,
  444. };
  445. static struct nft_object_type nft_tunnel_obj_type __read_mostly = {
  446. .type = NFT_OBJECT_TUNNEL,
  447. .ops = &nft_tunnel_obj_ops,
  448. .maxattr = NFTA_TUNNEL_KEY_MAX,
  449. .policy = nft_tunnel_key_policy,
  450. .owner = THIS_MODULE,
  451. };
  452. static int __init nft_tunnel_module_init(void)
  453. {
  454. int err;
  455. err = nft_register_expr(&nft_tunnel_type);
  456. if (err < 0)
  457. return err;
  458. err = nft_register_obj(&nft_tunnel_obj_type);
  459. if (err < 0)
  460. nft_unregister_expr(&nft_tunnel_type);
  461. return err;
  462. }
  463. static void __exit nft_tunnel_module_exit(void)
  464. {
  465. nft_unregister_obj(&nft_tunnel_obj_type);
  466. nft_unregister_expr(&nft_tunnel_type);
  467. }
  468. module_init(nft_tunnel_module_init);
  469. module_exit(nft_tunnel_module_exit);
  470. MODULE_LICENSE("GPL");
  471. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  472. MODULE_ALIAS_NFT_EXPR("tunnel");
  473. MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_TUNNEL);