1 /*
2  * s_common.c - common utilities for Solaris PPP
3  *
4  * Copyright (c) 2000 by Sun Microsystems, Inc.
5  * All rights reserved.
6  *
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.
10  *
11  * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
12  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SUN SHALL NOT BE LIABLE FOR
15  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
16  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
17  */
18 
19 #pragma ident	"%Z%%M%	%I%	%E% SMI"
20 #define	RCSID	"$Id: s_common.c,v 1.0 2000/05/08 01:10:12 masputra Exp $"
21 
22 #include <sys/types.h>
23 #include <sys/debug.h>
24 #include <sys/param.h>
25 #include <sys/stat.h>
26 #include <sys/stream.h>
27 #include <sys/stropts.h>
28 #include <sys/errno.h>
29 #include <sys/ioccom.h>
30 #include <sys/cmn_err.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/strsun.h>
34 #include <net/ppp_defs.h>
35 #include <net/pppio.h>
36 #include "s_common.h"
37 
38 /*
39  * putctl4()
40  *
41  * Description:
42  *    Create and send a 4-byte message.
43  */
44 int
putctl4(queue_t * q,uchar_t type,uchar_t code,uint16_t val)45 putctl4(queue_t *q, uchar_t type, uchar_t code, uint16_t val)
46 {
47 	mblk_t	*mp;
48 
49 	if ((mp = allocb(4, BPRI_HI)) == NULL) {
50 		return (0);
51 	}
52 	MTYPE(mp) = type;
53 	mp->b_wptr[0] = code;
54 	((uint16_t *)mp->b_wptr)[1] = val;
55 	mp->b_wptr += 4;
56 
57 	putnext(q, mp);
58 	return (1);
59 }
60 
61 /*
62  * putctl8()
63  *
64  * Description:
65  *    Create and send a 8-byte message.
66  */
67 int
putctl8(queue_t * q,uchar_t type,uchar_t code,uint32_t val)68 putctl8(queue_t *q, uchar_t type, uchar_t code, uint32_t val)
69 {
70 	mblk_t	*mp;
71 
72 	if ((mp = allocb(8, BPRI_HI)) == NULL) {
73 		return (0);
74 	}
75 	MTYPE(mp) = type;
76 	mp->b_wptr[0] = code;
77 	((uint32_t *)mp->b_wptr)[1] = val;
78 	mp->b_wptr += 8;
79 
80 	putnext(q, mp);
81 	return (1);
82 }
83 
84 /*
85  * msg_byte()
86  *
87  * Description:
88  *    Helper routine to return a specific byte off a data buffer.
89  */
90 int
msg_byte(mblk_t * mp,unsigned int i)91 msg_byte(mblk_t *mp, unsigned int i)
92 {
93 	while (mp != NULL) {
94 		if (i < MBLKL(mp)) {
95 			break;
96 		}
97 		i -= MBLKL(mp);
98 		mp = mp->b_cont;
99 	}
100 	if (mp == NULL) {
101 		return (-1);
102 	}
103 	return (mp->b_rptr[i]);
104 }
105 
106 /*
107  * sppp_create_lsmsg()
108  *
109  * Description:
110  *    Create a PPP link status message.
111  */
112 mblk_t *
create_lsmsg(enum LSstat ls_type)113 create_lsmsg(enum LSstat ls_type)
114 {
115 	mblk_t		*mp;
116 	struct ppp_ls	*plt;
117 
118 	if ((mp = allocb(sizeof (*plt), BPRI_HI)) == NULL) {
119 		return (NULL);
120 	}
121 	/*
122 	 * Make sure that this message is a control message, and contains
123 	 * a notification that the link has been terminated.
124 	 */
125 	MTYPE(mp) = M_PROTO;
126 	mp->b_wptr += sizeof (*plt);
127 	plt = (struct ppp_ls *)mp->b_rptr;
128 	plt->magic = PPPLSMAGIC;
129 	plt->ppp_message = ls_type;
130 
131 	return (mp);
132 }
133