utils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 digit2bin(char c, int delim)
  85. {
  86. if (c == delim || c == '\0')
  87. return IN6PTON_DELIM;
  88. if (c == '.')
  89. return IN6PTON_DOT;
  90. if (c >= '0' && c <= '9')
  91. return (IN6PTON_DIGIT | (c - '0'));
  92. return IN6PTON_UNKNOWN;
  93. }
  94. static inline int xdigit2bin(char c, int delim)
  95. {
  96. if (c == delim || c == '\0')
  97. return IN6PTON_DELIM;
  98. if (c == ':')
  99. return IN6PTON_COLON_MASK;
  100. if (c == '.')
  101. return IN6PTON_DOT;
  102. if (c >= '0' && c <= '9')
  103. return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
  104. if (c >= 'a' && c <= 'f')
  105. return (IN6PTON_XDIGIT | (c - 'a' + 10));
  106. if (c >= 'A' && c <= 'F')
  107. return (IN6PTON_XDIGIT | (c - 'A' + 10));
  108. if (delim == -1)
  109. return IN6PTON_DELIM;
  110. return IN6PTON_UNKNOWN;
  111. }
  112. int in4_pton(const char *src, int srclen,
  113. u8 *dst,
  114. int delim, const char **end)
  115. {
  116. const char *s;
  117. u8 *d;
  118. u8 dbuf[4];
  119. int ret = 0;
  120. int i;
  121. int w = 0;
  122. if (srclen < 0)
  123. srclen = strlen(src);
  124. s = src;
  125. d = dbuf;
  126. i = 0;
  127. while(1) {
  128. int c;
  129. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  130. if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK))) {
  131. goto out;
  132. }
  133. if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  134. if (w == 0)
  135. goto out;
  136. *d++ = w & 0xff;
  137. w = 0;
  138. i++;
  139. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  140. if (i != 4)
  141. goto out;
  142. break;
  143. }
  144. goto cont;
  145. }
  146. w = (w * 10) + c;
  147. if ((w & 0xffff) > 255) {
  148. goto out;
  149. }
  150. cont:
  151. if (i >= 4)
  152. goto out;
  153. s++;
  154. srclen--;
  155. }
  156. ret = 1;
  157. memcpy(dst, dbuf, sizeof(dbuf));
  158. out:
  159. if (end)
  160. *end = s;
  161. return ret;
  162. }
  163. EXPORT_SYMBOL(in4_pton);
  164. int in6_pton(const char *src, int srclen,
  165. u8 *dst,
  166. int delim, const char **end)
  167. {
  168. const char *s, *tok = NULL;
  169. u8 *d, *dc = NULL;
  170. u8 dbuf[16];
  171. int ret = 0;
  172. int i;
  173. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  174. int w = 0;
  175. memset(dbuf, 0, sizeof(dbuf));
  176. s = src;
  177. d = dbuf;
  178. if (srclen < 0)
  179. srclen = strlen(src);
  180. while (1) {
  181. int c;
  182. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  183. if (!(c & state))
  184. goto out;
  185. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  186. /* process one 16-bit word */
  187. if (!(state & IN6PTON_NULL)) {
  188. *d++ = (w >> 8) & 0xff;
  189. *d++ = w & 0xff;
  190. }
  191. w = 0;
  192. if (c & IN6PTON_DELIM) {
  193. /* We've processed last word */
  194. break;
  195. }
  196. /*
  197. * COLON_1 => XDIGIT
  198. * COLON_2 => XDIGIT|DELIM
  199. * COLON_1_2 => COLON_2
  200. */
  201. switch (state & IN6PTON_COLON_MASK) {
  202. case IN6PTON_COLON_2:
  203. dc = d;
  204. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  205. if (dc - dbuf >= sizeof(dbuf))
  206. state |= IN6PTON_NULL;
  207. break;
  208. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  209. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  210. break;
  211. case IN6PTON_COLON_1:
  212. state = IN6PTON_XDIGIT;
  213. break;
  214. case IN6PTON_COLON_1_2:
  215. state = IN6PTON_COLON_2;
  216. break;
  217. default:
  218. state = 0;
  219. }
  220. tok = s + 1;
  221. goto cont;
  222. }
  223. if (c & IN6PTON_DOT) {
  224. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  225. if (ret > 0) {
  226. d += 4;
  227. break;
  228. }
  229. goto out;
  230. }
  231. w = (w << 4) | (0xff & c);
  232. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  233. if (!(w & 0xf000)) {
  234. state |= IN6PTON_XDIGIT;
  235. }
  236. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  237. state |= IN6PTON_COLON_1_2;
  238. state &= ~IN6PTON_DELIM;
  239. }
  240. if (d + 2 >= dbuf + sizeof(dbuf)) {
  241. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  242. }
  243. cont:
  244. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  245. d + 4 == dbuf + sizeof(dbuf)) {
  246. state |= IN6PTON_DOT;
  247. }
  248. if (d >= dbuf + sizeof(dbuf)) {
  249. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  250. }
  251. s++;
  252. srclen--;
  253. }
  254. i = 15; d--;
  255. if (dc) {
  256. while(d >= dc)
  257. dst[i--] = *d--;
  258. while(i >= dc - dbuf)
  259. dst[i--] = 0;
  260. while(i >= 0)
  261. dst[i--] = *d--;
  262. } else
  263. memcpy(dst, dbuf, sizeof(dbuf));
  264. ret = 1;
  265. out:
  266. if (end)
  267. *end = s;
  268. return ret;
  269. }
  270. EXPORT_SYMBOL(in6_pton);