dvb_net.c 41 KB

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