tuntap_helpers.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _TUNTAP_HELPERS_H
  3. #define _TUNTAP_HELPERS_H
  4. #include <errno.h>
  5. #include <linux/if_packet.h>
  6. #include <linux/ipv6.h>
  7. #include <linux/virtio_net.h>
  8. #include <netinet/in.h>
  9. #include <netinet/if_ether.h>
  10. #include <netinet/udp.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <ynl.h>
  16. #include "rt-route-user.h"
  17. #include "rt-addr-user.h"
  18. #include "rt-neigh-user.h"
  19. #include "rt-link-user.h"
  20. #define GENEVE_HLEN 8
  21. #define PKT_DATA 0xCB
  22. #define TUNTAP_DEFAULT_TTL 8
  23. #define TUNTAP_DEFAULT_IPID 1337
  24. unsigned int if_nametoindex(const char *ifname);
  25. static inline int ip_addr_len(int family)
  26. {
  27. return (family == AF_INET) ? sizeof(struct in_addr) :
  28. sizeof(struct in6_addr);
  29. }
  30. static inline void fill_ifaddr_msg(struct ifaddrmsg *ifam, int family,
  31. int prefix, int flags, const char *dev)
  32. {
  33. ifam->ifa_family = family;
  34. ifam->ifa_prefixlen = prefix;
  35. ifam->ifa_index = if_nametoindex(dev);
  36. ifam->ifa_flags = flags;
  37. ifam->ifa_scope = RT_SCOPE_UNIVERSE;
  38. }
  39. static inline int ip_addr_add(const char *dev, int family, void *addr,
  40. uint8_t prefix)
  41. {
  42. int nl_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
  43. int ifa_flags = IFA_F_PERMANENT | IFA_F_NODAD;
  44. int ret = -1, ipalen = ip_addr_len(family);
  45. struct rt_addr_newaddr_req *req;
  46. struct ynl_sock *ys;
  47. ys = ynl_sock_create(&ynl_rt_addr_family, NULL);
  48. if (!ys)
  49. return -1;
  50. req = rt_addr_newaddr_req_alloc();
  51. if (!req)
  52. goto err_req_alloc;
  53. fill_ifaddr_msg(&req->_hdr, family, prefix, ifa_flags, dev);
  54. rt_addr_newaddr_req_set_nlflags(req, nl_flags);
  55. rt_addr_newaddr_req_set_local(req, addr, ipalen);
  56. ret = rt_addr_newaddr(ys, req);
  57. rt_addr_newaddr_req_free(req);
  58. err_req_alloc:
  59. ynl_sock_destroy(ys);
  60. return ret;
  61. }
  62. static inline void fill_neigh_req_header(struct ndmsg *ndm, int family,
  63. int state, const char *dev)
  64. {
  65. ndm->ndm_family = family;
  66. ndm->ndm_ifindex = if_nametoindex(dev);
  67. ndm->ndm_state = state;
  68. ndm->ndm_flags = 0;
  69. ndm->ndm_type = RTN_UNICAST;
  70. }
  71. static inline int ip_neigh_add(const char *dev, int family, void *addr,
  72. unsigned char *lladdr)
  73. {
  74. int nl_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
  75. int ret = -1, ipalen = ip_addr_len(family);
  76. struct rt_neigh_newneigh_req *req;
  77. struct ynl_sock *ys;
  78. ys = ynl_sock_create(&ynl_rt_neigh_family, NULL);
  79. if (!ys)
  80. return -1;
  81. req = rt_neigh_newneigh_req_alloc();
  82. if (!req)
  83. goto err_req_alloc;
  84. fill_neigh_req_header(&req->_hdr, family, NUD_PERMANENT, dev);
  85. rt_neigh_newneigh_req_set_nlflags(req, nl_flags);
  86. rt_neigh_newneigh_req_set_dst(req, addr, ipalen);
  87. rt_neigh_newneigh_req_set_lladdr(req, lladdr, ETH_ALEN);
  88. rt_neigh_newneigh_req_set_ifindex(req, if_nametoindex(dev));
  89. ret = rt_neigh_newneigh(ys, req);
  90. rt_neigh_newneigh_req_free(req);
  91. err_req_alloc:
  92. ynl_sock_destroy(ys);
  93. return ret;
  94. }
  95. static inline void fill_route_req_header(struct rtmsg *rtm, int family,
  96. int table)
  97. {
  98. rtm->rtm_family = family;
  99. rtm->rtm_table = table;
  100. }
  101. static inline int
  102. ip_route_get(const char *dev, int family, int table, void *dst,
  103. void (*parse_rsp)(struct rt_route_getroute_rsp *rsp, void *out),
  104. void *out)
  105. {
  106. int ret = -1, ipalen = ip_addr_len(family);
  107. struct rt_route_getroute_req *req;
  108. struct rt_route_getroute_rsp *rsp;
  109. struct ynl_sock *ys;
  110. ys = ynl_sock_create(&ynl_rt_route_family, NULL);
  111. if (!ys)
  112. return -1;
  113. req = rt_route_getroute_req_alloc();
  114. if (!req)
  115. goto err_req_alloc;
  116. fill_route_req_header(&req->_hdr, family, table);
  117. rt_route_getroute_req_set_nlflags(req, NLM_F_REQUEST);
  118. rt_route_getroute_req_set_dst(req, dst, ipalen);
  119. rt_route_getroute_req_set_oif(req, if_nametoindex(dev));
  120. rsp = rt_route_getroute(ys, req);
  121. if (!rsp)
  122. goto err_rsp_get;
  123. ret = 0;
  124. if (parse_rsp)
  125. parse_rsp(rsp, out);
  126. rt_route_getroute_rsp_free(rsp);
  127. err_rsp_get:
  128. rt_route_getroute_req_free(req);
  129. err_req_alloc:
  130. ynl_sock_destroy(ys);
  131. return ret;
  132. }
  133. static inline int
  134. ip_link_add(const char *dev, char *link_type,
  135. int (*fill_link_attr)(struct rt_link_newlink_req *req, void *data),
  136. void *data)
  137. {
  138. int nl_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
  139. struct rt_link_newlink_req *req;
  140. struct ynl_sock *ys;
  141. int ret = -1;
  142. ys = ynl_sock_create(&ynl_rt_link_family, NULL);
  143. if (!ys)
  144. return -1;
  145. req = rt_link_newlink_req_alloc();
  146. if (!req)
  147. goto err_req_alloc;
  148. req->_hdr.ifi_flags = IFF_UP;
  149. rt_link_newlink_req_set_nlflags(req, nl_flags);
  150. rt_link_newlink_req_set_ifname(req, dev);
  151. rt_link_newlink_req_set_linkinfo_kind(req, link_type);
  152. if (fill_link_attr && fill_link_attr(req, data) < 0)
  153. goto err_attr_fill;
  154. ret = rt_link_newlink(ys, req);
  155. err_attr_fill:
  156. rt_link_newlink_req_free(req);
  157. err_req_alloc:
  158. ynl_sock_destroy(ys);
  159. return ret;
  160. }
  161. static inline int ip_link_del(const char *dev)
  162. {
  163. struct rt_link_dellink_req *req;
  164. struct ynl_sock *ys;
  165. int ret = -1;
  166. ys = ynl_sock_create(&ynl_rt_link_family, NULL);
  167. if (!ys)
  168. return -1;
  169. req = rt_link_dellink_req_alloc();
  170. if (!req)
  171. goto err_req_alloc;
  172. rt_link_dellink_req_set_nlflags(req, NLM_F_REQUEST);
  173. rt_link_dellink_req_set_ifname(req, dev);
  174. ret = rt_link_dellink(ys, req);
  175. rt_link_dellink_req_free(req);
  176. err_req_alloc:
  177. ynl_sock_destroy(ys);
  178. return ret;
  179. }
  180. static inline size_t build_eth(uint8_t *buf, uint16_t proto, unsigned char *src,
  181. unsigned char *dest)
  182. {
  183. struct ethhdr *eth = (struct ethhdr *)buf;
  184. eth->h_proto = htons(proto);
  185. memcpy(eth->h_source, src, ETH_ALEN);
  186. memcpy(eth->h_dest, dest, ETH_ALEN);
  187. return ETH_HLEN;
  188. }
  189. static inline uint32_t add_csum(const uint8_t *buf, int len)
  190. {
  191. uint16_t *sbuf = (uint16_t *)buf;
  192. uint32_t sum = 0;
  193. while (len > 1) {
  194. sum += *sbuf++;
  195. len -= 2;
  196. }
  197. if (len)
  198. sum += *(uint8_t *)sbuf;
  199. return sum;
  200. }
  201. static inline uint16_t finish_ip_csum(uint32_t sum)
  202. {
  203. while (sum >> 16)
  204. sum = (sum & 0xffff) + (sum >> 16);
  205. return ~((uint16_t)sum);
  206. }
  207. static inline uint16_t build_ip_csum(const uint8_t *buf, int len, uint32_t sum)
  208. {
  209. sum += add_csum(buf, len);
  210. return finish_ip_csum(sum);
  211. }
  212. static inline int build_ipv4_header(uint8_t *buf, uint8_t proto,
  213. int payload_len, struct in_addr *src,
  214. struct in_addr *dst)
  215. {
  216. struct iphdr *iph = (struct iphdr *)buf;
  217. iph->ihl = 5;
  218. iph->version = 4;
  219. iph->ttl = TUNTAP_DEFAULT_TTL;
  220. iph->tot_len = htons(sizeof(*iph) + payload_len);
  221. iph->id = htons(TUNTAP_DEFAULT_IPID);
  222. iph->protocol = proto;
  223. iph->saddr = src->s_addr;
  224. iph->daddr = dst->s_addr;
  225. iph->check = build_ip_csum(buf, iph->ihl << 2, 0);
  226. return iph->ihl << 2;
  227. }
  228. static inline void ipv6_set_dsfield(struct ipv6hdr *ip6h, uint8_t dsfield)
  229. {
  230. uint16_t val, *ptr = (uint16_t *)ip6h;
  231. val = ntohs(*ptr);
  232. val &= 0xF00F;
  233. val |= ((uint16_t)dsfield) << 4;
  234. *ptr = htons(val);
  235. }
  236. static inline int build_ipv6_header(uint8_t *buf, uint8_t proto,
  237. uint8_t dsfield, int payload_len,
  238. struct in6_addr *src, struct in6_addr *dst)
  239. {
  240. struct ipv6hdr *ip6h = (struct ipv6hdr *)buf;
  241. ip6h->version = 6;
  242. ip6h->payload_len = htons(payload_len);
  243. ip6h->nexthdr = proto;
  244. ip6h->hop_limit = TUNTAP_DEFAULT_TTL;
  245. ipv6_set_dsfield(ip6h, dsfield);
  246. memcpy(&ip6h->saddr, src, sizeof(ip6h->saddr));
  247. memcpy(&ip6h->daddr, dst, sizeof(ip6h->daddr));
  248. return sizeof(struct ipv6hdr);
  249. }
  250. static inline int build_geneve_header(uint8_t *buf, uint32_t vni)
  251. {
  252. uint16_t protocol = htons(ETH_P_TEB);
  253. uint32_t geneve_vni = htonl((vni << 8) & 0xffffff00);
  254. memcpy(buf + 2, &protocol, 2);
  255. memcpy(buf + 4, &geneve_vni, 4);
  256. return GENEVE_HLEN;
  257. }
  258. static inline int build_udp_header(uint8_t *buf, uint16_t sport, uint16_t dport,
  259. int payload_len)
  260. {
  261. struct udphdr *udph = (struct udphdr *)buf;
  262. udph->source = htons(sport);
  263. udph->dest = htons(dport);
  264. udph->len = htons(sizeof(*udph) + payload_len);
  265. return sizeof(*udph);
  266. }
  267. static inline void build_udp_packet_csum(uint8_t *buf, int family,
  268. bool csum_off)
  269. {
  270. struct udphdr *udph = (struct udphdr *)buf;
  271. size_t ipalen = ip_addr_len(family);
  272. uint32_t sum;
  273. /* No extension IPv4 and IPv6 headers addresses are the last fields */
  274. sum = add_csum(buf - 2 * ipalen, 2 * ipalen);
  275. sum += htons(IPPROTO_UDP) + udph->len;
  276. if (!csum_off)
  277. sum += add_csum(buf, udph->len);
  278. udph->check = finish_ip_csum(sum);
  279. }
  280. static inline int build_udp_packet(uint8_t *buf, uint16_t sport, uint16_t dport,
  281. int payload_len, int family, bool csum_off)
  282. {
  283. struct udphdr *udph = (struct udphdr *)buf;
  284. build_udp_header(buf, sport, dport, payload_len);
  285. memset(buf + sizeof(*udph), PKT_DATA, payload_len);
  286. build_udp_packet_csum(buf, family, csum_off);
  287. return sizeof(*udph) + payload_len;
  288. }
  289. static inline int build_virtio_net_hdr_v1_hash_tunnel(uint8_t *buf, bool is_tap,
  290. int hdr_len, int gso_size,
  291. int outer_family,
  292. int inner_family)
  293. {
  294. struct virtio_net_hdr_v1_hash_tunnel *vh_tunnel = (void *)buf;
  295. struct virtio_net_hdr_v1 *vh = &vh_tunnel->hash_hdr.hdr;
  296. int outer_iphlen, inner_iphlen, eth_hlen, gso_type;
  297. eth_hlen = is_tap ? ETH_HLEN : 0;
  298. outer_iphlen = (outer_family == AF_INET) ? sizeof(struct iphdr) :
  299. sizeof(struct ipv6hdr);
  300. inner_iphlen = (inner_family == AF_INET) ? sizeof(struct iphdr) :
  301. sizeof(struct ipv6hdr);
  302. vh_tunnel->outer_th_offset = eth_hlen + outer_iphlen;
  303. vh_tunnel->inner_nh_offset = vh_tunnel->outer_th_offset + ETH_HLEN +
  304. GENEVE_HLEN + sizeof(struct udphdr);
  305. vh->csum_start = vh_tunnel->inner_nh_offset + inner_iphlen;
  306. vh->csum_offset = __builtin_offsetof(struct udphdr, check);
  307. vh->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  308. vh->hdr_len = hdr_len;
  309. vh->gso_size = gso_size;
  310. if (gso_size) {
  311. gso_type = outer_family == AF_INET ?
  312. VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4 :
  313. VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6;
  314. vh->gso_type = VIRTIO_NET_HDR_GSO_UDP_L4 | gso_type;
  315. }
  316. return sizeof(struct virtio_net_hdr_v1_hash_tunnel);
  317. }
  318. #endif /* _TUNTAP_HELPERS_H */