Deleted Added
full compact
subr_ndis.c (131953) subr_ndis.c (132973)
1/*
2 * Copyright (c) 2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 17 unchanged lines hidden (view full) ---

26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
1/*
2 * Copyright (c) 2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 17 unchanged lines hidden (view full) ---

26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/compat/ndis/subr_ndis.c 131953 2004-07-11 00:19:30Z wpaul $");
34__FBSDID("$FreeBSD: head/sys/compat/ndis/subr_ndis.c 132973 2004-08-01 20:04:31Z wpaul $");
35
36/*
37 * This file implements a translation layer between the BSD networking
38 * infrasturcture and Windows(R) NDIS network driver modules. A Windows
39 * NDIS driver calls into several functions in the NDIS.SYS Windows
40 * kernel module and exports a table of functions designed to be called
41 * by the NDIS subsystem. Using the PE loader, we can patch our own
42 * versions of the NDIS routines into a given Windows driver module and

--- 23 unchanged lines hidden (view full) ---

66#include <sys/smp.h>
67#include <sys/queue.h>
68#include <sys/proc.h>
69#include <sys/filedesc.h>
70#include <sys/namei.h>
71#include <sys/fcntl.h>
72#include <sys/vnode.h>
73#include <sys/kthread.h>
35
36/*
37 * This file implements a translation layer between the BSD networking
38 * infrasturcture and Windows(R) NDIS network driver modules. A Windows
39 * NDIS driver calls into several functions in the NDIS.SYS Windows
40 * kernel module and exports a table of functions designed to be called
41 * by the NDIS subsystem. Using the PE loader, we can patch our own
42 * versions of the NDIS routines into a given Windows driver module and

--- 23 unchanged lines hidden (view full) ---

66#include <sys/smp.h>
67#include <sys/queue.h>
68#include <sys/proc.h>
69#include <sys/filedesc.h>
70#include <sys/namei.h>
71#include <sys/fcntl.h>
72#include <sys/vnode.h>
73#include <sys/kthread.h>
74#include <sys/linker.h>
75#include <sys/mount.h>
76#include <sys/sysproto.h>
74
75#include <net/if.h>
76#include <net/if_arp.h>
77#include <net/ethernet.h>
78#include <net/if_dl.h>
79#include <net/if_media.h>
80
81#include <machine/atomic.h>

--- 173 unchanged lines hidden (view full) ---

255__stdcall static void ndis_termwrap(ndis_handle, void *);
256__stdcall static void ndis_get_devprop(ndis_handle, device_object **,
257 device_object **, device_object **, cm_resource_list *,
258 cm_resource_list *);
259__stdcall static void ndis_firstbuf(ndis_packet *, ndis_buffer **,
260 void **, uint32_t *, uint32_t *);
261__stdcall static void ndis_firstbuf_safe(ndis_packet *, ndis_buffer **,
262 void **, uint32_t *, uint32_t *, uint32_t);
77
78#include <net/if.h>
79#include <net/if_arp.h>
80#include <net/ethernet.h>
81#include <net/if_dl.h>
82#include <net/if_media.h>
83
84#include <machine/atomic.h>

--- 173 unchanged lines hidden (view full) ---

258__stdcall static void ndis_termwrap(ndis_handle, void *);
259__stdcall static void ndis_get_devprop(ndis_handle, device_object **,
260 device_object **, device_object **, cm_resource_list *,
261 cm_resource_list *);
262__stdcall static void ndis_firstbuf(ndis_packet *, ndis_buffer **,
263 void **, uint32_t *, uint32_t *);
264__stdcall static void ndis_firstbuf_safe(ndis_packet *, ndis_buffer **,
265 void **, uint32_t *, uint32_t *, uint32_t);
266static int ndis_find_sym(linker_file_t, char *, char *, caddr_t *);
263__stdcall static void ndis_open_file(ndis_status *, ndis_handle *, uint32_t *,
264 ndis_unicode_string *, ndis_physaddr);
265__stdcall static void ndis_map_file(ndis_status *, void **, ndis_handle);
266__stdcall static void ndis_unmap_file(ndis_handle);
267__stdcall static void ndis_close_file(ndis_handle);
268__stdcall static u_int8_t ndis_cpu_cnt(void);
269__stdcall static void ndis_ind_statusdone(ndis_handle);
270__stdcall static void ndis_ind_status(ndis_handle, ndis_status,

--- 296 unchanged lines hidden (view full) ---

567 * not to use pointer arithmetic when passing it arguments.
568 */
569
570 while(1) {
571 a = *s1;
572 b = *s2++;
573 if (toupper(a) != toupper(b))
574 break;
267__stdcall static void ndis_open_file(ndis_status *, ndis_handle *, uint32_t *,
268 ndis_unicode_string *, ndis_physaddr);
269__stdcall static void ndis_map_file(ndis_status *, void **, ndis_handle);
270__stdcall static void ndis_unmap_file(ndis_handle);
271__stdcall static void ndis_close_file(ndis_handle);
272__stdcall static u_int8_t ndis_cpu_cnt(void);
273__stdcall static void ndis_ind_statusdone(ndis_handle);
274__stdcall static void ndis_ind_status(ndis_handle, ndis_status,

--- 296 unchanged lines hidden (view full) ---

571 * not to use pointer arithmetic when passing it arguments.
572 */
573
574 while(1) {
575 a = *s1;
576 b = *s2++;
577 if (toupper(a) != toupper(b))
578 break;
575 if (*s1++ == 0)
579 if (*s1++ == '\0')
576 return(0);
577 }
578
579 return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
580}
581
580 return(0);
581 }
582
583 return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
584}
585
586int
587ndis_strncasecmp(s1, s2, n)
588 const char *s1;
589 const char *s2;
590 size_t n;
591{
592 char a, b;
593
594 if (n != 0) {
595 do {
596 a = *s1;
597 b = *s2++;
598 if (toupper(a) != toupper(b))
599 return (*(const unsigned char *)s1 -
600 *(const unsigned char *)(s2 - 1));
601 if (*s1++ == '\0')
602 break;
603 } while (--n != 0);
604 }
605
606 return(0);
607}
608
582__stdcall static void
583ndis_read_cfg(status, parm, cfg, key, type)
584 ndis_status *status;
585 ndis_config_parm **parm;
586 ndis_handle cfg;
587 ndis_unicode_string *key;
588 ndis_parm_type type;
589{

--- 1938 unchanged lines hidden (view full) ---

2528 void **firstva;
2529 uint32_t *firstlen;
2530 uint32_t *totlen;
2531 uint32_t prio;
2532{
2533 ndis_firstbuf(packet, buf, firstva, firstlen, totlen);
2534}
2535
609__stdcall static void
610ndis_read_cfg(status, parm, cfg, key, type)
611 ndis_status *status;
612 ndis_config_parm **parm;
613 ndis_handle cfg;
614 ndis_unicode_string *key;
615 ndis_parm_type type;
616{

--- 1938 unchanged lines hidden (view full) ---

2555 void **firstva;
2556 uint32_t *firstlen;
2557 uint32_t *totlen;
2558 uint32_t prio;
2559{
2560 ndis_firstbuf(packet, buf, firstva, firstlen, totlen);
2561}
2562
2563static int
2564ndis_find_sym(lf, filename, suffix, sym)
2565 linker_file_t lf;
2566 char *filename;
2567 char *suffix;
2568 caddr_t *sym;
2569{
2570 char fullsym[MAXPATHLEN];
2571 int i;
2572
2573 bzero(fullsym, sizeof(fullsym));
2574 strcpy(fullsym, filename);
2575 for (i = 0; i < strlen(fullsym); i++) {
2576 if (fullsym[i] == '.')
2577 fullsym[i] = '_';
2578 else
2579 fullsym[i] = tolower(fullsym[i]);
2580 }
2581 strcat(fullsym, suffix);
2582 *sym = linker_file_lookup_symbol(lf, fullsym, 0);
2583 if (*sym == 0)
2584 return(ENOENT);
2585
2586 return(0);
2587}
2588
2536/* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2537__stdcall static void
2538ndis_open_file(status, filehandle, filelength, filename, highestaddr)
2539 ndis_status *status;
2540 ndis_handle *filehandle;
2541 uint32_t *filelength;
2542 ndis_unicode_string *filename;
2543 ndis_physaddr highestaddr;
2544{
2545 char *afilename = NULL;
2546 struct thread *td = curthread;
2547 struct nameidata nd;
2548 int flags, error;
2549 struct vattr vat;
2550 struct vattr *vap = &vat;
2551 ndis_fh *fh;
2552 char path[MAXPATHLEN];
2589/* can also return NDIS_STATUS_RESOURCES/NDIS_STATUS_ERROR_READING_FILE */
2590__stdcall static void
2591ndis_open_file(status, filehandle, filelength, filename, highestaddr)
2592 ndis_status *status;
2593 ndis_handle *filehandle;
2594 uint32_t *filelength;
2595 ndis_unicode_string *filename;
2596 ndis_physaddr highestaddr;
2597{
2598 char *afilename = NULL;
2599 struct thread *td = curthread;
2600 struct nameidata nd;
2601 int flags, error;
2602 struct vattr vat;
2603 struct vattr *vap = &vat;
2604 ndis_fh *fh;
2605 char path[MAXPATHLEN];
2606 linker_file_t head, lf;
2607 caddr_t kldstart, kldend;
2553
2554 ndis_unicode_to_ascii(filename->nus_buf,
2555 filename->nus_len, &afilename);
2556
2608
2609 ndis_unicode_to_ascii(filename->nus_buf,
2610 filename->nus_len, &afilename);
2611
2557 sprintf(path, "%s/%s", ndis_filepath, afilename);
2558 free(afilename, M_DEVBUF);
2559
2560 fh = malloc(sizeof(ndis_fh), M_TEMP, M_NOWAIT);
2561 if (fh == NULL) {
2562 *status = NDIS_STATUS_RESOURCES;
2563 return;
2564 }
2565
2612 fh = malloc(sizeof(ndis_fh), M_TEMP, M_NOWAIT);
2613 if (fh == NULL) {
2614 *status = NDIS_STATUS_RESOURCES;
2615 return;
2616 }
2617
2618 /*
2619 * During system bootstrap, it's impossible to load files
2620 * from the rootfs since it's not mounted yet. We therefore
2621 * offer the possibility of opening files that have been
2622 * preloaded as modules instead. Both choices will work
2623 * when kldloading a module from multiuser, but only the
2624 * module option will work during bootstrap. The module
2625 * loading option works by using the ndiscvt(8) utility
2626 * to convert the arbitrary file into a .ko using objcopy(1).
2627 * This file will contain two special symbols: filename_start
2628 * and filename_end. All we have to do is traverse the KLD
2629 * list in search of those symbols and we've found the file
2630 * data. As an added bonus, ndiscvt(8) will also generate
2631 * a normal .o file which can be linked statically with
2632 * the kernel. This means that the symbols will actual reside
2633 * in the kernel's symbol table, but that doesn't matter to
2634 * us since the kernel appears to us as just another module.
2635 */
2636
2637 /*
2638 * This is an evil trick for getting the head of the linked
2639 * file list, which is not exported from kern_linker.o. It
2640 * happens that linker file #1 is always the kernel, and is
2641 * always the first element in the list.
2642 */
2643
2644 head = linker_find_file_by_id(1);
2645 for (lf = head; lf != NULL; lf = TAILQ_NEXT(lf, link)) {
2646 if (ndis_find_sym(lf, afilename, "_start", &kldstart))
2647 continue;
2648 if (ndis_find_sym(lf, afilename, "_end", &kldend))
2649 continue;
2650 fh->nf_vp = lf;
2651 fh->nf_type = NDIS_FH_TYPE_MODULE;
2652 fh->nf_map = kldstart;
2653 *filelength = fh->nf_maplen = (kldend - kldstart) & 0xFFFFFFFF;
2654 *filehandle = fh;
2655 free(afilename, M_DEVBUF);
2656 *status = NDIS_STATUS_SUCCESS;
2657 return;
2658 }
2659
2660 if (TAILQ_EMPTY(&mountlist)) {
2661 free(fh, M_TEMP);
2662 *status = NDIS_STATUS_FILE_NOT_FOUND;
2663 printf("NDIS: could not find file %s in linker list\n",
2664 afilename);
2665 printf("NDIS: and no filesystems mounted yet, "
2666 "aborting NdisOpenFile()\n");
2667 free(afilename, M_DEVBUF);
2668 return;
2669 }
2670
2671 sprintf(path, "%s/%s", ndis_filepath, afilename);
2672 free(afilename, M_DEVBUF);
2673
2566 mtx_lock(&Giant);
2567
2568 /* Some threads don't have a current working directory. */
2569
2570 if (td->td_proc->p_fd->fd_rdir == NULL)
2571 td->td_proc->p_fd->fd_rdir = rootvnode;
2572 if (td->td_proc->p_fd->fd_cdir == NULL)
2573 td->td_proc->p_fd->fd_cdir = rootvnode;

--- 14 unchanged lines hidden (view full) ---

2588
2589 /* Get the file size. */
2590 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
2591 VOP_UNLOCK(nd.ni_vp, 0, td);
2592 mtx_unlock(&Giant);
2593
2594 fh->nf_vp = nd.ni_vp;
2595 fh->nf_map = NULL;
2674 mtx_lock(&Giant);
2675
2676 /* Some threads don't have a current working directory. */
2677
2678 if (td->td_proc->p_fd->fd_rdir == NULL)
2679 td->td_proc->p_fd->fd_rdir = rootvnode;
2680 if (td->td_proc->p_fd->fd_cdir == NULL)
2681 td->td_proc->p_fd->fd_cdir = rootvnode;

--- 14 unchanged lines hidden (view full) ---

2696
2697 /* Get the file size. */
2698 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred, td);
2699 VOP_UNLOCK(nd.ni_vp, 0, td);
2700 mtx_unlock(&Giant);
2701
2702 fh->nf_vp = nd.ni_vp;
2703 fh->nf_map = NULL;
2704 fh->nf_type = NDIS_FH_TYPE_VFS;
2596 *filehandle = fh;
2597 *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF;
2598 *status = NDIS_STATUS_SUCCESS;
2599
2600 return;
2601}
2602
2603__stdcall static void

--- 18 unchanged lines hidden (view full) ---

2622 return;
2623 }
2624
2625 if (fh->nf_map != NULL) {
2626 *status = NDIS_STATUS_ALREADY_MAPPED;
2627 return;
2628 }
2629
2705 *filehandle = fh;
2706 *filelength = fh->nf_maplen = vap->va_size & 0xFFFFFFFF;
2707 *status = NDIS_STATUS_SUCCESS;
2708
2709 return;
2710}
2711
2712__stdcall static void

