2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Attribute Value Pair creating routines
17 #include <netinet/in.h>
18 #include <sys/utsname.h>
26 } __attribute__ ((packed));
28 void add_header(struct buffer *buf, _u8 length, _u16 type) {
29 struct avp_hdr *avp = (struct avp_hdr *) (buf->start + buf->len);
30 avp->length = htons (length | MBIT);
31 avp->vendorid = htons (VENDOR_ID);
32 avp->attr = htons (type);
36 * These routines should add avp's to a buffer
40 /* FIXME: If SANITY is on, we should check for buffer overruns */
42 int add_message_type_avp (struct buffer *buf, _u16 type)
44 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
45 add_header(buf, 0x8, 0);
46 ptr->s0 = htons(type);
51 int add_protocol_avp (struct buffer *buf)
53 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
54 add_header(buf, 0x8, 0x2); /* Length and M bit */
55 ptr->s0 = htons (OUR_L2TP_VERSION);
60 int add_frame_caps_avp (struct buffer *buf, _u16 caps)
62 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
63 add_header(buf, 0xA, 0x3);
65 ptr->s1 = htons (caps);
70 int add_bearer_caps_avp (struct buffer *buf, _u16 caps)
72 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
73 add_header(buf, 0xA, 0x4);
75 ptr->s1 = htons (caps);
80 /* FIXME: I need to send tie breaker AVP's */
82 int add_firmware_avp (struct buffer *buf)
84 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
85 add_header(buf, 0x8, 0x6);
86 ptr->s0 = htons (FIRMWARE_REV);
91 int add_hostname_avp (struct buffer *buf, const char *hostname)
93 size_t namelen = strlen(hostname);
94 if (namelen > MAXAVPSIZE - 6) {
95 namelen = MAXAVPSIZE - 6;
97 add_header(buf, 0x6 + namelen, 0x7);
98 strncpy ((char *) (buf->start + buf->len + sizeof(struct avp_hdr)),
100 buf->len += 0x6 + namelen;
104 int add_vendor_avp (struct buffer *buf)
106 add_header(buf, 0x6 + strlen (VENDOR_NAME), 0x8);
107 strcpy ((char *) (buf->start + buf->len + sizeof(struct avp_hdr)), VENDOR_NAME);
108 buf->len += 0x6 + strlen (VENDOR_NAME);
112 int add_tunnelid_avp (struct buffer *buf, _u16 tid)
114 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
115 add_header(buf, 0x8, 0x9);
116 ptr->s0 = htons (tid);
121 int add_avp_rws (struct buffer *buf, _u16 rws)
123 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
124 add_header(buf, 0x8, 0xA);
125 ptr->s0 = htons (rws);
130 int add_challenge_avp (struct buffer *buf, unsigned char *c, int len)
132 add_header(buf, (0x6 + len), 0xB);
133 memcpy((char *) (buf->start + buf->len + sizeof(struct avp_hdr)), c, len);
134 buf->len += 0x6 + len;
138 int add_chalresp_avp (struct buffer *buf, unsigned char *c, int len)
140 add_header(buf, (0x6 + len), 0xD);
141 memcpy((char *) (buf->start + buf->len + sizeof(struct avp_hdr)), c, len);
142 buf->len += 0x6 + len;
146 int add_randvect_avp (struct buffer *buf, unsigned char *c, int len)
148 add_header(buf, (0x6 + len), 0x24);
149 memcpy((char *) (buf->start + buf->len + sizeof(struct avp_hdr)), c, len);
150 buf->len += 0x6 + len;
154 int add_result_code_avp (struct buffer *buf, _u16 result, _u16 error,
157 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
158 add_header(buf, (0xA + len), 0x1);
159 ptr->s0 = htons (result);
160 ptr->s1 = htons (error);
161 memcpy ((char *) &ptr->s2, msg, len);
162 buf->len += 0xA + len;
167 int add_callid_avp (struct buffer *buf, _u16 callid, struct tunnel *t)
170 int add_callid_avp (struct buffer *buf, _u16 callid)
173 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
178 add_header(buf, 0x8, 0xE);
179 ptr->s0 = htons (callid);
183 encrypt_avp (buf, 8, t);
188 int add_serno_avp (struct buffer *buf, unsigned int serno)
190 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
191 add_header(buf, 0xA, 0xF);
192 ptr->s0 = htons ((serno >> 16) & 0xFFFF);
193 ptr->s1 = htons (serno & 0xFFFF);
198 int add_bearer_avp (struct buffer *buf, int bearer)
200 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
201 add_header(buf, 0xA, 0x12);
202 ptr->s0 = htons ((bearer >> 16) & 0xFFFF);
203 ptr->s1 = htons (bearer & 0xFFFF);
208 int add_frame_avp (struct buffer *buf, int frame)
210 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
211 add_header(buf, 0xA, 0x13);
212 ptr->s0 = htons ((frame >> 16) & 0xFFFF);
213 ptr->s1 = htons (frame & 0xFFFF);
218 int add_txspeed_avp (struct buffer *buf, int speed)
220 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
221 add_header(buf, 0xA, 0x18);
222 ptr->s0 = htons ((speed >> 16) & 0xFFFF);
223 ptr->s1 = htons (speed & 0xFFFF);
228 int add_rxspeed_avp (struct buffer *buf, int speed)
230 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
231 add_header(buf, 0xA, 0x26);
232 ptr->s0 = htons ((speed >> 16) & 0xFFFF);
233 ptr->s1 = htons (speed & 0xFFFF);
238 int add_physchan_avp (struct buffer *buf, unsigned int physchan)
240 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
241 add_header(buf, 0xA, 0x19);
242 ptr->s0 = htons ((physchan >> 16) & 0xFFFF);
243 ptr->s1 = htons (physchan & 0xFFFF);
248 int add_ppd_avp (struct buffer *buf, _u16 ppd)
250 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
251 add_header(buf, 0x8, 0x14);
252 ptr->s0 = htons (ppd);
257 int add_seqreqd_avp (struct buffer *buf)
259 add_header(buf, 0x6, 0x27);
264 /* jz: options dor the outgoing call */
266 /* jz: Minimum BPS - 16 */
267 int add_minbps_avp (struct buffer *buf, int speed)
269 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
270 add_header(buf, 0xA, 0x10);
271 ptr->s0 = htons ((speed >> 16) & 0xFFFF);
272 ptr->s1 = htons (speed & 0xFFFF);
277 /* jz: Maximum BPS - 17 */
278 int add_maxbps_avp (struct buffer *buf, int speed)
280 struct half_words *ptr = (struct half_words *) (buf->start + buf->len + sizeof(struct avp_hdr));
281 add_header(buf, 0xA, 0x11);
282 ptr->s0 = htons ((speed >> 16) & 0xFFFF);
283 ptr->s1 = htons (speed & 0xFFFF);
288 /* jz: Dialed Number 21 */
289 int add_number_avp (struct buffer *buf, char *no)
291 add_header(buf, (0x6 + strlen (no)), 0x15);
292 strncpy ((char *) (buf->start + buf->len + sizeof(struct avp_hdr)), no, strlen (no));
293 buf->len += 0x6 + strlen (no);