dvb_net.c 42 KB

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