utils.c 12 KB

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