Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / if_atmsubr.c
1 /*      $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $       */
2
3 /*
4  *
5  * Copyright (c) 1996 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor and 
19  *      Washington University.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/net/if_atmsubr.c,v 1.10.2.1 2001/03/06 00:29:26 obrien Exp $
35  */
36
37 /*
38  * if_atmsubr.c
39  */
40
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_natm.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/errno.h>
51
52 #include <net/if.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/if_atm.h>
58
59 #include <netinet/in.h>
60 #include <netinet/if_atm.h>
61 #include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
62 #if defined(INET) || defined(INET6)
63 #include <netinet/in_var.h>
64 #endif
65 #ifdef NATM
66 #include <netnatm/natm.h>
67 #endif
68
69 #ifndef ETHERTYPE_IPV6
70 #define ETHERTYPE_IPV6  0x86dd
71 #endif
72
73 #define senderr(e) { error = (e); goto bad;}
74
75 /*
76  * atm_output: ATM output routine
77  *   inputs:
78  *     "ifp" = ATM interface to output to
79  *     "m0" = the packet to output
80  *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
81  *     "rt0" = the route to use
82  *   returns: error code   [0 == ok]
83  *
84  *   note: special semantic: if (dst == NULL) then we assume "m" already
85  *              has an atm_pseudohdr on it and just send it directly.
86  *              [for native mode ATM output]   if dst is null, then
87  *              rt0 must also be NULL.
88  */
89
90 int
91 atm_output(ifp, m0, dst, rt0)
92         register struct ifnet *ifp;
93         struct mbuf *m0;
94         struct sockaddr *dst;
95         struct rtentry *rt0;
96 {
97         u_int16_t etype = 0;                    /* if using LLC/SNAP */
98         int s, error = 0, sz;
99         struct atm_pseudohdr atmdst, *ad;
100         struct mbuf *m = m0;
101         struct rtentry *rt;
102         struct atmllc *atmllc;
103         struct atmllc *llc_hdr = NULL;
104         u_int32_t atm_flags;
105
106         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
107                 senderr(ENETDOWN);
108
109         /*
110          * check route
111          */
112         if ((rt = rt0) != NULL) {
113
114                 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
115                         if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
116                                 rt->rt_refcnt--;
117                         else 
118                                 senderr(EHOSTUNREACH);
119                 }
120
121                 if (rt->rt_flags & RTF_GATEWAY) {
122                         if (rt->rt_gwroute == 0)
123                                 goto lookup;
124                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
125                                 rtfree(rt); rt = rt0;
126                         lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
127                                 if ((rt = rt->rt_gwroute) == 0)
128                                         senderr(EHOSTUNREACH);
129                         }
130                 }
131
132                 /* XXX: put RTF_REJECT code here if doing ATMARP */
133
134         }
135
136         /*
137          * check for non-native ATM traffic   (dst != NULL)
138          */
139         if (dst) {
140                 switch (dst->sa_family) {
141 #if defined(INET) || defined(INET6)
142                 case AF_INET:
143                 case AF_INET6:
144                         if (dst->sa_family == AF_INET6)
145                                 etype = htons(ETHERTYPE_IPV6);
146                         else
147                                 etype = htons(ETHERTYPE_IP);
148                         if (!atmresolve(rt, m, dst, &atmdst)) {
149                                 m = NULL; 
150                                 /* XXX: atmresolve already free'd it */
151                                 senderr(EHOSTUNREACH);
152                                 /* XXX: put ATMARP stuff here */
153                                 /* XXX: watch who frees m on failure */
154                         }
155                         break;
156 #endif /* INET || INET6 */
157
158                 case AF_UNSPEC:
159                         /*
160                          * XXX: bpfwrite. assuming dst contains 12 bytes
161                          * (atm pseudo header (4) + LLC/SNAP (8))
162                          */
163                         bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
164                         llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst));
165                         break;
166                         
167                 default:
168 #if defined(__NetBSD__) || defined(__OpenBSD__)
169                         printf("%s: can't handle af%d\n", ifp->if_xname, 
170                             dst->sa_family);
171 #elif defined(__FreeBSD__) || defined(__bsdi__)
172                         printf("%s%d: can't handle af%d\n", ifp->if_name, 
173                             ifp->if_unit, dst->sa_family);
174 #endif
175                         senderr(EAFNOSUPPORT);
176                 }
177
178                 /*
179                  * must add atm_pseudohdr to data
180                  */
181                 sz = sizeof(atmdst);
182                 atm_flags = ATM_PH_FLAGS(&atmdst);
183                 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
184                 M_PREPEND(m, sz, M_DONTWAIT);
185                 if (m == 0)
186                         senderr(ENOBUFS);
187                 ad = mtod(m, struct atm_pseudohdr *);
188                 *ad = atmdst;
189                 if (atm_flags & ATM_PH_LLCSNAP) {
190                         atmllc = (struct atmllc *)(ad + 1);
191                         if (llc_hdr == NULL) {
192                                 bcopy(ATMLLC_HDR, atmllc->llchdr, 
193                                       sizeof(atmllc->llchdr));
194                                 ATM_LLC_SETTYPE(atmllc, etype); 
195                                         /* note: already in network order */
196                         }
197                         else
198                                 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
199                 }
200         }
201
202         /*
203          * Queue message on interface, and start output if interface
204          * not yet active.
205          */
206         s = splimp();
207         if (IF_QFULL(&ifp->if_snd)) {
208                 IF_DROP(&ifp->if_snd);
209                 splx(s);
210                 senderr(ENOBUFS);
211         }
212         ifp->if_obytes += m->m_pkthdr.len;
213         IF_ENQUEUE(&ifp->if_snd, m);
214         if ((ifp->if_flags & IFF_OACTIVE) == 0)
215                 (*ifp->if_start)(ifp);
216         splx(s);
217         return (error);
218
219 bad:
220         if (m)
221                 m_freem(m);
222         return (error);
223 }
224
225 /*
226  * Process a received ATM packet;
227  * the packet is in the mbuf chain m.
228  */
229 void
230 atm_input(ifp, ah, m, rxhand)
231         struct ifnet *ifp;
232         register struct atm_pseudohdr *ah;
233         struct mbuf *m;
234         void *rxhand;
235 {
236         register struct ifqueue *inq;
237         u_int16_t etype = ETHERTYPE_IP; /* default */
238         int s;
239
240         if ((ifp->if_flags & IFF_UP) == 0) {
241                 m_freem(m);
242                 return;
243         }
244         ifp->if_ibytes += m->m_pkthdr.len;
245
246         if (rxhand) {
247 #ifdef NATM
248                 struct natmpcb *npcb = rxhand;
249                 s = splimp();           /* in case 2 atm cards @ diff lvls */
250                 npcb->npcb_inq++;       /* count # in queue */
251                 splx(s);
252                 schednetisr(NETISR_NATM);
253                 inq = &natmintrq;
254                 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
255 #else
256                 printf("atm_input: NATM detected but not configured in kernel\n");
257                 m_freem(m);
258                 return;
259 #endif
260         } else {
261                 /*
262                  * handle LLC/SNAP header, if present
263                  */
264                 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
265                         struct atmllc *alc;
266                         if (m->m_len < sizeof(*alc) &&
267                             (m = m_pullup(m, sizeof(*alc))) == 0)
268                                 return; /* failed */
269                         alc = mtod(m, struct atmllc *);
270                         if (bcmp(alc, ATMLLC_HDR, 6)) {
271 #if defined(__NetBSD__) || defined(__OpenBSD__)
272                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
273                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
274 #elif defined(__FreeBSD__) || defined(__bsdi__)
275                                 printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
276                                        ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
277 #endif
278                                 m_freem(m);
279                                 return;
280                         }
281                         etype = ATM_LLC_TYPE(alc);
282                         m_adj(m, sizeof(*alc));
283                 }
284
285                 switch (etype) {
286 #ifdef INET
287                 case ETHERTYPE_IP:
288                         schednetisr(NETISR_IP);
289                         inq = &ipintrq;
290                         break;
291 #endif
292 #ifdef INET6
293                 case ETHERTYPE_IPV6:
294                         schednetisr(NETISR_IPV6);
295                         inq = &ip6intrq;
296                         break;
297 #endif
298                 default:
299                         m_freem(m);
300                         return;
301                 }
302         }
303
304         s = splimp();
305         if (IF_QFULL(inq)) {
306                 IF_DROP(inq);
307                 m_freem(m);
308         } else
309                 IF_ENQUEUE(inq, m);
310         splx(s);
311 }
312
313 /*
314  * Perform common duties while attaching to interface list
315  */
316 void
317 atm_ifattach(ifp)
318         register struct ifnet *ifp;
319 {
320         register struct ifaddr *ifa;
321         register struct sockaddr_dl *sdl;
322
323         ifp->if_type = IFT_ATM;
324         ifp->if_addrlen = 0;
325         ifp->if_hdrlen = 0;
326         ifp->if_mtu = ATMMTU;
327         ifp->if_output = atm_output;
328         ifp->if_snd.ifq_maxlen = 50;    /* dummy */
329
330 #if defined(__NetBSD__) || defined(__OpenBSD__)
331         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != 0;
332             ifa = TAILQ_NEXT(ifa, ifa_list))
333 #elif defined(__FreeBSD__) && (__FreeBSD__ > 2)
334         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 
335             ifa = TAILQ_NEXT(ifa, ifa_link))
336 #elif defined(__FreeBSD__) || defined(__bsdi__)
337         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 
338 #endif
339                 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
340                     sdl->sdl_family == AF_LINK) {
341                         sdl->sdl_type = IFT_ATM;
342                         sdl->sdl_alen = ifp->if_addrlen;
343 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
344                         bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
345 #endif
346                         break;
347                 }
348
349 }