utils.c 5.8 KB

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