1 /* $NetBSD: dev_net.c,v 1.12 2002/02/24 01:51:04 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Gordon W. Ross 5 * All rights reserved. 6 * Copyright (c) 1996 7 * Matthias Drochner. 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, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 4. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by Gordon W. Ross 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * network device for libsa 37 * supports BOOTP, RARP and BOOTPARAM 38 */ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <net/if.h> 43 #include <netinet/in.h> 44 #include <netinet/in_systm.h> 45 46 #include <lib/libsa/stand.h> 47 #include <lib/libsa/net.h> 48 #include <lib/libsa/bootparam.h> 49 #include <machine/stdarg.h> 50 51 #include <netif/netif_small.h> 52 53 #include <lib/libkern/libkern.h> 54 55 #include "dev_net.h" 56 57 #ifdef SUPPORT_BOOTP 58 void bootp __P((int)); 59 #endif 60 61 static int netdev_sock = -1; 62 63 /* 64 * Called by devopen after it sets f->f_dev to our devsw entry. 65 * This opens the low-level device and sets f->f_devdata. 66 */ 67 int 68 net_open(struct open_file *f, ...) 69 { 70 int error = 0; 71 72 #ifdef NET_DEBUG 73 if (netdev_sock != -1) 74 panic("net_open"); 75 #endif 76 77 /* Find network interface. */ 78 if ((netdev_sock = netif_open()) < 0) 79 return (ENXIO); 80 81 #ifdef SUPPORT_BOOTP 82 printf("configure network...trying bootp\n"); 83 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 84 bootp(netdev_sock); 85 #endif 86 87 if (myip.s_addr != INADDR_ANY) { /* got bootp reply or 88 * manually set */ 89 90 #ifdef TFTP_HACK 91 int num, i; 92 /* XXX (some) tftp servers don't like leading "/" */ 93 for (num = 0; bootfile[num] == '/'; num++); 94 for (i = 0; (bootfile[i] = bootfile[i + num]) != 0; i++); 95 #endif 96 97 printf("boot: client IP address: %s\n", 98 inet_ntoa(myip)); 99 printf("boot: client name: %s\n", hostname); 100 } else { 101 102 #ifdef SUPPORT_RARP 103 /* 104 * no answer, Get boot info using RARP and Sun 105 * bootparams. 106 */ 107 printf("configure network...trying rarp\n"); 108 109 /* Get our IP address. (rarp.c) */ 110 if (rarp_getipaddress(netdev_sock)) { 111 error = EIO; 112 goto bad; 113 } 114 printf("boot: client IP address: %s\n", 115 inet_ntoa(myip)); 116 117 #ifdef SUPPORT_BOOTPARAM 118 /* Get our hostname, server IP address. */ 119 if (!bp_whoami(netdev_sock)) { 120 printf("boot: client name: %s\n", hostname); 121 122 /* Get the root pathname. */ 123 bp_getfile(netdev_sock, "root", &rootip, 124 rootpath); 125 } 126 #else 127 /* 128 * else fallback: use rarp server address 129 */ 130 #endif 131 132 #else /* no SUPPORT_RARP */ 133 error = EIO; 134 goto bad; 135 #endif 136 137 } 138 printf("boot: server: %s, rootpath: %s, bootfile: %s\n", 139 inet_ntoa(rootip), rootpath, bootfile); 140 141 f->f_devdata = &netdev_sock; 142 return (error); 143 144 bad: 145 printf("net_open failed\n"); 146 netif_close(netdev_sock); 147 return (error); 148 } 149 150 int 151 net_close(f) 152 struct open_file *f; 153 { 154 #ifdef NET_DEBUG 155 if (netdev_sock == -1) 156 panic("net_close"); 157 #endif 158 159 netif_close(netdev_sock); 160 netdev_sock = -1; 161 162 f->f_devdata = NULL; 163 164 return (0); 165 } 166 167 int 168 net_ioctl(f, c, d) 169 struct open_file *f; 170 u_long c; 171 void *d; 172 { 173 return EIO; 174 } 175 176 int 177 net_strategy(d, f, b, s, buf, r) 178 void *d; 179 int f; 180 daddr_t b; 181 size_t s; 182 void *buf; 183 size_t *r; 184 { 185 return EIO; 186 } 187