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.2 1996/08/28 06:35:11 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 *));
262 FREE(ifs, ppp_nalloc * sizeof(struct ifnet *));
263 FREE(states, ppp_nalloc * sizeof(if_ppp_t *));
269 /* Allocate a new ifnet struct if necessary. */
272 ifp = (struct ifnet *) ALLOC_NOSLEEP(sizeof (struct ifnet));
275 bzero(ifp, sizeof (struct ifnet));
277 ifp->if_name = "ppp";
279 ifp->if_mtu = PPP_MRU;
280 ifp->if_flags = IFF_POINTOPOINT | IFF_RUNNING;
282 ifp->if_flags |= IFF_MULTICAST;
284 ifp->if_output = if_ppp_output;
286 ifp->if_version = "Point-to-Point Protocol, version 2.3";
287 ifp->if_mediamtu = 1500;
288 ifp->if_type = IFT_PPP;
289 ifp->if_hdrlen = PPP_HDRLEN;
292 ifp->if_affinity = NETMASTERCPU;
295 ifp->if_ioctl = if_ppp_ioctl;
296 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
298 if (sp->flags & DBGLOG)
299 printf("if_ppp: created unit %d\n", unit);
301 ifp->if_mtu = PPP_MRU;
302 ifp->if_flags |= IFF_RUNNING;
310 if (sp->flags & DBGLOG)
311 printf("if_ppp: attached unit %d, sp=%x q=%x\n", unit,
317 if (iop->ioc_count == sizeof(int)) {
318 if (*(int *)mp->b_cont->b_rptr == PPPDBG_LOG + PPPDBG_IF) {
319 printf("if_ppp: debug log enabled, q=%x sp=%x\n", q, sp);
332 if (sp->flags & DBGLOG)
333 printf("if_ppp: ioctl result %d\n", error);
336 else if (error == 0) {
337 mp->b_datap->db_type = M_IOCACK;
340 mp->b_datap->db_type = M_IOCNAK;
342 iop->ioc_error = error;
365 sp = (if_ppp_t *) q->q_ptr;
366 switch (mp->b_datap->db_type) {
369 * Convert the message into an mbuf chain
370 * and inject it into the network code.
372 if (sp->flags & DBGLOG)
373 printf("if_ppp: rput pkt len %d data %x %x %x %x %x %x %x %x\n",
374 msgdsize(mp), mp->b_rptr[0], mp->b_rptr[1], mp->b_rptr[2],
375 mp->b_rptr[3], mp->b_rptr[4], mp->b_rptr[5], mp->b_rptr[6],
382 if (sp->unit >= ppp_nalloc || (ifp = ifs[sp->unit]) == 0) {
384 printf("if_ppp: no unit %d!\n", sp->unit);
390 if ((ifp->if_flags & IFF_UP) == 0) {
396 proto = PPP_PROTOCOL(mp->b_rptr);
397 adjmsg(mp, PPP_HDRLEN);
399 mb = make_mbufs(mp, sizeof(struct ifnet *));
402 if (sp->flags & DBGLOG)
403 printf("if_ppp%d: make_mbufs failed\n", ifp->if_unit);
408 /* For Digital UNIX, there's space set aside in the header mbuf
409 * for the interface info.
411 * For Sun it's smuggled around via a pointer at the front of the mbuf
414 mb->m_pkthdr.rcvif = ifp;
415 mb->m_pkthdr.len = len;
417 mb->m_off -= sizeof(struct ifnet *);
418 mb->m_len += sizeof(struct ifnet *);
419 *mtod(mb, struct ifnet **) = ifp;
426 schednetisr(NETISR_IP);
434 if (sp->flags & DBGLOG)
435 printf("if_ppp: inq full, proto=%x\n", proto);
442 if (sp->flags & DBGLOG)
443 printf("if_ppp%d: proto=%x?\n", ifp->if_unit, proto);
456 * Network code wants to output a packet.
457 * Turn it into a STREAMS message and send it down.
460 if_ppp_output(ifp, m0, dst)
463 struct sockaddr *dst;
470 if ((ifp->if_flags & IFF_UP) == 0) {
475 if ((unsigned)ifp->if_unit >= ppp_nalloc) {
477 printf("if_ppp_output: unit %d?\n", ifp->if_unit);
482 sp = states[ifp->if_unit];
485 printf("if_ppp_output: no queue?\n");
491 if (sp->flags & DBGLOG) {
492 p = mtod(m0, u_char *);
493 printf("if_ppp_output%d: af=%d data=%x %x %x %x %x %x %x %x q=%x\n",
494 ifp->if_unit, dst->sa_family, p[0], p[1], p[2], p[3], p[4],
495 p[5], p[6], p[7], sp->q);
498 switch (dst->sa_family) {
508 mp = make_message(m0, PPP_HDRLEN);
514 mp->b_rptr -= PPP_HDRLEN;
515 mp->b_rptr[0] = PPP_ALLSTATIONS;
516 mp->b_rptr[1] = PPP_UI;
517 mp->b_rptr[2] = proto >> 8;
518 mp->b_rptr[3] = proto;
521 if (sp->flags & DBGLOG)
522 printf("if_ppp: putnext(%x, %x), r=%x w=%x p=%x\n",
523 sp->q, mp, mp->b_rptr, mp->b_wptr, proto);
531 * Socket ioctl routine for ppp interfaces.
534 if_ppp_ioctl(ifp, cmd, data)
540 struct ifreq *ifr = (struct ifreq *) data;
541 struct ifaddr *ifa = (struct ifaddr *) data;
547 if ((ifp->if_flags & IFF_RUNNING) == 0)
548 ifp->if_flags &= ~IFF_UP;
552 if (IFA_ADDR(ifa).sa_family != AF_INET)
553 error = EAFNOSUPPORT;
557 if (IFA_ADDR(ifa).sa_family != AF_INET)
558 error = EAFNOSUPPORT;
562 if ((error = NOTSUSER()) != 0)
564 if (ifr->ifr_mtu < PPP_MINMTU || ifr->ifr_mtu > PPP_MAXMTU) {
568 ifp->if_mtu = ifr->ifr_mtu;
572 ifr->ifr_mtu = ifp->if_mtu;
577 switch(ifr->ifr_addr.sa_family) {
581 error = EAFNOSUPPORT;
594 * Turn a STREAMS message into an mbuf chain.
601 struct mbuf *head, **prevp, *m;
603 unsigned char *cp, *dp;
612 MGETHDR(m, M_DONTWAIT, MT_DATA);
617 dp = mtod(m, unsigned char *);
622 while (cp >= mp->b_wptr) {
632 MGET(m, M_DONTWAIT, MT_DATA);
639 if (len + off > 2 * MLEN) {
641 MCLGET(m, M_DONTWAIT);
647 space = ((m->m_flags & M_EXT) ? MCLBYTES : MLEN);
649 space = (m->m_off > MMAXOFF? MCLBYTES: MLEN) - off;
654 dp = mtod(m, unsigned char *);
669 * Turn an mbuf chain into a STREAMS message.
671 #define ALLOCB_MAX 4096
678 mblk_t *head, **prevp, *mp;
679 int len, space, n, nb;
680 unsigned char *cp, *dp;
684 for (nm = m; nm != 0; nm = nm->m_next)
688 cp = mtod(m, unsigned char *);
697 cp = mtod(m, unsigned char *);
702 if (space > ALLOCB_MAX)
704 mp = allocb(space, BPRI_LO);
711 dp = mp->b_rptr += off;
717 n = nb < space? nb: space;
727 /* Digital UNIX doesn't allow for removing ifnet structures
728 * from the list. Taking the i/f down from pppd will take
729 * care of most of the stuff that this code intends to do
735 * Remove an interface from the system.
736 * This routine contains magic.
738 #include <net/route.h>
739 #include <netinet/in_pcb.h>
740 #include <netinet/ip_var.h>
741 #include <netinet/tcp.h>
742 #include <netinet/tcp_timer.h>
743 #include <netinet/tcp_var.h>
744 #include <netinet/udp.h>
745 #include <netinet/udp_var.h>
754 struct in_ifaddr **inap;
760 * Clear the interface from any routes currently cached in
761 * TCP or UDP protocol control blocks.
763 for (pcb = tcb.inp_next; pcb != &tcb; pcb = pcb->inp_next)
764 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
766 for (pcb = udb.inp_next; pcb != &udb; pcb = pcb->inp_next)
767 if (pcb->inp_route.ro_rt && pcb->inp_route.ro_rt->rt_ifp == ifp)
771 * Delete routes through all addresses of the interface.
773 for (ifa = ifp->if_addrlist; ifa != 0; ifa = ifa->ifa_next) {
774 rtinit(ifa, ifa, SIOCDELRT, RTF_HOST);
775 rtinit(ifa, ifa, SIOCDELRT, 0);
779 * Unlink the interface's address(es) from the in_ifaddr list.
781 for (inap = &in_ifaddr; *inap != 0; ) {
782 if ((*inap)->ia_ifa.ifa_ifp == ifp)
783 *inap = (*inap)->ia_next;
785 inap = &(*inap)->ia_next;
789 * Delete the interface from the ifnet list.
791 for (ifpp = &ifnet; (*ifpp) != 0; ) {
794 ifpp = &(*ifpp)->if_next;
797 printf("couldn't find interface ppp%d in ifnet list\n", ifp->if_unit);
799 *ifpp = ifp->if_next;