Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / netproto / atalk / ddp_output.c
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: src/sys/netatalk/ddp_output.c,v 1.13.6.1 2000/06/02 22:39:07 archie Exp $ */
25 /* $DragonFly: src/sys/netproto/atalk/ddp_output.c,v 1.8 2005/11/28 17:13:46 dillon Exp $ */
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/mbuf.h>
30 #include <sys/socket.h>
31 #include <sys/socketvar.h>
32
33 #include <net/if.h>
34 #include <net/route.h>
35
36 #undef s_net
37
38 #include "at.h"
39 #include "at_var.h"
40 #include "ddp.h"
41 #include "ddp_var.h"
42 #include "at_extern.h"
43
44 int     ddp_cksum = 1;
45
46 int
47 ddp_output(struct mbuf *m, struct socket *so, ...)
48 {
49     struct ddpehdr      *deh;
50     struct ddpcb *ddp = sotoddpcb( so );
51
52     M_PREPEND( m, sizeof( struct ddpehdr ), MB_WAIT );
53     if (m == NULL)
54             return (ENOBUFS);
55
56     deh = mtod( m, struct ddpehdr *);
57     deh->deh_pad = 0;
58     deh->deh_hops = 0;
59
60     deh->deh_len = m->m_pkthdr.len;
61
62     deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
63     deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
64     deh->deh_dport = ddp->ddp_fsat.sat_port;
65     deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
66     deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
67     deh->deh_sport = ddp->ddp_lsat.sat_port;
68
69     /*
70      * The checksum calculation is done after all of the other bytes have
71      * been filled in.
72      */
73     if ( ddp_cksum ) {
74         deh->deh_sum = at_cksum( m, sizeof( int ));
75     } else {
76         deh->deh_sum = 0;
77     }
78     deh->deh_bytes = htonl( deh->deh_bytes );
79
80 #ifdef NETATALK_DEBUG
81     printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
82         ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
83         ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
84 #endif
85     return( ddp_route( m, &ddp->ddp_route ));
86 }
87
88 u_short
89 at_cksum( struct mbuf *m, int skip)
90 {
91     u_char      *data, *end;
92     u_long      cksum = 0;
93
94     for (; m; m = m->m_next ) {
95         for ( data = mtod( m, u_char * ), end = data + m->m_len; data < end;
96                 data++ ) {
97             if ( skip ) {
98                 skip--;
99                 continue;
100             }
101             cksum = ( cksum + *data ) << 1;
102             if ( cksum & 0x00010000 ) {
103                 cksum++;
104             }
105             cksum &= 0x0000ffff;
106         }
107     }
108
109     if ( cksum == 0 ) {
110         cksum = 0x0000ffff;
111     }
112     return( (u_short)cksum );
113 }
114
115 int
116 ddp_route( struct mbuf *m, struct route *ro)
117 {
118     struct sockaddr_at  gate;
119     struct elaphdr      *elh;
120     struct mbuf         *m0;
121     struct at_ifaddr    *aa = NULL;
122     struct ifnet        *ifp = NULL;
123     u_short             net;
124     int error;
125
126 #if 0
127     /* Check for net zero, node zero ("myself") */
128     if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
129         && satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
130             /* Find the loopback interface */
131     }
132 #endif
133
134     /*
135      * if we have a route, find the ifa that refers to this route.
136      * I.e The ifa used to get to the gateway.
137      */
138     if ( (ro->ro_rt == NULL)
139     || ( ro->ro_rt->rt_ifa == NULL )
140     || ( (ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL )) {
141         rtalloc(ro);
142     }
143     if ( (ro->ro_rt != NULL)
144     && ( ro->ro_rt->rt_ifa )
145     && ( ifp = ro->ro_rt->rt_ifa->ifa_ifp )) {
146         net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
147         for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
148             if (((net == 0) || (aa->aa_ifp == ifp)) &&
149                     net >= ntohs( aa->aa_firstnet ) &&
150                     net <= ntohs( aa->aa_lastnet )) {
151                 break;
152             }
153         }
154     } else {
155         m_freem( m );
156 #ifdef NETATALK_DEBUG
157         if (ro->ro_rt == NULL)
158             printf ("ddp_route: no ro_rt.\n");
159         else if (ro->ro_rt->rt_ifa == NULL)
160             printf ("ddp_route: no ro_rt->rt_ifa\n");
161         else
162             printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
163 #endif
164         return( ENETUNREACH );
165     }
166
167     if ( aa == NULL ) {
168 #ifdef NETATALK_DEBUG
169         printf( "ddp_route: no atalk address found for %s\n", 
170             ifp->if_xname);
171 #endif
172         m_freem( m );
173         return( ENETUNREACH );
174     }
175
176     /*
177      * if the destination address is on a directly attached node use that,
178      * else use the official gateway.
179      */
180     if ( ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) >=
181             ntohs( aa->aa_firstnet ) &&
182             ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) <=
183             ntohs( aa->aa_lastnet )) {
184         gate = *satosat( &ro->ro_dst );
185     } else {
186         gate = *satosat( ro->ro_rt->rt_gateway );
187     }
188
189     /*
190      * There are several places in the kernel where data is added to
191      * an mbuf without ensuring that the mbuf pointer is aligned.
192      * This is bad for transition routing, since phase 1 and phase 2
193      * packets end up poorly aligned due to the three byte elap header.
194      */
195     if ( !(aa->aa_flags & AFA_PHASE2) ) {
196         MGET( m0, MB_WAIT, MT_HEADER );
197         if ( m0 == 0 ) {
198             m_freem( m );
199             printf("ddp_route: no buffers\n");
200             return( ENOBUFS );
201         }
202         m0->m_next = m;
203         /* XXX perhaps we ought to align the header? */
204         m0->m_len = SZ_ELAPHDR;
205         m = m0;
206
207         elh = mtod( m, struct elaphdr *);
208         elh->el_snode = satosat( &aa->aa_addr )->sat_addr.s_node;
209         elh->el_type = ELAP_DDPEXTEND;
210         elh->el_dnode = gate.sat_addr.s_node;
211     }
212     ro->ro_rt->rt_use++;
213
214 #ifdef NETATALK_DEBUG
215     printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s)\n",
216         ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
217         satosat(&aa->aa_addr)->sat_addr.s_node,
218         ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
219         satosat(&ro->ro_dst)->sat_addr.s_node,
220         ntohs(gate.sat_addr.s_net),
221         gate.sat_addr.s_node,
222         ifp->if_xname);
223 #endif
224
225     /* short-circuit the output if we're sending this to ourself */
226     if ((satosat(&aa->aa_addr)->sat_addr.s_net  == satosat(&ro->ro_dst)->sat_addr.s_net) &&
227         (satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
228     {
229         return (if_simloop(ifp, m, gate.sat_family, 0));
230     }
231
232     /* XXX */
233     lwkt_serialize_enter(ifp->if_serializer);
234     error = (*ifp->if_output)( ifp, m, (struct sockaddr *)&gate, NULL);
235     lwkt_serialize_exit(ifp->if_serializer);
236     return (error);
237 }