utils.c 5.9 KB

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