Home | History | Annotate | Line # | Download | only in pxeboot
dev_net.c revision 1.4
      1  1.4  drochner /*	$NetBSD: dev_net.c,v 1.4 2003/03/12 13:15:08 drochner Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*-
      4  1.1   thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1   thorpej  * All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   thorpej  * by Gordon W. Ross.
      9  1.1   thorpej  *
     10  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1   thorpej  * modification, are permitted provided that the following conditions
     12  1.1   thorpej  * are met:
     13  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1   thorpej  *    must display the following acknowledgement:
     20  1.1   thorpej  *        This product includes software developed by the NetBSD
     21  1.1   thorpej  *        Foundation, Inc. and its contributors.
     22  1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1   thorpej  *    from this software without specific prior written permission.
     25  1.1   thorpej  *
     26  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1   thorpej  */
     38  1.1   thorpej 
     39  1.1   thorpej /*
     40  1.1   thorpej  * This module implements a "raw device" interface suitable for
     41  1.4  drochner  * use by the stand-alone I/O library NFS and TFTP code.  This interface
     42  1.1   thorpej  * does not support any "block" access, and exists only for the
     43  1.4  drochner  * purpose of initializing the network interface and getting boot
     44  1.4  drochner  * parameters.
     45  1.1   thorpej  *
     46  1.1   thorpej  * At open time, this does:
     47  1.1   thorpej  *
     48  1.4  drochner  * find interface       - netif_open()
     49  1.4  drochner  * BOOTP for IP address - bootp()
     50  1.1   thorpej  */
     51  1.1   thorpej 
     52  1.1   thorpej #include <machine/stdarg.h>
     53  1.1   thorpej #include <sys/param.h>
     54  1.1   thorpej #include <sys/socket.h>
     55  1.1   thorpej #include <net/if.h>
     56  1.1   thorpej #include <netinet/in.h>
     57  1.1   thorpej #include <netinet/in_systm.h>
     58  1.1   thorpej 
     59  1.1   thorpej #include <lib/libkern/libkern.h>
     60  1.1   thorpej 
     61  1.1   thorpej #include "stand.h"
     62  1.1   thorpej #include "net.h"
     63  1.1   thorpej #include "netif.h"
     64  1.1   thorpej #include "nfs.h"
     65  1.1   thorpej #include "dev_net.h"
     66  1.1   thorpej 
     67  1.1   thorpej static int netdev_sock = -1;
     68  1.1   thorpej static int netdev_opens;
     69  1.1   thorpej 
     70  1.1   thorpej static int net_getparams __P((int sock));
     71  1.1   thorpej 
     72  1.1   thorpej /*
     73  1.1   thorpej  * Called by devopen after it sets f->f_dev to our devsw entry.
     74  1.1   thorpej  * This opens the low-level device and sets f->f_devdata.
     75  1.1   thorpej  * This is declared with variable arguments...
     76  1.1   thorpej  */
     77  1.1   thorpej int
     78  1.1   thorpej net_open(struct open_file *f, ...)
     79  1.1   thorpej {
     80  1.1   thorpej 	int error = 0;
     81  1.1   thorpej 
     82  1.1   thorpej #ifdef	NETIF_DEBUG
     83  1.1   thorpej 	if (debug)
     84  1.4  drochner 		printf("net_open\n");
     85  1.1   thorpej #endif
     86  1.1   thorpej 
     87  1.1   thorpej 	/* On first open, do netif open, mount, etc. */
     88  1.1   thorpej 	if (netdev_opens == 0) {
     89  1.1   thorpej 		/* Find network interface. */
     90  1.1   thorpej 		if (netdev_sock < 0) {
     91  1.4  drochner 			netdev_sock = netif_open(0);
     92  1.1   thorpej 			if (netdev_sock < 0) {
     93  1.1   thorpej 				printf("net_open: netif_open() failed\n");
     94  1.1   thorpej 				return (ENXIO);
     95  1.1   thorpej 			}
     96  1.1   thorpej 			if (debug)
     97  1.1   thorpej 				printf("net_open: netif_open() succeeded\n");
     98  1.1   thorpej 		}
     99  1.1   thorpej 		if (rootip.s_addr == 0) {
    100  1.1   thorpej 			/* Get root IP address, and path, etc. */
    101  1.1   thorpej 			error = net_getparams(netdev_sock);
    102  1.1   thorpej 			if (error) {
    103  1.1   thorpej 				/* getparams makes its own noise */
    104  1.1   thorpej 				netif_close(netdev_sock);
    105  1.1   thorpej 				netdev_sock = -1;
    106  1.1   thorpej 				return (error);
    107  1.1   thorpej 			}
    108  1.1   thorpej 		}
    109  1.1   thorpej 	}
    110  1.1   thorpej 	netdev_opens++;
    111  1.2  drochner 	f->f_devdata = &netdev_sock;
    112  1.1   thorpej 	return (error);
    113  1.1   thorpej }
    114  1.1   thorpej 
    115  1.1   thorpej int
    116  1.1   thorpej net_close(f)
    117  1.1   thorpej 	struct open_file *f;
    118  1.1   thorpej {
    119  1.1   thorpej 
    120  1.1   thorpej #ifdef	NETIF_DEBUG
    121  1.1   thorpej 	if (debug)
    122  1.1   thorpej 		printf("net_close: opens=%d\n", netdev_opens);
    123  1.1   thorpej #endif
    124  1.1   thorpej 
    125  1.1   thorpej 	/* On last close, do netif close, etc. */
    126  1.1   thorpej 	f->f_devdata = NULL;
    127  1.1   thorpej 	/* Extra close call? */
    128  1.1   thorpej 	if (netdev_opens <= 0)
    129  1.1   thorpej 		return (0);
    130  1.1   thorpej 	netdev_opens--;
    131  1.1   thorpej 	/* Not last close? */
    132  1.1   thorpej 	if (netdev_opens > 0)
    133  1.1   thorpej 		return(0);
    134  1.1   thorpej 	rootip.s_addr = 0;
    135  1.1   thorpej 	if (netdev_sock >= 0) {
    136  1.1   thorpej 		if (debug)
    137  1.1   thorpej 			printf("net_close: calling netif_close()\n");
    138  1.1   thorpej 		netif_close(netdev_sock);
    139  1.1   thorpej 		netdev_sock = -1;
    140  1.1   thorpej 	}
    141  1.1   thorpej 	return (0);
    142  1.1   thorpej }
    143  1.1   thorpej 
    144  1.1   thorpej int
    145  1.1   thorpej net_ioctl(f, cmd, data)
    146  1.1   thorpej 	struct open_file *f;
    147  1.1   thorpej 	u_long cmd;
    148  1.1   thorpej 	void *data;
    149  1.1   thorpej {
    150  1.1   thorpej 	return EIO;
    151  1.1   thorpej }
    152  1.1   thorpej 
    153  1.1   thorpej int
    154  1.1   thorpej net_strategy(devdata, rw, blk, size, buf, rsize)
    155  1.1   thorpej 	void *devdata;
    156  1.1   thorpej 	int rw;
    157  1.1   thorpej 	daddr_t blk;
    158  1.1   thorpej 	size_t size;
    159  1.1   thorpej 	void *buf;
    160  1.1   thorpej 	size_t *rsize;
    161  1.1   thorpej {
    162  1.1   thorpej 	return EIO;
    163  1.1   thorpej }
    164  1.1   thorpej 
    165  1.1   thorpej 
    166  1.1   thorpej /*
    167  1.1   thorpej  * Get info for NFS boot: our IP address, our hostname,
    168  1.1   thorpej  * server IP address, and our root path on the server.
    169  1.1   thorpej  */
    170  1.1   thorpej #ifdef	SUPPORT_BOOTP
    171  1.1   thorpej int bootp __P((int sock));
    172  1.1   thorpej #endif
    173  1.1   thorpej 
    174  1.1   thorpej static int
    175  1.1   thorpej net_getparams(sock)
    176  1.1   thorpej 	int sock;
    177  1.1   thorpej {
    178  1.3  drochner 
    179  1.1   thorpej #ifdef	SUPPORT_BOOTP
    180  1.1   thorpej 	/*
    181  1.1   thorpej 	 * Try to get boot info using BOOTP.  If we succeed, then
    182  1.1   thorpej 	 * the server IP address, gateway, and root path will all
    183  1.1   thorpej 	 * be initialized.  If any remain uninitialized, we will
    184  1.1   thorpej 	 * use RARP and RPC/bootparam (the Sun way) to get them.
    185  1.1   thorpej 	 */
    186  1.3  drochner 	bootp(sock);
    187  1.1   thorpej 	if (myip.s_addr != 0)
    188  1.1   thorpej 		return (0);
    189  1.1   thorpej 	if (debug)
    190  1.3  drochner 		printf("net_open: BOOTP failed\n");
    191  1.1   thorpej #endif
    192  1.1   thorpej 
    193  1.3  drochner 	return (EIO);
    194  1.1   thorpej }
    195