Home | History | Annotate | Line # | Download | only in pxeboot
dev_net.c revision 1.11
      1  1.11     joerg /*	$NetBSD: dev_net.c,v 1.11 2011/07/17 20:54:42 joerg 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 <sys/param.h>
     46   1.1   thorpej #include <sys/socket.h>
     47   1.1   thorpej #include <net/if.h>
     48   1.1   thorpej #include <netinet/in.h>
     49   1.1   thorpej #include <netinet/in_systm.h>
     50   1.1   thorpej 
     51   1.1   thorpej #include <lib/libkern/libkern.h>
     52   1.1   thorpej 
     53   1.5  drochner #include <lib/libsa/stand.h>
     54   1.5  drochner #include <lib/libsa/net.h>
     55   1.5  drochner #include "pxe_netif.h"
     56   1.1   thorpej #include "dev_net.h"
     57   1.1   thorpej 
     58   1.1   thorpej static int netdev_sock = -1;
     59   1.1   thorpej static int netdev_opens;
     60   1.1   thorpej 
     61   1.8       dsl static int net_getparams(int sock);
     62   1.1   thorpej 
     63   1.1   thorpej /*
     64   1.1   thorpej  * Called by devopen after it sets f->f_dev to our devsw entry.
     65   1.1   thorpej  * This opens the low-level device and sets f->f_devdata.
     66   1.1   thorpej  * This is declared with variable arguments...
     67   1.1   thorpej  */
     68   1.1   thorpej int
     69   1.1   thorpej net_open(struct open_file *f, ...)
     70   1.1   thorpej {
     71   1.1   thorpej 	int error = 0;
     72   1.1   thorpej 
     73   1.1   thorpej #ifdef	NETIF_DEBUG
     74   1.1   thorpej 	if (debug)
     75   1.4  drochner 		printf("net_open\n");
     76   1.1   thorpej #endif
     77   1.1   thorpej 
     78   1.1   thorpej 	/* On first open, do netif open, mount, etc. */
     79   1.1   thorpej 	if (netdev_opens == 0) {
     80   1.1   thorpej 		/* Find network interface. */
     81   1.1   thorpej 		if (netdev_sock < 0) {
     82   1.5  drochner 			netdev_sock = pxe_netif_open();
     83   1.1   thorpej 			if (netdev_sock < 0) {
     84   1.1   thorpej 				printf("net_open: netif_open() failed\n");
     85   1.1   thorpej 				return (ENXIO);
     86   1.1   thorpej 			}
     87   1.1   thorpej 			if (debug)
     88   1.1   thorpej 				printf("net_open: netif_open() succeeded\n");
     89   1.1   thorpej 		}
     90   1.1   thorpej 		if (rootip.s_addr == 0) {
     91   1.1   thorpej 			/* Get root IP address, and path, etc. */
     92   1.1   thorpej 			error = net_getparams(netdev_sock);
     93   1.1   thorpej 			if (error) {
     94   1.1   thorpej 				/* getparams makes its own noise */
     95   1.5  drochner 				pxe_netif_close(netdev_sock);
     96   1.1   thorpej 				netdev_sock = -1;
     97   1.1   thorpej 				return (error);
     98   1.1   thorpej 			}
     99   1.1   thorpej 		}
    100   1.1   thorpej 	}
    101   1.1   thorpej 	netdev_opens++;
    102   1.2  drochner 	f->f_devdata = &netdev_sock;
    103   1.1   thorpej 	return (error);
    104   1.1   thorpej }
    105   1.1   thorpej 
    106   1.1   thorpej int
    107   1.9       dsl net_close(struct open_file *f)
    108   1.1   thorpej {
    109   1.1   thorpej 
    110   1.1   thorpej #ifdef	NETIF_DEBUG
    111   1.1   thorpej 	if (debug)
    112   1.1   thorpej 		printf("net_close: opens=%d\n", netdev_opens);
    113   1.1   thorpej #endif
    114   1.1   thorpej 
    115   1.1   thorpej 	/* On last close, do netif close, etc. */
    116   1.1   thorpej 	f->f_devdata = NULL;
    117   1.1   thorpej 	/* Extra close call? */
    118   1.1   thorpej 	if (netdev_opens <= 0)
    119   1.1   thorpej 		return (0);
    120   1.1   thorpej 	netdev_opens--;
    121   1.1   thorpej 	/* Not last close? */
    122   1.1   thorpej 	if (netdev_opens > 0)
    123   1.1   thorpej 		return(0);
    124   1.1   thorpej 	rootip.s_addr = 0;
    125   1.1   thorpej 	if (netdev_sock >= 0) {
    126   1.1   thorpej 		if (debug)
    127   1.1   thorpej 			printf("net_close: calling netif_close()\n");
    128   1.5  drochner 		pxe_netif_close(netdev_sock);
    129   1.1   thorpej 		netdev_sock = -1;
    130   1.1   thorpej 	}
    131   1.1   thorpej 	return (0);
    132   1.1   thorpej }
    133   1.1   thorpej 
    134   1.1   thorpej int
    135   1.9       dsl net_ioctl(struct open_file *f, u_long cmd, void *data)
    136   1.1   thorpej {
    137   1.1   thorpej 	return EIO;
    138   1.1   thorpej }
    139   1.1   thorpej 
    140   1.1   thorpej int
    141   1.9       dsl net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
    142   1.1   thorpej {
    143   1.1   thorpej 	return EIO;
    144   1.1   thorpej }
    145   1.1   thorpej 
    146   1.1   thorpej 
    147   1.1   thorpej /*
    148   1.5  drochner  * Get info for network boot: our IP address, our hostname,
    149   1.1   thorpej  * server IP address, and our root path on the server.
    150   1.1   thorpej  */
    151   1.1   thorpej #ifdef	SUPPORT_BOOTP
    152   1.8       dsl int bootp(int sock);
    153   1.1   thorpej #endif
    154   1.1   thorpej 
    155   1.1   thorpej static int
    156   1.9       dsl net_getparams(int sock)
    157   1.1   thorpej {
    158   1.3  drochner 
    159   1.1   thorpej #ifdef	SUPPORT_BOOTP
    160   1.1   thorpej 	/*
    161   1.1   thorpej 	 * Try to get boot info using BOOTP.  If we succeed, then
    162   1.1   thorpej 	 * the server IP address, gateway, and root path will all
    163   1.1   thorpej 	 * be initialized.  If any remain uninitialized, we will
    164   1.1   thorpej 	 * use RARP and RPC/bootparam (the Sun way) to get them.
    165   1.1   thorpej 	 */
    166   1.3  drochner 	bootp(sock);
    167   1.1   thorpej 	if (myip.s_addr != 0)
    168   1.1   thorpej 		return (0);
    169   1.1   thorpej 	if (debug)
    170   1.3  drochner 		printf("net_open: BOOTP failed\n");
    171   1.1   thorpej #endif
    172   1.1   thorpej 
    173   1.3  drochner 	return (EIO);
    174   1.1   thorpej }
    175