dvb_net.c 42 KB

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