1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 6 * Copyright (c) 2014-2015 François Tigeot 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LINUXKPI_LINUX_KERNEL_H_ 31 #define _LINUXKPI_LINUX_KERNEL_H_ 32 33 #include <sys/cdefs.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/param.h> 37 #include <sys/libkern.h> 38 #include <sys/stat.h> 39 #include <sys/smp.h> 40 #include <sys/stddef.h> 41 #include <sys/syslog.h> 42 #include <sys/time.h> 43 44 #include <linux/bitops.h> 45 #include <linux/build_bug.h> 46 #include <linux/compiler.h> 47 #include <linux/container_of.h> 48 #include <linux/stringify.h> 49 #include <linux/errno.h> 50 #include <linux/sched.h> 51 #include <linux/types.h> 52 #include <linux/typecheck.h> 53 #include <linux/jiffies.h> 54 #include <linux/log2.h> 55 #include <linux/kconfig.h> 56 57 #include <asm/byteorder.h> 58 #include <asm/cpufeature.h> 59 #include <asm/processor.h> 60 #include <asm/uaccess.h> 61 62 #include <linux/stdarg.h> 63 64 #define KERN_CONT "" 65 #define KERN_EMERG "<0>" 66 #define KERN_ALERT "<1>" 67 #define KERN_CRIT "<2>" 68 #define KERN_ERR "<3>" 69 #define KERN_WARNING "<4>" 70 #define KERN_NOTICE "<5>" 71 #define KERN_INFO "<6>" 72 #define KERN_DEBUG "<7>" 73 74 #define U8_MAX ((u8)~0U) 75 #define S8_MAX ((s8)(U8_MAX >> 1)) 76 #define S8_MIN ((s8)(-S8_MAX - 1)) 77 #define U16_MAX ((u16)~0U) 78 #define S16_MAX ((s16)(U16_MAX >> 1)) 79 #define S16_MIN ((s16)(-S16_MAX - 1)) 80 #define U32_MAX ((u32)~0U) 81 #define S32_MAX ((s32)(U32_MAX >> 1)) 82 #define S32_MIN ((s32)(-S32_MAX - 1)) 83 #define U64_MAX ((u64)~0ULL) 84 #define S64_MAX ((s64)(U64_MAX >> 1)) 85 #define S64_MIN ((s64)(-S64_MAX - 1)) 86 87 #define S8_C(x) x 88 #define U8_C(x) x ## U 89 #define S16_C(x) x 90 #define U16_C(x) x ## U 91 #define S32_C(x) x 92 #define U32_C(x) x ## U 93 #define S64_C(x) x ## LL 94 #define U64_C(x) x ## ULL 95 96 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 97 #define BUG_ON(cond) do { \ 98 if (cond) { \ 99 panic("BUG ON %s failed at %s:%d", \ 100 __stringify(cond), __FILE__, __LINE__); \ 101 } \ 102 } while (0) 103 104 extern int linuxkpi_warn_dump_stack; 105 #define WARN_ON(cond) ({ \ 106 bool __ret = (cond); \ 107 if (__ret) { \ 108 printf("WARNING %s failed at %s:%d\n", \ 109 __stringify(cond), __FILE__, __LINE__); \ 110 if (linuxkpi_warn_dump_stack) \ 111 linux_dump_stack(); \ 112 } \ 113 unlikely(__ret); \ 114 }) 115 116 #define WARN_ON_SMP(cond) WARN_ON(cond) 117 118 #define WARN_ON_ONCE(cond) ({ \ 119 static bool __warn_on_once; \ 120 bool __ret = (cond); \ 121 if (__ret && !__warn_on_once) { \ 122 __warn_on_once = 1; \ 123 printf("WARNING %s failed at %s:%d\n", \ 124 __stringify(cond), __FILE__, __LINE__); \ 125 if (linuxkpi_warn_dump_stack) \ 126 linux_dump_stack(); \ 127 } \ 128 unlikely(__ret); \ 129 }) 130 131 #define oops_in_progress SCHEDULER_STOPPED() 132 133 #undef ALIGN 134 #define ALIGN(x, y) roundup2((x), (y)) 135 #define ALIGN_DOWN(x, y) rounddown2(x, y) 136 #undef PTR_ALIGN 137 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 138 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 139 #define DIV_ROUND_UP(x, n) howmany(x, n) 140 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 141 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 142 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 143 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 144 145 #define printk(...) printf(__VA_ARGS__) 146 #define vprintk(f, a) vprintf(f, a) 147 148 #define asm __asm 149 150 extern void linux_dump_stack(void); 151 #define dump_stack() linux_dump_stack() 152 153 struct va_format { 154 const char *fmt; 155 va_list *va; 156 }; 157 158 static inline int 159 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 160 { 161 ssize_t ssize = size; 162 int i; 163 164 i = vsnprintf(buf, size, fmt, args); 165 166 return ((i >= ssize) ? (ssize - 1) : i); 167 } 168 169 static inline int 170 scnprintf(char *buf, size_t size, const char *fmt, ...) 171 { 172 va_list args; 173 int i; 174 175 va_start(args, fmt); 176 i = vscnprintf(buf, size, fmt, args); 177 va_end(args); 178 179 return (i); 180 } 181 182 /* 183 * The "pr_debug()" and "pr_devel()" macros should produce zero code 184 * unless DEBUG is defined: 185 */ 186 #ifdef DEBUG 187 extern int linuxkpi_debug; 188 #define pr_debug(fmt, ...) \ 189 do { \ 190 if (linuxkpi_debug) \ 191 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 192 } while (0) 193 #define pr_devel(fmt, ...) \ 194 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 195 #else 196 #define pr_debug(fmt, ...) \ 197 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 198 #define pr_devel(fmt, ...) \ 199 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 200 #endif 201 202 #ifndef pr_fmt 203 #define pr_fmt(fmt) fmt 204 #endif 205 206 /* 207 * Print a one-time message (analogous to WARN_ONCE() et al): 208 */ 209 #define printk_once(...) do { \ 210 static bool __print_once; \ 211 \ 212 if (!__print_once) { \ 213 __print_once = true; \ 214 printk(__VA_ARGS__); \ 215 } \ 216 } while (0) 217 218 /* 219 * Log a one-time message (analogous to WARN_ONCE() et al): 220 */ 221 #define log_once(level,...) do { \ 222 static bool __log_once; \ 223 \ 224 if (unlikely(!__log_once)) { \ 225 __log_once = true; \ 226 log(level, __VA_ARGS__); \ 227 } \ 228 } while (0) 229 230 #define pr_emerg(fmt, ...) \ 231 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 232 #define pr_alert(fmt, ...) \ 233 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 234 #define pr_crit(fmt, ...) \ 235 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 236 #define pr_err(fmt, ...) \ 237 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 238 #define pr_err_once(fmt, ...) \ 239 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 240 #define pr_warning(fmt, ...) \ 241 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 242 #define pr_warn(...) \ 243 pr_warning(__VA_ARGS__) 244 #define pr_warn_once(fmt, ...) \ 245 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 246 #define pr_notice(fmt, ...) \ 247 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 248 #define pr_info(fmt, ...) \ 249 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 250 #define pr_info_once(fmt, ...) \ 251 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 252 #define pr_cont(fmt, ...) \ 253 printk(KERN_CONT fmt, ##__VA_ARGS__) 254 #define pr_warn_ratelimited(...) do { \ 255 static linux_ratelimit_t __ratelimited; \ 256 if (linux_ratelimited(&__ratelimited)) \ 257 pr_warning(__VA_ARGS__); \ 258 } while (0) 259 260 #ifndef WARN 261 #define WARN(condition, ...) ({ \ 262 bool __ret_warn_on = (condition); \ 263 if (unlikely(__ret_warn_on)) \ 264 pr_warning(__VA_ARGS__); \ 265 unlikely(__ret_warn_on); \ 266 }) 267 #endif 268 269 #ifndef WARN_ONCE 270 #define WARN_ONCE(condition, ...) ({ \ 271 bool __ret_warn_on = (condition); \ 272 if (unlikely(__ret_warn_on)) \ 273 pr_warn_once(__VA_ARGS__); \ 274 unlikely(__ret_warn_on); \ 275 }) 276 #endif 277 278 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 279 280 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 281 282 #define _RET_IP_ __builtin_return_address(0) 283 284 static inline unsigned long long 285 simple_strtoull(const char *cp, char **endp, unsigned int base) 286 { 287 return (strtouq(cp, endp, base)); 288 } 289 290 static inline long long 291 simple_strtoll(const char *cp, char **endp, unsigned int base) 292 { 293 return (strtoq(cp, endp, base)); 294 } 295 296 static inline unsigned long 297 simple_strtoul(const char *cp, char **endp, unsigned int base) 298 { 299 return (strtoul(cp, endp, base)); 300 } 301 302 static inline long 303 simple_strtol(const char *cp, char **endp, unsigned int base) 304 { 305 return (strtol(cp, endp, base)); 306 } 307 308 static inline int 309 kstrtoul(const char *cp, unsigned int base, unsigned long *res) 310 { 311 char *end; 312 313 *res = strtoul(cp, &end, base); 314 315 /* skip newline character, if any */ 316 if (*end == '\n') 317 end++; 318 if (*cp == 0 || *end != 0) 319 return (-EINVAL); 320 return (0); 321 } 322 323 static inline int 324 kstrtol(const char *cp, unsigned int base, long *res) 325 { 326 char *end; 327 328 *res = strtol(cp, &end, base); 329 330 /* skip newline character, if any */ 331 if (*end == '\n') 332 end++; 333 if (*cp == 0 || *end != 0) 334 return (-EINVAL); 335 return (0); 336 } 337 338 static inline int 339 kstrtoint(const char *cp, unsigned int base, int *res) 340 { 341 char *end; 342 long temp; 343 344 *res = temp = strtol(cp, &end, base); 345 346 /* skip newline character, if any */ 347 if (*end == '\n') 348 end++; 349 if (*cp == 0 || *end != 0) 350 return (-EINVAL); 351 if (temp != (int)temp) 352 return (-ERANGE); 353 return (0); 354 } 355 356 static inline int 357 kstrtouint(const char *cp, unsigned int base, unsigned int *res) 358 { 359 char *end; 360 unsigned long temp; 361 362 *res = temp = strtoul(cp, &end, base); 363 364 /* skip newline character, if any */ 365 if (*end == '\n') 366 end++; 367 if (*cp == 0 || *end != 0) 368 return (-EINVAL); 369 if (temp != (unsigned int)temp) 370 return (-ERANGE); 371 return (0); 372 } 373 374 static inline int 375 kstrtou8(const char *cp, unsigned int base, u8 *res) 376 { 377 char *end; 378 unsigned long temp; 379 380 *res = temp = strtoul(cp, &end, base); 381 382 /* skip newline character, if any */ 383 if (*end == '\n') 384 end++; 385 if (*cp == 0 || *end != 0) 386 return (-EINVAL); 387 if (temp != (u8)temp) 388 return (-ERANGE); 389 return (0); 390 } 391 392 static inline int 393 kstrtou16(const char *cp, unsigned int base, u16 *res) 394 { 395 char *end; 396 unsigned long temp; 397 398 *res = temp = strtoul(cp, &end, base); 399 400 /* skip newline character, if any */ 401 if (*end == '\n') 402 end++; 403 if (*cp == 0 || *end != 0) 404 return (-EINVAL); 405 if (temp != (u16)temp) 406 return (-ERANGE); 407 return (0); 408 } 409 410 static inline int 411 kstrtou32(const char *cp, unsigned int base, u32 *res) 412 { 413 414 return (kstrtouint(cp, base, res)); 415 } 416 417 static inline int 418 kstrtou64(const char *cp, unsigned int base, u64 *res) 419 { 420 char *end; 421 422 *res = strtouq(cp, &end, base); 423 424 /* skip newline character, if any */ 425 if (*end == '\n') 426 end++; 427 if (*cp == 0 || *end != 0) 428 return (-EINVAL); 429 return (0); 430 } 431 432 static inline int 433 kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 434 { 435 return (kstrtou64(cp, base, (u64 *)res)); 436 } 437 438 static inline int 439 kstrtobool(const char *s, bool *res) 440 { 441 int len; 442 443 if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 444 return (-EINVAL); 445 446 /* skip newline character, if any */ 447 if (s[len - 1] == '\n') 448 len--; 449 450 if (len == 1 && strchr("yY1", s[0]) != NULL) 451 *res = true; 452 else if (len == 1 && strchr("nN0", s[0]) != NULL) 453 *res = false; 454 else if (strncasecmp("on", s, len) == 0) 455 *res = true; 456 else if (strncasecmp("off", s, len) == 0) 457 *res = false; 458 else 459 return (-EINVAL); 460 461 return (0); 462 } 463 464 static inline int 465 kstrtobool_from_user(const char __user *s, size_t count, bool *res) 466 { 467 char buf[8] = {}; 468 469 if (count > (sizeof(buf) - 1)) 470 count = (sizeof(buf) - 1); 471 472 if (copy_from_user(buf, s, count)) 473 return (-EFAULT); 474 475 return (kstrtobool(buf, res)); 476 } 477 478 static inline int 479 kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, 480 int *p) 481 { 482 char buf[36] = {}; 483 484 if (count > (sizeof(buf) - 1)) 485 count = (sizeof(buf) - 1); 486 487 if (copy_from_user(buf, s, count)) 488 return (-EFAULT); 489 490 return (kstrtoint(buf, base, p)); 491 } 492 493 static inline int 494 kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, 495 unsigned int *p) 496 { 497 char buf[36] = {}; 498 499 if (count > (sizeof(buf) - 1)) 500 count = (sizeof(buf) - 1); 501 502 if (copy_from_user(buf, s, count)) 503 return (-EFAULT); 504 505 return (kstrtouint(buf, base, p)); 506 } 507 508 static inline int 509 kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, 510 unsigned int *p) 511 { 512 513 return (kstrtouint_from_user(s, count, base, p)); 514 } 515 516 static inline int 517 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 518 u8 *p) 519 { 520 char buf[8] = {}; 521 522 if (count > (sizeof(buf) - 1)) 523 count = (sizeof(buf) - 1); 524 525 if (copy_from_user(buf, s, count)) 526 return (-EFAULT); 527 528 return (kstrtou8(buf, base, p)); 529 } 530 531 #define min(x, y) ((x) < (y) ? (x) : (y)) 532 #define max(x, y) ((x) > (y) ? (x) : (y)) 533 534 #define min3(a, b, c) min(a, min(b,c)) 535 #define max3(a, b, c) max(a, max(b,c)) 536 537 #define min_t(type, x, y) ({ \ 538 type __min1 = (x); \ 539 type __min2 = (y); \ 540 __min1 < __min2 ? __min1 : __min2; }) 541 542 #define max_t(type, x, y) ({ \ 543 type __max1 = (x); \ 544 type __max2 = (y); \ 545 __max1 > __max2 ? __max1 : __max2; }) 546 547 #define offsetofend(t, m) \ 548 (offsetof(t, m) + sizeof((((t *)0)->m))) 549 550 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 551 #define clamp(x, lo, hi) min( max(x,lo), hi) 552 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 553 554 /* 555 * This looks more complex than it should be. But we need to 556 * get the type for the ~ right in round_down (it needs to be 557 * as wide as the result!), and we want to evaluate the macro 558 * arguments just once each. 559 */ 560 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 561 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 562 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 563 564 #define smp_processor_id() PCPU_GET(cpuid) 565 #define num_possible_cpus() mp_ncpus 566 #define num_online_cpus() mp_ncpus 567 568 #if defined(__i386__) || defined(__amd64__) 569 extern bool linux_cpu_has_clflush; 570 #define cpu_has_clflush linux_cpu_has_clflush 571 #endif 572 573 /* Swap values of a and b */ 574 #define swap(a, b) do { \ 575 typeof(a) _swap_tmp = a; \ 576 a = b; \ 577 b = _swap_tmp; \ 578 } while (0) 579 580 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 581 582 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 583 __typeof(divisor) __d = (divisor); \ 584 unsigned long long __ret = (x) + (__d) / 2; \ 585 __ret /= __d; \ 586 __ret; \ 587 }) 588 589 static inline uintmax_t 590 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 591 { 592 uintmax_t q = (x / divisor); 593 uintmax_t r = (x % divisor); 594 595 return ((q * multiplier) + ((r * multiplier) / divisor)); 596 } 597 598 typedef struct linux_ratelimit { 599 struct timeval lasttime; 600 int counter; 601 } linux_ratelimit_t; 602 603 static inline bool 604 linux_ratelimited(linux_ratelimit_t *rl) 605 { 606 return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 607 } 608 609 #define struct_size(ptr, field, num) ({ \ 610 const size_t __size = offsetof(__typeof(*(ptr)), field); \ 611 const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 612 ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 613 }) 614 615 #define __is_constexpr(x) \ 616 __builtin_constant_p(x) 617 618 /* 619 * The is_signed() macro below returns true if the passed data type is 620 * signed. Else false is returned. 621 */ 622 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 623 624 /* 625 * The type_max() macro below returns the maxium positive value the 626 * passed data type can hold. 627 */ 628 #define type_max(datatype) ( \ 629 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 630 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 631 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 632 (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 633 ) 634 635 /* 636 * The type_min() macro below returns the minimum value the passed 637 * data type can hold. For unsigned types the minimum value is always 638 * zero. For signed types it may vary. 639 */ 640 #define type_min(datatype) ( \ 641 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 642 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 643 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 644 (is_signed(datatype) ? INT8_MIN : 0) \ 645 ) 646 647 #define TAINT_WARN 0 648 #define test_taint(x) (0) 649 #define add_taint(x,y) do { \ 650 } while (0) 651 652 static inline int 653 _h2b(const char c) 654 { 655 656 if (c >= '0' && c <= '9') 657 return (c - '0'); 658 if (c >= 'a' && c <= 'f') 659 return (10 + c - 'a'); 660 if (c >= 'A' && c <= 'F') 661 return (10 + c - 'A'); 662 return (-EINVAL); 663 } 664 665 static inline int 666 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 667 { 668 int hi4, lo4; 669 670 while (binlen > 0) { 671 hi4 = _h2b(*hexsrc++); 672 lo4 = _h2b(*hexsrc++); 673 if (hi4 < 0 || lo4 < 0) 674 return (-EINVAL); 675 676 *bindst++ = (hi4 << 4) | lo4; 677 binlen--; 678 } 679 680 return (0); 681 } 682 683 static inline bool 684 mac_pton(const char *macin, uint8_t *macout) 685 { 686 const char *s, *d; 687 uint8_t mac[6], hx, lx;; 688 int i; 689 690 if (strlen(macin) < (3 * 6 - 1)) 691 return (false); 692 693 i = 0; 694 s = macin; 695 do { 696 /* Should we also support '-'-delimiters? */ 697 d = strchrnul(s, ':'); 698 hx = lx = 0; 699 while (s < d) { 700 /* Fail on abc:123:xxx:... */ 701 if ((d - s) > 2) 702 return (false); 703 /* We do support non-well-formed strings: 3:45:6:... */ 704 if ((d - s) > 1) { 705 hx = _h2b(*s); 706 if (hx < 0) 707 return (false); 708 s++; 709 } 710 lx = _h2b(*s); 711 if (lx < 0) 712 return (false); 713 s++; 714 } 715 mac[i] = (hx << 4) | lx; 716 i++; 717 if (i >= 6) 718 return (false); 719 } while (d != NULL && *d != '\0'); 720 721 memcpy(macout, mac, 6); 722 return (true); 723 } 724 725 #define DECLARE_FLEX_ARRAY(_t, _n) \ 726 struct { struct { } __dummy_ ## _n; _t _n[0]; } 727 728 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 729