utils.c 6.7 KB

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