57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/netdevice.h>
62 #include <linux/uio.h>
63 #include <asm/uaccess.h>
66 #include <linux/sched.h>
71 static int dvb_net_debug;
75 #define dprintk(x...) do { if (dvb_net_debug) printk(x); } while (0)
81 for (j = 0; j <
cnt; j++)
82 c =
crc32_be( c, iov[j].iov_base, iov[j].iov_len );
87 #define DVB_NET_MULTICAST_MAX 10
93 #define MAC_ADDR_PRINTFMT "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
94 #define MAX_ADDR_PRINTFMT_ARGS(macap) (macap)[0],(macap)[1],(macap)[2],(macap)[3],(macap)[4],(macap)[5]
96 #define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
98 static void hexdump(
const unsigned char *
buf,
unsigned short len )
100 char str[80], octet[10];
103 for (ofs = 0; ofs < len; ofs += 16) {
106 for (i = 0; i < 16; i++) {
108 sprintf( octet,
"%02x ", buf[ofs + i] );
117 for (i = 0; (i < 16) && ((i + ofs) < len); i++)
118 str[l++] =
isprint( buf[ofs + i] ) ? buf[ofs +
i] :
'.';
140 #define RX_MODE_UNI 0
141 #define RX_MODE_MULTI 1
142 #define RX_MODE_ALL_MULTI 2
143 #define RX_MODE_PROMISC 3
177 skb_reset_mac_header(skb);
199 if (*(
unsigned short *)rawp == 0xFFFF)
219 #define ULE_BRIDGED 1
221 #define ULE_OPTEXTHDR_PADDING 0
258 static int handle_one_ule_extension(
struct dvb_net_priv *p )
262 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] =
NULL, };
266 { [0] = ule_exthdr_padding, [1] =
NULL, };
275 if (ule_mandatory_ext_handlers[htype]) {
276 ext_len = ule_mandatory_ext_handlers[htype](
p );
294 if (ule_optional_ext_handlers[htype])
295 (
void)ule_optional_ext_handlers[htype]( p );
307 static int handle_ule_extensions(
struct dvb_net_priv *p )
309 int total_ext_len = 0,
l;
313 l = handle_one_ule_extension( p );
318 dprintk(
"handle_ule_extensions: ule_next_hdr=%p, ule_sndu_type=%i, "
325 return total_ext_len;
346 static void dvb_net_ule(
struct net_device *dev,
const u8 *
buf,
size_t buf_len )
349 unsigned long skipped = 0
L;
350 const u8 *
ts, *ts_end, *from_where =
NULL;
351 u8 ts_remain = 0, how_much = 0, new_ts = 1;
357 static unsigned char ule_hist[100*
TS_SZ];
358 static unsigned char *ule_where = ule_hist, ule_dump;
364 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; ) {
370 if (ule_where >= &ule_hist[100*
TS_SZ]) ule_where = ule_hist;
371 memcpy( ule_where, ts, TS_SZ );
373 hexdump( ule_where, TS_SZ );
382 priv->
ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
386 dev_kfree_skb( priv->
ule_skb );
388 dev->
stats.rx_errors++;
389 dev->
stats.rx_frame_errors++;
408 priv->
tscc = ts[3] & 0x0F;
410 if (ts[4] > ts_remain) {
412 "(pointer field %d)\n", priv->
ts_count, ts[4]);
418 from_where = &ts[5] + ts[4];
419 ts_remain -= 1 + ts[4];
431 if ((ts[3] & 0x0F) == priv->
tscc)
432 priv->
tscc = (priv->
tscc + 1) & 0x0F;
436 "expected %#x.\n", priv->
ts_count, ts[3] & 0x0F, priv->
tscc);
439 dev_kfree_skb( priv->
ule_skb );
442 dev->
stats.rx_errors++;
443 dev->
stats.rx_frame_errors++;
454 if (ts[1] & TS_PUSI) {
456 if (!(*from_where < (ts_remain-1)) || *from_where != priv->
ule_sndu_remain) {
459 "field: %u.\n", priv->
ts_count, *from_where);
468 dev->
stats.rx_errors++;
469 dev->
stats.rx_frame_errors++;
487 dev->
stats.rx_errors++;
488 dev->
stats.rx_length_errors++;
490 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
511 "bytes left in TS. Resyncing.\n", ts_remain);
520 priv->
ule_sndu_len = from_where[0] << 8 | from_where[1];
531 dev->
stats.rx_errors++;
532 dev->
stats.rx_length_errors++;
557 ts_remain -= 1; from_where += 1;
587 dev->
stats.rx_dropped++;
602 ts_remain -= how_much;
603 from_where += how_much;
611 struct kvec iov[3] = {
612 { &
ulen,
sizeof ulen },
613 { &utype,
sizeof utype },
616 u32 ule_crc = ~0
L, expected_crc;
620 ulen |=
htons(0x8000);
623 ule_crc = iov_crc32(ule_crc, iov, 3);
624 tail = skb_tail_pointer(priv->
ule_skb);
625 expected_crc = *(tail - 4) << 24 |
629 if (ule_crc != expected_crc) {
630 printk(
KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
638 if (ule_where == ule_hist) {
639 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
640 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
641 }
else if (ule_where == &ule_hist[TS_SZ]) {
642 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
643 hexdump( ule_hist, TS_SZ );
645 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
646 hexdump( ule_where - TS_SZ, TS_SZ );
651 dev->
stats.rx_errors++;
652 dev->
stats.rx_crc_errors++;
673 register int drop = 0;
675 if (priv->
ule_skb->data[0] & 0x01) {
699 dprintk(
"Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT
", dev addr: "MAC_ADDR_PRINTFMT
"\n",
700 MAX_ADDR_PRINTFMT_ARGS(priv->
ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->
dev_addr));
707 skb_copy_from_linear_data(priv->
ule_skb,
717 int l = handle_ule_extensions(priv);
753 priv->
ule_skb->protocol = dvb_net_eth_type_trans(priv->
ule_skb, dev);
758 dev->
stats.rx_packets++;
768 if (ts_remain >= 2 && *((
unsigned short *)from_where) != 0xFFFF) {
792 static int dvb_net_ts_callback(
const u8 *
buffer1,
size_t buffer1_len,
800 if (buffer1_len > 32768)
804 dvb_net_ule(dev, buffer1, buffer1_len);
809 static void dvb_net_sec(
struct net_device *dev,
819 printk(
"%s: IP/MPE packet length = %d too small.\n",
828 if ((pkt[5] & 0xfd) != 0xc1) {
831 if ((pkt[5] & 0x3c) != 0x00) {
840 if (pkt_len < 24 ||
memcmp(&pkt[12],
"\xaa\xaa\x03\0\0\0", 6)) {
856 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
865 eth = (
u8 *)
skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
866 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
876 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
885 if (pkt[12] >> 4 == 6) {
894 skb->
protocol = dvb_net_eth_type_trans(skb, dev);
901 static int dvb_net_sec_callback(
const u8 *buffer1,
size_t buffer1_len,
902 const u8 *buffer2,
size_t buffer2_len,
912 dvb_net_sec (dev, buffer1, buffer1_len);
922 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
923 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
924 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
925 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
927 static int dvb_net_filter_sec_set(
struct net_device *dev,
937 printk(
"%s: could not get filter\n", dev->
name);
941 (*secfilter)->priv=(
void *) dev;
947 (*secfilter)->filter_value[0]=0x3e;
948 (*secfilter)->filter_value[3]=mac[5];
949 (*secfilter)->filter_value[4]=mac[4];
950 (*secfilter)->filter_value[8]=mac[3];
951 (*secfilter)->filter_value[9]=mac[2];
952 (*secfilter)->filter_value[10]=mac[1];
953 (*secfilter)->filter_value[11]=mac[0];
955 (*secfilter)->filter_mask[0] = 0xff;
956 (*secfilter)->filter_mask[3] = mac_mask[5];
957 (*secfilter)->filter_mask[4] = mac_mask[4];
958 (*secfilter)->filter_mask[8] = mac_mask[3];
959 (*secfilter)->filter_mask[9] = mac_mask[2];
960 (*secfilter)->filter_mask[10] = mac_mask[1];
961 (*secfilter)->filter_mask[11]=mac_mask[0];
964 dprintk(
"%s: filter mask=%pM\n", dev->
name, mac_mask);
969 static int dvb_net_feed_start(
struct net_device *dev)
974 unsigned char *mac = (
unsigned char *) dev->
dev_addr;
979 printk(
"%s: BUG %d\n", __func__, __LINE__);
986 dprintk(
"%s: alloc secfeed\n", __func__);
988 dvb_net_sec_callback);
990 printk(
"%s: could not allocate section feed\n", dev->
name);
997 printk(
"%s: could not set section feed\n", dev->
name);
1004 dprintk(
"%s: set secfilter\n", __func__);
1005 dvb_net_filter_sec_set(dev, &priv->
secfilter, mac, mask_normal);
1011 dprintk(
"%s: set multi_secfilter[%d]\n", __func__, i);
1018 dprintk(
"%s: set multi_secfilter[0]\n", __func__);
1020 mac_allmulti, mask_allmulti);
1024 dprintk(
"%s: set secfilter\n", __func__);
1025 dvb_net_filter_sec_set(dev, &priv->
secfilter, mac, mask_promisc);
1029 dprintk(
"%s: start filtering\n", __func__);
1035 dprintk(
"%s: alloc tsfeed\n", __func__);
1038 printk(
"%s: could not allocate ts feed\n", dev->
name);
1043 priv->
tsfeed->priv = (
void *)dev;
1053 printk(
"%s: could not set ts feed\n", dev->
name);
1059 dprintk(
"%s: start filtering\n", __func__);
1069 static int dvb_net_feed_stop(
struct net_device *dev)
1078 if (priv->
secfeed->is_filtering) {
1079 dprintk(
"%s: stop secfeed\n", __func__);
1084 dprintk(
"%s: release secfilter\n", __func__);
1092 dprintk(
"%s: release multi_filter[%d]\n",
1106 if (priv->
tsfeed->is_filtering) {
1107 dprintk(
"%s: stop tsfeed\n", __func__);
1114 printk(
"%s: no ts feed to stop\n", dev->
name);
1122 static int dvb_set_mc_filter(
struct net_device *dev,
unsigned char *
addr)
1142 dvb_net_feed_stop(dev);
1144 netif_addr_lock_bh(dev);
1155 dprintk(
"%s: set_mc_list, %d entries\n",
1162 dvb_set_mc_filter(dev, ha->addr);
1165 netif_addr_unlock_bh(dev);
1166 dvb_net_feed_start(dev);
1177 static void wq_restart_net_feed (
struct work_struct *work)
1183 if (netif_running(dev)) {
1184 dvb_net_feed_stop(dev);
1185 dvb_net_feed_start(dev);
1190 static int dvb_net_set_mac (
struct net_device *dev,
void *p)
1197 if (netif_running(dev))
1204 static int dvb_net_open(
struct net_device *dev)
1209 dvb_net_feed_start(dev);
1214 static int dvb_net_stop(
struct net_device *dev)
1219 return dvb_net_feed_stop(dev);
1222 static const struct header_ops dvb_header_ops = {
1230 .ndo_open = dvb_net_open,
1231 .ndo_stop = dvb_net_stop,
1232 .ndo_start_xmit = dvb_net_tx,
1233 .ndo_set_rx_mode = dvb_net_set_multicast_list,
1234 .ndo_set_mac_address = dvb_net_set_mac,
1239 static void dvb_net_setup(
struct net_device *dev)
1250 static int get_if(
struct dvb_net *dvbnet)
1255 if (!dvbnet->state[i])
1258 if (i == DVB_NET_DEVICES_MAX)
1265 static int dvb_net_add_if(
struct dvb_net *dvbnet,
u16 pid,
u8 feedtype)
1274 if ((if_num = get_if(dvbnet)) < 0)
1283 dvbnet->
dvbdev->adapter->num, dvbnet->
dvbdev->id, if_num);
1287 dvbnet->
dvbdev->adapter->num, if_num);
1292 dvbnet->device[if_num] =
net;
1294 priv = netdev_priv(net);
1296 priv->
demux = dvbnet->demux;
1311 dvbnet->device[if_num] =
NULL;
1315 printk(
"dvb_net: created network interface %s\n", net->
name);
1320 static int dvb_net_remove_if(
struct dvb_net *dvbnet,
unsigned long num)
1322 struct net_device *net = dvbnet->device[num];
1325 if (!dvbnet->state[num])
1327 priv = netdev_priv(net);
1334 printk(
"dvb_net: removed network interface %s\n", net->
name);
1336 dvbnet->state[num]=0;
1337 dvbnet->device[num] =
NULL;
1343 static int dvb_net_do_ioctl(
struct file *
file,
1344 unsigned int cmd,
void *parg)
1361 if (!try_module_get(dvbdev->
adapter->module))
1364 result=dvb_net_add_if(dvbnet, dvbnetif->
pid, dvbnetif->
feedtype);
1366 module_put(dvbdev->
adapter->module);
1378 if (dvbnetif->
if_num >= DVB_NET_DEVICES_MAX ||
1379 !dvbnet->state[dvbnetif->
if_num])
1382 netdev = dvbnet->device[dvbnetif->
if_num];
1384 priv_data = netdev_priv(netdev);
1385 dvbnetif->
pid=priv_data->
pid;
1395 if ((
unsigned long) parg >= DVB_NET_DEVICES_MAX)
1397 ret = dvb_net_remove_if(dvbnet, (
unsigned long) parg);
1399 module_put(dvbdev->
adapter->module);
1412 if (!try_module_get(dvbdev->
adapter->module))
1417 module_put(dvbdev->
adapter->module);
1429 if (dvbnetif->
if_num >= DVB_NET_DEVICES_MAX ||
1430 !dvbnet->state[dvbnetif->
if_num])
1433 netdev = dvbnet->device[dvbnetif->
if_num];
1435 priv_data = netdev_priv(netdev);
1436 dvbnetif->
pid=priv_data->
pid;
1445 static long dvb_net_ioctl(
struct file *file,
1446 unsigned int cmd,
unsigned long arg)
1451 static int dvb_net_close(
struct inode *
inode,
struct file *file)
1458 if(dvbdev->
users == 1 && dvbnet->exit == 1) {
1469 .unlocked_ioctl = dvb_net_ioctl,
1471 .release = dvb_net_close,
1479 .fops = &dvb_net_fops,
1488 if (dvbnet->
dvbdev->users < 1)
1490 dvbnet->
dvbdev->users==1);
1495 if (!dvbnet->state[i])
1497 dvb_net_remove_if(dvbnet, i);
1508 dvbnet->demux = dmx;
1511 dvbnet->state[i] = 0;