utils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/inet.h>
  20. #include <linux/mm.h>
  21. #include <linux/net.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <linux/random.h>
  25. #include <linux/percpu.h>
  26. #include <linux/init.h>
  27. #include <net/sock.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/system.h>
  30. #include <asm/uaccess.h>
  31. int net_msg_warn __read_mostly = 1;
  32. EXPORT_SYMBOL(net_msg_warn);
  33. DEFINE_RATELIMIT_STATE(net_ratelimit_state, 5 * HZ, 10);
  34. /*
  35. * All net warning printk()s should be guarded by this function.
  36. */
  37. int net_ratelimit(void)
  38. {
  39. return __ratelimit(&net_ratelimit_state);
  40. }
  41. EXPORT_SYMBOL(net_ratelimit);
  42. /*
  43. * Convert an ASCII string to binary IP.
  44. * This is outside of net/ipv4/ because various code that uses IP addresses
  45. * is otherwise not dependent on the TCP/IP stack.
  46. */
  47. __be32 in_aton(const char *str)
  48. {
  49. unsigned long l;
  50. unsigned int val;
  51. int i;
  52. l = 0;
  53. for (i = 0; i < 4; i++)
  54. {
  55. l <<= 8;
  56. if (*str != '\0')
  57. {
  58. val = 0;
  59. while (*str != '\0' && *str != '.' && *str != '\n')
  60. {
  61. val *= 10;
  62. val += *str - '0';
  63. str++;
  64. }
  65. l |= val;
  66. if (*str != '\0')
  67. str++;
  68. }
  69. }
  70. return(htonl(l));
  71. }
  72. EXPORT_SYMBOL(in_aton);
  73. #define IN6PTON_XDIGIT 0x00010000
  74. #define IN6PTON_DIGIT 0x00020000
  75. #define IN6PTON_COLON_MASK 0x00700000
  76. #define IN6PTON_COLON_1 0x00100000 /* single : requested */
  77. #define IN6PTON_COLON_2 0x00200000 /* second : requested */
  78. #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
  79. #define IN6PTON_DOT 0x00800000 /* . */
  80. #define IN6PTON_DELIM 0x10000000
  81. #define IN6PTON_NULL 0x20000000 /* first/tail */
  82. #define IN6PTON_UNKNOWN 0x40000000
  83. static inline int xdigit2bin(char c, int delim)
  84. {
  85. if (c == delim || c == '\0')
  86. return IN6PTON_DELIM;
  87. if (c == ':')
  88. return IN6PTON_COLON_MASK;
  89. if (c == '.')
  90. return IN6PTON_DOT;
  91. if (c >= '0' && c <= '9')
  92. return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
  93. if (c >= 'a' && c <= 'f')
  94. return (IN6PTON_XDIGIT | (c - 'a' + 10));
  95. if (c >= 'A' && c <= 'F')
  96. return (IN6PTON_XDIGIT | (c - 'A' + 10));
  97. if (delim == -1)
  98. return IN6PTON_DELIM;
  99. return IN6PTON_UNKNOWN;
  100. }
  101. int in4_pton(const char *src, int srclen,
  102. u8 *dst,
  103. int delim, const char **end)
  104. {
  105. const char *s;
  106. u8 *d;
  107. u8 dbuf[4];
  108. int ret = 0;
  109. int i;
  110. int w = 0;
  111. if (srclen < 0)
  112. srclen = strlen(src);
  113. s = src;
  114. d = dbuf;
  115. i = 0;
  116. while(1) {
  117. int c;
  118. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  119. if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK))) {
  120. goto out;
  121. }
  122. if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  123. if (w == 0)
  124. goto out;
  125. *d++ = w & 0xff;
  126. w = 0;
  127. i++;
  128. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  129. if (i != 4)
  130. goto out;
  131. break;
  132. }
  133. goto cont;
  134. }
  135. w = (w * 10) + c;
  136. if ((w & 0xffff) > 255) {
  137. goto out;
  138. }
  139. cont:
  140. if (i >= 4)
  141. goto out;
  142. s++;
  143. srclen--;
  144. }
  145. ret = 1;
  146. memcpy(dst, dbuf, sizeof(dbuf));
  147. out:
  148. if (end)
  149. *end = s;
  150. return ret;
  151. }
  152. EXPORT_SYMBOL(in4_pton);
  153. int in6_pton(const char *src, int srclen,
  154. u8 *dst,
  155. int delim, const char **end)
  156. {
  157. const char *s, *tok = NULL;
  158. u8 *d, *dc = NULL;
  159. u8 dbuf[16];
  160. int ret = 0;
  161. int i;
  162. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  163. int w = 0;
  164. memset(dbuf, 0, sizeof(dbuf));
  165. s = src;
  166. d = dbuf;
  167. if (srclen < 0)
  168. srclen = strlen(src);
  169. while (1) {
  170. int c;
  171. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  172. if (!(c & state))
  173. goto out;
  174. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  175. /* process one 16-bit word */
  176. if (!(state & IN6PTON_NULL)) {
  177. *d++ = (w >> 8) & 0xff;
  178. *d++ = w & 0xff;
  179. }
  180. w = 0;
  181. if (c & IN6PTON_DELIM) {
  182. /* We've processed last word */
  183. break;
  184. }
  185. /*
  186. * COLON_1 => XDIGIT
  187. * COLON_2 => XDIGIT|DELIM
  188. * COLON_1_2 => COLON_2
  189. */
  190. switch (state & IN6PTON_COLON_MASK) {
  191. case IN6PTON_COLON_2:
  192. dc = d;
  193. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  194. if (dc - dbuf >= sizeof(dbuf))
  195. state |= IN6PTON_NULL;
  196. break;
  197. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  198. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  199. break;
  200. case IN6PTON_COLON_1:
  201. state = IN6PTON_XDIGIT;
  202. break;
  203. case IN6PTON_COLON_1_2:
  204. state = IN6PTON_COLON_2;
  205. break;
  206. default:
  207. state = 0;
  208. }
  209. tok = s + 1;
  210. goto cont;
  211. }
  212. if (c & IN6PTON_DOT) {
  213. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  214. if (ret > 0) {
  215. d += 4;
  216. break;
  217. }
  218. goto out;
  219. }
  220. w = (w << 4) | (0xff & c);
  221. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  222. if (!(w & 0xf000)) {
  223. state |= IN6PTON_XDIGIT;
  224. }
  225. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  226. state |= IN6PTON_COLON_1_2;
  227. state &= ~IN6PTON_DELIM;
  228. }
  229. if (d + 2 >= dbuf + sizeof(dbuf)) {
  230. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  231. }
  232. cont:
  233. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  234. d + 4 == dbuf + sizeof(dbuf)) {
  235. state |= IN6PTON_DOT;
  236. }
  237. if (d >= dbuf + sizeof(dbuf)) {
  238. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  239. }
  240. s++;
  241. srclen--;
  242. }
  243. i = 15; d--;
  244. if (dc) {
  245. while(d >= dc)
  246. dst[i--] = *d--;
  247. while(i >= dc - dbuf)
  248. dst[i--] = 0;
  249. while(i >= 0)
  250. dst[i--] = *d--;
  251. } else
  252. memcpy(dst, dbuf, sizeof(dbuf));
  253. ret = 1;
  254. out:
  255. if (end)
  256. *end = s;
  257. return ret;
  258. }
  259. EXPORT_SYMBOL(in6_pton);
  260. void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  261. __be32 from, __be32 to, int pseudohdr)
  262. {
  263. __be32 diff[] = { ~from, to };
  264. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  265. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  266. ~csum_unfold(*sum)));
  267. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  268. skb->csum = ~csum_partial(diff, sizeof(diff),
  269. ~skb->csum);
  270. } else if (pseudohdr)
  271. *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
  272. csum_unfold(*sum)));
  273. }
  274. EXPORT_SYMBOL(inet_proto_csum_replace4);