kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / netproto / atm / ipatm / ipatm_output.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/sys/netatm/ipatm/ipatm_output.c,v 1.4.2.1 2000/06/02 22:39:08 archie Exp $
27  *      @(#) $DragonFly: src/sys/netproto/atm/ipatm/ipatm_output.c,v 1.3 2003/08/07 21:17:34 dillon Exp $
28  */
29
30 /*
31  * IP Over ATM Support
32  * -------------------
33  *
34  * Output IP packets across an ATM VCC
35  *
36  */
37
38 #include <netatm/kern_include.h>
39
40 #include "ipatm_var.h"
41 #include "ipatm_serv.h"
42
43 /*
44  * Output an IP Packet
45  * 
46  * All IP packets output to an ATM interface will be directed here via
47  * the atm_ifoutput() function.  If there is an ATM VCC already setup for
48  * the destination IP address, then we'll just send the packet to that VCC.
49  * Otherwise we will have to setup a new VCC, ARPing for the corresponding
50  * destination ATM hardware address along the way.
51  *
52  * Arguments:
53  *      ifp     pointer to ifnet structure
54  *      m       pointer to packet buffer chain to be output
55  *      dst     pointer to packet's IP destination address
56  *
57  * Returns:
58  *      0       packet "output" was successful 
59  *      errno   output failed - reason indicated
60  *
61  */
62 int
63 ipatm_ifoutput(ifp, m, dst)
64         struct ifnet    *ifp;
65         KBuffer         *m;
66         struct sockaddr *dst;
67 {
68         struct ipvcc    *ivp;
69         int     err = 0;
70
71 #ifdef DIAGNOSTIC
72         if (ipatm_print) {
73                 atm_pdu_print(m, "ipatm_ifoutput");
74         }
75 #endif
76
77         /*
78          * See if we've already got an appropriate VCC
79          */
80         ivp = ipatm_iptovc((struct sockaddr_in *)dst, (struct atm_nif *)ifp);
81         if (ivp) {
82
83                 /*
84                  * Reset idle timer
85                  */
86                 ivp->iv_idle = 0;
87
88                 /*
89                  * Can we use this VCC now??
90                  */
91                 if ((ivp->iv_state == IPVCC_ACTIVE) && 
92                     (ivp->iv_flags & IVF_MAPOK)) {
93
94                         /*
95                          * OK, now send packet
96                          */
97                         err = atm_cm_cpcs_data(ivp->iv_conn, m);
98                         if (err) {
99                                 /*
100                                  * Output problem, drop packet
101                                  */
102                                 KB_FREEALL(m);
103                         }
104                 } else {
105
106                         /*
107                          * VCC is unavailable for data packets.  Queue packet
108                          * for now, but only maintain a queue length of one.
109                          */
110                         if (ivp->iv_queue)
111                                 KB_FREEALL(ivp->iv_queue);
112
113                         ivp->iv_queue = m;
114                 }
115         } else {
116                 struct in_ifaddr        *ia;
117 #if (defined(BSD) && (BSD < 199306))
118                 extern struct ifnet     loif;
119 #endif
120
121                 /*
122                  * No VCC to destination
123                  */
124
125                 /*
126                  * Is packet for our interface address?
127                  */
128                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
129                         if (ia->ia_ifp != ifp)
130                                 continue;
131                         if (((struct sockaddr_in *)dst)->sin_addr.s_addr == 
132                                         IA_SIN(ia)->sin_addr.s_addr) {
133
134                                 /*
135                                  * It's for us - hand packet to loopback driver
136                                  */
137                                 (void) if_simloop(ifp, m, dst->sa_family, 0);
138                                 goto done;
139                         }
140                 }
141
142                 /*
143                  * Is this a broadcast packet ??
144                  */
145 #if (defined(BSD) && (BSD >= 199306))
146                 if (in_broadcast(((struct sockaddr_in *)dst)->sin_addr, ifp)) {
147 #else
148                 if (in_broadcast(((struct sockaddr_in *)dst)->sin_addr)) {
149 #endif
150                         struct ip_nif   *inp;
151                         int     s;
152
153                         /*
154                          * If interface server exists and provides broadcast 
155                          * services, then let it deal with this packet
156                          */
157                         s = splnet();
158                         for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
159                                 if (inp->inf_nif == (struct atm_nif *)ifp)
160                                         break;
161                         }
162                         (void) splx(s);
163
164                         if ((inp == NULL) ||
165                             (inp->inf_serv == NULL) ||
166                             (inp->inf_serv->is_bcast_output == NULL)) {
167                                 KB_FREEALL(m);
168                                 err = EADDRNOTAVAIL;
169                                 goto done;
170                         }
171
172                         err = (*inp->inf_serv->is_bcast_output)(inp, m);
173                         goto done;
174                 }
175
176                 /*
177                  * How about a multicast packet ??
178                  */
179                 if (IN_MULTICAST(ntohl(SATOSIN(dst)->sin_addr.s_addr))) {
180                         /* 
181                          * Multicast isn't currently supported
182                          */
183                         KB_FREEALL(m);
184                         err = EADDRNOTAVAIL;
185                         goto done;
186                 }
187
188                 /*
189                  * Well, I guess we need to create an SVC to the destination
190                  */
191                 if ((err = ipatm_createsvc(ifp, AF_INET,
192                                 (caddr_t)&((struct sockaddr_in *)dst)->sin_addr,
193                                 &ivp)) == 0) {
194                         /*
195                          * SVC open is proceeding, queue packet 
196                          */
197                         ivp->iv_queue = m;
198
199                 } else {
200                         /*
201                          * SVC open failed, release buffers and return
202                          */
203                         KB_FREEALL(m);
204                 }
205         }
206
207 done:
208         return (err);
209 }
210