2 * Generic address resultion entity
6 * net_ratelimit Andi Kleen
7 * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
9 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
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.
17 #include <linux/module.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/inet.h>
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/random.h>
26 #include <linux/percpu.h>
27 #include <linux/init.h>
29 #include <asm/byteorder.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 int net_msg_cost __read_mostly
= 5*HZ
;
34 int net_msg_burst __read_mostly
= 10;
35 int net_msg_warn __read_mostly
= 1;
36 EXPORT_SYMBOL(net_msg_warn
);
39 * All net warning printk()s should be guarded by this function.
41 int net_ratelimit(void)
43 return __printk_ratelimit(net_msg_cost
, net_msg_burst
);
45 EXPORT_SYMBOL(net_ratelimit
);
48 * Convert an ASCII string to binary IP.
49 * This is outside of net/ipv4/ because various code that uses IP addresses
50 * is otherwise not dependent on the TCP/IP stack.
53 __be32
in_aton(const char *str
)
60 for (i
= 0; i
< 4; i
++)
66 while (*str
!= '\0' && *str
!= '.' && *str
!= '\n')
80 EXPORT_SYMBOL(in_aton
);
82 #define IN6PTON_XDIGIT 0x00010000
83 #define IN6PTON_DIGIT 0x00020000
84 #define IN6PTON_COLON_MASK 0x00700000
85 #define IN6PTON_COLON_1 0x00100000 /* single : requested */
86 #define IN6PTON_COLON_2 0x00200000 /* second : requested */
87 #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
88 #define IN6PTON_DOT 0x00800000 /* . */
89 #define IN6PTON_DELIM 0x10000000
90 #define IN6PTON_NULL 0x20000000 /* first/tail */
91 #define IN6PTON_UNKNOWN 0x40000000
93 static inline int digit2bin(char c
, int delim
)
95 if (c
== delim
|| c
== '\0')
99 if (c
>= '0' && c
<= '9')
100 return (IN6PTON_DIGIT
| (c
- '0'));
101 return IN6PTON_UNKNOWN
;
104 static inline int xdigit2bin(char c
, int delim
)
106 if (c
== delim
|| c
== '\0')
107 return IN6PTON_DELIM
;
109 return IN6PTON_COLON_MASK
;
112 if (c
>= '0' && c
<= '9')
113 return (IN6PTON_XDIGIT
| IN6PTON_DIGIT
| (c
- '0'));
114 if (c
>= 'a' && c
<= 'f')
115 return (IN6PTON_XDIGIT
| (c
- 'a' + 10));
116 if (c
>= 'A' && c
<= 'F')
117 return (IN6PTON_XDIGIT
| (c
- 'A' + 10));
119 return IN6PTON_DELIM
;
120 return IN6PTON_UNKNOWN
;
123 int in4_pton(const char *src
, int srclen
,
125 int delim
, const char **end
)
135 srclen
= strlen(src
);
141 c
= xdigit2bin(srclen
> 0 ? *s
: '\0', delim
);
142 if (!(c
& (IN6PTON_DIGIT
| IN6PTON_DOT
| IN6PTON_DELIM
| IN6PTON_COLON_MASK
))) {
145 if (c
& (IN6PTON_DOT
| IN6PTON_DELIM
| IN6PTON_COLON_MASK
)) {
151 if (c
& (IN6PTON_DELIM
| IN6PTON_COLON_MASK
)) {
159 if ((w
& 0xffff) > 255) {
169 memcpy(dst
, dbuf
, sizeof(dbuf
));
176 EXPORT_SYMBOL(in4_pton
);
178 int in6_pton(const char *src
, int srclen
,
180 int delim
, const char **end
)
182 const char *s
, *tok
= NULL
;
187 int state
= IN6PTON_COLON_1_2
| IN6PTON_XDIGIT
| IN6PTON_NULL
;
190 memset(dbuf
, 0, sizeof(dbuf
));
195 srclen
= strlen(src
);
200 c
= xdigit2bin(srclen
> 0 ? *s
: '\0', delim
);
203 if (c
& (IN6PTON_DELIM
| IN6PTON_COLON_MASK
)) {
204 /* process one 16-bit word */
205 if (!(state
& IN6PTON_NULL
)) {
206 *d
++ = (w
>> 8) & 0xff;
210 if (c
& IN6PTON_DELIM
) {
211 /* We've processed last word */
216 * COLON_2 => XDIGIT|DELIM
217 * COLON_1_2 => COLON_2
219 switch (state
& IN6PTON_COLON_MASK
) {
220 case IN6PTON_COLON_2
:
222 state
= IN6PTON_XDIGIT
| IN6PTON_DELIM
;
223 if (dc
- dbuf
>= sizeof(dbuf
))
224 state
|= IN6PTON_NULL
;
226 case IN6PTON_COLON_1
|IN6PTON_COLON_1_2
:
227 state
= IN6PTON_XDIGIT
| IN6PTON_COLON_2
;
229 case IN6PTON_COLON_1
:
230 state
= IN6PTON_XDIGIT
;
232 case IN6PTON_COLON_1_2
:
233 state
= IN6PTON_COLON_2
;
242 if (c
& IN6PTON_DOT
) {
243 ret
= in4_pton(tok
? tok
: s
, srclen
+ (int)(s
- tok
), d
, delim
, &s
);
251 w
= (w
<< 4) | (0xff & c
);
252 state
= IN6PTON_COLON_1
| IN6PTON_DELIM
;
254 state
|= IN6PTON_XDIGIT
;
256 if (!dc
&& d
+ 2 < dbuf
+ sizeof(dbuf
)) {
257 state
|= IN6PTON_COLON_1_2
;
258 state
&= ~IN6PTON_DELIM
;
260 if (d
+ 2 >= dbuf
+ sizeof(dbuf
)) {
261 state
&= ~(IN6PTON_COLON_1
|IN6PTON_COLON_1_2
);
264 if ((dc
&& d
+ 4 < dbuf
+ sizeof(dbuf
)) ||
265 d
+ 4 == dbuf
+ sizeof(dbuf
)) {
266 state
|= IN6PTON_DOT
;
268 if (d
>= dbuf
+ sizeof(dbuf
)) {
269 state
&= ~(IN6PTON_XDIGIT
|IN6PTON_COLON_MASK
);
280 while(i
>= dc
- dbuf
)
285 memcpy(dst
, dbuf
, sizeof(dbuf
));
294 EXPORT_SYMBOL(in6_pton
);