Harald Welte | 96f71f2 | 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> |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #include <osmocore/msgb.h> |
| 28 | #include <osmocore/linuxlist.h> |
| 29 | #include <osmocore/timer.h> |
| 30 | #include <osmocore/talloc.h> |
| 31 | |
| 32 | #include <openbsc/gsm_data.h> |
| 33 | #include <openbsc/debug.h> |
| 34 | #include <openbsc/gprs_bssgp.h> |
| 35 | #include <openbsc/gprs_llc.h> |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 36 | #include <openbsc/sgsn.h> |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 37 | |
| 38 | /* Chapter 7.2: SN-PDU Formats */ |
| 39 | struct sndcp_common_hdr { |
| 40 | /* octet 1 */ |
| 41 | uint8_t nsapi:4; |
| 42 | uint8_t more:1; |
| 43 | uint8_t type:1; |
| 44 | uint8_t first:1; |
| 45 | uint8_t spare:1; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 46 | } __attribute__((packed)); |
| 47 | |
| 48 | /* PCOMP / DCOMP only exist in first fragment */ |
| 49 | struct sndcp_comp_hdr { |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 50 | /* octet 2 */ |
Harald Welte | 5cc2bc3 | 2010-06-02 23:17:05 +0200 | [diff] [blame] | 51 | uint8_t pcomp:4; |
| 52 | uint8_t dcomp:4; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 53 | } __attribute__((packed)); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 54 | |
| 55 | struct sndcp_udata_hdr { |
| 56 | /* octet 3 */ |
| 57 | uint8_t npdu_high:4; |
| 58 | uint8_t seg_nr:4; |
| 59 | /* octet 4 */ |
| 60 | uint8_t npdu_low; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 61 | } __attribute__((packed)); |
| 62 | |
| 63 | /* See 6.7.1.2 Reassembly */ |
| 64 | enum sndcp_rx_state { |
| 65 | SNDCP_RX_S_FIRST, |
| 66 | SNDCP_RX_S_SUBSEQ, |
| 67 | SNDCP_RX_S_DISCARD, |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | static void *tall_sndcp_ctx; |
| 72 | |
| 73 | /* A fragment queue entry, containing one framgent of a N-PDU */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 74 | struct defrag_queue_entry { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 75 | struct llist_head list; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 76 | /* segment number of this fragment */ |
| 77 | uint32_t seg_nr; |
| 78 | /* length of the data area of this fragment */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 79 | uint32_t data_len; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 80 | /* pointer to the data of this fragment */ |
| 81 | uint8_t *data; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* A fragment queue header, maintaining list of fragments for one N-PDU */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 85 | struct defrag_state { |
| 86 | /* PDU number for which the defragmentation state applies */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 87 | uint16_t npdu; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 88 | /* highest segment number we have received so far */ |
| 89 | uint8_t highest_seg; |
| 90 | /* bitmask of the segments we already have */ |
| 91 | uint32_t seg_have; |
| 92 | /* do we still expect more segments? */ |
| 93 | unsigned int no_more; |
| 94 | /* total length of all segments together */ |
| 95 | unsigned int tot_len; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 96 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 97 | /* linked list of defrag_queue_entry: one for each fragment */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 98 | struct llist_head frag_list; |
| 99 | |
| 100 | struct timer_list timer; |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 103 | struct sndcp_entity { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 104 | struct llist_head list; |
| 105 | |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 106 | /* reference to the LLC Entity below this SNDCP entity */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 107 | struct gprs_llc_lle *lle; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 108 | /* The NSAPI we shall use on top of LLC */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 109 | uint8_t nsapi; |
| 110 | |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 111 | /* NPDU number for the GTP->SNDCP side */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 112 | uint16_t tx_npdu_nr; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 113 | /* SNDCP eeceiver state */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 114 | enum sndcp_rx_state rx_state; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 115 | /* The defragmentation queue */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 116 | struct defrag_state defrag; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 119 | LLIST_HEAD(sndcp_entities); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 120 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 121 | /* Enqueue a fragment into the defragment queue */ |
| 122 | static int defrag_enqueue(struct sndcp_entity *sne, uint8_t seg_nr, |
| 123 | uint32_t data_len, uint8_t *data) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 124 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 125 | struct defrag_queue_entry *dqe; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 126 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 127 | dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry); |
| 128 | if (!dqe) |
| 129 | return -ENOMEM; |
| 130 | dqe->data = talloc_zero_size(dqe, data_len); |
| 131 | if (!dqe->data) { |
| 132 | talloc_free(dqe); |
| 133 | return -ENOMEM; |
| 134 | } |
| 135 | dqe->seg_nr = seg_nr; |
| 136 | dqe->data_len = data_len; |
| 137 | |
| 138 | llist_add(&dqe->list, &sne->defrag.frag_list); |
| 139 | |
| 140 | if (seg_nr > sne->defrag.highest_seg) |
| 141 | sne->defrag.highest_seg = seg_nr; |
| 142 | |
| 143 | sne->defrag.seg_have |= (1 << seg_nr); |
| 144 | sne->defrag.tot_len += data_len; |
| 145 | |
| 146 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 149 | /* return if we have all segments of this N-PDU */ |
| 150 | static int defrag_have_all_segments(struct sndcp_entity *sne) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 151 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 152 | uint32_t seg_needed = 0; |
| 153 | unsigned int i; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 154 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 155 | /* create a bitmask of needed segments */ |
| 156 | for (i = 0; i < sne->defrag.highest_seg; i++) |
| 157 | seg_needed |= (1 << i); |
| 158 | |
| 159 | if (seg_needed == sne->defrag.seg_have) |
| 160 | return 1; |
| 161 | |
| 162 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 165 | static struct defrag_queue_entry *defrag_get_seg(struct sndcp_entity *sne, |
| 166 | uint32_t seg_nr) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 167 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 168 | struct defrag_queue_entry *dqe; |
| 169 | |
| 170 | llist_for_each_entry(dqe, &sne->defrag.frag_list, list) { |
| 171 | if (dqe->seg_nr == seg_nr) { |
| 172 | llist_del(&dqe->list); |
| 173 | return dqe; |
| 174 | } |
| 175 | } |
| 176 | return NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 177 | } |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 178 | |
| 179 | static int defrag_segments(struct sndcp_entity *sne) |
| 180 | { |
| 181 | struct msgb *msg; |
| 182 | unsigned int seg_nr; |
| 183 | uint8_t *npdu; |
| 184 | |
Sylvain Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 185 | msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag"); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 186 | if (!msg) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | /* FIXME: message headers + identifiers */ |
| 190 | |
| 191 | npdu = msg->data; |
| 192 | |
| 193 | for (seg_nr = 0; seg_nr < sne->defrag.highest_seg; seg_nr++) { |
| 194 | struct defrag_queue_entry *dqe; |
| 195 | uint8_t *data; |
| 196 | |
| 197 | dqe = defrag_get_seg(sne, seg_nr); |
| 198 | if (!dqe) { |
| 199 | LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr); |
| 200 | talloc_free(msg); |
| 201 | return -EIO; |
| 202 | } |
| 203 | /* actually append the segment to the N-PDU */ |
| 204 | data = msgb_put(msg, dqe->data_len); |
| 205 | memcpy(data, dqe->data, dqe->data_len); |
| 206 | |
| 207 | /* release memory for the fragment queue entry */ |
| 208 | talloc_free(dqe); |
| 209 | } |
| 210 | |
| 211 | /* actually send the N-PDU to the SGSN core code, which then |
| 212 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
| 213 | return sgsn_rx_sndcp_ud_ind(sne->lle->llme->tlli, sne->nsapi, msg, |
| 214 | sne->defrag.tot_len, npdu); |
| 215 | } |
| 216 | |
| 217 | static int defrag_input(struct sndcp_entity *sne, struct msgb *msg, uint8_t *hdr) |
| 218 | { |
| 219 | struct sndcp_common_hdr *sch; |
| 220 | struct sndcp_comp_hdr *scomph = NULL; |
| 221 | struct sndcp_udata_hdr *suh; |
| 222 | uint16_t npdu_num; |
| 223 | uint8_t *data; |
| 224 | int rc; |
| 225 | |
| 226 | sch = (struct sndcp_common_hdr *) hdr; |
| 227 | if (sch->first) { |
| 228 | scomph = (struct sndcp_comp_hdr *) (hdr + 1); |
| 229 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 230 | } else |
| 231 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 232 | |
| 233 | data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr); |
| 234 | |
| 235 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
| 236 | |
| 237 | if (sch->first) { |
| 238 | /* first segment of a new packet. Discard all leftover fragments of |
| 239 | * previous packet */ |
| 240 | if (!llist_empty(&sne->defrag.frag_list)) { |
| 241 | struct defrag_queue_entry *dqe; |
| 242 | LOGP(DSNDCP, LOGL_INFO, "Dropping SN-PDU due to " |
| 243 | "insufficient segments\n"); |
| 244 | llist_for_each_entry(dqe, &sne->defrag.frag_list, list) { |
| 245 | llist_del(&dqe->list); |
| 246 | talloc_free(dqe); |
| 247 | } |
| 248 | } |
| 249 | /* store the currently de-fragmented PDU number */ |
| 250 | sne->defrag.npdu = npdu_num; |
| 251 | sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0; |
| 252 | /* FIXME: Start timer */ |
| 253 | } |
| 254 | |
| 255 | if (sne->defrag.npdu != npdu_num) { |
| 256 | LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU " |
| 257 | "(%u != %u)\n", npdu_num, sne->defrag.npdu); |
| 258 | /* FIXME */ |
| 259 | } |
| 260 | |
| 261 | /* FIXME: check if seg_nr already exists */ |
| 262 | rc = defrag_enqueue(sne, suh->seg_nr, (msg->data + msg->len) - data, data); |
| 263 | if (rc < 0) |
| 264 | return rc; |
| 265 | |
| 266 | if (!sch->more) { |
| 267 | /* this is suppsed to be the last segment of the N-PDU, but it |
| 268 | * might well be not the last to arrive */ |
| 269 | sne->defrag.no_more = 1; |
| 270 | } |
| 271 | |
| 272 | if (sne->defrag.no_more) { |
| 273 | /* we have already received the last segment before, let's check |
| 274 | * if all the previous segments exist */ |
| 275 | if (defrag_have_all_segments(sne)) |
| 276 | return defrag_segments(sne); |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 281 | |
| 282 | static struct sndcp_entity *sndcp_entity_by_lle(const struct gprs_llc_lle *lle, |
| 283 | uint8_t nsapi) |
| 284 | { |
| 285 | struct sndcp_entity *sne; |
| 286 | |
| 287 | llist_for_each_entry(sne, &sndcp_entities, list) { |
| 288 | if (sne->lle == lle && sne->nsapi == nsapi) |
| 289 | return sne; |
| 290 | } |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | static struct sndcp_entity *sndcp_entity_alloc(struct gprs_llc_lle *lle, |
| 295 | uint8_t nsapi) |
| 296 | { |
| 297 | struct sndcp_entity *sne; |
| 298 | |
| 299 | sne = talloc_zero(tall_sndcp_ctx, struct sndcp_entity); |
| 300 | if (!sne) |
| 301 | return NULL; |
| 302 | |
| 303 | sne->lle = lle; |
| 304 | sne->nsapi = nsapi; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 305 | sne->defrag.timer.data = sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 306 | //sne->fqueue.timer.cb = FIXME; |
| 307 | sne->rx_state = SNDCP_RX_S_FIRST; |
| 308 | |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 309 | llist_add(&sne->list, &sndcp_entities); |
| 310 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 311 | return sne; |
| 312 | } |
| 313 | |
| 314 | /* Entry point for the SNSM-ACTIVATE.indication */ |
| 315 | int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 316 | { |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 317 | LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, " |
| 318 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 319 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 320 | if (sndcp_entity_by_lle(lle, nsapi)) { |
| 321 | LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE " |
| 322 | "already-existing entity (TLLI=%08x, NSAPI=%u)\n", |
| 323 | lle->llme->tlli, nsapi); |
| 324 | return -EEXIST; |
| 325 | } |
| 326 | |
| 327 | if (!sndcp_entity_alloc(lle, nsapi)) { |
| 328 | LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 329 | return -ENOMEM; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 330 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 335 | /* Entry point for the SNSM-DEACTIVATE.indication */ |
| 336 | int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 337 | { |
| 338 | struct sndcp_entity *sne; |
| 339 | |
| 340 | LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, " |
| 341 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
| 342 | |
| 343 | sne = sndcp_entity_by_lle(lle, nsapi); |
| 344 | if (!sne) { |
| 345 | LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-" |
| 346 | "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli, |
| 347 | lle->sapi, nsapi); |
| 348 | return -ENOENT; |
| 349 | } |
| 350 | llist_del(&sne->list); |
| 351 | /* frag queue entries are hierarchically allocated, so no need to |
| 352 | * free them explicitly here */ |
| 353 | talloc_free(sne); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* Fragmenter state */ |
| 359 | struct sndcp_frag_state { |
| 360 | uint8_t frag_nr; |
| 361 | struct msgb *msg; /* original message */ |
| 362 | uint8_t *next_byte; /* first byte of next fragment */ |
| 363 | |
| 364 | struct sndcp_entity *sne; |
| 365 | void *mmcontext; |
| 366 | }; |
| 367 | |
| 368 | /* returns '1' if there are more fragments to send, '0' if none */ |
| 369 | static int sndcp_send_ud_frag(struct sndcp_frag_state *fs) |
| 370 | { |
| 371 | struct sndcp_entity *sne = fs->sne; |
| 372 | struct gprs_llc_lle *lle = sne->lle; |
| 373 | struct sndcp_common_hdr *sch; |
| 374 | struct sndcp_comp_hdr *scomph; |
| 375 | struct sndcp_udata_hdr *suh; |
| 376 | struct msgb *fmsg; |
| 377 | unsigned int max_payload_len; |
| 378 | unsigned int len; |
| 379 | uint8_t *data; |
| 380 | int rc, more; |
| 381 | |
Sylvain Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 382 | fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128, |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 383 | "SNDCP Frag"); |
| 384 | if (!fmsg) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | /* make sure lower layers route the fragment like the original */ |
| 388 | msgb_tlli(fmsg) = msgb_tlli(fs->msg); |
| 389 | msgb_bvci(fmsg) = msgb_bvci(fs->msg); |
| 390 | msgb_nsei(fmsg) = msgb_nsei(fs->msg); |
| 391 | |
| 392 | /* prepend common SNDCP header */ |
| 393 | sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch)); |
| 394 | sch->nsapi = sne->nsapi; |
| 395 | /* Set FIRST bit if we are the first fragment in a series */ |
| 396 | if (fs->frag_nr == 0) |
| 397 | sch->first = 1; |
| 398 | sch->type = 1; |
| 399 | |
| 400 | /* append the compression header for first fragment */ |
| 401 | if (sch->first) { |
| 402 | scomph = (struct sndcp_comp_hdr *) |
| 403 | msgb_put(fmsg, sizeof(*scomph)); |
| 404 | scomph->pcomp = 0; |
| 405 | scomph->dcomp = 0; |
| 406 | } |
| 407 | |
| 408 | /* append the user-data header */ |
| 409 | suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh)); |
| 410 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 411 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 412 | suh->seg_nr = fs->frag_nr % 0xf; |
| 413 | |
| 414 | /* calculate remaining length to be sent */ |
| 415 | len = (fs->msg->data + fs->msg->len) - fs->next_byte; |
| 416 | /* how much payload can we actually send via LLC? */ |
| 417 | max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh)); |
| 418 | if (sch->first) |
| 419 | max_payload_len -= sizeof(*scomph); |
| 420 | /* check if we're exceeding the max */ |
| 421 | if (len > max_payload_len) |
| 422 | len = max_payload_len; |
| 423 | |
| 424 | /* copy the actual fragment data into our fmsg */ |
| 425 | data = msgb_put(fmsg, len); |
| 426 | memcpy(data, fs->next_byte, len); |
| 427 | |
| 428 | /* Increment fragment number and data pointer to next fragment */ |
| 429 | fs->frag_nr++; |
| 430 | fs->next_byte += len; |
| 431 | |
| 432 | /* determine if we have more fragemnts to send */ |
| 433 | if ((fs->msg->data + fs->msg->len) <= fs->next_byte) |
| 434 | more = 0; |
| 435 | else |
| 436 | more = 1; |
| 437 | |
| 438 | /* set the MORE bit of the SNDCP header accordingly */ |
| 439 | sch->more = more; |
| 440 | |
| 441 | rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext); |
| 442 | if (rc < 0) { |
| 443 | /* abort in case of error, do not advance frag_nr / next_byte */ |
| 444 | msgb_free(fmsg); |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | if (!more) { |
| 449 | /* we've sent all fragments */ |
| 450 | msgb_free(fs->msg); |
| 451 | memset(fs, 0, sizeof(*fs)); |
| 452 | /* increment NPDU number for next frame */ |
| 453 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* default: more fragments to send */ |
| 458 | return 1; |
| 459 | } |
| 460 | |
Harald Welte | db2c39f | 2010-06-03 07:14:59 +0200 | [diff] [blame] | 461 | /* Request transmission of a SN-PDU over specified LLC Entity + SAPI */ |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 462 | int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi, |
| 463 | void *mmcontext) |
| 464 | { |
| 465 | struct sndcp_entity *sne; |
| 466 | struct sndcp_common_hdr *sch; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 467 | struct sndcp_comp_hdr *scomph; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 468 | struct sndcp_udata_hdr *suh; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 469 | struct sndcp_frag_state fs; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 470 | |
| 471 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 472 | |
| 473 | sne = sndcp_entity_by_lle(lle, nsapi); |
| 474 | if (!sne) { |
| 475 | LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n"); |
| 476 | return -EIO; |
| 477 | } |
| 478 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 479 | /* Check if we need to fragment this N-PDU into multiple SN-PDUs */ |
| 480 | if (msg->len > lle->params.n201_u - |
| 481 | (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) { |
| 482 | /* initialize the fragmenter state */ |
| 483 | fs.msg = msg; |
| 484 | fs.frag_nr = 0; |
| 485 | fs.next_byte = msg->data; |
| 486 | fs.sne = sne; |
| 487 | fs.mmcontext = mmcontext; |
| 488 | |
| 489 | /* call function to generate and send fragments until all |
| 490 | * of the N-PDU has been sent */ |
| 491 | while (1) { |
| 492 | int rc = sndcp_send_ud_frag(&fs); |
| 493 | if (rc == 0) |
| 494 | return 0; |
| 495 | if (rc < 0) |
| 496 | return rc; |
| 497 | } |
| 498 | /* not reached */ |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* this is the non-fragmenting case where we only build 1 SN-PDU */ |
| 503 | |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 504 | /* prepend the user-data header */ |
| 505 | suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh)); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 506 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 507 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 508 | suh->seg_nr = 0; |
| 509 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 510 | |
| 511 | scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph)); |
| 512 | scomph->pcomp = 0; |
| 513 | scomph->dcomp = 0; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 514 | |
| 515 | /* prepend common SNDCP header */ |
| 516 | sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch)); |
| 517 | sch->first = 1; |
| 518 | sch->type = 1; |
| 519 | sch->nsapi = nsapi; |
| 520 | |
| 521 | return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext); |
| 522 | } |
| 523 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 524 | /* Section 5.1.2.17 LL-UNITDATA.ind */ |
| 525 | int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len) |
| 526 | { |
| 527 | struct sndcp_entity *sne; |
| 528 | struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 529 | struct sndcp_comp_hdr *scomph = NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 530 | struct sndcp_udata_hdr *suh; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 531 | uint8_t *npdu; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 532 | uint16_t npdu_num; |
| 533 | int npdu_len; |
| 534 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 535 | sch = (struct sndcp_common_hdr *) hdr; |
| 536 | if (sch->first) { |
| 537 | scomph = (struct sndcp_comp_hdr *) (hdr + 1); |
| 538 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 539 | } else |
| 540 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 541 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 542 | if (sch->type == 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 543 | LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n"); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 544 | return -EINVAL; |
| 545 | } |
| 546 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 547 | if (len < sizeof(*sch) + sizeof(*suh)) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 548 | LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 549 | return -EIO; |
| 550 | } |
| 551 | |
| 552 | sne = sndcp_entity_by_lle(lle, sch->nsapi); |
| 553 | if (!sne) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 554 | LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity " |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 555 | "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle, |
| 556 | lle->llme->tlli, lle->sapi, sch->nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 557 | return -EIO; |
| 558 | } |
| 559 | |
| 560 | if (!sch->first || sch->more) { |
| 561 | /* FIXME: implement fragment re-assembly */ |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 562 | LOGP(DSNDCP, LOGL_ERROR, "We don't support reassembly yet\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 563 | return -EIO; |
| 564 | } |
| 565 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 566 | if (scomph && (scomph->pcomp || scomph->dcomp)) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 567 | LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 568 | return -EIO; |
| 569 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 570 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 571 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 572 | npdu = (uint8_t *)suh + sizeof(*suh); |
| 573 | npdu_len = (msg->data + msg->len) - npdu; |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 574 | if (npdu_len <= 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 575 | LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 576 | return -EIO; |
| 577 | } |
| 578 | /* actually send the N-PDU to the SGSN core code, which then |
| 579 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
| 580 | return sgsn_rx_sndcp_ud_ind(lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 583 | /* Section 5.1.2.1 LL-RESET.ind */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 584 | static int sndcp_ll_reset_ind(struct sndcp_entity *se) |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 585 | { |
| 586 | /* treat all outstanding SNDCP-LLC request type primitives as not sent */ |
| 587 | /* reset all SNDCP XID parameters to default values */ |
| 588 | } |
| 589 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 590 | static int sndcp_ll_status_ind() |
| 591 | { |
| 592 | /* inform the SM sub-layer by means of SNSM-STATUS.req */ |
| 593 | } |
| 594 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 595 | #if 0 |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 596 | static struct sndcp_state_list {{ |
| 597 | uint32_t states; |
| 598 | unsigned int type; |
| 599 | int (*rout)(struct sndcp_entity *se, struct msgb *msg); |
| 600 | } sndcp_state_list[] = { |
| 601 | { ALL_STATES, |
| 602 | LL_RESET_IND, sndcp_ll_reset_ind }, |
| 603 | { ALL_STATES, |
| 604 | LL_ESTABLISH_IND, sndcp_ll_est_ind }, |
| 605 | { SBIT(SNDCP_S_EST_RQD), |
| 606 | LL_ESTABLISH_RESP, sndcp_ll_est_ind }, |
| 607 | { SBIT(SNDCP_S_EST_RQD), |
| 608 | LL_ESTABLISH_CONF, sndcp_ll_est_conf }, |
| 609 | { SBIT(SNDCP_S_ |
| 610 | }; |
| 611 | |
| 612 | static int sndcp_rx_llc_prim() |
| 613 | { |
| 614 | case LL_ESTABLISH_REQ: |
| 615 | case LL_RELEASE_REQ: |
| 616 | case LL_XID_REQ: |
| 617 | case LL_DATA_REQ: |
| 618 | LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 619 | |
| 620 | switch (prim) { |
| 621 | case LL_RESET_IND: |
| 622 | case LL_ESTABLISH_IND: |
| 623 | case LL_ESTABLISH_RESP: |
| 624 | case LL_ESTABLISH_CONF: |
| 625 | case LL_RELEASE_IND: |
| 626 | case LL_RELEASE_CONF: |
| 627 | case LL_XID_IND: |
| 628 | case LL_XID_RESP: |
| 629 | case LL_XID_CONF: |
| 630 | case LL_DATA_IND: |
| 631 | case LL_DATA_CONF: |
| 632 | case LL_UNITDATA_IND: |
| 633 | case LL_STATUS_IND: |
| 634 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 635 | #endif |