af_mpls.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/skbuff.h>
  4. #include <linux/socket.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/net.h>
  7. #include <linux/module.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/ipv6.h>
  10. #include <linux/mpls.h>
  11. #include <linux/netconf.h>
  12. #include <linux/nospec.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/percpu.h>
  15. #include <net/gso.h>
  16. #include <net/ip.h>
  17. #include <net/dst.h>
  18. #include <net/sock.h>
  19. #include <net/arp.h>
  20. #include <net/ip_fib.h>
  21. #include <net/netevent.h>
  22. #include <net/ip_tunnels.h>
  23. #include <net/netns/generic.h>
  24. #if IS_ENABLED(CONFIG_IPV6)
  25. #include <net/ipv6.h>
  26. #endif
  27. #include <net/ipv6_stubs.h>
  28. #include <net/rtnh.h>
  29. #include "internal.h"
  30. /* max memory we will use for mpls_route */
  31. #define MAX_MPLS_ROUTE_MEM 4096
  32. /* Maximum number of labels to look ahead at when selecting a path of
  33. * a multipath route
  34. */
  35. #define MAX_MP_SELECT_LABELS 4
  36. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  37. static int label_limit = (1 << 20) - 1;
  38. static int ttl_max = 255;
  39. #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
  40. static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
  41. {
  42. return sizeof(struct mpls_shim_hdr);
  43. }
  44. static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
  45. .encap_hlen = ipgre_mpls_encap_hlen,
  46. };
  47. static int ipgre_tunnel_encap_add_mpls_ops(void)
  48. {
  49. return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  50. }
  51. static void ipgre_tunnel_encap_del_mpls_ops(void)
  52. {
  53. ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  54. }
  55. #else
  56. static int ipgre_tunnel_encap_add_mpls_ops(void)
  57. {
  58. return 0;
  59. }
  60. static void ipgre_tunnel_encap_del_mpls_ops(void)
  61. {
  62. }
  63. #endif
  64. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  65. struct nlmsghdr *nlh, struct net *net, u32 portid,
  66. unsigned int nlm_flags);
  67. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  68. {
  69. struct mpls_route *rt = NULL;
  70. if (index < net->mpls.platform_labels) {
  71. struct mpls_route __rcu **platform_label =
  72. rcu_dereference(net->mpls.platform_label);
  73. rt = rcu_dereference(platform_label[index]);
  74. }
  75. return rt;
  76. }
  77. bool mpls_output_possible(const struct net_device *dev)
  78. {
  79. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  80. }
  81. EXPORT_SYMBOL_GPL(mpls_output_possible);
  82. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  83. {
  84. return (u8 *)nh + rt->rt_via_offset;
  85. }
  86. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  87. const struct mpls_nh *nh)
  88. {
  89. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  90. }
  91. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  92. {
  93. /* The size of the layer 2.5 labels to be added for this route */
  94. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  95. }
  96. unsigned int mpls_dev_mtu(const struct net_device *dev)
  97. {
  98. /* The amount of data the layer 2 frame can hold */
  99. return dev->mtu;
  100. }
  101. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  102. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  103. {
  104. if (skb->len <= mtu)
  105. return false;
  106. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  107. return false;
  108. return true;
  109. }
  110. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  111. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  112. const struct sk_buff *skb)
  113. {
  114. struct mpls_dev *mdev;
  115. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  116. mdev = mpls_dev_get(dev);
  117. if (mdev)
  118. MPLS_INC_STATS_LEN(mdev, skb->len,
  119. tx_packets,
  120. tx_bytes);
  121. } else if (skb->protocol == htons(ETH_P_IP)) {
  122. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  123. #if IS_ENABLED(CONFIG_IPV6)
  124. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  125. struct inet6_dev *in6dev = __in6_dev_get(dev);
  126. if (in6dev)
  127. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  128. IPSTATS_MIB_OUT, skb->len);
  129. #endif
  130. }
  131. }
  132. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  133. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  134. {
  135. struct mpls_entry_decoded dec;
  136. unsigned int mpls_hdr_len = 0;
  137. struct mpls_shim_hdr *hdr;
  138. bool eli_seen = false;
  139. int label_index;
  140. u32 hash = 0;
  141. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  142. label_index++) {
  143. mpls_hdr_len += sizeof(*hdr);
  144. if (!pskb_may_pull(skb, mpls_hdr_len))
  145. break;
  146. /* Read and decode the current label */
  147. hdr = mpls_hdr(skb) + label_index;
  148. dec = mpls_entry_decode(hdr);
  149. /* RFC6790 - reserved labels MUST NOT be used as keys
  150. * for the load-balancing function
  151. */
  152. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  153. hash = jhash_1word(dec.label, hash);
  154. /* The entropy label follows the entropy label
  155. * indicator, so this means that the entropy
  156. * label was just added to the hash - no need to
  157. * go any deeper either in the label stack or in the
  158. * payload
  159. */
  160. if (eli_seen)
  161. break;
  162. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  163. eli_seen = true;
  164. }
  165. if (!dec.bos)
  166. continue;
  167. /* found bottom label; does skb have room for a header? */
  168. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  169. const struct iphdr *v4hdr;
  170. v4hdr = (const struct iphdr *)(hdr + 1);
  171. if (v4hdr->version == 4) {
  172. hash = jhash_3words(ntohl(v4hdr->saddr),
  173. ntohl(v4hdr->daddr),
  174. v4hdr->protocol, hash);
  175. } else if (v4hdr->version == 6 &&
  176. pskb_may_pull(skb, mpls_hdr_len +
  177. sizeof(struct ipv6hdr))) {
  178. const struct ipv6hdr *v6hdr;
  179. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  180. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  181. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  182. hash = jhash_1word(v6hdr->nexthdr, hash);
  183. }
  184. }
  185. break;
  186. }
  187. return hash;
  188. }
  189. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  190. {
  191. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  192. }
  193. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  194. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  195. * Since those fields can change at any moment, use READ_ONCE to
  196. * access both.
  197. */
  198. static const struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  199. struct sk_buff *skb)
  200. {
  201. u32 hash = 0;
  202. int nh_index = 0;
  203. int n = 0;
  204. u8 alive;
  205. /* No need to look further into packet if there's only
  206. * one path
  207. */
  208. if (rt->rt_nhn == 1)
  209. return rt->rt_nh;
  210. alive = READ_ONCE(rt->rt_nhn_alive);
  211. if (alive == 0)
  212. return NULL;
  213. hash = mpls_multipath_hash(rt, skb);
  214. nh_index = hash % alive;
  215. if (alive == rt->rt_nhn)
  216. goto out;
  217. for_nexthops(rt) {
  218. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  219. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  220. continue;
  221. if (n == nh_index)
  222. return nh;
  223. n++;
  224. } endfor_nexthops(rt);
  225. out:
  226. return mpls_get_nexthop(rt, nh_index);
  227. }
  228. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  229. struct sk_buff *skb, struct mpls_entry_decoded dec)
  230. {
  231. enum mpls_payload_type payload_type;
  232. bool success = false;
  233. /* The IPv4 code below accesses through the IPv4 header
  234. * checksum, which is 12 bytes into the packet.
  235. * The IPv6 code below accesses through the IPv6 hop limit
  236. * which is 8 bytes into the packet.
  237. *
  238. * For all supported cases there should always be at least 12
  239. * bytes of packet data present. The IPv4 header is 20 bytes
  240. * without options and the IPv6 header is always 40 bytes
  241. * long.
  242. */
  243. if (!pskb_may_pull(skb, 12))
  244. return false;
  245. payload_type = rt->rt_payload_type;
  246. if (payload_type == MPT_UNSPEC)
  247. payload_type = ip_hdr(skb)->version;
  248. switch (payload_type) {
  249. case MPT_IPV4: {
  250. struct iphdr *hdr4 = ip_hdr(skb);
  251. u8 new_ttl;
  252. skb->protocol = htons(ETH_P_IP);
  253. /* If propagating TTL, take the decremented TTL from
  254. * the incoming MPLS header, otherwise decrement the
  255. * TTL, but only if not 0 to avoid underflow.
  256. */
  257. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  258. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  259. net->mpls.ip_ttl_propagate))
  260. new_ttl = dec.ttl;
  261. else
  262. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  263. csum_replace2(&hdr4->check,
  264. htons(hdr4->ttl << 8),
  265. htons(new_ttl << 8));
  266. hdr4->ttl = new_ttl;
  267. success = true;
  268. break;
  269. }
  270. case MPT_IPV6: {
  271. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  272. skb->protocol = htons(ETH_P_IPV6);
  273. /* If propagating TTL, take the decremented TTL from
  274. * the incoming MPLS header, otherwise decrement the
  275. * hop limit, but only if not 0 to avoid underflow.
  276. */
  277. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  278. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  279. net->mpls.ip_ttl_propagate))
  280. hdr6->hop_limit = dec.ttl;
  281. else if (hdr6->hop_limit)
  282. hdr6->hop_limit = hdr6->hop_limit - 1;
  283. success = true;
  284. break;
  285. }
  286. case MPT_UNSPEC:
  287. /* Should have decided which protocol it is by now */
  288. break;
  289. }
  290. return success;
  291. }
  292. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  293. struct packet_type *pt, struct net_device *orig_dev)
  294. {
  295. struct net *net = dev_net(dev);
  296. struct mpls_shim_hdr *hdr;
  297. const struct mpls_nh *nh;
  298. struct mpls_route *rt;
  299. struct mpls_entry_decoded dec;
  300. struct net_device *out_dev;
  301. struct mpls_dev *out_mdev;
  302. struct mpls_dev *mdev;
  303. unsigned int hh_len;
  304. unsigned int new_header_size;
  305. unsigned int mtu;
  306. int err;
  307. /* Careful this entire function runs inside of an rcu critical section */
  308. mdev = mpls_dev_get(dev);
  309. if (!mdev)
  310. goto drop;
  311. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  312. rx_bytes);
  313. if (!mdev->input_enabled) {
  314. MPLS_INC_STATS(mdev, rx_dropped);
  315. goto drop;
  316. }
  317. if (skb->pkt_type != PACKET_HOST)
  318. goto err;
  319. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  320. goto err;
  321. if (!pskb_may_pull(skb, sizeof(*hdr)))
  322. goto err;
  323. skb_dst_drop(skb);
  324. /* Read and decode the label */
  325. hdr = mpls_hdr(skb);
  326. dec = mpls_entry_decode(hdr);
  327. rt = mpls_route_input_rcu(net, dec.label);
  328. if (!rt) {
  329. MPLS_INC_STATS(mdev, rx_noroute);
  330. goto drop;
  331. }
  332. nh = mpls_select_multipath(rt, skb);
  333. if (!nh)
  334. goto err;
  335. /* Pop the label */
  336. skb_pull(skb, sizeof(*hdr));
  337. skb_reset_network_header(skb);
  338. skb_orphan(skb);
  339. if (skb_warn_if_lro(skb))
  340. goto err;
  341. skb_forward_csum(skb);
  342. /* Verify ttl is valid */
  343. if (dec.ttl <= 1)
  344. goto err;
  345. /* Find the output device */
  346. out_dev = nh->nh_dev;
  347. if (!mpls_output_possible(out_dev))
  348. goto tx_err;
  349. /* Verify the destination can hold the packet */
  350. new_header_size = mpls_nh_header_size(nh);
  351. mtu = mpls_dev_mtu(out_dev);
  352. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  353. goto tx_err;
  354. hh_len = LL_RESERVED_SPACE(out_dev);
  355. if (!out_dev->header_ops)
  356. hh_len = 0;
  357. /* Ensure there is enough space for the headers in the skb */
  358. if (skb_cow(skb, hh_len + new_header_size))
  359. goto tx_err;
  360. skb->dev = out_dev;
  361. skb->protocol = htons(ETH_P_MPLS_UC);
  362. dec.ttl -= 1;
  363. if (unlikely(!new_header_size && dec.bos)) {
  364. /* Penultimate hop popping */
  365. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  366. goto err;
  367. } else {
  368. bool bos;
  369. int i;
  370. skb_push(skb, new_header_size);
  371. skb_reset_network_header(skb);
  372. /* Push the new labels */
  373. hdr = mpls_hdr(skb);
  374. bos = dec.bos;
  375. for (i = nh->nh_labels - 1; i >= 0; i--) {
  376. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  377. dec.ttl, 0, bos);
  378. bos = false;
  379. }
  380. }
  381. mpls_stats_inc_outucastpkts(out_dev, skb);
  382. /* If via wasn't specified then send out using device address */
  383. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  384. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  385. out_dev->dev_addr, skb);
  386. else
  387. err = neigh_xmit(nh->nh_via_table, out_dev,
  388. mpls_nh_via(rt, nh), skb);
  389. if (err)
  390. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  391. __func__, err);
  392. return 0;
  393. tx_err:
  394. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  395. if (out_mdev)
  396. MPLS_INC_STATS(out_mdev, tx_errors);
  397. goto drop;
  398. err:
  399. MPLS_INC_STATS(mdev, rx_errors);
  400. drop:
  401. kfree_skb(skb);
  402. return NET_RX_DROP;
  403. }
  404. static struct packet_type mpls_packet_type __read_mostly = {
  405. .type = cpu_to_be16(ETH_P_MPLS_UC),
  406. .func = mpls_forward,
  407. };
  408. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  409. [RTA_DST] = { .type = NLA_U32 },
  410. [RTA_OIF] = { .type = NLA_U32 },
  411. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  412. };
  413. struct mpls_route_config {
  414. u32 rc_protocol;
  415. u32 rc_ifindex;
  416. u8 rc_via_table;
  417. u8 rc_via_alen;
  418. u8 rc_via[MAX_VIA_ALEN];
  419. u32 rc_label;
  420. u8 rc_ttl_propagate;
  421. u8 rc_output_labels;
  422. u32 rc_output_label[MAX_NEW_LABELS];
  423. u32 rc_nlflags;
  424. enum mpls_payload_type rc_payload_type;
  425. struct nl_info rc_nlinfo;
  426. struct rtnexthop *rc_mp;
  427. int rc_mp_len;
  428. };
  429. /* all nexthops within a route have the same size based on max
  430. * number of labels and max via length for a hop
  431. */
  432. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  433. {
  434. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  435. struct mpls_route *rt;
  436. size_t size;
  437. size = sizeof(*rt) + num_nh * nh_size;
  438. if (size > MAX_MPLS_ROUTE_MEM)
  439. return ERR_PTR(-EINVAL);
  440. rt = kzalloc(size, GFP_KERNEL);
  441. if (!rt)
  442. return ERR_PTR(-ENOMEM);
  443. rt->rt_nhn = num_nh;
  444. rt->rt_nhn_alive = num_nh;
  445. rt->rt_nh_size = nh_size;
  446. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  447. return rt;
  448. }
  449. static void mpls_rt_free(struct mpls_route *rt)
  450. {
  451. if (rt)
  452. kfree_rcu(rt, rt_rcu);
  453. }
  454. static void mpls_notify_route(struct net *net, unsigned index,
  455. struct mpls_route *old, struct mpls_route *new,
  456. const struct nl_info *info)
  457. {
  458. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  459. unsigned portid = info ? info->portid : 0;
  460. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  461. struct mpls_route *rt = new ? new : old;
  462. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  463. /* Ignore reserved labels for now */
  464. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  465. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  466. }
  467. static void mpls_route_update(struct net *net, unsigned index,
  468. struct mpls_route *new,
  469. const struct nl_info *info)
  470. {
  471. struct mpls_route __rcu **platform_label;
  472. struct mpls_route *rt;
  473. ASSERT_RTNL();
  474. platform_label = rtnl_dereference(net->mpls.platform_label);
  475. rt = rtnl_dereference(platform_label[index]);
  476. rcu_assign_pointer(platform_label[index], new);
  477. mpls_notify_route(net, index, rt, new, info);
  478. /* If we removed a route free it now */
  479. mpls_rt_free(rt);
  480. }
  481. static unsigned find_free_label(struct net *net)
  482. {
  483. struct mpls_route __rcu **platform_label;
  484. size_t platform_labels;
  485. unsigned index;
  486. platform_label = rtnl_dereference(net->mpls.platform_label);
  487. platform_labels = net->mpls.platform_labels;
  488. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  489. index++) {
  490. if (!rtnl_dereference(platform_label[index]))
  491. return index;
  492. }
  493. return LABEL_NOT_SPECIFIED;
  494. }
  495. #if IS_ENABLED(CONFIG_INET)
  496. static struct net_device *inet_fib_lookup_dev(struct net *net,
  497. const void *addr)
  498. {
  499. struct net_device *dev;
  500. struct rtable *rt;
  501. struct in_addr daddr;
  502. memcpy(&daddr, addr, sizeof(struct in_addr));
  503. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0, RT_SCOPE_UNIVERSE);
  504. if (IS_ERR(rt))
  505. return ERR_CAST(rt);
  506. dev = rt->dst.dev;
  507. dev_hold(dev);
  508. ip_rt_put(rt);
  509. return dev;
  510. }
  511. #else
  512. static struct net_device *inet_fib_lookup_dev(struct net *net,
  513. const void *addr)
  514. {
  515. return ERR_PTR(-EAFNOSUPPORT);
  516. }
  517. #endif
  518. #if IS_ENABLED(CONFIG_IPV6)
  519. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  520. const void *addr)
  521. {
  522. struct net_device *dev;
  523. struct dst_entry *dst;
  524. struct flowi6 fl6;
  525. if (!ipv6_stub)
  526. return ERR_PTR(-EAFNOSUPPORT);
  527. memset(&fl6, 0, sizeof(fl6));
  528. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  529. dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
  530. if (IS_ERR(dst))
  531. return ERR_CAST(dst);
  532. dev = dst->dev;
  533. dev_hold(dev);
  534. dst_release(dst);
  535. return dev;
  536. }
  537. #else
  538. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  539. const void *addr)
  540. {
  541. return ERR_PTR(-EAFNOSUPPORT);
  542. }
  543. #endif
  544. static struct net_device *find_outdev(struct net *net,
  545. struct mpls_route *rt,
  546. struct mpls_nh *nh, int oif)
  547. {
  548. struct net_device *dev = NULL;
  549. if (!oif) {
  550. switch (nh->nh_via_table) {
  551. case NEIGH_ARP_TABLE:
  552. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  553. break;
  554. case NEIGH_ND_TABLE:
  555. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  556. break;
  557. case NEIGH_LINK_TABLE:
  558. break;
  559. }
  560. } else {
  561. dev = dev_get_by_index(net, oif);
  562. }
  563. if (!dev)
  564. return ERR_PTR(-ENODEV);
  565. if (IS_ERR(dev))
  566. return dev;
  567. /* The caller is holding rtnl anyways, so release the dev reference */
  568. dev_put(dev);
  569. return dev;
  570. }
  571. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  572. struct mpls_nh *nh, int oif)
  573. {
  574. struct net_device *dev = NULL;
  575. int err = -ENODEV;
  576. dev = find_outdev(net, rt, nh, oif);
  577. if (IS_ERR(dev)) {
  578. err = PTR_ERR(dev);
  579. dev = NULL;
  580. goto errout;
  581. }
  582. /* Ensure this is a supported device */
  583. err = -EINVAL;
  584. if (!mpls_dev_get(dev))
  585. goto errout;
  586. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  587. (dev->addr_len != nh->nh_via_alen))
  588. goto errout;
  589. nh->nh_dev = dev;
  590. if (!(dev->flags & IFF_UP)) {
  591. nh->nh_flags |= RTNH_F_DEAD;
  592. } else {
  593. unsigned int flags;
  594. flags = dev_get_flags(dev);
  595. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  596. nh->nh_flags |= RTNH_F_LINKDOWN;
  597. }
  598. return 0;
  599. errout:
  600. return err;
  601. }
  602. static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  603. u8 via_addr[], struct netlink_ext_ack *extack)
  604. {
  605. struct rtvia *via = nla_data(nla);
  606. int err = -EINVAL;
  607. int alen;
  608. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  609. NL_SET_ERR_MSG_ATTR(extack, nla,
  610. "Invalid attribute length for RTA_VIA");
  611. goto errout;
  612. }
  613. alen = nla_len(nla) -
  614. offsetof(struct rtvia, rtvia_addr);
  615. if (alen > MAX_VIA_ALEN) {
  616. NL_SET_ERR_MSG_ATTR(extack, nla,
  617. "Invalid address length for RTA_VIA");
  618. goto errout;
  619. }
  620. /* Validate the address family */
  621. switch (via->rtvia_family) {
  622. case AF_PACKET:
  623. *via_table = NEIGH_LINK_TABLE;
  624. break;
  625. case AF_INET:
  626. *via_table = NEIGH_ARP_TABLE;
  627. if (alen != 4)
  628. goto errout;
  629. break;
  630. case AF_INET6:
  631. *via_table = NEIGH_ND_TABLE;
  632. if (alen != 16)
  633. goto errout;
  634. break;
  635. default:
  636. /* Unsupported address family */
  637. goto errout;
  638. }
  639. memcpy(via_addr, via->rtvia_addr, alen);
  640. *via_alen = alen;
  641. err = 0;
  642. errout:
  643. return err;
  644. }
  645. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  646. struct mpls_route *rt)
  647. {
  648. struct net *net = cfg->rc_nlinfo.nl_net;
  649. struct mpls_nh *nh = rt->rt_nh;
  650. int err;
  651. int i;
  652. if (!nh)
  653. return -ENOMEM;
  654. nh->nh_labels = cfg->rc_output_labels;
  655. for (i = 0; i < nh->nh_labels; i++)
  656. nh->nh_label[i] = cfg->rc_output_label[i];
  657. nh->nh_via_table = cfg->rc_via_table;
  658. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  659. nh->nh_via_alen = cfg->rc_via_alen;
  660. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  661. if (err)
  662. goto errout;
  663. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  664. rt->rt_nhn_alive--;
  665. return 0;
  666. errout:
  667. return err;
  668. }
  669. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  670. struct mpls_nh *nh, int oif, struct nlattr *via,
  671. struct nlattr *newdst, u8 max_labels,
  672. struct netlink_ext_ack *extack)
  673. {
  674. int err = -ENOMEM;
  675. if (!nh)
  676. goto errout;
  677. if (newdst) {
  678. err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
  679. nh->nh_label, extack);
  680. if (err)
  681. goto errout;
  682. }
  683. if (via) {
  684. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  685. __mpls_nh_via(rt, nh), extack);
  686. if (err)
  687. goto errout;
  688. } else {
  689. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  690. }
  691. err = mpls_nh_assign_dev(net, rt, nh, oif);
  692. if (err)
  693. goto errout;
  694. return 0;
  695. errout:
  696. return err;
  697. }
  698. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  699. u8 cfg_via_alen, u8 *max_via_alen,
  700. u8 *max_labels)
  701. {
  702. int remaining = len;
  703. u8 nhs = 0;
  704. *max_via_alen = 0;
  705. *max_labels = 0;
  706. while (rtnh_ok(rtnh, remaining)) {
  707. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  708. int attrlen;
  709. u8 n_labels = 0;
  710. attrlen = rtnh_attrlen(rtnh);
  711. nla = nla_find(attrs, attrlen, RTA_VIA);
  712. if (nla && nla_len(nla) >=
  713. offsetof(struct rtvia, rtvia_addr)) {
  714. int via_alen = nla_len(nla) -
  715. offsetof(struct rtvia, rtvia_addr);
  716. if (via_alen <= MAX_VIA_ALEN)
  717. *max_via_alen = max_t(u16, *max_via_alen,
  718. via_alen);
  719. }
  720. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  721. if (nla &&
  722. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
  723. NULL, NULL) != 0)
  724. return 0;
  725. *max_labels = max_t(u8, *max_labels, n_labels);
  726. /* number of nexthops is tracked by a u8.
  727. * Check for overflow.
  728. */
  729. if (nhs == 255)
  730. return 0;
  731. nhs++;
  732. rtnh = rtnh_next(rtnh, &remaining);
  733. }
  734. /* leftover implies invalid nexthop configuration, discard it */
  735. return remaining > 0 ? 0 : nhs;
  736. }
  737. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  738. struct mpls_route *rt, u8 max_labels,
  739. struct netlink_ext_ack *extack)
  740. {
  741. struct rtnexthop *rtnh = cfg->rc_mp;
  742. struct nlattr *nla_via, *nla_newdst;
  743. int remaining = cfg->rc_mp_len;
  744. int err = 0;
  745. u8 nhs = 0;
  746. change_nexthops(rt) {
  747. int attrlen;
  748. nla_via = NULL;
  749. nla_newdst = NULL;
  750. err = -EINVAL;
  751. if (!rtnh_ok(rtnh, remaining))
  752. goto errout;
  753. /* neither weighted multipath nor any flags
  754. * are supported
  755. */
  756. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  757. goto errout;
  758. attrlen = rtnh_attrlen(rtnh);
  759. if (attrlen > 0) {
  760. struct nlattr *attrs = rtnh_attrs(rtnh);
  761. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  762. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  763. }
  764. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  765. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  766. max_labels, extack);
  767. if (err)
  768. goto errout;
  769. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  770. rt->rt_nhn_alive--;
  771. rtnh = rtnh_next(rtnh, &remaining);
  772. nhs++;
  773. } endfor_nexthops(rt);
  774. rt->rt_nhn = nhs;
  775. return 0;
  776. errout:
  777. return err;
  778. }
  779. static bool mpls_label_ok(struct net *net, unsigned int *index,
  780. struct netlink_ext_ack *extack)
  781. {
  782. bool is_ok = true;
  783. /* Reserved labels may not be set */
  784. if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
  785. NL_SET_ERR_MSG(extack,
  786. "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
  787. is_ok = false;
  788. }
  789. /* The full 20 bit range may not be supported. */
  790. if (is_ok && *index >= net->mpls.platform_labels) {
  791. NL_SET_ERR_MSG(extack,
  792. "Label >= configured maximum in platform_labels");
  793. is_ok = false;
  794. }
  795. *index = array_index_nospec(*index, net->mpls.platform_labels);
  796. return is_ok;
  797. }
  798. static int mpls_route_add(struct mpls_route_config *cfg,
  799. struct netlink_ext_ack *extack)
  800. {
  801. struct mpls_route __rcu **platform_label;
  802. struct net *net = cfg->rc_nlinfo.nl_net;
  803. struct mpls_route *rt, *old;
  804. int err = -EINVAL;
  805. u8 max_via_alen;
  806. unsigned index;
  807. u8 max_labels;
  808. u8 nhs;
  809. index = cfg->rc_label;
  810. /* If a label was not specified during insert pick one */
  811. if ((index == LABEL_NOT_SPECIFIED) &&
  812. (cfg->rc_nlflags & NLM_F_CREATE)) {
  813. index = find_free_label(net);
  814. }
  815. if (!mpls_label_ok(net, &index, extack))
  816. goto errout;
  817. /* Append makes no sense with mpls */
  818. err = -EOPNOTSUPP;
  819. if (cfg->rc_nlflags & NLM_F_APPEND) {
  820. NL_SET_ERR_MSG(extack, "MPLS does not support route append");
  821. goto errout;
  822. }
  823. err = -EEXIST;
  824. platform_label = rtnl_dereference(net->mpls.platform_label);
  825. old = rtnl_dereference(platform_label[index]);
  826. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  827. goto errout;
  828. err = -EEXIST;
  829. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  830. goto errout;
  831. err = -ENOENT;
  832. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  833. goto errout;
  834. err = -EINVAL;
  835. if (cfg->rc_mp) {
  836. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  837. cfg->rc_via_alen, &max_via_alen,
  838. &max_labels);
  839. } else {
  840. max_via_alen = cfg->rc_via_alen;
  841. max_labels = cfg->rc_output_labels;
  842. nhs = 1;
  843. }
  844. if (nhs == 0) {
  845. NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
  846. goto errout;
  847. }
  848. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  849. if (IS_ERR(rt)) {
  850. err = PTR_ERR(rt);
  851. goto errout;
  852. }
  853. rt->rt_protocol = cfg->rc_protocol;
  854. rt->rt_payload_type = cfg->rc_payload_type;
  855. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  856. if (cfg->rc_mp)
  857. err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
  858. else
  859. err = mpls_nh_build_from_cfg(cfg, rt);
  860. if (err)
  861. goto freert;
  862. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  863. return 0;
  864. freert:
  865. mpls_rt_free(rt);
  866. errout:
  867. return err;
  868. }
  869. static int mpls_route_del(struct mpls_route_config *cfg,
  870. struct netlink_ext_ack *extack)
  871. {
  872. struct net *net = cfg->rc_nlinfo.nl_net;
  873. unsigned index;
  874. int err = -EINVAL;
  875. index = cfg->rc_label;
  876. if (!mpls_label_ok(net, &index, extack))
  877. goto errout;
  878. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  879. err = 0;
  880. errout:
  881. return err;
  882. }
  883. static void mpls_get_stats(struct mpls_dev *mdev,
  884. struct mpls_link_stats *stats)
  885. {
  886. struct mpls_pcpu_stats *p;
  887. int i;
  888. memset(stats, 0, sizeof(*stats));
  889. for_each_possible_cpu(i) {
  890. struct mpls_link_stats local;
  891. unsigned int start;
  892. p = per_cpu_ptr(mdev->stats, i);
  893. do {
  894. start = u64_stats_fetch_begin(&p->syncp);
  895. local = p->stats;
  896. } while (u64_stats_fetch_retry(&p->syncp, start));
  897. stats->rx_packets += local.rx_packets;
  898. stats->rx_bytes += local.rx_bytes;
  899. stats->tx_packets += local.tx_packets;
  900. stats->tx_bytes += local.tx_bytes;
  901. stats->rx_errors += local.rx_errors;
  902. stats->tx_errors += local.tx_errors;
  903. stats->rx_dropped += local.rx_dropped;
  904. stats->tx_dropped += local.tx_dropped;
  905. stats->rx_noroute += local.rx_noroute;
  906. }
  907. }
  908. static int mpls_fill_stats_af(struct sk_buff *skb,
  909. const struct net_device *dev)
  910. {
  911. struct mpls_link_stats *stats;
  912. struct mpls_dev *mdev;
  913. struct nlattr *nla;
  914. mdev = mpls_dev_get(dev);
  915. if (!mdev)
  916. return -ENODATA;
  917. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  918. sizeof(struct mpls_link_stats),
  919. MPLS_STATS_UNSPEC);
  920. if (!nla)
  921. return -EMSGSIZE;
  922. stats = nla_data(nla);
  923. mpls_get_stats(mdev, stats);
  924. return 0;
  925. }
  926. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  927. {
  928. struct mpls_dev *mdev;
  929. mdev = mpls_dev_get(dev);
  930. if (!mdev)
  931. return 0;
  932. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  933. }
  934. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  935. u32 portid, u32 seq, int event,
  936. unsigned int flags, int type)
  937. {
  938. struct nlmsghdr *nlh;
  939. struct netconfmsg *ncm;
  940. bool all = false;
  941. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  942. flags);
  943. if (!nlh)
  944. return -EMSGSIZE;
  945. if (type == NETCONFA_ALL)
  946. all = true;
  947. ncm = nlmsg_data(nlh);
  948. ncm->ncm_family = AF_MPLS;
  949. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  950. goto nla_put_failure;
  951. if ((all || type == NETCONFA_INPUT) &&
  952. nla_put_s32(skb, NETCONFA_INPUT,
  953. READ_ONCE(mdev->input_enabled)) < 0)
  954. goto nla_put_failure;
  955. nlmsg_end(skb, nlh);
  956. return 0;
  957. nla_put_failure:
  958. nlmsg_cancel(skb, nlh);
  959. return -EMSGSIZE;
  960. }
  961. static int mpls_netconf_msgsize_devconf(int type)
  962. {
  963. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  964. + nla_total_size(4); /* NETCONFA_IFINDEX */
  965. bool all = false;
  966. if (type == NETCONFA_ALL)
  967. all = true;
  968. if (all || type == NETCONFA_INPUT)
  969. size += nla_total_size(4);
  970. return size;
  971. }
  972. static void mpls_netconf_notify_devconf(struct net *net, int event,
  973. int type, struct mpls_dev *mdev)
  974. {
  975. struct sk_buff *skb;
  976. int err = -ENOBUFS;
  977. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  978. if (!skb)
  979. goto errout;
  980. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  981. if (err < 0) {
  982. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  983. WARN_ON(err == -EMSGSIZE);
  984. kfree_skb(skb);
  985. goto errout;
  986. }
  987. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  988. return;
  989. errout:
  990. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  991. }
  992. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  993. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  994. };
  995. static int mpls_netconf_valid_get_req(struct sk_buff *skb,
  996. const struct nlmsghdr *nlh,
  997. struct nlattr **tb,
  998. struct netlink_ext_ack *extack)
  999. {
  1000. int i, err;
  1001. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  1002. NL_SET_ERR_MSG_MOD(extack,
  1003. "Invalid header for netconf get request");
  1004. return -EINVAL;
  1005. }
  1006. if (!netlink_strict_get_check(skb))
  1007. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  1008. tb, NETCONFA_MAX,
  1009. devconf_mpls_policy, extack);
  1010. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  1011. tb, NETCONFA_MAX,
  1012. devconf_mpls_policy, extack);
  1013. if (err)
  1014. return err;
  1015. for (i = 0; i <= NETCONFA_MAX; i++) {
  1016. if (!tb[i])
  1017. continue;
  1018. switch (i) {
  1019. case NETCONFA_IFINDEX:
  1020. break;
  1021. default:
  1022. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
  1023. return -EINVAL;
  1024. }
  1025. }
  1026. return 0;
  1027. }
  1028. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  1029. struct nlmsghdr *nlh,
  1030. struct netlink_ext_ack *extack)
  1031. {
  1032. struct net *net = sock_net(in_skb->sk);
  1033. struct nlattr *tb[NETCONFA_MAX + 1];
  1034. struct net_device *dev;
  1035. struct mpls_dev *mdev;
  1036. struct sk_buff *skb;
  1037. int ifindex;
  1038. int err;
  1039. err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
  1040. if (err < 0)
  1041. goto errout;
  1042. err = -EINVAL;
  1043. if (!tb[NETCONFA_IFINDEX])
  1044. goto errout;
  1045. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1046. dev = __dev_get_by_index(net, ifindex);
  1047. if (!dev)
  1048. goto errout;
  1049. mdev = mpls_dev_get(dev);
  1050. if (!mdev)
  1051. goto errout;
  1052. err = -ENOBUFS;
  1053. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1054. if (!skb)
  1055. goto errout;
  1056. err = mpls_netconf_fill_devconf(skb, mdev,
  1057. NETLINK_CB(in_skb).portid,
  1058. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1059. NETCONFA_ALL);
  1060. if (err < 0) {
  1061. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1062. WARN_ON(err == -EMSGSIZE);
  1063. kfree_skb(skb);
  1064. goto errout;
  1065. }
  1066. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1067. errout:
  1068. return err;
  1069. }
  1070. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  1071. struct netlink_callback *cb)
  1072. {
  1073. const struct nlmsghdr *nlh = cb->nlh;
  1074. struct net *net = sock_net(skb->sk);
  1075. struct {
  1076. unsigned long ifindex;
  1077. } *ctx = (void *)cb->ctx;
  1078. struct net_device *dev;
  1079. struct mpls_dev *mdev;
  1080. int err = 0;
  1081. if (cb->strict_check) {
  1082. struct netlink_ext_ack *extack = cb->extack;
  1083. struct netconfmsg *ncm;
  1084. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  1085. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  1086. return -EINVAL;
  1087. }
  1088. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  1089. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  1090. return -EINVAL;
  1091. }
  1092. }
  1093. rcu_read_lock();
  1094. for_each_netdev_dump(net, dev, ctx->ifindex) {
  1095. mdev = mpls_dev_get(dev);
  1096. if (!mdev)
  1097. continue;
  1098. err = mpls_netconf_fill_devconf(skb, mdev,
  1099. NETLINK_CB(cb->skb).portid,
  1100. nlh->nlmsg_seq,
  1101. RTM_NEWNETCONF,
  1102. NLM_F_MULTI,
  1103. NETCONFA_ALL);
  1104. if (err < 0)
  1105. break;
  1106. }
  1107. rcu_read_unlock();
  1108. return err;
  1109. }
  1110. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  1111. (&((struct mpls_dev *)0)->field)
  1112. static int mpls_conf_proc(const struct ctl_table *ctl, int write,
  1113. void *buffer, size_t *lenp, loff_t *ppos)
  1114. {
  1115. int oval = *(int *)ctl->data;
  1116. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1117. if (write) {
  1118. struct mpls_dev *mdev = ctl->extra1;
  1119. int i = (int *)ctl->data - (int *)mdev;
  1120. struct net *net = ctl->extra2;
  1121. int val = *(int *)ctl->data;
  1122. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1123. val != oval) {
  1124. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1125. NETCONFA_INPUT, mdev);
  1126. }
  1127. }
  1128. return ret;
  1129. }
  1130. static const struct ctl_table mpls_dev_table[] = {
  1131. {
  1132. .procname = "input",
  1133. .maxlen = sizeof(int),
  1134. .mode = 0644,
  1135. .proc_handler = mpls_conf_proc,
  1136. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1137. },
  1138. };
  1139. static int mpls_dev_sysctl_register(struct net_device *dev,
  1140. struct mpls_dev *mdev)
  1141. {
  1142. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1143. size_t table_size = ARRAY_SIZE(mpls_dev_table);
  1144. struct net *net = dev_net(dev);
  1145. struct ctl_table *table;
  1146. int i;
  1147. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1148. if (!table)
  1149. goto out;
  1150. /* Table data contains only offsets relative to the base of
  1151. * the mdev at this point, so make them absolute.
  1152. */
  1153. for (i = 0; i < table_size; i++) {
  1154. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1155. table[i].extra1 = mdev;
  1156. table[i].extra2 = net;
  1157. }
  1158. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1159. mdev->sysctl = register_net_sysctl_sz(net, path, table, table_size);
  1160. if (!mdev->sysctl)
  1161. goto free;
  1162. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1163. return 0;
  1164. free:
  1165. kfree(table);
  1166. out:
  1167. mdev->sysctl = NULL;
  1168. return -ENOBUFS;
  1169. }
  1170. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1171. struct mpls_dev *mdev)
  1172. {
  1173. struct net *net = dev_net(dev);
  1174. const struct ctl_table *table;
  1175. if (!mdev->sysctl)
  1176. return;
  1177. table = mdev->sysctl->ctl_table_arg;
  1178. unregister_net_sysctl_table(mdev->sysctl);
  1179. kfree(table);
  1180. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1181. }
  1182. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1183. {
  1184. struct mpls_dev *mdev;
  1185. int err = -ENOMEM;
  1186. int i;
  1187. ASSERT_RTNL();
  1188. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1189. if (!mdev)
  1190. return ERR_PTR(err);
  1191. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1192. if (!mdev->stats)
  1193. goto free;
  1194. for_each_possible_cpu(i) {
  1195. struct mpls_pcpu_stats *mpls_stats;
  1196. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1197. u64_stats_init(&mpls_stats->syncp);
  1198. }
  1199. mdev->dev = dev;
  1200. err = mpls_dev_sysctl_register(dev, mdev);
  1201. if (err)
  1202. goto free;
  1203. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1204. return mdev;
  1205. free:
  1206. free_percpu(mdev->stats);
  1207. kfree(mdev);
  1208. return ERR_PTR(err);
  1209. }
  1210. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1211. {
  1212. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1213. free_percpu(mdev->stats);
  1214. kfree(mdev);
  1215. }
  1216. static int mpls_ifdown(struct net_device *dev, int event)
  1217. {
  1218. struct mpls_route __rcu **platform_label;
  1219. struct net *net = dev_net(dev);
  1220. unsigned index;
  1221. platform_label = rtnl_dereference(net->mpls.platform_label);
  1222. for (index = 0; index < net->mpls.platform_labels; index++) {
  1223. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1224. bool nh_del = false;
  1225. u8 alive = 0;
  1226. if (!rt)
  1227. continue;
  1228. if (event == NETDEV_UNREGISTER) {
  1229. u8 deleted = 0;
  1230. for_nexthops(rt) {
  1231. if (!nh->nh_dev || nh->nh_dev == dev)
  1232. deleted++;
  1233. if (nh->nh_dev == dev)
  1234. nh_del = true;
  1235. } endfor_nexthops(rt);
  1236. /* if there are no more nexthops, delete the route */
  1237. if (deleted == rt->rt_nhn) {
  1238. mpls_route_update(net, index, NULL, NULL);
  1239. continue;
  1240. }
  1241. if (nh_del) {
  1242. size_t size = sizeof(*rt) + rt->rt_nhn *
  1243. rt->rt_nh_size;
  1244. struct mpls_route *orig = rt;
  1245. rt = kmemdup(orig, size, GFP_KERNEL);
  1246. if (!rt)
  1247. return -ENOMEM;
  1248. }
  1249. }
  1250. change_nexthops(rt) {
  1251. unsigned int nh_flags = nh->nh_flags;
  1252. if (nh->nh_dev != dev)
  1253. goto next;
  1254. switch (event) {
  1255. case NETDEV_DOWN:
  1256. case NETDEV_UNREGISTER:
  1257. nh_flags |= RTNH_F_DEAD;
  1258. fallthrough;
  1259. case NETDEV_CHANGE:
  1260. nh_flags |= RTNH_F_LINKDOWN;
  1261. break;
  1262. }
  1263. if (event == NETDEV_UNREGISTER)
  1264. nh->nh_dev = NULL;
  1265. if (nh->nh_flags != nh_flags)
  1266. WRITE_ONCE(nh->nh_flags, nh_flags);
  1267. next:
  1268. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1269. alive++;
  1270. } endfor_nexthops(rt);
  1271. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1272. if (nh_del)
  1273. mpls_route_update(net, index, rt, NULL);
  1274. }
  1275. return 0;
  1276. }
  1277. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1278. {
  1279. struct mpls_route __rcu **platform_label;
  1280. struct net *net = dev_net(dev);
  1281. unsigned index;
  1282. u8 alive;
  1283. platform_label = rtnl_dereference(net->mpls.platform_label);
  1284. for (index = 0; index < net->mpls.platform_labels; index++) {
  1285. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1286. if (!rt)
  1287. continue;
  1288. alive = 0;
  1289. change_nexthops(rt) {
  1290. unsigned int nh_flags = nh->nh_flags;
  1291. if (!(nh_flags & flags)) {
  1292. alive++;
  1293. continue;
  1294. }
  1295. if (nh->nh_dev != dev)
  1296. continue;
  1297. alive++;
  1298. nh_flags &= ~flags;
  1299. WRITE_ONCE(nh->nh_flags, nh_flags);
  1300. } endfor_nexthops(rt);
  1301. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1302. }
  1303. }
  1304. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1305. void *ptr)
  1306. {
  1307. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1308. struct mpls_dev *mdev;
  1309. unsigned int flags;
  1310. int err;
  1311. if (event == NETDEV_REGISTER) {
  1312. mdev = mpls_add_dev(dev);
  1313. if (IS_ERR(mdev))
  1314. return notifier_from_errno(PTR_ERR(mdev));
  1315. return NOTIFY_OK;
  1316. }
  1317. mdev = mpls_dev_get(dev);
  1318. if (!mdev)
  1319. return NOTIFY_OK;
  1320. switch (event) {
  1321. case NETDEV_DOWN:
  1322. err = mpls_ifdown(dev, event);
  1323. if (err)
  1324. return notifier_from_errno(err);
  1325. break;
  1326. case NETDEV_UP:
  1327. flags = dev_get_flags(dev);
  1328. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1329. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1330. else
  1331. mpls_ifup(dev, RTNH_F_DEAD);
  1332. break;
  1333. case NETDEV_CHANGE:
  1334. flags = dev_get_flags(dev);
  1335. if (flags & (IFF_RUNNING | IFF_LOWER_UP)) {
  1336. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1337. } else {
  1338. err = mpls_ifdown(dev, event);
  1339. if (err)
  1340. return notifier_from_errno(err);
  1341. }
  1342. break;
  1343. case NETDEV_UNREGISTER:
  1344. err = mpls_ifdown(dev, event);
  1345. if (err)
  1346. return notifier_from_errno(err);
  1347. mdev = mpls_dev_get(dev);
  1348. if (mdev) {
  1349. mpls_dev_sysctl_unregister(dev, mdev);
  1350. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1351. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1352. }
  1353. break;
  1354. case NETDEV_CHANGENAME:
  1355. mdev = mpls_dev_get(dev);
  1356. if (mdev) {
  1357. mpls_dev_sysctl_unregister(dev, mdev);
  1358. err = mpls_dev_sysctl_register(dev, mdev);
  1359. if (err)
  1360. return notifier_from_errno(err);
  1361. }
  1362. break;
  1363. }
  1364. return NOTIFY_OK;
  1365. }
  1366. static struct notifier_block mpls_dev_notifier = {
  1367. .notifier_call = mpls_dev_notify,
  1368. };
  1369. static int nla_put_via(struct sk_buff *skb,
  1370. u8 table, const void *addr, int alen)
  1371. {
  1372. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1373. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1374. };
  1375. struct nlattr *nla;
  1376. struct rtvia *via;
  1377. int family = AF_UNSPEC;
  1378. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1379. if (!nla)
  1380. return -EMSGSIZE;
  1381. if (table <= NEIGH_NR_TABLES)
  1382. family = table_to_family[table];
  1383. via = nla_data(nla);
  1384. via->rtvia_family = family;
  1385. memcpy(via->rtvia_addr, addr, alen);
  1386. return 0;
  1387. }
  1388. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1389. u8 labels, const u32 label[])
  1390. {
  1391. struct nlattr *nla;
  1392. struct mpls_shim_hdr *nla_label;
  1393. bool bos;
  1394. int i;
  1395. nla = nla_reserve(skb, attrtype, labels*4);
  1396. if (!nla)
  1397. return -EMSGSIZE;
  1398. nla_label = nla_data(nla);
  1399. bos = true;
  1400. for (i = labels - 1; i >= 0; i--) {
  1401. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1402. bos = false;
  1403. }
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL_GPL(nla_put_labels);
  1407. int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
  1408. u32 label[], struct netlink_ext_ack *extack)
  1409. {
  1410. unsigned len = nla_len(nla);
  1411. struct mpls_shim_hdr *nla_label;
  1412. u8 nla_labels;
  1413. bool bos;
  1414. int i;
  1415. /* len needs to be an even multiple of 4 (the label size). Number
  1416. * of labels is a u8 so check for overflow.
  1417. */
  1418. if (len & 3 || len / 4 > 255) {
  1419. NL_SET_ERR_MSG_ATTR(extack, nla,
  1420. "Invalid length for labels attribute");
  1421. return -EINVAL;
  1422. }
  1423. /* Limit the number of new labels allowed */
  1424. nla_labels = len/4;
  1425. if (nla_labels > max_labels) {
  1426. NL_SET_ERR_MSG(extack, "Too many labels");
  1427. return -EINVAL;
  1428. }
  1429. /* when label == NULL, caller wants number of labels */
  1430. if (!label)
  1431. goto out;
  1432. nla_label = nla_data(nla);
  1433. bos = true;
  1434. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1435. struct mpls_entry_decoded dec;
  1436. dec = mpls_entry_decode(nla_label + i);
  1437. /* Ensure the bottom of stack flag is properly set
  1438. * and ttl and tc are both clear.
  1439. */
  1440. if (dec.ttl) {
  1441. NL_SET_ERR_MSG_ATTR(extack, nla,
  1442. "TTL in label must be 0");
  1443. return -EINVAL;
  1444. }
  1445. if (dec.tc) {
  1446. NL_SET_ERR_MSG_ATTR(extack, nla,
  1447. "Traffic class in label must be 0");
  1448. return -EINVAL;
  1449. }
  1450. if (dec.bos != bos) {
  1451. NL_SET_BAD_ATTR(extack, nla);
  1452. if (bos) {
  1453. NL_SET_ERR_MSG(extack,
  1454. "BOS bit must be set in first label");
  1455. } else {
  1456. NL_SET_ERR_MSG(extack,
  1457. "BOS bit can only be set in first label");
  1458. }
  1459. return -EINVAL;
  1460. }
  1461. switch (dec.label) {
  1462. case MPLS_LABEL_IMPLNULL:
  1463. /* RFC3032: This is a label that an LSR may
  1464. * assign and distribute, but which never
  1465. * actually appears in the encapsulation.
  1466. */
  1467. NL_SET_ERR_MSG_ATTR(extack, nla,
  1468. "Implicit NULL Label (3) can not be used in encapsulation");
  1469. return -EINVAL;
  1470. }
  1471. label[i] = dec.label;
  1472. }
  1473. out:
  1474. *labels = nla_labels;
  1475. return 0;
  1476. }
  1477. EXPORT_SYMBOL_GPL(nla_get_labels);
  1478. static int rtm_to_route_config(struct sk_buff *skb,
  1479. struct nlmsghdr *nlh,
  1480. struct mpls_route_config *cfg,
  1481. struct netlink_ext_ack *extack)
  1482. {
  1483. struct rtmsg *rtm;
  1484. struct nlattr *tb[RTA_MAX+1];
  1485. int index;
  1486. int err;
  1487. err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1488. rtm_mpls_policy, extack);
  1489. if (err < 0)
  1490. goto errout;
  1491. err = -EINVAL;
  1492. rtm = nlmsg_data(nlh);
  1493. if (rtm->rtm_family != AF_MPLS) {
  1494. NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
  1495. goto errout;
  1496. }
  1497. if (rtm->rtm_dst_len != 20) {
  1498. NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
  1499. goto errout;
  1500. }
  1501. if (rtm->rtm_src_len != 0) {
  1502. NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
  1503. goto errout;
  1504. }
  1505. if (rtm->rtm_tos != 0) {
  1506. NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
  1507. goto errout;
  1508. }
  1509. if (rtm->rtm_table != RT_TABLE_MAIN) {
  1510. NL_SET_ERR_MSG(extack,
  1511. "MPLS only supports the main route table");
  1512. goto errout;
  1513. }
  1514. /* Any value is acceptable for rtm_protocol */
  1515. /* As mpls uses destination specific addresses
  1516. * (or source specific address in the case of multicast)
  1517. * all addresses have universal scope.
  1518. */
  1519. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
  1520. NL_SET_ERR_MSG(extack,
  1521. "Invalid route scope - MPLS only supports UNIVERSE");
  1522. goto errout;
  1523. }
  1524. if (rtm->rtm_type != RTN_UNICAST) {
  1525. NL_SET_ERR_MSG(extack,
  1526. "Invalid route type - MPLS only supports UNICAST");
  1527. goto errout;
  1528. }
  1529. if (rtm->rtm_flags != 0) {
  1530. NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
  1531. goto errout;
  1532. }
  1533. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1534. cfg->rc_protocol = rtm->rtm_protocol;
  1535. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1536. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1537. cfg->rc_nlflags = nlh->nlmsg_flags;
  1538. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1539. cfg->rc_nlinfo.nlh = nlh;
  1540. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1541. for (index = 0; index <= RTA_MAX; index++) {
  1542. struct nlattr *nla = tb[index];
  1543. if (!nla)
  1544. continue;
  1545. switch (index) {
  1546. case RTA_OIF:
  1547. cfg->rc_ifindex = nla_get_u32(nla);
  1548. break;
  1549. case RTA_NEWDST:
  1550. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1551. &cfg->rc_output_labels,
  1552. cfg->rc_output_label, extack))
  1553. goto errout;
  1554. break;
  1555. case RTA_DST:
  1556. {
  1557. u8 label_count;
  1558. if (nla_get_labels(nla, 1, &label_count,
  1559. &cfg->rc_label, extack))
  1560. goto errout;
  1561. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1562. &cfg->rc_label, extack))
  1563. goto errout;
  1564. break;
  1565. }
  1566. case RTA_GATEWAY:
  1567. NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
  1568. goto errout;
  1569. case RTA_VIA:
  1570. {
  1571. if (nla_get_via(nla, &cfg->rc_via_alen,
  1572. &cfg->rc_via_table, cfg->rc_via,
  1573. extack))
  1574. goto errout;
  1575. break;
  1576. }
  1577. case RTA_MULTIPATH:
  1578. {
  1579. cfg->rc_mp = nla_data(nla);
  1580. cfg->rc_mp_len = nla_len(nla);
  1581. break;
  1582. }
  1583. case RTA_TTL_PROPAGATE:
  1584. {
  1585. u8 ttl_propagate = nla_get_u8(nla);
  1586. if (ttl_propagate > 1) {
  1587. NL_SET_ERR_MSG_ATTR(extack, nla,
  1588. "RTA_TTL_PROPAGATE can only be 0 or 1");
  1589. goto errout;
  1590. }
  1591. cfg->rc_ttl_propagate = ttl_propagate ?
  1592. MPLS_TTL_PROP_ENABLED :
  1593. MPLS_TTL_PROP_DISABLED;
  1594. break;
  1595. }
  1596. default:
  1597. NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
  1598. /* Unsupported attribute */
  1599. goto errout;
  1600. }
  1601. }
  1602. err = 0;
  1603. errout:
  1604. return err;
  1605. }
  1606. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1607. struct netlink_ext_ack *extack)
  1608. {
  1609. struct mpls_route_config *cfg;
  1610. int err;
  1611. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1612. if (!cfg)
  1613. return -ENOMEM;
  1614. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1615. if (err < 0)
  1616. goto out;
  1617. err = mpls_route_del(cfg, extack);
  1618. out:
  1619. kfree(cfg);
  1620. return err;
  1621. }
  1622. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1623. struct netlink_ext_ack *extack)
  1624. {
  1625. struct mpls_route_config *cfg;
  1626. int err;
  1627. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1628. if (!cfg)
  1629. return -ENOMEM;
  1630. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1631. if (err < 0)
  1632. goto out;
  1633. err = mpls_route_add(cfg, extack);
  1634. out:
  1635. kfree(cfg);
  1636. return err;
  1637. }
  1638. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1639. u32 label, struct mpls_route *rt, int flags)
  1640. {
  1641. struct net_device *dev;
  1642. struct nlmsghdr *nlh;
  1643. struct rtmsg *rtm;
  1644. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1645. if (nlh == NULL)
  1646. return -EMSGSIZE;
  1647. rtm = nlmsg_data(nlh);
  1648. rtm->rtm_family = AF_MPLS;
  1649. rtm->rtm_dst_len = 20;
  1650. rtm->rtm_src_len = 0;
  1651. rtm->rtm_tos = 0;
  1652. rtm->rtm_table = RT_TABLE_MAIN;
  1653. rtm->rtm_protocol = rt->rt_protocol;
  1654. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1655. rtm->rtm_type = RTN_UNICAST;
  1656. rtm->rtm_flags = 0;
  1657. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1658. goto nla_put_failure;
  1659. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1660. bool ttl_propagate =
  1661. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1662. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1663. ttl_propagate))
  1664. goto nla_put_failure;
  1665. }
  1666. if (rt->rt_nhn == 1) {
  1667. const struct mpls_nh *nh = rt->rt_nh;
  1668. if (nh->nh_labels &&
  1669. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1670. nh->nh_label))
  1671. goto nla_put_failure;
  1672. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1673. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1674. nh->nh_via_alen))
  1675. goto nla_put_failure;
  1676. dev = nh->nh_dev;
  1677. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1678. goto nla_put_failure;
  1679. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1680. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1681. if (nh->nh_flags & RTNH_F_DEAD)
  1682. rtm->rtm_flags |= RTNH_F_DEAD;
  1683. } else {
  1684. struct rtnexthop *rtnh;
  1685. struct nlattr *mp;
  1686. u8 linkdown = 0;
  1687. u8 dead = 0;
  1688. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  1689. if (!mp)
  1690. goto nla_put_failure;
  1691. for_nexthops(rt) {
  1692. dev = nh->nh_dev;
  1693. if (!dev)
  1694. continue;
  1695. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1696. if (!rtnh)
  1697. goto nla_put_failure;
  1698. rtnh->rtnh_ifindex = dev->ifindex;
  1699. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1700. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1701. linkdown++;
  1702. }
  1703. if (nh->nh_flags & RTNH_F_DEAD) {
  1704. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1705. dead++;
  1706. }
  1707. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1708. nh->nh_labels,
  1709. nh->nh_label))
  1710. goto nla_put_failure;
  1711. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1712. nla_put_via(skb, nh->nh_via_table,
  1713. mpls_nh_via(rt, nh),
  1714. nh->nh_via_alen))
  1715. goto nla_put_failure;
  1716. /* length of rtnetlink header + attributes */
  1717. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1718. } endfor_nexthops(rt);
  1719. if (linkdown == rt->rt_nhn)
  1720. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1721. if (dead == rt->rt_nhn)
  1722. rtm->rtm_flags |= RTNH_F_DEAD;
  1723. nla_nest_end(skb, mp);
  1724. }
  1725. nlmsg_end(skb, nlh);
  1726. return 0;
  1727. nla_put_failure:
  1728. nlmsg_cancel(skb, nlh);
  1729. return -EMSGSIZE;
  1730. }
  1731. #if IS_ENABLED(CONFIG_INET)
  1732. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1733. struct fib_dump_filter *filter,
  1734. struct netlink_callback *cb)
  1735. {
  1736. return ip_valid_fib_dump_req(net, nlh, filter, cb);
  1737. }
  1738. #else
  1739. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1740. struct fib_dump_filter *filter,
  1741. struct netlink_callback *cb)
  1742. {
  1743. struct netlink_ext_ack *extack = cb->extack;
  1744. struct nlattr *tb[RTA_MAX + 1];
  1745. struct rtmsg *rtm;
  1746. int err, i;
  1747. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  1748. NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
  1749. return -EINVAL;
  1750. }
  1751. rtm = nlmsg_data(nlh);
  1752. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  1753. rtm->rtm_table || rtm->rtm_scope || rtm->rtm_type ||
  1754. rtm->rtm_flags) {
  1755. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
  1756. return -EINVAL;
  1757. }
  1758. if (rtm->rtm_protocol) {
  1759. filter->protocol = rtm->rtm_protocol;
  1760. filter->filter_set = 1;
  1761. cb->answer_flags = NLM_F_DUMP_FILTERED;
  1762. }
  1763. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1764. rtm_mpls_policy, extack);
  1765. if (err < 0)
  1766. return err;
  1767. for (i = 0; i <= RTA_MAX; ++i) {
  1768. int ifindex;
  1769. if (i == RTA_OIF) {
  1770. ifindex = nla_get_u32(tb[i]);
  1771. filter->dev = __dev_get_by_index(net, ifindex);
  1772. if (!filter->dev)
  1773. return -ENODEV;
  1774. filter->filter_set = 1;
  1775. } else if (tb[i]) {
  1776. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  1777. return -EINVAL;
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. #endif
  1783. static bool mpls_rt_uses_dev(struct mpls_route *rt,
  1784. const struct net_device *dev)
  1785. {
  1786. if (rt->rt_nhn == 1) {
  1787. struct mpls_nh *nh = rt->rt_nh;
  1788. if (nh->nh_dev == dev)
  1789. return true;
  1790. } else {
  1791. for_nexthops(rt) {
  1792. if (nh->nh_dev == dev)
  1793. return true;
  1794. } endfor_nexthops(rt);
  1795. }
  1796. return false;
  1797. }
  1798. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1799. {
  1800. const struct nlmsghdr *nlh = cb->nlh;
  1801. struct net *net = sock_net(skb->sk);
  1802. struct mpls_route __rcu **platform_label;
  1803. struct fib_dump_filter filter = {
  1804. .rtnl_held = true,
  1805. };
  1806. unsigned int flags = NLM_F_MULTI;
  1807. size_t platform_labels;
  1808. unsigned int index;
  1809. ASSERT_RTNL();
  1810. if (cb->strict_check) {
  1811. int err;
  1812. err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
  1813. if (err < 0)
  1814. return err;
  1815. /* for MPLS, there is only 1 table with fixed type and flags.
  1816. * If either are set in the filter then return nothing.
  1817. */
  1818. if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
  1819. (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
  1820. filter.flags)
  1821. return skb->len;
  1822. }
  1823. index = cb->args[0];
  1824. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1825. index = MPLS_LABEL_FIRST_UNRESERVED;
  1826. platform_label = rtnl_dereference(net->mpls.platform_label);
  1827. platform_labels = net->mpls.platform_labels;
  1828. if (filter.filter_set)
  1829. flags |= NLM_F_DUMP_FILTERED;
  1830. for (; index < platform_labels; index++) {
  1831. struct mpls_route *rt;
  1832. rt = rtnl_dereference(platform_label[index]);
  1833. if (!rt)
  1834. continue;
  1835. if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
  1836. (filter.protocol && rt->rt_protocol != filter.protocol))
  1837. continue;
  1838. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1839. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1840. index, rt, flags) < 0)
  1841. break;
  1842. }
  1843. cb->args[0] = index;
  1844. return skb->len;
  1845. }
  1846. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1847. {
  1848. size_t payload =
  1849. NLMSG_ALIGN(sizeof(struct rtmsg))
  1850. + nla_total_size(4) /* RTA_DST */
  1851. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1852. if (rt->rt_nhn == 1) {
  1853. struct mpls_nh *nh = rt->rt_nh;
  1854. if (nh->nh_dev)
  1855. payload += nla_total_size(4); /* RTA_OIF */
  1856. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1857. payload += nla_total_size(2 + nh->nh_via_alen);
  1858. if (nh->nh_labels) /* RTA_NEWDST */
  1859. payload += nla_total_size(nh->nh_labels * 4);
  1860. } else {
  1861. /* each nexthop is packed in an attribute */
  1862. size_t nhsize = 0;
  1863. for_nexthops(rt) {
  1864. if (!nh->nh_dev)
  1865. continue;
  1866. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1867. /* RTA_VIA */
  1868. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1869. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1870. if (nh->nh_labels)
  1871. nhsize += nla_total_size(nh->nh_labels * 4);
  1872. } endfor_nexthops(rt);
  1873. /* nested attribute */
  1874. payload += nla_total_size(nhsize);
  1875. }
  1876. return payload;
  1877. }
  1878. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1879. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1880. unsigned int nlm_flags)
  1881. {
  1882. struct sk_buff *skb;
  1883. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1884. int err = -ENOBUFS;
  1885. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1886. if (skb == NULL)
  1887. goto errout;
  1888. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1889. if (err < 0) {
  1890. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1891. WARN_ON(err == -EMSGSIZE);
  1892. kfree_skb(skb);
  1893. goto errout;
  1894. }
  1895. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1896. return;
  1897. errout:
  1898. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1899. }
  1900. static int mpls_valid_getroute_req(struct sk_buff *skb,
  1901. const struct nlmsghdr *nlh,
  1902. struct nlattr **tb,
  1903. struct netlink_ext_ack *extack)
  1904. {
  1905. struct rtmsg *rtm;
  1906. int i, err;
  1907. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  1908. NL_SET_ERR_MSG_MOD(extack,
  1909. "Invalid header for get route request");
  1910. return -EINVAL;
  1911. }
  1912. if (!netlink_strict_get_check(skb))
  1913. return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1914. rtm_mpls_policy, extack);
  1915. rtm = nlmsg_data(nlh);
  1916. if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
  1917. rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
  1918. rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
  1919. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
  1920. return -EINVAL;
  1921. }
  1922. if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
  1923. NL_SET_ERR_MSG_MOD(extack,
  1924. "Invalid flags for get route request");
  1925. return -EINVAL;
  1926. }
  1927. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1928. rtm_mpls_policy, extack);
  1929. if (err)
  1930. return err;
  1931. if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
  1932. NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
  1933. return -EINVAL;
  1934. }
  1935. for (i = 0; i <= RTA_MAX; i++) {
  1936. if (!tb[i])
  1937. continue;
  1938. switch (i) {
  1939. case RTA_DST:
  1940. case RTA_NEWDST:
  1941. break;
  1942. default:
  1943. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
  1944. return -EINVAL;
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  1950. struct netlink_ext_ack *extack)
  1951. {
  1952. struct net *net = sock_net(in_skb->sk);
  1953. u32 portid = NETLINK_CB(in_skb).portid;
  1954. u32 in_label = LABEL_NOT_SPECIFIED;
  1955. struct nlattr *tb[RTA_MAX + 1];
  1956. u32 labels[MAX_NEW_LABELS];
  1957. struct mpls_shim_hdr *hdr;
  1958. unsigned int hdr_size = 0;
  1959. const struct mpls_nh *nh;
  1960. struct net_device *dev;
  1961. struct mpls_route *rt;
  1962. struct rtmsg *rtm, *r;
  1963. struct nlmsghdr *nlh;
  1964. struct sk_buff *skb;
  1965. u8 n_labels;
  1966. int err;
  1967. err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
  1968. if (err < 0)
  1969. goto errout;
  1970. rtm = nlmsg_data(in_nlh);
  1971. if (tb[RTA_DST]) {
  1972. u8 label_count;
  1973. if (nla_get_labels(tb[RTA_DST], 1, &label_count,
  1974. &in_label, extack)) {
  1975. err = -EINVAL;
  1976. goto errout;
  1977. }
  1978. if (!mpls_label_ok(net, &in_label, extack)) {
  1979. err = -EINVAL;
  1980. goto errout;
  1981. }
  1982. }
  1983. rt = mpls_route_input_rcu(net, in_label);
  1984. if (!rt) {
  1985. err = -ENETUNREACH;
  1986. goto errout;
  1987. }
  1988. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  1989. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1990. if (!skb) {
  1991. err = -ENOBUFS;
  1992. goto errout;
  1993. }
  1994. err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
  1995. RTM_NEWROUTE, in_label, rt, 0);
  1996. if (err < 0) {
  1997. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1998. WARN_ON(err == -EMSGSIZE);
  1999. goto errout_free;
  2000. }
  2001. return rtnl_unicast(skb, net, portid);
  2002. }
  2003. if (tb[RTA_NEWDST]) {
  2004. if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
  2005. labels, extack) != 0) {
  2006. err = -EINVAL;
  2007. goto errout;
  2008. }
  2009. hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
  2010. }
  2011. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2012. if (!skb) {
  2013. err = -ENOBUFS;
  2014. goto errout;
  2015. }
  2016. skb->protocol = htons(ETH_P_MPLS_UC);
  2017. if (hdr_size) {
  2018. bool bos;
  2019. int i;
  2020. if (skb_cow(skb, hdr_size)) {
  2021. err = -ENOBUFS;
  2022. goto errout_free;
  2023. }
  2024. skb_reserve(skb, hdr_size);
  2025. skb_push(skb, hdr_size);
  2026. skb_reset_network_header(skb);
  2027. /* Push new labels */
  2028. hdr = mpls_hdr(skb);
  2029. bos = true;
  2030. for (i = n_labels - 1; i >= 0; i--) {
  2031. hdr[i] = mpls_entry_encode(labels[i],
  2032. 1, 0, bos);
  2033. bos = false;
  2034. }
  2035. }
  2036. nh = mpls_select_multipath(rt, skb);
  2037. if (!nh) {
  2038. err = -ENETUNREACH;
  2039. goto errout_free;
  2040. }
  2041. if (hdr_size) {
  2042. skb_pull(skb, hdr_size);
  2043. skb_reset_network_header(skb);
  2044. }
  2045. nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
  2046. RTM_NEWROUTE, sizeof(*r), 0);
  2047. if (!nlh) {
  2048. err = -EMSGSIZE;
  2049. goto errout_free;
  2050. }
  2051. r = nlmsg_data(nlh);
  2052. r->rtm_family = AF_MPLS;
  2053. r->rtm_dst_len = 20;
  2054. r->rtm_src_len = 0;
  2055. r->rtm_table = RT_TABLE_MAIN;
  2056. r->rtm_type = RTN_UNICAST;
  2057. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2058. r->rtm_protocol = rt->rt_protocol;
  2059. r->rtm_flags = 0;
  2060. if (nla_put_labels(skb, RTA_DST, 1, &in_label))
  2061. goto nla_put_failure;
  2062. if (nh->nh_labels &&
  2063. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  2064. nh->nh_label))
  2065. goto nla_put_failure;
  2066. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  2067. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  2068. nh->nh_via_alen))
  2069. goto nla_put_failure;
  2070. dev = nh->nh_dev;
  2071. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  2072. goto nla_put_failure;
  2073. nlmsg_end(skb, nlh);
  2074. err = rtnl_unicast(skb, net, portid);
  2075. errout:
  2076. return err;
  2077. nla_put_failure:
  2078. nlmsg_cancel(skb, nlh);
  2079. err = -EMSGSIZE;
  2080. errout_free:
  2081. kfree_skb(skb);
  2082. return err;
  2083. }
  2084. static int resize_platform_label_table(struct net *net, size_t limit)
  2085. {
  2086. size_t size = sizeof(struct mpls_route *) * limit;
  2087. size_t old_limit;
  2088. size_t cp_size;
  2089. struct mpls_route __rcu **labels = NULL, **old;
  2090. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  2091. unsigned index;
  2092. if (size) {
  2093. labels = kvzalloc(size, GFP_KERNEL);
  2094. if (!labels)
  2095. goto nolabels;
  2096. }
  2097. /* In case the predefined labels need to be populated */
  2098. if (limit > MPLS_LABEL_IPV4NULL) {
  2099. struct net_device *lo = net->loopback_dev;
  2100. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  2101. if (IS_ERR(rt0))
  2102. goto nort0;
  2103. rt0->rt_nh->nh_dev = lo;
  2104. rt0->rt_protocol = RTPROT_KERNEL;
  2105. rt0->rt_payload_type = MPT_IPV4;
  2106. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2107. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2108. rt0->rt_nh->nh_via_alen = lo->addr_len;
  2109. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  2110. lo->addr_len);
  2111. }
  2112. if (limit > MPLS_LABEL_IPV6NULL) {
  2113. struct net_device *lo = net->loopback_dev;
  2114. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  2115. if (IS_ERR(rt2))
  2116. goto nort2;
  2117. rt2->rt_nh->nh_dev = lo;
  2118. rt2->rt_protocol = RTPROT_KERNEL;
  2119. rt2->rt_payload_type = MPT_IPV6;
  2120. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2121. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2122. rt2->rt_nh->nh_via_alen = lo->addr_len;
  2123. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  2124. lo->addr_len);
  2125. }
  2126. rtnl_lock();
  2127. /* Remember the original table */
  2128. old = rtnl_dereference(net->mpls.platform_label);
  2129. old_limit = net->mpls.platform_labels;
  2130. /* Free any labels beyond the new table */
  2131. for (index = limit; index < old_limit; index++)
  2132. mpls_route_update(net, index, NULL, NULL);
  2133. /* Copy over the old labels */
  2134. cp_size = size;
  2135. if (old_limit < limit)
  2136. cp_size = old_limit * sizeof(struct mpls_route *);
  2137. memcpy(labels, old, cp_size);
  2138. /* If needed set the predefined labels */
  2139. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  2140. (limit > MPLS_LABEL_IPV6NULL)) {
  2141. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  2142. rt2 = NULL;
  2143. }
  2144. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  2145. (limit > MPLS_LABEL_IPV4NULL)) {
  2146. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  2147. rt0 = NULL;
  2148. }
  2149. /* Update the global pointers */
  2150. net->mpls.platform_labels = limit;
  2151. rcu_assign_pointer(net->mpls.platform_label, labels);
  2152. rtnl_unlock();
  2153. mpls_rt_free(rt2);
  2154. mpls_rt_free(rt0);
  2155. if (old) {
  2156. synchronize_rcu();
  2157. kvfree(old);
  2158. }
  2159. return 0;
  2160. nort2:
  2161. mpls_rt_free(rt0);
  2162. nort0:
  2163. kvfree(labels);
  2164. nolabels:
  2165. return -ENOMEM;
  2166. }
  2167. static int mpls_platform_labels(const struct ctl_table *table, int write,
  2168. void *buffer, size_t *lenp, loff_t *ppos)
  2169. {
  2170. struct net *net = table->data;
  2171. int platform_labels = net->mpls.platform_labels;
  2172. int ret;
  2173. struct ctl_table tmp = {
  2174. .procname = table->procname,
  2175. .data = &platform_labels,
  2176. .maxlen = sizeof(int),
  2177. .mode = table->mode,
  2178. .extra1 = SYSCTL_ZERO,
  2179. .extra2 = &label_limit,
  2180. };
  2181. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2182. if (write && ret == 0)
  2183. ret = resize_platform_label_table(net, platform_labels);
  2184. return ret;
  2185. }
  2186. #define MPLS_NS_SYSCTL_OFFSET(field) \
  2187. (&((struct net *)0)->field)
  2188. static const struct ctl_table mpls_table[] = {
  2189. {
  2190. .procname = "platform_labels",
  2191. .data = NULL,
  2192. .maxlen = sizeof(int),
  2193. .mode = 0644,
  2194. .proc_handler = mpls_platform_labels,
  2195. },
  2196. {
  2197. .procname = "ip_ttl_propagate",
  2198. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  2199. .maxlen = sizeof(int),
  2200. .mode = 0644,
  2201. .proc_handler = proc_dointvec_minmax,
  2202. .extra1 = SYSCTL_ZERO,
  2203. .extra2 = SYSCTL_ONE,
  2204. },
  2205. {
  2206. .procname = "default_ttl",
  2207. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  2208. .maxlen = sizeof(int),
  2209. .mode = 0644,
  2210. .proc_handler = proc_dointvec_minmax,
  2211. .extra1 = SYSCTL_ONE,
  2212. .extra2 = &ttl_max,
  2213. },
  2214. };
  2215. static int mpls_net_init(struct net *net)
  2216. {
  2217. size_t table_size = ARRAY_SIZE(mpls_table);
  2218. struct ctl_table *table;
  2219. int i;
  2220. net->mpls.platform_labels = 0;
  2221. net->mpls.platform_label = NULL;
  2222. net->mpls.ip_ttl_propagate = 1;
  2223. net->mpls.default_ttl = 255;
  2224. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  2225. if (table == NULL)
  2226. return -ENOMEM;
  2227. /* Table data contains only offsets relative to the base of
  2228. * the mdev at this point, so make them absolute.
  2229. */
  2230. for (i = 0; i < table_size; i++)
  2231. table[i].data = (char *)net + (uintptr_t)table[i].data;
  2232. net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
  2233. table_size);
  2234. if (net->mpls.ctl == NULL) {
  2235. kfree(table);
  2236. return -ENOMEM;
  2237. }
  2238. return 0;
  2239. }
  2240. static void mpls_net_exit(struct net *net)
  2241. {
  2242. struct mpls_route __rcu **platform_label;
  2243. size_t platform_labels;
  2244. const struct ctl_table *table;
  2245. unsigned int index;
  2246. table = net->mpls.ctl->ctl_table_arg;
  2247. unregister_net_sysctl_table(net->mpls.ctl);
  2248. kfree(table);
  2249. /* An rcu grace period has passed since there was a device in
  2250. * the network namespace (and thus the last in flight packet)
  2251. * left this network namespace. This is because
  2252. * unregister_netdevice_many and netdev_run_todo has completed
  2253. * for each network device that was in this network namespace.
  2254. *
  2255. * As such no additional rcu synchronization is necessary when
  2256. * freeing the platform_label table.
  2257. */
  2258. rtnl_lock();
  2259. platform_label = rtnl_dereference(net->mpls.platform_label);
  2260. platform_labels = net->mpls.platform_labels;
  2261. for (index = 0; index < platform_labels; index++) {
  2262. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  2263. RCU_INIT_POINTER(platform_label[index], NULL);
  2264. mpls_notify_route(net, index, rt, NULL, NULL);
  2265. mpls_rt_free(rt);
  2266. }
  2267. rtnl_unlock();
  2268. kvfree(platform_label);
  2269. }
  2270. static struct pernet_operations mpls_net_ops = {
  2271. .init = mpls_net_init,
  2272. .exit = mpls_net_exit,
  2273. };
  2274. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  2275. .family = AF_MPLS,
  2276. .fill_stats_af = mpls_fill_stats_af,
  2277. .get_stats_af_size = mpls_get_stats_af_size,
  2278. };
  2279. static const struct rtnl_msg_handler mpls_rtnl_msg_handlers[] __initdata_or_module = {
  2280. {THIS_MODULE, PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, 0},
  2281. {THIS_MODULE, PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, 0},
  2282. {THIS_MODULE, PF_MPLS, RTM_GETROUTE, mpls_getroute, mpls_dump_routes, 0},
  2283. {THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
  2284. mpls_netconf_get_devconf, mpls_netconf_dump_devconf,
  2285. RTNL_FLAG_DUMP_UNLOCKED},
  2286. };
  2287. static int __init mpls_init(void)
  2288. {
  2289. int err;
  2290. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  2291. err = register_pernet_subsys(&mpls_net_ops);
  2292. if (err)
  2293. goto out;
  2294. err = register_netdevice_notifier(&mpls_dev_notifier);
  2295. if (err)
  2296. goto out_unregister_pernet;
  2297. dev_add_pack(&mpls_packet_type);
  2298. rtnl_af_register(&mpls_af_ops);
  2299. err = rtnl_register_many(mpls_rtnl_msg_handlers);
  2300. if (err)
  2301. goto out_unregister_rtnl_af;
  2302. err = ipgre_tunnel_encap_add_mpls_ops();
  2303. if (err) {
  2304. pr_err("Can't add mpls over gre tunnel ops\n");
  2305. goto out_unregister_rtnl;
  2306. }
  2307. err = 0;
  2308. out:
  2309. return err;
  2310. out_unregister_rtnl:
  2311. rtnl_unregister_many(mpls_rtnl_msg_handlers);
  2312. out_unregister_rtnl_af:
  2313. rtnl_af_unregister(&mpls_af_ops);
  2314. dev_remove_pack(&mpls_packet_type);
  2315. out_unregister_pernet:
  2316. unregister_pernet_subsys(&mpls_net_ops);
  2317. goto out;
  2318. }
  2319. module_init(mpls_init);
  2320. static void __exit mpls_exit(void)
  2321. {
  2322. rtnl_unregister_all(PF_MPLS);
  2323. rtnl_af_unregister(&mpls_af_ops);
  2324. dev_remove_pack(&mpls_packet_type);
  2325. unregister_netdevice_notifier(&mpls_dev_notifier);
  2326. unregister_pernet_subsys(&mpls_net_ops);
  2327. ipgre_tunnel_encap_del_mpls_ops();
  2328. }
  2329. module_exit(mpls_exit);
  2330. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  2331. MODULE_LICENSE("GPL v2");
  2332. MODULE_ALIAS_NETPROTO(PF_MPLS);