af_mpls.c 64 KB

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