utils.c 6.2 KB

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