utils.c 7.3 KB

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