xref: /illumos-gate/usr/src/uts/common/io/ppp/sppp/s_common.c (revision fc910014e8a32a65612105835a10995f2c13d942)
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 #define	RCSID	"$Id: s_common.c,v 1.0 2000/05/08 01:10:12 masputra Exp $"
20 
21 #include <sys/types.h>
22 #include <sys/debug.h>
23 #include <sys/param.h>
24 #include <sys/stat.h>
25 #include <sys/stream.h>
26 #include <sys/stropts.h>
27 #include <sys/errno.h>
28 #include <sys/ioccom.h>
29 #include <sys/cmn_err.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <sys/strsun.h>
33 #include <net/ppp_defs.h>
34 #include <net/pppio.h>
35 #include "s_common.h"
36 
37 /*
38  * putctl4()
39  *
40  * Description:
41  *    Create and send a 4-byte message.
42  */
43 int
44 putctl4(queue_t *q, uchar_t type, uchar_t code, uint16_t val)
45 {
46 	mblk_t	*mp;
47 
48 	if ((mp = allocb(4, BPRI_HI)) == NULL) {
49 		return (0);
50 	}
51 	MTYPE(mp) = type;
52 	mp->b_wptr[0] = code;
53 	((uint16_t *)mp->b_wptr)[1] = val;
54 	mp->b_wptr += 4;
55 
56 	putnext(q, mp);
57 	return (1);
58 }
59 
60 /*
61  * putctl8()
62  *
63  * Description:
64  *    Create and send a 8-byte message.
65  */
66 int
67 putctl8(queue_t *q, uchar_t type, uchar_t code, uint32_t val)
68 {
69 	mblk_t	*mp;
70 
71 	if ((mp = allocb(8, BPRI_HI)) == NULL) {
72 		return (0);
73 	}
74 	MTYPE(mp) = type;
75 	mp->b_wptr[0] = code;
76 	((uint32_t *)mp->b_wptr)[1] = val;
77 	mp->b_wptr += 8;
78 
79 	putnext(q, mp);
80 	return (1);
81 }
82 
83 /*
84  * msg_byte()
85  *
86  * Description:
87  *    Helper routine to return a specific byte off a data buffer.
88  */
89 int
90 msg_byte(mblk_t *mp, unsigned int i)
91 {
92 	while (mp != NULL) {
93 		if (i < MBLKL(mp)) {
94 			break;
95 		}
96 		i -= MBLKL(mp);
97 		mp = mp->b_cont;
98 	}
99 	if (mp == NULL) {
100 		return (-1);
101 	}
102 	return (mp->b_rptr[i]);
103 }
104 
105 /*
106  * sppp_create_lsmsg()
107  *
108  * Description:
109  *    Create a PPP link status message.
110  */
111 mblk_t *
112 create_lsmsg(enum LSstat ls_type)
113 {
114 	mblk_t		*mp;
115 	struct ppp_ls	*plt;
116 
117 	if ((mp = allocb(sizeof (*plt), BPRI_HI)) == NULL) {
118 		return (NULL);
119 	}
120 	/*
121 	 * Make sure that this message is a control message, and contains
122 	 * a notification that the link has been terminated.
123 	 */
124 	MTYPE(mp) = M_PROTO;
125 	mp->b_wptr += sizeof (*plt);
126 	plt = (struct ppp_ls *)mp->b_rptr;
127 	plt->magic = PPPLSMAGIC;
128 	plt->ppp_message = ls_type;
129 
130 	return (mp);
131 }
132