dvb_net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
  329. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  330. if (priv->ule_skb) {
  331. dev_kfree_skb( priv->ule_skb );
  332. /* Prepare for next SNDU. */
  333. dev->stats.rx_errors++;
  334. dev->stats.rx_frame_errors++;
  335. }
  336. reset_ule(priv);
  337. priv->need_pusi = 1;
  338. /* Continue with next TS cell. */
  339. ts += TS_SZ;
  340. priv->ts_count++;
  341. continue;
  342. }
  343. ts_remain = 184;
  344. from_where = ts + 4;
  345. }
  346. /* Synchronize on PUSI, if required. */
  347. if (priv->need_pusi) {
  348. if (ts[1] & TS_PUSI) {
  349. /* Find beginning of first ULE SNDU in current TS cell. */
  350. /* Synchronize continuity counter. */
  351. priv->tscc = ts[3] & 0x0F;
  352. /* There is a pointer field here. */
  353. if (ts[4] > ts_remain) {
  354. printk(KERN_ERR "%lu: Invalid ULE packet "
  355. "(pointer field %d)\n", priv->ts_count, ts[4]);
  356. ts += TS_SZ;
  357. priv->ts_count++;
  358. continue;
  359. }
  360. /* Skip to destination of pointer field. */
  361. from_where = &ts[5] + ts[4];
  362. ts_remain -= 1 + ts[4];
  363. skipped = 0;
  364. } else {
  365. skipped++;
  366. ts += TS_SZ;
  367. priv->ts_count++;
  368. continue;
  369. }
  370. }
  371. if (new_ts) {
  372. /* Check continuity counter. */
  373. if ((ts[3] & 0x0F) == priv->tscc)
  374. priv->tscc = (priv->tscc + 1) & 0x0F;
  375. else {
  376. /* TS discontinuity handling: */
  377. printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
  378. "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
  379. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  380. if (priv->ule_skb) {
  381. dev_kfree_skb( priv->ule_skb );
  382. /* Prepare for next SNDU. */
  383. // reset_ule(priv); moved to below.
  384. dev->stats.rx_errors++;
  385. dev->stats.rx_frame_errors++;
  386. }
  387. reset_ule(priv);
  388. /* skip to next PUSI. */
  389. priv->need_pusi = 1;
  390. continue;
  391. }
  392. /* If we still have an incomplete payload, but PUSI is
  393. * set; some TS cells are missing.
  394. * This is only possible here, if we missed exactly 16 TS
  395. * cells (continuity counter wrap). */
  396. if (ts[1] & TS_PUSI) {
  397. if (! priv->need_pusi) {
  398. if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
  399. /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
  400. printk(KERN_WARNING "%lu: Invalid pointer "
  401. "field: %u.\n", priv->ts_count, *from_where);
  402. /* Drop partly decoded SNDU, reset state, resync on PUSI. */
  403. if (priv->ule_skb) {
  404. error = true;
  405. dev_kfree_skb(priv->ule_skb);
  406. }
  407. if (error || priv->ule_sndu_remain) {
  408. dev->stats.rx_errors++;
  409. dev->stats.rx_frame_errors++;
  410. error = false;
  411. }
  412. reset_ule(priv);
  413. priv->need_pusi = 1;
  414. continue;
  415. }
  416. /* Skip pointer field (we're processing a
  417. * packed payload). */
  418. from_where += 1;
  419. ts_remain -= 1;
  420. } else
  421. priv->need_pusi = 0;
  422. if (priv->ule_sndu_remain > 183) {
  423. /* Current SNDU lacks more data than there could be available in the
  424. * current TS cell. */
  425. dev->stats.rx_errors++;
  426. dev->stats.rx_length_errors++;
  427. printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
  428. "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
  429. priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
  430. dev_kfree_skb(priv->ule_skb);
  431. /* Prepare for next SNDU. */
  432. reset_ule(priv);
  433. /* Resync: go to where pointer field points to: start of next ULE SNDU. */
  434. from_where += ts[4];
  435. ts_remain -= ts[4];
  436. }
  437. }
  438. }
  439. /* Check if new payload needs to be started. */
  440. if (priv->ule_skb == NULL) {
  441. /* Start a new payload with skb.
  442. * Find ULE header. It is only guaranteed that the
  443. * length field (2 bytes) is contained in the current
  444. * TS.
  445. * Check ts_remain has to be >= 2 here. */
  446. if (ts_remain < 2) {
  447. printk(KERN_WARNING "Invalid payload packing: only %d "
  448. "bytes left in TS. Resyncing.\n", ts_remain);
  449. priv->ule_sndu_len = 0;
  450. priv->need_pusi = 1;
  451. ts += TS_SZ;
  452. continue;
  453. }
  454. if (! priv->ule_sndu_len) {
  455. /* Got at least two bytes, thus extrace the SNDU length. */
  456. priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
  457. if (priv->ule_sndu_len & 0x8000) {
  458. /* D-Bit is set: no dest mac present. */
  459. priv->ule_sndu_len &= 0x7FFF;
  460. priv->ule_dbit = 1;
  461. } else
  462. priv->ule_dbit = 0;
  463. if (priv->ule_sndu_len < 5) {
  464. printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
  465. "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
  466. dev->stats.rx_errors++;
  467. dev->stats.rx_length_errors++;
  468. priv->ule_sndu_len = 0;
  469. priv->need_pusi = 1;
  470. new_ts = 1;
  471. ts += TS_SZ;
  472. priv->ts_count++;
  473. continue;
  474. }
  475. ts_remain -= 2; /* consume the 2 bytes SNDU length. */
  476. from_where += 2;
  477. }
  478. priv->ule_sndu_remain = priv->ule_sndu_len + 2;
  479. /*
  480. * State of current TS:
  481. * ts_remain (remaining bytes in the current TS cell)
  482. * 0 ule_type is not available now, we need the next TS cell
  483. * 1 the first byte of the ule_type is present
  484. * >=2 full ULE header present, maybe some payload data as well.
  485. */
  486. switch (ts_remain) {
  487. case 1:
  488. priv->ule_sndu_remain--;
  489. priv->ule_sndu_type = from_where[0] << 8;
  490. priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
  491. ts_remain -= 1; from_where += 1;
  492. /* Continue w/ next TS. */
  493. case 0:
  494. new_ts = 1;
  495. ts += TS_SZ;
  496. priv->ts_count++;
  497. continue;
  498. default: /* complete ULE header is present in current TS. */
  499. /* Extract ULE type field. */
  500. if (priv->ule_sndu_type_1) {
  501. priv->ule_sndu_type_1 = 0;
  502. priv->ule_sndu_type |= from_where[0];
  503. from_where += 1; /* points to payload start. */
  504. ts_remain -= 1;
  505. } else {
  506. /* Complete type is present in new TS. */
  507. priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
  508. from_where += 2; /* points to payload start. */
  509. ts_remain -= 2;
  510. }
  511. break;
  512. }
  513. /* Allocate the skb (decoder target buffer) with the correct size, as follows:
  514. * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
  515. priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
  516. if (priv->ule_skb == NULL) {
  517. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
  518. dev->name);
  519. dev->stats.rx_dropped++;
  520. return;
  521. }
  522. /* This includes the CRC32 _and_ dest mac, if !dbit. */
  523. priv->ule_sndu_remain = priv->ule_sndu_len;
  524. priv->ule_skb->dev = dev;
  525. /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
  526. skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
  527. }
  528. /* Copy data into our current skb. */
  529. how_much = min(priv->ule_sndu_remain, (int)ts_remain);
  530. memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
  531. priv->ule_sndu_remain -= how_much;
  532. ts_remain -= how_much;
  533. from_where += how_much;
  534. /* Check for complete payload. */
  535. if (priv->ule_sndu_remain <= 0) {
  536. /* Check CRC32, we've got it in our skb already. */
  537. __be16 ulen = htons(priv->ule_sndu_len);
  538. __be16 utype = htons(priv->ule_sndu_type);
  539. const u8 *tail;
  540. struct kvec iov[3] = {
  541. { &ulen, sizeof ulen },
  542. { &utype, sizeof utype },
  543. { priv->ule_skb->data, priv->ule_skb->len - 4 }
  544. };
  545. u32 ule_crc = ~0L, expected_crc;
  546. if (priv->ule_dbit) {
  547. /* Set D-bit for CRC32 verification,
  548. * if it was set originally. */
  549. ulen |= htons(0x8000);
  550. }
  551. ule_crc = iov_crc32(ule_crc, iov, 3);
  552. tail = skb_tail_pointer(priv->ule_skb);
  553. expected_crc = *(tail - 4) << 24 |
  554. *(tail - 3) << 16 |
  555. *(tail - 2) << 8 |
  556. *(tail - 1);
  557. if (ule_crc != expected_crc) {
  558. printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
  559. 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);
  560. #ifdef ULE_DEBUG
  561. hexdump( iov[0].iov_base, iov[0].iov_len );
  562. hexdump( iov[1].iov_base, iov[1].iov_len );
  563. hexdump( iov[2].iov_base, iov[2].iov_len );
  564. if (ule_where == ule_hist) {
  565. hexdump( &ule_hist[98*TS_SZ], TS_SZ );
  566. hexdump( &ule_hist[99*TS_SZ], TS_SZ );
  567. } else if (ule_where == &ule_hist[TS_SZ]) {
  568. hexdump( &ule_hist[99*TS_SZ], TS_SZ );
  569. hexdump( ule_hist, TS_SZ );
  570. } else {
  571. hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
  572. hexdump( ule_where - TS_SZ, TS_SZ );
  573. }
  574. ule_dump = 1;
  575. #endif
  576. dev->stats.rx_errors++;
  577. dev->stats.rx_crc_errors++;
  578. dev_kfree_skb(priv->ule_skb);
  579. } else {
  580. /* CRC32 verified OK. */
  581. u8 dest_addr[ETH_ALEN];
  582. static const u8 bc_addr[ETH_ALEN] =
  583. { [ 0 ... ETH_ALEN-1] = 0xff };
  584. /* CRC32 was OK. Remove it from skb. */
  585. priv->ule_skb->tail -= 4;
  586. priv->ule_skb->len -= 4;
  587. if (!priv->ule_dbit) {
  588. /*
  589. * The destination MAC address is the
  590. * next data in the skb. It comes
  591. * before any extension headers.
  592. *
  593. * Check if the payload of this SNDU
  594. * should be passed up the stack.
  595. */
  596. register int drop = 0;
  597. if (priv->rx_mode != RX_MODE_PROMISC) {
  598. if (priv->ule_skb->data[0] & 0x01) {
  599. /* multicast or broadcast */
  600. if (!ether_addr_equal(priv->ule_skb->data, bc_addr)) {
  601. /* multicast */
  602. if (priv->rx_mode == RX_MODE_MULTI) {
  603. int i;
  604. for(i = 0; i < priv->multi_num &&
  605. !ether_addr_equal(priv->ule_skb->data,
  606. priv->multi_macs[i]); i++)
  607. ;
  608. if (i == priv->multi_num)
  609. drop = 1;
  610. } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
  611. drop = 1; /* no broadcast; */
  612. /* else: all multicast mode: accept all multicast packets */
  613. }
  614. /* else: broadcast */
  615. }
  616. else if (!ether_addr_equal(priv->ule_skb->data, dev->dev_addr))
  617. drop = 1;
  618. /* else: destination address matches the MAC address of our receiver device */
  619. }
  620. /* else: promiscuous mode; pass everything up the stack */
  621. if (drop) {
  622. #ifdef ULE_DEBUG
  623. dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
  624. MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
  625. #endif
  626. dev_kfree_skb(priv->ule_skb);
  627. goto sndu_done;
  628. }
  629. else
  630. {
  631. skb_copy_from_linear_data(priv->ule_skb,
  632. dest_addr,
  633. ETH_ALEN);
  634. skb_pull(priv->ule_skb, ETH_ALEN);
  635. }
  636. }
  637. /* Handle ULE Extension Headers. */
  638. if (priv->ule_sndu_type < ETH_P_802_3_MIN) {
  639. /* There is an extension header. Handle it accordingly. */
  640. int l = handle_ule_extensions(priv);
  641. if (l < 0) {
  642. /* Mandatory extension header unknown or TEST SNDU. Drop it. */
  643. // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
  644. dev_kfree_skb(priv->ule_skb);
  645. goto sndu_done;
  646. }
  647. skb_pull(priv->ule_skb, l);
  648. }
  649. /*
  650. * Construct/assure correct ethernet header.
  651. * Note: in bridged mode (priv->ule_bridged !=
  652. * 0) we already have the (original) ethernet
  653. * header at the start of the payload (after
  654. * optional dest. address and any extension
  655. * headers).
  656. */
  657. if (!priv->ule_bridged) {
  658. skb_push(priv->ule_skb, ETH_HLEN);
  659. ethh = (struct ethhdr *)priv->ule_skb->data;
  660. if (!priv->ule_dbit) {
  661. /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
  662. memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
  663. memset(ethh->h_source, 0, ETH_ALEN);
  664. }
  665. else /* zeroize source and dest */
  666. memset( ethh, 0, ETH_ALEN*2 );
  667. ethh->h_proto = htons(priv->ule_sndu_type);
  668. }
  669. /* else: skb is in correct state; nothing to do. */
  670. priv->ule_bridged = 0;
  671. /* Stuff into kernel's protocol stack. */
  672. priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
  673. /* If D-bit is set (i.e. destination MAC address not present),
  674. * receive the packet anyhow. */
  675. /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
  676. priv->ule_skb->pkt_type = PACKET_HOST; */
  677. dev->stats.rx_packets++;
  678. dev->stats.rx_bytes += priv->ule_skb->len;
  679. netif_rx(priv->ule_skb);
  680. }
  681. sndu_done:
  682. /* Prepare for next SNDU. */
  683. reset_ule(priv);
  684. }
  685. /* More data in current TS (look at the bytes following the CRC32)? */
  686. if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
  687. /* Next ULE SNDU starts right there. */
  688. new_ts = 0;
  689. priv->ule_skb = NULL;
  690. priv->ule_sndu_type_1 = 0;
  691. priv->ule_sndu_len = 0;
  692. // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
  693. // *(from_where + 0), *(from_where + 1),
  694. // *(from_where + 2), *(from_where + 3));
  695. // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
  696. // hexdump(ts, 188);
  697. } else {
  698. new_ts = 1;
  699. ts += TS_SZ;
  700. priv->ts_count++;
  701. if (priv->ule_skb == NULL) {
  702. priv->need_pusi = 1;
  703. priv->ule_sndu_type_1 = 0;
  704. priv->ule_sndu_len = 0;
  705. }
  706. }
  707. } /* for all available TS cells */
  708. }
  709. static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
  710. const u8 *buffer2, size_t buffer2_len,
  711. struct dmx_ts_feed *feed, enum dmx_success success)
  712. {
  713. struct net_device *dev = feed->priv;
  714. if (buffer2)
  715. printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
  716. if (buffer1_len > 32768)
  717. printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
  718. /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
  719. buffer1_len, buffer1_len / TS_SZ, buffer1); */
  720. dvb_net_ule(dev, buffer1, buffer1_len);
  721. return 0;
  722. }
  723. static void dvb_net_sec(struct net_device *dev,
  724. const u8 *pkt, int pkt_len)
  725. {
  726. u8 *eth;
  727. struct sk_buff *skb;
  728. struct net_device_stats *stats = &dev->stats;
  729. int snap = 0;
  730. /* note: pkt_len includes a 32bit checksum */
  731. if (pkt_len < 16) {
  732. printk("%s: IP/MPE packet length = %d too small.\n",
  733. dev->name, pkt_len);
  734. stats->rx_errors++;
  735. stats->rx_length_errors++;
  736. return;
  737. }
  738. /* it seems some ISPs manage to screw up here, so we have to
  739. * relax the error checks... */
  740. #if 0
  741. if ((pkt[5] & 0xfd) != 0xc1) {
  742. /* drop scrambled or broken packets */
  743. #else
  744. if ((pkt[5] & 0x3c) != 0x00) {
  745. /* drop scrambled */
  746. #endif
  747. stats->rx_errors++;
  748. stats->rx_crc_errors++;
  749. return;
  750. }
  751. if (pkt[5] & 0x02) {
  752. /* handle LLC/SNAP, see rfc-1042 */
  753. if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
  754. stats->rx_dropped++;
  755. return;
  756. }
  757. snap = 8;
  758. }
  759. if (pkt[7]) {
  760. /* FIXME: assemble datagram from multiple sections */
  761. stats->rx_errors++;
  762. stats->rx_frame_errors++;
  763. return;
  764. }
  765. /* we have 14 byte ethernet header (ip header follows);
  766. * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
  767. */
  768. if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
  769. //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  770. stats->rx_dropped++;
  771. return;
  772. }
  773. skb_reserve(skb, 2); /* longword align L3 header */
  774. skb->dev = dev;
  775. /* copy L3 payload */
  776. eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
  777. memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
  778. /* create ethernet header: */
  779. eth[0]=pkt[0x0b];
  780. eth[1]=pkt[0x0a];
  781. eth[2]=pkt[0x09];
  782. eth[3]=pkt[0x08];
  783. eth[4]=pkt[0x04];
  784. eth[5]=pkt[0x03];
  785. eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
  786. if (snap) {
  787. eth[12] = pkt[18];
  788. eth[13] = pkt[19];
  789. } else {
  790. /* protocol numbers are from rfc-1700 or
  791. * http://www.iana.org/assignments/ethernet-numbers
  792. */
  793. if (pkt[12] >> 4 == 6) { /* version field from IP header */
  794. eth[12] = 0x86; /* IPv6 */
  795. eth[13] = 0xdd;
  796. } else {
  797. eth[12] = 0x08; /* IPv4 */
  798. eth[13] = 0x00;
  799. }
  800. }
  801. skb->protocol = dvb_net_eth_type_trans(skb, dev);
  802. stats->rx_packets++;
  803. stats->rx_bytes+=skb->len;
  804. netif_rx(skb);
  805. }
  806. static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
  807. const u8 *buffer2, size_t buffer2_len,
  808. struct dmx_section_filter *filter,
  809. enum dmx_success success)
  810. {
  811. struct net_device *dev = filter->priv;
  812. /**
  813. * we rely on the DVB API definition where exactly one complete
  814. * section is delivered in buffer1
  815. */
  816. dvb_net_sec (dev, buffer1, buffer1_len);
  817. return 0;
  818. }
  819. static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
  820. {
  821. dev_kfree_skb(skb);
  822. return NETDEV_TX_OK;
  823. }
  824. static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  825. static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
  826. static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
  827. static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  828. static int dvb_net_filter_sec_set(struct net_device *dev,
  829. struct dmx_section_filter **secfilter,
  830. u8 *mac, u8 *mac_mask)
  831. {
  832. struct dvb_net_priv *priv = netdev_priv(dev);
  833. int ret;
  834. *secfilter=NULL;
  835. ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
  836. if (ret<0) {
  837. printk("%s: could not get filter\n", dev->name);
  838. return ret;
  839. }
  840. (*secfilter)->priv=(void *) dev;
  841. memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
  842. memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
  843. memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
  844. (*secfilter)->filter_value[0]=0x3e;
  845. (*secfilter)->filter_value[3]=mac[5];
  846. (*secfilter)->filter_value[4]=mac[4];
  847. (*secfilter)->filter_value[8]=mac[3];
  848. (*secfilter)->filter_value[9]=mac[2];
  849. (*secfilter)->filter_value[10]=mac[1];
  850. (*secfilter)->filter_value[11]=mac[0];
  851. (*secfilter)->filter_mask[0] = 0xff;
  852. (*secfilter)->filter_mask[3] = mac_mask[5];
  853. (*secfilter)->filter_mask[4] = mac_mask[4];
  854. (*secfilter)->filter_mask[8] = mac_mask[3];
  855. (*secfilter)->filter_mask[9] = mac_mask[2];
  856. (*secfilter)->filter_mask[10] = mac_mask[1];
  857. (*secfilter)->filter_mask[11]=mac_mask[0];
  858. dprintk("%s: filter mac=%pM\n", dev->name, mac);
  859. dprintk("%s: filter mask=%pM\n", dev->name, mac_mask);
  860. return 0;
  861. }
  862. static int dvb_net_feed_start(struct net_device *dev)
  863. {
  864. int ret = 0, i;
  865. struct dvb_net_priv *priv = netdev_priv(dev);
  866. struct dmx_demux *demux = priv->demux;
  867. unsigned char *mac = (unsigned char *) dev->dev_addr;
  868. dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
  869. mutex_lock(&priv->mutex);
  870. if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
  871. printk("%s: BUG %d\n", __func__, __LINE__);
  872. priv->secfeed=NULL;
  873. priv->secfilter=NULL;
  874. priv->tsfeed = NULL;
  875. if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
  876. dprintk("%s: alloc secfeed\n", __func__);
  877. ret=demux->allocate_section_feed(demux, &priv->secfeed,
  878. dvb_net_sec_callback);
  879. if (ret<0) {
  880. printk("%s: could not allocate section feed\n", dev->name);
  881. goto error;
  882. }
  883. ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
  884. if (ret<0) {
  885. printk("%s: could not set section feed\n", dev->name);
  886. priv->demux->release_section_feed(priv->demux, priv->secfeed);
  887. priv->secfeed=NULL;
  888. goto error;
  889. }
  890. if (priv->rx_mode != RX_MODE_PROMISC) {
  891. dprintk("%s: set secfilter\n", __func__);
  892. dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
  893. }
  894. switch (priv->rx_mode) {
  895. case RX_MODE_MULTI:
  896. for (i = 0; i < priv->multi_num; i++) {
  897. dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
  898. dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
  899. priv->multi_macs[i], mask_normal);
  900. }
  901. break;
  902. case RX_MODE_ALL_MULTI:
  903. priv->multi_num=1;
  904. dprintk("%s: set multi_secfilter[0]\n", __func__);
  905. dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
  906. mac_allmulti, mask_allmulti);
  907. break;
  908. case RX_MODE_PROMISC:
  909. priv->multi_num=0;
  910. dprintk("%s: set secfilter\n", __func__);
  911. dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
  912. break;
  913. }
  914. dprintk("%s: start filtering\n", __func__);
  915. priv->secfeed->start_filtering(priv->secfeed);
  916. } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
  917. struct timespec timeout = { 0, 10000000 }; // 10 msec
  918. /* we have payloads encapsulated in TS */
  919. dprintk("%s: alloc tsfeed\n", __func__);
  920. ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
  921. if (ret < 0) {
  922. printk("%s: could not allocate ts feed\n", dev->name);
  923. goto error;
  924. }
  925. /* Set netdevice pointer for ts decaps callback. */
  926. priv->tsfeed->priv = (void *)dev;
  927. ret = priv->tsfeed->set(priv->tsfeed,
  928. priv->pid, /* pid */
  929. TS_PACKET, /* type */
  930. DMX_PES_OTHER, /* pes type */
  931. 32768, /* circular buffer size */
  932. timeout /* timeout */
  933. );
  934. if (ret < 0) {
  935. printk("%s: could not set ts feed\n", dev->name);
  936. priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
  937. priv->tsfeed = NULL;
  938. goto error;
  939. }
  940. dprintk("%s: start filtering\n", __func__);
  941. priv->tsfeed->start_filtering(priv->tsfeed);
  942. } else
  943. ret = -EINVAL;
  944. error:
  945. mutex_unlock(&priv->mutex);
  946. return ret;
  947. }
  948. static int dvb_net_feed_stop(struct net_device *dev)
  949. {
  950. struct dvb_net_priv *priv = netdev_priv(dev);
  951. int i, ret = 0;
  952. dprintk("%s\n", __func__);
  953. mutex_lock(&priv->mutex);
  954. if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
  955. if (priv->secfeed) {
  956. if (priv->secfeed->is_filtering) {
  957. dprintk("%s: stop secfeed\n", __func__);
  958. priv->secfeed->stop_filtering(priv->secfeed);
  959. }
  960. if (priv->secfilter) {
  961. dprintk("%s: release secfilter\n", __func__);
  962. priv->secfeed->release_filter(priv->secfeed,
  963. priv->secfilter);
  964. priv->secfilter=NULL;
  965. }
  966. for (i=0; i<priv->multi_num; i++) {
  967. if (priv->multi_secfilter[i]) {
  968. dprintk("%s: release multi_filter[%d]\n",
  969. __func__, i);
  970. priv->secfeed->release_filter(priv->secfeed,
  971. priv->multi_secfilter[i]);
  972. priv->multi_secfilter[i] = NULL;
  973. }
  974. }
  975. priv->demux->release_section_feed(priv->demux, priv->secfeed);
  976. priv->secfeed = NULL;
  977. } else
  978. printk("%s: no feed to stop\n", dev->name);
  979. } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
  980. if (priv->tsfeed) {
  981. if (priv->tsfeed->is_filtering) {
  982. dprintk("%s: stop tsfeed\n", __func__);
  983. priv->tsfeed->stop_filtering(priv->tsfeed);
  984. }
  985. priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
  986. priv->tsfeed = NULL;
  987. }
  988. else
  989. printk("%s: no ts feed to stop\n", dev->name);
  990. } else
  991. ret = -EINVAL;
  992. mutex_unlock(&priv->mutex);
  993. return ret;
  994. }
  995. static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
  996. {
  997. struct dvb_net_priv *priv = netdev_priv(dev);
  998. if (priv->multi_num == DVB_NET_MULTICAST_MAX)
  999. return -ENOMEM;
  1000. memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
  1001. priv->multi_num++;
  1002. return 0;
  1003. }
  1004. static void wq_set_multicast_list (struct work_struct *work)
  1005. {
  1006. struct dvb_net_priv *priv =
  1007. container_of(work, struct dvb_net_priv, set_multicast_list_wq);
  1008. struct net_device *dev = priv->net;
  1009. dvb_net_feed_stop(dev);
  1010. priv->rx_mode = RX_MODE_UNI;
  1011. netif_addr_lock_bh(dev);
  1012. if (dev->flags & IFF_PROMISC) {
  1013. dprintk("%s: promiscuous mode\n", dev->name);
  1014. priv->rx_mode = RX_MODE_PROMISC;
  1015. } else if ((dev->flags & IFF_ALLMULTI)) {
  1016. dprintk("%s: allmulti mode\n", dev->name);
  1017. priv->rx_mode = RX_MODE_ALL_MULTI;
  1018. } else if (!netdev_mc_empty(dev)) {
  1019. struct netdev_hw_addr *ha;
  1020. dprintk("%s: set_mc_list, %d entries\n",
  1021. dev->name, netdev_mc_count(dev));
  1022. priv->rx_mode = RX_MODE_MULTI;
  1023. priv->multi_num = 0;
  1024. netdev_for_each_mc_addr(ha, dev)
  1025. dvb_set_mc_filter(dev, ha->addr);
  1026. }
  1027. netif_addr_unlock_bh(dev);
  1028. dvb_net_feed_start(dev);
  1029. }
  1030. static void dvb_net_set_multicast_list (struct net_device *dev)
  1031. {
  1032. struct dvb_net_priv *priv = netdev_priv(dev);
  1033. schedule_work(&priv->set_multicast_list_wq);
  1034. }
  1035. static void wq_restart_net_feed (struct work_struct *work)
  1036. {
  1037. struct dvb_net_priv *priv =
  1038. container_of(work, struct dvb_net_priv, restart_net_feed_wq);
  1039. struct net_device *dev = priv->net;
  1040. if (netif_running(dev)) {
  1041. dvb_net_feed_stop(dev);
  1042. dvb_net_feed_start(dev);
  1043. }
  1044. }
  1045. static int dvb_net_set_mac (struct net_device *dev, void *p)
  1046. {
  1047. struct dvb_net_priv *priv = netdev_priv(dev);
  1048. struct sockaddr *addr=p;
  1049. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1050. if (netif_running(dev))
  1051. schedule_work(&priv->restart_net_feed_wq);
  1052. return 0;
  1053. }
  1054. static int dvb_net_open(struct net_device *dev)
  1055. {
  1056. struct dvb_net_priv *priv = netdev_priv(dev);
  1057. priv->in_use++;
  1058. dvb_net_feed_start(dev);
  1059. return 0;
  1060. }
  1061. static int dvb_net_stop(struct net_device *dev)
  1062. {
  1063. struct dvb_net_priv *priv = netdev_priv(dev);
  1064. priv->in_use--;
  1065. return dvb_net_feed_stop(dev);
  1066. }
  1067. static const struct header_ops dvb_header_ops = {
  1068. .create = eth_header,
  1069. .parse = eth_header_parse,
  1070. .rebuild = eth_rebuild_header,
  1071. };
  1072. static const struct net_device_ops dvb_netdev_ops = {
  1073. .ndo_open = dvb_net_open,
  1074. .ndo_stop = dvb_net_stop,
  1075. .ndo_start_xmit = dvb_net_tx,
  1076. .ndo_set_rx_mode = dvb_net_set_multicast_list,
  1077. .ndo_set_mac_address = dvb_net_set_mac,
  1078. .ndo_change_mtu = eth_change_mtu,
  1079. .ndo_validate_addr = eth_validate_addr,
  1080. };
  1081. static void dvb_net_setup(struct net_device *dev)
  1082. {
  1083. ether_setup(dev);
  1084. dev->header_ops = &dvb_header_ops;
  1085. dev->netdev_ops = &dvb_netdev_ops;
  1086. dev->mtu = 4096;
  1087. dev->flags |= IFF_NOARP;
  1088. }
  1089. static int get_if(struct dvb_net *dvbnet)
  1090. {
  1091. int i;
  1092. for (i=0; i<DVB_NET_DEVICES_MAX; i++)
  1093. if (!dvbnet->state[i])
  1094. break;
  1095. if (i == DVB_NET_DEVICES_MAX)
  1096. return -1;
  1097. dvbnet->state[i]=1;
  1098. return i;
  1099. }
  1100. static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
  1101. {
  1102. struct net_device *net;
  1103. struct dvb_net_priv *priv;
  1104. int result;
  1105. int if_num;
  1106. if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
  1107. return -EINVAL;
  1108. if ((if_num = get_if(dvbnet)) < 0)
  1109. return -EINVAL;
  1110. net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
  1111. if (!net)
  1112. return -ENOMEM;
  1113. if (dvbnet->dvbdev->id)
  1114. snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
  1115. dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
  1116. else
  1117. /* compatibility fix to keep dvb0_0 format */
  1118. snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
  1119. dvbnet->dvbdev->adapter->num, if_num);
  1120. net->addr_len = 6;
  1121. memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
  1122. dvbnet->device[if_num] = net;
  1123. priv = netdev_priv(net);
  1124. priv->net = net;
  1125. priv->demux = dvbnet->demux;
  1126. priv->pid = pid;
  1127. priv->rx_mode = RX_MODE_UNI;
  1128. priv->need_pusi = 1;
  1129. priv->tscc = 0;
  1130. priv->feedtype = feedtype;
  1131. reset_ule(priv);
  1132. INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
  1133. INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
  1134. mutex_init(&priv->mutex);
  1135. net->base_addr = pid;
  1136. if ((result = register_netdev(net)) < 0) {
  1137. dvbnet->device[if_num] = NULL;
  1138. free_netdev(net);
  1139. return result;
  1140. }
  1141. printk("dvb_net: created network interface %s\n", net->name);
  1142. return if_num;
  1143. }
  1144. static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
  1145. {
  1146. struct net_device *net = dvbnet->device[num];
  1147. struct dvb_net_priv *priv;
  1148. if (!dvbnet->state[num])
  1149. return -EINVAL;
  1150. priv = netdev_priv(net);
  1151. if (priv->in_use)
  1152. return -EBUSY;
  1153. dvb_net_stop(net);
  1154. flush_work(&priv->set_multicast_list_wq);
  1155. flush_work(&priv->restart_net_feed_wq);
  1156. printk("dvb_net: removed network interface %s\n", net->name);
  1157. unregister_netdev(net);
  1158. dvbnet->state[num]=0;
  1159. dvbnet->device[num] = NULL;
  1160. free_netdev(net);
  1161. return 0;
  1162. }
  1163. static int dvb_net_do_ioctl(struct file *file,
  1164. unsigned int cmd, void *parg)
  1165. {
  1166. struct dvb_device *dvbdev = file->private_data;
  1167. struct dvb_net *dvbnet = dvbdev->priv;
  1168. int ret = 0;
  1169. if (((file->f_flags&O_ACCMODE)==O_RDONLY))
  1170. return -EPERM;
  1171. if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
  1172. return -ERESTARTSYS;
  1173. switch (cmd) {
  1174. case NET_ADD_IF:
  1175. {
  1176. struct dvb_net_if *dvbnetif = parg;
  1177. int result;
  1178. if (!capable(CAP_SYS_ADMIN)) {
  1179. ret = -EPERM;
  1180. goto ioctl_error;
  1181. }
  1182. if (!try_module_get(dvbdev->adapter->module)) {
  1183. ret = -EPERM;
  1184. goto ioctl_error;
  1185. }
  1186. result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
  1187. if (result<0) {
  1188. module_put(dvbdev->adapter->module);
  1189. ret = result;
  1190. goto ioctl_error;
  1191. }
  1192. dvbnetif->if_num=result;
  1193. break;
  1194. }
  1195. case NET_GET_IF:
  1196. {
  1197. struct net_device *netdev;
  1198. struct dvb_net_priv *priv_data;
  1199. struct dvb_net_if *dvbnetif = parg;
  1200. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1201. !dvbnet->state[dvbnetif->if_num]) {
  1202. ret = -EINVAL;
  1203. goto ioctl_error;
  1204. }
  1205. netdev = dvbnet->device[dvbnetif->if_num];
  1206. priv_data = netdev_priv(netdev);
  1207. dvbnetif->pid=priv_data->pid;
  1208. dvbnetif->feedtype=priv_data->feedtype;
  1209. break;
  1210. }
  1211. case NET_REMOVE_IF:
  1212. {
  1213. if (!capable(CAP_SYS_ADMIN)) {
  1214. ret = -EPERM;
  1215. goto ioctl_error;
  1216. }
  1217. if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
  1218. ret = -EINVAL;
  1219. goto ioctl_error;
  1220. }
  1221. ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
  1222. if (!ret)
  1223. module_put(dvbdev->adapter->module);
  1224. break;
  1225. }
  1226. /* binary compatibility cruft */
  1227. case __NET_ADD_IF_OLD:
  1228. {
  1229. struct __dvb_net_if_old *dvbnetif = parg;
  1230. int result;
  1231. if (!capable(CAP_SYS_ADMIN)) {
  1232. ret = -EPERM;
  1233. goto ioctl_error;
  1234. }
  1235. if (!try_module_get(dvbdev->adapter->module)) {
  1236. ret = -EPERM;
  1237. goto ioctl_error;
  1238. }
  1239. result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
  1240. if (result<0) {
  1241. module_put(dvbdev->adapter->module);
  1242. ret = result;
  1243. goto ioctl_error;
  1244. }
  1245. dvbnetif->if_num=result;
  1246. break;
  1247. }
  1248. case __NET_GET_IF_OLD:
  1249. {
  1250. struct net_device *netdev;
  1251. struct dvb_net_priv *priv_data;
  1252. struct __dvb_net_if_old *dvbnetif = parg;
  1253. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1254. !dvbnet->state[dvbnetif->if_num]) {
  1255. ret = -EINVAL;
  1256. goto ioctl_error;
  1257. }
  1258. netdev = dvbnet->device[dvbnetif->if_num];
  1259. priv_data = netdev_priv(netdev);
  1260. dvbnetif->pid=priv_data->pid;
  1261. break;
  1262. }
  1263. default:
  1264. ret = -ENOTTY;
  1265. break;
  1266. }
  1267. ioctl_error:
  1268. mutex_unlock(&dvbnet->ioctl_mutex);
  1269. return ret;
  1270. }
  1271. static long dvb_net_ioctl(struct file *file,
  1272. unsigned int cmd, unsigned long arg)
  1273. {
  1274. return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
  1275. }
  1276. static int dvb_net_close(struct inode *inode, struct file *file)
  1277. {
  1278. struct dvb_device *dvbdev = file->private_data;
  1279. struct dvb_net *dvbnet = dvbdev->priv;
  1280. dvb_generic_release(inode, file);
  1281. if(dvbdev->users == 1 && dvbnet->exit == 1)
  1282. wake_up(&dvbdev->wait_queue);
  1283. return 0;
  1284. }
  1285. static const struct file_operations dvb_net_fops = {
  1286. .owner = THIS_MODULE,
  1287. .unlocked_ioctl = dvb_net_ioctl,
  1288. .open = dvb_generic_open,
  1289. .release = dvb_net_close,
  1290. .llseek = noop_llseek,
  1291. };
  1292. static struct dvb_device dvbdev_net = {
  1293. .priv = NULL,
  1294. .users = 1,
  1295. .writers = 1,
  1296. .fops = &dvb_net_fops,
  1297. };
  1298. void dvb_net_release (struct dvb_net *dvbnet)
  1299. {
  1300. int i;
  1301. dvbnet->exit = 1;
  1302. if (dvbnet->dvbdev->users < 1)
  1303. wait_event(dvbnet->dvbdev->wait_queue,
  1304. dvbnet->dvbdev->users==1);
  1305. dvb_unregister_device(dvbnet->dvbdev);
  1306. for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
  1307. if (!dvbnet->state[i])
  1308. continue;
  1309. dvb_net_remove_if(dvbnet, i);
  1310. }
  1311. }
  1312. EXPORT_SYMBOL(dvb_net_release);
  1313. int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
  1314. struct dmx_demux *dmx)
  1315. {
  1316. int i;
  1317. mutex_init(&dvbnet->ioctl_mutex);
  1318. dvbnet->demux = dmx;
  1319. for (i=0; i<DVB_NET_DEVICES_MAX; i++)
  1320. dvbnet->state[i] = 0;
  1321. return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
  1322. dvbnet, DVB_DEVICE_NET);
  1323. }
  1324. EXPORT_SYMBOL(dvb_net_init);