Home | History | Annotate | Line # | Download | only in netatalk
ddp_output.c revision 1.3
      1  1.3     lukem /*	$NetBSD: ddp_output.c,v 1.3 2001/11/13 00:00:58 lukem 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.3     lukem 
     29  1.3     lukem #include <sys/cdefs.h>
     30  1.3     lukem __KERNEL_RCSID(0, "$NetBSD: ddp_output.c,v 1.3 2001/11/13 00:00:58 lukem Exp $");
     31  1.1  christos 
     32  1.1  christos #include <sys/types.h>
     33  1.1  christos #include <sys/param.h>
     34  1.1  christos #include <sys/systm.h>
     35  1.1  christos #include <sys/mbuf.h>
     36  1.1  christos #include <sys/socket.h>
     37  1.1  christos #include <sys/errno.h>
     38  1.1  christos #include <sys/syslog.h>
     39  1.1  christos 
     40  1.1  christos #include <net/if.h>
     41  1.1  christos #include <net/route.h>
     42  1.1  christos #include <net/if_ether.h>
     43  1.1  christos 
     44  1.1  christos #include <netinet/in.h>
     45  1.1  christos #undef s_net
     46  1.1  christos 
     47  1.1  christos #include <netatalk/at.h>
     48  1.1  christos #include <netatalk/at_var.h>
     49  1.1  christos #include <netatalk/ddp.h>
     50  1.1  christos #include <netatalk/ddp_var.h>
     51  1.1  christos #include <netatalk/at_extern.h>
     52  1.1  christos 
     53  1.1  christos #include <machine/stdarg.h>
     54  1.1  christos 
     55  1.1  christos int ddp_cksum = 1;
     56  1.1  christos 
     57  1.1  christos int
     58  1.1  christos #if __STDC__
     59  1.1  christos ddp_output(struct mbuf *m,...)
     60  1.1  christos #else
     61  1.1  christos ddp_output(va_alist)
     62  1.1  christos 	va_dcl
     63  1.1  christos #endif
     64  1.1  christos {
     65  1.1  christos 	struct ddpcb   *ddp;
     66  1.1  christos 	struct ddpehdr *deh;
     67  1.1  christos 	va_list         ap;
     68  1.1  christos 
     69  1.1  christos #if __STDC__
     70  1.1  christos 	va_start(ap, m);
     71  1.1  christos #else
     72  1.1  christos 	struct mbuf    *m;
     73  1.1  christos 
     74  1.1  christos 	va_start(ap);
     75  1.1  christos 	m = va_arg(ap, struct mbuf *);
     76  1.1  christos #endif
     77  1.1  christos 	ddp = va_arg(ap, struct ddpcb *);
     78  1.1  christos 	va_end(ap);
     79  1.1  christos 
     80  1.1  christos 	M_PREPEND(m, sizeof(struct ddpehdr), M_WAIT);
     81  1.1  christos 
     82  1.1  christos 	deh = mtod(m, struct ddpehdr *);
     83  1.1  christos 	deh->deh_pad = 0;
     84  1.1  christos 	deh->deh_hops = 0;
     85  1.1  christos 
     86  1.1  christos 	deh->deh_len = m->m_pkthdr.len;
     87  1.1  christos 
     88  1.1  christos 	deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
     89  1.1  christos 	deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
     90  1.1  christos 	deh->deh_dport = ddp->ddp_fsat.sat_port;
     91  1.1  christos 	deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
     92  1.1  christos 	deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
     93  1.1  christos 	deh->deh_sport = ddp->ddp_lsat.sat_port;
     94  1.1  christos 
     95  1.1  christos 	/*
     96  1.1  christos          * The checksum calculation is done after all of the other bytes have
     97  1.1  christos          * been filled in.
     98  1.1  christos          */
     99  1.1  christos 	if (ddp_cksum) {
    100  1.1  christos 		deh->deh_sum = at_cksum(m, sizeof(int));
    101  1.1  christos 	} else {
    102  1.1  christos 		deh->deh_sum = 0;
    103  1.1  christos 	}
    104  1.1  christos 	deh->deh_bytes = htonl(deh->deh_bytes);
    105  1.1  christos 
    106  1.1  christos 	return (ddp_route(m, &ddp->ddp_route));
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos u_short
    110  1.1  christos at_cksum(m, skip)
    111  1.1  christos 	struct mbuf *m;
    112  1.1  christos 	int skip;
    113  1.1  christos {
    114  1.1  christos 	u_char         *data, *end;
    115  1.1  christos 	u_long          cksum = 0;
    116  1.1  christos 
    117  1.1  christos 	for (; m; m = m->m_next) {
    118  1.1  christos 		for (data = mtod(m, u_char *), end = data + m->m_len;
    119  1.1  christos 		    data < end; data++) {
    120  1.1  christos 			if (skip) {
    121  1.1  christos 				skip--;
    122  1.1  christos 				continue;
    123  1.1  christos 			}
    124  1.1  christos 			cksum = (cksum + *data) << 1;
    125  1.1  christos 			if (cksum & 0x00010000) {
    126  1.1  christos 				cksum++;
    127  1.1  christos 			}
    128  1.1  christos 			cksum &= 0x0000ffff;
    129  1.1  christos 		}
    130  1.1  christos 	}
    131  1.1  christos 
    132  1.1  christos 	if (cksum == 0) {
    133  1.1  christos 		cksum = 0x0000ffff;
    134  1.1  christos 	}
    135  1.1  christos 	return ((u_short) cksum);
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos int
    139  1.1  christos ddp_route(m, ro)
    140  1.1  christos 	struct mbuf *m;
    141  1.1  christos 	struct route *ro;
    142  1.1  christos {
    143  1.1  christos 	struct sockaddr_at gate;
    144  1.1  christos 	struct elaphdr *elh;
    145  1.1  christos 	struct mbuf    *m0;
    146  1.1  christos 	struct at_ifaddr *aa = NULL;
    147  1.1  christos 	struct ifnet   *ifp = NULL;
    148  1.1  christos 	u_short         net;
    149  1.1  christos 
    150  1.1  christos 	if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) {
    151  1.1  christos 		net = satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net;
    152  1.1  christos 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    153  1.1  christos 			if (aa->aa_ifp == ifp &&
    154  1.1  christos 			    ntohs(net) >= ntohs(aa->aa_firstnet) &&
    155  1.1  christos 			    ntohs(net) <= ntohs(aa->aa_lastnet)) {
    156  1.1  christos 				break;
    157  1.1  christos 			}
    158  1.1  christos 		}
    159  1.1  christos 	}
    160  1.1  christos 	if (aa == NULL) {
    161  1.1  christos 		printf("ddp_route: oops\n");
    162  1.1  christos 		m_freem(m);
    163  1.1  christos 		return (EINVAL);
    164  1.1  christos 	}
    165  1.1  christos 	/*
    166  1.1  christos          * There are several places in the kernel where data is added to
    167  1.1  christos          * an mbuf without ensuring that the mbuf pointer is aligned.
    168  1.1  christos          * This is bad for transition routing, since phase 1 and phase 2
    169  1.1  christos          * packets end up poorly aligned due to the three byte elap header.
    170  1.1  christos          */
    171  1.1  christos 	if (!(aa->aa_flags & AFA_PHASE2)) {
    172  1.1  christos 		MGET(m0, M_WAIT, MT_HEADER);
    173  1.1  christos 		if (m0 == 0) {
    174  1.1  christos 			m_freem(m);
    175  1.1  christos 			printf("ddp_route: no buffers\n");
    176  1.1  christos 			return (ENOBUFS);
    177  1.1  christos 		}
    178  1.1  christos 		m0->m_next = m;
    179  1.1  christos 		/* XXX perhaps we ought to align the header? */
    180  1.1  christos 		m0->m_len = SZ_ELAPHDR;
    181  1.1  christos 		m = m0;
    182  1.1  christos 
    183  1.1  christos 		elh = mtod(m, struct elaphdr *);
    184  1.1  christos 		elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node;
    185  1.1  christos 		elh->el_type = ELAP_DDPEXTEND;
    186  1.1  christos 		if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >=
    187  1.1  christos 		    ntohs(aa->aa_firstnet) &&
    188  1.1  christos 		    ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <=
    189  1.1  christos 		    ntohs(aa->aa_lastnet)) {
    190  1.1  christos 			elh->el_dnode = satosat(&ro->ro_dst)->sat_addr.s_node;
    191  1.1  christos 		} else {
    192  1.1  christos 			elh->el_dnode =
    193  1.1  christos 			    satosat(ro->ro_rt->rt_gateway)->sat_addr.s_node;
    194  1.1  christos 		}
    195  1.1  christos 	}
    196  1.1  christos 	if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >=
    197  1.1  christos 	    ntohs(aa->aa_firstnet) &&
    198  1.1  christos 	    ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <=
    199  1.1  christos 	    ntohs(aa->aa_lastnet)) {
    200  1.1  christos 		gate = *satosat(&ro->ro_dst);
    201  1.1  christos 	} else {
    202  1.1  christos 		gate = *satosat(ro->ro_rt->rt_gateway);
    203  1.1  christos 	}
    204  1.1  christos 	ro->ro_rt->rt_use++;
    205  1.2     aidan 
    206  1.2     aidan #if IFA_STATS
    207  1.2     aidan 	aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
    208  1.2     aidan #endif
    209  1.1  christos 
    210  1.1  christos 	/* XXX */
    211  1.1  christos 	return ((*ifp->if_output) (ifp, m, (struct sockaddr *) &gate, NULL));
    212  1.1  christos }
    213