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.1 1996/07/01 01:01:30 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>
60 #define SIOCSIFMTU SIOCSIPMTU
61 #define SIOCGIFMTU SIOCRIPMTU
62 #define IFA_ADDR(ifa) (*(ifa)->ifa_addr)
64 #define IFA_ADDR(ifa) ((ifa)->ifa_addr)
67 #define ifr_mtu ifr_metric
70 #define PPP_MAXMTU 65536
72 static int if_ppp_open __P((queue_t *, int, int, int));
73 static int if_ppp_close __P((queue_t *, int));
74 static int if_ppp_wput __P((queue_t *, mblk_t *));
75 static int if_ppp_rput __P((queue_t *, mblk_t *));
77 #define PPP_IF_ID 0x8021
78 static struct module_info minfo = {
79 PPP_IF_ID, "if_ppp", 0, INFPSZ, 4096, 128
82 static struct qinit rinit = {
83 if_ppp_rput, NULL, if_ppp_open, if_ppp_close, NULL, &minfo, NULL
86 static struct qinit winit = {
87 if_ppp_wput, NULL, NULL, NULL, NULL, &minfo, NULL
90 struct streamtab if_pppinfo = {
91 &rinit, &winit, NULL, NULL
94 typedef struct if_ppp_state {
100 /* Values for flags */
103 static int if_ppp_count; /* Number of currently-active streams */
105 static int ppp_nalloc; /* Number of elements of ifs and states */
106 static struct ifnet **ifs; /* Array of pointers to interface structs */
107 static if_ppp_t **states; /* Array of pointers to state structs */
109 static int if_ppp_output __P((struct ifnet *, struct mbuf *,
111 static int if_ppp_ioctl __P((struct ifnet *, u_int, caddr_t));
112 static struct mbuf *make_mbufs __P((mblk_t *, int));
113 static mblk_t *make_message __P((struct mbuf *, int));
116 static void ppp_if_detach __P((struct ifnet *));
119 * Detach all the interfaces before unloading.
120 * Not sure this works.
127 if (if_ppp_count > 0)
129 for (i = 0; i < ppp_nalloc; ++i)
131 ppp_if_detach(ifs[i]);
132 FREE(ifs, ppp_nalloc * sizeof (struct ifnet *));
133 FREE(states, ppp_nalloc * sizeof (struct if_ppp_t *));
140 * STREAMS module entry points.
143 if_ppp_open(q, dev, flag, sflag)
151 sp = (if_ppp_t *) ALLOC_SLEEP(sizeof (if_ppp_t));
154 bzero(sp, sizeof (if_ppp_t));
155 q->q_ptr = (caddr_t) sp;
156 WR(q)->q_ptr = (caddr_t) sp;
157 sp->unit = -1; /* no interface unit attached at present */
166 if_ppp_close(q, flag)
173 sp = (if_ppp_t *) q->q_ptr;
175 if (sp->flags & DBGLOG)
176 printf("if_ppp closed, q=%x sp=%x\n", q, sp);
178 if (sp->unit < ppp_nalloc) {
179 states[sp->unit] = 0;
182 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
185 printf("if_ppp: unit %d nonexistent!\n", sp->unit);
189 FREE(sp, sizeof (if_ppp_t));
205 sp = (if_ppp_t *) q->q_ptr;
206 switch (mp->b_datap->db_type) {
209 * Now why would we be getting data coming in here??
211 if (sp->flags & DBGLOG)
212 printf("if_ppp: got M_DATA len=%d\n", msgdsize(mp));
217 iop = (struct iocblk *) mp->b_rptr;
220 if (sp->flags & DBGLOG)
221 printf("if_ppp: got ioctl cmd=%x count=%d\n",
222 iop->ioc_cmd, iop->ioc_count);
224 switch (iop->ioc_cmd) {
225 case PPPIO_NEWPPA: /* well almost */
226 if (iop->ioc_count != sizeof(int) || sp->unit >= 0)
228 if ((error = NOTSUSER()) != 0)
230 unit = *(int *)mp->b_cont->b_rptr;
232 /* Check that this unit isn't already in use */
233 if (unit < ppp_nalloc && states[unit] != 0) {
238 /* Extend ifs and states arrays if necessary. */
240 if (unit >= ppp_nalloc) {
242 struct ifnet **newifs;
243 if_ppp_t **newstates;
246 if (sp->flags & DBGLOG)
247 printf("if_ppp: extending ifs to %d\n", newn);
248 newifs = (struct ifnet **)
249 ALLOC_NOSLEEP(newn * sizeof (struct ifnet *));
252 bzero(newifs, newn * sizeof (struct ifnet *));
253 newstates = (if_ppp_t **)
254 ALLOC_NOSLEEP(newn * sizeof (struct if_ppp_t *));
255 if (newstates == 0) {
256 FREE(newifs, newn * sizeof (struct ifnet *));
259 bzero(newstates, newn * sizeof (struct if_ppp_t *));
260 bcopy(ifs, newifs, ppp_nalloc * sizeof(struct ifnet *));
261 bcopy(states, newstates, ppp_nalloc * sizeof(if_ppp_t *));
267 /* Allocate a new ifnet struct if necessary. */
270 ifp = (struct ifnet *) ALLOC_NOSLEEP(sizeof (struct ifnet));
273 bzero(ifp, sizeof (struct ifnet));
275 ifp->if_name = "ppp";
277 ifp->if_mtu = PPP_MRU;
278 ifp->if_flags = IFF_POINTOPOINT | IFF_RUNNING;
280 ifp->if_flags |= IFF_MULTICAST;
282 ifp->if_output = if_ppp_output;
284 ifp->if_version = "Point-to-Point Protocol, version 2.3";
285 ifp->if_mediamtu = 1500;
286 ifp->if_type = IFT_PPP;
287 ifp->if_hdrlen = PPP_HDRLEN;
290 ifp->if_affinity = NETMASTERCPU;
293 ifp->if_ioctl = if_ppp_ioctl;
294 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
296 if (sp->flags & DBGLOG)
297 printf("if_ppp: created unit %d\n", unit);
299 ifp->if_mtu = PPP_MRU;
300 ifp->if_flags |= IFF_RUNNING;
308 if (sp->flags & DBGLOG)
309 printf("if_ppp: attached unit %d, sp=%x q=%x\n", unit,
315 if (iop->ioc_count == sizeof(int)) {
316 if (*(int *)mp->b_cont->b_rptr == PPPDBG_LOG + PPPDBG_IF) {
317 printf("if_ppp: debug log enabled, q=%x sp=%x\n", q, sp);
330 if (sp->flags & DBGLOG)
331 printf("if_ppp: ioctl result %d\n", error);
334 else if (error == 0) {
335 mp->b_datap->db_type = M_IOCACK;
338 mp->b_datap->db_type = M_IOCNAK;
340 iop->ioc_error = error;
363 sp = (if_ppp_t *) q->q_ptr;
364 switch (mp->b_datap->db_type) {
367 * Convert the message into an mbuf chain
368 * and inject it into the network code.
370 if (sp->flags & DBGLOG)
371 printf("if_ppp: rput pkt len %d data %x %x %x %x %x %x %x %x\n",
372 msgdsize(mp), mp->b_rptr[0], mp->b_rptr[1], mp->b_rptr[2],
373 mp->b_rptr[3], mp->b_rptr[4], mp->b_rptr[5], mp->b_rptr[6],
380 if (sp->unit >= ppp_nalloc || (ifp = ifs[sp->unit]) == 0) {
382 printf("if_ppp: no unit %d!\n", sp->unit);
388 if ((ifp->if_flags & IFF_UP) == 0) {
394 proto = PPP_PROTOCOL(mp->b_rptr);
395 adjmsg(mp, PPP_HDRLEN);
397 mb = make_mbufs(mp, sizeof(struct ifnet *));
400 if (sp->flags & DBGLOG)
401 printf("if_ppp%d: make_mbufs failed\n", ifp->if_unit);
406 /* For Digital UNIX, there's space set aside in the header mbuf
407 * for the interface info.
409 * For Sun it's smuggled around via a pointer at the front of the mbuf
412 mb->m_pkthdr.rcvif = ifp;
413 mb->m_pkthdr.len = len;
415 mb->m_off -= sizeof(struct ifnet *);
416 mb->m_len += sizeof(struct ifnet *);
417 *mtod(mb, struct ifnet **) = ifp;
424 schednetisr(NETISR_IP);
432 if (sp->flags & DBGLOG)
433 printf("if_ppp: inq full, proto=%x\n", proto);
440 if (sp->flags & DBGLOG)
441 printf("if_ppp%d: proto=%x?\n", ifp->if_unit, proto);
454 * Network code wants to output a packet.
455 * Turn it into a STREAMS message and send it down.
458 if_ppp_output(ifp, m0, dst)
461 struct sockaddr *dst;
468 if ((ifp->if_flags & IFF_UP) == 0) {
473 if ((unsigned)ifp->if_unit >= ppp_nalloc) {
475 printf("if_ppp_output: unit %d?\n", ifp->if_unit);
480 sp = states[ifp->if_unit];
483 printf("if_ppp_output: no queue?\n");
489 if (sp->flags & DBGLOG) {
490 p = mtod(m0, u_char *);
491 printf("if_ppp_output%d: af=%d data=%x %x %x %x %x %x %x %x q=%x\n",
492 ifp->if_unit, dst->sa_family, p[0], p[1], p[2], p[3], p[4],
493 p[5], p[6], p[7], sp->q);
496 switch (dst->sa_family) {
506 mp = make_message(m0, PPP_HDRLEN);
512 mp->b_rptr -= PPP_HDRLEN;
513 mp->b_rptr[0] = PPP_ALLSTATIONS;
514 mp->b_rptr[1] = PPP_UI;
515 mp->b_rptr[2] = proto >> 8;
516 mp->b_rptr[3] = proto;
519 if (sp->flags & DBGLOG)
520 printf("if_ppp: putnext(%x, %x), r=%x w=%x p=%x\n",
521 sp->q, mp, mp->b_rptr, mp->b_wptr, proto);
529 * Socket ioctl routine for ppp interfaces.
532 if_ppp_ioctl(ifp, cmd, data)
538 struct ifreq *ifr = (struct ifreq *) data;
539 struct ifaddr *ifa = (struct ifaddr *) data;
545 if ((ifp->if_flags & IFF_RUNNING) == 0)
546 ifp->if_flags &= ~IFF_UP;
550 if (IFA_ADDR(ifa).sa_family != AF_INET)
551 error = EAFNOSUPPORT;
555 if (IFA_ADDR(ifa).sa_family != AF_INET)
556 error = EAFNOSUPPORT;
560 if ((error = NOTSUSER()) != 0)
562 if (ifr->ifr_mtu < PPP_MINMTU || ifr->ifr_mtu > PPP_MAXMTU) {
566 ifp->if_mtu = ifr->ifr_mtu;
570 ifr->ifr_mtu = ifp->if_mtu;
575 switch(ifr->ifr_addr.sa_family) {
579 error = EAFNOSUPPORT;
592 * Turn a STREAMS message into an mbuf chain.
599 struct mbuf *head, **prevp, *m;
601 unsigned char *cp, *dp;
610 MGETHDR(m, M_DONTWAIT, MT_DATA);
615 dp = mtod(m, unsigned char *);
620 while (cp >= mp->b_wptr) {
630 MGET(m, M_DONTWAIT, MT_DATA);
637 if (len + off > 2 * MLEN) {
639 MCLGET(m, M_DONTWAIT);
645 space = ((m->m_flags & M_EXT) ? MCLBYTES : MLEN);
647 space = (m->m_off > MMAXOFF? MCLBYTES: MLEN) - off;
652 dp = mtod(m, unsigned char *);
667 * Turn an mbuf chain into a STREAMS message.
669 #define ALLOCB_MAX 4096
676 mblk_t *head, **prevp, *mp;
677 int len, space, n, nb;
678 unsigned char *cp, *dp;
682 for (nm = m; nm != 0; nm = nm->m_next)
686 cp = mtod(m, unsigned char *);
695 cp = mtod(m, unsigned char *);
700 if (space > ALLOCB_MAX)
702 mp = allocb(space, BPRI_LO);
709 dp = mp->b_rptr += off;
715 n = nb < space? nb: space;
725 /* Digital UNIX doesn't allow for removing ifnet structures
726 * from the list. Taking the i/f down from pppd will take
727 * care of most of the stuff that this code intends to do
733 * Remove an interface from the system.
734 * This routine contains magic.
736 #include <net/route.h>
737 #include <netinet/in_pcb.h>
738 #include <netinet/ip_var.h>
739 #include <netinet/tcp.h>
740 #include <netinet/tcp_timer.h>
741 #include <netinet/tcp_var.h>
742 #include <netinet/udp.h>
743 #include <netinet/udp_var.h>
752 struct in_ifaddr **inap;
758 * Clear the interface from any routes currently cached in
759 * TCP or UDP protocol control blocks.
761 for (pcb = tcb.inp_next; pcb != &tcb; pcb = pcb->inp_next)
762 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
764 for (pcb = udb.inp_next; pcb != &udb; pcb = pcb->inp_next)
765 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
769 * Delete routes through all addresses of the interface.
771 for (ifa = ifp->if_addrlist; ifa != 0; ifa = ifa->ifa_next) {
772 rtinit(ifa, ifa, SIOCDELRT, RTF_HOST);
773 rtinit(ifa, ifa, SIOCDELRT, 0);
777 * Unlink the interface's address(es) from the in_ifaddr list.
779 for (inap = &in_ifaddr; *inap != 0; ) {
780 if ((*inap)->ia_ifa.ifa_ifp == ifp)
781 *inap = (*inap)->ia_next;
783 inap = &(*inap)->ia_next;
787 * Delete the interface from the ifnet list.
789 for (ifpp = &ifnet; (*ifpp) != 0; ) {
792 ifpp = &(*ifpp)->if_next;
795 printf("couldn't find interface ppp%d in ifnet list\n", ifp->if_unit);
797 *ifpp = ifp->if_next;