--- 18 unchanged lines hidden (view full) ---

2731 return;
2732 }
2733
2734 if (fh->nf_map != NULL) {
2735 *status = NDIS_STATUS_ALREADY_MAPPED;
2736 return;
2737 }
2738
2739 if (fh->nf_type == NDIS_FH_TYPE_MODULE) {
2740 /* Already found the mapping address during the open. */
2741 *status = NDIS_STATUS_SUCCESS;
2742 *mappedbuffer = fh->nf_map;
2743 return;
2744 }
2745
2630 fh->nf_map = malloc(fh->nf_maplen, M_DEVBUF, M_NOWAIT);
2631
2632 if (fh->nf_map == NULL) {
2633 *status = NDIS_STATUS_RESOURCES;
2634 return;
2635 }
2636
2637 mtx_lock(&Giant);

--- 15 unchanged lines hidden (view full) ---

2653ndis_unmap_file(filehandle)
2654 ndis_handle filehandle;
2655{
2656 ndis_fh *fh;
2657 fh = (ndis_fh *)filehandle;
2658
2659 if (fh->nf_map == NULL)
2660 return;
2746 fh->nf_map = malloc(fh->nf_maplen, M_DEVBUF, M_NOWAIT);
2747
2748 if (fh->nf_map == NULL) {
2749 *status = NDIS_STATUS_RESOURCES;
2750 return;
2751 }
2752
2753 mtx_lock(&Giant);

--- 15 unchanged lines hidden (view full) ---

2769ndis_unmap_file(filehandle)
2770 ndis_handle filehandle;
2771{
2772 ndis_fh *fh;
2773 fh = (ndis_fh *)filehandle;
2774
2775 if (fh->nf_map == NULL)
2776 return;
2661 free(fh->nf_map, M_DEVBUF);
2777
2778 if (fh->nf_type == NDIS_FH_TYPE_VFS)
2779 free(fh->nf_map, M_DEVBUF);
2662 fh->nf_map = NULL;
2663
2664 return;
2665}
2666
2667__stdcall static void
2668ndis_close_file(filehandle)
2669 ndis_handle filehandle;
2670{
2671 struct thread *td = curthread;
2672 ndis_fh *fh;
2673
2674 if (filehandle == NULL)
2675 return;
2676
2677 fh = (ndis_fh *)filehandle;
2678 if (fh->nf_map != NULL) {
2780 fh->nf_map = NULL;
2781
2782 return;
2783}
2784
2785__stdcall static void
2786ndis_close_file(filehandle)
2787 ndis_handle filehandle;
2788{
2789 struct thread *td = curthread;
2790 ndis_fh *fh;
2791
2792 if (filehandle == NULL)
2793 return;
2794
2795 fh = (ndis_fh *)filehandle;
2796 if (fh->nf_map != NULL) {
2679 free(fh->nf_map, M_DEVBUF);
2797 if (fh->nf_type == NDIS_FH_TYPE_VFS)
2798 free(fh->nf_map, M_DEVBUF);
2680 fh->nf_map = NULL;
2681 }
2682
2683 if (fh->nf_vp == NULL)
2684 return;
2685
2799 fh->nf_map = NULL;
2800 }
2801
2802 if (fh->nf_vp == NULL)
2803 return;
2804
2686 mtx_lock(&Giant);
2687 vn_close(fh->nf_vp, FREAD, td->td_ucred, td);
2688 mtx_unlock(&Giant);
2805 if (fh->nf_type == NDIS_FH_TYPE_VFS) {
2806 mtx_lock(&Giant);
2807 vn_close(fh->nf_vp, FREAD, td->td_ucred, td);
2808 mtx_unlock(&Giant);
2809 }
2689
2690 fh->nf_vp = NULL;
2691 free(fh, M_DEVBUF);
2692
2693 return;
2694}
2695
2696__stdcall static uint8_t

--- 349 unchanged lines hidden ---
2810
2811 fh->nf_vp = NULL;
2812 free(fh, M_DEVBUF);
2813
2814 return;
2815}
2816
2817__stdcall static uint8_t

--- 349 unchanged lines hidden ---