Initial import from FreeBSD RELENG_4:
[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  *
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 <netatm/ipatm/ipatm_var.h>
41 #include <netatm/ipatm/ipatm_serv.h>
42
43 #ifndef lint
44 __RCSID("@(#) $FreeBSD: src/sys/netatm/ipatm/ipatm_output.c,v 1.4.2.1 2000/06/02 22:39:08 archie Exp $");
45 #endif
46
47
48 /*
49  * Output an IP Packet
50  * 
51  * All IP packets output to an ATM interface will be directed here via
52  * the atm_ifoutput() function.  If there is an ATM VCC already setup for
53  * the destination IP address, then we'll just send the packet to that VCC.
54  * Otherwise we will have to setup a new VCC, ARPing for the corresponding
55  * destination ATM hardware address along the way.
56  *
57  * Arguments:
58  *      ifp     pointer to ifnet structure
59  *      m       pointer to packet buffer chain to be output
60  *      dst     pointer to packet's IP destination address
61  *
62  * Returns:
63  *      0       packet "output" was successful 
64  *      errno   output failed - reason indicated
65  *
66  */
67 int
68 ipatm_ifoutput(ifp, m, dst)
69         struct ifnet    *ifp;
70         KBuffer         *m;
71         struct sockaddr *dst;
72 {
73         struct ipvcc    *ivp;
74         int     err = 0;
75
76 #ifdef DIAGNOSTIC
77         if (ipatm_print) {
78                 atm_pdu_print(m, "ipatm_ifoutput");
79         }
80 #endif
81
82         /*
83          * See if we've already got an appropriate VCC
84          */
85         ivp = ipatm_iptovc((struct sockaddr_in *)dst, (struct atm_nif *)ifp);
86         if (ivp) {
87
88                 /*
89                  * Reset idle timer
90                  */
91                 ivp->iv_idle = 0;
92
93                 /*
94                  * Can we use this VCC now??
95                  */
96                 if ((ivp->iv_state == IPVCC_ACTIVE) && 
97                     (ivp->iv_flags & IVF_MAPOK)) {
98
99                         /*
100                          * OK, now send packet
101                          */
102                         err = atm_cm_cpcs_data(ivp->iv_conn, m);
103                         if (err) {
104                                 /*
105                                  * Output problem, drop packet
106                                  */
107                                 KB_FREEALL(m);
108                         }
109                 } else {
110
111                         /*
112                          * VCC is unavailable for data packets.  Queue packet
113                          * for now, but only maintain a queue length of one.
114                          */
115                         if (ivp->iv_queue)
116                                 KB_FREEALL(ivp->iv_queue);
117
118                         ivp->iv_queue = m;
119                 }
120         } else {
121                 struct in_ifaddr        *ia;
122 #if (defined(BSD) && (BSD < 199306))
123                 extern struct ifnet     loif;
124 #endif
125
126                 /*
127                  * No VCC to destination
128                  */
129
130                 /*
131                  * Is packet for our interface address?
132                  */
133                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
134                         if (ia->ia_ifp != ifp)
135                                 continue;
136                         if (((struct sockaddr_in *)dst)->sin_addr.s_addr == 
137                                         IA_SIN(ia)->sin_addr.s_addr) {
138
139                                 /*
140                                  * It's for us - hand packet to loopback driver
141                                  */
142                                 (void) if_simloop(ifp, m, dst->sa_family, 0);
143                                 goto done;
144                         }
145                 }
146
147                 /*
148                  * Is this a broadcast packet ??
149                  */
150 #if (defined(BSD) && (BSD >= 199306))
151                 if (in_broadcast(((struct sockaddr_in *)dst)->sin_addr, ifp)) {
152 #else
153                 if (in_broadcast(((struct sockaddr_in *)dst)->sin_addr)) {
154 #endif
155                         struct ip_nif   *inp;
156                         int     s;
157
158                         /*
159                          * If interface server exists and provides broadcast 
160                          * services, then let it deal with this packet
161                          */
162                         s = splnet();
163                         for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
164                                 if (inp->inf_nif == (struct atm_nif *)ifp)
165                                         break;
166                         }
167                         (void) splx(s);
168
169                         if ((inp == NULL) ||
170                             (inp->inf_serv == NULL) ||
171                             (inp->inf_serv->is_bcast_output == NULL)) {
172                                 KB_FREEALL(m);
173                                 err = EADDRNOTAVAIL;
174                                 goto done;
175                         }
176
177                         err = (*inp->inf_serv->is_bcast_output)(inp, m);
178                         goto done;
179                 }
180
181                 /*
182                  * How about a multicast packet ??
183                  */
184                 if (IN_MULTICAST(ntohl(SATOSIN(dst)->sin_addr.s_addr))) {
185                         /* 
186                          * Multicast isn't currently supported
187                          */
188                         KB_FREEALL(m);
189                         err = EADDRNOTAVAIL;
190                         goto done;
191                 }
192
193                 /*
194                  * Well, I guess we need to create an SVC to the destination
195                  */
196                 if ((err = ipatm_createsvc(ifp, AF_INET,
197                                 (caddr_t)&((struct sockaddr_in *)dst)->sin_addr,
198                                 &ivp)) == 0) {
199                         /*
200                          * SVC open is proceeding, queue packet 
201                          */
202                         ivp->iv_queue = m;
203
204                 } else {
205                         /*
206                          * SVC open failed, release buffers and return
207                          */
208                         KB_FREEALL(m);
209                 }
210         }
211
212 done:
213         return (err);
214 }
215