tunnels.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/ethtool_netlink.h>
  3. #include <net/udp_tunnel.h>
  4. #include <net/vxlan.h>
  5. #include "bitset.h"
  6. #include "common.h"
  7. #include "netlink.h"
  8. const struct nla_policy ethnl_tunnel_info_get_policy[] = {
  9. [ETHTOOL_A_TUNNEL_INFO_HEADER] =
  10. NLA_POLICY_NESTED(ethnl_header_policy),
  11. };
  12. static_assert(ETHTOOL_UDP_TUNNEL_TYPE_VXLAN == ilog2(UDP_TUNNEL_TYPE_VXLAN));
  13. static_assert(ETHTOOL_UDP_TUNNEL_TYPE_GENEVE == ilog2(UDP_TUNNEL_TYPE_GENEVE));
  14. static_assert(ETHTOOL_UDP_TUNNEL_TYPE_VXLAN_GPE ==
  15. ilog2(UDP_TUNNEL_TYPE_VXLAN_GPE));
  16. static ssize_t ethnl_udp_table_reply_size(unsigned int types, bool compact)
  17. {
  18. ssize_t size;
  19. size = ethnl_bitset32_size(&types, NULL, __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
  20. udp_tunnel_type_names, compact);
  21. if (size < 0)
  22. return size;
  23. return size +
  24. nla_total_size(0) + /* _UDP_TABLE */
  25. nla_total_size(sizeof(u32)); /* _UDP_TABLE_SIZE */
  26. }
  27. static ssize_t
  28. ethnl_tunnel_info_reply_size(const struct ethnl_req_info *req_base,
  29. struct netlink_ext_ack *extack)
  30. {
  31. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  32. const struct udp_tunnel_nic_info *info;
  33. unsigned int i;
  34. ssize_t ret;
  35. size_t size;
  36. info = req_base->dev->udp_tunnel_nic_info;
  37. if (!info) {
  38. NL_SET_ERR_MSG(extack,
  39. "device does not report tunnel offload info");
  40. return -EOPNOTSUPP;
  41. }
  42. size = nla_total_size(0); /* _INFO_UDP_PORTS */
  43. for (i = 0; i < UDP_TUNNEL_NIC_MAX_TABLES; i++) {
  44. if (!info->tables[i].n_entries)
  45. break;
  46. ret = ethnl_udp_table_reply_size(info->tables[i].tunnel_types,
  47. compact);
  48. if (ret < 0)
  49. return ret;
  50. size += ret;
  51. size += udp_tunnel_nic_dump_size(req_base->dev, i);
  52. }
  53. if (info->flags & UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN) {
  54. ret = ethnl_udp_table_reply_size(0, compact);
  55. if (ret < 0)
  56. return ret;
  57. size += ret;
  58. size += nla_total_size(0) + /* _TABLE_ENTRY */
  59. nla_total_size(sizeof(__be16)) + /* _ENTRY_PORT */
  60. nla_total_size(sizeof(u32)); /* _ENTRY_TYPE */
  61. }
  62. return size;
  63. }
  64. static int
  65. ethnl_tunnel_info_fill_reply(const struct ethnl_req_info *req_base,
  66. struct sk_buff *skb)
  67. {
  68. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  69. const struct udp_tunnel_nic_info *info;
  70. struct nlattr *ports, *table, *entry;
  71. unsigned int i;
  72. info = req_base->dev->udp_tunnel_nic_info;
  73. if (!info)
  74. return -EOPNOTSUPP;
  75. ports = nla_nest_start(skb, ETHTOOL_A_TUNNEL_INFO_UDP_PORTS);
  76. if (!ports)
  77. return -EMSGSIZE;
  78. for (i = 0; i < UDP_TUNNEL_NIC_MAX_TABLES; i++) {
  79. if (!info->tables[i].n_entries)
  80. break;
  81. table = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE);
  82. if (!table)
  83. goto err_cancel_ports;
  84. if (nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_SIZE,
  85. info->tables[i].n_entries))
  86. goto err_cancel_table;
  87. if (ethnl_put_bitset32(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_TYPES,
  88. &info->tables[i].tunnel_types, NULL,
  89. __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
  90. udp_tunnel_type_names, compact))
  91. goto err_cancel_table;
  92. if (udp_tunnel_nic_dump_write(req_base->dev, i, skb))
  93. goto err_cancel_table;
  94. nla_nest_end(skb, table);
  95. }
  96. if (info->flags & UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN) {
  97. u32 zero = 0;
  98. table = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE);
  99. if (!table)
  100. goto err_cancel_ports;
  101. if (nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_SIZE, 1))
  102. goto err_cancel_table;
  103. if (ethnl_put_bitset32(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_TYPES,
  104. &zero, NULL,
  105. __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
  106. udp_tunnel_type_names, compact))
  107. goto err_cancel_table;
  108. entry = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_ENTRY);
  109. if (!entry)
  110. goto err_cancel_entry;
  111. if (nla_put_be16(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_PORT,
  112. htons(IANA_VXLAN_UDP_PORT)) ||
  113. nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_TYPE,
  114. ilog2(UDP_TUNNEL_TYPE_VXLAN)))
  115. goto err_cancel_entry;
  116. nla_nest_end(skb, entry);
  117. nla_nest_end(skb, table);
  118. }
  119. nla_nest_end(skb, ports);
  120. return 0;
  121. err_cancel_entry:
  122. nla_nest_cancel(skb, entry);
  123. err_cancel_table:
  124. nla_nest_cancel(skb, table);
  125. err_cancel_ports:
  126. nla_nest_cancel(skb, ports);
  127. return -EMSGSIZE;
  128. }
  129. int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info)
  130. {
  131. struct ethnl_req_info req_info = {};
  132. struct nlattr **tb = info->attrs;
  133. struct sk_buff *rskb;
  134. void *reply_payload;
  135. int reply_len;
  136. int ret;
  137. ret = ethnl_parse_header_dev_get(&req_info,
  138. tb[ETHTOOL_A_TUNNEL_INFO_HEADER],
  139. genl_info_net(info), info->extack,
  140. true);
  141. if (ret < 0)
  142. return ret;
  143. rtnl_lock();
  144. ret = ethnl_tunnel_info_reply_size(&req_info, info->extack);
  145. if (ret < 0)
  146. goto err_unlock_rtnl;
  147. reply_len = ret + ethnl_reply_header_size();
  148. rskb = ethnl_reply_init(reply_len, req_info.dev,
  149. ETHTOOL_MSG_TUNNEL_INFO_GET_REPLY,
  150. ETHTOOL_A_TUNNEL_INFO_HEADER,
  151. info, &reply_payload);
  152. if (!rskb) {
  153. ret = -ENOMEM;
  154. goto err_unlock_rtnl;
  155. }
  156. ret = ethnl_tunnel_info_fill_reply(&req_info, rskb);
  157. if (ret)
  158. goto err_free_msg;
  159. rtnl_unlock();
  160. ethnl_parse_header_dev_put(&req_info);
  161. genlmsg_end(rskb, reply_payload);
  162. return genlmsg_reply(rskb, info);
  163. err_free_msg:
  164. nlmsg_free(rskb);
  165. err_unlock_rtnl:
  166. rtnl_unlock();
  167. ethnl_parse_header_dev_put(&req_info);
  168. return ret;
  169. }
  170. struct ethnl_tunnel_info_dump_ctx {
  171. struct ethnl_req_info req_info;
  172. int pos_hash;
  173. int pos_idx;
  174. };
  175. int ethnl_tunnel_info_start(struct netlink_callback *cb)
  176. {
  177. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  178. struct ethnl_tunnel_info_dump_ctx *ctx = (void *)cb->ctx;
  179. struct nlattr **tb = info->attrs;
  180. int ret;
  181. BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
  182. memset(ctx, 0, sizeof(*ctx));
  183. ret = ethnl_parse_header_dev_get(&ctx->req_info,
  184. tb[ETHTOOL_A_TUNNEL_INFO_HEADER],
  185. sock_net(cb->skb->sk), cb->extack,
  186. false);
  187. if (ctx->req_info.dev) {
  188. ethnl_parse_header_dev_put(&ctx->req_info);
  189. ctx->req_info.dev = NULL;
  190. }
  191. return ret;
  192. }
  193. int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  194. {
  195. struct ethnl_tunnel_info_dump_ctx *ctx = (void *)cb->ctx;
  196. struct net *net = sock_net(skb->sk);
  197. int s_idx = ctx->pos_idx;
  198. int h, idx = 0;
  199. int ret = 0;
  200. void *ehdr;
  201. rtnl_lock();
  202. cb->seq = net->dev_base_seq;
  203. for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  204. struct hlist_head *head;
  205. struct net_device *dev;
  206. head = &net->dev_index_head[h];
  207. idx = 0;
  208. hlist_for_each_entry(dev, head, index_hlist) {
  209. if (idx < s_idx)
  210. goto cont;
  211. ehdr = ethnl_dump_put(skb, cb,
  212. ETHTOOL_MSG_TUNNEL_INFO_GET_REPLY);
  213. if (!ehdr) {
  214. ret = -EMSGSIZE;
  215. goto out;
  216. }
  217. ret = ethnl_fill_reply_header(skb, dev, ETHTOOL_A_TUNNEL_INFO_HEADER);
  218. if (ret < 0) {
  219. genlmsg_cancel(skb, ehdr);
  220. goto out;
  221. }
  222. ctx->req_info.dev = dev;
  223. ret = ethnl_tunnel_info_fill_reply(&ctx->req_info, skb);
  224. ctx->req_info.dev = NULL;
  225. if (ret < 0) {
  226. genlmsg_cancel(skb, ehdr);
  227. if (ret == -EOPNOTSUPP)
  228. goto cont;
  229. goto out;
  230. }
  231. genlmsg_end(skb, ehdr);
  232. cont:
  233. idx++;
  234. }
  235. }
  236. out:
  237. rtnl_unlock();
  238. ctx->pos_hash = h;
  239. ctx->pos_idx = idx;
  240. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  241. if (ret == -EMSGSIZE && skb->len)
  242. return skb->len;
  243. return ret;
  244. }