xref: /netbsd-src/sys/netatalk/ddp_output.c (revision ca453df649ce9db45b64d73678ba06cbccf9aa11)
1 /*	$NetBSD: ddp_output.c,v 1.15 2011/07/17 20:54:53 joerg 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@umich.edu
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ddp_output.c,v 1.15 2011/07/17 20:54:53 joerg 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 int ddp_cksum = 1;
53 
54 int
55 ddp_output(struct mbuf *m,...)
56 {
57 	struct ddpcb   *ddp;
58 	struct ddpehdr *deh;
59 	va_list         ap;
60 
61 	va_start(ap, m);
62 	ddp = va_arg(ap, struct ddpcb *);
63 	va_end(ap);
64 
65 	M_PREPEND(m, sizeof(struct ddpehdr), M_DONTWAIT);
66 	if (!m)
67 		return (ENOBUFS);
68 
69 	deh = mtod(m, struct ddpehdr *);
70 	deh->deh_pad = 0;
71 	deh->deh_hops = 0;
72 
73 	deh->deh_len = m->m_pkthdr.len;
74 
75 	deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
76 	deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
77 	deh->deh_dport = ddp->ddp_fsat.sat_port;
78 	deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
79 	deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
80 	deh->deh_sport = ddp->ddp_lsat.sat_port;
81 
82 	/*
83          * The checksum calculation is done after all of the other bytes have
84          * been filled in.
85          */
86 	if (ddp_cksum)
87 		deh->deh_sum = at_cksum(m, sizeof(int));
88 	else
89 		deh->deh_sum = 0;
90 	deh->deh_bytes = htonl(deh->deh_bytes);
91 
92 	return ddp_route(m, &ddp->ddp_route);
93 }
94 
95 u_short
96 at_cksum(struct mbuf *m, int skip)
97 {
98 	u_char         *data, *end;
99 	u_long          cksum = 0;
100 
101 	for (; m; m = m->m_next) {
102 		for (data = mtod(m, u_char *), end = data + m->m_len;
103 		    data < end; data++) {
104 			if (skip) {
105 				skip--;
106 				continue;
107 			}
108 			cksum = (cksum + *data) << 1;
109 			if (cksum & 0x00010000)
110 				cksum++;
111 			cksum &= 0x0000ffff;
112 		}
113 	}
114 
115 	if (cksum == 0) {
116 		cksum = 0x0000ffff;
117 	}
118 	return (u_short)cksum;
119 }
120 
121 int
122 ddp_route(struct mbuf *m, struct route *ro)
123 {
124 	struct rtentry *rt;
125 	struct sockaddr_at gate;
126 	struct elaphdr *elh;
127 	struct at_ifaddr *aa = NULL;
128 	struct ifnet   *ifp = NULL;
129 	u_short         net;
130 
131 	if ((rt = rtcache_validate(ro)) != NULL && (ifp = rt->rt_ifp) != NULL) {
132 		net = satosat(rt->rt_gateway)->sat_addr.s_net;
133 		TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
134 			if (aa->aa_ifp == ifp &&
135 			    ntohs(net) >= ntohs(aa->aa_firstnet) &&
136 			    ntohs(net) <= ntohs(aa->aa_lastnet)) {
137 				break;
138 			}
139 		}
140 	}
141 	if (aa == NULL) {
142 		printf("%s: no address found\n", __func__);
143 		m_freem(m);
144 		return EINVAL;
145 	}
146 	/*
147          * There are several places in the kernel where data is added to
148          * an mbuf without ensuring that the mbuf pointer is aligned.
149          * This is bad for transition routing, since phase 1 and phase 2
150          * packets end up poorly aligned due to the three byte elap header.
151          */
152 	if (!(aa->aa_flags & AFA_PHASE2)) {
153 		M_PREPEND(m, SZ_ELAPHDR, M_DONTWAIT);
154 		if (m == NULL)
155 			return ENOBUFS;
156 
157 		elh = mtod(m, struct elaphdr *);
158 		elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node;
159 		elh->el_type = ELAP_DDPEXTEND;
160 		if (ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) >=
161 		    ntohs(aa->aa_firstnet) &&
162 		    ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) <=
163 		    ntohs(aa->aa_lastnet)) {
164 			elh->el_dnode = satocsat(rtcache_getdst(ro))->sat_addr.s_node;
165 		} else {
166 			elh->el_dnode =
167 			    satosat(rt->rt_gateway)->sat_addr.s_node;
168 		}
169 	}
170 	if (ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) >=
171 	    ntohs(aa->aa_firstnet) &&
172 	    ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) <=
173 	    ntohs(aa->aa_lastnet)) {
174 		gate = *satocsat(rtcache_getdst(ro));
175 	} else {
176 		gate = *satosat(rt->rt_gateway);
177 	}
178 	rt->rt_use++;
179 
180 #if IFA_STATS
181 	aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
182 #endif
183 
184 	/* XXX */
185 	return (*ifp->if_output)(ifp, m, (struct sockaddr *)&gate, NULL);
186 }
187