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