Merge from vendor branch CVS:
[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  * $DragonFly: src/sys/net/if_atmsubr.c,v 1.12 2005/02/11 22:25:57 joerg Exp $
36  */
37
38 /*
39  * if_atmsubr.c
40  */
41
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_natm.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/errno.h>
52
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/ifq_var.h>
56 #include <net/bpf.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_atm.h>
62
63 #include <netinet/in.h>
64 #include <netinet/if_atm.h>
65 #include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in_var.h>
68 #endif
69 #ifdef NATM
70 #include <netproto/natm/natm.h>
71 #endif
72
73 #ifndef ETHERTYPE_IPV6
74 #define ETHERTYPE_IPV6  0x86dd
75 #endif
76
77 #define gotoerr(e) { error = (e); goto bad;}
78
79 /*
80  * atm_output: ATM output routine
81  *   inputs:
82  *     "ifp" = ATM interface to output to
83  *     "m0" = the packet to output
84  *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
85  *     "rt0" = the route to use
86  *   returns: error code   [0 == ok]
87  *
88  *   note: special semantic: if (dst == NULL) then we assume "m" already
89  *              has an atm_pseudohdr on it and just send it directly.
90  *              [for native mode ATM output]   if dst is null, then
91  *              rt0 must also be NULL.
92  */
93
94 int
95 atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
96            struct rtentry *rt0)
97 {
98         u_int16_t etype = 0;                    /* if using LLC/SNAP */
99         int s, error = 0, sz;
100         struct atm_pseudohdr atmdst, *ad;
101         struct mbuf *m = m0;
102         struct rtentry *rt;
103         struct atmllc *atmllc;
104         struct atmllc *llc_hdr = NULL;
105         u_int32_t atm_flags;
106         struct altq_pktattr pktattr;
107
108         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
109                 gotoerr(ENETDOWN);
110
111         /*
112          * if the queueing discipline needs packet classification,
113          * do it before prepending link headers.
114          */
115         ifq_classify(&ifp->if_snd, m,
116                      (dst != NULL ? dst->sa_family : AF_UNSPEC), &pktattr);
117
118         /*
119          * check route
120          */
121         if ((rt = rt0) != NULL) {
122
123                 if (!(rt->rt_flags & RTF_UP)) { /* route went down! */
124                         if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
125                                 rt->rt_refcnt--;
126                         else 
127                                 gotoerr(EHOSTUNREACH);
128                 }
129
130                 if (rt->rt_flags & RTF_GATEWAY) {
131                         if (rt->rt_gwroute == NULL)
132                                 goto lookup;
133                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == NULL) {
134                                 rtfree(rt); rt = rt0;
135                         lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
136                                 if ((rt = rt->rt_gwroute) == NULL)
137                                         gotoerr(EHOSTUNREACH);
138                         }
139                 }
140
141                 /* XXX: put RTF_REJECT code here if doing ATMARP */
142
143         }
144
145         /*
146          * check for non-native ATM traffic   (dst != NULL)
147          */
148         if (dst) {
149                 switch (dst->sa_family) {
150 #if defined(INET) || defined(INET6)
151                 case AF_INET:
152                 case AF_INET6:
153                         if (dst->sa_family == AF_INET6)
154                                 etype = htons(ETHERTYPE_IPV6);
155                         else
156                                 etype = htons(ETHERTYPE_IP);
157                         if (!atmresolve(rt, m, dst, &atmdst)) {
158                                 m = NULL; 
159                                 /* XXX: atmresolve already free'd it */
160                                 gotoerr(EHOSTUNREACH);
161                                 /* XXX: put ATMARP stuff here */
162                                 /* XXX: watch who frees m on failure */
163                         }
164                         break;
165 #endif /* INET || INET6 */
166
167                 case AF_UNSPEC:
168                         /*
169                          * XXX: bpfwrite. assuming dst contains 12 bytes
170                          * (atm pseudo header (4) + LLC/SNAP (8))
171                          */
172                         bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
173                         llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst));
174                         break;
175                         
176                 default:
177 #if defined(__NetBSD__) || defined(__OpenBSD__)
178                         printf("%s: can't handle af%d\n", ifp->if_xname, 
179                             dst->sa_family);
180 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
181                         printf("%s: can't handle af%d\n", ifp->if_xname, 
182                             dst->sa_family);
183 #endif
184                         gotoerr(EAFNOSUPPORT);
185                 }
186
187                 /*
188                  * must add atm_pseudohdr to data
189                  */
190                 sz = sizeof(atmdst);
191                 atm_flags = ATM_PH_FLAGS(&atmdst);
192                 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
193                 M_PREPEND(m, sz, MB_DONTWAIT);
194                 if (m == NULL)
195                         gotoerr(ENOBUFS);
196                 ad = mtod(m, struct atm_pseudohdr *);
197                 *ad = atmdst;
198                 if (atm_flags & ATM_PH_LLCSNAP) {
199                         atmllc = (struct atmllc *)(ad + 1);
200                         if (llc_hdr == NULL) {
201                                 bcopy(ATMLLC_HDR, atmllc->llchdr, 
202                                       sizeof(atmllc->llchdr));
203                                 ATM_LLC_SETTYPE(atmllc, etype); 
204                                         /* note: already in network order */
205                         }
206                         else
207                                 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
208                 }
209         }
210
211         /*
212          * Queue message on interface, and start output if interface
213          * not yet active.
214          */
215         s = splimp();
216         error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
217         if (error) {
218                 splx(s);
219                 return (ENOBUFS);
220         }
221         ifp->if_obytes += m->m_pkthdr.len;
222         if (!(ifp->if_flags & IFF_OACTIVE))
223                 (*ifp->if_start)(ifp);
224         splx(s);
225         return (error);
226
227 bad:
228         if (m != NULL)
229                 m_freem(m);
230         return (error);
231 }
232
233 /*
234  * Process a received ATM packet;
235  * the packet is in the mbuf chain m.
236  */
237 void
238 atm_input(ifp, ah, m, rxhand)
239         struct ifnet *ifp;
240         struct atm_pseudohdr *ah;
241         struct mbuf *m;
242         void *rxhand;
243 {
244         u_int16_t etype = ETHERTYPE_IP; /* default */
245         int isr;
246         int s;
247
248         if (!(ifp->if_flags & IFF_UP)) {
249                 m_freem(m);
250                 return;
251         }
252         ifp->if_ibytes += m->m_pkthdr.len;
253
254         if (rxhand) {
255 #ifdef NATM
256                 struct natmpcb *npcb = rxhand;
257                 s = splimp();           /* in case 2 atm cards @ diff lvls */
258                 npcb->npcb_inq++;       /* count # in queue */
259                 splx(s);
260                 isr = NETISR_NATM;
261                 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
262 #else
263                 printf("atm_input: NATM detected but not configured in kernel\n");
264                 m_freem(m);
265                 return;
266 #endif
267         } else {
268                 /*
269                  * handle LLC/SNAP header, if present
270                  */
271                 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
272                         struct atmllc *alc;
273                         if (m->m_len < sizeof(*alc) &&
274                             (m = m_pullup(m, sizeof(*alc))) == NULL)
275                                 return; /* failed */
276                         alc = mtod(m, struct atmllc *);
277                         if (bcmp(alc, ATMLLC_HDR, 6)) {
278 #if defined(__NetBSD__) || defined(__OpenBSD__)
279                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
280                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
281 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
282                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
283                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
284 #endif
285                                 m_freem(m);
286                                 return;
287                         }
288                         etype = ATM_LLC_TYPE(alc);
289                         m_adj(m, sizeof(*alc));
290                 }
291
292                 switch (etype) {
293 #ifdef INET
294                 case ETHERTYPE_IP:
295                         isr = NETISR_IP;
296                         break;
297 #endif
298 #ifdef INET6
299                 case ETHERTYPE_IPV6:
300                         isr = NETISR_IPV6;
301                         break;
302 #endif
303                 default:
304                         m_freem(m);
305                         return;
306                 }
307         }
308
309         netisr_dispatch(isr, m);
310 }
311
312 /*
313  * Perform common duties while attaching to interface list
314  */
315 void
316 atm_ifattach(ifp)
317         struct ifnet *ifp;
318 {
319         struct ifaddr *ifa;
320         struct sockaddr_dl *sdl;
321
322         ifp->if_type = IFT_ATM;
323         ifp->if_addrlen = 0;
324         ifp->if_hdrlen = 0;
325         ifp->if_mtu = ATMMTU;
326 #if 0 /* XXX */
327         ifp->if_input = atm_input;
328 #endif
329         ifp->if_output = atm_output;
330         ifq_set_maxlen(&ifp->if_snd, 50);
331         if_attach(ifp);
332
333 #if defined(__NetBSD__) || defined(__OpenBSD__)
334         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != 0;
335             ifa = TAILQ_NEXT(ifa, ifa_list))
336 #elif defined(__DragonFly__) || (defined(__FreeBSD__) && (__FreeBSD__ > 2))
337         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 
338             ifa = TAILQ_NEXT(ifa, ifa_link))
339 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
340         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 
341 #endif
342                 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
343                     sdl->sdl_family == AF_LINK) {
344                         sdl->sdl_type = IFT_ATM;
345                         sdl->sdl_alen = ifp->if_addrlen;
346 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
347                         bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
348 #endif
349                         break;
350                 }
351         bpfattach(ifp, DLT_ATM_RFC1483, sizeof(struct atmllc));
352 }