macsec.c 85 KB

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