dvb_net.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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",
  1111. NET_NAME_UNKNOWN, dvb_net_setup);
  1112. if (!net)
  1113. return -ENOMEM;
  1114. if (dvbnet->dvbdev->id)
  1115. snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
  1116. dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
  1117. else
  1118. /* compatibility fix to keep dvb0_0 format */
  1119. snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
  1120. dvbnet->dvbdev->adapter->num, if_num);
  1121. net->addr_len = 6;
  1122. memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
  1123. dvbnet->device[if_num] = net;
  1124. priv = netdev_priv(net);
  1125. priv->net = net;
  1126. priv->demux = dvbnet->demux;
  1127. priv->pid = pid;
  1128. priv->rx_mode = RX_MODE_UNI;
  1129. priv->need_pusi = 1;
  1130. priv->tscc = 0;
  1131. priv->feedtype = feedtype;
  1132. reset_ule(priv);
  1133. INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
  1134. INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
  1135. mutex_init(&priv->mutex);
  1136. net->base_addr = pid;
  1137. if ((result = register_netdev(net)) < 0) {
  1138. dvbnet->device[if_num] = NULL;
  1139. free_netdev(net);
  1140. return result;
  1141. }
  1142. printk("dvb_net: created network interface %s\n", net->name);
  1143. return if_num;
  1144. }
  1145. static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
  1146. {
  1147. struct net_device *net = dvbnet->device[num];
  1148. struct dvb_net_priv *priv;
  1149. if (!dvbnet->state[num])
  1150. return -EINVAL;
  1151. priv = netdev_priv(net);
  1152. if (priv->in_use)
  1153. return -EBUSY;
  1154. dvb_net_stop(net);
  1155. flush_work(&priv->set_multicast_list_wq);
  1156. flush_work(&priv->restart_net_feed_wq);
  1157. printk("dvb_net: removed network interface %s\n", net->name);
  1158. unregister_netdev(net);
  1159. dvbnet->state[num]=0;
  1160. dvbnet->device[num] = NULL;
  1161. free_netdev(net);
  1162. return 0;
  1163. }
  1164. static int dvb_net_do_ioctl(struct file *file,
  1165. unsigned int cmd, void *parg)
  1166. {
  1167. struct dvb_device *dvbdev = file->private_data;
  1168. struct dvb_net *dvbnet = dvbdev->priv;
  1169. int ret = 0;
  1170. if (((file->f_flags&O_ACCMODE)==O_RDONLY))
  1171. return -EPERM;
  1172. if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
  1173. return -ERESTARTSYS;
  1174. switch (cmd) {
  1175. case NET_ADD_IF:
  1176. {
  1177. struct dvb_net_if *dvbnetif = parg;
  1178. int result;
  1179. if (!capable(CAP_SYS_ADMIN)) {
  1180. ret = -EPERM;
  1181. goto ioctl_error;
  1182. }
  1183. if (!try_module_get(dvbdev->adapter->module)) {
  1184. ret = -EPERM;
  1185. goto ioctl_error;
  1186. }
  1187. result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
  1188. if (result<0) {
  1189. module_put(dvbdev->adapter->module);
  1190. ret = result;
  1191. goto ioctl_error;
  1192. }
  1193. dvbnetif->if_num=result;
  1194. break;
  1195. }
  1196. case NET_GET_IF:
  1197. {
  1198. struct net_device *netdev;
  1199. struct dvb_net_priv *priv_data;
  1200. struct dvb_net_if *dvbnetif = parg;
  1201. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1202. !dvbnet->state[dvbnetif->if_num]) {
  1203. ret = -EINVAL;
  1204. goto ioctl_error;
  1205. }
  1206. netdev = dvbnet->device[dvbnetif->if_num];
  1207. priv_data = netdev_priv(netdev);
  1208. dvbnetif->pid=priv_data->pid;
  1209. dvbnetif->feedtype=priv_data->feedtype;
  1210. break;
  1211. }
  1212. case NET_REMOVE_IF:
  1213. {
  1214. if (!capable(CAP_SYS_ADMIN)) {
  1215. ret = -EPERM;
  1216. goto ioctl_error;
  1217. }
  1218. if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
  1219. ret = -EINVAL;
  1220. goto ioctl_error;
  1221. }
  1222. ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
  1223. if (!ret)
  1224. module_put(dvbdev->adapter->module);
  1225. break;
  1226. }
  1227. /* binary compatibility cruft */
  1228. case __NET_ADD_IF_OLD:
  1229. {
  1230. struct __dvb_net_if_old *dvbnetif = parg;
  1231. int result;
  1232. if (!capable(CAP_SYS_ADMIN)) {
  1233. ret = -EPERM;
  1234. goto ioctl_error;
  1235. }
  1236. if (!try_module_get(dvbdev->adapter->module)) {
  1237. ret = -EPERM;
  1238. goto ioctl_error;
  1239. }
  1240. result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
  1241. if (result<0) {
  1242. module_put(dvbdev->adapter->module);
  1243. ret = result;
  1244. goto ioctl_error;
  1245. }
  1246. dvbnetif->if_num=result;
  1247. break;
  1248. }
  1249. case __NET_GET_IF_OLD:
  1250. {
  1251. struct net_device *netdev;
  1252. struct dvb_net_priv *priv_data;
  1253. struct __dvb_net_if_old *dvbnetif = parg;
  1254. if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
  1255. !dvbnet->state[dvbnetif->if_num]) {
  1256. ret = -EINVAL;
  1257. goto ioctl_error;
  1258. }
  1259. netdev = dvbnet->device[dvbnetif->if_num];
  1260. priv_data = netdev_priv(netdev);
  1261. dvbnetif->pid=priv_data->pid;
  1262. break;
  1263. }
  1264. default:
  1265. ret = -ENOTTY;
  1266. break;
  1267. }
  1268. ioctl_error:
  1269. mutex_unlock(&dvbnet->ioctl_mutex);
  1270. return ret;
  1271. }
  1272. static long dvb_net_ioctl(struct file *file,
  1273. unsigned int cmd, unsigned long arg)
  1274. {
  1275. return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
  1276. }
  1277. static int dvb_net_close(struct inode *inode, struct file *file)
  1278. {
  1279. struct dvb_device *dvbdev = file->private_data;
  1280. struct dvb_net *dvbnet = dvbdev->priv;
  1281. dvb_generic_release(inode, file);
  1282. if(dvbdev->users == 1 && dvbnet->exit == 1)
  1283. wake_up(&dvbdev->wait_queue);
  1284. return 0;
  1285. }
  1286. static const struct file_operations dvb_net_fops = {
  1287. .owner = THIS_MODULE,
  1288. .unlocked_ioctl = dvb_net_ioctl,
  1289. .open = dvb_generic_open,
  1290. .release = dvb_net_close,
  1291. .llseek = noop_llseek,
  1292. };
  1293. static struct dvb_device dvbdev_net = {
  1294. .priv = NULL,
  1295. .users = 1,
  1296. .writers = 1,
  1297. .fops = &dvb_net_fops,
  1298. };
  1299. void dvb_net_release (struct dvb_net *dvbnet)
  1300. {
  1301. int i;
  1302. dvbnet->exit = 1;
  1303. if (dvbnet->dvbdev->users < 1)
  1304. wait_event(dvbnet->dvbdev->wait_queue,
  1305. dvbnet->dvbdev->users==1);
  1306. dvb_unregister_device(dvbnet->dvbdev);
  1307. for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
  1308. if (!dvbnet->state[i])
  1309. continue;
  1310. dvb_net_remove_if(dvbnet, i);
  1311. }
  1312. }
  1313. EXPORT_SYMBOL(dvb_net_release);
  1314. int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
  1315. struct dmx_demux *dmx)
  1316. {
  1317. int i;
  1318. mutex_init(&dvbnet->ioctl_mutex);
  1319. dvbnet->demux = dmx;
  1320. for (i=0; i<DVB_NET_DEVICES_MAX; i++)
  1321. dvbnet->state[i] = 0;
  1322. return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
  1323. dvbnet, DVB_DEVICE_NET);
  1324. }
  1325. EXPORT_SYMBOL(dvb_net_init);