utils.c 6.2 KB

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