macsec.c 80 KB

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