dvb_net.c 42 KB

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