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