utils.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/ctype.h>
  20. #include <linux/inet.h>
  21. #include <linux/mm.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/types.h>
  25. #include <linux/percpu.h>
  26. #include <linux/init.h>
  27. #include <linux/ratelimit.h>
  28. #include <net/sock.h>
  29. #include <net/net_ratelimit.h>
  30. #include <asm/byteorder.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. l <<= 8;
  56. if (*str != '\0') {
  57. val = 0;
  58. while (*str != '\0' && *str != '.' && *str != '\n') {
  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 xdigit2bin(char c, int delim)
  82. {
  83. int val;
  84. if (c == delim || c == '\0')
  85. return IN6PTON_DELIM;
  86. if (c == ':')
  87. return IN6PTON_COLON_MASK;
  88. if (c == '.')
  89. return IN6PTON_DOT;
  90. val = hex_to_bin(c);
  91. if (val >= 0)
  92. return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0);
  93. if (delim == -1)
  94. return IN6PTON_DELIM;
  95. return IN6PTON_UNKNOWN;
  96. }
  97. /**
  98. * in4_pton - convert an IPv4 address from literal to binary representation
  99. * @src: the start of the IPv4 address string
  100. * @srclen: the length of the string, -1 means strlen(src)
  101. * @dst: the binary (u8[4] array) representation of the IPv4 address
  102. * @delim: the delimiter of the IPv4 address in @src, -1 means no delimiter
  103. * @end: A pointer to the end of the parsed string will be placed here
  104. *
  105. * Return one on success, return zero when any error occurs
  106. * and @end will point to the end of the parsed string.
  107. *
  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 | IN6PTON_COLON_MASK))) {
  128. goto out;
  129. }
  130. if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  131. if (w == 0)
  132. goto out;
  133. *d++ = w & 0xff;
  134. w = 0;
  135. i++;
  136. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  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. /**
  162. * in6_pton - convert an IPv6 address from literal to binary representation
  163. * @src: the start of the IPv6 address string
  164. * @srclen: the length of the string, -1 means strlen(src)
  165. * @dst: the binary (u8[16] array) representation of the IPv6 address
  166. * @delim: the delimiter of the IPv6 address in @src, -1 means no delimiter
  167. * @end: A pointer to the end of the parsed string will be placed here
  168. *
  169. * Return one on success, return zero when any error occurs
  170. * and @end will point to the end of the parsed string.
  171. *
  172. */
  173. int in6_pton(const char *src, int srclen,
  174. u8 *dst,
  175. int delim, const char **end)
  176. {
  177. const char *s, *tok = NULL;
  178. u8 *d, *dc = NULL;
  179. u8 dbuf[16];
  180. int ret = 0;
  181. int i;
  182. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  183. int w = 0;
  184. memset(dbuf, 0, sizeof(dbuf));
  185. s = src;
  186. d = dbuf;
  187. if (srclen < 0)
  188. srclen = strlen(src);
  189. while (1) {
  190. int c;
  191. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  192. if (!(c & state))
  193. goto out;
  194. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  195. /* process one 16-bit word */
  196. if (!(state & IN6PTON_NULL)) {
  197. *d++ = (w >> 8) & 0xff;
  198. *d++ = w & 0xff;
  199. }
  200. w = 0;
  201. if (c & IN6PTON_DELIM) {
  202. /* We've processed last word */
  203. break;
  204. }
  205. /*
  206. * COLON_1 => XDIGIT
  207. * COLON_2 => XDIGIT|DELIM
  208. * COLON_1_2 => COLON_2
  209. */
  210. switch (state & IN6PTON_COLON_MASK) {
  211. case IN6PTON_COLON_2:
  212. dc = d;
  213. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  214. if (dc - dbuf >= sizeof(dbuf))
  215. state |= IN6PTON_NULL;
  216. break;
  217. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  218. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  219. break;
  220. case IN6PTON_COLON_1:
  221. state = IN6PTON_XDIGIT;
  222. break;
  223. case IN6PTON_COLON_1_2:
  224. state = IN6PTON_COLON_2;
  225. break;
  226. default:
  227. state = 0;
  228. }
  229. tok = s + 1;
  230. goto cont;
  231. }
  232. if (c & IN6PTON_DOT) {
  233. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  234. if (ret > 0) {
  235. d += 4;
  236. break;
  237. }
  238. goto out;
  239. }
  240. w = (w << 4) | (0xff & c);
  241. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  242. if (!(w & 0xf000)) {
  243. state |= IN6PTON_XDIGIT;
  244. }
  245. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  246. state |= IN6PTON_COLON_1_2;
  247. state &= ~IN6PTON_DELIM;
  248. }
  249. if (d + 2 >= dbuf + sizeof(dbuf)) {
  250. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  251. }
  252. cont:
  253. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  254. d + 4 == dbuf + sizeof(dbuf)) {
  255. state |= IN6PTON_DOT;
  256. }
  257. if (d >= dbuf + sizeof(dbuf)) {
  258. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  259. }
  260. s++;
  261. srclen--;
  262. }
  263. i = 15; d--;
  264. if (dc) {
  265. while(d >= dc)
  266. dst[i--] = *d--;
  267. while(i >= dc - dbuf)
  268. dst[i--] = 0;
  269. while(i >= 0)
  270. dst[i--] = *d--;
  271. } else
  272. memcpy(dst, dbuf, sizeof(dbuf));
  273. ret = 1;
  274. out:
  275. if (end)
  276. *end = s;
  277. return ret;
  278. }
  279. EXPORT_SYMBOL(in6_pton);
  280. void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  281. __be32 from, __be32 to, int pseudohdr)
  282. {
  283. __be32 diff[] = { ~from, to };
  284. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  285. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  286. ~csum_unfold(*sum)));
  287. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  288. skb->csum = ~csum_partial(diff, sizeof(diff),
  289. ~skb->csum);
  290. } else if (pseudohdr)
  291. *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
  292. csum_unfold(*sum)));
  293. }
  294. EXPORT_SYMBOL(inet_proto_csum_replace4);
  295. void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
  296. const __be32 *from, const __be32 *to,
  297. int pseudohdr)
  298. {
  299. __be32 diff[] = {
  300. ~from[0], ~from[1], ~from[2], ~from[3],
  301. to[0], to[1], to[2], to[3],
  302. };
  303. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  304. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  305. ~csum_unfold(*sum)));
  306. if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
  307. skb->csum = ~csum_partial(diff, sizeof(diff),
  308. ~skb->csum);
  309. } else if (pseudohdr)
  310. *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
  311. csum_unfold(*sum)));
  312. }
  313. EXPORT_SYMBOL(inet_proto_csum_replace16);
  314. int mac_pton(const char *s, u8 *mac)
  315. {
  316. int i;
  317. /* XX:XX:XX:XX:XX:XX */
  318. if (strlen(s) < 3 * ETH_ALEN - 1)
  319. return 0;
  320. /* Don't dirty result unless string is valid MAC. */
  321. for (i = 0; i < ETH_ALEN; i++) {
  322. if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1]))
  323. return 0;
  324. if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
  325. return 0;
  326. }
  327. for (i = 0; i < ETH_ALEN; i++) {
  328. mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
  329. }
  330. return 1;
  331. }
  332. EXPORT_SYMBOL(mac_pton);