libbpf_utils.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /*
  3. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  4. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  5. * Copyright (C) 2015 Huawei Inc.
  6. * Copyright (C) 2017 Nicira, Inc.
  7. */
  8. #undef _GNU_SOURCE
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <inttypes.h>
  13. #include <linux/kernel.h>
  14. #include "libbpf.h"
  15. #include "libbpf_internal.h"
  16. #ifndef ENOTSUPP
  17. #define ENOTSUPP 524
  18. #endif
  19. /* make sure libbpf doesn't use kernel-only integer typedefs */
  20. #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
  21. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  22. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  23. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  24. static const char *libbpf_strerror_table[NR_ERRNO] = {
  25. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  26. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  27. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  28. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  29. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  30. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  31. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  32. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  33. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  34. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  35. [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
  36. [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
  37. [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing",
  38. };
  39. int libbpf_strerror(int err, char *buf, size_t size)
  40. {
  41. int ret;
  42. if (!buf || !size)
  43. return libbpf_err(-EINVAL);
  44. err = err > 0 ? err : -err;
  45. if (err < __LIBBPF_ERRNO__START) {
  46. ret = strerror_r(err, buf, size);
  47. buf[size - 1] = '\0';
  48. return libbpf_err_errno(ret);
  49. }
  50. if (err < __LIBBPF_ERRNO__END) {
  51. const char *msg;
  52. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  53. ret = snprintf(buf, size, "%s", msg);
  54. buf[size - 1] = '\0';
  55. /* The length of the buf and msg is positive.
  56. * A negative number may be returned only when the
  57. * size exceeds INT_MAX. Not likely to appear.
  58. */
  59. if (ret >= size)
  60. return libbpf_err(-ERANGE);
  61. return 0;
  62. }
  63. ret = snprintf(buf, size, "Unknown libbpf error %d", err);
  64. buf[size - 1] = '\0';
  65. if (ret >= size)
  66. return libbpf_err(-ERANGE);
  67. return libbpf_err(-ENOENT);
  68. }
  69. const char *libbpf_errstr(int err)
  70. {
  71. static __thread char buf[12];
  72. if (err > 0)
  73. err = -err;
  74. switch (err) {
  75. case -E2BIG: return "-E2BIG";
  76. case -EACCES: return "-EACCES";
  77. case -EADDRINUSE: return "-EADDRINUSE";
  78. case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL";
  79. case -EAGAIN: return "-EAGAIN";
  80. case -EALREADY: return "-EALREADY";
  81. case -EBADF: return "-EBADF";
  82. case -EBADFD: return "-EBADFD";
  83. case -EBUSY: return "-EBUSY";
  84. case -ECANCELED: return "-ECANCELED";
  85. case -ECHILD: return "-ECHILD";
  86. case -EDEADLK: return "-EDEADLK";
  87. case -EDOM: return "-EDOM";
  88. case -EEXIST: return "-EEXIST";
  89. case -EFAULT: return "-EFAULT";
  90. case -EFBIG: return "-EFBIG";
  91. case -EILSEQ: return "-EILSEQ";
  92. case -EINPROGRESS: return "-EINPROGRESS";
  93. case -EINTR: return "-EINTR";
  94. case -EINVAL: return "-EINVAL";
  95. case -EIO: return "-EIO";
  96. case -EISDIR: return "-EISDIR";
  97. case -ELOOP: return "-ELOOP";
  98. case -EMFILE: return "-EMFILE";
  99. case -EMLINK: return "-EMLINK";
  100. case -EMSGSIZE: return "-EMSGSIZE";
  101. case -ENAMETOOLONG: return "-ENAMETOOLONG";
  102. case -ENFILE: return "-ENFILE";
  103. case -ENODATA: return "-ENODATA";
  104. case -ENODEV: return "-ENODEV";
  105. case -ENOENT: return "-ENOENT";
  106. case -ENOEXEC: return "-ENOEXEC";
  107. case -ENOLINK: return "-ENOLINK";
  108. case -ENOMEM: return "-ENOMEM";
  109. case -ENOSPC: return "-ENOSPC";
  110. case -ENOTBLK: return "-ENOTBLK";
  111. case -ENOTDIR: return "-ENOTDIR";
  112. case -ENOTSUPP: return "-ENOTSUPP";
  113. case -ENOTTY: return "-ENOTTY";
  114. case -ENXIO: return "-ENXIO";
  115. case -EOPNOTSUPP: return "-EOPNOTSUPP";
  116. case -EOVERFLOW: return "-EOVERFLOW";
  117. case -EPERM: return "-EPERM";
  118. case -EPIPE: return "-EPIPE";
  119. case -EPROTO: return "-EPROTO";
  120. case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT";
  121. case -ERANGE: return "-ERANGE";
  122. case -EROFS: return "-EROFS";
  123. case -ESPIPE: return "-ESPIPE";
  124. case -ESRCH: return "-ESRCH";
  125. case -ETXTBSY: return "-ETXTBSY";
  126. case -EUCLEAN: return "-EUCLEAN";
  127. case -EXDEV: return "-EXDEV";
  128. default:
  129. snprintf(buf, sizeof(buf), "%d", err);
  130. return buf;
  131. }
  132. }
  133. static inline __u32 get_unaligned_be32(const void *p)
  134. {
  135. __be32 val;
  136. memcpy(&val, p, sizeof(val));
  137. return be32_to_cpu(val);
  138. }
  139. static inline void put_unaligned_be32(__u32 val, void *p)
  140. {
  141. __be32 be_val = cpu_to_be32(val);
  142. memcpy(p, &be_val, sizeof(be_val));
  143. }
  144. #define SHA256_BLOCK_LENGTH 64
  145. #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  146. #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  147. #define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
  148. #define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
  149. #define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
  150. #define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
  151. static const __u32 sha256_K[64] = {
  152. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  153. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  154. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  155. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  156. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  157. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  158. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  159. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  160. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  161. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  162. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  163. };
  164. #define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \
  165. { \
  166. __u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
  167. d += tmp; \
  168. h = tmp + Sigma_0(a) + Maj(a, b, c); \
  169. }
  170. static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
  171. {
  172. while (nblocks--) {
  173. __u32 a = state[0];
  174. __u32 b = state[1];
  175. __u32 c = state[2];
  176. __u32 d = state[3];
  177. __u32 e = state[4];
  178. __u32 f = state[5];
  179. __u32 g = state[6];
  180. __u32 h = state[7];
  181. __u32 w[64];
  182. int i;
  183. for (i = 0; i < 16; i++)
  184. w[i] = get_unaligned_be32(&data[4 * i]);
  185. for (; i < ARRAY_SIZE(w); i++)
  186. w[i] = sigma_1(w[i - 2]) + w[i - 7] +
  187. sigma_0(w[i - 15]) + w[i - 16];
  188. for (i = 0; i < ARRAY_SIZE(w); i += 8) {
  189. SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
  190. SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
  191. SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
  192. SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
  193. SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
  194. SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
  195. SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
  196. SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
  197. }
  198. state[0] += a;
  199. state[1] += b;
  200. state[2] += c;
  201. state[3] += d;
  202. state[4] += e;
  203. state[5] += f;
  204. state[6] += g;
  205. state[7] += h;
  206. data += SHA256_BLOCK_LENGTH;
  207. }
  208. }
  209. void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
  210. {
  211. __u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  212. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
  213. const __be64 bitcount = cpu_to_be64((__u64)len * 8);
  214. __u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
  215. size_t final_len = len % SHA256_BLOCK_LENGTH;
  216. int i;
  217. sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
  218. memcpy(final_data, data + len - final_len, final_len);
  219. final_data[final_len] = 0x80;
  220. final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
  221. memcpy(&final_data[final_len - 8], &bitcount, 8);
  222. sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
  223. for (i = 0; i < ARRAY_SIZE(state); i++)
  224. put_unaligned_be32(state[i], &out[4 * i]);
  225. }