utils.c 8.3 KB

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