// SPDX-License-Identifier: GPL-2.0-or-later /* * dvb_net.c * * Copyright (C) 2001 Convergence integrated media GmbH * Ralph Metzler <ralph@convergence.de> * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de> * * ULE Decapsulation code: * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH. * and Department of Scientific Computing * Paris Lodron University of Salzburg. * Hilmar Linder <hlinder@cosy.sbg.ac.at> * and Wolfram Stering <wstering@cosy.sbg.ac.at> * * ULE Decaps according to RFC 4326.
*/
/* * ULE ChangeLog: * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt * * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt: * ULE Extension header handling. * Bugreports by Moritz Vieth and Hanno Tersteegen, * Fraunhofer Institute for Open Communication Systems * Competence Center for Advanced Satellite Communications. * Bugfixes and robustness improvements. * Filtering on dest MAC addresses, if present (D-Bit = 0) * DVB_ULE_DEBUG compile-time option. * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by * Christian Praehauser <cpraehaus@cosy.sbg.ac.at>, * Paris Lodron University of Salzburg.
*/
/* * FIXME / TODO (dvb_net.c): * * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero. *
*/
struct dvb_net_priv { int in_use;
u16 pid; struct net_device *net; struct dvb_net *host; struct dmx_demux *demux; struct dmx_section_feed *secfeed; struct dmx_section_filter *secfilter; struct dmx_ts_feed *tsfeed; int multi_num; struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX]; unsignedchar multi_macs[DVB_NET_MULTICAST_MAX][6]; int rx_mode; #define RX_MODE_UNI 0 #define RX_MODE_MULTI 1 #define RX_MODE_ALL_MULTI 2 #define RX_MODE_PROMISC 3 struct work_struct set_multicast_list_wq; struct work_struct restart_net_feed_wq; unsignedchar feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */ int need_pusi; /* Set to 1, if synchronization on PUSI required. */ unsignedchar tscc; /* TS continuity counter after sync on PUSI. */ struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */ unsignedchar *ule_next_hdr; /* Pointer into skb to next ULE extension header. */ unsignedshort ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */ unsignedshort ule_sndu_type; /* ULE SNDU type field, complete. */ unsignedchar ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */ unsignedchar ule_dbit; /* Whether the DestMAC address present
* or not (bit is set). */ unsignedchar ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */ int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */ unsignedlong ts_count; /* Current ts cell counter. */ struct mutex mutex;
};
/* * Determine the packet's protocol ID. The rule here is that we * assume 802.3 if the type field is short enough to be a length. * This is normal practice and works for any 'now in use' protocol. * * stolen from eth.c out of the linux kernel, hacked for dvb-device * by Michael Holzt <kju@debian.org>
*/ static __be16 dvb_net_eth_type_trans(struct sk_buff *skb, struct net_device *dev)
{ struct ethhdr *eth; unsignedchar *rawp;
skb_reset_mac_header(skb);
skb_pull(skb,dev->hard_header_len);
eth = eth_hdr(skb);
if (*eth->h_dest & 1) { if(ether_addr_equal(eth->h_dest,dev->broadcast))
skb->pkt_type=PACKET_BROADCAST; else
skb->pkt_type=PACKET_MULTICAST;
}
if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) return eth->h_proto;
rawp = skb->data;
/* * This is a magic hack to spot IPX packets. Older Novell breaks * the protocol design and runs IPX over 802.3 without an 802.2 LLC * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This * won't work for fault tolerant netware but does for the rest.
*/ if (*(unsignedshort *)rawp == 0xFFFF) return htons(ETH_P_802_3);
/* * Real 802.2 LLC
*/ return htons(ETH_P_802_2);
}
staticint ule_bridged_sndu( struct dvb_net_priv *p )
{ struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr; if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) { int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data); /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */ if(framelen != ntohs(hdr->h_proto)) { return -1;
}
} /* Note: * From RFC4326: * "A bridged SNDU is a Mandatory Extension Header of Type 1. * It must be the final (or only) extension header specified in the header chain of a SNDU." * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
*/
p->ule_bridged = 1; return 0;
}
/* * Handle ULE extension headers. * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding. * Returns: >= 0: nr. of bytes consumed by next extension header * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
*/ staticint handle_one_ule_extension( struct dvb_net_priv *p )
{ /* Table of mandatory extension header handlers. The header type is the index. */ staticint (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
{ [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
/* Table of optional extension header handlers. The header type is the index. */ staticint (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
{ [0] = ule_exthdr_padding, [1] = NULL, };
/* Discriminate mandatory and optional extension headers. */ if (hlen == 0) { /* Mandatory extension header */ if (ule_mandatory_ext_handlers[htype]) {
ext_len = ule_mandatory_ext_handlers[htype]( p ); if(ext_len >= 0) {
p->ule_next_hdr += ext_len; if (!p->ule_bridged) {
p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
p->ule_next_hdr += 2;
} else {
p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN))); /* This assures the extension handling loop will terminate. */
}
} // else: extension handler failed or SNDU should be discarded
} else
ext_len = -1; /* SNDU has to be discarded. */
} else { /* Optional extension header. Calculate the length. */
ext_len = hlen << 1; /* Process the optional extension header according to its type. */ if (ule_optional_ext_handlers[htype])
(void)ule_optional_ext_handlers[htype]( p );
p->ule_next_hdr += ext_len;
p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) ); /* * note: the length of the next header type is included in the * length of THIS optional extension header
*/
}
/* Drop partly decoded SNDU, reset state, resync on PUSI. */ if (h->priv->ule_skb) {
dev_kfree_skb(h->priv->ule_skb); /* Prepare for next SNDU. */
h->dev->stats.rx_errors++;
h->dev->stats.rx_frame_errors++;
}
reset_ule(h->priv);
h->priv->need_pusi = 1;
/* Continue with next TS cell. */
h->ts += TS_SZ;
h->priv->ts_count++; return 1;
}
h->ts_remain = 184;
h->from_where = h->ts + 4;
return 0;
}
staticint dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
{ if (h->ts[1] & TS_PUSI) { /* Find beginning of first ULE SNDU in current TS cell. */ /* Synchronize continuity counter. */
h->priv->tscc = h->ts[3] & 0x0F; /* There is a pointer field here. */ if (h->ts[4] > h->ts_remain) {
pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
h->priv->ts_count, h->ts[4]);
h->ts += TS_SZ;
h->priv->ts_count++; return 1;
} /* Skip to destination of pointer field. */
h->from_where = &h->ts[5] + h->ts[4];
h->ts_remain -= 1 + h->ts[4];
h->skipped = 0;
} else {
h->skipped++;
h->ts += TS_SZ;
h->priv->ts_count++; return 1;
}
return 0;
}
staticint dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
{ /* Check continuity counter. */ if ((h->ts[3] & 0x0F) == h->priv->tscc)
h->priv->tscc = (h->priv->tscc + 1) & 0x0F; else { /* TS discontinuity handling: */
pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
h->priv->ts_count, h->ts[3] & 0x0F,
h->priv->tscc); /* Drop partly decoded SNDU, reset state, resync on PUSI. */ if (h->priv->ule_skb) {
dev_kfree_skb(h->priv->ule_skb); /* Prepare for next SNDU. */ // reset_ule(h->priv); moved to below.
h->dev->stats.rx_errors++;
h->dev->stats.rx_frame_errors++;
}
reset_ule(h->priv); /* skip to next PUSI. */
h->priv->need_pusi = 1; return 1;
} /* * If we still have an incomplete payload, but PUSI is * set; some TS cells are missing. * This is only possible here, if we missed exactly 16 TS * cells (continuity counter wrap).
*/ if (h->ts[1] & TS_PUSI) { if (!h->priv->need_pusi) { if (!(*h->from_where < (h->ts_remain-1)) ||
*h->from_where != h->priv->ule_sndu_remain) { /* * Pointer field is invalid. * Drop this TS cell and any started ULE SNDU.
*/
pr_warn("%lu: Invalid pointer field: %u.\n",
h->priv->ts_count,
*h->from_where);
/* * Drop partly decoded SNDU, reset state, * resync on PUSI.
*/ if (h->priv->ule_skb) {
h->error = true;
dev_kfree_skb(h->priv->ule_skb);
}
if (h->priv->ule_sndu_remain > 183) { /* * Current SNDU lacks more data than there * could be available in the current TS cell.
*/
h->dev->stats.rx_errors++;
h->dev->stats.rx_length_errors++;
pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n",
h->priv->ts_count,
h->priv->ule_sndu_remain,
h->ts[4], h->ts_remain);
dev_kfree_skb(h->priv->ule_skb); /* Prepare for next SNDU. */
reset_ule(h->priv); /* * Resync: go to where pointer field points to: * start of next ULE SNDU.
*/
h->from_where += h->ts[4];
h->ts_remain -= h->ts[4];
}
} return 0;
}
/* * Start a new payload with skb. * Find ULE header. It is only guaranteed that the * length field (2 bytes) is contained in the current * TS. * Check h.ts_remain has to be >= 2 here.
*/ staticint dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
{ if (h->ts_remain < 2) {
pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n",
h->ts_remain);
h->priv->ule_sndu_len = 0;
h->priv->need_pusi = 1;
h->ts += TS_SZ; return 1;
}
if (!h->priv->ule_sndu_len) { /* Got at least two bytes, thus extrace the SNDU length. */
h->priv->ule_sndu_len = h->from_where[0] << 8 |
h->from_where[1]; if (h->priv->ule_sndu_len & 0x8000) { /* D-Bit is set: no dest mac present. */
h->priv->ule_sndu_len &= 0x7FFF;
h->priv->ule_dbit = 1;
} else
h->priv->ule_dbit = 0;
h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2; /* * State of current TS: * h->ts_remain (remaining bytes in the current TS cell) * 0 ule_type is not available now, we need the next TS cell * 1 the first byte of the ule_type is present * >=2 full ULE header present, maybe some payload data as well.
*/ switch (h->ts_remain) { case 1:
h->priv->ule_sndu_remain--;
h->priv->ule_sndu_type = h->from_where[0] << 8;
/* first byte of ule_type is set. */
h->priv->ule_sndu_type_1 = 1;
h->ts_remain -= 1;
h->from_where += 1;
fallthrough; case 0:
h->new_ts = 1;
h->ts += TS_SZ;
h->priv->ts_count++; return 1;
default: /* complete ULE header is present in current TS. */ /* Extract ULE type field. */ if (h->priv->ule_sndu_type_1) {
h->priv->ule_sndu_type_1 = 0;
h->priv->ule_sndu_type |= h->from_where[0];
h->from_where += 1; /* points to payload start. */
h->ts_remain -= 1;
} else { /* Complete type is present in new TS. */
h->priv->ule_sndu_type = h->from_where[0] << 8 |
h->from_where[1];
h->from_where += 2; /* points to payload start. */
h->ts_remain -= 2;
} break;
}
/* * Allocate the skb (decoder target buffer) with the correct size, * as follows: * * prepare for the largest case: bridged SNDU with MAC address * (dbit = 0).
*/
h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
ETH_HLEN + ETH_ALEN); if (!h->priv->ule_skb) {
pr_notice("%s: Memory squeeze, dropping packet.\n",
h->dev->name);
h->dev->stats.rx_dropped++; return -1;
}
/* This includes the CRC32 _and_ dest mac, if !dbit. */
h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
h->priv->ule_skb->dev = h->dev; /* * Leave space for Ethernet or bridged SNDU header * (eth hdr plus one MAC addr).
*/
skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
/* * The destination MAC address is the next data in the skb. It comes * before any extension headers. * * Check if the payload of this SNDU should be passed up the stack.
*/ if (h->priv->rx_mode == RX_MODE_PROMISC) return 0;
if (h->priv->ule_skb->data[0] & 0x01) { /* multicast or broadcast */ if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) { /* multicast */ if (h->priv->rx_mode == RX_MODE_MULTI) { int i;
for (i = 0; i < h->priv->multi_num &&
!ether_addr_equal(h->priv->ule_skb->data,
h->priv->multi_macs[i]);
i++)
; if (i == h->priv->multi_num) return 1;
} elseif (h->priv->rx_mode != RX_MODE_ALL_MULTI) return 1; /* no broadcast; */ /* * else: * all multicast mode: accept all multicast packets
*/
} /* else: broadcast */
} elseif (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr)) return 1;
/* CRC32 was OK, so remove it from skb. */
h->priv->ule_skb->tail -= 4;
h->priv->ule_skb->len -= 4;
if (!h->priv->ule_dbit) { if (dvb_net_ule_should_drop(h)) {
netdev_dbg(h->dev, "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
h->priv->ule_skb->data, h->dev->dev_addr);
dev_kfree_skb(h->priv->ule_skb); return;
}
skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
ETH_ALEN);
skb_pull(h->priv->ule_skb, ETH_ALEN);
} else { /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */
eth_zero_addr(dest_addr);
}
/* Handle ULE Extension Headers. */ if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) { /* There is an extension header. Handle it accordingly. */ int l = handle_ule_extensions(h->priv);
if (l < 0) { /* * Mandatory extension header unknown or TEST SNDU. * Drop it.
*/
/* * For all TS cells in current buffer. * Appearently, we are called for every single TS cell.
*/ for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
h.ts < h.ts_end; /* no incr. */) { if (h.new_ts) { /* We are about to process a new TS cell. */ if (dvb_net_ule_new_ts_cell(&h)) continue;
}
/* Synchronize on PUSI, if required. */ if (h.priv->need_pusi) { if (dvb_net_ule_ts_pusi(&h)) continue;
}
if (h.new_ts) { if (dvb_net_ule_new_ts(&h)) continue;
}
/* Check if new payload needs to be started. */ if (h.priv->ule_skb == NULL) {
ret = dvb_net_ule_new_payload(&h); if (ret < 0) return; if (ret) continue;
}
/* Copy data into our current skb. */
h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
skb_put_data(h.priv->ule_skb, h.from_where, h.how_much);
h.priv->ule_sndu_remain -= h.how_much;
h.ts_remain -= h.how_much;
h.from_where += h.how_much;
/* Check for complete payload. */ if (h.priv->ule_sndu_remain <= 0) { /* Check CRC32, we've got it in our skb already. */
__be16 ulen = htons(h.priv->ule_sndu_len);
__be16 utype = htons(h.priv->ule_sndu_type); const u8 *tail; struct kvec iov[3] = {
{ &ulen, sizeof ulen },
{ &utype, sizeof utype },
{ h.priv->ule_skb->data,
h.priv->ule_skb->len - 4 }
};
u32 ule_crc = ~0L, expected_crc; if (h.priv->ule_dbit) { /* Set D-bit for CRC32 verification,
* if it was set originally. */
ulen |= htons(0x8000);
}
/* * we rely on the DVB API definition where exactly one complete * section is delivered in buffer1
*/
dvb_net_sec (dev, buffer1, buffer1_len); return 0;
}
*secfilter=NULL;
ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter); if (ret<0) {
pr_err("%s: could not get filter\n", dev->name); return ret;
}
if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
netdev_dbg(dev, "alloc secfeed\n");
ret=demux->allocate_section_feed(demux, &priv->secfeed,
dvb_net_sec_callback); if (ret<0) {
pr_err("%s: could not allocate section feed\n",
dev->name); goto error;
}
ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
if (ret<0) {
pr_err("%s: could not set section feed\n", dev->name);
priv->demux->release_section_feed(priv->demux, priv->secfeed);
priv->secfeed=NULL; goto error;
}
/* we have payloads encapsulated in TS */
netdev_dbg(dev, "alloc tsfeed\n");
ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback); if (ret < 0) {
pr_err("%s: could not allocate ts feed\n", dev->name); goto error;
}
/* Set netdevice pointer for ts decaps callback. */
priv->tsfeed->priv = (void *)dev;
ret = priv->tsfeed->set(priv->tsfeed,
priv->pid, /* pid */
TS_PACKET, /* type */
DMX_PES_OTHER, /* pes type */
timeout /* timeout */
);
if (ret < 0) {
pr_err("%s: could not set ts feed\n", dev->name);
priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
priv->tsfeed = NULL; goto error;
}
netdev_dbg(dev, "start filtering\n");
priv->tsfeed->start_filtering(priv->tsfeed);
} else
ret = -EINVAL;
error:
mutex_unlock(&priv->mutex); return ret;
}
staticint dvb_net_feed_stop(struct net_device *dev)
{ struct dvb_net_priv *priv = netdev_priv(dev); int i, ret = 0;
mutex_lock(&priv->mutex); if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) { if (priv->secfeed) { if (priv->secfeed->is_filtering) {
netdev_dbg(dev, "stop secfeed\n");
priv->secfeed->stop_filtering(priv->secfeed);
}
if (priv->secfilter) {
netdev_dbg(dev, "release secfilter\n");
priv->secfeed->release_filter(priv->secfeed,
priv->secfilter);
priv->secfilter=NULL;
}
for (i=0; i<priv->multi_num; i++) { if (priv->multi_secfilter[i]) {
netdev_dbg(dev, "release multi_filter[%d]\n",
i);
priv->secfeed->release_filter(priv->secfeed,
priv->multi_secfilter[i]);
priv->multi_secfilter[i] = NULL;
}
}
priv->demux->release_section_feed(priv->demux, priv->secfeed);
priv->secfeed = NULL;
} else
pr_err("%s: no feed to stop\n", dev->name);
} elseif (priv->feedtype == DVB_NET_FEEDTYPE_ULE) { if (priv->tsfeed) { if (priv->tsfeed->is_filtering) {
netdev_dbg(dev, "stop tsfeed\n");
priv->tsfeed->stop_filtering(priv->tsfeed);
}
priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
priv->tsfeed = NULL;
} else
pr_err("%s: no ts feed to stop\n", dev->name);
} else
ret = -EINVAL;
mutex_unlock(&priv->mutex); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.