Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <openbsc/bsc_nat.h> |
| 26 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 27 | #include <openbsc/bssap.h> |
| 28 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 30 | #include <osmocore/linuxlist.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 31 | #include <osmocore/talloc.h> |
| 32 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 36 | struct bsc_nat *bsc_nat_alloc(void) |
| 37 | { |
| 38 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 39 | if (!nat) |
| 40 | return NULL; |
| 41 | |
| 42 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 43 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 44 | INIT_LLIST_HEAD(&nat->bsc_configs); |
| 45 | return nat; |
| 46 | } |
| 47 | |
| 48 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 49 | { |
| 50 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 51 | if (!con) |
| 52 | return NULL; |
| 53 | |
Holger Hans Peter Freyther | 72deaea | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 54 | con->nat = nat; |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 55 | return con; |
| 56 | } |
| 57 | |
| 58 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac) |
| 59 | { |
| 60 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 61 | if (!conf) |
| 62 | return NULL; |
| 63 | |
| 64 | conf->token = talloc_strdup(conf, token); |
| 65 | conf->lac = lac; |
| 66 | conf->nr = nat->num_bsc; |
| 67 | conf->nat = nat; |
| 68 | |
| 69 | llist_add(&conf->entry, &nat->bsc_configs); |
| 70 | ++nat->num_bsc; |
| 71 | |
| 72 | return conf; |
| 73 | } |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 74 | |
| 75 | struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg) |
| 76 | { |
| 77 | struct bsc_connection *bsc; |
| 78 | int data_length; |
| 79 | const u_int8_t *data; |
| 80 | struct tlv_parsed tp; |
| 81 | int i = 0; |
| 82 | |
| 83 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 84 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 85 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 90 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 91 | if (data[0] != CELL_IDENT_LAC) { |
| 92 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %c\n", data[0]); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /* Currently we only handle one BSC */ |
| 97 | for (i = 1; i < data_length - 1; i += 2) { |
| 98 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
| 99 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
| 100 | if (!bsc->authenticated || _lac != bsc->lac) |
| 101 | continue; |
| 102 | |
| 103 | return bsc; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return NULL; |
| 108 | } |