utils.c 5.7 KB

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