Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 1 | /* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #include <osmocore/msgb.h> |
| 27 | #include <osmocore/linuxlist.h> |
| 28 | #include <osmocore/timer.h> |
| 29 | #include <osmocore/talloc.h> |
| 30 | |
| 31 | #include <openbsc/gsm_data.h> |
| 32 | #include <openbsc/debug.h> |
| 33 | #include <openbsc/gprs_bssgp.h> |
| 34 | #include <openbsc/gprs_llc.h> |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 35 | #include <openbsc/sgsn.h> |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 36 | |
| 37 | /* Chapter 7.2: SN-PDU Formats */ |
| 38 | struct sndcp_common_hdr { |
| 39 | /* octet 1 */ |
| 40 | uint8_t nsapi:4; |
| 41 | uint8_t more:1; |
| 42 | uint8_t type:1; |
| 43 | uint8_t first:1; |
| 44 | uint8_t spare:1; |
| 45 | /* octet 2 */ |
Harald Welte | cc2279b | 2010-06-02 23:17:05 +0200 | [diff] [blame] | 46 | uint8_t pcomp:4; |
| 47 | uint8_t dcomp:4; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 48 | } __attribute__((packed)); |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 49 | |
| 50 | struct sndcp_udata_hdr { |
| 51 | /* octet 3 */ |
| 52 | uint8_t npdu_high:4; |
| 53 | uint8_t seg_nr:4; |
| 54 | /* octet 4 */ |
| 55 | uint8_t npdu_low; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 56 | } __attribute__((packed)); |
| 57 | |
| 58 | /* See 6.7.1.2 Reassembly */ |
| 59 | enum sndcp_rx_state { |
| 60 | SNDCP_RX_S_FIRST, |
| 61 | SNDCP_RX_S_SUBSEQ, |
| 62 | SNDCP_RX_S_DISCARD, |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | static void *tall_sndcp_ctx; |
| 67 | |
| 68 | /* A fragment queue entry, containing one framgent of a N-PDU */ |
| 69 | struct frag_queue_entry { |
| 70 | struct llist_head list; |
| 71 | uint8_t seg_nr; |
| 72 | uint32_t data_len; |
| 73 | uint8_t data[0]; |
| 74 | }; |
| 75 | |
| 76 | /* A fragment queue header, maintaining list of fragments for one N-PDU */ |
| 77 | struct frag_queue_head { |
| 78 | uint16_t npdu; |
| 79 | |
| 80 | /* linked list of frag_queue_entry: one for each fragment */ |
| 81 | struct llist_head frag_list; |
| 82 | |
| 83 | struct timer_list timer; |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 86 | struct sndcp_entity { |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 87 | struct llist_head list; |
| 88 | |
Harald Welte | ef1bef7 | 2010-06-03 06:38:38 +0200 | [diff] [blame^] | 89 | /* reference to the LLC Entity below this SNDCP entity */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 90 | struct gprs_llc_lle *lle; |
Harald Welte | ef1bef7 | 2010-06-03 06:38:38 +0200 | [diff] [blame^] | 91 | /* The NSAPI we shall use on top of LLC */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 92 | uint8_t nsapi; |
| 93 | |
Harald Welte | ef1bef7 | 2010-06-03 06:38:38 +0200 | [diff] [blame^] | 94 | /* NPDU number for the GTP->SNDCP side */ |
| 95 | uint16_t npdu_nr; |
| 96 | /* SNDCP eeceiver state */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 97 | enum sndcp_rx_state rx_state; |
Harald Welte | ef1bef7 | 2010-06-03 06:38:38 +0200 | [diff] [blame^] | 98 | /* The defragmentation queue */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 99 | struct frag_queue_head fqueue; |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 102 | LLIST_HEAD(sndcp_entities); |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 103 | |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 104 | #if 0 |
| 105 | static struct frag_queue_entry _find_fqe(struct freg_queue_head *fqh, uint8_t seg_nr) |
| 106 | { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | static struct frag_queue_head _find_fqh(struct sndcp_entity *sne, uint16_t npdu) |
| 111 | { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | static int ul_enqueue_fragment(struct sndcp_entity *sne, uint16_t npdu, |
| 116 | uint8_t seg_nr, uint32_t data_len, uint8_t *data) |
| 117 | { |
| 118 | |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | static struct sndcp_entity *sndcp_entity_by_lle(const struct gprs_llc_lle *lle, |
| 123 | uint8_t nsapi) |
| 124 | { |
| 125 | struct sndcp_entity *sne; |
| 126 | |
| 127 | llist_for_each_entry(sne, &sndcp_entities, list) { |
| 128 | if (sne->lle == lle && sne->nsapi == nsapi) |
| 129 | return sne; |
| 130 | } |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | static struct sndcp_entity *sndcp_entity_alloc(struct gprs_llc_lle *lle, |
| 135 | uint8_t nsapi) |
| 136 | { |
| 137 | struct sndcp_entity *sne; |
| 138 | |
| 139 | sne = talloc_zero(tall_sndcp_ctx, struct sndcp_entity); |
| 140 | if (!sne) |
| 141 | return NULL; |
| 142 | |
| 143 | sne->lle = lle; |
| 144 | sne->nsapi = nsapi; |
| 145 | sne->fqueue.timer.data = sne; |
| 146 | //sne->fqueue.timer.cb = FIXME; |
| 147 | sne->rx_state = SNDCP_RX_S_FIRST; |
| 148 | |
Harald Welte | d5b31a8 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 149 | llist_add(&sne->list, &sndcp_entities); |
| 150 | |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 151 | return sne; |
| 152 | } |
| 153 | |
| 154 | /* Entry point for the SNSM-ACTIVATE.indication */ |
| 155 | int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 156 | { |
Harald Welte | d5b31a8 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 157 | LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, " |
| 158 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 159 | |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 160 | if (sndcp_entity_by_lle(lle, nsapi)) { |
| 161 | LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE " |
| 162 | "already-existing entity (TLLI=%08x, NSAPI=%u)\n", |
| 163 | lle->llme->tlli, nsapi); |
| 164 | return -EEXIST; |
| 165 | } |
| 166 | |
| 167 | if (!sndcp_entity_alloc(lle, nsapi)) { |
| 168 | LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n"); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 169 | return -ENOMEM; |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 170 | } |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Harald Welte | ef1bef7 | 2010-06-03 06:38:38 +0200 | [diff] [blame^] | 175 | int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi, |
| 176 | void *mmcontext) |
| 177 | { |
| 178 | struct sndcp_entity *sne; |
| 179 | struct sndcp_common_hdr *sch; |
| 180 | struct sndcp_udata_hdr *suh; |
| 181 | |
| 182 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 183 | |
| 184 | sne = sndcp_entity_by_lle(lle, nsapi); |
| 185 | if (!sne) { |
| 186 | LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n"); |
| 187 | return -EIO; |
| 188 | } |
| 189 | |
| 190 | /* prepend the user-data header */ |
| 191 | suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh)); |
| 192 | suh->npdu_low = sne->npdu_nr & 0xff; |
| 193 | suh->npdu_high = (sne->npdu_nr >> 8) & 0xf; |
| 194 | sne->npdu_nr = (sne->npdu_nr + 1) % 0xfff; |
| 195 | |
| 196 | /* prepend common SNDCP header */ |
| 197 | sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch)); |
| 198 | sch->first = 1; |
| 199 | sch->type = 1; |
| 200 | sch->nsapi = nsapi; |
| 201 | |
| 202 | return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext); |
| 203 | } |
| 204 | |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 205 | /* Section 5.1.2.17 LL-UNITDATA.ind */ |
| 206 | int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len) |
| 207 | { |
| 208 | struct sndcp_entity *sne; |
| 209 | struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr; |
| 210 | struct sndcp_udata_hdr *suh; |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 211 | uint8_t *npdu; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 212 | uint16_t npdu_num; |
| 213 | int npdu_len; |
| 214 | |
| 215 | if (sch->type == 0) { |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 216 | LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n"); |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 220 | if (len < sizeof(*sch) + sizeof(*suh)) { |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 221 | LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 222 | return -EIO; |
| 223 | } |
| 224 | |
| 225 | sne = sndcp_entity_by_lle(lle, sch->nsapi); |
| 226 | if (!sne) { |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 227 | LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity " |
Harald Welte | d5b31a8 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 228 | "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle, |
| 229 | lle->llme->tlli, lle->sapi, sch->nsapi); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 230 | return -EIO; |
| 231 | } |
| 232 | |
| 233 | if (!sch->first || sch->more) { |
| 234 | /* FIXME: implement fragment re-assembly */ |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 235 | LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n"); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 236 | return -EIO; |
| 237 | } |
| 238 | |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 239 | if (sch->pcomp || sch->dcomp) { |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 240 | LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n"); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 241 | return -EIO; |
| 242 | } |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 243 | |
Harald Welte | f6554f7 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 244 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 245 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 246 | npdu = (uint8_t *)suh + sizeof(*suh); |
| 247 | npdu_len = (msg->data + msg->len) - npdu; |
Harald Welte | d5b31a8 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 248 | if (npdu_len <= 0) { |
Harald Welte | d83e324 | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 249 | LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len); |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 250 | return -EIO; |
| 251 | } |
| 252 | /* actually send the N-PDU to the SGSN core code, which then |
| 253 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
| 254 | return sgsn_rx_sndcp_ud_ind(lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu); |
Harald Welte | e661b27 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 257 | /* Section 5.1.2.1 LL-RESET.ind */ |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 258 | static int sndcp_ll_reset_ind(struct sndcp_entity *se) |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 259 | { |
| 260 | /* treat all outstanding SNDCP-LLC request type primitives as not sent */ |
| 261 | /* reset all SNDCP XID parameters to default values */ |
| 262 | } |
| 263 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 264 | static int sndcp_ll_status_ind() |
| 265 | { |
| 266 | /* inform the SM sub-layer by means of SNSM-STATUS.req */ |
| 267 | } |
| 268 | |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 269 | #if 0 |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 270 | static struct sndcp_state_list {{ |
| 271 | uint32_t states; |
| 272 | unsigned int type; |
| 273 | int (*rout)(struct sndcp_entity *se, struct msgb *msg); |
| 274 | } sndcp_state_list[] = { |
| 275 | { ALL_STATES, |
| 276 | LL_RESET_IND, sndcp_ll_reset_ind }, |
| 277 | { ALL_STATES, |
| 278 | LL_ESTABLISH_IND, sndcp_ll_est_ind }, |
| 279 | { SBIT(SNDCP_S_EST_RQD), |
| 280 | LL_ESTABLISH_RESP, sndcp_ll_est_ind }, |
| 281 | { SBIT(SNDCP_S_EST_RQD), |
| 282 | LL_ESTABLISH_CONF, sndcp_ll_est_conf }, |
| 283 | { SBIT(SNDCP_S_ |
| 284 | }; |
| 285 | |
| 286 | static int sndcp_rx_llc_prim() |
| 287 | { |
| 288 | case LL_ESTABLISH_REQ: |
| 289 | case LL_RELEASE_REQ: |
| 290 | case LL_XID_REQ: |
| 291 | case LL_DATA_REQ: |
| 292 | LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 293 | |
| 294 | switch (prim) { |
| 295 | case LL_RESET_IND: |
| 296 | case LL_ESTABLISH_IND: |
| 297 | case LL_ESTABLISH_RESP: |
| 298 | case LL_ESTABLISH_CONF: |
| 299 | case LL_RELEASE_IND: |
| 300 | case LL_RELEASE_CONF: |
| 301 | case LL_XID_IND: |
| 302 | case LL_XID_RESP: |
| 303 | case LL_XID_CONF: |
| 304 | case LL_DATA_IND: |
| 305 | case LL_DATA_CONF: |
| 306 | case LL_UNITDATA_IND: |
| 307 | case LL_STATUS_IND: |
| 308 | } |
Harald Welte | d0e36dc | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 309 | #endif |