utils.c 6.6 KB

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