1 /*
2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * All Rights Reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
14 *
15 * Research Systems Unix Group
16 * The University of Michigan
17 * c/o Mike Clark
18 * 535 W. William Street
19 * Ann Arbor, Michigan
20 * +1-313-763-0525
21 * netatalk@itd.umich.edu
22 */
23
24 /* $FreeBSD: releng/5.2/sys/netatalk/ddp_output.c 121816 2003-10-31 18:32:15Z brooks $ */
25
26 #include "opt_mac.h"
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/mac.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34
35 #include <net/if.h>
36 #include <net/route.h>
37
38 #undef s_net
39
40 #include <netatalk/at.h>
41 #include <netatalk/at_var.h>
42 #include <netatalk/ddp.h>
43 #include <netatalk/ddp_var.h>
44 #include <netatalk/at_extern.h>
45
46 int ddp_cksum = 1;
47
48 int
49 ddp_output( struct mbuf *m, struct socket *so)
50 {
51 struct ddpehdr *deh;
52 struct ddpcb *ddp = sotoddpcb( so );
53
54 #ifdef MAC
55 mac_create_mbuf_from_socket(so, m);
56 #endif
57
58 M_PREPEND( m, sizeof( struct ddpehdr ), M_TRYWAIT );
59 if ( m == NULL )
60 return( ENOBUFS );
61
62 deh = mtod( m, struct ddpehdr *);
63 deh->deh_pad = 0;
64 deh->deh_hops = 0;
65
66 deh->deh_len = m->m_pkthdr.len;
67
68 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
69 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
70 deh->deh_dport = ddp->ddp_fsat.sat_port;
71 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
72 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
73 deh->deh_sport = ddp->ddp_lsat.sat_port;
74
75 /*
76 * The checksum calculation is done after all of the other bytes have
77 * been filled in.
78 */
79 if ( ddp_cksum ) {
80 deh->deh_sum = at_cksum( m, sizeof( int ));
81 } else {
82 deh->deh_sum = 0;
83 }
84 deh->deh_bytes = htonl( deh->deh_bytes );
85
86 #ifdef NETATALK_DEBUG
87 printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
88 ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
89 ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
90 #endif
91 return( ddp_route( m, &ddp->ddp_route ));
92 }
93
94 u_short
95 at_cksum( struct mbuf *m, int skip)
96 {
97 u_char *data, *end;
98 u_long cksum = 0;
99
100 for (; m; m = m->m_next ) {
101 for ( data = mtod( m, u_char * ), end = data + m->m_len; data < end;
102 data++ ) {
103 if ( skip ) {
104 skip--;
105 continue;
106 }
107 cksum = ( cksum + *data ) << 1;
108 if ( cksum & 0x00010000 ) {
109 cksum++;
110 }
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 sockaddr_at gate;
125 struct elaphdr *elh;
126 struct mbuf *m0;
127 struct at_ifaddr *aa = NULL;
128 struct ifnet *ifp = NULL;
129 u_short net;
130
131 #if 0
132 /* Check for net zero, node zero ("myself") */
133 if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
134 && satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
135 /* Find the loopback interface */
136 }
137 #endif
138
139 /*
140 * if we have a route, find the ifa that refers to this route.
141 * I.e The ifa used to get to the gateway.
142 */
143 if ( (ro->ro_rt == NULL)
144 || ( ro->ro_rt->rt_ifa == NULL )
145 || ( (ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL )) {
146 rtalloc(ro);
147 }
148 if ( (ro->ro_rt != NULL)
149 && ( ro->ro_rt->rt_ifa )
150 && ( ifp = ro->ro_rt->rt_ifa->ifa_ifp )) {
151 net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
152 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
153 if (((net == 0) || (aa->aa_ifp == ifp)) &&
154 net >= ntohs( aa->aa_firstnet ) &&
155 net <= ntohs( aa->aa_lastnet )) {
156 break;
157 }
158 }
159 } else {
160 m_freem( m );
161 #ifdef NETATALK_DEBUG
162 if (ro->ro_rt == NULL)
163 printf ("ddp_route: no ro_rt.\n");
164 else if (ro->ro_rt->rt_ifa == NULL)
165 printf ("ddp_route: no ro_rt->rt_ifa\n");
166 else
167 printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
168 #endif
169 return( ENETUNREACH );
170 }
171
172 if ( aa == NULL ) {
173 #ifdef NETATALK_DEBUG
174 printf( "ddp_route: no atalk address found for %s\n",
175 ifp->if_xname);
176 #endif
177 m_freem( m );
178 return( ENETUNREACH );
179 }
180
181 /*
182 * if the destination address is on a directly attached node use that,
183 * else use the official gateway.
184 */
185 if ( ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) >=
186 ntohs( aa->aa_firstnet ) &&
187 ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) <=
188 ntohs( aa->aa_lastnet )) {
189 gate = *satosat( &ro->ro_dst );
190 } else {
191 gate = *satosat( ro->ro_rt->rt_gateway );
192 }
193
194 /*
195 * There are several places in the kernel where data is added to
196 * an mbuf without ensuring that the mbuf pointer is aligned.
197 * This is bad for transition routing, since phase 1 and phase 2
198 * packets end up poorly aligned due to the three byte elap header.
199 */
200 if ( !(aa->aa_flags & AFA_PHASE2) ) {
201 MGET( m0, M_TRYWAIT, MT_HEADER );
202 if ( m0 == 0 ) {
203 m_freem( m );
204 printf("ddp_route: no buffers\n");
205 return( ENOBUFS );
206 }
207 #ifdef MAC
208 mac_create_mbuf_from_mbuf(m, m0);
209 #endif
210 m0->m_next = m;
211 /* XXX perhaps we ought to align the header? */
212 m0->m_len = SZ_ELAPHDR;
213 m = m0;
214
215 elh = mtod( m, struct elaphdr *);
216 elh->el_snode = satosat( &aa->aa_addr )->sat_addr.s_node;
217 elh->el_type = ELAP_DDPEXTEND;
218 elh->el_dnode = gate.sat_addr.s_node;
219 }
220 ro->ro_rt->rt_use++;
221
222 #ifdef NETATALK_DEBUG
223 printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s)\n",
224 ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
225 satosat(&aa->aa_addr)->sat_addr.s_node,
226 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
227 satosat(&ro->ro_dst)->sat_addr.s_node,
228 ntohs(gate.sat_addr.s_net),
229 gate.sat_addr.s_node,
230 ifp->if_xname);
231 #endif
232
233 /* short-circuit the output if we're sending this to ourself */
234 if ((satosat(&aa->aa_addr)->sat_addr.s_net == satosat(&ro->ro_dst)->sat_addr.s_net) &&
235 (satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
236 {
237 return (if_simloop(ifp, m, gate.sat_family, 0));
238 }
239
240 return((*ifp->if_output)( ifp,
241 m, (struct sockaddr *)&gate, NULL)); /* XXX */
242 }
Cache object: 7c03ac4fd136fbbd2effcdf8b16218b2
|