1e7a69ba65675f48b4a65305c3e1ac169fa70042
[compat-rdma/compat.git] / compat / compat-3.0.c
1 /*
2  * Copyright 2011    Hauke Mehrtens <hauke@hauke-m.de>
3  * Copyright 2011    Alexey Dobriyan <adobriyan@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Compatibility file for Linux wireless for kernels 3.0.
10  */
11
12 #ifndef CONFIG_COMPAT_RHEL_6_4
13
14 #include <linux/compat.h>
15 #include <linux/if_ether.h>
16
17 int mac_pton(const char *s, u8 *mac)
18 {
19         int i;
20
21         /* XX:XX:XX:XX:XX:XX */
22         if (strlen(s) < 3 * ETH_ALEN - 1)
23                 return 0;
24
25         /* Don't dirty result unless string is valid MAC. */
26         for (i = 0; i < ETH_ALEN; i++) {
27                 if (!strchr("0123456789abcdefABCDEF", s[i * 3]))
28                         return 0;
29                 if (!strchr("0123456789abcdefABCDEF", s[i * 3 + 1]))
30                         return 0;
31                 if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':')
32                         return 0;
33         }
34         for (i = 0; i < ETH_ALEN; i++) {
35                 mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]);
36         }
37         return 1;
38 }
39 EXPORT_SYMBOL_GPL(mac_pton);
40
41 #define kstrto_from_user(f, g, type)                                    \
42 int f(const char __user *s, size_t count, unsigned int base, type *res) \
43 {                                                                       \
44         /* sign, base 2 representation, newline, terminator */          \
45         char buf[1 + sizeof(type) * 8 + 1 + 1];                         \
46                                                                         \
47         count = min(count, sizeof(buf) - 1);                            \
48         if (copy_from_user(buf, s, count))                              \
49                 return -EFAULT;                                         \
50         buf[count] = '\0';                                              \
51         return g(buf, base, res);                                       \
52 }                                                                       \
53 EXPORT_SYMBOL_GPL(f)
54
55 kstrto_from_user(kstrtoull_from_user,   kstrtoull,      unsigned long long);
56 kstrto_from_user(kstrtoll_from_user,    kstrtoll,       long long);
57 kstrto_from_user(kstrtoul_from_user,    kstrtoul,       unsigned long);
58 kstrto_from_user(kstrtol_from_user,     kstrtol,        long);
59 kstrto_from_user(kstrtouint_from_user,  kstrtouint,     unsigned int);
60 kstrto_from_user(kstrtoint_from_user,   kstrtoint,      int);
61 kstrto_from_user(kstrtou16_from_user,   kstrtou16,      u16);
62 kstrto_from_user(kstrtos16_from_user,   kstrtos16,      s16);
63 kstrto_from_user(kstrtou8_from_user,    kstrtou8,       u8);
64 kstrto_from_user(kstrtos8_from_user,    kstrtos8,       s8);
65
66 #endif /* CONFIG_COMPAT_RHEL_6_4 */