Home | History | Annotate | Line # | Download | only in netatalk
ddp_output.c revision 1.1.14.1
      1  1.1.14.1      kenh /*	$NetBSD: ddp_output.c,v 1.1.14.1 1998/12/11 04:53:06 kenh Exp $	 */
      2       1.1  christos 
      3       1.1  christos /*
      4       1.1  christos  * Copyright (c) 1990,1991 Regents of The University of Michigan.
      5       1.1  christos  * All Rights Reserved.
      6       1.1  christos  *
      7       1.1  christos  * Permission to use, copy, modify, and distribute this software and
      8       1.1  christos  * its documentation for any purpose and without fee is hereby granted,
      9       1.1  christos  * provided that the above copyright notice appears in all copies and
     10       1.1  christos  * that both that copyright notice and this permission notice appear
     11       1.1  christos  * in supporting documentation, and that the name of The University
     12       1.1  christos  * of Michigan not be used in advertising or publicity pertaining to
     13       1.1  christos  * distribution of the software without specific, written prior
     14       1.1  christos  * permission. This software is supplied as is without expressed or
     15       1.1  christos  * implied warranties of any kind.
     16       1.1  christos  *
     17       1.1  christos  * This product includes software developed by the University of
     18       1.1  christos  * California, Berkeley and its contributors.
     19       1.1  christos  *
     20       1.1  christos  *	Research Systems Unix Group
     21       1.1  christos  *	The University of Michigan
     22       1.1  christos  *	c/o Wesley Craig
     23       1.1  christos  *	535 W. William Street
     24       1.1  christos  *	Ann Arbor, Michigan
     25       1.1  christos  *	+1-313-764-2278
     26       1.1  christos  *	netatalk (at) umich.edu
     27       1.1  christos  */
     28       1.1  christos 
     29       1.1  christos #include <sys/types.h>
     30       1.1  christos #include <sys/param.h>
     31       1.1  christos #include <sys/systm.h>
     32       1.1  christos #include <sys/mbuf.h>
     33       1.1  christos #include <sys/socket.h>
     34       1.1  christos #include <sys/errno.h>
     35       1.1  christos #include <sys/syslog.h>
     36       1.1  christos 
     37       1.1  christos #include <net/if.h>
     38       1.1  christos #include <net/route.h>
     39       1.1  christos #include <net/if_ether.h>
     40       1.1  christos 
     41       1.1  christos #include <netinet/in.h>
     42       1.1  christos #undef s_net
     43       1.1  christos 
     44       1.1  christos #include <netatalk/at.h>
     45       1.1  christos #include <netatalk/at_var.h>
     46       1.1  christos #include <netatalk/ddp.h>
     47       1.1  christos #include <netatalk/ddp_var.h>
     48       1.1  christos #include <netatalk/at_extern.h>
     49       1.1  christos 
     50       1.1  christos #include <machine/stdarg.h>
     51       1.1  christos 
     52       1.1  christos int ddp_cksum = 1;
     53       1.1  christos 
     54       1.1  christos int
     55       1.1  christos #if __STDC__
     56       1.1  christos ddp_output(struct mbuf *m,...)
     57       1.1  christos #else
     58       1.1  christos ddp_output(va_alist)
     59       1.1  christos 	va_dcl
     60       1.1  christos #endif
     61       1.1  christos {
     62       1.1  christos 	struct ddpcb   *ddp;
     63       1.1  christos 	struct ddpehdr *deh;
     64       1.1  christos 	va_list         ap;
     65       1.1  christos 
     66       1.1  christos #if __STDC__
     67       1.1  christos 	va_start(ap, m);
     68       1.1  christos #else
     69       1.1  christos 	struct mbuf    *m;
     70       1.1  christos 
     71       1.1  christos 	va_start(ap);
     72       1.1  christos 	m = va_arg(ap, struct mbuf *);
     73       1.1  christos #endif
     74       1.1  christos 	ddp = va_arg(ap, struct ddpcb *);
     75       1.1  christos 	va_end(ap);
     76       1.1  christos 
     77       1.1  christos 	M_PREPEND(m, sizeof(struct ddpehdr), M_WAIT);
     78       1.1  christos 
     79       1.1  christos 	deh = mtod(m, struct ddpehdr *);
     80       1.1  christos 	deh->deh_pad = 0;
     81       1.1  christos 	deh->deh_hops = 0;
     82       1.1  christos 
     83       1.1  christos 	deh->deh_len = m->m_pkthdr.len;
     84       1.1  christos 
     85       1.1  christos 	deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
     86       1.1  christos 	deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
     87       1.1  christos 	deh->deh_dport = ddp->ddp_fsat.sat_port;
     88       1.1  christos 	deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
     89       1.1  christos 	deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
     90       1.1  christos 	deh->deh_sport = ddp->ddp_lsat.sat_port;
     91       1.1  christos 
     92       1.1  christos 	/*
     93       1.1  christos          * The checksum calculation is done after all of the other bytes have
     94       1.1  christos          * been filled in.
     95       1.1  christos          */
     96       1.1  christos 	if (ddp_cksum) {
     97       1.1  christos 		deh->deh_sum = at_cksum(m, sizeof(int));
     98       1.1  christos 	} else {
     99       1.1  christos 		deh->deh_sum = 0;
    100       1.1  christos 	}
    101       1.1  christos 	deh->deh_bytes = htonl(deh->deh_bytes);
    102       1.1  christos 
    103       1.1  christos 	return (ddp_route(m, &ddp->ddp_route));
    104       1.1  christos }
    105       1.1  christos 
    106       1.1  christos u_short
    107       1.1  christos at_cksum(m, skip)
    108       1.1  christos 	struct mbuf *m;
    109       1.1  christos 	int skip;
    110       1.1  christos {
    111       1.1  christos 	u_char         *data, *end;
    112       1.1  christos 	u_long          cksum = 0;
    113       1.1  christos 
    114       1.1  christos 	for (; m; m = m->m_next) {
    115       1.1  christos 		for (data = mtod(m, u_char *), end = data + m->m_len;
    116       1.1  christos 		    data < end; data++) {
    117       1.1  christos 			if (skip) {
    118       1.1  christos 				skip--;
    119       1.1  christos 				continue;
    120       1.1  christos 			}
    121       1.1  christos 			cksum = (cksum + *data) << 1;
    122       1.1  christos 			if (cksum & 0x00010000) {
    123       1.1  christos 				cksum++;
    124       1.1  christos 			}
    125       1.1  christos 			cksum &= 0x0000ffff;
    126       1.1  christos 		}
    127       1.1  christos 	}
    128       1.1  christos 
    129       1.1  christos 	if (cksum == 0) {
    130       1.1  christos 		cksum = 0x0000ffff;
    131       1.1  christos 	}
    132       1.1  christos 	return ((u_short) cksum);
    133       1.1  christos }
    134       1.1  christos 
    135       1.1  christos int
    136       1.1  christos ddp_route(m, ro)
    137       1.1  christos 	struct mbuf *m;
    138       1.1  christos 	struct route *ro;
    139       1.1  christos {
    140       1.1  christos 	struct sockaddr_at gate;
    141       1.1  christos 	struct elaphdr *elh;
    142       1.1  christos 	struct mbuf    *m0;
    143       1.1  christos 	struct at_ifaddr *aa = NULL;
    144       1.1  christos 	struct ifnet   *ifp = NULL;
    145       1.1  christos 	u_short         net;
    146  1.1.14.1      kenh 	int s;
    147       1.1  christos 
    148       1.1  christos 	if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) {
    149       1.1  christos 		net = satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net;
    150  1.1.14.1      kenh 		s = splimp();
    151       1.1  christos 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    152       1.1  christos 			if (aa->aa_ifp == ifp &&
    153       1.1  christos 			    ntohs(net) >= ntohs(aa->aa_firstnet) &&
    154       1.1  christos 			    ntohs(net) <= ntohs(aa->aa_lastnet)) {
    155       1.1  christos 				break;
    156       1.1  christos 			}
    157       1.1  christos 		}
    158  1.1.14.1      kenh 		if (aa)
    159  1.1.14.1      kenh 			ifa_addref(&aa->aa_ifa);
    160  1.1.14.1      kenh 		splx(s);
    161       1.1  christos 	}
    162       1.1  christos 	if (aa == NULL) {
    163       1.1  christos 		printf("ddp_route: oops\n");
    164       1.1  christos 		m_freem(m);
    165       1.1  christos 		return (EINVAL);
    166       1.1  christos 	}
    167       1.1  christos 	/*
    168       1.1  christos          * There are several places in the kernel where data is added to
    169       1.1  christos          * an mbuf without ensuring that the mbuf pointer is aligned.
    170       1.1  christos          * This is bad for transition routing, since phase 1 and phase 2
    171       1.1  christos          * packets end up poorly aligned due to the three byte elap header.
    172       1.1  christos          */
    173       1.1  christos 	if (!(aa->aa_flags & AFA_PHASE2)) {
    174       1.1  christos 		MGET(m0, M_WAIT, MT_HEADER);
    175       1.1  christos 		if (m0 == 0) {
    176  1.1.14.1      kenh 			if (aa)
    177  1.1.14.1      kenh 				ifa_delref(&aa->aa_ifa);
    178       1.1  christos 			m_freem(m);
    179       1.1  christos 			printf("ddp_route: no buffers\n");
    180       1.1  christos 			return (ENOBUFS);
    181       1.1  christos 		}
    182       1.1  christos 		m0->m_next = m;
    183       1.1  christos 		/* XXX perhaps we ought to align the header? */
    184       1.1  christos 		m0->m_len = SZ_ELAPHDR;
    185       1.1  christos 		m = m0;
    186       1.1  christos 
    187       1.1  christos 		elh = mtod(m, struct elaphdr *);
    188       1.1  christos 		elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node;
    189       1.1  christos 		elh->el_type = ELAP_DDPEXTEND;
    190       1.1  christos 		if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >=
    191       1.1  christos 		    ntohs(aa->aa_firstnet) &&
    192       1.1  christos 		    ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <=
    193       1.1  christos 		    ntohs(aa->aa_lastnet)) {
    194       1.1  christos 			elh->el_dnode = satosat(&ro->ro_dst)->sat_addr.s_node;
    195       1.1  christos 		} else {
    196       1.1  christos 			elh->el_dnode =
    197       1.1  christos 			    satosat(ro->ro_rt->rt_gateway)->sat_addr.s_node;
    198       1.1  christos 		}
    199       1.1  christos 	}
    200       1.1  christos 	if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >=
    201       1.1  christos 	    ntohs(aa->aa_firstnet) &&
    202       1.1  christos 	    ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <=
    203       1.1  christos 	    ntohs(aa->aa_lastnet)) {
    204       1.1  christos 		gate = *satosat(&ro->ro_dst);
    205       1.1  christos 	} else {
    206       1.1  christos 		gate = *satosat(ro->ro_rt->rt_gateway);
    207       1.1  christos 	}
    208       1.1  christos 	ro->ro_rt->rt_use++;
    209       1.1  christos 
    210       1.1  christos 	/* XXX */
    211  1.1.14.1      kenh 	ifa_delref(&aa->aa_ifa);
    212       1.1  christos 	return ((*ifp->if_output) (ifp, m, (struct sockaddr *) &gate, NULL));
    213       1.1  christos }
    214