2 * if_ppp.c - a network interface connected to a STREAMS module.
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
27 * $Id: if_ppp.c,v 1.3 1996/09/14 05:11:13 paulus Exp $
31 * This file is used under SunOS 4 and Digital UNIX.
33 * This file provides the glue between PPP and IP.
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/stream.h>
41 #include <sys/errno.h>
43 #include <sys/socket.h>
45 #include <net/netisr.h>
46 #include <net/ppp_defs.h>
47 #include <net/pppio.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
51 #include <sys/ioctl.h>
52 #include <net/if_types.h>
55 #include <sys/sockio.h>
56 #include <modules/ppp_mod.h>
61 #include <net/nit_if.h>
62 #include <netinet/if_ether.h>
66 #define SIOCSIFMTU SIOCSIPMTU
67 #define SIOCGIFMTU SIOCRIPMTU
68 #define IFA_ADDR(ifa) (*(ifa)->ifa_addr)
70 #define IFA_ADDR(ifa) ((ifa)->ifa_addr)
73 #define ifr_mtu ifr_metric
76 #define PPP_MAXMTU 65536
78 static int if_ppp_open __P((queue_t *, int, int, int));
79 static int if_ppp_close __P((queue_t *, int));
80 static int if_ppp_wput __P((queue_t *, mblk_t *));
81 static int if_ppp_rput __P((queue_t *, mblk_t *));
83 #define PPP_IF_ID 0x8021
84 static struct module_info minfo = {
85 PPP_IF_ID, "if_ppp", 0, INFPSZ, 4096, 128
88 static struct qinit rinit = {
89 if_ppp_rput, NULL, if_ppp_open, if_ppp_close, NULL, &minfo, NULL
92 static struct qinit winit = {
93 if_ppp_wput, NULL, NULL, NULL, NULL, &minfo, NULL
96 struct streamtab if_pppinfo = {
97 &rinit, &winit, NULL, NULL
100 typedef struct if_ppp_state {
106 /* Values for flags */
109 static int if_ppp_count; /* Number of currently-active streams */
111 static int ppp_nalloc; /* Number of elements of ifs and states */
112 static struct ifnet **ifs; /* Array of pointers to interface structs */
113 static if_ppp_t **states; /* Array of pointers to state structs */
115 static int if_ppp_output __P((struct ifnet *, struct mbuf *,
117 static int if_ppp_ioctl __P((struct ifnet *, u_int, caddr_t));
118 static struct mbuf *make_mbufs __P((mblk_t *, int));
119 static mblk_t *make_message __P((struct mbuf *, int));
122 /* Fake ether header for SNIT */
123 static struct ether_header snit_ehdr = {{0}, {0}, ETHERTYPE_IP};
127 static void ppp_if_detach __P((struct ifnet *));
130 * Detach all the interfaces before unloading.
131 * Not sure this works.
138 if (if_ppp_count > 0)
140 for (i = 0; i < ppp_nalloc; ++i)
142 ppp_if_detach(ifs[i]);
143 FREE(ifs, ppp_nalloc * sizeof (struct ifnet *));
144 FREE(states, ppp_nalloc * sizeof (struct if_ppp_t *));
151 * STREAMS module entry points.
154 if_ppp_open(q, dev, flag, sflag)
162 sp = (if_ppp_t *) ALLOC_SLEEP(sizeof (if_ppp_t));
165 bzero(sp, sizeof (if_ppp_t));
166 q->q_ptr = (caddr_t) sp;
167 WR(q)->q_ptr = (caddr_t) sp;
168 sp->unit = -1; /* no interface unit attached at present */
177 if_ppp_close(q, flag)
184 sp = (if_ppp_t *) q->q_ptr;
186 if (sp->flags & DBGLOG)
187 printf("if_ppp closed, q=%x sp=%x\n", q, sp);
189 if (sp->unit < ppp_nalloc) {
190 states[sp->unit] = 0;
193 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
196 printf("if_ppp: unit %d nonexistent!\n", sp->unit);
200 FREE(sp, sizeof (if_ppp_t));
216 sp = (if_ppp_t *) q->q_ptr;
217 switch (mp->b_datap->db_type) {
220 * Now why would we be getting data coming in here??
222 if (sp->flags & DBGLOG)
223 printf("if_ppp: got M_DATA len=%d\n", msgdsize(mp));
228 iop = (struct iocblk *) mp->b_rptr;
231 if (sp->flags & DBGLOG)
232 printf("if_ppp: got ioctl cmd=%x count=%d\n",
233 iop->ioc_cmd, iop->ioc_count);
235 switch (iop->ioc_cmd) {
236 case PPPIO_NEWPPA: /* well almost */
237 if (iop->ioc_count != sizeof(int) || sp->unit >= 0)
239 if ((error = NOTSUSER()) != 0)
241 unit = *(int *)mp->b_cont->b_rptr;
243 /* Check that this unit isn't already in use */
244 if (unit < ppp_nalloc && states[unit] != 0) {
249 /* Extend ifs and states arrays if necessary. */
251 if (unit >= ppp_nalloc) {
253 struct ifnet **newifs;
254 if_ppp_t **newstates;
257 if (sp->flags & DBGLOG)
258 printf("if_ppp: extending ifs to %d\n", newn);
259 newifs = (struct ifnet **)
260 ALLOC_NOSLEEP(newn * sizeof (struct ifnet *));
263 bzero(newifs, newn * sizeof (struct ifnet *));
264 newstates = (if_ppp_t **)
265 ALLOC_NOSLEEP(newn * sizeof (struct if_ppp_t *));
266 if (newstates == 0) {
267 FREE(newifs, newn * sizeof (struct ifnet *));
270 bzero(newstates, newn * sizeof (struct if_ppp_t *));
271 bcopy(ifs, newifs, ppp_nalloc * sizeof(struct ifnet *));
272 bcopy(states, newstates, ppp_nalloc * sizeof(if_ppp_t *));
273 FREE(ifs, ppp_nalloc * sizeof(struct ifnet *));
274 FREE(states, ppp_nalloc * sizeof(if_ppp_t *));
280 /* Allocate a new ifnet struct if necessary. */
283 ifp = (struct ifnet *) ALLOC_NOSLEEP(sizeof (struct ifnet));
286 bzero(ifp, sizeof (struct ifnet));
288 ifp->if_name = "ppp";
290 ifp->if_mtu = PPP_MRU;
291 ifp->if_flags = IFF_POINTOPOINT | IFF_RUNNING;
293 ifp->if_flags |= IFF_MULTICAST;
295 ifp->if_output = if_ppp_output;
297 ifp->if_version = "Point-to-Point Protocol, version 2.3";
298 ifp->if_mediamtu = 1500;
299 ifp->if_type = IFT_PPP;
300 ifp->if_hdrlen = PPP_HDRLEN;
303 ifp->if_affinity = NETMASTERCPU;
306 ifp->if_ioctl = if_ppp_ioctl;
307 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
309 if (sp->flags & DBGLOG)
310 printf("if_ppp: created unit %d\n", unit);
312 ifp->if_mtu = PPP_MRU;
313 ifp->if_flags |= IFF_RUNNING;
321 if (sp->flags & DBGLOG)
322 printf("if_ppp: attached unit %d, sp=%x q=%x\n", unit,
328 if (iop->ioc_count == sizeof(int)) {
329 if (*(int *)mp->b_cont->b_rptr == PPPDBG_LOG + PPPDBG_IF) {
330 printf("if_ppp: debug log enabled, q=%x sp=%x\n", q, sp);
343 if (sp->flags & DBGLOG)
344 printf("if_ppp: ioctl result %d\n", error);
347 else if (error == 0) {
348 mp->b_datap->db_type = M_IOCACK;
351 mp->b_datap->db_type = M_IOCNAK;
353 iop->ioc_error = error;
376 sp = (if_ppp_t *) q->q_ptr;
377 switch (mp->b_datap->db_type) {
380 * Convert the message into an mbuf chain
381 * and inject it into the network code.
383 if (sp->flags & DBGLOG)
384 printf("if_ppp: rput pkt len %d data %x %x %x %x %x %x %x %x\n",
385 msgdsize(mp), mp->b_rptr[0], mp->b_rptr[1], mp->b_rptr[2],
386 mp->b_rptr[3], mp->b_rptr[4], mp->b_rptr[5], mp->b_rptr[6],
393 if (sp->unit >= ppp_nalloc || (ifp = ifs[sp->unit]) == 0) {
395 printf("if_ppp: no unit %d!\n", sp->unit);
401 if ((ifp->if_flags & IFF_UP) == 0) {
407 proto = PPP_PROTOCOL(mp->b_rptr);
408 adjmsg(mp, PPP_HDRLEN);
410 mb = make_mbufs(mp, sizeof(struct ifnet *));
413 if (sp->flags & DBGLOG)
414 printf("if_ppp%d: make_mbufs failed\n", ifp->if_unit);
420 if (proto == PPP_IP && (ifp->if_flags & IFF_PROMISC)) {
423 nif.nif_header = (caddr_t) &snit_ehdr;
424 nif.nif_hdrlen = sizeof(snit_ehdr);
425 nif.nif_bodylen = len;
427 snit_intr(ifp, mb, &nif);
432 * For Digital UNIX, there's space set aside in the header mbuf
433 * for the interface info.
435 * For Sun it's smuggled around via a pointer at the front of the mbuf.
438 mb->m_pkthdr.rcvif = ifp;
439 mb->m_pkthdr.len = len;
441 mb->m_off -= sizeof(struct ifnet *);
442 mb->m_len += sizeof(struct ifnet *);
443 *mtod(mb, struct ifnet **) = ifp;
450 schednetisr(NETISR_IP);
458 if (sp->flags & DBGLOG)
459 printf("if_ppp: inq full, proto=%x\n", proto);
466 if (sp->flags & DBGLOG)
467 printf("if_ppp%d: proto=%x?\n", ifp->if_unit, proto);
480 * Network code wants to output a packet.
481 * Turn it into a STREAMS message and send it down.
484 if_ppp_output(ifp, m0, dst)
487 struct sockaddr *dst;
494 if ((ifp->if_flags & IFF_UP) == 0) {
499 if ((unsigned)ifp->if_unit >= ppp_nalloc) {
501 printf("if_ppp_output: unit %d?\n", ifp->if_unit);
506 sp = states[ifp->if_unit];
509 printf("if_ppp_output: no queue?\n");
515 if (sp->flags & DBGLOG) {
516 p = mtod(m0, u_char *);
517 printf("if_ppp_output%d: af=%d data=%x %x %x %x %x %x %x %x q=%x\n",
518 ifp->if_unit, dst->sa_family, p[0], p[1], p[2], p[3], p[4],
519 p[5], p[6], p[7], sp->q);
522 switch (dst->sa_family) {
526 if (ifp->if_flags & IFF_PROMISC) {
531 for (len = 0, m = m0; m != NULL; m = m->m_next)
533 nif.nif_header = (caddr_t) &snit_ehdr;
534 nif.nif_hdrlen = sizeof(snit_ehdr);
535 nif.nif_bodylen = len;
537 snit_intr(ifp, m0, &nif);
548 mp = make_message(m0, PPP_HDRLEN);
554 mp->b_rptr -= PPP_HDRLEN;
555 mp->b_rptr[0] = PPP_ALLSTATIONS;
556 mp->b_rptr[1] = PPP_UI;
557 mp->b_rptr[2] = proto >> 8;
558 mp->b_rptr[3] = proto;
561 if (sp->flags & DBGLOG)
562 printf("if_ppp: putnext(%x, %x), r=%x w=%x p=%x\n",
563 sp->q, mp, mp->b_rptr, mp->b_wptr, proto);
571 * Socket ioctl routine for ppp interfaces.
574 if_ppp_ioctl(ifp, cmd, data)
580 struct ifreq *ifr = (struct ifreq *) data;
581 struct ifaddr *ifa = (struct ifaddr *) data;
587 if ((ifp->if_flags & IFF_RUNNING) == 0)
588 ifp->if_flags &= ~IFF_UP;
592 if (IFA_ADDR(ifa).sa_family != AF_INET)
593 error = EAFNOSUPPORT;
597 if (IFA_ADDR(ifa).sa_family != AF_INET)
598 error = EAFNOSUPPORT;
602 if ((error = NOTSUSER()) != 0)
604 if (ifr->ifr_mtu < PPP_MINMTU || ifr->ifr_mtu > PPP_MAXMTU) {
608 ifp->if_mtu = ifr->ifr_mtu;
612 ifr->ifr_mtu = ifp->if_mtu;
617 switch(ifr->ifr_addr.sa_family) {
621 error = EAFNOSUPPORT;
634 * Turn a STREAMS message into an mbuf chain.
641 struct mbuf *head, **prevp, *m;
643 unsigned char *cp, *dp;
652 MGETHDR(m, M_DONTWAIT, MT_DATA);
657 dp = mtod(m, unsigned char *);
662 while (cp >= mp->b_wptr) {
672 MGET(m, M_DONTWAIT, MT_DATA);
679 if (len + off > 2 * MLEN) {
681 MCLGET(m, M_DONTWAIT);
687 space = ((m->m_flags & M_EXT) ? MCLBYTES : MLEN);
689 space = (m->m_off > MMAXOFF? MCLBYTES: MLEN) - off;
694 dp = mtod(m, unsigned char *);
709 * Turn an mbuf chain into a STREAMS message.
711 #define ALLOCB_MAX 4096
718 mblk_t *head, **prevp, *mp;
719 int len, space, n, nb;
720 unsigned char *cp, *dp;
724 for (nm = m; nm != 0; nm = nm->m_next)
728 cp = mtod(m, unsigned char *);
737 cp = mtod(m, unsigned char *);
742 if (space > ALLOCB_MAX)
744 mp = allocb(space, BPRI_LO);
751 dp = mp->b_rptr += off;
757 n = nb < space? nb: space;
768 * Digital UNIX doesn't allow for removing ifnet structures
769 * from the list. But then we're not using this as a loadable
770 * module anyway, so that's OK.
772 * Under SunOS, this should allow the module to be unloaded.
773 * Unfortunately, it doesn't seem to detach all the references,
774 * so your system may well crash after you unload this module :-(
779 * Remove an interface from the system.
780 * This routine contains magic.
782 #include <net/route.h>
783 #include <netinet/in_pcb.h>
784 #include <netinet/ip_var.h>
785 #include <netinet/tcp.h>
786 #include <netinet/tcp_timer.h>
787 #include <netinet/tcp_var.h>
788 #include <netinet/udp.h>
789 #include <netinet/udp_var.h>
798 struct in_ifaddr **inap;
804 * Clear the interface from any routes currently cached in
805 * TCP or UDP protocol control blocks.
807 for (pcb = tcb.inp_next; pcb != &tcb; pcb = pcb->inp_next)
808 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
810 for (pcb = udb.inp_next; pcb != &udb; pcb = pcb->inp_next)
811 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
815 * Delete routes through all addresses of the interface.
817 for (ifa = ifp->if_addrlist; ifa != 0; ifa = ifa->ifa_next) {
818 rtinit(ifa, ifa, SIOCDELRT, RTF_HOST);
819 rtinit(ifa, ifa, SIOCDELRT, 0);
823 * Unlink the interface's address(es) from the in_ifaddr list.
825 for (inap = &in_ifaddr; *inap != 0; ) {
826 if ((*inap)->ia_ifa.ifa_ifp == ifp)
827 *inap = (*inap)->ia_next;
829 inap = &(*inap)->ia_next;
833 * Delete the interface from the ifnet list.
835 for (ifpp = &ifnet; (*ifpp) != 0; ) {
838 ifpp = &(*ifpp)->if_next;
841 printf("couldn't find interface ppp%d in ifnet list\n", ifp->if_unit);
843 *ifpp = ifp->if_next;