Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1 | /* SIP extension for IP connection tracking. |
| 2 | * |
Ravinder Konka | 4eafdb9 | 2016-01-18 19:35:52 +0530 | [diff] [blame] | 3 | * Copyright (c) 2015,2017-2019 The Linux Foundation. All rights reserved. |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 4 | * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar> |
| 5 | * based on RR's ip_conntrack_ftp.c and other modules. |
Patrick McHardy | f49e1aa | 2008-03-25 20:27:05 -0700 | [diff] [blame] | 6 | * (C) 2007 United Security Providers |
| 7 | * (C) 2007, 2008 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 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 version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Pablo Neira Ayuso | ad6d950 | 2016-01-03 22:41:24 +0100 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/skbuff.h> |
| 19 | #include <linux/inet.h> |
| 20 | #include <linux/in.h> |
| 21 | #include <linux/udp.h> |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 22 | #include <linux/tcp.h> |
Yasuyuki Kozakai | 1863f09 | 2006-12-02 22:12:54 -0800 | [diff] [blame] | 23 | #include <linux/netfilter.h> |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 24 | #include <net/tcp.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack.h> |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_core.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 27 | #include <net/netfilter/nf_conntrack_expect.h> |
| 28 | #include <net/netfilter/nf_conntrack_helper.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_zones.h> |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 30 | #include <linux/netfilter/nf_conntrack_sip.h> |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 31 | #include <net/netfilter/nf_nat.h> |
| 32 | #include <net/netfilter/nf_nat_l3proto.h> |
| 33 | #include <net/netfilter/nf_nat_l4proto.h> |
| 34 | #include <net/netfilter/nf_queue.h> |
| 35 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 36 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 37 | MODULE_LICENSE("GPL"); |
| 38 | MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>"); |
| 39 | MODULE_DESCRIPTION("SIP connection tracking helper"); |
| 40 | MODULE_ALIAS("ip_conntrack_sip"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 41 | MODULE_ALIAS_NFCT_HELPER("sip"); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 42 | |
| 43 | #define MAX_PORTS 8 |
| 44 | static unsigned short ports[MAX_PORTS]; |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 45 | static unsigned int ports_c; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 46 | module_param_array(ports, ushort, &ports_c, 0400); |
| 47 | MODULE_PARM_DESC(ports, "port numbers of SIP servers"); |
| 48 | |
| 49 | static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT; |
| 50 | module_param(sip_timeout, uint, 0600); |
| 51 | MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session"); |
| 52 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 53 | static int sip_direct_signalling __read_mostly = 1; |
| 54 | module_param(sip_direct_signalling, int, 0600); |
| 55 | MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar " |
| 56 | "only (default 1)"); |
| 57 | |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 58 | const struct nf_nat_sip_hooks *nf_nat_sip_hooks; |
| 59 | EXPORT_SYMBOL_GPL(nf_nat_sip_hooks); |
Tyler Wear | d2aeec0 | 2013-09-20 12:12:07 -0700 | [diff] [blame] | 60 | static struct ctl_table_header *sip_sysctl_header; |
| 61 | static unsigned int nf_ct_disable_sip_alg; |
Tyler Wear | 6cd63b0 | 2014-01-13 12:33:17 -0800 | [diff] [blame] | 62 | static int sip_direct_media = 1; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 63 | static unsigned int nf_ct_enable_sip_segmentation; |
| 64 | static int packet_count; |
| 65 | static |
| 66 | int proc_sip_segment(struct ctl_table *ctl, int write, |
| 67 | void __user *buffer, size_t *lenp, loff_t *ppos); |
| 68 | |
Tyler Wear | d2aeec0 | 2013-09-20 12:12:07 -0700 | [diff] [blame] | 69 | static struct ctl_table sip_sysctl_tbl[] = { |
| 70 | { |
| 71 | .procname = "nf_conntrack_disable_sip_alg", |
| 72 | .data = &nf_ct_disable_sip_alg, |
| 73 | .maxlen = sizeof(unsigned int), |
| 74 | .mode = 0644, |
| 75 | .proc_handler = proc_dointvec, |
| 76 | }, |
Tyler Wear | 6cd63b0 | 2014-01-13 12:33:17 -0800 | [diff] [blame] | 77 | { |
| 78 | .procname = "nf_conntrack_sip_direct_media", |
| 79 | .data = &sip_direct_media, |
| 80 | .maxlen = sizeof(int), |
| 81 | .mode = 0644, |
| 82 | .proc_handler = proc_dointvec, |
| 83 | }, |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 84 | { |
| 85 | .procname = "nf_conntrack_enable_sip_segmentation", |
| 86 | .data = &nf_ct_enable_sip_segmentation, |
| 87 | .maxlen = sizeof(unsigned int), |
| 88 | .mode = 0644, |
| 89 | .proc_handler = proc_sip_segment, |
| 90 | }, |
Tyler Wear | d2aeec0 | 2013-09-20 12:12:07 -0700 | [diff] [blame] | 91 | {} |
| 92 | }; |
| 93 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 94 | static unsigned int (*nf_nat_sip_hook) |
| 95 | (struct sk_buff *skb, |
| 96 | unsigned int protoff, |
| 97 | unsigned int dataoff, |
| 98 | const char **dptr, |
| 99 | unsigned int *datalen) |
| 100 | __read_mostly; |
| 101 | EXPORT_SYMBOL(nf_nat_sip_hook); |
| 102 | static void sip_calculate_parameters(s16 *diff, s16 *tdiff, |
| 103 | unsigned int *dataoff, const char **dptr, |
| 104 | unsigned int *datalen, |
| 105 | unsigned int msglen, unsigned int origlen) |
| 106 | { |
| 107 | *diff = msglen - origlen; |
| 108 | *tdiff += *diff; |
| 109 | *dataoff += msglen; |
| 110 | *dptr += msglen; |
| 111 | *datalen = *datalen + *diff - msglen; |
| 112 | } |
| 113 | |
| 114 | static void sip_update_params(enum ip_conntrack_dir dir, |
| 115 | unsigned int *msglen, unsigned int *origlen, |
| 116 | const char **dptr, unsigned int *datalen, |
| 117 | bool skb_is_combined, struct nf_conn *ct) |
| 118 | { |
| 119 | if (skb_is_combined) { |
| 120 | /* The msglen of first skb has the total msg length of |
| 121 | * the two fragments. hence after combining,we update |
| 122 | * the msglen to that of the msglen of first skb |
| 123 | */ |
| 124 | *msglen = (dir == IP_CT_DIR_ORIGINAL) ? |
| 125 | ct->segment.msg_length[0] : ct->segment.msg_length[1]; |
| 126 | *origlen = *msglen; |
| 127 | *dptr = ct->dptr_prev; |
| 128 | *datalen = *msglen; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* This function is to save all the information of the first segment |
| 133 | * that will be needed for combining the two segments |
| 134 | */ |
| 135 | static bool sip_save_segment_info(struct nf_conn *ct, struct sk_buff *skb, |
| 136 | unsigned int msglen, unsigned int datalen, |
| 137 | const char *dptr, |
| 138 | enum ip_conntrack_info ctinfo) |
| 139 | { |
| 140 | enum ip_conntrack_dir dir = IP_CT_DIR_MAX; |
| 141 | bool skip = false; |
| 142 | |
| 143 | /* one set of information is saved per direction ,also only one segment |
| 144 | * per direction is queued based on the assumption that after the first |
| 145 | * complete message leaves the kernel, only then the next fragmented |
| 146 | * segment will reach the kernel |
| 147 | */ |
| 148 | dir = CTINFO2DIR(ctinfo); |
| 149 | if (dir == IP_CT_DIR_ORIGINAL) { |
| 150 | /* here we check if there is already an element queued for this |
| 151 | * direction, in that case we do not queue the next element,we |
| 152 | * make skip 1.ideally this scenario should never be hit |
| 153 | */ |
| 154 | if (ct->sip_original_dir == 1) { |
| 155 | skip = true; |
| 156 | } else { |
| 157 | ct->segment.msg_length[0] = msglen; |
| 158 | ct->segment.data_len[0] = datalen; |
| 159 | ct->segment.skb_len[0] = skb->len; |
| 160 | ct->dptr_prev = dptr; |
| 161 | ct->sip_original_dir = 1; |
| 162 | skip = false; |
| 163 | } |
| 164 | } else { |
| 165 | if (ct->sip_reply_dir == 1) { |
| 166 | skip = true; |
| 167 | } else { |
| 168 | if (ct->sip_reply_dir == 1) { |
| 169 | skip = true; |
| 170 | } else { |
| 171 | ct->segment.msg_length[1] = msglen; |
| 172 | ct->segment.data_len[1] = datalen; |
| 173 | ct->segment.skb_len[1] = skb->len; |
| 174 | ct->dptr_prev = dptr; |
| 175 | ct->sip_reply_dir = 1; |
| 176 | skip = false; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return skip; |
| 181 | } |
| 182 | |
| 183 | static struct sip_list *sip_coalesce_segments(struct nf_conn *ct, |
| 184 | struct sk_buff **skb_ref, |
| 185 | unsigned int dataoff, |
| 186 | struct sk_buff **combined_skb_ref, |
| 187 | bool *skip_sip_process, |
| 188 | bool do_not_process, |
| 189 | enum ip_conntrack_info ctinfo, |
| 190 | bool *success) |
| 191 | |
| 192 | { |
| 193 | struct list_head *list_trav_node; |
| 194 | struct list_head *list_backup_node; |
| 195 | struct nf_conn *ct_list; |
| 196 | enum ip_conntrack_info ctinfo_list; |
| 197 | enum ip_conntrack_dir dir_list; |
| 198 | enum ip_conntrack_dir dir = IP_CT_DIR_MAX; |
| 199 | const struct tcphdr *th_old; |
| 200 | unsigned int prev_data_len; |
| 201 | unsigned int seq_no, seq_old, exp_seq_no; |
| 202 | const struct tcphdr *th_new; |
| 203 | bool fragstolen = false; |
| 204 | int delta_truesize = 0; |
| 205 | struct sip_list *sip_entry = NULL; |
| 206 | |
| 207 | th_new = (struct tcphdr *)(skb_network_header(*skb_ref) + |
| 208 | ip_hdrlen(*skb_ref)); |
| 209 | seq_no = ntohl(th_new->seq); |
| 210 | |
| 211 | if (ct) { |
| 212 | dir = CTINFO2DIR(ctinfo); |
| 213 | /* traverse the list it would have 1 or 2 elements. 1 element |
| 214 | * per direction at max |
| 215 | */ |
| 216 | list_for_each_safe(list_trav_node, list_backup_node, |
| 217 | &ct->sip_segment_list){ |
| 218 | sip_entry = list_entry(list_trav_node, struct sip_list, |
| 219 | list); |
| 220 | ct_list = nf_ct_get(sip_entry->entry->skb, |
| 221 | &ctinfo_list); |
| 222 | dir_list = CTINFO2DIR(ctinfo_list); |
| 223 | /* take an element and check if its direction matches |
| 224 | * with the current one |
| 225 | */ |
| 226 | if (dir_list == dir) { |
| 227 | /* once we have the two elements to be combined |
| 228 | * we do another check. match the next expected |
| 229 | * seq no of the packet in the list with the |
| 230 | * seq no of the current packet.this is to be |
| 231 | * protected against out of order fragments |
| 232 | */ |
| 233 | th_old = ((struct tcphdr *)(skb_network_header |
| 234 | (sip_entry->entry->skb) + |
| 235 | ip_hdrlen(sip_entry->entry->skb))); |
| 236 | |
| 237 | prev_data_len = (dir == IP_CT_DIR_ORIGINAL) ? |
| 238 | ct->segment.data_len[0] : |
| 239 | ct->segment.data_len[1]; |
| 240 | seq_old = (ntohl(th_old->seq)); |
| 241 | exp_seq_no = seq_old + prev_data_len; |
| 242 | |
| 243 | if (exp_seq_no == seq_no) { |
| 244 | /* Found packets to be combined.Pull |
| 245 | * header from second skb when |
| 246 | * preparing combined skb.This shifts |
| 247 | * the second skb start pointer to its |
| 248 | * data that was initially at the start |
| 249 | * of its headers.This so that the |
| 250 | * combined skb has the tcp ip headerof |
| 251 | * the first skb followed by the data |
| 252 | * of first skb followed by the data |
| 253 | * of second skb. |
| 254 | */ |
| 255 | skb_pull(*skb_ref, dataoff); |
| 256 | if (skb_try_coalesce( |
| 257 | sip_entry->entry->skb, |
| 258 | *skb_ref, &fragstolen, |
| 259 | &delta_truesize)) { |
| 260 | pr_debug(" Combining segments\n"); |
| 261 | *combined_skb_ref = |
| 262 | sip_entry->entry->skb; |
| 263 | *success = true; |
| 264 | list_del(list_trav_node); |
| 265 | } else{ |
| 266 | skb_push(*skb_ref, dataoff); |
| 267 | } |
| 268 | } |
| 269 | } else if (do_not_process) { |
| 270 | *skip_sip_process = true; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | return sip_entry; |
| 275 | } |
| 276 | |
| 277 | static void recalc_header(struct sk_buff *skb, unsigned int skblen, |
| 278 | unsigned int oldlen, unsigned int protoff) |
| 279 | { |
| 280 | unsigned int datalen; |
| 281 | struct tcphdr *tcph; |
| 282 | const struct nf_nat_l3proto *l3proto; |
| 283 | |
| 284 | /* here we recalculate ip and tcp headers */ |
| 285 | if (nf_ct_l3num((struct nf_conn *)skb->nfct) == NFPROTO_IPV4) { |
| 286 | /* fix IP hdr checksum information */ |
| 287 | ip_hdr(skb)->tot_len = htons(skblen); |
| 288 | ip_send_check(ip_hdr(skb)); |
| 289 | } else { |
| 290 | ipv6_hdr(skb)->payload_len = |
| 291 | htons(skblen - sizeof(struct ipv6hdr)); |
| 292 | } |
| 293 | datalen = skb->len - protoff; |
| 294 | tcph = (struct tcphdr *)((void *)skb->data + protoff); |
| 295 | l3proto = __nf_nat_l3proto_find(nf_ct_l3num |
| 296 | ((struct nf_conn *)skb->nfct)); |
| 297 | l3proto->csum_recalc(skb, IPPROTO_TCP, tcph, &tcph->check, |
| 298 | datalen, oldlen); |
| 299 | } |
| 300 | |
| 301 | void (*nf_nat_sip_seq_adjust_hook) |
| 302 | (struct sk_buff *skb, |
| 303 | unsigned int protoff, |
| 304 | s16 off); |
| 305 | |
| 306 | static unsigned int (*nf_nat_sip_expect_hook) |
| 307 | (struct sk_buff *skb, |
| 308 | unsigned int protoff, |
| 309 | unsigned int dataoff, |
| 310 | const char **dptr, |
| 311 | unsigned int *datalen, |
| 312 | struct nf_conntrack_expect *exp, |
| 313 | unsigned int matchoff, |
| 314 | unsigned int matchlen) |
| 315 | __read_mostly; |
| 316 | EXPORT_SYMBOL(nf_nat_sip_expect_hook); |
| 317 | |
| 318 | static unsigned int (*nf_nat_sdp_addr_hook) |
| 319 | (struct sk_buff *skb, |
| 320 | unsigned int protoff, |
| 321 | unsigned int dataoff, |
| 322 | const char **dptr, |
| 323 | unsigned int *datalen, |
| 324 | unsigned int sdpoff, |
| 325 | enum sdp_header_types type, |
| 326 | enum sdp_header_types term, |
| 327 | const union nf_inet_addr *addr) |
| 328 | __read_mostly; |
| 329 | EXPORT_SYMBOL(nf_nat_sdp_addr_hook); |
| 330 | |
| 331 | static unsigned int (*nf_nat_sdp_port_hook) |
| 332 | (struct sk_buff *skb, |
| 333 | unsigned int protoff, |
| 334 | unsigned int dataoff, |
| 335 | const char **dptr, |
| 336 | unsigned int *datalen, |
| 337 | unsigned int matchoff, |
| 338 | unsigned int matchlen, |
| 339 | u_int16_t port) __read_mostly; |
| 340 | EXPORT_SYMBOL(nf_nat_sdp_port_hook); |
| 341 | |
| 342 | static unsigned int (*nf_nat_sdp_session_hook) |
| 343 | (struct sk_buff *skb, |
| 344 | unsigned int protoff, |
| 345 | unsigned int dataoff, |
| 346 | const char **dptr, |
| 347 | unsigned int *datalen, |
| 348 | unsigned int sdpoff, |
| 349 | const union nf_inet_addr *addr) |
| 350 | __read_mostly; |
| 351 | EXPORT_SYMBOL(nf_nat_sdp_session_hook); |
| 352 | |
| 353 | static unsigned int (*nf_nat_sdp_media_hook) |
| 354 | (struct sk_buff *skb, |
| 355 | unsigned int protoff, |
| 356 | unsigned int dataoff, |
| 357 | const char **dptr, |
| 358 | unsigned int *datalen, |
| 359 | struct nf_conntrack_expect *rtp_exp, |
| 360 | struct nf_conntrack_expect *rtcp_exp, |
| 361 | unsigned int mediaoff, |
| 362 | unsigned int medialen, |
| 363 | union nf_inet_addr *rtp_addr) |
| 364 | __read_mostly; |
| 365 | EXPORT_SYMBOL(nf_nat_sdp_media_hook); |
| 366 | |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 367 | static int string_len(const struct nf_conn *ct, const char *dptr, |
| 368 | const char *limit, int *shift) |
| 369 | { |
| 370 | int len = 0; |
| 371 | |
| 372 | while (dptr < limit && isalpha(*dptr)) { |
| 373 | dptr++; |
| 374 | len++; |
| 375 | } |
| 376 | return len; |
| 377 | } |
| 378 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 379 | static int nf_sip_enqueue_packet(struct nf_queue_entry *entry, |
| 380 | unsigned int queuenum) |
| 381 | { |
| 382 | enum ip_conntrack_info ctinfo_list; |
| 383 | struct nf_conn *ct_temp; |
| 384 | struct sip_list *node = kzalloc(sizeof(*node), |
| 385 | GFP_ATOMIC | __GFP_NOWARN); |
| 386 | if (!node) |
| 387 | return XT_CONTINUE; |
| 388 | |
| 389 | ct_temp = nf_ct_get(entry->skb, &ctinfo_list); |
| 390 | node->entry = entry; |
| 391 | list_add(&node->list, &ct_temp->sip_segment_list); |
| 392 | return 0; |
| 393 | } |
| 394 | |
Ravinder Konka | 4eafdb9 | 2016-01-18 19:35:52 +0530 | [diff] [blame] | 395 | static void nf_hook_drop_sip(struct net *net, const struct nf_hook_entry *hook) |
| 396 | { |
| 397 | } |
| 398 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 399 | static const struct nf_queue_handler nf_sip_qh = { |
| 400 | .outfn = &nf_sip_enqueue_packet, |
Ravinder Konka | 4eafdb9 | 2016-01-18 19:35:52 +0530 | [diff] [blame] | 401 | .nf_hook_drop = &nf_hook_drop_sip, |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | static |
| 405 | int proc_sip_segment(struct ctl_table *ctl, int write, |
| 406 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 407 | { |
| 408 | int ret; |
| 409 | |
| 410 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 411 | if (nf_ct_enable_sip_segmentation) { |
| 412 | pr_debug("registering queue handler\n"); |
| 413 | nf_register_queue_handler(&init_net, &nf_sip_qh); |
| 414 | } else { |
| 415 | pr_debug("de-registering queue handler\n"); |
| 416 | nf_unregister_queue_handler(&init_net); |
| 417 | } |
| 418 | return ret; |
| 419 | } |
| 420 | |
Jan Engelhardt | 13f7d63 | 2008-01-31 04:50:25 -0800 | [diff] [blame] | 421 | static int digits_len(const struct nf_conn *ct, const char *dptr, |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 422 | const char *limit, int *shift) |
| 423 | { |
| 424 | int len = 0; |
Patrick McHardy | b1ec488 | 2008-03-25 20:10:11 -0700 | [diff] [blame] | 425 | while (dptr < limit && isdigit(*dptr)) { |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 426 | dptr++; |
| 427 | len++; |
| 428 | } |
| 429 | return len; |
| 430 | } |
| 431 | |
Simon Horman | 001985b | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 432 | static int iswordc(const char c) |
| 433 | { |
| 434 | if (isalnum(c) || c == '!' || c == '"' || c == '%' || |
Marco Angaroni | f0608ce | 2016-08-30 18:48:24 +0200 | [diff] [blame] | 435 | (c >= '(' && c <= '+') || c == ':' || c == '<' || c == '>' || |
Simon Horman | 001985b | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 436 | c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' || |
Marco Angaroni | f0608ce | 2016-08-30 18:48:24 +0200 | [diff] [blame] | 437 | c == '{' || c == '}' || c == '~' || (c >= '-' && c <= '/') || |
| 438 | c == '\'') |
Simon Horman | 001985b | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 439 | return 1; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int word_len(const char *dptr, const char *limit) |
| 444 | { |
| 445 | int len = 0; |
| 446 | while (dptr < limit && iswordc(*dptr)) { |
| 447 | dptr++; |
| 448 | len++; |
| 449 | } |
| 450 | return len; |
| 451 | } |
| 452 | |
| 453 | static int callid_len(const struct nf_conn *ct, const char *dptr, |
| 454 | const char *limit, int *shift) |
| 455 | { |
| 456 | int len, domain_len; |
| 457 | |
| 458 | len = word_len(dptr, limit); |
| 459 | dptr += len; |
| 460 | if (!len || dptr == limit || *dptr != '@') |
| 461 | return len; |
| 462 | dptr++; |
| 463 | len++; |
| 464 | |
| 465 | domain_len = word_len(dptr, limit); |
| 466 | if (!domain_len) |
| 467 | return 0; |
| 468 | return len + domain_len; |
| 469 | } |
| 470 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 471 | /* get media type + port length */ |
| 472 | static int media_len(const struct nf_conn *ct, const char *dptr, |
| 473 | const char *limit, int *shift) |
| 474 | { |
| 475 | int len = string_len(ct, dptr, limit, shift); |
| 476 | |
| 477 | dptr += len; |
| 478 | if (dptr >= limit || *dptr != ' ') |
| 479 | return 0; |
| 480 | len++; |
| 481 | dptr++; |
| 482 | |
| 483 | return len + digits_len(ct, dptr, limit, shift); |
| 484 | } |
| 485 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 486 | static int sip_parse_addr(const struct nf_conn *ct, const char *cp, |
| 487 | const char **endp, union nf_inet_addr *addr, |
| 488 | const char *limit, bool delim) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 489 | { |
| 490 | const char *end; |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 491 | int ret; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 492 | |
Simon Horman | 5adbb9f | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 493 | if (!ct) |
| 494 | return 0; |
| 495 | |
Patrick McHardy | fa913dd | 2008-04-14 11:15:45 +0200 | [diff] [blame] | 496 | memset(addr, 0, sizeof(*addr)); |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 497 | switch (nf_ct_l3num(ct)) { |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 498 | case AF_INET: |
| 499 | ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end); |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 500 | if (ret == 0) |
| 501 | return 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 502 | break; |
| 503 | case AF_INET6: |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 504 | if (cp < limit && *cp == '[') |
| 505 | cp++; |
| 506 | else if (delim) |
| 507 | return 0; |
| 508 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 509 | ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end); |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 510 | if (ret == 0) |
| 511 | return 0; |
| 512 | |
| 513 | if (end < limit && *end == ']') |
| 514 | end++; |
| 515 | else if (delim) |
| 516 | return 0; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 517 | break; |
| 518 | default: |
| 519 | BUG(); |
| 520 | } |
| 521 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 522 | if (endp) |
| 523 | *endp = end; |
| 524 | return 1; |
| 525 | } |
| 526 | |
| 527 | /* skip ip address. returns its length. */ |
Jan Engelhardt | 13f7d63 | 2008-01-31 04:50:25 -0800 | [diff] [blame] | 528 | static int epaddr_len(const struct nf_conn *ct, const char *dptr, |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 529 | const char *limit, int *shift) |
| 530 | { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 531 | union nf_inet_addr addr; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 532 | const char *aux = dptr; |
| 533 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 534 | if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 535 | pr_debug("ip: %s parse failed.!\n", dptr); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* Port number */ |
| 540 | if (*dptr == ':') { |
| 541 | dptr++; |
| 542 | dptr += digits_len(ct, dptr, limit, shift); |
| 543 | } |
| 544 | return dptr - aux; |
| 545 | } |
| 546 | |
| 547 | /* get address length, skiping user info. */ |
Jan Engelhardt | 13f7d63 | 2008-01-31 04:50:25 -0800 | [diff] [blame] | 548 | static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr, |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 549 | const char *limit, int *shift) |
| 550 | { |
Patrick McHardy | aa584ed | 2007-08-14 13:14:35 -0700 | [diff] [blame] | 551 | const char *start = dptr; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 552 | int s = *shift; |
| 553 | |
Lars Immisch | 7da5bfb | 2007-01-30 14:24:57 -0800 | [diff] [blame] | 554 | /* Search for @, but stop at the end of the line. |
| 555 | * We are inside a sip: URI, so we don't need to worry about |
| 556 | * continuation lines. */ |
Patrick McHardy | b1ec488 | 2008-03-25 20:10:11 -0700 | [diff] [blame] | 557 | while (dptr < limit && |
Lars Immisch | 7da5bfb | 2007-01-30 14:24:57 -0800 | [diff] [blame] | 558 | *dptr != '@' && *dptr != '\r' && *dptr != '\n') { |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 559 | (*shift)++; |
Lars Immisch | 7da5bfb | 2007-01-30 14:24:57 -0800 | [diff] [blame] | 560 | dptr++; |
| 561 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 562 | |
Patrick McHardy | b1ec488 | 2008-03-25 20:10:11 -0700 | [diff] [blame] | 563 | if (dptr < limit && *dptr == '@') { |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 564 | dptr++; |
| 565 | (*shift)++; |
Patrick McHardy | aa584ed | 2007-08-14 13:14:35 -0700 | [diff] [blame] | 566 | } else { |
| 567 | dptr = start; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 568 | *shift = s; |
Patrick McHardy | aa584ed | 2007-08-14 13:14:35 -0700 | [diff] [blame] | 569 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 570 | |
| 571 | return epaddr_len(ct, dptr, limit, shift); |
| 572 | } |
| 573 | |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 574 | /* Parse a SIP request line of the form: |
| 575 | * |
| 576 | * Request-Line = Method SP Request-URI SP SIP-Version CRLF |
| 577 | * |
| 578 | * and return the offset and length of the address contained in the Request-URI. |
| 579 | */ |
| 580 | int ct_sip_parse_request(const struct nf_conn *ct, |
| 581 | const char *dptr, unsigned int datalen, |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 582 | unsigned int *matchoff, unsigned int *matchlen, |
| 583 | union nf_inet_addr *addr, __be16 *port) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 584 | { |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 585 | const char *start = dptr, *limit = dptr + datalen, *end; |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 586 | unsigned int mlen; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 587 | unsigned int p; |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 588 | int shift = 0; |
| 589 | |
| 590 | /* Skip method and following whitespace */ |
| 591 | mlen = string_len(ct, dptr, limit, NULL); |
| 592 | if (!mlen) |
| 593 | return 0; |
| 594 | dptr += mlen; |
| 595 | if (++dptr >= limit) |
| 596 | return 0; |
| 597 | |
| 598 | /* Find SIP URI */ |
Patrick McHardy | 54101f4 | 2010-02-11 12:23:12 +0100 | [diff] [blame] | 599 | for (; dptr < limit - strlen("sip:"); dptr++) { |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 600 | if (*dptr == '\r' || *dptr == '\n') |
| 601 | return -1; |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 602 | if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) { |
Patrick McHardy | 54101f4 | 2010-02-11 12:23:12 +0100 | [diff] [blame] | 603 | dptr += strlen("sip:"); |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 604 | break; |
Patrick McHardy | 54101f4 | 2010-02-11 12:23:12 +0100 | [diff] [blame] | 605 | } |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 606 | } |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 607 | if (!skp_epaddr_len(ct, dptr, limit, &shift)) |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 608 | return 0; |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 609 | dptr += shift; |
| 610 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 611 | if (!sip_parse_addr(ct, dptr, &end, addr, limit, true)) |
Patrick McHardy | 624f8b7 | 2008-03-25 20:19:30 -0700 | [diff] [blame] | 612 | return -1; |
| 613 | if (end < limit && *end == ':') { |
| 614 | end++; |
| 615 | p = simple_strtoul(end, (char **)&end, 10); |
| 616 | if (p < 1024 || p > 65535) |
| 617 | return -1; |
| 618 | *port = htons(p); |
| 619 | } else |
| 620 | *port = htons(SIP_PORT); |
| 621 | |
| 622 | if (end == dptr) |
| 623 | return 0; |
| 624 | *matchoff = dptr - start; |
| 625 | *matchlen = end - dptr; |
Patrick McHardy | ac36774 | 2008-03-25 20:18:40 -0700 | [diff] [blame] | 626 | return 1; |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(ct_sip_parse_request); |
| 629 | |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 630 | /* SIP header parsing: SIP headers are located at the beginning of a line, but |
| 631 | * may span several lines, in which case the continuation lines begin with a |
| 632 | * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or |
| 633 | * CRLF, RFC 3261 allows only CRLF, we support both. |
| 634 | * |
| 635 | * Headers are followed by (optionally) whitespace, a colon, again (optionally) |
| 636 | * whitespace and the values. Whitespace in this context means any amount of |
| 637 | * tabs, spaces and continuation lines, which are treated as a single whitespace |
| 638 | * character. |
Patrick McHardy | 05e3ced | 2008-03-25 20:19:13 -0700 | [diff] [blame] | 639 | * |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 640 | * Some headers may appear multiple times. A comma separated list of values is |
Patrick McHardy | 05e3ced | 2008-03-25 20:19:13 -0700 | [diff] [blame] | 641 | * equivalent to multiple headers. |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 642 | */ |
| 643 | static const struct sip_header ct_sip_hdrs[] = { |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 644 | [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len), |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 645 | [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len), |
| 646 | [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len), |
| 647 | [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len), |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 648 | [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len), |
| 649 | [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len), |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 650 | [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len), |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 651 | [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len), |
Simon Horman | 001985b | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 652 | [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len), |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | static const char *sip_follow_continuation(const char *dptr, const char *limit) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 656 | { |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 657 | /* Walk past newline */ |
| 658 | if (++dptr >= limit) |
| 659 | return NULL; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 660 | |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 661 | /* Skip '\n' in CR LF */ |
| 662 | if (*(dptr - 1) == '\r' && *dptr == '\n') { |
| 663 | if (++dptr >= limit) |
| 664 | return NULL; |
| 665 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 666 | |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 667 | /* Continuation line? */ |
| 668 | if (*dptr != ' ' && *dptr != '\t') |
| 669 | return NULL; |
| 670 | |
| 671 | /* skip leading whitespace */ |
| 672 | for (; dptr < limit; dptr++) { |
| 673 | if (*dptr != ' ' && *dptr != '\t') |
| 674 | break; |
| 675 | } |
| 676 | return dptr; |
| 677 | } |
| 678 | |
| 679 | static const char *sip_skip_whitespace(const char *dptr, const char *limit) |
| 680 | { |
| 681 | for (; dptr < limit; dptr++) { |
Marco Angaroni | 1bcabc8 | 2016-08-30 18:52:22 +0200 | [diff] [blame] | 682 | if (*dptr == ' ' || *dptr == '\t') |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 683 | continue; |
| 684 | if (*dptr != '\r' && *dptr != '\n') |
| 685 | break; |
| 686 | dptr = sip_follow_continuation(dptr, limit); |
Marco Angaroni | 68cb9fe | 2016-08-30 18:48:19 +0200 | [diff] [blame] | 687 | break; |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 688 | } |
| 689 | return dptr; |
| 690 | } |
| 691 | |
| 692 | /* Search within a SIP header value, dealing with continuation lines */ |
| 693 | static const char *ct_sip_header_search(const char *dptr, const char *limit, |
| 694 | const char *needle, unsigned int len) |
| 695 | { |
| 696 | for (limit -= len; dptr < limit; dptr++) { |
| 697 | if (*dptr == '\r' || *dptr == '\n') { |
| 698 | dptr = sip_follow_continuation(dptr, limit); |
| 699 | if (dptr == NULL) |
| 700 | break; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 701 | continue; |
| 702 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 703 | |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 704 | if (strncasecmp(dptr, needle, len) == 0) |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 705 | return dptr; |
| 706 | } |
| 707 | return NULL; |
| 708 | } |
| 709 | |
| 710 | int ct_sip_get_header(const struct nf_conn *ct, const char *dptr, |
| 711 | unsigned int dataoff, unsigned int datalen, |
| 712 | enum sip_header_types type, |
| 713 | unsigned int *matchoff, unsigned int *matchlen) |
| 714 | { |
| 715 | const struct sip_header *hdr = &ct_sip_hdrs[type]; |
| 716 | const char *start = dptr, *limit = dptr + datalen; |
| 717 | int shift = 0; |
| 718 | |
| 719 | for (dptr += dataoff; dptr < limit; dptr++) { |
| 720 | /* Find beginning of line */ |
| 721 | if (*dptr != '\r' && *dptr != '\n') |
| 722 | continue; |
| 723 | if (++dptr >= limit) |
| 724 | break; |
| 725 | if (*(dptr - 1) == '\r' && *dptr == '\n') { |
| 726 | if (++dptr >= limit) |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | /* Skip continuation lines */ |
| 731 | if (*dptr == ' ' || *dptr == '\t') |
| 732 | continue; |
| 733 | |
| 734 | /* Find header. Compact headers must be followed by a |
| 735 | * non-alphabetic character to avoid mismatches. */ |
| 736 | if (limit - dptr >= hdr->len && |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 737 | strncasecmp(dptr, hdr->name, hdr->len) == 0) |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 738 | dptr += hdr->len; |
| 739 | else if (hdr->cname && limit - dptr >= hdr->clen + 1 && |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 740 | strncasecmp(dptr, hdr->cname, hdr->clen) == 0 && |
Patrick McHardy | 135d018 | 2010-01-19 19:06:59 +0100 | [diff] [blame] | 741 | !isalpha(*(dptr + hdr->clen))) |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 742 | dptr += hdr->clen; |
| 743 | else |
| 744 | continue; |
| 745 | |
| 746 | /* Find and skip colon */ |
| 747 | dptr = sip_skip_whitespace(dptr, limit); |
| 748 | if (dptr == NULL) |
| 749 | break; |
| 750 | if (*dptr != ':' || ++dptr >= limit) |
| 751 | break; |
| 752 | |
| 753 | /* Skip whitespace after colon */ |
| 754 | dptr = sip_skip_whitespace(dptr, limit); |
| 755 | if (dptr == NULL) |
| 756 | break; |
| 757 | |
| 758 | *matchoff = dptr - start; |
| 759 | if (hdr->search) { |
| 760 | dptr = ct_sip_header_search(dptr, limit, hdr->search, |
| 761 | hdr->slen); |
| 762 | if (!dptr) |
| 763 | return -1; |
| 764 | dptr += hdr->slen; |
| 765 | } |
| 766 | |
| 767 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 768 | if (!*matchlen) |
| 769 | return -1; |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 770 | *matchoff = dptr - start + shift; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 771 | return 1; |
| 772 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 773 | return 0; |
| 774 | } |
Patrick McHardy | ea45f12 | 2008-03-25 20:18:57 -0700 | [diff] [blame] | 775 | EXPORT_SYMBOL_GPL(ct_sip_get_header); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 776 | |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 777 | /* Get next header field in a list of comma separated values */ |
Patrick McHardy | 05e3ced | 2008-03-25 20:19:13 -0700 | [diff] [blame] | 778 | static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr, |
| 779 | unsigned int dataoff, unsigned int datalen, |
| 780 | enum sip_header_types type, |
| 781 | unsigned int *matchoff, unsigned int *matchlen) |
| 782 | { |
| 783 | const struct sip_header *hdr = &ct_sip_hdrs[type]; |
| 784 | const char *start = dptr, *limit = dptr + datalen; |
| 785 | int shift = 0; |
| 786 | |
| 787 | dptr += dataoff; |
| 788 | |
| 789 | dptr = ct_sip_header_search(dptr, limit, ",", strlen(",")); |
| 790 | if (!dptr) |
| 791 | return 0; |
| 792 | |
| 793 | dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen); |
| 794 | if (!dptr) |
| 795 | return 0; |
| 796 | dptr += hdr->slen; |
| 797 | |
| 798 | *matchoff = dptr - start; |
| 799 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); |
| 800 | if (!*matchlen) |
| 801 | return -1; |
| 802 | *matchoff += shift; |
| 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | /* Walk through headers until a parsable one is found or no header of the |
| 807 | * given type is left. */ |
| 808 | static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr, |
| 809 | unsigned int dataoff, unsigned int datalen, |
| 810 | enum sip_header_types type, int *in_header, |
| 811 | unsigned int *matchoff, unsigned int *matchlen) |
| 812 | { |
| 813 | int ret; |
| 814 | |
| 815 | if (in_header && *in_header) { |
| 816 | while (1) { |
| 817 | ret = ct_sip_next_header(ct, dptr, dataoff, datalen, |
| 818 | type, matchoff, matchlen); |
| 819 | if (ret > 0) |
| 820 | return ret; |
| 821 | if (ret == 0) |
| 822 | break; |
| 823 | dataoff += *matchoff; |
| 824 | } |
| 825 | *in_header = 0; |
| 826 | } |
| 827 | |
| 828 | while (1) { |
| 829 | ret = ct_sip_get_header(ct, dptr, dataoff, datalen, |
| 830 | type, matchoff, matchlen); |
| 831 | if (ret > 0) |
| 832 | break; |
| 833 | if (ret == 0) |
| 834 | return ret; |
| 835 | dataoff += *matchoff; |
| 836 | } |
| 837 | |
| 838 | if (in_header) |
| 839 | *in_header = 1; |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | /* Locate a SIP header, parse the URI and return the offset and length of |
| 844 | * the address as well as the address and port themselves. A stream of |
| 845 | * headers can be parsed by handing in a non-NULL datalen and in_header |
| 846 | * pointer. |
| 847 | */ |
| 848 | int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr, |
| 849 | unsigned int *dataoff, unsigned int datalen, |
| 850 | enum sip_header_types type, int *in_header, |
| 851 | unsigned int *matchoff, unsigned int *matchlen, |
| 852 | union nf_inet_addr *addr, __be16 *port) |
| 853 | { |
| 854 | const char *c, *limit = dptr + datalen; |
| 855 | unsigned int p; |
| 856 | int ret; |
| 857 | |
| 858 | ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen, |
| 859 | type, in_header, matchoff, matchlen); |
| 860 | WARN_ON(ret < 0); |
| 861 | if (ret == 0) |
| 862 | return ret; |
| 863 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 864 | if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true)) |
Patrick McHardy | 05e3ced | 2008-03-25 20:19:13 -0700 | [diff] [blame] | 865 | return -1; |
| 866 | if (*c == ':') { |
| 867 | c++; |
| 868 | p = simple_strtoul(c, (char **)&c, 10); |
| 869 | if (p < 1024 || p > 65535) |
| 870 | return -1; |
| 871 | *port = htons(p); |
| 872 | } else |
| 873 | *port = htons(SIP_PORT); |
| 874 | |
| 875 | if (dataoff) |
| 876 | *dataoff = c - dptr; |
| 877 | return 1; |
| 878 | } |
| 879 | EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri); |
| 880 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 881 | static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr, |
| 882 | unsigned int dataoff, unsigned int datalen, |
| 883 | const char *name, |
| 884 | unsigned int *matchoff, unsigned int *matchlen) |
| 885 | { |
| 886 | const char *limit = dptr + datalen; |
| 887 | const char *start; |
| 888 | const char *end; |
| 889 | |
| 890 | limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(",")); |
| 891 | if (!limit) |
| 892 | limit = dptr + datalen; |
| 893 | |
| 894 | start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name)); |
| 895 | if (!start) |
| 896 | return 0; |
| 897 | start += strlen(name); |
| 898 | |
| 899 | end = ct_sip_header_search(start, limit, ";", strlen(";")); |
| 900 | if (!end) |
| 901 | end = limit; |
| 902 | |
| 903 | *matchoff = start - dptr; |
| 904 | *matchlen = end - start; |
| 905 | return 1; |
| 906 | } |
| 907 | |
Patrick McHardy | 2bbb211 | 2008-03-25 20:24:24 -0700 | [diff] [blame] | 908 | /* Parse address from header parameter and return address, offset and length */ |
| 909 | int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr, |
| 910 | unsigned int dataoff, unsigned int datalen, |
| 911 | const char *name, |
| 912 | unsigned int *matchoff, unsigned int *matchlen, |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 913 | union nf_inet_addr *addr, bool delim) |
Patrick McHardy | 2bbb211 | 2008-03-25 20:24:24 -0700 | [diff] [blame] | 914 | { |
| 915 | const char *limit = dptr + datalen; |
| 916 | const char *start, *end; |
| 917 | |
| 918 | limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(",")); |
| 919 | if (!limit) |
| 920 | limit = dptr + datalen; |
| 921 | |
| 922 | start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name)); |
| 923 | if (!start) |
| 924 | return 0; |
| 925 | |
| 926 | start += strlen(name); |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 927 | if (!sip_parse_addr(ct, start, &end, addr, limit, delim)) |
Patrick McHardy | 2bbb211 | 2008-03-25 20:24:24 -0700 | [diff] [blame] | 928 | return 0; |
| 929 | *matchoff = start - dptr; |
| 930 | *matchlen = end - start; |
| 931 | return 1; |
| 932 | } |
| 933 | EXPORT_SYMBOL_GPL(ct_sip_parse_address_param); |
| 934 | |
| 935 | /* Parse numerical header parameter and return value, offset and length */ |
| 936 | int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr, |
| 937 | unsigned int dataoff, unsigned int datalen, |
| 938 | const char *name, |
| 939 | unsigned int *matchoff, unsigned int *matchlen, |
| 940 | unsigned int *val) |
| 941 | { |
| 942 | const char *limit = dptr + datalen; |
| 943 | const char *start; |
| 944 | char *end; |
| 945 | |
| 946 | limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(",")); |
| 947 | if (!limit) |
| 948 | limit = dptr + datalen; |
| 949 | |
| 950 | start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name)); |
| 951 | if (!start) |
| 952 | return 0; |
| 953 | |
| 954 | start += strlen(name); |
| 955 | *val = simple_strtoul(start, &end, 0); |
| 956 | if (start == end) |
| 957 | return 0; |
| 958 | if (matchoff && matchlen) { |
| 959 | *matchoff = start - dptr; |
| 960 | *matchlen = end - start; |
| 961 | } |
| 962 | return 1; |
| 963 | } |
| 964 | EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param); |
| 965 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 966 | static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr, |
| 967 | unsigned int dataoff, unsigned int datalen, |
| 968 | u8 *proto) |
| 969 | { |
| 970 | unsigned int matchoff, matchlen; |
| 971 | |
| 972 | if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=", |
| 973 | &matchoff, &matchlen)) { |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 974 | if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP"))) |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 975 | *proto = IPPROTO_TCP; |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 976 | else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP"))) |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 977 | *proto = IPPROTO_UDP; |
| 978 | else |
| 979 | return 0; |
| 980 | |
| 981 | if (*proto != nf_ct_protonum(ct)) |
| 982 | return 0; |
| 983 | } else |
| 984 | *proto = nf_ct_protonum(ct); |
| 985 | |
| 986 | return 1; |
| 987 | } |
| 988 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 989 | static int sdp_parse_addr(const struct nf_conn *ct, const char *cp, |
| 990 | const char **endp, union nf_inet_addr *addr, |
| 991 | const char *limit) |
| 992 | { |
| 993 | const char *end; |
| 994 | int ret; |
| 995 | |
| 996 | memset(addr, 0, sizeof(*addr)); |
| 997 | switch (nf_ct_l3num(ct)) { |
| 998 | case AF_INET: |
| 999 | ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end); |
| 1000 | break; |
| 1001 | case AF_INET6: |
| 1002 | ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end); |
| 1003 | break; |
| 1004 | default: |
| 1005 | BUG(); |
| 1006 | } |
| 1007 | |
| 1008 | if (ret == 0) |
| 1009 | return 0; |
| 1010 | if (endp) |
| 1011 | *endp = end; |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
| 1015 | /* skip ip address. returns its length. */ |
| 1016 | static int sdp_addr_len(const struct nf_conn *ct, const char *dptr, |
| 1017 | const char *limit, int *shift) |
| 1018 | { |
| 1019 | union nf_inet_addr addr; |
| 1020 | const char *aux = dptr; |
| 1021 | |
| 1022 | if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) { |
| 1023 | pr_debug("ip: %s parse failed.!\n", dptr); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | return dptr - aux; |
| 1028 | } |
| 1029 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1030 | /* SDP header parsing: a SDP session description contains an ordered set of |
| 1031 | * headers, starting with a section containing general session parameters, |
| 1032 | * optionally followed by multiple media descriptions. |
| 1033 | * |
| 1034 | * SDP headers always start at the beginning of a line. According to RFC 2327: |
| 1035 | * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should |
| 1036 | * be tolerant and also accept records terminated with a single newline |
| 1037 | * character". We handle both cases. |
| 1038 | */ |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1039 | static const struct sip_header ct_sdp_hdrs_v4[] = { |
| 1040 | [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len), |
| 1041 | [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len), |
| 1042 | [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len), |
| 1043 | [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len), |
| 1044 | }; |
| 1045 | |
| 1046 | static const struct sip_header ct_sdp_hdrs_v6[] = { |
| 1047 | [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len), |
| 1048 | [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len), |
| 1049 | [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len), |
| 1050 | [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len), |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | |
| 1053 | /* Linear string search within SDP header values */ |
| 1054 | static const char *ct_sdp_header_search(const char *dptr, const char *limit, |
| 1055 | const char *needle, unsigned int len) |
| 1056 | { |
| 1057 | for (limit -= len; dptr < limit; dptr++) { |
| 1058 | if (*dptr == '\r' || *dptr == '\n') |
| 1059 | break; |
| 1060 | if (strncmp(dptr, needle, len) == 0) |
| 1061 | return dptr; |
| 1062 | } |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | /* Locate a SDP header (optionally a substring within the header value), |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1067 | * optionally stopping at the first occurrence of the term header, parse |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1068 | * it and return the offset and length of the data we're interested in. |
| 1069 | */ |
| 1070 | int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr, |
| 1071 | unsigned int dataoff, unsigned int datalen, |
| 1072 | enum sdp_header_types type, |
| 1073 | enum sdp_header_types term, |
| 1074 | unsigned int *matchoff, unsigned int *matchlen) |
| 1075 | { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1076 | const struct sip_header *hdrs, *hdr, *thdr; |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1077 | const char *start = dptr, *limit = dptr + datalen; |
| 1078 | int shift = 0; |
| 1079 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1080 | hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6; |
| 1081 | hdr = &hdrs[type]; |
| 1082 | thdr = &hdrs[term]; |
| 1083 | |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1084 | for (dptr += dataoff; dptr < limit; dptr++) { |
| 1085 | /* Find beginning of line */ |
| 1086 | if (*dptr != '\r' && *dptr != '\n') |
| 1087 | continue; |
| 1088 | if (++dptr >= limit) |
| 1089 | break; |
| 1090 | if (*(dptr - 1) == '\r' && *dptr == '\n') { |
| 1091 | if (++dptr >= limit) |
| 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | if (term != SDP_HDR_UNSPEC && |
| 1096 | limit - dptr >= thdr->len && |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 1097 | strncasecmp(dptr, thdr->name, thdr->len) == 0) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1098 | break; |
| 1099 | else if (limit - dptr >= hdr->len && |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 1100 | strncasecmp(dptr, hdr->name, hdr->len) == 0) |
Patrick McHardy | 3e9b4600b | 2008-03-25 20:17:55 -0700 | [diff] [blame] | 1101 | dptr += hdr->len; |
| 1102 | else |
| 1103 | continue; |
| 1104 | |
| 1105 | *matchoff = dptr - start; |
| 1106 | if (hdr->search) { |
| 1107 | dptr = ct_sdp_header_search(dptr, limit, hdr->search, |
| 1108 | hdr->slen); |
| 1109 | if (!dptr) |
| 1110 | return -1; |
| 1111 | dptr += hdr->slen; |
| 1112 | } |
| 1113 | |
| 1114 | *matchlen = hdr->match_len(ct, dptr, limit, &shift); |
| 1115 | if (!*matchlen) |
| 1116 | return -1; |
| 1117 | *matchoff = dptr - start + shift; |
| 1118 | return 1; |
| 1119 | } |
| 1120 | return 0; |
| 1121 | } |
| 1122 | EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header); |
| 1123 | |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1124 | static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr, |
| 1125 | unsigned int dataoff, unsigned int datalen, |
| 1126 | enum sdp_header_types type, |
| 1127 | enum sdp_header_types term, |
| 1128 | unsigned int *matchoff, unsigned int *matchlen, |
| 1129 | union nf_inet_addr *addr) |
| 1130 | { |
| 1131 | int ret; |
| 1132 | |
| 1133 | ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term, |
| 1134 | matchoff, matchlen); |
| 1135 | if (ret <= 0) |
| 1136 | return ret; |
| 1137 | |
Patrick McHardy | 02b69cb | 2012-08-09 10:08:46 +0000 | [diff] [blame] | 1138 | if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr, |
| 1139 | dptr + *matchoff + *matchlen)) |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1140 | return -1; |
| 1141 | return 1; |
| 1142 | } |
| 1143 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1144 | static int refresh_signalling_expectation(struct nf_conn *ct, |
| 1145 | union nf_inet_addr *addr, |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1146 | u8 proto, __be16 port, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1147 | unsigned int expires) |
| 1148 | { |
| 1149 | struct nf_conn_help *help = nfct_help(ct); |
| 1150 | struct nf_conntrack_expect *exp; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1151 | struct hlist_node *next; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1152 | int found = 0; |
| 1153 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1154 | spin_lock_bh(&nf_conntrack_expect_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1155 | hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) { |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1156 | if (exp->class != SIP_EXPECT_SIGNALLING || |
| 1157 | !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) || |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1158 | exp->tuple.dst.protonum != proto || |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1159 | exp->tuple.dst.u.udp.port != port) |
| 1160 | continue; |
| 1161 | if (!del_timer(&exp->timeout)) |
| 1162 | continue; |
| 1163 | exp->flags &= ~NF_CT_EXPECT_INACTIVE; |
| 1164 | exp->timeout.expires = jiffies + expires * HZ; |
| 1165 | add_timer(&exp->timeout); |
| 1166 | found = 1; |
| 1167 | break; |
| 1168 | } |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1169 | spin_unlock_bh(&nf_conntrack_expect_lock); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1170 | return found; |
| 1171 | } |
| 1172 | |
| 1173 | static void flush_expectations(struct nf_conn *ct, bool media) |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1174 | { |
| 1175 | struct nf_conn_help *help = nfct_help(ct); |
| 1176 | struct nf_conntrack_expect *exp; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1177 | struct hlist_node *next; |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1178 | |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1179 | spin_lock_bh(&nf_conntrack_expect_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1180 | hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) { |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1181 | if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media) |
| 1182 | continue; |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1183 | if (!del_timer(&exp->timeout)) |
| 1184 | continue; |
| 1185 | nf_ct_unlink_expect(exp); |
| 1186 | nf_ct_expect_put(exp); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1187 | if (!media) |
| 1188 | break; |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1189 | } |
Jesper Dangaard Brouer | ca7433d | 2014-03-03 14:46:01 +0100 | [diff] [blame] | 1190 | spin_unlock_bh(&nf_conntrack_expect_lock); |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1193 | static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff, |
| 1194 | unsigned int dataoff, |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1195 | const char **dptr, unsigned int *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1196 | union nf_inet_addr *daddr, __be16 port, |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1197 | enum sip_expectation_classes class, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1198 | unsigned int mediaoff, unsigned int medialen) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1199 | { |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1200 | struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp; |
Patrick McHardy | 212440a | 2008-03-25 20:17:13 -0700 | [diff] [blame] | 1201 | enum ip_conntrack_info ctinfo; |
| 1202 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Alexey Dobriyan | a5c3a80 | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 1203 | struct net *net = nf_ct_net(ct); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1204 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1205 | union nf_inet_addr *saddr; |
| 1206 | struct nf_conntrack_tuple tuple; |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1207 | int direct_rtp = 0, skip_expect = 0, ret = NF_DROP; |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1208 | u_int16_t base_port; |
| 1209 | __be16 rtp_port, rtcp_port; |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1210 | const struct nf_nat_sip_hooks *hooks; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1211 | |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1212 | saddr = NULL; |
| 1213 | if (sip_direct_media) { |
| 1214 | if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3)) |
| 1215 | return NF_ACCEPT; |
| 1216 | saddr = &ct->tuplehash[!dir].tuple.src.u3; |
| 1217 | } |
| 1218 | |
| 1219 | /* We need to check whether the registration exists before attempting |
| 1220 | * to register it since we can see the same media description multiple |
| 1221 | * times on different connections in case multiple endpoints receive |
| 1222 | * the same call. |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1223 | * |
| 1224 | * RTP optimization: if we find a matching media channel expectation |
| 1225 | * and both the expectation and this connection are SNATed, we assume |
| 1226 | * both sides can reach each other directly and use the final |
| 1227 | * destination address from the expectation. We still need to keep |
| 1228 | * the NATed expectations for media that might arrive from the |
| 1229 | * outside, and additionally need to expect the direct RTP stream |
| 1230 | * in case it passes through us even without NAT. |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1231 | */ |
| 1232 | memset(&tuple, 0, sizeof(tuple)); |
| 1233 | if (saddr) |
| 1234 | tuple.src.u3 = *saddr; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 1235 | tuple.src.l3num = nf_ct_l3num(ct); |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1236 | tuple.dst.protonum = IPPROTO_UDP; |
| 1237 | tuple.dst.u3 = *daddr; |
| 1238 | tuple.dst.u.udp.port = port; |
| 1239 | |
| 1240 | rcu_read_lock(); |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1241 | do { |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 1242 | exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple); |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1243 | |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1244 | if (!exp || exp->master == ct || |
| 1245 | nfct_help(exp->master)->helper != nfct_help(ct)->helper || |
| 1246 | exp->class != class) |
| 1247 | break; |
Patrick McHardy | e1f9a46 | 2008-04-19 17:53:52 -0700 | [diff] [blame] | 1248 | #ifdef CONFIG_NF_NAT_NEEDED |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1249 | if (!direct_rtp && |
| 1250 | (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) || |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1251 | exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) && |
| 1252 | ct->status & IPS_NAT_MASK) { |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1253 | *daddr = exp->saved_addr; |
| 1254 | tuple.dst.u3 = exp->saved_addr; |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1255 | tuple.dst.u.udp.port = exp->saved_proto.udp.port; |
| 1256 | direct_rtp = 1; |
| 1257 | } else |
Patrick McHardy | e1f9a46 | 2008-04-19 17:53:52 -0700 | [diff] [blame] | 1258 | #endif |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1259 | skip_expect = 1; |
| 1260 | } while (!skip_expect); |
Patrick McHardy | d901a93 | 2008-03-25 20:25:32 -0700 | [diff] [blame] | 1261 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1262 | base_port = ntohs(tuple.dst.u.udp.port) & ~1; |
| 1263 | rtp_port = htons(base_port); |
| 1264 | rtcp_port = htons(base_port + 1); |
| 1265 | |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1266 | if (direct_rtp) { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1267 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 1268 | if (hooks && |
| 1269 | !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen, |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1270 | mediaoff, medialen, ntohs(rtp_port))) |
| 1271 | goto err1; |
| 1272 | } |
| 1273 | |
holger@eitzenberger.org | b21613a | 2013-09-20 22:43:04 +0200 | [diff] [blame] | 1274 | if (skip_expect) { |
| 1275 | rcu_read_unlock(); |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1276 | return NF_ACCEPT; |
holger@eitzenberger.org | b21613a | 2013-09-20 22:43:04 +0200 | [diff] [blame] | 1277 | } |
Patrick McHardy | c7f485a | 2008-03-25 20:26:43 -0700 | [diff] [blame] | 1278 | |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1279 | rtp_exp = nf_ct_expect_alloc(ct); |
| 1280 | if (rtp_exp == NULL) |
| 1281 | goto err1; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 1282 | nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr, |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1283 | IPPROTO_UDP, NULL, &rtp_port); |
| 1284 | |
| 1285 | rtcp_exp = nf_ct_expect_alloc(ct); |
| 1286 | if (rtcp_exp == NULL) |
| 1287 | goto err2; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 1288 | nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr, |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1289 | IPPROTO_UDP, NULL, &rtcp_port); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1290 | |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1291 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 1292 | if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp) |
| 1293 | ret = hooks->sdp_media(skb, protoff, dataoff, dptr, |
| 1294 | datalen, rtp_exp, rtcp_exp, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1295 | mediaoff, medialen, daddr); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1296 | else { |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1297 | if (nf_ct_expect_related(rtp_exp) == 0) { |
| 1298 | if (nf_ct_expect_related(rtcp_exp) != 0) |
| 1299 | nf_ct_unexpect_related(rtp_exp); |
| 1300 | else |
| 1301 | ret = NF_ACCEPT; |
| 1302 | } |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1303 | } |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 1304 | nf_ct_expect_put(rtcp_exp); |
| 1305 | err2: |
| 1306 | nf_ct_expect_put(rtp_exp); |
| 1307 | err1: |
holger@eitzenberger.org | b21613a | 2013-09-20 22:43:04 +0200 | [diff] [blame] | 1308 | rcu_read_unlock(); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 1309 | return ret; |
| 1310 | } |
| 1311 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1312 | static const struct sdp_media_type sdp_media_types[] = { |
| 1313 | SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO), |
| 1314 | SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO), |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1315 | SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE), |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1316 | }; |
| 1317 | |
| 1318 | static const struct sdp_media_type *sdp_media_type(const char *dptr, |
| 1319 | unsigned int matchoff, |
| 1320 | unsigned int matchlen) |
| 1321 | { |
| 1322 | const struct sdp_media_type *t; |
| 1323 | unsigned int i; |
| 1324 | |
| 1325 | for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) { |
| 1326 | t = &sdp_media_types[i]; |
| 1327 | if (matchlen < t->len || |
| 1328 | strncmp(dptr + matchoff, t->name, t->len)) |
| 1329 | continue; |
| 1330 | return t; |
| 1331 | } |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1335 | static int process_sdp(struct sk_buff *skb, unsigned int protoff, |
| 1336 | unsigned int dataoff, |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1337 | const char **dptr, unsigned int *datalen, |
| 1338 | unsigned int cseq) |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1339 | { |
| 1340 | enum ip_conntrack_info ctinfo; |
| 1341 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1342 | unsigned int matchoff, matchlen; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1343 | unsigned int mediaoff, medialen; |
| 1344 | unsigned int sdpoff; |
| 1345 | unsigned int caddr_len, maddr_len; |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1346 | unsigned int i; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1347 | union nf_inet_addr caddr, maddr, rtp_addr; |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1348 | const struct nf_nat_sip_hooks *hooks; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1349 | unsigned int port; |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1350 | const struct sdp_media_type *t; |
| 1351 | int ret = NF_ACCEPT; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1352 | |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1353 | hooks = rcu_dereference(nf_nat_sip_hooks); |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1354 | |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1355 | /* Find beginning of session description */ |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1356 | if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1357 | SDP_HDR_VERSION, SDP_HDR_UNSPEC, |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1358 | &matchoff, &matchlen) <= 0) |
| 1359 | return NF_ACCEPT; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1360 | sdpoff = matchoff; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1361 | |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1362 | /* The connection information is contained in the session description |
| 1363 | * and/or once per media description. The first media description marks |
| 1364 | * the end of the session description. */ |
| 1365 | caddr_len = 0; |
| 1366 | if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen, |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1367 | SDP_HDR_CONNECTION, SDP_HDR_MEDIA, |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1368 | &matchoff, &matchlen, &caddr) > 0) |
| 1369 | caddr_len = matchlen; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1370 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1371 | mediaoff = sdpoff; |
| 1372 | for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) { |
| 1373 | if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen, |
| 1374 | SDP_HDR_MEDIA, SDP_HDR_UNSPEC, |
| 1375 | &mediaoff, &medialen) <= 0) |
| 1376 | break; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1377 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1378 | /* Get media type and port number. A media port value of zero |
| 1379 | * indicates an inactive stream. */ |
| 1380 | t = sdp_media_type(*dptr, mediaoff, medialen); |
| 1381 | if (!t) { |
| 1382 | mediaoff += medialen; |
| 1383 | continue; |
| 1384 | } |
| 1385 | mediaoff += t->len; |
| 1386 | medialen -= t->len; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1387 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1388 | port = simple_strtoul(*dptr + mediaoff, NULL, 10); |
| 1389 | if (port == 0) |
| 1390 | continue; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1391 | if (port < 1024 || port > 65535) { |
| 1392 | nf_ct_helper_log(skb, ct, "wrong port %u", port); |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1393 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1394 | } |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1395 | |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1396 | /* The media description overrides the session description. */ |
| 1397 | maddr_len = 0; |
| 1398 | if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen, |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1399 | SDP_HDR_CONNECTION, SDP_HDR_MEDIA, |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1400 | &matchoff, &matchlen, &maddr) > 0) { |
| 1401 | maddr_len = matchlen; |
| 1402 | memcpy(&rtp_addr, &maddr, sizeof(rtp_addr)); |
| 1403 | } else if (caddr_len) |
| 1404 | memcpy(&rtp_addr, &caddr, sizeof(rtp_addr)); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1405 | else { |
| 1406 | nf_ct_helper_log(skb, ct, "cannot parse SDP message"); |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1407 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1408 | } |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1409 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1410 | ret = set_expected_rtp_rtcp(skb, protoff, dataoff, |
| 1411 | dptr, datalen, |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1412 | &rtp_addr, htons(port), t->class, |
| 1413 | mediaoff, medialen); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1414 | if (ret != NF_ACCEPT) { |
| 1415 | nf_ct_helper_log(skb, ct, |
| 1416 | "cannot add expectation for voice"); |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1417 | return ret; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1418 | } |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1419 | |
| 1420 | /* Update media connection address if present */ |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1421 | if (maddr_len && hooks && ct->status & IPS_NAT_MASK) { |
| 1422 | ret = hooks->sdp_addr(skb, protoff, dataoff, |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1423 | dptr, datalen, mediaoff, |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1424 | SDP_HDR_CONNECTION, |
| 1425 | SDP_HDR_MEDIA, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1426 | &rtp_addr); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1427 | if (ret != NF_ACCEPT) { |
| 1428 | nf_ct_helper_log(skb, ct, "cannot mangle SDP"); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1429 | return ret; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1430 | } |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1431 | } |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 1432 | i++; |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* Update session connection and owner addresses */ |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1436 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 1437 | if (hooks && ct->status & IPS_NAT_MASK) |
| 1438 | ret = hooks->sdp_session(skb, protoff, dataoff, |
| 1439 | dptr, datalen, sdpoff, |
| 1440 | &rtp_addr); |
Patrick McHardy | 4ab9e64 | 2008-03-25 20:26:08 -0700 | [diff] [blame] | 1441 | |
| 1442 | return ret; |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1443 | } |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1444 | static int process_invite_response(struct sk_buff *skb, unsigned int protoff, |
| 1445 | unsigned int dataoff, |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1446 | const char **dptr, unsigned int *datalen, |
| 1447 | unsigned int cseq, unsigned int code) |
| 1448 | { |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1449 | enum ip_conntrack_info ctinfo; |
| 1450 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1451 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1452 | |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1453 | if ((code >= 100 && code <= 199) || |
| 1454 | (code >= 200 && code <= 299)) |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1455 | return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1456 | else if (ct_sip_info->invite_cseq == cseq) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1457 | flush_expectations(ct, true); |
Patrick McHardy | ef75d49 | 2008-05-08 01:15:21 -0700 | [diff] [blame] | 1458 | return NF_ACCEPT; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1461 | static int process_update_response(struct sk_buff *skb, unsigned int protoff, |
| 1462 | unsigned int dataoff, |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1463 | const char **dptr, unsigned int *datalen, |
| 1464 | unsigned int cseq, unsigned int code) |
| 1465 | { |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1466 | enum ip_conntrack_info ctinfo; |
| 1467 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1468 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1469 | |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1470 | if ((code >= 100 && code <= 199) || |
| 1471 | (code >= 200 && code <= 299)) |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1472 | return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1473 | else if (ct_sip_info->invite_cseq == cseq) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1474 | flush_expectations(ct, true); |
Patrick McHardy | ef75d49 | 2008-05-08 01:15:21 -0700 | [diff] [blame] | 1475 | return NF_ACCEPT; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1478 | static int process_prack_response(struct sk_buff *skb, unsigned int protoff, |
| 1479 | unsigned int dataoff, |
Patrick McHardy | 595a8ec | 2008-03-25 20:22:53 -0700 | [diff] [blame] | 1480 | const char **dptr, unsigned int *datalen, |
| 1481 | unsigned int cseq, unsigned int code) |
| 1482 | { |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1483 | enum ip_conntrack_info ctinfo; |
| 1484 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1485 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1486 | |
Patrick McHardy | 595a8ec | 2008-03-25 20:22:53 -0700 | [diff] [blame] | 1487 | if ((code >= 100 && code <= 199) || |
| 1488 | (code >= 200 && code <= 299)) |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1489 | return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1490 | else if (ct_sip_info->invite_cseq == cseq) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1491 | flush_expectations(ct, true); |
Patrick McHardy | ef75d49 | 2008-05-08 01:15:21 -0700 | [diff] [blame] | 1492 | return NF_ACCEPT; |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1493 | } |
Patrick McHardy | 595a8ec | 2008-03-25 20:22:53 -0700 | [diff] [blame] | 1494 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1495 | static int process_invite_request(struct sk_buff *skb, unsigned int protoff, |
| 1496 | unsigned int dataoff, |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1497 | const char **dptr, unsigned int *datalen, |
| 1498 | unsigned int cseq) |
| 1499 | { |
| 1500 | enum ip_conntrack_info ctinfo; |
| 1501 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1502 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1503 | unsigned int ret; |
| 1504 | |
| 1505 | flush_expectations(ct, true); |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1506 | ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq); |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1507 | if (ret == NF_ACCEPT) |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1508 | ct_sip_info->invite_cseq = cseq; |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1509 | return ret; |
| 1510 | } |
| 1511 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1512 | static int process_bye_request(struct sk_buff *skb, unsigned int protoff, |
| 1513 | unsigned int dataoff, |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1514 | const char **dptr, unsigned int *datalen, |
| 1515 | unsigned int cseq) |
| 1516 | { |
| 1517 | enum ip_conntrack_info ctinfo; |
| 1518 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
| 1519 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1520 | flush_expectations(ct, true); |
| 1521 | return NF_ACCEPT; |
| 1522 | } |
| 1523 | |
| 1524 | /* Parse a REGISTER request and create a permanent expectation for incoming |
| 1525 | * signalling connections. The expectation is marked inactive and is activated |
| 1526 | * when receiving a response indicating success from the registrar. |
| 1527 | */ |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1528 | static int process_register_request(struct sk_buff *skb, unsigned int protoff, |
| 1529 | unsigned int dataoff, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1530 | const char **dptr, unsigned int *datalen, |
| 1531 | unsigned int cseq) |
| 1532 | { |
| 1533 | enum ip_conntrack_info ctinfo; |
| 1534 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1535 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1536 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1537 | unsigned int matchoff, matchlen; |
| 1538 | struct nf_conntrack_expect *exp; |
| 1539 | union nf_inet_addr *saddr, daddr; |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1540 | const struct nf_nat_sip_hooks *hooks; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1541 | __be16 port; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1542 | u8 proto; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1543 | unsigned int expires = 0; |
| 1544 | int ret; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* Expected connections can not register again. */ |
| 1547 | if (ct->status & IPS_EXPECTED) |
| 1548 | return NF_ACCEPT; |
| 1549 | |
| 1550 | /* We must check the expiration time: a value of zero signals the |
| 1551 | * registrar to release the binding. We'll remove our expectation |
| 1552 | * when receiving the new bindings in the response, but we don't |
| 1553 | * want to create new ones. |
| 1554 | * |
| 1555 | * The expiration time may be contained in Expires: header, the |
| 1556 | * Contact: header parameters or the URI parameters. |
| 1557 | */ |
| 1558 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES, |
| 1559 | &matchoff, &matchlen) > 0) |
| 1560 | expires = simple_strtoul(*dptr + matchoff, NULL, 10); |
| 1561 | |
| 1562 | ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, |
| 1563 | SIP_HDR_CONTACT, NULL, |
| 1564 | &matchoff, &matchlen, &daddr, &port); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1565 | if (ret < 0) { |
| 1566 | nf_ct_helper_log(skb, ct, "cannot parse contact"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1567 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1568 | } else if (ret == 0) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1569 | return NF_ACCEPT; |
| 1570 | |
| 1571 | /* We don't support third-party registrations */ |
| 1572 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr)) |
| 1573 | return NF_ACCEPT; |
| 1574 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1575 | if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen, |
| 1576 | &proto) == 0) |
| 1577 | return NF_ACCEPT; |
| 1578 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1579 | if (ct_sip_parse_numerical_param(ct, *dptr, |
| 1580 | matchoff + matchlen, *datalen, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1581 | "expires=", NULL, NULL, &expires) < 0) { |
| 1582 | nf_ct_helper_log(skb, ct, "cannot parse expires"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1583 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1584 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1585 | |
| 1586 | if (expires == 0) { |
| 1587 | ret = NF_ACCEPT; |
| 1588 | goto store_cseq; |
| 1589 | } |
| 1590 | |
| 1591 | exp = nf_ct_expect_alloc(ct); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1592 | if (!exp) { |
| 1593 | nf_ct_helper_log(skb, ct, "cannot alloc expectation"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1594 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1595 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1596 | |
| 1597 | saddr = NULL; |
| 1598 | if (sip_direct_signalling) |
| 1599 | saddr = &ct->tuplehash[!dir].tuple.src.u3; |
| 1600 | |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 1601 | nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct), |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1602 | saddr, &daddr, proto, NULL, &port); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1603 | exp->timeout.expires = sip_timeout * HZ; |
| 1604 | exp->helper = nfct_help(ct)->helper; |
| 1605 | exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE; |
| 1606 | |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1607 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 1608 | if (hooks && ct->status & IPS_NAT_MASK) |
| 1609 | ret = hooks->expect(skb, protoff, dataoff, dptr, datalen, |
| 1610 | exp, matchoff, matchlen); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1611 | else { |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1612 | if (nf_ct_expect_related(exp) != 0) { |
| 1613 | nf_ct_helper_log(skb, ct, "cannot add expectation"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1614 | ret = NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1615 | } else |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1616 | ret = NF_ACCEPT; |
| 1617 | } |
| 1618 | nf_ct_expect_put(exp); |
| 1619 | |
| 1620 | store_cseq: |
| 1621 | if (ret == NF_ACCEPT) |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1622 | ct_sip_info->register_cseq = cseq; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1626 | static int process_register_response(struct sk_buff *skb, unsigned int protoff, |
| 1627 | unsigned int dataoff, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1628 | const char **dptr, unsigned int *datalen, |
| 1629 | unsigned int cseq, unsigned int code) |
| 1630 | { |
| 1631 | enum ip_conntrack_info ctinfo; |
| 1632 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1633 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1634 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 1635 | union nf_inet_addr addr; |
| 1636 | __be16 port; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1637 | u8 proto; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1638 | unsigned int matchoff, matchlen, coff = 0; |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1639 | unsigned int expires = 0; |
| 1640 | int in_contact = 0, ret; |
| 1641 | |
| 1642 | /* According to RFC 3261, "UAs MUST NOT send a new registration until |
| 1643 | * they have received a final response from the registrar for the |
| 1644 | * previous one or the previous REGISTER request has timed out". |
| 1645 | * |
| 1646 | * However, some servers fail to detect retransmissions and send late |
| 1647 | * responses, so we store the sequence number of the last valid |
| 1648 | * request and compare it here. |
| 1649 | */ |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 1650 | if (ct_sip_info->register_cseq != cseq) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1651 | return NF_ACCEPT; |
| 1652 | |
| 1653 | if (code >= 100 && code <= 199) |
| 1654 | return NF_ACCEPT; |
| 1655 | if (code < 200 || code > 299) |
| 1656 | goto flush; |
| 1657 | |
| 1658 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES, |
| 1659 | &matchoff, &matchlen) > 0) |
| 1660 | expires = simple_strtoul(*dptr + matchoff, NULL, 10); |
| 1661 | |
| 1662 | while (1) { |
| 1663 | unsigned int c_expires = expires; |
| 1664 | |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1665 | ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1666 | SIP_HDR_CONTACT, &in_contact, |
| 1667 | &matchoff, &matchlen, |
| 1668 | &addr, &port); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1669 | if (ret < 0) { |
| 1670 | nf_ct_helper_log(skb, ct, "cannot parse contact"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1671 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1672 | } else if (ret == 0) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1673 | break; |
| 1674 | |
| 1675 | /* We don't support third-party registrations */ |
| 1676 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr)) |
| 1677 | continue; |
| 1678 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1679 | if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, |
| 1680 | *datalen, &proto) == 0) |
| 1681 | continue; |
| 1682 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1683 | ret = ct_sip_parse_numerical_param(ct, *dptr, |
| 1684 | matchoff + matchlen, |
| 1685 | *datalen, "expires=", |
| 1686 | NULL, NULL, &c_expires); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1687 | if (ret < 0) { |
| 1688 | nf_ct_helper_log(skb, ct, "cannot parse expires"); |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1689 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1690 | } |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1691 | if (c_expires == 0) |
| 1692 | break; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1693 | if (refresh_signalling_expectation(ct, &addr, proto, port, |
| 1694 | c_expires)) |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1695 | return NF_ACCEPT; |
| 1696 | } |
| 1697 | |
| 1698 | flush: |
| 1699 | flush_expectations(ct, false); |
Patrick McHardy | 595a8ec | 2008-03-25 20:22:53 -0700 | [diff] [blame] | 1700 | return NF_ACCEPT; |
| 1701 | } |
| 1702 | |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1703 | static const struct sip_handler sip_handlers[] = { |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 1704 | SIP_HANDLER("INVITE", process_invite_request, process_invite_response), |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1705 | SIP_HANDLER("UPDATE", process_sdp, process_update_response), |
Patrick McHardy | 595a8ec | 2008-03-25 20:22:53 -0700 | [diff] [blame] | 1706 | SIP_HANDLER("ACK", process_sdp, NULL), |
| 1707 | SIP_HANDLER("PRACK", process_sdp, process_prack_response), |
Patrick McHardy | 9467ee3 | 2008-03-25 20:24:04 -0700 | [diff] [blame] | 1708 | SIP_HANDLER("BYE", process_bye_request, NULL), |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 1709 | SIP_HANDLER("REGISTER", process_register_request, process_register_response), |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1710 | }; |
| 1711 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1712 | static int process_sip_response(struct sk_buff *skb, unsigned int protoff, |
| 1713 | unsigned int dataoff, |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1714 | const char **dptr, unsigned int *datalen) |
| 1715 | { |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1716 | enum ip_conntrack_info ctinfo; |
| 1717 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1718 | unsigned int matchoff, matchlen, matchend; |
| 1719 | unsigned int code, cseq, i; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1720 | |
| 1721 | if (*datalen < strlen("SIP/2.0 200")) |
| 1722 | return NF_ACCEPT; |
| 1723 | code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1724 | if (!code) { |
| 1725 | nf_ct_helper_log(skb, ct, "cannot get code"); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1726 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1727 | } |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1728 | |
| 1729 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1730 | &matchoff, &matchlen) <= 0) { |
| 1731 | nf_ct_helper_log(skb, ct, "cannot parse cseq"); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1732 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1733 | } |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1734 | cseq = simple_strtoul(*dptr + matchoff, NULL, 10); |
Christophe Leroy | 0d35d081 | 2016-08-03 12:41:40 +0200 | [diff] [blame] | 1735 | if (!cseq && *(*dptr + matchoff) != '0') { |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1736 | nf_ct_helper_log(skb, ct, "cannot get cseq"); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1737 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1738 | } |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1739 | matchend = matchoff + matchlen + 1; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1740 | |
| 1741 | for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) { |
Alexey Dobriyan | 66bf791 | 2008-09-07 18:19:25 -0700 | [diff] [blame] | 1742 | const struct sip_handler *handler; |
| 1743 | |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1744 | handler = &sip_handlers[i]; |
| 1745 | if (handler->response == NULL) |
| 1746 | continue; |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1747 | if (*datalen < matchend + handler->len || |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 1748 | strncasecmp(*dptr + matchend, handler->method, handler->len)) |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1749 | continue; |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1750 | return handler->response(skb, protoff, dataoff, dptr, datalen, |
Patrick McHardy | 3b6b9fa | 2010-02-11 12:23:53 +0100 | [diff] [blame] | 1751 | cseq, code); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1752 | } |
| 1753 | return NF_ACCEPT; |
| 1754 | } |
| 1755 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1756 | static int process_sip_request(struct sk_buff *skb, unsigned int protoff, |
| 1757 | unsigned int dataoff, |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1758 | const char **dptr, unsigned int *datalen) |
| 1759 | { |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1760 | enum ip_conntrack_info ctinfo; |
| 1761 | struct nf_conn *ct = nf_ct_get(skb, &ctinfo); |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 1762 | struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct); |
| 1763 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1764 | unsigned int matchoff, matchlen; |
| 1765 | unsigned int cseq, i; |
Kevin Cernekee | 7266507 | 2012-12-17 18:33:58 +0000 | [diff] [blame] | 1766 | union nf_inet_addr addr; |
| 1767 | __be16 port; |
| 1768 | |
| 1769 | /* Many Cisco IP phones use a high source port for SIP requests, but |
| 1770 | * listen for the response on port 5060. If we are the local |
| 1771 | * router for one of these phones, save the port number from the |
| 1772 | * Via: header so that nf_nat_sip can redirect the responses to |
| 1773 | * the correct port. |
| 1774 | */ |
| 1775 | if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen, |
| 1776 | SIP_HDR_VIA_UDP, NULL, &matchoff, |
| 1777 | &matchlen, &addr, &port) > 0 && |
| 1778 | port != ct->tuplehash[dir].tuple.src.u.udp.port && |
| 1779 | nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3)) |
| 1780 | ct_sip_info->forced_dport = port; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1781 | |
| 1782 | for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) { |
Alexey Dobriyan | 66bf791 | 2008-09-07 18:19:25 -0700 | [diff] [blame] | 1783 | const struct sip_handler *handler; |
| 1784 | |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1785 | handler = &sip_handlers[i]; |
| 1786 | if (handler->request == NULL) |
| 1787 | continue; |
Ulrich Weber | 444f901 | 2016-10-24 18:07:23 +0200 | [diff] [blame] | 1788 | if (*datalen < handler->len + 2 || |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 1789 | strncasecmp(*dptr, handler->method, handler->len)) |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1790 | continue; |
Ulrich Weber | 444f901 | 2016-10-24 18:07:23 +0200 | [diff] [blame] | 1791 | if ((*dptr)[handler->len] != ' ' || |
| 1792 | !isalpha((*dptr)[handler->len+1])) |
| 1793 | continue; |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1794 | |
| 1795 | if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ, |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1796 | &matchoff, &matchlen) <= 0) { |
| 1797 | nf_ct_helper_log(skb, ct, "cannot parse cseq"); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1798 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1799 | } |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1800 | cseq = simple_strtoul(*dptr + matchoff, NULL, 10); |
Christophe Leroy | 0d35d081 | 2016-08-03 12:41:40 +0200 | [diff] [blame] | 1801 | if (!cseq && *(*dptr + matchoff) != '0') { |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1802 | nf_ct_helper_log(skb, ct, "cannot get cseq"); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1803 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1804 | } |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1805 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1806 | return handler->request(skb, protoff, dataoff, dptr, datalen, |
| 1807 | cseq); |
Patrick McHardy | 30f33e6 | 2008-03-25 20:22:20 -0700 | [diff] [blame] | 1808 | } |
| 1809 | return NF_ACCEPT; |
| 1810 | } |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 1811 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1812 | static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1813 | unsigned int protoff, unsigned int dataoff, |
| 1814 | const char **dptr, unsigned int *datalen) |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1815 | { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1816 | const struct nf_nat_sip_hooks *hooks; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1817 | int ret; |
| 1818 | |
Tyler Wear | d2aeec0 | 2013-09-20 12:12:07 -0700 | [diff] [blame] | 1819 | if (nf_ct_disable_sip_alg) |
| 1820 | return NF_ACCEPT; |
Rasmus Villemoes | 1808274 | 2014-10-13 15:54:31 -0700 | [diff] [blame] | 1821 | if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0) |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1822 | ret = process_sip_request(skb, protoff, dataoff, dptr, datalen); |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1823 | else |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1824 | ret = process_sip_response(skb, protoff, dataoff, dptr, datalen); |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1825 | |
Patrick McHardy | 9a66482 | 2012-08-26 19:14:25 +0200 | [diff] [blame] | 1826 | if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 1827 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 1828 | if (hooks && !hooks->msg(skb, protoff, dataoff, |
| 1829 | dptr, datalen)) { |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1830 | nf_ct_helper_log(skb, ct, "cannot NAT SIP message"); |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1831 | ret = NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1832 | } |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | return ret; |
| 1836 | } |
| 1837 | |
| 1838 | static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff, |
| 1839 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
| 1840 | { |
| 1841 | struct tcphdr *th, _tcph; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1842 | unsigned int dataoff; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1843 | unsigned int matchoff, matchlen, clen; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1844 | const char *dptr, *end; |
| 1845 | s16 diff, tdiff = 0; |
Simon Horman | 7874896 | 2010-09-21 21:17:30 +0000 | [diff] [blame] | 1846 | int ret = NF_ACCEPT; |
Patrick McHardy | e6e4d9e | 2011-05-16 14:45:39 +0200 | [diff] [blame] | 1847 | bool term; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1848 | unsigned int datalen = 0, msglen = 0, origlen = 0; |
| 1849 | unsigned int dataoff_orig = 0; |
| 1850 | unsigned int splitlen, oldlen, oldlen1; |
| 1851 | struct sip_list *sip_entry = NULL; |
| 1852 | bool skip_sip_process = false; |
| 1853 | bool do_not_process = false; |
| 1854 | bool skip = false; |
| 1855 | bool skb_is_combined = false; |
| 1856 | enum ip_conntrack_dir dir = IP_CT_DIR_MAX; |
| 1857 | struct sk_buff *combined_skb = NULL; |
| 1858 | bool content_len_exists = 1; |
| 1859 | |
| 1860 | packet_count++; |
| 1861 | pr_debug("packet count %d\n", packet_count); |
| 1862 | |
| 1863 | if (nf_ct_disable_sip_alg) |
| 1864 | return NF_ACCEPT; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1865 | |
| 1866 | if (ctinfo != IP_CT_ESTABLISHED && |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 1867 | ctinfo != IP_CT_ESTABLISHED_REPLY) |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1868 | return NF_ACCEPT; |
| 1869 | |
| 1870 | /* No Data ? */ |
| 1871 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
| 1872 | if (th == NULL) |
| 1873 | return NF_ACCEPT; |
| 1874 | dataoff = protoff + th->doff * 4; |
| 1875 | if (dataoff >= skb->len) |
| 1876 | return NF_ACCEPT; |
| 1877 | |
| 1878 | nf_ct_refresh(ct, skb, sip_timeout * HZ); |
| 1879 | |
Patrick McHardy | a1d7c1b | 2010-05-14 21:18:17 +0200 | [diff] [blame] | 1880 | if (unlikely(skb_linearize(skb))) |
| 1881 | return NF_DROP; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1882 | |
| 1883 | dptr = skb->data + dataoff; |
| 1884 | datalen = skb->len - dataoff; |
| 1885 | if (datalen < strlen("SIP/2.0 200")) |
| 1886 | return NF_ACCEPT; |
| 1887 | |
Mohammed Javid | 3cfcd57 | 2018-01-15 16:53:18 +0530 | [diff] [blame] | 1888 | /* Check if the header contains SIP version */ |
| 1889 | if (!strnstr(dptr, "SIP/2.0", datalen)) |
| 1890 | return NF_ACCEPT; |
| 1891 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1892 | /* here we save the original datalength and data offset of the skb, this |
| 1893 | * is needed later to split combined skbs |
| 1894 | */ |
| 1895 | oldlen1 = skb->len - protoff; |
| 1896 | dataoff_orig = dataoff; |
| 1897 | |
| 1898 | if (!ct) |
| 1899 | return NF_DROP; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1900 | while (1) { |
| 1901 | if (ct_sip_get_header(ct, dptr, 0, datalen, |
| 1902 | SIP_HDR_CONTENT_LENGTH, |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1903 | &matchoff, &matchlen) <= 0){ |
| 1904 | if (nf_ct_enable_sip_segmentation) { |
| 1905 | do_not_process = true; |
| 1906 | content_len_exists = 0; |
| 1907 | goto destination; |
| 1908 | } else { |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1909 | break; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1910 | } |
| 1911 | } |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1912 | |
| 1913 | clen = simple_strtoul(dptr + matchoff, (char **)&end, 10); |
| 1914 | if (dptr + matchoff == end) |
| 1915 | break; |
| 1916 | |
Patrick McHardy | e6e4d9e | 2011-05-16 14:45:39 +0200 | [diff] [blame] | 1917 | term = false; |
| 1918 | for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) { |
| 1919 | if (end[0] == '\r' && end[1] == '\n' && |
| 1920 | end[2] == '\r' && end[3] == '\n') { |
| 1921 | term = true; |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
| 1925 | if (!term) |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1926 | break; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1927 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1928 | end += strlen("\r\n\r\n") + clen; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1929 | destination: |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1930 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1931 | if (content_len_exists == 0) { |
| 1932 | origlen = datalen; |
| 1933 | msglen = origlen; |
| 1934 | } else { |
| 1935 | origlen = end - dptr; |
| 1936 | msglen = origlen; |
| 1937 | } |
| 1938 | pr_debug("mslgen %d datalen %d\n", msglen, datalen); |
| 1939 | dir = CTINFO2DIR(ctinfo); |
| 1940 | combined_skb = skb; |
| 1941 | if (nf_ct_enable_sip_segmentation) { |
| 1942 | /* Segmented Packet */ |
| 1943 | if (msglen > datalen) { |
| 1944 | skip = sip_save_segment_info(ct, skb, msglen, |
| 1945 | datalen, dptr, |
| 1946 | ctinfo); |
| 1947 | if (!skip) |
| 1948 | return NF_QUEUE; |
| 1949 | } |
| 1950 | /* Traverse list to find prev segment */ |
| 1951 | /*Traverse the list if list non empty */ |
| 1952 | if (((&ct->sip_segment_list)->next) != |
| 1953 | (&ct->sip_segment_list)) { |
| 1954 | /* Combine segments if they are fragments of |
| 1955 | * the same message. |
| 1956 | */ |
| 1957 | sip_entry = sip_coalesce_segments(ct, &skb, |
| 1958 | dataoff, |
| 1959 | &combined_skb, |
| 1960 | &skip_sip_process, |
| 1961 | do_not_process, |
| 1962 | ctinfo, |
| 1963 | &skb_is_combined); |
| 1964 | sip_update_params(dir, &msglen, &origlen, &dptr, |
| 1965 | &datalen, |
| 1966 | skb_is_combined, ct); |
| 1967 | |
| 1968 | if (skip_sip_process) |
| 1969 | goto here; |
| 1970 | } else if (do_not_process) { |
| 1971 | goto here; |
| 1972 | } |
| 1973 | } else if (msglen > datalen) { |
Patrick McHardy | 3a7b21e | 2013-04-05 08:13:30 +0000 | [diff] [blame] | 1974 | return NF_ACCEPT; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1975 | } |
| 1976 | /* process the combined skb having the complete SIP message */ |
| 1977 | ret = process_sip_msg(combined_skb, ct, protoff, dataoff, |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 1978 | &dptr, &msglen); |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1979 | |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 1980 | /* process_sip_* functions report why this packet is dropped */ |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 1981 | if (ret != NF_ACCEPT) |
| 1982 | break; |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 1983 | sip_calculate_parameters(&diff, &tdiff, &dataoff, &dptr, |
| 1984 | &datalen, msglen, origlen); |
| 1985 | if (nf_ct_enable_sip_segmentation && skb_is_combined) |
| 1986 | break; |
| 1987 | } |
| 1988 | if (skb_is_combined) { |
| 1989 | /* once combined skb is processed, split the skbs again The |
| 1990 | * length to split at is the same as length of first skb. Any |
| 1991 | * changes in the combined skb length because of SIP processing |
| 1992 | * will reflect in the second fragment |
| 1993 | */ |
| 1994 | splitlen = (dir == IP_CT_DIR_ORIGINAL) ? |
| 1995 | ct->segment.skb_len[0] : ct->segment.skb_len[1]; |
| 1996 | oldlen = combined_skb->len - protoff; |
| 1997 | skb_split(combined_skb, skb, splitlen); |
| 1998 | /* Headers need to be recalculated since during SIP processing |
| 1999 | * headers are calculated based on the change in length of the |
| 2000 | * combined message |
| 2001 | */ |
| 2002 | recalc_header(combined_skb, splitlen, oldlen, protoff); |
| 2003 | /* Reinject the first skb now that the processing is complete */ |
| 2004 | if (sip_entry) { |
| 2005 | nf_reinject(sip_entry->entry, NF_ACCEPT); |
| 2006 | kfree(sip_entry); |
| 2007 | } |
| 2008 | skb->len = (oldlen1 + protoff) + tdiff - dataoff_orig; |
| 2009 | /* After splitting, push the headers back to the first skb which |
| 2010 | * were removed before combining the skbs.This moves the skb |
| 2011 | * begin pointer back to the beginning of its headers |
| 2012 | */ |
| 2013 | skb_push(skb, dataoff_orig); |
| 2014 | /* Since the length of this second segment willbe affected |
| 2015 | * because of SIP processing,we need to recalculate its header |
| 2016 | * as well. |
| 2017 | */ |
| 2018 | recalc_header(skb, skb->len, oldlen1, protoff); |
| 2019 | /* Now that the processing is done and the first skb reinjected. |
| 2020 | * We allow addition of fragmented skbs to the list for this |
| 2021 | * direction |
| 2022 | */ |
| 2023 | if (dir == IP_CT_DIR_ORIGINAL) |
| 2024 | ct->sip_original_dir = 0; |
| 2025 | else |
| 2026 | ct->sip_reply_dir = 0; |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
Mohammed Javid | f4530ef | 2017-11-20 20:15:46 +0530 | [diff] [blame] | 2029 | here: |
| 2030 | |
| 2031 | if (ret == NF_ACCEPT && ct && ct->status & IPS_NAT_MASK) { |
holger@eitzenberger.org | 180cf72 | 2013-09-30 17:07:28 +0200 | [diff] [blame] | 2032 | const struct nf_nat_sip_hooks *hooks; |
| 2033 | |
| 2034 | hooks = rcu_dereference(nf_nat_sip_hooks); |
| 2035 | if (hooks) |
| 2036 | hooks->seq_adjust(skb, protoff, tdiff); |
Patrick McHardy | 48f8ac2 | 2010-02-11 12:29:38 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 2039 | return ret; |
| 2040 | } |
| 2041 | |
| 2042 | static int sip_help_udp(struct sk_buff *skb, unsigned int protoff, |
| 2043 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2044 | { |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2045 | unsigned int dataoff, datalen; |
| 2046 | const char *dptr; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2047 | |
| 2048 | /* No Data ? */ |
| 2049 | dataoff = protoff + sizeof(struct udphdr); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 2050 | if (dataoff >= skb->len) |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2051 | return NF_ACCEPT; |
| 2052 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 2053 | nf_ct_refresh(ct, skb, sip_timeout * HZ); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2054 | |
Patrick McHardy | a1d7c1b | 2010-05-14 21:18:17 +0200 | [diff] [blame] | 2055 | if (unlikely(skb_linearize(skb))) |
| 2056 | return NF_DROP; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2057 | |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 2058 | dptr = skb->data + dataoff; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 2059 | datalen = skb->len - dataoff; |
Patrick McHardy | 779382e | 2008-03-25 20:17:36 -0700 | [diff] [blame] | 2060 | if (datalen < strlen("SIP/2.0 200")) |
Patrick McHardy | 7d3dd04 | 2008-03-25 20:19:46 -0700 | [diff] [blame] | 2061 | return NF_ACCEPT; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2062 | |
Mohammed Javid | 3cfcd57 | 2018-01-15 16:53:18 +0530 | [diff] [blame] | 2063 | /* Check if the header contains SIP version */ |
| 2064 | if (!strnstr(dptr, "SIP/2.0", datalen)) |
| 2065 | return NF_ACCEPT; |
| 2066 | |
Patrick McHardy | 051966c | 2012-08-26 19:14:04 +0200 | [diff] [blame] | 2067 | return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2068 | } |
| 2069 | |
Gao Feng | 82de0be | 2016-07-18 11:39:23 +0800 | [diff] [blame] | 2070 | static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2071 | |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 2072 | static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = { |
| 2073 | [SIP_EXPECT_SIGNALLING] = { |
Patrick McHardy | b87921b | 2010-02-11 12:22:48 +0100 | [diff] [blame] | 2074 | .name = "signalling", |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 2075 | .max_expected = 1, |
| 2076 | .timeout = 3 * 60, |
| 2077 | }, |
| 2078 | [SIP_EXPECT_AUDIO] = { |
Patrick McHardy | b87921b | 2010-02-11 12:22:48 +0100 | [diff] [blame] | 2079 | .name = "audio", |
Patrick McHardy | a9c1d35 | 2008-03-25 20:25:49 -0700 | [diff] [blame] | 2080 | .max_expected = 2 * IP_CT_DIR_MAX, |
Patrick McHardy | 0f32a40 | 2008-03-25 20:25:13 -0700 | [diff] [blame] | 2081 | .timeout = 3 * 60, |
| 2082 | }, |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 2083 | [SIP_EXPECT_VIDEO] = { |
Patrick McHardy | b87921b | 2010-02-11 12:22:48 +0100 | [diff] [blame] | 2084 | .name = "video", |
Patrick McHardy | 0d0ab03 | 2008-03-25 20:26:24 -0700 | [diff] [blame] | 2085 | .max_expected = 2 * IP_CT_DIR_MAX, |
| 2086 | .timeout = 3 * 60, |
| 2087 | }, |
Patrick McHardy | 9d288df | 2010-02-11 12:30:21 +0100 | [diff] [blame] | 2088 | [SIP_EXPECT_IMAGE] = { |
| 2089 | .name = "image", |
| 2090 | .max_expected = IP_CT_DIR_MAX, |
| 2091 | .timeout = 3 * 60, |
| 2092 | }, |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 2093 | }; |
| 2094 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2095 | static void nf_conntrack_sip_fini(void) |
| 2096 | { |
Gao Feng | 82de0be | 2016-07-18 11:39:23 +0800 | [diff] [blame] | 2097 | nf_conntrack_helpers_unregister(sip, ports_c * 4); |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | static int __init nf_conntrack_sip_init(void) |
| 2101 | { |
Gao Feng | 82de0be | 2016-07-18 11:39:23 +0800 | [diff] [blame] | 2102 | int i, ret; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2103 | |
Tyler Wear | 2e6eecd | 2014-05-16 11:58:23 -0700 | [diff] [blame] | 2104 | sip_sysctl_header = register_net_sysctl(&init_net, "net/netfilter", |
| 2105 | sip_sysctl_tbl); |
Tyler Wear | d2aeec0 | 2013-09-20 12:12:07 -0700 | [diff] [blame] | 2106 | if (!sip_sysctl_header) |
| 2107 | pr_debug("nf_ct_sip:Unable to register SIP systbl\n"); |
| 2108 | |
| 2109 | if (nf_ct_disable_sip_alg) |
| 2110 | pr_debug("nf_ct_sip: SIP ALG disabled\n"); |
| 2111 | else |
| 2112 | pr_debug("nf_ct_sip: SIP ALG enabled\n"); |
| 2113 | |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2114 | if (ports_c == 0) |
| 2115 | ports[ports_c++] = SIP_PORT; |
| 2116 | |
| 2117 | for (i = 0; i < ports_c; i++) { |
Gao Feng | 82de0be | 2016-07-18 11:39:23 +0800 | [diff] [blame] | 2118 | nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP, "sip", |
| 2119 | SIP_PORT, ports[i], i, sip_exp_policy, |
| 2120 | SIP_EXPECT_MAX, |
| 2121 | sizeof(struct nf_ct_sip_master), sip_help_udp, |
| 2122 | NULL, THIS_MODULE); |
| 2123 | nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP, "sip", |
| 2124 | SIP_PORT, ports[i], i, sip_exp_policy, |
| 2125 | SIP_EXPECT_MAX, |
| 2126 | sizeof(struct nf_ct_sip_master), sip_help_tcp, |
| 2127 | NULL, THIS_MODULE); |
| 2128 | nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP, "sip", |
| 2129 | SIP_PORT, ports[i], i, sip_exp_policy, |
| 2130 | SIP_EXPECT_MAX, |
| 2131 | sizeof(struct nf_ct_sip_master), sip_help_udp, |
| 2132 | NULL, THIS_MODULE); |
| 2133 | nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP, "sip", |
| 2134 | SIP_PORT, ports[i], i, sip_exp_policy, |
| 2135 | SIP_EXPECT_MAX, |
| 2136 | sizeof(struct nf_ct_sip_master), sip_help_tcp, |
| 2137 | NULL, THIS_MODULE); |
| 2138 | } |
Patrick McHardy | f5b321b | 2010-02-11 12:26:19 +0100 | [diff] [blame] | 2139 | |
Gao Feng | 82de0be | 2016-07-18 11:39:23 +0800 | [diff] [blame] | 2140 | ret = nf_conntrack_helpers_register(sip, ports_c * 4); |
| 2141 | if (ret < 0) { |
| 2142 | pr_err("failed to register helpers\n"); |
| 2143 | return ret; |
Patrick McHardy | 9fafcd7 | 2006-12-02 22:09:57 -0800 | [diff] [blame] | 2144 | } |
| 2145 | return 0; |
| 2146 | } |
| 2147 | |
| 2148 | module_init(nf_conntrack_sip_init); |
| 2149 | module_exit(nf_conntrack_sip_fini); |