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