Home | History | Annotate | Line # | Download | only in pxeboot
dev_net.c revision 1.7.12.1
      1  1.7.12.1       jym /*	$NetBSD: dev_net.c,v 1.7.12.1 2009/05/13 17:17:52 jym 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  *
     19       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   thorpej  */
     31       1.1   thorpej 
     32       1.1   thorpej /*
     33       1.1   thorpej  * This module implements a "raw device" interface suitable for
     34       1.4  drochner  * use by the stand-alone I/O library NFS and TFTP code.  This interface
     35       1.1   thorpej  * does not support any "block" access, and exists only for the
     36       1.4  drochner  * purpose of initializing the network interface and getting boot
     37       1.4  drochner  * parameters.
     38       1.1   thorpej  *
     39       1.1   thorpej  * At open time, this does:
     40       1.1   thorpej  *
     41       1.4  drochner  * find interface       - netif_open()
     42       1.4  drochner  * BOOTP for IP address - bootp()
     43       1.1   thorpej  */
     44       1.1   thorpej 
     45       1.1   thorpej #include <machine/stdarg.h>
     46       1.1   thorpej #include <sys/param.h>
     47       1.1   thorpej #include <sys/socket.h>
     48       1.1   thorpej #include <net/if.h>
     49       1.1   thorpej #include <netinet/in.h>
     50       1.1   thorpej #include <netinet/in_systm.h>
     51       1.1   thorpej 
     52       1.1   thorpej #include <lib/libkern/libkern.h>
     53       1.1   thorpej 
     54       1.5  drochner #include <lib/libsa/stand.h>
     55       1.5  drochner #include <lib/libsa/net.h>
     56       1.5  drochner #include "pxe_netif.h"
     57       1.1   thorpej #include "dev_net.h"
     58       1.1   thorpej 
     59       1.1   thorpej static int netdev_sock = -1;
     60       1.1   thorpej static int netdev_opens;
     61       1.1   thorpej 
     62  1.7.12.1       jym static int net_getparams(int sock);
     63       1.1   thorpej 
     64       1.1   thorpej /*
     65       1.1   thorpej  * Called by devopen after it sets f->f_dev to our devsw entry.
     66       1.1   thorpej  * This opens the low-level device and sets f->f_devdata.
     67       1.1   thorpej  * This is declared with variable arguments...
     68       1.1   thorpej  */
     69       1.1   thorpej int
     70       1.1   thorpej net_open(struct open_file *f, ...)
     71       1.1   thorpej {
     72       1.1   thorpej 	int error = 0;
     73       1.1   thorpej 
     74       1.1   thorpej #ifdef	NETIF_DEBUG
     75       1.1   thorpej 	if (debug)
     76       1.4  drochner 		printf("net_open\n");
     77       1.1   thorpej #endif
     78       1.1   thorpej 
     79       1.1   thorpej 	/* On first open, do netif open, mount, etc. */
     80       1.1   thorpej 	if (netdev_opens == 0) {
     81       1.1   thorpej 		/* Find network interface. */
     82       1.1   thorpej 		if (netdev_sock < 0) {
     83       1.5  drochner 			netdev_sock = pxe_netif_open();
     84       1.1   thorpej 			if (netdev_sock < 0) {
     85       1.1   thorpej 				printf("net_open: netif_open() failed\n");
     86       1.1   thorpej 				return (ENXIO);
     87       1.1   thorpej 			}
     88       1.1   thorpej 			if (debug)
     89       1.1   thorpej 				printf("net_open: netif_open() succeeded\n");
     90       1.1   thorpej 		}
     91       1.1   thorpej 		if (rootip.s_addr == 0) {
     92       1.1   thorpej 			/* Get root IP address, and path, etc. */
     93       1.1   thorpej 			error = net_getparams(netdev_sock);
     94       1.1   thorpej 			if (error) {
     95       1.1   thorpej 				/* getparams makes its own noise */
     96       1.5  drochner 				pxe_netif_close(netdev_sock);
     97       1.1   thorpej 				netdev_sock = -1;
     98       1.1   thorpej 				return (error);
     99       1.1   thorpej 			}
    100       1.1   thorpej 		}
    101       1.1   thorpej 	}
    102       1.1   thorpej 	netdev_opens++;
    103       1.2  drochner 	f->f_devdata = &netdev_sock;
    104       1.1   thorpej 	return (error);
    105       1.1   thorpej }
    106       1.1   thorpej 
    107       1.1   thorpej int
    108  1.7.12.1       jym net_close(struct open_file *f)
    109       1.1   thorpej {
    110       1.1   thorpej 
    111       1.1   thorpej #ifdef	NETIF_DEBUG
    112       1.1   thorpej 	if (debug)
    113       1.1   thorpej 		printf("net_close: opens=%d\n", netdev_opens);
    114       1.1   thorpej #endif
    115       1.1   thorpej 
    116       1.1   thorpej 	/* On last close, do netif close, etc. */
    117       1.1   thorpej 	f->f_devdata = NULL;
    118       1.1   thorpej 	/* Extra close call? */
    119       1.1   thorpej 	if (netdev_opens <= 0)
    120       1.1   thorpej 		return (0);
    121       1.1   thorpej 	netdev_opens--;
    122       1.1   thorpej 	/* Not last close? */
    123       1.1   thorpej 	if (netdev_opens > 0)
    124       1.1   thorpej 		return(0);
    125       1.1   thorpej 	rootip.s_addr = 0;
    126       1.1   thorpej 	if (netdev_sock >= 0) {
    127       1.1   thorpej 		if (debug)
    128       1.1   thorpej 			printf("net_close: calling netif_close()\n");
    129       1.5  drochner 		pxe_netif_close(netdev_sock);
    130       1.7     chris 		//pxe_netif_shutdown(); /* XXX shouldn't be done here */
    131       1.1   thorpej 		netdev_sock = -1;
    132       1.1   thorpej 	}
    133       1.1   thorpej 	return (0);
    134       1.1   thorpej }
    135       1.1   thorpej 
    136       1.1   thorpej int
    137  1.7.12.1       jym net_ioctl(struct open_file *f, u_long cmd, void *data)
    138       1.1   thorpej {
    139       1.1   thorpej 	return EIO;
    140       1.1   thorpej }
    141       1.1   thorpej 
    142       1.1   thorpej int
    143  1.7.12.1       jym net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
    144       1.1   thorpej {
    145       1.1   thorpej 	return EIO;
    146       1.1   thorpej }
    147       1.1   thorpej 
    148       1.1   thorpej 
    149       1.1   thorpej /*
    150       1.5  drochner  * Get info for network boot: our IP address, our hostname,
    151       1.1   thorpej  * server IP address, and our root path on the server.
    152       1.1   thorpej  */
    153       1.1   thorpej #ifdef	SUPPORT_BOOTP
    154  1.7.12.1       jym int bootp(int sock);
    155       1.1   thorpej #endif
    156       1.1   thorpej 
    157       1.1   thorpej static int
    158  1.7.12.1       jym net_getparams(int sock)
    159       1.1   thorpej {
    160       1.3  drochner 
    161       1.1   thorpej #ifdef	SUPPORT_BOOTP
    162       1.1   thorpej 	/*
    163       1.1   thorpej 	 * Try to get boot info using BOOTP.  If we succeed, then
    164       1.1   thorpej 	 * the server IP address, gateway, and root path will all
    165       1.1   thorpej 	 * be initialized.  If any remain uninitialized, we will
    166       1.1   thorpej 	 * use RARP and RPC/bootparam (the Sun way) to get them.
    167       1.1   thorpej 	 */
    168       1.3  drochner 	bootp(sock);
    169       1.1   thorpej 	if (myip.s_addr != 0)
    170       1.1   thorpej 		return (0);
    171       1.1   thorpej 	if (debug)
    172       1.3  drochner 		printf("net_open: BOOTP failed\n");
    173       1.1   thorpej #endif
    174       1.1   thorpej 
    175       1.3  drochner 	return (EIO);
    176       1.1   thorpej }
    177