dvb_net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. /*
  2. * dvb_net.c
  3. *
  4. * Copyright (C) 2001 Convergence integrated media GmbH
  5. * Ralph Metzler <ralph@convergence.de>
  6. * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
  7. *
  8. * ULE Decapsulation code:
  9. * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
  10. * and Department of Scientific Computing
  11. * Paris Lodron University of Salzburg.
  12. * Hilmar Linder <hlinder@cosy.sbg.ac.at>
  13. * and Wolfram Stering <wstering@cosy.sbg.ac.at>
  14. *
  15. * ULE Decaps according to RFC 4326.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version 2
  20. * of the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  31. */
  32. /*
  33. * ULE ChangeLog:
  34. * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
  35. *
  36. * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
  37. * ULE Extension header handling.
  38. * Bugreports by Moritz Vieth and Hanno Tersteegen,
  39. * Fraunhofer Institute for Open Communication Systems
  40. * Competence Center for Advanced Satellite Communications.
  41. * Bugfixes and robustness improvements.
  42. * Filtering on dest MAC addresses, if present (D-Bit = 0)
  43. * ULE_DEBUG compile-time option.
  44. * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by
  45. * Christian Praehauser <cpraehaus@cosy.sbg.ac.at>,
  46. * Paris Lodron University of Salzburg.
  47. */
  48. /*
  49. * FIXME / TODO (dvb_net.c):
  50. *
  51. * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
  52. *
  53. */
  54. #include <linux/module.h>
  55. #include <linux/kernel.h>
  56. #include <linux/netdevice.h>
  57. #include <linux/etherdevice.h>
  58. #include <linux/dvb/net.h>
  59. #include <linux/uio.h>
  60. #include <asm/uaccess.h>
  61. #include <linux/crc32.h>
  62. #include <linux/mutex.h>
  63. #include <linux/sched.h>
  64. #include "dvb_demux.h"
  65. #include "dvb_net.h"
  66. static int dvb_net_debug;
  67. module_param(dvb_net_debug, int, 0444);
  68. MODULE_PARM_DESC(dvb_net_debug, "enable debug messages");
  69. #define dprintk(x...) do { if (dvb_net_debug) printk(x); } while (0)
  70. static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
  71. {
  72. unsigned int j;
  73. for (j = 0; j < cnt; j++)
  74. c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
  75. return c;
  76. }
  77. #define DVB_NET_MULTICAST_MAX 10
  78. #undef ULE_DEBUG
  79. #ifdef ULE_DEBUG
  80. #define MAC_ADDR_PRINTFMT "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
  81. #define MAX_ADDR_PRINTFMT_ARGS(macap) (macap)[0],(macap)[1],(macap)[2],(macap)[3],(macap)[4],(macap)[5]
  82. #define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
  83. static void hexdump( const unsigned char *buf, unsigned short len )
  84. {
  85. char str[80], octet[10];
  86. int ofs, i, l;
  87. for (ofs = 0; ofs < len; ofs += 16) {
  88. sprintf( str, "%03d: ", ofs );
  89. for (i = 0; i < 16; i++) {
  90. if ((i + ofs) < len)
  91. sprintf( octet, "%02x ", buf[ofs + i] );
  92. else
  93. strcpy( octet, " " );
  94. strcat( str, octet );
  95. }
  96. strcat( str, " " );
  97. l = strlen( str );
  98. for (i = 0; (i < 16) && ((i + ofs) < len); i++)
  99. str[l++] = isprint( buf[ofs + i] ) ? buf[ofs + i] : '.';
  100. str[l] = '\0';
  101. printk( KERN_WARNING "%s\n", str );
  102. }
  103. }
  104. #endif
  105. struct dvb_net_priv {
  106. int in_use;
  107. u16 pid;
  108. struct net_device *net;
  109. struct dvb_net *host;
  110. struct dmx_demux *demux;
  111. struct dmx_section_feed *secfeed;
  112. struct dmx_section_filter *secfilter;
  113. struct dmx_ts_feed *tsfeed;
  114. int multi_num;
  115. struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
  116. unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
  117. int rx_mode;
  118. #define RX_MODE_UNI 0
  119. #define RX_MODE_MULTI 1
  120. #define RX_MODE_ALL_MULTI 2
  121. #define RX_MODE_PROMISC 3
  122. struct work_struct set_multicast_list_wq;
  123. struct work_struct restart_net_feed_wq;
  124. unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
  125. int need_pusi; /* Set to 1, if synchronization on PUSI required. */
  126. unsigned char tscc; /* TS continuity counter after sync on PUSI. */
  127. struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
  128. unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
  129. unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
  130. unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
  131. unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
  132. unsigned char ule_dbit; /* Whether the DestMAC address present
  133. * or not (bit is set). */
  134. unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
  135. int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
  136. unsigned long ts_count; /* Current ts cell counter. */
  137. struct mutex mutex;
  138. };
  139. /**
  140. * Determine the packet's protocol ID. The rule here is that we
  141. * assume 802.3 if the type field is short enough to be a length.
  142. * This is normal practice and works for any 'now in use' protocol.
  143. *
  144. * stolen from eth.c out of the linux kernel, hacked for dvb-device
  145. * by Michael Holzt <kju@debian.org>
  146. */
  147. static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
  148. struct net_device *dev)
  149. {
  150. struct ethhdr *eth;
  151. unsigned char *rawp;
  152. skb_reset_mac_header(skb);
  153. skb_pull(skb,dev->hard_header_len);
  154. eth = eth_hdr(skb);
  155. if (*eth->h_dest & 1) {
  156. if(ether_addr_equal(eth->h_dest,dev->broadcast))
  157. skb->pkt_type=PACKET_BROADCAST;
  158. else
  159. skb->pkt_type=PACKET_MULTICAST;
  160. }
  161. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  162. return eth->h_proto;
  163. rawp = skb->data;
  164. /**
  165. * This is a magic hack to spot IPX packets. Older Novell breaks
  166. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  167. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  168. * won't work for fault tolerant netware but does for the rest.
  169. */
  170. if (*(unsigned short *)rawp == 0xFFFF)
  171. return htons(ETH_P_802_3);
  172. /**
  173. * Real 802.2 LLC
  174. */
  175. return htons(ETH_P_802_2);
  176. }
  177. #define TS_SZ 188
  178. #define TS_SYNC 0x47
  179. #define TS_TEI 0x80
  180. #define TS_SC 0xC0
  181. #define TS_PUSI 0x40
  182. #define TS_AF_A 0x20
  183. #define TS_AF_D 0x10
  184. /* ULE Extension Header handlers. */
  185. #define ULE_TEST 0
  186. #define ULE_BRIDGED 1
  187. #define ULE_OPTEXTHDR_PADDING 0
  188. static int ule_test_sndu( struct dvb_net_priv *p )
  189. {
  190. return -1;
  191. }
  192. static int ule_bridged_sndu( struct dvb_net_priv *p )
  193. {
  194. struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
  195. if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
  196. int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
  197. /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
  198. if(framelen != ntohs(hdr->h_proto)) {
  199. return -1;
  200. }
  201. }
  202. /* Note:
  203. * From RFC4326:
  204. * "A bridged SNDU is a Mandatory Extension Header of Type 1.
  205. * It must be the final (or only) extension header specified in the header chain of a SNDU."
  206. * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
  207. */
  208. p->ule_bridged = 1;
  209. return 0;
  210. }
  211. static int ule_exthdr_padding(struct dvb_net_priv *p)
  212. {
  213. return 0;
  214. }
  215. /** Handle ULE extension headers.
  216. * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
  217. * Returns: >= 0: nr. of bytes consumed by next extension header
  218. * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
  219. */
  220. static int handle_one_ule_extension( struct dvb_net_priv *p )
  221. {
  222. /* Table of mandatory extension header handlers. The header type is the index. */
  223. static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
  224. { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
  225. /* Table of optional extension header handlers. The header type is the index. */
  226. static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
  227. { [0] = ule_exthdr_padding, [1] = NULL, };
  228. int ext_len = 0;
  229. unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
  230. unsigned char htype = p->ule_sndu_type & 0x00FF;
  231. /* Discriminate mandatory and optional extension headers. */
  232. if (hlen == 0) {
  233. /* Mandatory extension header */
  234. if (ule_mandatory_ext_handlers[htype]) {
  235. ext_len = ule_mandatory_ext_handlers[htype]( p );
  236. if(ext_len >= 0) {
  237. p->ule_next_hdr += ext_len;
  238. if (!p->ule_bridged) {
  239. p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
  240. p->ule_next_hdr += 2;
  241. } else {
  242. p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
  243. /* This assures the extension handling loop will terminate. */
  244. }
  245. }
  246. // else: extension handler failed or SNDU should be discarded
  247. } else
  248. ext_len = -1; /* SNDU has to be discarded. */
  249. } else {
  250. /* Optional extension header. Calculate the length. */
  251. ext_len = hlen << 1;
  252. /* Process the optional extension header according to its type. */
  253. if (ule_optional_ext_handlers[htype])
  254. (void)ule_optional_ext_handlers[htype]( p );
  255. p->ule_next_hdr += ext_len;
  256. p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
  257. /*
  258. * note: the length of the next header type is included in the
  259. * length of THIS optional extension header
  260. */
  261. }
  262. return ext_len;
  263. }
  264. static int handle_ule_extensions( struct dvb_net_priv *p )
  265. {
  266. int total_ext_len = 0, l;
  267. p->ule_next_hdr = p->ule_skb->data;
  268. do {
  269. l = handle_one_ule_extension( p );
  270. if (l < 0)
  271. return l; /* Stop extension header processing and discard SNDU. */
  272. total_ext_len += l;
  273. #ifdef ULE_DEBUG
  274. dprintk("handle_ule_extensions: ule_next_hdr=%p, ule_sndu_type=%i, "
  275. "l=%i, total_ext_len=%i\n", p->ule_next_hdr,
  276. (int) p->ule_sndu_type, l, total_ext_len);
  277. #endif
  278. } while (p->ule_sndu_type < ETH_P_802_3_MIN);
  279. return total_ext_len;
  280. }
  281. /** Prepare for a new ULE SNDU: reset the decoder state. */
  282. static inline void reset_ule( struct dvb_net_priv *p )
  283. {
  284. p->ule_skb = NULL;
  285. p->ule_next_hdr = NULL;
  286. p->ule_sndu_len = 0;
  287. p->ule_sndu_type = 0;
  288. p->ule_sndu_type_1 = 0;
  289. p->ule_sndu_remain = 0;
  290. p->ule_dbit = 0xFF;
  291. p->ule_bridged = 0;
  292. }
  293. /**
  294. * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
  295. * TS cells of a single PID.
  296. */
  297. static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
  298. {
  299. struct dvb_net_priv *priv = netdev_priv(dev);
  300. unsigned long skipped = 0L;
  301. const u8 *ts, *ts_end, *from_where = NULL;
  302. u8 ts_remain = 0, how_much = 0, new_ts = 1;
  303. struct ethhdr *ethh = NULL;
  304. bool error = false;
  305. #ifdef ULE_DEBUG
  306. /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
  307. static unsigned char ule_hist[100*TS_SZ];
  308. static unsigned char *ule_where = ule_hist, ule_dump;
  309. #endif
  310. /* For all TS cells in current buffer.
  311. * Appearently, we are called for every single TS cell.
  312. */
  313. for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
  314. if (new_ts) {
  315. /* We are about to process a new TS cell. */
  316. #ifdef ULE_DEBUG
  317. if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
  318. memcpy( ule_where, ts, TS_SZ );
  319. if (ule_dump) {
  320. hexdump( ule_where, TS_SZ );
  321. ule_dump = 0;
  322. }
  323. ule_where += TS_SZ;
  324. #endif
  325. /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
  326. if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
  327. printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
  328. priv->ts_count, ts[0],
  329. (ts[1] & TS_TEI) >> 7,
  330. (ts[3] & TS_SC) >> 6);
  331. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  332. if (priv->ule_skb) {
  333. dev_kfree_skb( priv->ule_skb );
  334. /* Prepare for next SNDU. */
  335. dev->stats.rx_errors++;
  336. dev->stats.rx_frame_errors++;
  337. }
  338. reset_ule(priv);
  339. priv->need_pusi = 1;
  340. /* Continue with next TS cell. */
  341. ts += TS_SZ;
  342. priv->ts_count++;
  343. continue;
  344. }
  345. ts_remain = 184;
  346. from_where = ts + 4;
  347. }
  348. /* Synchronize on PUSI, if required. */
  349. if (priv->need_pusi) {
  350. if (ts[1] & TS_PUSI) {
  351. /* Find beginning of first ULE SNDU in current TS cell. */
  352. /* Synchronize continuity counter. */
  353. priv->tscc = ts[3] & 0x0F;
  354. /* There is a pointer field here. */
  355. if (ts[4] > ts_remain) {
  356. printk(KERN_ERR "%lu: Invalid ULE packet "
  357. "(pointer field %d)\n", priv->ts_count, ts[4]);
  358. ts += TS_SZ;
  359. priv->ts_count++;
  360. continue;
  361. }
  362. /* Skip to destination of pointer field. */
  363. from_where = &ts[5] + ts[4];
  364. ts_remain -= 1 + ts[4];
  365. skipped = 0;
  366. } else {
  367. skipped++;
  368. ts += TS_SZ;
  369. priv->ts_count++;
  370. continue;
  371. }
  372. }
  373. if (new_ts) {
  374. /* Check continuity counter. */
  375. if ((ts[3] & 0x0F) == priv->tscc)
  376. priv->tscc = (priv->tscc + 1) & 0x0F;
  377. else {
  378. /* TS discontinuity handling: */
  379. printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
  380. "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
  381. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  382. if (priv->ule_skb) {
  383. dev_kfree_skb( priv->ule_skb );
  384. /* Prepare for next SNDU. */
  385. // reset_ule(priv); moved to below.
  386. dev->stats.rx_errors++;
  387. dev->stats.rx_frame_errors++;
  388. }
  389. reset_ule(priv);
  390. /* skip to next PUSI. */
  391. priv->need_pusi = 1;
  392. continue;
  393. }
  394. /* If we still have an incomplete payload, but PUSI is
  395. * set; some TS cells are missing.
  396. * This is only possible here, if we missed exactly 16 TS
  397. * cells (continuity counter wrap). */
  398. if (ts[1] & TS_PUSI) {
  399. if (! priv->need_pusi) {
  400. if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
  401. /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
  402. printk(KERN_WARNING "%lu: Invalid pointer "
  403. "field: %u.\n", priv->ts_count, *from_where);
  404. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  405. if (priv->ule_skb) {
  406. error = true;
  407. dev_kfree_skb(priv->ule_skb);
  408. }
  409. if (error || priv->ule_sndu_remain) {
  410. dev->stats.rx_errors++;
  411. dev->stats.rx_frame_errors++;
  412. error = false;
  413. }
  414. reset_ule(priv);
  415. priv->need_pusi = 1;
  416. continue;
  417. }
  418. /* Skip pointer field (we're processing a
  419. * packed payload). */
  420. from_where += 1;
  421. ts_remain -= 1;
  422. } else
  423. priv->need_pusi = 0;
  424. if (priv->ule_sndu_remain > 183) {
  425. /* Current SNDU lacks more data than there could be available in the
  426. * current TS cell. */
  427. dev->stats.rx_errors++;
  428. dev->stats.rx_length_errors++;
  429. printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
  430. "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
  431. priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
  432. dev_kfree_skb(priv->ule_skb);
  433. /* Prepare for next SNDU. */
  434. reset_ule(priv);
  435. /* Resync: go to where pointer field points to: start of next ULE SNDU. */
  436. from_where += ts[4];
  437. ts_remain -= ts[4];
  438. }
  439. }
  440. }
  441. /* Check if new payload needs to be started. */
  442. if (priv->ule_skb == NULL) {
  443. /* Start a new payload with skb.
  444. * Find ULE header. It is only guaranteed that the
  445. * length field (2 bytes) is contained in the current
  446. * TS.
  447. * Check ts_remain has to be >= 2 here. */
  448. if (ts_remain < 2) {
  449. printk(KERN_WARNING "Invalid payload packing: only %d "
  450. "bytes left in TS. Resyncing.\n", ts_remain);
  451. priv->ule_sndu_len = 0;
  452. priv->need_pusi = 1;
  453. ts += TS_SZ;
  454. continue;
  455. }
  456. if (! priv->ule_sndu_len) {
  457. /* Got at least two bytes, thus extrace the SNDU length. */
  458. priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
  459. if (priv->ule_sndu_len & 0x8000) {
  460. /* D-Bit is set: no dest mac present. */
  461. priv->ule_sndu_len &= 0x7FFF;
  462. priv->ule_dbit = 1;
  463. } else
  464. priv->ule_dbit = 0;
  465. if (priv->ule_sndu_len < 5) {
  466. printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
  467. "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
  468. dev->stats.rx_errors++;
  469. dev->stats.rx_length_errors++;
  470. priv->ule_sndu_len = 0;
  471. priv->need_pusi = 1;
  472. new_ts = 1;
  473. ts += TS_SZ;
  474. priv->ts_count++;
  475. continue;
  476. }
  477. ts_remain -= 2; /* consume the 2 bytes SNDU length. */
  478. from_where += 2;
  479. }
  480. priv->ule_sndu_remain = priv->ule_sndu_len + 2;
  481. /*
  482. * State of current TS:
  483. * ts_remain (remaining bytes in the current TS cell)
  484. * 0 ule_type is not available now, we need the next TS cell
  485. * 1 the first byte of the ule_type is present
  486. * >=2 full ULE header present, maybe some payload data as well.
  487. */
  488. switch (ts_remain) {
  489. case 1:
  490. priv->ule_sndu_remain--;
  491. priv->ule_sndu_type = from_where[0] << 8;
  492. priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
  493. ts_remain -= 1; from_where += 1;
  494. /* Continue w/ next TS. */
  495. case 0:
  496. new_ts = 1;
  497. ts += TS_SZ;
  498. priv->ts_count++;
  499. continue;
  500. default: /* complete ULE header is present in current TS. */
  501. /* Extract ULE type field. */
  502. if (priv->ule_sndu_type_1) {
  503. priv->ule_sndu_type_1 = 0;
  504. priv->ule_sndu_type |= from_where[0];
  505. from_where += 1; /* points to payload start. */
  506. ts_remain -= 1;
  507. } else {
  508. /* Complete type is present in new TS. */
  509. priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
  510. from_where += 2; /* points to payload start. */
  511. ts_remain -= 2;
  512. }
  513. break;
  514. }
  515. /* Allocate the skb (decoder target buffer) with the correct size, as follows:
  516. * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
  517. priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
  518. if (priv->ule_skb == NULL) {
  519. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
  520. dev->name);
  521. dev->stats.rx_dropped++;
  522. return;
  523. }
  524. /* This includes the CRC32 _and_ dest mac, if !dbit. */
  525. priv->ule_sndu_remain = priv->ule_sndu_len;
  526. priv->ule_skb->dev = dev;
  527. /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
  528. skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
  529. }
  530. /* Copy data into our current skb. */
  531. how_much = min(priv->ule_sndu_remain, (int)ts_remain);
  532. memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
  533. priv->ule_sndu_remain -= how_much;
  534. ts_remain -= how_much;
  535. from_where += how_much;
  536. /* Check for complete payload. */
  537. if (priv->ule_sndu_remain <= 0) {
  538. /* Check CRC32, we've got it in our skb already. */
  539. __be16 ulen = htons(priv->ule_sndu_len);
  540. __be16 utype = htons(priv->ule_sndu_type);
  541. const u8 *tail;
  542. struct kvec iov[3] = {
  543. { &ulen, sizeof ulen },
  544. { &utype, sizeof utype },
  545. { priv->ule_skb->data, priv->ule_skb->len - 4 }
  546. };
  547. u32 ule_crc = ~0L, expected_crc;
  548. if (priv->ule_dbit) {
  549. /* Set D-bit for CRC32 verification,
  550. * if it was set originally. */
  551. ulen |= htons(0x8000);
  552. }
  553. ule_crc = iov_crc32(ule_crc, iov, 3);
  554. tail = skb_tail_pointer(priv->ule_skb);
  555. expected_crc = *(tail - 4) << 24 |
  556. *(tail - 3) << 16 |
  557. *(tail - 2) << 8 |
  558. *(tail - 1);
  559. if (ule_crc != expected_crc) {
  560. printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
  561. priv->ts_count, ule_crc, expected_crc, priv->ule_sndu_len, priv->ule_sndu_type, ts_remain, ts_remain > 2 ? *(unsigned short *)from_where : 0);
  562. #ifdef ULE_DEBUG
  563. hexdump( iov[0].iov_base, iov[0].iov_len );
  564. hexdump( iov[1].iov_base, iov[1].iov_len );
  565. hexdump( iov[2].iov_base, iov[2].iov_len );
  566. if (ule_where == ule_hist) {
  567. hexdump( &ule_hist[98*TS_SZ], TS_SZ );
  568. hexdump( &ule_hist[99*TS_SZ], TS_SZ );
  569. } else if (ule_where == &ule_hist[TS_SZ]) {
  570. hexdump( &ule_hist[99*TS_SZ], TS_SZ );
  571. hexdump( ule_hist, TS_SZ );
  572. } else {
  573. hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
  574. hexdump( ule_where - TS_SZ, TS_SZ );
  575. }
  576. ule_dump = 1;
  577. #endif
  578. dev->stats.rx_errors++;
  579. dev->stats.rx_crc_errors++;
  580. dev_kfree_skb(priv->ule_skb);
  581. } else {
  582. /* CRC32 verified OK. */
  583. u8 dest_addr[ETH_ALEN];
  584. static const u8 bc_addr[ETH_ALEN] =
  585. { [ 0 ... ETH_ALEN-1] = 0xff };
  586. /* CRC32 was OK. Remove it from skb. */
  587. priv->ule_skb->tail -= 4;
  588. priv->ule_skb->len -= 4;
  589. if (!priv->ule_dbit) {
  590. /*
  591. * The destination MAC address is the
  592. * next data in the skb. It comes
  593. * before any extension headers.
  594. *
  595. * Check if the payload of this SNDU
  596. * should be passed up the stack.
  597. */
  598. register int drop = 0;
  599. if (priv->rx_mode != RX_MODE_PROMISC) {
  600. if (priv->ule_skb->data[0] & 0x01) {
  601. /* multicast or broadcast */
  602. if (!ether_addr_equal(priv->ule_skb->data, bc_addr)) {
  603. /* multicast */
  604. if (priv->rx_mode == RX_MODE_MULTI) {
  605. int i;
  606. for(i = 0; i < priv->multi_num &&
  607. !ether_addr_equal(priv->ule_skb->data,
  608. priv->multi_macs[i]); i++)
  609. ;
  610. if (i == priv->multi_num)
  611. drop = 1;
  612. } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
  613. drop = 1; /* no broadcast; */
  614. /* else: all multicast mode: accept all multicast packets */
  615. }
  616. /* else: broadcast */
  617. }
  618. else if (!ether_addr_equal(priv->ule_skb->data, dev->dev_addr))
  619. drop = 1;
  620. /* else: destination address matches the MAC address of our receiver device */
  621. }
  622. /* else: promiscuous mode; pass everything up the stack */
  623. if (drop) {
  624. #ifdef ULE_DEBUG
  625. dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
  626. MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
  627. #endif
  628. dev_kfree_skb(priv->ule_skb);
  629. goto sndu_done;
  630. }
  631. else
  632. {
  633. skb_copy_from_linear_data(priv->ule_skb,
  634. dest_addr,
  635. ETH_ALEN);
  636. skb_pull(priv->ule_skb, ETH_ALEN);
  637. }
  638. }
  639. /* Handle ULE Extension Headers. */
  640. if (priv->ule_sndu_type < ETH_P_802_3_MIN) {
  641. /* There is an extension header. Handle it accordingly. */
  642. int l = handle_ule_extensions(priv);
  643. if (l < 0) {
  644. /* Mandatory extension header unknown or TEST SNDU. Drop it. */
  645. // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
  646. dev_kfree_skb(priv->ule_skb);
  647. goto sndu_done;
  648. }
  649. skb_pull(priv->ule_skb, l);
  650. }
  651. /*
  652. * Construct/assure correct ethernet header.
  653. * Note: in bridged mode (priv->ule_bridged !=
  654. * 0) we already have the (original) ethernet
  655. * header at the start of the payload (after
  656. * optional dest. address and any extension
  657. * headers).
  658. */
  659. if (!priv->ule_bridged) {
  660. skb_push(priv->ule_skb, ETH_HLEN);
  661. ethh = (struct ethhdr *)priv->ule_skb->data;
  662. if (!priv->ule_dbit) {
  663. /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
  664. memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
  665. memset(ethh->h_source, 0, ETH_ALEN);
  666. }
  667. else /* zeroize source and dest */
  668. memset( ethh, 0, ETH_ALEN*2 );
  669. ethh->h_proto = htons(priv->ule_sndu_type);
  670. }
  671. /* else: skb is in correct state; nothing to do. */
  672. priv->ule_bridged = 0;
  673. /* Stuff into kernel's protocol stack. */
  674. priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
  675. /* If D-bit is set (i.e. destination MAC address not present),
  676. * receive the packet anyhow. */
  677. /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
  678. priv->ule_skb->pkt_type = PACKET_HOST; */
  679. dev->stats.rx_packets++;
  680. dev->stats.rx_bytes += priv->ule_skb->len;
  681. netif_rx(priv->ule_skb);
  682. }
  683. sndu_done:
  684. /* Prepare for next SNDU. */
  685. reset_ule(priv);
  686. }
  687. /* More data in current TS (look at the bytes following the CRC32)? */
  688. if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
  689. /* Next ULE SNDU starts right there. */
  690. new_ts = 0;
  691. priv->ule_skb = NULL;
  692. priv->ule_sndu_type_1 = 0;
  693. priv->ule_sndu_len = 0;
  694. // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
  695. // *(from_where + 0), *(from_where + 1),
  696. // *(from_where + 2), *(from_where + 3));
  697. // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
  698. // hexdump(ts, 188);
  699. } else {
  700. new_ts = 1;
  701. ts += TS_SZ;
  702. priv->ts_count++;
  703. if (priv->ule_skb == NULL) {
  704. priv->need_pusi = 1;
  705. priv->ule_sndu_type_1 = 0;
  706. priv->ule_sndu_len = 0;
  707. }
  708. }
  709. } /* for all available TS cells */
  710. }
  711. static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
  712. const u8 *buffer2, size_t buffer2_len,
  713. struct dmx_ts_feed *feed, enum dmx_success success)
  714. {
  715. struct net_device *dev = feed->priv;
  716. if (buffer2)
  717. printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
  718. if (buffer1_len > 32768)
  719. printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
  720. /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
  721. buffer1_len, buffer1_len / TS_SZ, buffer1); */
  722. dvb_net_ule(dev, buffer1, buffer1_len);
  723. return 0;
  724. }
  725. static void dvb_net_sec(struct net_device *dev,
  726. const u8 *pkt, int pkt_len)
  727. {
  728. u8 *eth;
  729. struct sk_buff *skb;
  730. struct net_device_stats *stats = &dev->stats;
  731. int snap = 0;
  732. /* note: pkt_len includes a 32bit checksum */
  733. if (pkt_len < 16) {
  734. printk("%s: IP/MPE packet length = %d too small.\n",
  735. dev->name, pkt_len);
  736. stats->rx_errors++;
  737. stats->rx_length_errors++;
  738. return;
  739. }
  740. /* it seems some ISPs manage to screw up here, so we have to
  741. * relax the error checks... */
  742. #if 0
  743. if ((pkt[5] & 0xfd) != 0xc1) {
  744. /* drop scrambled or broken packets */
  745. #else
  746. if ((pkt[5] & 0x3c) != 0x00) {
  747. /* drop scrambled */
  748. #endif
  749. stats->rx_errors++;
  750. stats->rx_crc_errors++;
  751. return;
  752. }
  753. if (pkt[5] & 0x02) {
  754. /* handle LLC/SNAP, see rfc-1042 */
  755. if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
  756. stats->rx_dropped++;
  757. return;
  758. }
  759. snap = 8;
  760. }
  761. if (pkt[7]) {
  762. /* FIXME: assemble datagram from multiple sections */
  763. stats->rx_errors++;
  764. stats->rx_frame_errors++;
  765. return;
  766. }
  767. /* we have 14 byte ethernet header (ip header follows);
  768. * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
  769. */
  770. if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
  771. //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  772. stats->rx_dropped++;
  773. return;
  774. }
  775. skb_reserve(skb, 2); /* longword align L3 header */
  776. skb->dev = dev;
  777. /* copy L3 payload */
  778. eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
  779. memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
  780. /* create ethernet header: */
  781. eth[0]=pkt[0x0b];
  782. eth[1]=pkt[0x0a];
  783. eth[2]=pkt[0x09];
  784. eth[3]=pkt[0x08];
  785. eth[4]=pkt[0x04];
  786. eth[5]=pkt[0x03];
  787. eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
  788. if (snap) {
  789. eth[12] = pkt[18];
  790. eth[13] = pkt[19];
  791. } else {
  792. /* protocol numbers are from rfc-1700 or
  793. * http://www.iana.org/assignments/ethernet-numbers
  794. */
  795. if (pkt[12] >> 4 == 6) { /* version field from IP header */
  796. eth[12] = 0x86; /* IPv6 */
  797. eth[13] = 0xdd;
  798. } else {
  799. eth[12] = 0x08; /* IPv4 */
  800. eth[13] = 0x00;
  801. }
  802. }
  803. skb->protocol = dvb_net_eth_type_trans(skb, dev);
  804. stats->rx_packets++;
  805. stats->rx_bytes+=skb->len;
  806. netif_rx(skb);
  807. }
  808. static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
  809. const u8 *buffer2, size_t buffer2_len,
  810. struct dmx_section_filter *filter,
  811. enum dmx_success success)
  812. {
  813. struct net_device *dev = filter->priv;
  814. /**
  815. * we rely on the DVB API definition where exactly one complete
  816. * section is delivered in buffer1
  817. */
  818. dvb_net_sec (dev, buffer1, buffer1_len);
  819. return 0;
  820. }
  821. static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
  822. {
  823. dev_kfree_skb(skb);
  824. return NETDEV_TX_OK;
  825. }
  826. static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  827. static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
  828. static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
  829. static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  830. static int dvb_net_filter_sec_set(struct net_device *dev,
  831. struct dmx_section_filter **secfilter,
  832. u8 *mac, u8 *mac_mask)
  833. {
  834. struct dvb_net_priv *priv = netdev_priv(dev);
  835. int ret;
  836. *secfilter=NULL;
  837. ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
  838. if (ret<0) {
  839. printk("%s: could not get filter\n", dev->name);
  840. return ret;
  841. }
  842. (*secfilter)->priv=(void *) dev;
  843. memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
  844. memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
  845. memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
  846. (*secfilter)->filter_value[0]=0x3e;
  847. (*secfilter)->filter_value[3]=mac[5];
  848. (*secfilter)->filter_value[4]=mac[4];
  849. (*secfilter)->filter_value[8]=mac[3];
  850. (*secfilter)->filter_value[9]=mac[2];
  851. (*secfilter)->filter_value[10]=mac[1];
  852. (*secfilter)->filter_value[11]=mac[0];
  853. (*secfilter)->filter_mask[0] = 0xff;
  854. (*secfilter)->filter_mask[3] = mac_mask[5];
  855. (*secfilter)->filter_mask[4] = mac_mask[4];
  856. (*secfilter)->filter_mask[8] = mac_mask[3];
  857. (*secfilter)->filter_mask[9] = mac_mask[2];
  858. (*secfilter)->filter_mask[10] = mac_mask[1];
  859. (*secfilter)->filter_mask[11]=mac_mask[0];
  860. dprintk("%s: filter mac=%pM\n", dev->name, mac);
  861. dprintk("%s: filter mask=%pM\n", dev->name, mac_mask);
  862. return 0;
  863. }
  864. static int dvb_net_feed_start(struct net_device *dev)
  865. {
  866. int ret = 0, i;
  867. struct dvb_net_priv *priv = netdev_priv(dev);
  868. struct dmx_demux *demux = priv->demux;
  869. unsigned char *mac = (unsigned char *) dev->dev_addr;
  870. dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
  871. mutex_lock(&priv->mutex);
  872. if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
  873. printk("%s: BUG %d\n", __func__, __LINE__);
  874. priv->secfeed=NULL;
  875. priv->secfilter=NULL;
  876. priv->tsfeed = NULL;
  877. if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
  878. dprintk("%s: alloc secfeed\n", __func__);
  879. ret=demux->allocate_section_feed(demux, &priv->secfeed,
  880. dvb_net_sec_callback);
  881. if (ret<0) {
  882. printk("%s: could not allocate section feed\n", dev->name);
  883. goto error;
  884. }
  885. ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
  886. if (ret<0) {
  887. printk("%s: could not set section feed\n", dev->name);
  888. priv->demux->release_section_feed(priv->demux, priv->secfeed);
  889. priv->secfeed=NULL;
  890. goto error;
  891. }
  892. if (priv->rx_mode != RX_MODE_PROMISC) {
  893. dprintk("%s: set secfilter\n", __func__);
  894. dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
  895. }
  896. switch (priv->rx_mode) {
  897. case RX_MODE_MULTI:
  898. for (i = 0; i < priv->multi_num; i++) {
  899. dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
  900. dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
  901. priv->multi_macs[i], mask_normal);
  902. }
  903. break;
  904. case RX_MODE_ALL_MULTI:
  905. priv->multi_num=1;
  906. dprintk("%s: set multi_secfilter[0]\n", __func__);
  907. dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
  908. mac_allmulti, mask_allmulti);
  909. break;
  910. case RX_MODE_PROMISC:
  911. priv->multi_num=0;
  912. dprintk("%s: set secfilter\n", __func__);
  913. dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
  914. break;
  915. }
  916. dprintk("%s: start filtering\n", __func__);
  917. priv->secfeed->start_filtering(priv->secfeed);
  918. } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
  919. struct timespec timeout = { 0, 10000000 }; // 10 msec
  920. /* we have payloads encapsulated in TS */
  921. dprintk("%s: alloc tsfeed\n", __func__);
  922. ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
  923. if (ret < 0) {
  924. printk("%s: could not allocate ts feed\n", dev->name);
  925. goto error;
  926. }
  927. /* Set netdevice pointer for ts decaps callback. */
  928. priv->tsfeed->priv = (void *)dev;
  929. ret = priv->tsfeed->set(priv->tsfeed,
  930. priv->pid, /* pid */
  931. TS_PACKET, /* type */
  932. DMX_PES_OTHER, /* pes type */
  933. 32768, /* circular buffer size */
  934. timeout /* timeout */
  935. );
  936. if (ret < 0) {
  937. printk("%s: could not set ts feed\n", dev->name);
  938. priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
  939. priv->tsfeed = NULL;
  940. goto error;
  941. }
  942. dprintk("%s: start filtering\n", __func__);
  943. priv->tsfeed->start_filtering(priv->tsfeed);
  944. } else
  945. ret = -EINVAL;
  946. error:
  947. mutex_unlock(&priv->mutex);
  948. return ret;
  949. }
  950. static int dvb_net_feed_stop(struct net_device *dev)
  951. {
  952. struct dvb_net_priv *priv = netdev_priv(dev);
  953. int i, ret = 0;
  954. dprintk("%s\n", __func__);
  955. mutex_lock(&priv->mutex);
  956. if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
  957. if (priv->secfeed) {
  958. if (priv->secfeed->is_filtering) {
  959. dprintk("%s: stop secfeed\n", __func__);
  960. priv->secfeed->stop_filtering(priv->secfeed);
  961. }
  962. if (priv->secfilter) {
  963. dprintk("%s: release secfilter\n", __func__);
  964. priv->secfeed->release_filter(priv->secfeed,
  965. priv->secfilter);
  966. priv->secfilter=NULL;
  967. }
  968. for (i=0; i<priv->multi_num; i++) {
  969. if (priv->multi_secfilter[i]) {
  970. dprintk("%s: release multi_filter[%d]\n",
  971. __func__, i);
  972. priv->secfeed->release_filter(priv->secfeed,
  973. priv->multi_secfilter[i]);
  974. priv->multi_secfilter[i] = NULL;
  975. }
  976. }
  977. priv->demux->release_section_feed(priv->demux, priv->secfeed);
  978. priv->secfeed = NULL;
  979. } else
  980. printk("%s: no feed to stop\n", dev->name);
  981. } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
  982. if (priv->tsfeed) {
  983. if (priv->tsfeed->is_filtering) {
  984. dprintk("%s: stop tsfeed\n", __func__);
  985. priv->tsfeed->stop_filtering(priv->tsfeed);
  986. }
  987. priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
  988. priv->tsfeed = NULL;
  989. }
  990. else
  991. printk("%s: no ts feed to stop\n", dev->name);
  992. } else
  993. ret = -EINVAL;
  994. mutex_unlock(&priv->mutex);
  995. return ret;
  996. }
  997. static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
  998. {
  999. struct dvb_net_priv *priv = netdev_priv(dev);
  1000. if (priv->multi_num == DVB_NET_MULTICAST_MAX)
  1001. return -ENOMEM;
  1002. memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
  1003. priv->multi_num++;
  1004. return 0;
  1005. }
  1006. static void wq_set_multicast_list (struct work_struct *work)
  1007. {
  1008. struct dvb_net_priv *priv =
  1009. container_of(work, struct dvb_net_priv, set_multicast_list_wq);
  1010. struct net_device *dev = priv->net;
  1011. dvb_net_feed_stop(dev);
  1012. priv->rx_mode = RX_MODE_UNI;
  1013. netif_addr_lock_bh(dev);
  1014. if (dev->flags & IFF_PROMISC) {
  1015. dprintk("%s: promiscuous mode\n", dev->name);
  1016. priv->rx_mode = RX_MODE_PROMISC;
  1017. } else if ((dev->flags & IFF_ALLMULTI)) {
  1018. dprintk("%s: allmulti mode\n", dev->name);
  1019. priv->rx_mode = RX_MODE_ALL_MULTI;
  1020. } else if (!netdev_mc_empty(dev)) {
  1021. struct netdev_hw_addr *ha;
  1022. dprintk("%s: set_mc_list, %d entries\n",
  1023. dev->name, netdev_mc_count(dev));
  1024. priv->rx_mode = RX_MODE_MULTI;
  1025. priv->multi_num = 0;
  1026. netdev_for_each_mc_addr(ha, dev)
  1027. dvb_set_mc_filter(dev, ha->addr);
  1028. }
  1029. netif_addr_unlock_bh(dev);
  1030. dvb_net_feed_start(dev);
  1031. }
  1032. static void dvb_net_set_multicast_list (struct net_device *dev)
  1033. {
  1034. struct dvb_net_priv *priv = netdev_priv(dev);
  1035. schedule_work(&priv->set_multicast_list_wq);
  1036. }
  1037. static void wq_restart_net_feed (struct work_struct *work)
  1038. {
  1039. struct dvb_net_priv *priv =
  1040. container_of(work, struct dvb_net_priv, restart_net_feed_wq);
  1041. struct net_device *dev = priv->net;
  1042. if (netif_running(dev)) {
  1043. dvb_net_feed_stop(dev);
  1044. dvb_net_feed_start(dev);
  1045. }
  1046. }
  1047. static int dvb_net_set_mac (struct net_device *dev, void *p)
  1048. {
  1049. struct dvb_net_priv *priv = netdev_priv(dev);
  1050. struct sockaddr *addr=p;
  1051. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1052. if (netif_running(dev))
  1053. schedule_work(&priv->restart_net_feed_wq);
  1054. return 0;
  1055. }
  1056. static int dvb_net_open(struct net_device *dev)
  1057. {
  1058. struct dvb_net_priv *priv = netdev_priv(dev);
  1059. priv->in_use++;
  1060. dvb_net_feed_start(dev);
  1061. return 0;
  1062. }
  1063. static int dvb_net_stop(struct net_device *dev)
  1064. {
  1065. struct dvb_net_priv *priv = netdev_priv(dev);
  1066. priv->in_use--;
  1067. return dvb_net_feed_stop(dev);
  1068. }
  1069. static const struct header_ops dvb_header_ops = {
  1070. .create = eth_header,
  1071. .parse = eth_header_parse,
  1072. .rebuild = eth_rebuild_header,
  1073. };
  1074. static const struct net_device_ops dvb_netdev_ops = {
  1075. .ndo_open = dvb_net_open,
  1076. .ndo_stop = dvb_net_stop,
  1077. .ndo_start_xmit = dvb_net_tx,
  1078. .ndo_set_rx_mode = dvb_net_set_multicast_list,
  1079. .ndo_set_mac_address = dvb_net_set_mac,
  1080. .ndo_change_mtu = eth_change_mtu,
  1081. .ndo_validate_addr = eth_validate_addr,
  1082. };
  1083. static void dvb_net_setup(struct net_device *dev)
  1084. {
  1085. ether_setup(dev);
  1086. dev->header_ops = &dvb_header_ops;
  1087. dev->netdev_ops = &dvb_netdev_ops;
  1088. dev->mtu = 4096;
  1089. dev->flags |= IFF_NOARP;
  1090. }
  1091. static int get_if(struct dvb_net *dvbnet)
  1092. {
  1093. int i;
  1094. for (i=0; i<DVB_NET_DEVICES_MAX; i++)
  1095. if (!dvbnet->state[i])
  1096. break;
  1097. if (i == DVB_NET_DEVICES_MAX)
  1098. return -1;
  1099. dvbnet->state[i]=1;
  1100. return i;
  1101. }
  1102. static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
  1103. {
  1104. struct net_device *net;
  1105. struct dvb_net_priv *priv;
  1106. int result;
  1107. int if_num;
  1108. if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
  1109. return -EINVAL;
  1110. if ((if_num = get_if(dvbnet)) < 0)
  1111. return -EINVAL;
  1112. net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
  1113. NET_NAME_UNKNOWN, dvb_net_setup);
  1114. if (!net)
  1115. return -ENOMEM;
  1116. if (dvbnet->dvbdev->id)
  1117. snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
  1118. dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
  1119. else
  1120. /* compatibility fix to keep dvb0_0 format */
  1121. snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
  1122. dvbnet->dvbdev->adapter->num, if_num);
  1123. net->addr_len = 6;
  1124. memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
  1125. dvbnet->device[if_num] = net;
  1126. priv = netdev_priv(net);
  1127. priv->net = net;
  1128. priv->demux = dvbnet->demux;
  1129. priv->pid = pid;
  1130. priv->rx_mode = RX_MODE_UNI;
  1131. priv->need_pusi = 1;
  1132. priv->tscc = 0;
  1133. priv->feedtype = feedtype;
  1134. reset_ule(priv);
  1135. INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
  1136. INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
  1137. mutex_init(&priv->mutex);
  1138. net->base_addr = pid;
  1139. if ((result = register_netdev(net)) < 0) {
  1140. dvbnet->device[if_num] = NULL;
  1141. free_netdev(net);
  1142. return result;
  1143. }
  1144. printk("dvb_net: created network interface %s\n", net->name);
  1145. return if_num;
  1146. }
  1147. static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
  1148. {
  1149. struct net_device *net = dvbnet->device[num];
  1150. struct dvb_net_priv *priv;
  1151. if (!dvbnet->state[num])
  1152. return -EINVAL;
  1153. priv = netdev_priv(net);
  1154. if (priv->in_use)
  1155. return -EBUSY;
  1156. dvb_net_stop(net);
  1157. flush_work(&priv->set_multicast_list_wq);
  1158. flush_work(&priv->restart_net_feed_wq);
  1159. printk("dvb_net: removed network interface %s\n", net->name);
  1160. unregister_netdev(net);
  1161. dvbnet->state[num]=0;
  1162. dvbnet->device[num] = NULL;
  1163. free_netdev(net);
  1164. return 0;
  1165. }
  1166. static int dvb_net_do_ioctl(struct file *file,
  1167. unsigned int cmd, void *parg)
  1168. {
  1169. struct dvb_device *dvbdev = file->private_data;
  1170. struct dvb_net *dvbnet = dvbdev->priv;
  1171. int ret = 0;
  1172. if (((file->f_flags&O_ACCMODE)==O_RDONLY))
  1173. return -EPERM;
  1174. if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
  1175. return -ERESTARTSYS;
  1176. switch (cmd) {
  1177. case NET_ADD_IF:
  1178. {
  1179. struct dvb_net_if *dvbnetif = parg;
  1180. int result;
  1181. if (!capable(CAP_SYS_ADMIN)) {
  1182. ret = -EPERM;
  1183. goto ioctl_error;
  1184. }
  1185. if (!try_module_get(dvbdev->adapter->module)) {
  1186. ret = -EPERM;
  1187. goto ioctl_error;
  1188. }
  1189. result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
  1190. if (result<0) {
  1191. module_put(dvbdev->adapter->module);
  1192. ret = result;
  1193. goto ioctl_error;
  1194. }
  1195. dvbnetif->if_num=result;
  1196. break;
  1197. }
  1198. case NET_GET_IF:
  1199. {
  1200. struct net_device *netdev;
  1201. struct dvb_net_priv *priv_data;
  1202. struct dvb_net_if *dvbnetif = parg;
  1203. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1204. !dvbnet->state[dvbnetif->if_num]) {
  1205. ret = -EINVAL;
  1206. goto ioctl_error;
  1207. }
  1208. netdev = dvbnet->device[dvbnetif->if_num];
  1209. priv_data = netdev_priv(netdev);
  1210. dvbnetif->pid=priv_data->pid;
  1211. dvbnetif->feedtype=priv_data->feedtype;
  1212. break;
  1213. }
  1214. case NET_REMOVE_IF:
  1215. {
  1216. if (!capable(CAP_SYS_ADMIN)) {
  1217. ret = -EPERM;
  1218. goto ioctl_error;
  1219. }
  1220. if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
  1221. ret = -EINVAL;
  1222. goto ioctl_error;
  1223. }
  1224. ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
  1225. if (!ret)
  1226. module_put(dvbdev->adapter->module);
  1227. break;
  1228. }
  1229. /* binary compatibility cruft */
  1230. case __NET_ADD_IF_OLD:
  1231. {
  1232. struct __dvb_net_if_old *dvbnetif = parg;
  1233. int result;
  1234. if (!capable(CAP_SYS_ADMIN)) {
  1235. ret = -EPERM;
  1236. goto ioctl_error;
  1237. }
  1238. if (!try_module_get(dvbdev->adapter->module)) {
  1239. ret = -EPERM;
  1240. goto ioctl_error;
  1241. }
  1242. result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
  1243. if (result<0) {
  1244. module_put(dvbdev->adapter->module);
  1245. ret = result;
  1246. goto ioctl_error;
  1247. }
  1248. dvbnetif->if_num=result;
  1249. break;
  1250. }
  1251. case __NET_GET_IF_OLD:
  1252. {
  1253. struct net_device *netdev;
  1254. struct dvb_net_priv *priv_data;
  1255. struct __dvb_net_if_old *dvbnetif = parg;
  1256. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1257. !dvbnet->state[dvbnetif->if_num]) {
  1258. ret = -EINVAL;
  1259. goto ioctl_error;
  1260. }
  1261. netdev = dvbnet->device[dvbnetif->if_num];
  1262. priv_data = netdev_priv(netdev);
  1263. dvbnetif->pid=priv_data->pid;
  1264. break;
  1265. }
  1266. default:
  1267. ret = -ENOTTY;
  1268. break;
  1269. }
  1270. ioctl_error:
  1271. mutex_unlock(&dvbnet->ioctl_mutex);
  1272. return ret;
  1273. }
  1274. static long dvb_net_ioctl(struct file *file,
  1275. unsigned int cmd, unsigned long arg)
  1276. {
  1277. return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
  1278. }
  1279. static int dvb_net_close(struct inode *inode, struct file *file)
  1280. {
  1281. struct dvb_device *dvbdev = file->private_data;
  1282. struct dvb_net *dvbnet = dvbdev->priv;
  1283. dvb_generic_release(inode, file);
  1284. if(dvbdev->users == 1 && dvbnet->exit == 1)
  1285. wake_up(&dvbdev->wait_queue);
  1286. return 0;
  1287. }
  1288. static const struct file_operations dvb_net_fops = {
  1289. .owner = THIS_MODULE,
  1290. .unlocked_ioctl = dvb_net_ioctl,
  1291. .open = dvb_generic_open,
  1292. .release = dvb_net_close,
  1293. .llseek = noop_llseek,
  1294. };
  1295. static struct dvb_device dvbdev_net = {
  1296. .priv = NULL,
  1297. .users = 1,
  1298. .writers = 1,
  1299. .fops = &dvb_net_fops,
  1300. };
  1301. void dvb_net_release (struct dvb_net *dvbnet)
  1302. {
  1303. int i;
  1304. dvbnet->exit = 1;
  1305. if (dvbnet->dvbdev->users < 1)
  1306. wait_event(dvbnet->dvbdev->wait_queue,
  1307. dvbnet->dvbdev->users==1);
  1308. dvb_unregister_device(dvbnet->dvbdev);
  1309. for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
  1310. if (!dvbnet->state[i])
  1311. continue;
  1312. dvb_net_remove_if(dvbnet, i);
  1313. }
  1314. }
  1315. EXPORT_SYMBOL(dvb_net_release);
  1316. int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
  1317. struct dmx_demux *dmx)
  1318. {
  1319. int i;
  1320. mutex_init(&dvbnet->ioctl_mutex);
  1321. dvbnet->demux = dmx;
  1322. for (i=0; i<DVB_NET_DEVICES_MAX; i++)
  1323. dvbnet->state[i] = 0;
  1324. return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
  1325. dvbnet, DVB_DEVICE_NET);
  1326. }
  1327. EXPORT_SYMBOL(dvb_net_init);