blob: 5715865fa4b40da45731903f37273d5ba65301d7 [file] [log] [blame]
Pau Espin Pedrolf7884e82019-08-20 12:06:13 +02001/*
2 * PCO parsing related code
3 * Copyright (C) 2002, 2003, 2004 Mondru AB.
4 * Copyright (C) 2017-2019 by Harald Welte <laforge@gnumonks.org>
5 * Copyright (C) 2019 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 *
7 * The contents of this file may be used under the terms of the GNU
8 * General Public License Version 2, provided that the above copyright
9 * notice and this permission notice is included in all copies or
10 * substantial portions of the software.
11 *
12 */
13
14#include <unistd.h>
15#include <string.h>
16
17#include <osmocom/core/msgb.h>
18#include <osmocom/gsm/tlv.h>
19
20#include "pco.h"
21#include "ggsn.h"
22
23/* determine if IPCP contains given option */
24static const uint8_t *ipcp_contains_option(const struct ipcp_hdr *ipcp, size_t ipcp_len,
25 enum ipcp_options opt, size_t opt_minlen)
26{
27 const uint8_t *cur_opt = ipcp->options;
28
29 /* iterate over Options and check if protocol contained */
30 while (cur_opt + sizeof(struct ipcp_option_hdr) <= (uint8_t*)ipcp + ipcp_len) {
31 const struct ipcp_option_hdr *cur_opt_hdr = (const struct ipcp_option_hdr *)cur_opt;
32 /* length value includes 2 bytes type/length */
33 if (cur_opt_hdr->len < sizeof(struct ipcp_option_hdr))
34 return NULL;
35 if (cur_opt_hdr->type == opt &&
36 cur_opt_hdr->len >= sizeof(struct ipcp_option_hdr) + opt_minlen)
37 return cur_opt;
38 cur_opt += cur_opt_hdr->len;
39 }
40 return NULL;
41}
42
43
44static const char *pap_welcome = "Welcome to OsmoGGSN " PACKAGE_VERSION;
45
46/* Handle PAP protocol according to RFC 1334 */
47static void process_pco_element_pap(const struct pco_element *pco_in, struct msgb *resp,
48 const struct apn_ctx *apn, struct pdp_t *pdp)
49{
50 const struct pap_element *pap_in = (const struct pap_element *) pco_in->data;
51 uint16_t pap_in_len;
52 uint8_t peer_id_len;
53 const uint8_t *peer_id;
54 unsigned int pap_welcome_len;
55 uint8_t pap_out_size;
56 struct pap_element *pap_out;
57
58 if (pco_in->length < sizeof(struct pap_element))
59 goto ret_broken;
60
61 pap_in_len = osmo_load16be(&pap_in->len);
62 if (pco_in->length < pap_in_len)
63 goto ret_broken;
64 /* "pco_in->length > pap_in_len" is allowed: RFC1334 2.2 states:
65 "Octets outside the range of the Length field should be treated as
66 Data Link Layer padding and should be ignored on reception."
67 */
68
69 switch (pap_in->code) {
70 case PAP_CODE_AUTH_REQ:
71 if (pap_in_len < sizeof(struct pap_element) + 1)
72 goto ret_broken_auth;
73 peer_id_len = pap_in->data[0];
74 if (pap_in_len < sizeof(struct pap_element) + 1 + peer_id_len)
75 goto ret_broken_auth;
76 peer_id = &pap_in->data[1];
77 LOGPPDP(LOGL_DEBUG, pdp, "PCO PAP PeerId = %s, ACKing\n",
78 osmo_quote_str((const char *)peer_id, peer_id_len));
79 /* Password-Length + Password following here, but we don't care */
80
81 /* Prepare response, we ACK all of them: */
82 pap_welcome_len = strlen(pap_welcome);
83 /* +1: Length field of pap_welcome Message */
84 pap_out_size = sizeof(struct pap_element) + 1 + pap_welcome_len;
85 pap_out = alloca(pap_out_size);
86 pap_out->code = PAP_CODE_AUTH_ACK;
87 pap_out->id = pap_in->id;
88 pap_out->len = htons(pap_out_size);
89 pap_out->data[0] = pap_welcome_len;
90 memcpy(pap_out->data+1, pap_welcome, pap_welcome_len);
91 msgb_t16lv_put(resp, PCO_P_PAP, pap_out_size, (uint8_t *) pap_out);
92 break;
93 case PAP_CODE_AUTH_ACK:
94 case PAP_CODE_AUTH_NAK:
95 default:
96 LOGPPDP(LOGL_NOTICE, pdp, "Unsupported PAP PCO Code %u, ignoring\n", pap_in->code);
97 break;
98 }
99 return;
100
101ret_broken_auth:
102 LOGPPDP(LOGL_NOTICE, pdp, "Invalid PAP AuthenticateReq: %s, ignoring\n",
103 osmo_hexdump_nospc((const uint8_t *)pco_in, pco_in->length));
104 return;
105
106ret_broken:
107 LOGPPDP(LOGL_NOTICE, pdp, "Invalid PAP PCO Length: %s, ignoring\n",
108 osmo_hexdump_nospc((const uint8_t *)pco_in, pco_in->length));
109}
110
111static void process_pco_element_ipcp(const struct pco_element *pco_elem, struct msgb *resp,
112 const struct apn_ctx *apn, struct pdp_t *pdp)
113{
114 struct ippoolm_t *peer_v4 = pdp_get_peer_ipv(pdp, false);
115 const struct in46_addr *dns1 = &apn->v4.cfg.dns[0];
116 const struct in46_addr *dns2 = &apn->v4.cfg.dns[1];
117 uint8_t *start = resp->tail;
118 const struct ipcp_hdr *ipcp;
119 uint16_t ipcp_len;
120 uint8_t *len1, *len2;
121 unsigned int len_appended;
122 ptrdiff_t consumed;
123 size_t remain;
124
125 if (!peer_v4) {
126 LOGPPDP(LOGL_ERROR, pdp, "IPCP but no IPv4 type ?!?\n");
127 return;
128 }
129
130 ipcp = (const struct ipcp_hdr *)pco_elem->data;
131 consumed = (pco_elem->data - &pdp->pco_req.v[0]);
132 remain = sizeof(pdp->pco_req.v) - consumed;
133 ipcp_len = osmo_load16be(&ipcp->len);
134 if (remain < 0 || remain < ipcp_len) {
135 LOGPPDP(LOGL_ERROR, pdp, "Malformed IPCP, ignoring\n");
136 return;
137 }
138
139 /* Three byte T16L header */
140 msgb_put_u16(resp, 0x8021); /* IPCP */
141 len1 = msgb_put(resp, 1); /* Length of contents: delay */
142
143 msgb_put_u8(resp, 0x02); /* ACK */
144 msgb_put_u8(resp, ipcp->id); /* ID: Needs to match request */
145 msgb_put_u8(resp, 0x00); /* Length MSB */
146 len2 = msgb_put(resp, 1); /* Length LSB: delay */
147
148 if (dns1->len == 4 && ipcp_contains_option(ipcp, ipcp_len, IPCP_OPT_PRIMARY_DNS, 4)) {
149 msgb_put_u8(resp, 0x81); /* DNS1 Tag */
150 msgb_put_u8(resp, 2 + dns1->len); /* DNS1 Length, incl. TL */
151 msgb_put_u32(resp, ntohl(dns1->v4.s_addr));
152 }
153
154 if (dns2->len == 4 && ipcp_contains_option(ipcp, ipcp_len, IPCP_OPT_SECONDARY_DNS, 4)) {
155 msgb_put_u8(resp, 0x83); /* DNS2 Tag */
156 msgb_put_u8(resp, 2 + dns2->len); /* DNS2 Length, incl. TL */
157 msgb_put_u32(resp, ntohl(dns2->v4.s_addr));
158 }
159
160 /* patch in length values */
161 len_appended = resp->tail - start;
162 *len1 = len_appended - 3;
163 *len2 = len_appended - 3;
164}
165
166static void process_pco_element_dns_ipv6(const struct pco_element *pco_elem, struct msgb *resp,
167 const struct apn_ctx *apn, struct pdp_t *pdp)
168{
169 unsigned int i;
170 const uint8_t *tail = resp->tail;
171
172 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
173 const struct in46_addr *i46a = &apn->v6.cfg.dns[i];
174 if (i46a->len != 16)
175 continue;
176 msgb_t16lv_put(resp, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
177 }
178 if (resp->tail == tail)
179 LOGPPDP(LOGL_NOTICE, pdp, "MS requested IPv6 DNS, but APN has none configured\n");
180}
181
182static void process_pco_element_dns_ipv4(const struct pco_element *pco_elem, struct msgb *resp,
183 const struct apn_ctx *apn, struct pdp_t *pdp)
184{
185 unsigned int i;
186 const uint8_t *tail = resp->tail;
187
188 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
189 const struct in46_addr *i46a = &apn->v4.cfg.dns[i];
190 if (i46a->len != 4)
191 continue;
192 msgb_t16lv_put(resp, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
193 }
194 if (resp->tail == tail)
195 LOGPPDP(LOGL_NOTICE, pdp, "MS requested IPv4 DNS, but APN has none configured\n");
196}
197
198static void process_pco_element(const struct pco_element *pco_elem, struct msgb *resp,
199 const struct apn_ctx *apn, struct pdp_t *pdp)
200{
201 uint16_t protocol_id = osmo_load16be(&pco_elem->protocol_id);
202
203 LOGPPDP(LOGL_DEBUG, pdp, "PCO Protocol 0x%04x\n", protocol_id);
204 switch (protocol_id) {
205 case PCO_P_PAP:
206 process_pco_element_pap(pco_elem, resp, apn, pdp);
207 break;
208 case PCO_P_IPCP:
209 process_pco_element_ipcp(pco_elem, resp, apn, pdp);
210 break;
211 case PCO_P_DNS_IPv6_ADDR:
212 process_pco_element_dns_ipv6(pco_elem, resp, apn, pdp);
213 break;
214 case PCO_P_DNS_IPv4_ADDR:
215 process_pco_element_dns_ipv4(pco_elem, resp, apn, pdp);
216 break;
217 default:
218 LOGPPDP(LOGL_INFO, pdp, "Unknown/Unimplemented PCO Protocol 0x%04x: %s\n",
219 protocol_id, osmo_hexdump_nospc(pco_elem->data, pco_elem->length));
220 break;
221 }
222}
223
224/* process one PCO request from a MS/UE, putting together the proper responses */
225void process_pco(const struct apn_ctx *apn, struct pdp_t *pdp)
226{
227 struct msgb *resp = msgb_alloc(256, "PCO.resp");
228 const struct ul255_t *pco = &pdp->pco_req;
229 const struct pco_element *pco_elem;
230 const uint8_t *cur;
231
232 /* build the header of the PCO response */
233 OSMO_ASSERT(resp);
234 msgb_put_u8(resp, 0x80); /* ext-bit + configuration protocol byte */
235
236 /* iterate over the PCO elements in the request; call process_pco_element() for each */
237 for (cur = pco->v + 1, pco_elem = (const struct pco_element *) cur;
238 cur + sizeof(struct pco_element) <= pco->v + pco->l;
239 cur += pco_elem->length + sizeof(*pco_elem), pco_elem = (const struct pco_element *) cur) {
240 process_pco_element(pco_elem, resp, apn, pdp);
241 }
242
243 /* copy the PCO response msgb and copy its contents over to the PDP context */
244 if (msgb_length(resp) > 1) {
245 memcpy(pdp->pco_neg.v, msgb_data(resp), msgb_length(resp));
246 pdp->pco_neg.l = msgb_length(resp);
247 } else
248 pdp->pco_neg.l = 0;
249 msgb_free(resp);
250}