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