macsec.c 82 KB

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