utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic address resolution entity
  4. *
  5. * Authors:
  6. * net_random Alan Cox
  7. * net_ratelimit Andi Kleen
  8. * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
  9. *
  10. * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/hex.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ctype.h>
  17. #include <linux/inet.h>
  18. #include <linux/mm.h>
  19. #include <linux/net.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/percpu.h>
  23. #include <linux/init.h>
  24. #include <linux/ratelimit.h>
  25. #include <linux/socket.h>
  26. #include <net/sock.h>
  27. #include <net/net_ratelimit.h>
  28. #include <net/ipv6.h>
  29. #include <asm/byteorder.h>
  30. #include <linux/uaccess.h>
  31. DEFINE_RATELIMIT_STATE(net_ratelimit_state, 5 * HZ, 10);
  32. /*
  33. * All net warning printk()s should be guarded by this function.
  34. */
  35. int net_ratelimit(void)
  36. {
  37. return __ratelimit(&net_ratelimit_state);
  38. }
  39. EXPORT_SYMBOL(net_ratelimit);
  40. /*
  41. * Convert an ASCII string to binary IP.
  42. * This is outside of net/ipv4/ because various code that uses IP addresses
  43. * is otherwise not dependent on the TCP/IP stack.
  44. */
  45. __be32 in_aton(const char *str)
  46. {
  47. unsigned int l;
  48. unsigned int val;
  49. int i;
  50. l = 0;
  51. for (i = 0; i < 4; i++) {
  52. l <<= 8;
  53. if (*str != '\0') {
  54. val = 0;
  55. while (*str != '\0' && *str != '.' && *str != '\n') {
  56. val *= 10;
  57. val += *str - '0';
  58. str++;
  59. }
  60. l |= val;
  61. if (*str != '\0')
  62. str++;
  63. }
  64. }
  65. return htonl(l);
  66. }
  67. EXPORT_SYMBOL(in_aton);
  68. #define IN6PTON_XDIGIT 0x00010000
  69. #define IN6PTON_DIGIT 0x00020000
  70. #define IN6PTON_COLON_MASK 0x00700000
  71. #define IN6PTON_COLON_1 0x00100000 /* single : requested */
  72. #define IN6PTON_COLON_2 0x00200000 /* second : requested */
  73. #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
  74. #define IN6PTON_DOT 0x00800000 /* . */
  75. #define IN6PTON_DELIM 0x10000000
  76. #define IN6PTON_NULL 0x20000000 /* first/tail */
  77. #define IN6PTON_UNKNOWN 0x40000000
  78. static inline int xdigit2bin(char c, int delim)
  79. {
  80. int val;
  81. if (c == delim || c == '\0')
  82. return IN6PTON_DELIM;
  83. if (c == ':')
  84. return IN6PTON_COLON_MASK;
  85. if (c == '.')
  86. return IN6PTON_DOT;
  87. val = hex_to_bin(c);
  88. if (val >= 0)
  89. return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0);
  90. if (delim == -1)
  91. return IN6PTON_DELIM;
  92. return IN6PTON_UNKNOWN;
  93. }
  94. /**
  95. * in4_pton - convert an IPv4 address from literal to binary representation
  96. * @src: the start of the IPv4 address string
  97. * @srclen: the length of the string, -1 means strlen(src)
  98. * @dst: the binary (u8[4] array) representation of the IPv4 address
  99. * @delim: the delimiter of the IPv4 address in @src, -1 means no delimiter
  100. * @end: A pointer to the end of the parsed string will be placed here
  101. *
  102. * Return one on success, return zero when any error occurs
  103. * and @end will point to the end of the parsed string.
  104. *
  105. */
  106. int in4_pton(const char *src, int srclen,
  107. u8 *dst,
  108. int delim, const char **end)
  109. {
  110. const char *s;
  111. u8 *d;
  112. u8 dbuf[4];
  113. int ret = 0;
  114. int i;
  115. int w = 0;
  116. if (srclen < 0)
  117. srclen = strlen(src);
  118. s = src;
  119. d = dbuf;
  120. i = 0;
  121. while (1) {
  122. int c;
  123. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  124. if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK))) {
  125. goto out;
  126. }
  127. if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  128. if (w == 0)
  129. goto out;
  130. *d++ = w & 0xff;
  131. w = 0;
  132. i++;
  133. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  134. if (i != 4)
  135. goto out;
  136. break;
  137. }
  138. goto cont;
  139. }
  140. w = (w * 10) + c;
  141. if ((w & 0xffff) > 255) {
  142. goto out;
  143. }
  144. cont:
  145. if (i >= 4)
  146. goto out;
  147. s++;
  148. srclen--;
  149. }
  150. ret = 1;
  151. memcpy(dst, dbuf, sizeof(dbuf));
  152. out:
  153. if (end)
  154. *end = s;
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(in4_pton);
  158. /**
  159. * in6_pton - convert an IPv6 address from literal to binary representation
  160. * @src: the start of the IPv6 address string
  161. * @srclen: the length of the string, -1 means strlen(src)
  162. * @dst: the binary (u8[16] array) representation of the IPv6 address
  163. * @delim: the delimiter of the IPv6 address in @src, -1 means no delimiter
  164. * @end: A pointer to the end of the parsed string will be placed here
  165. *
  166. * Return one on success, return zero when any error occurs
  167. * and @end will point to the end of the parsed string.
  168. *
  169. */
  170. int in6_pton(const char *src, int srclen,
  171. u8 *dst,
  172. int delim, const char **end)
  173. {
  174. const char *s, *tok = NULL;
  175. u8 *d, *dc = NULL;
  176. u8 dbuf[16];
  177. int ret = 0;
  178. int i;
  179. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  180. int w = 0;
  181. memset(dbuf, 0, sizeof(dbuf));
  182. s = src;
  183. d = dbuf;
  184. if (srclen < 0)
  185. srclen = strlen(src);
  186. while (1) {
  187. int c;
  188. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  189. if (!(c & state))
  190. goto out;
  191. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  192. /* process one 16-bit word */
  193. if (!(state & IN6PTON_NULL)) {
  194. *d++ = (w >> 8) & 0xff;
  195. *d++ = w & 0xff;
  196. }
  197. w = 0;
  198. if (c & IN6PTON_DELIM) {
  199. /* We've processed last word */
  200. break;
  201. }
  202. /*
  203. * COLON_1 => XDIGIT
  204. * COLON_2 => XDIGIT|DELIM
  205. * COLON_1_2 => COLON_2
  206. */
  207. switch (state & IN6PTON_COLON_MASK) {
  208. case IN6PTON_COLON_2:
  209. dc = d;
  210. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  211. if (dc - dbuf >= sizeof(dbuf))
  212. state |= IN6PTON_NULL;
  213. break;
  214. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  215. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  216. break;
  217. case IN6PTON_COLON_1:
  218. state = IN6PTON_XDIGIT;
  219. break;
  220. case IN6PTON_COLON_1_2:
  221. state = IN6PTON_COLON_2;
  222. break;
  223. default:
  224. state = 0;
  225. }
  226. tok = s + 1;
  227. goto cont;
  228. }
  229. if (c & IN6PTON_DOT) {
  230. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  231. if (ret > 0) {
  232. d += 4;
  233. break;
  234. }
  235. goto out;
  236. }
  237. w = (w << 4) | (0xff & c);
  238. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  239. if (!(w & 0xf000)) {
  240. state |= IN6PTON_XDIGIT;
  241. }
  242. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  243. state |= IN6PTON_COLON_1_2;
  244. state &= ~IN6PTON_DELIM;
  245. }
  246. if (d + 2 >= dbuf + sizeof(dbuf)) {
  247. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  248. }
  249. cont:
  250. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  251. d + 4 == dbuf + sizeof(dbuf)) {
  252. state |= IN6PTON_DOT;
  253. }
  254. if (d >= dbuf + sizeof(dbuf)) {
  255. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  256. }
  257. s++;
  258. srclen--;
  259. }
  260. i = 15; d--;
  261. if (dc) {
  262. while (d >= dc)
  263. dst[i--] = *d--;
  264. while (i >= dc - dbuf)
  265. dst[i--] = 0;
  266. while (i >= 0)
  267. dst[i--] = *d--;
  268. } else
  269. memcpy(dst, dbuf, sizeof(dbuf));
  270. ret = 1;
  271. out:
  272. if (end)
  273. *end = s;
  274. return ret;
  275. }
  276. EXPORT_SYMBOL(in6_pton);
  277. static int inet4_pton(const char *src, u16 port_num,
  278. struct sockaddr_storage *addr)
  279. {
  280. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  281. size_t srclen = strlen(src);
  282. if (srclen > INET_ADDRSTRLEN)
  283. return -EINVAL;
  284. if (in4_pton(src, srclen, (u8 *)&addr4->sin_addr.s_addr,
  285. '\n', NULL) == 0)
  286. return -EINVAL;
  287. addr4->sin_family = AF_INET;
  288. addr4->sin_port = htons(port_num);
  289. return 0;
  290. }
  291. static int inet6_pton(struct net *net, const char *src, u16 port_num,
  292. struct sockaddr_storage *addr)
  293. {
  294. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  295. const char *scope_delim;
  296. size_t srclen = strlen(src);
  297. if (srclen > INET6_ADDRSTRLEN)
  298. return -EINVAL;
  299. if (in6_pton(src, srclen, (u8 *)&addr6->sin6_addr.s6_addr,
  300. '%', &scope_delim) == 0)
  301. return -EINVAL;
  302. if (ipv6_addr_type(&addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL &&
  303. src + srclen != scope_delim && *scope_delim == '%') {
  304. struct net_device *dev;
  305. char scope_id[16];
  306. size_t scope_len = min_t(size_t, sizeof(scope_id) - 1,
  307. src + srclen - scope_delim - 1);
  308. memcpy(scope_id, scope_delim + 1, scope_len);
  309. scope_id[scope_len] = '\0';
  310. dev = dev_get_by_name(net, scope_id);
  311. if (dev) {
  312. addr6->sin6_scope_id = dev->ifindex;
  313. dev_put(dev);
  314. } else if (kstrtouint(scope_id, 0, &addr6->sin6_scope_id)) {
  315. return -EINVAL;
  316. }
  317. }
  318. addr6->sin6_family = AF_INET6;
  319. addr6->sin6_port = htons(port_num);
  320. return 0;
  321. }
  322. /**
  323. * inet_pton_with_scope - convert an IPv4/IPv6 and port to socket address
  324. * @net: net namespace (used for scope handling)
  325. * @af: address family, AF_INET, AF_INET6 or AF_UNSPEC for either
  326. * @src: the start of the address string
  327. * @port: the start of the port string (or NULL for none)
  328. * @addr: output socket address
  329. *
  330. * Return zero on success, return errno when any error occurs.
  331. */
  332. int inet_pton_with_scope(struct net *net, __kernel_sa_family_t af,
  333. const char *src, const char *port, struct sockaddr_storage *addr)
  334. {
  335. u16 port_num;
  336. int ret = -EINVAL;
  337. if (port) {
  338. if (kstrtou16(port, 0, &port_num))
  339. return -EINVAL;
  340. } else {
  341. port_num = 0;
  342. }
  343. switch (af) {
  344. case AF_INET:
  345. ret = inet4_pton(src, port_num, addr);
  346. break;
  347. case AF_INET6:
  348. ret = inet6_pton(net, src, port_num, addr);
  349. break;
  350. case AF_UNSPEC:
  351. ret = inet4_pton(src, port_num, addr);
  352. if (ret)
  353. ret = inet6_pton(net, src, port_num, addr);
  354. break;
  355. default:
  356. pr_err("unexpected address family %d\n", af);
  357. }
  358. return ret;
  359. }
  360. EXPORT_SYMBOL(inet_pton_with_scope);
  361. bool inet_addr_is_any(struct sockaddr_storage *addr)
  362. {
  363. if (addr->ss_family == AF_INET6) {
  364. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
  365. const struct sockaddr_in6 in6_any =
  366. { .sin6_addr = IN6ADDR_ANY_INIT };
  367. if (!memcmp(in6->sin6_addr.s6_addr,
  368. in6_any.sin6_addr.s6_addr, 16))
  369. return true;
  370. } else if (addr->ss_family == AF_INET) {
  371. struct sockaddr_in *in = (struct sockaddr_in *)addr;
  372. if (in->sin_addr.s_addr == htonl(INADDR_ANY))
  373. return true;
  374. } else {
  375. pr_warn("unexpected address family %u\n", addr->ss_family);
  376. }
  377. return false;
  378. }
  379. EXPORT_SYMBOL(inet_addr_is_any);
  380. void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  381. __be32 from, __be32 to, bool pseudohdr)
  382. {
  383. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  384. csum_replace4(sum, from, to);
  385. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  386. skb->csum = ~csum_add(csum_sub(~(skb->csum),
  387. (__force __wsum)from),
  388. (__force __wsum)to);
  389. } else if (pseudohdr)
  390. *sum = ~csum_fold(csum_add(csum_sub(csum_unfold(*sum),
  391. (__force __wsum)from),
  392. (__force __wsum)to));
  393. }
  394. EXPORT_SYMBOL(inet_proto_csum_replace4);
  395. /**
  396. * inet_proto_csum_replace16 - update layer 4 header checksum field
  397. * @sum: Layer 4 header checksum field
  398. * @skb: sk_buff for the packet
  399. * @from: old IPv6 address
  400. * @to: new IPv6 address
  401. * @pseudohdr: True if layer 4 header checksum includes pseudoheader
  402. *
  403. * Update layer 4 header as per the update in IPv6 src/dst address.
  404. *
  405. * There is no need to update skb->csum in this function, because update in two
  406. * fields a.) IPv6 src/dst address and b.) L4 header checksum cancels each other
  407. * for skb->csum calculation. Whereas inet_proto_csum_replace4 function needs to
  408. * update skb->csum, because update in 3 fields a.) IPv4 src/dst address,
  409. * b.) IPv4 Header checksum and c.) L4 header checksum results in same diff as
  410. * L4 Header checksum for skb->csum calculation.
  411. */
  412. void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
  413. const __be32 *from, const __be32 *to,
  414. bool pseudohdr)
  415. {
  416. __be32 diff[] = {
  417. ~from[0], ~from[1], ~from[2], ~from[3],
  418. to[0], to[1], to[2], to[3],
  419. };
  420. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  421. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  422. ~csum_unfold(*sum)));
  423. } else if (pseudohdr)
  424. *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
  425. csum_unfold(*sum)));
  426. }
  427. EXPORT_SYMBOL(inet_proto_csum_replace16);
  428. void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
  429. __wsum diff, bool pseudohdr, bool ipv6)
  430. {
  431. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  432. csum_replace_by_diff(sum, diff);
  433. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr && !ipv6)
  434. skb->csum = ~csum_sub(diff, skb->csum);
  435. } else if (pseudohdr) {
  436. *sum = ~csum_fold(csum_add(diff, csum_unfold(*sum)));
  437. }
  438. }
  439. EXPORT_SYMBOL(inet_proto_csum_replace_by_diff);