utils.c 12 KB

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