utils.c 7.8 KB

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