macsec.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  1. /*
  2. * drivers/net/macsec.c - MACsec device
  3. *
  4. * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/socket.h>
  14. #include <linux/module.h>
  15. #include <crypto/aead.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/genetlink.h>
  19. #include <net/sock.h>
  20. #include <uapi/linux/if_macsec.h>
  21. typedef u64 __bitwise sci_t;
  22. #define MACSEC_SCI_LEN 8
  23. /* SecTAG length = macsec_eth_header without the optional SCI */
  24. #define MACSEC_TAG_LEN 6
  25. struct macsec_eth_header {
  26. struct ethhdr eth;
  27. /* SecTAG */
  28. u8 tci_an;
  29. #if defined(__LITTLE_ENDIAN_BITFIELD)
  30. u8 short_length:6,
  31. unused:2;
  32. #elif defined(__BIG_ENDIAN_BITFIELD)
  33. u8 unused:2,
  34. short_length:6;
  35. #else
  36. #error "Please fix <asm/byteorder.h>"
  37. #endif
  38. __be32 packet_number;
  39. u8 secure_channel_id[8]; /* optional */
  40. } __packed;
  41. #define MACSEC_TCI_VERSION 0x80
  42. #define MACSEC_TCI_ES 0x40 /* end station */
  43. #define MACSEC_TCI_SC 0x20 /* SCI present */
  44. #define MACSEC_TCI_SCB 0x10 /* epon */
  45. #define MACSEC_TCI_E 0x08 /* encryption */
  46. #define MACSEC_TCI_C 0x04 /* changed text */
  47. #define MACSEC_AN_MASK 0x03 /* association number */
  48. #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
  49. /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
  50. #define MIN_NON_SHORT_LEN 48
  51. #define GCM_AES_IV_LEN 12
  52. #define DEFAULT_ICV_LEN 16
  53. #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
  54. #define for_each_rxsc(secy, sc) \
  55. for (sc = rcu_dereference_bh(secy->rx_sc); \
  56. sc; \
  57. sc = rcu_dereference_bh(sc->next))
  58. #define for_each_rxsc_rtnl(secy, sc) \
  59. for (sc = rtnl_dereference(secy->rx_sc); \
  60. sc; \
  61. sc = rtnl_dereference(sc->next))
  62. struct gcm_iv {
  63. union {
  64. u8 secure_channel_id[8];
  65. sci_t sci;
  66. };
  67. __be32 pn;
  68. };
  69. /**
  70. * struct macsec_key - SA key
  71. * @id: user-provided key identifier
  72. * @tfm: crypto struct, key storage
  73. */
  74. struct macsec_key {
  75. u64 id;
  76. struct crypto_aead *tfm;
  77. };
  78. struct macsec_rx_sc_stats {
  79. __u64 InOctetsValidated;
  80. __u64 InOctetsDecrypted;
  81. __u64 InPktsUnchecked;
  82. __u64 InPktsDelayed;
  83. __u64 InPktsOK;
  84. __u64 InPktsInvalid;
  85. __u64 InPktsLate;
  86. __u64 InPktsNotValid;
  87. __u64 InPktsNotUsingSA;
  88. __u64 InPktsUnusedSA;
  89. };
  90. struct macsec_rx_sa_stats {
  91. __u32 InPktsOK;
  92. __u32 InPktsInvalid;
  93. __u32 InPktsNotValid;
  94. __u32 InPktsNotUsingSA;
  95. __u32 InPktsUnusedSA;
  96. };
  97. struct macsec_tx_sa_stats {
  98. __u32 OutPktsProtected;
  99. __u32 OutPktsEncrypted;
  100. };
  101. struct macsec_tx_sc_stats {
  102. __u64 OutPktsProtected;
  103. __u64 OutPktsEncrypted;
  104. __u64 OutOctetsProtected;
  105. __u64 OutOctetsEncrypted;
  106. };
  107. struct macsec_dev_stats {
  108. __u64 OutPktsUntagged;
  109. __u64 InPktsUntagged;
  110. __u64 OutPktsTooLong;
  111. __u64 InPktsNoTag;
  112. __u64 InPktsBadTag;
  113. __u64 InPktsUnknownSCI;
  114. __u64 InPktsNoSCI;
  115. __u64 InPktsOverrun;
  116. };
  117. /**
  118. * struct macsec_rx_sa - receive secure association
  119. * @active:
  120. * @next_pn: packet number expected for the next packet
  121. * @lock: protects next_pn manipulations
  122. * @key: key structure
  123. * @stats: per-SA stats
  124. */
  125. struct macsec_rx_sa {
  126. struct macsec_key key;
  127. spinlock_t lock;
  128. u32 next_pn;
  129. atomic_t refcnt;
  130. bool active;
  131. struct macsec_rx_sa_stats __percpu *stats;
  132. struct macsec_rx_sc *sc;
  133. struct rcu_head rcu;
  134. };
  135. struct pcpu_rx_sc_stats {
  136. struct macsec_rx_sc_stats stats;
  137. struct u64_stats_sync syncp;
  138. };
  139. /**
  140. * struct macsec_rx_sc - receive secure channel
  141. * @sci: secure channel identifier for this SC
  142. * @active: channel is active
  143. * @sa: array of secure associations
  144. * @stats: per-SC stats
  145. */
  146. struct macsec_rx_sc {
  147. struct macsec_rx_sc __rcu *next;
  148. sci_t sci;
  149. bool active;
  150. struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
  151. struct pcpu_rx_sc_stats __percpu *stats;
  152. atomic_t refcnt;
  153. struct rcu_head rcu_head;
  154. };
  155. /**
  156. * struct macsec_tx_sa - transmit secure association
  157. * @active:
  158. * @next_pn: packet number to use for the next packet
  159. * @lock: protects next_pn manipulations
  160. * @key: key structure
  161. * @stats: per-SA stats
  162. */
  163. struct macsec_tx_sa {
  164. struct macsec_key key;
  165. spinlock_t lock;
  166. u32 next_pn;
  167. atomic_t refcnt;
  168. bool active;
  169. struct macsec_tx_sa_stats __percpu *stats;
  170. struct rcu_head rcu;
  171. };
  172. struct pcpu_tx_sc_stats {
  173. struct macsec_tx_sc_stats stats;
  174. struct u64_stats_sync syncp;
  175. };
  176. /**
  177. * struct macsec_tx_sc - transmit secure channel
  178. * @active:
  179. * @encoding_sa: association number of the SA currently in use
  180. * @encrypt: encrypt packets on transmit, or authenticate only
  181. * @send_sci: always include the SCI in the SecTAG
  182. * @end_station:
  183. * @scb: single copy broadcast flag
  184. * @sa: array of secure associations
  185. * @stats: stats for this TXSC
  186. */
  187. struct macsec_tx_sc {
  188. bool active;
  189. u8 encoding_sa;
  190. bool encrypt;
  191. bool send_sci;
  192. bool end_station;
  193. bool scb;
  194. struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
  195. struct pcpu_tx_sc_stats __percpu *stats;
  196. };
  197. #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
  198. /**
  199. * struct macsec_secy - MACsec Security Entity
  200. * @netdev: netdevice for this SecY
  201. * @n_rx_sc: number of receive secure channels configured on this SecY
  202. * @sci: secure channel identifier used for tx
  203. * @key_len: length of keys used by the cipher suite
  204. * @icv_len: length of ICV used by the cipher suite
  205. * @validate_frames: validation mode
  206. * @operational: MAC_Operational flag
  207. * @protect_frames: enable protection for this SecY
  208. * @replay_protect: enable packet number checks on receive
  209. * @replay_window: size of the replay window
  210. * @tx_sc: transmit secure channel
  211. * @rx_sc: linked list of receive secure channels
  212. */
  213. struct macsec_secy {
  214. struct net_device *netdev;
  215. unsigned int n_rx_sc;
  216. sci_t sci;
  217. u16 key_len;
  218. u16 icv_len;
  219. enum macsec_validation_type validate_frames;
  220. bool operational;
  221. bool protect_frames;
  222. bool replay_protect;
  223. u32 replay_window;
  224. struct macsec_tx_sc tx_sc;
  225. struct macsec_rx_sc __rcu *rx_sc;
  226. };
  227. struct pcpu_secy_stats {
  228. struct macsec_dev_stats stats;
  229. struct u64_stats_sync syncp;
  230. };
  231. /**
  232. * struct macsec_dev - private data
  233. * @secy: SecY config
  234. * @real_dev: pointer to underlying netdevice
  235. * @stats: MACsec device stats
  236. * @secys: linked list of SecY's on the underlying device
  237. */
  238. struct macsec_dev {
  239. struct macsec_secy secy;
  240. struct net_device *real_dev;
  241. struct pcpu_secy_stats __percpu *stats;
  242. struct list_head secys;
  243. };
  244. /**
  245. * struct macsec_rxh_data - rx_handler private argument
  246. * @secys: linked list of SecY's on this underlying device
  247. */
  248. struct macsec_rxh_data {
  249. struct list_head secys;
  250. };
  251. static struct macsec_dev *macsec_priv(const struct net_device *dev)
  252. {
  253. return (struct macsec_dev *)netdev_priv(dev);
  254. }
  255. static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
  256. {
  257. return rcu_dereference_bh(dev->rx_handler_data);
  258. }
  259. static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
  260. {
  261. return rtnl_dereference(dev->rx_handler_data);
  262. }
  263. struct macsec_cb {
  264. struct aead_request *req;
  265. union {
  266. struct macsec_tx_sa *tx_sa;
  267. struct macsec_rx_sa *rx_sa;
  268. };
  269. u8 assoc_num;
  270. bool valid;
  271. bool has_sci;
  272. };
  273. static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
  274. {
  275. struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
  276. if (!sa || !sa->active)
  277. return NULL;
  278. if (!atomic_inc_not_zero(&sa->refcnt))
  279. return NULL;
  280. return sa;
  281. }
  282. static void free_rx_sc_rcu(struct rcu_head *head)
  283. {
  284. struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
  285. free_percpu(rx_sc->stats);
  286. kfree(rx_sc);
  287. }
  288. static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
  289. {
  290. return atomic_inc_not_zero(&sc->refcnt) ? sc : NULL;
  291. }
  292. static void macsec_rxsc_put(struct macsec_rx_sc *sc)
  293. {
  294. if (atomic_dec_and_test(&sc->refcnt))
  295. call_rcu(&sc->rcu_head, free_rx_sc_rcu);
  296. }
  297. static void free_rxsa(struct rcu_head *head)
  298. {
  299. struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
  300. crypto_free_aead(sa->key.tfm);
  301. free_percpu(sa->stats);
  302. macsec_rxsc_put(sa->sc);
  303. kfree(sa);
  304. }
  305. static void macsec_rxsa_put(struct macsec_rx_sa *sa)
  306. {
  307. if (atomic_dec_and_test(&sa->refcnt))
  308. call_rcu(&sa->rcu, free_rxsa);
  309. }
  310. static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
  311. {
  312. struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
  313. if (!sa || !sa->active)
  314. return NULL;
  315. if (!atomic_inc_not_zero(&sa->refcnt))
  316. return NULL;
  317. return sa;
  318. }
  319. static void free_txsa(struct rcu_head *head)
  320. {
  321. struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
  322. crypto_free_aead(sa->key.tfm);
  323. free_percpu(sa->stats);
  324. kfree(sa);
  325. }
  326. static void macsec_txsa_put(struct macsec_tx_sa *sa)
  327. {
  328. if (atomic_dec_and_test(&sa->refcnt))
  329. call_rcu(&sa->rcu, free_txsa);
  330. }
  331. static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
  332. {
  333. BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
  334. return (struct macsec_cb *)skb->cb;
  335. }
  336. #define MACSEC_PORT_ES (htons(0x0001))
  337. #define MACSEC_PORT_SCB (0x0000)
  338. #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
  339. #define DEFAULT_SAK_LEN 16
  340. #define DEFAULT_SEND_SCI true
  341. #define DEFAULT_ENCRYPT false
  342. #define DEFAULT_ENCODING_SA 0
  343. static sci_t make_sci(u8 *addr, __be16 port)
  344. {
  345. sci_t sci;
  346. memcpy(&sci, addr, ETH_ALEN);
  347. memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
  348. return sci;
  349. }
  350. static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
  351. {
  352. sci_t sci;
  353. if (sci_present)
  354. memcpy(&sci, hdr->secure_channel_id,
  355. sizeof(hdr->secure_channel_id));
  356. else
  357. sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
  358. return sci;
  359. }
  360. static unsigned int macsec_sectag_len(bool sci_present)
  361. {
  362. return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
  363. }
  364. static unsigned int macsec_hdr_len(bool sci_present)
  365. {
  366. return macsec_sectag_len(sci_present) + ETH_HLEN;
  367. }
  368. static unsigned int macsec_extra_len(bool sci_present)
  369. {
  370. return macsec_sectag_len(sci_present) + sizeof(__be16);
  371. }
  372. /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
  373. static void macsec_fill_sectag(struct macsec_eth_header *h,
  374. const struct macsec_secy *secy, u32 pn)
  375. {
  376. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  377. memset(&h->tci_an, 0, macsec_sectag_len(tx_sc->send_sci));
  378. h->eth.h_proto = htons(ETH_P_MACSEC);
  379. if (tx_sc->send_sci ||
  380. (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb)) {
  381. h->tci_an |= MACSEC_TCI_SC;
  382. memcpy(&h->secure_channel_id, &secy->sci,
  383. sizeof(h->secure_channel_id));
  384. } else {
  385. if (tx_sc->end_station)
  386. h->tci_an |= MACSEC_TCI_ES;
  387. if (tx_sc->scb)
  388. h->tci_an |= MACSEC_TCI_SCB;
  389. }
  390. h->packet_number = htonl(pn);
  391. /* with GCM, C/E clear for !encrypt, both set for encrypt */
  392. if (tx_sc->encrypt)
  393. h->tci_an |= MACSEC_TCI_CONFID;
  394. else if (secy->icv_len != DEFAULT_ICV_LEN)
  395. h->tci_an |= MACSEC_TCI_C;
  396. h->tci_an |= tx_sc->encoding_sa;
  397. }
  398. static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
  399. {
  400. if (data_len < MIN_NON_SHORT_LEN)
  401. h->short_length = data_len;
  402. }
  403. /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
  404. static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
  405. {
  406. struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
  407. int len = skb->len - 2 * ETH_ALEN;
  408. int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
  409. /* a) It comprises at least 17 octets */
  410. if (skb->len <= 16)
  411. return false;
  412. /* b) MACsec EtherType: already checked */
  413. /* c) V bit is clear */
  414. if (h->tci_an & MACSEC_TCI_VERSION)
  415. return false;
  416. /* d) ES or SCB => !SC */
  417. if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
  418. (h->tci_an & MACSEC_TCI_SC))
  419. return false;
  420. /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
  421. if (h->unused)
  422. return false;
  423. /* rx.pn != 0 (figure 10-5) */
  424. if (!h->packet_number)
  425. return false;
  426. /* length check, f) g) h) i) */
  427. if (h->short_length)
  428. return len == extra_len + h->short_length;
  429. return len >= extra_len + MIN_NON_SHORT_LEN;
  430. }
  431. #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
  432. #define MACSEC_NEEDED_TAILROOM MACSEC_MAX_ICV_LEN
  433. static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
  434. {
  435. struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
  436. gcm_iv->sci = sci;
  437. gcm_iv->pn = htonl(pn);
  438. }
  439. static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
  440. {
  441. return (struct macsec_eth_header *)skb_mac_header(skb);
  442. }
  443. static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
  444. {
  445. u32 pn;
  446. spin_lock_bh(&tx_sa->lock);
  447. pn = tx_sa->next_pn;
  448. tx_sa->next_pn++;
  449. if (tx_sa->next_pn == 0) {
  450. pr_debug("PN wrapped, transitioning to !oper\n");
  451. tx_sa->active = false;
  452. if (secy->protect_frames)
  453. secy->operational = false;
  454. }
  455. spin_unlock_bh(&tx_sa->lock);
  456. return pn;
  457. }
  458. static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
  459. {
  460. struct macsec_dev *macsec = netdev_priv(dev);
  461. skb->dev = macsec->real_dev;
  462. skb_reset_mac_header(skb);
  463. skb->protocol = eth_hdr(skb)->h_proto;
  464. }
  465. static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
  466. struct macsec_tx_sa *tx_sa)
  467. {
  468. struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
  469. u64_stats_update_begin(&txsc_stats->syncp);
  470. if (tx_sc->encrypt) {
  471. txsc_stats->stats.OutOctetsEncrypted += skb->len;
  472. txsc_stats->stats.OutPktsEncrypted++;
  473. this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
  474. } else {
  475. txsc_stats->stats.OutOctetsProtected += skb->len;
  476. txsc_stats->stats.OutPktsProtected++;
  477. this_cpu_inc(tx_sa->stats->OutPktsProtected);
  478. }
  479. u64_stats_update_end(&txsc_stats->syncp);
  480. }
  481. static void count_tx(struct net_device *dev, int ret, int len)
  482. {
  483. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  484. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  485. u64_stats_update_begin(&stats->syncp);
  486. stats->tx_packets++;
  487. stats->tx_bytes += len;
  488. u64_stats_update_end(&stats->syncp);
  489. } else {
  490. dev->stats.tx_dropped++;
  491. }
  492. }
  493. static void macsec_encrypt_done(struct crypto_async_request *base, int err)
  494. {
  495. struct sk_buff *skb = base->data;
  496. struct net_device *dev = skb->dev;
  497. struct macsec_dev *macsec = macsec_priv(dev);
  498. struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
  499. int len, ret;
  500. aead_request_free(macsec_skb_cb(skb)->req);
  501. rcu_read_lock_bh();
  502. macsec_encrypt_finish(skb, dev);
  503. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  504. len = skb->len;
  505. ret = dev_queue_xmit(skb);
  506. count_tx(dev, ret, len);
  507. rcu_read_unlock_bh();
  508. macsec_txsa_put(sa);
  509. dev_put(dev);
  510. }
  511. static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
  512. struct net_device *dev)
  513. {
  514. int ret;
  515. struct scatterlist sg[MAX_SKB_FRAGS + 1];
  516. unsigned char iv[GCM_AES_IV_LEN];
  517. struct ethhdr *eth;
  518. struct macsec_eth_header *hh;
  519. size_t unprotected_len;
  520. struct aead_request *req;
  521. struct macsec_secy *secy;
  522. struct macsec_tx_sc *tx_sc;
  523. struct macsec_tx_sa *tx_sa;
  524. struct macsec_dev *macsec = macsec_priv(dev);
  525. u32 pn;
  526. secy = &macsec->secy;
  527. tx_sc = &secy->tx_sc;
  528. /* 10.5.1 TX SA assignment */
  529. tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
  530. if (!tx_sa) {
  531. secy->operational = false;
  532. kfree_skb(skb);
  533. return ERR_PTR(-EINVAL);
  534. }
  535. if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
  536. skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
  537. struct sk_buff *nskb = skb_copy_expand(skb,
  538. MACSEC_NEEDED_HEADROOM,
  539. MACSEC_NEEDED_TAILROOM,
  540. GFP_ATOMIC);
  541. if (likely(nskb)) {
  542. consume_skb(skb);
  543. skb = nskb;
  544. } else {
  545. macsec_txsa_put(tx_sa);
  546. kfree_skb(skb);
  547. return ERR_PTR(-ENOMEM);
  548. }
  549. } else {
  550. skb = skb_unshare(skb, GFP_ATOMIC);
  551. if (!skb) {
  552. macsec_txsa_put(tx_sa);
  553. return ERR_PTR(-ENOMEM);
  554. }
  555. }
  556. unprotected_len = skb->len;
  557. eth = eth_hdr(skb);
  558. hh = (struct macsec_eth_header *)skb_push(skb, macsec_extra_len(tx_sc->send_sci));
  559. memmove(hh, eth, 2 * ETH_ALEN);
  560. pn = tx_sa_update_pn(tx_sa, secy);
  561. if (pn == 0) {
  562. macsec_txsa_put(tx_sa);
  563. kfree_skb(skb);
  564. return ERR_PTR(-ENOLINK);
  565. }
  566. macsec_fill_sectag(hh, secy, pn);
  567. macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
  568. macsec_fill_iv(iv, secy->sci, pn);
  569. skb_put(skb, secy->icv_len);
  570. if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
  571. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  572. u64_stats_update_begin(&secy_stats->syncp);
  573. secy_stats->stats.OutPktsTooLong++;
  574. u64_stats_update_end(&secy_stats->syncp);
  575. macsec_txsa_put(tx_sa);
  576. kfree_skb(skb);
  577. return ERR_PTR(-EINVAL);
  578. }
  579. req = aead_request_alloc(tx_sa->key.tfm, GFP_ATOMIC);
  580. if (!req) {
  581. macsec_txsa_put(tx_sa);
  582. kfree_skb(skb);
  583. return ERR_PTR(-ENOMEM);
  584. }
  585. sg_init_table(sg, MAX_SKB_FRAGS + 1);
  586. skb_to_sgvec(skb, sg, 0, skb->len);
  587. if (tx_sc->encrypt) {
  588. int len = skb->len - macsec_hdr_len(tx_sc->send_sci) -
  589. secy->icv_len;
  590. aead_request_set_crypt(req, sg, sg, len, iv);
  591. aead_request_set_ad(req, macsec_hdr_len(tx_sc->send_sci));
  592. } else {
  593. aead_request_set_crypt(req, sg, sg, 0, iv);
  594. aead_request_set_ad(req, skb->len - secy->icv_len);
  595. }
  596. macsec_skb_cb(skb)->req = req;
  597. macsec_skb_cb(skb)->tx_sa = tx_sa;
  598. aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
  599. dev_hold(skb->dev);
  600. ret = crypto_aead_encrypt(req);
  601. if (ret == -EINPROGRESS) {
  602. return ERR_PTR(ret);
  603. } else if (ret != 0) {
  604. dev_put(skb->dev);
  605. kfree_skb(skb);
  606. aead_request_free(req);
  607. macsec_txsa_put(tx_sa);
  608. return ERR_PTR(-EINVAL);
  609. }
  610. dev_put(skb->dev);
  611. aead_request_free(req);
  612. macsec_txsa_put(tx_sa);
  613. return skb;
  614. }
  615. static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
  616. {
  617. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  618. struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
  619. struct macsec_eth_header *hdr = macsec_ethhdr(skb);
  620. u32 lowest_pn = 0;
  621. spin_lock(&rx_sa->lock);
  622. if (rx_sa->next_pn >= secy->replay_window)
  623. lowest_pn = rx_sa->next_pn - secy->replay_window;
  624. /* Now perform replay protection check again
  625. * (see IEEE 802.1AE-2006 figure 10-5)
  626. */
  627. if (secy->replay_protect && pn < lowest_pn) {
  628. spin_unlock(&rx_sa->lock);
  629. u64_stats_update_begin(&rxsc_stats->syncp);
  630. rxsc_stats->stats.InPktsLate++;
  631. u64_stats_update_end(&rxsc_stats->syncp);
  632. return false;
  633. }
  634. if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
  635. u64_stats_update_begin(&rxsc_stats->syncp);
  636. if (hdr->tci_an & MACSEC_TCI_E)
  637. rxsc_stats->stats.InOctetsDecrypted += skb->len;
  638. else
  639. rxsc_stats->stats.InOctetsValidated += skb->len;
  640. u64_stats_update_end(&rxsc_stats->syncp);
  641. }
  642. if (!macsec_skb_cb(skb)->valid) {
  643. spin_unlock(&rx_sa->lock);
  644. /* 10.6.5 */
  645. if (hdr->tci_an & MACSEC_TCI_C ||
  646. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  647. u64_stats_update_begin(&rxsc_stats->syncp);
  648. rxsc_stats->stats.InPktsNotValid++;
  649. u64_stats_update_end(&rxsc_stats->syncp);
  650. return false;
  651. }
  652. u64_stats_update_begin(&rxsc_stats->syncp);
  653. if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
  654. rxsc_stats->stats.InPktsInvalid++;
  655. this_cpu_inc(rx_sa->stats->InPktsInvalid);
  656. } else if (pn < lowest_pn) {
  657. rxsc_stats->stats.InPktsDelayed++;
  658. } else {
  659. rxsc_stats->stats.InPktsUnchecked++;
  660. }
  661. u64_stats_update_end(&rxsc_stats->syncp);
  662. } else {
  663. u64_stats_update_begin(&rxsc_stats->syncp);
  664. if (pn < lowest_pn) {
  665. rxsc_stats->stats.InPktsDelayed++;
  666. } else {
  667. rxsc_stats->stats.InPktsOK++;
  668. this_cpu_inc(rx_sa->stats->InPktsOK);
  669. }
  670. u64_stats_update_end(&rxsc_stats->syncp);
  671. if (pn >= rx_sa->next_pn)
  672. rx_sa->next_pn = pn + 1;
  673. spin_unlock(&rx_sa->lock);
  674. }
  675. return true;
  676. }
  677. static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
  678. {
  679. skb->pkt_type = PACKET_HOST;
  680. skb->protocol = eth_type_trans(skb, dev);
  681. skb_reset_network_header(skb);
  682. if (!skb_transport_header_was_set(skb))
  683. skb_reset_transport_header(skb);
  684. skb_reset_mac_len(skb);
  685. }
  686. static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
  687. {
  688. memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
  689. skb_pull(skb, hdr_len);
  690. pskb_trim_unique(skb, skb->len - icv_len);
  691. }
  692. static void count_rx(struct net_device *dev, int len)
  693. {
  694. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  695. u64_stats_update_begin(&stats->syncp);
  696. stats->rx_packets++;
  697. stats->rx_bytes += len;
  698. u64_stats_update_end(&stats->syncp);
  699. }
  700. static void macsec_decrypt_done(struct crypto_async_request *base, int err)
  701. {
  702. struct sk_buff *skb = base->data;
  703. struct net_device *dev = skb->dev;
  704. struct macsec_dev *macsec = macsec_priv(dev);
  705. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  706. int len, ret;
  707. u32 pn;
  708. aead_request_free(macsec_skb_cb(skb)->req);
  709. rcu_read_lock_bh();
  710. pn = ntohl(macsec_ethhdr(skb)->packet_number);
  711. if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
  712. rcu_read_unlock_bh();
  713. kfree_skb(skb);
  714. goto out;
  715. }
  716. macsec_finalize_skb(skb, macsec->secy.icv_len,
  717. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  718. macsec_reset_skb(skb, macsec->secy.netdev);
  719. len = skb->len;
  720. ret = netif_rx(skb);
  721. if (ret == NET_RX_SUCCESS)
  722. count_rx(dev, len);
  723. else
  724. macsec->secy.netdev->stats.rx_dropped++;
  725. rcu_read_unlock_bh();
  726. out:
  727. macsec_rxsa_put(rx_sa);
  728. dev_put(dev);
  729. return;
  730. }
  731. static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
  732. struct net_device *dev,
  733. struct macsec_rx_sa *rx_sa,
  734. sci_t sci,
  735. struct macsec_secy *secy)
  736. {
  737. int ret;
  738. struct scatterlist sg[MAX_SKB_FRAGS + 1];
  739. unsigned char iv[GCM_AES_IV_LEN];
  740. struct aead_request *req;
  741. struct macsec_eth_header *hdr;
  742. u16 icv_len = secy->icv_len;
  743. macsec_skb_cb(skb)->valid = false;
  744. skb = skb_share_check(skb, GFP_ATOMIC);
  745. if (!skb)
  746. return NULL;
  747. req = aead_request_alloc(rx_sa->key.tfm, GFP_ATOMIC);
  748. if (!req) {
  749. kfree_skb(skb);
  750. return NULL;
  751. }
  752. hdr = (struct macsec_eth_header *)skb->data;
  753. macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
  754. sg_init_table(sg, MAX_SKB_FRAGS + 1);
  755. skb_to_sgvec(skb, sg, 0, skb->len);
  756. if (hdr->tci_an & MACSEC_TCI_E) {
  757. /* confidentiality: ethernet + macsec header
  758. * authenticated, encrypted payload
  759. */
  760. int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
  761. aead_request_set_crypt(req, sg, sg, len, iv);
  762. aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
  763. skb = skb_unshare(skb, GFP_ATOMIC);
  764. if (!skb) {
  765. aead_request_free(req);
  766. return NULL;
  767. }
  768. } else {
  769. /* integrity only: all headers + data authenticated */
  770. aead_request_set_crypt(req, sg, sg, icv_len, iv);
  771. aead_request_set_ad(req, skb->len - icv_len);
  772. }
  773. macsec_skb_cb(skb)->req = req;
  774. macsec_skb_cb(skb)->rx_sa = rx_sa;
  775. skb->dev = dev;
  776. aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
  777. dev_hold(dev);
  778. ret = crypto_aead_decrypt(req);
  779. if (ret == -EINPROGRESS) {
  780. return NULL;
  781. } else if (ret != 0) {
  782. /* decryption/authentication failed
  783. * 10.6 if validateFrames is disabled, deliver anyway
  784. */
  785. if (ret != -EBADMSG) {
  786. kfree_skb(skb);
  787. skb = NULL;
  788. }
  789. } else {
  790. macsec_skb_cb(skb)->valid = true;
  791. }
  792. dev_put(dev);
  793. aead_request_free(req);
  794. return skb;
  795. }
  796. static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
  797. {
  798. struct macsec_rx_sc *rx_sc;
  799. for_each_rxsc(secy, rx_sc) {
  800. if (rx_sc->sci == sci)
  801. return rx_sc;
  802. }
  803. return NULL;
  804. }
  805. static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
  806. {
  807. struct macsec_rx_sc *rx_sc;
  808. for_each_rxsc_rtnl(secy, rx_sc) {
  809. if (rx_sc->sci == sci)
  810. return rx_sc;
  811. }
  812. return NULL;
  813. }
  814. static void handle_not_macsec(struct sk_buff *skb)
  815. {
  816. struct macsec_rxh_data *rxd;
  817. struct macsec_dev *macsec;
  818. rcu_read_lock();
  819. rxd = macsec_data_rcu(skb->dev);
  820. /* 10.6 If the management control validateFrames is not
  821. * Strict, frames without a SecTAG are received, counted, and
  822. * delivered to the Controlled Port
  823. */
  824. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  825. struct sk_buff *nskb;
  826. int ret;
  827. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  828. if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  829. u64_stats_update_begin(&secy_stats->syncp);
  830. secy_stats->stats.InPktsNoTag++;
  831. u64_stats_update_end(&secy_stats->syncp);
  832. continue;
  833. }
  834. /* deliver on this port */
  835. nskb = skb_clone(skb, GFP_ATOMIC);
  836. if (!nskb)
  837. break;
  838. nskb->dev = macsec->secy.netdev;
  839. ret = netif_rx(nskb);
  840. if (ret == NET_RX_SUCCESS) {
  841. u64_stats_update_begin(&secy_stats->syncp);
  842. secy_stats->stats.InPktsUntagged++;
  843. u64_stats_update_end(&secy_stats->syncp);
  844. } else {
  845. macsec->secy.netdev->stats.rx_dropped++;
  846. }
  847. }
  848. rcu_read_unlock();
  849. }
  850. static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
  851. {
  852. struct sk_buff *skb = *pskb;
  853. struct net_device *dev = skb->dev;
  854. struct macsec_eth_header *hdr;
  855. struct macsec_secy *secy = NULL;
  856. struct macsec_rx_sc *rx_sc;
  857. struct macsec_rx_sa *rx_sa;
  858. struct macsec_rxh_data *rxd;
  859. struct macsec_dev *macsec;
  860. sci_t sci;
  861. u32 pn;
  862. bool cbit;
  863. struct pcpu_rx_sc_stats *rxsc_stats;
  864. struct pcpu_secy_stats *secy_stats;
  865. bool pulled_sci;
  866. if (skb_headroom(skb) < ETH_HLEN)
  867. goto drop_direct;
  868. hdr = macsec_ethhdr(skb);
  869. if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
  870. handle_not_macsec(skb);
  871. /* and deliver to the uncontrolled port */
  872. return RX_HANDLER_PASS;
  873. }
  874. skb = skb_unshare(skb, GFP_ATOMIC);
  875. if (!skb) {
  876. *pskb = NULL;
  877. return RX_HANDLER_CONSUMED;
  878. }
  879. pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
  880. if (!pulled_sci) {
  881. if (!pskb_may_pull(skb, macsec_extra_len(false)))
  882. goto drop_direct;
  883. }
  884. hdr = macsec_ethhdr(skb);
  885. /* Frames with a SecTAG that has the TCI E bit set but the C
  886. * bit clear are discarded, as this reserved encoding is used
  887. * to identify frames with a SecTAG that are not to be
  888. * delivered to the Controlled Port.
  889. */
  890. if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
  891. return RX_HANDLER_PASS;
  892. /* now, pull the extra length */
  893. if (hdr->tci_an & MACSEC_TCI_SC) {
  894. if (!pulled_sci)
  895. goto drop_direct;
  896. }
  897. /* ethernet header is part of crypto processing */
  898. skb_push(skb, ETH_HLEN);
  899. macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
  900. macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
  901. sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
  902. rcu_read_lock();
  903. rxd = macsec_data_rcu(skb->dev);
  904. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  905. struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
  906. if (sc) {
  907. secy = &macsec->secy;
  908. rx_sc = sc;
  909. break;
  910. }
  911. }
  912. if (!secy)
  913. goto nosci;
  914. dev = secy->netdev;
  915. macsec = macsec_priv(dev);
  916. secy_stats = this_cpu_ptr(macsec->stats);
  917. rxsc_stats = this_cpu_ptr(rx_sc->stats);
  918. if (!macsec_validate_skb(skb, secy->icv_len)) {
  919. u64_stats_update_begin(&secy_stats->syncp);
  920. secy_stats->stats.InPktsBadTag++;
  921. u64_stats_update_end(&secy_stats->syncp);
  922. goto drop_nosa;
  923. }
  924. rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
  925. if (!rx_sa) {
  926. /* 10.6.1 if the SA is not in use */
  927. /* If validateFrames is Strict or the C bit in the
  928. * SecTAG is set, discard
  929. */
  930. if (hdr->tci_an & MACSEC_TCI_C ||
  931. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  932. u64_stats_update_begin(&rxsc_stats->syncp);
  933. rxsc_stats->stats.InPktsNotUsingSA++;
  934. u64_stats_update_end(&rxsc_stats->syncp);
  935. goto drop_nosa;
  936. }
  937. /* not Strict, the frame (with the SecTAG and ICV
  938. * removed) is delivered to the Controlled Port.
  939. */
  940. u64_stats_update_begin(&rxsc_stats->syncp);
  941. rxsc_stats->stats.InPktsUnusedSA++;
  942. u64_stats_update_end(&rxsc_stats->syncp);
  943. goto deliver;
  944. }
  945. /* First, PN check to avoid decrypting obviously wrong packets */
  946. pn = ntohl(hdr->packet_number);
  947. if (secy->replay_protect) {
  948. bool late;
  949. spin_lock(&rx_sa->lock);
  950. late = rx_sa->next_pn >= secy->replay_window &&
  951. pn < (rx_sa->next_pn - secy->replay_window);
  952. spin_unlock(&rx_sa->lock);
  953. if (late) {
  954. u64_stats_update_begin(&rxsc_stats->syncp);
  955. rxsc_stats->stats.InPktsLate++;
  956. u64_stats_update_end(&rxsc_stats->syncp);
  957. goto drop;
  958. }
  959. }
  960. /* Disabled && !changed text => skip validation */
  961. if (hdr->tci_an & MACSEC_TCI_C ||
  962. secy->validate_frames != MACSEC_VALIDATE_DISABLED)
  963. skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
  964. if (!skb) {
  965. macsec_rxsa_put(rx_sa);
  966. rcu_read_unlock();
  967. *pskb = NULL;
  968. return RX_HANDLER_CONSUMED;
  969. }
  970. if (!macsec_post_decrypt(skb, secy, pn))
  971. goto drop;
  972. deliver:
  973. macsec_finalize_skb(skb, secy->icv_len,
  974. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  975. macsec_reset_skb(skb, secy->netdev);
  976. macsec_rxsa_put(rx_sa);
  977. count_rx(dev, skb->len);
  978. rcu_read_unlock();
  979. *pskb = skb;
  980. return RX_HANDLER_ANOTHER;
  981. drop:
  982. macsec_rxsa_put(rx_sa);
  983. drop_nosa:
  984. rcu_read_unlock();
  985. drop_direct:
  986. kfree_skb(skb);
  987. *pskb = NULL;
  988. return RX_HANDLER_CONSUMED;
  989. nosci:
  990. /* 10.6.1 if the SC is not found */
  991. cbit = !!(hdr->tci_an & MACSEC_TCI_C);
  992. if (!cbit)
  993. macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
  994. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  995. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  996. struct sk_buff *nskb;
  997. int ret;
  998. secy_stats = this_cpu_ptr(macsec->stats);
  999. /* If validateFrames is Strict or the C bit in the
  1000. * SecTAG is set, discard
  1001. */
  1002. if (cbit ||
  1003. macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  1004. u64_stats_update_begin(&secy_stats->syncp);
  1005. secy_stats->stats.InPktsNoSCI++;
  1006. u64_stats_update_end(&secy_stats->syncp);
  1007. continue;
  1008. }
  1009. /* not strict, the frame (with the SecTAG and ICV
  1010. * removed) is delivered to the Controlled Port.
  1011. */
  1012. nskb = skb_clone(skb, GFP_ATOMIC);
  1013. if (!nskb)
  1014. break;
  1015. macsec_reset_skb(nskb, macsec->secy.netdev);
  1016. ret = netif_rx(nskb);
  1017. if (ret == NET_RX_SUCCESS) {
  1018. u64_stats_update_begin(&secy_stats->syncp);
  1019. secy_stats->stats.InPktsUnknownSCI++;
  1020. u64_stats_update_end(&secy_stats->syncp);
  1021. } else {
  1022. macsec->secy.netdev->stats.rx_dropped++;
  1023. }
  1024. }
  1025. rcu_read_unlock();
  1026. *pskb = skb;
  1027. return RX_HANDLER_PASS;
  1028. }
  1029. static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
  1030. {
  1031. struct crypto_aead *tfm;
  1032. int ret;
  1033. tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  1034. if (!tfm || IS_ERR(tfm))
  1035. return NULL;
  1036. ret = crypto_aead_setkey(tfm, key, key_len);
  1037. if (ret < 0) {
  1038. crypto_free_aead(tfm);
  1039. return NULL;
  1040. }
  1041. ret = crypto_aead_setauthsize(tfm, icv_len);
  1042. if (ret < 0) {
  1043. crypto_free_aead(tfm);
  1044. return NULL;
  1045. }
  1046. return tfm;
  1047. }
  1048. static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
  1049. int icv_len)
  1050. {
  1051. rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
  1052. if (!rx_sa->stats)
  1053. return -1;
  1054. rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1055. if (!rx_sa->key.tfm) {
  1056. free_percpu(rx_sa->stats);
  1057. return -1;
  1058. }
  1059. rx_sa->active = false;
  1060. rx_sa->next_pn = 1;
  1061. atomic_set(&rx_sa->refcnt, 1);
  1062. spin_lock_init(&rx_sa->lock);
  1063. return 0;
  1064. }
  1065. static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
  1066. {
  1067. rx_sa->active = false;
  1068. macsec_rxsa_put(rx_sa);
  1069. }
  1070. static void free_rx_sc(struct macsec_rx_sc *rx_sc)
  1071. {
  1072. int i;
  1073. for (i = 0; i < MACSEC_NUM_AN; i++) {
  1074. struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
  1075. RCU_INIT_POINTER(rx_sc->sa[i], NULL);
  1076. if (sa)
  1077. clear_rx_sa(sa);
  1078. }
  1079. macsec_rxsc_put(rx_sc);
  1080. }
  1081. static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
  1082. {
  1083. struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
  1084. for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
  1085. rx_sc;
  1086. rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
  1087. if (rx_sc->sci == sci) {
  1088. if (rx_sc->active)
  1089. secy->n_rx_sc--;
  1090. rcu_assign_pointer(*rx_scp, rx_sc->next);
  1091. return rx_sc;
  1092. }
  1093. }
  1094. return NULL;
  1095. }
  1096. static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
  1097. {
  1098. struct macsec_rx_sc *rx_sc;
  1099. struct macsec_dev *macsec;
  1100. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  1101. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  1102. struct macsec_secy *secy;
  1103. list_for_each_entry(macsec, &rxd->secys, secys) {
  1104. if (find_rx_sc_rtnl(&macsec->secy, sci))
  1105. return ERR_PTR(-EEXIST);
  1106. }
  1107. rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
  1108. if (!rx_sc)
  1109. return ERR_PTR(-ENOMEM);
  1110. rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
  1111. if (!rx_sc->stats) {
  1112. kfree(rx_sc);
  1113. return ERR_PTR(-ENOMEM);
  1114. }
  1115. rx_sc->sci = sci;
  1116. rx_sc->active = true;
  1117. atomic_set(&rx_sc->refcnt, 1);
  1118. secy = &macsec_priv(dev)->secy;
  1119. rcu_assign_pointer(rx_sc->next, secy->rx_sc);
  1120. rcu_assign_pointer(secy->rx_sc, rx_sc);
  1121. if (rx_sc->active)
  1122. secy->n_rx_sc++;
  1123. return rx_sc;
  1124. }
  1125. static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
  1126. int icv_len)
  1127. {
  1128. tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
  1129. if (!tx_sa->stats)
  1130. return -1;
  1131. tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1132. if (!tx_sa->key.tfm) {
  1133. free_percpu(tx_sa->stats);
  1134. return -1;
  1135. }
  1136. tx_sa->active = false;
  1137. atomic_set(&tx_sa->refcnt, 1);
  1138. spin_lock_init(&tx_sa->lock);
  1139. return 0;
  1140. }
  1141. static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
  1142. {
  1143. tx_sa->active = false;
  1144. macsec_txsa_put(tx_sa);
  1145. }
  1146. static struct genl_family macsec_fam = {
  1147. .id = GENL_ID_GENERATE,
  1148. .name = MACSEC_GENL_NAME,
  1149. .hdrsize = 0,
  1150. .version = MACSEC_GENL_VERSION,
  1151. .maxattr = MACSEC_ATTR_MAX,
  1152. .netnsok = true,
  1153. };
  1154. static struct net_device *get_dev_from_nl(struct net *net,
  1155. struct nlattr **attrs)
  1156. {
  1157. int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
  1158. struct net_device *dev;
  1159. dev = __dev_get_by_index(net, ifindex);
  1160. if (!dev)
  1161. return ERR_PTR(-ENODEV);
  1162. if (!netif_is_macsec(dev))
  1163. return ERR_PTR(-ENODEV);
  1164. return dev;
  1165. }
  1166. static sci_t nla_get_sci(const struct nlattr *nla)
  1167. {
  1168. return (__force sci_t)nla_get_u64(nla);
  1169. }
  1170. static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value)
  1171. {
  1172. return nla_put_u64(skb, attrtype, (__force u64)value);
  1173. }
  1174. static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
  1175. struct nlattr **attrs,
  1176. struct nlattr **tb_sa,
  1177. struct net_device **devp,
  1178. struct macsec_secy **secyp,
  1179. struct macsec_tx_sc **scp,
  1180. u8 *assoc_num)
  1181. {
  1182. struct net_device *dev;
  1183. struct macsec_secy *secy;
  1184. struct macsec_tx_sc *tx_sc;
  1185. struct macsec_tx_sa *tx_sa;
  1186. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1187. return ERR_PTR(-EINVAL);
  1188. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1189. dev = get_dev_from_nl(net, attrs);
  1190. if (IS_ERR(dev))
  1191. return ERR_CAST(dev);
  1192. if (*assoc_num >= MACSEC_NUM_AN)
  1193. return ERR_PTR(-EINVAL);
  1194. secy = &macsec_priv(dev)->secy;
  1195. tx_sc = &secy->tx_sc;
  1196. tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
  1197. if (!tx_sa)
  1198. return ERR_PTR(-ENODEV);
  1199. *devp = dev;
  1200. *scp = tx_sc;
  1201. *secyp = secy;
  1202. return tx_sa;
  1203. }
  1204. static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
  1205. struct nlattr **attrs,
  1206. struct nlattr **tb_rxsc,
  1207. struct net_device **devp,
  1208. struct macsec_secy **secyp)
  1209. {
  1210. struct net_device *dev;
  1211. struct macsec_secy *secy;
  1212. struct macsec_rx_sc *rx_sc;
  1213. sci_t sci;
  1214. dev = get_dev_from_nl(net, attrs);
  1215. if (IS_ERR(dev))
  1216. return ERR_CAST(dev);
  1217. secy = &macsec_priv(dev)->secy;
  1218. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1219. return ERR_PTR(-EINVAL);
  1220. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1221. rx_sc = find_rx_sc_rtnl(secy, sci);
  1222. if (!rx_sc)
  1223. return ERR_PTR(-ENODEV);
  1224. *secyp = secy;
  1225. *devp = dev;
  1226. return rx_sc;
  1227. }
  1228. static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
  1229. struct nlattr **attrs,
  1230. struct nlattr **tb_rxsc,
  1231. struct nlattr **tb_sa,
  1232. struct net_device **devp,
  1233. struct macsec_secy **secyp,
  1234. struct macsec_rx_sc **scp,
  1235. u8 *assoc_num)
  1236. {
  1237. struct macsec_rx_sc *rx_sc;
  1238. struct macsec_rx_sa *rx_sa;
  1239. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1240. return ERR_PTR(-EINVAL);
  1241. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1242. if (*assoc_num >= MACSEC_NUM_AN)
  1243. return ERR_PTR(-EINVAL);
  1244. rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
  1245. if (IS_ERR(rx_sc))
  1246. return ERR_CAST(rx_sc);
  1247. rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
  1248. if (!rx_sa)
  1249. return ERR_PTR(-ENODEV);
  1250. *scp = rx_sc;
  1251. return rx_sa;
  1252. }
  1253. static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
  1254. [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
  1255. [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
  1256. [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
  1257. };
  1258. static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
  1259. [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
  1260. [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
  1261. };
  1262. static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
  1263. [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
  1264. [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
  1265. [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
  1266. [MACSEC_SA_ATTR_KEYID] = { .type = NLA_U64 },
  1267. [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
  1268. .len = MACSEC_MAX_KEY_LEN, },
  1269. };
  1270. static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
  1271. {
  1272. if (!attrs[MACSEC_ATTR_SA_CONFIG])
  1273. return -EINVAL;
  1274. if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX, attrs[MACSEC_ATTR_SA_CONFIG],
  1275. macsec_genl_sa_policy))
  1276. return -EINVAL;
  1277. return 0;
  1278. }
  1279. static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
  1280. {
  1281. if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
  1282. return -EINVAL;
  1283. if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX, attrs[MACSEC_ATTR_RXSC_CONFIG],
  1284. macsec_genl_rxsc_policy))
  1285. return -EINVAL;
  1286. return 0;
  1287. }
  1288. static bool validate_add_rxsa(struct nlattr **attrs)
  1289. {
  1290. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1291. !attrs[MACSEC_SA_ATTR_KEY] ||
  1292. !attrs[MACSEC_SA_ATTR_KEYID])
  1293. return false;
  1294. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1295. return false;
  1296. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1297. return false;
  1298. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1299. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1300. return false;
  1301. }
  1302. return true;
  1303. }
  1304. static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
  1305. {
  1306. struct net_device *dev;
  1307. struct nlattr **attrs = info->attrs;
  1308. struct macsec_secy *secy;
  1309. struct macsec_rx_sc *rx_sc;
  1310. struct macsec_rx_sa *rx_sa;
  1311. unsigned char assoc_num;
  1312. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1313. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1314. if (!attrs[MACSEC_ATTR_IFINDEX])
  1315. return -EINVAL;
  1316. if (parse_sa_config(attrs, tb_sa))
  1317. return -EINVAL;
  1318. if (parse_rxsc_config(attrs, tb_rxsc))
  1319. return -EINVAL;
  1320. if (!validate_add_rxsa(tb_sa))
  1321. return -EINVAL;
  1322. rtnl_lock();
  1323. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1324. if (IS_ERR(rx_sc) || !macsec_rxsc_get(rx_sc)) {
  1325. rtnl_unlock();
  1326. return PTR_ERR(rx_sc);
  1327. }
  1328. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1329. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1330. pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
  1331. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1332. rtnl_unlock();
  1333. return -EINVAL;
  1334. }
  1335. rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
  1336. if (rx_sa) {
  1337. rtnl_unlock();
  1338. return -EBUSY;
  1339. }
  1340. rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
  1341. if (init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len,
  1342. secy->icv_len)) {
  1343. rtnl_unlock();
  1344. return -ENOMEM;
  1345. }
  1346. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1347. spin_lock_bh(&rx_sa->lock);
  1348. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1349. spin_unlock_bh(&rx_sa->lock);
  1350. }
  1351. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1352. rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1353. rx_sa->key.id = nla_get_u64(tb_sa[MACSEC_SA_ATTR_KEYID]);
  1354. rx_sa->sc = rx_sc;
  1355. rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
  1356. rtnl_unlock();
  1357. return 0;
  1358. }
  1359. static bool validate_add_rxsc(struct nlattr **attrs)
  1360. {
  1361. if (!attrs[MACSEC_RXSC_ATTR_SCI])
  1362. return false;
  1363. if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
  1364. if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
  1365. return false;
  1366. }
  1367. return true;
  1368. }
  1369. static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
  1370. {
  1371. struct net_device *dev;
  1372. sci_t sci = MACSEC_UNDEF_SCI;
  1373. struct nlattr **attrs = info->attrs;
  1374. struct macsec_rx_sc *rx_sc;
  1375. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1376. if (!attrs[MACSEC_ATTR_IFINDEX])
  1377. return -EINVAL;
  1378. if (parse_rxsc_config(attrs, tb_rxsc))
  1379. return -EINVAL;
  1380. if (!validate_add_rxsc(tb_rxsc))
  1381. return -EINVAL;
  1382. rtnl_lock();
  1383. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1384. if (IS_ERR(dev)) {
  1385. rtnl_unlock();
  1386. return PTR_ERR(dev);
  1387. }
  1388. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1389. rx_sc = create_rx_sc(dev, sci);
  1390. if (IS_ERR(rx_sc)) {
  1391. rtnl_unlock();
  1392. return PTR_ERR(rx_sc);
  1393. }
  1394. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
  1395. rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1396. rtnl_unlock();
  1397. return 0;
  1398. }
  1399. static bool validate_add_txsa(struct nlattr **attrs)
  1400. {
  1401. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1402. !attrs[MACSEC_SA_ATTR_PN] ||
  1403. !attrs[MACSEC_SA_ATTR_KEY] ||
  1404. !attrs[MACSEC_SA_ATTR_KEYID])
  1405. return false;
  1406. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1407. return false;
  1408. if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1409. return false;
  1410. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1411. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1412. return false;
  1413. }
  1414. return true;
  1415. }
  1416. static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
  1417. {
  1418. struct net_device *dev;
  1419. struct nlattr **attrs = info->attrs;
  1420. struct macsec_secy *secy;
  1421. struct macsec_tx_sc *tx_sc;
  1422. struct macsec_tx_sa *tx_sa;
  1423. unsigned char assoc_num;
  1424. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1425. if (!attrs[MACSEC_ATTR_IFINDEX])
  1426. return -EINVAL;
  1427. if (parse_sa_config(attrs, tb_sa))
  1428. return -EINVAL;
  1429. if (!validate_add_txsa(tb_sa))
  1430. return -EINVAL;
  1431. rtnl_lock();
  1432. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1433. if (IS_ERR(dev)) {
  1434. rtnl_unlock();
  1435. return PTR_ERR(dev);
  1436. }
  1437. secy = &macsec_priv(dev)->secy;
  1438. tx_sc = &secy->tx_sc;
  1439. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1440. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1441. pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
  1442. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1443. rtnl_unlock();
  1444. return -EINVAL;
  1445. }
  1446. tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
  1447. if (tx_sa) {
  1448. rtnl_unlock();
  1449. return -EBUSY;
  1450. }
  1451. tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
  1452. if (!tx_sa || init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1453. secy->key_len, secy->icv_len)) {
  1454. rtnl_unlock();
  1455. return -ENOMEM;
  1456. }
  1457. tx_sa->key.id = nla_get_u64(tb_sa[MACSEC_SA_ATTR_KEYID]);
  1458. spin_lock_bh(&tx_sa->lock);
  1459. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1460. spin_unlock_bh(&tx_sa->lock);
  1461. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1462. tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1463. if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
  1464. secy->operational = true;
  1465. rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
  1466. rtnl_unlock();
  1467. return 0;
  1468. }
  1469. static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
  1470. {
  1471. struct nlattr **attrs = info->attrs;
  1472. struct net_device *dev;
  1473. struct macsec_secy *secy;
  1474. struct macsec_rx_sc *rx_sc;
  1475. struct macsec_rx_sa *rx_sa;
  1476. u8 assoc_num;
  1477. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1478. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1479. if (!attrs[MACSEC_ATTR_IFINDEX])
  1480. return -EINVAL;
  1481. if (parse_sa_config(attrs, tb_sa))
  1482. return -EINVAL;
  1483. if (parse_rxsc_config(attrs, tb_rxsc))
  1484. return -EINVAL;
  1485. rtnl_lock();
  1486. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1487. &dev, &secy, &rx_sc, &assoc_num);
  1488. if (IS_ERR(rx_sa)) {
  1489. rtnl_unlock();
  1490. return PTR_ERR(rx_sa);
  1491. }
  1492. if (rx_sa->active) {
  1493. rtnl_unlock();
  1494. return -EBUSY;
  1495. }
  1496. RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
  1497. clear_rx_sa(rx_sa);
  1498. rtnl_unlock();
  1499. return 0;
  1500. }
  1501. static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
  1502. {
  1503. struct nlattr **attrs = info->attrs;
  1504. struct net_device *dev;
  1505. struct macsec_secy *secy;
  1506. struct macsec_rx_sc *rx_sc;
  1507. sci_t sci;
  1508. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1509. if (!attrs[MACSEC_ATTR_IFINDEX])
  1510. return -EINVAL;
  1511. if (parse_rxsc_config(attrs, tb_rxsc))
  1512. return -EINVAL;
  1513. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1514. return -EINVAL;
  1515. rtnl_lock();
  1516. dev = get_dev_from_nl(genl_info_net(info), info->attrs);
  1517. if (IS_ERR(dev)) {
  1518. rtnl_unlock();
  1519. return PTR_ERR(dev);
  1520. }
  1521. secy = &macsec_priv(dev)->secy;
  1522. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1523. rx_sc = del_rx_sc(secy, sci);
  1524. if (!rx_sc) {
  1525. rtnl_unlock();
  1526. return -ENODEV;
  1527. }
  1528. free_rx_sc(rx_sc);
  1529. rtnl_unlock();
  1530. return 0;
  1531. }
  1532. static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
  1533. {
  1534. struct nlattr **attrs = info->attrs;
  1535. struct net_device *dev;
  1536. struct macsec_secy *secy;
  1537. struct macsec_tx_sc *tx_sc;
  1538. struct macsec_tx_sa *tx_sa;
  1539. u8 assoc_num;
  1540. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1541. if (!attrs[MACSEC_ATTR_IFINDEX])
  1542. return -EINVAL;
  1543. if (parse_sa_config(attrs, tb_sa))
  1544. return -EINVAL;
  1545. rtnl_lock();
  1546. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1547. &dev, &secy, &tx_sc, &assoc_num);
  1548. if (IS_ERR(tx_sa)) {
  1549. rtnl_unlock();
  1550. return PTR_ERR(tx_sa);
  1551. }
  1552. if (tx_sa->active) {
  1553. rtnl_unlock();
  1554. return -EBUSY;
  1555. }
  1556. RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
  1557. clear_tx_sa(tx_sa);
  1558. rtnl_unlock();
  1559. return 0;
  1560. }
  1561. static bool validate_upd_sa(struct nlattr **attrs)
  1562. {
  1563. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1564. attrs[MACSEC_SA_ATTR_KEY] ||
  1565. attrs[MACSEC_SA_ATTR_KEYID])
  1566. return false;
  1567. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1568. return false;
  1569. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1570. return false;
  1571. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1572. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1573. return false;
  1574. }
  1575. return true;
  1576. }
  1577. static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
  1578. {
  1579. struct nlattr **attrs = info->attrs;
  1580. struct net_device *dev;
  1581. struct macsec_secy *secy;
  1582. struct macsec_tx_sc *tx_sc;
  1583. struct macsec_tx_sa *tx_sa;
  1584. u8 assoc_num;
  1585. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1586. if (!attrs[MACSEC_ATTR_IFINDEX])
  1587. return -EINVAL;
  1588. if (parse_sa_config(attrs, tb_sa))
  1589. return -EINVAL;
  1590. if (!validate_upd_sa(tb_sa))
  1591. return -EINVAL;
  1592. rtnl_lock();
  1593. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1594. &dev, &secy, &tx_sc, &assoc_num);
  1595. if (IS_ERR(tx_sa)) {
  1596. rtnl_unlock();
  1597. return PTR_ERR(tx_sa);
  1598. }
  1599. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1600. spin_lock_bh(&tx_sa->lock);
  1601. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1602. spin_unlock_bh(&tx_sa->lock);
  1603. }
  1604. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1605. tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1606. if (assoc_num == tx_sc->encoding_sa)
  1607. secy->operational = tx_sa->active;
  1608. rtnl_unlock();
  1609. return 0;
  1610. }
  1611. static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
  1612. {
  1613. struct nlattr **attrs = info->attrs;
  1614. struct net_device *dev;
  1615. struct macsec_secy *secy;
  1616. struct macsec_rx_sc *rx_sc;
  1617. struct macsec_rx_sa *rx_sa;
  1618. u8 assoc_num;
  1619. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1620. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1621. if (!attrs[MACSEC_ATTR_IFINDEX])
  1622. return -EINVAL;
  1623. if (parse_rxsc_config(attrs, tb_rxsc))
  1624. return -EINVAL;
  1625. if (parse_sa_config(attrs, tb_sa))
  1626. return -EINVAL;
  1627. if (!validate_upd_sa(tb_sa))
  1628. return -EINVAL;
  1629. rtnl_lock();
  1630. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1631. &dev, &secy, &rx_sc, &assoc_num);
  1632. if (IS_ERR(rx_sa)) {
  1633. rtnl_unlock();
  1634. return PTR_ERR(rx_sa);
  1635. }
  1636. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1637. spin_lock_bh(&rx_sa->lock);
  1638. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1639. spin_unlock_bh(&rx_sa->lock);
  1640. }
  1641. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1642. rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1643. rtnl_unlock();
  1644. return 0;
  1645. }
  1646. static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
  1647. {
  1648. struct nlattr **attrs = info->attrs;
  1649. struct net_device *dev;
  1650. struct macsec_secy *secy;
  1651. struct macsec_rx_sc *rx_sc;
  1652. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1653. if (!attrs[MACSEC_ATTR_IFINDEX])
  1654. return -EINVAL;
  1655. if (parse_rxsc_config(attrs, tb_rxsc))
  1656. return -EINVAL;
  1657. if (!validate_add_rxsc(tb_rxsc))
  1658. return -EINVAL;
  1659. rtnl_lock();
  1660. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1661. if (IS_ERR(rx_sc)) {
  1662. rtnl_unlock();
  1663. return PTR_ERR(rx_sc);
  1664. }
  1665. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
  1666. bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1667. if (rx_sc->active != new)
  1668. secy->n_rx_sc += new ? 1 : -1;
  1669. rx_sc->active = new;
  1670. }
  1671. rtnl_unlock();
  1672. return 0;
  1673. }
  1674. static int copy_tx_sa_stats(struct sk_buff *skb,
  1675. struct macsec_tx_sa_stats __percpu *pstats)
  1676. {
  1677. struct macsec_tx_sa_stats sum = {0, };
  1678. int cpu;
  1679. for_each_possible_cpu(cpu) {
  1680. const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1681. sum.OutPktsProtected += stats->OutPktsProtected;
  1682. sum.OutPktsEncrypted += stats->OutPktsEncrypted;
  1683. }
  1684. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
  1685. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
  1686. return -EMSGSIZE;
  1687. return 0;
  1688. }
  1689. static int copy_rx_sa_stats(struct sk_buff *skb,
  1690. struct macsec_rx_sa_stats __percpu *pstats)
  1691. {
  1692. struct macsec_rx_sa_stats sum = {0, };
  1693. int cpu;
  1694. for_each_possible_cpu(cpu) {
  1695. const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1696. sum.InPktsOK += stats->InPktsOK;
  1697. sum.InPktsInvalid += stats->InPktsInvalid;
  1698. sum.InPktsNotValid += stats->InPktsNotValid;
  1699. sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
  1700. sum.InPktsUnusedSA += stats->InPktsUnusedSA;
  1701. }
  1702. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
  1703. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
  1704. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
  1705. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
  1706. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
  1707. return -EMSGSIZE;
  1708. return 0;
  1709. }
  1710. static int copy_rx_sc_stats(struct sk_buff *skb,
  1711. struct pcpu_rx_sc_stats __percpu *pstats)
  1712. {
  1713. struct macsec_rx_sc_stats sum = {0, };
  1714. int cpu;
  1715. for_each_possible_cpu(cpu) {
  1716. const struct pcpu_rx_sc_stats *stats;
  1717. struct macsec_rx_sc_stats tmp;
  1718. unsigned int start;
  1719. stats = per_cpu_ptr(pstats, cpu);
  1720. do {
  1721. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1722. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1723. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1724. sum.InOctetsValidated += tmp.InOctetsValidated;
  1725. sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
  1726. sum.InPktsUnchecked += tmp.InPktsUnchecked;
  1727. sum.InPktsDelayed += tmp.InPktsDelayed;
  1728. sum.InPktsOK += tmp.InPktsOK;
  1729. sum.InPktsInvalid += tmp.InPktsInvalid;
  1730. sum.InPktsLate += tmp.InPktsLate;
  1731. sum.InPktsNotValid += tmp.InPktsNotValid;
  1732. sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
  1733. sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
  1734. }
  1735. if (nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED, sum.InOctetsValidated) ||
  1736. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED, sum.InOctetsDecrypted) ||
  1737. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED, sum.InPktsUnchecked) ||
  1738. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED, sum.InPktsDelayed) ||
  1739. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
  1740. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
  1741. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE, sum.InPktsLate) ||
  1742. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
  1743. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
  1744. nla_put_u64(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
  1745. return -EMSGSIZE;
  1746. return 0;
  1747. }
  1748. static int copy_tx_sc_stats(struct sk_buff *skb,
  1749. struct pcpu_tx_sc_stats __percpu *pstats)
  1750. {
  1751. struct macsec_tx_sc_stats sum = {0, };
  1752. int cpu;
  1753. for_each_possible_cpu(cpu) {
  1754. const struct pcpu_tx_sc_stats *stats;
  1755. struct macsec_tx_sc_stats tmp;
  1756. unsigned int start;
  1757. stats = per_cpu_ptr(pstats, cpu);
  1758. do {
  1759. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1760. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1761. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1762. sum.OutPktsProtected += tmp.OutPktsProtected;
  1763. sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
  1764. sum.OutOctetsProtected += tmp.OutOctetsProtected;
  1765. sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
  1766. }
  1767. if (nla_put_u64(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
  1768. nla_put_u64(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted) ||
  1769. nla_put_u64(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED, sum.OutOctetsProtected) ||
  1770. nla_put_u64(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED, sum.OutOctetsEncrypted))
  1771. return -EMSGSIZE;
  1772. return 0;
  1773. }
  1774. static int copy_secy_stats(struct sk_buff *skb,
  1775. struct pcpu_secy_stats __percpu *pstats)
  1776. {
  1777. struct macsec_dev_stats sum = {0, };
  1778. int cpu;
  1779. for_each_possible_cpu(cpu) {
  1780. const struct pcpu_secy_stats *stats;
  1781. struct macsec_dev_stats tmp;
  1782. unsigned int start;
  1783. stats = per_cpu_ptr(pstats, cpu);
  1784. do {
  1785. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1786. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1787. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1788. sum.OutPktsUntagged += tmp.OutPktsUntagged;
  1789. sum.InPktsUntagged += tmp.InPktsUntagged;
  1790. sum.OutPktsTooLong += tmp.OutPktsTooLong;
  1791. sum.InPktsNoTag += tmp.InPktsNoTag;
  1792. sum.InPktsBadTag += tmp.InPktsBadTag;
  1793. sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
  1794. sum.InPktsNoSCI += tmp.InPktsNoSCI;
  1795. sum.InPktsOverrun += tmp.InPktsOverrun;
  1796. }
  1797. if (nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED, sum.OutPktsUntagged) ||
  1798. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED, sum.InPktsUntagged) ||
  1799. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG, sum.OutPktsTooLong) ||
  1800. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG, sum.InPktsNoTag) ||
  1801. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG, sum.InPktsBadTag) ||
  1802. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI, sum.InPktsUnknownSCI) ||
  1803. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI, sum.InPktsNoSCI) ||
  1804. nla_put_u64(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN, sum.InPktsOverrun))
  1805. return -EMSGSIZE;
  1806. return 0;
  1807. }
  1808. static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
  1809. {
  1810. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1811. struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
  1812. if (!secy_nest)
  1813. return 1;
  1814. if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci) ||
  1815. nla_put_u64(skb, MACSEC_SECY_ATTR_CIPHER_SUITE, DEFAULT_CIPHER_ID) ||
  1816. nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
  1817. nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
  1818. nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
  1819. nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
  1820. nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
  1821. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
  1822. nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
  1823. nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
  1824. nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
  1825. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
  1826. goto cancel;
  1827. if (secy->replay_protect) {
  1828. if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
  1829. goto cancel;
  1830. }
  1831. nla_nest_end(skb, secy_nest);
  1832. return 0;
  1833. cancel:
  1834. nla_nest_cancel(skb, secy_nest);
  1835. return 1;
  1836. }
  1837. static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
  1838. struct sk_buff *skb, struct netlink_callback *cb)
  1839. {
  1840. struct macsec_rx_sc *rx_sc;
  1841. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1842. struct nlattr *txsa_list, *rxsc_list;
  1843. int i, j;
  1844. void *hdr;
  1845. struct nlattr *attr;
  1846. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1847. &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
  1848. if (!hdr)
  1849. return -EMSGSIZE;
  1850. rtnl_lock();
  1851. if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
  1852. goto nla_put_failure;
  1853. if (nla_put_secy(secy, skb))
  1854. goto nla_put_failure;
  1855. attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
  1856. if (!attr)
  1857. goto nla_put_failure;
  1858. if (copy_tx_sc_stats(skb, tx_sc->stats)) {
  1859. nla_nest_cancel(skb, attr);
  1860. goto nla_put_failure;
  1861. }
  1862. nla_nest_end(skb, attr);
  1863. attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
  1864. if (!attr)
  1865. goto nla_put_failure;
  1866. if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
  1867. nla_nest_cancel(skb, attr);
  1868. goto nla_put_failure;
  1869. }
  1870. nla_nest_end(skb, attr);
  1871. txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
  1872. if (!txsa_list)
  1873. goto nla_put_failure;
  1874. for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
  1875. struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
  1876. struct nlattr *txsa_nest;
  1877. if (!tx_sa)
  1878. continue;
  1879. txsa_nest = nla_nest_start(skb, j++);
  1880. if (!txsa_nest) {
  1881. nla_nest_cancel(skb, txsa_list);
  1882. goto nla_put_failure;
  1883. }
  1884. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  1885. nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
  1886. nla_put_u64(skb, MACSEC_SA_ATTR_KEYID, tx_sa->key.id) ||
  1887. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
  1888. nla_nest_cancel(skb, txsa_nest);
  1889. nla_nest_cancel(skb, txsa_list);
  1890. goto nla_put_failure;
  1891. }
  1892. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  1893. if (!attr) {
  1894. nla_nest_cancel(skb, txsa_nest);
  1895. nla_nest_cancel(skb, txsa_list);
  1896. goto nla_put_failure;
  1897. }
  1898. if (copy_tx_sa_stats(skb, tx_sa->stats)) {
  1899. nla_nest_cancel(skb, attr);
  1900. nla_nest_cancel(skb, txsa_nest);
  1901. nla_nest_cancel(skb, txsa_list);
  1902. goto nla_put_failure;
  1903. }
  1904. nla_nest_end(skb, attr);
  1905. nla_nest_end(skb, txsa_nest);
  1906. }
  1907. nla_nest_end(skb, txsa_list);
  1908. rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
  1909. if (!rxsc_list)
  1910. goto nla_put_failure;
  1911. j = 1;
  1912. for_each_rxsc_rtnl(secy, rx_sc) {
  1913. int k;
  1914. struct nlattr *rxsa_list;
  1915. struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
  1916. if (!rxsc_nest) {
  1917. nla_nest_cancel(skb, rxsc_list);
  1918. goto nla_put_failure;
  1919. }
  1920. if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
  1921. nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci)) {
  1922. nla_nest_cancel(skb, rxsc_nest);
  1923. nla_nest_cancel(skb, rxsc_list);
  1924. goto nla_put_failure;
  1925. }
  1926. attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
  1927. if (!attr) {
  1928. nla_nest_cancel(skb, rxsc_nest);
  1929. nla_nest_cancel(skb, rxsc_list);
  1930. goto nla_put_failure;
  1931. }
  1932. if (copy_rx_sc_stats(skb, rx_sc->stats)) {
  1933. nla_nest_cancel(skb, attr);
  1934. nla_nest_cancel(skb, rxsc_nest);
  1935. nla_nest_cancel(skb, rxsc_list);
  1936. goto nla_put_failure;
  1937. }
  1938. nla_nest_end(skb, attr);
  1939. rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
  1940. if (!rxsa_list) {
  1941. nla_nest_cancel(skb, rxsc_nest);
  1942. nla_nest_cancel(skb, rxsc_list);
  1943. goto nla_put_failure;
  1944. }
  1945. for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
  1946. struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
  1947. struct nlattr *rxsa_nest;
  1948. if (!rx_sa)
  1949. continue;
  1950. rxsa_nest = nla_nest_start(skb, k++);
  1951. if (!rxsa_nest) {
  1952. nla_nest_cancel(skb, rxsa_list);
  1953. nla_nest_cancel(skb, rxsc_nest);
  1954. nla_nest_cancel(skb, rxsc_list);
  1955. goto nla_put_failure;
  1956. }
  1957. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  1958. if (!attr) {
  1959. nla_nest_cancel(skb, rxsa_list);
  1960. nla_nest_cancel(skb, rxsc_nest);
  1961. nla_nest_cancel(skb, rxsc_list);
  1962. goto nla_put_failure;
  1963. }
  1964. if (copy_rx_sa_stats(skb, rx_sa->stats)) {
  1965. nla_nest_cancel(skb, attr);
  1966. nla_nest_cancel(skb, rxsa_list);
  1967. nla_nest_cancel(skb, rxsc_nest);
  1968. nla_nest_cancel(skb, rxsc_list);
  1969. goto nla_put_failure;
  1970. }
  1971. nla_nest_end(skb, attr);
  1972. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  1973. nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
  1974. nla_put_u64(skb, MACSEC_SA_ATTR_KEYID, rx_sa->key.id) ||
  1975. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
  1976. nla_nest_cancel(skb, rxsa_nest);
  1977. nla_nest_cancel(skb, rxsc_nest);
  1978. nla_nest_cancel(skb, rxsc_list);
  1979. goto nla_put_failure;
  1980. }
  1981. nla_nest_end(skb, rxsa_nest);
  1982. }
  1983. nla_nest_end(skb, rxsa_list);
  1984. nla_nest_end(skb, rxsc_nest);
  1985. }
  1986. nla_nest_end(skb, rxsc_list);
  1987. rtnl_unlock();
  1988. genlmsg_end(skb, hdr);
  1989. return 0;
  1990. nla_put_failure:
  1991. rtnl_unlock();
  1992. genlmsg_cancel(skb, hdr);
  1993. return -EMSGSIZE;
  1994. }
  1995. static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
  1996. {
  1997. struct net *net = sock_net(skb->sk);
  1998. struct net_device *dev;
  1999. int dev_idx, d;
  2000. dev_idx = cb->args[0];
  2001. d = 0;
  2002. for_each_netdev(net, dev) {
  2003. struct macsec_secy *secy;
  2004. if (d < dev_idx)
  2005. goto next;
  2006. if (!netif_is_macsec(dev))
  2007. goto next;
  2008. secy = &macsec_priv(dev)->secy;
  2009. if (dump_secy(secy, dev, skb, cb) < 0)
  2010. goto done;
  2011. next:
  2012. d++;
  2013. }
  2014. done:
  2015. cb->args[0] = d;
  2016. return skb->len;
  2017. }
  2018. static const struct genl_ops macsec_genl_ops[] = {
  2019. {
  2020. .cmd = MACSEC_CMD_GET_TXSC,
  2021. .dumpit = macsec_dump_txsc,
  2022. .policy = macsec_genl_policy,
  2023. },
  2024. {
  2025. .cmd = MACSEC_CMD_ADD_RXSC,
  2026. .doit = macsec_add_rxsc,
  2027. .policy = macsec_genl_policy,
  2028. .flags = GENL_ADMIN_PERM,
  2029. },
  2030. {
  2031. .cmd = MACSEC_CMD_DEL_RXSC,
  2032. .doit = macsec_del_rxsc,
  2033. .policy = macsec_genl_policy,
  2034. .flags = GENL_ADMIN_PERM,
  2035. },
  2036. {
  2037. .cmd = MACSEC_CMD_UPD_RXSC,
  2038. .doit = macsec_upd_rxsc,
  2039. .policy = macsec_genl_policy,
  2040. .flags = GENL_ADMIN_PERM,
  2041. },
  2042. {
  2043. .cmd = MACSEC_CMD_ADD_TXSA,
  2044. .doit = macsec_add_txsa,
  2045. .policy = macsec_genl_policy,
  2046. .flags = GENL_ADMIN_PERM,
  2047. },
  2048. {
  2049. .cmd = MACSEC_CMD_DEL_TXSA,
  2050. .doit = macsec_del_txsa,
  2051. .policy = macsec_genl_policy,
  2052. .flags = GENL_ADMIN_PERM,
  2053. },
  2054. {
  2055. .cmd = MACSEC_CMD_UPD_TXSA,
  2056. .doit = macsec_upd_txsa,
  2057. .policy = macsec_genl_policy,
  2058. .flags = GENL_ADMIN_PERM,
  2059. },
  2060. {
  2061. .cmd = MACSEC_CMD_ADD_RXSA,
  2062. .doit = macsec_add_rxsa,
  2063. .policy = macsec_genl_policy,
  2064. .flags = GENL_ADMIN_PERM,
  2065. },
  2066. {
  2067. .cmd = MACSEC_CMD_DEL_RXSA,
  2068. .doit = macsec_del_rxsa,
  2069. .policy = macsec_genl_policy,
  2070. .flags = GENL_ADMIN_PERM,
  2071. },
  2072. {
  2073. .cmd = MACSEC_CMD_UPD_RXSA,
  2074. .doit = macsec_upd_rxsa,
  2075. .policy = macsec_genl_policy,
  2076. .flags = GENL_ADMIN_PERM,
  2077. },
  2078. };
  2079. static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
  2080. struct net_device *dev)
  2081. {
  2082. struct macsec_dev *macsec = netdev_priv(dev);
  2083. struct macsec_secy *secy = &macsec->secy;
  2084. struct pcpu_secy_stats *secy_stats;
  2085. int ret, len;
  2086. /* 10.5 */
  2087. if (!secy->protect_frames) {
  2088. secy_stats = this_cpu_ptr(macsec->stats);
  2089. u64_stats_update_begin(&secy_stats->syncp);
  2090. secy_stats->stats.OutPktsUntagged++;
  2091. u64_stats_update_end(&secy_stats->syncp);
  2092. len = skb->len;
  2093. ret = dev_queue_xmit(skb);
  2094. count_tx(dev, ret, len);
  2095. return ret;
  2096. }
  2097. if (!secy->operational) {
  2098. kfree_skb(skb);
  2099. dev->stats.tx_dropped++;
  2100. return NETDEV_TX_OK;
  2101. }
  2102. skb = macsec_encrypt(skb, dev);
  2103. if (IS_ERR(skb)) {
  2104. if (PTR_ERR(skb) != -EINPROGRESS)
  2105. dev->stats.tx_dropped++;
  2106. return NETDEV_TX_OK;
  2107. }
  2108. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  2109. macsec_encrypt_finish(skb, dev);
  2110. len = skb->len;
  2111. ret = dev_queue_xmit(skb);
  2112. count_tx(dev, ret, len);
  2113. return ret;
  2114. }
  2115. #define MACSEC_FEATURES \
  2116. (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
  2117. static int macsec_dev_init(struct net_device *dev)
  2118. {
  2119. struct macsec_dev *macsec = macsec_priv(dev);
  2120. struct net_device *real_dev = macsec->real_dev;
  2121. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2122. if (!dev->tstats)
  2123. return -ENOMEM;
  2124. dev->features = real_dev->features & MACSEC_FEATURES;
  2125. dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2126. dev->needed_headroom = real_dev->needed_headroom +
  2127. MACSEC_NEEDED_HEADROOM;
  2128. dev->needed_tailroom = real_dev->needed_tailroom +
  2129. MACSEC_NEEDED_TAILROOM;
  2130. if (is_zero_ether_addr(dev->dev_addr))
  2131. eth_hw_addr_inherit(dev, real_dev);
  2132. if (is_zero_ether_addr(dev->broadcast))
  2133. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  2134. return 0;
  2135. }
  2136. static void macsec_dev_uninit(struct net_device *dev)
  2137. {
  2138. free_percpu(dev->tstats);
  2139. }
  2140. static netdev_features_t macsec_fix_features(struct net_device *dev,
  2141. netdev_features_t features)
  2142. {
  2143. struct macsec_dev *macsec = macsec_priv(dev);
  2144. struct net_device *real_dev = macsec->real_dev;
  2145. features &= real_dev->features & MACSEC_FEATURES;
  2146. features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2147. return features;
  2148. }
  2149. static int macsec_dev_open(struct net_device *dev)
  2150. {
  2151. struct macsec_dev *macsec = macsec_priv(dev);
  2152. struct net_device *real_dev = macsec->real_dev;
  2153. int err;
  2154. if (!(real_dev->flags & IFF_UP))
  2155. return -ENETDOWN;
  2156. err = dev_uc_add(real_dev, dev->dev_addr);
  2157. if (err < 0)
  2158. return err;
  2159. if (dev->flags & IFF_ALLMULTI) {
  2160. err = dev_set_allmulti(real_dev, 1);
  2161. if (err < 0)
  2162. goto del_unicast;
  2163. }
  2164. if (dev->flags & IFF_PROMISC) {
  2165. err = dev_set_promiscuity(real_dev, 1);
  2166. if (err < 0)
  2167. goto clear_allmulti;
  2168. }
  2169. if (netif_carrier_ok(real_dev))
  2170. netif_carrier_on(dev);
  2171. return 0;
  2172. clear_allmulti:
  2173. if (dev->flags & IFF_ALLMULTI)
  2174. dev_set_allmulti(real_dev, -1);
  2175. del_unicast:
  2176. dev_uc_del(real_dev, dev->dev_addr);
  2177. netif_carrier_off(dev);
  2178. return err;
  2179. }
  2180. static int macsec_dev_stop(struct net_device *dev)
  2181. {
  2182. struct macsec_dev *macsec = macsec_priv(dev);
  2183. struct net_device *real_dev = macsec->real_dev;
  2184. netif_carrier_off(dev);
  2185. dev_mc_unsync(real_dev, dev);
  2186. dev_uc_unsync(real_dev, dev);
  2187. if (dev->flags & IFF_ALLMULTI)
  2188. dev_set_allmulti(real_dev, -1);
  2189. if (dev->flags & IFF_PROMISC)
  2190. dev_set_promiscuity(real_dev, -1);
  2191. dev_uc_del(real_dev, dev->dev_addr);
  2192. return 0;
  2193. }
  2194. static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
  2195. {
  2196. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2197. if (!(dev->flags & IFF_UP))
  2198. return;
  2199. if (change & IFF_ALLMULTI)
  2200. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  2201. if (change & IFF_PROMISC)
  2202. dev_set_promiscuity(real_dev,
  2203. dev->flags & IFF_PROMISC ? 1 : -1);
  2204. }
  2205. static void macsec_dev_set_rx_mode(struct net_device *dev)
  2206. {
  2207. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2208. dev_mc_sync(real_dev, dev);
  2209. dev_uc_sync(real_dev, dev);
  2210. }
  2211. static int macsec_set_mac_address(struct net_device *dev, void *p)
  2212. {
  2213. struct macsec_dev *macsec = macsec_priv(dev);
  2214. struct net_device *real_dev = macsec->real_dev;
  2215. struct sockaddr *addr = p;
  2216. int err;
  2217. if (!is_valid_ether_addr(addr->sa_data))
  2218. return -EADDRNOTAVAIL;
  2219. if (!(dev->flags & IFF_UP))
  2220. goto out;
  2221. err = dev_uc_add(real_dev, addr->sa_data);
  2222. if (err < 0)
  2223. return err;
  2224. dev_uc_del(real_dev, dev->dev_addr);
  2225. out:
  2226. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2227. return 0;
  2228. }
  2229. static int macsec_change_mtu(struct net_device *dev, int new_mtu)
  2230. {
  2231. struct macsec_dev *macsec = macsec_priv(dev);
  2232. unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
  2233. if (macsec->real_dev->mtu - extra < new_mtu)
  2234. return -ERANGE;
  2235. dev->mtu = new_mtu;
  2236. return 0;
  2237. }
  2238. static struct rtnl_link_stats64 *macsec_get_stats64(struct net_device *dev,
  2239. struct rtnl_link_stats64 *s)
  2240. {
  2241. int cpu;
  2242. if (!dev->tstats)
  2243. return s;
  2244. for_each_possible_cpu(cpu) {
  2245. struct pcpu_sw_netstats *stats;
  2246. struct pcpu_sw_netstats tmp;
  2247. int start;
  2248. stats = per_cpu_ptr(dev->tstats, cpu);
  2249. do {
  2250. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2251. tmp.rx_packets = stats->rx_packets;
  2252. tmp.rx_bytes = stats->rx_bytes;
  2253. tmp.tx_packets = stats->tx_packets;
  2254. tmp.tx_bytes = stats->tx_bytes;
  2255. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2256. s->rx_packets += tmp.rx_packets;
  2257. s->rx_bytes += tmp.rx_bytes;
  2258. s->tx_packets += tmp.tx_packets;
  2259. s->tx_bytes += tmp.tx_bytes;
  2260. }
  2261. s->rx_dropped = dev->stats.rx_dropped;
  2262. s->tx_dropped = dev->stats.tx_dropped;
  2263. return s;
  2264. }
  2265. static int macsec_get_iflink(const struct net_device *dev)
  2266. {
  2267. return macsec_priv(dev)->real_dev->ifindex;
  2268. }
  2269. static const struct net_device_ops macsec_netdev_ops = {
  2270. .ndo_init = macsec_dev_init,
  2271. .ndo_uninit = macsec_dev_uninit,
  2272. .ndo_open = macsec_dev_open,
  2273. .ndo_stop = macsec_dev_stop,
  2274. .ndo_fix_features = macsec_fix_features,
  2275. .ndo_change_mtu = macsec_change_mtu,
  2276. .ndo_set_rx_mode = macsec_dev_set_rx_mode,
  2277. .ndo_change_rx_flags = macsec_dev_change_rx_flags,
  2278. .ndo_set_mac_address = macsec_set_mac_address,
  2279. .ndo_start_xmit = macsec_start_xmit,
  2280. .ndo_get_stats64 = macsec_get_stats64,
  2281. .ndo_get_iflink = macsec_get_iflink,
  2282. };
  2283. static const struct device_type macsec_type = {
  2284. .name = "macsec",
  2285. };
  2286. static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
  2287. [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
  2288. [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
  2289. [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
  2290. [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
  2291. [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
  2292. [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
  2293. [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
  2294. [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
  2295. [IFLA_MACSEC_ES] = { .type = NLA_U8 },
  2296. [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
  2297. [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
  2298. [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
  2299. };
  2300. static void macsec_free_netdev(struct net_device *dev)
  2301. {
  2302. struct macsec_dev *macsec = macsec_priv(dev);
  2303. struct net_device *real_dev = macsec->real_dev;
  2304. free_percpu(macsec->stats);
  2305. free_percpu(macsec->secy.tx_sc.stats);
  2306. dev_put(real_dev);
  2307. free_netdev(dev);
  2308. }
  2309. static void macsec_setup(struct net_device *dev)
  2310. {
  2311. ether_setup(dev);
  2312. dev->tx_queue_len = 0;
  2313. dev->netdev_ops = &macsec_netdev_ops;
  2314. dev->destructor = macsec_free_netdev;
  2315. eth_zero_addr(dev->broadcast);
  2316. }
  2317. static void macsec_changelink_common(struct net_device *dev,
  2318. struct nlattr *data[])
  2319. {
  2320. struct macsec_secy *secy;
  2321. struct macsec_tx_sc *tx_sc;
  2322. secy = &macsec_priv(dev)->secy;
  2323. tx_sc = &secy->tx_sc;
  2324. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2325. struct macsec_tx_sa *tx_sa;
  2326. tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
  2327. tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
  2328. secy->operational = tx_sa && tx_sa->active;
  2329. }
  2330. if (data[IFLA_MACSEC_WINDOW])
  2331. secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
  2332. if (data[IFLA_MACSEC_ENCRYPT])
  2333. tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
  2334. if (data[IFLA_MACSEC_PROTECT])
  2335. secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
  2336. if (data[IFLA_MACSEC_INC_SCI])
  2337. tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  2338. if (data[IFLA_MACSEC_ES])
  2339. tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
  2340. if (data[IFLA_MACSEC_SCB])
  2341. tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
  2342. if (data[IFLA_MACSEC_REPLAY_PROTECT])
  2343. secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
  2344. if (data[IFLA_MACSEC_VALIDATION])
  2345. secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
  2346. }
  2347. static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
  2348. struct nlattr *data[])
  2349. {
  2350. if (!data)
  2351. return 0;
  2352. if (data[IFLA_MACSEC_CIPHER_SUITE] ||
  2353. data[IFLA_MACSEC_ICV_LEN] ||
  2354. data[IFLA_MACSEC_SCI] ||
  2355. data[IFLA_MACSEC_PORT])
  2356. return -EINVAL;
  2357. macsec_changelink_common(dev, data);
  2358. return 0;
  2359. }
  2360. static void macsec_del_dev(struct macsec_dev *macsec)
  2361. {
  2362. int i;
  2363. while (macsec->secy.rx_sc) {
  2364. struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
  2365. rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
  2366. free_rx_sc(rx_sc);
  2367. }
  2368. for (i = 0; i < MACSEC_NUM_AN; i++) {
  2369. struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
  2370. if (sa) {
  2371. RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
  2372. clear_tx_sa(sa);
  2373. }
  2374. }
  2375. }
  2376. static void macsec_dellink(struct net_device *dev, struct list_head *head)
  2377. {
  2378. struct macsec_dev *macsec = macsec_priv(dev);
  2379. struct net_device *real_dev = macsec->real_dev;
  2380. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2381. unregister_netdevice_queue(dev, head);
  2382. list_del_rcu(&macsec->secys);
  2383. if (list_empty(&rxd->secys))
  2384. netdev_rx_handler_unregister(real_dev);
  2385. macsec_del_dev(macsec);
  2386. }
  2387. static int register_macsec_dev(struct net_device *real_dev,
  2388. struct net_device *dev)
  2389. {
  2390. struct macsec_dev *macsec = macsec_priv(dev);
  2391. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2392. if (!rxd) {
  2393. int err;
  2394. rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
  2395. if (!rxd)
  2396. return -ENOMEM;
  2397. INIT_LIST_HEAD(&rxd->secys);
  2398. err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
  2399. rxd);
  2400. if (err < 0)
  2401. return err;
  2402. }
  2403. list_add_tail_rcu(&macsec->secys, &rxd->secys);
  2404. return 0;
  2405. }
  2406. static bool sci_exists(struct net_device *dev, sci_t sci)
  2407. {
  2408. struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
  2409. struct macsec_dev *macsec;
  2410. list_for_each_entry(macsec, &rxd->secys, secys) {
  2411. if (macsec->secy.sci == sci)
  2412. return true;
  2413. }
  2414. return false;
  2415. }
  2416. static sci_t dev_to_sci(struct net_device *dev, __be16 port)
  2417. {
  2418. return make_sci(dev->dev_addr, port);
  2419. }
  2420. static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
  2421. {
  2422. struct macsec_dev *macsec = macsec_priv(dev);
  2423. struct macsec_secy *secy = &macsec->secy;
  2424. macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
  2425. if (!macsec->stats)
  2426. return -ENOMEM;
  2427. secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
  2428. if (!secy->tx_sc.stats) {
  2429. free_percpu(macsec->stats);
  2430. return -ENOMEM;
  2431. }
  2432. if (sci == MACSEC_UNDEF_SCI)
  2433. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2434. secy->netdev = dev;
  2435. secy->operational = true;
  2436. secy->key_len = DEFAULT_SAK_LEN;
  2437. secy->icv_len = icv_len;
  2438. secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
  2439. secy->protect_frames = true;
  2440. secy->replay_protect = false;
  2441. secy->sci = sci;
  2442. secy->tx_sc.active = true;
  2443. secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
  2444. secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
  2445. secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
  2446. secy->tx_sc.end_station = false;
  2447. secy->tx_sc.scb = false;
  2448. return 0;
  2449. }
  2450. static int macsec_newlink(struct net *net, struct net_device *dev,
  2451. struct nlattr *tb[], struct nlattr *data[])
  2452. {
  2453. struct macsec_dev *macsec = macsec_priv(dev);
  2454. struct net_device *real_dev;
  2455. int err;
  2456. sci_t sci;
  2457. u8 icv_len = DEFAULT_ICV_LEN;
  2458. rx_handler_func_t *rx_handler;
  2459. if (!tb[IFLA_LINK])
  2460. return -EINVAL;
  2461. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  2462. if (!real_dev)
  2463. return -ENODEV;
  2464. dev->priv_flags |= IFF_MACSEC;
  2465. macsec->real_dev = real_dev;
  2466. if (data && data[IFLA_MACSEC_ICV_LEN])
  2467. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2468. dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
  2469. rx_handler = rtnl_dereference(real_dev->rx_handler);
  2470. if (rx_handler && rx_handler != macsec_handle_frame)
  2471. return -EBUSY;
  2472. err = register_netdevice(dev);
  2473. if (err < 0)
  2474. return err;
  2475. /* need to be already registered so that ->init has run and
  2476. * the MAC addr is set
  2477. */
  2478. if (data && data[IFLA_MACSEC_SCI])
  2479. sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
  2480. else if (data && data[IFLA_MACSEC_PORT])
  2481. sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
  2482. else
  2483. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2484. if (rx_handler && sci_exists(real_dev, sci)) {
  2485. err = -EBUSY;
  2486. goto unregister;
  2487. }
  2488. err = macsec_add_dev(dev, sci, icv_len);
  2489. if (err)
  2490. goto unregister;
  2491. if (data)
  2492. macsec_changelink_common(dev, data);
  2493. err = register_macsec_dev(real_dev, dev);
  2494. if (err < 0)
  2495. goto del_dev;
  2496. dev_hold(real_dev);
  2497. return 0;
  2498. del_dev:
  2499. macsec_del_dev(macsec);
  2500. unregister:
  2501. unregister_netdevice(dev);
  2502. return err;
  2503. }
  2504. static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[])
  2505. {
  2506. u64 csid = DEFAULT_CIPHER_ID;
  2507. u8 icv_len = DEFAULT_ICV_LEN;
  2508. int flag;
  2509. bool es, scb, sci;
  2510. if (!data)
  2511. return 0;
  2512. if (data[IFLA_MACSEC_CIPHER_SUITE])
  2513. csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
  2514. if (data[IFLA_MACSEC_ICV_LEN])
  2515. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2516. switch (csid) {
  2517. case DEFAULT_CIPHER_ID:
  2518. case DEFAULT_CIPHER_ALT:
  2519. if (icv_len < MACSEC_MIN_ICV_LEN ||
  2520. icv_len > MACSEC_MAX_ICV_LEN)
  2521. return -EINVAL;
  2522. break;
  2523. default:
  2524. return -EINVAL;
  2525. }
  2526. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2527. if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
  2528. return -EINVAL;
  2529. }
  2530. for (flag = IFLA_MACSEC_ENCODING_SA + 1;
  2531. flag < IFLA_MACSEC_VALIDATION;
  2532. flag++) {
  2533. if (data[flag]) {
  2534. if (nla_get_u8(data[flag]) > 1)
  2535. return -EINVAL;
  2536. }
  2537. }
  2538. es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
  2539. sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
  2540. scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
  2541. if ((sci && (scb || es)) || (scb && es))
  2542. return -EINVAL;
  2543. if (data[IFLA_MACSEC_VALIDATION] &&
  2544. nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
  2545. return -EINVAL;
  2546. if ((data[IFLA_MACSEC_PROTECT] &&
  2547. nla_get_u8(data[IFLA_MACSEC_PROTECT])) &&
  2548. !data[IFLA_MACSEC_WINDOW])
  2549. return -EINVAL;
  2550. return 0;
  2551. }
  2552. static struct net *macsec_get_link_net(const struct net_device *dev)
  2553. {
  2554. return dev_net(macsec_priv(dev)->real_dev);
  2555. }
  2556. static size_t macsec_get_size(const struct net_device *dev)
  2557. {
  2558. return 0 +
  2559. nla_total_size(8) + /* SCI */
  2560. nla_total_size(1) + /* ICV_LEN */
  2561. nla_total_size(8) + /* CIPHER_SUITE */
  2562. nla_total_size(4) + /* WINDOW */
  2563. nla_total_size(1) + /* ENCODING_SA */
  2564. nla_total_size(1) + /* ENCRYPT */
  2565. nla_total_size(1) + /* PROTECT */
  2566. nla_total_size(1) + /* INC_SCI */
  2567. nla_total_size(1) + /* ES */
  2568. nla_total_size(1) + /* SCB */
  2569. nla_total_size(1) + /* REPLAY_PROTECT */
  2570. nla_total_size(1) + /* VALIDATION */
  2571. 0;
  2572. }
  2573. static int macsec_fill_info(struct sk_buff *skb,
  2574. const struct net_device *dev)
  2575. {
  2576. struct macsec_secy *secy = &macsec_priv(dev)->secy;
  2577. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2578. if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci) ||
  2579. nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
  2580. nla_put_u64(skb, IFLA_MACSEC_CIPHER_SUITE, DEFAULT_CIPHER_ID) ||
  2581. nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
  2582. nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
  2583. nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
  2584. nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
  2585. nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
  2586. nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
  2587. nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
  2588. nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
  2589. 0)
  2590. goto nla_put_failure;
  2591. if (secy->replay_protect) {
  2592. if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
  2593. goto nla_put_failure;
  2594. }
  2595. return 0;
  2596. nla_put_failure:
  2597. return -EMSGSIZE;
  2598. }
  2599. static struct rtnl_link_ops macsec_link_ops __read_mostly = {
  2600. .kind = "macsec",
  2601. .priv_size = sizeof(struct macsec_dev),
  2602. .maxtype = IFLA_MACSEC_MAX,
  2603. .policy = macsec_rtnl_policy,
  2604. .setup = macsec_setup,
  2605. .validate = macsec_validate_attr,
  2606. .newlink = macsec_newlink,
  2607. .changelink = macsec_changelink,
  2608. .dellink = macsec_dellink,
  2609. .get_size = macsec_get_size,
  2610. .fill_info = macsec_fill_info,
  2611. .get_link_net = macsec_get_link_net,
  2612. };
  2613. static bool is_macsec_master(struct net_device *dev)
  2614. {
  2615. return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
  2616. }
  2617. static int macsec_notify(struct notifier_block *this, unsigned long event,
  2618. void *ptr)
  2619. {
  2620. struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
  2621. LIST_HEAD(head);
  2622. if (!is_macsec_master(real_dev))
  2623. return NOTIFY_DONE;
  2624. switch (event) {
  2625. case NETDEV_UNREGISTER: {
  2626. struct macsec_dev *m, *n;
  2627. struct macsec_rxh_data *rxd;
  2628. rxd = macsec_data_rtnl(real_dev);
  2629. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2630. macsec_dellink(m->secy.netdev, &head);
  2631. }
  2632. unregister_netdevice_many(&head);
  2633. break;
  2634. }
  2635. case NETDEV_CHANGEMTU: {
  2636. struct macsec_dev *m;
  2637. struct macsec_rxh_data *rxd;
  2638. rxd = macsec_data_rtnl(real_dev);
  2639. list_for_each_entry(m, &rxd->secys, secys) {
  2640. struct net_device *dev = m->secy.netdev;
  2641. unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
  2642. macsec_extra_len(true));
  2643. if (dev->mtu > mtu)
  2644. dev_set_mtu(dev, mtu);
  2645. }
  2646. }
  2647. }
  2648. return NOTIFY_OK;
  2649. }
  2650. static struct notifier_block macsec_notifier = {
  2651. .notifier_call = macsec_notify,
  2652. };
  2653. static int __init macsec_init(void)
  2654. {
  2655. int err;
  2656. pr_info("MACsec IEEE 802.1AE\n");
  2657. err = register_netdevice_notifier(&macsec_notifier);
  2658. if (err)
  2659. return err;
  2660. err = rtnl_link_register(&macsec_link_ops);
  2661. if (err)
  2662. goto notifier;
  2663. err = genl_register_family_with_ops(&macsec_fam, macsec_genl_ops);
  2664. if (err)
  2665. goto rtnl;
  2666. return 0;
  2667. rtnl:
  2668. rtnl_link_unregister(&macsec_link_ops);
  2669. notifier:
  2670. unregister_netdevice_notifier(&macsec_notifier);
  2671. return err;
  2672. }
  2673. static void __exit macsec_exit(void)
  2674. {
  2675. genl_unregister_family(&macsec_fam);
  2676. rtnl_link_unregister(&macsec_link_ops);
  2677. unregister_netdevice_notifier(&macsec_notifier);
  2678. }
  2679. module_init(macsec_init);
  2680. module_exit(macsec_exit);
  2681. MODULE_ALIAS_RTNL_LINK("macsec");
  2682. MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
  2683. MODULE_LICENSE("GPL v2");