Home | History | Annotate | Line # | Download | only in libsa
ip.c revision 1.1
      1  1.1  zoltan /* $NetBSD: ip.c,v 1.1 2011/05/11 16:23:40 zoltan Exp $ */
      2  1.1  zoltan 
      3  1.1  zoltan /*
      4  1.1  zoltan  * Copyright (c) 1992 Regents of the University of California.
      5  1.1  zoltan  * Copyright (c) 2010 Zoltan Arnold NAGY
      6  1.1  zoltan  * All rights reserved.
      7  1.1  zoltan  *
      8  1.1  zoltan  * This software was developed by the Computer Systems Engineering group
      9  1.1  zoltan  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     10  1.1  zoltan  * contributed to Berkeley.
     11  1.1  zoltan  *
     12  1.1  zoltan  * Redistribution and use in source and binary forms, with or without
     13  1.1  zoltan  * modification, are permitted provided that the following conditions
     14  1.1  zoltan  * are met:
     15  1.1  zoltan  * 1. Redistributions of source code must retain the above copyright
     16  1.1  zoltan  *    notice, this list of conditions and the following disclaimer.
     17  1.1  zoltan  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1  zoltan  *    notice, this list of conditions and the following disclaimer in the
     19  1.1  zoltan  *    documentation and/or other materials provided with the distribution.
     20  1.1  zoltan  * 3. All advertising materials mentioning features or use of this software
     21  1.1  zoltan  *    must display the following acknowledgement:
     22  1.1  zoltan  *	This product includes software developed by the University of
     23  1.1  zoltan  *	California, Lawrence Berkeley Laboratory and its contributors.
     24  1.1  zoltan  * 4. Neither the name of the University nor the names of its contributors
     25  1.1  zoltan  *    may be used to endorse or promote products derived from this software
     26  1.1  zoltan  *    without specific prior written permission.
     27  1.1  zoltan  *
     28  1.1  zoltan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1  zoltan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1  zoltan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1  zoltan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1  zoltan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1  zoltan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1  zoltan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1  zoltan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1  zoltan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1  zoltan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1  zoltan  * SUCH DAMAGE.
     39  1.1  zoltan  *
     40  1.1  zoltan  */
     41  1.1  zoltan 
     42  1.1  zoltan #include <sys/param.h>
     43  1.1  zoltan #include <sys/socket.h>
     44  1.1  zoltan #include <net/if.h>
     45  1.1  zoltan #include <net/if_ether.h>
     46  1.1  zoltan #include <netinet/in.h>
     47  1.1  zoltan #include <netinet/in_systm.h>
     48  1.1  zoltan #include <netinet/ip.h>
     49  1.1  zoltan #include <netinet/ip_var.h>
     50  1.1  zoltan #include <netinet/udp.h>
     51  1.1  zoltan #include <netinet/udp_var.h>
     52  1.1  zoltan 
     53  1.1  zoltan #ifdef _STANDALONE
     54  1.1  zoltan #include <lib/libkern/libkern.h>
     55  1.1  zoltan #else
     56  1.1  zoltan #include <string.h>
     57  1.1  zoltan #endif
     58  1.1  zoltan 
     59  1.1  zoltan #include "stand.h"
     60  1.1  zoltan #include "net.h"
     61  1.1  zoltan 
     62  1.1  zoltan /*
     63  1.1  zoltan  * sends an IP packet, if it's alredy constructed
     64  1.1  zoltan */
     65  1.1  zoltan static ssize_t
     66  1.1  zoltan _sendip(struct iodesc * d, struct ip * ip, size_t len)
     67  1.1  zoltan {
     68  1.1  zoltan 	u_char *ea;
     69  1.1  zoltan 
     70  1.1  zoltan 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
     71  1.1  zoltan 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) {
     72  1.1  zoltan 		ea = arpwhohas(d, ip->ip_dst);
     73  1.1  zoltan 	} else {
     74  1.1  zoltan 		ea = arpwhohas(d, gateip);
     75  1.1  zoltan 	}
     76  1.1  zoltan 
     77  1.1  zoltan 	return sendether(d, ip, len, ea, ETHERTYPE_IP);
     78  1.1  zoltan }
     79  1.1  zoltan 
     80  1.1  zoltan /*
     81  1.1  zoltan  * fills out the IP header
     82  1.1  zoltan  * Caller must leave room for ethernet, ip and udp headers in front!
     83  1.1  zoltan */
     84  1.1  zoltan ssize_t
     85  1.1  zoltan sendip(struct iodesc * d, void *pkt, size_t len, u_int8_t proto)
     86  1.1  zoltan {
     87  1.1  zoltan 	ssize_t cc;
     88  1.1  zoltan 	struct ip *ip;
     89  1.1  zoltan 
     90  1.1  zoltan 	ip = (struct ip *) pkt - 1;
     91  1.1  zoltan 	len += sizeof(*ip);
     92  1.1  zoltan 
     93  1.1  zoltan 	(void) memset(ip, 0, sizeof(*ip));
     94  1.1  zoltan 
     95  1.1  zoltan 	ip->ip_v = IPVERSION;
     96  1.1  zoltan 	ip->ip_hl = sizeof(*ip) >> 2;
     97  1.1  zoltan 	ip->ip_len = htons(len);
     98  1.1  zoltan 	ip->ip_p = proto;
     99  1.1  zoltan 	ip->ip_ttl = IPDEFTTL;
    100  1.1  zoltan 	ip->ip_src = d->myip;
    101  1.1  zoltan 	ip->ip_dst = d->destip;
    102  1.1  zoltan 	ip->ip_sum = ip_cksum(ip, sizeof(*ip));
    103  1.1  zoltan 
    104  1.1  zoltan 	cc = _sendip(d, ip, len);
    105  1.1  zoltan 
    106  1.1  zoltan 	if (cc == -1)
    107  1.1  zoltan 		return -1;
    108  1.1  zoltan 	if ((size_t) cc != len)
    109  1.1  zoltan 		panic("sendip: bad write (%d != %d)", cc, len);
    110  1.1  zoltan 	return (cc - (sizeof(*ip)));
    111  1.1  zoltan }
    112  1.1  zoltan 
    113  1.1  zoltan /*
    114  1.1  zoltan  * reads an IP packet
    115  1.1  zoltan  * WARNING: the old version stripped the IP options, if there were
    116  1.1  zoltan  * any. Because we have absolutely no idea if the upper layer needs
    117  1.1  zoltan  * these or not, it's best to leave them there.
    118  1.1  zoltan  *
    119  1.1  zoltan  * The size returned is the size indicated in the header.
    120  1.1  zoltan  */
    121  1.1  zoltan ssize_t
    122  1.1  zoltan readip(struct iodesc * d, void *pkt, size_t len, time_t tleft, u_int8_t proto)
    123  1.1  zoltan {
    124  1.1  zoltan 	ssize_t n;
    125  1.1  zoltan 	size_t hlen;
    126  1.1  zoltan 	struct ip *ip;
    127  1.1  zoltan 	u_int16_t etype;
    128  1.1  zoltan 
    129  1.1  zoltan 	ip = (struct ip *) pkt - 1;
    130  1.1  zoltan 
    131  1.1  zoltan 	n = readether(d, ip, len + sizeof(*ip), tleft, &etype);
    132  1.1  zoltan 	if (n == -1 || (size_t) n < sizeof(*ip))
    133  1.1  zoltan 		return -1;
    134  1.1  zoltan 
    135  1.1  zoltan 	if (etype == ETHERTYPE_ARP) {
    136  1.1  zoltan 		struct arphdr *ah = (void *) ip;
    137  1.1  zoltan 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
    138  1.1  zoltan 			/* Send ARP reply */
    139  1.1  zoltan 			arp_reply(d, ah);
    140  1.1  zoltan 		}
    141  1.1  zoltan 		return -1;
    142  1.1  zoltan 	}
    143  1.1  zoltan 
    144  1.1  zoltan 	if (etype != ETHERTYPE_IP) {
    145  1.1  zoltan #ifdef NET_DEBUG
    146  1.1  zoltan 		if (debug)
    147  1.1  zoltan 			printf("readip: not IP. ether_type=%x\n", etype);
    148  1.1  zoltan #endif
    149  1.1  zoltan 		return -1;
    150  1.1  zoltan 	}
    151  1.1  zoltan 
    152  1.1  zoltan 	/* Check ip header */
    153  1.1  zoltan 	if (ip->ip_v != IPVERSION ||
    154  1.1  zoltan 	    ip->ip_p != proto) { /* half char */
    155  1.1  zoltan #ifdef NET_DEBUG
    156  1.1  zoltan 		if (debug) {
    157  1.1  zoltan 			printf("readip: wrong IP version or wrong proto "
    158  1.1  zoltan 			        "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
    159  1.1  zoltan 		}
    160  1.1  zoltan #endif
    161  1.1  zoltan 		return -1;
    162  1.1  zoltan 	}
    163  1.1  zoltan 
    164  1.1  zoltan 	hlen = ip->ip_hl << 2;
    165  1.1  zoltan 	if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) {
    166  1.1  zoltan #ifdef NET_DEBUG
    167  1.1  zoltan 		if (debug)
    168  1.1  zoltan 			printf("readip: short hdr or bad cksum.\n");
    169  1.1  zoltan #endif
    170  1.1  zoltan 		return -1;
    171  1.1  zoltan 	}
    172  1.1  zoltan 	if (n < ntohs(ip->ip_len)) {
    173  1.1  zoltan #ifdef NET_DEBUG
    174  1.1  zoltan 		if (debug)
    175  1.1  zoltan 			printf("readip: bad length %d < %d.\n",
    176  1.1  zoltan 			       (int) n, ntohs(ip->ip_len));
    177  1.1  zoltan #endif
    178  1.1  zoltan 		return -1;
    179  1.1  zoltan 	}
    180  1.1  zoltan 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
    181  1.1  zoltan #ifdef NET_DEBUG
    182  1.1  zoltan 		if (debug) {
    183  1.1  zoltan 			printf("readip: bad saddr %s != ", inet_ntoa(d->myip));
    184  1.1  zoltan 			printf("%s\n", inet_ntoa(ip->ip_dst));
    185  1.1  zoltan 		}
    186  1.1  zoltan #endif
    187  1.1  zoltan 		return -1;
    188  1.1  zoltan 	}
    189  1.1  zoltan 	return (ntohs(ip->ip_len) - 20);
    190  1.1  zoltan }
    191