utils.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <net/sock.h>
  29. #include <net/net_ratelimit.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/uaccess.h>
  32. DEFINE_RATELIMIT_STATE(net_ratelimit_state, 5 * HZ, 10);
  33. /*
  34. * All net warning printk()s should be guarded by this function.
  35. */
  36. int net_ratelimit(void)
  37. {
  38. return __ratelimit(&net_ratelimit_state);
  39. }
  40. EXPORT_SYMBOL(net_ratelimit);
  41. /*
  42. * Convert an ASCII string to binary IP.
  43. * This is outside of net/ipv4/ because various code that uses IP addresses
  44. * is otherwise not dependent on the TCP/IP stack.
  45. */
  46. __be32 in_aton(const char *str)
  47. {
  48. unsigned long l;
  49. unsigned int val;
  50. int i;
  51. l = 0;
  52. for (i = 0; i < 4; i++) {
  53. l <<= 8;
  54. if (*str != '\0') {
  55. val = 0;
  56. while (*str != '\0' && *str != '.' && *str != '\n') {
  57. val *= 10;
  58. val += *str - '0';
  59. str++;
  60. }
  61. l |= val;
  62. if (*str != '\0')
  63. str++;
  64. }
  65. }
  66. return htonl(l);
  67. }
  68. EXPORT_SYMBOL(in_aton);
  69. #define IN6PTON_XDIGIT 0x00010000
  70. #define IN6PTON_DIGIT 0x00020000
  71. #define IN6PTON_COLON_MASK 0x00700000
  72. #define IN6PTON_COLON_1 0x00100000 /* single : requested */
  73. #define IN6PTON_COLON_2 0x00200000 /* second : requested */
  74. #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
  75. #define IN6PTON_DOT 0x00800000 /* . */
  76. #define IN6PTON_DELIM 0x10000000
  77. #define IN6PTON_NULL 0x20000000 /* first/tail */
  78. #define IN6PTON_UNKNOWN 0x40000000
  79. static inline int xdigit2bin(char c, int delim)
  80. {
  81. int val;
  82. if (c == delim || c == '\0')
  83. return IN6PTON_DELIM;
  84. if (c == ':')
  85. return IN6PTON_COLON_MASK;
  86. if (c == '.')
  87. return IN6PTON_DOT;
  88. val = hex_to_bin(c);
  89. if (val >= 0)
  90. return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0);
  91. if (delim == -1)
  92. return IN6PTON_DELIM;
  93. return IN6PTON_UNKNOWN;
  94. }
  95. /**
  96. * in4_pton - convert an IPv4 address from literal to binary representation
  97. * @src: the start of the IPv4 address string
  98. * @srclen: the length of the string, -1 means strlen(src)
  99. * @dst: the binary (u8[4] array) representation of the IPv4 address
  100. * @delim: the delimiter of the IPv4 address in @src, -1 means no delimiter
  101. * @end: A pointer to the end of the parsed string will be placed here
  102. *
  103. * Return one on success, return zero when any error occurs
  104. * and @end will point to the end of the parsed string.
  105. *
  106. */
  107. int in4_pton(const char *src, int srclen,
  108. u8 *dst,
  109. int delim, const char **end)
  110. {
  111. const char *s;
  112. u8 *d;
  113. u8 dbuf[4];
  114. int ret = 0;
  115. int i;
  116. int w = 0;
  117. if (srclen < 0)
  118. srclen = strlen(src);
  119. s = src;
  120. d = dbuf;
  121. i = 0;
  122. while (1) {
  123. int c;
  124. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  125. if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK))) {
  126. goto out;
  127. }
  128. if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  129. if (w == 0)
  130. goto out;
  131. *d++ = w & 0xff;
  132. w = 0;
  133. i++;
  134. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  135. if (i != 4)
  136. goto out;
  137. break;
  138. }
  139. goto cont;
  140. }
  141. w = (w * 10) + c;
  142. if ((w & 0xffff) > 255) {
  143. goto out;
  144. }
  145. cont:
  146. if (i >= 4)
  147. goto out;
  148. s++;
  149. srclen--;
  150. }
  151. ret = 1;
  152. memcpy(dst, dbuf, sizeof(dbuf));
  153. out:
  154. if (end)
  155. *end = s;
  156. return ret;
  157. }
  158. EXPORT_SYMBOL(in4_pton);
  159. /**
  160. * in6_pton - convert an IPv6 address from literal to binary representation
  161. * @src: the start of the IPv6 address string
  162. * @srclen: the length of the string, -1 means strlen(src)
  163. * @dst: the binary (u8[16] array) representation of the IPv6 address
  164. * @delim: the delimiter of the IPv6 address in @src, -1 means no delimiter
  165. * @end: A pointer to the end of the parsed string will be placed here
  166. *
  167. * Return one on success, return zero when any error occurs
  168. * and @end will point to the end of the parsed string.
  169. *
  170. */
  171. int in6_pton(const char *src, int srclen,
  172. u8 *dst,
  173. int delim, const char **end)
  174. {
  175. const char *s, *tok = NULL;
  176. u8 *d, *dc = NULL;
  177. u8 dbuf[16];
  178. int ret = 0;
  179. int i;
  180. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  181. int w = 0;
  182. memset(dbuf, 0, sizeof(dbuf));
  183. s = src;
  184. d = dbuf;
  185. if (srclen < 0)
  186. srclen = strlen(src);
  187. while (1) {
  188. int c;
  189. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  190. if (!(c & state))
  191. goto out;
  192. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  193. /* process one 16-bit word */
  194. if (!(state & IN6PTON_NULL)) {
  195. *d++ = (w >> 8) & 0xff;
  196. *d++ = w & 0xff;
  197. }
  198. w = 0;
  199. if (c & IN6PTON_DELIM) {
  200. /* We've processed last word */
  201. break;
  202. }
  203. /*
  204. * COLON_1 => XDIGIT
  205. * COLON_2 => XDIGIT|DELIM
  206. * COLON_1_2 => COLON_2
  207. */
  208. switch (state & IN6PTON_COLON_MASK) {
  209. case IN6PTON_COLON_2:
  210. dc = d;
  211. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  212. if (dc - dbuf >= sizeof(dbuf))
  213. state |= IN6PTON_NULL;
  214. break;
  215. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  216. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  217. break;
  218. case IN6PTON_COLON_1:
  219. state = IN6PTON_XDIGIT;
  220. break;
  221. case IN6PTON_COLON_1_2:
  222. state = IN6PTON_COLON_2;
  223. break;
  224. default:
  225. state = 0;
  226. }
  227. tok = s + 1;
  228. goto cont;
  229. }
  230. if (c & IN6PTON_DOT) {
  231. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  232. if (ret > 0) {
  233. d += 4;
  234. break;
  235. }
  236. goto out;
  237. }
  238. w = (w << 4) | (0xff & c);
  239. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  240. if (!(w & 0xf000)) {
  241. state |= IN6PTON_XDIGIT;
  242. }
  243. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  244. state |= IN6PTON_COLON_1_2;
  245. state &= ~IN6PTON_DELIM;
  246. }
  247. if (d + 2 >= dbuf + sizeof(dbuf)) {
  248. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  249. }
  250. cont:
  251. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  252. d + 4 == dbuf + sizeof(dbuf)) {
  253. state |= IN6PTON_DOT;
  254. }
  255. if (d >= dbuf + sizeof(dbuf)) {
  256. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  257. }
  258. s++;
  259. srclen--;
  260. }
  261. i = 15; d--;
  262. if (dc) {
  263. while (d >= dc)
  264. dst[i--] = *d--;
  265. while (i >= dc - dbuf)
  266. dst[i--] = 0;
  267. while (i >= 0)
  268. dst[i--] = *d--;
  269. } else
  270. memcpy(dst, dbuf, sizeof(dbuf));
  271. ret = 1;
  272. out:
  273. if (end)
  274. *end = s;
  275. return ret;
  276. }
  277. EXPORT_SYMBOL(in6_pton);
  278. void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  279. __be32 from, __be32 to, bool pseudohdr)
  280. {
  281. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  282. csum_replace4(sum, from, to);
  283. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  284. skb->csum = ~csum_add(csum_sub(~(skb->csum),
  285. (__force __wsum)from),
  286. (__force __wsum)to);
  287. } else if (pseudohdr)
  288. *sum = ~csum_fold(csum_add(csum_sub(csum_unfold(*sum),
  289. (__force __wsum)from),
  290. (__force __wsum)to));
  291. }
  292. EXPORT_SYMBOL(inet_proto_csum_replace4);
  293. void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
  294. const __be32 *from, const __be32 *to,
  295. bool pseudohdr)
  296. {
  297. __be32 diff[] = {
  298. ~from[0], ~from[1], ~from[2], ~from[3],
  299. to[0], to[1], to[2], to[3],
  300. };
  301. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  302. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  303. ~csum_unfold(*sum)));
  304. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  305. skb->csum = ~csum_partial(diff, sizeof(diff),
  306. ~skb->csum);
  307. } else if (pseudohdr)
  308. *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
  309. csum_unfold(*sum)));
  310. }
  311. EXPORT_SYMBOL(inet_proto_csum_replace16);
  312. void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
  313. __wsum diff, bool pseudohdr)
  314. {
  315. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  316. *sum = csum_fold(csum_add(diff, ~csum_unfold(*sum)));
  317. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  318. skb->csum = ~csum_add(diff, ~skb->csum);
  319. } else if (pseudohdr) {
  320. *sum = ~csum_fold(csum_add(diff, csum_unfold(*sum)));
  321. }
  322. }
  323. EXPORT_SYMBOL(inet_proto_csum_replace_by_diff);